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);
59 static ssize_t
g_lock_put(uint8_t *buf
, size_t buflen
,
60 const struct g_lock_rec
*locks
,
62 const uint8_t *data
, size_t datalen
)
66 if (num_locks
> UINT32_MAX
/G_LOCK_REC_LENGTH
) {
70 len
= num_locks
* G_LOCK_REC_LENGTH
;
72 len
+= sizeof(uint32_t);
73 if (len
< sizeof(uint32_t)) {
87 SIVAL(buf
, ofs
, num_locks
);
88 ofs
+= sizeof(uint32_t);
90 for (i
=0; i
<num_locks
; i
++) {
91 g_lock_rec_put(buf
+ofs
, locks
[i
]);
92 ofs
+= G_LOCK_REC_LENGTH
;
95 if ((data
!= NULL
) && (datalen
!= 0)) {
96 memcpy(buf
+ofs
, data
, datalen
);
102 static ssize_t
g_lock_get(TDB_DATA recval
,
103 struct g_lock_rec
*locks
, size_t num_locks
,
104 uint8_t **data
, size_t *datalen
)
108 if (recval
.dsize
< sizeof(uint32_t)) {
109 /* Fresh or invalid record */
114 found_locks
= IVAL(recval
.dptr
, 0);
115 recval
.dptr
+= sizeof(uint32_t);
116 recval
.dsize
-= sizeof(uint32_t);
118 if (found_locks
> recval
.dsize
/G_LOCK_REC_LENGTH
) {
123 if (found_locks
<= num_locks
) {
126 for (i
=0; i
<found_locks
; i
++) {
127 g_lock_rec_get(&locks
[i
], recval
.dptr
);
128 recval
.dptr
+= G_LOCK_REC_LENGTH
;
129 recval
.dsize
-= G_LOCK_REC_LENGTH
;
133 * Not enough space passed in by the caller, don't
136 recval
.dptr
+= found_locks
* G_LOCK_REC_LENGTH
;
137 recval
.dsize
-= found_locks
* G_LOCK_REC_LENGTH
;
144 if (datalen
!= NULL
) {
145 *datalen
= recval
.dsize
;
150 static NTSTATUS
g_lock_get_talloc(TALLOC_CTX
*mem_ctx
, TDB_DATA recval
,
151 struct g_lock_rec
**plocks
,
153 uint8_t **data
, size_t *datalen
)
155 struct g_lock_rec
*locks
;
158 num_locks
= g_lock_get(recval
, NULL
, 0, NULL
, NULL
);
159 if (num_locks
== -1) {
160 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
162 locks
= talloc_array(mem_ctx
, struct g_lock_rec
, num_locks
);
164 return NT_STATUS_NO_MEMORY
;
166 g_lock_get(recval
, locks
, num_locks
, data
, datalen
);
169 *pnum_locks
= num_locks
;
174 struct g_lock_ctx
*g_lock_ctx_init(TALLOC_CTX
*mem_ctx
,
175 struct messaging_context
*msg
)
177 struct g_lock_ctx
*result
;
178 struct db_context
*backend
;
181 result
= talloc(mem_ctx
, struct g_lock_ctx
);
182 if (result
== NULL
) {
187 db_path
= lock_path("g_lock.tdb");
188 if (db_path
== NULL
) {
193 backend
= db_open(result
, db_path
, 0,
194 TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
195 O_RDWR
|O_CREAT
, 0600,
198 TALLOC_FREE(db_path
);
199 if (backend
== NULL
) {
200 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb\n"));
205 result
->db
= db_open_watched(result
, backend
, msg
);
206 if (result
->db
== NULL
) {
207 DBG_WARNING("g_lock_init: db_open_watched failed\n");
214 static bool g_lock_conflicts(enum g_lock_type l1
, enum g_lock_type l2
)
217 * Only tested write locks so far. Very likely this routine
218 * needs to be fixed for read locks....
220 if ((l1
== G_LOCK_READ
) && (l2
== G_LOCK_READ
)) {
226 static NTSTATUS
g_lock_record_store(struct db_record
*rec
,
227 const struct g_lock_rec
*locks
,
229 const uint8_t *data
, size_t datalen
)
235 len
= g_lock_put(NULL
, 0, locks
, num_locks
, data
, datalen
);
237 return NT_STATUS_BUFFER_TOO_SMALL
;
240 buf
= talloc_array(rec
, uint8_t, len
);
242 return NT_STATUS_NO_MEMORY
;
245 g_lock_put(buf
, len
, locks
, num_locks
, data
, datalen
);
247 status
= dbwrap_record_store(
248 rec
, (TDB_DATA
) { .dptr
= buf
, .dsize
= len
}, 0);
255 static NTSTATUS
g_lock_trylock(struct db_record
*rec
, struct server_id self
,
256 enum g_lock_type type
,
257 struct server_id
*blocker
)
259 TDB_DATA data
, userdata
;
261 struct g_lock_rec
*locks
, *tmp
;
263 bool modified
= false;
265 data
= dbwrap_record_get_value(rec
);
267 status
= g_lock_get_talloc(talloc_tos(), data
, &locks
, &num_locks
,
268 &userdata
.dptr
, &userdata
.dsize
);
269 if (!NT_STATUS_IS_OK(status
)) {
273 for (i
=0; i
<num_locks
; i
++) {
274 if (serverid_equal(&self
, &locks
[i
].pid
)) {
275 status
= NT_STATUS_INTERNAL_ERROR
;
278 if (g_lock_conflicts(type
, locks
[i
].lock_type
)) {
279 struct server_id pid
= locks
[i
].pid
;
282 * As the serverid_exists might recurse into
283 * the g_lock code, we use
284 * SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
286 pid
.unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
;
288 if (serverid_exists(&pid
)) {
289 status
= NT_STATUS_LOCK_NOT_GRANTED
;
290 *blocker
= locks
[i
].pid
;
295 * Delete stale conflicting entry
297 locks
[i
] = locks
[num_locks
-1];
303 tmp
= talloc_realloc(talloc_tos(), locks
, struct g_lock_rec
,
306 status
= NT_STATUS_NO_MEMORY
;
311 ZERO_STRUCT(locks
[num_locks
]);
312 locks
[num_locks
].pid
= self
;
313 locks
[num_locks
].lock_type
= type
;
317 status
= NT_STATUS_OK
;
320 NTSTATUS store_status
;
321 store_status
= g_lock_record_store(
322 rec
, locks
, num_locks
, userdata
.dptr
, userdata
.dsize
);
323 if (!NT_STATUS_IS_OK(store_status
)) {
324 DBG_WARNING("g_lock_record_store failed: %s\n",
325 nt_errstr(store_status
));
326 status
= store_status
;
333 struct g_lock_lock_state
{
334 struct tevent_context
*ev
;
335 struct g_lock_ctx
*ctx
;
337 enum g_lock_type type
;
340 static void g_lock_lock_retry(struct tevent_req
*subreq
);
342 struct tevent_req
*g_lock_lock_send(TALLOC_CTX
*mem_ctx
,
343 struct tevent_context
*ev
,
344 struct g_lock_ctx
*ctx
,
346 enum g_lock_type type
)
348 struct tevent_req
*req
, *subreq
;
349 struct g_lock_lock_state
*state
;
350 struct db_record
*rec
;
351 struct server_id self
, blocker
;
354 req
= tevent_req_create(mem_ctx
, &state
, struct g_lock_lock_state
);
363 rec
= dbwrap_fetch_locked(ctx
->db
, talloc_tos(),
364 string_term_tdb_data(state
->name
));
366 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name
));
367 tevent_req_nterror(req
, NT_STATUS_LOCK_NOT_GRANTED
);
368 return tevent_req_post(req
, ev
);
371 self
= messaging_server_id(state
->ctx
->msg
);
373 status
= g_lock_trylock(rec
, self
, state
->type
, &blocker
);
374 if (NT_STATUS_IS_OK(status
)) {
376 tevent_req_done(req
);
377 return tevent_req_post(req
, ev
);
379 if (!NT_STATUS_EQUAL(status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
381 tevent_req_nterror(req
, status
);
382 return tevent_req_post(req
, ev
);
384 subreq
= dbwrap_watched_watch_send(state
, state
->ev
, rec
, blocker
);
386 if (tevent_req_nomem(subreq
, req
)) {
387 return tevent_req_post(req
, ev
);
389 if (!tevent_req_set_endtime(
391 timeval_current_ofs(5 + sys_random() % 5, 0))) {
393 return tevent_req_post(req
, ev
);
395 tevent_req_set_callback(subreq
, g_lock_lock_retry
, req
);
399 static void g_lock_lock_retry(struct tevent_req
*subreq
)
401 struct tevent_req
*req
= tevent_req_callback_data(
402 subreq
, struct tevent_req
);
403 struct g_lock_lock_state
*state
= tevent_req_data(
404 req
, struct g_lock_lock_state
);
405 struct server_id self
= messaging_server_id(state
->ctx
->msg
);
406 struct server_id blocker
;
407 struct db_record
*rec
;
410 status
= dbwrap_watched_watch_recv(subreq
, talloc_tos(), &rec
, NULL
,
414 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
415 rec
= dbwrap_fetch_locked(
416 state
->ctx
->db
, talloc_tos(),
417 string_term_tdb_data(state
->name
));
419 status
= map_nt_error_from_unix(errno
);
421 status
= NT_STATUS_OK
;
425 if (tevent_req_nterror(req
, status
)) {
428 status
= g_lock_trylock(rec
, self
, state
->type
, &blocker
);
429 if (NT_STATUS_IS_OK(status
)) {
431 tevent_req_done(req
);
434 if (!NT_STATUS_EQUAL(status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
436 tevent_req_nterror(req
, status
);
439 subreq
= dbwrap_watched_watch_send(state
, state
->ev
, rec
, blocker
);
441 if (tevent_req_nomem(subreq
, req
)) {
444 if (!tevent_req_set_endtime(
446 timeval_current_ofs(5 + sys_random() % 5, 0))) {
450 tevent_req_set_callback(subreq
, g_lock_lock_retry
, req
);
455 NTSTATUS
g_lock_lock_recv(struct tevent_req
*req
)
457 return tevent_req_simple_recv_ntstatus(req
);
460 NTSTATUS
g_lock_lock(struct g_lock_ctx
*ctx
, const char *name
,
461 enum g_lock_type type
, struct timeval timeout
)
463 TALLOC_CTX
*frame
= talloc_stackframe();
464 struct tevent_context
*ev
;
465 struct tevent_req
*req
;
467 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
469 ev
= samba_tevent_context_init(frame
);
473 req
= g_lock_lock_send(frame
, ev
, ctx
, name
, type
);
477 end
= timeval_current_ofs(timeout
.tv_sec
, timeout
.tv_usec
);
478 if (!tevent_req_set_endtime(req
, ev
, end
)) {
481 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
484 status
= g_lock_lock_recv(req
);
490 NTSTATUS
g_lock_unlock(struct g_lock_ctx
*ctx
, const char *name
)
492 struct server_id self
= messaging_server_id(ctx
->msg
);
493 struct db_record
*rec
= NULL
;
494 struct g_lock_rec
*locks
= NULL
;
497 TDB_DATA value
, userdata
;
499 rec
= dbwrap_fetch_locked(ctx
->db
, talloc_tos(),
500 string_term_tdb_data(name
));
502 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name
));
503 status
= NT_STATUS_INTERNAL_ERROR
;
507 value
= dbwrap_record_get_value(rec
);
509 status
= g_lock_get_talloc(talloc_tos(), value
, &locks
, &num_locks
,
510 &userdata
.dptr
, &userdata
.dsize
);
511 if (!NT_STATUS_IS_OK(status
)) {
512 DBG_DEBUG("g_lock_get for %s failed: %s\n", name
,
514 status
= NT_STATUS_FILE_INVALID
;
517 for (i
=0; i
<num_locks
; i
++) {
518 if (serverid_equal(&self
, &locks
[i
].pid
)) {
522 if (i
== num_locks
) {
523 DBG_DEBUG("Lock not found, num_locks=%zu\n", num_locks
);
524 status
= NT_STATUS_NOT_FOUND
;
528 locks
[i
] = locks
[num_locks
-1];
531 if ((num_locks
== 0) && (userdata
.dsize
== 0)) {
532 status
= dbwrap_record_delete(rec
);
534 status
= g_lock_record_store(
535 rec
, locks
, num_locks
, userdata
.dptr
, userdata
.dsize
);
537 if (!NT_STATUS_IS_OK(status
)) {
538 DBG_WARNING("Could not store record: %s\n", nt_errstr(status
));
542 status
= NT_STATUS_OK
;
549 NTSTATUS
g_lock_write_data(struct g_lock_ctx
*ctx
, const char *name
,
550 const uint8_t *buf
, size_t buflen
)
552 struct server_id self
= messaging_server_id(ctx
->msg
);
553 struct db_record
*rec
= NULL
;
554 struct g_lock_rec
*locks
= NULL
;
559 rec
= dbwrap_fetch_locked(ctx
->db
, talloc_tos(),
560 string_term_tdb_data(name
));
562 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name
));
563 status
= NT_STATUS_INTERNAL_ERROR
;
567 value
= dbwrap_record_get_value(rec
);
569 status
= g_lock_get_talloc(talloc_tos(), value
, &locks
, &num_locks
,
571 if (!NT_STATUS_IS_OK(status
)) {
572 DBG_DEBUG("g_lock_get for %s failed: %s\n", name
,
574 status
= NT_STATUS_FILE_INVALID
;
578 for (i
=0; i
<num_locks
; i
++) {
579 if (server_id_equal(&self
, &locks
[i
].pid
) &&
580 (locks
[i
].lock_type
== G_LOCK_WRITE
)) {
584 if (i
== num_locks
) {
585 DBG_DEBUG("Not locked by us\n");
586 status
= NT_STATUS_NOT_LOCKED
;
590 status
= g_lock_record_store(rec
, locks
, num_locks
, buf
, buflen
);
598 struct g_lock_locks_state
{
599 int (*fn
)(const char *name
, void *private_data
);
603 static int g_lock_locks_fn(struct db_record
*rec
, void *priv
)
606 struct g_lock_locks_state
*state
= (struct g_lock_locks_state
*)priv
;
608 key
= dbwrap_record_get_key(rec
);
609 if ((key
.dsize
== 0) || (key
.dptr
[key
.dsize
-1] != 0)) {
610 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
613 return state
->fn((char *)key
.dptr
, state
->private_data
);
616 int g_lock_locks(struct g_lock_ctx
*ctx
,
617 int (*fn
)(const char *name
, void *private_data
),
620 struct g_lock_locks_state state
;
625 state
.private_data
= private_data
;
627 status
= dbwrap_traverse_read(ctx
->db
, g_lock_locks_fn
, &state
, &count
);
628 if (!NT_STATUS_IS_OK(status
)) {
634 NTSTATUS
g_lock_dump(struct g_lock_ctx
*ctx
, const char *name
,
635 void (*fn
)(const struct g_lock_rec
*locks
,
644 struct g_lock_rec
*locks
= NULL
;
649 status
= dbwrap_fetch_bystring(ctx
->db
, talloc_tos(), name
, &data
);
650 if (!NT_STATUS_IS_OK(status
)) {
654 if ((data
.dsize
== 0) || (data
.dptr
== NULL
)) {
658 status
= g_lock_get_talloc(talloc_tos(), data
, &locks
, &num_locks
,
659 &userdata
, &userdatalen
);
661 if (!NT_STATUS_IS_OK(status
)) {
662 DBG_DEBUG("g_lock_get for %s failed: %s\n", name
,
664 TALLOC_FREE(data
.dptr
);
665 return NT_STATUS_INTERNAL_ERROR
;
668 fn(locks
, num_locks
, userdata
, userdatalen
, private_data
);
671 TALLOC_FREE(data
.dptr
);
675 static bool g_lock_init_all(TALLOC_CTX
*mem_ctx
,
676 struct tevent_context
**pev
,
677 struct messaging_context
**pmsg
,
678 struct g_lock_ctx
**pg_ctx
)
680 struct tevent_context
*ev
= NULL
;
681 struct messaging_context
*msg
= NULL
;
682 struct g_lock_ctx
*g_ctx
= NULL
;
684 ev
= samba_tevent_context_init(mem_ctx
);
686 d_fprintf(stderr
, "ERROR: could not init event context\n");
689 msg
= messaging_init(mem_ctx
, ev
);
691 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
694 g_ctx
= g_lock_ctx_init(mem_ctx
, msg
);
696 d_fprintf(stderr
, "ERROR: could not init g_lock context\n");
711 NTSTATUS
g_lock_do(const char *name
, enum g_lock_type lock_type
,
712 struct timeval timeout
,
713 void (*fn
)(void *private_data
), void *private_data
)
715 struct tevent_context
*ev
= NULL
;
716 struct messaging_context
*msg
= NULL
;
717 struct g_lock_ctx
*g_ctx
= NULL
;
720 if (!g_lock_init_all(talloc_tos(), &ev
, &msg
, &g_ctx
)) {
721 status
= NT_STATUS_ACCESS_DENIED
;
725 status
= g_lock_lock(g_ctx
, name
, lock_type
, timeout
);
726 if (!NT_STATUS_IS_OK(status
)) {
730 g_lock_unlock(g_ctx
, name
);