CVE-2016-0771: tests/dns: RPC => DNS roundtrip test
[Samba.git] / ctdb / server / ctdb_lock.c
blob5b63d1eee1c26a52142f25e83c24a8c7ca25cacd
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 "includes.h"
21 #include "include/ctdb_private.h"
22 #include "include/ctdb_protocol.h"
23 #include "tevent.h"
24 #include "tdb.h"
25 #include "lib/tdb_wrap/tdb_wrap.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 * execute an external script to debug.
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
44 * = false is used for freezing databases for
45 * recovery since the recovery cannot start till
46 * databases are locked on all the nodes.
47 * = true is used for record locks.
50 enum lock_type {
51 LOCK_RECORD,
52 LOCK_DB,
53 LOCK_ALLDB_PRIO,
54 LOCK_ALLDB,
57 static const char * const lock_type_str[] = {
58 "lock_record",
59 "lock_db",
60 "lock_alldb_prio",
61 "lock_alldb",
64 struct lock_request;
66 /* lock_context is the common part for a lock request */
67 struct lock_context {
68 struct lock_context *next, *prev;
69 enum lock_type type;
70 struct ctdb_context *ctdb;
71 struct ctdb_db_context *ctdb_db;
72 TDB_DATA key;
73 uint32_t priority;
74 bool auto_mark;
75 struct lock_request *request;
76 pid_t child;
77 int fd[2];
78 struct tevent_fd *tfd;
79 struct tevent_timer *ttimer;
80 struct timeval start_time;
81 uint32_t key_hash;
82 bool can_schedule;
85 /* lock_request is the client specific part for a lock request */
86 struct lock_request {
87 struct lock_context *lctx;
88 void (*callback)(void *, bool);
89 void *private_data;
94 * Support samba 3.6.x (and older) versions which do not set db priority.
96 * By default, all databases are set to priority 1. So only when priority
97 * is set to 1, check for databases that need higher priority.
99 static bool later_db(struct ctdb_context *ctdb, const char *name)
101 if (ctdb->tunable.samba3_hack == 0) {
102 return false;
105 if (strstr(name, "brlock") ||
106 strstr(name, "g_lock") ||
107 strstr(name, "notify_onelevel") ||
108 strstr(name, "serverid") ||
109 strstr(name, "xattr_tdb")) {
110 return true;
113 return false;
116 typedef int (*db_handler_t)(struct ctdb_db_context *ctdb_db,
117 uint32_t priority,
118 void *private_data);
120 static int ctdb_db_iterator(struct ctdb_context *ctdb, uint32_t priority,
121 db_handler_t handler, void *private_data)
123 struct ctdb_db_context *ctdb_db;
124 int ret;
126 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
127 if (ctdb_db->priority != priority) {
128 continue;
130 if (later_db(ctdb, ctdb_db->db_name)) {
131 continue;
133 ret = handler(ctdb_db, priority, private_data);
134 if (ret != 0) {
135 return -1;
139 /* If priority != 1, later_db check is not required and can return */
140 if (priority != 1) {
141 return 0;
144 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
145 if (!later_db(ctdb, ctdb_db->db_name)) {
146 continue;
148 ret = handler(ctdb_db, priority, private_data);
149 if (ret != 0) {
150 return -1;
154 return 0;
159 * lock all databases - mark only
161 static int db_lock_mark_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
162 void *private_data)
164 int tdb_transaction_write_lock_mark(struct tdb_context *);
166 DEBUG(DEBUG_INFO, ("marking locked database %s, priority:%u\n",
167 ctdb_db->db_name, priority));
169 if (tdb_transaction_write_lock_mark(ctdb_db->ltdb->tdb) != 0) {
170 DEBUG(DEBUG_ERR, ("Failed to mark (transaction lock) database %s\n",
171 ctdb_db->db_name));
172 return -1;
175 if (tdb_lockall_mark(ctdb_db->ltdb->tdb) != 0) {
176 DEBUG(DEBUG_ERR, ("Failed to mark (all lock) database %s\n",
177 ctdb_db->db_name));
178 return -1;
181 return 0;
184 int ctdb_lockall_mark_prio(struct ctdb_context *ctdb, uint32_t priority)
187 * This function is only used by the main dameon during recovery.
188 * At this stage, the databases have already been locked, by a
189 * dedicated child process. The freeze_mode variable is used to track
190 * whether the actual locks are held by the child process or not.
193 if (ctdb->freeze_mode[priority] != CTDB_FREEZE_FROZEN) {
194 DEBUG(DEBUG_ERR, ("Attempt to mark all databases locked when not frozen\n"));
195 return -1;
198 return ctdb_db_iterator(ctdb, priority, db_lock_mark_handler, NULL);
201 static int ctdb_lockall_mark(struct ctdb_context *ctdb)
203 uint32_t priority;
205 for (priority=1; priority<=NUM_DB_PRIORITIES; priority++) {
206 if (ctdb_db_iterator(ctdb, priority, db_lock_mark_handler, NULL) != 0) {
207 return -1;
211 return 0;
216 * lock all databases - unmark only
218 static int db_lock_unmark_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
219 void *private_data)
221 int tdb_transaction_write_lock_unmark(struct tdb_context *);
223 DEBUG(DEBUG_INFO, ("unmarking locked database %s, priority:%u\n",
224 ctdb_db->db_name, priority));
226 if (tdb_transaction_write_lock_unmark(ctdb_db->ltdb->tdb) != 0) {
227 DEBUG(DEBUG_ERR, ("Failed to unmark (transaction lock) database %s\n",
228 ctdb_db->db_name));
229 return -1;
232 if (tdb_lockall_unmark(ctdb_db->ltdb->tdb) != 0) {
233 DEBUG(DEBUG_ERR, ("Failed to unmark (all lock) database %s\n",
234 ctdb_db->db_name));
235 return -1;
238 return 0;
241 int ctdb_lockall_unmark_prio(struct ctdb_context *ctdb, uint32_t priority)
244 * This function is only used by the main daemon during recovery.
245 * At this stage, the databases have already been locked, by a
246 * dedicated child process. The freeze_mode variable is used to track
247 * whether the actual locks are held by the child process or not.
250 if (ctdb->freeze_mode[priority] != CTDB_FREEZE_FROZEN) {
251 DEBUG(DEBUG_ERR, ("Attempt to unmark all databases locked when not frozen\n"));
252 return -1;
255 return ctdb_db_iterator(ctdb, priority, db_lock_unmark_handler, NULL);
258 static int ctdb_lockall_unmark(struct ctdb_context *ctdb)
260 uint32_t priority;
262 for (priority=NUM_DB_PRIORITIES; priority>0; priority--) {
263 if (ctdb_db_iterator(ctdb, priority, db_lock_unmark_handler, NULL) != 0) {
264 return -1;
268 return 0;
272 static void ctdb_lock_schedule(struct ctdb_context *ctdb);
275 * Destructor to kill the child locking process
277 static int ctdb_lock_context_destructor(struct lock_context *lock_ctx)
279 if (lock_ctx->request) {
280 lock_ctx->request->lctx = NULL;
282 if (lock_ctx->child > 0) {
283 ctdb_kill(lock_ctx->ctdb, lock_ctx->child, SIGKILL);
284 if (lock_ctx->type == LOCK_RECORD) {
285 DLIST_REMOVE(lock_ctx->ctdb_db->lock_current, lock_ctx);
286 } else {
287 DLIST_REMOVE(lock_ctx->ctdb->lock_current, lock_ctx);
289 if (lock_ctx->ctdb_db) {
290 lock_ctx->ctdb_db->lock_num_current--;
292 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_current);
293 if (lock_ctx->ctdb_db) {
294 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_current);
296 } else {
297 if (lock_ctx->type == LOCK_RECORD) {
298 DLIST_REMOVE(lock_ctx->ctdb_db->lock_pending, lock_ctx);
299 } else {
300 DLIST_REMOVE(lock_ctx->ctdb->lock_pending, lock_ctx);
302 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_pending);
303 if (lock_ctx->ctdb_db) {
304 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
308 ctdb_lock_schedule(lock_ctx->ctdb);
310 return 0;
315 * Destructor to remove lock request
317 static int ctdb_lock_request_destructor(struct lock_request *lock_request)
319 if (lock_request->lctx == NULL) {
320 return 0;
323 lock_request->lctx->request = NULL;
324 TALLOC_FREE(lock_request->lctx);
326 return 0;
330 * Process all the callbacks waiting for lock
332 * If lock has failed, callback is executed with locked=false
334 static void process_callbacks(struct lock_context *lock_ctx, bool locked)
336 struct lock_request *request;
337 bool auto_mark = lock_ctx->auto_mark;
339 if (auto_mark && locked) {
340 switch (lock_ctx->type) {
341 case LOCK_RECORD:
342 tdb_chainlock_mark(lock_ctx->ctdb_db->ltdb->tdb, lock_ctx->key);
343 break;
345 case LOCK_DB:
346 tdb_lockall_mark(lock_ctx->ctdb_db->ltdb->tdb);
347 break;
349 case LOCK_ALLDB_PRIO:
350 ctdb_lockall_mark_prio(lock_ctx->ctdb, lock_ctx->priority);
351 break;
353 case LOCK_ALLDB:
354 ctdb_lockall_mark(lock_ctx->ctdb);
355 break;
359 request = lock_ctx->request;
360 if (auto_mark) {
361 /* Since request may be freed in the callback, unset the lock
362 * context, so request destructor will not free lock context.
364 request->lctx = NULL;
367 /* Since request may be freed in the callback, unset the request */
368 lock_ctx->request = NULL;
370 request->callback(request->private_data, locked);
372 if (!auto_mark) {
373 return;
376 if (locked) {
377 switch (lock_ctx->type) {
378 case LOCK_RECORD:
379 tdb_chainlock_unmark(lock_ctx->ctdb_db->ltdb->tdb, lock_ctx->key);
380 break;
382 case LOCK_DB:
383 tdb_lockall_unmark(lock_ctx->ctdb_db->ltdb->tdb);
384 break;
386 case LOCK_ALLDB_PRIO:
387 ctdb_lockall_unmark_prio(lock_ctx->ctdb, lock_ctx->priority);
388 break;
390 case LOCK_ALLDB:
391 ctdb_lockall_unmark(lock_ctx->ctdb);
392 break;
396 talloc_free(lock_ctx);
400 static int lock_bucket_id(double t)
402 double ms = 1.e-3, s = 1;
403 int id;
405 if (t < 1*ms) {
406 id = 0;
407 } else if (t < 10*ms) {
408 id = 1;
409 } else if (t < 100*ms) {
410 id = 2;
411 } else if (t < 1*s) {
412 id = 3;
413 } else if (t < 2*s) {
414 id = 4;
415 } else if (t < 4*s) {
416 id = 5;
417 } else if (t < 8*s) {
418 id = 6;
419 } else if (t < 16*s) {
420 id = 7;
421 } else if (t < 32*s) {
422 id = 8;
423 } else if (t < 64*s) {
424 id = 9;
425 } else {
426 id = 10;
429 return id;
433 * Callback routine when the required locks are obtained.
434 * Called from parent context
436 static void ctdb_lock_handler(struct tevent_context *ev,
437 struct tevent_fd *tfd,
438 uint16_t flags,
439 void *private_data)
441 struct lock_context *lock_ctx;
442 char c;
443 bool locked;
444 double t;
445 int id;
447 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
449 /* cancel the timeout event */
450 TALLOC_FREE(lock_ctx->ttimer);
452 t = timeval_elapsed(&lock_ctx->start_time);
453 id = lock_bucket_id(t);
455 /* Read the status from the child process */
456 if (sys_read(lock_ctx->fd[0], &c, 1) != 1) {
457 locked = false;
458 } else {
459 locked = (c == 0 ? true : false);
462 /* Update statistics */
463 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_calls);
464 if (lock_ctx->ctdb_db) {
465 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_calls);
468 if (locked) {
469 if (lock_ctx->ctdb_db) {
470 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.buckets[id]);
471 CTDB_UPDATE_LATENCY(lock_ctx->ctdb, lock_ctx->ctdb_db,
472 lock_type_str[lock_ctx->type], locks.latency,
473 lock_ctx->start_time);
475 CTDB_UPDATE_DB_LATENCY(lock_ctx->ctdb_db, lock_type_str[lock_ctx->type], locks.latency, t);
476 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.buckets[id]);
478 } else {
479 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_failed);
480 if (lock_ctx->ctdb_db) {
481 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_failed);
485 process_callbacks(lock_ctx, locked);
490 * Callback routine when required locks are not obtained within timeout
491 * Called from parent context
493 static void ctdb_lock_timeout_handler(struct tevent_context *ev,
494 struct tevent_timer *ttimer,
495 struct timeval current_time,
496 void *private_data)
498 static const char * debug_locks = NULL;
499 struct lock_context *lock_ctx;
500 struct ctdb_context *ctdb;
501 pid_t pid;
503 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
504 ctdb = lock_ctx->ctdb;
506 /* If a node stopped/banned, don't spam the logs */
507 if (ctdb->nodes[ctdb->pnn]->flags & NODE_FLAGS_INACTIVE) {
508 lock_ctx->ttimer = NULL;
509 return;
511 if (lock_ctx->ctdb_db) {
512 DEBUG(DEBUG_WARNING,
513 ("Unable to get %s lock on database %s for %.0lf seconds\n",
514 (lock_ctx->type == LOCK_RECORD ? "RECORD" : "DB"),
515 lock_ctx->ctdb_db->db_name,
516 timeval_elapsed(&lock_ctx->start_time)));
517 } else {
518 DEBUG(DEBUG_WARNING,
519 ("Unable to get ALLDB locks for %.0lf seconds\n",
520 timeval_elapsed(&lock_ctx->start_time)));
523 /* Fire a child process to find the blocking process. */
524 if (debug_locks == NULL) {
525 debug_locks = getenv("CTDB_DEBUG_LOCKS");
526 if (debug_locks == NULL) {
527 debug_locks = talloc_asprintf(ctdb,
528 "%s/debug_locks.sh",
529 getenv("CTDB_BASE"));
532 if (debug_locks != NULL) {
533 pid = vfork();
534 if (pid == 0) {
535 execl(debug_locks, debug_locks, NULL);
536 _exit(0);
538 ctdb_track_child(ctdb, pid);
539 } else {
540 DEBUG(DEBUG_WARNING,
541 (__location__
542 " Unable to setup lock debugging - no memory?\n"));
545 /* reset the timeout timer */
546 // talloc_free(lock_ctx->ttimer);
547 lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
548 lock_ctx,
549 timeval_current_ofs(10, 0),
550 ctdb_lock_timeout_handler,
551 (void *)lock_ctx);
555 static int db_count_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
556 void *private_data)
558 int *count = (int *)private_data;
560 (*count) += 2;
562 return 0;
565 static int db_flags(struct ctdb_db_context *ctdb_db)
567 int tdb_flags = TDB_DEFAULT;
569 #ifdef TDB_MUTEX_LOCKING
570 if (!ctdb_db->persistent && ctdb_db->ctdb->tunable.mutex_enabled) {
571 tdb_flags = (TDB_MUTEX_LOCKING | TDB_CLEAR_IF_FIRST);
573 #endif
574 return tdb_flags;
577 struct db_namelist {
578 const char **names;
579 int n;
582 static int db_name_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
583 void *private_data)
585 struct db_namelist *list = (struct db_namelist *)private_data;
587 list->names[list->n] = talloc_strdup(list->names, ctdb_db->db_path);
588 list->names[list->n+1] = talloc_asprintf(list->names, "0x%x",
589 db_flags(ctdb_db));
590 list->n += 2;
592 return 0;
595 static bool lock_helper_args(TALLOC_CTX *mem_ctx,
596 struct lock_context *lock_ctx, int fd,
597 int *argc, const char ***argv)
599 struct ctdb_context *ctdb = lock_ctx->ctdb;
600 const char **args = NULL;
601 int nargs, i;
602 int priority;
603 struct db_namelist list;
605 switch (lock_ctx->type) {
606 case LOCK_RECORD:
607 nargs = 6;
608 break;
610 case LOCK_DB:
611 nargs = 5;
612 break;
614 case LOCK_ALLDB_PRIO:
615 nargs = 3;
616 ctdb_db_iterator(ctdb, lock_ctx->priority, db_count_handler, &nargs);
617 break;
619 case LOCK_ALLDB:
620 nargs = 3;
621 for (priority=1; priority<NUM_DB_PRIORITIES; priority++) {
622 ctdb_db_iterator(ctdb, priority, db_count_handler, &nargs);
624 break;
627 /* Add extra argument for null termination */
628 nargs++;
630 args = talloc_array(mem_ctx, const char *, nargs);
631 if (args == NULL) {
632 return false;
635 args[0] = talloc_asprintf(args, "%d", getpid());
636 args[1] = talloc_asprintf(args, "%d", fd);
638 switch (lock_ctx->type) {
639 case LOCK_RECORD:
640 args[2] = talloc_strdup(args, "RECORD");
641 args[3] = talloc_strdup(args, lock_ctx->ctdb_db->db_path);
642 args[4] = talloc_asprintf(args, "0x%x",
643 db_flags(lock_ctx->ctdb_db));
644 if (lock_ctx->key.dsize == 0) {
645 args[5] = talloc_strdup(args, "NULL");
646 } else {
647 args[5] = hex_encode_talloc(args, lock_ctx->key.dptr, lock_ctx->key.dsize);
649 break;
651 case LOCK_DB:
652 args[2] = talloc_strdup(args, "DB");
653 args[3] = talloc_strdup(args, lock_ctx->ctdb_db->db_path);
654 args[4] = talloc_asprintf(args, "0x%x",
655 db_flags(lock_ctx->ctdb_db));
656 break;
658 case LOCK_ALLDB_PRIO:
659 args[2] = talloc_strdup(args, "DB");
660 list.names = args;
661 list.n = 3;
662 ctdb_db_iterator(ctdb, lock_ctx->priority, db_name_handler, &list);
663 break;
665 case LOCK_ALLDB:
666 args[2] = talloc_strdup(args, "DB");
667 list.names = args;
668 list.n = 3;
669 for (priority=1; priority<NUM_DB_PRIORITIES; priority++) {
670 ctdb_db_iterator(ctdb, priority, db_name_handler, &list);
672 break;
675 /* Make sure last argument is NULL */
676 args[nargs-1] = NULL;
678 for (i=0; i<nargs-1; i++) {
679 if (args[i] == NULL) {
680 talloc_free(args);
681 return false;
685 *argc = nargs;
686 *argv = args;
687 return true;
691 * Find a lock request that can be scheduled
693 static struct lock_context *ctdb_find_lock_context(struct ctdb_context *ctdb)
695 struct lock_context *lock_ctx, *next_ctx;
696 struct ctdb_db_context *ctdb_db;
698 /* First check if there are database lock requests */
700 for (lock_ctx = ctdb->lock_pending; lock_ctx != NULL;
701 lock_ctx = next_ctx) {
703 if (lock_ctx->request != NULL) {
704 /* Found a lock context with a request */
705 return lock_ctx;
708 next_ctx = lock_ctx->next;
710 DEBUG(DEBUG_INFO, ("Removing lock context without lock "
711 "request\n"));
712 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
713 CTDB_DECREMENT_STAT(ctdb, locks.num_pending);
714 if (lock_ctx->ctdb_db) {
715 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db,
716 locks.num_pending);
718 talloc_free(lock_ctx);
721 /* Next check database queues */
722 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
723 if (ctdb_db->lock_num_current ==
724 ctdb->tunable.lock_processes_per_db) {
725 continue;
728 for (lock_ctx = ctdb_db->lock_pending; lock_ctx != NULL;
729 lock_ctx = next_ctx) {
731 next_ctx = lock_ctx->next;
733 if (lock_ctx->request != NULL) {
734 return lock_ctx;
737 DEBUG(DEBUG_INFO, ("Removing lock context without "
738 "lock request\n"));
739 DLIST_REMOVE(ctdb_db->lock_pending, lock_ctx);
740 CTDB_DECREMENT_STAT(ctdb, locks.num_pending);
741 CTDB_DECREMENT_DB_STAT(ctdb_db, locks.num_pending);
742 talloc_free(lock_ctx);
746 return NULL;
750 * Schedule a new lock child process
751 * Set up callback handler and timeout handler
753 static void ctdb_lock_schedule(struct ctdb_context *ctdb)
755 struct lock_context *lock_ctx;
756 int ret, argc;
757 TALLOC_CTX *tmp_ctx;
758 const char *helper = CTDB_HELPER_BINDIR "/ctdb_lock_helper";
759 static const char *prog = NULL;
760 const char **args;
762 if (prog == NULL) {
763 const char *t;
765 t = getenv("CTDB_LOCK_HELPER");
766 if (t != NULL) {
767 prog = talloc_strdup(ctdb, t);
768 } else {
769 prog = talloc_strdup(ctdb, helper);
771 CTDB_NO_MEMORY_VOID(ctdb, prog);
774 /* Find a lock context with requests */
775 lock_ctx = ctdb_find_lock_context(ctdb);
776 if (lock_ctx == NULL) {
777 return;
780 lock_ctx->child = -1;
781 ret = pipe(lock_ctx->fd);
782 if (ret != 0) {
783 DEBUG(DEBUG_ERR, ("Failed to create pipe in ctdb_lock_schedule\n"));
784 return;
787 set_close_on_exec(lock_ctx->fd[0]);
789 /* Create data for child process */
790 tmp_ctx = talloc_new(lock_ctx);
791 if (tmp_ctx == NULL) {
792 DEBUG(DEBUG_ERR, ("Failed to allocate memory for helper args\n"));
793 close(lock_ctx->fd[0]);
794 close(lock_ctx->fd[1]);
795 return;
798 /* Create arguments for lock helper */
799 if (!lock_helper_args(tmp_ctx, lock_ctx, lock_ctx->fd[1],
800 &argc, &args)) {
801 DEBUG(DEBUG_ERR, ("Failed to create lock helper args\n"));
802 close(lock_ctx->fd[0]);
803 close(lock_ctx->fd[1]);
804 talloc_free(tmp_ctx);
805 return;
808 if (!ctdb_vfork_with_logging(lock_ctx, ctdb, "lock_helper",
809 prog, argc, (const char **)args,
810 NULL, NULL, &lock_ctx->child)) {
811 DEBUG(DEBUG_ERR, ("Failed to create a child in ctdb_lock_schedule\n"));
812 close(lock_ctx->fd[0]);
813 close(lock_ctx->fd[1]);
814 talloc_free(tmp_ctx);
815 return;
818 /* Parent process */
819 close(lock_ctx->fd[1]);
821 talloc_free(tmp_ctx);
823 /* Set up timeout handler */
824 lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
825 lock_ctx,
826 timeval_current_ofs(10, 0),
827 ctdb_lock_timeout_handler,
828 (void *)lock_ctx);
829 if (lock_ctx->ttimer == NULL) {
830 ctdb_kill(ctdb, lock_ctx->child, SIGKILL);
831 lock_ctx->child = -1;
832 close(lock_ctx->fd[0]);
833 return;
836 /* Set up callback */
837 lock_ctx->tfd = tevent_add_fd(ctdb->ev,
838 lock_ctx,
839 lock_ctx->fd[0],
840 EVENT_FD_READ,
841 ctdb_lock_handler,
842 (void *)lock_ctx);
843 if (lock_ctx->tfd == NULL) {
844 TALLOC_FREE(lock_ctx->ttimer);
845 ctdb_kill(ctdb, lock_ctx->child, SIGKILL);
846 lock_ctx->child = -1;
847 close(lock_ctx->fd[0]);
848 return;
850 tevent_fd_set_auto_close(lock_ctx->tfd);
852 /* Move the context from pending to current */
853 if (lock_ctx->type == LOCK_RECORD) {
854 DLIST_REMOVE(lock_ctx->ctdb_db->lock_pending, lock_ctx);
855 DLIST_ADD_END(lock_ctx->ctdb_db->lock_current, lock_ctx, NULL);
856 } else {
857 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
858 DLIST_ADD_END(ctdb->lock_current, lock_ctx, NULL);
860 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_pending);
861 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_current);
862 if (lock_ctx->ctdb_db) {
863 lock_ctx->ctdb_db->lock_num_current++;
864 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
865 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_current);
871 * Lock record / db depending on type
873 static struct lock_request *ctdb_lock_internal(TALLOC_CTX *mem_ctx,
874 struct ctdb_context *ctdb,
875 struct ctdb_db_context *ctdb_db,
876 TDB_DATA key,
877 uint32_t priority,
878 void (*callback)(void *, bool),
879 void *private_data,
880 enum lock_type type,
881 bool auto_mark)
883 struct lock_context *lock_ctx = NULL;
884 struct lock_request *request;
886 if (callback == NULL) {
887 DEBUG(DEBUG_WARNING, ("No callback function specified, not locking\n"));
888 return NULL;
891 lock_ctx = talloc_zero(ctdb, struct lock_context);
892 if (lock_ctx == NULL) {
893 DEBUG(DEBUG_ERR, ("Failed to create a new lock context\n"));
894 return NULL;
897 if ((request = talloc_zero(mem_ctx, struct lock_request)) == NULL) {
898 talloc_free(lock_ctx);
899 return NULL;
902 lock_ctx->type = type;
903 lock_ctx->ctdb = ctdb;
904 lock_ctx->ctdb_db = ctdb_db;
905 lock_ctx->key.dsize = key.dsize;
906 if (key.dsize > 0) {
907 lock_ctx->key.dptr = talloc_memdup(lock_ctx, key.dptr, key.dsize);
908 if (lock_ctx->key.dptr == NULL) {
909 DEBUG(DEBUG_ERR, (__location__ "Memory allocation error\n"));
910 talloc_free(lock_ctx);
911 talloc_free(request);
912 return NULL;
914 lock_ctx->key_hash = ctdb_hash(&key);
915 } else {
916 lock_ctx->key.dptr = NULL;
918 lock_ctx->priority = priority;
919 lock_ctx->auto_mark = auto_mark;
921 lock_ctx->request = request;
922 lock_ctx->child = -1;
924 /* Non-record locks are required by recovery and should be scheduled
925 * immediately, so keep them at the head of the pending queue.
927 if (lock_ctx->type == LOCK_RECORD) {
928 DLIST_ADD_END(ctdb_db->lock_pending, lock_ctx, NULL);
929 } else {
930 DLIST_ADD_END(ctdb->lock_pending, lock_ctx, NULL);
932 CTDB_INCREMENT_STAT(ctdb, locks.num_pending);
933 if (ctdb_db) {
934 CTDB_INCREMENT_DB_STAT(ctdb_db, locks.num_pending);
937 /* Start the timer when we activate the context */
938 lock_ctx->start_time = timeval_current();
940 request->lctx = lock_ctx;
941 request->callback = callback;
942 request->private_data = private_data;
944 talloc_set_destructor(request, ctdb_lock_request_destructor);
945 talloc_set_destructor(lock_ctx, ctdb_lock_context_destructor);
947 ctdb_lock_schedule(ctdb);
949 return request;
954 * obtain a lock on a record in a database
956 struct lock_request *ctdb_lock_record(TALLOC_CTX *mem_ctx,
957 struct ctdb_db_context *ctdb_db,
958 TDB_DATA key,
959 bool auto_mark,
960 void (*callback)(void *, bool),
961 void *private_data)
963 return ctdb_lock_internal(mem_ctx,
964 ctdb_db->ctdb,
965 ctdb_db,
966 key,
968 callback,
969 private_data,
970 LOCK_RECORD,
971 auto_mark);
976 * obtain a lock on a database
978 struct lock_request *ctdb_lock_db(TALLOC_CTX *mem_ctx,
979 struct ctdb_db_context *ctdb_db,
980 bool auto_mark,
981 void (*callback)(void *, bool),
982 void *private_data)
984 return ctdb_lock_internal(mem_ctx,
985 ctdb_db->ctdb,
986 ctdb_db,
987 tdb_null,
989 callback,
990 private_data,
991 LOCK_DB,
992 auto_mark);
997 * obtain locks on all databases of specified priority
999 struct lock_request *ctdb_lock_alldb_prio(TALLOC_CTX *mem_ctx,
1000 struct ctdb_context *ctdb,
1001 uint32_t priority,
1002 bool auto_mark,
1003 void (*callback)(void *, bool),
1004 void *private_data)
1006 if (priority < 1 || priority > NUM_DB_PRIORITIES) {
1007 DEBUG(DEBUG_ERR, ("Invalid db priority: %u\n", priority));
1008 return NULL;
1011 return ctdb_lock_internal(mem_ctx,
1012 ctdb,
1013 NULL,
1014 tdb_null,
1015 priority,
1016 callback,
1017 private_data,
1018 LOCK_ALLDB_PRIO,
1019 auto_mark);
1024 * obtain locks on all databases
1026 struct lock_request *ctdb_lock_alldb(TALLOC_CTX *mem_ctx,
1027 struct ctdb_context *ctdb,
1028 bool auto_mark,
1029 void (*callback)(void *, bool),
1030 void *private_data)
1032 return ctdb_lock_internal(mem_ctx,
1033 ctdb,
1034 NULL,
1035 tdb_null,
1037 callback,
1038 private_data,
1039 LOCK_ALLDB,
1040 auto_mark);