autobuild: Use make -j on samba-libs/samba-static build as well
[Samba.git] / ctdb / server / ctdb_lock.c
blob78ba35c78a6d90f60bfec6eb232f081065ab6de0
1 /*
2 ctdb lock handling
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/>.
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
24 #include <talloc.h>
25 #include <tevent.h>
27 #include "lib/tdb_wrap/tdb_wrap.h"
28 #include "lib/util/dlinklist.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/samba_util.h"
32 #include "ctdb_private.h"
34 #include "common/system.h"
35 #include "common/common.h"
36 #include "common/logging.h"
39 * Non-blocking Locking API
41 * 1. Create a child process to do blocking locks.
42 * 2. Once the locks are obtained, signal parent process via fd.
43 * 3. Invoke registered callback routine with locking status.
44 * 4. If the child process cannot get locks within certain time,
45 * execute an external script to debug.
47 * ctdb_lock_record() - get a lock on a record
48 * ctdb_lock_db() - get a lock on a DB
49 * ctdb_lock_alldb_prio() - get a lock on all DBs with given priority
50 * ctdb_lock_alldb() - get a lock on all DBs
52 * auto_mark - whether to mark/unmark DBs in before/after callback
53 * = false is used for freezing databases for
54 * recovery since the recovery cannot start till
55 * databases are locked on all the nodes.
56 * = true is used for record locks.
59 enum lock_type {
60 LOCK_RECORD,
61 LOCK_DB,
62 LOCK_ALLDB_PRIO,
63 LOCK_ALLDB,
66 static const char * const lock_type_str[] = {
67 "lock_record",
68 "lock_db",
69 "lock_alldb_prio",
70 "lock_alldb",
73 struct lock_request;
75 /* lock_context is the common part for a lock request */
76 struct lock_context {
77 struct lock_context *next, *prev;
78 enum lock_type type;
79 struct ctdb_context *ctdb;
80 struct ctdb_db_context *ctdb_db;
81 TDB_DATA key;
82 uint32_t priority;
83 bool auto_mark;
84 struct lock_request *request;
85 pid_t child;
86 int fd[2];
87 struct tevent_fd *tfd;
88 struct tevent_timer *ttimer;
89 struct timeval start_time;
90 uint32_t key_hash;
91 bool can_schedule;
94 /* lock_request is the client specific part for a lock request */
95 struct lock_request {
96 struct lock_context *lctx;
97 void (*callback)(void *, bool);
98 void *private_data;
103 * Support samba 3.6.x (and older) versions which do not set db priority.
105 * By default, all databases are set to priority 1. So only when priority
106 * is set to 1, check for databases that need higher priority.
108 static bool later_db(struct ctdb_context *ctdb, const char *name)
110 if (ctdb->tunable.samba3_hack == 0) {
111 return false;
114 if (strstr(name, "brlock") ||
115 strstr(name, "g_lock") ||
116 strstr(name, "notify_onelevel") ||
117 strstr(name, "serverid") ||
118 strstr(name, "xattr_tdb")) {
119 return true;
122 return false;
125 int ctdb_db_prio_iterator(struct ctdb_context *ctdb, uint32_t priority,
126 ctdb_db_handler_t handler, void *private_data)
128 struct ctdb_db_context *ctdb_db;
129 int ret;
131 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
132 if (ctdb_db->priority != priority) {
133 continue;
135 if (later_db(ctdb, ctdb_db->db_name)) {
136 continue;
138 ret = handler(ctdb_db, private_data);
139 if (ret != 0) {
140 return -1;
144 /* If priority != 1, later_db check is not required and can return */
145 if (priority != 1) {
146 return 0;
149 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
150 if (!later_db(ctdb, ctdb_db->db_name)) {
151 continue;
153 ret = handler(ctdb_db, private_data);
154 if (ret != 0) {
155 return -1;
159 return 0;
162 int ctdb_db_iterator(struct ctdb_context *ctdb, ctdb_db_handler_t handler,
163 void *private_data)
165 struct ctdb_db_context *ctdb_db;
166 int ret;
168 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
169 ret = handler(ctdb_db, private_data);
170 if (ret != 0) {
171 return -1;
175 return 0;
179 * lock all databases - mark only
181 static int db_lock_mark_handler(struct ctdb_db_context *ctdb_db,
182 void *private_data)
184 int tdb_transaction_write_lock_mark(struct tdb_context *);
186 DEBUG(DEBUG_INFO, ("marking locked database %s\n", ctdb_db->db_name));
188 if (tdb_transaction_write_lock_mark(ctdb_db->ltdb->tdb) != 0) {
189 DEBUG(DEBUG_ERR, ("Failed to mark (transaction lock) database %s\n",
190 ctdb_db->db_name));
191 return -1;
194 if (tdb_lockall_mark(ctdb_db->ltdb->tdb) != 0) {
195 DEBUG(DEBUG_ERR, ("Failed to mark (all lock) database %s\n",
196 ctdb_db->db_name));
197 return -1;
200 return 0;
203 int ctdb_lockdb_mark(struct ctdb_db_context *ctdb_db)
205 if (!ctdb_db_frozen(ctdb_db)) {
206 DEBUG(DEBUG_ERR,
207 ("Attempt to mark database locked when not frozen\n"));
208 return -1;
211 return db_lock_mark_handler(ctdb_db, NULL);
214 int ctdb_lockall_mark_prio(struct ctdb_context *ctdb, uint32_t priority)
217 * This function is only used by the main dameon during recovery.
218 * At this stage, the databases have already been locked, by a
219 * dedicated child process.
222 if (!ctdb_db_prio_frozen(ctdb, priority)) {
223 DEBUG(DEBUG_ERR, ("Attempt to mark all databases locked when not frozen\n"));
224 return -1;
227 return ctdb_db_prio_iterator(ctdb, priority, db_lock_mark_handler, NULL);
230 static int ctdb_lockall_mark(struct ctdb_context *ctdb)
232 uint32_t priority;
234 for (priority=1; priority<=NUM_DB_PRIORITIES; priority++) {
235 int ret;
237 ret = ctdb_db_prio_iterator(ctdb, priority,
238 db_lock_mark_handler, NULL);
239 if (ret != 0) {
240 return -1;
244 return 0;
249 * lock all databases - unmark only
251 static int db_lock_unmark_handler(struct ctdb_db_context *ctdb_db,
252 void *private_data)
254 int tdb_transaction_write_lock_unmark(struct tdb_context *);
256 DEBUG(DEBUG_INFO, ("unmarking locked database %s\n", ctdb_db->db_name));
258 if (tdb_transaction_write_lock_unmark(ctdb_db->ltdb->tdb) != 0) {
259 DEBUG(DEBUG_ERR, ("Failed to unmark (transaction lock) database %s\n",
260 ctdb_db->db_name));
261 return -1;
264 if (tdb_lockall_unmark(ctdb_db->ltdb->tdb) != 0) {
265 DEBUG(DEBUG_ERR, ("Failed to unmark (all lock) database %s\n",
266 ctdb_db->db_name));
267 return -1;
270 return 0;
273 int ctdb_lockdb_unmark(struct ctdb_db_context *ctdb_db)
275 if (!ctdb_db_frozen(ctdb_db)) {
276 DEBUG(DEBUG_ERR,
277 ("Attempt to unmark database locked when not frozen\n"));
278 return -1;
281 return db_lock_unmark_handler(ctdb_db, NULL);
284 int ctdb_lockall_unmark_prio(struct ctdb_context *ctdb, uint32_t priority)
287 * This function is only used by the main daemon during recovery.
288 * At this stage, the databases have already been locked, by a
289 * dedicated child process.
292 if (!ctdb_db_prio_frozen(ctdb, priority)) {
293 DEBUG(DEBUG_ERR, ("Attempt to unmark all databases locked when not frozen\n"));
294 return -1;
297 return ctdb_db_prio_iterator(ctdb, priority, db_lock_unmark_handler,
298 NULL);
301 static int ctdb_lockall_unmark(struct ctdb_context *ctdb)
303 uint32_t priority;
305 for (priority=NUM_DB_PRIORITIES; priority>0; priority--) {
306 int ret;
308 ret = ctdb_db_prio_iterator(ctdb, priority,
309 db_lock_unmark_handler, NULL);
310 if (ret != 0) {
311 return -1;
315 return 0;
319 static void ctdb_lock_schedule(struct ctdb_context *ctdb);
322 * Destructor to kill the child locking process
324 static int ctdb_lock_context_destructor(struct lock_context *lock_ctx)
326 if (lock_ctx->request) {
327 lock_ctx->request->lctx = NULL;
329 if (lock_ctx->child > 0) {
330 ctdb_kill(lock_ctx->ctdb, lock_ctx->child, SIGKILL);
331 if (lock_ctx->type == LOCK_RECORD) {
332 DLIST_REMOVE(lock_ctx->ctdb_db->lock_current, lock_ctx);
333 } else {
334 DLIST_REMOVE(lock_ctx->ctdb->lock_current, lock_ctx);
336 if (lock_ctx->ctdb_db) {
337 lock_ctx->ctdb_db->lock_num_current--;
339 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_current);
340 if (lock_ctx->ctdb_db) {
341 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_current);
343 } else {
344 if (lock_ctx->type == LOCK_RECORD) {
345 DLIST_REMOVE(lock_ctx->ctdb_db->lock_pending, lock_ctx);
346 } else {
347 DLIST_REMOVE(lock_ctx->ctdb->lock_pending, lock_ctx);
349 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_pending);
350 if (lock_ctx->ctdb_db) {
351 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
355 ctdb_lock_schedule(lock_ctx->ctdb);
357 return 0;
362 * Destructor to remove lock request
364 static int ctdb_lock_request_destructor(struct lock_request *lock_request)
366 if (lock_request->lctx == NULL) {
367 return 0;
370 lock_request->lctx->request = NULL;
371 TALLOC_FREE(lock_request->lctx);
373 return 0;
377 * Process all the callbacks waiting for lock
379 * If lock has failed, callback is executed with locked=false
381 static void process_callbacks(struct lock_context *lock_ctx, bool locked)
383 struct lock_request *request;
384 bool auto_mark = lock_ctx->auto_mark;
386 if (auto_mark && locked) {
387 switch (lock_ctx->type) {
388 case LOCK_RECORD:
389 tdb_chainlock_mark(lock_ctx->ctdb_db->ltdb->tdb, lock_ctx->key);
390 break;
392 case LOCK_DB:
393 ctdb_lockdb_mark(lock_ctx->ctdb_db);
394 break;
396 case LOCK_ALLDB_PRIO:
397 ctdb_lockall_mark_prio(lock_ctx->ctdb, lock_ctx->priority);
398 break;
400 case LOCK_ALLDB:
401 ctdb_lockall_mark(lock_ctx->ctdb);
402 break;
406 request = lock_ctx->request;
407 if (auto_mark) {
408 /* Since request may be freed in the callback, unset the lock
409 * context, so request destructor will not free lock context.
411 request->lctx = NULL;
414 /* Since request may be freed in the callback, unset the request */
415 lock_ctx->request = NULL;
417 request->callback(request->private_data, locked);
419 if (!auto_mark) {
420 return;
423 if (locked) {
424 switch (lock_ctx->type) {
425 case LOCK_RECORD:
426 tdb_chainlock_unmark(lock_ctx->ctdb_db->ltdb->tdb, lock_ctx->key);
427 break;
429 case LOCK_DB:
430 ctdb_lockdb_unmark(lock_ctx->ctdb_db);
431 break;
433 case LOCK_ALLDB_PRIO:
434 ctdb_lockall_unmark_prio(lock_ctx->ctdb, lock_ctx->priority);
435 break;
437 case LOCK_ALLDB:
438 ctdb_lockall_unmark(lock_ctx->ctdb);
439 break;
443 talloc_free(lock_ctx);
447 static int lock_bucket_id(double t)
449 double ms = 1.e-3, s = 1;
450 int id;
452 if (t < 1*ms) {
453 id = 0;
454 } else if (t < 10*ms) {
455 id = 1;
456 } else if (t < 100*ms) {
457 id = 2;
458 } else if (t < 1*s) {
459 id = 3;
460 } else if (t < 2*s) {
461 id = 4;
462 } else if (t < 4*s) {
463 id = 5;
464 } else if (t < 8*s) {
465 id = 6;
466 } else if (t < 16*s) {
467 id = 7;
468 } else if (t < 32*s) {
469 id = 8;
470 } else if (t < 64*s) {
471 id = 9;
472 } else {
473 id = 10;
476 return id;
480 * Callback routine when the required locks are obtained.
481 * Called from parent context
483 static void ctdb_lock_handler(struct tevent_context *ev,
484 struct tevent_fd *tfd,
485 uint16_t flags,
486 void *private_data)
488 struct lock_context *lock_ctx;
489 char c;
490 bool locked;
491 double t;
492 int id;
494 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
496 /* cancel the timeout event */
497 TALLOC_FREE(lock_ctx->ttimer);
499 t = timeval_elapsed(&lock_ctx->start_time);
500 id = lock_bucket_id(t);
502 /* Read the status from the child process */
503 if (sys_read(lock_ctx->fd[0], &c, 1) != 1) {
504 locked = false;
505 } else {
506 locked = (c == 0 ? true : false);
509 /* Update statistics */
510 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_calls);
511 if (lock_ctx->ctdb_db) {
512 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_calls);
515 if (locked) {
516 if (lock_ctx->ctdb_db) {
517 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.buckets[id]);
518 CTDB_UPDATE_LATENCY(lock_ctx->ctdb, lock_ctx->ctdb_db,
519 lock_type_str[lock_ctx->type], locks.latency,
520 lock_ctx->start_time);
522 CTDB_UPDATE_DB_LATENCY(lock_ctx->ctdb_db, lock_type_str[lock_ctx->type], locks.latency, t);
523 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.buckets[id]);
525 } else {
526 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_failed);
527 if (lock_ctx->ctdb_db) {
528 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_failed);
532 process_callbacks(lock_ctx, locked);
537 * Callback routine when required locks are not obtained within timeout
538 * Called from parent context
540 static void ctdb_lock_timeout_handler(struct tevent_context *ev,
541 struct tevent_timer *ttimer,
542 struct timeval current_time,
543 void *private_data)
545 static char debug_locks[PATH_MAX+1] = "";
546 struct lock_context *lock_ctx;
547 struct ctdb_context *ctdb;
548 pid_t pid;
549 double elapsed_time;
550 int new_timer;
552 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
553 ctdb = lock_ctx->ctdb;
555 /* If a node stopped/banned, don't spam the logs */
556 if (ctdb->nodes[ctdb->pnn]->flags & NODE_FLAGS_INACTIVE) {
557 lock_ctx->ttimer = NULL;
558 return;
561 elapsed_time = timeval_elapsed(&lock_ctx->start_time);
562 if (lock_ctx->ctdb_db) {
563 DEBUG(DEBUG_WARNING,
564 ("Unable to get %s lock on database %s for %.0lf seconds\n",
565 (lock_ctx->type == LOCK_RECORD ? "RECORD" : "DB"),
566 lock_ctx->ctdb_db->db_name, elapsed_time));
567 } else {
568 DEBUG(DEBUG_WARNING,
569 ("Unable to get ALLDB locks for %.0lf seconds\n",
570 elapsed_time));
573 if (ctdb_set_helper("lock debugging helper",
574 debug_locks, sizeof(debug_locks),
575 "CTDB_DEBUG_LOCKS",
576 getenv("CTDB_BASE"), "debug_locks.sh")) {
577 pid = vfork();
578 if (pid == 0) {
579 execl(debug_locks, debug_locks, NULL);
580 _exit(0);
582 ctdb_track_child(ctdb, pid);
583 } else {
584 DEBUG(DEBUG_WARNING,
585 (__location__
586 " Unable to setup lock debugging\n"));
589 /* Back-off logging if lock is not obtained for a long time */
590 if (elapsed_time < 100.0) {
591 new_timer = 10;
592 } else if (elapsed_time < 1000.0) {
593 new_timer = 100;
594 } else {
595 new_timer = 1000;
598 /* reset the timeout timer */
599 // talloc_free(lock_ctx->ttimer);
600 lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
601 lock_ctx,
602 timeval_current_ofs(new_timer, 0),
603 ctdb_lock_timeout_handler,
604 (void *)lock_ctx);
608 static int db_count_handler(struct ctdb_db_context *ctdb_db, void *private_data)
610 int *count = (int *)private_data;
612 (*count) += 2;
614 return 0;
617 static int db_flags(struct ctdb_db_context *ctdb_db)
619 int tdb_flags = TDB_DEFAULT;
621 #ifdef TDB_MUTEX_LOCKING
622 if (!ctdb_db->persistent && ctdb_db->ctdb->tunable.mutex_enabled) {
623 tdb_flags = (TDB_MUTEX_LOCKING | TDB_CLEAR_IF_FIRST);
625 #endif
626 return tdb_flags;
629 struct db_namelist {
630 const char **names;
631 int n;
634 static int db_name_handler(struct ctdb_db_context *ctdb_db, void *private_data)
636 struct db_namelist *list = (struct db_namelist *)private_data;
638 list->names[list->n] = talloc_strdup(list->names, ctdb_db->db_path);
639 list->names[list->n+1] = talloc_asprintf(list->names, "0x%x",
640 db_flags(ctdb_db));
641 list->n += 2;
643 return 0;
646 static bool lock_helper_args(TALLOC_CTX *mem_ctx,
647 struct lock_context *lock_ctx, int fd,
648 int *argc, const char ***argv)
650 struct ctdb_context *ctdb = lock_ctx->ctdb;
651 const char **args = NULL;
652 int nargs, i;
653 int priority;
654 struct db_namelist list;
656 switch (lock_ctx->type) {
657 case LOCK_RECORD:
658 nargs = 6;
659 break;
661 case LOCK_DB:
662 nargs = 5;
663 break;
665 case LOCK_ALLDB_PRIO:
666 nargs = 3;
667 ctdb_db_prio_iterator(ctdb, lock_ctx->priority,
668 db_count_handler, &nargs);
669 break;
671 case LOCK_ALLDB:
672 nargs = 3;
673 for (priority=1; priority<NUM_DB_PRIORITIES; priority++) {
674 ctdb_db_prio_iterator(ctdb, priority,
675 db_count_handler, &nargs);
677 break;
680 /* Add extra argument for null termination */
681 nargs++;
683 args = talloc_array(mem_ctx, const char *, nargs);
684 if (args == NULL) {
685 return false;
688 args[0] = talloc_asprintf(args, "%d", getpid());
689 args[1] = talloc_asprintf(args, "%d", fd);
691 switch (lock_ctx->type) {
692 case LOCK_RECORD:
693 args[2] = talloc_strdup(args, "RECORD");
694 args[3] = talloc_strdup(args, lock_ctx->ctdb_db->db_path);
695 args[4] = talloc_asprintf(args, "0x%x",
696 db_flags(lock_ctx->ctdb_db));
697 if (lock_ctx->key.dsize == 0) {
698 args[5] = talloc_strdup(args, "NULL");
699 } else {
700 args[5] = hex_encode_talloc(args, lock_ctx->key.dptr, lock_ctx->key.dsize);
702 break;
704 case LOCK_DB:
705 args[2] = talloc_strdup(args, "DB");
706 args[3] = talloc_strdup(args, lock_ctx->ctdb_db->db_path);
707 args[4] = talloc_asprintf(args, "0x%x",
708 db_flags(lock_ctx->ctdb_db));
709 break;
711 case LOCK_ALLDB_PRIO:
712 args[2] = talloc_strdup(args, "DB");
713 list.names = args;
714 list.n = 3;
715 ctdb_db_prio_iterator(ctdb, lock_ctx->priority,
716 db_name_handler, &list);
717 break;
719 case LOCK_ALLDB:
720 args[2] = talloc_strdup(args, "DB");
721 list.names = args;
722 list.n = 3;
723 for (priority=1; priority<NUM_DB_PRIORITIES; priority++) {
724 ctdb_db_prio_iterator(ctdb, priority,
725 db_name_handler, &list);
727 break;
730 /* Make sure last argument is NULL */
731 args[nargs-1] = NULL;
733 for (i=0; i<nargs-1; i++) {
734 if (args[i] == NULL) {
735 talloc_free(args);
736 return false;
740 *argc = nargs;
741 *argv = args;
742 return true;
746 * Find a lock request that can be scheduled
748 static struct lock_context *ctdb_find_lock_context(struct ctdb_context *ctdb)
750 struct lock_context *lock_ctx, *next_ctx;
751 struct ctdb_db_context *ctdb_db;
753 /* First check if there are database lock requests */
755 for (lock_ctx = ctdb->lock_pending; lock_ctx != NULL;
756 lock_ctx = next_ctx) {
758 if (lock_ctx->request != NULL) {
759 /* Found a lock context with a request */
760 return lock_ctx;
763 next_ctx = lock_ctx->next;
765 DEBUG(DEBUG_INFO, ("Removing lock context without lock "
766 "request\n"));
767 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
768 CTDB_DECREMENT_STAT(ctdb, locks.num_pending);
769 if (lock_ctx->ctdb_db) {
770 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db,
771 locks.num_pending);
773 talloc_free(lock_ctx);
776 /* Next check database queues */
777 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
778 if (ctdb_db->lock_num_current ==
779 ctdb->tunable.lock_processes_per_db) {
780 continue;
783 for (lock_ctx = ctdb_db->lock_pending; lock_ctx != NULL;
784 lock_ctx = next_ctx) {
786 next_ctx = lock_ctx->next;
788 if (lock_ctx->request != NULL) {
789 return lock_ctx;
792 DEBUG(DEBUG_INFO, ("Removing lock context without "
793 "lock request\n"));
794 DLIST_REMOVE(ctdb_db->lock_pending, lock_ctx);
795 CTDB_DECREMENT_STAT(ctdb, locks.num_pending);
796 CTDB_DECREMENT_DB_STAT(ctdb_db, locks.num_pending);
797 talloc_free(lock_ctx);
801 return NULL;
805 * Schedule a new lock child process
806 * Set up callback handler and timeout handler
808 static void ctdb_lock_schedule(struct ctdb_context *ctdb)
810 struct lock_context *lock_ctx;
811 int ret, argc;
812 TALLOC_CTX *tmp_ctx;
813 static char prog[PATH_MAX+1] = "";
814 const char **args;
816 if (!ctdb_set_helper("lock helper",
817 prog, sizeof(prog),
818 "CTDB_LOCK_HELPER",
819 CTDB_HELPER_BINDIR, "ctdb_lock_helper")) {
820 ctdb_die(ctdb, __location__
821 " Unable to set lock helper\n");
824 /* Find a lock context with requests */
825 lock_ctx = ctdb_find_lock_context(ctdb);
826 if (lock_ctx == NULL) {
827 return;
830 lock_ctx->child = -1;
831 ret = pipe(lock_ctx->fd);
832 if (ret != 0) {
833 DEBUG(DEBUG_ERR, ("Failed to create pipe in ctdb_lock_schedule\n"));
834 return;
837 set_close_on_exec(lock_ctx->fd[0]);
839 /* Create data for child process */
840 tmp_ctx = talloc_new(lock_ctx);
841 if (tmp_ctx == NULL) {
842 DEBUG(DEBUG_ERR, ("Failed to allocate memory for helper args\n"));
843 close(lock_ctx->fd[0]);
844 close(lock_ctx->fd[1]);
845 return;
848 /* Create arguments for lock helper */
849 if (!lock_helper_args(tmp_ctx, lock_ctx, lock_ctx->fd[1],
850 &argc, &args)) {
851 DEBUG(DEBUG_ERR, ("Failed to create lock helper args\n"));
852 close(lock_ctx->fd[0]);
853 close(lock_ctx->fd[1]);
854 talloc_free(tmp_ctx);
855 return;
858 if (!ctdb_vfork_with_logging(lock_ctx, ctdb, "lock_helper",
859 prog, argc, (const char **)args,
860 NULL, NULL, &lock_ctx->child)) {
861 DEBUG(DEBUG_ERR, ("Failed to create a child in ctdb_lock_schedule\n"));
862 close(lock_ctx->fd[0]);
863 close(lock_ctx->fd[1]);
864 talloc_free(tmp_ctx);
865 return;
868 /* Parent process */
869 close(lock_ctx->fd[1]);
871 talloc_free(tmp_ctx);
873 /* Set up timeout handler */
874 lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
875 lock_ctx,
876 timeval_current_ofs(10, 0),
877 ctdb_lock_timeout_handler,
878 (void *)lock_ctx);
879 if (lock_ctx->ttimer == NULL) {
880 ctdb_kill(ctdb, lock_ctx->child, SIGKILL);
881 lock_ctx->child = -1;
882 close(lock_ctx->fd[0]);
883 return;
886 /* Set up callback */
887 lock_ctx->tfd = tevent_add_fd(ctdb->ev,
888 lock_ctx,
889 lock_ctx->fd[0],
890 TEVENT_FD_READ,
891 ctdb_lock_handler,
892 (void *)lock_ctx);
893 if (lock_ctx->tfd == NULL) {
894 TALLOC_FREE(lock_ctx->ttimer);
895 ctdb_kill(ctdb, lock_ctx->child, SIGKILL);
896 lock_ctx->child = -1;
897 close(lock_ctx->fd[0]);
898 return;
900 tevent_fd_set_auto_close(lock_ctx->tfd);
902 /* Move the context from pending to current */
903 if (lock_ctx->type == LOCK_RECORD) {
904 DLIST_REMOVE(lock_ctx->ctdb_db->lock_pending, lock_ctx);
905 DLIST_ADD_END(lock_ctx->ctdb_db->lock_current, lock_ctx, NULL);
906 } else {
907 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
908 DLIST_ADD_END(ctdb->lock_current, lock_ctx, NULL);
910 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_pending);
911 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_current);
912 if (lock_ctx->ctdb_db) {
913 lock_ctx->ctdb_db->lock_num_current++;
914 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
915 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_current);
921 * Lock record / db depending on type
923 static struct lock_request *ctdb_lock_internal(TALLOC_CTX *mem_ctx,
924 struct ctdb_context *ctdb,
925 struct ctdb_db_context *ctdb_db,
926 TDB_DATA key,
927 uint32_t priority,
928 void (*callback)(void *, bool),
929 void *private_data,
930 enum lock_type type,
931 bool auto_mark)
933 struct lock_context *lock_ctx = NULL;
934 struct lock_request *request;
936 if (callback == NULL) {
937 DEBUG(DEBUG_WARNING, ("No callback function specified, not locking\n"));
938 return NULL;
941 lock_ctx = talloc_zero(ctdb, struct lock_context);
942 if (lock_ctx == NULL) {
943 DEBUG(DEBUG_ERR, ("Failed to create a new lock context\n"));
944 return NULL;
947 if ((request = talloc_zero(mem_ctx, struct lock_request)) == NULL) {
948 talloc_free(lock_ctx);
949 return NULL;
952 lock_ctx->type = type;
953 lock_ctx->ctdb = ctdb;
954 lock_ctx->ctdb_db = ctdb_db;
955 lock_ctx->key.dsize = key.dsize;
956 if (key.dsize > 0) {
957 lock_ctx->key.dptr = talloc_memdup(lock_ctx, key.dptr, key.dsize);
958 if (lock_ctx->key.dptr == NULL) {
959 DEBUG(DEBUG_ERR, (__location__ "Memory allocation error\n"));
960 talloc_free(lock_ctx);
961 talloc_free(request);
962 return NULL;
964 lock_ctx->key_hash = ctdb_hash(&key);
965 } else {
966 lock_ctx->key.dptr = NULL;
968 lock_ctx->priority = priority;
969 lock_ctx->auto_mark = auto_mark;
971 lock_ctx->request = request;
972 lock_ctx->child = -1;
974 /* Non-record locks are required by recovery and should be scheduled
975 * immediately, so keep them at the head of the pending queue.
977 if (lock_ctx->type == LOCK_RECORD) {
978 DLIST_ADD_END(ctdb_db->lock_pending, lock_ctx, NULL);
979 } else {
980 DLIST_ADD_END(ctdb->lock_pending, lock_ctx, NULL);
982 CTDB_INCREMENT_STAT(ctdb, locks.num_pending);
983 if (ctdb_db) {
984 CTDB_INCREMENT_DB_STAT(ctdb_db, locks.num_pending);
987 /* Start the timer when we activate the context */
988 lock_ctx->start_time = timeval_current();
990 request->lctx = lock_ctx;
991 request->callback = callback;
992 request->private_data = private_data;
994 talloc_set_destructor(request, ctdb_lock_request_destructor);
995 talloc_set_destructor(lock_ctx, ctdb_lock_context_destructor);
997 ctdb_lock_schedule(ctdb);
999 return request;
1004 * obtain a lock on a record in a database
1006 struct lock_request *ctdb_lock_record(TALLOC_CTX *mem_ctx,
1007 struct ctdb_db_context *ctdb_db,
1008 TDB_DATA key,
1009 bool auto_mark,
1010 void (*callback)(void *, bool),
1011 void *private_data)
1013 return ctdb_lock_internal(mem_ctx,
1014 ctdb_db->ctdb,
1015 ctdb_db,
1016 key,
1018 callback,
1019 private_data,
1020 LOCK_RECORD,
1021 auto_mark);
1026 * obtain a lock on a database
1028 struct lock_request *ctdb_lock_db(TALLOC_CTX *mem_ctx,
1029 struct ctdb_db_context *ctdb_db,
1030 bool auto_mark,
1031 void (*callback)(void *, bool),
1032 void *private_data)
1034 return ctdb_lock_internal(mem_ctx,
1035 ctdb_db->ctdb,
1036 ctdb_db,
1037 tdb_null,
1039 callback,
1040 private_data,
1041 LOCK_DB,
1042 auto_mark);
1047 * obtain locks on all databases of specified priority
1049 struct lock_request *ctdb_lock_alldb_prio(TALLOC_CTX *mem_ctx,
1050 struct ctdb_context *ctdb,
1051 uint32_t priority,
1052 bool auto_mark,
1053 void (*callback)(void *, bool),
1054 void *private_data)
1056 if (priority < 1 || priority > NUM_DB_PRIORITIES) {
1057 DEBUG(DEBUG_ERR, ("Invalid db priority: %u\n", priority));
1058 return NULL;
1061 return ctdb_lock_internal(mem_ctx,
1062 ctdb,
1063 NULL,
1064 tdb_null,
1065 priority,
1066 callback,
1067 private_data,
1068 LOCK_ALLDB_PRIO,
1069 auto_mark);
1074 * obtain locks on all databases
1076 struct lock_request *ctdb_lock_alldb(TALLOC_CTX *mem_ctx,
1077 struct ctdb_context *ctdb,
1078 bool auto_mark,
1079 void (*callback)(void *, bool),
1080 void *private_data)
1082 return ctdb_lock_internal(mem_ctx,
1083 ctdb,
1084 NULL,
1085 tdb_null,
1087 callback,
1088 private_data,
1089 LOCK_ALLDB,
1090 auto_mark);