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/tevent/tevent.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"
28 #include "../common/rb_tree.h"
30 static bool later_db(const char *name)
32 return (strstr(name, "notify") || strstr(name, "serverid"));
38 static int ctdb_lock_all_databases(struct ctdb_context *ctdb, uint32_t priority)
40 struct ctdb_db_context *ctdb_db;
42 /* This double loop is for backward compatibility and deadlock
43 avoidance for old samba versions that not yet support
45 This code shall be removed later
47 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
48 if (ctdb_db->priority != priority) {
51 if (later_db(ctdb_db->db_name)) {
54 DEBUG(DEBUG_INFO,("locking database 0x%08x priority:%u %s\n", ctdb_db->db_id, ctdb_db->priority, ctdb_db->db_name));
55 if (tdb_lockall(ctdb_db->ltdb->tdb) != 0) {
56 DEBUG(DEBUG_ERR,(__location__ " Failed to lock database %s\n", ctdb_db->db_name));
60 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
61 if (ctdb_db->priority != priority) {
64 if (!later_db(ctdb_db->db_name)) {
67 DEBUG(DEBUG_INFO,("locking database 0x%08x priority:%u %s\n", ctdb_db->db_id, ctdb_db->priority, ctdb_db->db_name));
68 if (tdb_lockall(ctdb_db->ltdb->tdb) != 0) {
69 DEBUG(DEBUG_ERR,(__location__ " Failed to lock database %s\n", ctdb_db->db_name));
77 a list of control requests waiting for a freeze lock child to get
80 struct ctdb_freeze_waiter {
81 struct ctdb_freeze_waiter *next, *prev;
82 struct ctdb_context *ctdb;
83 struct ctdb_req_control *c;
88 /* a handle to a freeze lock child process */
89 struct ctdb_freeze_handle {
90 struct ctdb_context *ctdb;
94 struct ctdb_freeze_waiter *waiters;
98 destroy a freeze handle
100 static int ctdb_freeze_handle_destructor(struct ctdb_freeze_handle *h)
102 struct ctdb_context *ctdb = h->ctdb;
103 struct ctdb_db_context *ctdb_db;
105 DEBUG(DEBUG_ERR,("Release freeze handler for prio %u\n", h->priority));
107 /* cancel any pending transactions */
108 if (ctdb->freeze_transaction_started) {
109 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
110 if (ctdb_db->priority != h->priority) {
113 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
114 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
115 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
118 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
120 ctdb->freeze_transaction_started = false;
123 ctdb->freeze_mode[h->priority] = CTDB_FREEZE_NONE;
124 ctdb->freeze_handles[h->priority] = NULL;
126 kill(h->child, SIGKILL);
131 called when the child writes its status to us
133 static void ctdb_freeze_lock_handler(struct event_context *ev, struct fd_event *fde,
134 uint16_t flags, void *private_data)
136 struct ctdb_freeze_handle *h = talloc_get_type(private_data, struct ctdb_freeze_handle);
138 struct ctdb_freeze_waiter *w;
140 if (h->ctdb->freeze_mode[h->priority] == CTDB_FREEZE_FROZEN) {
141 DEBUG(DEBUG_INFO,("freeze child died - unfreezing\n"));
146 if (read(h->fd, &status, sizeof(status)) != sizeof(status)) {
147 DEBUG(DEBUG_ERR,("read error from freeze lock child\n"));
152 DEBUG(DEBUG_ERR,("Failed to get locks in ctdb_freeze_child\n"));
153 /* we didn't get the locks - destroy the handle */
158 h->ctdb->freeze_mode[h->priority] = CTDB_FREEZE_FROZEN;
160 /* notify the waiters */
161 if (h != h->ctdb->freeze_handles[h->priority]) {
162 DEBUG(DEBUG_ERR,("lockwait finished but h is not linked\n"));
164 while ((w = h->waiters)) {
166 DLIST_REMOVE(h->waiters, w);
172 create a child which gets locks on all the open databases, then calls the callback telling the parent
175 static struct ctdb_freeze_handle *ctdb_freeze_lock(struct ctdb_context *ctdb, uint32_t priority)
177 struct ctdb_freeze_handle *h;
179 struct fd_event *fde;
181 h = talloc_zero(ctdb, struct ctdb_freeze_handle);
182 CTDB_NO_MEMORY_NULL(ctdb, h);
185 h->priority = priority;
187 if (pipe(fd) == -1) {
188 DEBUG(DEBUG_ERR,("Failed to create pipe for ctdb_freeze_lock\n"));
194 if (h->child == -1) {
195 DEBUG(DEBUG_ERR,("Failed to fork child for ctdb_freeze_lock\n"));
206 debug_extra = talloc_asprintf(NULL, "freeze_lock-%u:", priority);
207 ret = ctdb_lock_all_databases(ctdb, priority);
212 ret = write(fd[1], &ret, sizeof(ret));
213 if (ret != sizeof(ret)) {
214 DEBUG(DEBUG_ERR, (__location__ " Failed to write to socket from freeze child. ret:%d errno:%u\n", ret, errno));
220 if (kill(ctdb->ctdbd_pid, 0) != 0) {
221 DEBUG(DEBUG_ERR,("Parent died. Exiting lock wait child\n"));
228 talloc_set_destructor(h, ctdb_freeze_handle_destructor);
231 set_close_on_exec(fd[0]);
236 fde = event_add_fd(ctdb->ev, h, h->fd, EVENT_FD_READ,
237 ctdb_freeze_lock_handler, h);
239 DEBUG(DEBUG_ERR,("Failed to setup fd event for ctdb_freeze_lock\n"));
244 tevent_fd_set_auto_close(fde);
250 destroy a waiter for a freeze mode change
252 static int ctdb_freeze_waiter_destructor(struct ctdb_freeze_waiter *w)
254 ctdb_request_control_reply(w->ctdb, w->c, NULL, w->status, NULL);
259 start the freeze process for a certain priority
261 int ctdb_start_freeze(struct ctdb_context *ctdb, uint32_t priority)
264 DEBUG(DEBUG_ERR,("Freeze priority 0 requested, remapping to priority 1\n"));
268 if ((priority < 1) || (priority > NUM_DB_PRIORITIES)) {
269 DEBUG(DEBUG_ERR,(__location__ " Invalid db priority : %u\n", priority));
273 if (ctdb->freeze_mode[priority] == CTDB_FREEZE_FROZEN) {
274 /* we're already frozen */
278 /* Stop any vacuuming going on: we don't want to wait. */
279 ctdb_stop_vacuuming(ctdb);
281 /* if there isn't a freeze lock child then create one */
282 if (ctdb->freeze_handles[priority] == NULL) {
283 ctdb->freeze_handles[priority] = ctdb_freeze_lock(ctdb, priority);
284 CTDB_NO_MEMORY(ctdb, ctdb->freeze_handles[priority]);
285 ctdb->freeze_mode[priority] = CTDB_FREEZE_PENDING;
294 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
296 struct ctdb_freeze_waiter *w;
299 priority = (uint32_t)c->srvid;
301 DEBUG(DEBUG_ERR, ("Freeze priority %u\n", priority));
304 DEBUG(DEBUG_ERR,("Freeze priority 0 requested, remapping to priority 1\n"));
308 if ((priority < 1) || (priority > NUM_DB_PRIORITIES)) {
309 DEBUG(DEBUG_ERR,(__location__ " Invalid db priority : %u\n", priority));
313 if (ctdb->freeze_mode[priority] == CTDB_FREEZE_FROZEN) {
314 /* we're already frozen */
318 if (ctdb_start_freeze(ctdb, priority) != 0) {
319 DEBUG(DEBUG_ERR,(__location__ " Failed to start freezing databases with priority %u\n", priority));
323 /* add ourselves to list of waiters */
324 if (ctdb->freeze_handles[priority] == NULL) {
325 DEBUG(DEBUG_ERR,("No freeze lock handle when adding a waiter\n"));
329 w = talloc(ctdb->freeze_handles[priority], struct ctdb_freeze_waiter);
330 CTDB_NO_MEMORY(ctdb, w);
332 w->c = talloc_steal(w, c);
333 w->priority = priority;
335 talloc_set_destructor(w, ctdb_freeze_waiter_destructor);
336 DLIST_ADD(ctdb->freeze_handles[priority]->waiters, w);
338 /* we won't reply till later */
345 block until we are frozen, used during daemon startup
347 bool ctdb_blocking_freeze(struct ctdb_context *ctdb)
351 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
352 if (ctdb_start_freeze(ctdb, i)) {
353 DEBUG(DEBUG_ERR,(__location__ " Failed to freeze databases of prio %u\n", i));
357 /* block until frozen */
358 while (ctdb->freeze_mode[i] == CTDB_FREEZE_PENDING) {
359 event_loop_once(ctdb->ev);
367 static void thaw_priority(struct ctdb_context *ctdb, uint32_t priority)
369 DEBUG(DEBUG_ERR,("Thawing priority %u\n", priority));
371 /* cancel any pending transactions */
372 if (ctdb->freeze_transaction_started) {
373 struct ctdb_db_context *ctdb_db;
375 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
376 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
377 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
378 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
381 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
384 ctdb->freeze_transaction_started = false;
387 /* this hack can be used to get a copy of the databases at the end of a recovery */
388 system("mkdir -p /var/ctdb.saved; /usr/bin/rsync --delete -a /var/ctdb/ /var/ctdb.saved/$$ 2>&1 > /dev/null");
392 /* and this one for local testing */
393 system("mkdir -p test.db.saved; /usr/bin/rsync --delete -a test.db/ test.db.saved/$$ 2>&1 > /dev/null");
396 if (ctdb->freeze_handles[priority] != NULL) {
397 talloc_free(ctdb->freeze_handles[priority]);
398 ctdb->freeze_handles[priority] = NULL;
405 int32_t ctdb_control_thaw(struct ctdb_context *ctdb, uint32_t priority)
408 if (priority > NUM_DB_PRIORITIES) {
409 DEBUG(DEBUG_ERR,(__location__ " Invalid db priority : %u\n", priority));
415 for (i=1;i<=NUM_DB_PRIORITIES; i++) {
416 thaw_priority(ctdb, i);
419 thaw_priority(ctdb, priority);
422 ctdb_call_resend_all(ctdb);
428 start a transaction on all databases - used for recovery
430 int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id)
432 struct ctdb_db_context *ctdb_db;
435 for (i=1;i<=NUM_DB_PRIORITIES; i++) {
436 if (ctdb->freeze_mode[i] != CTDB_FREEZE_FROZEN) {
437 DEBUG(DEBUG_ERR,(__location__ " Failed transaction_start while not frozen\n"));
442 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
445 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
447 if (ctdb->freeze_transaction_started) {
448 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
449 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
451 /* not a fatal error */
455 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
457 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
460 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction for db '%s'\n",
466 ctdb->freeze_transaction_started = true;
467 ctdb->freeze_transaction_id = id;
473 cancel a transaction for all databases - used for recovery
475 int32_t ctdb_control_transaction_cancel(struct ctdb_context *ctdb)
477 struct ctdb_db_context *ctdb_db;
479 DEBUG(DEBUG_ERR,(__location__ " recovery transaction cancelled called\n"));
481 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
482 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
484 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
485 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n", ctdb_db->db_name));
486 /* not a fatal error */
489 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
492 ctdb->freeze_transaction_started = false;
498 commit transactions on all databases
500 int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id)
502 struct ctdb_db_context *ctdb_db;
504 int healthy_nodes = 0;
506 for (i=1;i<=NUM_DB_PRIORITIES; i++) {
507 if (ctdb->freeze_mode[i] != CTDB_FREEZE_FROZEN) {
508 DEBUG(DEBUG_ERR,(__location__ " Failed transaction_start while not frozen\n"));
513 if (!ctdb->freeze_transaction_started) {
514 DEBUG(DEBUG_ERR,(__location__ " transaction not started\n"));
518 if (id != ctdb->freeze_transaction_id) {
519 DEBUG(DEBUG_ERR,(__location__ " incorrect transaction id 0x%x in commit\n", id));
523 DEBUG(DEBUG_DEBUG,(__location__ " num_nodes[%d]\n", ctdb->num_nodes));
524 for (i=0; i < ctdb->num_nodes; i++) {
525 DEBUG(DEBUG_DEBUG,(__location__ " node[%d].flags[0x%X]\n",
526 i, ctdb->nodes[i]->flags));
527 if (ctdb->nodes[i]->flags == 0) {
531 DEBUG(DEBUG_INFO,(__location__ " healthy_nodes[%d]\n", healthy_nodes));
533 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
536 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
537 ret = tdb_transaction_commit(ctdb_db->ltdb->tdb);
539 DEBUG(DEBUG_ERR,(__location__ " Failed to commit transaction for db '%s'. Cancel all transactions and resetting transaction_started to false.\n",
543 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
545 ret = ctdb_update_persistent_health(ctdb, ctdb_db, NULL, healthy_nodes);
547 DEBUG(DEBUG_CRIT,(__location__ " Failed to update persistent health for db '%s'. "
548 "Cancel all remaining transactions and resetting transaction_started to false.\n",
554 ctdb->freeze_transaction_started = false;
555 ctdb->freeze_transaction_id = 0;
560 /* cancel any pending transactions */
561 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
562 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
563 if (tdb_transaction_cancel(ctdb_db->ltdb->tdb) != 0) {
564 DEBUG(DEBUG_ERR,(__location__ " Failed to cancel transaction for db '%s'\n",
567 tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_NOLOCK);
569 ctdb->freeze_transaction_started = false;
575 wipe a database - only possible when in a frozen transaction
577 int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata)
579 struct ctdb_control_wipe_database w = *(struct ctdb_control_wipe_database *)indata.dptr;
580 struct ctdb_db_context *ctdb_db;
582 ctdb_db = find_ctdb_db(ctdb, w.db_id);
584 DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%x\n", w.db_id));
588 if (ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_FROZEN) {
589 DEBUG(DEBUG_ERR,(__location__ " Failed transaction_start while not frozen\n"));
593 if (!ctdb->freeze_transaction_started) {
594 DEBUG(DEBUG_ERR,(__location__ " transaction not started\n"));
598 if (w.transaction_id != ctdb->freeze_transaction_id) {
599 DEBUG(DEBUG_ERR,(__location__ " incorrect transaction id 0x%x in commit\n", w.transaction_id));
603 if (tdb_wipe_all(ctdb_db->ltdb->tdb) != 0) {
604 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database for db '%s'\n",
609 if (!ctdb_db->persistent) {
610 talloc_free(ctdb_db->delete_queue);
611 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
612 if (ctdb_db->delete_queue == NULL) {
613 DEBUG(DEBUG_ERR, (__location__ " Failed to re-create "
614 "the vacuum tree.\n"));