4 Copyright (C) Ronnie Sahlberg 2009
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 #include "lib/tdb/include/tdb.h"
21 #include "system/time.h"
22 #include "system/network.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "../include/ctdb_client.h"
26 #include "../include/ctdb_private.h"
30 ctdb_ban_node_event(struct event_context *ev, struct timed_event *te,
31 struct timeval t, void *private_data)
33 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
35 DEBUG(DEBUG_ERR,("Banning timedout\n"));
36 ctdb->nodes[ctdb->pnn]->flags &= ~NODE_FLAGS_BANNED;
38 if (ctdb->banning_ctx != NULL) {
39 talloc_free(ctdb->banning_ctx);
40 ctdb->banning_ctx = NULL;
44 int32_t ctdb_local_node_got_banned(struct ctdb_context *ctdb)
48 /* make sure we are frozen */
49 DEBUG(DEBUG_NOTICE,("This node has been banned - forcing freeze and recovery\n"));
51 /* Reset the generation id to 1 to make us ignore any
52 REQ/REPLY CALL/DMASTER someone sends to us.
53 We are now banned so we shouldnt service database calls
56 ctdb->vnn_map->generation = INVALID_GENERATION;
58 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
59 if (ctdb_start_freeze(ctdb, i) != 0) {
60 DEBUG(DEBUG_ERR,(__location__ " Failed to freeze db priority %u\n", i));
63 ctdb_release_all_ips(ctdb);
64 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
69 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata)
71 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)indata.dptr;
73 DEBUG(DEBUG_INFO,("SET BAN STATE\n"));
75 if (bantime->pnn != ctdb->pnn) {
76 if (bantime->pnn < 0 || bantime->pnn >= ctdb->num_nodes) {
77 DEBUG(DEBUG_ERR,(__location__ " ERROR: Invalid ban request. PNN:%d is invalid. Max nodes %d\n", bantime->pnn, ctdb->num_nodes));
80 if (bantime->time == 0) {
81 DEBUG(DEBUG_INFO,("unbanning node %d\n", bantime->pnn));
82 ctdb->nodes[bantime->pnn]->flags &= ~NODE_FLAGS_BANNED;
84 DEBUG(DEBUG_INFO,("banning node %d\n", bantime->pnn));
85 if (ctdb->tunable.enable_bans == 0) {
86 DEBUG(DEBUG_INFO,("Bans are disabled - ignoring ban of node %u\n", bantime->pnn));
90 ctdb->nodes[bantime->pnn]->flags |= NODE_FLAGS_BANNED;
95 if (ctdb->banning_ctx != NULL) {
96 talloc_free(ctdb->banning_ctx);
97 ctdb->banning_ctx = NULL;
100 if (bantime->time == 0) {
101 DEBUG(DEBUG_ERR,("Unbanning this node\n"));
102 ctdb->nodes[bantime->pnn]->flags &= ~NODE_FLAGS_BANNED;
106 if (ctdb->tunable.enable_bans == 0) {
107 DEBUG(DEBUG_ERR,("Bans are disabled - ignoring ban of node %u\n", bantime->pnn));
111 ctdb->banning_ctx = talloc(ctdb, struct ctdb_ban_time);
112 if (ctdb->banning_ctx == NULL) {
113 DEBUG(DEBUG_CRIT,(__location__ " ERROR Failed to allocate new banning state\n"));
116 *((struct ctdb_ban_time *)(ctdb->banning_ctx)) = *bantime;
119 DEBUG(DEBUG_ERR,("Banning this node for %d seconds\n", bantime->time));
120 ctdb->nodes[bantime->pnn]->flags |= NODE_FLAGS_BANNED;
122 event_add_timed(ctdb->ev, ctdb->banning_ctx, timeval_current_ofs(bantime->time,0), ctdb_ban_node_event, ctdb);
123 if (bantime->pnn == ctdb->pnn) {
124 return ctdb_local_node_got_banned(ctdb);
130 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata)
132 struct ctdb_ban_time *bantime;
134 bantime = talloc(outdata, struct ctdb_ban_time);
135 CTDB_NO_MEMORY(ctdb, bantime);
137 if (ctdb->banning_ctx != NULL) {
138 *bantime = *(struct ctdb_ban_time *)(ctdb->banning_ctx);
140 bantime->pnn = ctdb->pnn;
144 outdata->dptr = (uint8_t *)bantime;
145 outdata->dsize = sizeof(struct ctdb_ban_time);
150 /* Routine to ban ourselves for a while when trouble strikes. */
151 void ctdb_ban_self(struct ctdb_context *ctdb)
154 struct ctdb_ban_time bantime;
156 bantime.pnn = ctdb->pnn;
157 bantime.time = ctdb->tunable.recovery_ban_period;
159 data.dsize = sizeof(bantime);
160 data.dptr = (uint8_t *)&bantime;
162 ctdb_control_set_ban_state(ctdb, data);