ctdb-daemon: Use statically allocated arrays for helper paths
[Samba.git] / ctdb / server / ctdb_lock.c
blobf5928343df48838fcb7884befcf442965a66717e
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
46 enum lock_type {
47 LOCK_RECORD,
48 LOCK_DB,
49 LOCK_ALLDB_PRIO,
50 LOCK_ALLDB,
53 static const char * const lock_type_str[] = {
54 "lock_record",
55 "lock_db",
56 "lock_alldb_prio",
57 "lock_alldb",
60 struct lock_request;
62 /* lock_context is the common part for a lock request */
63 struct lock_context {
64 struct lock_context *next, *prev;
65 enum lock_type type;
66 struct ctdb_context *ctdb;
67 struct ctdb_db_context *ctdb_db;
68 TDB_DATA key;
69 uint32_t priority;
70 bool auto_mark;
71 struct lock_request *request;
72 pid_t child;
73 int fd[2];
74 struct tevent_fd *tfd;
75 struct tevent_timer *ttimer;
76 struct timeval start_time;
77 uint32_t key_hash;
78 bool can_schedule;
81 /* lock_request is the client specific part for a lock request */
82 struct lock_request {
83 struct lock_context *lctx;
84 void (*callback)(void *, bool);
85 void *private_data;
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) {
98 return false;
101 if (strstr(name, "brlock") ||
102 strstr(name, "g_lock") ||
103 strstr(name, "notify_onelevel") ||
104 strstr(name, "serverid") ||
105 strstr(name, "xattr_tdb")) {
106 return true;
109 return false;
112 typedef int (*db_handler_t)(struct ctdb_db_context *ctdb_db,
113 uint32_t priority,
114 void *private_data);
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;
120 int ret;
122 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
123 if (ctdb_db->priority != priority) {
124 continue;
126 if (later_db(ctdb, ctdb_db->db_name)) {
127 continue;
129 ret = handler(ctdb_db, priority, private_data);
130 if (ret != 0) {
131 return -1;
135 /* If priority != 1, later_db check is not required and can return */
136 if (priority != 1) {
137 return 0;
140 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
141 if (!later_db(ctdb, ctdb_db->db_name)) {
142 continue;
144 ret = handler(ctdb_db, priority, private_data);
145 if (ret != 0) {
146 return -1;
150 return 0;
155 * lock all databases - mark only
157 static int db_lock_mark_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
158 void *private_data)
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",
167 ctdb_db->db_name));
168 return -1;
171 if (tdb_lockall_mark(ctdb_db->ltdb->tdb) != 0) {
172 DEBUG(DEBUG_ERR, ("Failed to mark (all lock) database %s\n",
173 ctdb_db->db_name));
174 return -1;
177 return 0;
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"));
191 return -1;
194 return ctdb_db_iterator(ctdb, priority, db_lock_mark_handler, NULL);
197 static int ctdb_lockall_mark(struct ctdb_context *ctdb)
199 uint32_t priority;
201 for (priority=1; priority<=NUM_DB_PRIORITIES; priority++) {
202 if (ctdb_db_iterator(ctdb, priority, db_lock_mark_handler, NULL) != 0) {
203 return -1;
207 return 0;
212 * lock all databases - unmark only
214 static int db_lock_unmark_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
215 void *private_data)
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",
224 ctdb_db->db_name));
225 return -1;
228 if (tdb_lockall_unmark(ctdb_db->ltdb->tdb) != 0) {
229 DEBUG(DEBUG_ERR, ("Failed to unmark (all lock) database %s\n",
230 ctdb_db->db_name));
231 return -1;
234 return 0;
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"));
248 return -1;
251 return ctdb_db_iterator(ctdb, priority, db_lock_unmark_handler, NULL);
254 static int ctdb_lockall_unmark(struct ctdb_context *ctdb)
256 uint32_t priority;
258 for (priority=NUM_DB_PRIORITIES; priority>0; priority--) {
259 if (ctdb_db_iterator(ctdb, priority, db_lock_unmark_handler, NULL) != 0) {
260 return -1;
264 return 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->request) {
276 lock_ctx->request->lctx = NULL;
278 if (lock_ctx->child > 0) {
279 ctdb_kill(lock_ctx->ctdb, lock_ctx->child, SIGKILL);
280 if (lock_ctx->type == LOCK_RECORD) {
281 DLIST_REMOVE(lock_ctx->ctdb_db->lock_current, lock_ctx);
282 } else {
283 DLIST_REMOVE(lock_ctx->ctdb->lock_current, lock_ctx);
285 if (lock_ctx->ctdb_db) {
286 lock_ctx->ctdb_db->lock_num_current--;
288 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_current);
289 if (lock_ctx->ctdb_db) {
290 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_current);
292 } else {
293 if (lock_ctx->type == LOCK_RECORD) {
294 DLIST_REMOVE(lock_ctx->ctdb_db->lock_pending, lock_ctx);
295 } else {
296 DLIST_REMOVE(lock_ctx->ctdb->lock_pending, lock_ctx);
298 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_pending);
299 if (lock_ctx->ctdb_db) {
300 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
304 ctdb_lock_schedule(lock_ctx->ctdb);
306 return 0;
311 * Destructor to remove lock request
313 static int ctdb_lock_request_destructor(struct lock_request *lock_request)
315 TALLOC_FREE(lock_request->lctx);
316 return 0;
320 * Process all the callbacks waiting for lock
322 * If lock has failed, callback is executed with locked=false
324 static void process_callbacks(struct lock_context *lock_ctx, bool locked)
326 struct lock_request *request;
328 if (lock_ctx->auto_mark && locked) {
329 switch (lock_ctx->type) {
330 case LOCK_RECORD:
331 tdb_chainlock_mark(lock_ctx->ctdb_db->ltdb->tdb, lock_ctx->key);
332 break;
334 case LOCK_DB:
335 tdb_lockall_mark(lock_ctx->ctdb_db->ltdb->tdb);
336 break;
338 case LOCK_ALLDB_PRIO:
339 ctdb_lockall_mark_prio(lock_ctx->ctdb, lock_ctx->priority);
340 break;
342 case LOCK_ALLDB:
343 ctdb_lockall_mark(lock_ctx->ctdb);
344 break;
348 request = lock_ctx->request;
349 if (lock_ctx->auto_mark) {
350 /* Reset the destructor, so request is not removed from the list */
351 talloc_set_destructor(request, NULL);
353 request->callback(request->private_data, locked);
355 if (lock_ctx->auto_mark && locked) {
356 switch (lock_ctx->type) {
357 case LOCK_RECORD:
358 tdb_chainlock_unmark(lock_ctx->ctdb_db->ltdb->tdb, lock_ctx->key);
359 break;
361 case LOCK_DB:
362 tdb_lockall_unmark(lock_ctx->ctdb_db->ltdb->tdb);
363 break;
365 case LOCK_ALLDB_PRIO:
366 ctdb_lockall_unmark_prio(lock_ctx->ctdb, lock_ctx->priority);
367 break;
369 case LOCK_ALLDB:
370 ctdb_lockall_unmark(lock_ctx->ctdb);
371 break;
377 static int lock_bucket_id(double t)
379 double ms = 1.e-3, s = 1;
380 int id;
382 if (t < 1*ms) {
383 id = 0;
384 } else if (t < 10*ms) {
385 id = 1;
386 } else if (t < 100*ms) {
387 id = 2;
388 } else if (t < 1*s) {
389 id = 3;
390 } else if (t < 2*s) {
391 id = 4;
392 } else if (t < 4*s) {
393 id = 5;
394 } else if (t < 8*s) {
395 id = 6;
396 } else if (t < 16*s) {
397 id = 7;
398 } else if (t < 32*s) {
399 id = 8;
400 } else if (t < 64*s) {
401 id = 9;
402 } else {
403 id = 10;
406 return id;
410 * Callback routine when the required locks are obtained.
411 * Called from parent context
413 static void ctdb_lock_handler(struct tevent_context *ev,
414 struct tevent_fd *tfd,
415 uint16_t flags,
416 void *private_data)
418 struct lock_context *lock_ctx;
419 TALLOC_CTX *tmp_ctx = NULL;
420 char c;
421 bool locked;
422 double t;
423 int id;
425 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
427 /* cancel the timeout event */
428 TALLOC_FREE(lock_ctx->ttimer);
430 t = timeval_elapsed(&lock_ctx->start_time);
431 id = lock_bucket_id(t);
433 if (lock_ctx->auto_mark) {
434 tmp_ctx = talloc_new(ev);
435 talloc_steal(tmp_ctx, lock_ctx);
438 /* Read the status from the child process */
439 if (sys_read(lock_ctx->fd[0], &c, 1) != 1) {
440 locked = false;
441 } else {
442 locked = (c == 0 ? true : false);
445 /* Update statistics */
446 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_calls);
447 if (lock_ctx->ctdb_db) {
448 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_calls);
451 if (locked) {
452 if (lock_ctx->ctdb_db) {
453 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.buckets[id]);
454 CTDB_UPDATE_LATENCY(lock_ctx->ctdb, lock_ctx->ctdb_db,
455 lock_type_str[lock_ctx->type], locks.latency,
456 lock_ctx->start_time);
458 CTDB_UPDATE_DB_LATENCY(lock_ctx->ctdb_db, lock_type_str[lock_ctx->type], locks.latency, t);
459 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.buckets[id]);
461 } else {
462 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_failed);
463 if (lock_ctx->ctdb_db) {
464 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_failed);
468 process_callbacks(lock_ctx, locked);
470 if (lock_ctx->auto_mark) {
471 talloc_free(tmp_ctx);
477 * Callback routine when required locks are not obtained within timeout
478 * Called from parent context
480 static void ctdb_lock_timeout_handler(struct tevent_context *ev,
481 struct tevent_timer *ttimer,
482 struct timeval current_time,
483 void *private_data)
485 static char debug_locks[PATH_MAX+1] = "";
486 struct lock_context *lock_ctx;
487 struct ctdb_context *ctdb;
488 pid_t pid;
489 double elapsed_time;
490 int new_timer;
492 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
493 ctdb = lock_ctx->ctdb;
495 /* If a node stopped/banned, don't spam the logs */
496 if (ctdb->nodes[ctdb->pnn]->flags & NODE_FLAGS_INACTIVE) {
497 lock_ctx->ttimer = NULL;
498 return;
501 elapsed_time = timeval_elapsed(&lock_ctx->start_time);
502 if (lock_ctx->ctdb_db) {
503 DEBUG(DEBUG_WARNING,
504 ("Unable to get %s lock on database %s for %.0lf seconds\n",
505 (lock_ctx->type == LOCK_RECORD ? "RECORD" : "DB"),
506 lock_ctx->ctdb_db->db_name, elapsed_time));
507 } else {
508 DEBUG(DEBUG_WARNING,
509 ("Unable to get ALLDB locks for %.0lf seconds\n",
510 elapsed_time));
513 if (ctdb_set_helper("lock debugging helper",
514 debug_locks, sizeof(debug_locks),
515 "CTDB_DEBUG_LOCKS",
516 getenv("CTDB_BASE"), "debug_locks.sh")) {
517 pid = vfork();
518 if (pid == 0) {
519 execl(debug_locks, debug_locks, NULL);
520 _exit(0);
522 ctdb_track_child(ctdb, pid);
523 } else {
524 DEBUG(DEBUG_WARNING,
525 (__location__
526 " Unable to setup lock debugging\n"));
529 /* Back-off logging if lock is not obtained for a long time */
530 if (elapsed_time < 100.0) {
531 new_timer = 10;
532 } else if (elapsed_time < 1000.0) {
533 new_timer = 100;
534 } else {
535 new_timer = 1000;
538 /* reset the timeout timer */
539 // talloc_free(lock_ctx->ttimer);
540 lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
541 lock_ctx,
542 timeval_current_ofs(new_timer, 0),
543 ctdb_lock_timeout_handler,
544 (void *)lock_ctx);
548 static int db_count_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
549 void *private_data)
551 int *count = (int *)private_data;
553 (*count) += 2;
555 return 0;
558 static int db_flags(struct ctdb_db_context *ctdb_db)
560 int tdb_flags = TDB_DEFAULT;
562 #ifdef TDB_MUTEX_LOCKING
563 if (!ctdb_db->persistent && ctdb_db->ctdb->tunable.mutex_enabled) {
564 tdb_flags = (TDB_MUTEX_LOCKING | TDB_CLEAR_IF_FIRST);
566 #endif
567 return tdb_flags;
570 struct db_namelist {
571 const char **names;
572 int n;
575 static int db_name_handler(struct ctdb_db_context *ctdb_db, uint32_t priority,
576 void *private_data)
578 struct db_namelist *list = (struct db_namelist *)private_data;
580 list->names[list->n] = talloc_strdup(list->names, ctdb_db->db_path);
581 list->names[list->n+1] = talloc_asprintf(list->names, "0x%x",
582 db_flags(ctdb_db));
583 list->n += 2;
585 return 0;
588 static bool lock_helper_args(TALLOC_CTX *mem_ctx,
589 struct lock_context *lock_ctx, int fd,
590 int *argc, const char ***argv)
592 struct ctdb_context *ctdb = lock_ctx->ctdb;
593 const char **args = NULL;
594 int nargs, i;
595 int priority;
596 struct db_namelist list;
598 switch (lock_ctx->type) {
599 case LOCK_RECORD:
600 nargs = 6;
601 break;
603 case LOCK_DB:
604 nargs = 5;
605 break;
607 case LOCK_ALLDB_PRIO:
608 nargs = 3;
609 ctdb_db_iterator(ctdb, lock_ctx->priority, db_count_handler, &nargs);
610 break;
612 case LOCK_ALLDB:
613 nargs = 3;
614 for (priority=1; priority<NUM_DB_PRIORITIES; priority++) {
615 ctdb_db_iterator(ctdb, priority, db_count_handler, &nargs);
617 break;
620 /* Add extra argument for null termination */
621 nargs++;
623 args = talloc_array(mem_ctx, const char *, nargs);
624 if (args == NULL) {
625 return false;
628 args[0] = talloc_asprintf(args, "%d", getpid());
629 args[1] = talloc_asprintf(args, "%d", fd);
631 switch (lock_ctx->type) {
632 case LOCK_RECORD:
633 args[2] = talloc_strdup(args, "RECORD");
634 args[3] = talloc_strdup(args, lock_ctx->ctdb_db->db_path);
635 args[4] = talloc_asprintf(args, "0x%x",
636 db_flags(lock_ctx->ctdb_db));
637 if (lock_ctx->key.dsize == 0) {
638 args[5] = talloc_strdup(args, "NULL");
639 } else {
640 args[5] = hex_encode_talloc(args, lock_ctx->key.dptr, lock_ctx->key.dsize);
642 break;
644 case LOCK_DB:
645 args[2] = talloc_strdup(args, "DB");
646 args[3] = talloc_strdup(args, lock_ctx->ctdb_db->db_path);
647 args[4] = talloc_asprintf(args, "0x%x",
648 db_flags(lock_ctx->ctdb_db));
649 break;
651 case LOCK_ALLDB_PRIO:
652 args[2] = talloc_strdup(args, "DB");
653 list.names = args;
654 list.n = 3;
655 ctdb_db_iterator(ctdb, lock_ctx->priority, db_name_handler, &list);
656 break;
658 case LOCK_ALLDB:
659 args[2] = talloc_strdup(args, "DB");
660 list.names = args;
661 list.n = 3;
662 for (priority=1; priority<NUM_DB_PRIORITIES; priority++) {
663 ctdb_db_iterator(ctdb, priority, db_name_handler, &list);
665 break;
668 /* Make sure last argument is NULL */
669 args[nargs-1] = NULL;
671 for (i=0; i<nargs-1; i++) {
672 if (args[i] == NULL) {
673 talloc_free(args);
674 return false;
678 *argc = nargs;
679 *argv = args;
680 return true;
684 * Find a lock request that can be scheduled
686 static struct lock_context *ctdb_find_lock_context(struct ctdb_context *ctdb)
688 struct lock_context *lock_ctx, *next_ctx;
689 struct ctdb_db_context *ctdb_db;
691 /* First check if there are database lock requests */
693 for (lock_ctx = ctdb->lock_pending; lock_ctx != NULL;
694 lock_ctx = next_ctx) {
696 if (lock_ctx->request != NULL) {
697 /* Found a lock context with a request */
698 return lock_ctx;
701 next_ctx = lock_ctx->next;
703 DEBUG(DEBUG_INFO, ("Removing lock context without lock "
704 "request\n"));
705 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
706 CTDB_DECREMENT_STAT(ctdb, locks.num_pending);
707 if (lock_ctx->ctdb_db) {
708 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db,
709 locks.num_pending);
711 talloc_free(lock_ctx);
714 /* Next check database queues */
715 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
716 if (ctdb_db->lock_num_current ==
717 ctdb->tunable.lock_processes_per_db) {
718 continue;
721 for (lock_ctx = ctdb_db->lock_pending; lock_ctx != NULL;
722 lock_ctx = next_ctx) {
724 next_ctx = lock_ctx->next;
726 if (lock_ctx->request != NULL) {
727 return lock_ctx;
730 DEBUG(DEBUG_INFO, ("Removing lock context without "
731 "lock request\n"));
732 DLIST_REMOVE(ctdb_db->lock_pending, lock_ctx);
733 CTDB_DECREMENT_STAT(ctdb, locks.num_pending);
734 CTDB_DECREMENT_DB_STAT(ctdb_db, locks.num_pending);
735 talloc_free(lock_ctx);
739 return NULL;
743 * Schedule a new lock child process
744 * Set up callback handler and timeout handler
746 static void ctdb_lock_schedule(struct ctdb_context *ctdb)
748 struct lock_context *lock_ctx;
749 int ret, argc;
750 TALLOC_CTX *tmp_ctx;
751 static char prog[PATH_MAX+1] = "";
752 const char **args;
754 if (!ctdb_set_helper("lock helper",
755 prog, sizeof(prog),
756 "CTDB_LOCK_HELPER",
757 CTDB_HELPER_BINDIR, "ctdb_lock_helper")) {
758 ctdb_die(ctdb, __location__
759 " Unable to set lock helper\n");
762 /* Find a lock context with requests */
763 lock_ctx = ctdb_find_lock_context(ctdb);
764 if (lock_ctx == NULL) {
765 return;
768 lock_ctx->child = -1;
769 ret = pipe(lock_ctx->fd);
770 if (ret != 0) {
771 DEBUG(DEBUG_ERR, ("Failed to create pipe in ctdb_lock_schedule\n"));
772 return;
775 set_close_on_exec(lock_ctx->fd[0]);
777 /* Create data for child process */
778 tmp_ctx = talloc_new(lock_ctx);
779 if (tmp_ctx == NULL) {
780 DEBUG(DEBUG_ERR, ("Failed to allocate memory for helper args\n"));
781 close(lock_ctx->fd[0]);
782 close(lock_ctx->fd[1]);
783 return;
786 /* Create arguments for lock helper */
787 if (!lock_helper_args(tmp_ctx, lock_ctx, lock_ctx->fd[1],
788 &argc, &args)) {
789 DEBUG(DEBUG_ERR, ("Failed to create lock helper args\n"));
790 close(lock_ctx->fd[0]);
791 close(lock_ctx->fd[1]);
792 talloc_free(tmp_ctx);
793 return;
796 if (!ctdb_vfork_with_logging(lock_ctx, ctdb, "lock_helper",
797 prog, argc, (const char **)args,
798 NULL, NULL, &lock_ctx->child)) {
799 DEBUG(DEBUG_ERR, ("Failed to create a child in ctdb_lock_schedule\n"));
800 close(lock_ctx->fd[0]);
801 close(lock_ctx->fd[1]);
802 talloc_free(tmp_ctx);
803 return;
806 /* Parent process */
807 close(lock_ctx->fd[1]);
809 talloc_set_destructor(lock_ctx, ctdb_lock_context_destructor);
811 talloc_free(tmp_ctx);
813 /* Set up timeout handler */
814 lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
815 lock_ctx,
816 timeval_current_ofs(10, 0),
817 ctdb_lock_timeout_handler,
818 (void *)lock_ctx);
819 if (lock_ctx->ttimer == NULL) {
820 ctdb_kill(ctdb, lock_ctx->child, SIGKILL);
821 lock_ctx->child = -1;
822 talloc_set_destructor(lock_ctx, NULL);
823 close(lock_ctx->fd[0]);
824 return;
827 /* Set up callback */
828 lock_ctx->tfd = tevent_add_fd(ctdb->ev,
829 lock_ctx,
830 lock_ctx->fd[0],
831 EVENT_FD_READ,
832 ctdb_lock_handler,
833 (void *)lock_ctx);
834 if (lock_ctx->tfd == NULL) {
835 TALLOC_FREE(lock_ctx->ttimer);
836 ctdb_kill(ctdb, lock_ctx->child, SIGKILL);
837 lock_ctx->child = -1;
838 talloc_set_destructor(lock_ctx, NULL);
839 close(lock_ctx->fd[0]);
840 return;
842 tevent_fd_set_auto_close(lock_ctx->tfd);
844 /* Move the context from pending to current */
845 if (lock_ctx->type == LOCK_RECORD) {
846 DLIST_REMOVE(lock_ctx->ctdb_db->lock_pending, lock_ctx);
847 DLIST_ADD_END(lock_ctx->ctdb_db->lock_current, lock_ctx, NULL);
848 } else {
849 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
850 DLIST_ADD_END(ctdb->lock_current, lock_ctx, NULL);
852 CTDB_DECREMENT_STAT(lock_ctx->ctdb, locks.num_pending);
853 CTDB_INCREMENT_STAT(lock_ctx->ctdb, locks.num_current);
854 if (lock_ctx->ctdb_db) {
855 lock_ctx->ctdb_db->lock_num_current++;
856 CTDB_DECREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_pending);
857 CTDB_INCREMENT_DB_STAT(lock_ctx->ctdb_db, locks.num_current);
863 * Lock record / db depending on type
865 static struct lock_request *ctdb_lock_internal(TALLOC_CTX *mem_ctx,
866 struct ctdb_context *ctdb,
867 struct ctdb_db_context *ctdb_db,
868 TDB_DATA key,
869 uint32_t priority,
870 void (*callback)(void *, bool),
871 void *private_data,
872 enum lock_type type,
873 bool auto_mark)
875 struct lock_context *lock_ctx = NULL;
876 struct lock_request *request;
878 if (callback == NULL) {
879 DEBUG(DEBUG_WARNING, ("No callback function specified, not locking\n"));
880 return NULL;
883 lock_ctx = talloc_zero(ctdb, struct lock_context);
884 if (lock_ctx == NULL) {
885 DEBUG(DEBUG_ERR, ("Failed to create a new lock context\n"));
886 return NULL;
889 if ((request = talloc_zero(mem_ctx, struct lock_request)) == NULL) {
890 talloc_free(lock_ctx);
891 return NULL;
894 lock_ctx->type = type;
895 lock_ctx->ctdb = ctdb;
896 lock_ctx->ctdb_db = ctdb_db;
897 lock_ctx->key.dsize = key.dsize;
898 if (key.dsize > 0) {
899 lock_ctx->key.dptr = talloc_memdup(lock_ctx, key.dptr, key.dsize);
900 if (lock_ctx->key.dptr == NULL) {
901 DEBUG(DEBUG_ERR, (__location__ "Memory allocation error\n"));
902 talloc_free(lock_ctx);
903 return NULL;
905 lock_ctx->key_hash = ctdb_hash(&key);
906 } else {
907 lock_ctx->key.dptr = NULL;
909 lock_ctx->priority = priority;
910 lock_ctx->auto_mark = auto_mark;
912 lock_ctx->request = request;
913 lock_ctx->child = -1;
915 /* Non-record locks are required by recovery and should be scheduled
916 * immediately, so keep them at the head of the pending queue.
918 if (lock_ctx->type == LOCK_RECORD) {
919 DLIST_ADD_END(ctdb_db->lock_pending, lock_ctx, NULL);
920 } else {
921 DLIST_ADD_END(ctdb->lock_pending, lock_ctx, NULL);
923 CTDB_INCREMENT_STAT(ctdb, locks.num_pending);
924 if (ctdb_db) {
925 CTDB_INCREMENT_DB_STAT(ctdb_db, locks.num_pending);
928 /* Start the timer when we activate the context */
929 lock_ctx->start_time = timeval_current();
931 request->lctx = lock_ctx;
932 request->callback = callback;
933 request->private_data = private_data;
935 talloc_set_destructor(request, ctdb_lock_request_destructor);
937 ctdb_lock_schedule(ctdb);
939 return request;
944 * obtain a lock on a record in a database
946 struct lock_request *ctdb_lock_record(TALLOC_CTX *mem_ctx,
947 struct ctdb_db_context *ctdb_db,
948 TDB_DATA key,
949 bool auto_mark,
950 void (*callback)(void *, bool),
951 void *private_data)
953 return ctdb_lock_internal(mem_ctx,
954 ctdb_db->ctdb,
955 ctdb_db,
956 key,
958 callback,
959 private_data,
960 LOCK_RECORD,
961 auto_mark);
966 * obtain a lock on a database
968 struct lock_request *ctdb_lock_db(TALLOC_CTX *mem_ctx,
969 struct ctdb_db_context *ctdb_db,
970 bool auto_mark,
971 void (*callback)(void *, bool),
972 void *private_data)
974 return ctdb_lock_internal(mem_ctx,
975 ctdb_db->ctdb,
976 ctdb_db,
977 tdb_null,
979 callback,
980 private_data,
981 LOCK_DB,
982 auto_mark);
987 * obtain locks on all databases of specified priority
989 struct lock_request *ctdb_lock_alldb_prio(TALLOC_CTX *mem_ctx,
990 struct ctdb_context *ctdb,
991 uint32_t priority,
992 bool auto_mark,
993 void (*callback)(void *, bool),
994 void *private_data)
996 if (priority < 1 || priority > NUM_DB_PRIORITIES) {
997 DEBUG(DEBUG_ERR, ("Invalid db priority: %u\n", priority));
998 return NULL;
1001 return ctdb_lock_internal(mem_ctx,
1002 ctdb,
1003 NULL,
1004 tdb_null,
1005 priority,
1006 callback,
1007 private_data,
1008 LOCK_ALLDB_PRIO,
1009 auto_mark);
1014 * obtain locks on all databases
1016 struct lock_request *ctdb_lock_alldb(TALLOC_CTX *mem_ctx,
1017 struct ctdb_context *ctdb,
1018 bool auto_mark,
1019 void (*callback)(void *, bool),
1020 void *private_data)
1022 return ctdb_lock_internal(mem_ctx,
1023 ctdb,
1024 NULL,
1025 tdb_null,
1027 callback,
1028 private_data,
1029 LOCK_ALLDB,
1030 auto_mark);