vfs_default: also call vfs_offload_token_ctx_init in vfswrap_offload_write_send
[Samba.git] / ctdb / server / ctdb_control.c
bloba51795f340a5dd9b24f1fa91afb7599b4c284250
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);
176 static int ctdb_control_disable_node(struct ctdb_context *ctdb)
178 struct ctdb_node *node;
180 node = ctdb_find_node(ctdb, CTDB_CURRENT_NODE);
181 if (node == NULL) {
182 /* Can't happen */
183 DBG_ERR("Unable to find current node\n");
184 return -1;
187 D_ERR("Disable node\n");
188 node->flags |= NODE_FLAGS_PERMANENTLY_DISABLED;
190 return 0;
193 static int ctdb_control_enable_node(struct ctdb_context *ctdb)
195 struct ctdb_node *node;
197 node = ctdb_find_node(ctdb, CTDB_CURRENT_NODE);
198 if (node == NULL) {
199 /* Can't happen */
200 DBG_ERR("Unable to find current node\n");
201 return -1;
204 D_ERR("Enable node\n");
205 node->flags &= ~NODE_FLAGS_PERMANENTLY_DISABLED;
207 return 0;
211 process a control request
213 static int32_t ctdb_control_dispatch(struct ctdb_context *ctdb,
214 struct ctdb_req_control_old *c,
215 TDB_DATA indata,
216 TDB_DATA *outdata, uint32_t srcnode,
217 const char **errormsg,
218 bool *async_reply)
220 uint32_t opcode = c->opcode;
221 uint64_t srvid = c->srvid;
222 uint32_t client_id = c->client_id;
223 static int level = DEBUG_ERR;
225 switch (opcode) {
226 case CTDB_CONTROL_PROCESS_EXISTS: {
227 CHECK_CONTROL_DATA_SIZE(sizeof(pid_t));
228 return ctdb_control_process_exists(ctdb, *(pid_t *)indata.dptr);
231 case CTDB_CONTROL_SET_DEBUG: {
232 union {
233 uint8_t *ptr;
234 int32_t *level;
235 } debug;
236 CHECK_CONTROL_DATA_SIZE(sizeof(int32_t));
237 debug.ptr = indata.dptr;
238 debuglevel_set(*debug.level);
239 return 0;
242 case CTDB_CONTROL_GET_DEBUG: {
243 CHECK_CONTROL_DATA_SIZE(0);
244 level = debuglevel_get();
245 outdata->dptr = (uint8_t *)&(level);
246 outdata->dsize = sizeof(DEBUGLEVEL);
247 return 0;
250 case CTDB_CONTROL_STATISTICS: {
251 CHECK_CONTROL_DATA_SIZE(0);
252 ctdb->statistics.memory_used = talloc_total_size(NULL);
253 ctdb->statistics.num_clients = ctdb->num_clients;
254 ctdb->statistics.frozen = (ctdb_db_all_frozen(ctdb) ? 1 : 0);
255 ctdb->statistics.recovering = (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE);
256 ctdb->statistics.statistics_current_time = timeval_current();
258 outdata->dptr = (uint8_t *)&ctdb->statistics;
259 outdata->dsize = sizeof(ctdb->statistics);
260 return 0;
263 case CTDB_CONTROL_GET_ALL_TUNABLES: {
264 CHECK_CONTROL_DATA_SIZE(0);
265 outdata->dptr = (uint8_t *)&ctdb->tunable;
266 outdata->dsize = sizeof(ctdb->tunable);
267 return 0;
270 case CTDB_CONTROL_DUMP_MEMORY: {
271 CHECK_CONTROL_DATA_SIZE(0);
272 return ctdb_dump_memory(ctdb, outdata);
275 case CTDB_CONTROL_STATISTICS_RESET: {
276 struct ctdb_db_context *ctdb_db;
278 CHECK_CONTROL_DATA_SIZE(0);
279 ZERO_STRUCT(ctdb->statistics);
280 for (ctdb_db = ctdb->db_list;
281 ctdb_db != NULL;
282 ctdb_db = ctdb_db->next) {
283 ctdb_db_statistics_reset(ctdb_db);
285 ctdb->statistics.statistics_start_time = timeval_current();
286 return 0;
289 case CTDB_CONTROL_GETVNNMAP:
290 return ctdb_control_getvnnmap(ctdb, opcode, indata, outdata);
292 case CTDB_CONTROL_GET_DBMAP:
293 return ctdb_control_getdbmap(ctdb, opcode, indata, outdata);
295 case CTDB_CONTROL_GET_NODEMAPv4:
296 return control_not_implemented("GET_NODEMAPv4", "GET_NODEMAP");
298 case CTDB_CONTROL_GET_NODEMAP:
299 return ctdb_control_getnodemap(ctdb, opcode, indata, outdata);
301 case CTDB_CONTROL_GET_NODES_FILE:
302 return ctdb_control_getnodesfile(ctdb, opcode, indata, outdata);
304 case CTDB_CONTROL_RELOAD_NODES_FILE:
305 CHECK_CONTROL_DATA_SIZE(0);
306 return ctdb_control_reload_nodes_file(ctdb, opcode);
308 case CTDB_CONTROL_SET_DB_STICKY: {
309 uint32_t db_id;
310 struct ctdb_db_context *ctdb_db;
312 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
313 db_id = *(uint32_t *)indata.dptr;
314 ctdb_db = find_ctdb_db(ctdb, db_id);
315 if (ctdb_db == NULL) return -1;
316 return ctdb_set_db_sticky(ctdb, ctdb_db);
319 case CTDB_CONTROL_SETVNNMAP:
320 return ctdb_control_setvnnmap(ctdb, opcode, indata, outdata);
322 case CTDB_CONTROL_PULL_DB:
323 return control_not_implemented("PULL_DB", NULL);
325 case CTDB_CONTROL_SET_DMASTER:
326 return control_not_implemented("SET_DMASTER", NULL);
328 case CTDB_CONTROL_PUSH_DB:
329 return control_not_implemented("PUSH_DB", NULL);
331 case CTDB_CONTROL_GET_RECMODE: {
332 return ctdb->recovery_mode;
335 case CTDB_CONTROL_SET_RECMASTER:
336 return control_not_implemented("SET_RECMASTER", NULL);
338 case CTDB_CONTROL_GET_RECMASTER:
339 return control_not_implemented("GET_RECMASTER", NULL);
341 case CTDB_CONTROL_GET_PID:
342 return getpid();
344 case CTDB_CONTROL_GET_PNN:
345 return ctdb->pnn;
347 case CTDB_CONTROL_PING:
348 CHECK_CONTROL_DATA_SIZE(0);
349 return ctdb->num_clients;
351 case CTDB_CONTROL_GET_RUNSTATE:
352 CHECK_CONTROL_DATA_SIZE(0);
353 outdata->dptr = (uint8_t *)&ctdb->runstate;
354 outdata->dsize = sizeof(uint32_t);
355 return 0;
358 case CTDB_CONTROL_SET_DB_READONLY: {
359 uint32_t db_id;
360 struct ctdb_db_context *ctdb_db;
362 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
363 db_id = *(uint32_t *)indata.dptr;
364 ctdb_db = find_ctdb_db(ctdb, db_id);
365 if (ctdb_db == NULL) return -1;
366 return ctdb_set_db_readonly(ctdb, ctdb_db);
368 case CTDB_CONTROL_GET_DBNAME: {
369 uint32_t db_id;
370 struct ctdb_db_context *ctdb_db;
372 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
373 db_id = *(uint32_t *)indata.dptr;
374 ctdb_db = find_ctdb_db(ctdb, db_id);
375 if (ctdb_db == NULL) return -1;
376 outdata->dptr = discard_const(ctdb_db->db_name);
377 outdata->dsize = strlen(ctdb_db->db_name)+1;
378 return 0;
381 case CTDB_CONTROL_GETDBPATH: {
382 uint32_t db_id;
383 struct ctdb_db_context *ctdb_db;
385 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
386 db_id = *(uint32_t *)indata.dptr;
387 ctdb_db = find_ctdb_db(ctdb, db_id);
388 if (ctdb_db == NULL) return -1;
389 outdata->dptr = discard_const(ctdb_db->db_path);
390 outdata->dsize = strlen(ctdb_db->db_path)+1;
391 return 0;
394 case CTDB_CONTROL_DB_ATTACH:
395 return ctdb_control_db_attach(ctdb,
396 indata,
397 outdata,
399 srcnode,
400 client_id,
402 async_reply);
404 case CTDB_CONTROL_DB_ATTACH_PERSISTENT:
405 return ctdb_control_db_attach(ctdb,
406 indata,
407 outdata,
408 CTDB_DB_FLAGS_PERSISTENT,
409 srcnode,
410 client_id,
412 async_reply);
414 case CTDB_CONTROL_DB_ATTACH_REPLICATED:
415 return ctdb_control_db_attach(ctdb,
416 indata,
417 outdata,
418 CTDB_DB_FLAGS_REPLICATED,
419 srcnode,
420 client_id,
422 async_reply);
424 case CTDB_CONTROL_SET_CALL:
425 return control_not_implemented("SET_CALL", NULL);
427 case CTDB_CONTROL_TRAVERSE_START:
428 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
429 return ctdb_control_traverse_start(ctdb, indata, outdata, srcnode, client_id);
431 case CTDB_CONTROL_TRAVERSE_START_EXT:
432 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start_ext));
433 return ctdb_control_traverse_start_ext(ctdb, indata, outdata, srcnode, client_id);
435 case CTDB_CONTROL_TRAVERSE_ALL:
436 return ctdb_control_traverse_all(ctdb, indata, outdata);
438 case CTDB_CONTROL_TRAVERSE_ALL_EXT:
439 return ctdb_control_traverse_all_ext(ctdb, indata, outdata);
441 case CTDB_CONTROL_TRAVERSE_DATA:
442 return ctdb_control_traverse_data(ctdb, indata, outdata);
444 case CTDB_CONTROL_TRAVERSE_KILL:
445 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
446 return ctdb_control_traverse_kill(ctdb, indata, outdata, srcnode);
448 case CTDB_CONTROL_REGISTER_SRVID:
449 return daemon_register_message_handler(ctdb, client_id, srvid);
451 case CTDB_CONTROL_DEREGISTER_SRVID:
452 return daemon_deregister_message_handler(ctdb, client_id, srvid);
454 case CTDB_CONTROL_CHECK_SRVIDS:
455 return control_not_implemented("CHECK_SRVIDS", NULL);
457 case CTDB_CONTROL_ENABLE_SEQNUM:
458 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
459 return ctdb_ltdb_enable_seqnum(ctdb, *(uint32_t *)indata.dptr);
461 case CTDB_CONTROL_UPDATE_SEQNUM:
462 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
463 return ctdb_ltdb_update_seqnum(ctdb, *(uint32_t *)indata.dptr, srcnode);
465 case CTDB_CONTROL_FREEZE:
466 CHECK_CONTROL_DATA_SIZE(0);
467 return ctdb_control_freeze(ctdb, c, async_reply);
469 case CTDB_CONTROL_THAW:
470 return control_not_implemented("THAW", NULL);
472 case CTDB_CONTROL_SET_RECMODE:
473 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
474 return ctdb_control_set_recmode(ctdb, c, indata, async_reply, errormsg);
476 case CTDB_CONTROL_GET_MONMODE:
477 return control_not_implemented("GET_MONMODE", NULL);
479 case CTDB_CONTROL_ENABLE_MONITOR:
480 return control_not_implemented("ENABLE_MONITOR", NULL);
482 case CTDB_CONTROL_RUN_EVENTSCRIPTS:
483 return control_not_implemented("RUN_EVENTSCRIPTS", NULL);
485 case CTDB_CONTROL_DISABLE_MONITOR:
486 return control_not_implemented("DISABLE_MONITOR", NULL);
488 case CTDB_CONTROL_SHUTDOWN:
489 DEBUG(DEBUG_NOTICE,("Received SHUTDOWN command.\n"));
490 ctdb_shutdown_sequence(ctdb, 0);
491 /* In case above returns due to duplicate shutdown */
492 return 0;
494 case CTDB_CONTROL_TAKEOVER_IPv4:
495 return control_not_implemented("TAKEOVER_IPv4", "TAKEOVER_IP");
497 case CTDB_CONTROL_TAKEOVER_IP:
498 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
499 return ctdb_control_takeover_ip(ctdb, c, indata, async_reply);
501 case CTDB_CONTROL_RELEASE_IPv4:
502 return control_not_implemented("RELEASE_IPv4", "RELEASE_IP");
504 case CTDB_CONTROL_RELEASE_IP:
505 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
506 return ctdb_control_release_ip(ctdb, c, indata, async_reply);
508 case CTDB_CONTROL_IPREALLOCATED:
509 CHECK_CONTROL_DATA_SIZE(0);
510 return ctdb_control_ipreallocated(ctdb, c, async_reply);
512 case CTDB_CONTROL_GET_PUBLIC_IPSv4:
513 return control_not_implemented("GET_PUBLIC_IPSv4",
514 "GET_PUBLIC_IPS");
516 case CTDB_CONTROL_GET_PUBLIC_IPS:
517 CHECK_CONTROL_DATA_SIZE(0);
518 return ctdb_control_get_public_ips(ctdb, c, outdata);
520 case CTDB_CONTROL_TCP_CLIENT:
521 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
522 return ctdb_control_tcp_client(ctdb, client_id, indata);
524 case CTDB_CONTROL_STARTUP:
525 CHECK_CONTROL_DATA_SIZE(0);
526 return ctdb_control_startup(ctdb, srcnode);
528 case CTDB_CONTROL_TCP_ADD:
529 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
530 return ctdb_control_tcp_add(ctdb, indata, false);
532 case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
533 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
534 return ctdb_control_tcp_add(ctdb, indata, true);
536 case CTDB_CONTROL_TCP_REMOVE:
537 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
538 return ctdb_control_tcp_remove(ctdb, indata);
540 case CTDB_CONTROL_SET_TUNABLE:
541 return ctdb_control_set_tunable(ctdb, indata);
543 case CTDB_CONTROL_GET_TUNABLE:
544 return ctdb_control_get_tunable(ctdb, indata, outdata);
546 case CTDB_CONTROL_LIST_TUNABLES:
547 return ctdb_control_list_tunables(ctdb, outdata);
549 case CTDB_CONTROL_MODIFY_FLAGS:
550 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_flag_change));
551 return ctdb_control_modflags(ctdb, indata);
553 case CTDB_CONTROL_KILL_TCP:
554 return control_not_implemented("KILL_TCP", NULL);
556 case CTDB_CONTROL_GET_TCP_TICKLE_LIST:
557 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
558 return ctdb_control_get_tcp_tickle_list(ctdb, indata, outdata);
560 case CTDB_CONTROL_SET_TCP_TICKLE_LIST:
561 /* data size is verified in the called function */
562 return ctdb_control_set_tcp_tickle_list(ctdb, indata);
564 case CTDB_CONTROL_REGISTER_SERVER_ID:
565 return control_not_implemented("REGISTER_SERVER_ID", NULL);
567 case CTDB_CONTROL_UNREGISTER_SERVER_ID:
568 return control_not_implemented("UNREGISTER_SERVER_ID", NULL);
570 case CTDB_CONTROL_CHECK_SERVER_ID:
571 return control_not_implemented("CHECK_SERVER_ID", NULL);
573 case CTDB_CONTROL_GET_SERVER_ID_LIST:
574 return control_not_implemented("SERVER_ID_LIST", NULL);
576 case CTDB_CONTROL_PERSISTENT_STORE:
577 return control_not_implemented("PERSISTENT_STORE", NULL);
579 case CTDB_CONTROL_UPDATE_RECORD:
580 return ctdb_control_update_record(ctdb, c, indata, async_reply);
582 case CTDB_CONTROL_SEND_GRATUITOUS_ARP:
583 return ctdb_control_send_gratious_arp(ctdb, indata);
585 case CTDB_CONTROL_TRANSACTION_START:
586 return control_not_implemented("TRANSACTION_START", NULL);
588 case CTDB_CONTROL_TRANSACTION_COMMIT:
589 return control_not_implemented("TRANSACTION_COMMIT", NULL);
591 case CTDB_CONTROL_WIPE_DATABASE:
592 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
593 return ctdb_control_wipe_database(ctdb, indata);
595 case CTDB_CONTROL_UPTIME:
596 return ctdb_control_uptime(ctdb, outdata);
598 case CTDB_CONTROL_START_RECOVERY:
599 return ctdb_control_start_recovery(ctdb, c, async_reply);
601 case CTDB_CONTROL_END_RECOVERY:
602 return ctdb_control_end_recovery(ctdb, c, async_reply);
604 case CTDB_CONTROL_TRY_DELETE_RECORDS:
605 return ctdb_control_try_delete_records(ctdb, indata, outdata);
607 case CTDB_CONTROL_ADD_PUBLIC_IP:
608 return ctdb_control_add_public_address(ctdb, indata);
610 case CTDB_CONTROL_DEL_PUBLIC_IP:
611 return ctdb_control_del_public_address(ctdb, indata);
613 case CTDB_CONTROL_GET_CAPABILITIES:
614 return ctdb_control_get_capabilities(ctdb, outdata);
616 case CTDB_CONTROL_START_PERSISTENT_UPDATE:
617 return ctdb_control_start_persistent_update(ctdb, c, indata);
619 case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE:
620 return ctdb_control_cancel_persistent_update(ctdb, c, indata);
622 case CTDB_CONTROL_TRANS2_COMMIT:
623 case CTDB_CONTROL_TRANS2_COMMIT_RETRY:
624 return control_not_implemented("TRANS2_COMMIT", "TRANS3_COMMIT");
626 case CTDB_CONTROL_TRANS2_ERROR:
627 return control_not_implemented("TRANS2_ERROR", NULL);
629 case CTDB_CONTROL_TRANS2_FINISHED:
630 return control_not_implemented("TRANS2_FINISHED", NULL);
632 case CTDB_CONTROL_TRANS2_ACTIVE:
633 return control_not_implemented("TRANS2_ACTIVE", NULL);
635 case CTDB_CONTROL_TRANS3_COMMIT:
636 return ctdb_control_trans3_commit(ctdb, c, indata, async_reply);
638 case CTDB_CONTROL_RECD_PING:
639 CHECK_CONTROL_DATA_SIZE(0);
640 return ctdb_control_recd_ping(ctdb);
642 case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
643 return control_not_implemented("GET_EVENT_SCRIPT_STATUS", NULL);
645 case CTDB_CONTROL_RECD_RECLOCK_LATENCY:
646 CHECK_CONTROL_DATA_SIZE(sizeof(double));
647 CTDB_UPDATE_RECLOCK_LATENCY(ctdb, "recd reclock", reclock.recd, *((double *)indata.dptr));
648 return 0;
649 case CTDB_CONTROL_GET_RECLOCK_FILE:
650 CHECK_CONTROL_DATA_SIZE(0);
651 if (ctdb->recovery_lock != NULL) {
652 outdata->dptr = discard_const(ctdb->recovery_lock);
653 outdata->dsize = strlen(ctdb->recovery_lock) + 1;
655 return 0;
656 case CTDB_CONTROL_SET_RECLOCK_FILE:
657 return control_not_implemented("SET_RECLOCK", NULL);
659 case CTDB_CONTROL_STOP_NODE:
660 CHECK_CONTROL_DATA_SIZE(0);
661 return ctdb_control_stop_node(ctdb);
663 case CTDB_CONTROL_CONTINUE_NODE:
664 CHECK_CONTROL_DATA_SIZE(0);
665 return ctdb_control_continue_node(ctdb);
667 case CTDB_CONTROL_SET_NATGWSTATE:
668 return control_not_implemented("SET_NATGWSTATE", NULL);
670 case CTDB_CONTROL_SET_LMASTERROLE: {
671 uint32_t lmasterrole;
673 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
674 lmasterrole = *(uint32_t *)indata.dptr;
675 if (lmasterrole == 0) {
676 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
677 } else {
678 ctdb->capabilities |= CTDB_CAP_LMASTER;
680 return 0;
683 case CTDB_CONTROL_SET_RECMASTERROLE: {
684 uint32_t recmasterrole;
686 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
687 recmasterrole = *(uint32_t *)indata.dptr;
688 if (recmasterrole == 0) {
689 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
690 } else {
691 ctdb->capabilities |= CTDB_CAP_RECMASTER;
693 return 0;
696 case CTDB_CONTROL_ENABLE_SCRIPT:
697 return control_not_implemented("ENABLE_SCRIPT", NULL);
699 case CTDB_CONTROL_DISABLE_SCRIPT:
700 return control_not_implemented("DISABLE_SCRIPT", NULL);
702 case CTDB_CONTROL_SET_BAN_STATE:
703 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_ban_state));
704 return ctdb_control_set_ban_state(ctdb, indata);
706 case CTDB_CONTROL_GET_BAN_STATE:
707 CHECK_CONTROL_DATA_SIZE(0);
708 return ctdb_control_get_ban_state(ctdb, outdata);
710 case CTDB_CONTROL_SET_DB_PRIORITY:
711 return control_not_implemented("SET_DB_PRIORITY", NULL);
713 case CTDB_CONTROL_GET_DB_PRIORITY:
714 return control_not_implemented("GET_DB_PRIORITY", NULL);
716 case CTDB_CONTROL_TRANSACTION_CANCEL:
717 return control_not_implemented("TRANSACTION_CANCEL", NULL);
719 case CTDB_CONTROL_REGISTER_NOTIFY:
720 return ctdb_control_register_notify(ctdb, client_id, indata);
722 case CTDB_CONTROL_DEREGISTER_NOTIFY:
723 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
724 return ctdb_control_deregister_notify(ctdb, client_id, indata);
726 case CTDB_CONTROL_GET_LOG:
727 return control_not_implemented("GET_LOG", NULL);
729 case CTDB_CONTROL_CLEAR_LOG:
730 return control_not_implemented("CLEAR_LOG", NULL);
732 case CTDB_CONTROL_GET_DB_SEQNUM:
733 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
734 return ctdb_control_get_db_seqnum(ctdb, indata, outdata);
736 case CTDB_CONTROL_DB_SET_HEALTHY:
737 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
738 return ctdb_control_db_set_healthy(ctdb, indata);
740 case CTDB_CONTROL_DB_GET_HEALTH:
741 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
742 return ctdb_control_db_get_health(ctdb, indata, outdata);
744 case CTDB_CONTROL_GET_PUBLIC_IP_INFO:
745 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
746 return ctdb_control_get_public_ip_info(ctdb, c, indata, outdata);
748 case CTDB_CONTROL_GET_IFACES:
749 CHECK_CONTROL_DATA_SIZE(0);
750 return ctdb_control_get_ifaces(ctdb, c, outdata);
752 case CTDB_CONTROL_SET_IFACE_LINK_STATE:
753 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_iface));
754 return ctdb_control_set_iface_link(ctdb, c, indata);
756 case CTDB_CONTROL_GET_STAT_HISTORY:
757 CHECK_CONTROL_DATA_SIZE(0);
758 return ctdb_control_get_stat_history(ctdb, c, outdata);
760 case CTDB_CONTROL_SCHEDULE_FOR_DELETION: {
761 struct ctdb_control_schedule_for_deletion *d;
762 size_t size = offsetof(struct ctdb_control_schedule_for_deletion, key);
763 CHECK_CONTROL_MIN_DATA_SIZE(size);
764 d = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
765 size += d->keylen;
766 CHECK_CONTROL_DATA_SIZE(size);
767 return ctdb_control_schedule_for_deletion(ctdb, indata);
769 case CTDB_CONTROL_GET_DB_STATISTICS:
770 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
771 return ctdb_control_get_db_statistics(ctdb, *(uint32_t *)indata.dptr, outdata);
773 case CTDB_CONTROL_RELOAD_PUBLIC_IPS:
774 CHECK_CONTROL_DATA_SIZE(0);
775 return ctdb_control_reload_public_ips(ctdb, c, async_reply);
777 case CTDB_CONTROL_RECEIVE_RECORDS:
778 return control_not_implemented("RECEIVE_RECORDS", NULL);
780 case CTDB_CONTROL_DB_DETACH:
781 return ctdb_control_db_detach(ctdb, indata, client_id);
783 case CTDB_CONTROL_DB_FREEZE:
784 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
785 return ctdb_control_db_freeze(ctdb, c, *(uint32_t *)indata.dptr,
786 async_reply);
788 case CTDB_CONTROL_DB_THAW:
789 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
790 return ctdb_control_db_thaw(ctdb, *(uint32_t *)indata.dptr);
792 case CTDB_CONTROL_DB_TRANSACTION_START:
793 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
794 return ctdb_control_db_transaction_start(ctdb, indata);
796 case CTDB_CONTROL_DB_TRANSACTION_COMMIT:
797 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
798 return ctdb_control_db_transaction_commit(ctdb, indata);
800 case CTDB_CONTROL_DB_TRANSACTION_CANCEL:
801 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
802 return ctdb_control_db_transaction_cancel(ctdb, indata);
804 case CTDB_CONTROL_DB_PULL:
805 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_pulldb_ext));
806 return ctdb_control_db_pull(ctdb, c, indata, outdata);
808 case CTDB_CONTROL_DB_PUSH_START:
809 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_pulldb_ext));
810 return ctdb_control_db_push_start(ctdb, indata);
812 case CTDB_CONTROL_DB_PUSH_CONFIRM:
813 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
814 return ctdb_control_db_push_confirm(ctdb, indata, outdata);
816 case CTDB_CONTROL_DB_OPEN_FLAGS: {
817 uint32_t db_id;
818 struct ctdb_db_context *ctdb_db;
819 int tdb_flags;
821 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
822 db_id = *(uint32_t *)indata.dptr;
823 ctdb_db = find_ctdb_db(ctdb, db_id);
824 if (ctdb_db == NULL) {
825 return -1;
828 tdb_flags = tdb_get_flags(ctdb_db->ltdb->tdb);
830 outdata->dptr = talloc_size(outdata, sizeof(tdb_flags));
831 if (outdata->dptr == NULL) {
832 return -1;
835 outdata->dsize = sizeof(tdb_flags);
836 memcpy(outdata->dptr, &tdb_flags, outdata->dsize);
837 return 0;
840 case CTDB_CONTROL_CHECK_PID_SRVID:
841 CHECK_CONTROL_DATA_SIZE((sizeof(pid_t) + sizeof(uint64_t)));
842 return ctdb_control_check_pid_srvid(ctdb, indata);
844 case CTDB_CONTROL_TUNNEL_REGISTER:
845 return ctdb_control_tunnel_register(ctdb, client_id, srvid);
847 case CTDB_CONTROL_TUNNEL_DEREGISTER:
848 return ctdb_control_tunnel_deregister(ctdb, client_id, srvid);
850 case CTDB_CONTROL_VACUUM_FETCH:
851 return ctdb_control_vacuum_fetch(ctdb, indata);
853 case CTDB_CONTROL_DB_VACUUM: {
854 struct ctdb_db_vacuum db_vacuum;
856 CHECK_CONTROL_DATA_SIZE(ctdb_db_vacuum_len(&db_vacuum));
857 return ctdb_control_db_vacuum(ctdb, c, indata, async_reply);
859 case CTDB_CONTROL_ECHO_DATA: {
860 return ctdb_control_echo_data(ctdb, c, indata, async_reply);
863 case CTDB_CONTROL_DISABLE_NODE:
864 CHECK_CONTROL_DATA_SIZE(0);
865 return ctdb_control_disable_node(ctdb);
867 case CTDB_CONTROL_ENABLE_NODE:
868 CHECK_CONTROL_DATA_SIZE(0);
869 return ctdb_control_enable_node(ctdb);
871 case CTDB_CONTROL_TCP_CLIENT_DISCONNECTED:
872 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
873 return ctdb_control_tcp_client_disconnected(ctdb, client_id, indata);
875 case CTDB_CONTROL_TCP_CLIENT_PASSED:
876 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
877 return ctdb_control_tcp_client_passed(ctdb, client_id, indata);
879 case CTDB_CONTROL_START_IPREALLOCATE:
880 CHECK_CONTROL_DATA_SIZE(0);
881 return ctdb_control_start_ipreallocate(ctdb, c, async_reply);
883 default:
884 DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
885 return -1;
890 send a reply for a ctdb control
892 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control_old *c,
893 TDB_DATA *outdata, int32_t status, const char *errormsg)
895 struct ctdb_reply_control_old *r;
896 size_t len;
898 /* some controls send no reply */
899 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
900 return;
903 len = offsetof(struct ctdb_reply_control_old, data) + (outdata?outdata->dsize:0);
904 if (errormsg) {
905 len += strlen(errormsg);
907 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control_old);
908 if (r == NULL) {
909 DEBUG(DEBUG_ERR,(__location__ "Unable to allocate transport - OOM or transport is down\n"));
910 return;
913 r->hdr.destnode = c->hdr.srcnode;
914 r->hdr.reqid = c->hdr.reqid;
915 r->status = status;
916 r->datalen = outdata?outdata->dsize:0;
917 if (outdata && outdata->dsize) {
918 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
920 if (errormsg) {
921 r->errorlen = strlen(errormsg);
922 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
925 ctdb_queue_packet_opcode(ctdb, &r->hdr, c->opcode);
927 talloc_free(r);
931 called when a CTDB_REQ_CONTROL packet comes in
933 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
935 struct ctdb_req_control_old *c = (struct ctdb_req_control_old *)hdr;
936 TDB_DATA data, *outdata;
937 int32_t status;
938 bool async_reply = false;
939 const char *errormsg = NULL;
941 data.dptr = &c->data[0];
942 data.dsize = c->datalen;
944 outdata = talloc_zero(c, TDB_DATA);
946 status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode,
947 &errormsg, &async_reply);
949 if (!async_reply) {
950 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
955 called when a CTDB_REPLY_CONTROL packet comes in
957 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
959 struct ctdb_reply_control_old *c = (struct ctdb_reply_control_old *)hdr;
960 TDB_DATA data;
961 struct ctdb_control_state *state;
962 const char *errormsg = NULL;
964 state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_control_state);
965 if (state == NULL) {
966 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_control\n",
967 ctdb->pnn, hdr->reqid));
968 return;
971 if (hdr->reqid != state->reqid) {
972 /* we found a record but it was the wrong one */
973 DEBUG(DEBUG_ERR, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
974 return;
977 data.dptr = &c->data[0];
978 data.dsize = c->datalen;
979 if (c->errorlen) {
980 errormsg = talloc_strndup(state,
981 (char *)&c->data[c->datalen], c->errorlen);
984 /* make state a child of the packet, so it goes away when the packet
985 is freed. */
986 talloc_steal(hdr, state);
988 state->callback(ctdb, c->status, data, errormsg, state->private_data);
991 static int ctdb_control_destructor(struct ctdb_control_state *state)
993 reqid_remove(state->ctdb->idr, state->reqid);
994 return 0;
998 handle a timeout of a control
1000 static void ctdb_control_timeout(struct tevent_context *ev,
1001 struct tevent_timer *te,
1002 struct timeval t, void *private_data)
1004 struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
1005 TALLOC_CTX *tmp_ctx = talloc_new(ev);
1007 CTDB_INCREMENT_STAT(state->ctdb, timeouts.control);
1009 talloc_steal(tmp_ctx, state);
1011 state->callback(state->ctdb, -1, tdb_null,
1012 "ctdb_control timed out",
1013 state->private_data);
1014 talloc_free(tmp_ctx);
1019 send a control message to a node
1021 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
1022 uint64_t srvid, uint32_t opcode, uint32_t client_id,
1023 uint32_t flags,
1024 TDB_DATA data,
1025 ctdb_control_callback_fn_t callback,
1026 void *private_data)
1028 struct ctdb_req_control_old *c;
1029 struct ctdb_control_state *state;
1030 size_t len;
1032 if (ctdb->methods == NULL) {
1033 DEBUG(DEBUG_INFO,(__location__ " Failed to send control. Transport is DOWN\n"));
1034 return -1;
1037 if (((destnode == CTDB_BROADCAST_ACTIVE) ||
1038 (destnode == CTDB_BROADCAST_ALL) ||
1039 (destnode == CTDB_BROADCAST_CONNECTED)) &&
1040 !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
1041 DEBUG(DEBUG_CRIT,("Attempt to broadcast control without NOREPLY\n"));
1042 return -1;
1045 if (destnode != CTDB_BROADCAST_ACTIVE &&
1046 destnode != CTDB_BROADCAST_ALL &&
1047 destnode != CTDB_BROADCAST_CONNECTED &&
1048 (!ctdb_validate_pnn(ctdb, destnode) ||
1049 (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
1050 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
1051 callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
1053 return 0;
1056 /* the state is made a child of private_data if possible. This means any reply
1057 will be discarded if the private_data goes away */
1058 state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
1059 CTDB_NO_MEMORY(ctdb, state);
1061 state->reqid = reqid_new(ctdb->idr, state);
1062 state->callback = callback;
1063 state->private_data = private_data;
1064 state->ctdb = ctdb;
1065 state->flags = flags;
1067 talloc_set_destructor(state, ctdb_control_destructor);
1069 len = offsetof(struct ctdb_req_control_old, data) + data.dsize;
1070 c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len,
1071 struct ctdb_req_control_old);
1072 CTDB_NO_MEMORY(ctdb, c);
1073 talloc_set_name_const(c, "ctdb_req_control packet");
1075 c->hdr.destnode = destnode;
1076 c->hdr.reqid = state->reqid;
1077 c->opcode = opcode;
1078 c->client_id = client_id;
1079 c->flags = flags;
1080 c->srvid = srvid;
1081 c->datalen = data.dsize;
1082 if (data.dsize) {
1083 memcpy(&c->data[0], data.dptr, data.dsize);
1086 ctdb_queue_packet(ctdb, &c->hdr);
1088 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
1089 talloc_free(state);
1090 return 0;
1093 if (ctdb->tunable.control_timeout) {
1094 tevent_add_timer(ctdb->ev, state,
1095 timeval_current_ofs(ctdb->tunable.control_timeout, 0),
1096 ctdb_control_timeout, state);
1099 talloc_free(c);
1100 return 0;