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
;
260 size_t i
, num_locks
, my_lock
;
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 my_lock
= num_locks
; /* doesn't exist yet */
275 for (i
=0; i
<num_locks
; i
++) {
276 struct g_lock_rec
*lock
= &locks
[i
];
278 if (serverid_equal(&self
, &lock
->pid
)) {
279 if (lock
->lock_type
== type
) {
280 status
= NT_STATUS_WAS_LOCKED
;
288 for (i
=0; i
<num_locks
; i
++) {
294 if (g_lock_conflicts(type
, locks
[i
].lock_type
)) {
295 struct server_id pid
= locks
[i
].pid
;
298 * As the serverid_exists might recurse into
299 * the g_lock code, we use
300 * SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
302 pid
.unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
;
304 if (serverid_exists(&pid
)) {
305 status
= NT_STATUS_LOCK_NOT_GRANTED
;
306 *blocker
= locks
[i
].pid
;
311 * Delete stale conflicting entry
313 locks
[i
] = locks
[num_locks
-1];
319 if (my_lock
>= num_locks
) {
320 tmp
= talloc_realloc(talloc_tos(), locks
, struct g_lock_rec
,
323 status
= NT_STATUS_NO_MEMORY
;
331 locks
[my_lock
] = (struct g_lock_rec
){ .pid
= self
, .lock_type
= type
};
334 status
= NT_STATUS_OK
;
337 NTSTATUS store_status
;
338 store_status
= g_lock_record_store(
339 rec
, locks
, num_locks
, userdata
.dptr
, userdata
.dsize
);
340 if (!NT_STATUS_IS_OK(store_status
)) {
341 DBG_WARNING("g_lock_record_store failed: %s\n",
342 nt_errstr(store_status
));
343 status
= store_status
;
350 struct g_lock_lock_state
{
351 struct tevent_context
*ev
;
352 struct g_lock_ctx
*ctx
;
354 enum g_lock_type type
;
357 static void g_lock_lock_retry(struct tevent_req
*subreq
);
359 struct tevent_req
*g_lock_lock_send(TALLOC_CTX
*mem_ctx
,
360 struct tevent_context
*ev
,
361 struct g_lock_ctx
*ctx
,
363 enum g_lock_type type
)
365 struct tevent_req
*req
, *subreq
;
366 struct g_lock_lock_state
*state
;
367 struct db_record
*rec
;
368 struct server_id self
, blocker
;
371 req
= tevent_req_create(mem_ctx
, &state
, struct g_lock_lock_state
);
380 rec
= dbwrap_fetch_locked(ctx
->db
, talloc_tos(),
381 string_term_tdb_data(state
->name
));
383 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name
));
384 tevent_req_nterror(req
, NT_STATUS_LOCK_NOT_GRANTED
);
385 return tevent_req_post(req
, ev
);
388 self
= messaging_server_id(state
->ctx
->msg
);
390 status
= g_lock_trylock(rec
, self
, state
->type
, &blocker
);
391 if (NT_STATUS_IS_OK(status
)) {
393 tevent_req_done(req
);
394 return tevent_req_post(req
, ev
);
396 if (!NT_STATUS_EQUAL(status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
398 tevent_req_nterror(req
, status
);
399 return tevent_req_post(req
, ev
);
401 subreq
= dbwrap_watched_watch_send(state
, state
->ev
, rec
, blocker
);
403 if (tevent_req_nomem(subreq
, req
)) {
404 return tevent_req_post(req
, ev
);
406 if (!tevent_req_set_endtime(
408 timeval_current_ofs(5 + sys_random() % 5, 0))) {
410 return tevent_req_post(req
, ev
);
412 tevent_req_set_callback(subreq
, g_lock_lock_retry
, req
);
416 static void g_lock_lock_retry(struct tevent_req
*subreq
)
418 struct tevent_req
*req
= tevent_req_callback_data(
419 subreq
, struct tevent_req
);
420 struct g_lock_lock_state
*state
= tevent_req_data(
421 req
, struct g_lock_lock_state
);
422 struct server_id self
= messaging_server_id(state
->ctx
->msg
);
423 struct server_id blocker
;
424 struct db_record
*rec
;
427 status
= dbwrap_watched_watch_recv(subreq
, talloc_tos(), &rec
, NULL
,
431 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
432 rec
= dbwrap_fetch_locked(
433 state
->ctx
->db
, talloc_tos(),
434 string_term_tdb_data(state
->name
));
436 status
= map_nt_error_from_unix(errno
);
438 status
= NT_STATUS_OK
;
442 if (tevent_req_nterror(req
, status
)) {
445 status
= g_lock_trylock(rec
, self
, state
->type
, &blocker
);
446 if (NT_STATUS_IS_OK(status
)) {
448 tevent_req_done(req
);
451 if (!NT_STATUS_EQUAL(status
, NT_STATUS_LOCK_NOT_GRANTED
)) {
453 tevent_req_nterror(req
, status
);
456 subreq
= dbwrap_watched_watch_send(state
, state
->ev
, rec
, blocker
);
458 if (tevent_req_nomem(subreq
, req
)) {
461 if (!tevent_req_set_endtime(
463 timeval_current_ofs(5 + sys_random() % 5, 0))) {
467 tevent_req_set_callback(subreq
, g_lock_lock_retry
, req
);
472 NTSTATUS
g_lock_lock_recv(struct tevent_req
*req
)
474 return tevent_req_simple_recv_ntstatus(req
);
477 NTSTATUS
g_lock_lock(struct g_lock_ctx
*ctx
, const char *name
,
478 enum g_lock_type type
, struct timeval timeout
)
480 TALLOC_CTX
*frame
= talloc_stackframe();
481 struct tevent_context
*ev
;
482 struct tevent_req
*req
;
484 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
486 ev
= samba_tevent_context_init(frame
);
490 req
= g_lock_lock_send(frame
, ev
, ctx
, name
, type
);
494 end
= timeval_current_ofs(timeout
.tv_sec
, timeout
.tv_usec
);
495 if (!tevent_req_set_endtime(req
, ev
, end
)) {
498 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
501 status
= g_lock_lock_recv(req
);
507 NTSTATUS
g_lock_unlock(struct g_lock_ctx
*ctx
, const char *name
)
509 struct server_id self
= messaging_server_id(ctx
->msg
);
510 struct db_record
*rec
= NULL
;
511 struct g_lock_rec
*locks
= NULL
;
514 TDB_DATA value
, userdata
;
516 rec
= dbwrap_fetch_locked(ctx
->db
, talloc_tos(),
517 string_term_tdb_data(name
));
519 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name
));
520 status
= NT_STATUS_INTERNAL_ERROR
;
524 value
= dbwrap_record_get_value(rec
);
526 status
= g_lock_get_talloc(talloc_tos(), value
, &locks
, &num_locks
,
527 &userdata
.dptr
, &userdata
.dsize
);
528 if (!NT_STATUS_IS_OK(status
)) {
529 DBG_DEBUG("g_lock_get for %s failed: %s\n", name
,
531 status
= NT_STATUS_FILE_INVALID
;
534 for (i
=0; i
<num_locks
; i
++) {
535 if (serverid_equal(&self
, &locks
[i
].pid
)) {
539 if (i
== num_locks
) {
540 DBG_DEBUG("Lock not found, num_locks=%zu\n", num_locks
);
541 status
= NT_STATUS_NOT_FOUND
;
545 locks
[i
] = locks
[num_locks
-1];
548 if ((num_locks
== 0) && (userdata
.dsize
== 0)) {
549 status
= dbwrap_record_delete(rec
);
551 status
= g_lock_record_store(
552 rec
, locks
, num_locks
, userdata
.dptr
, userdata
.dsize
);
554 if (!NT_STATUS_IS_OK(status
)) {
555 DBG_WARNING("Could not store record: %s\n", nt_errstr(status
));
559 status
= NT_STATUS_OK
;
566 NTSTATUS
g_lock_write_data(struct g_lock_ctx
*ctx
, const char *name
,
567 const uint8_t *buf
, size_t buflen
)
569 struct server_id self
= messaging_server_id(ctx
->msg
);
570 struct db_record
*rec
= NULL
;
571 struct g_lock_rec
*locks
= NULL
;
576 rec
= dbwrap_fetch_locked(ctx
->db
, talloc_tos(),
577 string_term_tdb_data(name
));
579 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name
));
580 status
= NT_STATUS_INTERNAL_ERROR
;
584 value
= dbwrap_record_get_value(rec
);
586 status
= g_lock_get_talloc(talloc_tos(), value
, &locks
, &num_locks
,
588 if (!NT_STATUS_IS_OK(status
)) {
589 DBG_DEBUG("g_lock_get for %s failed: %s\n", name
,
591 status
= NT_STATUS_FILE_INVALID
;
595 for (i
=0; i
<num_locks
; i
++) {
596 if (server_id_equal(&self
, &locks
[i
].pid
) &&
597 (locks
[i
].lock_type
== G_LOCK_WRITE
)) {
601 if (i
== num_locks
) {
602 DBG_DEBUG("Not locked by us\n");
603 status
= NT_STATUS_NOT_LOCKED
;
607 status
= g_lock_record_store(rec
, locks
, num_locks
, buf
, buflen
);
615 struct g_lock_locks_state
{
616 int (*fn
)(const char *name
, void *private_data
);
620 static int g_lock_locks_fn(struct db_record
*rec
, void *priv
)
623 struct g_lock_locks_state
*state
= (struct g_lock_locks_state
*)priv
;
625 key
= dbwrap_record_get_key(rec
);
626 if ((key
.dsize
== 0) || (key
.dptr
[key
.dsize
-1] != 0)) {
627 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
630 return state
->fn((char *)key
.dptr
, state
->private_data
);
633 int g_lock_locks(struct g_lock_ctx
*ctx
,
634 int (*fn
)(const char *name
, void *private_data
),
637 struct g_lock_locks_state state
;
642 state
.private_data
= private_data
;
644 status
= dbwrap_traverse_read(ctx
->db
, g_lock_locks_fn
, &state
, &count
);
645 if (!NT_STATUS_IS_OK(status
)) {
651 NTSTATUS
g_lock_dump(struct g_lock_ctx
*ctx
, const char *name
,
652 void (*fn
)(const struct g_lock_rec
*locks
,
661 struct g_lock_rec
*locks
= NULL
;
666 status
= dbwrap_fetch_bystring(ctx
->db
, talloc_tos(), name
, &data
);
667 if (!NT_STATUS_IS_OK(status
)) {
671 if ((data
.dsize
== 0) || (data
.dptr
== NULL
)) {
675 status
= g_lock_get_talloc(talloc_tos(), data
, &locks
, &num_locks
,
676 &userdata
, &userdatalen
);
678 if (!NT_STATUS_IS_OK(status
)) {
679 DBG_DEBUG("g_lock_get for %s failed: %s\n", name
,
681 TALLOC_FREE(data
.dptr
);
682 return NT_STATUS_INTERNAL_ERROR
;
685 fn(locks
, num_locks
, userdata
, userdatalen
, private_data
);
688 TALLOC_FREE(data
.dptr
);
692 static bool g_lock_init_all(TALLOC_CTX
*mem_ctx
,
693 struct tevent_context
**pev
,
694 struct messaging_context
**pmsg
,
695 struct g_lock_ctx
**pg_ctx
)
697 struct tevent_context
*ev
= NULL
;
698 struct messaging_context
*msg
= NULL
;
699 struct g_lock_ctx
*g_ctx
= NULL
;
701 ev
= samba_tevent_context_init(mem_ctx
);
703 d_fprintf(stderr
, "ERROR: could not init event context\n");
706 msg
= messaging_init(mem_ctx
, ev
);
708 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
711 g_ctx
= g_lock_ctx_init(mem_ctx
, msg
);
713 d_fprintf(stderr
, "ERROR: could not init g_lock context\n");
728 NTSTATUS
g_lock_do(const char *name
, enum g_lock_type lock_type
,
729 struct timeval timeout
,
730 void (*fn
)(void *private_data
), void *private_data
)
732 struct tevent_context
*ev
= NULL
;
733 struct messaging_context
*msg
= NULL
;
734 struct g_lock_ctx
*g_ctx
= NULL
;
737 if (!g_lock_init_all(talloc_tos(), &ev
, &msg
, &g_ctx
)) {
738 status
= NT_STATUS_ACCESS_DENIED
;
742 status
= g_lock_lock(g_ctx
, name
, lock_type
, timeout
);
743 if (!NT_STATUS_IS_OK(status
)) {
747 g_lock_unlock(g_ctx
, name
);