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 "lib/util/time_basic.h"
40 #include "system/filesys.h"
41 #include "lib/util/server_id.h"
42 #include "share_mode_lock.h"
43 #include "share_mode_lock_private.h"
44 #include "locking/proto.h"
45 #include "smbd/globals.h"
46 #include "dbwrap/dbwrap.h"
47 #include "dbwrap/dbwrap_open.h"
48 #include "dbwrap/dbwrap_private.h"
49 #include "../libcli/security/security.h"
53 #include "../librpc/gen_ndr/ndr_open_files.h"
54 #include "source3/lib/dbwrap/dbwrap_watch.h"
55 #include "locking/leases_db.h"
56 #include "../lib/util/memcache.h"
57 #include "lib/util/tevent_ntstatus.h"
59 #include "smbd/fd_handle.h"
60 #include "lib/global_contexts.h"
63 #define DBGC_CLASS DBGC_LOCKING
65 #define DBG_GET_SHARE_MODE_LOCK(__status, ...) \
67 NT_STATUS_EQUAL(__status, NT_STATUS_NOT_FOUND) ? \
68 DBGLVL_DEBUG : DBGLVL_ERR, \
71 struct share_mode_lock
{
73 struct share_mode_data
*cached_data
;
76 /* the locking database handle */
77 static struct g_lock_ctx
*lock_ctx
;
79 static bool locking_init_internal(bool read_only
)
81 struct db_context
*backend
;
86 if (lock_ctx
!= NULL
) {
90 db_path
= lock_path(talloc_tos(), "locking.tdb");
91 if (db_path
== NULL
) {
95 backend
= db_open(NULL
, db_path
,
96 SMB_OPEN_DATABASE_TDB_HASH_SIZE
,
100 TDB_INCOMPATIBLE_HASH
|
102 read_only
?O_RDONLY
:O_RDWR
|O_CREAT
, 0644,
103 DBWRAP_LOCK_ORDER_NONE
,
105 TALLOC_FREE(db_path
);
107 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
111 lock_ctx
= g_lock_ctx_init_backend(
112 NULL
, global_messaging_context(), &backend
);
113 if (lock_ctx
== NULL
) {
114 TALLOC_FREE(backend
);
117 g_lock_set_lock_order(lock_ctx
, DBWRAP_LOCK_ORDER_1
);
119 if (!posix_locking_init(read_only
)) {
120 TALLOC_FREE(lock_ctx
);
127 bool locking_init(void)
129 return locking_init_internal(false);
132 bool locking_init_readonly(void)
134 return locking_init_internal(true);
137 /*******************************************************************
138 Deinitialize the share_mode management.
139 ******************************************************************/
141 bool locking_end(void)
144 TALLOC_FREE(lock_ctx
);
148 /*******************************************************************
149 Form a static locking key for a dev/inode pair.
150 ******************************************************************/
152 static TDB_DATA
locking_key(const struct file_id
*id
)
154 return make_tdb_data((const uint8_t *)id
, sizeof(*id
));
157 /*******************************************************************
158 Share mode cache utility functions that store/delete/retrieve
159 entries from memcache.
161 For now share the statcache (global cache) memory space. If
162 a lock record gets orphaned (which shouldn't happen as we're
163 using the same locking_key data as lookup) it will eventually
164 fall out of the cache via the normal LRU trim mechanism. If
165 necessary we can always make this a separate (smaller) cache.
166 ******************************************************************/
168 static DATA_BLOB
memcache_key(const struct file_id
*id
)
170 return data_blob_const((const void *)id
, sizeof(*id
));
173 static void share_mode_memcache_store(struct share_mode_data
*d
)
175 const DATA_BLOB key
= memcache_key(&d
->id
);
176 struct file_id_buf idbuf
;
178 DBG_DEBUG("stored entry for file %s epoch %"PRIx64
" key %s\n",
180 d
->unique_content_epoch
,
181 file_id_str_buf(d
->id
, &idbuf
));
183 /* Ensure everything stored in the cache is pristine. */
184 SMB_ASSERT(!d
->modified
);
185 SMB_ASSERT(!d
->not_stored
);
188 * Ensure the memory going into the cache
189 * doesn't have a destructor so it can be
190 * cleanly evicted by the memcache LRU
193 talloc_set_destructor(d
, NULL
);
195 /* Cache will own d after this call. */
196 memcache_add_talloc(NULL
,
197 SHARE_MODE_LOCK_CACHE
,
203 * NB. We use ndr_pull_hyper on a stack-created
204 * struct ndr_pull with no talloc allowed, as we
205 * need this to be really fast as an ndr-peek into
206 * the first 10 bytes of the blob.
209 static enum ndr_err_code
get_share_mode_blob_header(
210 const uint8_t *buf
, size_t buflen
, uint64_t *pepoch
, uint16_t *pflags
)
212 struct ndr_pull ndr
= {
213 .data
= discard_const_p(uint8_t, buf
),
216 NDR_CHECK(ndr_pull_hyper(&ndr
, NDR_SCALARS
, pepoch
));
217 NDR_CHECK(ndr_pull_uint16(&ndr
, NDR_SCALARS
, pflags
));
218 return NDR_ERR_SUCCESS
;
221 static int share_mode_data_nofree_destructor(struct share_mode_data
*d
)
226 static struct share_mode_data
*share_mode_memcache_fetch(
232 const DATA_BLOB key
= memcache_key(&id
);
233 enum ndr_err_code ndr_err
;
234 struct share_mode_data
*d
;
235 uint64_t unique_content_epoch
;
238 struct file_id_buf idbuf
;
240 ptr
= memcache_lookup_talloc(NULL
,
241 SHARE_MODE_LOCK_CACHE
,
244 DBG_DEBUG("failed to find entry for key %s\n",
245 file_id_str_buf(id
, &idbuf
));
248 /* sequence number key is at start of blob. */
249 ndr_err
= get_share_mode_blob_header(
250 buf
, buflen
, &unique_content_epoch
, &flags
);
251 if (ndr_err
!= NDR_ERR_SUCCESS
) {
252 /* Bad blob. Remove entry. */
253 DBG_DEBUG("bad blob %u key %s\n",
254 (unsigned int)ndr_err
,
255 file_id_str_buf(id
, &idbuf
));
256 memcache_delete(NULL
,
257 SHARE_MODE_LOCK_CACHE
,
262 d
= (struct share_mode_data
*)ptr
;
263 if (d
->unique_content_epoch
!= unique_content_epoch
) {
264 DBG_DEBUG("epoch changed (cached %"PRIx64
") (new %"PRIx64
") "
266 d
->unique_content_epoch
,
267 unique_content_epoch
,
268 file_id_str_buf(id
, &idbuf
));
269 /* Cache out of date. Remove entry. */
270 memcache_delete(NULL
,
271 SHARE_MODE_LOCK_CACHE
,
276 /* Move onto mem_ctx. */
277 d
= talloc_move(mem_ctx
, &ptr
);
280 * Now we own d, prevent the cache from freeing it
281 * when we delete the entry.
283 talloc_set_destructor(d
, share_mode_data_nofree_destructor
);
285 /* Remove from the cache. We own it now. */
286 memcache_delete(NULL
,
287 SHARE_MODE_LOCK_CACHE
,
290 /* And reset the destructor to none. */
291 talloc_set_destructor(d
, NULL
);
293 DBG_DEBUG("fetched entry for file %s epoch %"PRIx64
" key %s\n",
295 d
->unique_content_epoch
,
296 file_id_str_buf(id
, &idbuf
));
302 * 132 is the sizeof an ndr-encoded struct share_mode_entry_buf.
303 * Reading/writing entries will immediately error out if this
304 * size differs (push/pull is done without allocs).
307 struct share_mode_entry_buf
{
310 #define SHARE_MODE_ENTRY_SIZE (sizeof(struct share_mode_entry_buf))
312 static bool share_mode_entry_put(
313 const struct share_mode_entry
*e
,
314 struct share_mode_entry_buf
*dst
)
316 DATA_BLOB blob
= { .data
= dst
->buf
, .length
= sizeof(dst
->buf
) };
317 enum ndr_err_code ndr_err
;
319 if (DEBUGLEVEL
>=10) {
320 DBG_DEBUG("share_mode_entry:\n");
321 NDR_PRINT_DEBUG(share_mode_entry
, discard_const_p(void, e
));
324 ndr_err
= ndr_push_struct_into_fixed_blob(
327 (ndr_push_flags_fn_t
)ndr_push_share_mode_entry
);
328 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
329 DBG_WARNING("ndr_push_share_mode_entry failed: %s\n",
330 ndr_errstr(ndr_err
));
337 static bool share_mode_entry_get(
338 const uint8_t ptr
[SHARE_MODE_ENTRY_SIZE
], struct share_mode_entry
*e
)
340 enum ndr_err_code ndr_err
= NDR_ERR_SUCCESS
;
342 .data
= discard_const_p(uint8_t, ptr
),
343 .length
= SHARE_MODE_ENTRY_SIZE
,
346 ndr_err
= ndr_pull_struct_blob_all_noalloc(
347 &blob
, e
, (ndr_pull_flags_fn_t
)ndr_pull_share_mode_entry
);
348 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
349 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
356 * locking.tdb records consist of
358 * uint32_t share_mode_data_len
359 * uint8_t [share_mode_data] This is struct share_mode_data in NDR
361 * 0 [SHARE_MODE_ENTRY_SIZE] Sorted array of share modes,
362 * 1 [SHARE_MODE_ENTRY_SIZE] filling up the rest of the data in the
363 * 2 [SHARE_MODE_ENTRY_SIZE] g_lock.c maintained record in locking.tdb
366 struct locking_tdb_data
{
367 const uint8_t *share_mode_data_buf
;
368 size_t share_mode_data_len
;
369 const uint8_t *share_entries
;
370 size_t num_share_entries
;
373 static bool locking_tdb_data_get(
374 struct locking_tdb_data
*data
, const uint8_t *buf
, size_t buflen
)
376 uint32_t share_mode_data_len
, share_entries_len
;
379 *data
= (struct locking_tdb_data
) { 0 };
382 if (buflen
< sizeof(uint32_t)) {
386 share_mode_data_len
= PULL_LE_U32(buf
, 0);
388 buf
+= sizeof(uint32_t);
389 buflen
-= sizeof(uint32_t);
391 if (buflen
< share_mode_data_len
) {
395 share_entries_len
= buflen
- share_mode_data_len
;
397 if ((share_entries_len
% SHARE_MODE_ENTRY_SIZE
) != 0) {
401 *data
= (struct locking_tdb_data
) {
402 .share_mode_data_buf
= buf
,
403 .share_mode_data_len
= share_mode_data_len
,
404 .share_entries
= buf
+ share_mode_data_len
,
405 .num_share_entries
= share_entries_len
/ SHARE_MODE_ENTRY_SIZE
,
411 struct locking_tdb_data_fetch_state
{
417 static void locking_tdb_data_fetch_fn(
418 struct server_id exclusive
,
420 const struct server_id
*shared
,
425 struct locking_tdb_data_fetch_state
*state
= private_data
;
426 state
->datalen
= datalen
;
427 state
->data
= talloc_memdup(state
->mem_ctx
, data
, datalen
);
430 static NTSTATUS
locking_tdb_data_fetch(
431 TDB_DATA key
, TALLOC_CTX
*mem_ctx
, struct locking_tdb_data
**ltdb
)
433 struct locking_tdb_data_fetch_state state
= { 0 };
434 struct locking_tdb_data
*result
= NULL
;
438 result
= talloc_zero(mem_ctx
, struct locking_tdb_data
);
439 if (result
== NULL
) {
440 return NT_STATUS_NO_MEMORY
;
442 state
.mem_ctx
= result
;
444 status
= g_lock_dump(lock_ctx
, key
, locking_tdb_data_fetch_fn
, &state
);
446 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
448 * Just return an empty record
452 if (!NT_STATUS_IS_OK(status
)) {
453 DBG_ERR("g_lock_dump failed: %s\n",
457 if (state
.datalen
== 0) {
461 ok
= locking_tdb_data_get(result
, state
.data
, state
.datalen
);
463 DBG_ERR("locking_tdb_data_get failed for %zu bytes\n",
466 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
474 static NTSTATUS
locking_tdb_data_store(
476 const struct locking_tdb_data
*ltdb
,
477 const TDB_DATA
*share_mode_dbufs
,
478 size_t num_share_mode_dbufs
)
480 uint8_t share_mode_data_len_buf
[4];
481 TDB_DATA dbufs
[num_share_mode_dbufs
+3];
484 if ((ltdb
->share_mode_data_len
== 0) &&
485 (ltdb
->num_share_entries
== 0) &&
486 (num_share_mode_dbufs
== 0)) {
490 status
= g_lock_write_data(lock_ctx
, key
, NULL
, 0);
491 if (!NT_STATUS_IS_OK(status
)) {
492 DBG_ERR("g_lock_writev_data() failed: %s\n",
498 PUSH_LE_U32(share_mode_data_len_buf
, 0, ltdb
->share_mode_data_len
);
500 dbufs
[0] = (TDB_DATA
) {
501 .dptr
= share_mode_data_len_buf
,
502 .dsize
= sizeof(share_mode_data_len_buf
),
504 dbufs
[1] = (TDB_DATA
) {
505 .dptr
= discard_const_p(uint8_t, ltdb
->share_mode_data_buf
),
506 .dsize
= ltdb
->share_mode_data_len
,
509 if (ltdb
->num_share_entries
> SIZE_MAX
/SHARE_MODE_ENTRY_SIZE
) {
511 return NT_STATUS_BUFFER_OVERFLOW
;
513 dbufs
[2] = (TDB_DATA
) {
514 .dptr
= discard_const_p(uint8_t, ltdb
->share_entries
),
515 .dsize
= ltdb
->num_share_entries
* SHARE_MODE_ENTRY_SIZE
,
518 if (num_share_mode_dbufs
!= 0) {
521 num_share_mode_dbufs
* sizeof(TDB_DATA
));
524 status
= g_lock_writev_data(lock_ctx
, key
, dbufs
, ARRAY_SIZE(dbufs
));
525 if (!NT_STATUS_IS_OK(status
)) {
526 DBG_ERR("g_lock_writev_data() failed: %s\n",
532 /*******************************************************************
533 Get all share mode entries for a dev/inode pair.
534 ********************************************************************/
536 static struct share_mode_data
*parse_share_modes(
542 struct share_mode_data
*d
;
543 enum ndr_err_code ndr_err
;
546 /* See if we already have a cached copy of this key. */
547 d
= share_mode_memcache_fetch(mem_ctx
, id
, buf
, buflen
);
552 d
= talloc(mem_ctx
, struct share_mode_data
);
554 DEBUG(0, ("talloc failed\n"));
559 .data
= discard_const_p(uint8_t, buf
),
562 ndr_err
= ndr_pull_struct_blob_all(
563 &blob
, d
, d
, (ndr_pull_flags_fn_t
)ndr_pull_share_mode_data
);
564 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
565 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
566 ndr_errstr(ndr_err
));
570 if (DEBUGLEVEL
>= 10) {
571 DEBUG(10, ("parse_share_modes:\n"));
572 NDR_PRINT_DEBUG(share_mode_data
, d
);
581 static NTSTATUS
share_mode_data_ltdb_store(struct share_mode_data
*d
,
583 struct locking_tdb_data
*ltdb
,
584 const TDB_DATA
*share_mode_dbufs
,
585 size_t num_share_mode_dbufs
)
587 DATA_BLOB blob
= { 0 };
591 DBG_DEBUG("share_mode_data not modified\n");
595 d
->unique_content_epoch
= generate_unique_u64(d
->unique_content_epoch
);
597 if (DEBUGLEVEL
>= 10) {
599 NDR_PRINT_DEBUG(share_mode_data
, d
);
602 if (ltdb
->num_share_entries
!= 0 || num_share_mode_dbufs
!= 0) {
603 enum ndr_err_code ndr_err
;
605 ndr_err
= ndr_push_struct_blob(
609 (ndr_push_flags_fn_t
)ndr_push_share_mode_data
);
610 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
611 DBG_ERR("ndr_push_share_mode_data failed: %s\n",
612 ndr_errstr(ndr_err
));
613 return ndr_map_error2ntstatus(ndr_err
);
617 ltdb
->share_mode_data_buf
= blob
.data
;
618 ltdb
->share_mode_data_len
= blob
.length
;
621 status
= locking_tdb_data_store(key
,
624 num_share_mode_dbufs
);
625 if (!NT_STATUS_IS_OK(status
)) {
626 DBG_ERR("locking_tdb_data_store failed: %s\n",
632 d
->not_stored
= (ltdb
->share_mode_data_len
== 0);
637 /*******************************************************************
638 If modified, store the share_mode_data back into the database.
639 ********************************************************************/
641 static NTSTATUS
share_mode_data_store(struct share_mode_data
*d
)
643 TDB_DATA key
= locking_key(&d
->id
);
644 struct locking_tdb_data
*ltdb
= NULL
;
648 DBG_DEBUG("not modified\n");
652 if (DEBUGLEVEL
>= 10) {
654 NDR_PRINT_DEBUG(share_mode_data
, d
);
657 status
= locking_tdb_data_fetch(key
, d
, <db
);
658 if (!NT_STATUS_IS_OK(status
)) {
659 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
664 status
= share_mode_data_ltdb_store(d
, key
, ltdb
, NULL
, 0);
666 if (!NT_STATUS_IS_OK(status
)) {
667 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
675 /*******************************************************************
676 Allocate a new share_mode_data struct, mark it unmodified.
677 fresh is set to note that currently there is no database entry.
678 ********************************************************************/
680 static struct share_mode_data
*fresh_share_mode_lock(
681 TALLOC_CTX
*mem_ctx
, const char *servicepath
,
682 const struct smb_filename
*smb_fname
,
683 const struct timespec
*old_write_time
)
685 struct share_mode_data
*d
;
687 if ((servicepath
== NULL
) || (smb_fname
== NULL
) ||
688 (old_write_time
== NULL
)) {
692 d
= talloc_zero(mem_ctx
, struct share_mode_data
);
696 d
->unique_content_epoch
= generate_unique_u64(0);
698 d
->base_name
= talloc_strdup(d
, smb_fname
->base_name
);
699 if (d
->base_name
== NULL
) {
702 if (smb_fname
->stream_name
!= NULL
) {
703 d
->stream_name
= talloc_strdup(d
, smb_fname
->stream_name
);
704 if (d
->stream_name
== NULL
) {
708 d
->servicepath
= talloc_strdup(d
, servicepath
);
709 if (d
->servicepath
== NULL
) {
712 d
->old_write_time
= full_timespec_to_nt_time(old_write_time
);
713 d
->flags
= SHARE_MODE_SHARE_DELETE
|
714 SHARE_MODE_SHARE_WRITE
|
715 SHARE_MODE_SHARE_READ
;
717 d
->not_stored
= true;
720 DEBUG(0, ("talloc failed\n"));
726 * Key that's locked with g_lock
728 static struct file_id share_mode_lock_key_id
= {};
729 static TDB_DATA share_mode_lock_key
= {
730 .dptr
= (uint8_t *)&share_mode_lock_key_id
,
731 .dsize
= sizeof(share_mode_lock_key_id
),
733 static size_t share_mode_lock_key_refcount
= 0;
736 * We can only ever have one share mode locked. Use a static
737 * share_mode_data pointer that is shared by multiple nested
738 * share_mode_lock structures, explicitly refcounted.
740 static struct share_mode_data
*static_share_mode_data
= NULL
;
742 /*******************************************************************
743 Either fetch a share mode from the database, or allocate a fresh
744 one if the record doesn't exist.
745 ********************************************************************/
747 struct get_static_share_mode_data_state
{
750 const char *servicepath
;
751 const struct smb_filename
*smb_fname
;
752 const struct timespec
*old_write_time
;
756 static void get_static_share_mode_data_fn(
757 struct server_id exclusive
,
759 const struct server_id
*shared
,
764 struct get_static_share_mode_data_state
*state
= private_data
;
765 struct share_mode_data
*d
= NULL
;
766 struct locking_tdb_data ltdb
= { 0 };
771 ok
= locking_tdb_data_get(<db
, data
, datalen
);
773 DBG_ERR("locking_tdb_data_get failed\n");
774 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
779 if (ltdb
.share_mode_data_len
== 0) {
780 if (state
->smb_fname
== NULL
) {
781 state
->status
= NT_STATUS_NOT_FOUND
;
784 d
= fresh_share_mode_lock(
788 state
->old_write_time
);
790 state
->status
= NT_STATUS_NO_MEMORY
;
794 d
= parse_share_modes(
797 ltdb
.share_mode_data_buf
,
798 ltdb
.share_mode_data_len
);
800 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
806 static_share_mode_data
= d
;
809 static NTSTATUS
get_static_share_mode_data(
811 const char *servicepath
,
812 const struct smb_filename
*smb_fname
,
813 const struct timespec
*old_write_time
)
815 struct get_static_share_mode_data_state state
= {
818 .servicepath
= servicepath
,
819 .smb_fname
= smb_fname
,
820 .old_write_time
= old_write_time
,
824 SMB_ASSERT(static_share_mode_data
== NULL
);
826 status
= g_lock_dump(
829 get_static_share_mode_data_fn
,
831 if (!NT_STATUS_IS_OK(status
)) {
832 DBG_GET_SHARE_MODE_LOCK(status
,
833 "g_lock_dump failed: %s\n",
837 if (!NT_STATUS_IS_OK(state
.status
)) {
838 DBG_GET_SHARE_MODE_LOCK(status
,
839 "get_static_share_mode_data_fn failed: %s\n",
840 nt_errstr(state
.status
));
847 struct file_id
share_mode_lock_file_id(const struct share_mode_lock
*lck
)
852 NTSTATUS
share_mode_lock_access_private_data(struct share_mode_lock
*lck
,
853 struct share_mode_data
**data
)
856 * For now we always have lck->cached_data,
857 * but we may change that in future.
859 SMB_ASSERT(lck
->cached_data
!= NULL
);
860 *data
= lck
->cached_data
;
864 /*******************************************************************
865 Get a share_mode_lock, Reference counted to allow nested calls.
866 ********************************************************************/
868 static int share_mode_lock_destructor(struct share_mode_lock
*lck
);
870 static NTSTATUS
get_share_mode_lock_internal(
872 const char *servicepath
,
873 const struct smb_filename
*smb_fname
,
874 const struct timespec
*old_write_time
,
875 struct share_mode_lock
*lck
)
879 *lck
= (struct share_mode_lock
) {
883 if (share_mode_lock_key_refcount
== 0) {
884 TDB_DATA key
= locking_key(&id
);
886 status
= g_lock_lock(
890 (struct timeval
) { .tv_sec
= 3600 });
891 if (!NT_STATUS_IS_OK(status
)) {
892 DBG_DEBUG("g_lock_lock failed: %s\n",
896 share_mode_lock_key_id
= id
;
899 if (!file_id_equal(&share_mode_lock_key_id
, &id
)) {
900 struct file_id_buf existing
;
901 struct file_id_buf requested
;
903 DBG_ERR("Can not lock two share modes "
904 "simultaneously: existing %s requested %s\n",
905 file_id_str_buf(share_mode_lock_key_id
, &existing
),
906 file_id_str_buf(id
, &requested
));
907 smb_panic(__location__
);
911 SMB_ASSERT(share_mode_lock_key_refcount
< SIZE_MAX
);
912 share_mode_lock_key_refcount
+= 1;
914 if (static_share_mode_data
!= NULL
) {
918 status
= get_static_share_mode_data(
923 if (!NT_STATUS_IS_OK(status
)) {
924 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
926 share_mode_lock_key_refcount
-= 1;
930 lck
->cached_data
= static_share_mode_data
;
932 if (CHECK_DEBUGLVL(DBGLVL_DEBUG
)) {
933 struct file_id_buf returned
;
935 DBG_DEBUG("Returning %s (data_cached=%u key_refcount=%zu)\n",
936 file_id_str_buf(id
, &returned
),
937 static_share_mode_data
!= NULL
,
938 share_mode_lock_key_refcount
);
943 if (share_mode_lock_key_refcount
== 0) {
944 NTSTATUS ulstatus
= g_lock_unlock(lock_ctx
, share_mode_lock_key
);
945 if (!NT_STATUS_IS_OK(ulstatus
)) {
946 DBG_ERR("g_lock_unlock failed: %s\n",
947 nt_errstr(ulstatus
));
953 static int share_mode_lock_destructor(struct share_mode_lock
*lck
)
957 SMB_ASSERT(share_mode_lock_key_refcount
> 0);
958 share_mode_lock_key_refcount
-= 1;
960 if (share_mode_lock_key_refcount
> 0) {
964 status
= share_mode_data_store(static_share_mode_data
);
965 if (!NT_STATUS_IS_OK(status
)) {
966 DBG_ERR("share_mode_data_store failed: %s\n",
968 smb_panic("Could not store share mode data\n");
971 status
= g_lock_unlock(lock_ctx
, share_mode_lock_key
);
972 if (!NT_STATUS_IS_OK(status
)) {
973 DBG_ERR("g_lock_unlock failed: %s\n",
975 smb_panic("Could not unlock share mode\n");
978 if (!static_share_mode_data
->not_stored
) {
980 * This is worth keeping. Without share modes,
981 * share_mode_data_store above has left nothing in the
984 share_mode_memcache_store(static_share_mode_data
);
985 static_share_mode_data
= NULL
;
988 TALLOC_FREE(static_share_mode_data
);
992 struct share_mode_lock
*get_share_mode_lock(
995 const char *servicepath
,
996 const struct smb_filename
*smb_fname
,
997 const struct timespec
*old_write_time
)
999 struct share_mode_lock
*lck
= NULL
;
1002 lck
= talloc(mem_ctx
, struct share_mode_lock
);
1007 status
= get_share_mode_lock_internal(id
,
1012 if (!NT_STATUS_IS_OK(status
)) {
1013 DBG_GET_SHARE_MODE_LOCK(status
,
1014 "get_share_mode_lock_internal() failed - %s\n",
1020 talloc_set_destructor(lck
, share_mode_lock_destructor
);
1024 /*******************************************************************
1025 Fetch a share mode where we know one MUST exist. This call reference
1026 counts it internally to allow for nested lock fetches.
1027 ********************************************************************/
1029 struct share_mode_lock
*get_existing_share_mode_lock(TALLOC_CTX
*mem_ctx
,
1030 const struct file_id id
)
1032 return get_share_mode_lock(mem_ctx
, id
, NULL
, NULL
, NULL
);
1035 static void share_mode_wakeup_waiters_fn(
1036 struct share_mode_lock
*lck
,
1039 g_lock_wake_watchers(lock_ctx
, share_mode_lock_key
);
1042 NTSTATUS
share_mode_wakeup_waiters(struct file_id id
)
1044 return share_mode_do_locked_vfs_denied(id
,
1045 share_mode_wakeup_waiters_fn
,
1049 struct fsp_update_share_mode_flags_state
{
1050 struct files_struct
*fsp
;
1051 enum ndr_err_code ndr_err
;
1052 uint64_t share_mode_epoch
;
1053 uint16_t share_mode_flags
;
1056 static void fsp_update_share_mode_flags_fn(
1057 struct server_id exclusive
,
1059 const struct server_id
*shared
,
1060 const uint8_t *data
,
1064 struct fsp_update_share_mode_flags_state
*state
= private_data
;
1065 struct locking_tdb_data ltdb
= { 0 };
1068 bool ok
= locking_tdb_data_get(<db
, data
, datalen
);
1070 DBG_DEBUG("locking_tdb_data_get failed\n");
1075 if (ltdb
.share_mode_data_len
== 0) {
1076 /* Likely a ctdb tombstone record, ignore it */
1080 if (exclusive
.pid
!= 0) {
1081 struct server_id self
=
1082 messaging_server_id(state
->fsp
->conn
->sconn
->msg_ctx
);
1083 bool is_self
= server_id_equal(&self
, &exclusive
);
1087 * If someone else is holding an exclusive
1088 * lock, pretend there's a read lease
1090 state
->share_mode_flags
= SHARE_MODE_LEASE_READ
;
1095 state
->ndr_err
= get_share_mode_blob_header(ltdb
.share_mode_data_buf
,
1096 ltdb
.share_mode_data_len
,
1097 &state
->share_mode_epoch
,
1098 &state
->share_mode_flags
);
1101 static NTSTATUS
fsp_update_share_mode_flags(struct files_struct
*fsp
)
1103 struct fsp_update_share_mode_flags_state state
= { .fsp
= fsp
, };
1104 int seqnum
= g_lock_seqnum(lock_ctx
);
1108 if (seqnum
== fsp
->share_mode_flags_seqnum
) {
1109 return NT_STATUS_OK
;
1112 key
= locking_key(&fsp
->file_id
);
1113 status
= g_lock_dump(lock_ctx
, key
,
1114 fsp_update_share_mode_flags_fn
,
1116 if (!NT_STATUS_IS_OK(status
)) {
1117 /* no DBG_GET_SHARE_MODE_LOCK here! */
1118 DBG_ERR("g_lock_dump returned %s\n",
1123 if (!NDR_ERR_CODE_IS_SUCCESS(state
.ndr_err
)) {
1124 DBG_ERR("get_share_mode_blob_header returned %s\n",
1125 ndr_errstr(state
.ndr_err
));
1126 return ndr_map_error2ntstatus(state
.ndr_err
);
1129 fsp
->share_mode_flags_seqnum
= seqnum
;
1130 fsp
->share_mode_flags
= state
.share_mode_flags
;
1132 return NT_STATUS_OK
;
1135 bool file_has_read_lease(struct files_struct
*fsp
)
1139 status
= fsp_update_share_mode_flags(fsp
);
1140 if (!NT_STATUS_IS_OK(status
)) {
1141 /* Safe default for leases */
1145 return (fsp
->share_mode_flags
& SHARE_MODE_LEASE_READ
) != 0;
1148 #define share_mode_lock_assert_private_data(__lck) \
1149 _share_mode_lock_assert_private_data(__lck, __func__, __location__)
1150 static struct share_mode_data
*_share_mode_lock_assert_private_data(
1151 struct share_mode_lock
*lck
,
1152 const char *caller_function
,
1153 const char *caller_location
)
1155 struct share_mode_data
*d
= NULL
;
1158 status
= share_mode_lock_access_private_data(lck
, &d
);
1159 if (!NT_STATUS_IS_OK(status
)) {
1160 struct file_id id
= share_mode_lock_file_id(lck
);
1161 struct file_id_buf id_buf
;
1162 /* Any error recovery possible here ? */
1163 D_ERR("%s:%s(): share_mode_lock_access_private_data() "
1164 "failed for id=%s - %s\n",
1165 caller_location
, caller_function
,
1166 file_id_str_buf(id
, &id_buf
),
1168 smb_panic(caller_location
);
1175 NTTIME
share_mode_changed_write_time(struct share_mode_lock
*lck
)
1177 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1178 return d
->changed_write_time
;
1181 void share_mode_set_changed_write_time(struct share_mode_lock
*lck
, struct timespec write_time
)
1183 struct file_id fileid
= share_mode_lock_file_id(lck
);
1184 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1185 struct file_id_buf ftmp
;
1186 struct timeval_buf tbuf
;
1187 NTTIME nt
= full_timespec_to_nt_time(&write_time
);
1189 DBG_INFO("%s id=%s\n",
1190 timespec_string_buf(&write_time
, true, &tbuf
),
1191 file_id_str_buf(fileid
, &ftmp
));
1193 if (d
->changed_write_time
!= nt
) {
1195 d
->changed_write_time
= nt
;
1199 void share_mode_set_old_write_time(struct share_mode_lock
*lck
, struct timespec write_time
)
1201 struct file_id fileid
= share_mode_lock_file_id(lck
);
1202 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1203 struct file_id_buf ftmp
;
1204 struct timeval_buf tbuf
;
1205 NTTIME nt
= full_timespec_to_nt_time(&write_time
);
1207 DBG_INFO("%s id=%s\n",
1208 timespec_string_buf(&write_time
, true, &tbuf
),
1209 file_id_str_buf(fileid
, &ftmp
));
1211 if (d
->changed_write_time
!= nt
) {
1213 d
->old_write_time
= nt
;
1217 const char *share_mode_servicepath(struct share_mode_lock
*lck
)
1219 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1220 return d
->servicepath
;
1223 char *share_mode_filename(TALLOC_CTX
*mem_ctx
, struct share_mode_lock
*lck
)
1225 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1226 bool has_stream
= (d
->stream_name
!= NULL
);
1229 fname
= talloc_asprintf(
1233 has_stream
? ":" : "",
1234 has_stream
? d
->stream_name
: "");
1238 char *share_mode_data_dump(
1239 TALLOC_CTX
*mem_ctx
, struct share_mode_lock
*lck
)
1241 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1242 struct ndr_print
*p
= talloc(mem_ctx
, struct ndr_print
);
1249 *p
= (struct ndr_print
) {
1250 .print
= ndr_print_string_helper
,
1252 .private_data
= talloc_strdup(mem_ctx
, ""),
1255 if (p
->private_data
== NULL
) {
1260 ndr_print_share_mode_data(p
, "SHARE_MODE_DATA", d
);
1262 ret
= p
->private_data
;
1269 void share_mode_flags_get(
1270 struct share_mode_lock
*lck
,
1271 uint32_t *access_mask
,
1272 uint32_t *share_mode
,
1273 uint32_t *lease_type
)
1275 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1276 uint16_t flags
= d
->flags
;
1278 if (access_mask
!= NULL
) {
1280 ((flags
& SHARE_MODE_ACCESS_READ
) ?
1281 FILE_READ_DATA
: 0) |
1282 ((flags
& SHARE_MODE_ACCESS_WRITE
) ?
1283 FILE_WRITE_DATA
: 0) |
1284 ((flags
& SHARE_MODE_ACCESS_DELETE
) ?
1287 if (share_mode
!= NULL
) {
1289 ((flags
& SHARE_MODE_SHARE_READ
) ?
1290 FILE_SHARE_READ
: 0) |
1291 ((flags
& SHARE_MODE_SHARE_WRITE
) ?
1292 FILE_SHARE_WRITE
: 0) |
1293 ((flags
& SHARE_MODE_SHARE_DELETE
) ?
1294 FILE_SHARE_DELETE
: 0);
1296 if (lease_type
!= NULL
) {
1298 ((flags
& SHARE_MODE_LEASE_READ
) ?
1299 SMB2_LEASE_READ
: 0) |
1300 ((flags
& SHARE_MODE_LEASE_WRITE
) ?
1301 SMB2_LEASE_WRITE
: 0) |
1302 ((flags
& SHARE_MODE_LEASE_HANDLE
) ?
1303 SMB2_LEASE_HANDLE
: 0);
1307 void share_mode_flags_set(
1308 struct share_mode_lock
*lck
,
1309 uint32_t access_mask
,
1310 uint32_t share_mode
,
1311 uint32_t lease_type
,
1314 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1317 flags
|= (access_mask
& (FILE_READ_DATA
| FILE_EXECUTE
)) ?
1318 SHARE_MODE_ACCESS_READ
: 0;
1319 flags
|= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ?
1320 SHARE_MODE_ACCESS_WRITE
: 0;
1321 flags
|= (access_mask
& (DELETE_ACCESS
)) ?
1322 SHARE_MODE_ACCESS_DELETE
: 0;
1324 flags
|= (share_mode
& FILE_SHARE_READ
) ?
1325 SHARE_MODE_SHARE_READ
: 0;
1326 flags
|= (share_mode
& FILE_SHARE_WRITE
) ?
1327 SHARE_MODE_SHARE_WRITE
: 0;
1328 flags
|= (share_mode
& FILE_SHARE_DELETE
) ?
1329 SHARE_MODE_SHARE_DELETE
: 0;
1331 flags
|= (lease_type
& SMB2_LEASE_READ
) ?
1332 SHARE_MODE_LEASE_READ
: 0;
1333 flags
|= (lease_type
& SMB2_LEASE_WRITE
) ?
1334 SHARE_MODE_LEASE_WRITE
: 0;
1335 flags
|= (lease_type
& SMB2_LEASE_HANDLE
) ?
1336 SHARE_MODE_LEASE_HANDLE
: 0;
1338 if (d
->flags
== flags
) {
1342 if (modified
!= NULL
) {
1349 struct share_mode_watch_state
{
1351 struct server_id blocker
;
1354 static void share_mode_watch_done(struct tevent_req
*subreq
);
1356 struct tevent_req
*share_mode_watch_send(
1357 TALLOC_CTX
*mem_ctx
,
1358 struct tevent_context
*ev
,
1359 struct share_mode_lock
*lck
,
1360 struct server_id blocker
)
1362 struct file_id id
= share_mode_lock_file_id(lck
);
1363 TDB_DATA key
= locking_key(&id
);
1364 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1365 struct share_mode_watch_state
*state
= NULL
;
1367 req
= tevent_req_create(
1368 mem_ctx
, &state
, struct share_mode_watch_state
);
1373 subreq
= g_lock_watch_data_send(state
, ev
, lock_ctx
, key
, blocker
);
1374 if (tevent_req_nomem(subreq
, req
)) {
1375 return tevent_req_post(req
, ev
);
1377 tevent_req_set_callback(subreq
, share_mode_watch_done
, req
);
1381 static void share_mode_watch_done(struct tevent_req
*subreq
)
1383 struct tevent_req
*req
= tevent_req_callback_data(
1384 subreq
, struct tevent_req
);
1385 struct share_mode_watch_state
*state
= tevent_req_data(
1386 req
, struct share_mode_watch_state
);
1389 status
= g_lock_watch_data_recv(
1390 subreq
, &state
->blockerdead
, &state
->blocker
);
1391 if (tevent_req_nterror(req
, status
)) {
1394 tevent_req_done(req
);
1397 NTSTATUS
share_mode_watch_recv(
1398 struct tevent_req
*req
, bool *blockerdead
, struct server_id
*blocker
)
1400 struct share_mode_watch_state
*state
= tevent_req_data(
1401 req
, struct share_mode_watch_state
);
1404 if (tevent_req_is_nterror(req
, &status
)) {
1407 if (blockerdead
!= NULL
) {
1408 *blockerdead
= state
->blockerdead
;
1410 if (blocker
!= NULL
) {
1411 *blocker
= state
->blocker
;
1413 return NT_STATUS_OK
;
1416 struct fetch_share_mode_unlocked_state
{
1417 TALLOC_CTX
*mem_ctx
;
1419 struct share_mode_lock
*lck
;
1422 static void fetch_share_mode_unlocked_parser(
1423 struct server_id exclusive
,
1425 const struct server_id
*shared
,
1426 const uint8_t *data
,
1430 struct fetch_share_mode_unlocked_state
*state
= private_data
;
1431 struct locking_tdb_data ltdb
= { 0 };
1434 bool ok
= locking_tdb_data_get(<db
, data
, datalen
);
1436 DBG_DEBUG("locking_tdb_data_get failed\n");
1441 if (ltdb
.share_mode_data_len
== 0) {
1442 /* Likely a ctdb tombstone record, ignore it */
1446 state
->lck
= talloc(state
->mem_ctx
, struct share_mode_lock
);
1447 if (state
->lck
== NULL
) {
1448 DEBUG(0, ("talloc failed\n"));
1451 state
->lck
->id
= state
->id
;
1453 state
->lck
->cached_data
= parse_share_modes(
1456 ltdb
.share_mode_data_buf
,
1457 ltdb
.share_mode_data_len
);
1458 if (state
->lck
->cached_data
== NULL
) {
1459 DBG_DEBUG("parse_share_modes failed\n");
1460 TALLOC_FREE(state
->lck
);
1464 /*******************************************************************
1465 Get a share_mode_lock without locking the database or reference
1466 counting. Used by smbstatus to display existing share modes.
1467 ********************************************************************/
1469 struct share_mode_lock
*fetch_share_mode_unlocked(TALLOC_CTX
*mem_ctx
,
1472 struct fetch_share_mode_unlocked_state state
= {
1476 TDB_DATA key
= locking_key(&id
);
1479 status
= g_lock_dump(
1480 lock_ctx
, key
, fetch_share_mode_unlocked_parser
, &state
);
1481 if (!NT_STATUS_IS_OK(status
)) {
1482 DBG_DEBUG("g_lock_dump failed: %s\n", nt_errstr(status
));
1488 struct fetch_share_mode_state
{
1490 struct share_mode_lock
*lck
;
1494 static void fetch_share_mode_fn(
1495 struct server_id exclusive
,
1497 const struct server_id
*shared
,
1498 const uint8_t *data
,
1500 void *private_data
);
1501 static void fetch_share_mode_done(struct tevent_req
*subreq
);
1504 * @brief Get a share_mode_lock without locking or refcounting
1506 * This can be used in a clustered Samba environment where the async dbwrap
1507 * request is sent over a socket to the local ctdbd. If the send queue is full
1508 * and the caller was issuing multiple async dbwrap requests in a loop, the
1509 * caller knows it's probably time to stop sending requests for now and try
1512 * @param[in] mem_ctx The talloc memory context to use.
1514 * @param[in] ev The event context to work on.
1516 * @param[in] id The file id for the locking.tdb key
1518 * @param[out] queued This boolean out parameter tells the caller whether the
1519 * async request is blocked in a full send queue:
1521 * false := request is dispatched
1523 * true := send queue is full, request waiting to be
1526 * @return The new async request, NULL on error.
1528 struct tevent_req
*fetch_share_mode_send(TALLOC_CTX
*mem_ctx
,
1529 struct tevent_context
*ev
,
1533 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1534 struct fetch_share_mode_state
*state
= NULL
;
1538 req
= tevent_req_create(mem_ctx
, &state
,
1539 struct fetch_share_mode_state
);
1545 subreq
= g_lock_dump_send(
1550 fetch_share_mode_fn
,
1552 if (tevent_req_nomem(subreq
, req
)) {
1553 return tevent_req_post(req
, ev
);
1555 tevent_req_set_callback(subreq
, fetch_share_mode_done
, req
);
1559 static void fetch_share_mode_fn(
1560 struct server_id exclusive
,
1562 const struct server_id
*shared
,
1563 const uint8_t *data
,
1567 struct fetch_share_mode_state
*state
= talloc_get_type_abort(
1568 private_data
, struct fetch_share_mode_state
);
1569 struct locking_tdb_data ltdb
= { 0 };
1572 bool ok
= locking_tdb_data_get(<db
, data
, datalen
);
1574 DBG_DEBUG("locking_tdb_data_get failed\n");
1579 if (ltdb
.share_mode_data_len
== 0) {
1580 /* Likely a ctdb tombstone record, ignore it */
1584 state
->lck
= talloc(state
, struct share_mode_lock
);
1585 if (state
->lck
== NULL
) {
1586 DBG_WARNING("talloc failed\n");
1587 state
->status
= NT_STATUS_NO_MEMORY
;
1590 state
->lck
->id
= state
->id
,
1592 state
->lck
->cached_data
= parse_share_modes(
1595 ltdb
.share_mode_data_buf
,
1596 ltdb
.share_mode_data_len
);
1597 if (state
->lck
->cached_data
== NULL
) {
1598 DBG_DEBUG("parse_share_modes failed\n");
1599 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
1600 TALLOC_FREE(state
->lck
);
1605 static void fetch_share_mode_done(struct tevent_req
*subreq
)
1607 struct tevent_req
*req
= tevent_req_callback_data(
1608 subreq
, struct tevent_req
);
1609 struct fetch_share_mode_state
*state
= tevent_req_data(
1610 req
, struct fetch_share_mode_state
);
1613 status
= g_lock_dump_recv(subreq
);
1614 TALLOC_FREE(subreq
);
1615 if (tevent_req_nterror(req
, status
)) {
1618 if (tevent_req_nterror(req
, state
->status
)) {
1621 tevent_req_done(req
);
1624 NTSTATUS
fetch_share_mode_recv(struct tevent_req
*req
,
1625 TALLOC_CTX
*mem_ctx
,
1626 struct share_mode_lock
**_lck
)
1628 struct fetch_share_mode_state
*state
= tevent_req_data(
1629 req
, struct fetch_share_mode_state
);
1630 struct share_mode_lock
*lck
= NULL
;
1634 if (tevent_req_is_nterror(req
, &status
)) {
1635 tevent_req_received(req
);
1639 if (state
->lck
== NULL
) {
1640 tevent_req_received(req
);
1641 return NT_STATUS_NOT_FOUND
;
1644 lck
= talloc_move(mem_ctx
, &state
->lck
);
1646 if (DEBUGLEVEL
>= 10) {
1647 DBG_DEBUG("share_mode_data:\n");
1648 NDR_PRINT_DEBUG(share_mode_data
, lck
->cached_data
);
1652 tevent_req_received(req
);
1653 return NT_STATUS_OK
;
1656 struct share_mode_forall_state
{
1658 int (*fn
)(struct file_id fid
,
1659 const struct share_mode_data
*data
,
1660 void *private_data
);
1664 static void share_mode_forall_dump_fn(
1665 struct server_id exclusive
,
1667 const struct server_id
*shared
,
1668 const uint8_t *data
,
1672 struct share_mode_forall_state
*state
= private_data
;
1674 struct locking_tdb_data ltdb
= { 0 };
1676 struct share_mode_data
*d
;
1678 if (state
->key
.dsize
!= sizeof(fid
)) {
1679 DBG_DEBUG("Got invalid key length %zu\n", state
->key
.dsize
);
1682 memcpy(&fid
, state
->key
.dptr
, sizeof(fid
));
1684 ok
= locking_tdb_data_get(<db
, data
, datalen
);
1686 DBG_DEBUG("locking_tdb_data_get() failed\n");
1690 d
= parse_share_modes(
1693 ltdb
.share_mode_data_buf
,
1694 ltdb
.share_mode_data_len
);
1696 DBG_DEBUG("parse_share_modes() failed\n");
1700 state
->fn(fid
, d
, state
->private_data
);
1704 static int share_mode_forall_fn(TDB_DATA key
, void *private_data
)
1706 struct share_mode_forall_state
*state
= private_data
;
1711 status
= g_lock_dump(
1712 lock_ctx
, key
, share_mode_forall_dump_fn
, private_data
);
1713 if (!NT_STATUS_IS_OK(status
)) {
1714 DBG_GET_SHARE_MODE_LOCK(status
,
1715 "g_lock_dump failed: %s\n",
1721 int share_mode_forall(int (*fn
)(struct file_id fid
,
1722 const struct share_mode_data
*data
,
1723 void *private_data
),
1726 struct share_mode_forall_state state
= {
1728 .private_data
= private_data
1732 if (lock_ctx
== NULL
) {
1737 lock_ctx
, share_mode_forall_fn
, &state
);
1739 DBG_ERR("g_lock_locks failed\n");
1744 struct share_entry_forall_state
{
1746 const struct share_mode_data
*data
;
1747 int (*fn
)(struct file_id fid
,
1748 const struct share_mode_data
*data
,
1749 const struct share_mode_entry
*entry
,
1750 void *private_data
);
1755 static bool share_entry_traverse_walker(
1756 struct share_mode_entry
*e
,
1760 struct share_entry_forall_state
*state
= private_data
;
1762 state
->ret
= state
->fn(
1763 state
->fid
, state
->data
, e
, state
->private_data
);
1764 return (state
->ret
!= 0);
1767 static int share_entry_traverse_fn(struct file_id fid
,
1768 const struct share_mode_data
*data
,
1771 struct share_entry_forall_state
*state
= private_data
;
1772 struct share_mode_lock lck
= {
1774 .cached_data
= discard_const_p(struct share_mode_data
, data
)
1781 ok
= share_mode_forall_entries(
1782 &lck
, share_entry_traverse_walker
, state
);
1784 DBG_ERR("share_mode_forall_entries failed\n");
1791 /*******************************************************************
1792 Call the specified function on each entry under management by the
1794 ********************************************************************/
1796 int share_entry_forall(int (*fn
)(struct file_id fid
,
1797 const struct share_mode_data
*data
,
1798 const struct share_mode_entry
*entry
,
1799 void *private_data
),
1802 struct share_entry_forall_state state
= {
1803 .fn
= fn
, .private_data
= private_data
};
1805 return share_mode_forall(share_entry_traverse_fn
, &state
);
1808 static int share_mode_entry_cmp(
1809 struct server_id pid1
,
1810 uint64_t share_file_id1
,
1811 struct server_id pid2
,
1812 uint64_t share_file_id2
)
1816 cmp
= server_id_cmp(&pid1
, &pid2
);
1820 if (share_file_id1
!= share_file_id2
) {
1821 return (share_file_id1
< share_file_id2
) ? -1 : 1;
1826 static size_t share_mode_entry_find(
1827 const uint8_t *data
,
1828 size_t num_share_modes
,
1829 struct server_id pid
,
1830 uint64_t share_file_id
,
1831 struct share_mode_entry
*e
,
1834 ssize_t left
, right
, middle
;
1838 if (num_share_modes
== 0) {
1843 right
= (num_share_modes
-1);
1845 while (left
<= right
) {
1846 const uint8_t *middle_ptr
= NULL
;
1850 middle
= left
+ ((right
- left
) / 2);
1851 middle_ptr
= data
+ middle
* SHARE_MODE_ENTRY_SIZE
;
1853 DBG_DEBUG("left=%zu, right=%zu, middle=%zu, middle_ptr=%p\n",
1859 ok
= share_mode_entry_get(middle_ptr
, e
);
1861 DBG_DEBUG("share_mode_entry_get failed\n");
1865 cmp
= share_mode_entry_cmp(
1866 e
->pid
, e
->share_file_id
, pid
, share_file_id
);
1882 bool set_share_mode(struct share_mode_lock
*lck
,
1883 struct files_struct
*fsp
,
1887 const struct smb2_lease_key
*lease_key
,
1888 uint32_t share_access
,
1889 uint32_t access_mask
)
1891 struct share_mode_data
*d
= share_mode_lock_assert_private_data(lck
);
1892 TDB_DATA key
= locking_key(&d
->id
);
1893 struct server_id my_pid
= messaging_server_id(
1894 fsp
->conn
->sconn
->msg_ctx
);
1895 struct locking_tdb_data
*ltdb
= NULL
;
1897 struct share_mode_entry e
= { .pid
.pid
= 0 };
1898 struct share_mode_entry_buf e_buf
;
1903 size_t num_dbufs
= 0;
1905 status
= locking_tdb_data_fetch(key
, talloc_tos(), <db
);
1906 if (!NT_STATUS_IS_OK(status
)) {
1907 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
1911 DBG_DEBUG("num_share_modes=%zu\n", ltdb
->num_share_entries
);
1913 idx
= share_mode_entry_find(
1914 ltdb
->share_entries
,
1915 ltdb
->num_share_entries
,
1917 fh_get_gen_id(fsp
->fh
),
1921 DBG_WARNING("Found duplicate share mode\n");
1922 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
1926 e
= (struct share_mode_entry
) {
1928 .share_access
= share_access
,
1929 .private_options
= fh_get_private_options(fsp
->fh
),
1930 .access_mask
= access_mask
,
1933 .time
.tv_sec
= fsp
->open_time
.tv_sec
,
1934 .time
.tv_usec
= fsp
->open_time
.tv_usec
,
1935 .share_file_id
= fh_get_gen_id(fsp
->fh
),
1936 .uid
= (uint32_t)uid
,
1937 .flags
= (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) ?
1938 SHARE_MODE_FLAG_POSIX_OPEN
: 0,
1939 .name_hash
= fsp
->name_hash
,
1942 if (op_type
== LEASE_OPLOCK
) {
1943 const struct GUID
*client_guid
= fsp_client_guid(fsp
);
1944 e
.client_guid
= *client_guid
;
1945 e
.lease_key
= *lease_key
;
1948 ok
= share_mode_entry_put(&e
, &e_buf
);
1950 DBG_DEBUG("share_mode_entry_put failed\n");
1951 status
= NT_STATUS_INTERNAL_ERROR
;
1955 DBG_DEBUG("idx=%zu, found=%d\n", idx
, (int)found
);
1958 dbufs
[num_dbufs
] = (TDB_DATA
) {
1959 .dptr
= discard_const_p(uint8_t, ltdb
->share_entries
),
1960 .dsize
= idx
* SHARE_MODE_ENTRY_SIZE
,
1965 dbufs
[num_dbufs
] = (TDB_DATA
) {
1966 .dptr
= e_buf
.buf
, .dsize
= SHARE_MODE_ENTRY_SIZE
,
1970 if (idx
< ltdb
->num_share_entries
) {
1971 size_t num_after_idx
= (ltdb
->num_share_entries
-idx
);
1972 dbufs
[num_dbufs
] = (TDB_DATA
) {
1973 .dptr
= discard_const_p(uint8_t, ltdb
->share_entries
) +
1974 idx
* SHARE_MODE_ENTRY_SIZE
,
1975 .dsize
= num_after_idx
* SHARE_MODE_ENTRY_SIZE
,
1982 for (i
=0; i
<num_dbufs
; i
++) {
1983 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
1990 if (num_dbufs
== 1) {
1992 * Storing a fresh record with just one share entry
1998 * If there was any existing data in
1999 * ltdb->share_entries, it's now been
2000 * moved and we've split it into:
2003 * dbufs[0] -> old sorted data less than new_entry
2004 * dbufs[1] -> new_share_mode_entry
2005 * dbufs[2] -> old sorted_data greater than new entry.
2007 * So the old data inside ltdb->share_entries is
2010 * If we're storing a brand new entry the
2014 * dbufs[0] -> new_share_mode_entry
2016 * Either way we must set ltdb->share_entries = NULL
2017 * and ltdb->num_share_entries = 0 so that
2018 * locking_tdb_data_store() doesn't use it to
2019 * store any data. It's no longer there.
2022 ltdb
->share_entries
= NULL
;
2023 ltdb
->num_share_entries
= 0;
2025 status
= share_mode_data_ltdb_store(d
, key
, ltdb
, dbufs
, num_dbufs
);
2026 if (!NT_STATUS_IS_OK(status
)) {
2027 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2032 return NT_STATUS_IS_OK(status
);
2035 static bool share_mode_for_one_entry(
2036 bool (*fn
)(struct share_mode_entry
*e
,
2038 void *private_data
),
2042 size_t *num_share_modes
,
2046 .data
= data
+ (*i
) * SHARE_MODE_ENTRY_SIZE
,
2047 .length
= SHARE_MODE_ENTRY_SIZE
,
2049 struct share_mode_entry e
= {.pid
.pid
=0};
2050 enum ndr_err_code ndr_err
= NDR_ERR_SUCCESS
;
2051 bool modified
= false;
2053 struct server_id e_pid
;
2054 uint64_t e_share_file_id
;
2056 ndr_err
= ndr_pull_struct_blob_all_noalloc(
2059 (ndr_pull_flags_fn_t
)ndr_pull_share_mode_entry
);
2060 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2061 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
2065 if (DEBUGLEVEL
>= 10) {
2066 DBG_DEBUG("entry[%zu]:\n", *i
);
2067 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
2071 e_share_file_id
= e
.share_file_id
;
2073 stop
= fn(&e
, &modified
, private_data
);
2075 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2081 if (DEBUGLEVEL
>=10) {
2082 DBG_DEBUG("share_mode_entry:\n");
2083 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
2086 if (*i
< *num_share_modes
) {
2088 blob
.data
+ SHARE_MODE_ENTRY_SIZE
,
2089 (*num_share_modes
- *i
- 1) *
2090 SHARE_MODE_ENTRY_SIZE
);
2092 *num_share_modes
-= 1;
2098 if (DEBUGLEVEL
>=10) {
2099 DBG_DEBUG("share_mode_entry:\n");
2100 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
2104 * Make sure sorting order is kept intact
2106 SMB_ASSERT(server_id_equal(&e_pid
, &e
.pid
));
2107 SMB_ASSERT(e_share_file_id
== e
.share_file_id
);
2109 ndr_err
= ndr_push_struct_into_fixed_blob(
2112 (ndr_push_flags_fn_t
)
2113 ndr_push_share_mode_entry
);
2114 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2115 DBG_WARNING("ndr_push_share_mode_entry "
2117 ndr_errstr(ndr_err
));
2119 * Not much we can do, just ignore it
2135 bool share_mode_forall_entries(
2136 struct share_mode_lock
*lck
,
2137 bool (*fn
)(struct share_mode_entry
*e
,
2139 void *private_data
),
2142 struct file_id id
= share_mode_lock_file_id(lck
);
2143 struct share_mode_data
*d
= NULL
;
2144 TDB_DATA key
= locking_key(&id
);
2145 struct locking_tdb_data
*ltdb
= NULL
;
2146 uint8_t *share_entries
= NULL
;
2147 size_t num_share_entries
;
2148 bool writeback
= false;
2153 status
= share_mode_lock_access_private_data(lck
, &d
);
2154 if (!NT_STATUS_IS_OK(status
)) {
2155 struct file_id_buf id_buf
;
2156 /* Any error recovery possible here ? */
2157 DBG_ERR("share_mode_lock_access_private_data() failed for "
2159 file_id_str_buf(id
, &id_buf
),
2164 status
= locking_tdb_data_fetch(key
, talloc_tos(), <db
);
2165 if (!NT_STATUS_IS_OK(status
)) {
2166 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2170 DBG_DEBUG("num_share_modes=%zu\n", ltdb
->num_share_entries
);
2172 num_share_entries
= ltdb
->num_share_entries
;
2173 share_entries
= discard_const_p(uint8_t, ltdb
->share_entries
);
2176 while (i
<num_share_entries
) {
2177 stop
= share_mode_for_one_entry(
2189 DBG_DEBUG("num_share_entries=%zu, writeback=%d\n",
2198 if ((ltdb
->num_share_entries
!= 0 ) && (num_share_entries
== 0)) {
2200 * This routine wiped all share entries, let
2201 * share_mode_data_store() delete the record
2206 ltdb
->num_share_entries
= num_share_entries
;
2207 ltdb
->share_entries
= share_entries
;
2209 status
= share_mode_data_ltdb_store(d
, key
, ltdb
, NULL
, 0);
2211 if (!NT_STATUS_IS_OK(status
)) {
2212 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2220 struct share_mode_count_entries_state
{
2221 size_t num_share_modes
;
2225 static void share_mode_count_entries_fn(
2226 struct server_id exclusive
,
2228 const struct server_id
*shared
,
2229 const uint8_t *data
,
2233 struct share_mode_count_entries_state
*state
= private_data
;
2234 struct locking_tdb_data ltdb
= { 0 };
2237 ok
= locking_tdb_data_get(<db
, data
, datalen
);
2239 DBG_WARNING("locking_tdb_data_get failed for %zu\n", datalen
);
2240 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2243 state
->num_share_modes
= ltdb
.num_share_entries
;
2244 state
->status
= NT_STATUS_OK
;
2247 NTSTATUS
share_mode_count_entries(struct file_id fid
, size_t *num_share_modes
)
2249 struct share_mode_count_entries_state state
= {
2250 .status
= NT_STATUS_NOT_FOUND
,
2254 status
= g_lock_dump(
2257 share_mode_count_entries_fn
,
2259 if (!NT_STATUS_IS_OK(status
)) {
2260 DBG_DEBUG("g_lock_dump failed: %s\n",
2264 if (!NT_STATUS_IS_OK(state
.status
)) {
2265 DBG_DEBUG("share_mode_count_entries_fn failed: %s\n",
2266 nt_errstr(state
.status
));
2267 return state
.status
;
2270 *num_share_modes
= state
.num_share_modes
;
2271 return NT_STATUS_OK
;
2274 static bool share_mode_entry_do(
2275 struct share_mode_data
*d
,
2276 struct server_id pid
,
2277 uint64_t share_file_id
,
2278 void (*fn
)(struct share_mode_entry
*e
,
2279 size_t num_share_modes
,
2281 void *private_data
),
2284 TDB_DATA key
= locking_key(&d
->id
);
2285 struct locking_tdb_data
*ltdb
= NULL
;
2288 bool modified
= false;
2289 struct share_mode_entry e
;
2290 uint8_t *e_ptr
= NULL
;
2294 status
= locking_tdb_data_fetch(key
, talloc_tos(), <db
);
2295 if (!NT_STATUS_IS_OK(status
)) {
2296 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2300 DBG_DEBUG("num_share_modes=%zu\n", ltdb
->num_share_entries
);
2302 idx
= share_mode_entry_find(
2303 ltdb
->share_entries
,
2304 ltdb
->num_share_entries
,
2310 DBG_WARNING("Did not find share mode entry for %"PRIu64
"\n",
2315 if (DEBUGLEVEL
>=10) {
2316 DBG_DEBUG("entry[%zu]:\n", idx
);
2317 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
2320 fn(&e
, ltdb
->num_share_entries
, &modified
, private_data
);
2322 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2327 if (!e
.stale
&& !modified
) {
2332 e_ptr
= discard_const_p(uint8_t, ltdb
->share_entries
) +
2333 idx
* SHARE_MODE_ENTRY_SIZE
;
2337 * Move the rest down one entry
2339 size_t behind
= ltdb
->num_share_entries
- idx
- 1;
2342 e_ptr
+ SHARE_MODE_ENTRY_SIZE
,
2343 behind
* SHARE_MODE_ENTRY_SIZE
);
2345 ltdb
->num_share_entries
-= 1;
2347 if (ltdb
->num_share_entries
== 0) {
2349 * Tell share_mode_lock_destructor() to delete
2355 if (DEBUGLEVEL
>=10) {
2356 DBG_DEBUG("share_mode_entry:\n");
2357 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
2360 struct share_mode_entry_buf buf
;
2363 if (ltdb
->num_share_entries
!= 1) {
2365 * Make sure the sorting order stays intact
2367 SMB_ASSERT(server_id_equal(&e
.pid
, &pid
));
2368 SMB_ASSERT(e
.share_file_id
== share_file_id
);
2371 ok
= share_mode_entry_put(&e
, &buf
);
2373 DBG_DEBUG("share_mode_entry_put failed\n");
2376 memcpy(e_ptr
, buf
.buf
, SHARE_MODE_ENTRY_SIZE
);
2379 status
= share_mode_data_ltdb_store(d
, key
, ltdb
, NULL
, 0);
2380 if (!NT_STATUS_IS_OK(status
)) {
2381 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2392 struct del_share_mode_state
{
2396 static void del_share_mode_fn(
2397 struct share_mode_entry
*e
,
2398 size_t num_share_modes
,
2402 struct del_share_mode_state
*state
= private_data
;
2407 bool del_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
)
2409 struct del_share_mode_state state
= { .ok
= false };
2410 struct share_mode_data
*d
= NULL
;
2414 status
= share_mode_lock_access_private_data(lck
, &d
);
2415 if (!NT_STATUS_IS_OK(status
)) {
2416 struct file_id id
= share_mode_lock_file_id(lck
);
2417 struct file_id_buf id_buf
;
2418 /* Any error recovery possible here ? */
2419 DBG_ERR("share_mode_lock_access_private_data() failed for "
2421 file_id_str_buf(id
, &id_buf
),
2427 ok
= share_mode_entry_do(
2429 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
2430 fh_get_gen_id(fsp
->fh
),
2434 DBG_DEBUG("share_mode_entry_do failed\n");
2438 DBG_DEBUG("del_share_mode_fn failed\n");
2444 struct remove_share_oplock_state
{
2448 static void remove_share_oplock_fn(
2449 struct share_mode_entry
*e
,
2450 size_t num_share_modes
,
2454 struct remove_share_oplock_state
*state
= private_data
;
2456 e
->op_type
= NO_OPLOCK
;
2461 bool remove_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
2463 struct remove_share_oplock_state state
= { .ok
= false };
2464 struct share_mode_data
*d
= NULL
;
2468 status
= share_mode_lock_access_private_data(lck
, &d
);
2469 if (!NT_STATUS_IS_OK(status
)) {
2470 struct file_id id
= share_mode_lock_file_id(lck
);
2471 struct file_id_buf id_buf
;
2472 /* Any error recovery possible here ? */
2473 DBG_ERR("share_mode_lock_access_private_data() failed for "
2475 file_id_str_buf(id
, &id_buf
),
2481 ok
= share_mode_entry_do(
2483 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
2484 fh_get_gen_id(fsp
->fh
),
2485 remove_share_oplock_fn
,
2488 DBG_DEBUG("share_mode_entry_do failed\n");
2492 DBG_DEBUG("remove_share_oplock_fn failed\n");
2496 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
2497 remove_lease_if_stale(
2499 fsp_client_guid(fsp
),
2500 &fsp
->lease
->lease
.lease_key
);
2503 share_mode_wakeup_waiters(fsp
->file_id
);
2508 struct downgrade_share_oplock_state
{
2512 static void downgrade_share_oplock_fn(
2513 struct share_mode_entry
*e
,
2514 size_t num_share_modes
,
2518 struct downgrade_share_oplock_state
*state
= private_data
;
2520 e
->op_type
= LEVEL_II_OPLOCK
;
2525 bool downgrade_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
2527 struct downgrade_share_oplock_state state
= { .ok
= false };
2528 struct share_mode_data
*d
= NULL
;
2532 status
= share_mode_lock_access_private_data(lck
, &d
);
2533 if (!NT_STATUS_IS_OK(status
)) {
2534 struct file_id id
= share_mode_lock_file_id(lck
);
2535 struct file_id_buf id_buf
;
2536 /* Any error recovery possible here ? */
2537 DBG_ERR("share_mode_lock_access_private_data() failed for "
2539 file_id_str_buf(id
, &id_buf
),
2545 ok
= share_mode_entry_do(
2547 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
2548 fh_get_gen_id(fsp
->fh
),
2549 downgrade_share_oplock_fn
,
2552 DBG_DEBUG("share_mode_entry_do failed\n");
2556 DBG_DEBUG("downgrade_share_oplock_fn failed\n");
2560 d
->flags
|= SHARE_MODE_LEASE_READ
;
2566 bool mark_share_mode_disconnected(struct share_mode_lock
*lck
,
2567 struct files_struct
*fsp
)
2569 struct server_id disconnected_pid
= { .pid
= 0 };
2572 if (fsp
->op
== NULL
) {
2575 if (!fsp
->op
->global
->durable
) {
2579 server_id_set_disconnected(&disconnected_pid
);
2581 ok
= reset_share_mode_entry(
2583 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
2584 fh_get_gen_id(fsp
->fh
),
2587 fsp
->op
->global
->open_persistent_id
);
2592 bool reset_share_mode_entry(
2593 struct share_mode_lock
*lck
,
2594 struct server_id old_pid
,
2595 uint64_t old_share_file_id
,
2596 struct server_id new_pid
,
2598 uint64_t new_share_file_id
)
2600 struct file_id id
= share_mode_lock_file_id(lck
);
2601 struct share_mode_data
*d
= NULL
;
2602 TDB_DATA key
= locking_key(&id
);
2603 struct locking_tdb_data
*ltdb
= NULL
;
2604 struct share_mode_entry e
;
2605 struct share_mode_entry_buf e_buf
;
2611 status
= share_mode_lock_access_private_data(lck
, &d
);
2612 if (!NT_STATUS_IS_OK(status
)) {
2613 struct file_id_buf id_buf
;
2614 /* Any error recovery possible here ? */
2615 DBG_ERR("share_mode_lock_access_private_data() failed for "
2617 file_id_str_buf(id
, &id_buf
),
2622 status
= locking_tdb_data_fetch(key
, talloc_tos(), <db
);
2623 if (!NT_STATUS_IS_OK(status
)) {
2624 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2629 if (ltdb
->num_share_entries
!= 1) {
2630 DBG_DEBUG("num_share_modes=%zu\n", ltdb
->num_share_entries
);
2634 ok
= share_mode_entry_get(ltdb
->share_entries
, &e
);
2636 DBG_WARNING("share_mode_entry_get failed\n");
2640 cmp
= share_mode_entry_cmp(
2641 old_pid
, old_share_file_id
, e
.pid
, e
.share_file_id
);
2643 struct server_id_buf tmp1
, tmp2
;
2644 DBG_WARNING("Expected pid=%s, file_id=%"PRIu64
", "
2645 "got pid=%s, file_id=%"PRIu64
"\n",
2646 server_id_str_buf(old_pid
, &tmp1
),
2648 server_id_str_buf(e
.pid
, &tmp2
),
2654 if (new_mid
!= UINT64_MAX
) {
2657 e
.share_file_id
= new_share_file_id
;
2659 ok
= share_mode_entry_put(&e
, &e_buf
);
2661 DBG_WARNING("share_mode_entry_put failed\n");
2665 ltdb
->share_entries
= e_buf
.buf
;
2669 status
= share_mode_data_ltdb_store(d
, key
, ltdb
, NULL
, 0);
2670 if (!NT_STATUS_IS_OK(status
)) {
2671 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2683 * @brief Run @fn protected with G_LOCK_WRITE in the given file_id
2685 * @fn is NOT allowed to call SMB_VFS_* or similar functions,
2686 * which may block for some time in the kernel.
2688 * There must be at least one share_mode_entry, otherwise
2689 * NT_STATUS_NOT_FOUND is returned.
2691 * @param[in] id The key for the share_mode record.
2692 * @param[in] fn The function to run under the g_lock.
2693 * @param[in] private_date A private pointer passed to @fn.
2695 NTSTATUS
_share_mode_do_locked_vfs_denied(
2697 share_mode_do_locked_vfs_fn_t fn
,
2699 const char *location
)
2701 struct smb_vfs_deny_state vfs_deny
= {};
2702 struct share_mode_lock
*lck
= NULL
;
2704 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
2706 NTSTATUS status
= NT_STATUS_NOT_FOUND
;
2707 DBG_DEBUG("get_existing_share_mode_lock failed: %s\n",
2712 _smb_vfs_deny_push(&vfs_deny
, location
);
2713 fn(lck
, private_data
);
2714 _smb_vfs_deny_pop(&vfs_deny
, location
);
2718 return NT_STATUS_OK
;
2722 * @brief Run @fn protected with G_LOCK_WRITE in the given file_id
2724 * @fn is allowed to call SMB_VFS_* or similar functions,
2725 * which may block for some time in the kernel.
2727 * There must be at least one share_mode_entry, otherwise
2728 * NT_STATUS_NOT_FOUND is returned.
2730 * @param[in] id The key for the share_mode record.
2731 * @param[in] fn The function to run under the g_lock.
2732 * @param[in] private_date A private pointer passed to @fn.
2734 NTSTATUS
_share_mode_do_locked_vfs_allowed(
2736 share_mode_do_locked_vfs_fn_t fn
,
2738 const char *location
)
2740 struct share_mode_lock
*lck
= NULL
;
2742 smb_vfs_assert_allowed();
2744 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
2746 NTSTATUS status
= NT_STATUS_NOT_FOUND
;
2747 DBG_DEBUG("get_existing_share_mode_lock failed: %s\n",
2752 fn(lck
, private_data
);
2756 return NT_STATUS_OK
;