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"
60 #define DBGC_CLASS DBGC_LOCKING
62 #define NO_LOCKING_COUNT (-1)
64 /* the locking database handle */
65 static struct g_lock_ctx
*lock_ctx
;
67 static bool locking_init_internal(bool read_only
)
69 struct db_context
*backend
;
74 if (lock_ctx
!= NULL
) {
78 db_path
= lock_path(talloc_tos(), "locking.tdb");
79 if (db_path
== NULL
) {
83 backend
= db_open(NULL
, db_path
,
84 SMB_OPEN_DATABASE_TDB_HASH_SIZE
,
88 TDB_INCOMPATIBLE_HASH
|
90 read_only
?O_RDONLY
:O_RDWR
|O_CREAT
, 0644,
91 DBWRAP_LOCK_ORDER_NONE
,
95 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
99 lock_ctx
= g_lock_ctx_init_backend(
100 NULL
, global_messaging_context(), &backend
);
101 if (lock_ctx
== NULL
) {
102 TALLOC_FREE(backend
);
105 g_lock_set_lock_order(lock_ctx
, DBWRAP_LOCK_ORDER_1
);
107 if (!posix_locking_init(read_only
)) {
108 TALLOC_FREE(lock_ctx
);
115 bool locking_init(void)
117 return locking_init_internal(false);
120 bool locking_init_readonly(void)
122 return locking_init_internal(true);
125 /*******************************************************************
126 Deinitialize the share_mode management.
127 ******************************************************************/
129 bool locking_end(void)
132 TALLOC_FREE(lock_ctx
);
136 /*******************************************************************
137 Form a static locking key for a dev/inode pair.
138 ******************************************************************/
140 static TDB_DATA
locking_key(const struct file_id
*id
)
142 return make_tdb_data((const uint8_t *)id
, sizeof(*id
));
145 /*******************************************************************
146 Share mode cache utility functions that store/delete/retrieve
147 entries from memcache.
149 For now share the statcache (global cache) memory space. If
150 a lock record gets orphaned (which shouldn't happen as we're
151 using the same locking_key data as lookup) it will eventually
152 fall out of the cache via the normal LRU trim mechanism. If
153 necessary we can always make this a separate (smaller) cache.
154 ******************************************************************/
156 static DATA_BLOB
memcache_key(const struct file_id
*id
)
158 return data_blob_const((const void *)id
, sizeof(*id
));
161 static void share_mode_memcache_store(struct share_mode_data
*d
)
163 const DATA_BLOB key
= memcache_key(&d
->id
);
164 struct file_id_buf idbuf
;
166 DBG_DEBUG("stored entry for file %s epoch %"PRIx64
" key %s\n",
168 d
->unique_content_epoch
,
169 file_id_str_buf(d
->id
, &idbuf
));
171 /* Ensure everything stored in the cache is pristine. */
176 * Ensure the memory going into the cache
177 * doesn't have a destructor so it can be
178 * cleanly evicted by the memcache LRU
181 talloc_set_destructor(d
, NULL
);
183 /* Cache will own d after this call. */
184 memcache_add_talloc(NULL
,
185 SHARE_MODE_LOCK_CACHE
,
191 * NB. We use ndr_pull_hyper on a stack-created
192 * struct ndr_pull with no talloc allowed, as we
193 * need this to be really fast as an ndr-peek into
194 * the first 10 bytes of the blob.
197 static enum ndr_err_code
get_share_mode_blob_header(
198 const uint8_t *buf
, size_t buflen
, uint64_t *pepoch
, uint16_t *pflags
)
200 struct ndr_pull ndr
= {
201 .data
= discard_const_p(uint8_t, buf
),
204 NDR_CHECK(ndr_pull_hyper(&ndr
, NDR_SCALARS
, pepoch
));
205 NDR_CHECK(ndr_pull_uint16(&ndr
, NDR_SCALARS
, pflags
));
206 return NDR_ERR_SUCCESS
;
209 struct fsp_update_share_mode_flags_state
{
210 enum ndr_err_code ndr_err
;
211 uint16_t share_mode_flags
;
214 static void fsp_update_share_mode_flags_fn(
217 bool *modified_dependent
,
220 struct fsp_update_share_mode_flags_state
*state
= private_data
;
223 state
->ndr_err
= get_share_mode_blob_header(
224 buf
, buflen
, &seq
, &state
->share_mode_flags
);
227 static NTSTATUS
fsp_update_share_mode_flags(struct files_struct
*fsp
)
229 struct fsp_update_share_mode_flags_state state
= {0};
230 int seqnum
= g_lock_seqnum(lock_ctx
);
233 if (seqnum
== fsp
->share_mode_flags_seqnum
) {
237 status
= share_mode_do_locked(
238 fsp
->file_id
, fsp_update_share_mode_flags_fn
, &state
);
239 if (!NT_STATUS_IS_OK(status
)) {
240 DBG_DEBUG("share_mode_do_locked returned %s\n",
245 if (!NDR_ERR_CODE_IS_SUCCESS(state
.ndr_err
)) {
246 DBG_DEBUG("get_share_mode_blob_header returned %s\n",
247 ndr_errstr(state
.ndr_err
));
248 return ndr_map_error2ntstatus(state
.ndr_err
);
251 fsp
->share_mode_flags_seqnum
= seqnum
;
252 fsp
->share_mode_flags
= state
.share_mode_flags
;
257 bool file_has_read_lease(struct files_struct
*fsp
)
261 status
= fsp_update_share_mode_flags(fsp
);
262 if (!NT_STATUS_IS_OK(status
)) {
263 /* Safe default for leases */
267 return (fsp
->share_mode_flags
& SHARE_MODE_LEASE_READ
) != 0;
270 static int share_mode_data_nofree_destructor(struct share_mode_data
*d
)
275 static struct share_mode_data
*share_mode_memcache_fetch(
281 const DATA_BLOB key
= memcache_key(&id
);
282 enum ndr_err_code ndr_err
;
283 struct share_mode_data
*d
;
284 uint64_t unique_content_epoch
;
287 struct file_id_buf idbuf
;
289 ptr
= memcache_lookup_talloc(NULL
,
290 SHARE_MODE_LOCK_CACHE
,
293 DBG_DEBUG("failed to find entry for key %s\n",
294 file_id_str_buf(id
, &idbuf
));
297 /* sequence number key is at start of blob. */
298 ndr_err
= get_share_mode_blob_header(
299 buf
, buflen
, &unique_content_epoch
, &flags
);
300 if (ndr_err
!= NDR_ERR_SUCCESS
) {
301 /* Bad blob. Remove entry. */
302 DBG_DEBUG("bad blob %u key %s\n",
303 (unsigned int)ndr_err
,
304 file_id_str_buf(id
, &idbuf
));
305 memcache_delete(NULL
,
306 SHARE_MODE_LOCK_CACHE
,
311 d
= (struct share_mode_data
*)ptr
;
312 if (d
->unique_content_epoch
!= unique_content_epoch
) {
313 DBG_DEBUG("epoch changed (cached %"PRIx64
") (new %"PRIx64
") "
315 d
->unique_content_epoch
,
316 unique_content_epoch
,
317 file_id_str_buf(id
, &idbuf
));
318 /* Cache out of date. Remove entry. */
319 memcache_delete(NULL
,
320 SHARE_MODE_LOCK_CACHE
,
325 /* Move onto mem_ctx. */
326 d
= talloc_move(mem_ctx
, &ptr
);
329 * Now we own d, prevent the cache from freeing it
330 * when we delete the entry.
332 talloc_set_destructor(d
, share_mode_data_nofree_destructor
);
334 /* Remove from the cache. We own it now. */
335 memcache_delete(NULL
,
336 SHARE_MODE_LOCK_CACHE
,
339 /* And reset the destructor to none. */
340 talloc_set_destructor(d
, NULL
);
342 DBG_DEBUG("fetched entry for file %s epoch %"PRIx64
" key %s\n",
344 d
->unique_content_epoch
,
345 file_id_str_buf(id
, &idbuf
));
351 * 132 is the sizeof an ndr-encoded struct share_mode_entry_buf.
352 * Reading/writing entries will immediately error out if this
353 * size differs (push/pull is done without allocs).
356 struct share_mode_entry_buf
{
359 #define SHARE_MODE_ENTRY_SIZE (sizeof(struct share_mode_entry_buf))
361 static bool share_mode_entry_put(
362 const struct share_mode_entry
*e
,
363 struct share_mode_entry_buf
*dst
)
365 DATA_BLOB blob
= { .data
= dst
->buf
, .length
= sizeof(dst
->buf
) };
366 enum ndr_err_code ndr_err
;
368 if (DEBUGLEVEL
>=10) {
369 DBG_DEBUG("share_mode_entry:\n");
370 NDR_PRINT_DEBUG(share_mode_entry
, discard_const_p(void, e
));
373 ndr_err
= ndr_push_struct_into_fixed_blob(
376 (ndr_push_flags_fn_t
)ndr_push_share_mode_entry
);
377 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
378 DBG_WARNING("ndr_push_share_mode_entry failed: %s\n",
379 ndr_errstr(ndr_err
));
386 static bool share_mode_entry_get(
387 const uint8_t ptr
[SHARE_MODE_ENTRY_SIZE
], struct share_mode_entry
*e
)
389 enum ndr_err_code ndr_err
= NDR_ERR_SUCCESS
;
391 .data
= discard_const_p(uint8_t, ptr
),
392 .length
= SHARE_MODE_ENTRY_SIZE
,
395 ndr_err
= ndr_pull_struct_blob_all_noalloc(
396 &blob
, e
, (ndr_pull_flags_fn_t
)ndr_pull_share_mode_entry
);
397 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
398 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
405 * locking.tdb records consist of
407 * uint32_t share_mode_data_len
408 * uint8_t [share_mode_data] This is struct share_mode_data in NDR
410 * 0 [SHARE_MODE_ENTRY_SIZE] Sorted array of share modes,
411 * 1 [SHARE_MODE_ENTRY_SIZE] filling up the rest of the data in the
412 * 2 [SHARE_MODE_ENTRY_SIZE] g_lock.c maintained record in locking.tdb
415 struct locking_tdb_data
{
416 const uint8_t *share_mode_data_buf
;
417 size_t share_mode_data_len
;
418 const uint8_t *share_entries
;
419 size_t num_share_entries
;
422 static bool locking_tdb_data_get(
423 struct locking_tdb_data
*data
, const uint8_t *buf
, size_t buflen
)
425 uint32_t share_mode_data_len
, share_entries_len
;
428 *data
= (struct locking_tdb_data
) { 0 };
431 if (buflen
< sizeof(uint32_t)) {
435 share_mode_data_len
= PULL_LE_U32(buf
, 0);
437 buf
+= sizeof(uint32_t);
438 buflen
-= sizeof(uint32_t);
440 if (buflen
< share_mode_data_len
) {
444 share_entries_len
= buflen
- share_mode_data_len
;
446 if ((share_entries_len
% SHARE_MODE_ENTRY_SIZE
) != 0) {
450 *data
= (struct locking_tdb_data
) {
451 .share_mode_data_buf
= buf
,
452 .share_mode_data_len
= share_mode_data_len
,
453 .share_entries
= buf
+ share_mode_data_len
,
454 .num_share_entries
= share_entries_len
/ SHARE_MODE_ENTRY_SIZE
,
460 struct locking_tdb_data_fetch_state
{
466 static void locking_tdb_data_fetch_fn(
467 struct server_id exclusive
,
469 struct server_id
*shared
,
474 struct locking_tdb_data_fetch_state
*state
= private_data
;
475 state
->datalen
= datalen
;
476 state
->data
= talloc_memdup(state
->mem_ctx
, data
, datalen
);
479 static NTSTATUS
locking_tdb_data_fetch(
480 TDB_DATA key
, TALLOC_CTX
*mem_ctx
, struct locking_tdb_data
**ltdb
)
482 struct locking_tdb_data_fetch_state state
= { 0 };
483 struct locking_tdb_data
*result
= NULL
;
487 result
= talloc_zero(mem_ctx
, struct locking_tdb_data
);
488 if (result
== NULL
) {
489 return NT_STATUS_NO_MEMORY
;
491 state
.mem_ctx
= result
;
493 status
= g_lock_dump(lock_ctx
, key
, locking_tdb_data_fetch_fn
, &state
);
495 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
497 * Just return an empty record
501 if (!NT_STATUS_IS_OK(status
)) {
502 DBG_DEBUG("g_lock_dump failed: %s\n", nt_errstr(status
));
505 if (state
.datalen
== 0) {
509 ok
= locking_tdb_data_get(result
, state
.data
, state
.datalen
);
511 DBG_DEBUG("locking_tdb_data_get failed for %zu bytes\n",
514 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
522 static NTSTATUS
locking_tdb_data_store(
524 const struct locking_tdb_data
*ltdb
,
525 const TDB_DATA
*share_mode_dbufs
,
526 size_t num_share_mode_dbufs
)
528 uint8_t share_mode_data_len_buf
[4];
529 TDB_DATA dbufs
[num_share_mode_dbufs
+3];
532 if ((ltdb
->share_mode_data_len
== 0) &&
533 (ltdb
->num_share_entries
== 0) &&
534 (num_share_mode_dbufs
== 0)) {
538 status
= g_lock_write_data(lock_ctx
, key
, NULL
, 0);
539 if (!NT_STATUS_IS_OK(status
)) {
540 DBG_DEBUG("g_lock_writev_data() failed: %s\n",
546 PUSH_LE_U32(share_mode_data_len_buf
, 0, ltdb
->share_mode_data_len
);
548 dbufs
[0] = (TDB_DATA
) {
549 .dptr
= share_mode_data_len_buf
,
550 .dsize
= sizeof(share_mode_data_len_buf
),
552 dbufs
[1] = (TDB_DATA
) {
553 .dptr
= discard_const_p(uint8_t, ltdb
->share_mode_data_buf
),
554 .dsize
= ltdb
->share_mode_data_len
,
557 if (ltdb
->num_share_entries
> SIZE_MAX
/SHARE_MODE_ENTRY_SIZE
) {
559 return NT_STATUS_BUFFER_OVERFLOW
;
561 dbufs
[2] = (TDB_DATA
) {
562 .dptr
= discard_const_p(uint8_t, ltdb
->share_entries
),
563 .dsize
= ltdb
->num_share_entries
* SHARE_MODE_ENTRY_SIZE
,
566 if (num_share_mode_dbufs
!= 0) {
569 num_share_mode_dbufs
* sizeof(TDB_DATA
));
572 status
= g_lock_writev_data(lock_ctx
, key
, dbufs
, ARRAY_SIZE(dbufs
));
573 if (!NT_STATUS_IS_OK(status
)) {
574 DBG_DEBUG("g_lock_writev_data() failed: %s\n",
580 /*******************************************************************
581 Get all share mode entries for a dev/inode pair.
582 ********************************************************************/
584 static struct share_mode_data
*parse_share_modes(
590 struct share_mode_data
*d
;
591 enum ndr_err_code ndr_err
;
594 /* See if we already have a cached copy of this key. */
595 d
= share_mode_memcache_fetch(mem_ctx
, id
, buf
, buflen
);
600 d
= talloc(mem_ctx
, struct share_mode_data
);
602 DEBUG(0, ("talloc failed\n"));
607 .data
= discard_const_p(uint8_t, buf
),
610 ndr_err
= ndr_pull_struct_blob_all(
611 &blob
, d
, d
, (ndr_pull_flags_fn_t
)ndr_pull_share_mode_data
);
612 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
613 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
614 ndr_errstr(ndr_err
));
618 if (DEBUGLEVEL
>= 10) {
619 DEBUG(10, ("parse_share_modes:\n"));
620 NDR_PRINT_DEBUG(share_mode_data
, d
);
629 /*******************************************************************
630 If modified, store the share_mode_data back into the database.
631 ********************************************************************/
633 static NTSTATUS
share_mode_data_store(
634 struct share_mode_data
*d
, bool *have_share_entries
)
636 TDB_DATA key
= locking_key(&d
->id
);
637 struct locking_tdb_data
*ltdb
= NULL
;
638 DATA_BLOB blob
= { 0 };
642 DBG_DEBUG("not modified\n");
646 if (DEBUGLEVEL
>= 10) {
648 NDR_PRINT_DEBUG(share_mode_data
, d
);
651 d
->unique_content_epoch
= generate_unique_u64(d
->unique_content_epoch
);
653 status
= locking_tdb_data_fetch(key
, d
, <db
);
654 if (!NT_STATUS_IS_OK(status
)) {
658 if (ltdb
->num_share_entries
!= 0) {
659 enum ndr_err_code ndr_err
;
661 ndr_err
= ndr_push_struct_blob(
665 (ndr_push_flags_fn_t
)ndr_push_share_mode_data
);
666 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
667 DBG_DEBUG("ndr_push_share_mode_data failed: %s\n",
668 ndr_errstr(ndr_err
));
670 return ndr_map_error2ntstatus(ndr_err
);
673 *have_share_entries
= true;
676 ltdb
->share_mode_data_buf
= blob
.data
;
677 ltdb
->share_mode_data_len
= blob
.length
;
679 status
= locking_tdb_data_store(key
, ltdb
, NULL
, 0);
684 /*******************************************************************
685 Allocate a new share_mode_data struct, mark it unmodified.
686 fresh is set to note that currently there is no database entry.
687 ********************************************************************/
689 static struct share_mode_data
*fresh_share_mode_lock(
690 TALLOC_CTX
*mem_ctx
, const char *servicepath
,
691 const struct smb_filename
*smb_fname
,
692 const struct timespec
*old_write_time
)
694 struct share_mode_data
*d
;
696 if ((servicepath
== NULL
) || (smb_fname
== NULL
) ||
697 (old_write_time
== NULL
)) {
701 d
= talloc_zero(mem_ctx
, struct share_mode_data
);
705 d
->unique_content_epoch
= generate_unique_u64(0);
707 d
->base_name
= talloc_strdup(d
, smb_fname
->base_name
);
708 if (d
->base_name
== NULL
) {
711 if (smb_fname
->stream_name
!= NULL
) {
712 d
->stream_name
= talloc_strdup(d
, smb_fname
->stream_name
);
713 if (d
->stream_name
== NULL
) {
717 d
->servicepath
= talloc_strdup(d
, servicepath
);
718 if (d
->servicepath
== NULL
) {
721 d
->old_write_time
= full_timespec_to_nt_time(old_write_time
);
722 d
->flags
= SHARE_MODE_SHARE_DELETE
|
723 SHARE_MODE_SHARE_WRITE
|
724 SHARE_MODE_SHARE_READ
;
729 DEBUG(0, ("talloc failed\n"));
735 * Key that's locked with g_lock
737 static uint8_t share_mode_lock_key_data
[sizeof(struct file_id
)];
738 static TDB_DATA share_mode_lock_key
= {
739 .dptr
= share_mode_lock_key_data
,
740 .dsize
= sizeof(share_mode_lock_key_data
),
742 static size_t share_mode_lock_key_refcount
= 0;
745 * We can only ever have one share mode locked. Use a static
746 * share_mode_data pointer that is shared by multiple nested
747 * share_mode_lock structures, explicitly refcounted.
749 static struct share_mode_data
*static_share_mode_data
= NULL
;
750 static size_t static_share_mode_data_refcount
= 0;
752 /*******************************************************************
753 Either fetch a share mode from the database, or allocate a fresh
754 one if the record doesn't exist.
755 ********************************************************************/
757 struct get_static_share_mode_data_state
{
760 const char *servicepath
;
761 const struct smb_filename
*smb_fname
;
762 const struct timespec
*old_write_time
;
766 static void get_static_share_mode_data_fn(
767 struct server_id exclusive
,
769 struct server_id
*shared
,
774 struct get_static_share_mode_data_state
*state
= private_data
;
775 struct share_mode_data
*d
= NULL
;
776 struct locking_tdb_data ltdb
= { 0 };
781 ok
= locking_tdb_data_get(<db
, data
, datalen
);
783 DBG_DEBUG("locking_tdb_data_get failed\n");
784 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
789 if (ltdb
.share_mode_data_len
== 0) {
790 if (state
->smb_fname
== NULL
) {
791 state
->status
= NT_STATUS_NOT_FOUND
;
794 d
= fresh_share_mode_lock(
798 state
->old_write_time
);
800 state
->status
= NT_STATUS_NO_MEMORY
;
804 d
= parse_share_modes(
807 ltdb
.share_mode_data_buf
,
808 ltdb
.share_mode_data_len
);
810 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
816 static_share_mode_data
= d
;
819 static NTSTATUS
get_static_share_mode_data(
821 const char *servicepath
,
822 const struct smb_filename
*smb_fname
,
823 const struct timespec
*old_write_time
)
825 struct get_static_share_mode_data_state state
= {
828 .servicepath
= servicepath
,
829 .smb_fname
= smb_fname
,
830 .old_write_time
= old_write_time
,
834 SMB_ASSERT(static_share_mode_data
== NULL
);
836 status
= g_lock_dump(
839 get_static_share_mode_data_fn
,
841 if (!NT_STATUS_IS_OK(status
)) {
842 DBG_DEBUG("g_lock_dump failed: %s\n",
846 if (!NT_STATUS_IS_OK(state
.status
)) {
847 DBG_DEBUG("get_static_share_mode_data_fn failed: %s\n",
848 nt_errstr(state
.status
));
855 /*******************************************************************
856 Get a share_mode_lock, Reference counted to allow nested calls.
857 ********************************************************************/
859 static int share_mode_lock_destructor(struct share_mode_lock
*lck
);
861 struct share_mode_lock
*get_share_mode_lock(
864 const char *servicepath
,
865 const struct smb_filename
*smb_fname
,
866 const struct timespec
*old_write_time
)
868 TDB_DATA key
= locking_key(&id
);
869 struct share_mode_lock
*lck
= NULL
;
873 lck
= talloc(mem_ctx
, struct share_mode_lock
);
875 DEBUG(1, ("talloc failed\n"));
879 if (static_share_mode_data
!= NULL
) {
880 if (!file_id_equal(&static_share_mode_data
->id
, &id
)) {
881 DEBUG(1, ("Can not lock two share modes "
882 "simultaneously\n"));
888 if (share_mode_lock_key_refcount
== 0) {
889 status
= g_lock_lock(
893 (struct timeval
) { .tv_sec
= 3600 });
894 if (!NT_STATUS_IS_OK(status
)) {
895 DBG_DEBUG("g_lock_lock failed: %s\n",
899 memcpy(share_mode_lock_key_data
, key
.dptr
, key
.dsize
);
902 cmp
= tdb_data_cmp(share_mode_lock_key
, key
);
904 DBG_WARNING("Can not lock two share modes simultaneously\n");
908 SMB_ASSERT(share_mode_lock_key_refcount
< SIZE_MAX
);
909 share_mode_lock_key_refcount
+= 1;
911 SMB_ASSERT(static_share_mode_data_refcount
== 0);
913 status
= get_static_share_mode_data(
918 if (!NT_STATUS_IS_OK(status
)) {
919 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
921 share_mode_lock_key_refcount
-= 1;
925 static_share_mode_data_refcount
+= 1;
926 lck
->data
= static_share_mode_data
;
928 talloc_set_destructor(lck
, share_mode_lock_destructor
);
933 if (share_mode_lock_key_refcount
== 0) {
934 status
= g_lock_unlock(lock_ctx
, share_mode_lock_key
);
935 if (!NT_STATUS_IS_OK(status
)) {
936 DBG_ERR("g_lock_unlock failed: %s\n",
943 static int share_mode_lock_destructor(struct share_mode_lock
*lck
)
945 bool have_share_entries
= false;
948 SMB_ASSERT(static_share_mode_data_refcount
> 0);
949 static_share_mode_data_refcount
-= 1;
951 if (static_share_mode_data_refcount
> 0) {
955 status
= share_mode_data_store(
956 static_share_mode_data
, &have_share_entries
);
957 if (!NT_STATUS_IS_OK(status
)) {
958 DBG_ERR("share_mode_data_store failed: %s\n",
960 smb_panic("Could not store share mode data\n");
963 SMB_ASSERT(share_mode_lock_key_refcount
> 0);
964 share_mode_lock_key_refcount
-= 1;
966 if (share_mode_lock_key_refcount
== 0) {
967 status
= g_lock_unlock(lock_ctx
, share_mode_lock_key
);
968 if (!NT_STATUS_IS_OK(status
)) {
969 DBG_ERR("g_lock_unlock failed: %s\n",
971 smb_panic("Could not unlock share mode\n");
975 if (have_share_entries
) {
977 * This is worth keeping. Without share modes,
978 * share_mode_data_store above has left nothing in the
981 share_mode_memcache_store(static_share_mode_data
);
982 static_share_mode_data
= NULL
;
985 TALLOC_FREE(static_share_mode_data
);
989 struct share_mode_do_locked_state
{
991 void (*fn
)(const uint8_t *buf
,
993 bool *modified_dependent
,
998 static void share_mode_do_locked_fn(
999 struct server_id exclusive
,
1001 struct server_id
*shared
,
1002 const uint8_t *data
,
1006 struct share_mode_do_locked_state
*state
= private_data
;
1007 bool modified_dependent
= false;
1008 struct locking_tdb_data ltdb
= { 0 };
1011 ok
= locking_tdb_data_get(
1012 <db
, discard_const_p(uint8_t, data
), datalen
);
1014 DBG_WARNING("locking_tdb_data_get failed\n");
1018 state
->fn(ltdb
.share_mode_data_buf
,
1019 ltdb
.share_mode_data_len
,
1020 &modified_dependent
,
1021 state
->private_data
);
1023 if (modified_dependent
) {
1024 g_lock_wake_watchers(lock_ctx
, state
->key
);
1028 NTSTATUS
share_mode_do_locked(
1030 void (*fn
)(const uint8_t *buf
,
1032 bool *modified_dependent
,
1033 void *private_data
),
1036 TDB_DATA key
= locking_key(&id
);
1037 size_t data_refcount
, key_refcount
;
1038 struct share_mode_do_locked_state state
= {
1039 .key
= key
, .fn
= fn
, .private_data
= private_data
,
1043 if (share_mode_lock_key_refcount
== 0) {
1044 status
= g_lock_lock(
1048 (struct timeval
) { .tv_sec
= 3600 });
1049 if (!NT_STATUS_IS_OK(status
)) {
1050 DBG_DEBUG("g_lock_lock failed: %s\n",
1054 memcpy(share_mode_lock_key_data
, key
.dptr
, key
.dsize
);
1057 SMB_ASSERT(share_mode_lock_key_refcount
< SIZE_MAX
);
1058 share_mode_lock_key_refcount
+= 1;
1060 key_refcount
= share_mode_lock_key_refcount
;
1061 data_refcount
= static_share_mode_data_refcount
;
1063 status
= g_lock_dump(
1064 lock_ctx
, key
, share_mode_do_locked_fn
, &state
);
1065 if (!NT_STATUS_IS_OK(status
)) {
1066 DBG_DEBUG("g_lock_dump failed: %s\n",
1070 SMB_ASSERT(data_refcount
== static_share_mode_data_refcount
);
1071 SMB_ASSERT(key_refcount
== share_mode_lock_key_refcount
);
1072 share_mode_lock_key_refcount
-= 1;
1074 if (share_mode_lock_key_refcount
== 0) {
1075 status
= g_lock_unlock(lock_ctx
, key
);
1076 if (!NT_STATUS_IS_OK(status
)) {
1077 DBG_DEBUG("g_lock_unlock failed: %s\n",
1085 static void share_mode_wakeup_waiters_fn(
1088 bool *modified_dependent
,
1091 *modified_dependent
= true;
1094 NTSTATUS
share_mode_wakeup_waiters(struct file_id id
)
1096 return share_mode_do_locked(id
, share_mode_wakeup_waiters_fn
, NULL
);
1099 NTTIME
share_mode_changed_write_time(struct share_mode_lock
*lck
)
1101 return lck
->data
->changed_write_time
;
1104 const char *share_mode_servicepath(struct share_mode_lock
*lck
)
1106 return lck
->data
->servicepath
;
1109 char *share_mode_filename(TALLOC_CTX
*mem_ctx
, struct share_mode_lock
*lck
)
1111 struct share_mode_data
*d
= lck
->data
;
1112 bool has_stream
= (d
->stream_name
!= NULL
);
1115 fname
= talloc_asprintf(
1119 has_stream
? ":" : "",
1120 has_stream
? d
->stream_name
: "");
1124 char *share_mode_data_dump(
1125 TALLOC_CTX
*mem_ctx
, struct share_mode_lock
*lck
)
1127 struct ndr_print
*p
= talloc(mem_ctx
, struct ndr_print
);
1134 *p
= (struct ndr_print
) {
1135 .print
= ndr_print_string_helper
,
1137 .private_data
= talloc_strdup(mem_ctx
, ""),
1140 if (p
->private_data
== NULL
) {
1145 ndr_print_share_mode_data(p
, "SHARE_MODE_DATA", lck
->data
);
1147 ret
= p
->private_data
;
1154 void share_mode_flags_get(
1155 struct share_mode_lock
*lck
,
1156 uint32_t *access_mask
,
1157 uint32_t *share_mode
,
1158 uint32_t *lease_type
)
1160 uint16_t flags
= lck
->data
->flags
;
1162 if (access_mask
!= NULL
) {
1164 ((flags
& SHARE_MODE_ACCESS_READ
) ?
1165 FILE_READ_DATA
: 0) |
1166 ((flags
& SHARE_MODE_ACCESS_WRITE
) ?
1167 FILE_WRITE_DATA
: 0) |
1168 ((flags
& SHARE_MODE_ACCESS_DELETE
) ?
1171 if (share_mode
!= NULL
) {
1173 ((flags
& SHARE_MODE_SHARE_READ
) ?
1174 FILE_SHARE_READ
: 0) |
1175 ((flags
& SHARE_MODE_SHARE_WRITE
) ?
1176 FILE_SHARE_WRITE
: 0) |
1177 ((flags
& SHARE_MODE_SHARE_DELETE
) ?
1178 FILE_SHARE_DELETE
: 0);
1180 if (lease_type
!= NULL
) {
1182 ((flags
& SHARE_MODE_LEASE_READ
) ?
1183 SMB2_LEASE_READ
: 0) |
1184 ((flags
& SHARE_MODE_LEASE_WRITE
) ?
1185 SMB2_LEASE_WRITE
: 0) |
1186 ((flags
& SHARE_MODE_LEASE_HANDLE
) ?
1187 SMB2_LEASE_HANDLE
: 0);
1191 void share_mode_flags_set(
1192 struct share_mode_lock
*lck
,
1193 uint32_t access_mask
,
1194 uint32_t share_mode
,
1195 uint32_t lease_type
,
1198 struct share_mode_data
*d
= lck
->data
;
1201 flags
|= (access_mask
& (FILE_READ_DATA
| FILE_EXECUTE
)) ?
1202 SHARE_MODE_ACCESS_READ
: 0;
1203 flags
|= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ?
1204 SHARE_MODE_ACCESS_WRITE
: 0;
1205 flags
|= (access_mask
& (DELETE_ACCESS
)) ?
1206 SHARE_MODE_ACCESS_DELETE
: 0;
1208 flags
|= (share_mode
& FILE_SHARE_READ
) ?
1209 SHARE_MODE_SHARE_READ
: 0;
1210 flags
|= (share_mode
& FILE_SHARE_WRITE
) ?
1211 SHARE_MODE_SHARE_WRITE
: 0;
1212 flags
|= (share_mode
& FILE_SHARE_DELETE
) ?
1213 SHARE_MODE_SHARE_DELETE
: 0;
1215 flags
|= (lease_type
& SMB2_LEASE_READ
) ?
1216 SHARE_MODE_LEASE_READ
: 0;
1217 flags
|= (lease_type
& SMB2_LEASE_WRITE
) ?
1218 SHARE_MODE_LEASE_WRITE
: 0;
1219 flags
|= (lease_type
& SMB2_LEASE_HANDLE
) ?
1220 SHARE_MODE_LEASE_HANDLE
: 0;
1222 if (d
->flags
== flags
) {
1226 if (modified
!= NULL
) {
1233 struct share_mode_watch_state
{
1235 struct server_id blocker
;
1238 static void share_mode_watch_done(struct tevent_req
*subreq
);
1240 struct tevent_req
*share_mode_watch_send(
1241 TALLOC_CTX
*mem_ctx
,
1242 struct tevent_context
*ev
,
1243 struct share_mode_lock
*lck
,
1244 struct server_id blocker
)
1246 TDB_DATA key
= locking_key(&lck
->data
->id
);
1247 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1248 struct share_mode_watch_state
*state
= NULL
;
1250 req
= tevent_req_create(
1251 mem_ctx
, &state
, struct share_mode_watch_state
);
1256 subreq
= g_lock_watch_data_send(state
, ev
, lock_ctx
, key
, blocker
);
1257 if (tevent_req_nomem(subreq
, req
)) {
1258 return tevent_req_post(req
, ev
);
1260 tevent_req_set_callback(subreq
, share_mode_watch_done
, req
);
1264 static void share_mode_watch_done(struct tevent_req
*subreq
)
1266 struct tevent_req
*req
= tevent_req_callback_data(
1267 subreq
, struct tevent_req
);
1268 struct share_mode_watch_state
*state
= tevent_req_data(
1269 req
, struct share_mode_watch_state
);
1272 status
= g_lock_watch_data_recv(
1273 subreq
, &state
->blockerdead
, &state
->blocker
);
1274 if (tevent_req_nterror(req
, status
)) {
1277 tevent_req_done(req
);
1280 NTSTATUS
share_mode_watch_recv(
1281 struct tevent_req
*req
, bool *blockerdead
, struct server_id
*blocker
)
1283 struct share_mode_watch_state
*state
= tevent_req_data(
1284 req
, struct share_mode_watch_state
);
1287 if (tevent_req_is_nterror(req
, &status
)) {
1290 if (blockerdead
!= NULL
) {
1291 *blockerdead
= state
->blockerdead
;
1293 if (blocker
!= NULL
) {
1294 *blocker
= state
->blocker
;
1296 return NT_STATUS_OK
;
1299 struct fetch_share_mode_unlocked_state
{
1300 TALLOC_CTX
*mem_ctx
;
1302 struct share_mode_lock
*lck
;
1305 static void fetch_share_mode_unlocked_parser(
1306 struct server_id exclusive
,
1308 struct server_id
*shared
,
1309 const uint8_t *data
,
1313 struct fetch_share_mode_unlocked_state
*state
= private_data
;
1314 struct locking_tdb_data ltdb
= { 0 };
1317 bool ok
= locking_tdb_data_get(<db
, data
, datalen
);
1319 DBG_DEBUG("locking_tdb_data_get failed\n");
1324 if (ltdb
.share_mode_data_len
== 0) {
1325 /* Likely a ctdb tombstone record, ignore it */
1329 state
->lck
= talloc(state
->mem_ctx
, struct share_mode_lock
);
1330 if (state
->lck
== NULL
) {
1331 DEBUG(0, ("talloc failed\n"));
1335 state
->lck
->data
= parse_share_modes(
1338 ltdb
.share_mode_data_buf
,
1339 ltdb
.share_mode_data_len
);
1340 if (state
->lck
->data
== NULL
) {
1341 DBG_DEBUG("parse_share_modes failed\n");
1342 TALLOC_FREE(state
->lck
);
1346 /*******************************************************************
1347 Get a share_mode_lock without locking the database or reference
1348 counting. Used by smbstatus to display existing share modes.
1349 ********************************************************************/
1351 struct share_mode_lock
*fetch_share_mode_unlocked(TALLOC_CTX
*mem_ctx
,
1354 struct fetch_share_mode_unlocked_state state
= {
1358 TDB_DATA key
= locking_key(&id
);
1361 status
= g_lock_dump(
1362 lock_ctx
, key
, fetch_share_mode_unlocked_parser
, &state
);
1363 if (!NT_STATUS_IS_OK(status
)) {
1364 DBG_DEBUG("g_lock_dump failed: %s\n", nt_errstr(status
));
1370 struct fetch_share_mode_state
{
1372 struct share_mode_lock
*lck
;
1376 static void fetch_share_mode_fn(
1377 struct server_id exclusive
,
1379 struct server_id
*shared
,
1380 const uint8_t *data
,
1382 void *private_data
);
1383 static void fetch_share_mode_done(struct tevent_req
*subreq
);
1386 * @brief Get a share_mode_lock without locking or refcounting
1388 * This can be used in a clustered Samba environment where the async dbwrap
1389 * request is sent over a socket to the local ctdbd. If the send queue is full
1390 * and the caller was issuing multiple async dbwrap requests in a loop, the
1391 * caller knows it's probably time to stop sending requests for now and try
1394 * @param[in] mem_ctx The talloc memory context to use.
1396 * @param[in] ev The event context to work on.
1398 * @param[in] id The file id for the locking.tdb key
1400 * @param[out] queued This boolean out parameter tells the caller whether the
1401 * async request is blocked in a full send queue:
1403 * false := request is dispatched
1405 * true := send queue is full, request waiting to be
1408 * @return The new async request, NULL on error.
1410 struct tevent_req
*fetch_share_mode_send(TALLOC_CTX
*mem_ctx
,
1411 struct tevent_context
*ev
,
1415 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1416 struct fetch_share_mode_state
*state
= NULL
;
1420 req
= tevent_req_create(mem_ctx
, &state
,
1421 struct fetch_share_mode_state
);
1427 subreq
= g_lock_dump_send(
1432 fetch_share_mode_fn
,
1434 if (tevent_req_nomem(subreq
, req
)) {
1435 return tevent_req_post(req
, ev
);
1437 tevent_req_set_callback(subreq
, fetch_share_mode_done
, req
);
1441 static void fetch_share_mode_fn(
1442 struct server_id exclusive
,
1444 struct server_id
*shared
,
1445 const uint8_t *data
,
1449 struct fetch_share_mode_state
*state
= talloc_get_type_abort(
1450 private_data
, struct fetch_share_mode_state
);
1451 struct locking_tdb_data ltdb
= { 0 };
1454 bool ok
= locking_tdb_data_get(<db
, data
, datalen
);
1456 DBG_DEBUG("locking_tdb_data_get failed\n");
1461 if (ltdb
.share_mode_data_len
== 0) {
1462 /* Likely a ctdb tombstone record, ignore it */
1466 state
->lck
= talloc(state
, struct share_mode_lock
);
1467 if (state
->lck
== NULL
) {
1468 DBG_WARNING("talloc failed\n");
1469 state
->status
= NT_STATUS_NO_MEMORY
;
1473 state
->lck
->data
= parse_share_modes(
1476 ltdb
.share_mode_data_buf
,
1477 ltdb
.share_mode_data_len
);
1478 if (state
->lck
->data
== NULL
) {
1479 DBG_DEBUG("parse_share_modes failed\n");
1480 TALLOC_FREE(state
->lck
);
1484 static void fetch_share_mode_done(struct tevent_req
*subreq
)
1486 struct tevent_req
*req
= tevent_req_callback_data(
1487 subreq
, struct tevent_req
);
1488 struct fetch_share_mode_state
*state
= tevent_req_data(
1489 req
, struct fetch_share_mode_state
);
1492 status
= g_lock_dump_recv(subreq
);
1493 TALLOC_FREE(subreq
);
1494 if (tevent_req_nterror(req
, status
)) {
1497 if (tevent_req_nterror(req
, state
->status
)) {
1500 tevent_req_done(req
);
1503 NTSTATUS
fetch_share_mode_recv(struct tevent_req
*req
,
1504 TALLOC_CTX
*mem_ctx
,
1505 struct share_mode_lock
**_lck
)
1507 struct fetch_share_mode_state
*state
= tevent_req_data(
1508 req
, struct fetch_share_mode_state
);
1509 struct share_mode_lock
*lck
= NULL
;
1513 if (tevent_req_is_nterror(req
, &status
)) {
1514 tevent_req_received(req
);
1518 if (state
->lck
== NULL
) {
1519 tevent_req_received(req
);
1520 return NT_STATUS_NOT_FOUND
;
1523 lck
= talloc_move(mem_ctx
, &state
->lck
);
1525 if (DEBUGLEVEL
>= 10) {
1526 DBG_DEBUG("share_mode_data:\n");
1527 NDR_PRINT_DEBUG(share_mode_data
, lck
->data
);
1531 tevent_req_received(req
);
1532 return NT_STATUS_OK
;
1535 struct share_mode_forall_state
{
1537 int (*fn
)(struct file_id fid
,
1538 const struct share_mode_data
*data
,
1539 void *private_data
);
1543 static void share_mode_forall_dump_fn(
1544 struct server_id exclusive
,
1546 struct server_id
*shared
,
1547 const uint8_t *data
,
1551 struct share_mode_forall_state
*state
= private_data
;
1553 struct locking_tdb_data ltdb
= { 0 };
1555 struct share_mode_data
*d
;
1557 if (state
->key
.dsize
!= sizeof(fid
)) {
1558 DBG_DEBUG("Got invalid key length %zu\n", state
->key
.dsize
);
1561 memcpy(&fid
, state
->key
.dptr
, sizeof(fid
));
1563 ok
= locking_tdb_data_get(<db
, data
, datalen
);
1565 DBG_DEBUG("locking_tdb_data_get() failed\n");
1569 d
= parse_share_modes(
1572 ltdb
.share_mode_data_buf
,
1573 ltdb
.share_mode_data_len
);
1575 DBG_DEBUG("parse_share_modes() failed\n");
1579 state
->fn(fid
, d
, state
->private_data
);
1583 static int share_mode_forall_fn(TDB_DATA key
, void *private_data
)
1585 struct share_mode_forall_state
*state
= private_data
;
1590 status
= g_lock_dump(
1591 lock_ctx
, key
, share_mode_forall_dump_fn
, private_data
);
1592 if (!NT_STATUS_IS_OK(status
)) {
1593 DBG_DEBUG("g_lock_dump failed: %s\n",
1599 int share_mode_forall(int (*fn
)(struct file_id fid
,
1600 const struct share_mode_data
*data
,
1601 void *private_data
),
1604 struct share_mode_forall_state state
= {
1606 .private_data
= private_data
1610 if (lock_ctx
== NULL
) {
1615 lock_ctx
, share_mode_forall_fn
, &state
);
1617 DBG_DEBUG("g_lock_locks failed\n");
1622 struct share_entry_forall_state
{
1624 const struct share_mode_data
*data
;
1625 int (*fn
)(struct file_id fid
,
1626 const struct share_mode_data
*data
,
1627 const struct share_mode_entry
*entry
,
1628 void *private_data
);
1633 static bool share_entry_traverse_walker(
1634 struct share_mode_entry
*e
,
1638 struct share_entry_forall_state
*state
= private_data
;
1640 state
->ret
= state
->fn(
1641 state
->fid
, state
->data
, e
, state
->private_data
);
1642 return (state
->ret
!= 0);
1645 static int share_entry_traverse_fn(struct file_id fid
,
1646 const struct share_mode_data
*data
,
1649 struct share_entry_forall_state
*state
= private_data
;
1650 struct share_mode_lock lck
= {
1651 .data
= discard_const_p(struct share_mode_data
, data
)
1658 ok
= share_mode_forall_entries(
1659 &lck
, share_entry_traverse_walker
, state
);
1661 DBG_DEBUG("share_mode_forall_entries failed\n");
1668 /*******************************************************************
1669 Call the specified function on each entry under management by the
1671 ********************************************************************/
1673 int share_entry_forall(int (*fn
)(struct file_id fid
,
1674 const struct share_mode_data
*data
,
1675 const struct share_mode_entry
*entry
,
1676 void *private_data
),
1679 struct share_entry_forall_state state
= {
1680 .fn
= fn
, .private_data
= private_data
};
1682 return share_mode_forall(share_entry_traverse_fn
, &state
);
1685 static int share_mode_entry_cmp(
1686 struct server_id pid1
,
1687 uint64_t share_file_id1
,
1688 struct server_id pid2
,
1689 uint64_t share_file_id2
)
1693 cmp
= server_id_cmp(&pid1
, &pid2
);
1697 if (share_file_id1
!= share_file_id2
) {
1698 return (share_file_id1
< share_file_id2
) ? -1 : 1;
1703 static size_t share_mode_entry_find(
1704 const uint8_t *data
,
1705 size_t num_share_modes
,
1706 struct server_id pid
,
1707 uint64_t share_file_id
,
1708 struct share_mode_entry
*e
,
1711 ssize_t left
, right
, middle
;
1715 if (num_share_modes
== 0) {
1720 right
= (num_share_modes
-1);
1722 while (left
<= right
) {
1723 const uint8_t *middle_ptr
= NULL
;
1727 middle
= left
+ ((right
- left
) / 2);
1728 middle_ptr
= data
+ middle
* SHARE_MODE_ENTRY_SIZE
;
1730 DBG_DEBUG("left=%zu, right=%zu, middle=%zu, middle_ptr=%p\n",
1736 ok
= share_mode_entry_get(middle_ptr
, e
);
1738 DBG_DEBUG("share_mode_entry_get failed\n");
1742 cmp
= share_mode_entry_cmp(
1743 e
->pid
, e
->share_file_id
, pid
, share_file_id
);
1759 bool set_share_mode(struct share_mode_lock
*lck
,
1760 struct files_struct
*fsp
,
1764 uint32_t share_access
,
1765 uint32_t access_mask
)
1767 struct share_mode_data
*d
= lck
->data
;
1768 TDB_DATA key
= locking_key(&d
->id
);
1769 struct server_id my_pid
= messaging_server_id(
1770 fsp
->conn
->sconn
->msg_ctx
);
1771 struct locking_tdb_data
*ltdb
= NULL
;
1773 struct share_mode_entry e
= { .pid
.pid
= 0 };
1774 struct share_mode_entry_buf e_buf
;
1779 size_t num_dbufs
= 0;
1781 status
= locking_tdb_data_fetch(key
, talloc_tos(), <db
);
1782 if (!NT_STATUS_IS_OK(status
)) {
1783 DBG_DEBUG("locking_tdb_data_fetch failed: %s\n",
1787 DBG_DEBUG("num_share_modes=%zu\n", ltdb
->num_share_entries
);
1789 idx
= share_mode_entry_find(
1790 ltdb
->share_entries
,
1791 ltdb
->num_share_entries
,
1797 DBG_WARNING("Found duplicate share mode\n");
1798 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
1802 e
= (struct share_mode_entry
) {
1804 .share_access
= share_access
,
1805 .private_options
= fsp
->fh
->private_options
,
1806 .access_mask
= access_mask
,
1809 .time
.tv_sec
= fsp
->open_time
.tv_sec
,
1810 .time
.tv_usec
= fsp
->open_time
.tv_usec
,
1811 .share_file_id
= fsp
->fh
->gen_id
,
1812 .uid
= (uint32_t)uid
,
1813 .flags
= (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) ?
1814 SHARE_MODE_FLAG_POSIX_OPEN
: 0,
1815 .name_hash
= fsp
->name_hash
,
1818 if (op_type
== LEASE_OPLOCK
) {
1819 const struct GUID
*client_guid
= fsp_client_guid(fsp
);
1820 e
.client_guid
= *client_guid
;
1821 e
.lease_key
= fsp
->lease
->lease
.lease_key
;
1824 ok
= share_mode_entry_put(&e
, &e_buf
);
1826 DBG_DEBUG("share_mode_entry_put failed\n");
1827 status
= NT_STATUS_INTERNAL_ERROR
;
1831 DBG_DEBUG("idx=%zu, found=%d\n", idx
, (int)found
);
1834 dbufs
[num_dbufs
] = (TDB_DATA
) {
1835 .dptr
= discard_const_p(uint8_t, ltdb
->share_entries
),
1836 .dsize
= idx
* SHARE_MODE_ENTRY_SIZE
,
1841 dbufs
[num_dbufs
] = (TDB_DATA
) {
1842 .dptr
= e_buf
.buf
, .dsize
= SHARE_MODE_ENTRY_SIZE
,
1846 if (idx
< ltdb
->num_share_entries
) {
1847 size_t num_after_idx
= (ltdb
->num_share_entries
-idx
);
1848 dbufs
[num_dbufs
] = (TDB_DATA
) {
1849 .dptr
= discard_const_p(uint8_t, ltdb
->share_entries
) +
1850 idx
* SHARE_MODE_ENTRY_SIZE
,
1851 .dsize
= num_after_idx
* SHARE_MODE_ENTRY_SIZE
,
1858 for (i
=0; i
<num_dbufs
; i
++) {
1859 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
1866 if (num_dbufs
== 1) {
1868 * Storing a fresh record with just one share entry
1874 * If there was any existing data in
1875 * ltdb->share_entries, it's now been
1876 * moved and we've split it into:
1879 * dbufs[0] -> old sorted data less than new_entry
1880 * dbufs[1] -> new_share_mode_entry
1881 * dbufs[2] -> old sorted_data greater than new entry.
1883 * So the old data inside ltdb->share_entries is
1886 * If we're storing a brand new entry the
1890 * dbufs[0] -> new_share_mode_entry
1892 * Either way we must set ltdb->share_entries = NULL
1893 * and ltdb->num_share_entries = 0 so that
1894 * locking_tdb_data_store() doesn't use it to
1895 * store any data. It's no longer there.
1898 ltdb
->share_entries
= NULL
;
1899 ltdb
->num_share_entries
= 0;
1901 status
= locking_tdb_data_store(key
, ltdb
, dbufs
, num_dbufs
);
1902 if (!NT_STATUS_IS_OK(status
)) {
1903 DBG_DEBUG("locking_tdb_data_store failed: %s\n",
1908 return NT_STATUS_IS_OK(status
);
1911 static bool share_mode_for_one_entry(
1912 bool (*fn
)(struct share_mode_entry
*e
,
1914 void *private_data
),
1918 size_t *num_share_modes
,
1922 .data
= data
+ (*i
) * SHARE_MODE_ENTRY_SIZE
,
1923 .length
= SHARE_MODE_ENTRY_SIZE
,
1925 struct share_mode_entry e
= {.pid
.pid
=0};
1926 enum ndr_err_code ndr_err
= NDR_ERR_SUCCESS
;
1927 bool modified
= false;
1929 struct server_id e_pid
;
1930 uint64_t e_share_file_id
;
1932 ndr_err
= ndr_pull_struct_blob_all_noalloc(
1935 (ndr_pull_flags_fn_t
)ndr_pull_share_mode_entry
);
1936 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1937 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
1941 if (DEBUGLEVEL
>= 10) {
1942 DBG_DEBUG("entry[%zu]:\n", *i
);
1943 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
1947 e_share_file_id
= e
.share_file_id
;
1949 stop
= fn(&e
, &modified
, private_data
);
1951 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
1957 if (DEBUGLEVEL
>=10) {
1958 DBG_DEBUG("share_mode_entry:\n");
1959 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
1962 if (*i
< *num_share_modes
) {
1964 blob
.data
+ SHARE_MODE_ENTRY_SIZE
,
1965 (*num_share_modes
- *i
- 1) *
1966 SHARE_MODE_ENTRY_SIZE
);
1968 *num_share_modes
-= 1;
1974 if (DEBUGLEVEL
>=10) {
1975 DBG_DEBUG("share_mode_entry:\n");
1976 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
1980 * Make sure sorting order is kept intact
1982 SMB_ASSERT(server_id_equal(&e_pid
, &e
.pid
));
1983 SMB_ASSERT(e_share_file_id
== e
.share_file_id
);
1985 ndr_err
= ndr_push_struct_into_fixed_blob(
1988 (ndr_push_flags_fn_t
)
1989 ndr_push_share_mode_entry
);
1990 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1991 DBG_WARNING("ndr_push_share_mode_entry "
1993 ndr_errstr(ndr_err
));
1995 * Not much we can do, just ignore it
2011 bool share_mode_forall_entries(
2012 struct share_mode_lock
*lck
,
2013 bool (*fn
)(struct share_mode_entry
*e
,
2015 void *private_data
),
2018 struct share_mode_data
*d
= lck
->data
;
2019 TDB_DATA key
= locking_key(&d
->id
);
2020 struct locking_tdb_data
*ltdb
= NULL
;
2021 uint8_t *share_entries
= NULL
;
2022 size_t num_share_entries
;
2023 bool writeback
= false;
2028 status
= locking_tdb_data_fetch(key
, talloc_tos(), <db
);
2029 if (!NT_STATUS_IS_OK(status
)) {
2030 DBG_DEBUG("locking_tdb_data_fetch failed: %s\n",
2034 DBG_DEBUG("num_share_modes=%zu\n", ltdb
->num_share_entries
);
2036 num_share_entries
= ltdb
->num_share_entries
;
2037 share_entries
= discard_const_p(uint8_t, ltdb
->share_entries
);
2040 while (i
<num_share_entries
) {
2041 stop
= share_mode_for_one_entry(
2053 DBG_DEBUG("num_share_entries=%zu, writeback=%d\n",
2061 if ((ltdb
->num_share_entries
!= 0 ) && (num_share_entries
== 0)) {
2063 * This routine wiped all share entries, let
2064 * share_mode_data_store() delete the record
2069 ltdb
->num_share_entries
= num_share_entries
;
2070 ltdb
->share_entries
= share_entries
;
2072 status
= locking_tdb_data_store(key
, ltdb
, NULL
, 0);
2073 if (!NT_STATUS_IS_OK(status
)) {
2074 DBG_DEBUG("locking_tdb_data_store failed: %s\n",
2082 struct share_mode_count_entries_state
{
2083 size_t num_share_modes
;
2087 static void share_mode_count_entries_fn(
2088 struct server_id exclusive
,
2090 struct server_id
*shared
,
2091 const uint8_t *data
,
2095 struct share_mode_count_entries_state
*state
= private_data
;
2096 struct locking_tdb_data ltdb
= { 0 };
2099 ok
= locking_tdb_data_get(<db
, data
, datalen
);
2101 DBG_WARNING("locking_tdb_data_get failed for %zu\n", datalen
);
2102 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
2105 state
->num_share_modes
= ltdb
.num_share_entries
;
2106 state
->status
= NT_STATUS_OK
;
2109 NTSTATUS
share_mode_count_entries(struct file_id fid
, size_t *num_share_modes
)
2111 struct share_mode_count_entries_state state
= {
2112 .status
= NT_STATUS_NOT_FOUND
,
2116 status
= g_lock_dump(
2119 share_mode_count_entries_fn
,
2121 if (!NT_STATUS_IS_OK(status
)) {
2122 DBG_DEBUG("g_lock_dump failed: %s\n",
2126 if (!NT_STATUS_IS_OK(state
.status
)) {
2127 DBG_DEBUG("share_mode_count_entries_fn failed: %s\n",
2128 nt_errstr(state
.status
));
2129 return state
.status
;
2132 *num_share_modes
= state
.num_share_modes
;
2133 return NT_STATUS_OK
;
2136 static bool share_mode_entry_do(
2137 struct share_mode_lock
*lck
,
2138 struct server_id pid
,
2139 uint64_t share_file_id
,
2140 void (*fn
)(struct share_mode_entry
*e
,
2141 size_t num_share_modes
,
2143 void *private_data
),
2146 struct share_mode_data
*d
= lck
->data
;
2147 TDB_DATA key
= locking_key(&d
->id
);
2148 struct locking_tdb_data
*ltdb
= NULL
;
2152 struct share_mode_entry e
;
2153 uint8_t *e_ptr
= NULL
;
2154 bool had_share_entries
, have_share_entries
;
2158 status
= locking_tdb_data_fetch(key
, talloc_tos(), <db
);
2159 if (!NT_STATUS_IS_OK(status
)) {
2160 DBG_DEBUG("locking_tdb_data_fetch failed: %s\n",
2164 DBG_DEBUG("num_share_modes=%zu\n", ltdb
->num_share_entries
);
2166 had_share_entries
= (ltdb
->num_share_entries
!= 0);
2168 idx
= share_mode_entry_find(
2169 ltdb
->share_entries
,
2170 ltdb
->num_share_entries
,
2176 DBG_WARNING("Did not find share mode entry for %"PRIu64
"\n",
2181 if (DEBUGLEVEL
>=10) {
2182 DBG_DEBUG("entry[%zu]:\n", idx
);
2183 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
2186 fn(&e
, ltdb
->num_share_entries
, &modified
, private_data
);
2188 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2193 if (!e
.stale
&& !modified
) {
2198 e_ptr
= discard_const_p(uint8_t, ltdb
->share_entries
) +
2199 idx
* SHARE_MODE_ENTRY_SIZE
;
2203 * Move the rest down one entry
2205 size_t behind
= ltdb
->num_share_entries
- idx
- 1;
2208 e_ptr
+ SHARE_MODE_ENTRY_SIZE
,
2209 behind
* SHARE_MODE_ENTRY_SIZE
);
2211 ltdb
->num_share_entries
-= 1;
2213 if (DEBUGLEVEL
>=10) {
2214 DBG_DEBUG("share_mode_entry:\n");
2215 NDR_PRINT_DEBUG(share_mode_entry
, &e
);
2218 struct share_mode_entry_buf buf
;
2221 if (ltdb
->num_share_entries
!= 1) {
2223 * Make sure the sorting order stays intact
2225 SMB_ASSERT(server_id_equal(&e
.pid
, &pid
));
2226 SMB_ASSERT(e
.share_file_id
== share_file_id
);
2229 ok
= share_mode_entry_put(&e
, &buf
);
2231 DBG_DEBUG("share_mode_entry_put failed\n");
2234 memcpy(e_ptr
, buf
.buf
, SHARE_MODE_ENTRY_SIZE
);
2237 status
= locking_tdb_data_store(key
, ltdb
, NULL
, 0);
2238 if (!NT_STATUS_IS_OK(status
)) {
2239 DBG_DEBUG("locking_tdb_data_store failed: %s\n",
2244 have_share_entries
= (ltdb
->num_share_entries
!= 0);
2245 if (had_share_entries
!= have_share_entries
) {
2247 * Make share_mode_data_store do the right thing wrt
2248 * possibly deleting the locking.tdb record
2259 struct del_share_mode_state
{
2263 static void del_share_mode_fn(
2264 struct share_mode_entry
*e
,
2265 size_t num_share_modes
,
2269 struct del_share_mode_state
*state
= private_data
;
2274 bool del_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
)
2276 struct del_share_mode_state state
= { .ok
= false };
2279 ok
= share_mode_entry_do(
2281 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
2286 DBG_DEBUG("share_mode_entry_do failed\n");
2290 DBG_DEBUG("del_share_mode_fn failed\n");
2296 struct remove_share_oplock_state
{
2300 static void remove_share_oplock_fn(
2301 struct share_mode_entry
*e
,
2302 size_t num_share_modes
,
2306 struct remove_share_oplock_state
*state
= private_data
;
2308 e
->op_type
= NO_OPLOCK
;
2313 bool remove_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
2315 struct remove_share_oplock_state state
= { .ok
= false };
2318 ok
= share_mode_entry_do(
2320 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
2322 remove_share_oplock_fn
,
2325 DBG_DEBUG("share_mode_entry_do failed\n");
2329 DBG_DEBUG("remove_share_oplock_fn failed\n");
2333 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
2334 remove_lease_if_stale(
2336 fsp_client_guid(fsp
),
2337 &fsp
->lease
->lease
.lease_key
);
2340 share_mode_wakeup_waiters(fsp
->file_id
);
2345 struct downgrade_share_oplock_state
{
2349 static void downgrade_share_oplock_fn(
2350 struct share_mode_entry
*e
,
2351 size_t num_share_modes
,
2355 struct downgrade_share_oplock_state
*state
= private_data
;
2357 e
->op_type
= LEVEL_II_OPLOCK
;
2362 bool downgrade_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
2364 struct downgrade_share_oplock_state state
= { .ok
= false };
2367 ok
= share_mode_entry_do(
2369 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
2371 downgrade_share_oplock_fn
,
2374 DBG_DEBUG("share_mode_entry_do failed\n");
2378 DBG_DEBUG("downgrade_share_oplock_fn failed\n");
2382 lck
->data
->flags
|= SHARE_MODE_LEASE_READ
;
2383 lck
->data
->modified
= true;
2388 struct mark_share_mode_disconnected_state
{
2389 uint64_t open_persistent_id
;
2393 static void mark_share_mode_disconnected_fn(
2394 struct share_mode_entry
*e
,
2395 size_t num_share_modes
,
2399 struct mark_share_mode_disconnected_state
*state
= private_data
;
2401 if (num_share_modes
!= 1) {
2406 server_id_set_disconnected(&e
->pid
);
2407 e
->share_file_id
= state
->open_persistent_id
;
2412 bool mark_share_mode_disconnected(struct share_mode_lock
*lck
,
2413 struct files_struct
*fsp
)
2415 struct mark_share_mode_disconnected_state state
;
2418 if (fsp
->op
== NULL
) {
2421 if (!fsp
->op
->global
->durable
) {
2425 state
= (struct mark_share_mode_disconnected_state
) {
2426 .open_persistent_id
= fsp
->op
->global
->open_persistent_id
,
2429 ok
= share_mode_entry_do(
2431 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
2433 mark_share_mode_disconnected_fn
,
2436 DBG_DEBUG("share_mode_entry_do failed\n");
2440 DBG_DEBUG("mark_share_mode_disconnected_fn failed\n");
2444 lck
->data
->modified
= true;
2448 bool reset_share_mode_entry(
2449 struct share_mode_lock
*lck
,
2450 struct server_id old_pid
,
2451 uint64_t old_share_file_id
,
2452 struct server_id new_pid
,
2454 uint64_t new_share_file_id
)
2456 struct share_mode_data
*d
= lck
->data
;
2457 TDB_DATA key
= locking_key(&d
->id
);
2458 struct locking_tdb_data
*ltdb
= NULL
;
2459 struct share_mode_entry e
;
2460 struct share_mode_entry_buf e_buf
;
2465 status
= locking_tdb_data_fetch(key
, talloc_tos(), <db
);
2466 if (!NT_STATUS_IS_OK(status
)) {
2467 DBG_DEBUG("locking_tdb_data_fetch failed: %s\n",
2472 if (ltdb
->num_share_entries
!= 1) {
2473 DBG_DEBUG("num_share_modes=%zu\n", ltdb
->num_share_entries
);
2477 ok
= share_mode_entry_get(ltdb
->share_entries
, &e
);
2479 DBG_WARNING("share_mode_entry_get failed\n");
2483 ret
= share_mode_entry_cmp(
2484 old_pid
, old_share_file_id
, e
.pid
, e
.share_file_id
);
2486 struct server_id_buf tmp1
, tmp2
;
2487 DBG_WARNING("Expected pid=%s, file_id=%"PRIu64
", "
2488 "got pid=%s, file_id=%"PRIu64
"\n",
2489 server_id_str_buf(old_pid
, &tmp1
),
2491 server_id_str_buf(e
.pid
, &tmp2
),
2498 e
.share_file_id
= new_share_file_id
;
2500 ok
= share_mode_entry_put(&e
, &e_buf
);
2502 DBG_WARNING("share_mode_entry_put failed\n");
2506 ltdb
->share_entries
= e_buf
.buf
;
2508 status
= locking_tdb_data_store(key
, ltdb
, NULL
, 0);
2509 if (!NT_STATUS_IS_OK(status
)) {
2510 DBG_DEBUG("locking_tdb_data_store failed: %s\n",