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 "replace.h"
|
---|
20 | #include "system/time.h"
|
---|
21 | #include "system/network.h"
|
---|
22 | #include "system/filesys.h"
|
---|
23 | #include "system/wait.h"
|
---|
24 |
|
---|
25 | #include <talloc.h>
|
---|
26 | #include <tevent.h>
|
---|
27 |
|
---|
28 | #include "lib/util/debug.h"
|
---|
29 | #include "lib/util/samba_util.h"
|
---|
30 |
|
---|
31 | #include "ctdb_private.h"
|
---|
32 | #include "ctdb_client.h"
|
---|
33 |
|
---|
34 | #include "common/common.h"
|
---|
35 | #include "common/logging.h"
|
---|
36 |
|
---|
37 | static void ctdb_ban_node_event(struct tevent_context *ev,
|
---|
38 | struct tevent_timer *te,
|
---|
39 | struct timeval t, void *private_data)
|
---|
40 | {
|
---|
41 | struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
|
---|
42 |
|
---|
43 | /* Make sure we were able to freeze databases during banning */
|
---|
44 | if (!ctdb_db_all_frozen(ctdb)) {
|
---|
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 | struct ctdb_db_context *ctdb_db;
|
---|
62 |
|
---|
63 | DEBUG(DEBUG_NOTICE,("This node has been banned - forcing recovery\n"));
|
---|
64 |
|
---|
65 | /* Reset the generation id to 1 to make us ignore any
|
---|
66 | REQ/REPLY CALL/DMASTER someone sends to us.
|
---|
67 | We are now banned so we shouldnt service database calls
|
---|
68 | anymore.
|
---|
69 | */
|
---|
70 | ctdb->vnn_map->generation = INVALID_GENERATION;
|
---|
71 | for (ctdb_db = ctdb->db_list; ctdb_db != NULL; ctdb_db = ctdb_db->next) {
|
---|
72 | ctdb_db->generation = INVALID_GENERATION;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* Recovery daemon will set the recovery mode ACTIVE and freeze
|
---|
76 | * databases.
|
---|
77 | */
|
---|
78 |
|
---|
79 | ctdb_release_all_ips(ctdb);
|
---|
80 | }
|
---|
81 |
|
---|
82 | int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata)
|
---|
83 | {
|
---|
84 | struct ctdb_ban_state *bantime = (struct ctdb_ban_state *)indata.dptr;
|
---|
85 | bool already_banned;
|
---|
86 |
|
---|
87 | DEBUG(DEBUG_INFO,("SET BAN STATE\n"));
|
---|
88 |
|
---|
89 | if (bantime->pnn != ctdb->pnn) {
|
---|
90 | DEBUG(DEBUG_WARNING,
|
---|
91 | ("SET_BAN_STATE control for PNN %d ignored\n",
|
---|
92 | bantime->pnn));
|
---|
93 | return -1;
|
---|
94 | }
|
---|
95 |
|
---|
96 | already_banned = false;
|
---|
97 | if (ctdb->banning_ctx != NULL) {
|
---|
98 | talloc_free(ctdb->banning_ctx);
|
---|
99 | ctdb->banning_ctx = NULL;
|
---|
100 | already_banned = true;
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (bantime->time == 0) {
|
---|
104 | DEBUG(DEBUG_ERR,("Unbanning this node\n"));
|
---|
105 | ctdb->nodes[bantime->pnn]->flags &= ~NODE_FLAGS_BANNED;
|
---|
106 | return 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (ctdb->tunable.enable_bans == 0) {
|
---|
110 | DEBUG(DEBUG_ERR,("Bans are disabled - ignoring ban of node %u\n", bantime->pnn));
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | ctdb->banning_ctx = talloc(ctdb, struct ctdb_ban_state);
|
---|
115 | if (ctdb->banning_ctx == NULL) {
|
---|
116 | DEBUG(DEBUG_CRIT,(__location__ " ERROR Failed to allocate new banning state\n"));
|
---|
117 | return -1;
|
---|
118 | }
|
---|
119 | *((struct ctdb_ban_state *)(ctdb->banning_ctx)) = *bantime;
|
---|
120 |
|
---|
121 |
|
---|
122 | DEBUG(DEBUG_ERR,("Banning this node for %d seconds\n", bantime->time));
|
---|
123 | ctdb->nodes[bantime->pnn]->flags |= NODE_FLAGS_BANNED;
|
---|
124 |
|
---|
125 | tevent_add_timer(ctdb->ev, ctdb->banning_ctx,
|
---|
126 | timeval_current_ofs(bantime->time,0),
|
---|
127 | ctdb_ban_node_event, ctdb);
|
---|
128 |
|
---|
129 | if (!already_banned) {
|
---|
130 | ctdb_local_node_got_banned(ctdb);
|
---|
131 | }
|
---|
132 | return 0;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata)
|
---|
136 | {
|
---|
137 | struct ctdb_ban_state *bantime;
|
---|
138 |
|
---|
139 | bantime = talloc(outdata, struct ctdb_ban_state);
|
---|
140 | CTDB_NO_MEMORY(ctdb, bantime);
|
---|
141 |
|
---|
142 | if (ctdb->banning_ctx != NULL) {
|
---|
143 | *bantime = *(struct ctdb_ban_state *)(ctdb->banning_ctx);
|
---|
144 | } else {
|
---|
145 | bantime->pnn = ctdb->pnn;
|
---|
146 | bantime->time = 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | outdata->dptr = (uint8_t *)bantime;
|
---|
150 | outdata->dsize = sizeof(struct ctdb_ban_state);
|
---|
151 |
|
---|
152 | return 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* Routine to ban ourselves for a while when trouble strikes. */
|
---|
156 | void ctdb_ban_self(struct ctdb_context *ctdb)
|
---|
157 | {
|
---|
158 | TDB_DATA data;
|
---|
159 | struct ctdb_ban_state bantime;
|
---|
160 |
|
---|
161 | bantime.pnn = ctdb->pnn;
|
---|
162 | bantime.time = ctdb->tunable.recovery_ban_period;
|
---|
163 |
|
---|
164 | data.dsize = sizeof(bantime);
|
---|
165 | data.dptr = (uint8_t *)&bantime;
|
---|
166 |
|
---|
167 | ctdb_control_set_ban_state(ctdb, data);
|
---|
168 | }
|
---|