smbd: Remove "have_share_modes" from "struct share_mode_data"
[Samba.git] / source3 / locking / share_mode_lock.c
blob8ad9341a765c907a7dc84719675419981863ea7c
1 /*
2 Unix SMB/CIFS implementation.
3 Locking functions
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/>.
21 Revision History:
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
30 support.
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.
38 #include "includes.h"
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"
49 #include "serverid.h"
50 #include "messages.h"
51 #include "util_tdb.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"
57 #include "g_lock.h"
59 #undef DBGC_CLASS
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;
70 char *db_path;
72 brl_init(read_only);
74 if (lock_ctx != NULL) {
75 return True;
78 db_path = lock_path(talloc_tos(), "locking.tdb");
79 if (db_path == NULL) {
80 return false;
83 backend = db_open(NULL, db_path,
84 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
85 TDB_DEFAULT|
86 TDB_VOLATILE|
87 TDB_CLEAR_IF_FIRST|
88 TDB_INCOMPATIBLE_HASH|
89 TDB_SEQNUM,
90 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
91 DBWRAP_LOCK_ORDER_NONE,
92 DBWRAP_FLAG_NONE);
93 TALLOC_FREE(db_path);
94 if (!backend) {
95 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
96 return False;
99 lock_ctx = g_lock_ctx_init_backend(
100 NULL, global_messaging_context(), &backend);
101 if (lock_ctx == NULL) {
102 TALLOC_FREE(backend);
103 return false;
105 g_lock_set_lock_order(lock_ctx, DBWRAP_LOCK_ORDER_1);
107 if (!posix_locking_init(read_only)) {
108 TALLOC_FREE(lock_ctx);
109 return False;
112 return True;
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)
131 brl_shutdown();
132 TALLOC_FREE(lock_ctx);
133 return true;
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",
167 d->base_name,
168 d->unique_content_epoch,
169 file_id_str_buf(d->id, &idbuf));
171 /* Ensure everything stored in the cache is pristine. */
172 d->modified = false;
173 d->fresh = false;
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
179 * mechanism.
181 talloc_set_destructor(d, NULL);
183 /* Cache will own d after this call. */
184 memcache_add_talloc(NULL,
185 SHARE_MODE_LOCK_CACHE,
186 key,
187 &d);
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),
202 .data_size = buflen,
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(
215 const uint8_t *buf,
216 size_t buflen,
217 bool *modified_dependent,
218 void *private_data)
220 struct fsp_update_share_mode_flags_state *state = private_data;
221 uint64_t seq;
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);
231 NTSTATUS status;
233 if (seqnum == fsp->share_mode_flags_seqnum) {
234 return NT_STATUS_OK;
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",
241 nt_errstr(status));
242 return status;
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;
254 return NT_STATUS_OK;
257 bool file_has_read_lease(struct files_struct *fsp)
259 NTSTATUS status;
261 status = fsp_update_share_mode_flags(fsp);
262 if (!NT_STATUS_IS_OK(status)) {
263 /* Safe default for leases */
264 return true;
267 return (fsp->share_mode_flags & SHARE_MODE_LEASE_READ) != 0;
270 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
272 return -1;
275 static struct share_mode_data *share_mode_memcache_fetch(
276 TALLOC_CTX *mem_ctx,
277 struct file_id id,
278 const uint8_t *buf,
279 size_t buflen)
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;
285 uint16_t flags;
286 void *ptr;
287 struct file_id_buf idbuf;
289 ptr = memcache_lookup_talloc(NULL,
290 SHARE_MODE_LOCK_CACHE,
291 key);
292 if (ptr == NULL) {
293 DBG_DEBUG("failed to find entry for key %s\n",
294 file_id_str_buf(id, &idbuf));
295 return NULL;
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,
307 key);
308 return NULL;
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") "
314 "for key %s\n",
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,
321 key);
322 return NULL;
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,
337 key);
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",
343 d->base_name,
344 d->unique_content_epoch,
345 file_id_str_buf(id, &idbuf));
347 return d;
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 {
357 uint8_t buf[132];
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(
374 &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));
380 return false;
383 return true;
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;
390 DATA_BLOB blob = {
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");
399 return false;
401 return true;
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;
427 if (buflen == 0) {
428 *data = (struct locking_tdb_data) { 0 };
429 return true;
431 if (buflen < sizeof(uint32_t)) {
432 return false;
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) {
441 return false;
444 share_entries_len = buflen - share_mode_data_len;
446 if ((share_entries_len % SHARE_MODE_ENTRY_SIZE) != 0) {
447 return false;
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,
457 return true;
460 struct locking_tdb_data_fetch_state {
461 TALLOC_CTX *mem_ctx;
462 uint8_t *data;
463 size_t datalen;
466 static void locking_tdb_data_fetch_fn(
467 struct server_id exclusive,
468 size_t num_shared,
469 struct server_id *shared,
470 const uint8_t *data,
471 size_t datalen,
472 void *private_data)
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;
484 NTSTATUS status;
485 bool ok;
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
499 goto done;
501 if (!NT_STATUS_IS_OK(status)) {
502 DBG_DEBUG("g_lock_dump failed: %s\n", nt_errstr(status));
503 return status;
505 if (state.datalen == 0) {
506 goto done;
509 ok = locking_tdb_data_get(result, state.data, state.datalen);
510 if (!ok) {
511 DBG_DEBUG("locking_tdb_data_get failed for %zu bytes\n",
512 state.datalen);
513 TALLOC_FREE(result);
514 return NT_STATUS_INTERNAL_DB_CORRUPTION;
517 done:
518 *ltdb = result;
519 return NT_STATUS_OK;
522 static NTSTATUS locking_tdb_data_store(
523 TDB_DATA key,
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];
530 NTSTATUS status;
532 if ((ltdb->share_mode_data_len == 0) &&
533 (ltdb->num_share_entries == 0) &&
534 (num_share_mode_dbufs == 0)) {
536 * Nothing to write
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",
541 nt_errstr(status));
543 return status;
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) {
558 /* overflow */
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) {
567 memcpy(&dbufs[3],
568 share_mode_dbufs,
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",
575 nt_errstr(status));
577 return status;
580 /*******************************************************************
581 Get all share mode entries for a dev/inode pair.
582 ********************************************************************/
584 static struct share_mode_data *parse_share_modes(
585 TALLOC_CTX *mem_ctx,
586 struct file_id id,
587 const uint8_t *buf,
588 size_t buflen)
590 struct share_mode_data *d;
591 enum ndr_err_code ndr_err;
592 DATA_BLOB blob;
594 /* See if we already have a cached copy of this key. */
595 d = share_mode_memcache_fetch(mem_ctx, id, buf, buflen);
596 if (d != NULL) {
597 return d;
600 d = talloc(mem_ctx, struct share_mode_data);
601 if (d == NULL) {
602 DEBUG(0, ("talloc failed\n"));
603 goto fail;
606 blob = (DATA_BLOB) {
607 .data = discard_const_p(uint8_t, buf),
608 .length = buflen,
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));
615 goto fail;
618 if (DEBUGLEVEL >= 10) {
619 DEBUG(10, ("parse_share_modes:\n"));
620 NDR_PRINT_DEBUG(share_mode_data, d);
623 return d;
624 fail:
625 TALLOC_FREE(d);
626 return NULL;
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 };
639 NTSTATUS status;
641 if (!d->modified) {
642 DBG_DEBUG("not modified\n");
643 return NT_STATUS_OK;
646 if (DEBUGLEVEL >= 10) {
647 DBG_DEBUG("\n");
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, &ltdb);
654 if (!NT_STATUS_IS_OK(status)) {
655 return status;
658 if (ltdb->num_share_entries != 0) {
659 enum ndr_err_code ndr_err;
661 ndr_err = ndr_push_struct_blob(
662 &blob,
663 ltdb,
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));
669 TALLOC_FREE(ltdb);
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);
680 TALLOC_FREE(ltdb);
681 return status;
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)) {
698 return NULL;
701 d = talloc_zero(mem_ctx, struct share_mode_data);
702 if (d == NULL) {
703 goto fail;
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) {
709 goto fail;
711 if (smb_fname->stream_name != NULL) {
712 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
713 if (d->stream_name == NULL) {
714 goto fail;
717 d->servicepath = talloc_strdup(d, servicepath);
718 if (d->servicepath == NULL) {
719 goto fail;
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;
725 d->modified = false;
726 d->fresh = true;
727 return d;
728 fail:
729 DEBUG(0, ("talloc failed\n"));
730 TALLOC_FREE(d);
731 return NULL;
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 {
758 TALLOC_CTX *mem_ctx;
759 struct file_id id;
760 const char *servicepath;
761 const struct smb_filename *smb_fname;
762 const struct timespec *old_write_time;
763 NTSTATUS status;
766 static void get_static_share_mode_data_fn(
767 struct server_id exclusive,
768 size_t num_shared,
769 struct server_id *shared,
770 const uint8_t *data,
771 size_t datalen,
772 void *private_data)
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 };
778 if (datalen != 0) {
779 bool ok;
781 ok = locking_tdb_data_get(&ltdb, data, datalen);
782 if (!ok) {
783 DBG_DEBUG("locking_tdb_data_get failed\n");
784 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
785 return;
789 if (ltdb.share_mode_data_len == 0) {
790 if (state->smb_fname == NULL) {
791 state->status = NT_STATUS_NOT_FOUND;
792 return;
794 d = fresh_share_mode_lock(
795 state->mem_ctx,
796 state->servicepath,
797 state->smb_fname,
798 state->old_write_time);
799 if (d == NULL) {
800 state->status = NT_STATUS_NO_MEMORY;
801 return;
803 } else {
804 d = parse_share_modes(
805 lock_ctx,
806 state->id,
807 ltdb.share_mode_data_buf,
808 ltdb.share_mode_data_len);
809 if (d == NULL) {
810 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
811 return;
815 d->id = state->id;
816 static_share_mode_data = d;
819 static NTSTATUS get_static_share_mode_data(
820 struct file_id id,
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 = {
826 .mem_ctx = lock_ctx,
827 .id = id,
828 .servicepath = servicepath,
829 .smb_fname = smb_fname,
830 .old_write_time = old_write_time,
832 NTSTATUS status;
834 SMB_ASSERT(static_share_mode_data == NULL);
836 status = g_lock_dump(
837 lock_ctx,
838 share_mode_lock_key,
839 get_static_share_mode_data_fn,
840 &state);
841 if (!NT_STATUS_IS_OK(status)) {
842 DBG_DEBUG("g_lock_dump failed: %s\n",
843 nt_errstr(status));
844 return status;
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));
849 return state.status;
852 return NT_STATUS_OK;
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(
862 TALLOC_CTX *mem_ctx,
863 struct file_id id,
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;
870 NTSTATUS status;
871 int cmp;
873 lck = talloc(mem_ctx, struct share_mode_lock);
874 if (lck == NULL) {
875 DEBUG(1, ("talloc failed\n"));
876 return NULL;
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"));
883 goto fail;
885 goto done;
888 if (share_mode_lock_key_refcount == 0) {
889 status = g_lock_lock(
890 lock_ctx,
891 key,
892 G_LOCK_WRITE,
893 (struct timeval) { .tv_sec = 3600 });
894 if (!NT_STATUS_IS_OK(status)) {
895 DBG_DEBUG("g_lock_lock failed: %s\n",
896 nt_errstr(status));
897 goto fail;
899 memcpy(share_mode_lock_key_data, key.dptr, key.dsize);
902 cmp = tdb_data_cmp(share_mode_lock_key, key);
903 if (cmp != 0) {
904 DBG_WARNING("Can not lock two share modes simultaneously\n");
905 goto fail;
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(
915 servicepath,
916 smb_fname,
917 old_write_time);
918 if (!NT_STATUS_IS_OK(status)) {
919 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
920 nt_errstr(status));
921 share_mode_lock_key_refcount -= 1;
922 goto fail;
924 done:
925 static_share_mode_data_refcount += 1;
926 lck->data = static_share_mode_data;
928 talloc_set_destructor(lck, share_mode_lock_destructor);
930 return lck;
931 fail:
932 TALLOC_FREE(lck);
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",
937 nt_errstr(status));
940 return NULL;
943 static int share_mode_lock_destructor(struct share_mode_lock *lck)
945 bool have_share_entries = false;
946 NTSTATUS status;
948 SMB_ASSERT(static_share_mode_data_refcount > 0);
949 static_share_mode_data_refcount -= 1;
951 if (static_share_mode_data_refcount > 0) {
952 return 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",
959 nt_errstr(status));
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",
970 nt_errstr(status));
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
979 * database.
981 share_mode_memcache_store(static_share_mode_data);
982 static_share_mode_data = NULL;
985 TALLOC_FREE(static_share_mode_data);
986 return 0;
989 struct share_mode_do_locked_state {
990 TDB_DATA key;
991 void (*fn)(const uint8_t *buf,
992 size_t buflen,
993 bool *modified_dependent,
994 void *private_data);
995 void *private_data;
998 static void share_mode_do_locked_fn(
999 struct server_id exclusive,
1000 size_t num_shared,
1001 struct server_id *shared,
1002 const uint8_t *data,
1003 size_t datalen,
1004 void *private_data)
1006 struct share_mode_do_locked_state *state = private_data;
1007 bool modified_dependent = false;
1008 struct locking_tdb_data ltdb = { 0 };
1009 bool ok;
1011 ok = locking_tdb_data_get(
1012 &ltdb, discard_const_p(uint8_t, data), datalen);
1013 if (!ok) {
1014 DBG_WARNING("locking_tdb_data_get failed\n");
1015 return;
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(
1029 struct file_id id,
1030 void (*fn)(const uint8_t *buf,
1031 size_t buflen,
1032 bool *modified_dependent,
1033 void *private_data),
1034 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,
1041 NTSTATUS status;
1043 if (share_mode_lock_key_refcount == 0) {
1044 status = g_lock_lock(
1045 lock_ctx,
1046 key,
1047 G_LOCK_WRITE,
1048 (struct timeval) { .tv_sec = 3600 });
1049 if (!NT_STATUS_IS_OK(status)) {
1050 DBG_DEBUG("g_lock_lock failed: %s\n",
1051 nt_errstr(status));
1052 return status;
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",
1067 nt_errstr(status));
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",
1078 nt_errstr(status));
1082 return status;
1085 static void share_mode_wakeup_waiters_fn(
1086 const uint8_t *buf,
1087 size_t buflen,
1088 bool *modified_dependent,
1089 void *private_data)
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);
1113 char *fname = NULL;
1115 fname = talloc_asprintf(
1116 mem_ctx,
1117 "%s%s%s",
1118 d->base_name,
1119 has_stream ? ":" : "",
1120 has_stream ? d->stream_name : "");
1121 return fname;
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);
1128 char *ret = NULL;
1130 if (p == NULL) {
1131 return NULL;
1134 *p = (struct ndr_print) {
1135 .print = ndr_print_string_helper,
1136 .depth = 1,
1137 .private_data = talloc_strdup(mem_ctx, ""),
1140 if (p->private_data == NULL) {
1141 TALLOC_FREE(p);
1142 return NULL;
1145 ndr_print_share_mode_data(p, "SHARE_MODE_DATA", lck->data);
1147 ret = p->private_data;
1149 TALLOC_FREE(p);
1151 return ret;
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) {
1163 *access_mask =
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) ?
1169 DELETE_ACCESS : 0);
1171 if (share_mode != NULL) {
1172 *share_mode =
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) {
1181 *lease_type =
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,
1196 bool *modified)
1198 struct share_mode_data *d = lck->data;
1199 uint16_t flags = 0;
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) {
1223 return;
1226 if (modified != NULL) {
1227 *modified = true;
1229 d->flags = flags;
1230 d->modified = true;
1233 struct share_mode_watch_state {
1234 bool blockerdead;
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);
1252 if (req == NULL) {
1253 return NULL;
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);
1261 return 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);
1270 NTSTATUS status;
1272 status = g_lock_watch_data_recv(
1273 subreq, &state->blockerdead, &state->blocker);
1274 if (tevent_req_nterror(req, status)) {
1275 return;
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);
1285 NTSTATUS status;
1287 if (tevent_req_is_nterror(req, &status)) {
1288 return 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;
1301 struct file_id id;
1302 struct share_mode_lock *lck;
1305 static void fetch_share_mode_unlocked_parser(
1306 struct server_id exclusive,
1307 size_t num_shared,
1308 struct server_id *shared,
1309 const uint8_t *data,
1310 size_t datalen,
1311 void *private_data)
1313 struct fetch_share_mode_unlocked_state *state = private_data;
1314 struct locking_tdb_data ltdb = { 0 };
1316 if (datalen != 0) {
1317 bool ok = locking_tdb_data_get(&ltdb, data, datalen);
1318 if (!ok) {
1319 DBG_DEBUG("locking_tdb_data_get failed\n");
1320 return;
1324 if (ltdb.share_mode_data_len == 0) {
1325 /* Likely a ctdb tombstone record, ignore it */
1326 return;
1329 state->lck = talloc(state->mem_ctx, struct share_mode_lock);
1330 if (state->lck == NULL) {
1331 DEBUG(0, ("talloc failed\n"));
1332 return;
1335 state->lck->data = parse_share_modes(
1336 state->lck,
1337 state->id,
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,
1352 struct file_id id)
1354 struct fetch_share_mode_unlocked_state state = {
1355 .mem_ctx = mem_ctx,
1356 .id = id,
1358 TDB_DATA key = locking_key(&id);
1359 NTSTATUS status;
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));
1365 return NULL;
1367 return state.lck;
1370 struct fetch_share_mode_state {
1371 struct file_id id;
1372 struct share_mode_lock *lck;
1373 NTSTATUS status;
1376 static void fetch_share_mode_fn(
1377 struct server_id exclusive,
1378 size_t num_shared,
1379 struct server_id *shared,
1380 const uint8_t *data,
1381 size_t datalen,
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
1392 * again later.
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
1406 * dispatched
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,
1412 struct file_id id,
1413 bool *queued)
1415 struct tevent_req *req = NULL, *subreq = NULL;
1416 struct fetch_share_mode_state *state = NULL;
1418 *queued = false;
1420 req = tevent_req_create(mem_ctx, &state,
1421 struct fetch_share_mode_state);
1422 if (req == NULL) {
1423 return NULL;
1425 state->id = id;
1427 subreq = g_lock_dump_send(
1428 state,
1430 lock_ctx,
1431 locking_key(&id),
1432 fetch_share_mode_fn,
1433 state);
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);
1438 return req;
1441 static void fetch_share_mode_fn(
1442 struct server_id exclusive,
1443 size_t num_shared,
1444 struct server_id *shared,
1445 const uint8_t *data,
1446 size_t datalen,
1447 void *private_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 };
1453 if (datalen != 0) {
1454 bool ok = locking_tdb_data_get(&ltdb, data, datalen);
1455 if (!ok) {
1456 DBG_DEBUG("locking_tdb_data_get failed\n");
1457 return;
1461 if (ltdb.share_mode_data_len == 0) {
1462 /* Likely a ctdb tombstone record, ignore it */
1463 return;
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;
1470 return;
1473 state->lck->data = parse_share_modes(
1474 state->lck,
1475 state->id,
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);
1490 NTSTATUS status;
1492 status = g_lock_dump_recv(subreq);
1493 TALLOC_FREE(subreq);
1494 if (tevent_req_nterror(req, status)) {
1495 return;
1497 if (tevent_req_nterror(req, state->status)) {
1498 return;
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;
1511 NTSTATUS status;
1513 if (tevent_req_is_nterror(req, &status)) {
1514 tevent_req_received(req);
1515 return status;
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);
1530 *_lck = lck;
1531 tevent_req_received(req);
1532 return NT_STATUS_OK;
1535 struct share_mode_forall_state {
1536 TDB_DATA key;
1537 int (*fn)(struct file_id fid,
1538 const struct share_mode_data *data,
1539 void *private_data);
1540 void *private_data;
1543 static void share_mode_forall_dump_fn(
1544 struct server_id exclusive,
1545 size_t num_shared,
1546 struct server_id *shared,
1547 const uint8_t *data,
1548 size_t datalen,
1549 void *private_data)
1551 struct share_mode_forall_state *state = private_data;
1552 struct file_id fid;
1553 struct locking_tdb_data ltdb = { 0 };
1554 bool ok;
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);
1559 return;
1561 memcpy(&fid, state->key.dptr, sizeof(fid));
1563 ok = locking_tdb_data_get(&ltdb, data, datalen);
1564 if (!ok) {
1565 DBG_DEBUG("locking_tdb_data_get() failed\n");
1566 return;
1569 d = parse_share_modes(
1570 talloc_tos(),
1571 fid,
1572 ltdb.share_mode_data_buf,
1573 ltdb.share_mode_data_len);
1574 if (d == NULL) {
1575 DBG_DEBUG("parse_share_modes() failed\n");
1576 return;
1579 state->fn(fid, d, state->private_data);
1580 TALLOC_FREE(d);
1583 static int share_mode_forall_fn(TDB_DATA key, void *private_data)
1585 struct share_mode_forall_state *state = private_data;
1586 NTSTATUS status;
1588 state->key = key;
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",
1594 nt_errstr(status));
1596 return 0;
1599 int share_mode_forall(int (*fn)(struct file_id fid,
1600 const struct share_mode_data *data,
1601 void *private_data),
1602 void *private_data)
1604 struct share_mode_forall_state state = {
1605 .fn = fn,
1606 .private_data = private_data
1608 int ret;
1610 if (lock_ctx == NULL) {
1611 return 0;
1614 ret = g_lock_locks(
1615 lock_ctx, share_mode_forall_fn, &state);
1616 if (ret < 0) {
1617 DBG_DEBUG("g_lock_locks failed\n");
1619 return ret;
1622 struct share_entry_forall_state {
1623 struct file_id fid;
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);
1629 void *private_data;
1630 int ret;
1633 static bool share_entry_traverse_walker(
1634 struct share_mode_entry *e,
1635 bool *modified,
1636 void *private_data)
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,
1647 void *private_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)
1653 bool ok;
1655 state->fid = fid;
1656 state->data = data;
1658 ok = share_mode_forall_entries(
1659 &lck, share_entry_traverse_walker, state);
1660 if (!ok) {
1661 DBG_DEBUG("share_mode_forall_entries failed\n");
1662 return false;
1665 return state->ret;
1668 /*******************************************************************
1669 Call the specified function on each entry under management by the
1670 share mode system.
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),
1677 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)
1691 int cmp;
1693 cmp = server_id_cmp(&pid1, &pid2);
1694 if (cmp != 0) {
1695 return cmp;
1697 if (share_file_id1 != share_file_id2) {
1698 return (share_file_id1 < share_file_id2) ? -1 : 1;
1700 return 0;
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,
1709 bool *match)
1711 ssize_t left, right, middle;
1713 *match = false;
1715 if (num_share_modes == 0) {
1716 return 0;
1719 left = 0;
1720 right = (num_share_modes-1);
1722 while (left <= right) {
1723 const uint8_t *middle_ptr = NULL;
1724 int cmp;
1725 bool ok;
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",
1731 left,
1732 right,
1733 middle,
1734 middle_ptr);
1736 ok = share_mode_entry_get(middle_ptr, e);
1737 if (!ok) {
1738 DBG_DEBUG("share_mode_entry_get failed\n");
1739 return 0;
1742 cmp = share_mode_entry_cmp(
1743 e->pid, e->share_file_id, pid, share_file_id);
1744 if (cmp == 0) {
1745 *match = true;
1746 return middle;
1749 if (cmp < 0) {
1750 right = middle-1;
1751 } else {
1752 left = middle+1;
1756 return left;
1759 bool set_share_mode(struct share_mode_lock *lck,
1760 struct files_struct *fsp,
1761 uid_t uid,
1762 uint64_t mid,
1763 uint16_t op_type,
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;
1772 size_t idx;
1773 struct share_mode_entry e = { .pid.pid = 0 };
1774 struct share_mode_entry_buf e_buf;
1775 NTSTATUS status;
1776 bool ok, found;
1778 TDB_DATA dbufs[3];
1779 size_t num_dbufs = 0;
1781 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
1782 if (!NT_STATUS_IS_OK(status)) {
1783 DBG_DEBUG("locking_tdb_data_fetch failed: %s\n",
1784 nt_errstr(status));
1785 return false;
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,
1792 my_pid,
1793 fsp->fh->gen_id,
1795 &found);
1796 if (found) {
1797 DBG_WARNING("Found duplicate share mode\n");
1798 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1799 goto done;
1802 e = (struct share_mode_entry) {
1803 .pid = my_pid,
1804 .share_access = share_access,
1805 .private_options = fsp->fh->private_options,
1806 .access_mask = access_mask,
1807 .op_mid = mid,
1808 .op_type = op_type,
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);
1825 if (!ok) {
1826 DBG_DEBUG("share_mode_entry_put failed\n");
1827 status = NT_STATUS_INTERNAL_ERROR;
1828 goto done;
1831 DBG_DEBUG("idx=%zu, found=%d\n", idx, (int)found);
1833 if (idx > 0) {
1834 dbufs[num_dbufs] = (TDB_DATA) {
1835 .dptr = discard_const_p(uint8_t, ltdb->share_entries),
1836 .dsize = idx * SHARE_MODE_ENTRY_SIZE,
1838 num_dbufs += 1;
1841 dbufs[num_dbufs] = (TDB_DATA) {
1842 .dptr = e_buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
1844 num_dbufs += 1;
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,
1853 num_dbufs += 1;
1857 size_t i;
1858 for (i=0; i<num_dbufs; i++) {
1859 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
1861 dbufs[i].dptr,
1862 dbufs[i].dsize);
1866 if (num_dbufs == 1) {
1868 * Storing a fresh record with just one share entry
1870 d->modified = true;
1874 * If there was any existing data in
1875 * ltdb->share_entries, it's now been
1876 * moved and we've split it into:
1878 * num_dbufs = 3
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
1884 * no longer valid.
1886 * If we're storing a brand new entry the
1887 * dbufs look like:
1889 * num_dbufs = 1
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",
1904 nt_errstr(status));
1906 done:
1907 TALLOC_FREE(ltdb);
1908 return NT_STATUS_IS_OK(status);
1911 static bool share_mode_for_one_entry(
1912 bool (*fn)(struct share_mode_entry *e,
1913 bool *modified,
1914 void *private_data),
1915 void *private_data,
1916 size_t *i,
1917 uint8_t *data,
1918 size_t *num_share_modes,
1919 bool *writeback)
1921 DATA_BLOB blob = {
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;
1928 bool stop = false;
1929 struct server_id e_pid;
1930 uint64_t e_share_file_id;
1932 ndr_err = ndr_pull_struct_blob_all_noalloc(
1933 &blob,
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");
1938 *i += 1;
1939 return false;
1941 if (DEBUGLEVEL >= 10) {
1942 DBG_DEBUG("entry[%zu]:\n", *i);
1943 NDR_PRINT_DEBUG(share_mode_entry, &e);
1946 e_pid = e.pid;
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",
1953 (int)modified,
1954 (int)e.stale);
1956 if (e.stale) {
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) {
1963 memmove(blob.data,
1964 blob.data + SHARE_MODE_ENTRY_SIZE,
1965 (*num_share_modes - *i - 1) *
1966 SHARE_MODE_ENTRY_SIZE);
1968 *num_share_modes -= 1;
1969 *writeback = true;
1970 return stop;
1973 if (modified) {
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(
1986 &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 "
1992 "failed: %s\n",
1993 ndr_errstr(ndr_err));
1995 * Not much we can do, just ignore it
1998 *i += 1;
1999 *writeback = true;
2000 return stop;
2003 if (stop) {
2004 return true;
2007 *i += 1;
2008 return false;
2011 bool share_mode_forall_entries(
2012 struct share_mode_lock *lck,
2013 bool (*fn)(struct share_mode_entry *e,
2014 bool *modified,
2015 void *private_data),
2016 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;
2024 NTSTATUS status;
2025 bool stop = false;
2026 size_t i;
2028 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2029 if (!NT_STATUS_IS_OK(status)) {
2030 DBG_DEBUG("locking_tdb_data_fetch failed: %s\n",
2031 nt_errstr(status));
2032 return false;
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);
2039 i = 0;
2040 while (i<num_share_entries) {
2041 stop = share_mode_for_one_entry(
2043 private_data,
2045 share_entries,
2046 &num_share_entries,
2047 &writeback);
2048 if (stop) {
2049 break;
2053 DBG_DEBUG("num_share_entries=%zu, writeback=%d\n",
2054 num_share_entries,
2055 (int)writeback);
2057 if (!writeback) {
2058 return true;
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
2066 d->modified = true;
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",
2075 nt_errstr(status));
2076 return false;
2079 return true;
2082 struct share_mode_count_entries_state {
2083 size_t num_share_modes;
2084 NTSTATUS status;
2087 static void share_mode_count_entries_fn(
2088 struct server_id exclusive,
2089 size_t num_shared,
2090 struct server_id *shared,
2091 const uint8_t *data,
2092 size_t datalen,
2093 void *private_data)
2095 struct share_mode_count_entries_state *state = private_data;
2096 struct locking_tdb_data ltdb = { 0 };
2097 bool ok;
2099 ok = locking_tdb_data_get(&ltdb, data, datalen);
2100 if (!ok) {
2101 DBG_WARNING("locking_tdb_data_get failed for %zu\n", datalen);
2102 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
2103 return;
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,
2114 NTSTATUS status;
2116 status = g_lock_dump(
2117 lock_ctx,
2118 locking_key(&fid),
2119 share_mode_count_entries_fn,
2120 &state);
2121 if (!NT_STATUS_IS_OK(status)) {
2122 DBG_DEBUG("g_lock_dump failed: %s\n",
2123 nt_errstr(status));
2124 return status;
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,
2142 bool *modified,
2143 void *private_data),
2144 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;
2149 size_t idx;
2150 bool found = false;
2151 bool modified;
2152 struct share_mode_entry e;
2153 uint8_t *e_ptr = NULL;
2154 bool had_share_entries, have_share_entries;
2155 NTSTATUS status;
2156 bool ret = false;
2158 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2159 if (!NT_STATUS_IS_OK(status)) {
2160 DBG_DEBUG("locking_tdb_data_fetch failed: %s\n",
2161 nt_errstr(status));
2162 return false;
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,
2171 pid,
2172 share_file_id,
2174 &found);
2175 if (!found) {
2176 DBG_WARNING("Did not find share mode entry for %"PRIu64"\n",
2177 share_file_id);
2178 goto done;
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",
2189 idx,
2190 (int)modified,
2191 (int)e.stale);
2193 if (!e.stale && !modified) {
2194 ret = true;
2195 goto done;
2198 e_ptr = discard_const_p(uint8_t, ltdb->share_entries) +
2199 idx * SHARE_MODE_ENTRY_SIZE;
2201 if (e.stale) {
2203 * Move the rest down one entry
2205 size_t behind = ltdb->num_share_entries - idx - 1;
2206 if (behind != 0) {
2207 memmove(e_ptr,
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);
2217 } else {
2218 struct share_mode_entry_buf buf;
2219 bool ok;
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);
2230 if (!ok) {
2231 DBG_DEBUG("share_mode_entry_put failed\n");
2232 goto done;
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",
2240 nt_errstr(status));
2241 goto done;
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
2250 d->modified = true;
2253 ret = true;
2254 done:
2255 TALLOC_FREE(ltdb);
2256 return ret;
2259 struct del_share_mode_state {
2260 bool ok;
2263 static void del_share_mode_fn(
2264 struct share_mode_entry *e,
2265 size_t num_share_modes,
2266 bool *modified,
2267 void *private_data)
2269 struct del_share_mode_state *state = private_data;
2270 e->stale = true;
2271 state->ok = true;
2274 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
2276 struct del_share_mode_state state = { .ok = false };
2277 bool ok;
2279 ok = share_mode_entry_do(
2280 lck,
2281 messaging_server_id(fsp->conn->sconn->msg_ctx),
2282 fsp->fh->gen_id,
2283 del_share_mode_fn,
2284 &state);
2285 if (!ok) {
2286 DBG_DEBUG("share_mode_entry_do failed\n");
2287 return false;
2289 if (!state.ok) {
2290 DBG_DEBUG("del_share_mode_fn failed\n");
2291 return false;
2293 return true;
2296 struct remove_share_oplock_state {
2297 bool ok;
2300 static void remove_share_oplock_fn(
2301 struct share_mode_entry *e,
2302 size_t num_share_modes,
2303 bool *modified,
2304 void *private_data)
2306 struct remove_share_oplock_state *state = private_data;
2308 e->op_type = NO_OPLOCK;
2309 *modified = true;
2310 state->ok = true;
2313 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2315 struct remove_share_oplock_state state = { .ok = false };
2316 bool ok;
2318 ok = share_mode_entry_do(
2319 lck,
2320 messaging_server_id(fsp->conn->sconn->msg_ctx),
2321 fsp->fh->gen_id,
2322 remove_share_oplock_fn,
2323 &state);
2324 if (!ok) {
2325 DBG_DEBUG("share_mode_entry_do failed\n");
2326 return false;
2328 if (!state.ok) {
2329 DBG_DEBUG("remove_share_oplock_fn failed\n");
2330 return false;
2333 if (fsp->oplock_type == LEASE_OPLOCK) {
2334 remove_lease_if_stale(
2335 lck,
2336 fsp_client_guid(fsp),
2337 &fsp->lease->lease.lease_key);
2340 share_mode_wakeup_waiters(fsp->file_id);
2342 return true;
2345 struct downgrade_share_oplock_state {
2346 bool ok;
2349 static void downgrade_share_oplock_fn(
2350 struct share_mode_entry *e,
2351 size_t num_share_modes,
2352 bool *modified,
2353 void *private_data)
2355 struct downgrade_share_oplock_state *state = private_data;
2357 e->op_type = LEVEL_II_OPLOCK;
2358 *modified = true;
2359 state->ok = true;
2362 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2364 struct downgrade_share_oplock_state state = { .ok = false };
2365 bool ok;
2367 ok = share_mode_entry_do(
2368 lck,
2369 messaging_server_id(fsp->conn->sconn->msg_ctx),
2370 fsp->fh->gen_id,
2371 downgrade_share_oplock_fn,
2372 &state);
2373 if (!ok) {
2374 DBG_DEBUG("share_mode_entry_do failed\n");
2375 return false;
2377 if (!state.ok) {
2378 DBG_DEBUG("downgrade_share_oplock_fn failed\n");
2379 return false;
2382 lck->data->flags |= SHARE_MODE_LEASE_READ;
2383 lck->data->modified = true;
2385 return true;
2388 struct mark_share_mode_disconnected_state {
2389 uint64_t open_persistent_id;
2390 bool ok;
2393 static void mark_share_mode_disconnected_fn(
2394 struct share_mode_entry *e,
2395 size_t num_share_modes,
2396 bool *modified,
2397 void *private_data)
2399 struct mark_share_mode_disconnected_state *state = private_data;
2401 if (num_share_modes != 1) {
2402 state->ok = false;
2403 return;
2406 server_id_set_disconnected(&e->pid);
2407 e->share_file_id = state->open_persistent_id;
2408 *modified = true;
2409 state->ok = true;
2412 bool mark_share_mode_disconnected(struct share_mode_lock *lck,
2413 struct files_struct *fsp)
2415 struct mark_share_mode_disconnected_state state;
2416 bool ok;
2418 if (fsp->op == NULL) {
2419 return false;
2421 if (!fsp->op->global->durable) {
2422 return false;
2425 state = (struct mark_share_mode_disconnected_state) {
2426 .open_persistent_id = fsp->op->global->open_persistent_id,
2429 ok = share_mode_entry_do(
2430 lck,
2431 messaging_server_id(fsp->conn->sconn->msg_ctx),
2432 fsp->fh->gen_id,
2433 mark_share_mode_disconnected_fn,
2434 &state);
2435 if (!ok) {
2436 DBG_DEBUG("share_mode_entry_do failed\n");
2437 return false;
2439 if (!state.ok) {
2440 DBG_DEBUG("mark_share_mode_disconnected_fn failed\n");
2441 return false;
2444 lck->data->modified = true;
2445 return 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,
2453 uint64_t new_mid,
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;
2461 NTSTATUS status;
2462 bool ret = false;
2463 bool ok;
2465 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2466 if (!NT_STATUS_IS_OK(status)) {
2467 DBG_DEBUG("locking_tdb_data_fetch failed: %s\n",
2468 nt_errstr(status));
2469 return false;
2472 if (ltdb->num_share_entries != 1) {
2473 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2474 goto done;
2477 ok = share_mode_entry_get(ltdb->share_entries, &e);
2478 if (!ok) {
2479 DBG_WARNING("share_mode_entry_get failed\n");
2480 goto done;
2483 ret = share_mode_entry_cmp(
2484 old_pid, old_share_file_id, e.pid, e.share_file_id);
2485 if (ret != 0) {
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),
2490 old_share_file_id,
2491 server_id_str_buf(e.pid, &tmp2),
2492 e.share_file_id);
2493 goto done;
2496 e.pid = new_pid;
2497 e.op_mid = new_mid;
2498 e.share_file_id = new_share_file_id;
2500 ok = share_mode_entry_put(&e, &e_buf);
2501 if (!ok) {
2502 DBG_WARNING("share_mode_entry_put failed\n");
2503 goto done;
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",
2511 nt_errstr(status));
2512 goto done;
2515 d->modified = true;
2516 ret = true;
2517 done:
2518 TALLOC_FREE(ltdb);
2519 return true;