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"
22 #include "lib/util/server_id.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "dbwrap/dbwrap_watch.h"
28 #include "../lib/util/tevent_ntstatus.h"
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"
43 #define G_LOCK_REC_LENGTH (SERVER_ID_BUF_LENGTH+1)
45 static void g_lock_rec_put(uint8_t buf
[G_LOCK_REC_LENGTH
],
46 const struct g_lock_rec rec
)
48 SCVAL(buf
, 0, rec
.lock_type
);
49 server_id_put(buf
+1, rec
.pid
);
52 static void g_lock_rec_get(struct g_lock_rec
*rec
,
53 const uint8_t buf
[G_LOCK_REC_LENGTH
])
55 rec
->lock_type
= CVAL(buf
, 0);
56 server_id_get(&rec
->pid
, buf
+1);
66 static bool g_lock_parse(uint8_t *buf
, size_t buflen
, struct g_lock
*lck
)
68 size_t found_recs
, data_ofs
;
70 if (buflen
< sizeof(uint32_t)) {
71 *lck
= (struct g_lock
) {0};
75 found_recs
= IVAL(buf
, 0);
77 buf
+= sizeof(uint32_t);
78 buflen
-= sizeof(uint32_t);
79 if (found_recs
> buflen
/G_LOCK_REC_LENGTH
) {
83 data_ofs
= found_recs
* G_LOCK_REC_LENGTH
;
85 *lck
= (struct g_lock
) {
86 .recsbuf
= buf
, .num_recs
= found_recs
,
87 .data
= buf
+data_ofs
, .datalen
= buflen
-data_ofs
93 static void g_lock_get_rec(struct g_lock
*lck
, size_t i
,
94 struct g_lock_rec
*rec
)
96 if (i
>= lck
->num_recs
) {
99 g_lock_rec_get(rec
, lck
->recsbuf
+ i
*G_LOCK_REC_LENGTH
);
102 static void g_lock_rec_del(struct g_lock
*lck
, size_t i
)
104 if (i
>= lck
->num_recs
) {
108 if (i
< lck
->num_recs
) {
109 uint8_t *recptr
= lck
->recsbuf
+ i
*G_LOCK_REC_LENGTH
;
110 memcpy(recptr
, lck
->recsbuf
+ lck
->num_recs
*G_LOCK_REC_LENGTH
,
115 static NTSTATUS
g_lock_store(struct db_record
*rec
, struct g_lock
*lck
,
116 struct g_lock_rec
*add
)
119 uint8_t addbuf
[G_LOCK_REC_LENGTH
];
121 struct TDB_DATA dbufs
[] = {
122 { .dptr
= sizebuf
, .dsize
= sizeof(sizebuf
) },
123 { .dptr
= lck
->recsbuf
,
124 .dsize
= lck
->num_recs
* G_LOCK_REC_LENGTH
},
126 { .dptr
= lck
->data
, .dsize
= lck
->datalen
}
130 g_lock_rec_put(addbuf
, *add
);
132 dbufs
[2] = (TDB_DATA
) {
133 .dptr
= addbuf
, .dsize
= G_LOCK_REC_LENGTH
139 SIVAL(sizebuf
, 0, lck
->num_recs
);
141 return dbwrap_record_storev(rec
, dbufs
, ARRAY_SIZE(dbufs
), 0);
144 struct g_lock_ctx
*g_lock_ctx_init(TALLOC_CTX
*mem_ctx
,
145 struct messaging_context
*msg
)
147 struct g_lock_ctx
*result
;
148 struct db_context
*backend
;
151 result
= talloc(mem_ctx
, struct g_lock_ctx
);
152 if (result
== NULL
) {
157 db_path
= lock_path("g_lock.tdb");
158 if (db_path
== NULL
) {
163 backend
= db_open(result
, db_path
, 0,
164 TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
165 O_RDWR
|O_CREAT
, 0600,
168 TALLOC_FREE(db_path
);
169 if (backend
== NULL
) {
170 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb\n"));
175 result
->db
= db_open_watched(result
, backend
, msg
);
176 if (result
->db
== NULL
) {
177 DBG_WARNING("g_lock_init: db_open_watched failed\n");
184 static bool g_lock_conflicts(enum g_lock_type l1
, enum g_lock_type l2
)
187 * Only tested write locks so far. Very likely this routine
188 * needs to be fixed for read locks....
190 if ((l1
== G_LOCK_READ
) && (l2
== G_LOCK_READ
)) {
196 static NTSTATUS
g_lock_trylock(struct db_record
*rec
, struct server_id self
,
197 enum g_lock_type type
,
198 struct server_id
*blocker
)
203 struct g_lock_rec _mylock
;
204 struct g_lock_rec
*mylock
= NULL
;
206 bool modified
= false;
209 data
= dbwrap_record_get_value(rec
);
211 ok
= g_lock_parse(data
.dptr
, data
.dsize
, &lck
);
213 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
216 if ((type
== G_LOCK_READ
) && (lck
.num_recs
> 0)) {
217 struct g_lock_rec check_rec
;
220 * Read locks can stay around forever if the process
221 * dies. Do a heuristic check for process existence:
222 * Check one random process for existence. Hopefully
223 * this will keep runaway read locks under control.
225 i
= generate_random() % lck
.num_recs
;
227 g_lock_get_rec(&lck
, i
, &check_rec
);
229 if (!serverid_exists(&check_rec
.pid
)) {
230 g_lock_rec_del(&lck
, i
);
237 while (i
< lck
.num_recs
) {
238 struct g_lock_rec lock
;
240 g_lock_get_rec(&lck
, i
, &lock
);
242 if (serverid_equal(&self
, &lock
.pid
)) {
243 if (lock
.lock_type
== type
) {
244 status
= NT_STATUS_WAS_LOCKED
;
247 if (mylock
!= NULL
) {
248 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
254 * Remove "our" lock entry. Re-add it later
255 * with our new lock type.
257 g_lock_rec_del(&lck
, i
);
262 if (g_lock_conflicts(type
, lock
.lock_type
)) {
263 struct server_id pid
= lock
.pid
;
266 * As the serverid_exists might recurse into
267 * the g_lock code, we use
268 * SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
270 pid
.unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
;
272 if (serverid_exists(&pid
)) {
273 status
= NT_STATUS_LOCK_NOT_GRANTED
;
279 * Delete stale conflicting entry
281 g_lock_rec_del(&lck
, i
);
290 _mylock
= (struct g_lock_rec
) {
296 status
= NT_STATUS_OK
;
299 NTSTATUS store_status
;
301 store_status
= g_lock_store(rec
, &lck
, mylock
);
302 if (!NT_STATUS_IS_OK(store_status
)) {
303 DBG_WARNING("g_lock_record_store failed: %s\n",
304 nt_errstr(store_status
));
305 status
= store_status
;
311 struct g_lock_lock_state
{
312 struct tevent_context
*ev
;
313 struct g_lock_ctx
*ctx
;
315 enum g_lock_type type
;
318 static void g_lock_lock_retry(struct tevent_req
*subreq
);
320 struct g_lock_lock_fn_state
{
321 struct g_lock_lock_state
*state
;
322 struct server_id self
;
324 struct tevent_req
*watch_req
;
328 static void g_lock_lock_fn(struct db_record
*rec
, void *private_data
)
330 struct g_lock_lock_fn_state
*state
= private_data
;
331 struct server_id blocker
;
333 state
->status
= g_lock_trylock(rec
, state
->self
, state
->state
->type
,
335 if (!NT_STATUS_EQUAL(state
->status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
339 state
->watch_req
= dbwrap_watched_watch_send(
340 state
->state
, state
->state
->ev
, rec
, blocker
);
343 struct tevent_req
*g_lock_lock_send(TALLOC_CTX
*mem_ctx
,
344 struct tevent_context
*ev
,
345 struct g_lock_ctx
*ctx
,
347 enum g_lock_type type
)
349 struct tevent_req
*req
;
350 struct g_lock_lock_state
*state
;
351 struct g_lock_lock_fn_state fn_state
;
354 req
= tevent_req_create(mem_ctx
, &state
, struct g_lock_lock_state
);
363 fn_state
= (struct g_lock_lock_fn_state
) {
364 .state
= state
, .self
= messaging_server_id(ctx
->msg
)
367 status
= dbwrap_do_locked(ctx
->db
, key
, g_lock_lock_fn
, &fn_state
);
368 if (tevent_req_nterror(req
, status
)) {
369 DBG_DEBUG("dbwrap_do_locked failed: %s\n",
371 return tevent_req_post(req
, ev
);
374 if (NT_STATUS_IS_OK(fn_state
.status
)) {
375 tevent_req_done(req
);
376 return tevent_req_post(req
, ev
);
378 if (!NT_STATUS_EQUAL(fn_state
.status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
379 tevent_req_nterror(req
, fn_state
.status
);
380 return tevent_req_post(req
, ev
);
383 if (tevent_req_nomem(fn_state
.watch_req
, req
)) {
384 return tevent_req_post(req
, ev
);
387 if (!tevent_req_set_endtime(
388 fn_state
.watch_req
, state
->ev
,
389 timeval_current_ofs(5 + sys_random() % 5, 0))) {
390 return tevent_req_post(req
, ev
);
392 tevent_req_set_callback(fn_state
.watch_req
, g_lock_lock_retry
, req
);
396 static void g_lock_lock_retry(struct tevent_req
*subreq
)
398 struct tevent_req
*req
= tevent_req_callback_data(
399 subreq
, struct tevent_req
);
400 struct g_lock_lock_state
*state
= tevent_req_data(
401 req
, struct g_lock_lock_state
);
402 struct g_lock_lock_fn_state fn_state
;
405 status
= dbwrap_watched_watch_recv(subreq
, NULL
, NULL
);
406 DBG_DEBUG("watch_recv returned %s\n", nt_errstr(status
));
409 if (!NT_STATUS_IS_OK(status
) &&
410 !NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
411 tevent_req_nterror(req
, status
);
415 fn_state
= (struct g_lock_lock_fn_state
) {
416 .state
= state
, .self
= messaging_server_id(state
->ctx
->msg
)
419 status
= dbwrap_do_locked(state
->ctx
->db
, state
->key
,
420 g_lock_lock_fn
, &fn_state
);
421 if (tevent_req_nterror(req
, status
)) {
422 DBG_DEBUG("dbwrap_do_locked failed: %s\n",
427 if (NT_STATUS_IS_OK(fn_state
.status
)) {
428 tevent_req_done(req
);
431 if (!NT_STATUS_EQUAL(fn_state
.status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
432 tevent_req_nterror(req
, fn_state
.status
);
436 if (tevent_req_nomem(fn_state
.watch_req
, req
)) {
440 if (!tevent_req_set_endtime(
441 fn_state
.watch_req
, state
->ev
,
442 timeval_current_ofs(5 + sys_random() % 5, 0))) {
445 tevent_req_set_callback(fn_state
.watch_req
, g_lock_lock_retry
, req
);
448 NTSTATUS
g_lock_lock_recv(struct tevent_req
*req
)
450 return tevent_req_simple_recv_ntstatus(req
);
453 NTSTATUS
g_lock_lock(struct g_lock_ctx
*ctx
, TDB_DATA key
,
454 enum g_lock_type type
, struct timeval timeout
)
456 TALLOC_CTX
*frame
= talloc_stackframe();
457 struct tevent_context
*ev
;
458 struct tevent_req
*req
;
460 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
462 ev
= samba_tevent_context_init(frame
);
466 req
= g_lock_lock_send(frame
, ev
, ctx
, key
, type
);
470 end
= timeval_current_ofs(timeout
.tv_sec
, timeout
.tv_usec
);
471 if (!tevent_req_set_endtime(req
, ev
, end
)) {
474 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
477 status
= g_lock_lock_recv(req
);
483 struct g_lock_unlock_state
{
485 struct server_id self
;
489 static void g_lock_unlock_fn(struct db_record
*rec
,
492 struct g_lock_unlock_state
*state
= private_data
;
498 value
= dbwrap_record_get_value(rec
);
500 ok
= g_lock_parse(value
.dptr
, value
.dsize
, &lck
);
502 DBG_DEBUG("g_lock_get for %s failed\n",
503 hex_encode_talloc(talloc_tos(),
506 state
->status
= NT_STATUS_FILE_INVALID
;
509 for (i
=0; i
<lck
.num_recs
; i
++) {
510 struct g_lock_rec lockrec
;
511 g_lock_get_rec(&lck
, i
, &lockrec
);
512 if (serverid_equal(&state
->self
, &lockrec
.pid
)) {
516 if (i
== lck
.num_recs
) {
517 DBG_DEBUG("Lock not found, num_rec=%zu\n", lck
.num_recs
);
518 state
->status
= NT_STATUS_NOT_FOUND
;
522 g_lock_rec_del(&lck
, i
);
524 if ((lck
.num_recs
== 0) && (lck
.datalen
== 0)) {
525 state
->status
= dbwrap_record_delete(rec
);
528 state
->status
= g_lock_store(rec
, &lck
, NULL
);
531 NTSTATUS
g_lock_unlock(struct g_lock_ctx
*ctx
, TDB_DATA key
)
533 struct g_lock_unlock_state state
= {
534 .self
= messaging_server_id(ctx
->msg
), .key
= key
538 status
= dbwrap_do_locked(ctx
->db
, key
, g_lock_unlock_fn
, &state
);
539 if (!NT_STATUS_IS_OK(status
)) {
540 DBG_WARNING("dbwrap_do_locked failed: %s\n",
544 if (!NT_STATUS_IS_OK(state
.status
)) {
545 DBG_WARNING("g_lock_unlock_fn failed: %s\n",
546 nt_errstr(state
.status
));
553 struct g_lock_write_data_state
{
555 struct server_id self
;
561 static void g_lock_write_data_fn(struct db_record
*rec
,
564 struct g_lock_write_data_state
*state
= private_data
;
570 value
= dbwrap_record_get_value(rec
);
572 ok
= g_lock_parse(value
.dptr
, value
.dsize
, &lck
);
574 DBG_DEBUG("g_lock_parse for %s failed\n",
575 hex_encode_talloc(talloc_tos(),
578 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
581 for (i
=0; i
<lck
.num_recs
; i
++) {
582 struct g_lock_rec lockrec
;
583 g_lock_get_rec(&lck
, i
, &lockrec
);
584 if ((lockrec
.lock_type
== G_LOCK_WRITE
) &&
585 serverid_equal(&state
->self
, &lockrec
.pid
)) {
589 if (i
== lck
.num_recs
) {
590 DBG_DEBUG("Not locked by us\n");
591 state
->status
= NT_STATUS_NOT_LOCKED
;
595 lck
.data
= discard_const_p(uint8_t, state
->data
);
596 lck
.datalen
= state
->datalen
;
597 state
->status
= g_lock_store(rec
, &lck
, NULL
);
600 NTSTATUS
g_lock_write_data(struct g_lock_ctx
*ctx
, TDB_DATA key
,
601 const uint8_t *buf
, size_t buflen
)
603 struct g_lock_write_data_state state
= {
604 .key
= key
, .self
= messaging_server_id(ctx
->msg
),
605 .data
= buf
, .datalen
= buflen
609 status
= dbwrap_do_locked(ctx
->db
, key
,
610 g_lock_write_data_fn
, &state
);
611 if (!NT_STATUS_IS_OK(status
)) {
612 DBG_WARNING("dbwrap_do_locked failed: %s\n",
616 if (!NT_STATUS_IS_OK(state
.status
)) {
617 DBG_WARNING("g_lock_write_data_fn failed: %s\n",
618 nt_errstr(state
.status
));
625 struct g_lock_locks_state
{
626 int (*fn
)(TDB_DATA key
, void *private_data
);
630 static int g_lock_locks_fn(struct db_record
*rec
, void *priv
)
633 struct g_lock_locks_state
*state
= (struct g_lock_locks_state
*)priv
;
635 key
= dbwrap_record_get_key(rec
);
636 return state
->fn(key
, state
->private_data
);
639 int g_lock_locks(struct g_lock_ctx
*ctx
,
640 int (*fn
)(TDB_DATA key
, void *private_data
),
643 struct g_lock_locks_state state
;
648 state
.private_data
= private_data
;
650 status
= dbwrap_traverse_read(ctx
->db
, g_lock_locks_fn
, &state
, &count
);
651 if (!NT_STATUS_IS_OK(status
)) {
657 struct g_lock_dump_state
{
660 void (*fn
)(const struct g_lock_rec
*locks
,
669 static void g_lock_dump_fn(TDB_DATA key
, TDB_DATA data
,
672 struct g_lock_dump_state
*state
= private_data
;
673 struct g_lock_rec
*recs
;
678 ok
= g_lock_parse(data
.dptr
, data
.dsize
, &lck
);
680 DBG_DEBUG("g_lock_parse failed for %s\n",
681 hex_encode_talloc(talloc_tos(),
684 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
688 recs
= talloc_array(state
->mem_ctx
, struct g_lock_rec
, lck
.num_recs
);
690 DBG_DEBUG("talloc failed\n");
691 state
->status
= NT_STATUS_NO_MEMORY
;
695 for (i
=0; i
<lck
.num_recs
; i
++) {
696 g_lock_get_rec(&lck
, i
, &recs
[i
]);
699 state
->fn(recs
, lck
.num_recs
, lck
.data
, lck
.datalen
,
700 state
->private_data
);
704 state
->status
= NT_STATUS_OK
;
707 NTSTATUS
g_lock_dump(struct g_lock_ctx
*ctx
, TDB_DATA key
,
708 void (*fn
)(const struct g_lock_rec
*locks
,
715 struct g_lock_dump_state state
= {
716 .mem_ctx
= ctx
, .key
= key
,
717 .fn
= fn
, .private_data
= private_data
721 status
= dbwrap_parse_record(ctx
->db
, key
, g_lock_dump_fn
, &state
);
722 if (!NT_STATUS_IS_OK(status
)) {
723 DBG_DEBUG("dbwrap_parse_record returned %s\n",
727 if (!NT_STATUS_IS_OK(state
.status
)) {
728 DBG_DEBUG("g_lock_dump_fn returned %s\n",
729 nt_errstr(state
.status
));
735 static bool g_lock_init_all(TALLOC_CTX
*mem_ctx
,
736 struct tevent_context
**pev
,
737 struct messaging_context
**pmsg
,
738 struct g_lock_ctx
**pg_ctx
)
740 struct tevent_context
*ev
= NULL
;
741 struct messaging_context
*msg
= NULL
;
742 struct g_lock_ctx
*g_ctx
= NULL
;
744 ev
= samba_tevent_context_init(mem_ctx
);
746 d_fprintf(stderr
, "ERROR: could not init event context\n");
749 msg
= messaging_init(mem_ctx
, ev
);
751 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
754 g_ctx
= g_lock_ctx_init(mem_ctx
, msg
);
756 d_fprintf(stderr
, "ERROR: could not init g_lock context\n");
771 NTSTATUS
g_lock_do(TDB_DATA key
, enum g_lock_type lock_type
,
772 struct timeval timeout
,
773 void (*fn
)(void *private_data
), void *private_data
)
775 struct tevent_context
*ev
= NULL
;
776 struct messaging_context
*msg
= NULL
;
777 struct g_lock_ctx
*g_ctx
= NULL
;
780 if (!g_lock_init_all(talloc_tos(), &ev
, &msg
, &g_ctx
)) {
781 status
= NT_STATUS_ACCESS_DENIED
;
785 status
= g_lock_lock(g_ctx
, key
, lock_type
, timeout
);
786 if (!NT_STATUS_IS_OK(status
)) {
790 g_lock_unlock(g_ctx
, key
);