scripts: Add support for CTDB_DBDIR in tmpfs
[ctdb.git] / server / ctdb_banning.c
1 /* 
2    ctdb banning code
3
4    Copyright (C) Ronnie Sahlberg  2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "includes.h"
20 #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"
27
28
29 static void
30 ctdb_ban_node_event(struct event_context *ev, struct timed_event *te, 
31                                struct timeval t, void *private_data)
32 {
33         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
34         bool freeze_failed = false;
35         int i;
36
37         /* Make sure we were able to freeze databases during banning */
38         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
39                 if (ctdb->freeze_mode[i] != CTDB_FREEZE_FROZEN) {
40                         freeze_failed = true;
41                         break;
42                 }
43         }
44         if (freeze_failed) {
45                 DEBUG(DEBUG_ERR, ("Banning timedout, but still unable to freeze databases\n"));
46                 ctdb_ban_self(ctdb);
47                 return;
48         }
49
50         DEBUG(DEBUG_ERR,("Banning timedout\n"));
51         ctdb->nodes[ctdb->pnn]->flags &= ~NODE_FLAGS_BANNED;
52
53         if (ctdb->banning_ctx != NULL) {
54                 talloc_free(ctdb->banning_ctx);
55                 ctdb->banning_ctx = NULL;
56         }
57 }
58
59 void ctdb_local_node_got_banned(struct ctdb_context *ctdb)
60 {
61         uint32_t i;
62
63         /* make sure we are frozen */
64         DEBUG(DEBUG_NOTICE,("This node has been banned - forcing freeze and recovery\n"));
65
66         /* Reset the generation id to 1 to make us ignore any
67            REQ/REPLY CALL/DMASTER someone sends to us.
68            We are now banned so we shouldnt service database calls
69            anymore.
70         */
71         ctdb->vnn_map->generation = INVALID_GENERATION;
72
73         ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
74         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
75                 ctdb_start_freeze(ctdb, i);
76         }
77         ctdb_release_all_ips(ctdb);
78 }
79
80 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata)
81 {
82         struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)indata.dptr;
83         bool already_banned;
84
85         DEBUG(DEBUG_INFO,("SET BAN STATE\n"));
86
87         if (bantime->pnn != ctdb->pnn) {
88                 if (bantime->pnn >= ctdb->num_nodes) {
89                         DEBUG(DEBUG_ERR,(__location__ " ERROR: Invalid ban request. PNN:%d is invalid. Max nodes %d\n", bantime->pnn, ctdb->num_nodes));
90                         return -1;
91                 }
92                 if (bantime->time == 0) {
93                         DEBUG(DEBUG_NOTICE,("unbanning node %d\n", bantime->pnn));
94                         ctdb->nodes[bantime->pnn]->flags &= ~NODE_FLAGS_BANNED;
95                 } else {
96                         DEBUG(DEBUG_NOTICE,("banning node %d\n", bantime->pnn));
97                         if (ctdb->tunable.enable_bans == 0) {
98                                 /* FIXME: This is bogus. We really should be
99                                  * taking decision based on the tunables on
100                                  * the banned node and not local node.
101                                  */
102                                 DEBUG(DEBUG_WARNING,("Bans are disabled - ignoring ban of node %u\n", bantime->pnn));
103                                 return 0;
104                         }
105
106                         ctdb->nodes[bantime->pnn]->flags |= NODE_FLAGS_BANNED;
107                 }
108                 return 0;
109         }
110
111         already_banned = false;
112         if (ctdb->banning_ctx != NULL) {
113                 talloc_free(ctdb->banning_ctx);
114                 ctdb->banning_ctx = NULL;
115                 already_banned = true;
116         }
117
118         if (bantime->time == 0) {
119                 DEBUG(DEBUG_ERR,("Unbanning this node\n"));
120                 ctdb->nodes[bantime->pnn]->flags &= ~NODE_FLAGS_BANNED;
121                 return 0;
122         }
123
124         if (ctdb->tunable.enable_bans == 0) {
125                 DEBUG(DEBUG_ERR,("Bans are disabled - ignoring ban of node %u\n", bantime->pnn));
126                 return 0;
127         }
128
129         ctdb->banning_ctx = talloc(ctdb, struct ctdb_ban_time);
130         if (ctdb->banning_ctx == NULL) {
131                 DEBUG(DEBUG_CRIT,(__location__ " ERROR Failed to allocate new banning state\n"));
132                 return -1;
133         }
134         *((struct ctdb_ban_time *)(ctdb->banning_ctx)) = *bantime;
135
136
137         DEBUG(DEBUG_ERR,("Banning this node for %d seconds\n", bantime->time));
138         ctdb->nodes[bantime->pnn]->flags |= NODE_FLAGS_BANNED;
139
140         event_add_timed(ctdb->ev, ctdb->banning_ctx, timeval_current_ofs(bantime->time,0), ctdb_ban_node_event, ctdb);
141
142         if (!already_banned) {
143                 ctdb_local_node_got_banned(ctdb);
144         }
145         return 0;
146 }
147
148 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata)
149 {
150         struct ctdb_ban_time *bantime;
151
152         bantime = talloc(outdata, struct ctdb_ban_time);
153         CTDB_NO_MEMORY(ctdb, bantime);
154
155         if (ctdb->banning_ctx != NULL) {
156                 *bantime = *(struct ctdb_ban_time *)(ctdb->banning_ctx);
157         } else {
158                 bantime->pnn = ctdb->pnn;
159                 bantime->time = 0;
160         }
161
162         outdata->dptr  = (uint8_t *)bantime;
163         outdata->dsize = sizeof(struct ctdb_ban_time);
164
165         return 0;
166 }
167
168 /* Routine to ban ourselves for a while when trouble strikes. */
169 void ctdb_ban_self(struct ctdb_context *ctdb)
170 {
171         TDB_DATA data;
172         struct ctdb_ban_time bantime;
173
174         bantime.pnn  = ctdb->pnn;
175         bantime.time = ctdb->tunable.recovery_ban_period;
176
177         data.dsize = sizeof(bantime);
178         data.dptr  = (uint8_t *)&bantime;
179
180         ctdb_control_set_ban_state(ctdb, data);
181 }