3 provide API to do non-blocking locks for single or all databases
5 Copyright (C) Amitay Isaacs 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "include/ctdb_private.h"
22 #include "include/ctdb_protocol.h"
26 #include "system/filesys.h"
27 #include "lib/util/dlinklist.h"
30 * Non-blocking Locking API
32 * 1. Create a child process to do blocking locks.
33 * 2. Once the locks are obtained, signal parent process via fd.
34 * 3. Invoke registered callback routine with locking status.
35 * 4. If the child process cannot get locks within certain time,
36 * diagnose using /proc/locks and log warning message
38 * ctdb_lock_record() - get a lock on a record
39 * ctdb_lock_db() - get a lock on a DB
40 * ctdb_lock_alldb_prio() - get a lock on all DBs with given priority
41 * ctdb_lock_alldb() - get a lock on all DBs
43 * auto_mark - whether to mark/unmark DBs in before/after callback
53 static const char * const lock_type_str[] = {
62 /* lock_context is the common part for a lock request */
64 struct lock_context *next, *prev;
66 struct ctdb_context *ctdb;
67 struct ctdb_db_context *ctdb_db;
71 struct lock_request *request;
74 struct tevent_fd *tfd;
75 struct tevent_timer *ttimer;
76 struct timeval start_time;
81 /* lock_request is the client specific part for a lock request */
83 struct lock_context *lctx;
84 void (*callback)(void *, bool);
90 * Support samba 3.6.x (and older) versions which do not set db priority.
92 * By default, all databases are set to priority 1. So only when priority
93 * is set to 1, check for databases that need higher priority.
95 static bool later_db(struct ctdb_context *ctdb, const char *name)
97 if (ctdb->tunable.samba3_hack == 0) {
101 if (strstr(name, "brlock") ||
102 strstr(name, "g_lock") ||
103 strstr(name, "notify_onelevel") ||
104 strstr(name, "serverid") ||
105 strstr(name, "xattr_tdb")) {
112 typedef int (*db_handler_t)(struct ctdb_db_context *ctdb_db,
116 static int ctdb_db_iterator(struct ctdb_context *ctdb, uint32_t priority,
117 db_handler_t handler, void *private_data)
119 struct ctdb_db_context *ctdb_db;
122 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
123 if (ctdb_db->priority != priority) {
126 if (later_db(ctdb, ctdb_db->db_name)) {
129 ret = handler(ctdb_db, priority, private_data);
135 /* If priority != 1, later_db check is not required and can return */
140 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
141 if (!later_db(ctdb, ctdb_db->db_name)) {
144 ret = handler(ctdb_db, priority, private_data);
155 * lock all databases - mark only
157 static int db_lock_mark_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
160 int tdb_transaction_write_lock_mark(struct tdb_context *);
162 DEBUG(DEBUG_INFO, ("marking locked database %s, priority:%u\n",
163 ctdb_db->db_name, priority));
165 if (tdb_transaction_write_lock_mark(ctdb_db->ltdb->tdb) != 0) {
166 DEBUG(DEBUG_ERR, ("Failed to mark (transaction lock) database %s\n",
171 if (tdb_lockall_mark(ctdb_db->ltdb->tdb) != 0) {
172 DEBUG(DEBUG_ERR, ("Failed to mark (all lock) database %s\n",
180 int ctdb_lockall_mark_prio(struct ctdb_context *ctdb, uint32_t priority)
183 * This function is only used by the main dameon during recovery.
184 * At this stage, the databases have already been locked, by a
185 * dedicated child process. The freeze_mode variable is used to track
186 * whether the actual locks are held by the child process or not.
189 if (ctdb->freeze_mode[priority] != CTDB_FREEZE_FROZEN) {
190 DEBUG(DEBUG_ERR, ("Attempt to mark all databases locked when not frozen\n"));
194 return ctdb_db_iterator(ctdb, priority, db_lock_mark_handler, NULL);
197 static int ctdb_lockall_mark(struct ctdb_context *ctdb)
201 for (priority=1; priority<=NUM_DB_PRIORITIES; priority++) {
202 if (ctdb_db_iterator(ctdb, priority, db_lock_mark_handler, NULL) != 0) {
212 * lock all databases - unmark only
214 static int db_lock_unmark_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
217 int tdb_transaction_write_lock_unmark(struct tdb_context *);
219 DEBUG(DEBUG_INFO, ("unmarking locked database %s, priority:%u\n",
220 ctdb_db->db_name, priority));
222 if (tdb_transaction_write_lock_unmark(ctdb_db->ltdb->tdb) != 0) {
223 DEBUG(DEBUG_ERR, ("Failed to unmark (transaction lock) database %s\n",
228 if (tdb_lockall_unmark(ctdb_db->ltdb->tdb) != 0) {
229 DEBUG(DEBUG_ERR, ("Failed to unmark (all lock) database %s\n",
237 int ctdb_lockall_unmark_prio(struct ctdb_context *ctdb, uint32_t priority)
240 * This function is only used by the main daemon during recovery.
241 * At this stage, the databases have already been locked, by a
242 * dedicated child process. The freeze_mode variable is used to track
243 * whether the actual locks are held by the child process or not.
246 if (ctdb->freeze_mode[priority] != CTDB_FREEZE_FROZEN) {
247 DEBUG(DEBUG_ERR, ("Attempt to unmark all databases locked when not frozen\n"));
251 return ctdb_db_iterator(ctdb, priority, db_lock_unmark_handler, NULL);
254 static int ctdb_lockall_unmark(struct ctdb_context *ctdb)
258 for (priority=NUM_DB_PRIORITIES; priority>0; priority--) {
259 if (ctdb_db_iterator(ctdb, priority, db_lock_unmark_handler, NULL) != 0) {
268 static void ctdb_lock_schedule(struct ctdb_context *ctdb);
271 * Destructor to kill the child locking process
273 static int ctdb_lock_context_destructor(struct lock_context *lock_ctx)
275 if (lock_ctx->child > 0) {
276 ctdb_kill(lock_ctx->ctdb, lock_ctx->child, SIGKILL);
277 DLIST_REMOVE(lock_ctx->ctdb->lock_current, lock_ctx);
278 if (lock_ctx->ctdb_db) {
279 lock_ctx->ctdb_db->lock_num_current--;
281 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_current);
282 if (lock_ctx->ctdb_db) {
283 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_current);
286 DLIST_REMOVE(lock_ctx->ctdb->lock_pending, lock_ctx);
287 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_pending);
288 if (lock_ctx->ctdb_db) {
289 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
293 ctdb_lock_schedule(lock_ctx->ctdb);
300 * Destructor to remove lock request
302 static int ctdb_lock_request_destructor(struct lock_request *lock_request)
304 lock_request->lctx->request = NULL;
308 void ctdb_lock_free_request_context(struct lock_request *lock_req)
310 struct lock_context *lock_ctx;
312 lock_ctx = lock_req->lctx;
313 talloc_free(lock_req);
314 talloc_free(lock_ctx);
319 * Process all the callbacks waiting for lock
321 * If lock has failed, callback is executed with locked=false
323 static void process_callbacks(struct lock_context *lock_ctx, bool locked)
325 struct lock_request *request;
327 if (lock_ctx->auto_mark && locked) {
328 switch (lock_ctx->type) {
330 tdb_chainlock_mark(lock_ctx->ctdb_db->ltdb->tdb, lock_ctx->key);
334 tdb_lockall_mark(lock_ctx->ctdb_db->ltdb->tdb);
337 case LOCK_ALLDB_PRIO:
338 ctdb_lockall_mark_prio(lock_ctx->ctdb, lock_ctx->priority);
342 ctdb_lockall_mark(lock_ctx->ctdb);
347 request = lock_ctx->request;
348 if (lock_ctx->auto_mark) {
349 /* Reset the destructor, so request is not removed from the list */
350 talloc_set_destructor(request, NULL);
352 request->callback(request->private_data, locked);
354 if (lock_ctx->auto_mark && locked) {
355 switch (lock_ctx->type) {
357 tdb_chainlock_unmark(lock_ctx->ctdb_db->ltdb->tdb, lock_ctx->key);
361 tdb_lockall_unmark(lock_ctx->ctdb_db->ltdb->tdb);
364 case LOCK_ALLDB_PRIO:
365 ctdb_lockall_unmark_prio(lock_ctx->ctdb, lock_ctx->priority);
369 ctdb_lockall_unmark(lock_ctx->ctdb);
376 static int lock_bucket_id(double t)
378 double ms = 1.e-3, s = 1;
383 } else if (t < 10*ms) {
385 } else if (t < 100*ms) {
387 } else if (t < 1*s) {
389 } else if (t < 2*s) {
391 } else if (t < 4*s) {
393 } else if (t < 8*s) {
395 } else if (t < 16*s) {
397 } else if (t < 32*s) {
399 } else if (t < 64*s) {
409 * Callback routine when the required locks are obtained.
410 * Called from parent context
412 static void ctdb_lock_handler(struct tevent_context *ev,
413 struct tevent_fd *tfd,
417 struct lock_context *lock_ctx;
418 TALLOC_CTX *tmp_ctx = NULL;
424 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
426 /* cancel the timeout event */
427 if (lock_ctx->ttimer) {
428 TALLOC_FREE(lock_ctx->ttimer);
431 t = timeval_elapsed(&lock_ctx->start_time);
432 id = lock_bucket_id(t);
434 if (lock_ctx->auto_mark) {
435 tmp_ctx = talloc_new(ev);
436 talloc_steal(tmp_ctx, lock_ctx);
439 /* Read the status from the child process */
440 if (read(lock_ctx->fd[0], &c, 1) != 1) {
443 locked = (c == 0 ? true : false);
446 /* Update statistics */
447 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_calls);
448 if (lock_ctx->ctdb_db) {
449 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_calls);
453 if (lock_ctx->ctdb_db) {
454 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.buckets[id]);
455 CTDB_UPDATE_LATENCY(lock_ctx->ctdb, lock_ctx->ctdb_db,
456 lock_type_str[lock_ctx->type], locks.latency,
457 lock_ctx->start_time);
459 CTDB_UPDATE_DB_LATENCY(lock_ctx->ctdb_db, lock_type_str[lock_ctx->type], locks.latency, t);
460 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.buckets[id]);
463 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_failed);
464 if (lock_ctx->ctdb_db) {
465 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_failed);
469 process_callbacks(lock_ctx, locked);
471 if (lock_ctx->auto_mark) {
472 talloc_free(tmp_ctx);
478 * Callback routine when required locks are not obtained within timeout
479 * Called from parent context
481 static void ctdb_lock_timeout_handler(struct tevent_context *ev,
482 struct tevent_timer *ttimer,
483 struct timeval current_time,
486 static const char * debug_locks = NULL;
487 struct lock_context *lock_ctx;
488 struct ctdb_context *ctdb;
491 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
492 ctdb = lock_ctx->ctdb;
494 if (lock_ctx->ctdb_db) {
496 ("Unable to get %s lock on database %s for %.0lf seconds\n",
497 (lock_ctx->type == LOCK_RECORD ? "RECORD" : "DB"),
498 lock_ctx->ctdb_db->db_name,
499 timeval_elapsed(&lock_ctx->start_time)));
502 ("Unable to get ALLDB locks for %.0lf seconds\n",
503 timeval_elapsed(&lock_ctx->start_time)));
506 /* Fire a child process to find the blocking process. */
507 if (debug_locks == NULL) {
508 debug_locks = getenv("CTDB_DEBUG_LOCKS");
509 if (debug_locks == NULL) {
510 debug_locks = talloc_asprintf(ctdb,
512 getenv("CTDB_BASE"));
515 if (debug_locks != NULL) {
518 execl(debug_locks, debug_locks, NULL);
521 ctdb_track_child(ctdb, pid);
525 " Unable to setup lock debugging - no memory?\n"));
528 /* reset the timeout timer */
529 // talloc_free(lock_ctx->ttimer);
530 lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
532 timeval_current_ofs(10, 0),
533 ctdb_lock_timeout_handler,
538 static int db_count_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
541 int *count = (int *)private_data;
553 static int db_name_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
556 struct db_namelist *list = (struct db_namelist *)private_data;
558 list->names[list->n] = talloc_strdup(list->names, ctdb_db->db_path);
564 static char **lock_helper_args(TALLOC_CTX *mem_ctx, struct lock_context *lock_ctx, int fd)
566 struct ctdb_context *ctdb = lock_ctx->ctdb;
570 struct db_namelist list;
572 switch (lock_ctx->type) {
581 case LOCK_ALLDB_PRIO:
583 ctdb_db_iterator(ctdb, lock_ctx->priority, db_count_handler, &nargs);
588 for (priority=1; priority<NUM_DB_PRIORITIES; priority++) {
589 ctdb_db_iterator(ctdb, priority, db_count_handler, &nargs);
594 /* Add extra argument for null termination */
597 args = talloc_array(mem_ctx, char *, nargs);
602 args[0] = talloc_strdup(args, "ctdb_lock_helper");
603 args[1] = talloc_asprintf(args, "%d", getpid());
604 args[2] = talloc_asprintf(args, "%d", fd);
606 switch (lock_ctx->type) {
608 args[3] = talloc_strdup(args, "RECORD");
609 args[4] = talloc_strdup(args, lock_ctx->ctdb_db->db_path);
610 if (lock_ctx->key.dsize == 0) {
611 args[5] = talloc_strdup(args, "NULL");
613 args[5] = hex_encode_talloc(args, lock_ctx->key.dptr, lock_ctx->key.dsize);
618 args[3] = talloc_strdup(args, "DB");
619 args[4] = talloc_strdup(args, lock_ctx->ctdb_db->db_path);
622 case LOCK_ALLDB_PRIO:
623 args[3] = talloc_strdup(args, "DB");
626 ctdb_db_iterator(ctdb, lock_ctx->priority, db_name_handler, &list);
630 args[3] = talloc_strdup(args, "DB");
633 for (priority=1; priority<NUM_DB_PRIORITIES; priority++) {
634 ctdb_db_iterator(ctdb, priority, db_name_handler, &list);
639 /* Make sure last argument is NULL */
640 args[nargs-1] = NULL;
642 for (i=0; i<nargs-1; i++) {
643 if (args[i] == NULL) {
654 * Schedule a new lock child process
655 * Set up callback handler and timeout handler
657 static void ctdb_lock_schedule(struct ctdb_context *ctdb)
659 struct lock_context *lock_ctx, *next_ctx;
662 const char *helper = BINDIR "/ctdb_lock_helper";
663 static const char *prog = NULL;
669 t = getenv("CTDB_LOCK_HELPER");
671 prog = talloc_strdup(ctdb, t);
673 prog = talloc_strdup(ctdb, helper);
675 CTDB_NO_MEMORY_VOID(ctdb, prog);
678 if (ctdb->lock_pending == NULL) {
682 /* Find a lock context with requests */
683 lock_ctx = ctdb->lock_pending;
684 while (lock_ctx != NULL) {
685 next_ctx = lock_ctx->next;
686 if (! lock_ctx->request) {
687 DEBUG(DEBUG_INFO, ("Removing lock context without lock request\n"));
688 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
689 CTDB_DECREMENT_STAT(ctdb, locks.num_pending);
690 if (lock_ctx->ctdb_db) {
691 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
693 talloc_free(lock_ctx);
695 if (lock_ctx->ctdb_db == NULL ||
696 lock_ctx->ctdb_db->lock_num_current < ctdb->tunable.lock_processes_per_db) {
697 /* Found a lock context with lock requests */
704 if (lock_ctx == NULL) {
708 lock_ctx->child = -1;
709 ret = pipe(lock_ctx->fd);
711 DEBUG(DEBUG_ERR, ("Failed to create pipe in ctdb_lock_schedule\n"));
715 set_close_on_exec(lock_ctx->fd[0]);
717 /* Create data for child process */
718 tmp_ctx = talloc_new(lock_ctx);
719 if (tmp_ctx == NULL) {
720 DEBUG(DEBUG_ERR, ("Failed to allocate memory for helper args\n"));
721 close(lock_ctx->fd[0]);
722 close(lock_ctx->fd[1]);
726 /* Create arguments for lock helper */
727 args = lock_helper_args(tmp_ctx, lock_ctx, lock_ctx->fd[1]);
729 DEBUG(DEBUG_ERR, ("Failed to create lock helper args\n"));
730 close(lock_ctx->fd[0]);
731 close(lock_ctx->fd[1]);
732 talloc_free(tmp_ctx);
736 lock_ctx->child = vfork();
738 if (lock_ctx->child == (pid_t)-1) {
739 DEBUG(DEBUG_ERR, ("Failed to create a child in ctdb_lock_schedule\n"));
740 close(lock_ctx->fd[0]);
741 close(lock_ctx->fd[1]);
742 talloc_free(tmp_ctx);
748 if (lock_ctx->child == 0) {
749 ret = execv(prog, args);
751 DEBUG(DEBUG_ERR, ("Failed to execute helper %s (%d, %s)\n",
752 prog, errno, strerror(errno)));
758 ctdb_track_child(ctdb, lock_ctx->child);
759 close(lock_ctx->fd[1]);
761 talloc_set_destructor(lock_ctx, ctdb_lock_context_destructor);
763 talloc_free(tmp_ctx);
765 /* Set up timeout handler */
766 lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
768 timeval_current_ofs(10, 0),
769 ctdb_lock_timeout_handler,
771 if (lock_ctx->ttimer == NULL) {
772 ctdb_kill(ctdb, lock_ctx->child, SIGKILL);
773 lock_ctx->child = -1;
774 talloc_set_destructor(lock_ctx, NULL);
775 close(lock_ctx->fd[0]);
779 /* Set up callback */
780 lock_ctx->tfd = tevent_add_fd(ctdb->ev,
786 if (lock_ctx->tfd == NULL) {
787 TALLOC_FREE(lock_ctx->ttimer);
788 ctdb_kill(ctdb, lock_ctx->child, SIGKILL);
789 lock_ctx->child = -1;
790 talloc_set_destructor(lock_ctx, NULL);
791 close(lock_ctx->fd[0]);
794 tevent_fd_set_auto_close(lock_ctx->tfd);
796 /* Move the context from pending to current */
797 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
798 DLIST_ADD_END(ctdb->lock_current, lock_ctx, NULL);
799 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_pending);
800 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_current);
801 if (lock_ctx->ctdb_db) {
802 lock_ctx->ctdb_db->lock_num_current++;
803 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
804 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_current);
810 * Lock record / db depending on type
812 static struct lock_request *ctdb_lock_internal(struct ctdb_context *ctdb,
813 struct ctdb_db_context *ctdb_db,
816 void (*callback)(void *, bool),
821 struct lock_context *lock_ctx = NULL;
822 struct lock_request *request;
824 if (callback == NULL) {
825 DEBUG(DEBUG_WARNING, ("No callback function specified, not locking\n"));
829 lock_ctx = talloc_zero(ctdb, struct lock_context);
830 if (lock_ctx == NULL) {
831 DEBUG(DEBUG_ERR, ("Failed to create a new lock context\n"));
835 if ((request = talloc_zero(lock_ctx, struct lock_request)) == NULL) {
836 talloc_free(lock_ctx);
840 lock_ctx->type = type;
841 lock_ctx->ctdb = ctdb;
842 lock_ctx->ctdb_db = ctdb_db;
843 lock_ctx->key.dsize = key.dsize;
845 lock_ctx->key.dptr = talloc_memdup(lock_ctx, key.dptr, key.dsize);
846 if (lock_ctx->key.dptr == NULL) {
847 DEBUG(DEBUG_ERR, (__location__ "Memory allocation error\n"));
848 talloc_free(lock_ctx);
851 lock_ctx->key_hash = ctdb_hash(&key);
853 lock_ctx->key.dptr = NULL;
855 lock_ctx->priority = priority;
856 lock_ctx->auto_mark = auto_mark;
858 lock_ctx->request = request;
859 lock_ctx->child = -1;
861 /* Non-record locks are required by recovery and should be scheduled
862 * immediately, so keep them at the head of the pending queue.
864 if (lock_ctx->type == LOCK_RECORD) {
865 DLIST_ADD_END(ctdb->lock_pending, lock_ctx, NULL);
867 DLIST_ADD(ctdb->lock_pending, lock_ctx);
869 CTDB_INCREMENT_STAT(ctdb, locks.num_pending);
871 CTDB_INCREMENT_DB_STAT(ctdb_db, locks.num_pending);
874 /* Start the timer when we activate the context */
875 lock_ctx->start_time = timeval_current();
877 request->lctx = lock_ctx;
878 request->callback = callback;
879 request->private_data = private_data;
881 talloc_set_destructor(request, ctdb_lock_request_destructor);
883 ctdb_lock_schedule(ctdb);
890 * obtain a lock on a record in a database
892 struct lock_request *ctdb_lock_record(struct ctdb_db_context *ctdb_db,
895 void (*callback)(void *, bool),
898 return ctdb_lock_internal(ctdb_db->ctdb,
910 * obtain a lock on a database
912 struct lock_request *ctdb_lock_db(struct ctdb_db_context *ctdb_db,
914 void (*callback)(void *, bool),
917 return ctdb_lock_internal(ctdb_db->ctdb,
929 * obtain locks on all databases of specified priority
931 struct lock_request *ctdb_lock_alldb_prio(struct ctdb_context *ctdb,
934 void (*callback)(void *, bool),
937 if (priority < 1 || priority > NUM_DB_PRIORITIES) {
938 DEBUG(DEBUG_ERR, ("Invalid db priority: %u\n", priority));
942 return ctdb_lock_internal(ctdb,
954 * obtain locks on all databases
956 struct lock_request *ctdb_lock_alldb(struct ctdb_context *ctdb,
958 void (*callback)(void *, bool),
961 return ctdb_lock_internal(ctdb,