2 Unix SMB/CIFS implementation.
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/>.
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
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.
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"
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"
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;
73 db_path = lock_path(talloc_tos(), "locking.tdb");
74 if (db_path == NULL) {
78 backend = db_open(NULL, db_path,
79 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
83 TDB_INCOMPATIBLE_HASH|
85 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
86 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
89 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
93 lock_db = db_open_watched(NULL, &backend, global_messaging_context());
94 if (lock_db == NULL) {
95 DBG_ERR("db_open_watched failed\n");
100 if (!posix_locking_init(read_only)) {
101 TALLOC_FREE(lock_db);
108 bool locking_init(void)
110 return locking_init_internal(false);
113 bool locking_init_readonly(void)
115 return locking_init_internal(true);
118 /*******************************************************************
119 Deinitialize the share_mode management.
120 ******************************************************************/
122 bool locking_end(void)
125 TALLOC_FREE(lock_db);
129 /*******************************************************************
130 Form a static locking key for a dev/inode pair.
131 ******************************************************************/
133 static TDB_DATA locking_key(const struct file_id *id)
135 return make_tdb_data((const uint8_t *)id, sizeof(*id));
138 /*******************************************************************
139 Share mode cache utility functions that store/delete/retrieve
140 entries from memcache.
142 For now share the statcache (global cache) memory space. If
143 a lock record gets orphaned (which shouldn't happen as we're
144 using the same locking_key data as lookup) it will eventually
145 fall out of the cache via the normal LRU trim mechanism. If
146 necessary we can always make this a separate (smaller) cache.
147 ******************************************************************/
149 static DATA_BLOB memcache_key(const struct file_id *id)
151 return data_blob_const((const void *)id, sizeof(*id));
154 static void share_mode_memcache_store(struct share_mode_data *d)
156 const DATA_BLOB key = memcache_key(&d->id);
158 DBG_DEBUG("stored entry for file %s seq %"PRIx64" key %s\n",
161 file_id_string(talloc_tos(), &d->id));
163 /* Ensure everything stored in the cache is pristine. */
168 * Ensure the memory going into the cache
169 * doesn't have a destructor so it can be
170 * cleanly evicted by the memcache LRU
173 talloc_set_destructor(d, NULL);
175 /* Cache will own d after this call. */
176 memcache_add_talloc(NULL,
177 SHARE_MODE_LOCK_CACHE,
183 * NB. We use ndr_pull_hyper on a stack-created
184 * struct ndr_pull with no talloc allowed, as we
185 * need this to be really fast as an ndr-peek into
186 * the first 9 bytes of the blob.
189 static enum ndr_err_code get_share_mode_blob_header(
190 DATA_BLOB *blob, uint64_t *pseq, uint8_t *pflags)
192 struct ndr_pull ndr = {.data = blob->data, .data_size = blob->length};
193 NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pseq));
194 NDR_CHECK(ndr_pull_uint8(&ndr, NDR_SCALARS, pflags));
195 return NDR_ERR_SUCCESS;
198 struct fsp_update_share_mode_flags_state {
199 enum ndr_err_code ndr_err;
200 uint8_t share_mode_flags;
203 static void fsp_update_share_mode_flags_fn(
204 struct db_record *rec, bool *modified_dependent, void *private_data)
206 struct fsp_update_share_mode_flags_state *state = private_data;
207 TDB_DATA value = dbwrap_record_get_value(rec);
208 DATA_BLOB blob = { .data = value.dptr, .length = value.dsize };
211 state->ndr_err = get_share_mode_blob_header(
212 &blob, &seq, &state->share_mode_flags);
215 static NTSTATUS fsp_update_share_mode_flags(struct files_struct *fsp)
217 struct fsp_update_share_mode_flags_state state = {0};
218 int seqnum = dbwrap_get_seqnum(lock_db);
221 if (seqnum == fsp->share_mode_flags_seqnum) {
225 status = share_mode_do_locked(
226 fsp->file_id, fsp_update_share_mode_flags_fn, &state);
227 if (!NT_STATUS_IS_OK(status)) {
228 DBG_DEBUG("share_mode_do_locked returned %s\n",
233 if (!NDR_ERR_CODE_IS_SUCCESS(state.ndr_err)) {
234 DBG_DEBUG("get_share_mode_blob_header returned %s\n",
235 ndr_errstr(state.ndr_err));
236 return ndr_map_error2ntstatus(state.ndr_err);
239 fsp->share_mode_flags_seqnum = seqnum;
240 fsp->share_mode_flags = state.share_mode_flags;
245 bool file_has_read_lease(struct files_struct *fsp)
249 status = fsp_update_share_mode_flags(fsp);
250 if (!NT_STATUS_IS_OK(status)) {
251 /* Safe default for leases */
255 return (fsp->share_mode_flags & SHARE_MODE_HAS_READ_LEASE) != 0;
258 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
263 static struct share_mode_data *share_mode_memcache_fetch(TALLOC_CTX *mem_ctx,
264 const TDB_DATA id_key,
267 enum ndr_err_code ndr_err;
268 struct share_mode_data *d;
269 uint64_t sequence_number;
275 /* Ensure this is a locking_key record. */
276 if (id_key.dsize != sizeof(id)) {
280 memcpy(&id, id_key.dptr, id_key.dsize);
281 key = memcache_key(&id);
283 ptr = memcache_lookup_talloc(NULL,
284 SHARE_MODE_LOCK_CACHE,
287 DEBUG(10,("failed to find entry for key %s\n",
288 file_id_string(mem_ctx, &id)));
291 /* sequence number key is at start of blob. */
292 ndr_err = get_share_mode_blob_header(blob, &sequence_number, &flags);
293 if (ndr_err != NDR_ERR_SUCCESS) {
294 /* Bad blob. Remove entry. */
295 DEBUG(10,("bad blob %u key %s\n",
296 (unsigned int)ndr_err,
297 file_id_string(mem_ctx, &id)));
298 memcache_delete(NULL,
299 SHARE_MODE_LOCK_CACHE,
304 d = (struct share_mode_data *)ptr;
305 if (d->sequence_number != sequence_number) {
306 DBG_DEBUG("seq changed (cached %"PRIx64") (new %"PRIx64") "
310 file_id_string(mem_ctx, &id));
311 /* Cache out of date. Remove entry. */
312 memcache_delete(NULL,
313 SHARE_MODE_LOCK_CACHE,
318 /* Move onto mem_ctx. */
319 d = talloc_move(mem_ctx, &ptr);
322 * Now we own d, prevent the cache from freeing it
323 * when we delete the entry.
325 talloc_set_destructor(d, share_mode_data_nofree_destructor);
327 /* Remove from the cache. We own it now. */
328 memcache_delete(NULL,
329 SHARE_MODE_LOCK_CACHE,
332 /* And reset the destructor to none. */
333 talloc_set_destructor(d, NULL);
335 DBG_DEBUG("fetched entry for file %s seq %"PRIx64" key %s\n",
338 file_id_string(mem_ctx, &id));
343 /*******************************************************************
344 Get all share mode entries for a dev/inode pair.
345 ********************************************************************/
347 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
351 struct share_mode_data *d;
352 enum ndr_err_code ndr_err;
355 blob.data = dbuf.dptr;
356 blob.length = dbuf.dsize;
358 /* See if we already have a cached copy of this key. */
359 d = share_mode_memcache_fetch(mem_ctx, key, &blob);
364 d = talloc(mem_ctx, struct share_mode_data);
366 DEBUG(0, ("talloc failed\n"));
370 ndr_err = ndr_pull_struct_blob_all(
371 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
372 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
373 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
374 ndr_errstr(ndr_err));
378 if (DEBUGLEVEL >= 10) {
379 DEBUG(10, ("parse_share_modes:\n"));
380 NDR_PRINT_DEBUG(share_mode_data, d);
389 /*******************************************************************
390 If modified, store the share_mode_data back into the database.
391 ********************************************************************/
393 static NTSTATUS share_mode_data_store(struct share_mode_data *d)
396 enum ndr_err_code ndr_err;
400 DBG_DEBUG("not modified\n");
404 if (DEBUGLEVEL >= 10) {
406 NDR_PRINT_DEBUG(share_mode_data, d);
409 d->sequence_number += 1;
410 remove_stale_share_mode_entries(d);
412 if (d->num_share_modes == 0) {
414 DBG_DEBUG("Ignoring fresh empty record\n");
417 status = dbwrap_record_delete(d->record);
421 ndr_err = ndr_push_struct_blob(
422 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
423 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
424 DBG_DEBUG("ndr_push_share_mode_data failed: %s\n",
425 ndr_errstr(ndr_err));
426 return ndr_map_error2ntstatus(ndr_err);
429 status = dbwrap_record_store(
431 (TDB_DATA) { .dptr = blob.data, .dsize = blob.length },
433 TALLOC_FREE(blob.data);
435 if (!NT_STATUS_IS_OK(status)) {
436 DBG_DEBUG("dbwrap_record_store failed: %s\n",
443 /*******************************************************************
444 Allocate a new share_mode_data struct, mark it unmodified.
445 fresh is set to note that currently there is no database entry.
446 ********************************************************************/
448 static struct share_mode_data *fresh_share_mode_lock(
449 TALLOC_CTX *mem_ctx, const char *servicepath,
450 const struct smb_filename *smb_fname,
451 const struct timespec *old_write_time)
453 struct share_mode_data *d;
455 if ((servicepath == NULL) || (smb_fname == NULL) ||
456 (old_write_time == NULL)) {
460 d = talloc_zero(mem_ctx, struct share_mode_data);
464 /* New record - new sequence number. */
465 generate_random_buffer((uint8_t *)&d->sequence_number, 8);
467 d->base_name = talloc_strdup(d, smb_fname->base_name);
468 if (d->base_name == NULL) {
471 if (smb_fname->stream_name != NULL) {
472 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
473 if (d->stream_name == NULL) {
477 d->servicepath = talloc_strdup(d, servicepath);
478 if (d->servicepath == NULL) {
481 d->old_write_time = *old_write_time;
486 DEBUG(0, ("talloc failed\n"));
492 * We can only ever have one share mode locked. Use a static
493 * share_mode_data pointer that is shared by multiple nested
494 * share_mode_lock structures, explicitly refcounted.
496 static struct share_mode_data *static_share_mode_data = NULL;
497 static size_t static_share_mode_data_refcount = 0;
500 * db_record for the above. With dbwrap_do_locked we can get a
501 * db_record on the stack, which we can't TALLOC_FREE but which we
502 * need to share with a nested get_share_mode_lock call.
504 static struct db_record *static_share_mode_record = NULL;
505 static bool static_share_mode_record_talloced = false;
507 /*******************************************************************
508 Either fetch a share mode from the database, or allocate a fresh
509 one if the record doesn't exist.
510 ********************************************************************/
512 static NTSTATUS get_static_share_mode_data(
513 struct db_record *rec,
515 const char *servicepath,
516 const struct smb_filename *smb_fname,
517 const struct timespec *old_write_time)
519 struct share_mode_data *d;
520 TDB_DATA value = dbwrap_record_get_value(rec);
522 SMB_ASSERT(static_share_mode_data == NULL);
524 if (value.dptr == NULL) {
525 d = fresh_share_mode_lock(
526 lock_db, servicepath, smb_fname, old_write_time);
528 return NT_STATUS_NO_MEMORY;
531 TDB_DATA key = locking_key(&id);
532 d = parse_share_modes(lock_db, key, value);
534 return NT_STATUS_INTERNAL_DB_CORRUPTION;
541 static_share_mode_data = d;
546 /*******************************************************************
547 Get a share_mode_lock, Reference counted to allow nested calls.
548 ********************************************************************/
550 static int share_mode_lock_destructor(struct share_mode_lock *lck);
552 struct share_mode_lock *get_share_mode_lock(
555 const char *servicepath,
556 const struct smb_filename *smb_fname,
557 const struct timespec *old_write_time)
559 TDB_DATA key = locking_key(&id);
560 struct share_mode_lock *lck = NULL;
563 lck = talloc(mem_ctx, struct share_mode_lock);
565 DEBUG(1, ("talloc failed\n"));
569 if (static_share_mode_data != NULL) {
570 if (!file_id_equal(&static_share_mode_data->id, &id)) {
571 DEBUG(1, ("Can not lock two share modes "
572 "simultaneously\n"));
578 SMB_ASSERT(static_share_mode_data_refcount == 0);
580 if (static_share_mode_record == NULL) {
581 static_share_mode_record = dbwrap_fetch_locked(
582 lock_db, lock_db, key);
583 if (static_share_mode_record == NULL) {
584 DEBUG(3, ("Could not lock share entry\n"));
587 static_share_mode_record_talloced = true;
589 status = get_static_share_mode_data(
590 static_share_mode_record,
595 if (!NT_STATUS_IS_OK(status)) {
596 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
598 TALLOC_FREE(static_share_mode_record);
605 static_key = dbwrap_record_get_key(static_share_mode_record);
607 cmp = tdb_data_cmp(static_key, key);
609 DBG_WARNING("Can not lock two share modes "
614 status = get_static_share_mode_data(
615 static_share_mode_record,
620 if (!NT_STATUS_IS_OK(status)) {
621 DBG_WARNING("get_static_share_mode_data failed: %s\n",
628 static_share_mode_data_refcount += 1;
629 lck->data = static_share_mode_data;
631 talloc_set_destructor(lck, share_mode_lock_destructor);
639 static int share_mode_lock_destructor(struct share_mode_lock *lck)
643 SMB_ASSERT(static_share_mode_data_refcount > 0);
644 static_share_mode_data_refcount -= 1;
646 if (static_share_mode_data_refcount > 0) {
650 status = share_mode_data_store(static_share_mode_data);
651 if (!NT_STATUS_IS_OK(status)) {
652 DBG_ERR("share_mode_data_store failed: %s\n",
654 smb_panic("Could not store share mode data\n");
658 * Drop the locking.tdb lock before moving the share_mode_data
661 SMB_ASSERT(static_share_mode_data->record == static_share_mode_record);
662 static_share_mode_data->record = NULL;
664 if (static_share_mode_record_talloced) {
665 TALLOC_FREE(static_share_mode_record);
668 if (static_share_mode_data->num_share_modes != 0) {
670 * This is worth keeping. Without share modes,
671 * share_mode_data_store above has left nothing in the
674 share_mode_memcache_store(static_share_mode_data);
675 static_share_mode_data = NULL;
678 * The next opener of this file will find an empty
679 * locking.tdb record. Don't store the share_mode_data
680 * in the memcache, fresh_share_mode_lock() will
681 * generate a fresh seqnum anyway, obsoleting the
684 TALLOC_FREE(static_share_mode_data);
690 struct share_mode_do_locked_state {
691 void (*fn)(struct db_record *rec,
692 bool *modified_dependent,
697 static void share_mode_do_locked_fn(struct db_record *rec,
700 struct share_mode_do_locked_state *state = private_data;
701 bool modified_dependent = false;
702 bool reset_static_share_mode_record = false;
704 if (static_share_mode_record == NULL) {
705 static_share_mode_record = rec;
706 static_share_mode_record_talloced = false;
707 reset_static_share_mode_record = true;
709 SMB_ASSERT(static_share_mode_record == rec);
712 state->fn(rec, &modified_dependent, state->private_data);
714 if (modified_dependent) {
715 dbwrap_watched_wakeup(rec);
718 if (reset_static_share_mode_record) {
719 static_share_mode_record = NULL;
723 NTSTATUS share_mode_do_locked(
725 void (*fn)(struct db_record *rec,
726 bool *modified_dependent,
730 TDB_DATA key = locking_key(&id);
731 size_t refcount = static_share_mode_data_refcount;
733 if (static_share_mode_record != NULL) {
734 bool modified_dependent = false;
738 static_key = dbwrap_record_get_key(static_share_mode_record);
740 cmp = tdb_data_cmp(static_key, key);
742 DBG_WARNING("Can not lock two share modes "
744 return NT_STATUS_INVALID_LOCK_SEQUENCE;
747 fn(static_share_mode_record,
751 if (modified_dependent) {
752 dbwrap_watched_wakeup(static_share_mode_record);
755 struct share_mode_do_locked_state state = {
756 .fn = fn, .private_data = private_data,
760 status = dbwrap_do_locked(
761 lock_db, key, share_mode_do_locked_fn, &state);
762 if (!NT_STATUS_IS_OK(status)) {
763 DBG_WARNING("dbwrap_do_locked failed: %s\n",
769 SMB_ASSERT(refcount == static_share_mode_data_refcount);
774 static void share_mode_wakeup_waiters_fn(struct db_record *rec,
775 bool *modified_dependent,
778 *modified_dependent = true;
781 NTSTATUS share_mode_wakeup_waiters(struct file_id id)
783 return share_mode_do_locked(id, share_mode_wakeup_waiters_fn, NULL);
786 struct fetch_share_mode_unlocked_state {
788 struct share_mode_lock *lck;
791 static void fetch_share_mode_unlocked_parser(
792 TDB_DATA key, TDB_DATA data, void *private_data)
794 struct fetch_share_mode_unlocked_state *state = private_data;
796 if (data.dsize == 0) {
797 /* Likely a ctdb tombstone record, ignore it */
801 state->lck = talloc(state->mem_ctx, struct share_mode_lock);
802 if (state->lck == NULL) {
803 DEBUG(0, ("talloc failed\n"));
807 state->lck->data = parse_share_modes(state->lck, key, data);
810 /*******************************************************************
811 Get a share_mode_lock without locking the database or reference
812 counting. Used by smbstatus to display existing share modes.
813 ********************************************************************/
815 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
818 struct fetch_share_mode_unlocked_state state = { .mem_ctx = mem_ctx };
819 TDB_DATA key = locking_key(&id);
822 status = dbwrap_parse_record(
823 lock_db, key, fetch_share_mode_unlocked_parser, &state);
824 if (!NT_STATUS_IS_OK(status)) {
830 static void fetch_share_mode_done(struct tevent_req *subreq);
832 struct fetch_share_mode_state {
835 struct fetch_share_mode_unlocked_state parser_state;
836 enum dbwrap_req_state req_state;
840 * @brief Get a share_mode_lock without locking or refcounting
842 * This can be used in a clustered Samba environment where the async dbwrap
843 * request is sent over a socket to the local ctdbd. If the send queue is full
844 * and the caller was issuing multiple async dbwrap requests in a loop, the
845 * caller knows it's probably time to stop sending requests for now and try
848 * @param[in] mem_ctx The talloc memory context to use.
850 * @param[in] ev The event context to work on.
852 * @param[in] id The file id for the locking.tdb key
854 * @param[out] queued This boolean out parameter tells the caller whether the
855 * async request is blocked in a full send queue:
857 * false := request is dispatched
859 * true := send queue is full, request waiting to be
862 * @return The new async request, NULL on error.
864 struct tevent_req *fetch_share_mode_send(TALLOC_CTX *mem_ctx,
865 struct tevent_context *ev,
869 struct tevent_req *req = NULL;
870 struct fetch_share_mode_state *state = NULL;
871 struct tevent_req *subreq = NULL;
875 req = tevent_req_create(mem_ctx, &state,
876 struct fetch_share_mode_state);
882 state->key = locking_key(&state->id);
883 state->parser_state.mem_ctx = state;
885 subreq = dbwrap_parse_record_send(state,
889 fetch_share_mode_unlocked_parser,
890 &state->parser_state,
892 if (tevent_req_nomem(subreq, req)) {
893 return tevent_req_post(req, ev);
895 tevent_req_set_callback(subreq, fetch_share_mode_done, req);
897 if (state->req_state < DBWRAP_REQ_DISPATCHED) {
903 static void fetch_share_mode_done(struct tevent_req *subreq)
905 struct tevent_req *req = tevent_req_callback_data(
906 subreq, struct tevent_req);
909 status = dbwrap_parse_record_recv(subreq);
911 if (tevent_req_nterror(req, status)) {
915 tevent_req_done(req);
919 NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
921 struct share_mode_lock **_lck)
923 struct fetch_share_mode_state *state = tevent_req_data(
924 req, struct fetch_share_mode_state);
925 struct share_mode_lock *lck = NULL;
929 if (tevent_req_is_nterror(req, &status)) {
930 tevent_req_received(req);
934 if (state->parser_state.lck->data == NULL) {
935 tevent_req_received(req);
936 return NT_STATUS_NOT_FOUND;
939 lck = talloc_move(mem_ctx, &state->parser_state.lck);
941 if (DEBUGLEVEL >= 10) {
942 DBG_DEBUG("share_mode_data:\n");
943 NDR_PRINT_DEBUG(share_mode_data, lck->data);
947 tevent_req_received(req);
951 struct share_mode_forall_state {
952 int (*fn)(struct file_id fid, const struct share_mode_data *data,
957 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
959 struct share_mode_forall_state *state =
960 (struct share_mode_forall_state *)_state;
964 enum ndr_err_code ndr_err;
965 struct share_mode_data *d;
969 key = dbwrap_record_get_key(rec);
970 value = dbwrap_record_get_value(rec);
972 /* Ensure this is a locking_key record. */
973 if (key.dsize != sizeof(fid)) {
976 memcpy(&fid, key.dptr, sizeof(fid));
978 d = talloc(talloc_tos(), struct share_mode_data);
983 blob.data = value.dptr;
984 blob.length = value.dsize;
986 ndr_err = ndr_pull_struct_blob_all(
987 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
988 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
989 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
993 if (DEBUGLEVEL > 10) {
994 DEBUG(11, ("parse_share_modes:\n"));
995 NDR_PRINT_DEBUG(share_mode_data, d);
998 ret = state->fn(fid, d, state->private_data);
1004 int share_mode_forall(int (*fn)(struct file_id fid,
1005 const struct share_mode_data *data,
1006 void *private_data),
1009 struct share_mode_forall_state state = {
1011 .private_data = private_data
1016 if (lock_db == NULL) {
1020 status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
1022 if (!NT_STATUS_IS_OK(status)) {
1029 struct share_entry_forall_state {
1030 int (*fn)(struct file_id fid,
1031 const struct share_mode_data *data,
1032 const struct share_mode_entry *entry,
1033 void *private_data);
1037 static int share_entry_traverse_fn(struct file_id fid,
1038 const struct share_mode_data *data,
1041 struct share_entry_forall_state *state = private_data;
1044 for (i=0; i<data->num_share_modes; i++) {
1047 ret = state->fn(fid,
1049 &data->share_modes[i],
1050 state->private_data);
1059 /*******************************************************************
1060 Call the specified function on each entry under management by the
1062 ********************************************************************/
1064 int share_entry_forall(int (*fn)(struct file_id fid,
1065 const struct share_mode_data *data,
1066 const struct share_mode_entry *entry,
1067 void *private_data),
1070 struct share_entry_forall_state state = {
1071 .fn = fn, .private_data = private_data };
1073 return share_mode_forall(share_entry_traverse_fn, &state);
1076 struct cleanup_disconnected_lease_state {
1080 static bool cleanup_disconnected_lease(struct share_mode_lock *lck,
1081 struct share_mode_entry *e,
1084 struct cleanup_disconnected_lease_state *state = private_data;
1087 status = leases_db_del(&e->client_guid, &e->lease_key, &state->fid);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 DBG_DEBUG("leases_db_del failed: %s\n",
1097 bool share_mode_cleanup_disconnected(struct file_id fid,
1098 uint64_t open_persistent_id)
1100 struct cleanup_disconnected_lease_state state = { .fid = fid };
1102 TALLOC_CTX *frame = talloc_stackframe();
1104 struct share_mode_data *data;
1105 struct share_mode_lock *lck;
1108 lck = get_existing_share_mode_lock(frame, fid);
1110 DEBUG(5, ("share_mode_cleanup_disconnected: "
1111 "Could not fetch share mode entry for %s\n",
1112 file_id_string(frame, &fid)));
1117 for (n=0; n < data->num_share_modes; n++) {
1118 struct share_mode_entry *entry = &data->share_modes[n];
1120 if (!server_id_is_disconnected(&entry->pid)) {
1121 struct server_id_buf tmp;
1122 DEBUG(5, ("share_mode_cleanup_disconnected: "
1123 "file (file-id='%s', servicepath='%s', "
1124 "base_name='%s%s%s') "
1125 "is used by server %s ==> do not cleanup\n",
1126 file_id_string(frame, &fid),
1129 (data->stream_name == NULL)
1130 ? "" : "', stream_name='",
1131 (data->stream_name == NULL)
1132 ? "" : data->stream_name,
1133 server_id_str_buf(entry->pid, &tmp)));
1136 if (open_persistent_id != entry->share_file_id) {
1137 DBG_INFO("entry for file "
1138 "(file-id='%s', servicepath='%s', "
1139 "base_name='%s%s%s') "
1140 "has share_file_id %"PRIu64" but expected "
1141 "%"PRIu64"==> do not cleanup\n",
1142 file_id_string(frame, &fid),
1145 (data->stream_name == NULL)
1146 ? "" : "', stream_name='",
1147 (data->stream_name == NULL)
1148 ? "" : data->stream_name,
1149 entry->share_file_id,
1150 open_persistent_id);
1155 ok = share_mode_forall_leases(lck, cleanup_disconnected_lease, &state);
1157 DBG_DEBUG("failed to clean up leases associated "
1158 "with file (file-id='%s', servicepath='%s', "
1159 "base_name='%s%s%s') and open_persistent_id %"PRIu64" "
1160 "==> do not cleanup\n",
1161 file_id_string(frame, &fid),
1164 (data->stream_name == NULL)
1165 ? "" : "', stream_name='",
1166 (data->stream_name == NULL)
1167 ? "" : data->stream_name,
1168 open_persistent_id);
1171 ok = brl_cleanup_disconnected(fid, open_persistent_id);
1173 DBG_DEBUG("failed to clean up byte range locks associated "
1174 "with file (file-id='%s', servicepath='%s', "
1175 "base_name='%s%s%s') and open_persistent_id %"PRIu64" "
1176 "==> do not cleanup\n",
1177 file_id_string(frame, &fid),
1180 (data->stream_name == NULL)
1181 ? "" : "', stream_name='",
1182 (data->stream_name == NULL)
1183 ? "" : data->stream_name,
1184 open_persistent_id);
1188 DBG_DEBUG("cleaning up %u entries for file "
1189 "(file-id='%s', servicepath='%s', "
1190 "base_name='%s%s%s') "
1191 "from open_persistent_id %"PRIu64"\n",
1192 data->num_share_modes,
1193 file_id_string(frame, &fid),
1196 (data->stream_name == NULL)
1197 ? "" : "', stream_name='",
1198 (data->stream_name == NULL)
1199 ? "" : data->stream_name,
1200 open_persistent_id);
1202 data->num_share_modes = 0;
1203 data->modified = true;