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)) &&
62 if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
64 "don't support them\n"));
65 return NT_STATUS_NOT_SUPPORTED;
68 if ((fsp->oplock_type != NO_OPLOCK) &&
70 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
72 return map_nt_error_from_unix(errno);
75 fsp->sent_oplock_break = NO_BREAK_SENT;
76 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
77 sconn->oplocks.level_II_open++;
78 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
79 sconn->oplocks.exclusive_open++;
82 DBG_INFO("granted oplock on file %s, %s/%"PRIu64", "
83 "tv_sec = %x, tv_usec = %x\n",
85 file_id_string_tos(&fsp->file_id),
87 (int)fsp->open_time.tv_sec,
88 (int)fsp->open_time.tv_usec);
93 static void release_fsp_kernel_oplock(files_struct *fsp)
95 struct smbd_server_connection *sconn = fsp->conn->sconn;
96 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
99 if (koplocks == NULL) {
102 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
106 if (fsp->oplock_type == NO_OPLOCK) {
109 if (fsp->oplock_type == LEASE_OPLOCK) {
111 * For leases we don't touch kernel oplocks at all
116 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
119 /****************************************************************************
120 Attempt to release an oplock on a file. Decrements oplock count.
121 ****************************************************************************/
123 static void release_file_oplock(files_struct *fsp)
125 struct smbd_server_connection *sconn = fsp->conn->sconn;
127 release_fsp_kernel_oplock(fsp);
129 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
130 sconn->oplocks.level_II_open--;
131 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
132 sconn->oplocks.exclusive_open--;
135 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
136 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
138 fsp->oplock_type = NO_OPLOCK;
139 fsp->sent_oplock_break = NO_BREAK_SENT;
141 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
142 delete_write_cache(fsp);
144 TALLOC_FREE(fsp->oplock_timeout);
147 /****************************************************************************
148 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
149 ****************************************************************************/
151 static void downgrade_file_oplock(files_struct *fsp)
153 struct smbd_server_connection *sconn = fsp->conn->sconn;
154 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
155 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
158 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
159 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
164 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
166 fsp->oplock_type = LEVEL_II_OPLOCK;
167 sconn->oplocks.exclusive_open--;
168 sconn->oplocks.level_II_open++;
169 fsp->sent_oplock_break = NO_BREAK_SENT;
171 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
172 delete_write_cache(fsp);
174 TALLOC_FREE(fsp->oplock_timeout);
177 uint32_t get_lease_type(const struct share_mode_entry *e, struct file_id id)
179 if (e->op_type == LEASE_OPLOCK) {
181 uint32_t current_state;
183 status = leases_db_get(
189 NULL, /* breaking_to_requested */
190 NULL, /* breaking_to_required */
191 NULL, /* lease_version */
193 SMB_ASSERT(NT_STATUS_IS_OK(status));
194 return current_state;
196 return map_oplock_to_lease_type(e->op_type);
199 /****************************************************************************
200 Remove a file oplock. Copes with level II and exclusive.
201 Locks then unlocks the share mode lock. Client can decide to go directly
202 to none even if a "break-to-level II" was sent.
203 ****************************************************************************/
205 bool remove_oplock(files_struct *fsp)
208 struct share_mode_lock *lck;
210 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
212 /* Remove the oplock flag from the sharemode. */
213 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
215 DBG_ERR("failed to lock share entry for "
216 "file %s\n", fsp_str_dbg(fsp));
220 ret = remove_share_oplock(lck, fsp);
222 DBG_ERR("failed to remove share oplock for "
224 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
225 file_id_string_tos(&fsp->file_id));
227 release_file_oplock(fsp);
234 * Deal with a reply when a break-to-level II was sent.
236 bool downgrade_oplock(files_struct *fsp)
239 struct share_mode_lock *lck;
241 DEBUG(10, ("downgrade_oplock called for %s\n",
244 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
246 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
247 "file %s\n", fsp_str_dbg(fsp)));
250 ret = downgrade_share_oplock(lck, fsp);
252 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
253 "for file %s, %s, file_id %s\n",
254 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
255 file_id_string_tos(&fsp->file_id)));
257 downgrade_file_oplock(fsp);
263 static void lease_timeout_handler(struct tevent_context *ctx,
264 struct tevent_timer *te,
268 struct fsp_lease *lease =
269 talloc_get_type_abort(private_data,
271 struct files_struct *fsp;
272 struct share_mode_lock *lck;
273 uint16_t old_epoch = lease->lease.lease_epoch;
275 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
276 &lease->lease.lease_key);
279 TALLOC_FREE(lease->timeout);
284 * Paranoia check: There can only be one fsp_lease per lease
287 SMB_ASSERT(fsp->lease == lease);
289 lck = get_existing_share_mode_lock(
290 talloc_tos(), fsp->file_id);
293 TALLOC_FREE(lease->timeout);
297 fsp_lease_update(fsp);
299 if (lease->lease.lease_epoch != old_epoch) {
301 * If the epoch changed we need to wait for
302 * the next timeout to happen.
304 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
310 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
312 * If the epoch changed we need to wait for
313 * the next timeout to happen.
315 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
321 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
323 (void)downgrade_lease(lease->sconn->client->connections,
326 &lease->lease.lease_key,
332 bool fsp_lease_update(struct files_struct *fsp)
334 const struct GUID *client_guid = fsp_client_guid(fsp);
335 struct fsp_lease *lease = fsp->lease;
336 uint32_t current_state;
338 uint16_t lease_version, epoch;
341 status = leases_db_get(client_guid,
342 &lease->lease.lease_key,
346 NULL, /* breaking_to_requested */
347 NULL, /* breaking_to_required */
350 if (!NT_STATUS_IS_OK(status)) {
351 DBG_WARNING("Could not find lease entry: %s\n",
353 TALLOC_FREE(lease->timeout);
354 lease->lease.lease_state = SMB2_LEASE_NONE;
355 lease->lease.lease_epoch += 1;
356 lease->lease.lease_flags = 0;
360 DEBUG(10,("%s: refresh lease state\n", __func__));
362 /* Ensure we're in sync with current lease state. */
363 if (lease->lease.lease_epoch != epoch) {
364 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
365 TALLOC_FREE(lease->timeout);
367 lease->lease.lease_epoch = epoch;
368 lease->lease.lease_state = current_state;
371 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
373 if (lease->timeout == NULL) {
374 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
376 DEBUG(10,("%s: setup timeout handler\n", __func__));
378 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
380 lease_timeout_handler,
382 if (lease->timeout == NULL) {
383 DEBUG(0, ("%s: Could not add lease timeout handler\n",
388 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
389 TALLOC_FREE(lease->timeout);
395 struct downgrade_lease_additional_state {
396 struct tevent_immediate *im;
397 struct smbXsrv_connection *xconn;
398 uint32_t break_flags;
399 struct smb2_lease_key lease_key;
405 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
406 struct tevent_immediate *im,
409 struct downgrade_lease_additional_state *state =
410 talloc_get_type_abort(private_data,
411 struct downgrade_lease_additional_state);
412 struct smbXsrv_connection *xconn = state->xconn;
415 status = smbd_smb2_send_lease_break(xconn,
422 if (!NT_STATUS_IS_OK(status)) {
423 smbd_server_connection_terminate(xconn,
429 struct fsps_lease_update_state {
430 const struct file_id *id;
431 const struct smb2_lease_key *key;
434 static struct files_struct *fsps_lease_update_fn(
435 struct files_struct *fsp, void *private_data)
437 struct fsps_lease_update_state *state =
438 (struct fsps_lease_update_state *)private_data;
440 if (fsp->oplock_type != LEASE_OPLOCK) {
443 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
446 if (!file_id_equal(&fsp->file_id, state->id)) {
450 fsp_lease_update(fsp);
455 static void fsps_lease_update(struct smbd_server_connection *sconn,
456 const struct file_id *id,
457 const struct smb2_lease_key *key)
459 struct fsps_lease_update_state state = { .id = id, .key = key };
460 files_forall(sconn, fsps_lease_update_fn, &state);
463 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
464 uint32_t num_file_ids,
465 const struct file_id *ids,
466 const struct smb2_lease_key *key,
467 uint32_t lease_state)
469 struct smbd_server_connection *sconn = xconn->client->sconn;
470 const struct GUID *client_guid = NULL;
471 struct share_mode_lock *lck;
472 const struct file_id id = ids[0];
473 uint32_t current_state, breaking_to_requested, breaking_to_required;
475 uint16_t lease_version, epoch;
479 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
480 file_id_string_tos(&id), (unsigned)lease_state));
482 lck = get_existing_share_mode_lock(talloc_tos(), id);
484 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
487 client_guid = &sconn->client->connections->smb2.client.guid;
489 status = leases_db_get(client_guid,
494 &breaking_to_requested,
495 &breaking_to_required,
498 if (!NT_STATUS_IS_OK(status)) {
499 DBG_WARNING("leases_db_get returned %s\n",
506 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
507 "but we're not in breaking state\n",
508 current_state, lease_state);
510 return NT_STATUS_UNSUCCESSFUL;
514 * Can't upgrade anything: breaking_to_requested (and current_state)
515 * must be a strict bitwise superset of new_lease_state
517 if ((lease_state & breaking_to_requested) != lease_state) {
518 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
519 "- expected %"PRIu32"\n",
520 current_state, lease_state,
521 breaking_to_requested);
523 return NT_STATUS_REQUEST_NOT_ACCEPTED;
526 if (current_state != lease_state) {
527 current_state = lease_state;
530 status = NT_STATUS_OK;
532 if ((lease_state & ~breaking_to_required) != 0) {
533 struct downgrade_lease_additional_state *state;
535 DBG_INFO("lease state %"PRIu32" not fully broken from "
536 "%"PRIu32" to %"PRIu32"\n",
539 breaking_to_required);
541 breaking_to_requested = breaking_to_required;
543 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
545 * Here we break in steps, as windows does
546 * see the breaking3 and v2_breaking3 tests.
548 breaking_to_requested |= SMB2_LEASE_READ;
551 state = talloc_zero(xconn,
552 struct downgrade_lease_additional_state);
555 return NT_STATUS_NO_MEMORY;
558 state->im = tevent_create_immediate(state);
559 if (state->im == NULL) {
562 return NT_STATUS_NO_MEMORY;
565 state->xconn = xconn;
566 state->lease_key = *key;
567 state->break_from = current_state;
568 state->break_to = breaking_to_requested;
569 if (lease_version > 1) {
570 state->new_epoch = epoch;
573 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
575 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
578 * This is an async break without
579 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
581 * we need to store NONE state in the
585 breaking_to_requested = 0;
586 breaking_to_required = 0;
592 set_status = leases_db_set(
593 &sconn->client->connections->
598 breaking_to_requested,
599 breaking_to_required,
603 if (!NT_STATUS_IS_OK(set_status)) {
604 DBG_DEBUG("leases_db_set failed: %s\n",
605 nt_errstr(set_status));
611 tevent_schedule_immediate(state->im,
612 xconn->client->raw_ev_ctx,
613 downgrade_lease_additional_trigger,
616 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
618 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
619 "expected %"PRIu32"\n",
622 breaking_to_requested);
624 breaking_to_requested = 0;
625 breaking_to_required = 0;
632 set_status = leases_db_set(
637 breaking_to_requested,
638 breaking_to_required,
642 if (!NT_STATUS_IS_OK(set_status)) {
643 DBG_DEBUG("leases_db_set failed: %s\n",
644 nt_errstr(set_status));
650 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
651 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
654 * No, we did not modify the share mode array. We did modify
655 * the leases_db. But without this we don't notify a lease
656 * break waiter via dbwrap_watch_record. We need to make
657 * leases_db watched too.
659 lck->data->modified = true;
661 fsps_lease_update(sconn, &id, key);
664 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
665 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
668 * Dynamic share case. Ensure other opens are copies.
669 * This will only be breaking to NONE.
672 for (i = 1; i < num_file_ids; i++) {
673 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
675 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
678 fsps_lease_update(sconn, &ids[i], key);
680 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
681 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
689 /****************************************************************************
690 Set up an oplock break message.
691 ****************************************************************************/
693 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
695 static void new_break_message_smb1(files_struct *fsp, int cmd,
696 char result[SMB1_BREAK_MESSAGE_LENGTH])
698 memset(result,'\0',smb_size);
699 srv_set_message(result,8,0,true);
700 SCVAL(result,smb_com,SMBlockingX);
701 SSVAL(result,smb_tid,fsp->conn->cnum);
702 SSVAL(result,smb_pid,0xFFFF);
703 SSVAL(result,smb_uid,0);
704 SSVAL(result,smb_mid,0xFFFF);
705 SCVAL(result,smb_vwv0,0xFF);
706 SSVAL(result,smb_vwv2,fsp->fnum);
707 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
708 SCVAL(result,smb_vwv3+1,cmd);
711 /****************************************************************************
712 Function to do the waiting before sending a local break.
713 ****************************************************************************/
715 static void wait_before_sending_break(void)
717 long wait_time = (long)lp_oplock_break_wait_time();
720 smb_msleep(wait_time);
724 /****************************************************************************
725 Ensure that we have a valid oplock.
726 ****************************************************************************/
728 static files_struct *initial_break_processing(
729 struct smbd_server_connection *sconn, struct file_id id,
730 unsigned long file_id)
732 files_struct *fsp = NULL;
734 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
735 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
736 file_id_string_tos(&id), (int)file_id,
737 sconn->oplocks.exclusive_open,
738 sconn->oplocks.level_II_open));
741 * We need to search the file open table for the
742 * entry containing this dev and inode, and ensure
743 * we have an oplock on it.
746 fsp = file_find_dif(sconn, id, file_id);
749 /* The file could have been closed in the meantime - return success. */
750 DEBUG(3, ("initial_break_processing: cannot find open file "
751 "with file_id %s gen_id = %lu, allowing break to "
752 "succeed.\n", file_id_string_tos(&id), file_id));
756 /* Ensure we have an oplock on the file */
759 * There is a potential race condition in that an oplock could
760 * have been broken due to another udp request, and yet there are
761 * still oplock break messages being sent in the udp message
762 * queue for this file. So return true if we don't have an oplock,
763 * as we may have just freed it.
766 if(fsp->oplock_type == NO_OPLOCK) {
767 DBG_NOTICE("file %s (file_id = %s gen_id = %"PRIu64") "
769 "Allowing break to succeed regardless.\n",
771 file_id_string_tos(&id),
779 static void oplock_timeout_handler(struct tevent_context *ctx,
780 struct tevent_timer *te,
784 files_struct *fsp = (files_struct *)private_data;
786 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
788 /* Remove the timed event handler. */
789 TALLOC_FREE(fsp->oplock_timeout);
790 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
795 /*******************************************************************
796 Add a timeout handler waiting for the client reply.
797 *******************************************************************/
799 static void add_oplock_timeout_handler(files_struct *fsp)
801 if (fsp->oplock_timeout != NULL) {
802 DEBUG(0, ("Logic problem -- have an oplock event hanging "
806 fsp->oplock_timeout =
807 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
808 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
809 oplock_timeout_handler, fsp);
811 if (fsp->oplock_timeout == NULL) {
812 DEBUG(0, ("Could not add oplock timeout handler\n"));
816 static void send_break_message_smb1(files_struct *fsp, int level)
818 struct smbXsrv_connection *xconn = NULL;
819 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
822 * For SMB1 we only have one connection
824 xconn = fsp->conn->sconn->client->connections;
826 new_break_message_smb1(fsp, level, break_msg);
829 if (!srv_send_smb(xconn,
831 IS_CONN_ENCRYPTED(fsp->conn),
833 exit_server_cleanly("send_break_message_smb1: "
834 "srv_send_smb failed.");
838 /*******************************************************************
839 This handles the generic oplock break message from another smbd.
840 *******************************************************************/
842 static void process_oplock_break_message(struct messaging_context *msg_ctx,
845 struct server_id src,
848 struct oplock_break_message *msg = NULL;
849 enum ndr_err_code ndr_err;
852 struct smbd_server_connection *sconn =
853 talloc_get_type_abort(private_data,
854 struct smbd_server_connection);
855 struct server_id self = messaging_server_id(sconn->msg_ctx);
856 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
859 bool break_needed = true;
861 msg = talloc(talloc_tos(), struct oplock_break_message);
863 DBG_WARNING("talloc failed\n");
867 ndr_err = ndr_pull_struct_blob_all(
871 (ndr_pull_flags_fn_t)ndr_pull_oplock_break_message);
872 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
873 DBG_DEBUG("ndr_pull_oplock_break_message failed: %s\n",
874 ndr_errstr(ndr_err));
878 if (DEBUGLEVEL >= 10) {
879 struct server_id_buf buf;
880 DBG_DEBUG("Got break message from %s\n",
881 server_id_str_buf(src, &buf));
882 NDR_PRINT_DEBUG(oplock_break_message, msg);
885 break_to = msg->break_to;
886 fsp = initial_break_processing(sconn, msg->id, msg->share_file_id);
891 /* We hit a race here. Break messages are sent, and before we
892 * get to process this message, we have closed the file. */
893 DEBUG(3, ("Did not find fsp\n"));
897 break_from = fsp_lease_type(fsp);
899 if (fsp->oplock_type != LEASE_OPLOCK) {
900 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
902 * Nothing to do anymore
904 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
905 fsp->sent_oplock_break));
910 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
911 DEBUG(10, ("client_caps without level2 oplocks\n"));
912 break_to &= ~SMB2_LEASE_READ;
915 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
918 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
919 break_to &= ~SMB2_LEASE_READ;
922 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
923 DEBUG(10, ("no level2 oplocks by config\n"));
924 break_to &= ~SMB2_LEASE_READ;
927 if (fsp->oplock_type == LEASE_OPLOCK) {
928 const struct GUID *client_guid = fsp_client_guid(fsp);
929 struct share_mode_lock *lck;
930 uint32_t current_state;
931 uint32_t breaking_to_requested, breaking_to_required;
933 uint16_t lease_version, epoch;
936 lck = get_existing_share_mode_lock(
937 talloc_tos(), fsp->file_id);
940 * We hit a race here. Break messages are sent, and
941 * before we get to process this message, we have closed
944 DEBUG(3, ("Did not find share_mode\n"));
948 status = leases_db_get(client_guid,
949 &fsp->lease->lease.lease_key,
953 &breaking_to_requested,
954 &breaking_to_required,
957 if (!NT_STATUS_IS_OK(status)) {
958 DBG_WARNING("leases_db_get returned %s\n",
964 break_from = current_state;
965 break_to &= current_state;
968 break_to &= breaking_to_required;
969 if (breaking_to_required != break_to) {
971 * Note we don't increment the epoch
972 * here, which might be a bug in
975 breaking_to_required = break_to;
977 break_needed = false;
978 } else if (current_state == break_to) {
979 break_needed = false;
980 } else if (current_state == SMB2_LEASE_READ) {
981 current_state = SMB2_LEASE_NONE;
982 /* Need to increment the epoch */
986 breaking_to_required = break_to;
987 breaking_to_requested = break_to;
988 /* Need to increment the epoch */
995 set_status = leases_db_set(
997 &fsp->lease->lease.lease_key,
1000 breaking_to_requested,
1001 breaking_to_required,
1005 if (!NT_STATUS_IS_OK(set_status)) {
1006 DBG_DEBUG("leases_db_set failed: %s\n",
1007 nt_errstr(set_status));
1012 /* Ensure we're in sync with current lease state. */
1013 fsp_lease_update(fsp);
1018 if (!break_needed) {
1019 DEBUG(10,("%s: skip break\n", __func__));
1023 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1024 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1025 file_id_string_tos(&fsp->file_id),
1030 DEBUG(10, ("break_from=%u, break_to=%u\n",
1031 (unsigned)break_from, (unsigned)break_to));
1033 if ((break_from == break_to) && !break_needed) {
1034 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1036 file_id_string_tos(&fsp->file_id),
1041 /* Need to wait before sending a break
1042 message if we sent ourselves this message. */
1043 if (serverid_equal(&self, &src)) {
1044 wait_before_sending_break();
1047 if (sconn->using_smb2) {
1048 send_break_message_smb2(fsp, break_from, break_to);
1050 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1051 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1054 if ((break_from == SMB2_LEASE_READ) &&
1055 (break_to == SMB2_LEASE_NONE)) {
1057 * This is an async break without a reply and thus no timeout
1059 * leases are handled above.
1061 if (fsp->oplock_type != LEASE_OPLOCK) {
1066 if (fsp->oplock_type == LEASE_OPLOCK) {
1070 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1071 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1073 add_oplock_timeout_handler(fsp);
1076 /*******************************************************************
1077 This handles the kernel oplock break message.
1078 *******************************************************************/
1080 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1083 struct server_id src,
1087 unsigned long file_id;
1089 struct smbd_server_connection *sconn =
1090 talloc_get_type_abort(private_data,
1091 struct smbd_server_connection);
1092 struct server_id_buf tmp;
1094 if (data->data == NULL) {
1095 DEBUG(0, ("Got NULL buffer\n"));
1099 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1100 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1104 /* Pull the data from the message. */
1105 pull_file_id_24((char *)data->data, &id);
1106 file_id = (unsigned long)IVAL(data->data, 24);
1108 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1109 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1110 (unsigned int)file_id));
1112 fsp = initial_break_processing(sconn, id, file_id);
1115 DEBUG(3, ("Got a kernel oplock break message for a file "
1116 "I don't know about\n"));
1120 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1121 /* This is ok, kernel oplocks come in completely async */
1122 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1127 if (sconn->using_smb2) {
1128 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1130 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1133 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1135 add_oplock_timeout_handler(fsp);
1138 static void send_break_to_none(struct messaging_context *msg_ctx,
1139 const struct file_id *id,
1140 const struct share_mode_entry *e)
1143 status = send_break_message(msg_ctx, id, e, OPLOCK_NONE);
1144 if (!NT_STATUS_IS_OK(status)) {
1145 DBG_DEBUG("send_break_message failed: %s\n",
1149 struct break_to_none_state {
1150 struct smbd_server_connection *sconn;
1152 struct smb2_lease_key lease_key;
1153 struct GUID client_guid;
1157 static bool do_break_lease_to_none(struct share_mode_entry *e,
1160 struct break_to_none_state *state = private_data;
1161 uint32_t current_state = 0;
1165 DBG_DEBUG("lease_key=%"PRIu64"/%"PRIu64"\n",
1166 e->lease_key.data[0],
1167 e->lease_key.data[1]);
1169 status = leases_db_get(&e->client_guid,
1173 NULL, /* breaking */
1174 NULL, /* breaking_to_requested */
1175 NULL, /* breaking_to_required */
1176 NULL, /* lease_version */
1178 if (!NT_STATUS_IS_OK(status)) {
1179 DBG_WARNING("leases_db_get failed: %s\n",
1184 if ((current_state & SMB2_LEASE_READ) == 0) {
1188 our_own = smb2_lease_equal(&state->client_guid,
1193 DEBUG(10, ("Don't break our own lease\n"));
1197 DBG_DEBUG("Breaking %"PRIu64"/%"PRIu64" to none\n",
1198 e->lease_key.data[0],
1199 e->lease_key.data[1]);
1201 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1203 state->num_broken += 1;
1208 static bool do_break_oplock_to_none(struct share_mode_entry *e,
1212 struct break_to_none_state *state = private_data;
1214 if (e->op_type == LEASE_OPLOCK) {
1216 * Already being taken care of
1222 * As there could have been multiple writes waiting at the
1223 * lock_share_entry gate we may not be the first to
1224 * enter. Hence the state of the op_types in the share mode
1225 * entries may be partly NO_OPLOCK and partly LEVEL_II
1226 * oplock. It will do no harm to re-send break messages to
1227 * those smbd's that are still waiting their turn to remove
1228 * their LEVEL_II state, and also no harm to ignore existing
1229 * NO_OPLOCK states. JRA.
1232 DBG_DEBUG("e->op_type == %d\n", e->op_type);
1234 if (e->op_type == NO_OPLOCK) {
1239 SMB_ASSERT(!EXCLUSIVE_OPLOCK_TYPE(e->op_type));
1241 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1242 state->num_broken += 1;
1247 /****************************************************************************
1248 This function is called on any file modification or lock request. If a file
1249 is level 2 oplocked then it must tell all other level 2 holders to break to
1251 ****************************************************************************/
1253 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1254 enum level2_contention_type type)
1256 struct break_to_none_state state = {
1257 .sconn = fsp->conn->sconn, .id = fsp->file_id,
1259 struct share_mode_lock *lck = NULL;
1260 struct share_mode_data *d = NULL;
1261 bool ok, has_read_lease;
1264 * If this file is level II oplocked then we need
1265 * to grab the shared memory lock and inform all
1266 * other files with a level II lock that they need
1267 * to flush their read caches. We keep the lock over
1268 * the shared memory area whilst doing this.
1271 if (fsp_lease_type_is_exclusive(fsp)) {
1273 * There can't be any level2 oplocks, we're alone.
1278 has_read_lease = file_has_read_lease(fsp);
1279 if (!has_read_lease) {
1280 DEBUG(10, ("No read oplocks around\n"));
1284 if (fsp->oplock_type == LEASE_OPLOCK) {
1285 state.client_guid = *fsp_client_guid(fsp);
1286 state.lease_key = fsp->lease->lease.lease_key;
1287 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1288 state.lease_key.data[0],
1289 state.lease_key.data[1]));
1292 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1294 DBG_WARNING("failed to lock share mode entry for file %s.\n",
1295 file_id_string_tos(&state.id));
1301 * Walk leases and oplocks separately: We have to send one break per
1302 * lease. If we have multiple share_mode_entry having a common lease,
1303 * we would break the lease twice if we don't walk the leases list
1307 ok = share_mode_forall_leases(lck, do_break_lease_to_none, &state);
1309 DBG_WARNING("share_mode_forall_leases failed\n");
1312 ok = share_mode_forall_entries(lck, do_break_oplock_to_none, &state);
1314 DBG_WARNING("share_mode_forall_entries failed\n");
1317 if (state.num_broken == 0) {
1319 * Lazy update here. It might be that the read lease
1320 * has gone in the meantime.
1322 d->flags &= ~SHARE_MODE_LEASE_READ;
1329 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1330 enum level2_contention_type type)
1332 contend_level2_oplocks_begin_default(fsp, type);
1335 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1336 enum level2_contention_type type)
1341 /****************************************************************************
1342 Linearize a share mode entry struct to an internal oplock break message.
1343 ****************************************************************************/
1345 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1346 const struct share_mode_entry *e)
1348 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1349 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1350 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1351 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1352 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1353 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1354 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1355 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1357 * "id" used to be part of share_mode_entry, thus the strange
1358 * place to put this. Feel free to move somewhere else :-)
1360 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1361 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1362 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1363 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1364 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1365 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1368 /****************************************************************************
1369 De-linearize an internal oplock break message to a share mode entry struct.
1370 ****************************************************************************/
1372 void message_to_share_mode_entry(struct file_id *id,
1373 struct share_mode_entry *e,
1376 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1377 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1378 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1379 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1380 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1381 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1382 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1383 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1385 * "id" used to be part of share_mode_entry, thus the strange
1386 * place to put this. Feel free to move somewhere else :-)
1388 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1389 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1390 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1391 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1392 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1393 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1396 /****************************************************************************
1397 Setup oplocks for this process.
1398 ****************************************************************************/
1400 bool init_oplocks(struct smbd_server_connection *sconn)
1402 DEBUG(3,("init_oplocks: initializing messages.\n"));
1404 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1405 process_oplock_break_message);
1406 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1407 process_kernel_oplock_break);
1411 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1413 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1415 /* only initialize once */
1416 if (koplocks == NULL) {
1417 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1418 koplocks = linux_init_kernel_oplocks(sconn);
1420 sconn->oplocks.kernel_ops = koplocks;