smbd: Make reopen_from_fsp() public
[samba4-gss.git] / source3 / locking / share_mode_lock.c
blob8ccb3bdeaecf319549463632a986595ae731418a
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 "lib/util/time_basic.h"
40 #include "system/filesys.h"
41 #include "lib/util/server_id.h"
42 #include "share_mode_lock_private.h"
43 struct share_mode_lock {
44 struct file_id id;
45 struct share_mode_data *cached_data;
47 #define SHARE_MODE_ENTRY_PREPARE_STATE_LCK_SPACE 1
48 #include "share_mode_lock.h"
49 #include "locking/proto.h"
50 #include "smbd/globals.h"
51 #include "dbwrap/dbwrap.h"
52 #include "dbwrap/dbwrap_open.h"
53 #include "dbwrap/dbwrap_private.h"
54 #include "../libcli/security/security.h"
55 #include "serverid.h"
56 #include "messages.h"
57 #include "util_tdb.h"
58 #include "../librpc/gen_ndr/ndr_open_files.h"
59 #include "source3/lib/dbwrap/dbwrap_watch.h"
60 #include "locking/leases_db.h"
61 #include "../lib/util/memcache.h"
62 #include "lib/util/tevent_ntstatus.h"
63 #include "g_lock.h"
64 #include "smbd/fd_handle.h"
65 #include "lib/global_contexts.h"
67 #undef DBGC_CLASS
68 #define DBGC_CLASS DBGC_LOCKING
70 #define DBG_GET_SHARE_MODE_LOCK(__status, ...) \
71 DBG_PREFIX( \
72 NT_STATUS_EQUAL(__status, NT_STATUS_NOT_FOUND) ? \
73 DBGLVL_DEBUG : DBGLVL_ERR, \
74 (__VA_ARGS__))
76 /* the locking database handle */
77 static struct g_lock_ctx *lock_ctx;
78 static struct g_lock_lock_cb_state *current_share_mode_glck = NULL;
80 static bool share_mode_g_lock_within_cb(TDB_DATA key);
82 static NTSTATUS share_mode_g_lock_dump(TDB_DATA key,
83 void (*fn)(struct server_id exclusive,
84 size_t num_shared,
85 const struct server_id *shared,
86 const uint8_t *data,
87 size_t datalen,
88 void *private_data),
89 void *private_data)
91 if (share_mode_g_lock_within_cb(key)) {
92 return g_lock_lock_cb_dump(current_share_mode_glck,
93 fn, private_data);
96 return g_lock_dump(lock_ctx, key, fn, private_data);
99 static NTSTATUS share_mode_g_lock_writev(TDB_DATA key,
100 const TDB_DATA *dbufs,
101 size_t num_dbufs)
103 if (share_mode_g_lock_within_cb(key)) {
104 return g_lock_lock_cb_writev(current_share_mode_glck,
105 dbufs, num_dbufs);
108 return g_lock_writev_data(lock_ctx, key, dbufs, num_dbufs);
111 static bool locking_init_internal(bool read_only)
113 struct db_context *backend;
114 char *db_path;
116 brl_init(read_only);
118 if (lock_ctx != NULL) {
119 return True;
122 db_path = lock_path(talloc_tos(), "locking.tdb");
123 if (db_path == NULL) {
124 return false;
127 backend = db_open(NULL, db_path,
128 SMBD_VOLATILE_TDB_HASH_SIZE,
129 SMBD_VOLATILE_TDB_FLAGS |
130 TDB_SEQNUM,
131 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
132 DBWRAP_LOCK_ORDER_NONE,
133 DBWRAP_FLAG_NONE);
134 TALLOC_FREE(db_path);
135 if (!backend) {
136 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
137 return False;
140 lock_ctx = g_lock_ctx_init_backend(
141 NULL, global_messaging_context(), &backend);
142 if (lock_ctx == NULL) {
143 TALLOC_FREE(backend);
144 return false;
146 g_lock_set_lock_order(lock_ctx, DBWRAP_LOCK_ORDER_1);
148 if (!posix_locking_init(read_only)) {
149 TALLOC_FREE(lock_ctx);
150 return False;
153 return True;
156 bool locking_init(void)
158 return locking_init_internal(false);
161 bool locking_init_readonly(void)
163 return locking_init_internal(true);
166 /*******************************************************************
167 Deinitialize the share_mode management.
168 ******************************************************************/
170 bool locking_end(void)
172 brl_shutdown();
173 TALLOC_FREE(lock_ctx);
174 return true;
177 /*******************************************************************
178 Form a static locking key for a dev/inode pair.
179 ******************************************************************/
181 static TDB_DATA locking_key(const struct file_id *id)
183 return make_tdb_data((const uint8_t *)id, sizeof(*id));
186 /*******************************************************************
187 Share mode cache utility functions that store/delete/retrieve
188 entries from memcache.
190 For now share the statcache (global cache) memory space. If
191 a lock record gets orphaned (which shouldn't happen as we're
192 using the same locking_key data as lookup) it will eventually
193 fall out of the cache via the normal LRU trim mechanism. If
194 necessary we can always make this a separate (smaller) cache.
195 ******************************************************************/
197 static DATA_BLOB memcache_key(const struct file_id *id)
199 return data_blob_const((const void *)id, sizeof(*id));
202 static void share_mode_memcache_store(struct share_mode_data *d)
204 const DATA_BLOB key = memcache_key(&d->id);
205 struct file_id_buf idbuf;
207 DBG_DEBUG("stored entry for file %s epoch %"PRIx64" key %s\n",
208 d->base_name,
209 d->unique_content_epoch,
210 file_id_str_buf(d->id, &idbuf));
212 /* Ensure everything stored in the cache is pristine. */
213 SMB_ASSERT(!d->modified);
214 SMB_ASSERT(!d->not_stored);
217 * Ensure the memory going into the cache
218 * doesn't have a destructor so it can be
219 * cleanly evicted by the memcache LRU
220 * mechanism.
222 talloc_set_destructor(d, NULL);
224 /* Cache will own d after this call. */
225 memcache_add_talloc(NULL,
226 SHARE_MODE_LOCK_CACHE,
227 key,
228 &d);
232 * NB. We use ndr_pull_hyper on a stack-created
233 * struct ndr_pull with no talloc allowed, as we
234 * need this to be really fast as an ndr-peek into
235 * the first 10 bytes of the blob.
238 static enum ndr_err_code get_share_mode_blob_header(
239 const uint8_t *buf, size_t buflen, uint64_t *pepoch, uint16_t *pflags)
241 struct ndr_pull ndr = {
242 .data = discard_const_p(uint8_t, buf),
243 .data_size = buflen,
245 NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pepoch));
246 NDR_CHECK(ndr_pull_uint16(&ndr, NDR_SCALARS, pflags));
247 return NDR_ERR_SUCCESS;
250 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
252 return -1;
255 static struct share_mode_data *share_mode_memcache_fetch(
256 TALLOC_CTX *mem_ctx,
257 struct file_id id,
258 const uint8_t *buf,
259 size_t buflen)
261 const DATA_BLOB key = memcache_key(&id);
262 enum ndr_err_code ndr_err;
263 struct share_mode_data *d;
264 uint64_t unique_content_epoch;
265 uint16_t flags;
266 void *ptr;
267 struct file_id_buf idbuf;
269 ptr = memcache_lookup_talloc(NULL,
270 SHARE_MODE_LOCK_CACHE,
271 key);
272 if (ptr == NULL) {
273 DBG_DEBUG("failed to find entry for key %s\n",
274 file_id_str_buf(id, &idbuf));
275 return NULL;
277 /* sequence number key is at start of blob. */
278 ndr_err = get_share_mode_blob_header(
279 buf, buflen, &unique_content_epoch, &flags);
280 if (ndr_err != NDR_ERR_SUCCESS) {
281 /* Bad blob. Remove entry. */
282 DBG_DEBUG("bad blob %u key %s\n",
283 (unsigned int)ndr_err,
284 file_id_str_buf(id, &idbuf));
285 memcache_delete(NULL,
286 SHARE_MODE_LOCK_CACHE,
287 key);
288 return NULL;
291 d = (struct share_mode_data *)ptr;
292 if (d->unique_content_epoch != unique_content_epoch) {
293 DBG_DEBUG("epoch changed (cached %"PRIx64") (new %"PRIx64") "
294 "for key %s\n",
295 d->unique_content_epoch,
296 unique_content_epoch,
297 file_id_str_buf(id, &idbuf));
298 /* Cache out of date. Remove entry. */
299 memcache_delete(NULL,
300 SHARE_MODE_LOCK_CACHE,
301 key);
302 return NULL;
305 /* Move onto mem_ctx. */
306 d = talloc_move(mem_ctx, &ptr);
309 * Now we own d, prevent the cache from freeing it
310 * when we delete the entry.
312 talloc_set_destructor(d, share_mode_data_nofree_destructor);
314 /* Remove from the cache. We own it now. */
315 memcache_delete(NULL,
316 SHARE_MODE_LOCK_CACHE,
317 key);
319 /* And reset the destructor to none. */
320 talloc_set_destructor(d, NULL);
322 DBG_DEBUG("fetched entry for file %s epoch %"PRIx64" key %s\n",
323 d->base_name,
324 d->unique_content_epoch,
325 file_id_str_buf(id, &idbuf));
327 return d;
331 * 132 is the sizeof an ndr-encoded struct share_mode_entry_buf.
332 * Reading/writing entries will immediately error out if this
333 * size differs (push/pull is done without allocs).
336 struct share_mode_entry_buf {
337 uint8_t buf[132];
339 #define SHARE_MODE_ENTRY_SIZE (sizeof(struct share_mode_entry_buf))
341 static bool share_mode_entry_put(
342 const struct share_mode_entry *e,
343 struct share_mode_entry_buf *dst)
345 DATA_BLOB blob = { .data = dst->buf, .length = sizeof(dst->buf) };
346 enum ndr_err_code ndr_err;
348 if (DEBUGLEVEL>=10) {
349 DBG_DEBUG("share_mode_entry:\n");
350 NDR_PRINT_DEBUG(share_mode_entry, discard_const_p(void, e));
353 ndr_err = ndr_push_struct_into_fixed_blob(
354 &blob,
356 (ndr_push_flags_fn_t)ndr_push_share_mode_entry);
357 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
358 DBG_WARNING("ndr_push_share_mode_entry failed: %s\n",
359 ndr_errstr(ndr_err));
360 return false;
363 return true;
366 static bool share_mode_entry_get(
367 const uint8_t ptr[SHARE_MODE_ENTRY_SIZE], struct share_mode_entry *e)
369 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
370 DATA_BLOB blob = {
371 .data = discard_const_p(uint8_t, ptr),
372 .length = SHARE_MODE_ENTRY_SIZE,
375 ndr_err = ndr_pull_struct_blob_all_noalloc(
376 &blob, e, (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
377 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
378 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
379 return false;
381 return true;
385 * locking.tdb records consist of
387 * uint32_t share_mode_data_len
388 * uint8_t [share_mode_data] This is struct share_mode_data in NDR
390 * 0 [SHARE_MODE_ENTRY_SIZE] Sorted array of share modes,
391 * 1 [SHARE_MODE_ENTRY_SIZE] filling up the rest of the data in the
392 * 2 [SHARE_MODE_ENTRY_SIZE] g_lock.c maintained record in locking.tdb
395 struct locking_tdb_data {
396 const uint8_t *share_mode_data_buf;
397 size_t share_mode_data_len;
398 const uint8_t *share_entries;
399 size_t num_share_entries;
402 static bool locking_tdb_data_get(
403 struct locking_tdb_data *data, const uint8_t *buf, size_t buflen)
405 uint32_t share_mode_data_len, share_entries_len;
407 if (buflen == 0) {
408 *data = (struct locking_tdb_data) { 0 };
409 return true;
411 if (buflen < sizeof(uint32_t)) {
412 return false;
415 share_mode_data_len = PULL_LE_U32(buf, 0);
417 buf += sizeof(uint32_t);
418 buflen -= sizeof(uint32_t);
420 if (buflen < share_mode_data_len) {
421 return false;
424 share_entries_len = buflen - share_mode_data_len;
426 if ((share_entries_len % SHARE_MODE_ENTRY_SIZE) != 0) {
427 return false;
430 *data = (struct locking_tdb_data) {
431 .share_mode_data_buf = buf,
432 .share_mode_data_len = share_mode_data_len,
433 .share_entries = buf + share_mode_data_len,
434 .num_share_entries = share_entries_len / SHARE_MODE_ENTRY_SIZE,
437 return true;
440 struct locking_tdb_data_fetch_state {
441 TALLOC_CTX *mem_ctx;
442 uint8_t *data;
443 size_t datalen;
446 static void locking_tdb_data_fetch_fn(
447 struct server_id exclusive,
448 size_t num_shared,
449 const struct server_id *shared,
450 const uint8_t *data,
451 size_t datalen,
452 void *private_data)
454 struct locking_tdb_data_fetch_state *state = private_data;
455 state->datalen = datalen;
456 state->data = talloc_memdup(state->mem_ctx, data, datalen);
459 static NTSTATUS locking_tdb_data_fetch(
460 TDB_DATA key, TALLOC_CTX *mem_ctx, struct locking_tdb_data **ltdb)
462 struct locking_tdb_data_fetch_state state = { 0 };
463 struct locking_tdb_data *result = NULL;
464 NTSTATUS status;
465 bool ok;
467 result = talloc_zero(mem_ctx, struct locking_tdb_data);
468 if (result == NULL) {
469 return NT_STATUS_NO_MEMORY;
471 state.mem_ctx = result;
473 status = share_mode_g_lock_dump(key, locking_tdb_data_fetch_fn, &state);
474 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
476 * Just return an empty record
478 goto done;
480 if (!NT_STATUS_IS_OK(status)) {
481 DBG_ERR("share_mode_g_lock_dump failed: %s\n",
482 nt_errstr(status));
483 return status;
485 if (state.datalen == 0) {
486 goto done;
489 ok = locking_tdb_data_get(result, state.data, state.datalen);
490 if (!ok) {
491 DBG_ERR("locking_tdb_data_get failed for %zu bytes\n",
492 state.datalen);
493 TALLOC_FREE(result);
494 return NT_STATUS_INTERNAL_DB_CORRUPTION;
497 done:
498 *ltdb = result;
499 return NT_STATUS_OK;
502 static NTSTATUS locking_tdb_data_store(
503 TDB_DATA key,
504 const struct locking_tdb_data *ltdb,
505 const TDB_DATA *share_mode_dbufs,
506 size_t num_share_mode_dbufs)
508 uint8_t share_mode_data_len_buf[4];
509 TDB_DATA dbufs[num_share_mode_dbufs+3];
510 NTSTATUS status;
512 if ((ltdb->share_mode_data_len == 0) &&
513 (ltdb->num_share_entries == 0) &&
514 (num_share_mode_dbufs == 0)) {
516 * Nothing to write
518 status = share_mode_g_lock_writev(key, NULL, 0);
519 if (!NT_STATUS_IS_OK(status)) {
520 DBG_ERR("share_mode_g_lock_writev(NULL) failed: %s\n",
521 nt_errstr(status));
523 return status;
526 PUSH_LE_U32(share_mode_data_len_buf, 0, ltdb->share_mode_data_len);
528 dbufs[0] = (TDB_DATA) {
529 .dptr = share_mode_data_len_buf,
530 .dsize = sizeof(share_mode_data_len_buf),
532 dbufs[1] = (TDB_DATA) {
533 .dptr = discard_const_p(uint8_t, ltdb->share_mode_data_buf),
534 .dsize = ltdb->share_mode_data_len,
537 if (ltdb->num_share_entries > SIZE_MAX/SHARE_MODE_ENTRY_SIZE) {
538 /* overflow */
539 return NT_STATUS_BUFFER_OVERFLOW;
541 dbufs[2] = (TDB_DATA) {
542 .dptr = discard_const_p(uint8_t, ltdb->share_entries),
543 .dsize = ltdb->num_share_entries * SHARE_MODE_ENTRY_SIZE,
546 if (num_share_mode_dbufs != 0) {
547 memcpy(&dbufs[3],
548 share_mode_dbufs,
549 num_share_mode_dbufs * sizeof(TDB_DATA));
552 status = share_mode_g_lock_writev(key, dbufs, ARRAY_SIZE(dbufs));
553 if (!NT_STATUS_IS_OK(status)) {
554 DBG_ERR("share_mode_g_lock_writev() failed: %s\n",
555 nt_errstr(status));
557 return status;
560 /*******************************************************************
561 Get all share mode entries for a dev/inode pair.
562 ********************************************************************/
564 static struct share_mode_data *parse_share_modes(
565 TALLOC_CTX *mem_ctx,
566 struct file_id id,
567 const uint8_t *buf,
568 size_t buflen)
570 struct share_mode_data *d;
571 enum ndr_err_code ndr_err;
572 DATA_BLOB blob;
574 /* See if we already have a cached copy of this key. */
575 d = share_mode_memcache_fetch(mem_ctx, id, buf, buflen);
576 if (d != NULL) {
577 return d;
580 d = talloc(mem_ctx, struct share_mode_data);
581 if (d == NULL) {
582 DEBUG(0, ("talloc failed\n"));
583 goto fail;
586 blob = (DATA_BLOB) {
587 .data = discard_const_p(uint8_t, buf),
588 .length = buflen,
590 ndr_err = ndr_pull_struct_blob_all(
591 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
592 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
593 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
594 ndr_errstr(ndr_err));
595 goto fail;
598 if (DEBUGLEVEL >= 10) {
599 DEBUG(10, ("parse_share_modes:\n"));
600 NDR_PRINT_DEBUG(share_mode_data, d);
603 return d;
604 fail:
605 TALLOC_FREE(d);
606 return NULL;
609 static NTSTATUS share_mode_data_ltdb_store(struct share_mode_data *d,
610 TDB_DATA key,
611 struct locking_tdb_data *ltdb,
612 const TDB_DATA *share_mode_dbufs,
613 size_t num_share_mode_dbufs)
615 DATA_BLOB blob = { 0 };
616 NTSTATUS status;
618 if (!d->modified) {
619 DBG_DEBUG("share_mode_data not modified\n");
620 goto store;
623 d->unique_content_epoch = generate_unique_u64(d->unique_content_epoch);
625 if (DEBUGLEVEL >= 10) {
626 DBG_DEBUG("\n");
627 NDR_PRINT_DEBUG(share_mode_data, d);
630 if (ltdb->num_share_entries != 0 || num_share_mode_dbufs != 0) {
631 enum ndr_err_code ndr_err;
633 ndr_err = ndr_push_struct_blob(
634 &blob,
635 ltdb,
637 (ndr_push_flags_fn_t)ndr_push_share_mode_data);
638 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
639 DBG_ERR("ndr_push_share_mode_data failed: %s\n",
640 ndr_errstr(ndr_err));
641 return ndr_map_error2ntstatus(ndr_err);
645 ltdb->share_mode_data_buf = blob.data;
646 ltdb->share_mode_data_len = blob.length;
648 store:
649 status = locking_tdb_data_store(key,
650 ltdb,
651 share_mode_dbufs,
652 num_share_mode_dbufs);
653 if (!NT_STATUS_IS_OK(status)) {
654 DBG_ERR("locking_tdb_data_store failed: %s\n",
655 nt_errstr(status));
656 return status;
659 d->modified = false;
660 d->not_stored = (ltdb->share_mode_data_len == 0);
662 return NT_STATUS_OK;
665 /*******************************************************************
666 If modified, store the share_mode_data back into the database.
667 ********************************************************************/
669 static NTSTATUS share_mode_data_store(struct share_mode_data *d)
671 TDB_DATA key = locking_key(&d->id);
672 struct locking_tdb_data *ltdb = NULL;
673 NTSTATUS status;
675 if (!d->modified) {
676 DBG_DEBUG("not modified\n");
677 return NT_STATUS_OK;
680 if (DEBUGLEVEL >= 10) {
681 DBG_DEBUG("\n");
682 NDR_PRINT_DEBUG(share_mode_data, d);
685 status = locking_tdb_data_fetch(key, d, &ltdb);
686 if (!NT_STATUS_IS_OK(status)) {
687 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
688 nt_errstr(status));
689 return status;
692 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
693 TALLOC_FREE(ltdb);
694 if (!NT_STATUS_IS_OK(status)) {
695 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
696 nt_errstr(status));
697 return status;
700 return NT_STATUS_OK;
703 /*******************************************************************
704 Allocate a new share_mode_data struct, mark it unmodified.
705 fresh is set to note that currently there is no database entry.
706 ********************************************************************/
708 static struct share_mode_data *fresh_share_mode_lock(
709 TALLOC_CTX *mem_ctx, const char *servicepath,
710 const struct smb_filename *smb_fname,
711 const struct timespec *old_write_time)
713 struct share_mode_data *d;
715 if ((servicepath == NULL) || (smb_fname == NULL) ||
716 (old_write_time == NULL)) {
717 return NULL;
720 d = talloc_zero(mem_ctx, struct share_mode_data);
721 if (d == NULL) {
722 goto fail;
724 d->unique_content_epoch = generate_unique_u64(0);
726 d->base_name = talloc_strdup(d, smb_fname->base_name);
727 if (d->base_name == NULL) {
728 goto fail;
730 if (smb_fname->stream_name != NULL) {
731 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
732 if (d->stream_name == NULL) {
733 goto fail;
736 d->servicepath = talloc_strdup(d, servicepath);
737 if (d->servicepath == NULL) {
738 goto fail;
740 d->old_write_time = full_timespec_to_nt_time(old_write_time);
741 d->flags = SHARE_MODE_SHARE_DELETE |
742 SHARE_MODE_SHARE_WRITE |
743 SHARE_MODE_SHARE_READ;
744 d->modified = false;
745 d->not_stored = true;
746 return d;
747 fail:
748 DEBUG(0, ("talloc failed\n"));
749 TALLOC_FREE(d);
750 return NULL;
754 * Key that's locked with g_lock
756 static struct file_id share_mode_lock_key_id = {};
757 static TDB_DATA share_mode_lock_key = {
758 .dptr = (uint8_t *)&share_mode_lock_key_id,
759 .dsize = sizeof(share_mode_lock_key_id),
761 static size_t share_mode_lock_key_refcount = 0;
763 static bool share_mode_g_lock_within_cb(TDB_DATA key)
765 int cmp;
767 if (current_share_mode_glck == NULL) {
768 return false;
771 cmp = tdb_data_cmp(share_mode_lock_key, key);
772 if (cmp != 0) {
773 struct file_id_buf existing;
775 DBG_ERR("Can not lock two share modes "
776 "simultaneously: existing %s requested %s\n",
777 file_id_str_buf(share_mode_lock_key_id, &existing),
778 tdb_data_dbg(key));
779 smb_panic(__location__);
780 return false;
783 return true;
787 * We can only ever have one share mode locked. Use a static
788 * share_mode_data pointer that is shared by multiple nested
789 * share_mode_lock structures, explicitly refcounted.
791 static struct share_mode_data *static_share_mode_data = NULL;
793 /*******************************************************************
794 Either fetch a share mode from the database, or allocate a fresh
795 one if the record doesn't exist.
796 ********************************************************************/
798 struct get_static_share_mode_data_state {
799 TALLOC_CTX *mem_ctx;
800 struct file_id id;
801 const char *servicepath;
802 const struct smb_filename *smb_fname;
803 const struct timespec *old_write_time;
804 NTSTATUS status;
807 static void get_static_share_mode_data_fn(
808 struct server_id exclusive,
809 size_t num_shared,
810 const struct server_id *shared,
811 const uint8_t *data,
812 size_t datalen,
813 void *private_data)
815 struct get_static_share_mode_data_state *state = private_data;
816 struct share_mode_data *d = NULL;
817 struct locking_tdb_data ltdb = { 0 };
819 if (datalen != 0) {
820 bool ok;
822 ok = locking_tdb_data_get(&ltdb, data, datalen);
823 if (!ok) {
824 DBG_ERR("locking_tdb_data_get failed\n");
825 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
826 return;
830 if (ltdb.share_mode_data_len == 0) {
831 if (state->smb_fname == NULL) {
832 state->status = NT_STATUS_NOT_FOUND;
833 return;
835 d = fresh_share_mode_lock(
836 state->mem_ctx,
837 state->servicepath,
838 state->smb_fname,
839 state->old_write_time);
840 if (d == NULL) {
841 state->status = NT_STATUS_NO_MEMORY;
842 return;
844 } else {
845 d = parse_share_modes(
846 lock_ctx,
847 state->id,
848 ltdb.share_mode_data_buf,
849 ltdb.share_mode_data_len);
850 if (d == NULL) {
851 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
852 return;
856 d->id = state->id;
857 static_share_mode_data = d;
860 static NTSTATUS get_static_share_mode_data(
861 struct file_id id,
862 const char *servicepath,
863 const struct smb_filename *smb_fname,
864 const struct timespec *old_write_time)
866 struct get_static_share_mode_data_state state = {
867 .mem_ctx = lock_ctx,
868 .id = id,
869 .servicepath = servicepath,
870 .smb_fname = smb_fname,
871 .old_write_time = old_write_time,
873 NTSTATUS status;
875 SMB_ASSERT(static_share_mode_data == NULL);
877 status = share_mode_g_lock_dump(
878 share_mode_lock_key,
879 get_static_share_mode_data_fn,
880 &state);
881 if (!NT_STATUS_IS_OK(status)) {
882 DBG_GET_SHARE_MODE_LOCK(status,
883 "share_mode_g_lock_dump failed: %s\n",
884 nt_errstr(status));
885 return status;
887 if (!NT_STATUS_IS_OK(state.status)) {
888 DBG_GET_SHARE_MODE_LOCK(state.status,
889 "get_static_share_mode_data_fn failed: %s\n",
890 nt_errstr(state.status));
891 return state.status;
894 return NT_STATUS_OK;
897 struct file_id share_mode_lock_file_id(const struct share_mode_lock *lck)
899 return lck->id;
902 NTSTATUS share_mode_lock_access_private_data(struct share_mode_lock *lck,
903 struct share_mode_data **data)
906 * For now we always have lck->cached_data,
907 * but we may change that in future.
909 SMB_ASSERT(lck->cached_data != NULL);
910 *data = lck->cached_data;
911 return NT_STATUS_OK;
914 /*******************************************************************
915 Get a share_mode_lock, Reference counted to allow nested calls.
916 ********************************************************************/
918 static int share_mode_lock_destructor(struct share_mode_lock *lck);
920 static bool share_mode_lock_skip_g_lock;
922 static NTSTATUS get_share_mode_lock_internal(
923 struct file_id id,
924 const char *servicepath,
925 const struct smb_filename *smb_fname,
926 const struct timespec *old_write_time,
927 struct share_mode_lock *lck)
929 NTSTATUS status;
931 *lck = (struct share_mode_lock) {
932 .id = id,
935 if (share_mode_lock_key_refcount == 0) {
936 if (!share_mode_lock_skip_g_lock) {
937 TDB_DATA key = locking_key(&id);
939 status = g_lock_lock(
940 lock_ctx,
941 key,
942 G_LOCK_WRITE,
943 (struct timeval) { .tv_sec = 3600 },
944 NULL, NULL);
945 if (!NT_STATUS_IS_OK(status)) {
946 DBG_DEBUG("g_lock_lock failed: %s\n",
947 nt_errstr(status));
948 return status;
951 share_mode_lock_key_id = id;
954 if (!file_id_equal(&share_mode_lock_key_id, &id)) {
955 struct file_id_buf existing;
956 struct file_id_buf requested;
958 DBG_ERR("Can not lock two share modes "
959 "simultaneously: existing %s requested %s\n",
960 file_id_str_buf(share_mode_lock_key_id, &existing),
961 file_id_str_buf(id, &requested));
962 smb_panic(__location__);
963 goto fail;
966 SMB_ASSERT(share_mode_lock_key_refcount < SIZE_MAX);
967 share_mode_lock_key_refcount += 1;
969 if (static_share_mode_data != NULL) {
970 goto done;
973 status = get_static_share_mode_data(
975 servicepath,
976 smb_fname,
977 old_write_time);
978 if (!NT_STATUS_IS_OK(status)) {
979 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
980 nt_errstr(status));
981 share_mode_lock_key_refcount -= 1;
982 goto fail;
984 done:
985 lck->cached_data = static_share_mode_data;
987 if (CHECK_DEBUGLVL(DBGLVL_DEBUG)) {
988 struct file_id_buf returned;
990 DBG_DEBUG("Returning %s (data_cached=%u key_refcount=%zu)\n",
991 file_id_str_buf(id, &returned),
992 static_share_mode_data != NULL,
993 share_mode_lock_key_refcount);
996 return NT_STATUS_OK;
997 fail:
998 if (share_mode_lock_key_refcount == 0) {
999 if (!share_mode_lock_skip_g_lock) {
1000 NTSTATUS ulstatus = g_lock_unlock(lock_ctx, share_mode_lock_key);
1001 if (!NT_STATUS_IS_OK(ulstatus)) {
1002 DBG_ERR("g_lock_unlock failed: %s\n",
1003 nt_errstr(ulstatus));
1007 return status;
1010 static NTSTATUS put_share_mode_lock_internal(struct share_mode_lock *lck)
1012 NTSTATUS status;
1014 SMB_ASSERT(share_mode_lock_key_refcount > 0);
1015 share_mode_lock_key_refcount -= 1;
1017 if (share_mode_lock_key_refcount > 0) {
1018 return NT_STATUS_OK;
1021 status = share_mode_data_store(static_share_mode_data);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 DBG_ERR("share_mode_data_store failed: %s\n",
1024 nt_errstr(status));
1025 return status;
1028 if (!share_mode_lock_skip_g_lock) {
1029 status = g_lock_unlock(lock_ctx, share_mode_lock_key);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 DBG_ERR("g_lock_unlock failed: %s\n",
1032 nt_errstr(status));
1033 return status;
1037 if (!static_share_mode_data->not_stored) {
1039 * This is worth keeping. Without share modes,
1040 * share_mode_data_store above has left nothing in the
1041 * database.
1043 share_mode_memcache_store(static_share_mode_data);
1044 static_share_mode_data = NULL;
1047 TALLOC_FREE(static_share_mode_data);
1048 return NT_STATUS_OK;
1051 static int share_mode_lock_destructor(struct share_mode_lock *lck)
1053 NTSTATUS status;
1055 status = put_share_mode_lock_internal(lck);
1056 if (!NT_STATUS_IS_OK(status)) {
1057 DBG_ERR("put_share_mode_lock_internal failed: %s\n",
1058 nt_errstr(status));
1059 smb_panic("put_share_mode_lock_internal failed\n");
1062 return 0;
1065 /*******************************************************************
1066 Fetch a share mode where we know one MUST exist. This call reference
1067 counts it internally to allow for nested lock fetches.
1068 ********************************************************************/
1070 struct share_mode_lock *get_existing_share_mode_lock(TALLOC_CTX *mem_ctx,
1071 const struct file_id id)
1073 struct share_mode_lock *lck = NULL;
1074 NTSTATUS status;
1076 lck = talloc(mem_ctx, struct share_mode_lock);
1077 if (lck == NULL) {
1078 return NULL;
1081 status = get_share_mode_lock_internal(id,
1082 NULL, /* servicepath */
1083 NULL, /* smb_fname */
1084 NULL, /* old_write_time */
1085 lck);
1086 if (!NT_STATUS_IS_OK(status)) {
1087 DBG_GET_SHARE_MODE_LOCK(status,
1088 "get_share_mode_lock_internal() failed - %s\n",
1089 nt_errstr(status));
1090 TALLOC_FREE(lck);
1091 return NULL;
1094 talloc_set_destructor(lck, share_mode_lock_destructor);
1095 return lck;
1098 static void share_mode_wakeup_waiters_fn(
1099 struct share_mode_lock *lck,
1100 void *private_data)
1102 if (share_mode_g_lock_within_cb(share_mode_lock_key)) {
1103 g_lock_lock_cb_wake_watchers(current_share_mode_glck);
1104 return;
1107 g_lock_wake_watchers(lock_ctx, share_mode_lock_key);
1110 NTSTATUS share_mode_wakeup_waiters(struct file_id id)
1112 return share_mode_do_locked_vfs_denied(id,
1113 share_mode_wakeup_waiters_fn,
1114 NULL);
1117 struct fsp_update_share_mode_flags_state {
1118 struct files_struct *fsp;
1119 enum ndr_err_code ndr_err;
1120 uint64_t share_mode_epoch;
1121 uint16_t share_mode_flags;
1124 static void fsp_update_share_mode_flags_fn(
1125 struct server_id exclusive,
1126 size_t num_shared,
1127 const struct server_id *shared,
1128 const uint8_t *data,
1129 size_t datalen,
1130 void *private_data)
1132 struct fsp_update_share_mode_flags_state *state = private_data;
1133 struct locking_tdb_data ltdb = { 0 };
1135 if (datalen != 0) {
1136 bool ok = locking_tdb_data_get(&ltdb, data, datalen);
1137 if (!ok) {
1138 DBG_DEBUG("locking_tdb_data_get failed\n");
1139 return;
1143 if (ltdb.share_mode_data_len == 0) {
1144 /* Likely a ctdb tombstone record, ignore it */
1145 return;
1148 if (exclusive.pid != 0) {
1149 struct server_id self =
1150 messaging_server_id(state->fsp->conn->sconn->msg_ctx);
1151 bool is_self = server_id_equal(&self, &exclusive);
1153 if (!is_self) {
1155 * If someone else is holding an exclusive
1156 * lock, pretend there's a read lease
1158 state->share_mode_flags = SHARE_MODE_LEASE_READ;
1159 return;
1163 state->ndr_err = get_share_mode_blob_header(ltdb.share_mode_data_buf,
1164 ltdb.share_mode_data_len,
1165 &state->share_mode_epoch,
1166 &state->share_mode_flags);
1169 static NTSTATUS fsp_update_share_mode_flags(struct files_struct *fsp)
1171 struct fsp_update_share_mode_flags_state state = { .fsp = fsp, };
1172 int seqnum = g_lock_seqnum(lock_ctx);
1173 TDB_DATA key = {0};
1174 NTSTATUS status;
1176 if (seqnum == fsp->share_mode_flags_seqnum) {
1177 return NT_STATUS_OK;
1180 key = locking_key(&fsp->file_id);
1181 status = share_mode_g_lock_dump(key,
1182 fsp_update_share_mode_flags_fn,
1183 &state);
1184 if (!NT_STATUS_IS_OK(status)) {
1185 /* no DBG_GET_SHARE_MODE_LOCK here! */
1186 DBG_ERR("share_mode_g_lock_dump returned %s\n",
1187 nt_errstr(status));
1188 return status;
1191 if (!NDR_ERR_CODE_IS_SUCCESS(state.ndr_err)) {
1192 DBG_ERR("get_share_mode_blob_header returned %s\n",
1193 ndr_errstr(state.ndr_err));
1194 return ndr_map_error2ntstatus(state.ndr_err);
1197 fsp->share_mode_flags_seqnum = seqnum;
1198 fsp->share_mode_flags = state.share_mode_flags;
1200 return NT_STATUS_OK;
1203 bool file_has_read_lease(struct files_struct *fsp)
1205 NTSTATUS status;
1207 status = fsp_update_share_mode_flags(fsp);
1208 if (!NT_STATUS_IS_OK(status)) {
1209 /* Safe default for leases */
1210 return true;
1213 return (fsp->share_mode_flags & SHARE_MODE_LEASE_READ) != 0;
1216 #define share_mode_lock_assert_private_data(__lck) \
1217 _share_mode_lock_assert_private_data(__lck, __func__, __location__)
1218 static struct share_mode_data *_share_mode_lock_assert_private_data(
1219 struct share_mode_lock *lck,
1220 const char *caller_function,
1221 const char *caller_location)
1223 struct share_mode_data *d = NULL;
1224 NTSTATUS status;
1226 status = share_mode_lock_access_private_data(lck, &d);
1227 if (!NT_STATUS_IS_OK(status)) {
1228 struct file_id id = share_mode_lock_file_id(lck);
1229 struct file_id_buf id_buf;
1230 /* Any error recovery possible here ? */
1231 D_ERR("%s:%s(): share_mode_lock_access_private_data() "
1232 "failed for id=%s - %s\n",
1233 caller_location, caller_function,
1234 file_id_str_buf(id, &id_buf),
1235 nt_errstr(status));
1236 smb_panic(caller_location);
1237 return NULL;
1240 return d;
1243 NTTIME share_mode_changed_write_time(struct share_mode_lock *lck)
1245 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1246 return d->changed_write_time;
1249 void share_mode_set_changed_write_time(struct share_mode_lock *lck, struct timespec write_time)
1251 struct file_id fileid = share_mode_lock_file_id(lck);
1252 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1253 struct file_id_buf ftmp;
1254 struct timeval_buf tbuf;
1255 NTTIME nt = full_timespec_to_nt_time(&write_time);
1257 DBG_INFO("%s id=%s\n",
1258 timespec_string_buf(&write_time, true, &tbuf),
1259 file_id_str_buf(fileid, &ftmp));
1261 if (d->changed_write_time != nt) {
1262 d->modified = true;
1263 d->changed_write_time = nt;
1267 void share_mode_set_old_write_time(struct share_mode_lock *lck, struct timespec write_time)
1269 struct file_id fileid = share_mode_lock_file_id(lck);
1270 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1271 struct file_id_buf ftmp;
1272 struct timeval_buf tbuf;
1273 NTTIME nt = full_timespec_to_nt_time(&write_time);
1275 DBG_INFO("%s id=%s\n",
1276 timespec_string_buf(&write_time, true, &tbuf),
1277 file_id_str_buf(fileid, &ftmp));
1279 if (d->changed_write_time != nt) {
1280 d->modified = true;
1281 d->old_write_time = nt;
1285 const char *share_mode_servicepath(struct share_mode_lock *lck)
1287 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1288 return d->servicepath;
1291 char *share_mode_filename(TALLOC_CTX *mem_ctx, struct share_mode_lock *lck)
1293 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1294 bool has_stream = (d->stream_name != NULL);
1295 char *fname = NULL;
1297 fname = talloc_asprintf(
1298 mem_ctx,
1299 "%s%s%s",
1300 d->base_name,
1301 has_stream ? ":" : "",
1302 has_stream ? d->stream_name : "");
1303 return fname;
1306 char *share_mode_data_dump(
1307 TALLOC_CTX *mem_ctx, struct share_mode_lock *lck)
1309 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1310 struct ndr_print *p = talloc(mem_ctx, struct ndr_print);
1311 char *ret = NULL;
1313 if (p == NULL) {
1314 return NULL;
1317 *p = (struct ndr_print) {
1318 .print = ndr_print_string_helper,
1319 .depth = 1,
1320 .private_data = talloc_strdup(mem_ctx, ""),
1323 if (p->private_data == NULL) {
1324 TALLOC_FREE(p);
1325 return NULL;
1328 ndr_print_share_mode_data(p, "SHARE_MODE_DATA", d);
1330 ret = p->private_data;
1332 TALLOC_FREE(p);
1334 return ret;
1337 void share_mode_flags_get(
1338 struct share_mode_lock *lck,
1339 uint32_t *access_mask,
1340 uint32_t *share_mode,
1341 uint32_t *lease_type)
1343 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1344 uint16_t flags = d->flags;
1346 if (access_mask != NULL) {
1347 *access_mask =
1348 ((flags & SHARE_MODE_ACCESS_READ) ?
1349 FILE_READ_DATA : 0) |
1350 ((flags & SHARE_MODE_ACCESS_WRITE) ?
1351 FILE_WRITE_DATA : 0) |
1352 ((flags & SHARE_MODE_ACCESS_DELETE) ?
1353 DELETE_ACCESS : 0);
1355 if (share_mode != NULL) {
1356 *share_mode =
1357 ((flags & SHARE_MODE_SHARE_READ) ?
1358 FILE_SHARE_READ : 0) |
1359 ((flags & SHARE_MODE_SHARE_WRITE) ?
1360 FILE_SHARE_WRITE : 0) |
1361 ((flags & SHARE_MODE_SHARE_DELETE) ?
1362 FILE_SHARE_DELETE : 0);
1364 if (lease_type != NULL) {
1365 *lease_type =
1366 ((flags & SHARE_MODE_LEASE_READ) ?
1367 SMB2_LEASE_READ : 0) |
1368 ((flags & SHARE_MODE_LEASE_WRITE) ?
1369 SMB2_LEASE_WRITE : 0) |
1370 ((flags & SHARE_MODE_LEASE_HANDLE) ?
1371 SMB2_LEASE_HANDLE : 0);
1375 void share_mode_flags_set(
1376 struct share_mode_lock *lck,
1377 uint32_t access_mask,
1378 uint32_t share_mode,
1379 uint32_t lease_type,
1380 bool *modified)
1382 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1383 uint16_t flags = 0;
1385 flags |= (access_mask & (FILE_READ_DATA | FILE_EXECUTE)) ?
1386 SHARE_MODE_ACCESS_READ : 0;
1387 flags |= (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
1388 SHARE_MODE_ACCESS_WRITE : 0;
1389 flags |= (access_mask & (DELETE_ACCESS)) ?
1390 SHARE_MODE_ACCESS_DELETE : 0;
1392 flags |= (share_mode & FILE_SHARE_READ) ?
1393 SHARE_MODE_SHARE_READ : 0;
1394 flags |= (share_mode & FILE_SHARE_WRITE) ?
1395 SHARE_MODE_SHARE_WRITE : 0;
1396 flags |= (share_mode & FILE_SHARE_DELETE) ?
1397 SHARE_MODE_SHARE_DELETE : 0;
1399 flags |= (lease_type & SMB2_LEASE_READ) ?
1400 SHARE_MODE_LEASE_READ : 0;
1401 flags |= (lease_type & SMB2_LEASE_WRITE) ?
1402 SHARE_MODE_LEASE_WRITE : 0;
1403 flags |= (lease_type & SMB2_LEASE_HANDLE) ?
1404 SHARE_MODE_LEASE_HANDLE : 0;
1406 if (d->flags == flags) {
1407 return;
1410 if (modified != NULL) {
1411 *modified = true;
1413 d->flags = flags;
1414 d->modified = true;
1417 struct share_mode_watch_state {
1418 bool blockerdead;
1419 struct server_id blocker;
1420 bool within_cb;
1423 static void share_mode_watch_done(struct tevent_req *subreq);
1425 struct tevent_req *share_mode_watch_send(
1426 TALLOC_CTX *mem_ctx,
1427 struct tevent_context *ev,
1428 struct file_id *id,
1429 struct server_id blocker)
1431 TDB_DATA key = locking_key(id);
1432 struct tevent_req *req = NULL, *subreq = NULL;
1433 struct share_mode_watch_state *state = NULL;
1435 req = tevent_req_create(
1436 mem_ctx, &state, struct share_mode_watch_state);
1437 if (req == NULL) {
1438 return NULL;
1441 if (share_mode_g_lock_within_cb(key)) {
1442 state->within_cb = true;
1443 subreq = g_lock_lock_cb_watch_data_send(state, ev,
1444 current_share_mode_glck,
1445 blocker);
1446 if (tevent_req_nomem(subreq, req)) {
1447 return tevent_req_post(req, ev);
1449 } else {
1450 subreq = g_lock_watch_data_send(state, ev, lock_ctx, key, blocker);
1451 if (tevent_req_nomem(subreq, req)) {
1452 return tevent_req_post(req, ev);
1455 tevent_req_set_callback(subreq, share_mode_watch_done, req);
1456 return req;
1459 static void share_mode_watch_done(struct tevent_req *subreq)
1461 struct tevent_req *req = tevent_req_callback_data(
1462 subreq, struct tevent_req);
1463 struct share_mode_watch_state *state = tevent_req_data(
1464 req, struct share_mode_watch_state);
1465 NTSTATUS status;
1467 if (state->within_cb) {
1468 status = g_lock_lock_cb_watch_data_recv(
1469 subreq, &state->blockerdead, &state->blocker);
1470 if (tevent_req_nterror(req, status)) {
1471 return;
1473 } else {
1474 status = g_lock_watch_data_recv(
1475 subreq, &state->blockerdead, &state->blocker);
1476 if (tevent_req_nterror(req, status)) {
1477 return;
1481 tevent_req_done(req);
1484 NTSTATUS share_mode_watch_recv(
1485 struct tevent_req *req, bool *blockerdead, struct server_id *blocker)
1487 struct share_mode_watch_state *state = tevent_req_data(
1488 req, struct share_mode_watch_state);
1489 NTSTATUS status;
1491 if (tevent_req_is_nterror(req, &status)) {
1492 return status;
1494 if (blockerdead != NULL) {
1495 *blockerdead = state->blockerdead;
1497 if (blocker != NULL) {
1498 *blocker = state->blocker;
1500 return NT_STATUS_OK;
1503 struct fetch_share_mode_unlocked_state {
1504 TALLOC_CTX *mem_ctx;
1505 struct file_id id;
1506 struct share_mode_lock *lck;
1509 static void fetch_share_mode_unlocked_parser(
1510 struct server_id exclusive,
1511 size_t num_shared,
1512 const struct server_id *shared,
1513 const uint8_t *data,
1514 size_t datalen,
1515 void *private_data)
1517 struct fetch_share_mode_unlocked_state *state = private_data;
1518 struct locking_tdb_data ltdb = { 0 };
1520 if (datalen != 0) {
1521 bool ok = locking_tdb_data_get(&ltdb, data, datalen);
1522 if (!ok) {
1523 DBG_DEBUG("locking_tdb_data_get failed\n");
1524 return;
1528 if (ltdb.share_mode_data_len == 0) {
1529 /* Likely a ctdb tombstone record, ignore it */
1530 return;
1533 state->lck = talloc(state->mem_ctx, struct share_mode_lock);
1534 if (state->lck == NULL) {
1535 DEBUG(0, ("talloc failed\n"));
1536 return;
1538 state->lck->id = state->id;
1540 state->lck->cached_data = parse_share_modes(
1541 state->lck,
1542 state->id,
1543 ltdb.share_mode_data_buf,
1544 ltdb.share_mode_data_len);
1545 if (state->lck->cached_data == NULL) {
1546 DBG_DEBUG("parse_share_modes failed\n");
1547 TALLOC_FREE(state->lck);
1551 /*******************************************************************
1552 Get a share_mode_lock without locking the database or reference
1553 counting. Used by smbstatus to display existing share modes.
1554 ********************************************************************/
1556 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
1557 struct file_id id)
1559 struct fetch_share_mode_unlocked_state state = {
1560 .mem_ctx = mem_ctx,
1561 .id = id,
1563 TDB_DATA key = locking_key(&id);
1564 NTSTATUS status;
1566 status = g_lock_dump(
1567 lock_ctx, key, fetch_share_mode_unlocked_parser, &state);
1568 if (!NT_STATUS_IS_OK(status)) {
1569 DBG_DEBUG("g_lock_dump failed: %s\n", nt_errstr(status));
1570 return NULL;
1572 return state.lck;
1575 struct fetch_share_mode_state {
1576 struct file_id id;
1577 struct share_mode_lock *lck;
1578 NTSTATUS status;
1581 static void fetch_share_mode_fn(
1582 struct server_id exclusive,
1583 size_t num_shared,
1584 const struct server_id *shared,
1585 const uint8_t *data,
1586 size_t datalen,
1587 void *private_data);
1588 static void fetch_share_mode_done(struct tevent_req *subreq);
1591 * @brief Get a share_mode_lock without locking or refcounting
1593 * This can be used in a clustered Samba environment where the async dbwrap
1594 * request is sent over a socket to the local ctdbd. If the send queue is full
1595 * and the caller was issuing multiple async dbwrap requests in a loop, the
1596 * caller knows it's probably time to stop sending requests for now and try
1597 * again later.
1599 * @param[in] mem_ctx The talloc memory context to use.
1601 * @param[in] ev The event context to work on.
1603 * @param[in] id The file id for the locking.tdb key
1605 * @param[out] queued This boolean out parameter tells the caller whether the
1606 * async request is blocked in a full send queue:
1608 * false := request is dispatched
1610 * true := send queue is full, request waiting to be
1611 * dispatched
1613 * @return The new async request, NULL on error.
1615 struct tevent_req *fetch_share_mode_send(TALLOC_CTX *mem_ctx,
1616 struct tevent_context *ev,
1617 struct file_id id,
1618 bool *queued)
1620 struct tevent_req *req = NULL, *subreq = NULL;
1621 struct fetch_share_mode_state *state = NULL;
1623 *queued = false;
1625 req = tevent_req_create(mem_ctx, &state,
1626 struct fetch_share_mode_state);
1627 if (req == NULL) {
1628 return NULL;
1630 state->id = id;
1632 subreq = g_lock_dump_send(
1633 state,
1635 lock_ctx,
1636 locking_key(&id),
1637 fetch_share_mode_fn,
1638 state);
1639 if (tevent_req_nomem(subreq, req)) {
1640 return tevent_req_post(req, ev);
1642 tevent_req_set_callback(subreq, fetch_share_mode_done, req);
1643 return req;
1646 static void fetch_share_mode_fn(
1647 struct server_id exclusive,
1648 size_t num_shared,
1649 const struct server_id *shared,
1650 const uint8_t *data,
1651 size_t datalen,
1652 void *private_data)
1654 struct fetch_share_mode_state *state = talloc_get_type_abort(
1655 private_data, struct fetch_share_mode_state);
1656 struct locking_tdb_data ltdb = { 0 };
1658 if (datalen != 0) {
1659 bool ok = locking_tdb_data_get(&ltdb, data, datalen);
1660 if (!ok) {
1661 DBG_DEBUG("locking_tdb_data_get failed\n");
1662 return;
1666 if (ltdb.share_mode_data_len == 0) {
1667 /* Likely a ctdb tombstone record, ignore it */
1668 return;
1671 state->lck = talloc(state, struct share_mode_lock);
1672 if (state->lck == NULL) {
1673 DBG_WARNING("talloc failed\n");
1674 state->status = NT_STATUS_NO_MEMORY;
1675 return;
1677 state->lck->id = state->id,
1679 state->lck->cached_data = parse_share_modes(
1680 state->lck,
1681 state->id,
1682 ltdb.share_mode_data_buf,
1683 ltdb.share_mode_data_len);
1684 if (state->lck->cached_data == NULL) {
1685 DBG_DEBUG("parse_share_modes failed\n");
1686 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1687 TALLOC_FREE(state->lck);
1688 return;
1692 static void fetch_share_mode_done(struct tevent_req *subreq)
1694 struct tevent_req *req = tevent_req_callback_data(
1695 subreq, struct tevent_req);
1696 struct fetch_share_mode_state *state = tevent_req_data(
1697 req, struct fetch_share_mode_state);
1698 NTSTATUS status;
1700 status = g_lock_dump_recv(subreq);
1701 TALLOC_FREE(subreq);
1702 if (tevent_req_nterror(req, status)) {
1703 return;
1705 if (tevent_req_nterror(req, state->status)) {
1706 return;
1708 tevent_req_done(req);
1711 NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
1712 TALLOC_CTX *mem_ctx,
1713 struct share_mode_lock **_lck)
1715 struct fetch_share_mode_state *state = tevent_req_data(
1716 req, struct fetch_share_mode_state);
1717 struct share_mode_lock *lck = NULL;
1719 NTSTATUS status;
1721 if (tevent_req_is_nterror(req, &status)) {
1722 tevent_req_received(req);
1723 return status;
1726 if (state->lck == NULL) {
1727 tevent_req_received(req);
1728 return NT_STATUS_NOT_FOUND;
1731 lck = talloc_move(mem_ctx, &state->lck);
1733 if (DEBUGLEVEL >= 10) {
1734 DBG_DEBUG("share_mode_data:\n");
1735 NDR_PRINT_DEBUG(share_mode_data, lck->cached_data);
1738 *_lck = lck;
1739 tevent_req_received(req);
1740 return NT_STATUS_OK;
1743 struct share_mode_forall_state {
1744 TDB_DATA key;
1745 int (*ro_fn)(struct file_id fid,
1746 const struct share_mode_data *data,
1747 void *private_data);
1748 int (*rw_fn)(struct file_id fid,
1749 struct share_mode_data *data,
1750 void *private_data);
1751 void *private_data;
1754 static void share_mode_forall_dump_fn(
1755 struct server_id exclusive,
1756 size_t num_shared,
1757 const struct server_id *shared,
1758 const uint8_t *data,
1759 size_t datalen,
1760 void *private_data)
1762 struct share_mode_forall_state *state = private_data;
1763 struct file_id fid;
1764 struct locking_tdb_data ltdb = { 0 };
1765 bool ok;
1766 struct share_mode_data *d;
1768 if (state->key.dsize != sizeof(fid)) {
1769 DBG_DEBUG("Got invalid key length %zu\n", state->key.dsize);
1770 return;
1772 memcpy(&fid, state->key.dptr, sizeof(fid));
1774 ok = locking_tdb_data_get(&ltdb, data, datalen);
1775 if (!ok) {
1776 DBG_DEBUG("locking_tdb_data_get() failed\n");
1777 return;
1780 d = parse_share_modes(
1781 talloc_tos(),
1782 fid,
1783 ltdb.share_mode_data_buf,
1784 ltdb.share_mode_data_len);
1785 if (d == NULL) {
1786 DBG_DEBUG("parse_share_modes() failed\n");
1787 return;
1790 if (state->ro_fn != NULL) {
1791 state->ro_fn(fid, d, state->private_data);
1792 } else {
1793 state->rw_fn(fid, d, state->private_data);
1795 TALLOC_FREE(d);
1798 static int share_mode_forall_fn(TDB_DATA key, void *private_data)
1800 struct share_mode_forall_state *state = private_data;
1801 NTSTATUS status;
1803 state->key = key;
1805 status = share_mode_g_lock_dump(
1806 key, share_mode_forall_dump_fn, private_data);
1807 if (!NT_STATUS_IS_OK(status)) {
1808 DBG_GET_SHARE_MODE_LOCK(status,
1809 "g_lock_dump failed: %s\n",
1810 nt_errstr(status));
1812 return 0;
1815 int share_mode_forall_read(int (*fn)(struct file_id fid,
1816 const struct share_mode_data *data,
1817 void *private_data),
1818 void *private_data)
1820 struct share_mode_forall_state state = {
1821 .ro_fn = fn,
1822 .private_data = private_data
1824 int ret;
1826 if (lock_ctx == NULL) {
1827 return 0;
1830 ret = g_lock_locks_read(
1831 lock_ctx, share_mode_forall_fn, &state);
1832 if (ret < 0) {
1833 DBG_ERR("g_lock_locks failed\n");
1835 return ret;
1838 int share_mode_forall(int (*fn)(struct file_id fid,
1839 struct share_mode_data *data,
1840 void *private_data),
1841 void *private_data)
1843 struct share_mode_forall_state state = {
1844 .rw_fn = fn,
1845 .private_data = private_data
1847 int ret;
1849 if (lock_ctx == NULL) {
1850 return 0;
1853 ret = g_lock_locks(
1854 lock_ctx, share_mode_forall_fn, &state);
1855 if (ret < 0) {
1856 DBG_ERR("g_lock_locks failed\n");
1858 return ret;
1861 struct share_entry_forall_state {
1862 struct file_id fid;
1863 struct share_mode_data *data;
1864 int (*ro_fn)(struct file_id fid,
1865 const struct share_mode_data *data,
1866 const struct share_mode_entry *entry,
1867 void *private_data);
1868 int (*rw_fn)(struct file_id fid,
1869 struct share_mode_data *data,
1870 struct share_mode_entry *entry,
1871 void *private_data);
1872 void *private_data;
1873 int ret;
1876 static bool share_entry_traverse_walker(
1877 struct share_mode_entry *e,
1878 bool *modified,
1879 void *private_data)
1881 struct share_entry_forall_state *state = private_data;
1882 int ret;
1884 if (state->ro_fn != NULL) {
1885 ret = state->ro_fn(state->fid,
1886 state->data,
1888 state->private_data);
1889 } else {
1890 ret = state->rw_fn(state->fid,
1891 state->data,
1893 state->private_data);
1895 if (ret == 0) {
1896 /* Continue the whole traverse */
1897 return 0;
1898 } else if (ret == 1) {
1900 * Just stop share_mode_entry loop: by not setting
1901 * state->ret (which was initialized to 0), the
1902 * share_mode_data traverse will continue.
1904 return 1;
1906 state->ret = ret;
1907 return 1;
1910 static int share_entry_ro_traverse_fn(struct file_id fid,
1911 const struct share_mode_data *data,
1912 void *private_data)
1914 struct share_entry_forall_state *state = private_data;
1915 struct share_mode_lock lck = {
1916 .id = fid,
1917 .cached_data = discard_const_p(struct share_mode_data, data)
1919 bool ok;
1921 state->fid = fid;
1922 state->data = discard_const_p(struct share_mode_data, data);
1923 state->ret = 0;
1925 ok = share_mode_forall_entries(
1926 &lck, share_entry_traverse_walker, state);
1927 if (!ok) {
1928 DBG_ERR("share_mode_forall_entries failed\n");
1929 return false;
1932 return state->ret;
1935 static int share_entry_rw_traverse_fn(struct file_id fid,
1936 struct share_mode_data *data,
1937 void *private_data)
1939 struct share_entry_forall_state *state = private_data;
1940 struct share_mode_lock lck = {
1941 .id = fid,
1942 .cached_data = data,
1944 bool ok;
1946 state->fid = fid;
1947 state->data = data;
1948 state->ret = 0;
1950 ok = share_mode_forall_entries(
1951 &lck, share_entry_traverse_walker, state);
1952 if (!ok) {
1953 DBG_ERR("share_mode_forall_entries failed\n");
1954 return false;
1957 return state->ret;
1960 /*******************************************************************
1961 Call the specified function on each entry under management by the
1962 share mode system. If the callback function returns:
1964 0 ... continue traverse
1965 1 ... stop loop over share_mode_entries, but continue share_mode_data traverse
1966 -1 ... stop whole share_mode_data traverse
1968 Any other return value is treated as -1.
1969 ********************************************************************/
1971 int share_entry_forall_read(int (*fn)(struct file_id fid,
1972 const struct share_mode_data *data,
1973 const struct share_mode_entry *entry,
1974 void *private_data),
1975 void *private_data)
1977 struct share_entry_forall_state state = {
1978 .ro_fn = fn,
1979 .private_data = private_data,
1982 return share_mode_forall_read(share_entry_ro_traverse_fn, &state);
1985 int share_entry_forall(int (*fn)(struct file_id fid,
1986 struct share_mode_data *data,
1987 struct share_mode_entry *entry,
1988 void *private_data),
1989 void *private_data)
1991 struct share_entry_forall_state state = {
1992 .rw_fn = fn,
1993 .private_data = private_data,
1996 return share_mode_forall(share_entry_rw_traverse_fn, &state);
1999 static int share_mode_entry_cmp(
2000 struct server_id pid1,
2001 uint64_t share_file_id1,
2002 struct server_id pid2,
2003 uint64_t share_file_id2)
2005 int cmp;
2007 cmp = server_id_cmp(&pid1, &pid2);
2008 if (cmp != 0) {
2009 return cmp;
2011 if (share_file_id1 != share_file_id2) {
2012 return (share_file_id1 < share_file_id2) ? -1 : 1;
2014 return 0;
2017 static size_t share_mode_entry_find(
2018 const uint8_t *data,
2019 size_t num_share_modes,
2020 struct server_id pid,
2021 uint64_t share_file_id,
2022 struct share_mode_entry *e,
2023 bool *match)
2025 ssize_t left, right, middle;
2027 *match = false;
2029 if (num_share_modes == 0) {
2030 return 0;
2033 left = 0;
2034 right = (num_share_modes-1);
2036 while (left <= right) {
2037 const uint8_t *middle_ptr = NULL;
2038 int cmp;
2039 bool ok;
2041 middle = left + ((right - left) / 2);
2042 middle_ptr = data + middle * SHARE_MODE_ENTRY_SIZE;
2044 DBG_DEBUG("left=%zu, right=%zu, middle=%zu, middle_ptr=%p\n",
2045 left,
2046 right,
2047 middle,
2048 middle_ptr);
2050 ok = share_mode_entry_get(middle_ptr, e);
2051 if (!ok) {
2052 DBG_DEBUG("share_mode_entry_get failed\n");
2053 return 0;
2056 cmp = share_mode_entry_cmp(
2057 e->pid, e->share_file_id, pid, share_file_id);
2058 if (cmp == 0) {
2059 *match = true;
2060 return middle;
2063 if (cmp < 0) {
2064 right = middle-1;
2065 } else {
2066 left = middle+1;
2070 return left;
2073 bool set_share_mode(struct share_mode_lock *lck,
2074 struct files_struct *fsp,
2075 uid_t uid,
2076 uint64_t mid,
2077 uint16_t op_type,
2078 const struct smb2_lease_key *lease_key,
2079 uint32_t share_access,
2080 uint32_t access_mask)
2082 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
2083 TDB_DATA key = locking_key(&d->id);
2084 struct server_id my_pid = messaging_server_id(
2085 fsp->conn->sconn->msg_ctx);
2086 struct locking_tdb_data *ltdb = NULL;
2087 size_t idx;
2088 struct share_mode_entry e = { .pid.pid = 0 };
2089 struct share_mode_entry_buf e_buf;
2090 NTSTATUS status;
2091 bool ok, found;
2093 TDB_DATA dbufs[3];
2094 size_t num_dbufs = 0;
2096 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2097 if (!NT_STATUS_IS_OK(status)) {
2098 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2099 nt_errstr(status));
2100 return false;
2102 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2104 idx = share_mode_entry_find(
2105 ltdb->share_entries,
2106 ltdb->num_share_entries,
2107 my_pid,
2108 fh_get_gen_id(fsp->fh),
2110 &found);
2111 if (found) {
2112 DBG_WARNING("Found duplicate share mode\n");
2113 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
2114 goto done;
2117 e = (struct share_mode_entry) {
2118 .pid = my_pid,
2119 .share_access = share_access,
2120 .private_options = fh_get_private_options(fsp->fh),
2121 .access_mask = access_mask,
2122 .op_mid = mid,
2123 .op_type = op_type,
2124 .time.tv_sec = fsp->open_time.tv_sec,
2125 .time.tv_usec = fsp->open_time.tv_usec,
2126 .share_file_id = fh_get_gen_id(fsp->fh),
2127 .uid = (uint32_t)uid,
2128 .flags = fsp->fsp_flags.posix_open ?
2129 SHARE_MODE_FLAG_POSIX_OPEN : 0,
2130 .name_hash = fsp->name_hash,
2133 if (op_type == LEASE_OPLOCK) {
2134 const struct GUID *client_guid = fsp_client_guid(fsp);
2135 e.client_guid = *client_guid;
2136 e.lease_key = *lease_key;
2139 ok = share_mode_entry_put(&e, &e_buf);
2140 if (!ok) {
2141 DBG_DEBUG("share_mode_entry_put failed\n");
2142 status = NT_STATUS_INTERNAL_ERROR;
2143 goto done;
2146 DBG_DEBUG("idx=%zu, found=%d\n", idx, (int)found);
2148 if (idx > 0) {
2149 dbufs[num_dbufs] = (TDB_DATA) {
2150 .dptr = discard_const_p(uint8_t, ltdb->share_entries),
2151 .dsize = idx * SHARE_MODE_ENTRY_SIZE,
2153 num_dbufs += 1;
2156 dbufs[num_dbufs] = (TDB_DATA) {
2157 .dptr = e_buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
2159 num_dbufs += 1;
2161 if (idx < ltdb->num_share_entries) {
2162 size_t num_after_idx = (ltdb->num_share_entries-idx);
2163 dbufs[num_dbufs] = (TDB_DATA) {
2164 .dptr = discard_const_p(uint8_t, ltdb->share_entries) +
2165 idx * SHARE_MODE_ENTRY_SIZE,
2166 .dsize = num_after_idx * SHARE_MODE_ENTRY_SIZE,
2168 num_dbufs += 1;
2172 size_t i;
2173 for (i=0; i<num_dbufs; i++) {
2174 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
2176 dbufs[i].dptr,
2177 dbufs[i].dsize);
2181 if (num_dbufs == 1) {
2183 * Storing a fresh record with just one share entry
2185 d->modified = true;
2189 * If there was any existing data in
2190 * ltdb->share_entries, it's now been
2191 * moved and we've split it into:
2193 * num_dbufs = 3
2194 * dbufs[0] -> old sorted data less than new_entry
2195 * dbufs[1] -> new_share_mode_entry
2196 * dbufs[2] -> old sorted_data greater than new entry.
2198 * So the old data inside ltdb->share_entries is
2199 * no longer valid.
2201 * If we're storing a brand new entry the
2202 * dbufs look like:
2204 * num_dbufs = 1
2205 * dbufs[0] -> new_share_mode_entry
2207 * Either way we must set ltdb->share_entries = NULL
2208 * and ltdb->num_share_entries = 0 so that
2209 * locking_tdb_data_store() doesn't use it to
2210 * store any data. It's no longer there.
2213 ltdb->share_entries = NULL;
2214 ltdb->num_share_entries = 0;
2216 status = share_mode_data_ltdb_store(d, key, ltdb, dbufs, num_dbufs);
2217 if (!NT_STATUS_IS_OK(status)) {
2218 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2219 nt_errstr(status));
2221 done:
2222 TALLOC_FREE(ltdb);
2223 return NT_STATUS_IS_OK(status);
2226 static bool share_mode_for_one_entry(
2227 bool (*fn)(struct share_mode_entry *e,
2228 bool *modified,
2229 void *private_data),
2230 void *private_data,
2231 size_t *i,
2232 uint8_t *data,
2233 size_t *num_share_modes,
2234 bool *writeback)
2236 DATA_BLOB blob = {
2237 .data = data + (*i) * SHARE_MODE_ENTRY_SIZE,
2238 .length = SHARE_MODE_ENTRY_SIZE,
2240 struct share_mode_entry e = {.pid.pid=0};
2241 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
2242 bool modified = false;
2243 bool stop = false;
2244 struct server_id e_pid;
2245 uint64_t e_share_file_id;
2247 ndr_err = ndr_pull_struct_blob_all_noalloc(
2248 &blob,
2250 (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
2251 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2252 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
2253 *i += 1;
2254 return false;
2256 if (DEBUGLEVEL >= 10) {
2257 DBG_DEBUG("entry[%zu]:\n", *i);
2258 NDR_PRINT_DEBUG(share_mode_entry, &e);
2261 e_pid = e.pid;
2262 e_share_file_id = e.share_file_id;
2264 stop = fn(&e, &modified, private_data);
2266 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2268 (int)modified,
2269 (int)e.stale);
2271 if (e.stale) {
2272 if (DEBUGLEVEL>=10) {
2273 DBG_DEBUG("share_mode_entry:\n");
2274 NDR_PRINT_DEBUG(share_mode_entry, &e);
2277 if (*i < *num_share_modes) {
2278 memmove(blob.data,
2279 blob.data + SHARE_MODE_ENTRY_SIZE,
2280 (*num_share_modes - *i - 1) *
2281 SHARE_MODE_ENTRY_SIZE);
2283 *num_share_modes -= 1;
2284 *writeback = true;
2285 return stop;
2288 if (modified) {
2289 if (DEBUGLEVEL>=10) {
2290 DBG_DEBUG("share_mode_entry:\n");
2291 NDR_PRINT_DEBUG(share_mode_entry, &e);
2295 * Make sure sorting order is kept intact
2297 SMB_ASSERT(server_id_equal(&e_pid, &e.pid));
2298 SMB_ASSERT(e_share_file_id == e.share_file_id);
2300 ndr_err = ndr_push_struct_into_fixed_blob(
2301 &blob,
2303 (ndr_push_flags_fn_t)
2304 ndr_push_share_mode_entry);
2305 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2306 DBG_WARNING("ndr_push_share_mode_entry "
2307 "failed: %s\n",
2308 ndr_errstr(ndr_err));
2310 * Not much we can do, just ignore it
2313 *i += 1;
2314 *writeback = true;
2315 return stop;
2318 if (stop) {
2319 return true;
2322 *i += 1;
2323 return false;
2326 bool share_mode_forall_entries(
2327 struct share_mode_lock *lck,
2328 bool (*fn)(struct share_mode_entry *e,
2329 bool *modified,
2330 void *private_data),
2331 void *private_data)
2333 struct file_id id = share_mode_lock_file_id(lck);
2334 struct share_mode_data *d = NULL;
2335 TDB_DATA key = locking_key(&id);
2336 struct locking_tdb_data *ltdb = NULL;
2337 uint8_t *share_entries = NULL;
2338 size_t num_share_entries;
2339 bool writeback = false;
2340 NTSTATUS status;
2341 bool stop = false;
2342 size_t i;
2344 status = share_mode_lock_access_private_data(lck, &d);
2345 if (!NT_STATUS_IS_OK(status)) {
2346 struct file_id_buf id_buf;
2347 /* Any error recovery possible here ? */
2348 DBG_ERR("share_mode_lock_access_private_data() failed for "
2349 "%s - %s\n",
2350 file_id_str_buf(id, &id_buf),
2351 nt_errstr(status));
2352 return false;
2355 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2356 if (!NT_STATUS_IS_OK(status)) {
2357 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2358 nt_errstr(status));
2359 return false;
2361 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2363 num_share_entries = ltdb->num_share_entries;
2364 share_entries = discard_const_p(uint8_t, ltdb->share_entries);
2366 i = 0;
2367 while (i<num_share_entries) {
2368 stop = share_mode_for_one_entry(
2370 private_data,
2372 share_entries,
2373 &num_share_entries,
2374 &writeback);
2375 if (stop) {
2376 break;
2380 DBG_DEBUG("num_share_entries=%zu, writeback=%d\n",
2381 num_share_entries,
2382 (int)writeback);
2384 if (!writeback) {
2385 TALLOC_FREE(ltdb);
2386 return true;
2389 if ((ltdb->num_share_entries != 0 ) && (num_share_entries == 0)) {
2391 * This routine wiped all share entries, let
2392 * share_mode_data_store() delete the record
2394 d->modified = true;
2397 ltdb->num_share_entries = num_share_entries;
2398 ltdb->share_entries = share_entries;
2400 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
2401 TALLOC_FREE(ltdb);
2402 if (!NT_STATUS_IS_OK(status)) {
2403 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2404 nt_errstr(status));
2405 return false;
2408 return true;
2411 struct share_mode_count_entries_state {
2412 size_t num_share_modes;
2413 NTSTATUS status;
2416 static void share_mode_count_entries_fn(
2417 struct server_id exclusive,
2418 size_t num_shared,
2419 const struct server_id *shared,
2420 const uint8_t *data,
2421 size_t datalen,
2422 void *private_data)
2424 struct share_mode_count_entries_state *state = private_data;
2425 struct locking_tdb_data ltdb = { 0 };
2426 bool ok;
2428 ok = locking_tdb_data_get(&ltdb, data, datalen);
2429 if (!ok) {
2430 DBG_WARNING("locking_tdb_data_get failed for %zu\n", datalen);
2431 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
2432 return;
2434 state->num_share_modes = ltdb.num_share_entries;
2435 state->status = NT_STATUS_OK;
2438 NTSTATUS share_mode_count_entries(struct file_id fid, size_t *num_share_modes)
2440 struct share_mode_count_entries_state state = {
2441 .status = NT_STATUS_NOT_FOUND,
2443 NTSTATUS status;
2445 status = g_lock_dump(
2446 lock_ctx,
2447 locking_key(&fid),
2448 share_mode_count_entries_fn,
2449 &state);
2450 if (!NT_STATUS_IS_OK(status)) {
2451 DBG_DEBUG("g_lock_dump failed: %s\n",
2452 nt_errstr(status));
2453 return status;
2455 if (!NT_STATUS_IS_OK(state.status)) {
2456 DBG_DEBUG("share_mode_count_entries_fn failed: %s\n",
2457 nt_errstr(state.status));
2458 return state.status;
2461 *num_share_modes = state.num_share_modes;
2462 return NT_STATUS_OK;
2465 static bool share_mode_entry_do(
2466 struct share_mode_data *d,
2467 struct server_id pid,
2468 uint64_t share_file_id,
2469 void (*fn)(struct share_mode_entry *e,
2470 size_t num_share_modes,
2471 bool *modified,
2472 void *private_data),
2473 void *private_data)
2475 TDB_DATA key = locking_key(&d->id);
2476 struct locking_tdb_data *ltdb = NULL;
2477 size_t idx;
2478 bool found = false;
2479 bool modified = false;
2480 struct share_mode_entry e;
2481 uint8_t *e_ptr = NULL;
2482 NTSTATUS status;
2483 bool ret = false;
2485 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2486 if (!NT_STATUS_IS_OK(status)) {
2487 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2488 nt_errstr(status));
2489 return false;
2491 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2493 idx = share_mode_entry_find(
2494 ltdb->share_entries,
2495 ltdb->num_share_entries,
2496 pid,
2497 share_file_id,
2499 &found);
2500 if (!found) {
2501 DBG_WARNING("Did not find share mode entry for %"PRIu64"\n",
2502 share_file_id);
2503 goto done;
2506 if (DEBUGLEVEL>=10) {
2507 DBG_DEBUG("entry[%zu]:\n", idx);
2508 NDR_PRINT_DEBUG(share_mode_entry, &e);
2511 fn(&e, ltdb->num_share_entries, &modified, private_data);
2513 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2514 idx,
2515 (int)modified,
2516 (int)e.stale);
2518 if (!e.stale && !modified) {
2519 ret = true;
2520 goto done;
2523 e_ptr = discard_const_p(uint8_t, ltdb->share_entries) +
2524 idx * SHARE_MODE_ENTRY_SIZE;
2526 if (e.stale) {
2528 * Move the rest down one entry
2530 size_t behind = ltdb->num_share_entries - idx - 1;
2531 if (behind != 0) {
2532 memmove(e_ptr,
2533 e_ptr + SHARE_MODE_ENTRY_SIZE,
2534 behind * SHARE_MODE_ENTRY_SIZE);
2536 ltdb->num_share_entries -= 1;
2538 if (ltdb->num_share_entries == 0) {
2540 * Tell share_mode_lock_destructor() to delete
2541 * the whole record
2543 d->modified = true;
2546 if (DEBUGLEVEL>=10) {
2547 DBG_DEBUG("share_mode_entry:\n");
2548 NDR_PRINT_DEBUG(share_mode_entry, &e);
2550 } else {
2551 struct share_mode_entry_buf buf;
2552 bool ok;
2554 if (ltdb->num_share_entries != 1) {
2556 * Make sure the sorting order stays intact
2558 SMB_ASSERT(server_id_equal(&e.pid, &pid));
2559 SMB_ASSERT(e.share_file_id == share_file_id);
2562 ok = share_mode_entry_put(&e, &buf);
2563 if (!ok) {
2564 DBG_DEBUG("share_mode_entry_put failed\n");
2565 goto done;
2567 memcpy(e_ptr, buf.buf, SHARE_MODE_ENTRY_SIZE);
2570 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
2571 if (!NT_STATUS_IS_OK(status)) {
2572 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2573 nt_errstr(status));
2574 goto done;
2577 ret = true;
2578 done:
2579 TALLOC_FREE(ltdb);
2580 return ret;
2583 struct del_share_mode_state {
2584 bool ok;
2587 static void del_share_mode_fn(
2588 struct share_mode_entry *e,
2589 size_t num_share_modes,
2590 bool *modified,
2591 void *private_data)
2593 struct del_share_mode_state *state = private_data;
2594 e->stale = true;
2595 state->ok = true;
2598 bool del_share_mode_open_id(struct share_mode_lock *lck,
2599 struct server_id open_pid,
2600 uint64_t open_file_id)
2602 struct del_share_mode_state state = { .ok = false };
2603 struct share_mode_data *d = NULL;
2604 NTSTATUS status;
2605 bool ok;
2607 status = share_mode_lock_access_private_data(lck, &d);
2608 if (!NT_STATUS_IS_OK(status)) {
2609 /* Any error recovery possible here ? */
2610 return false;
2613 ok = share_mode_entry_do(
2615 open_pid,
2616 open_file_id,
2617 del_share_mode_fn,
2618 &state);
2619 if (!ok) {
2620 DBG_DEBUG("share_mode_entry_do failed\n");
2621 return false;
2623 if (!state.ok) {
2624 DBG_DEBUG("del_share_mode_fn failed\n");
2625 return false;
2627 return true;
2630 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
2632 struct server_id pid =
2633 messaging_server_id(fsp->conn->sconn->msg_ctx);
2634 bool ok;
2636 ok = del_share_mode_open_id(lck, pid, fh_get_gen_id(fsp->fh));
2637 if (!ok) {
2638 struct file_id id = share_mode_lock_file_id(lck);
2639 struct file_id_buf id_buf;
2640 DBG_ERR("share_mode_lock_access_private_data() failed for "
2641 "%s %s\n",
2642 file_id_str_buf(id, &id_buf),
2643 fsp_str_dbg(fsp));
2644 return false;
2646 return true;
2649 struct remove_share_oplock_state {
2650 bool ok;
2653 static void remove_share_oplock_fn(
2654 struct share_mode_entry *e,
2655 size_t num_share_modes,
2656 bool *modified,
2657 void *private_data)
2659 struct remove_share_oplock_state *state = private_data;
2661 e->op_type = NO_OPLOCK;
2662 *modified = true;
2663 state->ok = true;
2666 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2668 struct remove_share_oplock_state state = { .ok = false };
2669 struct share_mode_data *d = NULL;
2670 NTSTATUS status;
2671 bool ok;
2673 status = share_mode_lock_access_private_data(lck, &d);
2674 if (!NT_STATUS_IS_OK(status)) {
2675 struct file_id id = share_mode_lock_file_id(lck);
2676 struct file_id_buf id_buf;
2677 /* Any error recovery possible here ? */
2678 DBG_ERR("share_mode_lock_access_private_data() failed for "
2679 "%s %s - %s\n",
2680 file_id_str_buf(id, &id_buf),
2681 fsp_str_dbg(fsp),
2682 nt_errstr(status));
2683 return false;
2686 ok = share_mode_entry_do(
2688 messaging_server_id(fsp->conn->sconn->msg_ctx),
2689 fh_get_gen_id(fsp->fh),
2690 remove_share_oplock_fn,
2691 &state);
2692 if (!ok) {
2693 DBG_DEBUG("share_mode_entry_do failed\n");
2694 return false;
2696 if (!state.ok) {
2697 DBG_DEBUG("remove_share_oplock_fn failed\n");
2698 return false;
2701 if (fsp->oplock_type == LEASE_OPLOCK) {
2702 remove_lease_if_stale(
2703 lck,
2704 fsp_client_guid(fsp),
2705 &fsp->lease->lease.lease_key);
2708 share_mode_wakeup_waiters(fsp->file_id);
2710 return true;
2713 struct downgrade_share_oplock_state {
2714 bool ok;
2717 static void downgrade_share_oplock_fn(
2718 struct share_mode_entry *e,
2719 size_t num_share_modes,
2720 bool *modified,
2721 void *private_data)
2723 struct downgrade_share_oplock_state *state = private_data;
2725 e->op_type = LEVEL_II_OPLOCK;
2726 *modified = true;
2727 state->ok = true;
2730 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2732 struct downgrade_share_oplock_state state = { .ok = false };
2733 struct share_mode_data *d = NULL;
2734 NTSTATUS status;
2735 bool ok;
2737 status = share_mode_lock_access_private_data(lck, &d);
2738 if (!NT_STATUS_IS_OK(status)) {
2739 struct file_id id = share_mode_lock_file_id(lck);
2740 struct file_id_buf id_buf;
2741 /* Any error recovery possible here ? */
2742 DBG_ERR("share_mode_lock_access_private_data() failed for "
2743 "%s %s - %s\n",
2744 file_id_str_buf(id, &id_buf),
2745 fsp_str_dbg(fsp),
2746 nt_errstr(status));
2747 return false;
2750 ok = share_mode_entry_do(
2752 messaging_server_id(fsp->conn->sconn->msg_ctx),
2753 fh_get_gen_id(fsp->fh),
2754 downgrade_share_oplock_fn,
2755 &state);
2756 if (!ok) {
2757 DBG_DEBUG("share_mode_entry_do failed\n");
2758 return false;
2760 if (!state.ok) {
2761 DBG_DEBUG("downgrade_share_oplock_fn failed\n");
2762 return false;
2765 d->flags |= SHARE_MODE_LEASE_READ;
2766 d->modified = true;
2768 return true;
2771 bool mark_share_mode_disconnected(struct share_mode_lock *lck,
2772 struct files_struct *fsp)
2774 struct server_id disconnected_pid = { .pid = 0 };
2775 bool ok;
2777 if (fsp->op == NULL) {
2778 return false;
2780 if (!fsp->op->global->durable) {
2781 return false;
2784 server_id_set_disconnected(&disconnected_pid);
2786 ok = reset_share_mode_entry(
2787 lck,
2788 messaging_server_id(fsp->conn->sconn->msg_ctx),
2789 fh_get_gen_id(fsp->fh),
2790 disconnected_pid,
2791 UINT64_MAX,
2792 fsp->op->global->open_persistent_id);
2794 return ok;
2797 bool reset_share_mode_entry(
2798 struct share_mode_lock *lck,
2799 struct server_id old_pid,
2800 uint64_t old_share_file_id,
2801 struct server_id new_pid,
2802 uint64_t new_mid,
2803 uint64_t new_share_file_id)
2805 struct file_id id = share_mode_lock_file_id(lck);
2806 struct share_mode_data *d = NULL;
2807 TDB_DATA key = locking_key(&id);
2808 struct locking_tdb_data *ltdb = NULL;
2809 struct share_mode_entry e = { .pid.pid = 0 };
2810 struct share_mode_entry_buf e_buf;
2811 size_t old_idx;
2812 size_t new_idx;
2813 bool found;
2814 NTSTATUS status;
2815 bool ret = false;
2816 bool ok;
2817 struct file_id_buf id_buf;
2818 struct server_id_buf pid_buf1;
2819 struct server_id_buf pid_buf2;
2820 size_t low_idx1, low_idx2, low_num;
2821 size_t mid_idx1, mid_idx2, mid_num;
2822 size_t high_idx1, high_idx2, high_num;
2823 TDB_DATA dbufs[4];
2824 size_t num_dbufs = 0;
2826 status = share_mode_lock_access_private_data(lck, &d);
2827 if (!NT_STATUS_IS_OK(status)) {
2828 /* Any error recovery possible here ? */
2829 DBG_ERR("share_mode_lock_access_private_data() failed for "
2830 "%s - %s\n",
2831 file_id_str_buf(id, &id_buf),
2832 nt_errstr(status));
2833 return false;
2836 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2837 if (!NT_STATUS_IS_OK(status)) {
2838 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2839 nt_errstr(status));
2840 return false;
2843 DBG_DEBUG("%s - num_share_modes=%zu\n",
2844 file_id_str_buf(id, &id_buf),
2845 ltdb->num_share_entries);
2847 new_idx = share_mode_entry_find(
2848 ltdb->share_entries,
2849 ltdb->num_share_entries,
2850 new_pid,
2851 new_share_file_id,
2853 &found);
2854 if (found) {
2855 DBG_ERR("%s - num_share_modes=%zu "
2856 "found NEW[%s][%"PRIu64"]\n",
2857 file_id_str_buf(id, &id_buf),
2858 ltdb->num_share_entries,
2859 server_id_str_buf(new_pid, &pid_buf2),
2860 new_share_file_id);
2861 goto done;
2864 old_idx = share_mode_entry_find(
2865 ltdb->share_entries,
2866 ltdb->num_share_entries,
2867 old_pid,
2868 old_share_file_id,
2870 &found);
2871 if (!found) {
2872 DBG_WARNING("%s - num_share_modes=%zu "
2873 "OLD[%s][%"PRIu64"] not found\n",
2874 file_id_str_buf(id, &id_buf),
2875 ltdb->num_share_entries,
2876 server_id_str_buf(old_pid, &pid_buf1),
2877 old_share_file_id);
2878 goto done;
2880 DBG_DEBUG("%s - num_share_modes=%zu "
2881 "OLD[%s][%"PRIu64"] => idx=%zu "
2882 "NEW[%s][%"PRIu64"] => idx=%zu\n",
2883 file_id_str_buf(id, &id_buf),
2884 ltdb->num_share_entries,
2885 server_id_str_buf(old_pid, &pid_buf1),
2886 old_share_file_id,
2887 old_idx,
2888 server_id_str_buf(new_pid, &pid_buf2),
2889 new_share_file_id,
2890 new_idx);
2892 e.pid = new_pid;
2893 if (new_mid != UINT64_MAX) {
2894 e.op_mid = new_mid;
2896 e.share_file_id = new_share_file_id;
2898 ok = share_mode_entry_put(&e, &e_buf);
2899 if (!ok) {
2900 DBG_WARNING("share_mode_entry_put failed\n");
2901 goto done;
2905 * The logic to remove the existing
2906 * entry and add the new one at the
2907 * same time is a bit complex because
2908 * we need to keep the entries sorted.
2910 * The following examples should catch
2911 * the corner cases and show that
2912 * the {low,mid,high}_{idx1,num} are
2913 * correctly calculated and the new
2914 * entry is put before or after the mid
2915 * elements...
2917 * 1.
2920 * 2 <- old_idx
2921 * new_idx -> 3
2925 * low_idx1 = 0;
2926 * low_idx2 = MIN(old_idx, new_idx); => 2
2927 * low_num = low_idx2 - low_idx1; => 2
2929 * if (new < old) => new; => no
2931 * mid_idx1 = MIN(old_idx+1, new_idx); => 3
2932 * mid_idx2 = MAX(old_idx, new_idx); => 3
2933 * mid_num = mid_idx2 - mid_idx1; => 0
2935 * if (new >= old) => new; => yes
2937 * high_idx1 = MAX(old_idx+1, new_idx); => 3
2938 * high_idx2 = num_share_entries; => 5
2939 * high_num = high_idx2 - high_idx1 = 2
2941 * 2.
2944 * new_idx -> 2
2945 * 2 <- old_idx
2949 * low_idx1 = 0;
2950 * low_idx2 = MIN(old_idx, new_idx); => 2
2951 * low_num = low_idx2 - low_idx1; => 2
2953 * if (new < old) => new; => no
2955 * mid_idx1 = MIN(old_idx+1, new_idx); => 2
2956 * mid_idx2 = MAX(old_idx, new_idx); => 2
2957 * mid_num = mid_idx2 - mid_idx1; => 0
2959 * if (new >= old) => new; => yes
2961 * high_idx1 = MAX(old_idx+1, new_idx); => 3
2962 * high_idx2 = num_share_entries; => 5
2963 * high_num = high_idx2 - high_idx1 = 2
2965 * 3.
2967 * 1 <- old_idx
2969 * new_idx -> 3
2973 * low_idx1 = 0;
2974 * low_idx2 = MIN(old_idx, new_idx); => 1
2975 * low_num = low_idx2 - low_idx1; => 1
2977 * if (new < old) => new; => no
2979 * mid_idx1 = MIN(old_idx+1, new_idx); => 2
2980 * mid_idx2 = MAX(old_idx, new_idx); => 3
2981 * mid_num = mid_idx2 - mid_idx1; => 1
2983 * if (new >= old) => new; => yes
2985 * high_idx1 = MAX(old_idx+1, new_idx); => 3
2986 * high_idx2 = num_share_entries; => 5
2987 * high_num = high_idx2 - high_idx1 = 2
2989 * 4.
2991 * new_idx -> 1
2994 * 3 <- old_idx
2997 * low_idx1 = 0;
2998 * low_idx2 = MIN(old_idx, new_idx); => 1
2999 * low_num = low_idx2 - low_idx1; => 1
3001 * if (new < old) => new; => yes
3003 * mid_idx1 = MIN(old_idx+1, new_idx); => 1
3004 * mid_idx2 = MAX(old_idx, new_idx); => 3
3005 * mid_num = mid_idx2 - mid_idx1; => 2
3007 * if (new >= old) => new; => no
3009 * high_idx1 = MAX(old_idx+1, new_idx); => 4
3010 * high_idx2 = num_share_entries; => 5
3011 * high_num = high_idx2 - high_idx1 = 1
3013 * 5.
3014 * new_idx -> 0
3019 * 4 <- old_idx
3021 * low_idx1 = 0;
3022 * low_idx2 = MIN(old_idx, new_idx); => 0
3023 * low_num = low_idx2 - low_idx1; => 0
3025 * if (new < old) => new; => yes
3027 * mid_idx1 = MIN(old_idx+1, new_idx); => 0
3028 * mid_idx2 = MAX(old_idx, new_idx); => 4
3029 * mid_num = mid_idx2 - mid_idx1; => 4
3031 * if (new >= old) => new; => no
3033 * high_idx1 = MAX(old_idx+1, new_idx); => 5
3034 * high_idx2 = num_share_entries; => 5
3035 * high_num = high_idx2 - high_idx1 = 0
3037 * 6.
3038 * new_idx -> 0
3039 * 0 <- old_idx
3041 * low_idx1 = 0;
3042 * low_idx2 = MIN(old_idx, new_idx); => 0
3043 * low_num = low_idx2 - low_idx1; => 0
3045 * if (new < old) => new; => no
3047 * mid_idx1 = MIN(old_idx+1, new_idx); => 0
3048 * mid_idx2 = MAX(old_idx, new_idx); => 0
3049 * mid_num = mid_idx2 - mid_idx1; => 0
3051 * if (new >= old) => new; => yes
3053 * high_idx1 = MAX(old_idx+1, new_idx); => 1
3054 * high_idx2 = num_share_entries; => 1
3055 * high_num = high_idx2 - high_idx1 = 0
3057 * 7.
3058 * 0 <- old_idx
3059 * new_idx -> 1
3061 * low_idx1 = 0;
3062 * low_idx2 = MIN(old_idx, new_idx); => 0
3063 * low_num = low_idx2 - low_idx1; => 0
3065 * if (new < old) => new; => no
3067 * mid_idx1 = MIN(old_idx+1, new_idx); => 1
3068 * mid_idx2 = MAX(old_idx, new_idx); => 1
3069 * mid_num = mid_idx2 - mid_idx1; => 0
3071 * if (new >= old) => new; => yes
3073 * high_idx1 = MAX(old_idx+1, new_idx); => 1
3074 * high_idx2 = num_share_entries; => 1
3075 * high_num = high_idx2 - high_idx1 = 0
3077 low_idx1 = 0;
3078 low_idx2 = MIN(old_idx, new_idx);
3079 low_num = low_idx2 - low_idx1;
3080 mid_idx1 = MIN(old_idx+1, new_idx);
3081 mid_idx2 = MAX(old_idx, new_idx);
3082 mid_num = mid_idx2 - mid_idx1;
3083 high_idx1 = MAX(old_idx+1, new_idx);
3084 high_idx2 = ltdb->num_share_entries;
3085 high_num = high_idx2 - high_idx1;
3087 if (low_num != 0) {
3088 dbufs[num_dbufs] = (TDB_DATA) {
3089 .dptr = discard_const_p(uint8_t, ltdb->share_entries) +
3090 low_idx1 * SHARE_MODE_ENTRY_SIZE,
3091 .dsize = low_num * SHARE_MODE_ENTRY_SIZE,
3093 num_dbufs += 1;
3096 if (new_idx < old_idx) {
3097 dbufs[num_dbufs] = (TDB_DATA) {
3098 .dptr = e_buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
3100 num_dbufs += 1;
3103 if (mid_num != 0) {
3104 dbufs[num_dbufs] = (TDB_DATA) {
3105 .dptr = discard_const_p(uint8_t, ltdb->share_entries) +
3106 mid_idx1 * SHARE_MODE_ENTRY_SIZE,
3107 .dsize = mid_num * SHARE_MODE_ENTRY_SIZE,
3109 num_dbufs += 1;
3112 if (new_idx >= old_idx) {
3113 dbufs[num_dbufs] = (TDB_DATA) {
3114 .dptr = e_buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
3116 num_dbufs += 1;
3119 if (high_num != 0) {
3120 dbufs[num_dbufs] = (TDB_DATA) {
3121 .dptr = discard_const_p(uint8_t, ltdb->share_entries) +
3122 high_idx1 * SHARE_MODE_ENTRY_SIZE,
3123 .dsize = high_num * SHARE_MODE_ENTRY_SIZE,
3125 num_dbufs += 1;
3129 size_t i;
3130 for (i=0; i<num_dbufs; i++) {
3131 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
3133 dbufs[i].dptr,
3134 dbufs[i].dsize);
3139 * We completely rewrite the entries...
3141 ltdb->share_entries = NULL;
3142 ltdb->num_share_entries = 0;
3143 d->modified = true;
3145 status = share_mode_data_ltdb_store(d, key, ltdb, dbufs, num_dbufs);
3146 if (!NT_STATUS_IS_OK(status)) {
3147 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
3148 nt_errstr(status));
3149 goto done;
3152 ret = true;
3153 done:
3154 TALLOC_FREE(ltdb);
3155 return ret;
3158 struct share_mode_do_locked_vfs_denied_state {
3159 struct file_id id;
3160 share_mode_do_locked_vfs_fn_t fn;
3161 void *private_data;
3162 const char *location;
3163 NTSTATUS status;
3166 static void share_mode_do_locked_vfs_denied_fn(struct g_lock_lock_cb_state *glck,
3167 void *cb_private)
3169 struct share_mode_do_locked_vfs_denied_state *state =
3170 (struct share_mode_do_locked_vfs_denied_state *)cb_private;
3171 struct smb_vfs_deny_state vfs_deny = {};
3172 struct share_mode_lock lck;
3174 if (glck != NULL) {
3175 current_share_mode_glck = glck;
3178 state->status = get_share_mode_lock_internal(state->id,
3179 NULL, /* servicepath */
3180 NULL, /* smb_fname */
3181 NULL, /* old_write_time */
3182 &lck);
3183 if (!NT_STATUS_IS_OK(state->status)) {
3184 DBG_GET_SHARE_MODE_LOCK(state->status,
3185 "get_share_mode_lock_internal failed: %s\n",
3186 nt_errstr(state->status));
3187 if (glck != NULL) {
3188 g_lock_lock_cb_unlock(glck);
3189 current_share_mode_glck = NULL;
3191 return;
3194 _smb_vfs_deny_push(&vfs_deny, state->location);
3195 state->fn(&lck, state->private_data);
3196 _smb_vfs_deny_pop(&vfs_deny, state->location);
3198 state->status = put_share_mode_lock_internal(&lck);
3199 if (!NT_STATUS_IS_OK(state->status)) {
3200 DBG_ERR("put_share_mode_lock_internal failed: %s\n",
3201 nt_errstr(state->status));
3202 smb_panic("put_share_mode_lock_internal failed\n");
3203 return;
3206 if (glck != NULL) {
3207 g_lock_lock_cb_unlock(glck);
3208 current_share_mode_glck = NULL;
3210 return;
3214 * @brief Run @fn protected with G_LOCK_WRITE in the given file_id
3216 * @fn is NOT allowed to call SMB_VFS_* or similar functions,
3217 * which may block for some time in the kernel.
3219 * There must be at least one share_mode_entry, otherwise
3220 * NT_STATUS_NOT_FOUND is returned.
3222 * @param[in] id The key for the share_mode record.
3223 * @param[in] fn The function to run under the g_lock.
3224 * @param[in] private_date A private pointer passed to @fn.
3226 NTSTATUS _share_mode_do_locked_vfs_denied(
3227 struct file_id id,
3228 share_mode_do_locked_vfs_fn_t fn,
3229 void *private_data,
3230 const char *location)
3232 struct share_mode_do_locked_vfs_denied_state state = {
3233 .id = id,
3234 .fn = fn,
3235 .private_data = private_data,
3236 .location = location,
3239 if (share_mode_lock_key_refcount == 0) {
3240 TDB_DATA key = locking_key(&id);
3241 NTSTATUS status;
3243 share_mode_lock_skip_g_lock = true;
3244 status = g_lock_lock(
3245 lock_ctx,
3246 key,
3247 G_LOCK_WRITE,
3248 (struct timeval) { .tv_sec = 3600 },
3249 share_mode_do_locked_vfs_denied_fn,
3250 &state);
3251 share_mode_lock_skip_g_lock = false;
3252 if (!NT_STATUS_IS_OK(status)) {
3253 DBG_DEBUG("g_lock_lock failed: %s\n",
3254 nt_errstr(status));
3255 return status;
3257 return state.status;
3260 share_mode_do_locked_vfs_denied_fn(NULL, &state);
3262 return state.status;
3266 * @brief Run @fn protected with G_LOCK_WRITE in the given file_id
3268 * @fn is allowed to call SMB_VFS_* or similar functions,
3269 * which may block for some time in the kernel.
3271 * There must be at least one share_mode_entry, otherwise
3272 * NT_STATUS_NOT_FOUND is returned.
3274 * @param[in] id The key for the share_mode record.
3275 * @param[in] fn The function to run under the g_lock.
3276 * @param[in] private_date A private pointer passed to @fn.
3278 NTSTATUS _share_mode_do_locked_vfs_allowed(
3279 struct file_id id,
3280 share_mode_do_locked_vfs_fn_t fn,
3281 void *private_data,
3282 const char *location)
3284 struct share_mode_lock lck;
3285 NTSTATUS status;
3287 smb_vfs_assert_allowed();
3289 status = get_share_mode_lock_internal(id,
3290 NULL, /* servicepath */
3291 NULL, /* smb_fname */
3292 NULL, /* old_write_time */
3293 &lck);
3294 if (!NT_STATUS_IS_OK(status)) {
3295 DBG_GET_SHARE_MODE_LOCK(status,
3296 "get_share_mode_lock_internal failed: %s\n",
3297 nt_errstr(status));
3298 return status;
3301 fn(&lck, private_data);
3303 status = put_share_mode_lock_internal(&lck);
3304 if (!NT_STATUS_IS_OK(status)) {
3305 DBG_ERR("put_share_mode_lock_internal failed: %s\n",
3306 nt_errstr(status));
3307 smb_panic("put_share_mode_lock_internal failed\n");
3308 return status;
3311 return NT_STATUS_OK;
3314 struct share_mode_entry_prepare_lock_state {
3315 struct file_id id;
3316 const char *servicepath;
3317 const struct smb_filename *smb_fname;
3318 const struct timespec *old_write_time;
3319 share_mode_entry_prepare_lock_fn_t fn;
3320 void *private_data;
3321 const char *location;
3322 bool keep_locked;
3323 struct share_mode_lock *lck;
3324 NTSTATUS status;
3327 static void share_mode_entry_prepare_lock_fn(struct g_lock_lock_cb_state *glck,
3328 void *cb_private)
3330 struct share_mode_entry_prepare_lock_state *state =
3331 (struct share_mode_entry_prepare_lock_state *)cb_private;
3332 struct smb_vfs_deny_state vfs_deny = {};
3334 SMB_ASSERT(glck != NULL);
3335 current_share_mode_glck = glck;
3337 state->status = get_share_mode_lock_internal(state->id,
3338 state->servicepath,
3339 state->smb_fname,
3340 state->old_write_time,
3341 state->lck);
3342 if (!NT_STATUS_IS_OK(state->status)) {
3343 /* no DBG_GET_SHARE_MODE_LOCK here! */
3344 DBG_ERR("get_share_mode_lock_internal failed: %s\n",
3345 nt_errstr(state->status));
3346 g_lock_lock_cb_unlock(glck);
3347 current_share_mode_glck = NULL;
3348 return;
3351 _smb_vfs_deny_push(&vfs_deny, state->location);
3352 state->fn(state->lck, &state->keep_locked, state->private_data);
3353 _smb_vfs_deny_pop(&vfs_deny, state->location);
3355 if (state->keep_locked) {
3356 current_share_mode_glck = NULL;
3357 return;
3360 state->status = put_share_mode_lock_internal(state->lck);
3361 if (!NT_STATUS_IS_OK(state->status)) {
3362 DBG_ERR("put_share_mode_lock_internal failed: %s\n",
3363 nt_errstr(state->status));
3364 smb_panic("put_share_mode_lock_internal failed\n");
3365 return;
3368 g_lock_lock_cb_unlock(glck);
3369 current_share_mode_glck = NULL;
3370 return;
3373 NTSTATUS _share_mode_entry_prepare_lock(
3374 struct share_mode_entry_prepare_state *prepare_state,
3375 struct file_id id,
3376 const char *servicepath,
3377 const struct smb_filename *smb_fname,
3378 const struct timespec *old_write_time,
3379 share_mode_entry_prepare_lock_fn_t fn,
3380 void *private_data,
3381 const char *location)
3383 struct share_mode_entry_prepare_lock_state state = {
3384 .id = id,
3385 .servicepath = servicepath,
3386 .smb_fname = smb_fname,
3387 .old_write_time = old_write_time,
3388 .fn = fn,
3389 .private_data = private_data,
3390 .location = location,
3392 TDB_DATA key = locking_key(&id);
3393 NTSTATUS status;
3395 SMB_ASSERT(share_mode_lock_key_refcount == 0);
3397 SMB_ASSERT(__SHARE_MODE_LOCK_SPACE >= sizeof(struct share_mode_lock));
3399 *prepare_state = (struct share_mode_entry_prepare_state) {
3400 .__fid = id,
3401 .__lck_ptr = &prepare_state->__lck_space,
3404 state.lck = prepare_state->__lck_ptr;
3406 share_mode_lock_skip_g_lock = true;
3407 status = g_lock_lock(
3408 lock_ctx,
3409 key,
3410 G_LOCK_WRITE,
3411 (struct timeval) { .tv_sec = 3600 },
3412 share_mode_entry_prepare_lock_fn,
3413 &state);
3414 share_mode_lock_skip_g_lock = false;
3415 if (!state.keep_locked) {
3416 prepare_state->__lck_ptr = NULL;
3418 if (!NT_STATUS_IS_OK(status)) {
3419 DBG_DEBUG("g_lock_lock failed: %s\n",
3420 nt_errstr(status));
3421 return status;
3424 return state.status;
3427 struct share_mode_entry_prepare_unlock_state {
3428 struct file_id id;
3429 share_mode_entry_prepare_unlock_fn_t fn;
3430 void *private_data;
3431 const char *location;
3432 struct share_mode_lock *lck;
3433 NTSTATUS status;
3436 static void share_mode_entry_prepare_unlock_existing_fn(
3437 struct share_mode_entry_prepare_unlock_state *state)
3439 if (state->fn != NULL) {
3440 struct smb_vfs_deny_state vfs_deny = {};
3442 _smb_vfs_deny_push(&vfs_deny, state->location);
3443 state->fn(state->lck, state->private_data);
3444 _smb_vfs_deny_pop(&vfs_deny, state->location);
3447 state->status = put_share_mode_lock_internal(state->lck);
3448 if (!NT_STATUS_IS_OK(state->status)) {
3449 DBG_ERR("put_share_mode_lock_internal failed: %s\n",
3450 nt_errstr(state->status));
3451 smb_panic("put_share_mode_lock_internal failed\n");
3452 return;
3455 return;
3458 static void share_mode_entry_prepare_unlock_relock_fn(struct g_lock_lock_cb_state *glck,
3459 void *cb_private)
3461 struct share_mode_entry_prepare_unlock_state *state =
3462 (struct share_mode_entry_prepare_unlock_state *)cb_private;
3463 struct smb_vfs_deny_state vfs_deny = {};
3465 SMB_ASSERT(glck != NULL);
3466 current_share_mode_glck = glck;
3468 state->status = get_share_mode_lock_internal(state->id,
3469 NULL, /* servicepath */
3470 NULL, /* smb_fname */
3471 NULL, /* old_write_time */
3472 state->lck);
3473 if (!NT_STATUS_IS_OK(state->status)) {
3474 /* no DBG_GET_SHARE_MODE_LOCK here! */
3475 DBG_ERR("get_share_mode_lock_internal failed: %s\n",
3476 nt_errstr(state->status));
3477 g_lock_lock_cb_unlock(glck);
3478 current_share_mode_glck = NULL;
3479 return;
3482 _smb_vfs_deny_push(&vfs_deny, state->location);
3483 state->fn(state->lck, state->private_data);
3484 _smb_vfs_deny_pop(&vfs_deny, state->location);
3486 state->status = put_share_mode_lock_internal(state->lck);
3487 if (!NT_STATUS_IS_OK(state->status)) {
3488 DBG_ERR("put_share_mode_lock_internal failed: %s\n",
3489 nt_errstr(state->status));
3490 smb_panic("put_share_mode_lock_internal failed\n");
3491 return;
3494 g_lock_lock_cb_unlock(glck);
3495 current_share_mode_glck = NULL;
3496 return;
3499 NTSTATUS _share_mode_entry_prepare_unlock(
3500 struct share_mode_entry_prepare_state *prepare_state,
3501 share_mode_entry_prepare_unlock_fn_t fn,
3502 void *private_data,
3503 const char *location)
3505 struct share_mode_entry_prepare_unlock_state state = {
3506 .id = prepare_state->__fid,
3507 .fn = fn,
3508 .private_data = private_data,
3509 .location = location,
3511 TDB_DATA key = locking_key(&prepare_state->__fid);
3512 NTSTATUS status;
3514 if (prepare_state->__lck_ptr != NULL) {
3516 * With an existing lock, we just run the unlock prepare
3517 * function following by the unlock.
3520 SMB_ASSERT(share_mode_lock_key_refcount == 1);
3522 state.lck = prepare_state->__lck_ptr;
3523 prepare_state->__lck_ptr = NULL;
3525 share_mode_entry_prepare_unlock_existing_fn(&state);
3526 return state.status;
3530 * No existing lock, which means
3531 * _share_mode_entry_prepare_lock() didn't steal
3532 * the lock...
3534 SMB_ASSERT(share_mode_lock_key_refcount == 0);
3536 if (fn == NULL) {
3538 * Without an existing lock and without
3539 * a prepare function there's nothing to
3540 * do...
3542 return NT_STATUS_OK;
3546 * In order to run the unlock prepare function
3547 * we need to relock the entry.
3549 state.lck = &prepare_state->__lck_space;
3551 share_mode_lock_skip_g_lock = true;
3552 status = g_lock_lock(
3553 lock_ctx,
3554 key,
3555 G_LOCK_WRITE,
3556 (struct timeval) { .tv_sec = 3600 },
3557 share_mode_entry_prepare_unlock_relock_fn,
3558 &state);
3559 share_mode_lock_skip_g_lock = false;
3560 if (!NT_STATUS_IS_OK(status)) {
3561 DBG_ERR("g_lock_lock failed: %s\n",
3562 nt_errstr(status));
3563 return status;
3566 return state.status;