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;
62 static struct db_context *share_entries_db;
64 static bool locking_init_internal(bool read_only)
66 struct db_context *backend;
74 db_path = lock_path(talloc_tos(), "locking.tdb");
75 if (db_path == NULL) {
79 backend = db_open(NULL, db_path,
80 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
84 TDB_INCOMPATIBLE_HASH|
86 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
87 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
90 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
94 lock_db = db_open_watched(NULL, &backend, global_messaging_context());
95 if (lock_db == NULL) {
96 DBG_ERR("db_open_watched failed\n");
101 db_path = lock_path(talloc_tos(), "share_entries.tdb");
102 if (db_path == NULL) {
106 share_entries_db = db_open(
108 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
112 TDB_INCOMPATIBLE_HASH,
113 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
114 DBWRAP_LOCK_ORDER_3, DBWRAP_FLAG_NONE);
115 TALLOC_FREE(db_path);
117 if (share_entries_db == NULL) {
118 TALLOC_FREE(lock_db);
122 if (!posix_locking_init(read_only)) {
123 TALLOC_FREE(share_entries_db);
124 TALLOC_FREE(lock_db);
131 bool locking_init(void)
133 return locking_init_internal(false);
136 bool locking_init_readonly(void)
138 return locking_init_internal(true);
141 /*******************************************************************
142 Deinitialize the share_mode management.
143 ******************************************************************/
145 bool locking_end(void)
148 TALLOC_FREE(lock_db);
152 /*******************************************************************
153 Form a static locking key for a dev/inode pair.
154 ******************************************************************/
156 static TDB_DATA locking_key(const struct file_id *id)
158 return make_tdb_data((const uint8_t *)id, sizeof(*id));
161 /*******************************************************************
162 Share mode cache utility functions that store/delete/retrieve
163 entries from memcache.
165 For now share the statcache (global cache) memory space. If
166 a lock record gets orphaned (which shouldn't happen as we're
167 using the same locking_key data as lookup) it will eventually
168 fall out of the cache via the normal LRU trim mechanism. If
169 necessary we can always make this a separate (smaller) cache.
170 ******************************************************************/
172 static DATA_BLOB memcache_key(const struct file_id *id)
174 return data_blob_const((const void *)id, sizeof(*id));
177 static void share_mode_memcache_store(struct share_mode_data *d)
179 const DATA_BLOB key = memcache_key(&d->id);
181 DBG_DEBUG("stored entry for file %s seq %"PRIx64" key %s\n",
184 file_id_string(talloc_tos(), &d->id));
186 /* Ensure everything stored in the cache is pristine. */
191 * Ensure the memory going into the cache
192 * doesn't have a destructor so it can be
193 * cleanly evicted by the memcache LRU
196 talloc_set_destructor(d, NULL);
198 /* Cache will own d after this call. */
199 memcache_add_talloc(NULL,
200 SHARE_MODE_LOCK_CACHE,
206 * NB. We use ndr_pull_hyper on a stack-created
207 * struct ndr_pull with no talloc allowed, as we
208 * need this to be really fast as an ndr-peek into
209 * the first 9 bytes of the blob.
212 static enum ndr_err_code get_share_mode_blob_header(
213 DATA_BLOB *blob, uint64_t *pseq, uint16_t *pflags)
215 struct ndr_pull ndr = {.data = blob->data, .data_size = blob->length};
216 NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pseq));
217 NDR_CHECK(ndr_pull_uint16(&ndr, NDR_SCALARS, pflags));
218 return NDR_ERR_SUCCESS;
221 struct fsp_update_share_mode_flags_state {
222 enum ndr_err_code ndr_err;
223 uint16_t share_mode_flags;
226 static void fsp_update_share_mode_flags_fn(
227 struct db_record *rec, bool *modified_dependent, void *private_data)
229 struct fsp_update_share_mode_flags_state *state = private_data;
230 TDB_DATA value = dbwrap_record_get_value(rec);
231 DATA_BLOB blob = { .data = value.dptr, .length = value.dsize };
234 state->ndr_err = get_share_mode_blob_header(
235 &blob, &seq, &state->share_mode_flags);
238 static NTSTATUS fsp_update_share_mode_flags(struct files_struct *fsp)
240 struct fsp_update_share_mode_flags_state state = {0};
241 int seqnum = dbwrap_get_seqnum(lock_db);
244 if (seqnum == fsp->share_mode_flags_seqnum) {
248 status = share_mode_do_locked(
249 fsp->file_id, fsp_update_share_mode_flags_fn, &state);
250 if (!NT_STATUS_IS_OK(status)) {
251 DBG_DEBUG("share_mode_do_locked returned %s\n",
256 if (!NDR_ERR_CODE_IS_SUCCESS(state.ndr_err)) {
257 DBG_DEBUG("get_share_mode_blob_header returned %s\n",
258 ndr_errstr(state.ndr_err));
259 return ndr_map_error2ntstatus(state.ndr_err);
262 fsp->share_mode_flags_seqnum = seqnum;
263 fsp->share_mode_flags = state.share_mode_flags;
268 bool file_has_read_lease(struct files_struct *fsp)
272 status = fsp_update_share_mode_flags(fsp);
273 if (!NT_STATUS_IS_OK(status)) {
274 /* Safe default for leases */
278 return (fsp->share_mode_flags & SHARE_MODE_LEASE_READ) != 0;
281 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
286 static struct share_mode_data *share_mode_memcache_fetch(TALLOC_CTX *mem_ctx,
287 const TDB_DATA id_key,
290 enum ndr_err_code ndr_err;
291 struct share_mode_data *d;
292 uint64_t sequence_number;
298 /* Ensure this is a locking_key record. */
299 if (id_key.dsize != sizeof(id)) {
303 memcpy(&id, id_key.dptr, id_key.dsize);
304 key = memcache_key(&id);
306 ptr = memcache_lookup_talloc(NULL,
307 SHARE_MODE_LOCK_CACHE,
310 DEBUG(10,("failed to find entry for key %s\n",
311 file_id_string(mem_ctx, &id)));
314 /* sequence number key is at start of blob. */
315 ndr_err = get_share_mode_blob_header(blob, &sequence_number, &flags);
316 if (ndr_err != NDR_ERR_SUCCESS) {
317 /* Bad blob. Remove entry. */
318 DEBUG(10,("bad blob %u key %s\n",
319 (unsigned int)ndr_err,
320 file_id_string(mem_ctx, &id)));
321 memcache_delete(NULL,
322 SHARE_MODE_LOCK_CACHE,
327 d = (struct share_mode_data *)ptr;
328 if (d->sequence_number != sequence_number) {
329 DBG_DEBUG("seq changed (cached %"PRIx64") (new %"PRIx64") "
333 file_id_string(mem_ctx, &id));
334 /* Cache out of date. Remove entry. */
335 memcache_delete(NULL,
336 SHARE_MODE_LOCK_CACHE,
341 /* Move onto mem_ctx. */
342 d = talloc_move(mem_ctx, &ptr);
345 * Now we own d, prevent the cache from freeing it
346 * when we delete the entry.
348 talloc_set_destructor(d, share_mode_data_nofree_destructor);
350 /* Remove from the cache. We own it now. */
351 memcache_delete(NULL,
352 SHARE_MODE_LOCK_CACHE,
355 /* And reset the destructor to none. */
356 talloc_set_destructor(d, NULL);
358 DBG_DEBUG("fetched entry for file %s seq %"PRIx64" key %s\n",
361 file_id_string(mem_ctx, &id));
366 /*******************************************************************
367 Get all share mode entries for a dev/inode pair.
368 ********************************************************************/
370 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
374 struct share_mode_data *d;
375 enum ndr_err_code ndr_err;
378 blob.data = dbuf.dptr;
379 blob.length = dbuf.dsize;
381 /* See if we already have a cached copy of this key. */
382 d = share_mode_memcache_fetch(mem_ctx, key, &blob);
387 d = talloc(mem_ctx, struct share_mode_data);
389 DEBUG(0, ("talloc failed\n"));
393 ndr_err = ndr_pull_struct_blob_all(
394 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
395 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
396 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
397 ndr_errstr(ndr_err));
401 if (DEBUGLEVEL >= 10) {
402 DEBUG(10, ("parse_share_modes:\n"));
403 NDR_PRINT_DEBUG(share_mode_data, d);
412 /*******************************************************************
413 If modified, store the share_mode_data back into the database.
414 ********************************************************************/
416 static NTSTATUS share_mode_data_store(struct share_mode_data *d)
419 enum ndr_err_code ndr_err;
423 DBG_DEBUG("not modified\n");
427 if (DEBUGLEVEL >= 10) {
429 NDR_PRINT_DEBUG(share_mode_data, d);
432 d->sequence_number += 1;
434 if (d->num_share_modes == 0) {
435 TALLOC_FREE(d->delete_tokens);
436 d->num_delete_tokens = 0;
439 DBG_DEBUG("Ignoring fresh empty record\n");
442 status = dbwrap_record_delete(d->record);
446 ndr_err = ndr_push_struct_blob(
447 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
448 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
449 DBG_DEBUG("ndr_push_share_mode_data failed: %s\n",
450 ndr_errstr(ndr_err));
451 return ndr_map_error2ntstatus(ndr_err);
454 status = dbwrap_record_store(
456 (TDB_DATA) { .dptr = blob.data, .dsize = blob.length },
458 TALLOC_FREE(blob.data);
460 if (!NT_STATUS_IS_OK(status)) {
461 DBG_DEBUG("dbwrap_record_store failed: %s\n",
468 /*******************************************************************
469 Allocate a new share_mode_data struct, mark it unmodified.
470 fresh is set to note that currently there is no database entry.
471 ********************************************************************/
473 static struct share_mode_data *fresh_share_mode_lock(
474 TALLOC_CTX *mem_ctx, const char *servicepath,
475 const struct smb_filename *smb_fname,
476 const struct timespec *old_write_time)
478 struct share_mode_data *d;
480 if ((servicepath == NULL) || (smb_fname == NULL) ||
481 (old_write_time == NULL)) {
485 d = talloc_zero(mem_ctx, struct share_mode_data);
489 /* New record - new sequence number. */
490 generate_random_buffer((uint8_t *)&d->sequence_number, 8);
492 d->base_name = talloc_strdup(d, smb_fname->base_name);
493 if (d->base_name == NULL) {
496 if (smb_fname->stream_name != NULL) {
497 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
498 if (d->stream_name == NULL) {
502 d->servicepath = talloc_strdup(d, servicepath);
503 if (d->servicepath == NULL) {
506 d->old_write_time = *old_write_time;
511 DEBUG(0, ("talloc failed\n"));
517 * We can only ever have one share mode locked. Use a static
518 * share_mode_data pointer that is shared by multiple nested
519 * share_mode_lock structures, explicitly refcounted.
521 static struct share_mode_data *static_share_mode_data = NULL;
522 static size_t static_share_mode_data_refcount = 0;
525 * db_record for the above. With dbwrap_do_locked we can get a
526 * db_record on the stack, which we can't TALLOC_FREE but which we
527 * need to share with a nested get_share_mode_lock call.
529 static struct db_record *static_share_mode_record = NULL;
530 static bool static_share_mode_record_talloced = false;
532 /*******************************************************************
533 Either fetch a share mode from the database, or allocate a fresh
534 one if the record doesn't exist.
535 ********************************************************************/
537 static NTSTATUS get_static_share_mode_data(
538 struct db_record *rec,
540 const char *servicepath,
541 const struct smb_filename *smb_fname,
542 const struct timespec *old_write_time)
544 struct share_mode_data *d;
545 TDB_DATA value = dbwrap_record_get_value(rec);
547 SMB_ASSERT(static_share_mode_data == NULL);
549 if (value.dptr == NULL) {
550 d = fresh_share_mode_lock(
551 lock_db, servicepath, smb_fname, old_write_time);
553 return NT_STATUS_NO_MEMORY;
556 TDB_DATA key = locking_key(&id);
557 d = parse_share_modes(lock_db, key, value);
559 return NT_STATUS_INTERNAL_DB_CORRUPTION;
566 static_share_mode_data = d;
571 /*******************************************************************
572 Get a share_mode_lock, Reference counted to allow nested calls.
573 ********************************************************************/
575 static int share_mode_lock_destructor(struct share_mode_lock *lck);
577 struct share_mode_lock *get_share_mode_lock(
580 const char *servicepath,
581 const struct smb_filename *smb_fname,
582 const struct timespec *old_write_time)
584 TDB_DATA key = locking_key(&id);
585 struct share_mode_lock *lck = NULL;
588 lck = talloc(mem_ctx, struct share_mode_lock);
590 DEBUG(1, ("talloc failed\n"));
594 if (static_share_mode_data != NULL) {
595 if (!file_id_equal(&static_share_mode_data->id, &id)) {
596 DEBUG(1, ("Can not lock two share modes "
597 "simultaneously\n"));
603 SMB_ASSERT(static_share_mode_data_refcount == 0);
605 if (static_share_mode_record == NULL) {
606 static_share_mode_record = dbwrap_fetch_locked(
607 lock_db, lock_db, key);
608 if (static_share_mode_record == NULL) {
609 DEBUG(3, ("Could not lock share entry\n"));
612 static_share_mode_record_talloced = true;
614 status = get_static_share_mode_data(
615 static_share_mode_record,
620 if (!NT_STATUS_IS_OK(status)) {
621 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
623 TALLOC_FREE(static_share_mode_record);
630 static_key = dbwrap_record_get_key(static_share_mode_record);
632 cmp = tdb_data_cmp(static_key, key);
634 DBG_WARNING("Can not lock two share modes "
639 status = get_static_share_mode_data(
640 static_share_mode_record,
645 if (!NT_STATUS_IS_OK(status)) {
646 DBG_WARNING("get_static_share_mode_data failed: %s\n",
653 static_share_mode_data_refcount += 1;
654 lck->data = static_share_mode_data;
656 talloc_set_destructor(lck, share_mode_lock_destructor);
664 static int share_mode_lock_destructor(struct share_mode_lock *lck)
668 SMB_ASSERT(static_share_mode_data_refcount > 0);
669 static_share_mode_data_refcount -= 1;
671 if (static_share_mode_data_refcount > 0) {
675 status = share_mode_data_store(static_share_mode_data);
676 if (!NT_STATUS_IS_OK(status)) {
677 DBG_ERR("share_mode_data_store failed: %s\n",
679 smb_panic("Could not store share mode data\n");
683 * Drop the locking.tdb lock before moving the share_mode_data
686 SMB_ASSERT(static_share_mode_data->record == static_share_mode_record);
687 static_share_mode_data->record = NULL;
689 if (static_share_mode_record_talloced) {
690 TALLOC_FREE(static_share_mode_record);
693 if (static_share_mode_data->num_share_modes != 0) {
695 * This is worth keeping. Without share modes,
696 * share_mode_data_store above has left nothing in the
699 share_mode_memcache_store(static_share_mode_data);
700 static_share_mode_data = NULL;
703 * The next opener of this file will find an empty
704 * locking.tdb record. Don't store the share_mode_data
705 * in the memcache, fresh_share_mode_lock() will
706 * generate a fresh seqnum anyway, obsoleting the
709 TALLOC_FREE(static_share_mode_data);
715 struct share_mode_do_locked_state {
716 void (*fn)(struct db_record *rec,
717 bool *modified_dependent,
722 static void share_mode_do_locked_fn(struct db_record *rec,
725 struct share_mode_do_locked_state *state = private_data;
726 bool modified_dependent = false;
727 bool reset_static_share_mode_record = false;
729 if (static_share_mode_record == NULL) {
730 static_share_mode_record = rec;
731 static_share_mode_record_talloced = false;
732 reset_static_share_mode_record = true;
734 SMB_ASSERT(static_share_mode_record == rec);
737 state->fn(rec, &modified_dependent, state->private_data);
739 if (modified_dependent) {
740 dbwrap_watched_wakeup(rec);
743 if (reset_static_share_mode_record) {
744 static_share_mode_record = NULL;
748 NTSTATUS share_mode_do_locked(
750 void (*fn)(struct db_record *rec,
751 bool *modified_dependent,
755 TDB_DATA key = locking_key(&id);
756 size_t refcount = static_share_mode_data_refcount;
758 if (static_share_mode_record != NULL) {
759 bool modified_dependent = false;
763 static_key = dbwrap_record_get_key(static_share_mode_record);
765 cmp = tdb_data_cmp(static_key, key);
767 DBG_WARNING("Can not lock two share modes "
769 return NT_STATUS_INVALID_LOCK_SEQUENCE;
772 fn(static_share_mode_record,
776 if (modified_dependent) {
777 dbwrap_watched_wakeup(static_share_mode_record);
780 struct share_mode_do_locked_state state = {
781 .fn = fn, .private_data = private_data,
785 status = dbwrap_do_locked(
786 lock_db, key, share_mode_do_locked_fn, &state);
787 if (!NT_STATUS_IS_OK(status)) {
788 DBG_WARNING("dbwrap_do_locked failed: %s\n",
794 SMB_ASSERT(refcount == static_share_mode_data_refcount);
799 static void share_mode_wakeup_waiters_fn(struct db_record *rec,
800 bool *modified_dependent,
803 *modified_dependent = true;
806 NTSTATUS share_mode_wakeup_waiters(struct file_id id)
808 return share_mode_do_locked(id, share_mode_wakeup_waiters_fn, NULL);
811 struct fetch_share_mode_unlocked_state {
813 struct share_mode_lock *lck;
816 static void fetch_share_mode_unlocked_parser(
817 TDB_DATA key, TDB_DATA data, void *private_data)
819 struct fetch_share_mode_unlocked_state *state = private_data;
821 if (data.dsize == 0) {
822 /* Likely a ctdb tombstone record, ignore it */
826 state->lck = talloc(state->mem_ctx, struct share_mode_lock);
827 if (state->lck == NULL) {
828 DEBUG(0, ("talloc failed\n"));
832 state->lck->data = parse_share_modes(state->lck, key, data);
835 /*******************************************************************
836 Get a share_mode_lock without locking the database or reference
837 counting. Used by smbstatus to display existing share modes.
838 ********************************************************************/
840 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
843 struct fetch_share_mode_unlocked_state state = { .mem_ctx = mem_ctx };
844 TDB_DATA key = locking_key(&id);
847 status = dbwrap_parse_record(
848 lock_db, key, fetch_share_mode_unlocked_parser, &state);
849 if (!NT_STATUS_IS_OK(status)) {
855 static void fetch_share_mode_done(struct tevent_req *subreq);
857 struct fetch_share_mode_state {
860 struct fetch_share_mode_unlocked_state parser_state;
861 enum dbwrap_req_state req_state;
865 * @brief Get a share_mode_lock without locking or refcounting
867 * This can be used in a clustered Samba environment where the async dbwrap
868 * request is sent over a socket to the local ctdbd. If the send queue is full
869 * and the caller was issuing multiple async dbwrap requests in a loop, the
870 * caller knows it's probably time to stop sending requests for now and try
873 * @param[in] mem_ctx The talloc memory context to use.
875 * @param[in] ev The event context to work on.
877 * @param[in] id The file id for the locking.tdb key
879 * @param[out] queued This boolean out parameter tells the caller whether the
880 * async request is blocked in a full send queue:
882 * false := request is dispatched
884 * true := send queue is full, request waiting to be
887 * @return The new async request, NULL on error.
889 struct tevent_req *fetch_share_mode_send(TALLOC_CTX *mem_ctx,
890 struct tevent_context *ev,
894 struct tevent_req *req = NULL;
895 struct fetch_share_mode_state *state = NULL;
896 struct tevent_req *subreq = NULL;
900 req = tevent_req_create(mem_ctx, &state,
901 struct fetch_share_mode_state);
907 state->key = locking_key(&state->id);
908 state->parser_state.mem_ctx = state;
910 subreq = dbwrap_parse_record_send(state,
914 fetch_share_mode_unlocked_parser,
915 &state->parser_state,
917 if (tevent_req_nomem(subreq, req)) {
918 return tevent_req_post(req, ev);
920 tevent_req_set_callback(subreq, fetch_share_mode_done, req);
922 if (state->req_state < DBWRAP_REQ_DISPATCHED) {
928 static void fetch_share_mode_done(struct tevent_req *subreq)
930 struct tevent_req *req = tevent_req_callback_data(
931 subreq, struct tevent_req);
934 status = dbwrap_parse_record_recv(subreq);
936 if (tevent_req_nterror(req, status)) {
940 tevent_req_done(req);
944 NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
946 struct share_mode_lock **_lck)
948 struct fetch_share_mode_state *state = tevent_req_data(
949 req, struct fetch_share_mode_state);
950 struct share_mode_lock *lck = NULL;
954 if (tevent_req_is_nterror(req, &status)) {
955 tevent_req_received(req);
959 if (state->parser_state.lck->data == NULL) {
960 tevent_req_received(req);
961 return NT_STATUS_NOT_FOUND;
964 lck = talloc_move(mem_ctx, &state->parser_state.lck);
966 if (DEBUGLEVEL >= 10) {
967 DBG_DEBUG("share_mode_data:\n");
968 NDR_PRINT_DEBUG(share_mode_data, lck->data);
972 tevent_req_received(req);
976 struct share_mode_forall_state {
977 int (*fn)(struct file_id fid, const struct share_mode_data *data,
982 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
984 struct share_mode_forall_state *state =
985 (struct share_mode_forall_state *)_state;
989 enum ndr_err_code ndr_err;
990 struct share_mode_data *d;
994 key = dbwrap_record_get_key(rec);
995 value = dbwrap_record_get_value(rec);
997 /* Ensure this is a locking_key record. */
998 if (key.dsize != sizeof(fid)) {
1001 memcpy(&fid, key.dptr, sizeof(fid));
1003 d = talloc(talloc_tos(), struct share_mode_data);
1008 blob.data = value.dptr;
1009 blob.length = value.dsize;
1011 ndr_err = ndr_pull_struct_blob_all(
1012 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
1013 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1014 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
1018 if (DEBUGLEVEL > 10) {
1019 DEBUG(11, ("parse_share_modes:\n"));
1020 NDR_PRINT_DEBUG(share_mode_data, d);
1023 ret = state->fn(fid, d, state->private_data);
1029 int share_mode_forall(int (*fn)(struct file_id fid,
1030 const struct share_mode_data *data,
1031 void *private_data),
1034 struct share_mode_forall_state state = {
1036 .private_data = private_data
1041 if (lock_db == NULL) {
1045 status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
1047 if (!NT_STATUS_IS_OK(status)) {
1054 struct share_entry_forall_state {
1056 const struct share_mode_data *data;
1057 int (*fn)(struct file_id fid,
1058 const struct share_mode_data *data,
1059 const struct share_mode_entry *entry,
1060 void *private_data);
1065 static bool share_entry_traverse_walker(
1066 struct share_mode_entry *e,
1070 struct share_entry_forall_state *state = private_data;
1072 state->ret = state->fn(
1073 state->fid, state->data, e, state->private_data);
1074 return (state->ret != 0);
1077 static int share_entry_traverse_fn(struct file_id fid,
1078 const struct share_mode_data *data,
1081 struct share_entry_forall_state *state = private_data;
1082 struct share_mode_lock lck = {
1083 .data = discard_const_p(struct share_mode_data, data)
1090 ok = share_mode_forall_entries(
1091 &lck, share_entry_traverse_walker, state);
1093 DBG_DEBUG("share_mode_forall_entries failed\n");
1100 /*******************************************************************
1101 Call the specified function on each entry under management by the
1103 ********************************************************************/
1105 int share_entry_forall(int (*fn)(struct file_id fid,
1106 const struct share_mode_data *data,
1107 const struct share_mode_entry *entry,
1108 void *private_data),
1111 struct share_entry_forall_state state = {
1112 .fn = fn, .private_data = private_data };
1114 return share_mode_forall(share_entry_traverse_fn, &state);
1117 struct cleanup_disconnected_state {
1118 struct share_mode_lock *lck;
1119 uint64_t open_persistent_id;
1120 bool found_connected;
1123 static bool cleanup_disconnected_lease(struct share_mode_entry *e,
1126 struct cleanup_disconnected_state *state = private_data;
1129 status = leases_db_del(
1130 &e->client_guid, &e->lease_key, &state->lck->data->id);
1132 if (!NT_STATUS_IS_OK(status)) {
1133 DBG_DEBUG("leases_db_del failed: %s\n",
1140 static bool share_mode_cleanup_disconnected_fn(
1141 struct share_mode_entry *e,
1145 struct cleanup_disconnected_state *state = private_data;
1146 struct share_mode_data *d = state->lck->data;
1149 disconnected = server_id_is_disconnected(&e->pid);
1150 if (!disconnected) {
1151 struct file_id_buf tmp1;
1152 struct server_id_buf tmp2;
1153 DBG_INFO("file (file-id='%s', servicepath='%s', "
1154 "base_name='%s%s%s') "
1155 "is used by server %s ==> do not cleanup\n",
1156 file_id_str_buf(d->id, &tmp1),
1159 (d->stream_name == NULL)
1160 ? "" : "', stream_name='",
1161 (d->stream_name == NULL)
1162 ? "" : d->stream_name,
1163 server_id_str_buf(e->pid, &tmp2));
1164 state->found_connected = true;
1168 if (state->open_persistent_id != e->share_file_id) {
1169 struct file_id_buf tmp;
1170 DBG_INFO("entry for file "
1171 "(file-id='%s', servicepath='%s', "
1172 "base_name='%s%s%s') "
1173 "has share_file_id %"PRIu64" but expected "
1174 "%"PRIu64"==> do not cleanup\n",
1175 file_id_str_buf(d->id, &tmp),
1178 (d->stream_name == NULL)
1179 ? "" : "', stream_name='",
1180 (d->stream_name == NULL)
1181 ? "" : d->stream_name,
1183 state->open_persistent_id);
1184 state->found_connected = true;
1191 bool share_mode_cleanup_disconnected(struct file_id fid,
1192 uint64_t open_persistent_id)
1194 struct cleanup_disconnected_state state = {
1195 .open_persistent_id = open_persistent_id
1197 struct share_mode_data *data;
1199 TALLOC_CTX *frame = talloc_stackframe();
1202 state.lck = get_existing_share_mode_lock(frame, fid);
1203 if (state.lck == NULL) {
1204 DEBUG(5, ("share_mode_cleanup_disconnected: "
1205 "Could not fetch share mode entry for %s\n",
1206 file_id_string(frame, &fid)));
1209 data = state.lck->data;
1211 ok = share_mode_forall_entries(
1212 state.lck, share_mode_cleanup_disconnected_fn, &state);
1214 DBG_DEBUG("share_mode_forall_entries failed\n");
1217 if (state.found_connected) {
1218 DBG_DEBUG("Found connected entry\n");
1222 ok = share_mode_forall_leases(
1223 state.lck, cleanup_disconnected_lease, &state);
1225 DBG_DEBUG("failed to clean up leases associated "
1226 "with file (file-id='%s', servicepath='%s', "
1227 "base_name='%s%s%s') and open_persistent_id %"PRIu64" "
1228 "==> do not cleanup\n",
1229 file_id_string(frame, &fid),
1232 (data->stream_name == NULL)
1233 ? "" : "', stream_name='",
1234 (data->stream_name == NULL)
1235 ? "" : data->stream_name,
1236 open_persistent_id);
1239 ok = brl_cleanup_disconnected(fid, open_persistent_id);
1241 DBG_DEBUG("failed to clean up byte range locks associated "
1242 "with file (file-id='%s', servicepath='%s', "
1243 "base_name='%s%s%s') and open_persistent_id %"PRIu64" "
1244 "==> do not cleanup\n",
1245 file_id_string(frame, &fid),
1248 (data->stream_name == NULL)
1249 ? "" : "', stream_name='",
1250 (data->stream_name == NULL)
1251 ? "" : data->stream_name,
1252 open_persistent_id);
1256 DBG_DEBUG("cleaning up %u entries for file "
1257 "(file-id='%s', servicepath='%s', "
1258 "base_name='%s%s%s') "
1259 "from open_persistent_id %"PRIu64"\n",
1260 data->num_share_modes,
1261 file_id_string(frame, &fid),
1264 (data->stream_name == NULL)
1265 ? "" : "', stream_name='",
1266 (data->stream_name == NULL)
1267 ? "" : data->stream_name,
1268 open_persistent_id);
1270 data->num_share_modes = 0;
1271 data->modified = true;
1279 static int share_mode_entry_cmp(
1280 struct server_id pid1,
1281 uint64_t share_file_id1,
1282 struct server_id pid2,
1283 uint64_t share_file_id2)
1287 cmp = server_id_cmp(&pid1, &pid2);
1291 if (share_file_id1 != share_file_id2) {
1292 return (share_file_id1 < share_file_id2) ? -1 : 1;
1298 * 132 is the sizeof an ndr-encoded struct share_mode_entry_buf.
1299 * Reading/writing entries will immediately error out if this
1300 * size differs (push/pull is done without allocs).
1303 struct share_mode_entry_buf {
1306 #define SHARE_MODE_ENTRY_SIZE (sizeof(struct share_mode_entry_buf))
1308 static bool share_mode_entry_put(
1309 const struct share_mode_entry *e,
1310 struct share_mode_entry_buf *dst)
1312 DATA_BLOB blob = { .data = dst->buf, .length = sizeof(dst->buf) };
1313 enum ndr_err_code ndr_err;
1315 if (DEBUGLEVEL>=10) {
1316 DBG_DEBUG("share_mode_entry:\n");
1317 NDR_PRINT_DEBUG(share_mode_entry, discard_const_p(void, e));
1320 ndr_err = ndr_push_struct_into_fixed_blob(
1323 (ndr_push_flags_fn_t)ndr_push_share_mode_entry);
1324 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1325 DBG_WARNING("ndr_push_share_mode_entry failed: %s\n",
1326 ndr_errstr(ndr_err));
1333 static bool share_mode_entry_get(
1334 DATA_BLOB blob, struct share_mode_entry *e)
1336 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
1338 ndr_err = ndr_pull_struct_blob_all_noalloc(
1339 &blob, e, (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
1340 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1341 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
1347 static size_t share_mode_entry_find(
1349 size_t num_share_modes,
1350 struct server_id pid,
1351 uint64_t share_file_id,
1352 struct share_mode_entry *e,
1355 ssize_t left, right, middle;
1357 if (num_share_modes == 0) {
1363 right = (num_share_modes-1);
1365 while (left <= right) {
1370 middle = left + ((right - left) / 2);
1372 DBG_DEBUG("left=%zu, right=%zu, middle=%zu\n",
1377 blob = (DATA_BLOB) {
1378 .data = data + middle * SHARE_MODE_ENTRY_SIZE,
1379 .length = SHARE_MODE_ENTRY_SIZE,
1382 DBG_DEBUG("blob.data=%p, blob.length=%zu\n",
1386 ok = share_mode_entry_get(blob, e);
1388 DBG_DEBUG("share_mode_entry_get failed\n");
1392 cmp = share_mode_entry_cmp(
1393 e->pid, e->share_file_id, pid, share_file_id);
1410 struct set_share_mode_state {
1411 struct share_mode_entry e;
1412 uint32_t num_share_modes;
1416 static void set_share_mode_fn(struct db_record *rec, void *private_data)
1418 struct set_share_mode_state *state = private_data;
1419 TDB_DATA data = dbwrap_record_get_value(rec);
1420 size_t idx, num_share_modes;
1421 struct share_mode_entry tmp;
1422 struct share_mode_entry_buf buf;
1426 size_t num_dbufs = 0;
1428 if ((data.dsize % SHARE_MODE_ENTRY_SIZE) != 0) {
1429 DBG_WARNING("Got invalid record size %zu\n", data.dsize);
1430 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1433 num_share_modes = data.dsize / SHARE_MODE_ENTRY_SIZE;
1435 ok = share_mode_entry_put(&state->e, &buf);
1437 DBG_DEBUG("share_mode_entry_put failed\n");
1438 state->status = NT_STATUS_INTERNAL_ERROR;
1442 DBG_DEBUG("num_share_modes=%zu\n", num_share_modes);
1444 idx = share_mode_entry_find(
1448 state->e.share_file_id,
1452 DBG_WARNING("Found duplicate share mode\n");
1453 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1457 DBG_DEBUG("idx=%zu, found=%d\n", idx, (int)found);
1460 dbufs[num_dbufs] = (TDB_DATA) {
1462 .dsize = idx * SHARE_MODE_ENTRY_SIZE,
1467 dbufs[num_dbufs] = (TDB_DATA) {
1468 .dptr = buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
1472 if (idx < num_share_modes) {
1473 dbufs[num_dbufs] = (TDB_DATA) {
1474 .dptr = data.dptr + idx * SHARE_MODE_ENTRY_SIZE,
1475 .dsize = (num_share_modes-idx) * SHARE_MODE_ENTRY_SIZE,
1482 for (i=0; i<num_dbufs; i++) {
1483 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
1490 state->num_share_modes = num_share_modes+1;
1491 state->status = dbwrap_record_storev(rec, dbufs, num_dbufs, 0);
1494 bool set_share_mode(struct share_mode_lock *lck,
1495 struct files_struct *fsp,
1499 uint32_t share_access,
1500 uint32_t access_mask)
1502 struct share_mode_data *d = lck->data;
1503 struct set_share_mode_state state = {
1504 .status = NT_STATUS_OK,
1505 .e.pid = messaging_server_id(fsp->conn->sconn->msg_ctx),
1506 .e.share_access = share_access,
1507 .e.private_options = fsp->fh->private_options,
1508 .e.access_mask = access_mask,
1510 .e.op_type = op_type,
1511 .e.time.tv_sec = fsp->open_time.tv_sec,
1512 .e.time.tv_usec = fsp->open_time.tv_usec,
1513 .e.share_file_id = fsp->fh->gen_id,
1514 .e.uid = (uint32_t)uid,
1515 .e.flags = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
1516 SHARE_MODE_FLAG_POSIX_OPEN : 0,
1517 .e.name_hash = fsp->name_hash,
1521 if (op_type == LEASE_OPLOCK) {
1522 const struct GUID *client_guid = fsp_client_guid(fsp);
1523 state.e.client_guid = *client_guid;
1524 state.e.lease_key = fsp->lease->lease.lease_key;
1527 status = dbwrap_do_locked(
1529 locking_key(&d->id),
1532 if (!NT_STATUS_IS_OK(status)) {
1533 DBG_WARNING("dbwrap_do_locked failed: %s\n",
1537 if (!NT_STATUS_IS_OK(state.status)) {
1538 DBG_WARNING("set_share_mode_fn failed: %s\n",
1539 nt_errstr(state.status));
1543 d->num_share_modes = state.num_share_modes;
1549 struct share_mode_forall_entries_state {
1550 struct share_mode_lock *lck;
1551 bool (*fn)(struct share_mode_entry *e,
1553 void *private_data);
1555 size_t num_share_modes;
1559 static bool share_mode_for_one_entry(
1560 struct share_mode_forall_entries_state *state,
1562 size_t *num_share_modes,
1567 .data = data.dptr + (*i) * SHARE_MODE_ENTRY_SIZE,
1568 .length = SHARE_MODE_ENTRY_SIZE,
1570 struct share_mode_entry e = {.pid.pid=0};
1571 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
1572 bool modified = false;
1574 struct server_id e_pid;
1575 uint64_t e_share_file_id;
1577 ndr_err = ndr_pull_struct_blob_all_noalloc(
1580 (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
1581 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1582 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
1586 if (DEBUGLEVEL >= 10) {
1587 DBG_DEBUG("entry[%zu]:\n", *i);
1588 NDR_PRINT_DEBUG(share_mode_entry, &e);
1592 e_share_file_id = e.share_file_id;
1594 stop = state->fn(&e, &modified, state->private_data);
1596 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
1602 if (DEBUGLEVEL>=10) {
1603 DBG_DEBUG("share_mode_entry:\n");
1604 NDR_PRINT_DEBUG(share_mode_entry, &e);
1608 * Make sure sorting order is kept intact
1610 SMB_ASSERT(server_id_equal(&e_pid, &e.pid));
1611 SMB_ASSERT(e_share_file_id == e.share_file_id);
1613 ndr_err = ndr_push_struct_into_fixed_blob(
1616 (ndr_push_flags_fn_t)
1617 ndr_push_share_mode_entry);
1618 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1619 DBG_WARNING("ndr_push_share_mode_entry "
1621 ndr_errstr(ndr_err));
1623 * Not much we can do, just ignore it
1632 if (*i < *num_share_modes) {
1634 blob.data + SHARE_MODE_ENTRY_SIZE,
1635 (*num_share_modes - *i - 1) *
1636 SHARE_MODE_ENTRY_SIZE);
1638 *num_share_modes -= 1;
1651 static void share_mode_forall_entries_fn(
1652 struct db_record *rec, void *private_data)
1654 struct share_mode_forall_entries_state *state = private_data;
1655 struct share_mode_data *d = state->lck->data;
1656 struct TDB_DATA data = dbwrap_record_get_value(rec);
1657 size_t num_share_modes;
1658 bool writeback = false;
1663 if ((data.dsize % SHARE_MODE_ENTRY_SIZE) != 0) {
1664 DBG_WARNING("Invalid data size %zu\n", data.dsize);
1667 num_share_modes = data.dsize / SHARE_MODE_ENTRY_SIZE;
1669 DBG_DEBUG("num_share_modes=%zu\n", num_share_modes);
1672 while (i<num_share_modes) {
1673 stop = share_mode_for_one_entry(
1674 state, &i, &num_share_modes, data, &writeback);
1680 DBG_DEBUG("num_share_modes=%zu, writeback=%d\n",
1689 if (num_share_modes != d->num_share_modes) {
1690 d->num_share_modes = num_share_modes;
1694 if (num_share_modes == 0) {
1695 status = dbwrap_record_delete(rec);
1699 .dsize = num_share_modes * SHARE_MODE_ENTRY_SIZE,
1701 status = dbwrap_record_store(rec, value, 0);
1704 if (!NT_STATUS_IS_OK(status)) {
1705 DBG_DEBUG("Storing record with %zu entries failed: %s\n",
1715 bool share_mode_forall_entries(
1716 struct share_mode_lock *lck,
1717 bool (*fn)(struct share_mode_entry *e,
1719 void *private_data),
1722 struct share_mode_forall_entries_state state = {
1725 .private_data = private_data,
1729 status = dbwrap_do_locked(
1731 locking_key(&lck->data->id),
1732 share_mode_forall_entries_fn,
1734 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1735 status = NT_STATUS_OK;
1738 if (!NT_STATUS_IS_OK(status)) {
1739 DBG_DEBUG("dbwrap_parse_record returned %s\n",
1747 struct share_mode_entry_do_state {
1748 struct server_id pid;
1749 uint64_t share_file_id;
1750 void (*fn)(struct share_mode_entry *e,
1752 void *private_data);
1754 size_t num_share_modes;
1758 static void share_mode_entry_do_fn(struct db_record *rec, void *private_data)
1760 struct share_mode_entry_do_state *state = private_data;
1761 struct TDB_DATA data = dbwrap_record_get_value(rec);
1765 struct share_mode_entry e;
1766 struct share_mode_entry_buf buf;
1768 size_t num_dbufs = 0;
1770 if ((data.dsize % SHARE_MODE_ENTRY_SIZE) != 0) {
1771 DBG_WARNING("Invalid data size %zu\n", data.dsize);
1772 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1775 state->num_share_modes = data.dsize / SHARE_MODE_ENTRY_SIZE;
1777 DBG_DEBUG("state->num_share_modes=%zu\n", state->num_share_modes);
1779 idx = share_mode_entry_find(
1781 state->num_share_modes,
1783 state->share_file_id,
1787 DBG_WARNING("Did not find share mode entry for %"PRIu64"\n",
1788 state->share_file_id);
1789 state->status = NT_STATUS_NOT_FOUND;
1793 state->fn(&e, &modified, state->private_data);
1795 if (!e.stale && !modified) {
1796 state->status = NT_STATUS_OK;
1801 dbufs[num_dbufs] = (TDB_DATA) {
1803 .dsize = idx * SHARE_MODE_ENTRY_SIZE,
1809 bool ok = share_mode_entry_put(&e, &buf);
1811 DBG_DEBUG("share_mode_entry_put failed\n");
1812 state->status = NT_STATUS_INTERNAL_ERROR;
1816 dbufs[num_dbufs] = (TDB_DATA) {
1817 .dptr = buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
1824 if (idx < state->num_share_modes) {
1825 size_t behind = state->num_share_modes - idx;
1826 dbufs[num_dbufs] = (TDB_DATA) {
1827 .dptr = data.dptr + idx * SHARE_MODE_ENTRY_SIZE,
1828 .dsize = behind * SHARE_MODE_ENTRY_SIZE,
1834 state->num_share_modes -= 1;
1837 state->status = dbwrap_record_storev(rec, dbufs, num_dbufs, 0);
1838 if (!NT_STATUS_IS_OK(state->status)) {
1839 DBG_DEBUG("dbwrap_record_storev failed: %s\n",
1840 nt_errstr(state->status));
1845 static bool share_mode_entry_do(
1846 struct share_mode_lock *lck,
1847 struct server_id pid,
1848 uint64_t share_file_id,
1849 void (*fn)(struct share_mode_entry *e,
1851 void *private_data),
1854 struct share_mode_data *d = lck->data;
1855 struct share_mode_entry_do_state state = {
1857 .share_file_id = share_file_id,
1859 .private_data = private_data,
1863 status = dbwrap_do_locked(
1865 locking_key(&d->id),
1866 share_mode_entry_do_fn,
1868 if (!NT_STATUS_IS_OK(status)) {
1869 DBG_DEBUG("share_mode_forall_entries failed: %s\n",
1873 if (!NT_STATUS_IS_OK(state.status)) {
1874 DBG_DEBUG("share_mode_entry_do_fn failed: %s\n",
1879 if (d->num_share_modes != state.num_share_modes) {
1880 d->num_share_modes = state.num_share_modes;
1887 struct del_share_mode_state {
1891 static void del_share_mode_fn(
1892 struct share_mode_entry *e,
1896 struct del_share_mode_state *state = private_data;
1901 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1903 struct del_share_mode_state state = { .ok = false };
1906 ok = share_mode_entry_do(
1908 messaging_server_id(fsp->conn->sconn->msg_ctx),
1913 DBG_DEBUG("share_mode_entry_do failed\n");
1917 DBG_DEBUG("del_share_mode_fn failed\n");
1923 struct remove_share_oplock_state {
1927 static void remove_share_oplock_fn(
1928 struct share_mode_entry *e,
1932 struct remove_share_oplock_state *state = private_data;
1934 e->op_type = NO_OPLOCK;
1939 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1941 struct remove_share_oplock_state state = { .ok = false };
1944 ok = share_mode_entry_do(
1946 messaging_server_id(fsp->conn->sconn->msg_ctx),
1948 remove_share_oplock_fn,
1951 DBG_DEBUG("share_mode_entry_do failed\n");
1955 DBG_DEBUG("remove_share_oplock_fn failed\n");
1959 if (fsp->oplock_type == LEASE_OPLOCK) {
1960 remove_lease_if_stale(
1962 fsp_client_guid(fsp),
1963 &fsp->lease->lease.lease_key);
1966 lck->data->modified = true; /* signal watchers */
1971 struct downgrade_share_oplock_state {
1975 static void downgrade_share_oplock_fn(
1976 struct share_mode_entry *e,
1980 struct downgrade_share_oplock_state *state = private_data;
1982 e->op_type = LEVEL_II_OPLOCK;
1987 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1989 struct downgrade_share_oplock_state state = { .ok = false };
1992 ok = share_mode_entry_do(
1994 messaging_server_id(fsp->conn->sconn->msg_ctx),
1996 downgrade_share_oplock_fn,
1999 DBG_DEBUG("share_mode_entry_do failed\n");
2003 DBG_DEBUG("downgrade_share_oplock_fn failed\n");
2007 lck->data->flags |= SHARE_MODE_LEASE_READ;
2008 lck->data->modified = true;
2013 struct mark_share_mode_disconnected_state {
2014 uint64_t open_persistent_id;
2018 static void mark_share_mode_disconnected_fn(
2019 struct share_mode_entry *e,
2023 struct mark_share_mode_disconnected_state *state = private_data;
2024 server_id_set_disconnected(&e->pid);
2025 e->share_file_id = state->open_persistent_id;
2030 bool mark_share_mode_disconnected(struct share_mode_lock *lck,
2031 struct files_struct *fsp)
2033 struct mark_share_mode_disconnected_state state;
2036 if (lck->data->num_share_modes != 1) {
2040 if (fsp->op == NULL) {
2043 if (!fsp->op->global->durable) {
2047 state = (struct mark_share_mode_disconnected_state) {
2048 .open_persistent_id = fsp->op->global->open_persistent_id,
2051 ok = share_mode_entry_do(
2053 messaging_server_id(fsp->conn->sconn->msg_ctx),
2055 mark_share_mode_disconnected_fn,
2058 DBG_DEBUG("share_mode_entry_do failed\n");
2062 DBG_DEBUG("mark_share_mode_disconnected_fn failed\n");
2066 lck->data->modified = true;
2070 static void reset_share_mode_entry_del_fn(
2071 struct share_mode_entry *e,
2075 struct set_share_mode_state *state = private_data;
2079 state->status = NT_STATUS_OK;
2082 bool reset_share_mode_entry(
2083 struct share_mode_lock *lck,
2084 struct server_id old_pid,
2085 uint64_t old_share_file_id,
2086 struct server_id new_pid,
2088 uint64_t new_share_file_id)
2090 struct share_mode_data *d = lck->data;
2091 struct set_share_mode_state state = {
2092 .status = NT_STATUS_INTERNAL_ERROR,
2097 ok = share_mode_entry_do(
2101 reset_share_mode_entry_del_fn,
2104 DBG_DEBUG("share_mode_entry_do failed\n");
2107 if (!NT_STATUS_IS_OK(state.status)) {
2108 DBG_DEBUG("reset_share_mode_entry_del_fn failed: %s\n",
2109 nt_errstr(state.status));
2113 state.status = NT_STATUS_INTERNAL_ERROR;
2114 state.e.pid = new_pid;
2115 state.e.op_mid = new_mid;
2116 state.e.share_file_id = new_share_file_id;
2118 status = dbwrap_do_locked(
2120 locking_key(&d->id),
2123 if (!NT_STATUS_IS_OK(status)) {
2124 DBG_WARNING("dbwrap_do_locked failed: %s\n",
2128 if (!NT_STATUS_IS_OK(state.status)) {
2129 DBG_WARNING("set_share_mode_fn failed: %s\n",
2130 nt_errstr(state.status));
2134 d->num_share_modes = state.num_share_modes;