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);
180 struct file_id_buf idbuf;
182 DBG_DEBUG("stored entry for file %s seq %"PRIx64" key %s\n",
185 file_id_str_buf(d->id, &idbuf));
187 /* Ensure everything stored in the cache is pristine. */
192 * Ensure the memory going into the cache
193 * doesn't have a destructor so it can be
194 * cleanly evicted by the memcache LRU
197 talloc_set_destructor(d, NULL);
199 /* Cache will own d after this call. */
200 memcache_add_talloc(NULL,
201 SHARE_MODE_LOCK_CACHE,
207 * NB. We use ndr_pull_hyper on a stack-created
208 * struct ndr_pull with no talloc allowed, as we
209 * need this to be really fast as an ndr-peek into
210 * the first 10 bytes of the blob.
213 static enum ndr_err_code get_share_mode_blob_header(
214 DATA_BLOB *blob, uint64_t *pseq, uint16_t *pflags)
216 struct ndr_pull ndr = {.data = blob->data, .data_size = blob->length};
217 NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pseq));
218 NDR_CHECK(ndr_pull_uint16(&ndr, NDR_SCALARS, pflags));
219 return NDR_ERR_SUCCESS;
222 struct fsp_update_share_mode_flags_state {
223 enum ndr_err_code ndr_err;
224 uint16_t share_mode_flags;
227 static void fsp_update_share_mode_flags_fn(
228 TDB_DATA value, bool *modified_dependent, void *private_data)
230 struct fsp_update_share_mode_flags_state *state = private_data;
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;
296 struct file_id_buf idbuf;
299 /* Ensure this is a locking_key record. */
300 if (id_key.dsize != sizeof(id)) {
304 memcpy(&id, id_key.dptr, id_key.dsize);
305 key = memcache_key(&id);
307 ptr = memcache_lookup_talloc(NULL,
308 SHARE_MODE_LOCK_CACHE,
311 DBG_DEBUG("failed to find entry for key %s\n",
312 file_id_str_buf(id, &idbuf));
315 /* sequence number key is at start of blob. */
316 ndr_err = get_share_mode_blob_header(blob, &sequence_number, &flags);
317 if (ndr_err != NDR_ERR_SUCCESS) {
318 /* Bad blob. Remove entry. */
319 DBG_DEBUG("bad blob %u key %s\n",
320 (unsigned int)ndr_err,
321 file_id_str_buf(id, &idbuf));
322 memcache_delete(NULL,
323 SHARE_MODE_LOCK_CACHE,
328 d = (struct share_mode_data *)ptr;
329 if (d->sequence_number != sequence_number) {
330 DBG_DEBUG("seq changed (cached %"PRIx64") (new %"PRIx64") "
334 file_id_str_buf(id, &idbuf));
335 /* Cache out of date. Remove entry. */
336 memcache_delete(NULL,
337 SHARE_MODE_LOCK_CACHE,
342 /* Move onto mem_ctx. */
343 d = talloc_move(mem_ctx, &ptr);
346 * Now we own d, prevent the cache from freeing it
347 * when we delete the entry.
349 talloc_set_destructor(d, share_mode_data_nofree_destructor);
351 /* Remove from the cache. We own it now. */
352 memcache_delete(NULL,
353 SHARE_MODE_LOCK_CACHE,
356 /* And reset the destructor to none. */
357 talloc_set_destructor(d, NULL);
359 DBG_DEBUG("fetched entry for file %s seq %"PRIx64" key %s\n",
362 file_id_str_buf(id, &idbuf));
367 /*******************************************************************
368 Get all share mode entries for a dev/inode pair.
369 ********************************************************************/
371 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
375 struct share_mode_data *d;
376 enum ndr_err_code ndr_err;
379 blob.data = dbuf.dptr;
380 blob.length = dbuf.dsize;
382 /* See if we already have a cached copy of this key. */
383 d = share_mode_memcache_fetch(mem_ctx, key, &blob);
388 d = talloc(mem_ctx, struct share_mode_data);
390 DEBUG(0, ("talloc failed\n"));
394 ndr_err = ndr_pull_struct_blob_all(
395 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
396 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
397 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
398 ndr_errstr(ndr_err));
402 if (DEBUGLEVEL >= 10) {
403 DEBUG(10, ("parse_share_modes:\n"));
404 NDR_PRINT_DEBUG(share_mode_data, d);
408 * We have a non-zero locking.tdb record that was correctly
409 * parsed. This means a share_entries.tdb entry exists,
410 * otherwise we'd have paniced before in
411 * share_mode_data_store()
413 d->have_share_modes = true;
421 /*******************************************************************
422 If modified, store the share_mode_data back into the database.
423 ********************************************************************/
425 static NTSTATUS share_mode_data_store(
426 struct share_mode_data *d, struct db_record *rec)
429 enum ndr_err_code ndr_err;
433 DBG_DEBUG("not modified\n");
437 if (DEBUGLEVEL >= 10) {
439 NDR_PRINT_DEBUG(share_mode_data, d);
442 d->sequence_number += 1;
444 if (!d->have_share_modes) {
445 TDB_DATA key = dbwrap_record_get_key(rec);
446 bool share_entries_exist;
447 share_entries_exist = dbwrap_exists(share_entries_db, key);
448 SMB_ASSERT(!share_entries_exist);
450 TALLOC_FREE(d->delete_tokens);
451 d->num_delete_tokens = 0;
454 DBG_DEBUG("Ignoring fresh empty record\n");
457 status = dbwrap_record_delete(rec);
461 ndr_err = ndr_push_struct_blob(
462 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
463 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
464 DBG_DEBUG("ndr_push_share_mode_data failed: %s\n",
465 ndr_errstr(ndr_err));
466 return ndr_map_error2ntstatus(ndr_err);
469 status = dbwrap_record_store(
471 (TDB_DATA) { .dptr = blob.data, .dsize = blob.length },
473 TALLOC_FREE(blob.data);
475 if (!NT_STATUS_IS_OK(status)) {
476 DBG_DEBUG("dbwrap_record_store failed: %s\n",
483 /*******************************************************************
484 Allocate a new share_mode_data struct, mark it unmodified.
485 fresh is set to note that currently there is no database entry.
486 ********************************************************************/
488 static struct share_mode_data *fresh_share_mode_lock(
489 TALLOC_CTX *mem_ctx, const char *servicepath,
490 const struct smb_filename *smb_fname,
491 const struct timespec *old_write_time)
493 struct share_mode_data *d;
495 if ((servicepath == NULL) || (smb_fname == NULL) ||
496 (old_write_time == NULL)) {
500 d = talloc_zero(mem_ctx, struct share_mode_data);
504 /* New record - new sequence number. */
505 generate_random_buffer((uint8_t *)&d->sequence_number, 8);
507 d->base_name = talloc_strdup(d, smb_fname->base_name);
508 if (d->base_name == NULL) {
511 if (smb_fname->stream_name != NULL) {
512 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
513 if (d->stream_name == NULL) {
517 d->servicepath = talloc_strdup(d, servicepath);
518 if (d->servicepath == NULL) {
521 d->old_write_time = full_timespec_to_nt_time(old_write_time);
526 DEBUG(0, ("talloc failed\n"));
532 * We can only ever have one share mode locked. Use a static
533 * share_mode_data pointer that is shared by multiple nested
534 * share_mode_lock structures, explicitly refcounted.
536 static struct share_mode_data *static_share_mode_data = NULL;
537 static size_t static_share_mode_data_refcount = 0;
540 * db_record for the above. With dbwrap_do_locked we can get a
541 * db_record on the stack, which we can't TALLOC_FREE but which we
542 * need to share with a nested get_share_mode_lock call.
544 static struct db_record *static_share_mode_record = NULL;
545 static TDB_DATA static_share_mode_record_value = {0};
546 static bool static_share_mode_record_talloced = false;
548 /*******************************************************************
549 Either fetch a share mode from the database, or allocate a fresh
550 one if the record doesn't exist.
551 ********************************************************************/
553 static NTSTATUS get_static_share_mode_data(
554 struct db_record *rec,
556 const char *servicepath,
557 const struct smb_filename *smb_fname,
558 const struct timespec *old_write_time)
560 struct share_mode_data *d;
561 TDB_DATA value = dbwrap_record_get_value(rec);
563 SMB_ASSERT(static_share_mode_data == NULL);
565 if (value.dptr == NULL) {
566 d = fresh_share_mode_lock(
567 lock_db, servicepath, smb_fname, old_write_time);
569 if (smb_fname == NULL) {
570 return NT_STATUS_NOT_FOUND;
572 return NT_STATUS_NO_MEMORY;
575 TDB_DATA key = locking_key(&id);
576 d = parse_share_modes(lock_db, key, value);
578 return NT_STATUS_INTERNAL_DB_CORRUPTION;
584 static_share_mode_data = d;
589 /*******************************************************************
590 Get a share_mode_lock, Reference counted to allow nested calls.
591 ********************************************************************/
593 static int share_mode_lock_destructor(struct share_mode_lock *lck);
595 struct share_mode_lock *get_share_mode_lock(
598 const char *servicepath,
599 const struct smb_filename *smb_fname,
600 const struct timespec *old_write_time)
602 TDB_DATA key = locking_key(&id);
603 struct share_mode_lock *lck = NULL;
606 lck = talloc(mem_ctx, struct share_mode_lock);
608 DEBUG(1, ("talloc failed\n"));
612 if (static_share_mode_data != NULL) {
613 if (!file_id_equal(&static_share_mode_data->id, &id)) {
614 DEBUG(1, ("Can not lock two share modes "
615 "simultaneously\n"));
621 SMB_ASSERT(static_share_mode_data_refcount == 0);
623 if (static_share_mode_record == NULL) {
624 static_share_mode_record = dbwrap_fetch_locked(
625 lock_db, lock_db, key);
626 if (static_share_mode_record == NULL) {
627 DEBUG(3, ("Could not lock share entry\n"));
630 static_share_mode_record_talloced = true;
631 static_share_mode_record_value = dbwrap_record_get_value(
632 static_share_mode_record);
638 static_key = dbwrap_record_get_key(static_share_mode_record);
640 cmp = tdb_data_cmp(static_key, key);
642 DBG_WARNING("Can not lock two share modes "
648 status = get_static_share_mode_data(static_share_mode_record,
653 if (!NT_STATUS_IS_OK(status)) {
654 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
656 TALLOC_FREE(static_share_mode_record);
661 static_share_mode_data_refcount += 1;
662 lck->data = static_share_mode_data;
664 talloc_set_destructor(lck, share_mode_lock_destructor);
672 static int share_mode_lock_destructor(struct share_mode_lock *lck)
676 SMB_ASSERT(static_share_mode_data_refcount > 0);
677 static_share_mode_data_refcount -= 1;
679 if (static_share_mode_data_refcount > 0) {
683 status = share_mode_data_store(
684 static_share_mode_data, static_share_mode_record);
685 if (!NT_STATUS_IS_OK(status)) {
686 DBG_ERR("share_mode_data_store failed: %s\n",
688 smb_panic("Could not store share mode data\n");
691 if (static_share_mode_record_talloced) {
692 TALLOC_FREE(static_share_mode_record);
695 if (static_share_mode_data->have_share_modes) {
697 * This is worth keeping. Without share modes,
698 * share_mode_data_store above has left nothing in the
701 share_mode_memcache_store(static_share_mode_data);
702 static_share_mode_data = NULL;
705 * The next opener of this file will find an empty
706 * locking.tdb record. Don't store the share_mode_data
707 * in the memcache, fresh_share_mode_lock() will
708 * generate a fresh seqnum anyway, obsoleting the
711 TALLOC_FREE(static_share_mode_data);
717 struct share_mode_do_locked_state {
718 void (*fn)(TDB_DATA value,
719 bool *modified_dependent,
724 static void share_mode_do_locked_fn(
725 struct db_record *rec,
729 struct share_mode_do_locked_state *state = private_data;
730 bool modified_dependent = false;
731 bool reset_static_share_mode_record = false;
733 if (static_share_mode_record == NULL) {
734 static_share_mode_record = rec;
735 static_share_mode_record_value = value;
736 static_share_mode_record_talloced = false;
737 reset_static_share_mode_record = true;
739 SMB_ASSERT(static_share_mode_record == rec);
742 state->fn(value, &modified_dependent, state->private_data);
744 if (modified_dependent) {
745 dbwrap_watched_wakeup(rec);
748 if (reset_static_share_mode_record) {
749 static_share_mode_record = NULL;
753 NTSTATUS share_mode_do_locked(
755 void (*fn)(TDB_DATA value,
756 bool *modified_dependent,
760 TDB_DATA key = locking_key(&id);
761 size_t refcount = static_share_mode_data_refcount;
763 if (static_share_mode_record != NULL) {
764 bool modified_dependent = false;
768 static_key = dbwrap_record_get_key(static_share_mode_record);
770 cmp = tdb_data_cmp(static_key, key);
772 DBG_WARNING("Can not lock two share modes "
774 return NT_STATUS_INVALID_LOCK_SEQUENCE;
777 fn(static_share_mode_record_value,
781 if (modified_dependent) {
782 dbwrap_watched_wakeup(static_share_mode_record);
785 struct share_mode_do_locked_state state = {
786 .fn = fn, .private_data = private_data,
790 status = dbwrap_do_locked(
791 lock_db, key, share_mode_do_locked_fn, &state);
792 if (!NT_STATUS_IS_OK(status)) {
793 DBG_WARNING("dbwrap_do_locked failed: %s\n",
799 SMB_ASSERT(refcount == static_share_mode_data_refcount);
804 static void share_mode_wakeup_waiters_fn(TDB_DATA value,
805 bool *modified_dependent,
808 *modified_dependent = true;
811 NTSTATUS share_mode_wakeup_waiters(struct file_id id)
813 return share_mode_do_locked(id, share_mode_wakeup_waiters_fn, NULL);
816 bool share_mode_have_entries(struct share_mode_lock *lck)
818 return lck->data->have_share_modes;
821 struct share_mode_watch_state {
822 struct tevent_context *ev;
824 struct server_id blocker;
825 struct tevent_req *subreq;
828 static void share_mode_watch_fn(
829 TDB_DATA value, bool *modified_dependent, void *private_data)
831 struct share_mode_watch_state *state = talloc_get_type_abort(
832 private_data, struct share_mode_watch_state);
834 state->subreq = dbwrap_watched_watch_send(
835 state, state->ev, static_share_mode_record, state->blocker);
838 static void share_mode_watch_done(struct tevent_req *subreq);
840 struct tevent_req *share_mode_watch_send(
842 struct tevent_context *ev,
844 struct server_id blocker)
846 struct tevent_req *req = NULL;
847 struct share_mode_watch_state *state = NULL;
850 req = tevent_req_create(
851 mem_ctx, &state, struct share_mode_watch_state);
856 state->blocker = blocker;
858 status = share_mode_do_locked(id, share_mode_watch_fn, state);
859 if (tevent_req_nterror(req, status)) {
860 return tevent_req_post(req, ev);
862 if (tevent_req_nomem(state->subreq, req)) {
863 return tevent_req_post(req, ev);
865 tevent_req_set_callback(state->subreq, share_mode_watch_done, req);
869 static void share_mode_watch_done(struct tevent_req *subreq)
871 struct tevent_req *req = tevent_req_callback_data(
872 subreq, struct tevent_req);
873 struct share_mode_watch_state *state = tevent_req_data(
874 req, struct share_mode_watch_state);
877 status = dbwrap_watched_watch_recv(
878 subreq, &state->blockerdead, &state->blocker);
879 if (tevent_req_nterror(req, status)) {
882 tevent_req_done(req);
885 NTSTATUS share_mode_watch_recv(
886 struct tevent_req *req, bool *blockerdead, struct server_id *blocker)
888 struct share_mode_watch_state *state = tevent_req_data(
889 req, struct share_mode_watch_state);
892 if (tevent_req_is_nterror(req, &status)) {
895 if (blockerdead != NULL) {
896 *blockerdead = state->blockerdead;
898 if (blocker != NULL) {
899 *blocker = state->blocker;
904 struct fetch_share_mode_unlocked_state {
906 struct share_mode_lock *lck;
909 static void fetch_share_mode_unlocked_parser(
910 TDB_DATA key, TDB_DATA data, void *private_data)
912 struct fetch_share_mode_unlocked_state *state = private_data;
914 if (data.dsize == 0) {
915 /* Likely a ctdb tombstone record, ignore it */
919 state->lck = talloc(state->mem_ctx, struct share_mode_lock);
920 if (state->lck == NULL) {
921 DEBUG(0, ("talloc failed\n"));
925 state->lck->data = parse_share_modes(state->lck, key, data);
928 /*******************************************************************
929 Get a share_mode_lock without locking the database or reference
930 counting. Used by smbstatus to display existing share modes.
931 ********************************************************************/
933 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
936 struct fetch_share_mode_unlocked_state state = { .mem_ctx = mem_ctx };
937 TDB_DATA key = locking_key(&id);
940 status = dbwrap_parse_record(
941 lock_db, key, fetch_share_mode_unlocked_parser, &state);
942 if (!NT_STATUS_IS_OK(status)) {
948 static void fetch_share_mode_done(struct tevent_req *subreq);
950 struct fetch_share_mode_state {
953 struct fetch_share_mode_unlocked_state parser_state;
954 enum dbwrap_req_state req_state;
958 * @brief Get a share_mode_lock without locking or refcounting
960 * This can be used in a clustered Samba environment where the async dbwrap
961 * request is sent over a socket to the local ctdbd. If the send queue is full
962 * and the caller was issuing multiple async dbwrap requests in a loop, the
963 * caller knows it's probably time to stop sending requests for now and try
966 * @param[in] mem_ctx The talloc memory context to use.
968 * @param[in] ev The event context to work on.
970 * @param[in] id The file id for the locking.tdb key
972 * @param[out] queued This boolean out parameter tells the caller whether the
973 * async request is blocked in a full send queue:
975 * false := request is dispatched
977 * true := send queue is full, request waiting to be
980 * @return The new async request, NULL on error.
982 struct tevent_req *fetch_share_mode_send(TALLOC_CTX *mem_ctx,
983 struct tevent_context *ev,
987 struct tevent_req *req = NULL;
988 struct fetch_share_mode_state *state = NULL;
989 struct tevent_req *subreq = NULL;
993 req = tevent_req_create(mem_ctx, &state,
994 struct fetch_share_mode_state);
1000 state->key = locking_key(&state->id);
1001 state->parser_state.mem_ctx = state;
1003 subreq = dbwrap_parse_record_send(state,
1007 fetch_share_mode_unlocked_parser,
1008 &state->parser_state,
1010 if (tevent_req_nomem(subreq, req)) {
1011 return tevent_req_post(req, ev);
1013 tevent_req_set_callback(subreq, fetch_share_mode_done, req);
1015 if (state->req_state < DBWRAP_REQ_DISPATCHED) {
1021 static void fetch_share_mode_done(struct tevent_req *subreq)
1023 struct tevent_req *req = tevent_req_callback_data(
1024 subreq, struct tevent_req);
1027 status = dbwrap_parse_record_recv(subreq);
1028 TALLOC_FREE(subreq);
1029 if (tevent_req_nterror(req, status)) {
1033 tevent_req_done(req);
1037 NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
1038 TALLOC_CTX *mem_ctx,
1039 struct share_mode_lock **_lck)
1041 struct fetch_share_mode_state *state = tevent_req_data(
1042 req, struct fetch_share_mode_state);
1043 struct share_mode_lock *lck = NULL;
1047 if (tevent_req_is_nterror(req, &status)) {
1048 tevent_req_received(req);
1052 if (state->parser_state.lck->data == NULL) {
1053 tevent_req_received(req);
1054 return NT_STATUS_NOT_FOUND;
1057 lck = talloc_move(mem_ctx, &state->parser_state.lck);
1059 if (DEBUGLEVEL >= 10) {
1060 DBG_DEBUG("share_mode_data:\n");
1061 NDR_PRINT_DEBUG(share_mode_data, lck->data);
1065 tevent_req_received(req);
1066 return NT_STATUS_OK;
1069 struct share_mode_forall_state {
1070 int (*fn)(struct file_id fid, const struct share_mode_data *data,
1071 void *private_data);
1075 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
1077 struct share_mode_forall_state *state =
1078 (struct share_mode_forall_state *)_state;
1082 enum ndr_err_code ndr_err;
1083 struct share_mode_data *d;
1087 key = dbwrap_record_get_key(rec);
1088 value = dbwrap_record_get_value(rec);
1090 /* Ensure this is a locking_key record. */
1091 if (key.dsize != sizeof(fid)) {
1094 memcpy(&fid, key.dptr, sizeof(fid));
1096 d = talloc(talloc_tos(), struct share_mode_data);
1101 blob.data = value.dptr;
1102 blob.length = value.dsize;
1104 ndr_err = ndr_pull_struct_blob_all(
1105 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
1106 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1107 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
1111 if (DEBUGLEVEL > 10) {
1112 DEBUG(11, ("parse_share_modes:\n"));
1113 NDR_PRINT_DEBUG(share_mode_data, d);
1116 ret = state->fn(fid, d, state->private_data);
1122 int share_mode_forall(int (*fn)(struct file_id fid,
1123 const struct share_mode_data *data,
1124 void *private_data),
1127 struct share_mode_forall_state state = {
1129 .private_data = private_data
1134 if (lock_db == NULL) {
1138 status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
1140 if (!NT_STATUS_IS_OK(status)) {
1147 struct share_entry_forall_state {
1149 const struct share_mode_data *data;
1150 int (*fn)(struct file_id fid,
1151 const struct share_mode_data *data,
1152 const struct share_mode_entry *entry,
1153 void *private_data);
1158 static bool share_entry_traverse_walker(
1159 struct share_mode_entry *e,
1163 struct share_entry_forall_state *state = private_data;
1165 state->ret = state->fn(
1166 state->fid, state->data, e, state->private_data);
1167 return (state->ret != 0);
1170 static int share_entry_traverse_fn(struct file_id fid,
1171 const struct share_mode_data *data,
1174 struct share_entry_forall_state *state = private_data;
1175 struct share_mode_lock lck = {
1176 .data = discard_const_p(struct share_mode_data, data)
1183 ok = share_mode_forall_entries(
1184 &lck, share_entry_traverse_walker, state);
1186 DBG_DEBUG("share_mode_forall_entries failed\n");
1193 /*******************************************************************
1194 Call the specified function on each entry under management by the
1196 ********************************************************************/
1198 int share_entry_forall(int (*fn)(struct file_id fid,
1199 const struct share_mode_data *data,
1200 const struct share_mode_entry *entry,
1201 void *private_data),
1204 struct share_entry_forall_state state = {
1205 .fn = fn, .private_data = private_data };
1207 return share_mode_forall(share_entry_traverse_fn, &state);
1210 struct cleanup_disconnected_state {
1211 struct share_mode_lock *lck;
1212 uint64_t open_persistent_id;
1213 size_t num_disconnected;
1214 bool found_connected;
1217 static bool cleanup_disconnected_lease(struct share_mode_entry *e,
1220 struct cleanup_disconnected_state *state = private_data;
1223 status = leases_db_del(
1224 &e->client_guid, &e->lease_key, &state->lck->data->id);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 DBG_DEBUG("leases_db_del failed: %s\n",
1234 static bool share_mode_find_connected_fn(
1235 struct share_mode_entry *e,
1239 struct cleanup_disconnected_state *state = private_data;
1240 struct share_mode_data *d = state->lck->data;
1243 disconnected = server_id_is_disconnected(&e->pid);
1244 if (!disconnected) {
1245 struct file_id_buf tmp1;
1246 struct server_id_buf tmp2;
1247 DBG_INFO("file (file-id='%s', servicepath='%s', "
1248 "base_name='%s%s%s') "
1249 "is used by server %s ==> do not cleanup\n",
1250 file_id_str_buf(d->id, &tmp1),
1253 (d->stream_name == NULL)
1254 ? "" : "', stream_name='",
1255 (d->stream_name == NULL)
1256 ? "" : d->stream_name,
1257 server_id_str_buf(e->pid, &tmp2));
1258 state->found_connected = true;
1262 if (state->open_persistent_id != e->share_file_id) {
1263 struct file_id_buf tmp;
1264 DBG_INFO("entry for file "
1265 "(file-id='%s', servicepath='%s', "
1266 "base_name='%s%s%s') "
1267 "has share_file_id %"PRIu64" but expected "
1268 "%"PRIu64"==> do not cleanup\n",
1269 file_id_str_buf(d->id, &tmp),
1272 (d->stream_name == NULL)
1273 ? "" : "', stream_name='",
1274 (d->stream_name == NULL)
1275 ? "" : d->stream_name,
1277 state->open_persistent_id);
1278 state->found_connected = true;
1282 state->num_disconnected += 1;
1287 bool share_mode_cleanup_disconnected(struct file_id fid,
1288 uint64_t open_persistent_id)
1290 struct cleanup_disconnected_state state = {
1291 .open_persistent_id = open_persistent_id
1293 struct share_mode_data *data;
1295 TALLOC_CTX *frame = talloc_stackframe();
1296 struct file_id_buf idbuf;
1300 state.lck = get_existing_share_mode_lock(frame, fid);
1301 if (state.lck == NULL) {
1302 DBG_INFO("Could not fetch share mode entry for %s\n",
1303 file_id_str_buf(fid, &idbuf));
1306 data = state.lck->data;
1308 ok = share_mode_forall_entries(
1309 state.lck, share_mode_find_connected_fn, &state);
1311 DBG_DEBUG("share_mode_forall_entries failed\n");
1314 if (state.found_connected) {
1315 DBG_DEBUG("Found connected entry\n");
1319 ok = share_mode_forall_leases(
1320 state.lck, cleanup_disconnected_lease, &state);
1322 DBG_DEBUG("failed to clean up leases associated "
1323 "with file (file-id='%s', servicepath='%s', "
1324 "base_name='%s%s%s') and open_persistent_id %"PRIu64" "
1325 "==> do not cleanup\n",
1326 file_id_str_buf(fid, &idbuf),
1329 (data->stream_name == NULL)
1330 ? "" : "', stream_name='",
1331 (data->stream_name == NULL)
1332 ? "" : data->stream_name,
1333 open_persistent_id);
1336 ok = brl_cleanup_disconnected(fid, open_persistent_id);
1338 DBG_DEBUG("failed to clean up byte range locks associated "
1339 "with file (file-id='%s', servicepath='%s', "
1340 "base_name='%s%s%s') and open_persistent_id %"PRIu64" "
1341 "==> do not cleanup\n",
1342 file_id_str_buf(fid, &idbuf),
1345 (data->stream_name == NULL)
1346 ? "" : "', stream_name='",
1347 (data->stream_name == NULL)
1348 ? "" : data->stream_name,
1349 open_persistent_id);
1353 DBG_DEBUG("cleaning up %zu entries for file "
1354 "(file-id='%s', servicepath='%s', "
1355 "base_name='%s%s%s') "
1356 "from open_persistent_id %"PRIu64"\n",
1357 state.num_disconnected,
1358 file_id_str_buf(fid, &idbuf),
1361 (data->stream_name == NULL)
1362 ? "" : "', stream_name='",
1363 (data->stream_name == NULL)
1364 ? "" : data->stream_name,
1365 open_persistent_id);
1368 * No connected share entries left, wipe them all
1370 status = dbwrap_delete(share_entries_db, locking_key(&fid));
1371 if (!NT_STATUS_IS_OK(status)) {
1372 DBG_DEBUG("dbwrap_delete failed: %s\n",
1377 data->have_share_modes = false;
1378 data->modified = true;
1386 static int share_mode_entry_cmp(
1387 struct server_id pid1,
1388 uint64_t share_file_id1,
1389 struct server_id pid2,
1390 uint64_t share_file_id2)
1394 cmp = server_id_cmp(&pid1, &pid2);
1398 if (share_file_id1 != share_file_id2) {
1399 return (share_file_id1 < share_file_id2) ? -1 : 1;
1405 * 132 is the sizeof an ndr-encoded struct share_mode_entry_buf.
1406 * Reading/writing entries will immediately error out if this
1407 * size differs (push/pull is done without allocs).
1410 struct share_mode_entry_buf {
1413 #define SHARE_MODE_ENTRY_SIZE (sizeof(struct share_mode_entry_buf))
1415 static bool share_mode_entry_put(
1416 const struct share_mode_entry *e,
1417 struct share_mode_entry_buf *dst)
1419 DATA_BLOB blob = { .data = dst->buf, .length = sizeof(dst->buf) };
1420 enum ndr_err_code ndr_err;
1422 if (DEBUGLEVEL>=10) {
1423 DBG_DEBUG("share_mode_entry:\n");
1424 NDR_PRINT_DEBUG(share_mode_entry, discard_const_p(void, e));
1427 ndr_err = ndr_push_struct_into_fixed_blob(
1430 (ndr_push_flags_fn_t)ndr_push_share_mode_entry);
1431 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1432 DBG_WARNING("ndr_push_share_mode_entry failed: %s\n",
1433 ndr_errstr(ndr_err));
1440 static bool share_mode_entry_get(
1441 const uint8_t ptr[SHARE_MODE_ENTRY_SIZE], struct share_mode_entry *e)
1443 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
1445 .data = discard_const_p(uint8_t, ptr),
1446 .length = SHARE_MODE_ENTRY_SIZE,
1449 ndr_err = ndr_pull_struct_blob_all_noalloc(
1450 &blob, e, (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
1451 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1452 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
1458 static size_t share_mode_entry_find(
1460 size_t num_share_modes,
1461 struct server_id pid,
1462 uint64_t share_file_id,
1463 struct share_mode_entry *e,
1466 ssize_t left, right, middle;
1468 if (num_share_modes == 0) {
1474 right = (num_share_modes-1);
1476 while (left <= right) {
1477 const uint8_t *middle_ptr = NULL;
1481 middle = left + ((right - left) / 2);
1482 middle_ptr = data + middle * SHARE_MODE_ENTRY_SIZE;
1484 DBG_DEBUG("left=%zu, right=%zu, middle=%zu, middle_ptr=%p\n",
1490 ok = share_mode_entry_get(middle_ptr, e);
1492 DBG_DEBUG("share_mode_entry_get failed\n");
1496 cmp = share_mode_entry_cmp(
1497 e->pid, e->share_file_id, pid, share_file_id);
1514 struct set_share_mode_state {
1515 struct share_mode_entry e;
1516 bool created_share_mode_record;
1520 static void set_share_mode_fn(
1521 struct db_record *rec,
1525 struct set_share_mode_state *state = private_data;
1526 size_t idx, num_share_modes;
1527 struct share_mode_entry tmp;
1528 struct share_mode_entry_buf buf;
1532 size_t num_dbufs = 0;
1534 if ((data.dsize % SHARE_MODE_ENTRY_SIZE) != 0) {
1535 DBG_WARNING("Got invalid record size %zu\n", data.dsize);
1536 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1539 num_share_modes = data.dsize / SHARE_MODE_ENTRY_SIZE;
1541 ok = share_mode_entry_put(&state->e, &buf);
1543 DBG_DEBUG("share_mode_entry_put failed\n");
1544 state->status = NT_STATUS_INTERNAL_ERROR;
1548 DBG_DEBUG("num_share_modes=%zu\n", num_share_modes);
1550 idx = share_mode_entry_find(
1554 state->e.share_file_id,
1558 DBG_WARNING("Found duplicate share mode\n");
1559 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1563 DBG_DEBUG("idx=%zu, found=%d\n", idx, (int)found);
1566 dbufs[num_dbufs] = (TDB_DATA) {
1568 .dsize = idx * SHARE_MODE_ENTRY_SIZE,
1573 dbufs[num_dbufs] = (TDB_DATA) {
1574 .dptr = buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
1578 if (idx < num_share_modes) {
1579 dbufs[num_dbufs] = (TDB_DATA) {
1580 .dptr = data.dptr + idx * SHARE_MODE_ENTRY_SIZE,
1581 .dsize = (num_share_modes-idx) * SHARE_MODE_ENTRY_SIZE,
1588 for (i=0; i<num_dbufs; i++) {
1589 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
1596 state->created_share_mode_record = (num_share_modes == 0);
1597 state->status = dbwrap_record_storev(rec, dbufs, num_dbufs, 0);
1600 bool set_share_mode(struct share_mode_lock *lck,
1601 struct files_struct *fsp,
1605 uint32_t share_access,
1606 uint32_t access_mask)
1608 struct share_mode_data *d = lck->data;
1609 struct set_share_mode_state state = {
1610 .status = NT_STATUS_OK,
1611 .e.pid = messaging_server_id(fsp->conn->sconn->msg_ctx),
1612 .e.share_access = share_access,
1613 .e.private_options = fsp->fh->private_options,
1614 .e.access_mask = access_mask,
1616 .e.op_type = op_type,
1617 .e.time.tv_sec = fsp->open_time.tv_sec,
1618 .e.time.tv_usec = fsp->open_time.tv_usec,
1619 .e.share_file_id = fsp->fh->gen_id,
1620 .e.uid = (uint32_t)uid,
1621 .e.flags = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
1622 SHARE_MODE_FLAG_POSIX_OPEN : 0,
1623 .e.name_hash = fsp->name_hash,
1627 if (op_type == LEASE_OPLOCK) {
1628 const struct GUID *client_guid = fsp_client_guid(fsp);
1629 state.e.client_guid = *client_guid;
1630 state.e.lease_key = fsp->lease->lease.lease_key;
1633 status = dbwrap_do_locked(
1635 locking_key(&d->id),
1638 if (!NT_STATUS_IS_OK(status)) {
1639 DBG_WARNING("dbwrap_do_locked failed: %s\n",
1643 if (!NT_STATUS_IS_OK(state.status)) {
1644 DBG_WARNING("set_share_mode_fn failed: %s\n",
1645 nt_errstr(state.status));
1649 if (state.created_share_mode_record) {
1650 d->have_share_modes = true;
1657 struct share_mode_forall_entries_state {
1658 struct share_mode_lock *lck;
1659 bool (*fn)(struct share_mode_entry *e,
1661 void *private_data);
1666 static bool share_mode_for_one_entry(
1667 struct share_mode_forall_entries_state *state,
1669 size_t *num_share_modes,
1674 .data = data.dptr + (*i) * SHARE_MODE_ENTRY_SIZE,
1675 .length = SHARE_MODE_ENTRY_SIZE,
1677 struct share_mode_entry e = {.pid.pid=0};
1678 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
1679 bool modified = false;
1681 struct server_id e_pid;
1682 uint64_t e_share_file_id;
1684 ndr_err = ndr_pull_struct_blob_all_noalloc(
1687 (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
1688 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1689 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
1693 if (DEBUGLEVEL >= 10) {
1694 DBG_DEBUG("entry[%zu]:\n", *i);
1695 NDR_PRINT_DEBUG(share_mode_entry, &e);
1699 e_share_file_id = e.share_file_id;
1701 stop = state->fn(&e, &modified, state->private_data);
1703 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
1709 if (DEBUGLEVEL>=10) {
1710 DBG_DEBUG("share_mode_entry:\n");
1711 NDR_PRINT_DEBUG(share_mode_entry, &e);
1715 * Make sure sorting order is kept intact
1717 SMB_ASSERT(server_id_equal(&e_pid, &e.pid));
1718 SMB_ASSERT(e_share_file_id == e.share_file_id);
1720 ndr_err = ndr_push_struct_into_fixed_blob(
1723 (ndr_push_flags_fn_t)
1724 ndr_push_share_mode_entry);
1725 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1726 DBG_WARNING("ndr_push_share_mode_entry "
1728 ndr_errstr(ndr_err));
1730 * Not much we can do, just ignore it
1739 if (*i < *num_share_modes) {
1741 blob.data + SHARE_MODE_ENTRY_SIZE,
1742 (*num_share_modes - *i - 1) *
1743 SHARE_MODE_ENTRY_SIZE);
1745 *num_share_modes -= 1;
1758 static void share_mode_forall_entries_fn(
1759 struct db_record *rec,
1763 struct share_mode_forall_entries_state *state = private_data;
1764 struct share_mode_data *d = state->lck->data;
1765 size_t num_share_modes;
1766 bool writeback = false;
1771 if ((data.dsize % SHARE_MODE_ENTRY_SIZE) != 0) {
1772 DBG_WARNING("Invalid data size %zu\n", data.dsize);
1775 num_share_modes = data.dsize / SHARE_MODE_ENTRY_SIZE;
1777 DBG_DEBUG("num_share_modes=%zu\n", num_share_modes);
1780 while (i<num_share_modes) {
1781 stop = share_mode_for_one_entry(
1782 state, &i, &num_share_modes, data, &writeback);
1788 DBG_DEBUG("num_share_modes=%zu, writeback=%d\n",
1797 if (num_share_modes == 0) {
1798 if (data.dsize != 0) {
1799 d->have_share_modes = false;
1802 status = dbwrap_record_delete(rec);
1806 .dsize = num_share_modes * SHARE_MODE_ENTRY_SIZE,
1808 status = dbwrap_record_store(rec, value, 0);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 DBG_DEBUG("Storing record with %zu entries failed: %s\n",
1822 bool share_mode_forall_entries(
1823 struct share_mode_lock *lck,
1824 bool (*fn)(struct share_mode_entry *e,
1826 void *private_data),
1829 struct share_mode_forall_entries_state state = {
1832 .private_data = private_data,
1836 status = dbwrap_do_locked(
1838 locking_key(&lck->data->id),
1839 share_mode_forall_entries_fn,
1841 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1842 status = NT_STATUS_OK;
1845 if (!NT_STATUS_IS_OK(status)) {
1846 DBG_DEBUG("dbwrap_do_locked failed: %s\n",
1854 struct share_mode_count_entries_state {
1855 size_t num_share_modes;
1859 static void share_mode_count_entries_fn(
1860 TDB_DATA key, TDB_DATA data, void *private_data)
1862 struct share_mode_count_entries_state *state = private_data;
1864 if ((data.dsize % SHARE_MODE_ENTRY_SIZE) != 0) {
1865 DBG_WARNING("Invalid data size %zu\n", data.dsize);
1866 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1869 state->num_share_modes = data.dsize / SHARE_MODE_ENTRY_SIZE;
1870 state->status = NT_STATUS_OK;
1873 NTSTATUS share_mode_count_entries(struct file_id fid, size_t *num_share_modes)
1875 struct share_mode_count_entries_state state = {
1876 .status = NT_STATUS_NOT_FOUND,
1880 status = dbwrap_parse_record(
1883 share_mode_count_entries_fn,
1885 if (!NT_STATUS_IS_OK(status)) {
1886 DBG_DEBUG("dbwrap_parse_record failed: %s\n",
1890 if (!NT_STATUS_IS_OK(state.status)) {
1891 DBG_DEBUG("share_mode_forall_entries_fn failed: %s\n",
1892 nt_errstr(state.status));
1893 return state.status;
1896 *num_share_modes = state.num_share_modes;
1897 return NT_STATUS_OK;
1900 struct share_mode_entry_do_state {
1901 struct server_id pid;
1902 uint64_t share_file_id;
1903 void (*fn)(struct share_mode_entry *e,
1904 size_t num_share_modes,
1906 void *private_data);
1908 size_t num_share_modes;
1912 static void share_mode_entry_do_fn(
1913 struct db_record *rec,
1917 struct share_mode_entry_do_state *state = private_data;
1921 struct share_mode_entry e;
1922 struct share_mode_entry_buf buf;
1924 size_t num_dbufs = 0;
1926 if ((data.dsize % SHARE_MODE_ENTRY_SIZE) != 0) {
1927 DBG_WARNING("Invalid data size %zu\n", data.dsize);
1928 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1931 state->num_share_modes = data.dsize / SHARE_MODE_ENTRY_SIZE;
1933 DBG_DEBUG("state->num_share_modes=%zu\n", state->num_share_modes);
1935 idx = share_mode_entry_find(
1937 state->num_share_modes,
1939 state->share_file_id,
1943 DBG_WARNING("Did not find share mode entry for %"PRIu64"\n",
1944 state->share_file_id);
1945 state->status = NT_STATUS_NOT_FOUND;
1949 state->fn(&e, state->num_share_modes, &modified, state->private_data);
1951 if (!e.stale && !modified) {
1952 state->status = NT_STATUS_OK;
1957 dbufs[num_dbufs] = (TDB_DATA) {
1959 .dsize = idx * SHARE_MODE_ENTRY_SIZE,
1965 bool ok = share_mode_entry_put(&e, &buf);
1967 DBG_DEBUG("share_mode_entry_put failed\n");
1968 state->status = NT_STATUS_INTERNAL_ERROR;
1972 dbufs[num_dbufs] = (TDB_DATA) {
1973 .dptr = buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
1980 if (idx < state->num_share_modes) {
1981 size_t behind = state->num_share_modes - idx;
1982 dbufs[num_dbufs] = (TDB_DATA) {
1983 .dptr = data.dptr + idx * SHARE_MODE_ENTRY_SIZE,
1984 .dsize = behind * SHARE_MODE_ENTRY_SIZE,
1990 state->num_share_modes -= 1;
1993 if (state->num_share_modes == 0) {
1994 state->status = dbwrap_record_delete(rec);
1995 if (!NT_STATUS_IS_OK(state->status)) {
1996 DBG_DEBUG("dbwrap_record_delete failed: %s\n",
1997 nt_errstr(state->status));
2002 state->status = dbwrap_record_storev(rec, dbufs, num_dbufs, 0);
2003 if (!NT_STATUS_IS_OK(state->status)) {
2004 DBG_DEBUG("dbwrap_record_storev failed: %s\n",
2005 nt_errstr(state->status));
2010 static bool share_mode_entry_do(
2011 struct share_mode_lock *lck,
2012 struct server_id pid,
2013 uint64_t share_file_id,
2014 void (*fn)(struct share_mode_entry *e,
2015 size_t num_share_modes,
2017 void *private_data),
2020 struct share_mode_data *d = lck->data;
2021 struct share_mode_entry_do_state state = {
2023 .share_file_id = share_file_id,
2025 .private_data = private_data,
2028 bool have_share_modes;
2030 status = dbwrap_do_locked(
2032 locking_key(&d->id),
2033 share_mode_entry_do_fn,
2035 if (!NT_STATUS_IS_OK(status)) {
2036 DBG_DEBUG("share_mode_forall_entries failed: %s\n",
2040 if (!NT_STATUS_IS_OK(state.status)) {
2041 DBG_DEBUG("share_mode_entry_do_fn failed: %s\n",
2046 have_share_modes = (state.num_share_modes != 0);
2047 if (d->have_share_modes != have_share_modes) {
2048 d->have_share_modes = have_share_modes;
2055 struct del_share_mode_state {
2059 static void del_share_mode_fn(
2060 struct share_mode_entry *e,
2061 size_t num_share_modes,
2065 struct del_share_mode_state *state = private_data;
2070 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
2072 struct del_share_mode_state state = { .ok = false };
2075 ok = share_mode_entry_do(
2077 messaging_server_id(fsp->conn->sconn->msg_ctx),
2082 DBG_DEBUG("share_mode_entry_do failed\n");
2086 DBG_DEBUG("del_share_mode_fn failed\n");
2092 struct remove_share_oplock_state {
2096 static void remove_share_oplock_fn(
2097 struct share_mode_entry *e,
2098 size_t num_share_modes,
2102 struct remove_share_oplock_state *state = private_data;
2104 e->op_type = NO_OPLOCK;
2109 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2111 struct remove_share_oplock_state state = { .ok = false };
2114 ok = share_mode_entry_do(
2116 messaging_server_id(fsp->conn->sconn->msg_ctx),
2118 remove_share_oplock_fn,
2121 DBG_DEBUG("share_mode_entry_do failed\n");
2125 DBG_DEBUG("remove_share_oplock_fn failed\n");
2129 if (fsp->oplock_type == LEASE_OPLOCK) {
2130 remove_lease_if_stale(
2132 fsp_client_guid(fsp),
2133 &fsp->lease->lease.lease_key);
2136 share_mode_wakeup_waiters(fsp->file_id);
2141 struct downgrade_share_oplock_state {
2145 static void downgrade_share_oplock_fn(
2146 struct share_mode_entry *e,
2147 size_t num_share_modes,
2151 struct downgrade_share_oplock_state *state = private_data;
2153 e->op_type = LEVEL_II_OPLOCK;
2158 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2160 struct downgrade_share_oplock_state state = { .ok = false };
2163 ok = share_mode_entry_do(
2165 messaging_server_id(fsp->conn->sconn->msg_ctx),
2167 downgrade_share_oplock_fn,
2170 DBG_DEBUG("share_mode_entry_do failed\n");
2174 DBG_DEBUG("downgrade_share_oplock_fn failed\n");
2178 lck->data->flags |= SHARE_MODE_LEASE_READ;
2179 lck->data->modified = true;
2184 struct mark_share_mode_disconnected_state {
2185 uint64_t open_persistent_id;
2189 static void mark_share_mode_disconnected_fn(
2190 struct share_mode_entry *e,
2191 size_t num_share_modes,
2195 struct mark_share_mode_disconnected_state *state = private_data;
2197 if (num_share_modes != 1) {
2202 server_id_set_disconnected(&e->pid);
2203 e->share_file_id = state->open_persistent_id;
2208 bool mark_share_mode_disconnected(struct share_mode_lock *lck,
2209 struct files_struct *fsp)
2211 struct mark_share_mode_disconnected_state state;
2214 if (fsp->op == NULL) {
2217 if (!fsp->op->global->durable) {
2221 state = (struct mark_share_mode_disconnected_state) {
2222 .open_persistent_id = fsp->op->global->open_persistent_id,
2225 ok = share_mode_entry_do(
2227 messaging_server_id(fsp->conn->sconn->msg_ctx),
2229 mark_share_mode_disconnected_fn,
2232 DBG_DEBUG("share_mode_entry_do failed\n");
2236 DBG_DEBUG("mark_share_mode_disconnected_fn failed\n");
2240 lck->data->modified = true;
2244 static void reset_share_mode_entry_del_fn(
2245 struct share_mode_entry *e,
2246 size_t num_share_modes,
2250 struct set_share_mode_state *state = private_data;
2254 state->status = NT_STATUS_OK;
2257 bool reset_share_mode_entry(
2258 struct share_mode_lock *lck,
2259 struct server_id old_pid,
2260 uint64_t old_share_file_id,
2261 struct server_id new_pid,
2263 uint64_t new_share_file_id)
2265 struct share_mode_data *d = lck->data;
2266 struct set_share_mode_state state = {
2267 .status = NT_STATUS_INTERNAL_ERROR,
2272 ok = share_mode_entry_do(
2276 reset_share_mode_entry_del_fn,
2279 DBG_DEBUG("share_mode_entry_do failed\n");
2282 if (!NT_STATUS_IS_OK(state.status)) {
2283 DBG_DEBUG("reset_share_mode_entry_del_fn failed: %s\n",
2284 nt_errstr(state.status));
2288 state.status = NT_STATUS_INTERNAL_ERROR;
2289 state.e.pid = new_pid;
2290 state.e.op_mid = new_mid;
2291 state.e.share_file_id = new_share_file_id;
2293 status = dbwrap_do_locked(
2295 locking_key(&d->id),
2298 if (!NT_STATUS_IS_OK(status)) {
2299 DBG_WARNING("dbwrap_do_locked failed: %s\n",
2303 if (!NT_STATUS_IS_OK(state.status)) {
2304 DBG_WARNING("set_share_mode_fn failed: %s\n",
2305 nt_errstr(state.status));
2308 d->have_share_modes = true;