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/>.
20 #include "lib/events/events.h"
21 #include "lib/tdb/include/tdb.h"
22 #include "system/network.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "../include/ctdb_private.h"
26 #include "lib/util/dlinklist.h"
33 static int ctdb_lock_all_databases(struct ctdb_context *ctdb, uint32_t priority)
35 struct ctdb_db_context *ctdb_db;
37 /* This double loop is for backward compatibility and deadlock
38 avoidance for old samba versions that not yet support
40 This code shall be removed later
42 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
43 if (ctdb_db->priority != priority) {
46 if (strstr(ctdb_db->db_name, "notify") != NULL) {
49 DEBUG(DEBUG_INFO,("locking database 0x%08x priority:%u %s\n", ctdb_db->db_id, ctdb_db->priority, ctdb_db->db_name));
50 if (tdb_lockall(ctdb_db->ltdb->tdb) != 0) {
54 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
55 if (ctdb_db->priority != priority) {
58 if (strstr(ctdb_db->db_name, "notify") == NULL) {
61 DEBUG(DEBUG_INFO,("locking database 0x%08x priority:%u %s\n", ctdb_db->db_id, ctdb_db->priority, ctdb_db->db_name));
62 if (tdb_lockall(ctdb_db->ltdb->tdb) != 0) {
70 a list of control requests waiting for a freeze lock child to get
73 struct ctdb_freeze_waiter {
74 struct ctdb_freeze_waiter *next, *prev;
75 struct ctdb_context *ctdb;
76 struct ctdb_req_control *c;
81 /* a handle to a freeze lock child process */
82 struct ctdb_freeze_handle {
83 struct ctdb_context *ctdb;
87 struct ctdb_freeze_waiter *waiters;
91 destroy a freeze handle
93 static int ctdb_freeze_handle_destructor(struct ctdb_freeze_handle *h)
95 struct ctdb_context *ctdb = h->ctdb;
96 struct ctdb_db_context *ctdb_db;
98 DEBUG(DEBUG_ERR,("Release freeze handler for prio %u\n", h->priority));
100 /* cancel any pending transactions */
101 if (ctdb->freeze_transaction_started) {
102 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
103 if (ctdb_db->priority != h->priority) {
106 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
107 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
108 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
111 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
113 ctdb->freeze_transaction_started = false;
116 ctdb->freeze_mode[h->priority] = CTDB_FREEZE_NONE;
117 ctdb->freeze_handles[h->priority] = NULL;
119 kill(h->child, SIGKILL);
124 called when the child writes its status to us
126 static void ctdb_freeze_lock_handler(struct event_context *ev, struct fd_event *fde,
127 uint16_t flags, void *private_data)
129 struct ctdb_freeze_handle *h = talloc_get_type(private_data, struct ctdb_freeze_handle);
131 struct ctdb_freeze_waiter *w;
133 if (h->ctdb->freeze_mode[h->priority] == CTDB_FREEZE_FROZEN) {
134 DEBUG(DEBUG_INFO,("freeze child died - unfreezing\n"));
139 if (read(h->fd, &status, sizeof(status)) != sizeof(status)) {
140 DEBUG(DEBUG_ERR,("read error from freeze lock child\n"));
145 DEBUG(DEBUG_ERR,("Failed to get locks in ctdb_freeze_child\n"));
146 /* we didn't get the locks - destroy the handle */
151 h->ctdb->freeze_mode[h->priority] = CTDB_FREEZE_FROZEN;
153 /* notify the waiters */
154 if (h != h->ctdb->freeze_handles[h->priority]) {
155 DEBUG(DEBUG_ERR,("lockwait finished but h is not linked\n"));
157 while ((w = h->waiters)) {
159 DLIST_REMOVE(h->waiters, w);
165 create a child which gets locks on all the open databases, then calls the callback telling the parent
168 static struct ctdb_freeze_handle *ctdb_freeze_lock(struct ctdb_context *ctdb, uint32_t priority)
170 struct ctdb_freeze_handle *h;
172 struct fd_event *fde;
174 h = talloc_zero(ctdb, struct ctdb_freeze_handle);
175 CTDB_NO_MEMORY_NULL(ctdb, h);
178 h->priority = priority;
180 /* use socketpair() instead of pipe() so we have bi-directional fds */
181 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0) {
182 DEBUG(DEBUG_ERR,("Failed to create pipe for ctdb_freeze_lock\n"));
188 if (h->child == -1) {
189 DEBUG(DEBUG_ERR,("Failed to fork child for ctdb_freeze_lock\n"));
199 ret = ctdb_lock_all_databases(ctdb, priority);
206 while (count++ < 30) {
207 ret = write(fd[1], &ret, sizeof(ret));
208 if (ret == sizeof(ret)) {
211 DEBUG(DEBUG_ERR, (__location__ " Failed to write to socket from freeze child. ret:%d errno:%u\n", ret, errno));
215 DEBUG(DEBUG_ERR, (__location__ " Failed to write to socket from freeze child. Aborting freeze child\n"));
219 /* the read here means we will die if the parent exits */
220 read(fd[1], &ret, sizeof(ret));
224 talloc_set_destructor(h, ctdb_freeze_handle_destructor);
230 fde = event_add_fd(ctdb->ev, h, h->fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
231 ctdb_freeze_lock_handler, h);
233 DEBUG(DEBUG_ERR,("Failed to setup fd event for ctdb_freeze_lock\n"));
243 destroy a waiter for a freeze mode change
245 static int ctdb_freeze_waiter_destructor(struct ctdb_freeze_waiter *w)
247 ctdb_request_control_reply(w->ctdb, w->c, NULL, w->status, NULL);
252 start the freeze process for a certain priority
254 int ctdb_start_freeze(struct ctdb_context *ctdb, uint32_t priority)
257 DEBUG(DEBUG_ERR,("Freeze priority 0 requested, remapping to priority 1\n"));
261 if ((priority < 1) || (priority > NUM_DB_PRIORITIES)) {
262 DEBUG(DEBUG_ERR,(__location__ " Invalid db priority : %u\n", priority));
266 if (ctdb->freeze_mode[priority] == CTDB_FREEZE_FROZEN) {
267 /* we're already frozen */
271 /* if there isn't a freeze lock child then create one */
272 if (ctdb->freeze_handles[priority] == NULL) {
273 ctdb->freeze_handles[priority] = ctdb_freeze_lock(ctdb, priority);
274 CTDB_NO_MEMORY(ctdb, ctdb->freeze_handles[priority]);
275 ctdb->freeze_mode[priority] = CTDB_FREEZE_PENDING;
284 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
286 struct ctdb_freeze_waiter *w;
289 priority = (uint32_t)c->srvid;
291 DEBUG(DEBUG_ERR, ("Freeze priority %u\n", priority));
294 DEBUG(DEBUG_ERR,("Freeze priority 0 requested, remapping to priority 1\n"));
298 if ((priority < 1) || (priority > NUM_DB_PRIORITIES)) {
299 DEBUG(DEBUG_ERR,(__location__ " Invalid db priority : %u\n", priority));
303 if (ctdb->freeze_mode[priority] == CTDB_FREEZE_FROZEN) {
304 /* we're already frozen */
308 if (ctdb_start_freeze(ctdb, priority) != 0) {
309 DEBUG(DEBUG_ERR,(__location__ " Failed to start freezing databases with priority %u\n", priority));
313 /* add ourselves to list of waiters */
314 if (ctdb->freeze_handles[priority] == NULL) {
315 DEBUG(DEBUG_ERR,("No freeze lock handle when adding a waiter\n"));
319 w = talloc(ctdb->freeze_handles[priority], struct ctdb_freeze_waiter);
320 CTDB_NO_MEMORY(ctdb, w);
322 w->c = talloc_steal(w, c);
323 w->priority = priority;
325 talloc_set_destructor(w, ctdb_freeze_waiter_destructor);
326 DLIST_ADD(ctdb->freeze_handles[priority]->waiters, w);
328 /* we won't reply till later */
335 block until we are frozen, used during daemon startup
337 bool ctdb_blocking_freeze(struct ctdb_context *ctdb)
341 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
342 if (ctdb_start_freeze(ctdb, i)) {
343 DEBUG(DEBUG_ERR,(__location__ " Failed to freeze databases of prio %u\n", i));
347 /* block until frozen */
348 while (ctdb->freeze_mode[i] == CTDB_FREEZE_PENDING) {
349 event_loop_once(ctdb->ev);
357 static void thaw_priority(struct ctdb_context *ctdb, uint32_t priority)
359 DEBUG(DEBUG_ERR,("Thawing priority %u\n", priority));
361 /* cancel any pending transactions */
362 if (ctdb->freeze_transaction_started) {
363 struct ctdb_db_context *ctdb_db;
365 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
366 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
367 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
368 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
371 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
374 ctdb->freeze_transaction_started = false;
377 /* this hack can be used to get a copy of the databases at the end of a recovery */
378 system("mkdir -p /var/ctdb.saved; /usr/bin/rsync --delete -a /var/ctdb/ /var/ctdb.saved/$$ 2>&1 > /dev/null");
382 /* and this one for local testing */
383 system("mkdir -p test.db.saved; /usr/bin/rsync --delete -a test.db/ test.db.saved/$$ 2>&1 > /dev/null");
386 if (ctdb->freeze_handles[priority] != NULL) {
387 talloc_free(ctdb->freeze_handles[priority]);
388 ctdb->freeze_handles[priority] = NULL;
395 int32_t ctdb_control_thaw(struct ctdb_context *ctdb, uint32_t priority)
398 if (priority > NUM_DB_PRIORITIES) {
399 DEBUG(DEBUG_ERR,(__location__ " Invalid db priority : %u\n", priority));
405 for (i=1;i<=NUM_DB_PRIORITIES; i++) {
406 thaw_priority(ctdb, i);
409 thaw_priority(ctdb, priority);
412 ctdb_call_resend_all(ctdb);
418 start a transaction on all databases - used for recovery
420 int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id)
422 struct ctdb_db_context *ctdb_db;
425 for (i=1;i<=NUM_DB_PRIORITIES; i++) {
426 if (ctdb->freeze_mode[i] != CTDB_FREEZE_FROZEN) {
427 DEBUG(DEBUG_ERR,(__location__ " Failed transaction_start while not frozen\n"));
432 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
435 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
437 if (ctdb->freeze_transaction_started) {
438 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
439 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
441 /* not a fatal error */
445 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
447 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
450 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction for db '%s'\n",
456 ctdb->freeze_transaction_started = true;
457 ctdb->freeze_transaction_id = id;
463 cancel a transaction for all databases - used for recovery
465 int32_t ctdb_control_transaction_cancel(struct ctdb_context *ctdb)
467 struct ctdb_db_context *ctdb_db;
469 DEBUG(DEBUG_ERR,(__location__ " recovery transaction cancelled called\n"));
471 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
472 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
474 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
475 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n", ctdb_db->db_name));
476 /* not a fatal error */
479 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
482 ctdb->freeze_transaction_started = false;
488 commit transactions on all databases
490 int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id)
492 struct ctdb_db_context *ctdb_db;
495 for (i=1;i<=NUM_DB_PRIORITIES; i++) {
496 if (ctdb->freeze_mode[i] != CTDB_FREEZE_FROZEN) {
497 DEBUG(DEBUG_ERR,(__location__ " Failed transaction_start while not frozen\n"));
502 if (!ctdb->freeze_transaction_started) {
503 DEBUG(DEBUG_ERR,(__location__ " transaction not started\n"));
507 if (id != ctdb->freeze_transaction_id) {
508 DEBUG(DEBUG_ERR,(__location__ " incorrect transaction id 0x%x in commit\n", id));
512 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
513 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
514 if (tdb_transaction_commit(ctdb_db->ltdb->tdb) != 0) {
515 DEBUG(DEBUG_ERR,(__location__ " Failed to commit transaction for db '%s'. Cancel all transactions and resetting transaction_started to false.\n",
518 /* cancel any pending transactions */
519 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
520 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
521 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
522 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
525 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
527 ctdb->freeze_transaction_started = false;
531 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
534 ctdb->freeze_transaction_started = false;
535 ctdb->freeze_transaction_id = 0;
541 wipe a database - only possible when in a frozen transaction
543 int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata)
545 struct ctdb_control_wipe_database w = *(struct ctdb_control_wipe_database *)indata.dptr;
546 struct ctdb_db_context *ctdb_db;
548 ctdb_db = find_ctdb_db(ctdb, w.db_id);
550 DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%x\n", w.db_id));
554 if (ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_FROZEN) {
555 DEBUG(DEBUG_ERR,(__location__ " Failed transaction_start while not frozen\n"));
559 if (!ctdb->freeze_transaction_started) {
560 DEBUG(DEBUG_ERR,(__location__ " transaction not started\n"));
564 if (w.transaction_id != ctdb->freeze_transaction_id) {
565 DEBUG(DEBUG_ERR,(__location__ " incorrect transaction id 0x%x in commit\n", w.transaction_id));
569 if (tdb_wipe_all(ctdb_db->ltdb->tdb) != 0) {
570 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database for db '%s'\n",