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"
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.
57 static const char * const lock_type_str
[] = {
66 /* lock_context is the common part for a lock request */
68 struct lock_context
*next
, *prev
;
70 struct ctdb_context
*ctdb
;
71 struct ctdb_db_context
*ctdb_db
;
75 struct lock_request
*request
;
78 struct tevent_fd
*tfd
;
79 struct tevent_timer
*ttimer
;
80 struct timeval start_time
;
85 /* lock_request is the client specific part for a lock request */
87 struct lock_context
*lctx
;
88 void (*callback
)(void *, bool);
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) {
105 if (strstr(name
, "brlock") ||
106 strstr(name
, "g_lock") ||
107 strstr(name
, "notify_onelevel") ||
108 strstr(name
, "serverid") ||
109 strstr(name
, "xattr_tdb")) {
116 typedef int (*db_handler_t
)(struct ctdb_db_context
*ctdb_db
,
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
;
126 for (ctdb_db
= ctdb
->db_list
; ctdb_db
; ctdb_db
= ctdb_db
->next
) {
127 if (ctdb_db
->priority
!= priority
) {
130 if (later_db(ctdb
, ctdb_db
->db_name
)) {
133 ret
= handler(ctdb_db
, priority
, private_data
);
139 /* If priority != 1, later_db check is not required and can return */
144 for (ctdb_db
= ctdb
->db_list
; ctdb_db
; ctdb_db
= ctdb_db
->next
) {
145 if (!later_db(ctdb
, ctdb_db
->db_name
)) {
148 ret
= handler(ctdb_db
, priority
, private_data
);
159 * lock all databases - mark only
161 static int db_lock_mark_handler(struct ctdb_db_context
*ctdb_db
, uint32_t priority
,
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",
175 if (tdb_lockall_mark(ctdb_db
->ltdb
->tdb
) != 0) {
176 DEBUG(DEBUG_ERR
, ("Failed to mark (all lock) database %s\n",
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"));
198 return ctdb_db_iterator(ctdb
, priority
, db_lock_mark_handler
, NULL
);
201 static int ctdb_lockall_mark(struct ctdb_context
*ctdb
)
205 for (priority
=1; priority
<=NUM_DB_PRIORITIES
; priority
++) {
206 if (ctdb_db_iterator(ctdb
, priority
, db_lock_mark_handler
, NULL
) != 0) {
216 * lock all databases - unmark only
218 static int db_lock_unmark_handler(struct ctdb_db_context
*ctdb_db
, uint32_t priority
,
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",
232 if (tdb_lockall_unmark(ctdb_db
->ltdb
->tdb
) != 0) {
233 DEBUG(DEBUG_ERR
, ("Failed to unmark (all lock) database %s\n",
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"));
255 return ctdb_db_iterator(ctdb
, priority
, db_lock_unmark_handler
, NULL
);
258 static int ctdb_lockall_unmark(struct ctdb_context
*ctdb
)
262 for (priority
=NUM_DB_PRIORITIES
; priority
>0; priority
--) {
263 if (ctdb_db_iterator(ctdb
, priority
, db_lock_unmark_handler
, NULL
) != 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
);
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
);
297 if (lock_ctx
->type
== LOCK_RECORD
) {
298 DLIST_REMOVE(lock_ctx
->ctdb_db
->lock_pending
, lock_ctx
);
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
);
315 * Destructor to remove lock request
317 static int ctdb_lock_request_destructor(struct lock_request
*lock_request
)
319 if (lock_request
->lctx
== NULL
) {
323 lock_request
->lctx
->request
= NULL
;
324 TALLOC_FREE(lock_request
->lctx
);
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
) {
342 tdb_chainlock_mark(lock_ctx
->ctdb_db
->ltdb
->tdb
, lock_ctx
->key
);
346 tdb_lockall_mark(lock_ctx
->ctdb_db
->ltdb
->tdb
);
349 case LOCK_ALLDB_PRIO
:
350 ctdb_lockall_mark_prio(lock_ctx
->ctdb
, lock_ctx
->priority
);
354 ctdb_lockall_mark(lock_ctx
->ctdb
);
359 request
= lock_ctx
->request
;
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
);
377 switch (lock_ctx
->type
) {
379 tdb_chainlock_unmark(lock_ctx
->ctdb_db
->ltdb
->tdb
, lock_ctx
->key
);
383 tdb_lockall_unmark(lock_ctx
->ctdb_db
->ltdb
->tdb
);
386 case LOCK_ALLDB_PRIO
:
387 ctdb_lockall_unmark_prio(lock_ctx
->ctdb
, lock_ctx
->priority
);
391 ctdb_lockall_unmark(lock_ctx
->ctdb
);
396 talloc_free(lock_ctx
);
400 static int lock_bucket_id(double t
)
402 double ms
= 1.e
-3, s
= 1;
407 } else if (t
< 10*ms
) {
409 } else if (t
< 100*ms
) {
411 } else if (t
< 1*s
) {
413 } else if (t
< 2*s
) {
415 } else if (t
< 4*s
) {
417 } else if (t
< 8*s
) {
419 } else if (t
< 16*s
) {
421 } else if (t
< 32*s
) {
423 } else if (t
< 64*s
) {
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
,
441 struct lock_context
*lock_ctx
;
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) {
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
);
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
]);
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
,
498 static char debug_locks
[PATH_MAX
+1] = "";
499 struct lock_context
*lock_ctx
;
500 struct ctdb_context
*ctdb
;
505 lock_ctx
= talloc_get_type_abort(private_data
, struct lock_context
);
506 ctdb
= lock_ctx
->ctdb
;
508 /* If a node stopped/banned, don't spam the logs */
509 if (ctdb
->nodes
[ctdb
->pnn
]->flags
& NODE_FLAGS_INACTIVE
) {
510 lock_ctx
->ttimer
= NULL
;
514 elapsed_time
= timeval_elapsed(&lock_ctx
->start_time
);
515 if (lock_ctx
->ctdb_db
) {
517 ("Unable to get %s lock on database %s for %.0lf seconds\n",
518 (lock_ctx
->type
== LOCK_RECORD
? "RECORD" : "DB"),
519 lock_ctx
->ctdb_db
->db_name
, elapsed_time
));
522 ("Unable to get ALLDB locks for %.0lf seconds\n",
526 if (ctdb_set_helper("lock debugging helper",
527 debug_locks
, sizeof(debug_locks
),
529 getenv("CTDB_BASE"), "debug_locks.sh")) {
532 execl(debug_locks
, debug_locks
, NULL
);
535 ctdb_track_child(ctdb
, pid
);
539 " Unable to setup lock debugging\n"));
542 /* Back-off logging if lock is not obtained for a long time */
543 if (elapsed_time
< 100.0) {
545 } else if (elapsed_time
< 1000.0) {
551 /* reset the timeout timer */
552 // talloc_free(lock_ctx->ttimer);
553 lock_ctx
->ttimer
= tevent_add_timer(ctdb
->ev
,
555 timeval_current_ofs(new_timer
, 0),
556 ctdb_lock_timeout_handler
,
561 static int db_count_handler(struct ctdb_db_context
*ctdb_db
, uint32_t priority
,
564 int *count
= (int *)private_data
;
571 static int db_flags(struct ctdb_db_context
*ctdb_db
)
573 int tdb_flags
= TDB_DEFAULT
;
575 #ifdef TDB_MUTEX_LOCKING
576 if (!ctdb_db
->persistent
&& ctdb_db
->ctdb
->tunable
.mutex_enabled
) {
577 tdb_flags
= (TDB_MUTEX_LOCKING
| TDB_CLEAR_IF_FIRST
);
588 static int db_name_handler(struct ctdb_db_context
*ctdb_db
, uint32_t priority
,
591 struct db_namelist
*list
= (struct db_namelist
*)private_data
;
593 list
->names
[list
->n
] = talloc_strdup(list
->names
, ctdb_db
->db_path
);
594 list
->names
[list
->n
+1] = talloc_asprintf(list
->names
, "0x%x",
601 static bool lock_helper_args(TALLOC_CTX
*mem_ctx
,
602 struct lock_context
*lock_ctx
, int fd
,
603 int *argc
, const char ***argv
)
605 struct ctdb_context
*ctdb
= lock_ctx
->ctdb
;
606 const char **args
= NULL
;
609 struct db_namelist list
;
611 switch (lock_ctx
->type
) {
620 case LOCK_ALLDB_PRIO
:
622 ctdb_db_iterator(ctdb
, lock_ctx
->priority
, db_count_handler
, &nargs
);
627 for (priority
=1; priority
<NUM_DB_PRIORITIES
; priority
++) {
628 ctdb_db_iterator(ctdb
, priority
, db_count_handler
, &nargs
);
633 /* Add extra argument for null termination */
636 args
= talloc_array(mem_ctx
, const char *, nargs
);
641 args
[0] = talloc_asprintf(args
, "%d", getpid());
642 args
[1] = talloc_asprintf(args
, "%d", fd
);
644 switch (lock_ctx
->type
) {
646 args
[2] = talloc_strdup(args
, "RECORD");
647 args
[3] = talloc_strdup(args
, lock_ctx
->ctdb_db
->db_path
);
648 args
[4] = talloc_asprintf(args
, "0x%x",
649 db_flags(lock_ctx
->ctdb_db
));
650 if (lock_ctx
->key
.dsize
== 0) {
651 args
[5] = talloc_strdup(args
, "NULL");
653 args
[5] = hex_encode_talloc(args
, lock_ctx
->key
.dptr
, lock_ctx
->key
.dsize
);
658 args
[2] = talloc_strdup(args
, "DB");
659 args
[3] = talloc_strdup(args
, lock_ctx
->ctdb_db
->db_path
);
660 args
[4] = talloc_asprintf(args
, "0x%x",
661 db_flags(lock_ctx
->ctdb_db
));
664 case LOCK_ALLDB_PRIO
:
665 args
[2] = talloc_strdup(args
, "DB");
668 ctdb_db_iterator(ctdb
, lock_ctx
->priority
, db_name_handler
, &list
);
672 args
[2] = talloc_strdup(args
, "DB");
675 for (priority
=1; priority
<NUM_DB_PRIORITIES
; priority
++) {
676 ctdb_db_iterator(ctdb
, priority
, db_name_handler
, &list
);
681 /* Make sure last argument is NULL */
682 args
[nargs
-1] = NULL
;
684 for (i
=0; i
<nargs
-1; i
++) {
685 if (args
[i
] == NULL
) {
697 * Find a lock request that can be scheduled
699 static struct lock_context
*ctdb_find_lock_context(struct ctdb_context
*ctdb
)
701 struct lock_context
*lock_ctx
, *next_ctx
;
702 struct ctdb_db_context
*ctdb_db
;
704 /* First check if there are database lock requests */
706 for (lock_ctx
= ctdb
->lock_pending
; lock_ctx
!= NULL
;
707 lock_ctx
= next_ctx
) {
709 if (lock_ctx
->request
!= NULL
) {
710 /* Found a lock context with a request */
714 next_ctx
= lock_ctx
->next
;
716 DEBUG(DEBUG_INFO
, ("Removing lock context without lock "
718 DLIST_REMOVE(ctdb
->lock_pending
, lock_ctx
);
719 CTDB_DECREMENT_STAT(ctdb
, locks
.num_pending
);
720 if (lock_ctx
->ctdb_db
) {
721 CTDB_DECREMENT_DB_STAT(lock_ctx
->ctdb_db
,
724 talloc_free(lock_ctx
);
727 /* Next check database queues */
728 for (ctdb_db
= ctdb
->db_list
; ctdb_db
; ctdb_db
= ctdb_db
->next
) {
729 if (ctdb_db
->lock_num_current
==
730 ctdb
->tunable
.lock_processes_per_db
) {
734 for (lock_ctx
= ctdb_db
->lock_pending
; lock_ctx
!= NULL
;
735 lock_ctx
= next_ctx
) {
737 next_ctx
= lock_ctx
->next
;
739 if (lock_ctx
->request
!= NULL
) {
743 DEBUG(DEBUG_INFO
, ("Removing lock context without "
745 DLIST_REMOVE(ctdb_db
->lock_pending
, lock_ctx
);
746 CTDB_DECREMENT_STAT(ctdb
, locks
.num_pending
);
747 CTDB_DECREMENT_DB_STAT(ctdb_db
, locks
.num_pending
);
748 talloc_free(lock_ctx
);
756 * Schedule a new lock child process
757 * Set up callback handler and timeout handler
759 static void ctdb_lock_schedule(struct ctdb_context
*ctdb
)
761 struct lock_context
*lock_ctx
;
764 static char prog
[PATH_MAX
+1] = "";
767 if (!ctdb_set_helper("lock helper",
770 CTDB_HELPER_BINDIR
, "ctdb_lock_helper")) {
771 ctdb_die(ctdb
, __location__
772 " Unable to set lock helper\n");
775 /* Find a lock context with requests */
776 lock_ctx
= ctdb_find_lock_context(ctdb
);
777 if (lock_ctx
== NULL
) {
781 lock_ctx
->child
= -1;
782 ret
= pipe(lock_ctx
->fd
);
784 DEBUG(DEBUG_ERR
, ("Failed to create pipe in ctdb_lock_schedule\n"));
788 set_close_on_exec(lock_ctx
->fd
[0]);
790 /* Create data for child process */
791 tmp_ctx
= talloc_new(lock_ctx
);
792 if (tmp_ctx
== NULL
) {
793 DEBUG(DEBUG_ERR
, ("Failed to allocate memory for helper args\n"));
794 close(lock_ctx
->fd
[0]);
795 close(lock_ctx
->fd
[1]);
799 /* Create arguments for lock helper */
800 if (!lock_helper_args(tmp_ctx
, lock_ctx
, lock_ctx
->fd
[1],
802 DEBUG(DEBUG_ERR
, ("Failed to create lock helper args\n"));
803 close(lock_ctx
->fd
[0]);
804 close(lock_ctx
->fd
[1]);
805 talloc_free(tmp_ctx
);
809 if (!ctdb_vfork_with_logging(lock_ctx
, ctdb
, "lock_helper",
810 prog
, argc
, (const char **)args
,
811 NULL
, NULL
, &lock_ctx
->child
)) {
812 DEBUG(DEBUG_ERR
, ("Failed to create a child in ctdb_lock_schedule\n"));
813 close(lock_ctx
->fd
[0]);
814 close(lock_ctx
->fd
[1]);
815 talloc_free(tmp_ctx
);
820 close(lock_ctx
->fd
[1]);
822 talloc_free(tmp_ctx
);
824 /* Set up timeout handler */
825 lock_ctx
->ttimer
= tevent_add_timer(ctdb
->ev
,
827 timeval_current_ofs(10, 0),
828 ctdb_lock_timeout_handler
,
830 if (lock_ctx
->ttimer
== NULL
) {
831 ctdb_kill(ctdb
, lock_ctx
->child
, SIGKILL
);
832 lock_ctx
->child
= -1;
833 close(lock_ctx
->fd
[0]);
837 /* Set up callback */
838 lock_ctx
->tfd
= tevent_add_fd(ctdb
->ev
,
844 if (lock_ctx
->tfd
== NULL
) {
845 TALLOC_FREE(lock_ctx
->ttimer
);
846 ctdb_kill(ctdb
, lock_ctx
->child
, SIGKILL
);
847 lock_ctx
->child
= -1;
848 close(lock_ctx
->fd
[0]);
851 tevent_fd_set_auto_close(lock_ctx
->tfd
);
853 /* Move the context from pending to current */
854 if (lock_ctx
->type
== LOCK_RECORD
) {
855 DLIST_REMOVE(lock_ctx
->ctdb_db
->lock_pending
, lock_ctx
);
856 DLIST_ADD_END(lock_ctx
->ctdb_db
->lock_current
, lock_ctx
, NULL
);
858 DLIST_REMOVE(ctdb
->lock_pending
, lock_ctx
);
859 DLIST_ADD_END(ctdb
->lock_current
, lock_ctx
, NULL
);
861 CTDB_DECREMENT_STAT(lock_ctx
->ctdb
, locks
.num_pending
);
862 CTDB_INCREMENT_STAT(lock_ctx
->ctdb
, locks
.num_current
);
863 if (lock_ctx
->ctdb_db
) {
864 lock_ctx
->ctdb_db
->lock_num_current
++;
865 CTDB_DECREMENT_DB_STAT(lock_ctx
->ctdb_db
, locks
.num_pending
);
866 CTDB_INCREMENT_DB_STAT(lock_ctx
->ctdb_db
, locks
.num_current
);
872 * Lock record / db depending on type
874 static struct lock_request
*ctdb_lock_internal(TALLOC_CTX
*mem_ctx
,
875 struct ctdb_context
*ctdb
,
876 struct ctdb_db_context
*ctdb_db
,
879 void (*callback
)(void *, bool),
884 struct lock_context
*lock_ctx
= NULL
;
885 struct lock_request
*request
;
887 if (callback
== NULL
) {
888 DEBUG(DEBUG_WARNING
, ("No callback function specified, not locking\n"));
892 lock_ctx
= talloc_zero(ctdb
, struct lock_context
);
893 if (lock_ctx
== NULL
) {
894 DEBUG(DEBUG_ERR
, ("Failed to create a new lock context\n"));
898 if ((request
= talloc_zero(mem_ctx
, struct lock_request
)) == NULL
) {
899 talloc_free(lock_ctx
);
903 lock_ctx
->type
= type
;
904 lock_ctx
->ctdb
= ctdb
;
905 lock_ctx
->ctdb_db
= ctdb_db
;
906 lock_ctx
->key
.dsize
= key
.dsize
;
908 lock_ctx
->key
.dptr
= talloc_memdup(lock_ctx
, key
.dptr
, key
.dsize
);
909 if (lock_ctx
->key
.dptr
== NULL
) {
910 DEBUG(DEBUG_ERR
, (__location__
"Memory allocation error\n"));
911 talloc_free(lock_ctx
);
912 talloc_free(request
);
915 lock_ctx
->key_hash
= ctdb_hash(&key
);
917 lock_ctx
->key
.dptr
= NULL
;
919 lock_ctx
->priority
= priority
;
920 lock_ctx
->auto_mark
= auto_mark
;
922 lock_ctx
->request
= request
;
923 lock_ctx
->child
= -1;
925 /* Non-record locks are required by recovery and should be scheduled
926 * immediately, so keep them at the head of the pending queue.
928 if (lock_ctx
->type
== LOCK_RECORD
) {
929 DLIST_ADD_END(ctdb_db
->lock_pending
, lock_ctx
, NULL
);
931 DLIST_ADD_END(ctdb
->lock_pending
, lock_ctx
, NULL
);
933 CTDB_INCREMENT_STAT(ctdb
, locks
.num_pending
);
935 CTDB_INCREMENT_DB_STAT(ctdb_db
, locks
.num_pending
);
938 /* Start the timer when we activate the context */
939 lock_ctx
->start_time
= timeval_current();
941 request
->lctx
= lock_ctx
;
942 request
->callback
= callback
;
943 request
->private_data
= private_data
;
945 talloc_set_destructor(request
, ctdb_lock_request_destructor
);
946 talloc_set_destructor(lock_ctx
, ctdb_lock_context_destructor
);
948 ctdb_lock_schedule(ctdb
);
955 * obtain a lock on a record in a database
957 struct lock_request
*ctdb_lock_record(TALLOC_CTX
*mem_ctx
,
958 struct ctdb_db_context
*ctdb_db
,
961 void (*callback
)(void *, bool),
964 return ctdb_lock_internal(mem_ctx
,
977 * obtain a lock on a database
979 struct lock_request
*ctdb_lock_db(TALLOC_CTX
*mem_ctx
,
980 struct ctdb_db_context
*ctdb_db
,
982 void (*callback
)(void *, bool),
985 return ctdb_lock_internal(mem_ctx
,
998 * obtain locks on all databases of specified priority
1000 struct lock_request
*ctdb_lock_alldb_prio(TALLOC_CTX
*mem_ctx
,
1001 struct ctdb_context
*ctdb
,
1004 void (*callback
)(void *, bool),
1007 if (priority
< 1 || priority
> NUM_DB_PRIORITIES
) {
1008 DEBUG(DEBUG_ERR
, ("Invalid db priority: %u\n", priority
));
1012 return ctdb_lock_internal(mem_ctx
,
1025 * obtain locks on all databases
1027 struct lock_request
*ctdb_lock_alldb(TALLOC_CTX
*mem_ctx
,
1028 struct ctdb_context
*ctdb
,
1030 void (*callback
)(void *, bool),
1033 return ctdb_lock_internal(mem_ctx
,