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 "share_mode_lock.h"
42 #include "share_mode_lock_private.h"
43 #include "locking/proto.h"
44 #include "smbd/globals.h"
45 #include "dbwrap/dbwrap.h"
46 #include "dbwrap/dbwrap_open.h"
47 #include "dbwrap/dbwrap_private.h"
48 #include "../libcli/security/security.h"
52 #include "../librpc/gen_ndr/ndr_open_files.h"
53 #include "source3/lib/dbwrap/dbwrap_watch.h"
54 #include "locking/leases_db.h"
55 #include "../lib/util/memcache.h"
56 #include "lib/util/tevent_ntstatus.h"
58 #include "smbd/fd_handle.h"
59 #include "lib/global_contexts.h"
62 #define DBGC_CLASS DBGC_LOCKING
64 #define DBG_GET_SHARE_MODE_LOCK(__status, ...) \
66 NT_STATUS_EQUAL(__status, NT_STATUS_NOT_FOUND) ? \
67 DBGLVL_DEBUG : DBGLVL_ERR, \
70 /* the locking database handle */
71 static struct g_lock_ctx *lock_ctx;
73 static bool locking_init_internal(bool read_only)
75 struct db_context *backend;
80 if (lock_ctx != NULL) {
84 db_path = lock_path(talloc_tos(), "locking.tdb");
85 if (db_path == NULL) {
89 backend = db_open(NULL, db_path,
90 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
94 TDB_INCOMPATIBLE_HASH|
96 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
97 DBWRAP_LOCK_ORDER_NONE,
101 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
105 lock_ctx = g_lock_ctx_init_backend(
106 NULL, global_messaging_context(), &backend);
107 if (lock_ctx == NULL) {
108 TALLOC_FREE(backend);
111 g_lock_set_lock_order(lock_ctx, DBWRAP_LOCK_ORDER_1);
113 if (!posix_locking_init(read_only)) {
114 TALLOC_FREE(lock_ctx);
121 bool locking_init(void)
123 return locking_init_internal(false);
126 bool locking_init_readonly(void)
128 return locking_init_internal(true);
131 /*******************************************************************
132 Deinitialize the share_mode management.
133 ******************************************************************/
135 bool locking_end(void)
138 TALLOC_FREE(lock_ctx);
142 /*******************************************************************
143 Form a static locking key for a dev/inode pair.
144 ******************************************************************/
146 static TDB_DATA locking_key(const struct file_id *id)
148 return make_tdb_data((const uint8_t *)id, sizeof(*id));
151 /*******************************************************************
152 Share mode cache utility functions that store/delete/retrieve
153 entries from memcache.
155 For now share the statcache (global cache) memory space. If
156 a lock record gets orphaned (which shouldn't happen as we're
157 using the same locking_key data as lookup) it will eventually
158 fall out of the cache via the normal LRU trim mechanism. If
159 necessary we can always make this a separate (smaller) cache.
160 ******************************************************************/
162 static DATA_BLOB memcache_key(const struct file_id *id)
164 return data_blob_const((const void *)id, sizeof(*id));
167 static void share_mode_memcache_store(struct share_mode_data *d)
169 const DATA_BLOB key = memcache_key(&d->id);
170 struct file_id_buf idbuf;
172 DBG_DEBUG("stored entry for file %s epoch %"PRIx64" key %s\n",
174 d->unique_content_epoch,
175 file_id_str_buf(d->id, &idbuf));
177 /* Ensure everything stored in the cache is pristine. */
178 SMB_ASSERT(!d->modified);
179 SMB_ASSERT(!d->not_stored);
182 * Ensure the memory going into the cache
183 * doesn't have a destructor so it can be
184 * cleanly evicted by the memcache LRU
187 talloc_set_destructor(d, NULL);
189 /* Cache will own d after this call. */
190 memcache_add_talloc(NULL,
191 SHARE_MODE_LOCK_CACHE,
197 * NB. We use ndr_pull_hyper on a stack-created
198 * struct ndr_pull with no talloc allowed, as we
199 * need this to be really fast as an ndr-peek into
200 * the first 10 bytes of the blob.
203 static enum ndr_err_code get_share_mode_blob_header(
204 const uint8_t *buf, size_t buflen, uint64_t *pepoch, uint16_t *pflags)
206 struct ndr_pull ndr = {
207 .data = discard_const_p(uint8_t, buf),
210 NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pepoch));
211 NDR_CHECK(ndr_pull_uint16(&ndr, NDR_SCALARS, pflags));
212 return NDR_ERR_SUCCESS;
215 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
220 static struct share_mode_data *share_mode_memcache_fetch(
226 const DATA_BLOB key = memcache_key(&id);
227 enum ndr_err_code ndr_err;
228 struct share_mode_data *d;
229 uint64_t unique_content_epoch;
232 struct file_id_buf idbuf;
234 ptr = memcache_lookup_talloc(NULL,
235 SHARE_MODE_LOCK_CACHE,
238 DBG_DEBUG("failed to find entry for key %s\n",
239 file_id_str_buf(id, &idbuf));
242 /* sequence number key is at start of blob. */
243 ndr_err = get_share_mode_blob_header(
244 buf, buflen, &unique_content_epoch, &flags);
245 if (ndr_err != NDR_ERR_SUCCESS) {
246 /* Bad blob. Remove entry. */
247 DBG_DEBUG("bad blob %u key %s\n",
248 (unsigned int)ndr_err,
249 file_id_str_buf(id, &idbuf));
250 memcache_delete(NULL,
251 SHARE_MODE_LOCK_CACHE,
256 d = (struct share_mode_data *)ptr;
257 if (d->unique_content_epoch != unique_content_epoch) {
258 DBG_DEBUG("epoch changed (cached %"PRIx64") (new %"PRIx64") "
260 d->unique_content_epoch,
261 unique_content_epoch,
262 file_id_str_buf(id, &idbuf));
263 /* Cache out of date. Remove entry. */
264 memcache_delete(NULL,
265 SHARE_MODE_LOCK_CACHE,
270 /* Move onto mem_ctx. */
271 d = talloc_move(mem_ctx, &ptr);
274 * Now we own d, prevent the cache from freeing it
275 * when we delete the entry.
277 talloc_set_destructor(d, share_mode_data_nofree_destructor);
279 /* Remove from the cache. We own it now. */
280 memcache_delete(NULL,
281 SHARE_MODE_LOCK_CACHE,
284 /* And reset the destructor to none. */
285 talloc_set_destructor(d, NULL);
287 DBG_DEBUG("fetched entry for file %s epoch %"PRIx64" key %s\n",
289 d->unique_content_epoch,
290 file_id_str_buf(id, &idbuf));
296 * 132 is the sizeof an ndr-encoded struct share_mode_entry_buf.
297 * Reading/writing entries will immediately error out if this
298 * size differs (push/pull is done without allocs).
301 struct share_mode_entry_buf {
304 #define SHARE_MODE_ENTRY_SIZE (sizeof(struct share_mode_entry_buf))
306 static bool share_mode_entry_put(
307 const struct share_mode_entry *e,
308 struct share_mode_entry_buf *dst)
310 DATA_BLOB blob = { .data = dst->buf, .length = sizeof(dst->buf) };
311 enum ndr_err_code ndr_err;
313 if (DEBUGLEVEL>=10) {
314 DBG_DEBUG("share_mode_entry:\n");
315 NDR_PRINT_DEBUG(share_mode_entry, discard_const_p(void, e));
318 ndr_err = ndr_push_struct_into_fixed_blob(
321 (ndr_push_flags_fn_t)ndr_push_share_mode_entry);
322 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
323 DBG_WARNING("ndr_push_share_mode_entry failed: %s\n",
324 ndr_errstr(ndr_err));
331 static bool share_mode_entry_get(
332 const uint8_t ptr[SHARE_MODE_ENTRY_SIZE], struct share_mode_entry *e)
334 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
336 .data = discard_const_p(uint8_t, ptr),
337 .length = SHARE_MODE_ENTRY_SIZE,
340 ndr_err = ndr_pull_struct_blob_all_noalloc(
341 &blob, e, (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
342 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
343 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
350 * locking.tdb records consist of
352 * uint32_t share_mode_data_len
353 * uint8_t [share_mode_data] This is struct share_mode_data in NDR
355 * 0 [SHARE_MODE_ENTRY_SIZE] Sorted array of share modes,
356 * 1 [SHARE_MODE_ENTRY_SIZE] filling up the rest of the data in the
357 * 2 [SHARE_MODE_ENTRY_SIZE] g_lock.c maintained record in locking.tdb
360 struct locking_tdb_data {
361 const uint8_t *share_mode_data_buf;
362 size_t share_mode_data_len;
363 const uint8_t *share_entries;
364 size_t num_share_entries;
367 static bool locking_tdb_data_get(
368 struct locking_tdb_data *data, const uint8_t *buf, size_t buflen)
370 uint32_t share_mode_data_len, share_entries_len;
373 *data = (struct locking_tdb_data) { 0 };
376 if (buflen < sizeof(uint32_t)) {
380 share_mode_data_len = PULL_LE_U32(buf, 0);
382 buf += sizeof(uint32_t);
383 buflen -= sizeof(uint32_t);
385 if (buflen < share_mode_data_len) {
389 share_entries_len = buflen - share_mode_data_len;
391 if ((share_entries_len % SHARE_MODE_ENTRY_SIZE) != 0) {
395 *data = (struct locking_tdb_data) {
396 .share_mode_data_buf = buf,
397 .share_mode_data_len = share_mode_data_len,
398 .share_entries = buf + share_mode_data_len,
399 .num_share_entries = share_entries_len / SHARE_MODE_ENTRY_SIZE,
405 struct locking_tdb_data_fetch_state {
411 static void locking_tdb_data_fetch_fn(
412 struct server_id exclusive,
414 const struct server_id *shared,
419 struct locking_tdb_data_fetch_state *state = private_data;
420 state->datalen = datalen;
421 state->data = talloc_memdup(state->mem_ctx, data, datalen);
424 static NTSTATUS locking_tdb_data_fetch(
425 TDB_DATA key, TALLOC_CTX *mem_ctx, struct locking_tdb_data **ltdb)
427 struct locking_tdb_data_fetch_state state = { 0 };
428 struct locking_tdb_data *result = NULL;
432 result = talloc_zero(mem_ctx, struct locking_tdb_data);
433 if (result == NULL) {
434 return NT_STATUS_NO_MEMORY;
436 state.mem_ctx = result;
438 status = g_lock_dump(lock_ctx, key, locking_tdb_data_fetch_fn, &state);
440 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
442 * Just return an empty record
446 if (!NT_STATUS_IS_OK(status)) {
447 DBG_ERR("g_lock_dump failed: %s\n",
451 if (state.datalen == 0) {
455 ok = locking_tdb_data_get(result, state.data, state.datalen);
457 DBG_ERR("locking_tdb_data_get failed for %zu bytes\n",
460 return NT_STATUS_INTERNAL_DB_CORRUPTION;
468 static NTSTATUS locking_tdb_data_store(
470 const struct locking_tdb_data *ltdb,
471 const TDB_DATA *share_mode_dbufs,
472 size_t num_share_mode_dbufs)
474 uint8_t share_mode_data_len_buf[4];
475 TDB_DATA dbufs[num_share_mode_dbufs+3];
478 if ((ltdb->share_mode_data_len == 0) &&
479 (ltdb->num_share_entries == 0) &&
480 (num_share_mode_dbufs == 0)) {
484 status = g_lock_write_data(lock_ctx, key, NULL, 0);
485 if (!NT_STATUS_IS_OK(status)) {
486 DBG_ERR("g_lock_writev_data() failed: %s\n",
492 PUSH_LE_U32(share_mode_data_len_buf, 0, ltdb->share_mode_data_len);
494 dbufs[0] = (TDB_DATA) {
495 .dptr = share_mode_data_len_buf,
496 .dsize = sizeof(share_mode_data_len_buf),
498 dbufs[1] = (TDB_DATA) {
499 .dptr = discard_const_p(uint8_t, ltdb->share_mode_data_buf),
500 .dsize = ltdb->share_mode_data_len,
503 if (ltdb->num_share_entries > SIZE_MAX/SHARE_MODE_ENTRY_SIZE) {
505 return NT_STATUS_BUFFER_OVERFLOW;
507 dbufs[2] = (TDB_DATA) {
508 .dptr = discard_const_p(uint8_t, ltdb->share_entries),
509 .dsize = ltdb->num_share_entries * SHARE_MODE_ENTRY_SIZE,
512 if (num_share_mode_dbufs != 0) {
515 num_share_mode_dbufs * sizeof(TDB_DATA));
518 status = g_lock_writev_data(lock_ctx, key, dbufs, ARRAY_SIZE(dbufs));
519 if (!NT_STATUS_IS_OK(status)) {
520 DBG_ERR("g_lock_writev_data() failed: %s\n",
526 /*******************************************************************
527 Get all share mode entries for a dev/inode pair.
528 ********************************************************************/
530 static struct share_mode_data *parse_share_modes(
536 struct share_mode_data *d;
537 enum ndr_err_code ndr_err;
540 /* See if we already have a cached copy of this key. */
541 d = share_mode_memcache_fetch(mem_ctx, id, buf, buflen);
546 d = talloc(mem_ctx, struct share_mode_data);
548 DEBUG(0, ("talloc failed\n"));
553 .data = discard_const_p(uint8_t, buf),
556 ndr_err = ndr_pull_struct_blob_all(
557 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
558 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
559 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
560 ndr_errstr(ndr_err));
564 if (DEBUGLEVEL >= 10) {
565 DEBUG(10, ("parse_share_modes:\n"));
566 NDR_PRINT_DEBUG(share_mode_data, d);
575 static NTSTATUS share_mode_data_ltdb_store(struct share_mode_data *d,
577 struct locking_tdb_data *ltdb,
578 const TDB_DATA *share_mode_dbufs,
579 size_t num_share_mode_dbufs)
581 DATA_BLOB blob = { 0 };
585 DBG_DEBUG("share_mode_data not modified\n");
589 d->unique_content_epoch = generate_unique_u64(d->unique_content_epoch);
591 if (DEBUGLEVEL >= 10) {
593 NDR_PRINT_DEBUG(share_mode_data, d);
596 if (ltdb->num_share_entries != 0 || num_share_mode_dbufs != 0) {
597 enum ndr_err_code ndr_err;
599 ndr_err = ndr_push_struct_blob(
603 (ndr_push_flags_fn_t)ndr_push_share_mode_data);
604 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
605 DBG_ERR("ndr_push_share_mode_data failed: %s\n",
606 ndr_errstr(ndr_err));
607 return ndr_map_error2ntstatus(ndr_err);
611 ltdb->share_mode_data_buf = blob.data;
612 ltdb->share_mode_data_len = blob.length;
615 status = locking_tdb_data_store(key,
618 num_share_mode_dbufs);
619 if (!NT_STATUS_IS_OK(status)) {
620 DBG_ERR("locking_tdb_data_store failed: %s\n",
626 d->not_stored = (ltdb->share_mode_data_len == 0);
631 /*******************************************************************
632 If modified, store the share_mode_data back into the database.
633 ********************************************************************/
635 static NTSTATUS share_mode_data_store(struct share_mode_data *d)
637 TDB_DATA key = locking_key(&d->id);
638 struct locking_tdb_data *ltdb = NULL;
642 DBG_DEBUG("not modified\n");
646 if (DEBUGLEVEL >= 10) {
648 NDR_PRINT_DEBUG(share_mode_data, d);
651 status = locking_tdb_data_fetch(key, d, <db);
652 if (!NT_STATUS_IS_OK(status)) {
653 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
658 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
660 if (!NT_STATUS_IS_OK(status)) {
661 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
669 /*******************************************************************
670 Allocate a new share_mode_data struct, mark it unmodified.
671 fresh is set to note that currently there is no database entry.
672 ********************************************************************/
674 static struct share_mode_data *fresh_share_mode_lock(
675 TALLOC_CTX *mem_ctx, const char *servicepath,
676 const struct smb_filename *smb_fname,
677 const struct timespec *old_write_time)
679 struct share_mode_data *d;
681 if ((servicepath == NULL) || (smb_fname == NULL) ||
682 (old_write_time == NULL)) {
686 d = talloc_zero(mem_ctx, struct share_mode_data);
690 d->unique_content_epoch = generate_unique_u64(0);
692 d->base_name = talloc_strdup(d, smb_fname->base_name);
693 if (d->base_name == NULL) {
696 if (smb_fname->stream_name != NULL) {
697 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
698 if (d->stream_name == NULL) {
702 d->servicepath = talloc_strdup(d, servicepath);
703 if (d->servicepath == NULL) {
706 d->old_write_time = full_timespec_to_nt_time(old_write_time);
707 d->flags = SHARE_MODE_SHARE_DELETE |
708 SHARE_MODE_SHARE_WRITE |
709 SHARE_MODE_SHARE_READ;
711 d->not_stored = true;
714 DEBUG(0, ("talloc failed\n"));
720 * Key that's locked with g_lock
722 static uint8_t share_mode_lock_key_data[sizeof(struct file_id)];
723 static TDB_DATA share_mode_lock_key = {
724 .dptr = share_mode_lock_key_data,
725 .dsize = sizeof(share_mode_lock_key_data),
727 static size_t share_mode_lock_key_refcount = 0;
730 * We can only ever have one share mode locked. Use a static
731 * share_mode_data pointer that is shared by multiple nested
732 * share_mode_lock structures, explicitly refcounted.
734 static struct share_mode_data *static_share_mode_data = NULL;
735 static size_t static_share_mode_data_refcount = 0;
737 /*******************************************************************
738 Either fetch a share mode from the database, or allocate a fresh
739 one if the record doesn't exist.
740 ********************************************************************/
742 struct get_static_share_mode_data_state {
745 const char *servicepath;
746 const struct smb_filename *smb_fname;
747 const struct timespec *old_write_time;
751 static void get_static_share_mode_data_fn(
752 struct server_id exclusive,
754 const struct server_id *shared,
759 struct get_static_share_mode_data_state *state = private_data;
760 struct share_mode_data *d = NULL;
761 struct locking_tdb_data ltdb = { 0 };
766 ok = locking_tdb_data_get(<db, data, datalen);
768 DBG_ERR("locking_tdb_data_get failed\n");
769 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
774 if (ltdb.share_mode_data_len == 0) {
775 if (state->smb_fname == NULL) {
776 state->status = NT_STATUS_NOT_FOUND;
779 d = fresh_share_mode_lock(
783 state->old_write_time);
785 state->status = NT_STATUS_NO_MEMORY;
789 d = parse_share_modes(
792 ltdb.share_mode_data_buf,
793 ltdb.share_mode_data_len);
795 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
801 static_share_mode_data = d;
804 static NTSTATUS get_static_share_mode_data(
806 const char *servicepath,
807 const struct smb_filename *smb_fname,
808 const struct timespec *old_write_time)
810 struct get_static_share_mode_data_state state = {
813 .servicepath = servicepath,
814 .smb_fname = smb_fname,
815 .old_write_time = old_write_time,
819 SMB_ASSERT(static_share_mode_data == NULL);
821 status = g_lock_dump(
824 get_static_share_mode_data_fn,
826 if (!NT_STATUS_IS_OK(status)) {
827 DBG_GET_SHARE_MODE_LOCK(status,
828 "g_lock_dump failed: %s\n",
832 if (!NT_STATUS_IS_OK(state.status)) {
833 DBG_GET_SHARE_MODE_LOCK(status,
834 "get_static_share_mode_data_fn failed: %s\n",
835 nt_errstr(state.status));
842 /*******************************************************************
843 Get a share_mode_lock, Reference counted to allow nested calls.
844 ********************************************************************/
846 static int share_mode_lock_destructor(struct share_mode_lock *lck);
848 struct share_mode_lock *get_share_mode_lock(
851 const char *servicepath,
852 const struct smb_filename *smb_fname,
853 const struct timespec *old_write_time)
855 TDB_DATA key = locking_key(&id);
856 struct share_mode_lock *lck = NULL;
860 lck = talloc(mem_ctx, struct share_mode_lock);
862 DEBUG(1, ("talloc failed\n"));
866 if (static_share_mode_data != NULL) {
867 if (!file_id_equal(&static_share_mode_data->id, &id)) {
868 struct file_id_buf existing;
869 struct file_id_buf requested;
871 DBG_ERR("Can not lock two share modes "
872 "simultaneously: existing %s requested %s\n",
873 file_id_str_buf(static_share_mode_data->id, &existing),
874 file_id_str_buf(id, &requested));
876 smb_panic(__location__);
882 if (share_mode_lock_key_refcount == 0) {
883 status = g_lock_lock(
887 (struct timeval) { .tv_sec = 3600 });
888 if (!NT_STATUS_IS_OK(status)) {
889 DBG_DEBUG("g_lock_lock failed: %s\n",
893 memcpy(share_mode_lock_key_data, key.dptr, key.dsize);
896 cmp = tdb_data_cmp(share_mode_lock_key, key);
898 DBG_WARNING("Can not lock two share modes simultaneously\n");
899 smb_panic(__location__);
903 SMB_ASSERT(share_mode_lock_key_refcount < SIZE_MAX);
904 share_mode_lock_key_refcount += 1;
906 SMB_ASSERT(static_share_mode_data_refcount == 0);
908 status = get_static_share_mode_data(
913 if (!NT_STATUS_IS_OK(status)) {
914 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
916 share_mode_lock_key_refcount -= 1;
920 static_share_mode_data_refcount += 1;
921 lck->data = static_share_mode_data;
923 talloc_set_destructor(lck, share_mode_lock_destructor);
925 if (CHECK_DEBUGLVL(DBGLVL_DEBUG)) {
926 struct file_id_buf returned;
928 DBG_DEBUG("Returning %s (data_refcount=%zu key_refcount=%zu)\n",
929 file_id_str_buf(id, &returned),
930 static_share_mode_data_refcount,
931 share_mode_lock_key_refcount);
937 if (share_mode_lock_key_refcount == 0) {
938 status = g_lock_unlock(lock_ctx, share_mode_lock_key);
939 if (!NT_STATUS_IS_OK(status)) {
940 DBG_ERR("g_lock_unlock failed: %s\n",
947 static int share_mode_lock_destructor(struct share_mode_lock *lck)
951 SMB_ASSERT(static_share_mode_data_refcount > 0);
952 static_share_mode_data_refcount -= 1;
954 if (static_share_mode_data_refcount > 0) {
958 status = share_mode_data_store(static_share_mode_data);
959 if (!NT_STATUS_IS_OK(status)) {
960 DBG_ERR("share_mode_data_store failed: %s\n",
962 smb_panic("Could not store share mode data\n");
965 SMB_ASSERT(share_mode_lock_key_refcount > 0);
966 share_mode_lock_key_refcount -= 1;
968 if (share_mode_lock_key_refcount == 0) {
969 status = g_lock_unlock(lock_ctx, share_mode_lock_key);
970 if (!NT_STATUS_IS_OK(status)) {
971 DBG_ERR("g_lock_unlock failed: %s\n",
973 smb_panic("Could not unlock share mode\n");
977 if (!static_share_mode_data->not_stored) {
979 * This is worth keeping. Without share modes,
980 * share_mode_data_store above has left nothing in the
983 share_mode_memcache_store(static_share_mode_data);
984 static_share_mode_data = NULL;
987 TALLOC_FREE(static_share_mode_data);
991 /*******************************************************************
992 Fetch a share mode where we know one MUST exist. This call reference
993 counts it internally to allow for nested lock fetches.
994 ********************************************************************/
996 struct share_mode_lock *get_existing_share_mode_lock(TALLOC_CTX *mem_ctx,
997 const struct file_id id)
999 return get_share_mode_lock(mem_ctx, id, NULL, NULL, NULL);
1002 struct share_mode_do_locked_state {
1004 void (*fn)(const uint8_t *buf,
1006 bool *modified_dependent,
1007 void *private_data);
1011 static void share_mode_do_locked_fn(
1012 struct server_id exclusive,
1014 const struct server_id *shared,
1015 const uint8_t *data,
1019 struct share_mode_do_locked_state *state = private_data;
1020 bool modified_dependent = false;
1021 struct locking_tdb_data ltdb = { 0 };
1024 ok = locking_tdb_data_get(
1025 <db, discard_const_p(uint8_t, data), datalen);
1027 DBG_WARNING("locking_tdb_data_get failed\n");
1031 state->fn(ltdb.share_mode_data_buf,
1032 ltdb.share_mode_data_len,
1033 &modified_dependent,
1034 state->private_data);
1036 if (modified_dependent) {
1037 g_lock_wake_watchers(lock_ctx, state->key);
1041 NTSTATUS share_mode_do_locked(
1043 void (*fn)(const uint8_t *buf,
1045 bool *modified_dependent,
1046 void *private_data),
1049 TDB_DATA key = locking_key(&id);
1050 size_t data_refcount, key_refcount;
1051 struct share_mode_do_locked_state state = {
1052 .key = key, .fn = fn, .private_data = private_data,
1056 if (share_mode_lock_key_refcount == 0) {
1057 status = g_lock_lock(
1061 (struct timeval) { .tv_sec = 3600 });
1062 if (!NT_STATUS_IS_OK(status)) {
1063 DBG_DEBUG("g_lock_lock failed: %s\n",
1067 memcpy(share_mode_lock_key_data, key.dptr, key.dsize);
1070 SMB_ASSERT(share_mode_lock_key_refcount < SIZE_MAX);
1071 share_mode_lock_key_refcount += 1;
1073 key_refcount = share_mode_lock_key_refcount;
1074 data_refcount = static_share_mode_data_refcount;
1076 status = g_lock_dump(
1077 lock_ctx, key, share_mode_do_locked_fn, &state);
1078 if (!NT_STATUS_IS_OK(status)) {
1079 DBG_DEBUG("g_lock_dump failed: %s\n",
1083 SMB_ASSERT(data_refcount == static_share_mode_data_refcount);
1084 SMB_ASSERT(key_refcount == share_mode_lock_key_refcount);
1085 share_mode_lock_key_refcount -= 1;
1087 if (share_mode_lock_key_refcount == 0) {
1088 status = g_lock_unlock(lock_ctx, key);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 DBG_DEBUG("g_lock_unlock failed: %s\n",
1098 static void share_mode_wakeup_waiters_fn(
1101 bool *modified_dependent,
1104 *modified_dependent = true;
1107 NTSTATUS share_mode_wakeup_waiters(struct file_id id)
1109 return share_mode_do_locked(id, share_mode_wakeup_waiters_fn, NULL);
1112 struct fsp_update_share_mode_flags_state {
1113 struct files_struct *fsp;
1114 enum ndr_err_code ndr_err;
1115 uint64_t share_mode_epoch;
1116 uint16_t share_mode_flags;
1119 static void fsp_update_share_mode_flags_fn(
1120 struct server_id exclusive,
1122 const struct server_id *shared,
1123 const uint8_t *data,
1127 struct fsp_update_share_mode_flags_state *state = private_data;
1128 struct locking_tdb_data ltdb = { 0 };
1131 bool ok = locking_tdb_data_get(<db, data, datalen);
1133 DBG_DEBUG("locking_tdb_data_get failed\n");
1138 if (ltdb.share_mode_data_len == 0) {
1139 /* Likely a ctdb tombstone record, ignore it */
1143 if (exclusive.pid != 0) {
1144 struct server_id self =
1145 messaging_server_id(state->fsp->conn->sconn->msg_ctx);
1146 bool is_self = server_id_equal(&self, &exclusive);
1150 * If someone else is holding an exclusive
1151 * lock, pretend there's a read lease
1153 state->share_mode_flags = SHARE_MODE_LEASE_READ;
1158 state->ndr_err = get_share_mode_blob_header(ltdb.share_mode_data_buf,
1159 ltdb.share_mode_data_len,
1160 &state->share_mode_epoch,
1161 &state->share_mode_flags);
1164 static NTSTATUS fsp_update_share_mode_flags(struct files_struct *fsp)
1166 struct fsp_update_share_mode_flags_state state = { .fsp = fsp, };
1167 int seqnum = g_lock_seqnum(lock_ctx);
1171 if (seqnum == fsp->share_mode_flags_seqnum) {
1172 return NT_STATUS_OK;
1175 key = locking_key(&fsp->file_id);
1176 status = g_lock_dump(lock_ctx, key,
1177 fsp_update_share_mode_flags_fn,
1179 if (!NT_STATUS_IS_OK(status)) {
1180 /* no DBG_GET_SHARE_MODE_LOCK here! */
1181 DBG_ERR("g_lock_dump returned %s\n",
1186 if (!NDR_ERR_CODE_IS_SUCCESS(state.ndr_err)) {
1187 DBG_ERR("get_share_mode_blob_header returned %s\n",
1188 ndr_errstr(state.ndr_err));
1189 return ndr_map_error2ntstatus(state.ndr_err);
1192 fsp->share_mode_flags_seqnum = seqnum;
1193 fsp->share_mode_flags = state.share_mode_flags;
1195 return NT_STATUS_OK;
1198 bool file_has_read_lease(struct files_struct *fsp)
1202 status = fsp_update_share_mode_flags(fsp);
1203 if (!NT_STATUS_IS_OK(status)) {
1204 /* Safe default for leases */
1208 return (fsp->share_mode_flags & SHARE_MODE_LEASE_READ) != 0;
1211 NTTIME share_mode_changed_write_time(struct share_mode_lock *lck)
1213 return lck->data->changed_write_time;
1216 const char *share_mode_servicepath(struct share_mode_lock *lck)
1218 return lck->data->servicepath;
1221 char *share_mode_filename(TALLOC_CTX *mem_ctx, struct share_mode_lock *lck)
1223 struct share_mode_data *d = lck->data;
1224 bool has_stream = (d->stream_name != NULL);
1227 fname = talloc_asprintf(
1231 has_stream ? ":" : "",
1232 has_stream ? d->stream_name : "");
1236 char *share_mode_data_dump(
1237 TALLOC_CTX *mem_ctx, struct share_mode_lock *lck)
1239 struct ndr_print *p = talloc(mem_ctx, struct ndr_print);
1246 *p = (struct ndr_print) {
1247 .print = ndr_print_string_helper,
1249 .private_data = talloc_strdup(mem_ctx, ""),
1252 if (p->private_data == NULL) {
1257 ndr_print_share_mode_data(p, "SHARE_MODE_DATA", lck->data);
1259 ret = p->private_data;
1266 void share_mode_flags_get(
1267 struct share_mode_lock *lck,
1268 uint32_t *access_mask,
1269 uint32_t *share_mode,
1270 uint32_t *lease_type)
1272 uint16_t flags = lck->data->flags;
1274 if (access_mask != NULL) {
1276 ((flags & SHARE_MODE_ACCESS_READ) ?
1277 FILE_READ_DATA : 0) |
1278 ((flags & SHARE_MODE_ACCESS_WRITE) ?
1279 FILE_WRITE_DATA : 0) |
1280 ((flags & SHARE_MODE_ACCESS_DELETE) ?
1283 if (share_mode != NULL) {
1285 ((flags & SHARE_MODE_SHARE_READ) ?
1286 FILE_SHARE_READ : 0) |
1287 ((flags & SHARE_MODE_SHARE_WRITE) ?
1288 FILE_SHARE_WRITE : 0) |
1289 ((flags & SHARE_MODE_SHARE_DELETE) ?
1290 FILE_SHARE_DELETE : 0);
1292 if (lease_type != NULL) {
1294 ((flags & SHARE_MODE_LEASE_READ) ?
1295 SMB2_LEASE_READ : 0) |
1296 ((flags & SHARE_MODE_LEASE_WRITE) ?
1297 SMB2_LEASE_WRITE : 0) |
1298 ((flags & SHARE_MODE_LEASE_HANDLE) ?
1299 SMB2_LEASE_HANDLE : 0);
1303 void share_mode_flags_set(
1304 struct share_mode_lock *lck,
1305 uint32_t access_mask,
1306 uint32_t share_mode,
1307 uint32_t lease_type,
1310 struct share_mode_data *d = lck->data;
1313 flags |= (access_mask & (FILE_READ_DATA | FILE_EXECUTE)) ?
1314 SHARE_MODE_ACCESS_READ : 0;
1315 flags |= (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
1316 SHARE_MODE_ACCESS_WRITE : 0;
1317 flags |= (access_mask & (DELETE_ACCESS)) ?
1318 SHARE_MODE_ACCESS_DELETE : 0;
1320 flags |= (share_mode & FILE_SHARE_READ) ?
1321 SHARE_MODE_SHARE_READ : 0;
1322 flags |= (share_mode & FILE_SHARE_WRITE) ?
1323 SHARE_MODE_SHARE_WRITE : 0;
1324 flags |= (share_mode & FILE_SHARE_DELETE) ?
1325 SHARE_MODE_SHARE_DELETE : 0;
1327 flags |= (lease_type & SMB2_LEASE_READ) ?
1328 SHARE_MODE_LEASE_READ : 0;
1329 flags |= (lease_type & SMB2_LEASE_WRITE) ?
1330 SHARE_MODE_LEASE_WRITE : 0;
1331 flags |= (lease_type & SMB2_LEASE_HANDLE) ?
1332 SHARE_MODE_LEASE_HANDLE : 0;
1334 if (d->flags == flags) {
1338 if (modified != NULL) {
1345 struct share_mode_watch_state {
1347 struct server_id blocker;
1350 static void share_mode_watch_done(struct tevent_req *subreq);
1352 struct tevent_req *share_mode_watch_send(
1353 TALLOC_CTX *mem_ctx,
1354 struct tevent_context *ev,
1355 struct share_mode_lock *lck,
1356 struct server_id blocker)
1358 TDB_DATA key = locking_key(&lck->data->id);
1359 struct tevent_req *req = NULL, *subreq = NULL;
1360 struct share_mode_watch_state *state = NULL;
1362 req = tevent_req_create(
1363 mem_ctx, &state, struct share_mode_watch_state);
1368 subreq = g_lock_watch_data_send(state, ev, lock_ctx, key, blocker);
1369 if (tevent_req_nomem(subreq, req)) {
1370 return tevent_req_post(req, ev);
1372 tevent_req_set_callback(subreq, share_mode_watch_done, req);
1376 static void share_mode_watch_done(struct tevent_req *subreq)
1378 struct tevent_req *req = tevent_req_callback_data(
1379 subreq, struct tevent_req);
1380 struct share_mode_watch_state *state = tevent_req_data(
1381 req, struct share_mode_watch_state);
1384 status = g_lock_watch_data_recv(
1385 subreq, &state->blockerdead, &state->blocker);
1386 if (tevent_req_nterror(req, status)) {
1389 tevent_req_done(req);
1392 NTSTATUS share_mode_watch_recv(
1393 struct tevent_req *req, bool *blockerdead, struct server_id *blocker)
1395 struct share_mode_watch_state *state = tevent_req_data(
1396 req, struct share_mode_watch_state);
1399 if (tevent_req_is_nterror(req, &status)) {
1402 if (blockerdead != NULL) {
1403 *blockerdead = state->blockerdead;
1405 if (blocker != NULL) {
1406 *blocker = state->blocker;
1408 return NT_STATUS_OK;
1411 struct fetch_share_mode_unlocked_state {
1412 TALLOC_CTX *mem_ctx;
1414 struct share_mode_lock *lck;
1417 static void fetch_share_mode_unlocked_parser(
1418 struct server_id exclusive,
1420 const struct server_id *shared,
1421 const uint8_t *data,
1425 struct fetch_share_mode_unlocked_state *state = private_data;
1426 struct locking_tdb_data ltdb = { 0 };
1429 bool ok = locking_tdb_data_get(<db, data, datalen);
1431 DBG_DEBUG("locking_tdb_data_get failed\n");
1436 if (ltdb.share_mode_data_len == 0) {
1437 /* Likely a ctdb tombstone record, ignore it */
1441 state->lck = talloc(state->mem_ctx, struct share_mode_lock);
1442 if (state->lck == NULL) {
1443 DEBUG(0, ("talloc failed\n"));
1447 state->lck->data = parse_share_modes(
1450 ltdb.share_mode_data_buf,
1451 ltdb.share_mode_data_len);
1452 if (state->lck->data == NULL) {
1453 DBG_DEBUG("parse_share_modes failed\n");
1454 TALLOC_FREE(state->lck);
1458 /*******************************************************************
1459 Get a share_mode_lock without locking the database or reference
1460 counting. Used by smbstatus to display existing share modes.
1461 ********************************************************************/
1463 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
1466 struct fetch_share_mode_unlocked_state state = {
1470 TDB_DATA key = locking_key(&id);
1473 status = g_lock_dump(
1474 lock_ctx, key, fetch_share_mode_unlocked_parser, &state);
1475 if (!NT_STATUS_IS_OK(status)) {
1476 DBG_DEBUG("g_lock_dump failed: %s\n", nt_errstr(status));
1482 struct fetch_share_mode_state {
1484 struct share_mode_lock *lck;
1488 static void fetch_share_mode_fn(
1489 struct server_id exclusive,
1491 const struct server_id *shared,
1492 const uint8_t *data,
1494 void *private_data);
1495 static void fetch_share_mode_done(struct tevent_req *subreq);
1498 * @brief Get a share_mode_lock without locking or refcounting
1500 * This can be used in a clustered Samba environment where the async dbwrap
1501 * request is sent over a socket to the local ctdbd. If the send queue is full
1502 * and the caller was issuing multiple async dbwrap requests in a loop, the
1503 * caller knows it's probably time to stop sending requests for now and try
1506 * @param[in] mem_ctx The talloc memory context to use.
1508 * @param[in] ev The event context to work on.
1510 * @param[in] id The file id for the locking.tdb key
1512 * @param[out] queued This boolean out parameter tells the caller whether the
1513 * async request is blocked in a full send queue:
1515 * false := request is dispatched
1517 * true := send queue is full, request waiting to be
1520 * @return The new async request, NULL on error.
1522 struct tevent_req *fetch_share_mode_send(TALLOC_CTX *mem_ctx,
1523 struct tevent_context *ev,
1527 struct tevent_req *req = NULL, *subreq = NULL;
1528 struct fetch_share_mode_state *state = NULL;
1532 req = tevent_req_create(mem_ctx, &state,
1533 struct fetch_share_mode_state);
1539 subreq = g_lock_dump_send(
1544 fetch_share_mode_fn,
1546 if (tevent_req_nomem(subreq, req)) {
1547 return tevent_req_post(req, ev);
1549 tevent_req_set_callback(subreq, fetch_share_mode_done, req);
1553 static void fetch_share_mode_fn(
1554 struct server_id exclusive,
1556 const struct server_id *shared,
1557 const uint8_t *data,
1561 struct fetch_share_mode_state *state = talloc_get_type_abort(
1562 private_data, struct fetch_share_mode_state);
1563 struct locking_tdb_data ltdb = { 0 };
1566 bool ok = locking_tdb_data_get(<db, data, datalen);
1568 DBG_DEBUG("locking_tdb_data_get failed\n");
1573 if (ltdb.share_mode_data_len == 0) {
1574 /* Likely a ctdb tombstone record, ignore it */
1578 state->lck = talloc(state, struct share_mode_lock);
1579 if (state->lck == NULL) {
1580 DBG_WARNING("talloc failed\n");
1581 state->status = NT_STATUS_NO_MEMORY;
1585 state->lck->data = parse_share_modes(
1588 ltdb.share_mode_data_buf,
1589 ltdb.share_mode_data_len);
1590 if (state->lck->data == NULL) {
1591 DBG_DEBUG("parse_share_modes failed\n");
1592 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1593 TALLOC_FREE(state->lck);
1598 static void fetch_share_mode_done(struct tevent_req *subreq)
1600 struct tevent_req *req = tevent_req_callback_data(
1601 subreq, struct tevent_req);
1602 struct fetch_share_mode_state *state = tevent_req_data(
1603 req, struct fetch_share_mode_state);
1606 status = g_lock_dump_recv(subreq);
1607 TALLOC_FREE(subreq);
1608 if (tevent_req_nterror(req, status)) {
1611 if (tevent_req_nterror(req, state->status)) {
1614 tevent_req_done(req);
1617 NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
1618 TALLOC_CTX *mem_ctx,
1619 struct share_mode_lock **_lck)
1621 struct fetch_share_mode_state *state = tevent_req_data(
1622 req, struct fetch_share_mode_state);
1623 struct share_mode_lock *lck = NULL;
1627 if (tevent_req_is_nterror(req, &status)) {
1628 tevent_req_received(req);
1632 if (state->lck == NULL) {
1633 tevent_req_received(req);
1634 return NT_STATUS_NOT_FOUND;
1637 lck = talloc_move(mem_ctx, &state->lck);
1639 if (DEBUGLEVEL >= 10) {
1640 DBG_DEBUG("share_mode_data:\n");
1641 NDR_PRINT_DEBUG(share_mode_data, lck->data);
1645 tevent_req_received(req);
1646 return NT_STATUS_OK;
1649 struct share_mode_forall_state {
1651 int (*fn)(struct file_id fid,
1652 const struct share_mode_data *data,
1653 void *private_data);
1657 static void share_mode_forall_dump_fn(
1658 struct server_id exclusive,
1660 const struct server_id *shared,
1661 const uint8_t *data,
1665 struct share_mode_forall_state *state = private_data;
1667 struct locking_tdb_data ltdb = { 0 };
1669 struct share_mode_data *d;
1671 if (state->key.dsize != sizeof(fid)) {
1672 DBG_DEBUG("Got invalid key length %zu\n", state->key.dsize);
1675 memcpy(&fid, state->key.dptr, sizeof(fid));
1677 ok = locking_tdb_data_get(<db, data, datalen);
1679 DBG_DEBUG("locking_tdb_data_get() failed\n");
1683 d = parse_share_modes(
1686 ltdb.share_mode_data_buf,
1687 ltdb.share_mode_data_len);
1689 DBG_DEBUG("parse_share_modes() failed\n");
1693 state->fn(fid, d, state->private_data);
1697 static int share_mode_forall_fn(TDB_DATA key, void *private_data)
1699 struct share_mode_forall_state *state = private_data;
1704 status = g_lock_dump(
1705 lock_ctx, key, share_mode_forall_dump_fn, private_data);
1706 if (!NT_STATUS_IS_OK(status)) {
1707 DBG_GET_SHARE_MODE_LOCK(status,
1708 "g_lock_dump failed: %s\n",
1714 int share_mode_forall(int (*fn)(struct file_id fid,
1715 const struct share_mode_data *data,
1716 void *private_data),
1719 struct share_mode_forall_state state = {
1721 .private_data = private_data
1725 if (lock_ctx == NULL) {
1730 lock_ctx, share_mode_forall_fn, &state);
1732 DBG_ERR("g_lock_locks failed\n");
1737 struct share_entry_forall_state {
1739 const struct share_mode_data *data;
1740 int (*fn)(struct file_id fid,
1741 const struct share_mode_data *data,
1742 const struct share_mode_entry *entry,
1743 void *private_data);
1748 static bool share_entry_traverse_walker(
1749 struct share_mode_entry *e,
1753 struct share_entry_forall_state *state = private_data;
1755 state->ret = state->fn(
1756 state->fid, state->data, e, state->private_data);
1757 return (state->ret != 0);
1760 static int share_entry_traverse_fn(struct file_id fid,
1761 const struct share_mode_data *data,
1764 struct share_entry_forall_state *state = private_data;
1765 struct share_mode_lock lck = {
1766 .data = discard_const_p(struct share_mode_data, data)
1773 ok = share_mode_forall_entries(
1774 &lck, share_entry_traverse_walker, state);
1776 DBG_ERR("share_mode_forall_entries failed\n");
1783 /*******************************************************************
1784 Call the specified function on each entry under management by the
1786 ********************************************************************/
1788 int share_entry_forall(int (*fn)(struct file_id fid,
1789 const struct share_mode_data *data,
1790 const struct share_mode_entry *entry,
1791 void *private_data),
1794 struct share_entry_forall_state state = {
1795 .fn = fn, .private_data = private_data };
1797 return share_mode_forall(share_entry_traverse_fn, &state);
1800 static int share_mode_entry_cmp(
1801 struct server_id pid1,
1802 uint64_t share_file_id1,
1803 struct server_id pid2,
1804 uint64_t share_file_id2)
1808 cmp = server_id_cmp(&pid1, &pid2);
1812 if (share_file_id1 != share_file_id2) {
1813 return (share_file_id1 < share_file_id2) ? -1 : 1;
1818 static size_t share_mode_entry_find(
1819 const uint8_t *data,
1820 size_t num_share_modes,
1821 struct server_id pid,
1822 uint64_t share_file_id,
1823 struct share_mode_entry *e,
1826 ssize_t left, right, middle;
1830 if (num_share_modes == 0) {
1835 right = (num_share_modes-1);
1837 while (left <= right) {
1838 const uint8_t *middle_ptr = NULL;
1842 middle = left + ((right - left) / 2);
1843 middle_ptr = data + middle * SHARE_MODE_ENTRY_SIZE;
1845 DBG_DEBUG("left=%zu, right=%zu, middle=%zu, middle_ptr=%p\n",
1851 ok = share_mode_entry_get(middle_ptr, e);
1853 DBG_DEBUG("share_mode_entry_get failed\n");
1857 cmp = share_mode_entry_cmp(
1858 e->pid, e->share_file_id, pid, share_file_id);
1874 bool set_share_mode(struct share_mode_lock *lck,
1875 struct files_struct *fsp,
1879 const struct smb2_lease_key *lease_key,
1880 uint32_t share_access,
1881 uint32_t access_mask)
1883 struct share_mode_data *d = lck->data;
1884 TDB_DATA key = locking_key(&d->id);
1885 struct server_id my_pid = messaging_server_id(
1886 fsp->conn->sconn->msg_ctx);
1887 struct locking_tdb_data *ltdb = NULL;
1889 struct share_mode_entry e = { .pid.pid = 0 };
1890 struct share_mode_entry_buf e_buf;
1895 size_t num_dbufs = 0;
1897 status = locking_tdb_data_fetch(key, talloc_tos(), <db);
1898 if (!NT_STATUS_IS_OK(status)) {
1899 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
1903 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
1905 idx = share_mode_entry_find(
1906 ltdb->share_entries,
1907 ltdb->num_share_entries,
1909 fh_get_gen_id(fsp->fh),
1913 DBG_WARNING("Found duplicate share mode\n");
1914 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1918 e = (struct share_mode_entry) {
1920 .share_access = share_access,
1921 .private_options = fh_get_private_options(fsp->fh),
1922 .access_mask = access_mask,
1925 .time.tv_sec = fsp->open_time.tv_sec,
1926 .time.tv_usec = fsp->open_time.tv_usec,
1927 .share_file_id = fh_get_gen_id(fsp->fh),
1928 .uid = (uint32_t)uid,
1929 .flags = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
1930 SHARE_MODE_FLAG_POSIX_OPEN : 0,
1931 .name_hash = fsp->name_hash,
1934 if (op_type == LEASE_OPLOCK) {
1935 const struct GUID *client_guid = fsp_client_guid(fsp);
1936 e.client_guid = *client_guid;
1937 e.lease_key = *lease_key;
1940 ok = share_mode_entry_put(&e, &e_buf);
1942 DBG_DEBUG("share_mode_entry_put failed\n");
1943 status = NT_STATUS_INTERNAL_ERROR;
1947 DBG_DEBUG("idx=%zu, found=%d\n", idx, (int)found);
1950 dbufs[num_dbufs] = (TDB_DATA) {
1951 .dptr = discard_const_p(uint8_t, ltdb->share_entries),
1952 .dsize = idx * SHARE_MODE_ENTRY_SIZE,
1957 dbufs[num_dbufs] = (TDB_DATA) {
1958 .dptr = e_buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
1962 if (idx < ltdb->num_share_entries) {
1963 size_t num_after_idx = (ltdb->num_share_entries-idx);
1964 dbufs[num_dbufs] = (TDB_DATA) {
1965 .dptr = discard_const_p(uint8_t, ltdb->share_entries) +
1966 idx * SHARE_MODE_ENTRY_SIZE,
1967 .dsize = num_after_idx * SHARE_MODE_ENTRY_SIZE,
1974 for (i=0; i<num_dbufs; i++) {
1975 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
1982 if (num_dbufs == 1) {
1984 * Storing a fresh record with just one share entry
1990 * If there was any existing data in
1991 * ltdb->share_entries, it's now been
1992 * moved and we've split it into:
1995 * dbufs[0] -> old sorted data less than new_entry
1996 * dbufs[1] -> new_share_mode_entry
1997 * dbufs[2] -> old sorted_data greater than new entry.
1999 * So the old data inside ltdb->share_entries is
2002 * If we're storing a brand new entry the
2006 * dbufs[0] -> new_share_mode_entry
2008 * Either way we must set ltdb->share_entries = NULL
2009 * and ltdb->num_share_entries = 0 so that
2010 * locking_tdb_data_store() doesn't use it to
2011 * store any data. It's no longer there.
2014 ltdb->share_entries = NULL;
2015 ltdb->num_share_entries = 0;
2017 status = share_mode_data_ltdb_store(d, key, ltdb, dbufs, num_dbufs);
2018 if (!NT_STATUS_IS_OK(status)) {
2019 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2024 return NT_STATUS_IS_OK(status);
2027 static bool share_mode_for_one_entry(
2028 bool (*fn)(struct share_mode_entry *e,
2030 void *private_data),
2034 size_t *num_share_modes,
2038 .data = data + (*i) * SHARE_MODE_ENTRY_SIZE,
2039 .length = SHARE_MODE_ENTRY_SIZE,
2041 struct share_mode_entry e = {.pid.pid=0};
2042 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
2043 bool modified = false;
2045 struct server_id e_pid;
2046 uint64_t e_share_file_id;
2048 ndr_err = ndr_pull_struct_blob_all_noalloc(
2051 (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
2052 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2053 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
2057 if (DEBUGLEVEL >= 10) {
2058 DBG_DEBUG("entry[%zu]:\n", *i);
2059 NDR_PRINT_DEBUG(share_mode_entry, &e);
2063 e_share_file_id = e.share_file_id;
2065 stop = fn(&e, &modified, private_data);
2067 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2073 if (DEBUGLEVEL>=10) {
2074 DBG_DEBUG("share_mode_entry:\n");
2075 NDR_PRINT_DEBUG(share_mode_entry, &e);
2078 if (*i < *num_share_modes) {
2080 blob.data + SHARE_MODE_ENTRY_SIZE,
2081 (*num_share_modes - *i - 1) *
2082 SHARE_MODE_ENTRY_SIZE);
2084 *num_share_modes -= 1;
2090 if (DEBUGLEVEL>=10) {
2091 DBG_DEBUG("share_mode_entry:\n");
2092 NDR_PRINT_DEBUG(share_mode_entry, &e);
2096 * Make sure sorting order is kept intact
2098 SMB_ASSERT(server_id_equal(&e_pid, &e.pid));
2099 SMB_ASSERT(e_share_file_id == e.share_file_id);
2101 ndr_err = ndr_push_struct_into_fixed_blob(
2104 (ndr_push_flags_fn_t)
2105 ndr_push_share_mode_entry);
2106 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2107 DBG_WARNING("ndr_push_share_mode_entry "
2109 ndr_errstr(ndr_err));
2111 * Not much we can do, just ignore it
2127 bool share_mode_forall_entries(
2128 struct share_mode_lock *lck,
2129 bool (*fn)(struct share_mode_entry *e,
2131 void *private_data),
2134 struct share_mode_data *d = lck->data;
2135 TDB_DATA key = locking_key(&d->id);
2136 struct locking_tdb_data *ltdb = NULL;
2137 uint8_t *share_entries = NULL;
2138 size_t num_share_entries;
2139 bool writeback = false;
2144 status = locking_tdb_data_fetch(key, talloc_tos(), <db);
2145 if (!NT_STATUS_IS_OK(status)) {
2146 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2150 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2152 num_share_entries = ltdb->num_share_entries;
2153 share_entries = discard_const_p(uint8_t, ltdb->share_entries);
2156 while (i<num_share_entries) {
2157 stop = share_mode_for_one_entry(
2169 DBG_DEBUG("num_share_entries=%zu, writeback=%d\n",
2178 if ((ltdb->num_share_entries != 0 ) && (num_share_entries == 0)) {
2180 * This routine wiped all share entries, let
2181 * share_mode_data_store() delete the record
2186 ltdb->num_share_entries = num_share_entries;
2187 ltdb->share_entries = share_entries;
2189 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
2191 if (!NT_STATUS_IS_OK(status)) {
2192 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2200 struct share_mode_count_entries_state {
2201 size_t num_share_modes;
2205 static void share_mode_count_entries_fn(
2206 struct server_id exclusive,
2208 const struct server_id *shared,
2209 const uint8_t *data,
2213 struct share_mode_count_entries_state *state = private_data;
2214 struct locking_tdb_data ltdb = { 0 };
2217 ok = locking_tdb_data_get(<db, data, datalen);
2219 DBG_WARNING("locking_tdb_data_get failed for %zu\n", datalen);
2220 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
2223 state->num_share_modes = ltdb.num_share_entries;
2224 state->status = NT_STATUS_OK;
2227 NTSTATUS share_mode_count_entries(struct file_id fid, size_t *num_share_modes)
2229 struct share_mode_count_entries_state state = {
2230 .status = NT_STATUS_NOT_FOUND,
2234 status = g_lock_dump(
2237 share_mode_count_entries_fn,
2239 if (!NT_STATUS_IS_OK(status)) {
2240 DBG_DEBUG("g_lock_dump failed: %s\n",
2244 if (!NT_STATUS_IS_OK(state.status)) {
2245 DBG_DEBUG("share_mode_count_entries_fn failed: %s\n",
2246 nt_errstr(state.status));
2247 return state.status;
2250 *num_share_modes = state.num_share_modes;
2251 return NT_STATUS_OK;
2254 static bool share_mode_entry_do(
2255 struct share_mode_lock *lck,
2256 struct server_id pid,
2257 uint64_t share_file_id,
2258 void (*fn)(struct share_mode_entry *e,
2259 size_t num_share_modes,
2261 void *private_data),
2264 struct share_mode_data *d = lck->data;
2265 TDB_DATA key = locking_key(&d->id);
2266 struct locking_tdb_data *ltdb = NULL;
2269 bool modified = false;
2270 struct share_mode_entry e;
2271 uint8_t *e_ptr = NULL;
2275 status = locking_tdb_data_fetch(key, talloc_tos(), <db);
2276 if (!NT_STATUS_IS_OK(status)) {
2277 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2281 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2283 idx = share_mode_entry_find(
2284 ltdb->share_entries,
2285 ltdb->num_share_entries,
2291 DBG_WARNING("Did not find share mode entry for %"PRIu64"\n",
2296 if (DEBUGLEVEL>=10) {
2297 DBG_DEBUG("entry[%zu]:\n", idx);
2298 NDR_PRINT_DEBUG(share_mode_entry, &e);
2301 fn(&e, ltdb->num_share_entries, &modified, private_data);
2303 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2308 if (!e.stale && !modified) {
2313 e_ptr = discard_const_p(uint8_t, ltdb->share_entries) +
2314 idx * SHARE_MODE_ENTRY_SIZE;
2318 * Move the rest down one entry
2320 size_t behind = ltdb->num_share_entries - idx - 1;
2323 e_ptr + SHARE_MODE_ENTRY_SIZE,
2324 behind * SHARE_MODE_ENTRY_SIZE);
2326 ltdb->num_share_entries -= 1;
2328 if (ltdb->num_share_entries == 0) {
2330 * Tell share_mode_lock_destructor() to delete
2336 if (DEBUGLEVEL>=10) {
2337 DBG_DEBUG("share_mode_entry:\n");
2338 NDR_PRINT_DEBUG(share_mode_entry, &e);
2341 struct share_mode_entry_buf buf;
2344 if (ltdb->num_share_entries != 1) {
2346 * Make sure the sorting order stays intact
2348 SMB_ASSERT(server_id_equal(&e.pid, &pid));
2349 SMB_ASSERT(e.share_file_id == share_file_id);
2352 ok = share_mode_entry_put(&e, &buf);
2354 DBG_DEBUG("share_mode_entry_put failed\n");
2357 memcpy(e_ptr, buf.buf, SHARE_MODE_ENTRY_SIZE);
2360 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
2361 if (!NT_STATUS_IS_OK(status)) {
2362 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2373 struct del_share_mode_state {
2377 static void del_share_mode_fn(
2378 struct share_mode_entry *e,
2379 size_t num_share_modes,
2383 struct del_share_mode_state *state = private_data;
2388 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
2390 struct del_share_mode_state state = { .ok = false };
2393 ok = share_mode_entry_do(
2395 messaging_server_id(fsp->conn->sconn->msg_ctx),
2396 fh_get_gen_id(fsp->fh),
2400 DBG_DEBUG("share_mode_entry_do failed\n");
2404 DBG_DEBUG("del_share_mode_fn failed\n");
2410 struct remove_share_oplock_state {
2414 static void remove_share_oplock_fn(
2415 struct share_mode_entry *e,
2416 size_t num_share_modes,
2420 struct remove_share_oplock_state *state = private_data;
2422 e->op_type = NO_OPLOCK;
2427 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2429 struct remove_share_oplock_state state = { .ok = false };
2432 ok = share_mode_entry_do(
2434 messaging_server_id(fsp->conn->sconn->msg_ctx),
2435 fh_get_gen_id(fsp->fh),
2436 remove_share_oplock_fn,
2439 DBG_DEBUG("share_mode_entry_do failed\n");
2443 DBG_DEBUG("remove_share_oplock_fn failed\n");
2447 if (fsp->oplock_type == LEASE_OPLOCK) {
2448 remove_lease_if_stale(
2450 fsp_client_guid(fsp),
2451 &fsp->lease->lease.lease_key);
2454 share_mode_wakeup_waiters(fsp->file_id);
2459 struct downgrade_share_oplock_state {
2463 static void downgrade_share_oplock_fn(
2464 struct share_mode_entry *e,
2465 size_t num_share_modes,
2469 struct downgrade_share_oplock_state *state = private_data;
2471 e->op_type = LEVEL_II_OPLOCK;
2476 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2478 struct downgrade_share_oplock_state state = { .ok = false };
2481 ok = share_mode_entry_do(
2483 messaging_server_id(fsp->conn->sconn->msg_ctx),
2484 fh_get_gen_id(fsp->fh),
2485 downgrade_share_oplock_fn,
2488 DBG_DEBUG("share_mode_entry_do failed\n");
2492 DBG_DEBUG("downgrade_share_oplock_fn failed\n");
2496 lck->data->flags |= SHARE_MODE_LEASE_READ;
2497 lck->data->modified = true;
2502 bool mark_share_mode_disconnected(struct share_mode_lock *lck,
2503 struct files_struct *fsp)
2505 struct server_id disconnected_pid = { .pid = 0 };
2508 if (fsp->op == NULL) {
2511 if (!fsp->op->global->durable) {
2515 server_id_set_disconnected(&disconnected_pid);
2517 ok = reset_share_mode_entry(
2519 messaging_server_id(fsp->conn->sconn->msg_ctx),
2520 fh_get_gen_id(fsp->fh),
2523 fsp->op->global->open_persistent_id);
2528 bool reset_share_mode_entry(
2529 struct share_mode_lock *lck,
2530 struct server_id old_pid,
2531 uint64_t old_share_file_id,
2532 struct server_id new_pid,
2534 uint64_t new_share_file_id)
2536 struct share_mode_data *d = lck->data;
2537 TDB_DATA key = locking_key(&d->id);
2538 struct locking_tdb_data *ltdb = NULL;
2539 struct share_mode_entry e;
2540 struct share_mode_entry_buf e_buf;
2546 status = locking_tdb_data_fetch(key, talloc_tos(), <db);
2547 if (!NT_STATUS_IS_OK(status)) {
2548 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2553 if (ltdb->num_share_entries != 1) {
2554 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2558 ok = share_mode_entry_get(ltdb->share_entries, &e);
2560 DBG_WARNING("share_mode_entry_get failed\n");
2564 cmp = share_mode_entry_cmp(
2565 old_pid, old_share_file_id, e.pid, e.share_file_id);
2567 struct server_id_buf tmp1, tmp2;
2568 DBG_WARNING("Expected pid=%s, file_id=%"PRIu64", "
2569 "got pid=%s, file_id=%"PRIu64"\n",
2570 server_id_str_buf(old_pid, &tmp1),
2572 server_id_str_buf(e.pid, &tmp2),
2578 if (new_mid != UINT64_MAX) {
2581 e.share_file_id = new_share_file_id;
2583 ok = share_mode_entry_put(&e, &e_buf);
2585 DBG_WARNING("share_mode_entry_put failed\n");
2589 ltdb->share_entries = e_buf.buf;
2593 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
2594 if (!NT_STATUS_IS_OK(status)) {
2595 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2607 * @brief Run @fn protected with G_LOCK_WRITE in the given file_id
2609 * @fn is NOT allowed to call SMB_VFS_* or similar functions,
2610 * which may block for some time in the kernel.
2612 * There must be at least one share_mode_entry, otherwise
2613 * NT_STATUS_NOT_FOUND is returned.
2615 * @param[in] id The key for the share_mode record.
2616 * @param[in] fn The function to run under the g_lock.
2617 * @param[in] private_date A private pointer passed to @fn.
2619 NTSTATUS _share_mode_do_locked_vfs_denied(
2621 share_mode_do_locked_vfs_fn_t fn,
2623 const char *location)
2625 struct smb_vfs_deny_state vfs_deny = {};
2626 struct share_mode_lock *lck = NULL;
2628 lck = get_existing_share_mode_lock(talloc_tos(), id);
2630 NTSTATUS status = NT_STATUS_NOT_FOUND;
2631 DBG_DEBUG("get_existing_share_mode_lock failed: %s\n",
2636 _smb_vfs_deny_push(&vfs_deny, location);
2637 fn(lck, private_data);
2638 _smb_vfs_deny_pop(&vfs_deny, location);
2642 return NT_STATUS_OK;
2646 * @brief Run @fn protected with G_LOCK_WRITE in the given file_id
2648 * @fn is allowed to call SMB_VFS_* or similar functions,
2649 * which may block for some time in the kernel.
2651 * There must be at least one share_mode_entry, otherwise
2652 * NT_STATUS_NOT_FOUND is returned.
2654 * @param[in] id The key for the share_mode record.
2655 * @param[in] fn The function to run under the g_lock.
2656 * @param[in] private_date A private pointer passed to @fn.
2658 NTSTATUS _share_mode_do_locked_vfs_allowed(
2660 share_mode_do_locked_vfs_fn_t fn,
2662 const char *location)
2664 struct share_mode_lock *lck = NULL;
2666 smb_vfs_assert_allowed();
2668 lck = get_existing_share_mode_lock(talloc_tos(), id);
2670 NTSTATUS status = NT_STATUS_NOT_FOUND;
2671 DBG_DEBUG("get_existing_share_mode_lock failed: %s\n",
2676 fn(lck, private_data);
2680 return NT_STATUS_OK;