s3:locking: split out get_share_mode_lock_internal()
[Samba.git] / source3 / locking / share_mode_lock.c
blobd192dfdce876917b59a8456bc74149850df1b454
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.h"
43 #include "share_mode_lock_private.h"
44 #include "locking/proto.h"
45 #include "smbd/globals.h"
46 #include "dbwrap/dbwrap.h"
47 #include "dbwrap/dbwrap_open.h"
48 #include "dbwrap/dbwrap_private.h"
49 #include "../libcli/security/security.h"
50 #include "serverid.h"
51 #include "messages.h"
52 #include "util_tdb.h"
53 #include "../librpc/gen_ndr/ndr_open_files.h"
54 #include "source3/lib/dbwrap/dbwrap_watch.h"
55 #include "locking/leases_db.h"
56 #include "../lib/util/memcache.h"
57 #include "lib/util/tevent_ntstatus.h"
58 #include "g_lock.h"
59 #include "smbd/fd_handle.h"
60 #include "lib/global_contexts.h"
62 #undef DBGC_CLASS
63 #define DBGC_CLASS DBGC_LOCKING
65 #define DBG_GET_SHARE_MODE_LOCK(__status, ...) \
66 DBG_PREFIX( \
67 NT_STATUS_EQUAL(__status, NT_STATUS_NOT_FOUND) ? \
68 DBGLVL_DEBUG : DBGLVL_ERR, \
69 (__VA_ARGS__))
71 struct share_mode_lock {
72 struct file_id id;
73 struct share_mode_data *cached_data;
76 /* the locking database handle */
77 static struct g_lock_ctx *lock_ctx;
79 static bool locking_init_internal(bool read_only)
81 struct db_context *backend;
82 char *db_path;
84 brl_init(read_only);
86 if (lock_ctx != NULL) {
87 return True;
90 db_path = lock_path(talloc_tos(), "locking.tdb");
91 if (db_path == NULL) {
92 return false;
95 backend = db_open(NULL, db_path,
96 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
97 TDB_DEFAULT|
98 TDB_VOLATILE|
99 TDB_CLEAR_IF_FIRST|
100 TDB_INCOMPATIBLE_HASH|
101 TDB_SEQNUM,
102 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
103 DBWRAP_LOCK_ORDER_NONE,
104 DBWRAP_FLAG_NONE);
105 TALLOC_FREE(db_path);
106 if (!backend) {
107 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
108 return False;
111 lock_ctx = g_lock_ctx_init_backend(
112 NULL, global_messaging_context(), &backend);
113 if (lock_ctx == NULL) {
114 TALLOC_FREE(backend);
115 return false;
117 g_lock_set_lock_order(lock_ctx, DBWRAP_LOCK_ORDER_1);
119 if (!posix_locking_init(read_only)) {
120 TALLOC_FREE(lock_ctx);
121 return False;
124 return True;
127 bool locking_init(void)
129 return locking_init_internal(false);
132 bool locking_init_readonly(void)
134 return locking_init_internal(true);
137 /*******************************************************************
138 Deinitialize the share_mode management.
139 ******************************************************************/
141 bool locking_end(void)
143 brl_shutdown();
144 TALLOC_FREE(lock_ctx);
145 return true;
148 /*******************************************************************
149 Form a static locking key for a dev/inode pair.
150 ******************************************************************/
152 static TDB_DATA locking_key(const struct file_id *id)
154 return make_tdb_data((const uint8_t *)id, sizeof(*id));
157 /*******************************************************************
158 Share mode cache utility functions that store/delete/retrieve
159 entries from memcache.
161 For now share the statcache (global cache) memory space. If
162 a lock record gets orphaned (which shouldn't happen as we're
163 using the same locking_key data as lookup) it will eventually
164 fall out of the cache via the normal LRU trim mechanism. If
165 necessary we can always make this a separate (smaller) cache.
166 ******************************************************************/
168 static DATA_BLOB memcache_key(const struct file_id *id)
170 return data_blob_const((const void *)id, sizeof(*id));
173 static void share_mode_memcache_store(struct share_mode_data *d)
175 const DATA_BLOB key = memcache_key(&d->id);
176 struct file_id_buf idbuf;
178 DBG_DEBUG("stored entry for file %s epoch %"PRIx64" key %s\n",
179 d->base_name,
180 d->unique_content_epoch,
181 file_id_str_buf(d->id, &idbuf));
183 /* Ensure everything stored in the cache is pristine. */
184 SMB_ASSERT(!d->modified);
185 SMB_ASSERT(!d->not_stored);
188 * Ensure the memory going into the cache
189 * doesn't have a destructor so it can be
190 * cleanly evicted by the memcache LRU
191 * mechanism.
193 talloc_set_destructor(d, NULL);
195 /* Cache will own d after this call. */
196 memcache_add_talloc(NULL,
197 SHARE_MODE_LOCK_CACHE,
198 key,
199 &d);
203 * NB. We use ndr_pull_hyper on a stack-created
204 * struct ndr_pull with no talloc allowed, as we
205 * need this to be really fast as an ndr-peek into
206 * the first 10 bytes of the blob.
209 static enum ndr_err_code get_share_mode_blob_header(
210 const uint8_t *buf, size_t buflen, uint64_t *pepoch, uint16_t *pflags)
212 struct ndr_pull ndr = {
213 .data = discard_const_p(uint8_t, buf),
214 .data_size = buflen,
216 NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pepoch));
217 NDR_CHECK(ndr_pull_uint16(&ndr, NDR_SCALARS, pflags));
218 return NDR_ERR_SUCCESS;
221 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
223 return -1;
226 static struct share_mode_data *share_mode_memcache_fetch(
227 TALLOC_CTX *mem_ctx,
228 struct file_id id,
229 const uint8_t *buf,
230 size_t buflen)
232 const DATA_BLOB key = memcache_key(&id);
233 enum ndr_err_code ndr_err;
234 struct share_mode_data *d;
235 uint64_t unique_content_epoch;
236 uint16_t flags;
237 void *ptr;
238 struct file_id_buf idbuf;
240 ptr = memcache_lookup_talloc(NULL,
241 SHARE_MODE_LOCK_CACHE,
242 key);
243 if (ptr == NULL) {
244 DBG_DEBUG("failed to find entry for key %s\n",
245 file_id_str_buf(id, &idbuf));
246 return NULL;
248 /* sequence number key is at start of blob. */
249 ndr_err = get_share_mode_blob_header(
250 buf, buflen, &unique_content_epoch, &flags);
251 if (ndr_err != NDR_ERR_SUCCESS) {
252 /* Bad blob. Remove entry. */
253 DBG_DEBUG("bad blob %u key %s\n",
254 (unsigned int)ndr_err,
255 file_id_str_buf(id, &idbuf));
256 memcache_delete(NULL,
257 SHARE_MODE_LOCK_CACHE,
258 key);
259 return NULL;
262 d = (struct share_mode_data *)ptr;
263 if (d->unique_content_epoch != unique_content_epoch) {
264 DBG_DEBUG("epoch changed (cached %"PRIx64") (new %"PRIx64") "
265 "for key %s\n",
266 d->unique_content_epoch,
267 unique_content_epoch,
268 file_id_str_buf(id, &idbuf));
269 /* Cache out of date. Remove entry. */
270 memcache_delete(NULL,
271 SHARE_MODE_LOCK_CACHE,
272 key);
273 return NULL;
276 /* Move onto mem_ctx. */
277 d = talloc_move(mem_ctx, &ptr);
280 * Now we own d, prevent the cache from freeing it
281 * when we delete the entry.
283 talloc_set_destructor(d, share_mode_data_nofree_destructor);
285 /* Remove from the cache. We own it now. */
286 memcache_delete(NULL,
287 SHARE_MODE_LOCK_CACHE,
288 key);
290 /* And reset the destructor to none. */
291 talloc_set_destructor(d, NULL);
293 DBG_DEBUG("fetched entry for file %s epoch %"PRIx64" key %s\n",
294 d->base_name,
295 d->unique_content_epoch,
296 file_id_str_buf(id, &idbuf));
298 return d;
302 * 132 is the sizeof an ndr-encoded struct share_mode_entry_buf.
303 * Reading/writing entries will immediately error out if this
304 * size differs (push/pull is done without allocs).
307 struct share_mode_entry_buf {
308 uint8_t buf[132];
310 #define SHARE_MODE_ENTRY_SIZE (sizeof(struct share_mode_entry_buf))
312 static bool share_mode_entry_put(
313 const struct share_mode_entry *e,
314 struct share_mode_entry_buf *dst)
316 DATA_BLOB blob = { .data = dst->buf, .length = sizeof(dst->buf) };
317 enum ndr_err_code ndr_err;
319 if (DEBUGLEVEL>=10) {
320 DBG_DEBUG("share_mode_entry:\n");
321 NDR_PRINT_DEBUG(share_mode_entry, discard_const_p(void, e));
324 ndr_err = ndr_push_struct_into_fixed_blob(
325 &blob,
327 (ndr_push_flags_fn_t)ndr_push_share_mode_entry);
328 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
329 DBG_WARNING("ndr_push_share_mode_entry failed: %s\n",
330 ndr_errstr(ndr_err));
331 return false;
334 return true;
337 static bool share_mode_entry_get(
338 const uint8_t ptr[SHARE_MODE_ENTRY_SIZE], struct share_mode_entry *e)
340 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
341 DATA_BLOB blob = {
342 .data = discard_const_p(uint8_t, ptr),
343 .length = SHARE_MODE_ENTRY_SIZE,
346 ndr_err = ndr_pull_struct_blob_all_noalloc(
347 &blob, e, (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
348 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
349 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
350 return false;
352 return true;
356 * locking.tdb records consist of
358 * uint32_t share_mode_data_len
359 * uint8_t [share_mode_data] This is struct share_mode_data in NDR
361 * 0 [SHARE_MODE_ENTRY_SIZE] Sorted array of share modes,
362 * 1 [SHARE_MODE_ENTRY_SIZE] filling up the rest of the data in the
363 * 2 [SHARE_MODE_ENTRY_SIZE] g_lock.c maintained record in locking.tdb
366 struct locking_tdb_data {
367 const uint8_t *share_mode_data_buf;
368 size_t share_mode_data_len;
369 const uint8_t *share_entries;
370 size_t num_share_entries;
373 static bool locking_tdb_data_get(
374 struct locking_tdb_data *data, const uint8_t *buf, size_t buflen)
376 uint32_t share_mode_data_len, share_entries_len;
378 if (buflen == 0) {
379 *data = (struct locking_tdb_data) { 0 };
380 return true;
382 if (buflen < sizeof(uint32_t)) {
383 return false;
386 share_mode_data_len = PULL_LE_U32(buf, 0);
388 buf += sizeof(uint32_t);
389 buflen -= sizeof(uint32_t);
391 if (buflen < share_mode_data_len) {
392 return false;
395 share_entries_len = buflen - share_mode_data_len;
397 if ((share_entries_len % SHARE_MODE_ENTRY_SIZE) != 0) {
398 return false;
401 *data = (struct locking_tdb_data) {
402 .share_mode_data_buf = buf,
403 .share_mode_data_len = share_mode_data_len,
404 .share_entries = buf + share_mode_data_len,
405 .num_share_entries = share_entries_len / SHARE_MODE_ENTRY_SIZE,
408 return true;
411 struct locking_tdb_data_fetch_state {
412 TALLOC_CTX *mem_ctx;
413 uint8_t *data;
414 size_t datalen;
417 static void locking_tdb_data_fetch_fn(
418 struct server_id exclusive,
419 size_t num_shared,
420 const struct server_id *shared,
421 const uint8_t *data,
422 size_t datalen,
423 void *private_data)
425 struct locking_tdb_data_fetch_state *state = private_data;
426 state->datalen = datalen;
427 state->data = talloc_memdup(state->mem_ctx, data, datalen);
430 static NTSTATUS locking_tdb_data_fetch(
431 TDB_DATA key, TALLOC_CTX *mem_ctx, struct locking_tdb_data **ltdb)
433 struct locking_tdb_data_fetch_state state = { 0 };
434 struct locking_tdb_data *result = NULL;
435 NTSTATUS status;
436 bool ok;
438 result = talloc_zero(mem_ctx, struct locking_tdb_data);
439 if (result == NULL) {
440 return NT_STATUS_NO_MEMORY;
442 state.mem_ctx = result;
444 status = g_lock_dump(lock_ctx, key, locking_tdb_data_fetch_fn, &state);
446 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
448 * Just return an empty record
450 goto done;
452 if (!NT_STATUS_IS_OK(status)) {
453 DBG_ERR("g_lock_dump failed: %s\n",
454 nt_errstr(status));
455 return status;
457 if (state.datalen == 0) {
458 goto done;
461 ok = locking_tdb_data_get(result, state.data, state.datalen);
462 if (!ok) {
463 DBG_ERR("locking_tdb_data_get failed for %zu bytes\n",
464 state.datalen);
465 TALLOC_FREE(result);
466 return NT_STATUS_INTERNAL_DB_CORRUPTION;
469 done:
470 *ltdb = result;
471 return NT_STATUS_OK;
474 static NTSTATUS locking_tdb_data_store(
475 TDB_DATA key,
476 const struct locking_tdb_data *ltdb,
477 const TDB_DATA *share_mode_dbufs,
478 size_t num_share_mode_dbufs)
480 uint8_t share_mode_data_len_buf[4];
481 TDB_DATA dbufs[num_share_mode_dbufs+3];
482 NTSTATUS status;
484 if ((ltdb->share_mode_data_len == 0) &&
485 (ltdb->num_share_entries == 0) &&
486 (num_share_mode_dbufs == 0)) {
488 * Nothing to write
490 status = g_lock_write_data(lock_ctx, key, NULL, 0);
491 if (!NT_STATUS_IS_OK(status)) {
492 DBG_ERR("g_lock_writev_data() failed: %s\n",
493 nt_errstr(status));
495 return status;
498 PUSH_LE_U32(share_mode_data_len_buf, 0, ltdb->share_mode_data_len);
500 dbufs[0] = (TDB_DATA) {
501 .dptr = share_mode_data_len_buf,
502 .dsize = sizeof(share_mode_data_len_buf),
504 dbufs[1] = (TDB_DATA) {
505 .dptr = discard_const_p(uint8_t, ltdb->share_mode_data_buf),
506 .dsize = ltdb->share_mode_data_len,
509 if (ltdb->num_share_entries > SIZE_MAX/SHARE_MODE_ENTRY_SIZE) {
510 /* overflow */
511 return NT_STATUS_BUFFER_OVERFLOW;
513 dbufs[2] = (TDB_DATA) {
514 .dptr = discard_const_p(uint8_t, ltdb->share_entries),
515 .dsize = ltdb->num_share_entries * SHARE_MODE_ENTRY_SIZE,
518 if (num_share_mode_dbufs != 0) {
519 memcpy(&dbufs[3],
520 share_mode_dbufs,
521 num_share_mode_dbufs * sizeof(TDB_DATA));
524 status = g_lock_writev_data(lock_ctx, key, dbufs, ARRAY_SIZE(dbufs));
525 if (!NT_STATUS_IS_OK(status)) {
526 DBG_ERR("g_lock_writev_data() failed: %s\n",
527 nt_errstr(status));
529 return status;
532 /*******************************************************************
533 Get all share mode entries for a dev/inode pair.
534 ********************************************************************/
536 static struct share_mode_data *parse_share_modes(
537 TALLOC_CTX *mem_ctx,
538 struct file_id id,
539 const uint8_t *buf,
540 size_t buflen)
542 struct share_mode_data *d;
543 enum ndr_err_code ndr_err;
544 DATA_BLOB blob;
546 /* See if we already have a cached copy of this key. */
547 d = share_mode_memcache_fetch(mem_ctx, id, buf, buflen);
548 if (d != NULL) {
549 return d;
552 d = talloc(mem_ctx, struct share_mode_data);
553 if (d == NULL) {
554 DEBUG(0, ("talloc failed\n"));
555 goto fail;
558 blob = (DATA_BLOB) {
559 .data = discard_const_p(uint8_t, buf),
560 .length = buflen,
562 ndr_err = ndr_pull_struct_blob_all(
563 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
564 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
565 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
566 ndr_errstr(ndr_err));
567 goto fail;
570 if (DEBUGLEVEL >= 10) {
571 DEBUG(10, ("parse_share_modes:\n"));
572 NDR_PRINT_DEBUG(share_mode_data, d);
575 return d;
576 fail:
577 TALLOC_FREE(d);
578 return NULL;
581 static NTSTATUS share_mode_data_ltdb_store(struct share_mode_data *d,
582 TDB_DATA key,
583 struct locking_tdb_data *ltdb,
584 const TDB_DATA *share_mode_dbufs,
585 size_t num_share_mode_dbufs)
587 DATA_BLOB blob = { 0 };
588 NTSTATUS status;
590 if (!d->modified) {
591 DBG_DEBUG("share_mode_data not modified\n");
592 goto store;
595 d->unique_content_epoch = generate_unique_u64(d->unique_content_epoch);
597 if (DEBUGLEVEL >= 10) {
598 DBG_DEBUG("\n");
599 NDR_PRINT_DEBUG(share_mode_data, d);
602 if (ltdb->num_share_entries != 0 || num_share_mode_dbufs != 0) {
603 enum ndr_err_code ndr_err;
605 ndr_err = ndr_push_struct_blob(
606 &blob,
607 ltdb,
609 (ndr_push_flags_fn_t)ndr_push_share_mode_data);
610 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
611 DBG_ERR("ndr_push_share_mode_data failed: %s\n",
612 ndr_errstr(ndr_err));
613 return ndr_map_error2ntstatus(ndr_err);
617 ltdb->share_mode_data_buf = blob.data;
618 ltdb->share_mode_data_len = blob.length;
620 store:
621 status = locking_tdb_data_store(key,
622 ltdb,
623 share_mode_dbufs,
624 num_share_mode_dbufs);
625 if (!NT_STATUS_IS_OK(status)) {
626 DBG_ERR("locking_tdb_data_store failed: %s\n",
627 nt_errstr(status));
628 return status;
631 d->modified = false;
632 d->not_stored = (ltdb->share_mode_data_len == 0);
634 return NT_STATUS_OK;
637 /*******************************************************************
638 If modified, store the share_mode_data back into the database.
639 ********************************************************************/
641 static NTSTATUS share_mode_data_store(struct share_mode_data *d)
643 TDB_DATA key = locking_key(&d->id);
644 struct locking_tdb_data *ltdb = NULL;
645 NTSTATUS status;
647 if (!d->modified) {
648 DBG_DEBUG("not modified\n");
649 return NT_STATUS_OK;
652 if (DEBUGLEVEL >= 10) {
653 DBG_DEBUG("\n");
654 NDR_PRINT_DEBUG(share_mode_data, d);
657 status = locking_tdb_data_fetch(key, d, &ltdb);
658 if (!NT_STATUS_IS_OK(status)) {
659 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
660 nt_errstr(status));
661 return status;
664 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
665 TALLOC_FREE(ltdb);
666 if (!NT_STATUS_IS_OK(status)) {
667 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
668 nt_errstr(status));
669 return status;
672 return NT_STATUS_OK;
675 /*******************************************************************
676 Allocate a new share_mode_data struct, mark it unmodified.
677 fresh is set to note that currently there is no database entry.
678 ********************************************************************/
680 static struct share_mode_data *fresh_share_mode_lock(
681 TALLOC_CTX *mem_ctx, const char *servicepath,
682 const struct smb_filename *smb_fname,
683 const struct timespec *old_write_time)
685 struct share_mode_data *d;
687 if ((servicepath == NULL) || (smb_fname == NULL) ||
688 (old_write_time == NULL)) {
689 return NULL;
692 d = talloc_zero(mem_ctx, struct share_mode_data);
693 if (d == NULL) {
694 goto fail;
696 d->unique_content_epoch = generate_unique_u64(0);
698 d->base_name = talloc_strdup(d, smb_fname->base_name);
699 if (d->base_name == NULL) {
700 goto fail;
702 if (smb_fname->stream_name != NULL) {
703 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
704 if (d->stream_name == NULL) {
705 goto fail;
708 d->servicepath = talloc_strdup(d, servicepath);
709 if (d->servicepath == NULL) {
710 goto fail;
712 d->old_write_time = full_timespec_to_nt_time(old_write_time);
713 d->flags = SHARE_MODE_SHARE_DELETE |
714 SHARE_MODE_SHARE_WRITE |
715 SHARE_MODE_SHARE_READ;
716 d->modified = false;
717 d->not_stored = true;
718 return d;
719 fail:
720 DEBUG(0, ("talloc failed\n"));
721 TALLOC_FREE(d);
722 return NULL;
726 * Key that's locked with g_lock
728 static struct file_id share_mode_lock_key_id = {};
729 static TDB_DATA share_mode_lock_key = {
730 .dptr = (uint8_t *)&share_mode_lock_key_id,
731 .dsize = sizeof(share_mode_lock_key_id),
733 static size_t share_mode_lock_key_refcount = 0;
736 * We can only ever have one share mode locked. Use a static
737 * share_mode_data pointer that is shared by multiple nested
738 * share_mode_lock structures, explicitly refcounted.
740 static struct share_mode_data *static_share_mode_data = NULL;
742 /*******************************************************************
743 Either fetch a share mode from the database, or allocate a fresh
744 one if the record doesn't exist.
745 ********************************************************************/
747 struct get_static_share_mode_data_state {
748 TALLOC_CTX *mem_ctx;
749 struct file_id id;
750 const char *servicepath;
751 const struct smb_filename *smb_fname;
752 const struct timespec *old_write_time;
753 NTSTATUS status;
756 static void get_static_share_mode_data_fn(
757 struct server_id exclusive,
758 size_t num_shared,
759 const struct server_id *shared,
760 const uint8_t *data,
761 size_t datalen,
762 void *private_data)
764 struct get_static_share_mode_data_state *state = private_data;
765 struct share_mode_data *d = NULL;
766 struct locking_tdb_data ltdb = { 0 };
768 if (datalen != 0) {
769 bool ok;
771 ok = locking_tdb_data_get(&ltdb, data, datalen);
772 if (!ok) {
773 DBG_ERR("locking_tdb_data_get failed\n");
774 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
775 return;
779 if (ltdb.share_mode_data_len == 0) {
780 if (state->smb_fname == NULL) {
781 state->status = NT_STATUS_NOT_FOUND;
782 return;
784 d = fresh_share_mode_lock(
785 state->mem_ctx,
786 state->servicepath,
787 state->smb_fname,
788 state->old_write_time);
789 if (d == NULL) {
790 state->status = NT_STATUS_NO_MEMORY;
791 return;
793 } else {
794 d = parse_share_modes(
795 lock_ctx,
796 state->id,
797 ltdb.share_mode_data_buf,
798 ltdb.share_mode_data_len);
799 if (d == NULL) {
800 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
801 return;
805 d->id = state->id;
806 static_share_mode_data = d;
809 static NTSTATUS get_static_share_mode_data(
810 struct file_id id,
811 const char *servicepath,
812 const struct smb_filename *smb_fname,
813 const struct timespec *old_write_time)
815 struct get_static_share_mode_data_state state = {
816 .mem_ctx = lock_ctx,
817 .id = id,
818 .servicepath = servicepath,
819 .smb_fname = smb_fname,
820 .old_write_time = old_write_time,
822 NTSTATUS status;
824 SMB_ASSERT(static_share_mode_data == NULL);
826 status = g_lock_dump(
827 lock_ctx,
828 share_mode_lock_key,
829 get_static_share_mode_data_fn,
830 &state);
831 if (!NT_STATUS_IS_OK(status)) {
832 DBG_GET_SHARE_MODE_LOCK(status,
833 "g_lock_dump failed: %s\n",
834 nt_errstr(status));
835 return status;
837 if (!NT_STATUS_IS_OK(state.status)) {
838 DBG_GET_SHARE_MODE_LOCK(status,
839 "get_static_share_mode_data_fn failed: %s\n",
840 nt_errstr(state.status));
841 return state.status;
844 return NT_STATUS_OK;
847 struct file_id share_mode_lock_file_id(const struct share_mode_lock *lck)
849 return lck->id;
852 NTSTATUS share_mode_lock_access_private_data(struct share_mode_lock *lck,
853 struct share_mode_data **data)
856 * For now we always have lck->cached_data,
857 * but we may change that in future.
859 SMB_ASSERT(lck->cached_data != NULL);
860 *data = lck->cached_data;
861 return NT_STATUS_OK;
864 /*******************************************************************
865 Get a share_mode_lock, Reference counted to allow nested calls.
866 ********************************************************************/
868 static int share_mode_lock_destructor(struct share_mode_lock *lck);
870 static NTSTATUS get_share_mode_lock_internal(
871 struct file_id id,
872 const char *servicepath,
873 const struct smb_filename *smb_fname,
874 const struct timespec *old_write_time,
875 struct share_mode_lock *lck)
877 NTSTATUS status;
879 *lck = (struct share_mode_lock) {
880 .id = id,
883 if (share_mode_lock_key_refcount == 0) {
884 TDB_DATA key = locking_key(&id);
886 status = g_lock_lock(
887 lock_ctx,
888 key,
889 G_LOCK_WRITE,
890 (struct timeval) { .tv_sec = 3600 });
891 if (!NT_STATUS_IS_OK(status)) {
892 DBG_DEBUG("g_lock_lock failed: %s\n",
893 nt_errstr(status));
894 return status;
896 share_mode_lock_key_id = id;
899 if (!file_id_equal(&share_mode_lock_key_id, &id)) {
900 struct file_id_buf existing;
901 struct file_id_buf requested;
903 DBG_ERR("Can not lock two share modes "
904 "simultaneously: existing %s requested %s\n",
905 file_id_str_buf(share_mode_lock_key_id, &existing),
906 file_id_str_buf(id, &requested));
907 smb_panic(__location__);
908 goto fail;
911 SMB_ASSERT(share_mode_lock_key_refcount < SIZE_MAX);
912 share_mode_lock_key_refcount += 1;
914 if (static_share_mode_data != NULL) {
915 goto done;
918 status = get_static_share_mode_data(
920 servicepath,
921 smb_fname,
922 old_write_time);
923 if (!NT_STATUS_IS_OK(status)) {
924 DBG_DEBUG("get_static_share_mode_data failed: %s\n",
925 nt_errstr(status));
926 share_mode_lock_key_refcount -= 1;
927 goto fail;
929 done:
930 lck->cached_data = static_share_mode_data;
932 if (CHECK_DEBUGLVL(DBGLVL_DEBUG)) {
933 struct file_id_buf returned;
935 DBG_DEBUG("Returning %s (data_cached=%u key_refcount=%zu)\n",
936 file_id_str_buf(id, &returned),
937 static_share_mode_data != NULL,
938 share_mode_lock_key_refcount);
941 return NT_STATUS_OK;
942 fail:
943 if (share_mode_lock_key_refcount == 0) {
944 NTSTATUS ulstatus = g_lock_unlock(lock_ctx, share_mode_lock_key);
945 if (!NT_STATUS_IS_OK(ulstatus)) {
946 DBG_ERR("g_lock_unlock failed: %s\n",
947 nt_errstr(ulstatus));
950 return status;
953 static int share_mode_lock_destructor(struct share_mode_lock *lck)
955 NTSTATUS status;
957 SMB_ASSERT(share_mode_lock_key_refcount > 0);
958 share_mode_lock_key_refcount -= 1;
960 if (share_mode_lock_key_refcount > 0) {
961 return 0;
964 status = share_mode_data_store(static_share_mode_data);
965 if (!NT_STATUS_IS_OK(status)) {
966 DBG_ERR("share_mode_data_store failed: %s\n",
967 nt_errstr(status));
968 smb_panic("Could not store share mode data\n");
971 status = g_lock_unlock(lock_ctx, share_mode_lock_key);
972 if (!NT_STATUS_IS_OK(status)) {
973 DBG_ERR("g_lock_unlock failed: %s\n",
974 nt_errstr(status));
975 smb_panic("Could not unlock share mode\n");
978 if (!static_share_mode_data->not_stored) {
980 * This is worth keeping. Without share modes,
981 * share_mode_data_store above has left nothing in the
982 * database.
984 share_mode_memcache_store(static_share_mode_data);
985 static_share_mode_data = NULL;
988 TALLOC_FREE(static_share_mode_data);
989 return 0;
992 struct share_mode_lock *get_share_mode_lock(
993 TALLOC_CTX *mem_ctx,
994 struct file_id id,
995 const char *servicepath,
996 const struct smb_filename *smb_fname,
997 const struct timespec *old_write_time)
999 struct share_mode_lock *lck = NULL;
1000 NTSTATUS status;
1002 lck = talloc(mem_ctx, struct share_mode_lock);
1003 if (lck == NULL) {
1004 return NULL;
1007 status = get_share_mode_lock_internal(id,
1008 servicepath,
1009 smb_fname,
1010 old_write_time,
1011 lck);
1012 if (!NT_STATUS_IS_OK(status)) {
1013 DBG_GET_SHARE_MODE_LOCK(status,
1014 "get_share_mode_lock_internal() failed - %s\n",
1015 nt_errstr(status));
1016 TALLOC_FREE(lck);
1017 return NULL;
1020 talloc_set_destructor(lck, share_mode_lock_destructor);
1021 return lck;
1024 /*******************************************************************
1025 Fetch a share mode where we know one MUST exist. This call reference
1026 counts it internally to allow for nested lock fetches.
1027 ********************************************************************/
1029 struct share_mode_lock *get_existing_share_mode_lock(TALLOC_CTX *mem_ctx,
1030 const struct file_id id)
1032 return get_share_mode_lock(mem_ctx, id, NULL, NULL, NULL);
1035 static void share_mode_wakeup_waiters_fn(
1036 struct share_mode_lock *lck,
1037 void *private_data)
1039 g_lock_wake_watchers(lock_ctx, share_mode_lock_key);
1042 NTSTATUS share_mode_wakeup_waiters(struct file_id id)
1044 return share_mode_do_locked_vfs_denied(id,
1045 share_mode_wakeup_waiters_fn,
1046 NULL);
1049 struct fsp_update_share_mode_flags_state {
1050 struct files_struct *fsp;
1051 enum ndr_err_code ndr_err;
1052 uint64_t share_mode_epoch;
1053 uint16_t share_mode_flags;
1056 static void fsp_update_share_mode_flags_fn(
1057 struct server_id exclusive,
1058 size_t num_shared,
1059 const struct server_id *shared,
1060 const uint8_t *data,
1061 size_t datalen,
1062 void *private_data)
1064 struct fsp_update_share_mode_flags_state *state = private_data;
1065 struct locking_tdb_data ltdb = { 0 };
1067 if (datalen != 0) {
1068 bool ok = locking_tdb_data_get(&ltdb, data, datalen);
1069 if (!ok) {
1070 DBG_DEBUG("locking_tdb_data_get failed\n");
1071 return;
1075 if (ltdb.share_mode_data_len == 0) {
1076 /* Likely a ctdb tombstone record, ignore it */
1077 return;
1080 if (exclusive.pid != 0) {
1081 struct server_id self =
1082 messaging_server_id(state->fsp->conn->sconn->msg_ctx);
1083 bool is_self = server_id_equal(&self, &exclusive);
1085 if (!is_self) {
1087 * If someone else is holding an exclusive
1088 * lock, pretend there's a read lease
1090 state->share_mode_flags = SHARE_MODE_LEASE_READ;
1091 return;
1095 state->ndr_err = get_share_mode_blob_header(ltdb.share_mode_data_buf,
1096 ltdb.share_mode_data_len,
1097 &state->share_mode_epoch,
1098 &state->share_mode_flags);
1101 static NTSTATUS fsp_update_share_mode_flags(struct files_struct *fsp)
1103 struct fsp_update_share_mode_flags_state state = { .fsp = fsp, };
1104 int seqnum = g_lock_seqnum(lock_ctx);
1105 TDB_DATA key = {0};
1106 NTSTATUS status;
1108 if (seqnum == fsp->share_mode_flags_seqnum) {
1109 return NT_STATUS_OK;
1112 key = locking_key(&fsp->file_id);
1113 status = g_lock_dump(lock_ctx, key,
1114 fsp_update_share_mode_flags_fn,
1115 &state);
1116 if (!NT_STATUS_IS_OK(status)) {
1117 /* no DBG_GET_SHARE_MODE_LOCK here! */
1118 DBG_ERR("g_lock_dump returned %s\n",
1119 nt_errstr(status));
1120 return status;
1123 if (!NDR_ERR_CODE_IS_SUCCESS(state.ndr_err)) {
1124 DBG_ERR("get_share_mode_blob_header returned %s\n",
1125 ndr_errstr(state.ndr_err));
1126 return ndr_map_error2ntstatus(state.ndr_err);
1129 fsp->share_mode_flags_seqnum = seqnum;
1130 fsp->share_mode_flags = state.share_mode_flags;
1132 return NT_STATUS_OK;
1135 bool file_has_read_lease(struct files_struct *fsp)
1137 NTSTATUS status;
1139 status = fsp_update_share_mode_flags(fsp);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 /* Safe default for leases */
1142 return true;
1145 return (fsp->share_mode_flags & SHARE_MODE_LEASE_READ) != 0;
1148 #define share_mode_lock_assert_private_data(__lck) \
1149 _share_mode_lock_assert_private_data(__lck, __func__, __location__)
1150 static struct share_mode_data *_share_mode_lock_assert_private_data(
1151 struct share_mode_lock *lck,
1152 const char *caller_function,
1153 const char *caller_location)
1155 struct share_mode_data *d = NULL;
1156 NTSTATUS status;
1158 status = share_mode_lock_access_private_data(lck, &d);
1159 if (!NT_STATUS_IS_OK(status)) {
1160 struct file_id id = share_mode_lock_file_id(lck);
1161 struct file_id_buf id_buf;
1162 /* Any error recovery possible here ? */
1163 D_ERR("%s:%s(): share_mode_lock_access_private_data() "
1164 "failed for id=%s - %s\n",
1165 caller_location, caller_function,
1166 file_id_str_buf(id, &id_buf),
1167 nt_errstr(status));
1168 smb_panic(caller_location);
1169 return NULL;
1172 return d;
1175 NTTIME share_mode_changed_write_time(struct share_mode_lock *lck)
1177 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1178 return d->changed_write_time;
1181 void share_mode_set_changed_write_time(struct share_mode_lock *lck, struct timespec write_time)
1183 struct file_id fileid = share_mode_lock_file_id(lck);
1184 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1185 struct file_id_buf ftmp;
1186 struct timeval_buf tbuf;
1187 NTTIME nt = full_timespec_to_nt_time(&write_time);
1189 DBG_INFO("%s id=%s\n",
1190 timespec_string_buf(&write_time, true, &tbuf),
1191 file_id_str_buf(fileid, &ftmp));
1193 if (d->changed_write_time != nt) {
1194 d->modified = true;
1195 d->changed_write_time = nt;
1199 void share_mode_set_old_write_time(struct share_mode_lock *lck, struct timespec write_time)
1201 struct file_id fileid = share_mode_lock_file_id(lck);
1202 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1203 struct file_id_buf ftmp;
1204 struct timeval_buf tbuf;
1205 NTTIME nt = full_timespec_to_nt_time(&write_time);
1207 DBG_INFO("%s id=%s\n",
1208 timespec_string_buf(&write_time, true, &tbuf),
1209 file_id_str_buf(fileid, &ftmp));
1211 if (d->changed_write_time != nt) {
1212 d->modified = true;
1213 d->old_write_time = nt;
1217 const char *share_mode_servicepath(struct share_mode_lock *lck)
1219 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1220 return d->servicepath;
1223 char *share_mode_filename(TALLOC_CTX *mem_ctx, struct share_mode_lock *lck)
1225 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1226 bool has_stream = (d->stream_name != NULL);
1227 char *fname = NULL;
1229 fname = talloc_asprintf(
1230 mem_ctx,
1231 "%s%s%s",
1232 d->base_name,
1233 has_stream ? ":" : "",
1234 has_stream ? d->stream_name : "");
1235 return fname;
1238 char *share_mode_data_dump(
1239 TALLOC_CTX *mem_ctx, struct share_mode_lock *lck)
1241 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1242 struct ndr_print *p = talloc(mem_ctx, struct ndr_print);
1243 char *ret = NULL;
1245 if (p == NULL) {
1246 return NULL;
1249 *p = (struct ndr_print) {
1250 .print = ndr_print_string_helper,
1251 .depth = 1,
1252 .private_data = talloc_strdup(mem_ctx, ""),
1255 if (p->private_data == NULL) {
1256 TALLOC_FREE(p);
1257 return NULL;
1260 ndr_print_share_mode_data(p, "SHARE_MODE_DATA", d);
1262 ret = p->private_data;
1264 TALLOC_FREE(p);
1266 return ret;
1269 void share_mode_flags_get(
1270 struct share_mode_lock *lck,
1271 uint32_t *access_mask,
1272 uint32_t *share_mode,
1273 uint32_t *lease_type)
1275 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1276 uint16_t flags = d->flags;
1278 if (access_mask != NULL) {
1279 *access_mask =
1280 ((flags & SHARE_MODE_ACCESS_READ) ?
1281 FILE_READ_DATA : 0) |
1282 ((flags & SHARE_MODE_ACCESS_WRITE) ?
1283 FILE_WRITE_DATA : 0) |
1284 ((flags & SHARE_MODE_ACCESS_DELETE) ?
1285 DELETE_ACCESS : 0);
1287 if (share_mode != NULL) {
1288 *share_mode =
1289 ((flags & SHARE_MODE_SHARE_READ) ?
1290 FILE_SHARE_READ : 0) |
1291 ((flags & SHARE_MODE_SHARE_WRITE) ?
1292 FILE_SHARE_WRITE : 0) |
1293 ((flags & SHARE_MODE_SHARE_DELETE) ?
1294 FILE_SHARE_DELETE : 0);
1296 if (lease_type != NULL) {
1297 *lease_type =
1298 ((flags & SHARE_MODE_LEASE_READ) ?
1299 SMB2_LEASE_READ : 0) |
1300 ((flags & SHARE_MODE_LEASE_WRITE) ?
1301 SMB2_LEASE_WRITE : 0) |
1302 ((flags & SHARE_MODE_LEASE_HANDLE) ?
1303 SMB2_LEASE_HANDLE : 0);
1307 void share_mode_flags_set(
1308 struct share_mode_lock *lck,
1309 uint32_t access_mask,
1310 uint32_t share_mode,
1311 uint32_t lease_type,
1312 bool *modified)
1314 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1315 uint16_t flags = 0;
1317 flags |= (access_mask & (FILE_READ_DATA | FILE_EXECUTE)) ?
1318 SHARE_MODE_ACCESS_READ : 0;
1319 flags |= (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
1320 SHARE_MODE_ACCESS_WRITE : 0;
1321 flags |= (access_mask & (DELETE_ACCESS)) ?
1322 SHARE_MODE_ACCESS_DELETE : 0;
1324 flags |= (share_mode & FILE_SHARE_READ) ?
1325 SHARE_MODE_SHARE_READ : 0;
1326 flags |= (share_mode & FILE_SHARE_WRITE) ?
1327 SHARE_MODE_SHARE_WRITE : 0;
1328 flags |= (share_mode & FILE_SHARE_DELETE) ?
1329 SHARE_MODE_SHARE_DELETE : 0;
1331 flags |= (lease_type & SMB2_LEASE_READ) ?
1332 SHARE_MODE_LEASE_READ : 0;
1333 flags |= (lease_type & SMB2_LEASE_WRITE) ?
1334 SHARE_MODE_LEASE_WRITE : 0;
1335 flags |= (lease_type & SMB2_LEASE_HANDLE) ?
1336 SHARE_MODE_LEASE_HANDLE : 0;
1338 if (d->flags == flags) {
1339 return;
1342 if (modified != NULL) {
1343 *modified = true;
1345 d->flags = flags;
1346 d->modified = true;
1349 struct share_mode_watch_state {
1350 bool blockerdead;
1351 struct server_id blocker;
1354 static void share_mode_watch_done(struct tevent_req *subreq);
1356 struct tevent_req *share_mode_watch_send(
1357 TALLOC_CTX *mem_ctx,
1358 struct tevent_context *ev,
1359 struct share_mode_lock *lck,
1360 struct server_id blocker)
1362 struct file_id id = share_mode_lock_file_id(lck);
1363 TDB_DATA key = locking_key(&id);
1364 struct tevent_req *req = NULL, *subreq = NULL;
1365 struct share_mode_watch_state *state = NULL;
1367 req = tevent_req_create(
1368 mem_ctx, &state, struct share_mode_watch_state);
1369 if (req == NULL) {
1370 return NULL;
1373 subreq = g_lock_watch_data_send(state, ev, lock_ctx, key, blocker);
1374 if (tevent_req_nomem(subreq, req)) {
1375 return tevent_req_post(req, ev);
1377 tevent_req_set_callback(subreq, share_mode_watch_done, req);
1378 return req;
1381 static void share_mode_watch_done(struct tevent_req *subreq)
1383 struct tevent_req *req = tevent_req_callback_data(
1384 subreq, struct tevent_req);
1385 struct share_mode_watch_state *state = tevent_req_data(
1386 req, struct share_mode_watch_state);
1387 NTSTATUS status;
1389 status = g_lock_watch_data_recv(
1390 subreq, &state->blockerdead, &state->blocker);
1391 if (tevent_req_nterror(req, status)) {
1392 return;
1394 tevent_req_done(req);
1397 NTSTATUS share_mode_watch_recv(
1398 struct tevent_req *req, bool *blockerdead, struct server_id *blocker)
1400 struct share_mode_watch_state *state = tevent_req_data(
1401 req, struct share_mode_watch_state);
1402 NTSTATUS status;
1404 if (tevent_req_is_nterror(req, &status)) {
1405 return status;
1407 if (blockerdead != NULL) {
1408 *blockerdead = state->blockerdead;
1410 if (blocker != NULL) {
1411 *blocker = state->blocker;
1413 return NT_STATUS_OK;
1416 struct fetch_share_mode_unlocked_state {
1417 TALLOC_CTX *mem_ctx;
1418 struct file_id id;
1419 struct share_mode_lock *lck;
1422 static void fetch_share_mode_unlocked_parser(
1423 struct server_id exclusive,
1424 size_t num_shared,
1425 const struct server_id *shared,
1426 const uint8_t *data,
1427 size_t datalen,
1428 void *private_data)
1430 struct fetch_share_mode_unlocked_state *state = private_data;
1431 struct locking_tdb_data ltdb = { 0 };
1433 if (datalen != 0) {
1434 bool ok = locking_tdb_data_get(&ltdb, data, datalen);
1435 if (!ok) {
1436 DBG_DEBUG("locking_tdb_data_get failed\n");
1437 return;
1441 if (ltdb.share_mode_data_len == 0) {
1442 /* Likely a ctdb tombstone record, ignore it */
1443 return;
1446 state->lck = talloc(state->mem_ctx, struct share_mode_lock);
1447 if (state->lck == NULL) {
1448 DEBUG(0, ("talloc failed\n"));
1449 return;
1451 state->lck->id = state->id;
1453 state->lck->cached_data = parse_share_modes(
1454 state->lck,
1455 state->id,
1456 ltdb.share_mode_data_buf,
1457 ltdb.share_mode_data_len);
1458 if (state->lck->cached_data == NULL) {
1459 DBG_DEBUG("parse_share_modes failed\n");
1460 TALLOC_FREE(state->lck);
1464 /*******************************************************************
1465 Get a share_mode_lock without locking the database or reference
1466 counting. Used by smbstatus to display existing share modes.
1467 ********************************************************************/
1469 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
1470 struct file_id id)
1472 struct fetch_share_mode_unlocked_state state = {
1473 .mem_ctx = mem_ctx,
1474 .id = id,
1476 TDB_DATA key = locking_key(&id);
1477 NTSTATUS status;
1479 status = g_lock_dump(
1480 lock_ctx, key, fetch_share_mode_unlocked_parser, &state);
1481 if (!NT_STATUS_IS_OK(status)) {
1482 DBG_DEBUG("g_lock_dump failed: %s\n", nt_errstr(status));
1483 return NULL;
1485 return state.lck;
1488 struct fetch_share_mode_state {
1489 struct file_id id;
1490 struct share_mode_lock *lck;
1491 NTSTATUS status;
1494 static void fetch_share_mode_fn(
1495 struct server_id exclusive,
1496 size_t num_shared,
1497 const struct server_id *shared,
1498 const uint8_t *data,
1499 size_t datalen,
1500 void *private_data);
1501 static void fetch_share_mode_done(struct tevent_req *subreq);
1504 * @brief Get a share_mode_lock without locking or refcounting
1506 * This can be used in a clustered Samba environment where the async dbwrap
1507 * request is sent over a socket to the local ctdbd. If the send queue is full
1508 * and the caller was issuing multiple async dbwrap requests in a loop, the
1509 * caller knows it's probably time to stop sending requests for now and try
1510 * again later.
1512 * @param[in] mem_ctx The talloc memory context to use.
1514 * @param[in] ev The event context to work on.
1516 * @param[in] id The file id for the locking.tdb key
1518 * @param[out] queued This boolean out parameter tells the caller whether the
1519 * async request is blocked in a full send queue:
1521 * false := request is dispatched
1523 * true := send queue is full, request waiting to be
1524 * dispatched
1526 * @return The new async request, NULL on error.
1528 struct tevent_req *fetch_share_mode_send(TALLOC_CTX *mem_ctx,
1529 struct tevent_context *ev,
1530 struct file_id id,
1531 bool *queued)
1533 struct tevent_req *req = NULL, *subreq = NULL;
1534 struct fetch_share_mode_state *state = NULL;
1536 *queued = false;
1538 req = tevent_req_create(mem_ctx, &state,
1539 struct fetch_share_mode_state);
1540 if (req == NULL) {
1541 return NULL;
1543 state->id = id;
1545 subreq = g_lock_dump_send(
1546 state,
1548 lock_ctx,
1549 locking_key(&id),
1550 fetch_share_mode_fn,
1551 state);
1552 if (tevent_req_nomem(subreq, req)) {
1553 return tevent_req_post(req, ev);
1555 tevent_req_set_callback(subreq, fetch_share_mode_done, req);
1556 return req;
1559 static void fetch_share_mode_fn(
1560 struct server_id exclusive,
1561 size_t num_shared,
1562 const struct server_id *shared,
1563 const uint8_t *data,
1564 size_t datalen,
1565 void *private_data)
1567 struct fetch_share_mode_state *state = talloc_get_type_abort(
1568 private_data, struct fetch_share_mode_state);
1569 struct locking_tdb_data ltdb = { 0 };
1571 if (datalen != 0) {
1572 bool ok = locking_tdb_data_get(&ltdb, data, datalen);
1573 if (!ok) {
1574 DBG_DEBUG("locking_tdb_data_get failed\n");
1575 return;
1579 if (ltdb.share_mode_data_len == 0) {
1580 /* Likely a ctdb tombstone record, ignore it */
1581 return;
1584 state->lck = talloc(state, struct share_mode_lock);
1585 if (state->lck == NULL) {
1586 DBG_WARNING("talloc failed\n");
1587 state->status = NT_STATUS_NO_MEMORY;
1588 return;
1590 state->lck->id = state->id,
1592 state->lck->cached_data = parse_share_modes(
1593 state->lck,
1594 state->id,
1595 ltdb.share_mode_data_buf,
1596 ltdb.share_mode_data_len);
1597 if (state->lck->cached_data == NULL) {
1598 DBG_DEBUG("parse_share_modes failed\n");
1599 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1600 TALLOC_FREE(state->lck);
1601 return;
1605 static void fetch_share_mode_done(struct tevent_req *subreq)
1607 struct tevent_req *req = tevent_req_callback_data(
1608 subreq, struct tevent_req);
1609 struct fetch_share_mode_state *state = tevent_req_data(
1610 req, struct fetch_share_mode_state);
1611 NTSTATUS status;
1613 status = g_lock_dump_recv(subreq);
1614 TALLOC_FREE(subreq);
1615 if (tevent_req_nterror(req, status)) {
1616 return;
1618 if (tevent_req_nterror(req, state->status)) {
1619 return;
1621 tevent_req_done(req);
1624 NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
1625 TALLOC_CTX *mem_ctx,
1626 struct share_mode_lock **_lck)
1628 struct fetch_share_mode_state *state = tevent_req_data(
1629 req, struct fetch_share_mode_state);
1630 struct share_mode_lock *lck = NULL;
1632 NTSTATUS status;
1634 if (tevent_req_is_nterror(req, &status)) {
1635 tevent_req_received(req);
1636 return status;
1639 if (state->lck == NULL) {
1640 tevent_req_received(req);
1641 return NT_STATUS_NOT_FOUND;
1644 lck = talloc_move(mem_ctx, &state->lck);
1646 if (DEBUGLEVEL >= 10) {
1647 DBG_DEBUG("share_mode_data:\n");
1648 NDR_PRINT_DEBUG(share_mode_data, lck->cached_data);
1651 *_lck = lck;
1652 tevent_req_received(req);
1653 return NT_STATUS_OK;
1656 struct share_mode_forall_state {
1657 TDB_DATA key;
1658 int (*fn)(struct file_id fid,
1659 const struct share_mode_data *data,
1660 void *private_data);
1661 void *private_data;
1664 static void share_mode_forall_dump_fn(
1665 struct server_id exclusive,
1666 size_t num_shared,
1667 const struct server_id *shared,
1668 const uint8_t *data,
1669 size_t datalen,
1670 void *private_data)
1672 struct share_mode_forall_state *state = private_data;
1673 struct file_id fid;
1674 struct locking_tdb_data ltdb = { 0 };
1675 bool ok;
1676 struct share_mode_data *d;
1678 if (state->key.dsize != sizeof(fid)) {
1679 DBG_DEBUG("Got invalid key length %zu\n", state->key.dsize);
1680 return;
1682 memcpy(&fid, state->key.dptr, sizeof(fid));
1684 ok = locking_tdb_data_get(&ltdb, data, datalen);
1685 if (!ok) {
1686 DBG_DEBUG("locking_tdb_data_get() failed\n");
1687 return;
1690 d = parse_share_modes(
1691 talloc_tos(),
1692 fid,
1693 ltdb.share_mode_data_buf,
1694 ltdb.share_mode_data_len);
1695 if (d == NULL) {
1696 DBG_DEBUG("parse_share_modes() failed\n");
1697 return;
1700 state->fn(fid, d, state->private_data);
1701 TALLOC_FREE(d);
1704 static int share_mode_forall_fn(TDB_DATA key, void *private_data)
1706 struct share_mode_forall_state *state = private_data;
1707 NTSTATUS status;
1709 state->key = key;
1711 status = g_lock_dump(
1712 lock_ctx, key, share_mode_forall_dump_fn, private_data);
1713 if (!NT_STATUS_IS_OK(status)) {
1714 DBG_GET_SHARE_MODE_LOCK(status,
1715 "g_lock_dump failed: %s\n",
1716 nt_errstr(status));
1718 return 0;
1721 int share_mode_forall(int (*fn)(struct file_id fid,
1722 const struct share_mode_data *data,
1723 void *private_data),
1724 void *private_data)
1726 struct share_mode_forall_state state = {
1727 .fn = fn,
1728 .private_data = private_data
1730 int ret;
1732 if (lock_ctx == NULL) {
1733 return 0;
1736 ret = g_lock_locks(
1737 lock_ctx, share_mode_forall_fn, &state);
1738 if (ret < 0) {
1739 DBG_ERR("g_lock_locks failed\n");
1741 return ret;
1744 struct share_entry_forall_state {
1745 struct file_id fid;
1746 const struct share_mode_data *data;
1747 int (*fn)(struct file_id fid,
1748 const struct share_mode_data *data,
1749 const struct share_mode_entry *entry,
1750 void *private_data);
1751 void *private_data;
1752 int ret;
1755 static bool share_entry_traverse_walker(
1756 struct share_mode_entry *e,
1757 bool *modified,
1758 void *private_data)
1760 struct share_entry_forall_state *state = private_data;
1762 state->ret = state->fn(
1763 state->fid, state->data, e, state->private_data);
1764 return (state->ret != 0);
1767 static int share_entry_traverse_fn(struct file_id fid,
1768 const struct share_mode_data *data,
1769 void *private_data)
1771 struct share_entry_forall_state *state = private_data;
1772 struct share_mode_lock lck = {
1773 .id = fid,
1774 .cached_data = discard_const_p(struct share_mode_data, data)
1776 bool ok;
1778 state->fid = fid;
1779 state->data = data;
1781 ok = share_mode_forall_entries(
1782 &lck, share_entry_traverse_walker, state);
1783 if (!ok) {
1784 DBG_ERR("share_mode_forall_entries failed\n");
1785 return false;
1788 return state->ret;
1791 /*******************************************************************
1792 Call the specified function on each entry under management by the
1793 share mode system.
1794 ********************************************************************/
1796 int share_entry_forall(int (*fn)(struct file_id fid,
1797 const struct share_mode_data *data,
1798 const struct share_mode_entry *entry,
1799 void *private_data),
1800 void *private_data)
1802 struct share_entry_forall_state state = {
1803 .fn = fn, .private_data = private_data };
1805 return share_mode_forall(share_entry_traverse_fn, &state);
1808 static int share_mode_entry_cmp(
1809 struct server_id pid1,
1810 uint64_t share_file_id1,
1811 struct server_id pid2,
1812 uint64_t share_file_id2)
1814 int cmp;
1816 cmp = server_id_cmp(&pid1, &pid2);
1817 if (cmp != 0) {
1818 return cmp;
1820 if (share_file_id1 != share_file_id2) {
1821 return (share_file_id1 < share_file_id2) ? -1 : 1;
1823 return 0;
1826 static size_t share_mode_entry_find(
1827 const uint8_t *data,
1828 size_t num_share_modes,
1829 struct server_id pid,
1830 uint64_t share_file_id,
1831 struct share_mode_entry *e,
1832 bool *match)
1834 ssize_t left, right, middle;
1836 *match = false;
1838 if (num_share_modes == 0) {
1839 return 0;
1842 left = 0;
1843 right = (num_share_modes-1);
1845 while (left <= right) {
1846 const uint8_t *middle_ptr = NULL;
1847 int cmp;
1848 bool ok;
1850 middle = left + ((right - left) / 2);
1851 middle_ptr = data + middle * SHARE_MODE_ENTRY_SIZE;
1853 DBG_DEBUG("left=%zu, right=%zu, middle=%zu, middle_ptr=%p\n",
1854 left,
1855 right,
1856 middle,
1857 middle_ptr);
1859 ok = share_mode_entry_get(middle_ptr, e);
1860 if (!ok) {
1861 DBG_DEBUG("share_mode_entry_get failed\n");
1862 return 0;
1865 cmp = share_mode_entry_cmp(
1866 e->pid, e->share_file_id, pid, share_file_id);
1867 if (cmp == 0) {
1868 *match = true;
1869 return middle;
1872 if (cmp < 0) {
1873 right = middle-1;
1874 } else {
1875 left = middle+1;
1879 return left;
1882 bool set_share_mode(struct share_mode_lock *lck,
1883 struct files_struct *fsp,
1884 uid_t uid,
1885 uint64_t mid,
1886 uint16_t op_type,
1887 const struct smb2_lease_key *lease_key,
1888 uint32_t share_access,
1889 uint32_t access_mask)
1891 struct share_mode_data *d = share_mode_lock_assert_private_data(lck);
1892 TDB_DATA key = locking_key(&d->id);
1893 struct server_id my_pid = messaging_server_id(
1894 fsp->conn->sconn->msg_ctx);
1895 struct locking_tdb_data *ltdb = NULL;
1896 size_t idx;
1897 struct share_mode_entry e = { .pid.pid = 0 };
1898 struct share_mode_entry_buf e_buf;
1899 NTSTATUS status;
1900 bool ok, found;
1902 TDB_DATA dbufs[3];
1903 size_t num_dbufs = 0;
1905 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
1906 if (!NT_STATUS_IS_OK(status)) {
1907 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
1908 nt_errstr(status));
1909 return false;
1911 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
1913 idx = share_mode_entry_find(
1914 ltdb->share_entries,
1915 ltdb->num_share_entries,
1916 my_pid,
1917 fh_get_gen_id(fsp->fh),
1919 &found);
1920 if (found) {
1921 DBG_WARNING("Found duplicate share mode\n");
1922 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1923 goto done;
1926 e = (struct share_mode_entry) {
1927 .pid = my_pid,
1928 .share_access = share_access,
1929 .private_options = fh_get_private_options(fsp->fh),
1930 .access_mask = access_mask,
1931 .op_mid = mid,
1932 .op_type = op_type,
1933 .time.tv_sec = fsp->open_time.tv_sec,
1934 .time.tv_usec = fsp->open_time.tv_usec,
1935 .share_file_id = fh_get_gen_id(fsp->fh),
1936 .uid = (uint32_t)uid,
1937 .flags = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
1938 SHARE_MODE_FLAG_POSIX_OPEN : 0,
1939 .name_hash = fsp->name_hash,
1942 if (op_type == LEASE_OPLOCK) {
1943 const struct GUID *client_guid = fsp_client_guid(fsp);
1944 e.client_guid = *client_guid;
1945 e.lease_key = *lease_key;
1948 ok = share_mode_entry_put(&e, &e_buf);
1949 if (!ok) {
1950 DBG_DEBUG("share_mode_entry_put failed\n");
1951 status = NT_STATUS_INTERNAL_ERROR;
1952 goto done;
1955 DBG_DEBUG("idx=%zu, found=%d\n", idx, (int)found);
1957 if (idx > 0) {
1958 dbufs[num_dbufs] = (TDB_DATA) {
1959 .dptr = discard_const_p(uint8_t, ltdb->share_entries),
1960 .dsize = idx * SHARE_MODE_ENTRY_SIZE,
1962 num_dbufs += 1;
1965 dbufs[num_dbufs] = (TDB_DATA) {
1966 .dptr = e_buf.buf, .dsize = SHARE_MODE_ENTRY_SIZE,
1968 num_dbufs += 1;
1970 if (idx < ltdb->num_share_entries) {
1971 size_t num_after_idx = (ltdb->num_share_entries-idx);
1972 dbufs[num_dbufs] = (TDB_DATA) {
1973 .dptr = discard_const_p(uint8_t, ltdb->share_entries) +
1974 idx * SHARE_MODE_ENTRY_SIZE,
1975 .dsize = num_after_idx * SHARE_MODE_ENTRY_SIZE,
1977 num_dbufs += 1;
1981 size_t i;
1982 for (i=0; i<num_dbufs; i++) {
1983 DBG_DEBUG("dbufs[%zu]=(%p, %zu)\n",
1985 dbufs[i].dptr,
1986 dbufs[i].dsize);
1990 if (num_dbufs == 1) {
1992 * Storing a fresh record with just one share entry
1994 d->modified = true;
1998 * If there was any existing data in
1999 * ltdb->share_entries, it's now been
2000 * moved and we've split it into:
2002 * num_dbufs = 3
2003 * dbufs[0] -> old sorted data less than new_entry
2004 * dbufs[1] -> new_share_mode_entry
2005 * dbufs[2] -> old sorted_data greater than new entry.
2007 * So the old data inside ltdb->share_entries is
2008 * no longer valid.
2010 * If we're storing a brand new entry the
2011 * dbufs look like:
2013 * num_dbufs = 1
2014 * dbufs[0] -> new_share_mode_entry
2016 * Either way we must set ltdb->share_entries = NULL
2017 * and ltdb->num_share_entries = 0 so that
2018 * locking_tdb_data_store() doesn't use it to
2019 * store any data. It's no longer there.
2022 ltdb->share_entries = NULL;
2023 ltdb->num_share_entries = 0;
2025 status = share_mode_data_ltdb_store(d, key, ltdb, dbufs, num_dbufs);
2026 if (!NT_STATUS_IS_OK(status)) {
2027 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2028 nt_errstr(status));
2030 done:
2031 TALLOC_FREE(ltdb);
2032 return NT_STATUS_IS_OK(status);
2035 static bool share_mode_for_one_entry(
2036 bool (*fn)(struct share_mode_entry *e,
2037 bool *modified,
2038 void *private_data),
2039 void *private_data,
2040 size_t *i,
2041 uint8_t *data,
2042 size_t *num_share_modes,
2043 bool *writeback)
2045 DATA_BLOB blob = {
2046 .data = data + (*i) * SHARE_MODE_ENTRY_SIZE,
2047 .length = SHARE_MODE_ENTRY_SIZE,
2049 struct share_mode_entry e = {.pid.pid=0};
2050 enum ndr_err_code ndr_err = NDR_ERR_SUCCESS;
2051 bool modified = false;
2052 bool stop = false;
2053 struct server_id e_pid;
2054 uint64_t e_share_file_id;
2056 ndr_err = ndr_pull_struct_blob_all_noalloc(
2057 &blob,
2059 (ndr_pull_flags_fn_t)ndr_pull_share_mode_entry);
2060 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2061 DBG_WARNING("ndr_pull_share_mode_entry failed\n");
2062 *i += 1;
2063 return false;
2065 if (DEBUGLEVEL >= 10) {
2066 DBG_DEBUG("entry[%zu]:\n", *i);
2067 NDR_PRINT_DEBUG(share_mode_entry, &e);
2070 e_pid = e.pid;
2071 e_share_file_id = e.share_file_id;
2073 stop = fn(&e, &modified, private_data);
2075 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2077 (int)modified,
2078 (int)e.stale);
2080 if (e.stale) {
2081 if (DEBUGLEVEL>=10) {
2082 DBG_DEBUG("share_mode_entry:\n");
2083 NDR_PRINT_DEBUG(share_mode_entry, &e);
2086 if (*i < *num_share_modes) {
2087 memmove(blob.data,
2088 blob.data + SHARE_MODE_ENTRY_SIZE,
2089 (*num_share_modes - *i - 1) *
2090 SHARE_MODE_ENTRY_SIZE);
2092 *num_share_modes -= 1;
2093 *writeback = true;
2094 return stop;
2097 if (modified) {
2098 if (DEBUGLEVEL>=10) {
2099 DBG_DEBUG("share_mode_entry:\n");
2100 NDR_PRINT_DEBUG(share_mode_entry, &e);
2104 * Make sure sorting order is kept intact
2106 SMB_ASSERT(server_id_equal(&e_pid, &e.pid));
2107 SMB_ASSERT(e_share_file_id == e.share_file_id);
2109 ndr_err = ndr_push_struct_into_fixed_blob(
2110 &blob,
2112 (ndr_push_flags_fn_t)
2113 ndr_push_share_mode_entry);
2114 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2115 DBG_WARNING("ndr_push_share_mode_entry "
2116 "failed: %s\n",
2117 ndr_errstr(ndr_err));
2119 * Not much we can do, just ignore it
2122 *i += 1;
2123 *writeback = true;
2124 return stop;
2127 if (stop) {
2128 return true;
2131 *i += 1;
2132 return false;
2135 bool share_mode_forall_entries(
2136 struct share_mode_lock *lck,
2137 bool (*fn)(struct share_mode_entry *e,
2138 bool *modified,
2139 void *private_data),
2140 void *private_data)
2142 struct file_id id = share_mode_lock_file_id(lck);
2143 struct share_mode_data *d = NULL;
2144 TDB_DATA key = locking_key(&id);
2145 struct locking_tdb_data *ltdb = NULL;
2146 uint8_t *share_entries = NULL;
2147 size_t num_share_entries;
2148 bool writeback = false;
2149 NTSTATUS status;
2150 bool stop = false;
2151 size_t i;
2153 status = share_mode_lock_access_private_data(lck, &d);
2154 if (!NT_STATUS_IS_OK(status)) {
2155 struct file_id_buf id_buf;
2156 /* Any error recovery possible here ? */
2157 DBG_ERR("share_mode_lock_access_private_data() failed for "
2158 "%s - %s\n",
2159 file_id_str_buf(id, &id_buf),
2160 nt_errstr(status));
2161 return false;
2164 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2165 if (!NT_STATUS_IS_OK(status)) {
2166 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2167 nt_errstr(status));
2168 return false;
2170 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2172 num_share_entries = ltdb->num_share_entries;
2173 share_entries = discard_const_p(uint8_t, ltdb->share_entries);
2175 i = 0;
2176 while (i<num_share_entries) {
2177 stop = share_mode_for_one_entry(
2179 private_data,
2181 share_entries,
2182 &num_share_entries,
2183 &writeback);
2184 if (stop) {
2185 break;
2189 DBG_DEBUG("num_share_entries=%zu, writeback=%d\n",
2190 num_share_entries,
2191 (int)writeback);
2193 if (!writeback) {
2194 TALLOC_FREE(ltdb);
2195 return true;
2198 if ((ltdb->num_share_entries != 0 ) && (num_share_entries == 0)) {
2200 * This routine wiped all share entries, let
2201 * share_mode_data_store() delete the record
2203 d->modified = true;
2206 ltdb->num_share_entries = num_share_entries;
2207 ltdb->share_entries = share_entries;
2209 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
2210 TALLOC_FREE(ltdb);
2211 if (!NT_STATUS_IS_OK(status)) {
2212 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2213 nt_errstr(status));
2214 return false;
2217 return true;
2220 struct share_mode_count_entries_state {
2221 size_t num_share_modes;
2222 NTSTATUS status;
2225 static void share_mode_count_entries_fn(
2226 struct server_id exclusive,
2227 size_t num_shared,
2228 const struct server_id *shared,
2229 const uint8_t *data,
2230 size_t datalen,
2231 void *private_data)
2233 struct share_mode_count_entries_state *state = private_data;
2234 struct locking_tdb_data ltdb = { 0 };
2235 bool ok;
2237 ok = locking_tdb_data_get(&ltdb, data, datalen);
2238 if (!ok) {
2239 DBG_WARNING("locking_tdb_data_get failed for %zu\n", datalen);
2240 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
2241 return;
2243 state->num_share_modes = ltdb.num_share_entries;
2244 state->status = NT_STATUS_OK;
2247 NTSTATUS share_mode_count_entries(struct file_id fid, size_t *num_share_modes)
2249 struct share_mode_count_entries_state state = {
2250 .status = NT_STATUS_NOT_FOUND,
2252 NTSTATUS status;
2254 status = g_lock_dump(
2255 lock_ctx,
2256 locking_key(&fid),
2257 share_mode_count_entries_fn,
2258 &state);
2259 if (!NT_STATUS_IS_OK(status)) {
2260 DBG_DEBUG("g_lock_dump failed: %s\n",
2261 nt_errstr(status));
2262 return status;
2264 if (!NT_STATUS_IS_OK(state.status)) {
2265 DBG_DEBUG("share_mode_count_entries_fn failed: %s\n",
2266 nt_errstr(state.status));
2267 return state.status;
2270 *num_share_modes = state.num_share_modes;
2271 return NT_STATUS_OK;
2274 static bool share_mode_entry_do(
2275 struct share_mode_data *d,
2276 struct server_id pid,
2277 uint64_t share_file_id,
2278 void (*fn)(struct share_mode_entry *e,
2279 size_t num_share_modes,
2280 bool *modified,
2281 void *private_data),
2282 void *private_data)
2284 TDB_DATA key = locking_key(&d->id);
2285 struct locking_tdb_data *ltdb = NULL;
2286 size_t idx;
2287 bool found = false;
2288 bool modified = false;
2289 struct share_mode_entry e;
2290 uint8_t *e_ptr = NULL;
2291 NTSTATUS status;
2292 bool ret = false;
2294 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2295 if (!NT_STATUS_IS_OK(status)) {
2296 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2297 nt_errstr(status));
2298 return false;
2300 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2302 idx = share_mode_entry_find(
2303 ltdb->share_entries,
2304 ltdb->num_share_entries,
2305 pid,
2306 share_file_id,
2308 &found);
2309 if (!found) {
2310 DBG_WARNING("Did not find share mode entry for %"PRIu64"\n",
2311 share_file_id);
2312 goto done;
2315 if (DEBUGLEVEL>=10) {
2316 DBG_DEBUG("entry[%zu]:\n", idx);
2317 NDR_PRINT_DEBUG(share_mode_entry, &e);
2320 fn(&e, ltdb->num_share_entries, &modified, private_data);
2322 DBG_DEBUG("entry[%zu]: modified=%d, e.stale=%d\n",
2323 idx,
2324 (int)modified,
2325 (int)e.stale);
2327 if (!e.stale && !modified) {
2328 ret = true;
2329 goto done;
2332 e_ptr = discard_const_p(uint8_t, ltdb->share_entries) +
2333 idx * SHARE_MODE_ENTRY_SIZE;
2335 if (e.stale) {
2337 * Move the rest down one entry
2339 size_t behind = ltdb->num_share_entries - idx - 1;
2340 if (behind != 0) {
2341 memmove(e_ptr,
2342 e_ptr + SHARE_MODE_ENTRY_SIZE,
2343 behind * SHARE_MODE_ENTRY_SIZE);
2345 ltdb->num_share_entries -= 1;
2347 if (ltdb->num_share_entries == 0) {
2349 * Tell share_mode_lock_destructor() to delete
2350 * the whole record
2352 d->modified = true;
2355 if (DEBUGLEVEL>=10) {
2356 DBG_DEBUG("share_mode_entry:\n");
2357 NDR_PRINT_DEBUG(share_mode_entry, &e);
2359 } else {
2360 struct share_mode_entry_buf buf;
2361 bool ok;
2363 if (ltdb->num_share_entries != 1) {
2365 * Make sure the sorting order stays intact
2367 SMB_ASSERT(server_id_equal(&e.pid, &pid));
2368 SMB_ASSERT(e.share_file_id == share_file_id);
2371 ok = share_mode_entry_put(&e, &buf);
2372 if (!ok) {
2373 DBG_DEBUG("share_mode_entry_put failed\n");
2374 goto done;
2376 memcpy(e_ptr, buf.buf, SHARE_MODE_ENTRY_SIZE);
2379 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
2380 if (!NT_STATUS_IS_OK(status)) {
2381 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2382 nt_errstr(status));
2383 goto done;
2386 ret = true;
2387 done:
2388 TALLOC_FREE(ltdb);
2389 return ret;
2392 struct del_share_mode_state {
2393 bool ok;
2396 static void del_share_mode_fn(
2397 struct share_mode_entry *e,
2398 size_t num_share_modes,
2399 bool *modified,
2400 void *private_data)
2402 struct del_share_mode_state *state = private_data;
2403 e->stale = true;
2404 state->ok = true;
2407 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
2409 struct del_share_mode_state state = { .ok = false };
2410 struct share_mode_data *d = NULL;
2411 NTSTATUS status;
2412 bool ok;
2414 status = share_mode_lock_access_private_data(lck, &d);
2415 if (!NT_STATUS_IS_OK(status)) {
2416 struct file_id id = share_mode_lock_file_id(lck);
2417 struct file_id_buf id_buf;
2418 /* Any error recovery possible here ? */
2419 DBG_ERR("share_mode_lock_access_private_data() failed for "
2420 "%s %s - %s\n",
2421 file_id_str_buf(id, &id_buf),
2422 fsp_str_dbg(fsp),
2423 nt_errstr(status));
2424 return false;
2427 ok = share_mode_entry_do(
2429 messaging_server_id(fsp->conn->sconn->msg_ctx),
2430 fh_get_gen_id(fsp->fh),
2431 del_share_mode_fn,
2432 &state);
2433 if (!ok) {
2434 DBG_DEBUG("share_mode_entry_do failed\n");
2435 return false;
2437 if (!state.ok) {
2438 DBG_DEBUG("del_share_mode_fn failed\n");
2439 return false;
2441 return true;
2444 struct remove_share_oplock_state {
2445 bool ok;
2448 static void remove_share_oplock_fn(
2449 struct share_mode_entry *e,
2450 size_t num_share_modes,
2451 bool *modified,
2452 void *private_data)
2454 struct remove_share_oplock_state *state = private_data;
2456 e->op_type = NO_OPLOCK;
2457 *modified = true;
2458 state->ok = true;
2461 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2463 struct remove_share_oplock_state state = { .ok = false };
2464 struct share_mode_data *d = NULL;
2465 NTSTATUS status;
2466 bool ok;
2468 status = share_mode_lock_access_private_data(lck, &d);
2469 if (!NT_STATUS_IS_OK(status)) {
2470 struct file_id id = share_mode_lock_file_id(lck);
2471 struct file_id_buf id_buf;
2472 /* Any error recovery possible here ? */
2473 DBG_ERR("share_mode_lock_access_private_data() failed for "
2474 "%s %s - %s\n",
2475 file_id_str_buf(id, &id_buf),
2476 fsp_str_dbg(fsp),
2477 nt_errstr(status));
2478 return false;
2481 ok = share_mode_entry_do(
2483 messaging_server_id(fsp->conn->sconn->msg_ctx),
2484 fh_get_gen_id(fsp->fh),
2485 remove_share_oplock_fn,
2486 &state);
2487 if (!ok) {
2488 DBG_DEBUG("share_mode_entry_do failed\n");
2489 return false;
2491 if (!state.ok) {
2492 DBG_DEBUG("remove_share_oplock_fn failed\n");
2493 return false;
2496 if (fsp->oplock_type == LEASE_OPLOCK) {
2497 remove_lease_if_stale(
2498 lck,
2499 fsp_client_guid(fsp),
2500 &fsp->lease->lease.lease_key);
2503 share_mode_wakeup_waiters(fsp->file_id);
2505 return true;
2508 struct downgrade_share_oplock_state {
2509 bool ok;
2512 static void downgrade_share_oplock_fn(
2513 struct share_mode_entry *e,
2514 size_t num_share_modes,
2515 bool *modified,
2516 void *private_data)
2518 struct downgrade_share_oplock_state *state = private_data;
2520 e->op_type = LEVEL_II_OPLOCK;
2521 *modified = true;
2522 state->ok = true;
2525 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
2527 struct downgrade_share_oplock_state state = { .ok = false };
2528 struct share_mode_data *d = NULL;
2529 NTSTATUS status;
2530 bool ok;
2532 status = share_mode_lock_access_private_data(lck, &d);
2533 if (!NT_STATUS_IS_OK(status)) {
2534 struct file_id id = share_mode_lock_file_id(lck);
2535 struct file_id_buf id_buf;
2536 /* Any error recovery possible here ? */
2537 DBG_ERR("share_mode_lock_access_private_data() failed for "
2538 "%s %s - %s\n",
2539 file_id_str_buf(id, &id_buf),
2540 fsp_str_dbg(fsp),
2541 nt_errstr(status));
2542 return false;
2545 ok = share_mode_entry_do(
2547 messaging_server_id(fsp->conn->sconn->msg_ctx),
2548 fh_get_gen_id(fsp->fh),
2549 downgrade_share_oplock_fn,
2550 &state);
2551 if (!ok) {
2552 DBG_DEBUG("share_mode_entry_do failed\n");
2553 return false;
2555 if (!state.ok) {
2556 DBG_DEBUG("downgrade_share_oplock_fn failed\n");
2557 return false;
2560 d->flags |= SHARE_MODE_LEASE_READ;
2561 d->modified = true;
2563 return true;
2566 bool mark_share_mode_disconnected(struct share_mode_lock *lck,
2567 struct files_struct *fsp)
2569 struct server_id disconnected_pid = { .pid = 0 };
2570 bool ok;
2572 if (fsp->op == NULL) {
2573 return false;
2575 if (!fsp->op->global->durable) {
2576 return false;
2579 server_id_set_disconnected(&disconnected_pid);
2581 ok = reset_share_mode_entry(
2582 lck,
2583 messaging_server_id(fsp->conn->sconn->msg_ctx),
2584 fh_get_gen_id(fsp->fh),
2585 disconnected_pid,
2586 UINT64_MAX,
2587 fsp->op->global->open_persistent_id);
2589 return ok;
2592 bool reset_share_mode_entry(
2593 struct share_mode_lock *lck,
2594 struct server_id old_pid,
2595 uint64_t old_share_file_id,
2596 struct server_id new_pid,
2597 uint64_t new_mid,
2598 uint64_t new_share_file_id)
2600 struct file_id id = share_mode_lock_file_id(lck);
2601 struct share_mode_data *d = NULL;
2602 TDB_DATA key = locking_key(&id);
2603 struct locking_tdb_data *ltdb = NULL;
2604 struct share_mode_entry e;
2605 struct share_mode_entry_buf e_buf;
2606 NTSTATUS status;
2607 int cmp;
2608 bool ret = false;
2609 bool ok;
2611 status = share_mode_lock_access_private_data(lck, &d);
2612 if (!NT_STATUS_IS_OK(status)) {
2613 struct file_id_buf id_buf;
2614 /* Any error recovery possible here ? */
2615 DBG_ERR("share_mode_lock_access_private_data() failed for "
2616 "%s - %s\n",
2617 file_id_str_buf(id, &id_buf),
2618 nt_errstr(status));
2619 return false;
2622 status = locking_tdb_data_fetch(key, talloc_tos(), &ltdb);
2623 if (!NT_STATUS_IS_OK(status)) {
2624 DBG_ERR("locking_tdb_data_fetch failed: %s\n",
2625 nt_errstr(status));
2626 return false;
2629 if (ltdb->num_share_entries != 1) {
2630 DBG_DEBUG("num_share_modes=%zu\n", ltdb->num_share_entries);
2631 goto done;
2634 ok = share_mode_entry_get(ltdb->share_entries, &e);
2635 if (!ok) {
2636 DBG_WARNING("share_mode_entry_get failed\n");
2637 goto done;
2640 cmp = share_mode_entry_cmp(
2641 old_pid, old_share_file_id, e.pid, e.share_file_id);
2642 if (cmp != 0) {
2643 struct server_id_buf tmp1, tmp2;
2644 DBG_WARNING("Expected pid=%s, file_id=%"PRIu64", "
2645 "got pid=%s, file_id=%"PRIu64"\n",
2646 server_id_str_buf(old_pid, &tmp1),
2647 old_share_file_id,
2648 server_id_str_buf(e.pid, &tmp2),
2649 e.share_file_id);
2650 goto done;
2653 e.pid = new_pid;
2654 if (new_mid != UINT64_MAX) {
2655 e.op_mid = new_mid;
2657 e.share_file_id = new_share_file_id;
2659 ok = share_mode_entry_put(&e, &e_buf);
2660 if (!ok) {
2661 DBG_WARNING("share_mode_entry_put failed\n");
2662 goto done;
2665 ltdb->share_entries = e_buf.buf;
2667 d->modified = true;
2669 status = share_mode_data_ltdb_store(d, key, ltdb, NULL, 0);
2670 if (!NT_STATUS_IS_OK(status)) {
2671 DBG_ERR("share_mode_data_ltdb_store failed: %s\n",
2672 nt_errstr(status));
2673 goto done;
2676 ret = true;
2677 done:
2678 TALLOC_FREE(ltdb);
2679 return ret;
2683 * @brief Run @fn protected with G_LOCK_WRITE in the given file_id
2685 * @fn is NOT allowed to call SMB_VFS_* or similar functions,
2686 * which may block for some time in the kernel.
2688 * There must be at least one share_mode_entry, otherwise
2689 * NT_STATUS_NOT_FOUND is returned.
2691 * @param[in] id The key for the share_mode record.
2692 * @param[in] fn The function to run under the g_lock.
2693 * @param[in] private_date A private pointer passed to @fn.
2695 NTSTATUS _share_mode_do_locked_vfs_denied(
2696 struct file_id id,
2697 share_mode_do_locked_vfs_fn_t fn,
2698 void *private_data,
2699 const char *location)
2701 struct smb_vfs_deny_state vfs_deny = {};
2702 struct share_mode_lock *lck = NULL;
2704 lck = get_existing_share_mode_lock(talloc_tos(), id);
2705 if (lck == NULL) {
2706 NTSTATUS status = NT_STATUS_NOT_FOUND;
2707 DBG_DEBUG("get_existing_share_mode_lock failed: %s\n",
2708 nt_errstr(status));
2709 return status;
2712 _smb_vfs_deny_push(&vfs_deny, location);
2713 fn(lck, private_data);
2714 _smb_vfs_deny_pop(&vfs_deny, location);
2716 TALLOC_FREE(lck);
2718 return NT_STATUS_OK;
2722 * @brief Run @fn protected with G_LOCK_WRITE in the given file_id
2724 * @fn is allowed to call SMB_VFS_* or similar functions,
2725 * which may block for some time in the kernel.
2727 * There must be at least one share_mode_entry, otherwise
2728 * NT_STATUS_NOT_FOUND is returned.
2730 * @param[in] id The key for the share_mode record.
2731 * @param[in] fn The function to run under the g_lock.
2732 * @param[in] private_date A private pointer passed to @fn.
2734 NTSTATUS _share_mode_do_locked_vfs_allowed(
2735 struct file_id id,
2736 share_mode_do_locked_vfs_fn_t fn,
2737 void *private_data,
2738 const char *location)
2740 struct share_mode_lock *lck = NULL;
2742 smb_vfs_assert_allowed();
2744 lck = get_existing_share_mode_lock(talloc_tos(), id);
2745 if (lck == NULL) {
2746 NTSTATUS status = NT_STATUS_NOT_FOUND;
2747 DBG_DEBUG("get_existing_share_mode_lock failed: %s\n",
2748 nt_errstr(status));
2749 return status;
2752 fn(lck, private_data);
2754 TALLOC_FREE(lck);
2756 return NT_STATUS_OK;