source: vendor/current/ctdb/server/ctdb_lock.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 25.4 KB
Line 
1/*
2 ctdb lock handling
3 provide API to do non-blocking locks for single or all databases
4
5 Copyright (C) Amitay Isaacs 2012
6
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.
11
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.
16
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/>.
19*/
20#include "replace.h"
21#include "system/filesys.h"
22#include "system/network.h"
23
24#include <talloc.h>
25#include <tevent.h>
26
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"
31
32#include "ctdb_private.h"
33
34#include "common/system.h"
35#include "common/common.h"
36#include "common/logging.h"
37
38/*
39 * Non-blocking Locking API
40 *
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.
46 *
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
51 *
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.
57 */
58
59enum lock_type {
60 LOCK_RECORD,
61 LOCK_DB,
62 LOCK_ALLDB_PRIO,
63 LOCK_ALLDB,
64};
65
66static const char * const lock_type_str[] = {
67 "lock_record",
68 "lock_db",
69 "lock_alldb_prio",
70 "lock_alldb",
71};
72
73struct lock_request;
74
75/* lock_context is the common part for a lock request */
76struct 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;
92};
93
94/* lock_request is the client specific part for a lock request */
95struct lock_request {
96 struct lock_context *lctx;
97 void (*callback)(void *, bool);
98 void *private_data;
99};
100
101
102/*
103 * Support samba 3.6.x (and older) versions which do not set db priority.
104 *
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.
107 */
108static bool later_db(struct ctdb_context *ctdb, const char *name)
109{
110 if (ctdb->tunable.samba3_hack == 0) {
111 return false;
112 }
113
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;
120 }
121
122 return false;
123}
124
125int ctdb_db_prio_iterator(struct ctdb_context *ctdb, uint32_t priority,
126 ctdb_db_handler_t handler, void *private_data)
127{
128 struct ctdb_db_context *ctdb_db;
129 int ret;
130
131 for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
132 if (ctdb_db->priority != priority) {
133 continue;
134 }
135 if (later_db(ctdb, ctdb_db->db_name)) {
136 continue;
137 }
138 ret = handler(ctdb_db, private_data);
139 if (ret != 0) {
140 return -1;
141 }
142 }
143
144 /* If priority != 1, later_db check is not required and can return */
145 if (priority != 1) {
146 return 0;
147 }
148
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;
152 }
153 ret = handler(ctdb_db, private_data);
154 if (ret != 0) {
155 return -1;
156 }
157 }
158
159 return 0;
160}
161
162int ctdb_db_iterator(struct ctdb_context *ctdb, ctdb_db_handler_t handler,
163 void *private_data)
164{
165 struct ctdb_db_context *ctdb_db;
166 int ret;
167
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;
172 }
173 }
174
175 return 0;
176}
177
178/*
179 * lock all databases - mark only
180 */
181static int db_lock_mark_handler(struct ctdb_db_context *ctdb_db,
182 void *private_data)
183{
184 int tdb_transaction_write_lock_mark(struct tdb_context *);
185
186 DEBUG(DEBUG_INFO, ("marking locked database %s\n", ctdb_db->db_name));
187
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;
192 }
193
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;
198 }
199
200 return 0;
201}
202
203int ctdb_lockdb_mark(struct ctdb_db_context *ctdb_db)
204{
205 if (!ctdb_db_frozen(ctdb_db)) {
206 DEBUG(DEBUG_ERR,
207 ("Attempt to mark database locked when not frozen\n"));
208 return -1;
209 }
210
211 return db_lock_mark_handler(ctdb_db, NULL);
212}
213
214int ctdb_lockall_mark_prio(struct ctdb_context *ctdb, uint32_t priority)
215{
216 /*
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.
220 */
221
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;
225 }
226
227 return ctdb_db_prio_iterator(ctdb, priority, db_lock_mark_handler, NULL);
228}
229
230static int ctdb_lockall_mark(struct ctdb_context *ctdb)
231{
232 uint32_t priority;
233
234 for (priority=1; priority<=NUM_DB_PRIORITIES; priority++) {
235 int ret;
236
237 ret = ctdb_db_prio_iterator(ctdb, priority,
238 db_lock_mark_handler, NULL);
239 if (ret != 0) {
240 return -1;
241 }
242 }
243
244 return 0;
245}
246
247
248/*
249 * lock all databases - unmark only
250 */
251static int db_lock_unmark_handler(struct ctdb_db_context *ctdb_db,
252 void *private_data)
253{
254 int tdb_transaction_write_lock_unmark(struct tdb_context *);
255
256 DEBUG(DEBUG_INFO, ("unmarking locked database %s\n", ctdb_db->db_name));
257
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;
262 }
263
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;
268 }
269
270 return 0;
271}
272
273int ctdb_lockdb_unmark(struct ctdb_db_context *ctdb_db)
274{
275 if (!ctdb_db_frozen(ctdb_db)) {
276 DEBUG(DEBUG_ERR,
277 ("Attempt to unmark database locked when not frozen\n"));
278 return -1;
279 }
280
281 return db_lock_unmark_handler(ctdb_db, NULL);
282}
283
284int ctdb_lockall_unmark_prio(struct ctdb_context *ctdb, uint32_t priority)
285{
286 /*
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.
290 */
291
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;
295 }
296
297 return ctdb_db_prio_iterator(ctdb, priority, db_lock_unmark_handler,
298 NULL);
299}
300
301static int ctdb_lockall_unmark(struct ctdb_context *ctdb)
302{
303 uint32_t priority;
304
305 for (priority=NUM_DB_PRIORITIES; priority>0; priority--) {
306 int ret;
307
308 ret = ctdb_db_prio_iterator(ctdb, priority,
309 db_lock_unmark_handler, NULL);
310 if (ret != 0) {
311 return -1;
312 }
313 }
314
315 return 0;
316}
317
318
319static void ctdb_lock_schedule(struct ctdb_context *ctdb);
320
321/*
322 * Destructor to kill the child locking process
323 */
324static int ctdb_lock_context_destructor(struct lock_context *lock_ctx)
325{
326 if (lock_ctx->request) {
327 lock_ctx->request->lctx = NULL;
328 }
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);
335 }
336 if (lock_ctx->ctdb_db) {
337 lock_ctx->ctdb_db->lock_num_current--;
338 }
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);
342 }
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);
348 }
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);
352 }
353 }
354
355 ctdb_lock_schedule(lock_ctx->ctdb);
356
357 return 0;
358}
359
360
361/*
362 * Destructor to remove lock request
363 */
364static int ctdb_lock_request_destructor(struct lock_request *lock_request)
365{
366 if (lock_request->lctx == NULL) {
367 return 0;
368 }
369
370 lock_request->lctx->request = NULL;
371 TALLOC_FREE(lock_request->lctx);
372
373 return 0;
374}
375
376/*
377 * Process all the callbacks waiting for lock
378 *
379 * If lock has failed, callback is executed with locked=false
380 */
381static void process_callbacks(struct lock_context *lock_ctx, bool locked)
382{
383 struct lock_request *request;
384 bool auto_mark = lock_ctx->auto_mark;
385
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;
391
392 case LOCK_DB:
393 ctdb_lockdb_mark(lock_ctx->ctdb_db);
394 break;
395
396 case LOCK_ALLDB_PRIO:
397 ctdb_lockall_mark_prio(lock_ctx->ctdb, lock_ctx->priority);
398 break;
399
400 case LOCK_ALLDB:
401 ctdb_lockall_mark(lock_ctx->ctdb);
402 break;
403 }
404 }
405
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.
410 */
411 request->lctx = NULL;
412 }
413
414 /* Since request may be freed in the callback, unset the request */
415 lock_ctx->request = NULL;
416
417 request->callback(request->private_data, locked);
418
419 if (!auto_mark) {
420 return;
421 }
422
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;
428
429 case LOCK_DB:
430 ctdb_lockdb_unmark(lock_ctx->ctdb_db);
431 break;
432
433 case LOCK_ALLDB_PRIO:
434 ctdb_lockall_unmark_prio(lock_ctx->ctdb, lock_ctx->priority);
435 break;
436
437 case LOCK_ALLDB:
438 ctdb_lockall_unmark(lock_ctx->ctdb);
439 break;
440 }
441 }
442
443 talloc_free(lock_ctx);
444}
445
446
447static int lock_bucket_id(double t)
448{
449 double ms = 1.e-3, s = 1;
450 int id;
451
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;
474 }
475
476 return id;
477}
478
479/*
480 * Callback routine when the required locks are obtained.
481 * Called from parent context
482 */
483static void ctdb_lock_handler(struct tevent_context *ev,
484 struct tevent_fd *tfd,
485 uint16_t flags,
486 void *private_data)
487{
488 struct lock_context *lock_ctx;
489 char c;
490 bool locked;
491 double t;
492 int id;
493
494 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
495
496 /* cancel the timeout event */
497 TALLOC_FREE(lock_ctx->ttimer);
498
499 t = timeval_elapsed(&lock_ctx->start_time);
500 id = lock_bucket_id(t);
501
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);
507 }
508
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);
513 }
514
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);
521
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]);
524 }
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);
529 }
530 }
531
532 process_callbacks(lock_ctx, locked);
533}
534
535
536/*
537 * Callback routine when required locks are not obtained within timeout
538 * Called from parent context
539 */
540static void ctdb_lock_timeout_handler(struct tevent_context *ev,
541 struct tevent_timer *ttimer,
542 struct timeval current_time,
543 void *private_data)
544{
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;
551
552 lock_ctx = talloc_get_type_abort(private_data, struct lock_context);
553 ctdb = lock_ctx->ctdb;
554
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;
559 }
560
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));
571 }
572
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);
581 }
582 ctdb_track_child(ctdb, pid);
583 } else {
584 DEBUG(DEBUG_WARNING,
585 (__location__
586 " Unable to setup lock debugging\n"));
587 }
588
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;
596 }
597
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);
605}
606
607
608static int db_count_handler(struct ctdb_db_context *ctdb_db, void *private_data)
609{
610 int *count = (int *)private_data;
611
612 (*count) += 2;
613
614 return 0;
615}
616
617static int db_flags(struct ctdb_db_context *ctdb_db)
618{
619 int tdb_flags = TDB_DEFAULT;
620
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);
624 }
625#endif
626 return tdb_flags;
627}
628
629struct db_namelist {
630 const char **names;
631 int n;
632};
633
634static int db_name_handler(struct ctdb_db_context *ctdb_db, void *private_data)
635{
636 struct db_namelist *list = (struct db_namelist *)private_data;
637
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;
642
643 return 0;
644}
645
646static bool lock_helper_args(TALLOC_CTX *mem_ctx,
647 struct lock_context *lock_ctx, int fd,
648 int *argc, const char ***argv)
649{
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;
655
656 switch (lock_ctx->type) {
657 case LOCK_RECORD:
658 nargs = 6;
659 break;
660
661 case LOCK_DB:
662 nargs = 5;
663 break;
664
665 case LOCK_ALLDB_PRIO:
666 nargs = 3;
667 ctdb_db_prio_iterator(ctdb, lock_ctx->priority,
668 db_count_handler, &nargs);
669 break;
670
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);
676 }
677 break;
678 }
679
680 /* Add extra argument for null termination */
681 nargs++;
682
683 args = talloc_array(mem_ctx, const char *, nargs);
684 if (args == NULL) {
685 return false;
686 }
687
688 args[0] = talloc_asprintf(args, "%d", getpid());
689 args[1] = talloc_asprintf(args, "%d", fd);
690
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);
701 }
702 break;
703
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;
710
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;
718
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);
726 }
727 break;
728 }
729
730 /* Make sure last argument is NULL */
731 args[nargs-1] = NULL;
732
733 for (i=0; i<nargs-1; i++) {
734 if (args[i] == NULL) {
735 talloc_free(args);
736 return false;
737 }
738 }
739
740 *argc = nargs;
741 *argv = args;
742 return true;
743}
744
745/*
746 * Find a lock request that can be scheduled
747 */
748static struct lock_context *ctdb_find_lock_context(struct ctdb_context *ctdb)
749{
750 struct lock_context *lock_ctx, *next_ctx;
751 struct ctdb_db_context *ctdb_db;
752
753 /* First check if there are database lock requests */
754
755 for (lock_ctx = ctdb->lock_pending; lock_ctx != NULL;
756 lock_ctx = next_ctx) {
757
758 if (lock_ctx->request != NULL) {
759 /* Found a lock context with a request */
760 return lock_ctx;
761 }
762
763 next_ctx = lock_ctx->next;
764
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);
772 }
773 talloc_free(lock_ctx);
774 }
775
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;
781 }
782
783 for (lock_ctx = ctdb_db->lock_pending; lock_ctx != NULL;
784 lock_ctx = next_ctx) {
785
786 next_ctx = lock_ctx->next;
787
788 if (lock_ctx->request != NULL) {
789 return lock_ctx;
790 }
791
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);
798 }
799 }
800
801 return NULL;
802}
803
804/*
805 * Schedule a new lock child process
806 * Set up callback handler and timeout handler
807 */
808static void ctdb_lock_schedule(struct ctdb_context *ctdb)
809{
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;
815
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");
822 }
823
824 /* Find a lock context with requests */
825 lock_ctx = ctdb_find_lock_context(ctdb);
826 if (lock_ctx == NULL) {
827 return;
828 }
829
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;
835 }
836
837 set_close_on_exec(lock_ctx->fd[0]);
838
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;
846 }
847
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;
856 }
857
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;
866 }
867
868 /* Parent process */
869 close(lock_ctx->fd[1]);
870
871 talloc_free(tmp_ctx);
872
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;
884 }
885
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;
899 }
900 tevent_fd_set_auto_close(lock_ctx->tfd);
901
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);
906 } else {
907 DLIST_REMOVE(ctdb->lock_pending, lock_ctx);
908 DLIST_ADD_END(ctdb->lock_current, lock_ctx);
909 }
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);
916 }
917}
918
919
920/*
921 * Lock record / db depending on type
922 */
923static 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)
932{
933 struct lock_context *lock_ctx = NULL;
934 struct lock_request *request;
935
936 if (callback == NULL) {
937 DEBUG(DEBUG_WARNING, ("No callback function specified, not locking\n"));
938 return NULL;
939 }
940
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;
945 }
946
947 if ((request = talloc_zero(mem_ctx, struct lock_request)) == NULL) {
948 talloc_free(lock_ctx);
949 return NULL;
950 }
951
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;
963 }
964 lock_ctx->key_hash = ctdb_hash(&key);
965 } else {
966 lock_ctx->key.dptr = NULL;
967 }
968 lock_ctx->priority = priority;
969 lock_ctx->auto_mark = auto_mark;
970
971 lock_ctx->request = request;
972 lock_ctx->child = -1;
973
974 /* Non-record locks are required by recovery and should be scheduled
975 * immediately, so keep them at the head of the pending queue.
976 */
977 if (lock_ctx->type == LOCK_RECORD) {
978 DLIST_ADD_END(ctdb_db->lock_pending, lock_ctx);
979 } else {
980 DLIST_ADD_END(ctdb->lock_pending, lock_ctx);
981 }
982 CTDB_INCREMENT_STAT(ctdb, locks.num_pending);
983 if (ctdb_db) {
984 CTDB_INCREMENT_DB_STAT(ctdb_db, locks.num_pending);
985 }
986
987 /* Start the timer when we activate the context */
988 lock_ctx->start_time = timeval_current();
989
990 request->lctx = lock_ctx;
991 request->callback = callback;
992 request->private_data = private_data;
993
994 talloc_set_destructor(request, ctdb_lock_request_destructor);
995 talloc_set_destructor(lock_ctx, ctdb_lock_context_destructor);
996
997 ctdb_lock_schedule(ctdb);
998
999 return request;
1000}
1001
1002
1003/*
1004 * obtain a lock on a record in a database
1005 */
1006struct 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)
1012{
1013 return ctdb_lock_internal(mem_ctx,
1014 ctdb_db->ctdb,
1015 ctdb_db,
1016 key,
1017 0,
1018 callback,
1019 private_data,
1020 LOCK_RECORD,
1021 auto_mark);
1022}
1023
1024
1025/*
1026 * obtain a lock on a database
1027 */
1028struct 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)
1033{
1034 return ctdb_lock_internal(mem_ctx,
1035 ctdb_db->ctdb,
1036 ctdb_db,
1037 tdb_null,
1038 0,
1039 callback,
1040 private_data,
1041 LOCK_DB,
1042 auto_mark);
1043}
1044
1045
1046/*
1047 * obtain locks on all databases of specified priority
1048 */
1049struct 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)
1055{
1056 if (priority < 1 || priority > NUM_DB_PRIORITIES) {
1057 DEBUG(DEBUG_ERR, ("Invalid db priority: %u\n", priority));
1058 return NULL;
1059 }
1060
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);
1070}
1071
1072
1073/*
1074 * obtain locks on all databases
1075 */
1076struct 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)
1081{
1082 return ctdb_lock_internal(mem_ctx,
1083 ctdb,
1084 NULL,
1085 tdb_null,
1086 0,
1087 callback,
1088 private_data,
1089 LOCK_ALLDB,
1090 auto_mark);
1091}
1092
Note: See TracBrowser for help on using the repository browser.