PEP8: fix E305: expected 2 blank lines after class or function definition, found 1
[Samba.git] / source3 / locking / share_mode_lock.c
blob0bf7b9db90a874ae4239088669b7946dbc7bc496
1 /*
2 Unix SMB/CIFS implementation.
3 Locking functions
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1992-2006
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 Revision History:
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27 locking to deal with multiple share modes per open file.
29 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
30 support.
32 rewritten completely to use new tdb code. Tridge, Dec '99
34 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
35 Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
38 #include "includes.h"
39 #include "system/filesys.h"
40 #include "lib/util/server_id.h"
41 #include "locking/proto.h"
42 #include "smbd/globals.h"
43 #include "dbwrap/dbwrap.h"
44 #include "dbwrap/dbwrap_open.h"
45 #include "../libcli/security/security.h"
46 #include "serverid.h"
47 #include "messages.h"
48 #include "util_tdb.h"
49 #include "../librpc/gen_ndr/ndr_open_files.h"
50 #include "source3/lib/dbwrap/dbwrap_watch.h"
51 #include "locking/leases_db.h"
52 #include "../lib/util/memcache.h"
53 #include "lib/util/tevent_ntstatus.h"
55 #undef DBGC_CLASS
56 #define DBGC_CLASS DBGC_LOCKING
58 #define NO_LOCKING_COUNT (-1)
60 /* the locking database handle */
61 static struct db_context *lock_db;
63 static bool locking_init_internal(bool read_only)
65 struct db_context *backend;
66 char *db_path;
68 brl_init(read_only);
70 if (lock_db)
71 return True;
73 db_path = lock_path(talloc_tos(), "locking.tdb");
74 if (db_path == NULL) {
75 return false;
78 backend = db_open(NULL, db_path,
79 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
80 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
81 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
82 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
83 TALLOC_FREE(db_path);
84 if (!backend) {
85 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
86 return False;
89 lock_db = db_open_watched(NULL, &backend, server_messaging_context());
90 if (lock_db == NULL) {
91 DBG_ERR("db_open_watched failed\n");
92 TALLOC_FREE(backend);
93 return false;
96 if (!posix_locking_init(read_only)) {
97 TALLOC_FREE(lock_db);
98 return False;
101 return True;
104 bool locking_init(void)
106 return locking_init_internal(false);
109 bool locking_init_readonly(void)
111 return locking_init_internal(true);
114 /*******************************************************************
115 Deinitialize the share_mode management.
116 ******************************************************************/
118 bool locking_end(void)
120 brl_shutdown();
121 TALLOC_FREE(lock_db);
122 return true;
125 /*******************************************************************
126 Form a static locking key for a dev/inode pair.
127 ******************************************************************/
129 static TDB_DATA locking_key(const struct file_id *id)
131 return make_tdb_data((const uint8_t *)id, sizeof(*id));
134 /*******************************************************************
135 Share mode cache utility functions that store/delete/retrieve
136 entries from memcache.
138 For now share the statcache (global cache) memory space. If
139 a lock record gets orphaned (which shouldn't happen as we're
140 using the same locking_key data as lookup) it will eventually
141 fall out of the cache via the normal LRU trim mechanism. If
142 necessary we can always make this a separate (smaller) cache.
143 ******************************************************************/
145 static const DATA_BLOB memcache_key(const struct file_id *id)
147 return data_blob_const((const void *)id, sizeof(*id));
150 static void share_mode_memcache_delete(struct share_mode_data *d)
152 const DATA_BLOB key = memcache_key(&d->id);
154 DEBUG(10,("deleting entry for file %s seq 0x%llu key %s\n",
155 d->base_name,
156 (unsigned long long) d->sequence_number,
157 file_id_string(talloc_tos(), &d->id)));
159 memcache_delete(NULL,
160 SHARE_MODE_LOCK_CACHE,
161 key);
164 static void share_mode_memcache_store(struct share_mode_data *d)
166 const DATA_BLOB key = memcache_key(&d->id);
168 DEBUG(10,("stored entry for file %s seq 0x%llu key %s\n",
169 d->base_name,
170 (unsigned long long) d->sequence_number,
171 file_id_string(talloc_tos(), &d->id)));
173 /* Ensure everything stored in the cache is pristine. */
174 d->modified = false;
175 d->fresh = false;
178 * Ensure the memory going into the cache
179 * doesn't have a destructor so it can be
180 * cleanly freed by share_mode_memcache_delete().
182 talloc_set_destructor(d, NULL);
184 /* Cache will own d after this call. */
185 memcache_add_talloc(NULL,
186 SHARE_MODE_LOCK_CACHE,
187 key,
188 &d);
192 * NB. We use ndr_pull_hyper on a stack-created
193 * struct ndr_pull with no talloc allowed, as we
194 * need this to be really fast as an ndr-peek into
195 * the first 8 bytes of the blob.
198 static enum ndr_err_code get_blob_sequence_number(DATA_BLOB *blob,
199 uint64_t *pseq)
201 struct ndr_pull ndr = {.data = blob->data, .data_size = blob->length};
202 NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pseq));
203 return NDR_ERR_SUCCESS;
206 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
208 return -1;
211 static struct share_mode_data *share_mode_memcache_fetch(TALLOC_CTX *mem_ctx,
212 const TDB_DATA id_key,
213 DATA_BLOB *blob)
215 enum ndr_err_code ndr_err;
216 struct share_mode_data *d;
217 uint64_t sequence_number;
218 void *ptr;
219 struct file_id id;
220 DATA_BLOB key;
222 /* Ensure this is a locking_key record. */
223 if (id_key.dsize != sizeof(id)) {
224 return NULL;
227 memcpy(&id, id_key.dptr, id_key.dsize);
228 key = memcache_key(&id);
230 ptr = memcache_lookup_talloc(NULL,
231 SHARE_MODE_LOCK_CACHE,
232 key);
233 if (ptr == NULL) {
234 DEBUG(10,("failed to find entry for key %s\n",
235 file_id_string(mem_ctx, &id)));
236 return NULL;
238 /* sequence number key is at start of blob. */
239 ndr_err = get_blob_sequence_number(blob, &sequence_number);
240 if (ndr_err != NDR_ERR_SUCCESS) {
241 /* Bad blob. Remove entry. */
242 DEBUG(10,("bad blob %u key %s\n",
243 (unsigned int)ndr_err,
244 file_id_string(mem_ctx, &id)));
245 memcache_delete(NULL,
246 SHARE_MODE_LOCK_CACHE,
247 key);
248 return NULL;
251 d = (struct share_mode_data *)ptr;
252 if (d->sequence_number != sequence_number) {
253 DEBUG(10,("seq changed (cached 0x%llu) (new 0x%llu) "
254 "for key %s\n",
255 (unsigned long long)d->sequence_number,
256 (unsigned long long)sequence_number,
257 file_id_string(mem_ctx, &id)));
258 /* Cache out of date. Remove entry. */
259 memcache_delete(NULL,
260 SHARE_MODE_LOCK_CACHE,
261 key);
262 return NULL;
265 /* Move onto mem_ctx. */
266 d = talloc_move(mem_ctx, &ptr);
269 * Now we own d, prevent the cache from freeing it
270 * when we delete the entry.
272 talloc_set_destructor(d, share_mode_data_nofree_destructor);
274 /* Remove from the cache. We own it now. */
275 memcache_delete(NULL,
276 SHARE_MODE_LOCK_CACHE,
277 key);
279 /* And reset the destructor to none. */
280 talloc_set_destructor(d, NULL);
282 DEBUG(10,("fetched entry for file %s seq 0x%llu key %s\n",
283 d->base_name,
284 (unsigned long long)d->sequence_number,
285 file_id_string(mem_ctx, &id)));
287 return d;
290 /*******************************************************************
291 Get all share mode entries for a dev/inode pair.
292 ********************************************************************/
294 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
295 const TDB_DATA key,
296 const TDB_DATA dbuf)
298 struct share_mode_data *d;
299 enum ndr_err_code ndr_err;
300 uint32_t i;
301 DATA_BLOB blob;
303 blob.data = dbuf.dptr;
304 blob.length = dbuf.dsize;
306 /* See if we already have a cached copy of this key. */
307 d = share_mode_memcache_fetch(mem_ctx, key, &blob);
308 if (d != NULL) {
309 return d;
312 d = talloc(mem_ctx, struct share_mode_data);
313 if (d == NULL) {
314 DEBUG(0, ("talloc failed\n"));
315 goto fail;
318 ndr_err = ndr_pull_struct_blob_all(
319 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
320 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
321 DEBUG(1, ("ndr_pull_share_mode_lock failed: %s\n",
322 ndr_errstr(ndr_err)));
323 goto fail;
327 * Initialize the values that are [skip] or [ignore]
328 * in the idl. The NDR code does not initialize them.
331 for (i=0; i<d->num_share_modes; i++) {
332 d->share_modes[i].stale = false;
334 d->modified = false;
335 d->fresh = false;
337 if (DEBUGLEVEL >= 10) {
338 DEBUG(10, ("parse_share_modes:\n"));
339 NDR_PRINT_DEBUG(share_mode_data, d);
342 return d;
343 fail:
344 TALLOC_FREE(d);
345 return NULL;
348 /*******************************************************************
349 Create a storable data blob from a modified share_mode_data struct.
350 ********************************************************************/
352 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
354 DATA_BLOB blob;
355 enum ndr_err_code ndr_err;
357 if (DEBUGLEVEL >= 10) {
358 DEBUG(10, ("unparse_share_modes:\n"));
359 NDR_PRINT_DEBUG(share_mode_data, d);
362 share_mode_memcache_delete(d);
364 /* Update the sequence number. */
365 d->sequence_number += 1;
367 remove_stale_share_mode_entries(d);
369 if (d->num_share_modes == 0) {
370 DEBUG(10, ("No used share mode found\n"));
371 return make_tdb_data(NULL, 0);
374 ndr_err = ndr_push_struct_blob(
375 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
376 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
377 smb_panic("ndr_push_share_mode_lock failed");
380 return make_tdb_data(blob.data, blob.length);
383 /*******************************************************************
384 If modified, store the share_mode_data back into the database.
385 ********************************************************************/
387 static int share_mode_data_destructor(struct share_mode_data *d)
389 NTSTATUS status;
390 TDB_DATA data;
392 if (!d->modified) {
393 return 0;
396 data = unparse_share_modes(d);
398 if (data.dptr == NULL) {
399 if (!d->fresh) {
400 /* There has been an entry before, delete it */
402 status = dbwrap_record_delete(d->record);
403 if (!NT_STATUS_IS_OK(status)) {
404 char *errmsg;
406 DEBUG(0, ("delete_rec returned %s\n",
407 nt_errstr(status)));
409 if (asprintf(&errmsg, "could not delete share "
410 "entry: %s\n",
411 nt_errstr(status)) == -1) {
412 smb_panic("could not delete share"
413 "entry");
415 smb_panic(errmsg);
419 * Nothing to store in cache - allow the normal
420 * release of record lock and memory free.
422 return 0;
425 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
426 if (!NT_STATUS_IS_OK(status)) {
427 char *errmsg;
429 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
431 if (asprintf(&errmsg, "could not store share mode entry: %s",
432 nt_errstr(status)) == -1) {
433 smb_panic("could not store share mode entry");
435 smb_panic(errmsg);
439 * Release the record lock before putting in the cache.
441 TALLOC_FREE(d->record);
444 * Release the dptr as well before reparenting to NULL
445 * (in-memory cache) context.
447 TALLOC_FREE(data.dptr);
449 * Reparent d into the in-memory cache so it can be reused if the
450 * sequence number matches. See parse_share_modes()
451 * for details.
454 share_mode_memcache_store(d);
455 return -1;
458 /*******************************************************************
459 Allocate a new share_mode_data struct, mark it unmodified.
460 fresh is set to note that currently there is no database entry.
461 ********************************************************************/
463 static struct share_mode_data *fresh_share_mode_lock(
464 TALLOC_CTX *mem_ctx, const char *servicepath,
465 const struct smb_filename *smb_fname,
466 const struct timespec *old_write_time)
468 struct share_mode_data *d;
470 if ((servicepath == NULL) || (smb_fname == NULL) ||
471 (old_write_time == NULL)) {
472 return NULL;
475 d = talloc_zero(mem_ctx, struct share_mode_data);
476 if (d == NULL) {
477 goto fail;
479 /* New record - new sequence number. */
480 generate_random_buffer((uint8_t *)&d->sequence_number, 8);
482 d->base_name = talloc_strdup(d, smb_fname->base_name);
483 if (d->base_name == NULL) {
484 goto fail;
486 if (smb_fname->stream_name != NULL) {
487 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
488 if (d->stream_name == NULL) {
489 goto fail;
492 d->servicepath = talloc_strdup(d, servicepath);
493 if (d->servicepath == NULL) {
494 goto fail;
496 d->old_write_time = *old_write_time;
497 d->modified = false;
498 d->fresh = true;
499 return d;
500 fail:
501 DEBUG(0, ("talloc failed\n"));
502 TALLOC_FREE(d);
503 return NULL;
506 /*******************************************************************
507 Either fetch a share mode from the database, or allocate a fresh
508 one if the record doesn't exist.
509 ********************************************************************/
511 static struct share_mode_lock *get_share_mode_lock_internal(
512 TALLOC_CTX *mem_ctx, struct file_id id,
513 const char *servicepath, const struct smb_filename *smb_fname,
514 const struct timespec *old_write_time)
516 struct share_mode_lock *lck;
517 struct share_mode_data *d;
518 struct db_record *rec;
519 TDB_DATA key = locking_key(&id);
520 TDB_DATA value;
522 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
523 if (rec == NULL) {
524 DEBUG(3, ("Could not lock share entry\n"));
525 return NULL;
528 value = dbwrap_record_get_value(rec);
530 if (value.dptr == NULL) {
531 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
532 old_write_time);
533 } else {
534 d = parse_share_modes(mem_ctx, key, value);
537 if (d == NULL) {
538 DEBUG(5, ("get_share_mode_lock_internal: "
539 "Could not get share mode lock\n"));
540 TALLOC_FREE(rec);
541 return NULL;
543 d->id = id;
544 d->record = talloc_move(d, &rec);
545 talloc_set_destructor(d, share_mode_data_destructor);
547 lck = talloc(mem_ctx, struct share_mode_lock);
548 if (lck == NULL) {
549 DEBUG(1, ("talloc failed\n"));
550 TALLOC_FREE(d);
551 return NULL;
553 lck->data = talloc_move(lck, &d);
554 return lck;
558 * We can only ever have one share mode locked. Users of
559 * get_share_mode_lock never see this, it will be refcounted by
560 * talloc_reference.
562 static struct share_mode_lock *the_lock;
563 static struct file_id the_lock_id;
565 static int the_lock_destructor(struct share_mode_lock *l)
567 the_lock = NULL;
568 ZERO_STRUCT(the_lock_id);
569 return 0;
572 /*******************************************************************
573 Get a share_mode_lock, Reference counted to allow nested calls.
574 ********************************************************************/
576 struct share_mode_lock *get_share_mode_lock(
577 TALLOC_CTX *mem_ctx,
578 struct file_id id,
579 const char *servicepath,
580 const struct smb_filename *smb_fname,
581 const struct timespec *old_write_time)
583 struct share_mode_lock *lck;
585 lck = talloc(mem_ctx, struct share_mode_lock);
586 if (lck == NULL) {
587 DEBUG(1, ("talloc failed\n"));
588 return NULL;
591 if (the_lock == NULL) {
592 the_lock = get_share_mode_lock_internal(
593 lck, id, servicepath, smb_fname, old_write_time);
594 if (the_lock == NULL) {
595 goto fail;
597 talloc_set_destructor(the_lock, the_lock_destructor);
598 the_lock_id = id;
599 } else {
600 if (!file_id_equal(&the_lock_id, &id)) {
601 DEBUG(1, ("Can not lock two share modes "
602 "simultaneously\n"));
603 goto fail;
605 if (talloc_reference(lck, the_lock) == NULL) {
606 DEBUG(1, ("talloc_reference failed\n"));
607 goto fail;
610 lck->data = the_lock->data;
611 return lck;
612 fail:
613 TALLOC_FREE(lck);
614 return NULL;
617 struct fetch_share_mode_unlocked_state {
618 TALLOC_CTX *mem_ctx;
619 struct share_mode_lock *lck;
622 static void fetch_share_mode_unlocked_parser(
623 TDB_DATA key, TDB_DATA data, void *private_data)
625 struct fetch_share_mode_unlocked_state *state = private_data;
627 if (data.dsize == 0) {
628 /* Likely a ctdb tombstone record, ignore it */
629 return;
632 state->lck = talloc(state->mem_ctx, struct share_mode_lock);
633 if (state->lck == NULL) {
634 DEBUG(0, ("talloc failed\n"));
635 return;
638 state->lck->data = parse_share_modes(state->lck, key, data);
641 /*******************************************************************
642 Get a share_mode_lock without locking the database or reference
643 counting. Used by smbstatus to display existing share modes.
644 ********************************************************************/
646 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
647 struct file_id id)
649 struct fetch_share_mode_unlocked_state state = { .mem_ctx = mem_ctx };
650 TDB_DATA key = locking_key(&id);
651 NTSTATUS status;
653 status = dbwrap_parse_record(
654 lock_db, key, fetch_share_mode_unlocked_parser, &state);
655 if (!NT_STATUS_IS_OK(status)) {
656 return NULL;
658 return state.lck;
661 static void fetch_share_mode_done(struct tevent_req *subreq);
663 struct fetch_share_mode_state {
664 struct file_id id;
665 TDB_DATA key;
666 struct share_mode_lock *lck;
667 enum dbwrap_req_state req_state;
671 * @brief Get a share_mode_lock without locking or refcounting
673 * This can be used in a clustered Samba environment where the async dbwrap
674 * request is sent over a socket to the local ctdbd. If the send queue is full
675 * and the caller was issuing multiple async dbwrap requests in a loop, the
676 * caller knows it's probably time to stop sending requests for now and try
677 * again later.
679 * @param[in] mem_ctx The talloc memory context to use.
681 * @param[in] ev The event context to work on.
683 * @param[in] id The file id for the locking.tdb key
685 * @param[out] queued This boolean out parameter tells the caller whether the
686 * async request is blocked in a full send queue:
688 * false := request is dispatched
690 * true := send queue is full, request waiting to be
691 * dispatched
693 * @return The new async request, NULL on error.
695 struct tevent_req *fetch_share_mode_send(TALLOC_CTX *mem_ctx,
696 struct tevent_context *ev,
697 struct file_id id,
698 bool *queued)
700 struct tevent_req *req = NULL;
701 struct fetch_share_mode_state *state = NULL;
702 struct tevent_req *subreq = NULL;
704 *queued = false;
706 req = tevent_req_create(mem_ctx, &state,
707 struct fetch_share_mode_state);
708 if (req == NULL) {
709 return NULL;
712 state->id = id;
713 state->key = locking_key(&state->id);
714 state->lck = talloc_zero(state, struct share_mode_lock);
715 if (tevent_req_nomem(state->lck, req)) {
716 return tevent_req_post(req, ev);
719 subreq = dbwrap_parse_record_send(state,
721 lock_db,
722 state->key,
723 fetch_share_mode_unlocked_parser,
724 state->lck,
725 &state->req_state);
726 if (tevent_req_nomem(subreq, req)) {
727 return tevent_req_post(req, ev);
729 tevent_req_set_callback(subreq, fetch_share_mode_done, req);
731 if (state->req_state < DBWRAP_REQ_DISPATCHED) {
732 *queued = true;
734 return req;
737 static void fetch_share_mode_done(struct tevent_req *subreq)
739 struct tevent_req *req = tevent_req_callback_data(
740 subreq, struct tevent_req);
741 NTSTATUS status;
743 status = dbwrap_parse_record_recv(subreq);
744 TALLOC_FREE(subreq);
745 if (tevent_req_nterror(req, status)) {
746 return;
749 tevent_req_done(req);
750 return;
753 NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
754 TALLOC_CTX *mem_ctx,
755 struct share_mode_lock **_lck)
757 struct fetch_share_mode_state *state = tevent_req_data(
758 req, struct fetch_share_mode_state);
759 struct share_mode_lock *lck = NULL;
761 NTSTATUS status;
763 if (tevent_req_is_nterror(req, &status)) {
764 tevent_req_received(req);
765 return status;
768 if (state->lck->data == NULL) {
769 tevent_req_received(req);
770 return NT_STATUS_NOT_FOUND;
773 lck = talloc_move(mem_ctx, &state->lck);
775 if (DEBUGLEVEL >= 10) {
776 DBG_DEBUG("share_mode_data:\n");
777 NDR_PRINT_DEBUG(share_mode_data, lck->data);
780 *_lck = lck;
781 tevent_req_received(req);
782 return NT_STATUS_OK;
785 struct share_mode_forall_state {
786 int (*fn)(struct file_id fid, const struct share_mode_data *data,
787 void *private_data);
788 void *private_data;
791 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
793 struct share_mode_forall_state *state =
794 (struct share_mode_forall_state *)_state;
795 uint32_t i;
796 TDB_DATA key;
797 TDB_DATA value;
798 DATA_BLOB blob;
799 enum ndr_err_code ndr_err;
800 struct share_mode_data *d;
801 struct file_id fid;
802 int ret;
804 key = dbwrap_record_get_key(rec);
805 value = dbwrap_record_get_value(rec);
807 /* Ensure this is a locking_key record. */
808 if (key.dsize != sizeof(fid)) {
809 return 0;
811 memcpy(&fid, key.dptr, sizeof(fid));
813 d = talloc(talloc_tos(), struct share_mode_data);
814 if (d == NULL) {
815 return 0;
818 blob.data = value.dptr;
819 blob.length = value.dsize;
821 ndr_err = ndr_pull_struct_blob_all(
822 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
823 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
824 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
825 return 0;
828 for (i=0; i<d->num_share_modes; i++) {
829 d->share_modes[i].stale = false;
832 if (DEBUGLEVEL > 10) {
833 DEBUG(11, ("parse_share_modes:\n"));
834 NDR_PRINT_DEBUG(share_mode_data, d);
837 ret = state->fn(fid, d, state->private_data);
839 TALLOC_FREE(d);
840 return ret;
843 int share_mode_forall(int (*fn)(struct file_id fid,
844 const struct share_mode_data *data,
845 void *private_data),
846 void *private_data)
848 struct share_mode_forall_state state = {
849 .fn = fn,
850 .private_data = private_data
852 NTSTATUS status;
853 int count;
855 if (lock_db == NULL) {
856 return 0;
859 status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
860 &state, &count);
861 if (!NT_STATUS_IS_OK(status)) {
862 return -1;
865 return count;
868 struct share_entry_forall_state {
869 int (*fn)(struct file_id fid,
870 const struct share_mode_data *data,
871 const struct share_mode_entry *entry,
872 void *private_data);
873 void *private_data;
876 static int share_entry_traverse_fn(struct file_id fid,
877 const struct share_mode_data *data,
878 void *private_data)
880 struct share_entry_forall_state *state = private_data;
881 uint32_t i;
883 for (i=0; i<data->num_share_modes; i++) {
884 int ret;
886 ret = state->fn(fid,
887 data,
888 &data->share_modes[i],
889 state->private_data);
890 if (ret != 0) {
891 return ret;
895 return 0;
898 /*******************************************************************
899 Call the specified function on each entry under management by the
900 share mode system.
901 ********************************************************************/
903 int share_entry_forall(int (*fn)(struct file_id fid,
904 const struct share_mode_data *data,
905 const struct share_mode_entry *entry,
906 void *private_data),
907 void *private_data)
909 struct share_entry_forall_state state = {
910 .fn = fn, .private_data = private_data };
912 return share_mode_forall(share_entry_traverse_fn, &state);
915 bool share_mode_cleanup_disconnected(struct file_id fid,
916 uint64_t open_persistent_id)
918 bool ret = false;
919 TALLOC_CTX *frame = talloc_stackframe();
920 unsigned n;
921 struct share_mode_data *data;
922 struct share_mode_lock *lck;
923 bool ok;
925 lck = get_existing_share_mode_lock(frame, fid);
926 if (lck == NULL) {
927 DEBUG(5, ("share_mode_cleanup_disconnected: "
928 "Could not fetch share mode entry for %s\n",
929 file_id_string(frame, &fid)));
930 goto done;
932 data = lck->data;
934 for (n=0; n < data->num_share_modes; n++) {
935 struct share_mode_entry *entry = &data->share_modes[n];
937 if (!server_id_is_disconnected(&entry->pid)) {
938 struct server_id_buf tmp;
939 DEBUG(5, ("share_mode_cleanup_disconnected: "
940 "file (file-id='%s', servicepath='%s', "
941 "base_name='%s%s%s') "
942 "is used by server %s ==> do not cleanup\n",
943 file_id_string(frame, &fid),
944 data->servicepath,
945 data->base_name,
946 (data->stream_name == NULL)
947 ? "" : "', stream_name='",
948 (data->stream_name == NULL)
949 ? "" : data->stream_name,
950 server_id_str_buf(entry->pid, &tmp)));
951 goto done;
953 if (open_persistent_id != entry->share_file_id) {
954 DEBUG(5, ("share_mode_cleanup_disconnected: "
955 "entry for file "
956 "(file-id='%s', servicepath='%s', "
957 "base_name='%s%s%s') "
958 "has share_file_id %llu but expected %llu"
959 "==> do not cleanup\n",
960 file_id_string(frame, &fid),
961 data->servicepath,
962 data->base_name,
963 (data->stream_name == NULL)
964 ? "" : "', stream_name='",
965 (data->stream_name == NULL)
966 ? "" : data->stream_name,
967 (unsigned long long)entry->share_file_id,
968 (unsigned long long)open_persistent_id));
969 goto done;
973 for (n=0; n < data->num_leases; n++) {
974 struct share_mode_lease *l = &data->leases[n];
975 NTSTATUS status;
977 status = leases_db_del(&l->client_guid, &l->lease_key, &fid);
979 DEBUG(10, ("%s: leases_db_del returned %s\n", __func__,
980 nt_errstr(status)));
983 ok = brl_cleanup_disconnected(fid, open_persistent_id);
984 if (!ok) {
985 DEBUG(10, ("share_mode_cleanup_disconnected: "
986 "failed to clean up byte range locks associated "
987 "with file (file-id='%s', servicepath='%s', "
988 "base_name='%s%s%s') and open_persistent_id %llu "
989 "==> do not cleanup\n",
990 file_id_string(frame, &fid),
991 data->servicepath,
992 data->base_name,
993 (data->stream_name == NULL)
994 ? "" : "', stream_name='",
995 (data->stream_name == NULL)
996 ? "" : data->stream_name,
997 (unsigned long long)open_persistent_id));
998 goto done;
1001 DEBUG(10, ("share_mode_cleanup_disconnected: "
1002 "cleaning up %u entries for file "
1003 "(file-id='%s', servicepath='%s', "
1004 "base_name='%s%s%s') "
1005 "from open_persistent_id %llu\n",
1006 data->num_share_modes,
1007 file_id_string(frame, &fid),
1008 data->servicepath,
1009 data->base_name,
1010 (data->stream_name == NULL)
1011 ? "" : "', stream_name='",
1012 (data->stream_name == NULL)
1013 ? "" : data->stream_name,
1014 (unsigned long long)open_persistent_id));
1016 data->num_share_modes = 0;
1017 data->num_leases = 0;
1018 data->modified = true;
1020 ret = true;
1021 done:
1022 talloc_free(frame);
1023 return ret;