2 Unix SMB/CIFS implementation.
3 global locks based on dbwrap and messaging
4 Copyright (C) 2009 by Volker Lendecke
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
24 #include "ctdbd_conn.h"
25 #include "../lib/util/select.h"
26 #include "system/select.h"
29 static NTSTATUS
g_lock_force_unlock(struct g_lock_ctx
*ctx
, const char *name
,
30 struct server_id pid
);
33 struct db_context
*db
;
34 struct messaging_context
*msg
;
38 * The "g_lock.tdb" file contains records, indexed by the 0-terminated
39 * lockname. The record contains an array of "struct g_lock_rec"
40 * structures. Waiters have the lock_type with G_LOCK_PENDING or'ed.
44 enum g_lock_type lock_type
;
48 struct g_lock_ctx
*g_lock_ctx_init(TALLOC_CTX
*mem_ctx
,
49 struct messaging_context
*msg
)
51 struct g_lock_ctx
*result
;
53 result
= talloc(mem_ctx
, struct g_lock_ctx
);
59 result
->db
= db_open(result
, lock_path("g_lock.tdb"), 0,
60 TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
61 O_RDWR
|O_CREAT
, 0600);
62 if (result
->db
== NULL
) {
63 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb"));
70 static bool g_lock_conflicts(enum g_lock_type lock_type
,
71 const struct g_lock_rec
*rec
)
73 enum g_lock_type rec_lock
= rec
->lock_type
;
75 if ((rec_lock
& G_LOCK_PENDING
) != 0) {
80 * Only tested write locks so far. Very likely this routine
81 * needs to be fixed for read locks....
83 if ((lock_type
== G_LOCK_READ
) && (rec_lock
== G_LOCK_READ
)) {
89 static bool g_lock_parse(TALLOC_CTX
*mem_ctx
, TDB_DATA data
,
90 int *pnum_locks
, struct g_lock_rec
**plocks
)
93 struct g_lock_rec
*locks
;
95 if ((data
.dsize
% sizeof(struct g_lock_rec
)) != 0) {
96 DEBUG(1, ("invalid lock record length %d\n", (int)data
.dsize
));
100 num_locks
= data
.dsize
/ sizeof(struct g_lock_rec
);
101 locks
= talloc_array(mem_ctx
, struct g_lock_rec
, num_locks
);
103 DEBUG(1, ("talloc failed\n"));
107 memcpy(locks
, data
.dptr
, data
.dsize
);
109 DEBUG(10, ("locks:\n"));
110 for (i
=0; i
<num_locks
; i
++) {
111 DEBUGADD(10, ("%s: %s %s\n",
112 server_id_str(talloc_tos(), &locks
[i
].pid
),
113 ((locks
[i
].lock_type
& 1) == G_LOCK_READ
) ?
115 (locks
[i
].lock_type
& G_LOCK_PENDING
) ?
116 "(pending)" : "(owner)"));
118 if (((locks
[i
].lock_type
& G_LOCK_PENDING
) == 0)
119 && !process_exists(locks
[i
].pid
)) {
121 DEBUGADD(10, ("lock owner %s died -- discarding\n",
122 server_id_str(talloc_tos(),
125 if (i
< (num_locks
-1)) {
126 locks
[i
] = locks
[num_locks
-1];
133 *pnum_locks
= num_locks
;
137 static void g_lock_cleanup(int *pnum_locks
, struct g_lock_rec
*locks
)
141 num_locks
= *pnum_locks
;
143 DEBUG(10, ("g_lock_cleanup: %d locks\n", num_locks
));
145 for (i
=0; i
<num_locks
; i
++) {
146 if (process_exists(locks
[i
].pid
)) {
149 DEBUGADD(10, ("%s does not exist -- discarding\n",
150 server_id_str(talloc_tos(), &locks
[i
].pid
)));
152 if (i
< (num_locks
-1)) {
153 locks
[i
] = locks
[num_locks
-1];
157 *pnum_locks
= num_locks
;
161 static struct g_lock_rec
*g_lock_addrec(TALLOC_CTX
*mem_ctx
,
162 struct g_lock_rec
*locks
,
164 const struct server_id pid
,
165 enum g_lock_type lock_type
)
167 struct g_lock_rec
*result
;
168 int num_locks
= *pnum_locks
;
170 result
= talloc_realloc(mem_ctx
, locks
, struct g_lock_rec
,
172 if (result
== NULL
) {
176 result
[num_locks
].pid
= pid
;
177 result
[num_locks
].lock_type
= lock_type
;
182 static void g_lock_got_retry(struct messaging_context
*msg
,
185 struct server_id server_id
,
188 static NTSTATUS
g_lock_trylock(struct g_lock_ctx
*ctx
, const char *name
,
189 enum g_lock_type lock_type
)
191 struct db_record
*rec
= NULL
;
192 struct g_lock_rec
*locks
= NULL
;
194 struct server_id self
;
197 NTSTATUS status
= NT_STATUS_OK
;
198 NTSTATUS store_status
;
201 rec
= ctx
->db
->fetch_locked(ctx
->db
, talloc_tos(),
202 string_term_tdb_data(name
));
204 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name
));
205 status
= NT_STATUS_LOCK_NOT_GRANTED
;
209 if (!g_lock_parse(talloc_tos(), rec
->value
, &num_locks
, &locks
)) {
210 DEBUG(10, ("g_lock_parse for %s failed\n", name
));
211 status
= NT_STATUS_INTERNAL_ERROR
;
215 self
= messaging_server_id(ctx
->msg
);
218 for (i
=0; i
<num_locks
; i
++) {
219 if (procid_equal(&self
, &locks
[i
].pid
)) {
220 if (our_index
!= -1) {
221 DEBUG(1, ("g_lock_trylock: Added ourself "
223 status
= NT_STATUS_INTERNAL_ERROR
;
226 if ((locks
[i
].lock_type
& G_LOCK_PENDING
) == 0) {
227 DEBUG(1, ("g_lock_trylock: Found ourself not "
229 status
= NT_STATUS_INTERNAL_ERROR
;
235 /* never conflict with ourself */
238 if (g_lock_conflicts(lock_type
, &locks
[i
])) {
239 struct server_id pid
= locks
[i
].pid
;
241 if (!process_exists(pid
)) {
244 status
= g_lock_force_unlock(ctx
, name
, pid
);
245 if (!NT_STATUS_IS_OK(status
)) {
246 DEBUG(1, ("Could not unlock dead lock "
252 lock_type
|= G_LOCK_PENDING
;
256 if (our_index
== -1) {
257 /* First round, add ourself */
259 locks
= g_lock_addrec(talloc_tos(), locks
, &num_locks
,
262 DEBUG(10, ("g_lock_addrec failed\n"));
263 status
= NT_STATUS_NO_MEMORY
;
268 * Retry. We were pending last time. Overwrite the
269 * stored lock_type with what we calculated, we might
270 * have acquired the lock this time.
272 locks
[our_index
].lock_type
= lock_type
;
275 if (NT_STATUS_IS_OK(status
) && ((lock_type
& G_LOCK_PENDING
) == 0)) {
277 * Walk through the list of locks, search for dead entries
279 g_lock_cleanup(&num_locks
, locks
);
282 data
= make_tdb_data((uint8_t *)locks
, num_locks
* sizeof(*locks
));
283 store_status
= rec
->store(rec
, data
, 0);
284 if (!NT_STATUS_IS_OK(store_status
)) {
285 DEBUG(1, ("rec->store failed: %s\n",
286 nt_errstr(store_status
)));
287 status
= store_status
;
294 if (NT_STATUS_IS_OK(status
) && (lock_type
& G_LOCK_PENDING
) != 0) {
295 return STATUS_PENDING
;
301 NTSTATUS
g_lock_lock(struct g_lock_ctx
*ctx
, const char *name
,
302 enum g_lock_type lock_type
, struct timeval timeout
)
304 struct tevent_timer
*te
= NULL
;
307 struct timeval timeout_end
;
308 struct timeval time_now
;
310 DEBUG(10, ("Trying to acquire lock %d for %s\n", (int)lock_type
,
313 if (lock_type
& ~1) {
314 DEBUG(1, ("Got invalid lock type %d for %s\n",
315 (int)lock_type
, name
));
316 return NT_STATUS_INVALID_PARAMETER
;
319 #ifdef CLUSTER_SUPPORT
320 if (lp_clustering()) {
321 status
= ctdb_watch_us(messaging_ctdbd_connection());
322 if (!NT_STATUS_IS_OK(status
)) {
323 DEBUG(10, ("could not register retry with ctdb: %s\n",
330 status
= messaging_register(ctx
->msg
, &retry
, MSG_DBWRAP_G_LOCK_RETRY
,
332 if (!NT_STATUS_IS_OK(status
)) {
333 DEBUG(10, ("messaging_register failed: %s\n",
338 time_now
= timeval_current();
339 timeout_end
= timeval_sum(&time_now
, &timeout
);
342 struct pollfd
*pollfds
;
346 struct timeval timeout_remaining
, select_timeout
;
348 status
= g_lock_trylock(ctx
, name
, lock_type
);
349 if (NT_STATUS_IS_OK(status
)) {
350 DEBUG(10, ("Got lock %s\n", name
));
353 if (!NT_STATUS_EQUAL(status
, STATUS_PENDING
)) {
354 DEBUG(10, ("g_lock_trylock failed: %s\n",
359 DEBUG(10, ("g_lock_trylock: Did not get lock, waiting...\n"));
361 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
362 * !!! HACK ALERT --- FIX ME !!!
363 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
364 * What we really want to do here is to react to
365 * MSG_DBWRAP_G_LOCK_RETRY messages that are either sent
366 * by a client doing g_lock_unlock or by ourselves when
367 * we receive a CTDB_SRVID_SAMBA_NOTIFY or
368 * CTDB_SRVID_RECONFIGURE message from ctdbd, i.e. when
369 * either a client holding a lock or a complete node
372 * Doing this properly involves calling tevent_loop_once(),
373 * but doing this here with the main ctdbd messaging context
374 * creates a nested event loop when g_lock_lock() is called
375 * from the main event loop, e.g. in a tcon_and_X where the
376 * share_info.tdb needs to be initialized and is locked by
377 * another process, or when the remore registry is accessed
378 * for writing and some other process already holds a lock
379 * on the registry.tdb.
381 * So as a quick fix, we act a little coarsely here: we do
382 * a select on the ctdb connection fd and when it is readable
383 * or we get EINTR, then we retry without actually parsing
384 * any ctdb packages or dispatching messages. This means that
385 * we retry more often than intended by design, but this does
386 * not harm and it is unobtrusive. When we have finished,
387 * the main loop will pick up all the messages and ctdb
388 * packets. The only extra twist is that we cannot use timed
389 * events here but have to handcode a timeout.
393 * We allocate 2 entries here. One is needed anyway for
394 * sys_poll and in the clustering case we might have to add
395 * the ctdb fd. This avoids the realloc then.
397 pollfds
= talloc_array(talloc_tos(), struct pollfd
, 2);
398 if (pollfds
== NULL
) {
399 status
= NT_STATUS_NO_MEMORY
;
404 #ifdef CLUSTER_SUPPORT
405 if (lp_clustering()) {
406 struct ctdbd_connection
*conn
;
407 conn
= messaging_ctdbd_connection();
409 pollfds
[0].fd
= ctdbd_conn_get_fd(conn
);
410 pollfds
[0].events
= POLLIN
|POLLHUP
;
416 time_now
= timeval_current();
417 timeout_remaining
= timeval_until(&time_now
, &timeout_end
);
418 select_timeout
= timeval_set(60, 0);
420 select_timeout
= timeval_min(&select_timeout
,
423 ret
= sys_poll(pollfds
, num_pollfds
,
424 timeval_to_msec(select_timeout
));
427 * We're not *really interested in the actual flags. We just
428 * need to retry this whole thing.
431 TALLOC_FREE(pollfds
);
435 if (errno
!= EINTR
) {
436 DEBUG(1, ("error calling select: %s\n",
438 status
= NT_STATUS_INTERNAL_ERROR
;
443 * This means a signal was received.
444 * It might have been a MSG_DBWRAP_G_LOCK_RETRY message.
447 } else if (ret
== 0) {
448 if (timeval_expired(&timeout_end
)) {
449 DEBUG(10, ("g_lock_lock timed out\n"));
450 status
= NT_STATUS_LOCK_NOT_GRANTED
;
453 DEBUG(10, ("select returned 0 but timeout not "
454 "not expired, retrying\n"));
456 } else if (ret
!= 1) {
457 DEBUG(1, ("invalid return code of select: %d\n", ret
));
458 status
= NT_STATUS_INTERNAL_ERROR
;
463 * This means ctdbd has sent us some data.
464 * Might be a CTDB_SRVID_RECONFIGURE or a
465 * CTDB_SRVID_SAMBA_NOTIFY message.
470 #ifdef CLUSTER_SUPPORT
474 if (!NT_STATUS_IS_OK(status
)) {
475 NTSTATUS unlock_status
;
477 unlock_status
= g_lock_unlock(ctx
, name
);
479 if (!NT_STATUS_IS_OK(unlock_status
)) {
480 DEBUG(1, ("Could not remove ourself from the locking "
481 "db: %s\n", nt_errstr(status
)));
485 messaging_deregister(ctx
->msg
, MSG_DBWRAP_G_LOCK_RETRY
, &retry
);
491 static void g_lock_got_retry(struct messaging_context
*msg
,
494 struct server_id server_id
,
497 bool *pretry
= (bool *)private_data
;
499 DEBUG(10, ("Got retry message from pid %s\n",
500 server_id_str(talloc_tos(), &server_id
)));
505 static NTSTATUS
g_lock_force_unlock(struct g_lock_ctx
*ctx
, const char *name
,
506 struct server_id pid
)
508 struct db_record
*rec
= NULL
;
509 struct g_lock_rec
*locks
= NULL
;
511 enum g_lock_type lock_type
;
514 rec
= ctx
->db
->fetch_locked(ctx
->db
, talloc_tos(),
515 string_term_tdb_data(name
));
517 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name
));
518 status
= NT_STATUS_INTERNAL_ERROR
;
522 if (!g_lock_parse(talloc_tos(), rec
->value
, &num_locks
, &locks
)) {
523 DEBUG(10, ("g_lock_parse for %s failed\n", name
));
524 status
= NT_STATUS_INTERNAL_ERROR
;
528 for (i
=0; i
<num_locks
; i
++) {
529 if (procid_equal(&pid
, &locks
[i
].pid
)) {
534 if (i
== num_locks
) {
535 DEBUG(10, ("g_lock_force_unlock: Lock not found\n"));
536 status
= NT_STATUS_INTERNAL_ERROR
;
540 lock_type
= locks
[i
].lock_type
;
542 if (i
< (num_locks
-1)) {
543 locks
[i
] = locks
[num_locks
-1];
547 if (num_locks
== 0) {
548 status
= rec
->delete_rec(rec
);
551 data
= make_tdb_data((uint8_t *)locks
,
552 sizeof(struct g_lock_rec
) * num_locks
);
553 status
= rec
->store(rec
, data
, 0);
556 if (!NT_STATUS_IS_OK(status
)) {
557 DEBUG(1, ("g_lock_force_unlock: Could not store record: %s\n",
564 if ((lock_type
& G_LOCK_PENDING
) == 0) {
568 * We've been the lock holder. Others to retry. Don't
569 * tell all others to avoid a thundering herd. In case
570 * this leads to a complete stall because we miss some
571 * processes, the loop in g_lock_lock tries at least
575 for (i
=0; i
<num_locks
; i
++) {
576 if ((locks
[i
].lock_type
& G_LOCK_PENDING
) == 0) {
579 if (!process_exists(locks
[i
].pid
)) {
584 * Ping all waiters to retry
586 status
= messaging_send(ctx
->msg
, locks
[i
].pid
,
587 MSG_DBWRAP_G_LOCK_RETRY
,
589 if (!NT_STATUS_IS_OK(status
)) {
590 DEBUG(1, ("sending retry to %s failed: %s\n",
591 server_id_str(talloc_tos(),
597 if (num_wakeups
> 5) {
604 * For the error path, TALLOC_FREE(rec) as well. In the good
605 * path we have already freed it.
613 NTSTATUS
g_lock_unlock(struct g_lock_ctx
*ctx
, const char *name
)
617 status
= g_lock_force_unlock(ctx
, name
, messaging_server_id(ctx
->msg
));
619 #ifdef CLUSTER_SUPPORT
620 if (lp_clustering()) {
621 ctdb_unwatch(messaging_ctdbd_connection());
627 struct g_lock_locks_state
{
628 int (*fn
)(const char *name
, void *private_data
);
632 static int g_lock_locks_fn(struct db_record
*rec
, void *priv
)
634 struct g_lock_locks_state
*state
= (struct g_lock_locks_state
*)priv
;
636 if ((rec
->key
.dsize
== 0) || (rec
->key
.dptr
[rec
->key
.dsize
-1] != 0)) {
637 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
640 return state
->fn((char *)rec
->key
.dptr
, state
->private_data
);
643 int g_lock_locks(struct g_lock_ctx
*ctx
,
644 int (*fn
)(const char *name
, void *private_data
),
647 struct g_lock_locks_state state
;
650 state
.private_data
= private_data
;
652 return ctx
->db
->traverse_read(ctx
->db
, g_lock_locks_fn
, &state
);
655 NTSTATUS
g_lock_dump(struct g_lock_ctx
*ctx
, const char *name
,
656 int (*fn
)(struct server_id pid
,
657 enum g_lock_type lock_type
,
663 struct g_lock_rec
*locks
= NULL
;
666 if (ctx
->db
->fetch(ctx
->db
, talloc_tos(), string_term_tdb_data(name
),
668 return NT_STATUS_NOT_FOUND
;
671 if ((data
.dsize
== 0) || (data
.dptr
== NULL
)) {
675 ret
= g_lock_parse(talloc_tos(), data
, &num_locks
, &locks
);
677 TALLOC_FREE(data
.dptr
);
680 DEBUG(10, ("g_lock_parse for %s failed\n", name
));
681 return NT_STATUS_INTERNAL_ERROR
;
684 for (i
=0; i
<num_locks
; i
++) {
685 if (fn(locks
[i
].pid
, locks
[i
].lock_type
, private_data
) != 0) {
693 struct g_lock_get_state
{
695 struct server_id
*pid
;
698 static int g_lock_get_fn(struct server_id pid
, enum g_lock_type lock_type
,
701 struct g_lock_get_state
*state
= (struct g_lock_get_state
*)priv
;
703 if ((lock_type
& G_LOCK_PENDING
) != 0) {
712 NTSTATUS
g_lock_get(struct g_lock_ctx
*ctx
, const char *name
,
713 struct server_id
*pid
)
715 struct g_lock_get_state state
;
721 status
= g_lock_dump(ctx
, name
, g_lock_get_fn
, &state
);
722 if (!NT_STATUS_IS_OK(status
)) {
726 return NT_STATUS_NOT_FOUND
;
731 static bool g_lock_init_all(TALLOC_CTX
*mem_ctx
,
732 struct tevent_context
**pev
,
733 struct messaging_context
**pmsg
,
734 const struct server_id self
,
735 struct g_lock_ctx
**pg_ctx
)
737 struct tevent_context
*ev
= NULL
;
738 struct messaging_context
*msg
= NULL
;
739 struct g_lock_ctx
*g_ctx
= NULL
;
741 ev
= tevent_context_init(mem_ctx
);
743 d_fprintf(stderr
, "ERROR: could not init event context\n");
746 msg
= messaging_init(mem_ctx
, self
, ev
);
748 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
751 g_ctx
= g_lock_ctx_init(mem_ctx
, msg
);
753 d_fprintf(stderr
, "ERROR: could not init g_lock context\n");
768 NTSTATUS
g_lock_do(const char *name
, enum g_lock_type lock_type
,
769 struct timeval timeout
, const struct server_id self
,
770 void (*fn
)(void *private_data
), void *private_data
)
772 struct tevent_context
*ev
= NULL
;
773 struct messaging_context
*msg
= NULL
;
774 struct g_lock_ctx
*g_ctx
= NULL
;
777 if (!g_lock_init_all(talloc_tos(), &ev
, &msg
, self
, &g_ctx
)) {
778 status
= NT_STATUS_ACCESS_DENIED
;
782 status
= g_lock_lock(g_ctx
, name
, lock_type
, timeout
);
783 if (!NT_STATUS_IS_OK(status
)) {
787 g_lock_unlock(g_ctx
, name
);