2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998 - 2001
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/>.
22 #define DBGC_CLASS DBGC_LOCKING
24 #include "lib/util/server_id.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
28 #include "locking/leases_db.h"
29 #include "../librpc/gen_ndr/ndr_open_files.h"
32 * helper function used by the kernel oplock backends to post the break message
34 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
36 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
38 /* Put the kernel break info into the message. */
39 push_file_id_24((char *)msg, &fsp->file_id);
40 SIVAL(msg,24,fsp->fh->gen_id);
42 /* Don't need to be root here as we're only ever
43 sending to ourselves. */
45 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
47 msg, MSG_SMB_KERNEL_BREAK_SIZE);
50 /****************************************************************************
51 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
52 disabled (just sets flags).
53 ****************************************************************************/
55 NTSTATUS set_file_oplock(files_struct *fsp)
57 struct smbd_server_connection *sconn = fsp->conn->sconn;
58 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
59 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
61 struct file_id_buf buf;
63 if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
64 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
65 "don't support them\n"));
66 return NT_STATUS_NOT_SUPPORTED;
69 if ((fsp->oplock_type != NO_OPLOCK) &&
71 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
73 return map_nt_error_from_unix(errno);
76 fsp->sent_oplock_break = NO_BREAK_SENT;
77 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
78 sconn->oplocks.level_II_open++;
79 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
80 sconn->oplocks.exclusive_open++;
83 DBG_INFO("granted oplock on file %s, %s/%"PRIu64", "
84 "tv_sec = %x, tv_usec = %x\n",
86 file_id_str_buf(fsp->file_id, &buf),
88 (int)fsp->open_time.tv_sec,
89 (int)fsp->open_time.tv_usec);
94 static void release_fsp_kernel_oplock(files_struct *fsp)
96 struct smbd_server_connection *sconn = fsp->conn->sconn;
97 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
100 if (koplocks == NULL) {
103 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
107 if (fsp->oplock_type == NO_OPLOCK) {
110 if (fsp->oplock_type == LEASE_OPLOCK) {
112 * For leases we don't touch kernel oplocks at all
117 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
120 /****************************************************************************
121 Attempt to release an oplock on a file. Decrements oplock count.
122 ****************************************************************************/
124 static void release_file_oplock(files_struct *fsp)
126 struct smbd_server_connection *sconn = fsp->conn->sconn;
128 release_fsp_kernel_oplock(fsp);
130 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
131 sconn->oplocks.level_II_open--;
132 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
133 sconn->oplocks.exclusive_open--;
136 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
137 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
139 fsp->oplock_type = NO_OPLOCK;
140 fsp->sent_oplock_break = NO_BREAK_SENT;
142 TALLOC_FREE(fsp->oplock_timeout);
145 /****************************************************************************
146 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
147 ****************************************************************************/
149 static void downgrade_file_oplock(files_struct *fsp)
151 struct smbd_server_connection *sconn = fsp->conn->sconn;
152 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
153 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
156 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
157 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
162 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
164 fsp->oplock_type = LEVEL_II_OPLOCK;
165 sconn->oplocks.exclusive_open--;
166 sconn->oplocks.level_II_open++;
167 fsp->sent_oplock_break = NO_BREAK_SENT;
169 TALLOC_FREE(fsp->oplock_timeout);
172 uint32_t get_lease_type(struct share_mode_entry *e, struct file_id id)
174 struct GUID_txt_buf guid_strbuf;
175 struct file_id_buf file_id_strbuf;
177 uint32_t current_state;
179 if (e->op_type != LEASE_OPLOCK) {
180 return map_oplock_to_lease_type(e->op_type);
183 status = leases_db_get(&e->client_guid,
188 NULL, /* breaking_to_requested */
189 NULL, /* breaking_to_required */
190 NULL, /* lease_version */
192 if (NT_STATUS_IS_OK(status)) {
193 return current_state;
196 if (share_entry_stale_pid(e)) {
199 DBG_ERR("leases_db_get for client_guid [%s] "
200 "lease_key [%"PRIu64"/%"PRIu64"] "
201 "file_id [%s] failed: %s\n",
202 GUID_buf_string(&e->client_guid, &guid_strbuf),
203 e->lease_key.data[0],
204 e->lease_key.data[1],
205 file_id_str_buf(id, &file_id_strbuf),
207 smb_panic("leases_db_get() failed");
210 /****************************************************************************
211 Remove a file oplock. Copes with level II and exclusive.
212 Locks then unlocks the share mode lock. Client can decide to go directly
213 to none even if a "break-to-level II" was sent.
214 ****************************************************************************/
216 bool remove_oplock(files_struct *fsp)
219 struct share_mode_lock *lck;
221 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
223 /* Remove the oplock flag from the sharemode. */
224 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
226 DBG_ERR("failed to lock share entry for "
227 "file %s\n", fsp_str_dbg(fsp));
231 ret = remove_share_oplock(lck, fsp);
233 struct file_id_buf buf;
235 DBG_ERR("failed to remove share oplock for "
237 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
238 file_id_str_buf(fsp->file_id, &buf));
240 release_file_oplock(fsp);
247 * Deal with a reply when a break-to-level II was sent.
249 bool downgrade_oplock(files_struct *fsp)
252 struct share_mode_lock *lck;
254 DEBUG(10, ("downgrade_oplock called for %s\n",
257 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
259 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
260 "file %s\n", fsp_str_dbg(fsp)));
263 ret = downgrade_share_oplock(lck, fsp);
265 struct file_id_buf idbuf;
266 DBG_ERR("failed to downgrade share oplock "
267 "for file %s, %s, file_id %s\n",
270 file_id_str_buf(fsp->file_id, &idbuf));
272 downgrade_file_oplock(fsp);
278 static void lease_timeout_handler(struct tevent_context *ctx,
279 struct tevent_timer *te,
283 struct fsp_lease *lease =
284 talloc_get_type_abort(private_data,
286 struct files_struct *fsp;
287 struct share_mode_lock *lck;
288 uint16_t old_epoch = lease->lease.lease_epoch;
290 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
291 &lease->lease.lease_key);
294 TALLOC_FREE(lease->timeout);
299 * Paranoia check: There can only be one fsp_lease per lease
302 SMB_ASSERT(fsp->lease == lease);
304 lck = get_existing_share_mode_lock(
305 talloc_tos(), fsp->file_id);
308 TALLOC_FREE(lease->timeout);
312 fsp_lease_update(fsp);
314 if (lease->lease.lease_epoch != old_epoch) {
316 * If the epoch changed we need to wait for
317 * the next timeout to happen.
319 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
325 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
327 * If the epoch changed we need to wait for
328 * the next timeout to happen.
330 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
336 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
338 (void)downgrade_lease(lease->sconn->client,
341 &lease->lease.lease_key,
347 bool fsp_lease_update(struct files_struct *fsp)
349 const struct GUID *client_guid = fsp_client_guid(fsp);
350 struct fsp_lease *lease = fsp->lease;
351 uint32_t current_state;
353 uint16_t lease_version, epoch;
356 status = leases_db_get(client_guid,
357 &lease->lease.lease_key,
361 NULL, /* breaking_to_requested */
362 NULL, /* breaking_to_required */
365 if (!NT_STATUS_IS_OK(status)) {
366 DBG_WARNING("Could not find lease entry: %s\n",
368 TALLOC_FREE(lease->timeout);
369 lease->lease.lease_state = SMB2_LEASE_NONE;
370 lease->lease.lease_epoch += 1;
371 lease->lease.lease_flags = 0;
375 DEBUG(10,("%s: refresh lease state\n", __func__));
377 /* Ensure we're in sync with current lease state. */
378 if (lease->lease.lease_epoch != epoch) {
379 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
380 TALLOC_FREE(lease->timeout);
382 lease->lease.lease_epoch = epoch;
383 lease->lease.lease_state = current_state;
386 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
388 if (lease->timeout == NULL) {
389 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
391 DEBUG(10,("%s: setup timeout handler\n", __func__));
393 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
395 lease_timeout_handler,
397 if (lease->timeout == NULL) {
398 DEBUG(0, ("%s: Could not add lease timeout handler\n",
403 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
404 TALLOC_FREE(lease->timeout);
410 struct downgrade_lease_additional_state {
411 struct tevent_immediate *im;
412 struct smbXsrv_client *client;
413 uint32_t break_flags;
414 struct smb2_lease_key lease_key;
420 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
421 struct tevent_immediate *im,
424 struct downgrade_lease_additional_state *state =
425 talloc_get_type_abort(private_data,
426 struct downgrade_lease_additional_state);
429 status = smbd_smb2_send_lease_break(state->client,
436 if (!NT_STATUS_IS_OK(status)) {
437 smbd_server_disconnect_client(state->client,
443 struct fsps_lease_update_state {
444 const struct file_id *id;
445 const struct smb2_lease_key *key;
448 static struct files_struct *fsps_lease_update_fn(
449 struct files_struct *fsp, void *private_data)
451 struct fsps_lease_update_state *state =
452 (struct fsps_lease_update_state *)private_data;
454 if (fsp->oplock_type != LEASE_OPLOCK) {
457 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
460 if (!file_id_equal(&fsp->file_id, state->id)) {
464 fsp_lease_update(fsp);
469 static void fsps_lease_update(struct smbd_server_connection *sconn,
470 const struct file_id *id,
471 const struct smb2_lease_key *key)
473 struct fsps_lease_update_state state = { .id = id, .key = key };
474 files_forall(sconn, fsps_lease_update_fn, &state);
477 NTSTATUS downgrade_lease(struct smbXsrv_client *client,
478 uint32_t num_file_ids,
479 const struct file_id *ids,
480 const struct smb2_lease_key *key,
481 uint32_t lease_state)
483 struct smbd_server_connection *sconn = client->sconn;
484 const struct GUID *client_guid = NULL;
485 struct share_mode_lock *lck;
486 const struct file_id id = ids[0];
487 uint32_t current_state, breaking_to_requested, breaking_to_required;
489 uint16_t lease_version, epoch;
492 struct file_id_buf idbuf;
494 DBG_DEBUG("Downgrading %s to %"PRIu32"\n",
495 file_id_str_buf(id, &idbuf),
498 lck = get_existing_share_mode_lock(talloc_tos(), id);
500 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
503 client_guid = &sconn->client->global->client_guid;
505 status = leases_db_get(client_guid,
510 &breaking_to_requested,
511 &breaking_to_required,
514 if (!NT_STATUS_IS_OK(status)) {
515 DBG_WARNING("leases_db_get returned %s\n",
522 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
523 "but we're not in breaking state\n",
524 current_state, lease_state);
526 return NT_STATUS_UNSUCCESSFUL;
530 * Can't upgrade anything: breaking_to_requested (and current_state)
531 * must be a strict bitwise superset of new_lease_state
533 if ((lease_state & breaking_to_requested) != lease_state) {
534 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
535 "- expected %"PRIu32"\n",
536 current_state, lease_state,
537 breaking_to_requested);
539 return NT_STATUS_REQUEST_NOT_ACCEPTED;
542 if (current_state != lease_state) {
543 current_state = lease_state;
546 status = NT_STATUS_OK;
548 if ((lease_state & ~breaking_to_required) != 0) {
549 struct downgrade_lease_additional_state *state;
551 DBG_INFO("lease state %"PRIu32" not fully broken from "
552 "%"PRIu32" to %"PRIu32"\n",
555 breaking_to_required);
557 breaking_to_requested = breaking_to_required;
559 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
561 * Here we break in steps, as windows does
562 * see the breaking3 and v2_breaking3 tests.
564 breaking_to_requested |= SMB2_LEASE_READ;
567 state = talloc_zero(client,
568 struct downgrade_lease_additional_state);
571 return NT_STATUS_NO_MEMORY;
574 state->im = tevent_create_immediate(state);
575 if (state->im == NULL) {
578 return NT_STATUS_NO_MEMORY;
581 state->client = client;
582 state->lease_key = *key;
583 state->break_from = current_state;
584 state->break_to = breaking_to_requested;
585 if (lease_version > 1) {
586 state->new_epoch = epoch;
589 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
591 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
594 * This is an async break without
595 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
597 * we need to store NONE state in the
601 breaking_to_requested = 0;
602 breaking_to_required = 0;
608 set_status = leases_db_set(
609 &sconn->client->global->client_guid,
613 breaking_to_requested,
614 breaking_to_required,
618 if (!NT_STATUS_IS_OK(set_status)) {
619 DBG_DEBUG("leases_db_set failed: %s\n",
620 nt_errstr(set_status));
626 tevent_schedule_immediate(state->im,
628 downgrade_lease_additional_trigger,
631 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
633 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
634 "expected %"PRIu32"\n",
637 breaking_to_requested);
639 breaking_to_requested = 0;
640 breaking_to_required = 0;
647 set_status = leases_db_set(
652 breaking_to_requested,
653 breaking_to_required,
657 if (!NT_STATUS_IS_OK(set_status)) {
658 DBG_DEBUG("leases_db_set failed: %s\n",
659 nt_errstr(set_status));
665 DBG_DEBUG("Downgrading %s to %"PRIu32" => %s\n",
666 file_id_str_buf(id, &idbuf),
670 share_mode_wakeup_waiters(id);
672 fsps_lease_update(sconn, &id, key);
676 DBG_DEBUG("Downgrading %s to %"PRIu32" => %s\n",
677 file_id_str_buf(id, &idbuf),
682 * Dynamic share case. Ensure other opens are copies.
683 * This will only be breaking to NONE.
686 for (i = 1; i < num_file_ids; i++) {
687 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
689 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
692 fsps_lease_update(sconn, &ids[i], key);
694 DBG_DEBUG("Downgrading %s to %"PRIu32" => %s\n",
695 file_id_str_buf(ids[i], &idbuf),
705 /****************************************************************************
706 Set up an oplock break message.
707 ****************************************************************************/
709 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
711 static void new_break_message_smb1(files_struct *fsp, int cmd,
712 char result[SMB1_BREAK_MESSAGE_LENGTH])
714 memset(result,'\0',smb_size);
715 srv_set_message(result,8,0,true);
716 SCVAL(result,smb_com,SMBlockingX);
717 SSVAL(result,smb_tid,fsp->conn->cnum);
718 SSVAL(result,smb_pid,0xFFFF);
719 SSVAL(result,smb_uid,0);
720 SSVAL(result,smb_mid,0xFFFF);
721 SCVAL(result,smb_vwv0,0xFF);
722 SSVAL(result,smb_vwv2,fsp->fnum);
723 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
724 SCVAL(result,smb_vwv3+1,cmd);
727 /****************************************************************************
728 Function to do the waiting before sending a local break.
729 ****************************************************************************/
731 static void wait_before_sending_break(void)
733 long wait_time = (long)lp_oplock_break_wait_time();
736 smb_msleep(wait_time);
740 /****************************************************************************
741 Ensure that we have a valid oplock.
742 ****************************************************************************/
744 static files_struct *initial_break_processing(
745 struct smbd_server_connection *sconn, struct file_id id,
746 unsigned long file_id)
748 files_struct *fsp = NULL;
749 struct file_id_buf idbuf;
751 DBG_NOTICE("called for %s/%u\n"
752 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
753 file_id_str_buf(id, &idbuf),
755 sconn->oplocks.exclusive_open,
756 sconn->oplocks.level_II_open);
759 * We need to search the file open table for the
760 * entry containing this dev and inode, and ensure
761 * we have an oplock on it.
764 fsp = file_find_dif(sconn, id, file_id);
767 /* The file could have been closed in the meantime - return success. */
768 DBG_NOTICE("cannot find open file "
769 "with file_id %s gen_id = %lu, allowing break to "
771 file_id_str_buf(id, &idbuf),
776 /* Ensure we have an oplock on the file */
779 * There is a potential race condition in that an oplock could
780 * have been broken due to another udp request, and yet there are
781 * still oplock break messages being sent in the udp message
782 * queue for this file. So return true if we don't have an oplock,
783 * as we may have just freed it.
786 if(fsp->oplock_type == NO_OPLOCK) {
787 DBG_NOTICE("file %s (file_id = %s gen_id = %"PRIu64") "
789 "Allowing break to succeed regardless.\n",
791 file_id_str_buf(id, &idbuf),
799 static void oplock_timeout_handler(struct tevent_context *ctx,
800 struct tevent_timer *te,
804 files_struct *fsp = (files_struct *)private_data;
806 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
808 /* Remove the timed event handler. */
809 TALLOC_FREE(fsp->oplock_timeout);
810 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
815 /*******************************************************************
816 Add a timeout handler waiting for the client reply.
817 *******************************************************************/
819 static void add_oplock_timeout_handler(files_struct *fsp)
821 if (fsp->oplock_timeout != NULL) {
822 DEBUG(0, ("Logic problem -- have an oplock event hanging "
826 fsp->oplock_timeout =
827 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
828 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
829 oplock_timeout_handler, fsp);
831 if (fsp->oplock_timeout == NULL) {
832 DEBUG(0, ("Could not add oplock timeout handler\n"));
836 static void send_break_message_smb1(files_struct *fsp, int level)
838 struct smbXsrv_connection *xconn = NULL;
839 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
842 * For SMB1 we only have one connection
844 xconn = fsp->conn->sconn->client->connections;
846 new_break_message_smb1(fsp, level, break_msg);
849 if (!srv_send_smb(xconn,
851 IS_CONN_ENCRYPTED(fsp->conn),
853 exit_server_cleanly("send_break_message_smb1: "
854 "srv_send_smb failed.");
858 /*******************************************************************
859 This handles the generic oplock break message from another smbd.
860 *******************************************************************/
862 static void process_oplock_break_message(struct messaging_context *msg_ctx,
865 struct server_id src,
868 struct oplock_break_message *msg = NULL;
869 enum ndr_err_code ndr_err;
872 struct smbd_server_connection *sconn =
873 talloc_get_type_abort(private_data,
874 struct smbd_server_connection);
875 struct server_id self = messaging_server_id(sconn->msg_ctx);
876 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
879 bool break_needed = true;
881 msg = talloc(talloc_tos(), struct oplock_break_message);
883 DBG_WARNING("talloc failed\n");
887 ndr_err = ndr_pull_struct_blob_all(
891 (ndr_pull_flags_fn_t)ndr_pull_oplock_break_message);
892 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
893 DBG_DEBUG("ndr_pull_oplock_break_message failed: %s\n",
894 ndr_errstr(ndr_err));
898 if (DEBUGLEVEL >= 10) {
899 struct server_id_buf buf;
900 DBG_DEBUG("Got break message from %s\n",
901 server_id_str_buf(src, &buf));
902 NDR_PRINT_DEBUG(oplock_break_message, msg);
905 break_to = msg->break_to;
906 fsp = initial_break_processing(sconn, msg->id, msg->share_file_id);
911 /* We hit a race here. Break messages are sent, and before we
912 * get to process this message, we have closed the file. */
913 DEBUG(3, ("Did not find fsp\n"));
917 break_from = fsp_lease_type(fsp);
919 if (fsp->oplock_type != LEASE_OPLOCK) {
920 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
922 * Nothing to do anymore
924 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
925 fsp->sent_oplock_break));
930 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
931 DEBUG(10, ("client_caps without level2 oplocks\n"));
932 break_to &= ~SMB2_LEASE_READ;
935 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
938 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
939 break_to &= ~SMB2_LEASE_READ;
942 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
943 DEBUG(10, ("no level2 oplocks by config\n"));
944 break_to &= ~SMB2_LEASE_READ;
947 if (fsp->oplock_type == LEASE_OPLOCK) {
948 const struct GUID *client_guid = fsp_client_guid(fsp);
949 struct share_mode_lock *lck;
950 uint32_t current_state;
951 uint32_t breaking_to_requested, breaking_to_required;
953 uint16_t lease_version, epoch;
956 lck = get_existing_share_mode_lock(
957 talloc_tos(), fsp->file_id);
960 * We hit a race here. Break messages are sent, and
961 * before we get to process this message, we have closed
964 DEBUG(3, ("Did not find share_mode\n"));
968 status = leases_db_get(client_guid,
969 &fsp->lease->lease.lease_key,
973 &breaking_to_requested,
974 &breaking_to_required,
977 if (!NT_STATUS_IS_OK(status)) {
978 DBG_WARNING("leases_db_get returned %s\n",
984 break_from = current_state;
985 break_to &= current_state;
988 break_to &= breaking_to_required;
989 if (breaking_to_required != break_to) {
991 * Note we don't increment the epoch
992 * here, which might be a bug in
995 breaking_to_required = break_to;
997 break_needed = false;
998 } else if (current_state == break_to) {
999 break_needed = false;
1000 } else if (current_state == SMB2_LEASE_READ) {
1001 current_state = SMB2_LEASE_NONE;
1002 /* Need to increment the epoch */
1006 breaking_to_required = break_to;
1007 breaking_to_requested = break_to;
1008 /* Need to increment the epoch */
1013 NTSTATUS set_status;
1015 set_status = leases_db_set(
1017 &fsp->lease->lease.lease_key,
1020 breaking_to_requested,
1021 breaking_to_required,
1025 if (!NT_STATUS_IS_OK(set_status)) {
1026 DBG_DEBUG("leases_db_set failed: %s\n",
1027 nt_errstr(set_status));
1032 /* Ensure we're in sync with current lease state. */
1033 fsp_lease_update(fsp);
1038 if (!break_needed) {
1039 DEBUG(10,("%s: skip break\n", __func__));
1043 if (break_from == SMB2_LEASE_NONE) {
1044 struct file_id_buf idbuf;
1045 DBG_NOTICE("Already downgraded oplock to none on %s: %s\n",
1046 file_id_str_buf(fsp->file_id, &idbuf),
1051 DEBUG(10, ("break_from=%u, break_to=%u\n",
1052 (unsigned)break_from, (unsigned)break_to));
1054 if (break_from == break_to) {
1055 struct file_id_buf idbuf;
1056 DBG_NOTICE("Already downgraded oplock to %u on %s: %s\n",
1058 file_id_str_buf(fsp->file_id, &idbuf),
1063 /* Need to wait before sending a break
1064 message if we sent ourselves this message. */
1065 if (server_id_equal(&self, &src)) {
1066 wait_before_sending_break();
1069 if (sconn->using_smb2) {
1070 send_break_message_smb2(fsp, break_from, break_to);
1072 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1073 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1076 if ((break_from == SMB2_LEASE_READ) &&
1077 (break_to == SMB2_LEASE_NONE)) {
1079 * This is an async break without a reply and thus no timeout
1081 * leases are handled above.
1083 if (fsp->oplock_type != LEASE_OPLOCK) {
1088 if (fsp->oplock_type == LEASE_OPLOCK) {
1092 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1093 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1095 add_oplock_timeout_handler(fsp);
1098 /*******************************************************************
1099 This handles the kernel oplock break message.
1100 *******************************************************************/
1102 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1105 struct server_id src,
1109 struct file_id_buf idbuf;
1110 unsigned long file_id;
1112 struct smbd_server_connection *sconn =
1113 talloc_get_type_abort(private_data,
1114 struct smbd_server_connection);
1115 struct server_id_buf tmp;
1117 if (data->data == NULL) {
1118 DEBUG(0, ("Got NULL buffer\n"));
1122 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1123 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1127 /* Pull the data from the message. */
1128 pull_file_id_24((char *)data->data, &id);
1129 file_id = (unsigned long)IVAL(data->data, 24);
1131 DBG_DEBUG("Got kernel oplock break message from pid %s: %s/%u\n",
1132 server_id_str_buf(src, &tmp),
1133 file_id_str_buf(id, &idbuf),
1134 (unsigned int)file_id);
1136 fsp = initial_break_processing(sconn, id, file_id);
1139 DEBUG(3, ("Got a kernel oplock break message for a file "
1140 "I don't know about\n"));
1144 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1145 /* This is ok, kernel oplocks come in completely async */
1146 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1151 if (sconn->using_smb2) {
1152 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1154 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1157 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1159 add_oplock_timeout_handler(fsp);
1162 static void send_break_to_none(struct messaging_context *msg_ctx,
1163 const struct file_id *id,
1164 const struct share_mode_entry *e)
1167 status = send_break_message(msg_ctx, id, e, OPLOCK_NONE);
1168 if (!NT_STATUS_IS_OK(status)) {
1169 DBG_DEBUG("send_break_message failed: %s\n",
1173 struct break_to_none_state {
1174 struct smbd_server_connection *sconn;
1176 struct smb2_lease_key lease_key;
1177 struct GUID client_guid;
1181 static bool do_break_lease_to_none(struct share_mode_entry *e,
1184 struct break_to_none_state *state = private_data;
1185 uint32_t current_state = 0;
1189 DBG_DEBUG("lease_key=%"PRIu64"/%"PRIu64"\n",
1190 e->lease_key.data[0],
1191 e->lease_key.data[1]);
1193 status = leases_db_get(&e->client_guid,
1197 NULL, /* breaking */
1198 NULL, /* breaking_to_requested */
1199 NULL, /* breaking_to_required */
1200 NULL, /* lease_version */
1202 if (!NT_STATUS_IS_OK(status)) {
1203 DBG_WARNING("leases_db_get failed: %s\n",
1208 if ((current_state & SMB2_LEASE_READ) == 0) {
1212 our_own = smb2_lease_equal(&state->client_guid,
1217 DEBUG(10, ("Don't break our own lease\n"));
1221 DBG_DEBUG("Breaking %"PRIu64"/%"PRIu64" to none\n",
1222 e->lease_key.data[0],
1223 e->lease_key.data[1]);
1225 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1227 state->num_broken += 1;
1232 static bool do_break_oplock_to_none(struct share_mode_entry *e,
1236 struct break_to_none_state *state = private_data;
1238 if (e->op_type == LEASE_OPLOCK) {
1240 * Already being taken care of
1246 * As there could have been multiple writes waiting at the
1247 * lock_share_entry gate we may not be the first to
1248 * enter. Hence the state of the op_types in the share mode
1249 * entries may be partly NO_OPLOCK and partly LEVEL_II
1250 * oplock. It will do no harm to re-send break messages to
1251 * those smbd's that are still waiting their turn to remove
1252 * their LEVEL_II state, and also no harm to ignore existing
1253 * NO_OPLOCK states. JRA.
1256 DBG_DEBUG("e->op_type == %d\n", e->op_type);
1258 if (e->op_type == NO_OPLOCK) {
1263 SMB_ASSERT(!EXCLUSIVE_OPLOCK_TYPE(e->op_type));
1265 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1266 state->num_broken += 1;
1271 /****************************************************************************
1272 This function is called on any file modification or lock request. If a file
1273 is level 2 oplocked then it must tell all other level 2 holders to break to
1275 ****************************************************************************/
1277 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1278 enum level2_contention_type type)
1280 struct break_to_none_state state = {
1281 .sconn = fsp->conn->sconn, .id = fsp->file_id,
1283 struct share_mode_lock *lck = NULL;
1284 struct share_mode_data *d = NULL;
1285 bool ok, has_read_lease;
1288 * If this file is level II oplocked then we need
1289 * to grab the shared memory lock and inform all
1290 * other files with a level II lock that they need
1291 * to flush their read caches. We keep the lock over
1292 * the shared memory area whilst doing this.
1295 if (fsp_lease_type_is_exclusive(fsp)) {
1297 * There can't be any level2 oplocks, we're alone.
1302 has_read_lease = file_has_read_lease(fsp);
1303 if (!has_read_lease) {
1304 DEBUG(10, ("No read oplocks around\n"));
1308 if (fsp->oplock_type == LEASE_OPLOCK) {
1309 state.client_guid = *fsp_client_guid(fsp);
1310 state.lease_key = fsp->lease->lease.lease_key;
1311 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1312 state.lease_key.data[0],
1313 state.lease_key.data[1]));
1316 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1318 struct file_id_buf idbuf;
1319 DBG_WARNING("failed to lock share mode entry for file %s.\n",
1320 file_id_str_buf(state.id, &idbuf));
1326 * Walk leases and oplocks separately: We have to send one break per
1327 * lease. If we have multiple share_mode_entry having a common lease,
1328 * we would break the lease twice if we don't walk the leases list
1332 ok = share_mode_forall_leases(lck, do_break_lease_to_none, &state);
1334 DBG_WARNING("share_mode_forall_leases failed\n");
1337 ok = share_mode_forall_entries(lck, do_break_oplock_to_none, &state);
1339 DBG_WARNING("share_mode_forall_entries failed\n");
1342 if (state.num_broken == 0) {
1344 * Lazy update here. It might be that the read lease
1345 * has gone in the meantime.
1347 d->flags &= ~SHARE_MODE_LEASE_READ;
1354 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1355 enum level2_contention_type type)
1357 contend_level2_oplocks_begin_default(fsp, type);
1360 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1361 enum level2_contention_type type)
1366 /****************************************************************************
1367 Linearize a share mode entry struct to an internal oplock break message.
1368 ****************************************************************************/
1370 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1371 const struct share_mode_entry *e)
1373 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1374 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1375 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1376 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1377 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1378 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1379 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1380 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1382 * "id" used to be part of share_mode_entry, thus the strange
1383 * place to put this. Feel free to move somewhere else :-)
1385 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1386 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1387 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1388 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1389 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1390 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1393 /****************************************************************************
1394 De-linearize an internal oplock break message to a share mode entry struct.
1395 ****************************************************************************/
1397 void message_to_share_mode_entry(struct file_id *id,
1398 struct share_mode_entry *e,
1401 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1402 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1403 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1404 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1405 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1406 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1407 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1408 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1410 * "id" used to be part of share_mode_entry, thus the strange
1411 * place to put this. Feel free to move somewhere else :-)
1413 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1414 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1415 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1416 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1417 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1418 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1421 /****************************************************************************
1422 Setup oplocks for this process.
1423 ****************************************************************************/
1425 bool init_oplocks(struct smbd_server_connection *sconn)
1427 DEBUG(3,("init_oplocks: initializing messages.\n"));
1429 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1430 process_oplock_break_message);
1431 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1432 process_kernel_oplock_break);
1436 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1438 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1440 /* only initialize once */
1441 if (koplocks == NULL) {
1442 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1443 koplocks = linux_init_kernel_oplocks(sconn);
1445 sconn->oplocks.kernel_ops = koplocks;