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(const struct g_lock *lck,
95 struct g_lock_rec *rec)
97 if (i >= lck->num_recs) {
100 g_lock_rec_get(rec, lck->recsbuf + i*G_LOCK_REC_LENGTH);
103 static void g_lock_rec_del(struct g_lock *lck, size_t i)
105 if (i >= lck->num_recs) {
109 if (i < lck->num_recs) {
110 uint8_t *recptr = lck->recsbuf + i*G_LOCK_REC_LENGTH;
111 memcpy(recptr, lck->recsbuf + lck->num_recs*G_LOCK_REC_LENGTH,
116 static NTSTATUS g_lock_store(struct db_record *rec, struct g_lock *lck,
117 struct g_lock_rec *add)
120 uint8_t addbuf[G_LOCK_REC_LENGTH];
122 struct TDB_DATA dbufs[] = {
123 { .dptr = sizebuf, .dsize = sizeof(sizebuf) },
124 { .dptr = lck->recsbuf,
125 .dsize = lck->num_recs * G_LOCK_REC_LENGTH },
127 { .dptr = lck->data, .dsize = lck->datalen }
131 g_lock_rec_put(addbuf, *add);
133 dbufs[2] = (TDB_DATA) {
134 .dptr = addbuf, .dsize = G_LOCK_REC_LENGTH
140 SIVAL(sizebuf, 0, lck->num_recs);
142 return dbwrap_record_storev(rec, dbufs, ARRAY_SIZE(dbufs), 0);
145 struct g_lock_ctx *g_lock_ctx_init(TALLOC_CTX *mem_ctx,
146 struct messaging_context *msg)
148 struct g_lock_ctx *result;
149 struct db_context *backend;
152 result = talloc(mem_ctx, struct g_lock_ctx);
153 if (result == NULL) {
158 db_path = lock_path(talloc_tos(), "g_lock.tdb");
159 if (db_path == NULL) {
164 backend = db_open(result, db_path, 0,
165 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
166 O_RDWR|O_CREAT, 0600,
169 TALLOC_FREE(db_path);
170 if (backend == NULL) {
171 DBG_WARNING("Could not open g_lock.tdb\n");
176 result->db = db_open_watched(result, &backend, msg);
177 if (result->db == NULL) {
178 DBG_WARNING("db_open_watched failed\n");
185 static bool g_lock_conflicts(enum g_lock_type l1, enum g_lock_type l2)
188 * Only tested write locks so far. Very likely this routine
189 * needs to be fixed for read locks....
191 if ((l1 == G_LOCK_READ) && (l2 == G_LOCK_READ)) {
197 static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
198 enum g_lock_type type,
199 struct server_id *blocker)
204 struct g_lock_rec mylock = {0};
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 ((check_rec.lock_type == G_LOCK_READ) &&
230 !serverid_exists(&check_rec.pid)) {
231 g_lock_rec_del(&lck, i);
237 * For the lock upgrade/downgrade case, remove ourselves from
238 * the list. We re-add ourselves later after we checked the
239 * other entries for conflict.
242 for (i=0; i<lck.num_recs; i++) {
243 struct g_lock_rec lock;
245 g_lock_get_rec(&lck, i, &lock);
247 if (serverid_equal(&self, &lock.pid)) {
248 if (lock.lock_type == type) {
249 status = NT_STATUS_WAS_LOCKED;
254 g_lock_rec_del(&lck, i);
261 * Check for conflicts with everybody else. Not a for-loop
262 * because we remove stale entries in the meantime,
263 * decrementing lck.num_recs.
268 while (i < lck.num_recs) {
269 struct g_lock_rec lock;
271 g_lock_get_rec(&lck, i, &lock);
273 if (g_lock_conflicts(type, lock.lock_type)) {
274 struct server_id pid = lock.pid;
277 * As the serverid_exists might recurse into
278 * the g_lock code, we use
279 * SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
281 pid.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
283 if (serverid_exists(&pid)) {
284 status = NT_STATUS_LOCK_NOT_GRANTED;
290 * Delete stale conflicting entry
292 g_lock_rec_del(&lck, i);
301 mylock = (struct g_lock_rec) {
306 status = NT_STATUS_OK;
309 NTSTATUS store_status;
312 * (Re-)add ourselves if needed via non-NULL
313 * g_lock_store argument
316 store_status = g_lock_store(
319 mylock.pid.pid != 0 ? &mylock : NULL);
321 if (!NT_STATUS_IS_OK(store_status)) {
322 DBG_WARNING("g_lock_record_store failed: %s\n",
323 nt_errstr(store_status));
324 status = store_status;
330 struct g_lock_lock_state {
331 struct tevent_context *ev;
332 struct g_lock_ctx *ctx;
334 enum g_lock_type type;
337 static void g_lock_lock_retry(struct tevent_req *subreq);
339 struct g_lock_lock_fn_state {
340 struct g_lock_lock_state *state;
341 struct server_id self;
343 struct tevent_req *watch_req;
347 static void g_lock_lock_fn(struct db_record *rec, void *private_data)
349 struct g_lock_lock_fn_state *state = private_data;
350 struct server_id blocker;
352 state->status = g_lock_trylock(rec, state->self, state->state->type,
354 if (!NT_STATUS_EQUAL(state->status, NT_STATUS_LOCK_NOT_GRANTED)) {
358 state->watch_req = dbwrap_watched_watch_send(
359 state->state, state->state->ev, rec, blocker);
362 struct tevent_req *g_lock_lock_send(TALLOC_CTX *mem_ctx,
363 struct tevent_context *ev,
364 struct g_lock_ctx *ctx,
366 enum g_lock_type type)
368 struct tevent_req *req;
369 struct g_lock_lock_state *state;
370 struct g_lock_lock_fn_state fn_state;
373 req = tevent_req_create(mem_ctx, &state, struct g_lock_lock_state);
382 fn_state = (struct g_lock_lock_fn_state) {
383 .state = state, .self = messaging_server_id(ctx->msg)
386 status = dbwrap_do_locked(ctx->db, key, g_lock_lock_fn, &fn_state);
387 if (tevent_req_nterror(req, status)) {
388 DBG_DEBUG("dbwrap_do_locked failed: %s\n",
390 return tevent_req_post(req, ev);
393 if (NT_STATUS_IS_OK(fn_state.status)) {
394 tevent_req_done(req);
395 return tevent_req_post(req, ev);
397 if (!NT_STATUS_EQUAL(fn_state.status, NT_STATUS_LOCK_NOT_GRANTED)) {
398 tevent_req_nterror(req, fn_state.status);
399 return tevent_req_post(req, ev);
402 if (tevent_req_nomem(fn_state.watch_req, req)) {
403 return tevent_req_post(req, ev);
406 if (!tevent_req_set_endtime(
407 fn_state.watch_req, state->ev,
408 timeval_current_ofs(5 + sys_random() % 5, 0))) {
409 return tevent_req_post(req, ev);
411 tevent_req_set_callback(fn_state.watch_req, g_lock_lock_retry, req);
415 static void g_lock_lock_retry(struct tevent_req *subreq)
417 struct tevent_req *req = tevent_req_callback_data(
418 subreq, struct tevent_req);
419 struct g_lock_lock_state *state = tevent_req_data(
420 req, struct g_lock_lock_state);
421 struct g_lock_lock_fn_state fn_state;
424 status = dbwrap_watched_watch_recv(subreq, NULL, NULL);
425 DBG_DEBUG("watch_recv returned %s\n", nt_errstr(status));
428 if (!NT_STATUS_IS_OK(status) &&
429 !NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
430 tevent_req_nterror(req, status);
434 fn_state = (struct g_lock_lock_fn_state) {
435 .state = state, .self = messaging_server_id(state->ctx->msg)
438 status = dbwrap_do_locked(state->ctx->db, state->key,
439 g_lock_lock_fn, &fn_state);
440 if (tevent_req_nterror(req, status)) {
441 DBG_DEBUG("dbwrap_do_locked failed: %s\n",
446 if (NT_STATUS_IS_OK(fn_state.status)) {
447 tevent_req_done(req);
450 if (!NT_STATUS_EQUAL(fn_state.status, NT_STATUS_LOCK_NOT_GRANTED)) {
451 tevent_req_nterror(req, fn_state.status);
455 if (tevent_req_nomem(fn_state.watch_req, req)) {
459 if (!tevent_req_set_endtime(
460 fn_state.watch_req, state->ev,
461 timeval_current_ofs(5 + sys_random() % 5, 0))) {
464 tevent_req_set_callback(fn_state.watch_req, g_lock_lock_retry, req);
467 NTSTATUS g_lock_lock_recv(struct tevent_req *req)
469 return tevent_req_simple_recv_ntstatus(req);
472 NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, TDB_DATA key,
473 enum g_lock_type type, struct timeval timeout)
475 TALLOC_CTX *frame = talloc_stackframe();
476 struct tevent_context *ev;
477 struct tevent_req *req;
479 NTSTATUS status = NT_STATUS_NO_MEMORY;
481 ev = samba_tevent_context_init(frame);
485 req = g_lock_lock_send(frame, ev, ctx, key, type);
489 end = timeval_current_ofs(timeout.tv_sec, timeout.tv_usec);
490 if (!tevent_req_set_endtime(req, ev, end)) {
493 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
496 status = g_lock_lock_recv(req);
502 struct g_lock_unlock_state {
504 struct server_id self;
508 static void g_lock_unlock_fn(struct db_record *rec,
511 struct g_lock_unlock_state *state = private_data;
517 value = dbwrap_record_get_value(rec);
519 ok = g_lock_parse(value.dptr, value.dsize, &lck);
521 DBG_DEBUG("g_lock_parse for %s failed\n",
522 hex_encode_talloc(talloc_tos(),
525 state->status = NT_STATUS_FILE_INVALID;
528 for (i=0; i<lck.num_recs; i++) {
529 struct g_lock_rec lockrec;
530 g_lock_get_rec(&lck, i, &lockrec);
531 if (serverid_equal(&state->self, &lockrec.pid)) {
535 if (i == lck.num_recs) {
536 DBG_DEBUG("Lock not found, num_rec=%zu\n", lck.num_recs);
537 state->status = NT_STATUS_NOT_FOUND;
541 g_lock_rec_del(&lck, i);
543 if ((lck.num_recs == 0) && (lck.datalen == 0)) {
544 state->status = dbwrap_record_delete(rec);
547 state->status = g_lock_store(rec, &lck, NULL);
550 NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, TDB_DATA key)
552 struct g_lock_unlock_state state = {
553 .self = messaging_server_id(ctx->msg), .key = key
557 status = dbwrap_do_locked(ctx->db, key, g_lock_unlock_fn, &state);
558 if (!NT_STATUS_IS_OK(status)) {
559 DBG_WARNING("dbwrap_do_locked failed: %s\n",
563 if (!NT_STATUS_IS_OK(state.status)) {
564 DBG_WARNING("g_lock_unlock_fn failed: %s\n",
565 nt_errstr(state.status));
572 struct g_lock_write_data_state {
574 struct server_id self;
580 static void g_lock_write_data_fn(struct db_record *rec,
583 struct g_lock_write_data_state *state = private_data;
589 value = dbwrap_record_get_value(rec);
591 ok = g_lock_parse(value.dptr, value.dsize, &lck);
593 DBG_DEBUG("g_lock_parse for %s failed\n",
594 hex_encode_talloc(talloc_tos(),
597 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
600 for (i=0; i<lck.num_recs; i++) {
601 struct g_lock_rec lockrec;
602 g_lock_get_rec(&lck, i, &lockrec);
603 if ((lockrec.lock_type == G_LOCK_WRITE) &&
604 serverid_equal(&state->self, &lockrec.pid)) {
608 if (i == lck.num_recs) {
609 DBG_DEBUG("Not locked by us\n");
610 state->status = NT_STATUS_NOT_LOCKED;
614 lck.data = discard_const_p(uint8_t, state->data);
615 lck.datalen = state->datalen;
616 state->status = g_lock_store(rec, &lck, NULL);
619 NTSTATUS g_lock_write_data(struct g_lock_ctx *ctx, TDB_DATA key,
620 const uint8_t *buf, size_t buflen)
622 struct g_lock_write_data_state state = {
623 .key = key, .self = messaging_server_id(ctx->msg),
624 .data = buf, .datalen = buflen
628 status = dbwrap_do_locked(ctx->db, key,
629 g_lock_write_data_fn, &state);
630 if (!NT_STATUS_IS_OK(status)) {
631 DBG_WARNING("dbwrap_do_locked failed: %s\n",
635 if (!NT_STATUS_IS_OK(state.status)) {
636 DBG_WARNING("g_lock_write_data_fn failed: %s\n",
637 nt_errstr(state.status));
644 struct g_lock_locks_state {
645 int (*fn)(TDB_DATA key, void *private_data);
649 static int g_lock_locks_fn(struct db_record *rec, void *priv)
652 struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
654 key = dbwrap_record_get_key(rec);
655 return state->fn(key, state->private_data);
658 int g_lock_locks(struct g_lock_ctx *ctx,
659 int (*fn)(TDB_DATA key, void *private_data),
662 struct g_lock_locks_state state;
667 state.private_data = private_data;
669 status = dbwrap_traverse_read(ctx->db, g_lock_locks_fn, &state, &count);
670 if (!NT_STATUS_IS_OK(status)) {
676 struct g_lock_dump_state {
679 void (*fn)(const struct g_lock_rec *locks,
688 static void g_lock_dump_fn(TDB_DATA key, TDB_DATA data,
691 struct g_lock_dump_state *state = private_data;
692 struct g_lock_rec *recs;
697 ok = g_lock_parse(data.dptr, data.dsize, &lck);
699 DBG_DEBUG("g_lock_parse failed for %s\n",
700 hex_encode_talloc(talloc_tos(),
703 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
707 recs = talloc_array(state->mem_ctx, struct g_lock_rec, lck.num_recs);
709 DBG_DEBUG("talloc failed\n");
710 state->status = NT_STATUS_NO_MEMORY;
714 for (i=0; i<lck.num_recs; i++) {
715 g_lock_get_rec(&lck, i, &recs[i]);
718 state->fn(recs, lck.num_recs, lck.data, lck.datalen,
719 state->private_data);
723 state->status = NT_STATUS_OK;
726 NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, TDB_DATA key,
727 void (*fn)(const struct g_lock_rec *locks,
734 struct g_lock_dump_state state = {
735 .mem_ctx = ctx, .key = key,
736 .fn = fn, .private_data = private_data
740 status = dbwrap_parse_record(ctx->db, key, g_lock_dump_fn, &state);
741 if (!NT_STATUS_IS_OK(status)) {
742 DBG_DEBUG("dbwrap_parse_record returned %s\n",
746 if (!NT_STATUS_IS_OK(state.status)) {
747 DBG_DEBUG("g_lock_dump_fn returned %s\n",
748 nt_errstr(state.status));
754 static bool g_lock_init_all(TALLOC_CTX *mem_ctx,
755 struct tevent_context **pev,
756 struct messaging_context **pmsg,
757 struct g_lock_ctx **pg_ctx)
759 struct tevent_context *ev = NULL;
760 struct messaging_context *msg = NULL;
761 struct g_lock_ctx *g_ctx = NULL;
763 ev = samba_tevent_context_init(mem_ctx);
765 d_fprintf(stderr, "ERROR: could not init event context\n");
768 msg = messaging_init(mem_ctx, ev);
770 d_fprintf(stderr, "ERROR: could not init messaging context\n");
773 g_ctx = g_lock_ctx_init(mem_ctx, msg);
775 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
790 NTSTATUS g_lock_do(TDB_DATA key, enum g_lock_type lock_type,
791 struct timeval timeout,
792 void (*fn)(void *private_data), void *private_data)
794 struct tevent_context *ev = NULL;
795 struct messaging_context *msg = NULL;
796 struct g_lock_ctx *g_ctx = NULL;
799 if (!g_lock_init_all(talloc_tos(), &ev, &msg, &g_ctx)) {
800 status = NT_STATUS_ACCESS_DENIED;
804 status = g_lock_lock(g_ctx, key, lock_type, timeout);
805 if (!NT_STATUS_IS_OK(status)) {
809 g_lock_unlock(g_ctx, key);