ctdb-daemon: Drop implementation of old-style database pull/push controls
[Samba.git] / ctdb / server / ctdb_control.c
blob206ea14969324d2cf2db55e52defdadb6d751f24
1 /*
2 ctdb_control protocol code
4 Copyright (C) Andrew Tridgell 2007
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/>.
19 #include "replace.h"
20 #include "system/network.h"
21 #include "system/filesys.h"
22 #include "system/wait.h"
24 #include <talloc.h>
25 #include <tevent.h>
27 #include "lib/tdb_wrap/tdb_wrap.h"
28 #include "lib/util/dlinklist.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/samba_util.h"
31 #include "lib/util/talloc_report.h"
33 #include "ctdb_private.h"
34 #include "ctdb_client.h"
36 #include "protocol/protocol_private.h"
38 #include "common/reqid.h"
39 #include "common/common.h"
40 #include "common/logging.h"
43 struct ctdb_control_state {
44 struct ctdb_context *ctdb;
45 uint32_t reqid;
46 ctdb_control_callback_fn_t callback;
47 void *private_data;
48 unsigned flags;
53 dump talloc memory hierarchy, returning it as a blob to the client
55 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata)
57 char *report;
58 size_t reportlen;
60 report = talloc_report_str(outdata, NULL);
61 if (report == NULL) {
62 DEBUG(DEBUG_ERR,
63 (__location__ " talloc_report_str failed\n"));
64 return -1;
66 reportlen = talloc_get_size(report);
68 if (reportlen > 0) {
69 reportlen -= 1; /* strip trailing zero */
72 outdata->dptr = (uint8_t *)report;
73 outdata->dsize = reportlen;
74 return 0;
77 static int32_t control_not_implemented(const char *unsupported,
78 const char *alternate)
80 if (alternate == NULL) {
81 DEBUG(DEBUG_ERR,
82 ("Control %s is not implemented any more\n",
83 unsupported));
84 } else {
85 DEBUG(DEBUG_ERR,
86 ("Control %s is not implemented any more, use %s instead\n",
87 unsupported, alternate));
89 return -1;
92 struct ctdb_echo_data_state {
93 struct ctdb_context *ctdb;
94 struct ctdb_req_control_old *c;
95 struct ctdb_echo_data *data;
98 static void ctdb_echo_data_timeout(
99 struct tevent_context *ev,
100 struct tevent_timer *te,
101 struct timeval now,
102 void *private_data);
104 static int32_t ctdb_control_echo_data(
105 struct ctdb_context *ctdb,
106 struct ctdb_req_control_old *c,
107 TDB_DATA indata,
108 bool *async_reply)
110 struct ctdb_echo_data_state *state = NULL;
111 struct tevent_timer *te = NULL;
112 uint32_t delay = 0;
113 size_t np = 0;
114 int ret;
116 state = talloc_zero(ctdb, struct ctdb_echo_data_state);
117 CTDB_NO_MEMORY(ctdb, state);
118 state->ctdb = ctdb;
120 ret = ctdb_echo_data_pull(
121 indata.dptr, indata.dsize, state, &state->data, &np);
122 if (ret != 0) {
123 DBG_DEBUG("ctdb_echo_data_pull failed: %s\n",
124 strerror(ret));
125 TALLOC_FREE(state);
126 return -1;
129 te = tevent_add_timer(
130 ctdb->ev,
131 state,
132 timeval_current_ofs_msec(delay),
133 ctdb_echo_data_timeout,
134 state);
135 if (te == NULL) {
136 DBG_DEBUG("tevent_add_timer failed\n");
137 TALLOC_FREE(state);
138 return -1;
141 state->c = talloc_move(state, &c);
142 *async_reply = true;
144 return 0;
147 static void ctdb_echo_data_timeout(
148 struct tevent_context *ev,
149 struct tevent_timer *te,
150 struct timeval now,
151 void *private_data)
153 struct ctdb_echo_data_state *state = talloc_get_type_abort(
154 private_data, struct ctdb_echo_data_state);
155 size_t len = ctdb_echo_data_len(state->data);
156 uint8_t *buf = NULL;
157 size_t np;
158 TDB_DATA data;
160 DBG_DEBUG("reqid=%"PRIu32" len=%zu\n", state->c->hdr.reqid, len);
162 buf = talloc_array(state, uint8_t, len);
163 if (buf == NULL) {
164 DBG_WARNING("talloc_array(%zu) failed\n", len);
165 goto done;
167 ctdb_echo_data_push(state->data, buf, &np);
168 data = (TDB_DATA) { .dptr = buf, .dsize = np };
170 ctdb_request_control_reply(state->ctdb, state->c, &data, 0, NULL);
172 done:
173 TALLOC_FREE(state);
177 process a control request
179 static int32_t ctdb_control_dispatch(struct ctdb_context *ctdb,
180 struct ctdb_req_control_old *c,
181 TDB_DATA indata,
182 TDB_DATA *outdata, uint32_t srcnode,
183 const char **errormsg,
184 bool *async_reply)
186 uint32_t opcode = c->opcode;
187 uint64_t srvid = c->srvid;
188 uint32_t client_id = c->client_id;
189 static int level = DEBUG_ERR;
191 switch (opcode) {
192 case CTDB_CONTROL_PROCESS_EXISTS: {
193 CHECK_CONTROL_DATA_SIZE(sizeof(pid_t));
194 return ctdb_control_process_exists(ctdb, *(pid_t *)indata.dptr);
197 case CTDB_CONTROL_SET_DEBUG: {
198 union {
199 uint8_t *ptr;
200 int32_t *level;
201 } debug;
202 CHECK_CONTROL_DATA_SIZE(sizeof(int32_t));
203 debug.ptr = indata.dptr;
204 debuglevel_set(*debug.level);
205 return 0;
208 case CTDB_CONTROL_GET_DEBUG: {
209 CHECK_CONTROL_DATA_SIZE(0);
210 level = debuglevel_get();
211 outdata->dptr = (uint8_t *)&(level);
212 outdata->dsize = sizeof(DEBUGLEVEL);
213 return 0;
216 case CTDB_CONTROL_STATISTICS: {
217 CHECK_CONTROL_DATA_SIZE(0);
218 ctdb->statistics.memory_used = talloc_total_size(NULL);
219 ctdb->statistics.num_clients = ctdb->num_clients;
220 ctdb->statistics.frozen = (ctdb_db_all_frozen(ctdb) ? 1 : 0);
221 ctdb->statistics.recovering = (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE);
222 ctdb->statistics.statistics_current_time = timeval_current();
224 outdata->dptr = (uint8_t *)&ctdb->statistics;
225 outdata->dsize = sizeof(ctdb->statistics);
226 return 0;
229 case CTDB_CONTROL_GET_ALL_TUNABLES: {
230 CHECK_CONTROL_DATA_SIZE(0);
231 outdata->dptr = (uint8_t *)&ctdb->tunable;
232 outdata->dsize = sizeof(ctdb->tunable);
233 return 0;
236 case CTDB_CONTROL_DUMP_MEMORY: {
237 CHECK_CONTROL_DATA_SIZE(0);
238 return ctdb_dump_memory(ctdb, outdata);
241 case CTDB_CONTROL_STATISTICS_RESET: {
242 struct ctdb_db_context *ctdb_db;
244 CHECK_CONTROL_DATA_SIZE(0);
245 ZERO_STRUCT(ctdb->statistics);
246 for (ctdb_db = ctdb->db_list;
247 ctdb_db != NULL;
248 ctdb_db = ctdb_db->next) {
249 ctdb_db_statistics_reset(ctdb_db);
251 ctdb->statistics.statistics_start_time = timeval_current();
252 return 0;
255 case CTDB_CONTROL_GETVNNMAP:
256 return ctdb_control_getvnnmap(ctdb, opcode, indata, outdata);
258 case CTDB_CONTROL_GET_DBMAP:
259 return ctdb_control_getdbmap(ctdb, opcode, indata, outdata);
261 case CTDB_CONTROL_GET_NODEMAPv4:
262 return control_not_implemented("GET_NODEMAPv4", "GET_NODEMAP");
264 case CTDB_CONTROL_GET_NODEMAP:
265 return ctdb_control_getnodemap(ctdb, opcode, indata, outdata);
267 case CTDB_CONTROL_GET_NODES_FILE:
268 return ctdb_control_getnodesfile(ctdb, opcode, indata, outdata);
270 case CTDB_CONTROL_RELOAD_NODES_FILE:
271 CHECK_CONTROL_DATA_SIZE(0);
272 return ctdb_control_reload_nodes_file(ctdb, opcode);
274 case CTDB_CONTROL_SET_DB_STICKY: {
275 uint32_t db_id;
276 struct ctdb_db_context *ctdb_db;
278 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
279 db_id = *(uint32_t *)indata.dptr;
280 ctdb_db = find_ctdb_db(ctdb, db_id);
281 if (ctdb_db == NULL) return -1;
282 return ctdb_set_db_sticky(ctdb, ctdb_db);
285 case CTDB_CONTROL_SETVNNMAP:
286 return ctdb_control_setvnnmap(ctdb, opcode, indata, outdata);
288 case CTDB_CONTROL_PULL_DB:
289 return control_not_implemented("PULL_DB", NULL);
291 case CTDB_CONTROL_SET_DMASTER:
292 return control_not_implemented("SET_DMASTER", NULL);
294 case CTDB_CONTROL_PUSH_DB:
295 return control_not_implemented("PUSH_DB", NULL);
297 case CTDB_CONTROL_GET_RECMODE: {
298 return ctdb->recovery_mode;
301 case CTDB_CONTROL_SET_RECMASTER: {
302 return ctdb_control_set_recmaster(ctdb, opcode, indata);
305 case CTDB_CONTROL_GET_RECMASTER:
306 return ctdb->recovery_master;
308 case CTDB_CONTROL_GET_PID:
309 return getpid();
311 case CTDB_CONTROL_GET_PNN:
312 return ctdb->pnn;
314 case CTDB_CONTROL_PING:
315 CHECK_CONTROL_DATA_SIZE(0);
316 return ctdb->num_clients;
318 case CTDB_CONTROL_GET_RUNSTATE:
319 CHECK_CONTROL_DATA_SIZE(0);
320 outdata->dptr = (uint8_t *)&ctdb->runstate;
321 outdata->dsize = sizeof(uint32_t);
322 return 0;
325 case CTDB_CONTROL_SET_DB_READONLY: {
326 uint32_t db_id;
327 struct ctdb_db_context *ctdb_db;
329 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
330 db_id = *(uint32_t *)indata.dptr;
331 ctdb_db = find_ctdb_db(ctdb, db_id);
332 if (ctdb_db == NULL) return -1;
333 return ctdb_set_db_readonly(ctdb, ctdb_db);
335 case CTDB_CONTROL_GET_DBNAME: {
336 uint32_t db_id;
337 struct ctdb_db_context *ctdb_db;
339 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
340 db_id = *(uint32_t *)indata.dptr;
341 ctdb_db = find_ctdb_db(ctdb, db_id);
342 if (ctdb_db == NULL) return -1;
343 outdata->dptr = discard_const(ctdb_db->db_name);
344 outdata->dsize = strlen(ctdb_db->db_name)+1;
345 return 0;
348 case CTDB_CONTROL_GETDBPATH: {
349 uint32_t db_id;
350 struct ctdb_db_context *ctdb_db;
352 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
353 db_id = *(uint32_t *)indata.dptr;
354 ctdb_db = find_ctdb_db(ctdb, db_id);
355 if (ctdb_db == NULL) return -1;
356 outdata->dptr = discard_const(ctdb_db->db_path);
357 outdata->dsize = strlen(ctdb_db->db_path)+1;
358 return 0;
361 case CTDB_CONTROL_DB_ATTACH:
362 return ctdb_control_db_attach(ctdb,
363 indata,
364 outdata,
366 srcnode,
367 client_id,
369 async_reply);
371 case CTDB_CONTROL_DB_ATTACH_PERSISTENT:
372 return ctdb_control_db_attach(ctdb,
373 indata,
374 outdata,
375 CTDB_DB_FLAGS_PERSISTENT,
376 srcnode,
377 client_id,
379 async_reply);
381 case CTDB_CONTROL_DB_ATTACH_REPLICATED:
382 return ctdb_control_db_attach(ctdb,
383 indata,
384 outdata,
385 CTDB_DB_FLAGS_REPLICATED,
386 srcnode,
387 client_id,
389 async_reply);
391 case CTDB_CONTROL_SET_CALL:
392 return control_not_implemented("SET_CALL", NULL);
394 case CTDB_CONTROL_TRAVERSE_START:
395 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
396 return ctdb_control_traverse_start(ctdb, indata, outdata, srcnode, client_id);
398 case CTDB_CONTROL_TRAVERSE_START_EXT:
399 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start_ext));
400 return ctdb_control_traverse_start_ext(ctdb, indata, outdata, srcnode, client_id);
402 case CTDB_CONTROL_TRAVERSE_ALL:
403 return ctdb_control_traverse_all(ctdb, indata, outdata);
405 case CTDB_CONTROL_TRAVERSE_ALL_EXT:
406 return ctdb_control_traverse_all_ext(ctdb, indata, outdata);
408 case CTDB_CONTROL_TRAVERSE_DATA:
409 return ctdb_control_traverse_data(ctdb, indata, outdata);
411 case CTDB_CONTROL_TRAVERSE_KILL:
412 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
413 return ctdb_control_traverse_kill(ctdb, indata, outdata, srcnode);
415 case CTDB_CONTROL_REGISTER_SRVID:
416 return daemon_register_message_handler(ctdb, client_id, srvid);
418 case CTDB_CONTROL_DEREGISTER_SRVID:
419 return daemon_deregister_message_handler(ctdb, client_id, srvid);
421 case CTDB_CONTROL_CHECK_SRVIDS:
422 return control_not_implemented("CHECK_SRVIDS", NULL);
424 case CTDB_CONTROL_ENABLE_SEQNUM:
425 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
426 return ctdb_ltdb_enable_seqnum(ctdb, *(uint32_t *)indata.dptr);
428 case CTDB_CONTROL_UPDATE_SEQNUM:
429 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
430 return ctdb_ltdb_update_seqnum(ctdb, *(uint32_t *)indata.dptr, srcnode);
432 case CTDB_CONTROL_FREEZE:
433 CHECK_CONTROL_DATA_SIZE(0);
434 return ctdb_control_freeze(ctdb, c, async_reply);
436 case CTDB_CONTROL_THAW:
437 return control_not_implemented("THAW", NULL);
439 case CTDB_CONTROL_SET_RECMODE:
440 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
441 return ctdb_control_set_recmode(ctdb, c, indata, async_reply, errormsg);
443 case CTDB_CONTROL_GET_MONMODE:
444 return control_not_implemented("GET_MONMODE", NULL);
446 case CTDB_CONTROL_ENABLE_MONITOR:
447 return control_not_implemented("ENABLE_MONITOR", NULL);
449 case CTDB_CONTROL_RUN_EVENTSCRIPTS:
450 return control_not_implemented("RUN_EVENTSCRIPTS", NULL);
452 case CTDB_CONTROL_DISABLE_MONITOR:
453 return control_not_implemented("DISABLE_MONITOR", NULL);
455 case CTDB_CONTROL_SHUTDOWN:
456 DEBUG(DEBUG_NOTICE,("Received SHUTDOWN command.\n"));
457 ctdb_shutdown_sequence(ctdb, 0);
458 /* In case above returns due to duplicate shutdown */
459 return 0;
461 case CTDB_CONTROL_TAKEOVER_IPv4:
462 return control_not_implemented("TAKEOVER_IPv4", "TAKEOVER_IP");
464 case CTDB_CONTROL_TAKEOVER_IP:
465 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
466 return ctdb_control_takeover_ip(ctdb, c, indata, async_reply);
468 case CTDB_CONTROL_RELEASE_IPv4:
469 return control_not_implemented("RELEASE_IPv4", "RELEASE_IP");
471 case CTDB_CONTROL_RELEASE_IP:
472 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
473 return ctdb_control_release_ip(ctdb, c, indata, async_reply);
475 case CTDB_CONTROL_IPREALLOCATED:
476 CHECK_CONTROL_DATA_SIZE(0);
477 return ctdb_control_ipreallocated(ctdb, c, async_reply);
479 case CTDB_CONTROL_GET_PUBLIC_IPSv4:
480 return control_not_implemented("GET_PUBLIC_IPSv4",
481 "GET_PUBLIC_IPS");
483 case CTDB_CONTROL_GET_PUBLIC_IPS:
484 CHECK_CONTROL_DATA_SIZE(0);
485 return ctdb_control_get_public_ips(ctdb, c, outdata);
487 case CTDB_CONTROL_TCP_CLIENT:
488 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
489 return ctdb_control_tcp_client(ctdb, client_id, indata);
491 case CTDB_CONTROL_STARTUP:
492 CHECK_CONTROL_DATA_SIZE(0);
493 return ctdb_control_startup(ctdb, srcnode);
495 case CTDB_CONTROL_TCP_ADD:
496 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
497 return ctdb_control_tcp_add(ctdb, indata, false);
499 case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
500 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
501 return ctdb_control_tcp_add(ctdb, indata, true);
503 case CTDB_CONTROL_TCP_REMOVE:
504 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
505 return ctdb_control_tcp_remove(ctdb, indata);
507 case CTDB_CONTROL_SET_TUNABLE:
508 return ctdb_control_set_tunable(ctdb, indata);
510 case CTDB_CONTROL_GET_TUNABLE:
511 return ctdb_control_get_tunable(ctdb, indata, outdata);
513 case CTDB_CONTROL_LIST_TUNABLES:
514 return ctdb_control_list_tunables(ctdb, outdata);
516 case CTDB_CONTROL_MODIFY_FLAGS:
517 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_flag_change));
518 return ctdb_control_modflags(ctdb, indata);
520 case CTDB_CONTROL_KILL_TCP:
521 return control_not_implemented("KILL_TCP", NULL);
523 case CTDB_CONTROL_GET_TCP_TICKLE_LIST:
524 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
525 return ctdb_control_get_tcp_tickle_list(ctdb, indata, outdata);
527 case CTDB_CONTROL_SET_TCP_TICKLE_LIST:
528 /* data size is verified in the called function */
529 return ctdb_control_set_tcp_tickle_list(ctdb, indata);
531 case CTDB_CONTROL_REGISTER_SERVER_ID:
532 return control_not_implemented("REGISTER_SERVER_ID", NULL);
534 case CTDB_CONTROL_UNREGISTER_SERVER_ID:
535 return control_not_implemented("UNREGISTER_SERVER_ID", NULL);
537 case CTDB_CONTROL_CHECK_SERVER_ID:
538 return control_not_implemented("CHECK_SERVER_ID", NULL);
540 case CTDB_CONTROL_GET_SERVER_ID_LIST:
541 return control_not_implemented("SERVER_ID_LIST", NULL);
543 case CTDB_CONTROL_PERSISTENT_STORE:
544 return control_not_implemented("PERSISTENT_STORE", NULL);
546 case CTDB_CONTROL_UPDATE_RECORD:
547 return ctdb_control_update_record(ctdb, c, indata, async_reply);
549 case CTDB_CONTROL_SEND_GRATUITOUS_ARP:
550 return ctdb_control_send_gratious_arp(ctdb, indata);
552 case CTDB_CONTROL_TRANSACTION_START:
553 return control_not_implemented("TRANSACTION_START", NULL);
555 case CTDB_CONTROL_TRANSACTION_COMMIT:
556 return control_not_implemented("TRANSACTION_COMMIT", NULL);
558 case CTDB_CONTROL_WIPE_DATABASE:
559 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
560 return ctdb_control_wipe_database(ctdb, indata);
562 case CTDB_CONTROL_UPTIME:
563 return ctdb_control_uptime(ctdb, outdata);
565 case CTDB_CONTROL_START_RECOVERY:
566 return ctdb_control_start_recovery(ctdb, c, async_reply);
568 case CTDB_CONTROL_END_RECOVERY:
569 return ctdb_control_end_recovery(ctdb, c, async_reply);
571 case CTDB_CONTROL_TRY_DELETE_RECORDS:
572 return ctdb_control_try_delete_records(ctdb, indata, outdata);
574 case CTDB_CONTROL_ADD_PUBLIC_IP:
575 return ctdb_control_add_public_address(ctdb, indata);
577 case CTDB_CONTROL_DEL_PUBLIC_IP:
578 return ctdb_control_del_public_address(ctdb, indata);
580 case CTDB_CONTROL_GET_CAPABILITIES:
581 return ctdb_control_get_capabilities(ctdb, outdata);
583 case CTDB_CONTROL_START_PERSISTENT_UPDATE:
584 return ctdb_control_start_persistent_update(ctdb, c, indata);
586 case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE:
587 return ctdb_control_cancel_persistent_update(ctdb, c, indata);
589 case CTDB_CONTROL_TRANS2_COMMIT:
590 case CTDB_CONTROL_TRANS2_COMMIT_RETRY:
591 return control_not_implemented("TRANS2_COMMIT", "TRANS3_COMMIT");
593 case CTDB_CONTROL_TRANS2_ERROR:
594 return control_not_implemented("TRANS2_ERROR", NULL);
596 case CTDB_CONTROL_TRANS2_FINISHED:
597 return control_not_implemented("TRANS2_FINISHED", NULL);
599 case CTDB_CONTROL_TRANS2_ACTIVE:
600 return control_not_implemented("TRANS2_ACTIVE", NULL);
602 case CTDB_CONTROL_TRANS3_COMMIT:
603 return ctdb_control_trans3_commit(ctdb, c, indata, async_reply);
605 case CTDB_CONTROL_RECD_PING:
606 CHECK_CONTROL_DATA_SIZE(0);
607 return ctdb_control_recd_ping(ctdb);
609 case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
610 return control_not_implemented("GET_EVENT_SCRIPT_STATUS", NULL);
612 case CTDB_CONTROL_RECD_RECLOCK_LATENCY:
613 CHECK_CONTROL_DATA_SIZE(sizeof(double));
614 CTDB_UPDATE_RECLOCK_LATENCY(ctdb, "recd reclock", reclock.recd, *((double *)indata.dptr));
615 return 0;
616 case CTDB_CONTROL_GET_RECLOCK_FILE:
617 CHECK_CONTROL_DATA_SIZE(0);
618 if (ctdb->recovery_lock != NULL) {
619 outdata->dptr = discard_const(ctdb->recovery_lock);
620 outdata->dsize = strlen(ctdb->recovery_lock) + 1;
622 return 0;
623 case CTDB_CONTROL_SET_RECLOCK_FILE:
624 return control_not_implemented("SET_RECLOCK", NULL);
626 case CTDB_CONTROL_STOP_NODE:
627 CHECK_CONTROL_DATA_SIZE(0);
628 return ctdb_control_stop_node(ctdb);
630 case CTDB_CONTROL_CONTINUE_NODE:
631 CHECK_CONTROL_DATA_SIZE(0);
632 return ctdb_control_continue_node(ctdb);
634 case CTDB_CONTROL_SET_NATGWSTATE:
635 return control_not_implemented("SET_NATGWSTATE", NULL);
637 case CTDB_CONTROL_SET_LMASTERROLE: {
638 uint32_t lmasterrole;
640 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
641 lmasterrole = *(uint32_t *)indata.dptr;
642 if (lmasterrole == 0) {
643 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
644 } else {
645 ctdb->capabilities |= CTDB_CAP_LMASTER;
647 return 0;
650 case CTDB_CONTROL_SET_RECMASTERROLE: {
651 uint32_t recmasterrole;
653 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
654 recmasterrole = *(uint32_t *)indata.dptr;
655 if (recmasterrole == 0) {
656 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
657 } else {
658 ctdb->capabilities |= CTDB_CAP_RECMASTER;
660 return 0;
663 case CTDB_CONTROL_ENABLE_SCRIPT:
664 return control_not_implemented("ENABLE_SCRIPT", NULL);
666 case CTDB_CONTROL_DISABLE_SCRIPT:
667 return control_not_implemented("DISABLE_SCRIPT", NULL);
669 case CTDB_CONTROL_SET_BAN_STATE:
670 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_ban_state));
671 return ctdb_control_set_ban_state(ctdb, indata);
673 case CTDB_CONTROL_GET_BAN_STATE:
674 CHECK_CONTROL_DATA_SIZE(0);
675 return ctdb_control_get_ban_state(ctdb, outdata);
677 case CTDB_CONTROL_SET_DB_PRIORITY:
678 return control_not_implemented("SET_DB_PRIORITY", NULL);
680 case CTDB_CONTROL_GET_DB_PRIORITY:
681 return control_not_implemented("GET_DB_PRIORITY", NULL);
683 case CTDB_CONTROL_TRANSACTION_CANCEL:
684 return control_not_implemented("TRANSACTION_CANCEL", NULL);
686 case CTDB_CONTROL_REGISTER_NOTIFY:
687 return ctdb_control_register_notify(ctdb, client_id, indata);
689 case CTDB_CONTROL_DEREGISTER_NOTIFY:
690 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
691 return ctdb_control_deregister_notify(ctdb, client_id, indata);
693 case CTDB_CONTROL_GET_LOG:
694 return control_not_implemented("GET_LOG", NULL);
696 case CTDB_CONTROL_CLEAR_LOG:
697 return control_not_implemented("CLEAR_LOG", NULL);
699 case CTDB_CONTROL_GET_DB_SEQNUM:
700 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
701 return ctdb_control_get_db_seqnum(ctdb, indata, outdata);
703 case CTDB_CONTROL_DB_SET_HEALTHY:
704 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
705 return ctdb_control_db_set_healthy(ctdb, indata);
707 case CTDB_CONTROL_DB_GET_HEALTH:
708 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
709 return ctdb_control_db_get_health(ctdb, indata, outdata);
711 case CTDB_CONTROL_GET_PUBLIC_IP_INFO:
712 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
713 return ctdb_control_get_public_ip_info(ctdb, c, indata, outdata);
715 case CTDB_CONTROL_GET_IFACES:
716 CHECK_CONTROL_DATA_SIZE(0);
717 return ctdb_control_get_ifaces(ctdb, c, outdata);
719 case CTDB_CONTROL_SET_IFACE_LINK_STATE:
720 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_iface));
721 return ctdb_control_set_iface_link(ctdb, c, indata);
723 case CTDB_CONTROL_GET_STAT_HISTORY:
724 CHECK_CONTROL_DATA_SIZE(0);
725 return ctdb_control_get_stat_history(ctdb, c, outdata);
727 case CTDB_CONTROL_SCHEDULE_FOR_DELETION: {
728 struct ctdb_control_schedule_for_deletion *d;
729 size_t size = offsetof(struct ctdb_control_schedule_for_deletion, key);
730 CHECK_CONTROL_MIN_DATA_SIZE(size);
731 d = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
732 size += d->keylen;
733 CHECK_CONTROL_DATA_SIZE(size);
734 return ctdb_control_schedule_for_deletion(ctdb, indata);
736 case CTDB_CONTROL_GET_DB_STATISTICS:
737 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
738 return ctdb_control_get_db_statistics(ctdb, *(uint32_t *)indata.dptr, outdata);
740 case CTDB_CONTROL_RELOAD_PUBLIC_IPS:
741 CHECK_CONTROL_DATA_SIZE(0);
742 return ctdb_control_reload_public_ips(ctdb, c, async_reply);
744 case CTDB_CONTROL_RECEIVE_RECORDS:
745 return control_not_implemented("RECEIVE_RECORDS", NULL);
747 case CTDB_CONTROL_DB_DETACH:
748 return ctdb_control_db_detach(ctdb, indata, client_id);
750 case CTDB_CONTROL_DB_FREEZE:
751 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
752 return ctdb_control_db_freeze(ctdb, c, *(uint32_t *)indata.dptr,
753 async_reply);
755 case CTDB_CONTROL_DB_THAW:
756 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
757 return ctdb_control_db_thaw(ctdb, *(uint32_t *)indata.dptr);
759 case CTDB_CONTROL_DB_TRANSACTION_START:
760 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
761 return ctdb_control_db_transaction_start(ctdb, indata);
763 case CTDB_CONTROL_DB_TRANSACTION_COMMIT:
764 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
765 return ctdb_control_db_transaction_commit(ctdb, indata);
767 case CTDB_CONTROL_DB_TRANSACTION_CANCEL:
768 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
769 return ctdb_control_db_transaction_cancel(ctdb, indata);
771 case CTDB_CONTROL_DB_PULL:
772 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_pulldb_ext));
773 return ctdb_control_db_pull(ctdb, c, indata, outdata);
775 case CTDB_CONTROL_DB_PUSH_START:
776 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_pulldb_ext));
777 return ctdb_control_db_push_start(ctdb, indata);
779 case CTDB_CONTROL_DB_PUSH_CONFIRM:
780 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
781 return ctdb_control_db_push_confirm(ctdb, indata, outdata);
783 case CTDB_CONTROL_DB_OPEN_FLAGS: {
784 uint32_t db_id;
785 struct ctdb_db_context *ctdb_db;
786 int tdb_flags;
788 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
789 db_id = *(uint32_t *)indata.dptr;
790 ctdb_db = find_ctdb_db(ctdb, db_id);
791 if (ctdb_db == NULL) {
792 return -1;
795 tdb_flags = tdb_get_flags(ctdb_db->ltdb->tdb);
797 outdata->dptr = talloc_size(outdata, sizeof(tdb_flags));
798 if (outdata->dptr == NULL) {
799 return -1;
802 outdata->dsize = sizeof(tdb_flags);
803 memcpy(outdata->dptr, &tdb_flags, outdata->dsize);
804 return 0;
807 case CTDB_CONTROL_CHECK_PID_SRVID:
808 CHECK_CONTROL_DATA_SIZE((sizeof(pid_t) + sizeof(uint64_t)));
809 return ctdb_control_check_pid_srvid(ctdb, indata);
811 case CTDB_CONTROL_TUNNEL_REGISTER:
812 return ctdb_control_tunnel_register(ctdb, client_id, srvid);
814 case CTDB_CONTROL_TUNNEL_DEREGISTER:
815 return ctdb_control_tunnel_deregister(ctdb, client_id, srvid);
817 case CTDB_CONTROL_VACUUM_FETCH:
818 return ctdb_control_vacuum_fetch(ctdb, indata);
820 case CTDB_CONTROL_DB_VACUUM: {
821 struct ctdb_db_vacuum db_vacuum;
823 CHECK_CONTROL_DATA_SIZE(ctdb_db_vacuum_len(&db_vacuum));
824 return ctdb_control_db_vacuum(ctdb, c, indata, async_reply);
826 case CTDB_CONTROL_ECHO_DATA: {
827 return ctdb_control_echo_data(ctdb, c, indata, async_reply);
830 default:
831 DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
832 return -1;
837 send a reply for a ctdb control
839 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control_old *c,
840 TDB_DATA *outdata, int32_t status, const char *errormsg)
842 struct ctdb_reply_control_old *r;
843 size_t len;
845 /* some controls send no reply */
846 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
847 return;
850 len = offsetof(struct ctdb_reply_control_old, data) + (outdata?outdata->dsize:0);
851 if (errormsg) {
852 len += strlen(errormsg);
854 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control_old);
855 if (r == NULL) {
856 DEBUG(DEBUG_ERR,(__location__ "Unable to allocate transport - OOM or transport is down\n"));
857 return;
860 r->hdr.destnode = c->hdr.srcnode;
861 r->hdr.reqid = c->hdr.reqid;
862 r->status = status;
863 r->datalen = outdata?outdata->dsize:0;
864 if (outdata && outdata->dsize) {
865 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
867 if (errormsg) {
868 r->errorlen = strlen(errormsg);
869 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
872 ctdb_queue_packet_opcode(ctdb, &r->hdr, c->opcode);
874 talloc_free(r);
878 called when a CTDB_REQ_CONTROL packet comes in
880 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
882 struct ctdb_req_control_old *c = (struct ctdb_req_control_old *)hdr;
883 TDB_DATA data, *outdata;
884 int32_t status;
885 bool async_reply = false;
886 const char *errormsg = NULL;
888 data.dptr = &c->data[0];
889 data.dsize = c->datalen;
891 outdata = talloc_zero(c, TDB_DATA);
893 status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode,
894 &errormsg, &async_reply);
896 if (!async_reply) {
897 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
902 called when a CTDB_REPLY_CONTROL packet comes in
904 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
906 struct ctdb_reply_control_old *c = (struct ctdb_reply_control_old *)hdr;
907 TDB_DATA data;
908 struct ctdb_control_state *state;
909 const char *errormsg = NULL;
911 state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_control_state);
912 if (state == NULL) {
913 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_control\n",
914 ctdb->pnn, hdr->reqid));
915 return;
918 if (hdr->reqid != state->reqid) {
919 /* we found a record but it was the wrong one */
920 DEBUG(DEBUG_ERR, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
921 return;
924 data.dptr = &c->data[0];
925 data.dsize = c->datalen;
926 if (c->errorlen) {
927 errormsg = talloc_strndup(state,
928 (char *)&c->data[c->datalen], c->errorlen);
931 /* make state a child of the packet, so it goes away when the packet
932 is freed. */
933 talloc_steal(hdr, state);
935 state->callback(ctdb, c->status, data, errormsg, state->private_data);
938 static int ctdb_control_destructor(struct ctdb_control_state *state)
940 reqid_remove(state->ctdb->idr, state->reqid);
941 return 0;
945 handle a timeout of a control
947 static void ctdb_control_timeout(struct tevent_context *ev,
948 struct tevent_timer *te,
949 struct timeval t, void *private_data)
951 struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
952 TALLOC_CTX *tmp_ctx = talloc_new(ev);
954 CTDB_INCREMENT_STAT(state->ctdb, timeouts.control);
956 talloc_steal(tmp_ctx, state);
958 state->callback(state->ctdb, -1, tdb_null,
959 "ctdb_control timed out",
960 state->private_data);
961 talloc_free(tmp_ctx);
966 send a control message to a node
968 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
969 uint64_t srvid, uint32_t opcode, uint32_t client_id,
970 uint32_t flags,
971 TDB_DATA data,
972 ctdb_control_callback_fn_t callback,
973 void *private_data)
975 struct ctdb_req_control_old *c;
976 struct ctdb_control_state *state;
977 size_t len;
979 if (ctdb->methods == NULL) {
980 DEBUG(DEBUG_INFO,(__location__ " Failed to send control. Transport is DOWN\n"));
981 return -1;
984 if (((destnode == CTDB_BROADCAST_ACTIVE) ||
985 (destnode == CTDB_BROADCAST_ALL) ||
986 (destnode == CTDB_BROADCAST_CONNECTED)) &&
987 !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
988 DEBUG(DEBUG_CRIT,("Attempt to broadcast control without NOREPLY\n"));
989 return -1;
992 if (destnode != CTDB_BROADCAST_ACTIVE &&
993 destnode != CTDB_BROADCAST_ALL &&
994 destnode != CTDB_BROADCAST_CONNECTED &&
995 (!ctdb_validate_pnn(ctdb, destnode) ||
996 (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
997 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
998 callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
1000 return 0;
1003 /* the state is made a child of private_data if possible. This means any reply
1004 will be discarded if the private_data goes away */
1005 state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
1006 CTDB_NO_MEMORY(ctdb, state);
1008 state->reqid = reqid_new(ctdb->idr, state);
1009 state->callback = callback;
1010 state->private_data = private_data;
1011 state->ctdb = ctdb;
1012 state->flags = flags;
1014 talloc_set_destructor(state, ctdb_control_destructor);
1016 len = offsetof(struct ctdb_req_control_old, data) + data.dsize;
1017 c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len,
1018 struct ctdb_req_control_old);
1019 CTDB_NO_MEMORY(ctdb, c);
1020 talloc_set_name_const(c, "ctdb_req_control packet");
1022 c->hdr.destnode = destnode;
1023 c->hdr.reqid = state->reqid;
1024 c->opcode = opcode;
1025 c->client_id = client_id;
1026 c->flags = flags;
1027 c->srvid = srvid;
1028 c->datalen = data.dsize;
1029 if (data.dsize) {
1030 memcpy(&c->data[0], data.dptr, data.dsize);
1033 ctdb_queue_packet(ctdb, &c->hdr);
1035 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
1036 talloc_free(state);
1037 return 0;
1040 if (ctdb->tunable.control_timeout) {
1041 tevent_add_timer(ctdb->ev, state,
1042 timeval_current_ofs(ctdb->tunable.control_timeout, 0),
1043 ctdb_control_timeout, state);
1046 talloc_free(c);
1047 return 0;