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"
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;
52 struct db_context *backend;
55 result = talloc(mem_ctx, struct g_lock_ctx);
61 db_path = lock_path("g_lock.tdb");
62 if (db_path == NULL) {
67 backend = db_open(result, db_path, 0,
68 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
73 if (backend == NULL) {
74 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb\n"));
79 result->db = db_open_watched(result, backend, msg);
80 if (result->db == NULL) {
81 DBG_WARNING("g_lock_init: db_open_watched failed\n");
88 static bool g_lock_conflicts(enum g_lock_type l1, enum g_lock_type l2)
91 * Only tested write locks so far. Very likely this routine
92 * needs to be fixed for read locks....
94 if ((l1 == G_LOCK_READ) && (l2 == G_LOCK_READ)) {
100 static bool g_lock_parse(TALLOC_CTX *mem_ctx, TDB_DATA data,
101 unsigned *pnum_locks, struct g_lock_rec **plocks)
104 struct g_lock_rec *locks;
106 if ((data.dsize % sizeof(struct g_lock_rec)) != 0) {
107 DEBUG(1, ("invalid lock record length %zu\n", data.dsize));
110 num_locks = data.dsize / sizeof(struct g_lock_rec);
111 locks = talloc_memdup(mem_ctx, data.dptr, data.dsize);
113 DEBUG(1, ("talloc_memdup failed\n"));
117 *pnum_locks = num_locks;
121 static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
122 enum g_lock_type type,
123 struct server_id *blocker)
126 unsigned i, num_locks;
127 struct g_lock_rec *locks, *tmp;
129 bool modified = false;
131 data = dbwrap_record_get_value(rec);
133 if (!g_lock_parse(talloc_tos(), data, &num_locks, &locks)) {
134 return NT_STATUS_INTERNAL_ERROR;
137 for (i=0; i<num_locks; i++) {
138 if (serverid_equal(&self, &locks[i].pid)) {
139 status = NT_STATUS_INTERNAL_ERROR;
142 if (g_lock_conflicts(type, locks[i].lock_type)) {
143 struct server_id pid = locks[i].pid;
146 * As the serverid_exists might recurse into
147 * the g_lock code, we use
148 * SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
150 pid.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
152 if (serverid_exists(&pid)) {
153 status = NT_STATUS_LOCK_NOT_GRANTED;
154 *blocker = locks[i].pid;
159 * Delete stale conflicting entry
161 locks[i] = locks[num_locks-1];
167 tmp = talloc_realloc(talloc_tos(), locks, struct g_lock_rec,
170 status = NT_STATUS_NO_MEMORY;
175 ZERO_STRUCT(locks[num_locks]);
176 locks[num_locks].pid = self;
177 locks[num_locks].lock_type = type;
181 status = NT_STATUS_OK;
184 NTSTATUS store_status;
186 data = make_tdb_data((uint8_t *)locks, num_locks * sizeof(*locks));
187 store_status = dbwrap_record_store(rec, data, 0);
188 if (!NT_STATUS_IS_OK(store_status)) {
189 DEBUG(1, ("rec->store failed: %s\n",
190 nt_errstr(store_status)));
191 status = store_status;
198 struct g_lock_lock_state {
199 struct tevent_context *ev;
200 struct g_lock_ctx *ctx;
202 enum g_lock_type type;
205 static void g_lock_lock_retry(struct tevent_req *subreq);
207 struct tevent_req *g_lock_lock_send(TALLOC_CTX *mem_ctx,
208 struct tevent_context *ev,
209 struct g_lock_ctx *ctx,
211 enum g_lock_type type)
213 struct tevent_req *req, *subreq;
214 struct g_lock_lock_state *state;
215 struct db_record *rec;
216 struct server_id self, blocker;
219 req = tevent_req_create(mem_ctx, &state, struct g_lock_lock_state);
228 rec = dbwrap_fetch_locked(ctx->db, talloc_tos(),
229 string_term_tdb_data(state->name));
231 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
232 tevent_req_nterror(req, NT_STATUS_LOCK_NOT_GRANTED);
233 return tevent_req_post(req, ev);
236 self = messaging_server_id(state->ctx->msg);
238 status = g_lock_trylock(rec, self, state->type, &blocker);
239 if (NT_STATUS_IS_OK(status)) {
241 tevent_req_done(req);
242 return tevent_req_post(req, ev);
244 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
246 tevent_req_nterror(req, status);
247 return tevent_req_post(req, ev);
249 subreq = dbwrap_watched_watch_send(state, state->ev, rec, blocker);
251 if (tevent_req_nomem(subreq, req)) {
252 return tevent_req_post(req, ev);
254 if (!tevent_req_set_endtime(
256 timeval_current_ofs(5 + sys_random() % 5, 0))) {
258 return tevent_req_post(req, ev);
260 tevent_req_set_callback(subreq, g_lock_lock_retry, req);
264 static void g_lock_lock_retry(struct tevent_req *subreq)
266 struct tevent_req *req = tevent_req_callback_data(
267 subreq, struct tevent_req);
268 struct g_lock_lock_state *state = tevent_req_data(
269 req, struct g_lock_lock_state);
270 struct server_id self = messaging_server_id(state->ctx->msg);
271 struct server_id blocker;
272 struct db_record *rec;
275 status = dbwrap_watched_watch_recv(subreq, talloc_tos(), &rec, NULL,
279 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
280 rec = dbwrap_fetch_locked(
281 state->ctx->db, talloc_tos(),
282 string_term_tdb_data(state->name));
284 status = map_nt_error_from_unix(errno);
286 status = NT_STATUS_OK;
290 if (tevent_req_nterror(req, status)) {
293 status = g_lock_trylock(rec, self, state->type, &blocker);
294 if (NT_STATUS_IS_OK(status)) {
296 tevent_req_done(req);
299 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
301 tevent_req_nterror(req, status);
304 subreq = dbwrap_watched_watch_send(state, state->ev, rec, blocker);
306 if (tevent_req_nomem(subreq, req)) {
309 if (!tevent_req_set_endtime(
311 timeval_current_ofs(5 + sys_random() % 5, 0))) {
315 tevent_req_set_callback(subreq, g_lock_lock_retry, req);
320 NTSTATUS g_lock_lock_recv(struct tevent_req *req)
322 return tevent_req_simple_recv_ntstatus(req);
325 NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
326 enum g_lock_type type, struct timeval timeout)
328 TALLOC_CTX *frame = talloc_stackframe();
329 struct tevent_context *ev;
330 struct tevent_req *req;
332 NTSTATUS status = NT_STATUS_NO_MEMORY;
334 ev = samba_tevent_context_init(frame);
338 req = g_lock_lock_send(frame, ev, ctx, name, type);
342 end = timeval_current_ofs(timeout.tv_sec, timeout.tv_usec);
343 if (!tevent_req_set_endtime(req, ev, end)) {
346 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
349 status = g_lock_lock_recv(req);
355 NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, const char *name)
357 struct server_id self = messaging_server_id(ctx->msg);
358 struct db_record *rec = NULL;
359 struct g_lock_rec *locks = NULL;
360 unsigned i, num_locks;
364 rec = dbwrap_fetch_locked(ctx->db, talloc_tos(),
365 string_term_tdb_data(name));
367 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
368 status = NT_STATUS_INTERNAL_ERROR;
372 value = dbwrap_record_get_value(rec);
374 if (!g_lock_parse(talloc_tos(), value, &num_locks, &locks)) {
375 DEBUG(10, ("g_lock_parse for %s failed\n", name));
376 status = NT_STATUS_FILE_INVALID;
379 for (i=0; i<num_locks; i++) {
380 if (serverid_equal(&self, &locks[i].pid)) {
384 if (i == num_locks) {
385 DEBUG(10, ("g_lock_force_unlock: Lock not found\n"));
386 status = NT_STATUS_NOT_FOUND;
390 locks[i] = locks[num_locks-1];
393 if (num_locks == 0) {
394 status = dbwrap_record_delete(rec);
397 data = make_tdb_data((uint8_t *)locks,
398 sizeof(struct g_lock_rec) * num_locks);
399 status = dbwrap_record_store(rec, data, 0);
401 if (!NT_STATUS_IS_OK(status)) {
402 DEBUG(1, ("g_lock_force_unlock: Could not store record: %s\n",
407 status = NT_STATUS_OK;
414 struct g_lock_locks_state {
415 int (*fn)(const char *name, void *private_data);
419 static int g_lock_locks_fn(struct db_record *rec, void *priv)
422 struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
424 key = dbwrap_record_get_key(rec);
425 if ((key.dsize == 0) || (key.dptr[key.dsize-1] != 0)) {
426 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
429 return state->fn((char *)key.dptr, state->private_data);
432 int g_lock_locks(struct g_lock_ctx *ctx,
433 int (*fn)(const char *name, void *private_data),
436 struct g_lock_locks_state state;
441 state.private_data = private_data;
443 status = dbwrap_traverse_read(ctx->db, g_lock_locks_fn, &state, &count);
444 if (!NT_STATUS_IS_OK(status)) {
451 NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, const char *name,
452 int (*fn)(struct server_id pid,
453 enum g_lock_type lock_type,
458 unsigned i, num_locks;
459 struct g_lock_rec *locks = NULL;
463 status = dbwrap_fetch_bystring(ctx->db, talloc_tos(), name, &data);
464 if (!NT_STATUS_IS_OK(status)) {
468 if ((data.dsize == 0) || (data.dptr == NULL)) {
472 ret = g_lock_parse(talloc_tos(), data, &num_locks, &locks);
474 TALLOC_FREE(data.dptr);
477 DEBUG(10, ("g_lock_parse for %s failed\n", name));
478 return NT_STATUS_INTERNAL_ERROR;
481 for (i=0; i<num_locks; i++) {
482 if (fn(locks[i].pid, locks[i].lock_type, private_data) != 0) {
490 struct g_lock_get_state {
492 struct server_id *pid;
495 static int g_lock_get_fn(struct server_id pid, enum g_lock_type lock_type,
498 struct g_lock_get_state *state = (struct g_lock_get_state *)priv;
504 NTSTATUS g_lock_get(struct g_lock_ctx *ctx, const char *name,
505 struct server_id *pid)
507 struct g_lock_get_state state;
513 status = g_lock_dump(ctx, name, g_lock_get_fn, &state);
514 if (!NT_STATUS_IS_OK(status)) {
518 return NT_STATUS_NOT_FOUND;
523 static bool g_lock_init_all(TALLOC_CTX *mem_ctx,
524 struct tevent_context **pev,
525 struct messaging_context **pmsg,
526 struct g_lock_ctx **pg_ctx)
528 struct tevent_context *ev = NULL;
529 struct messaging_context *msg = NULL;
530 struct g_lock_ctx *g_ctx = NULL;
532 ev = samba_tevent_context_init(mem_ctx);
534 d_fprintf(stderr, "ERROR: could not init event context\n");
537 msg = messaging_init(mem_ctx, ev);
539 d_fprintf(stderr, "ERROR: could not init messaging context\n");
542 g_ctx = g_lock_ctx_init(mem_ctx, msg);
544 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
559 NTSTATUS g_lock_do(const char *name, enum g_lock_type lock_type,
560 struct timeval timeout,
561 void (*fn)(void *private_data), void *private_data)
563 struct tevent_context *ev = NULL;
564 struct messaging_context *msg = NULL;
565 struct g_lock_ctx *g_ctx = NULL;
568 if (!g_lock_init_all(talloc_tos(), &ev, &msg, &g_ctx)) {
569 status = NT_STATUS_ACCESS_DENIED;
573 status = g_lock_lock(g_ctx, name, lock_type, timeout);
574 if (!NT_STATUS_IS_OK(status)) {
578 g_lock_unlock(g_ctx, name);