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 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
83 "tv_sec = %x, tv_usec = %x\n",
84 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
85 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
86 (int)fsp->open_time.tv_usec ));
91 static void release_fsp_kernel_oplock(files_struct *fsp)
93 struct smbd_server_connection *sconn = fsp->conn->sconn;
94 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
97 if (koplocks == NULL) {
100 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
104 if (fsp->oplock_type == NO_OPLOCK) {
107 if (fsp->oplock_type == LEASE_OPLOCK) {
109 * For leases we don't touch kernel oplocks at all
114 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
117 /****************************************************************************
118 Attempt to release an oplock on a file. Decrements oplock count.
119 ****************************************************************************/
121 static void release_file_oplock(files_struct *fsp)
123 struct smbd_server_connection *sconn = fsp->conn->sconn;
125 release_fsp_kernel_oplock(fsp);
127 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
128 sconn->oplocks.level_II_open--;
129 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
130 sconn->oplocks.exclusive_open--;
133 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
134 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
136 fsp->oplock_type = NO_OPLOCK;
137 fsp->sent_oplock_break = NO_BREAK_SENT;
139 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
140 delete_write_cache(fsp);
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 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
170 delete_write_cache(fsp);
172 TALLOC_FREE(fsp->oplock_timeout);
175 uint32_t get_lease_type(const struct share_mode_data *d,
176 const struct share_mode_entry *e)
178 if (e->op_type == LEASE_OPLOCK) {
180 uint32_t current_state;
182 status = leases_db_get(
188 NULL, /* breaking_to_requested */
189 NULL, /* breaking_to_required */
190 NULL, /* lease_version */
192 SMB_ASSERT(NT_STATUS_IS_OK(status));
193 return current_state;
195 return map_oplock_to_lease_type(e->op_type);
198 /****************************************************************************
199 Remove a file oplock with lock already held. Copes with level II and exclusive.
200 ****************************************************************************/
202 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
206 ret = remove_share_oplock(lck, fsp);
208 DBG_ERR("failed to remove share oplock for "
210 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
211 file_id_string_tos(&fsp->file_id));
213 release_file_oplock(fsp);
218 /****************************************************************************
219 Remove a file oplock. Copes with level II and exclusive.
220 Locks then unlocks the share mode lock. Client can decide to go directly
221 to none even if a "break-to-level II" was sent.
222 ****************************************************************************/
224 bool remove_oplock(files_struct *fsp)
227 struct share_mode_lock *lck;
229 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
231 /* Remove the oplock flag from the sharemode. */
232 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
234 DBG_ERR("failed to lock share entry for "
235 "file %s\n", fsp_str_dbg(fsp));
239 ret = remove_oplock_under_lock(fsp, lck);
246 * Deal with a reply when a break-to-level II was sent.
248 bool downgrade_oplock(files_struct *fsp)
251 struct share_mode_lock *lck;
253 DEBUG(10, ("downgrade_oplock called for %s\n",
256 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
258 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
259 "file %s\n", fsp_str_dbg(fsp)));
262 ret = downgrade_share_oplock(lck, fsp);
264 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
265 "for file %s, %s, file_id %s\n",
266 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
267 file_id_string_tos(&fsp->file_id)));
269 downgrade_file_oplock(fsp);
275 static void lease_timeout_handler(struct tevent_context *ctx,
276 struct tevent_timer *te,
280 struct fsp_lease *lease =
281 talloc_get_type_abort(private_data,
283 struct files_struct *fsp;
284 struct share_mode_lock *lck;
285 uint16_t old_epoch = lease->lease.lease_epoch;
287 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
288 &lease->lease.lease_key);
291 TALLOC_FREE(lease->timeout);
296 * Paranoia check: There can only be one fsp_lease per lease
299 SMB_ASSERT(fsp->lease == lease);
301 lck = get_existing_share_mode_lock(
302 talloc_tos(), fsp->file_id);
305 TALLOC_FREE(lease->timeout);
309 fsp_lease_update(fsp);
311 if (lease->lease.lease_epoch != old_epoch) {
313 * If the epoch changed we need to wait for
314 * the next timeout to happen.
316 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
322 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
324 * If the epoch changed we need to wait for
325 * the next timeout to happen.
327 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
333 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
335 (void)downgrade_lease(lease->sconn->client->connections,
338 &lease->lease.lease_key,
344 bool fsp_lease_update(struct files_struct *fsp)
346 const struct GUID *client_guid = fsp_client_guid(fsp);
347 struct fsp_lease *lease = fsp->lease;
348 uint32_t current_state;
350 uint16_t lease_version, epoch;
353 status = leases_db_get(client_guid,
354 &fsp->lease->lease.lease_key,
358 NULL, /* breaking_to_requested */
359 NULL, /* breaking_to_required */
362 if (!NT_STATUS_IS_OK(status)) {
363 DBG_WARNING("Could not find lease entry: %s\n",
365 TALLOC_FREE(lease->timeout);
366 lease->lease.lease_state = SMB2_LEASE_NONE;
367 lease->lease.lease_epoch += 1;
368 lease->lease.lease_flags = 0;
372 DEBUG(10,("%s: refresh lease state\n", __func__));
374 /* Ensure we're in sync with current lease state. */
375 if (lease->lease.lease_epoch != epoch) {
376 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
377 TALLOC_FREE(lease->timeout);
379 lease->lease.lease_epoch = epoch;
380 lease->lease.lease_state = current_state;
383 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
385 if (lease->timeout == NULL) {
386 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
388 DEBUG(10,("%s: setup timeout handler\n", __func__));
390 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
392 lease_timeout_handler,
394 if (lease->timeout == NULL) {
395 DEBUG(0, ("%s: Could not add lease timeout handler\n",
400 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
401 TALLOC_FREE(lease->timeout);
407 struct downgrade_lease_additional_state {
408 struct tevent_immediate *im;
409 struct smbXsrv_connection *xconn;
410 uint32_t break_flags;
411 struct smb2_lease_key lease_key;
417 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
418 struct tevent_immediate *im,
421 struct downgrade_lease_additional_state *state =
422 talloc_get_type_abort(private_data,
423 struct downgrade_lease_additional_state);
424 struct smbXsrv_connection *xconn = state->xconn;
427 status = smbd_smb2_send_lease_break(xconn,
434 if (!NT_STATUS_IS_OK(status)) {
435 smbd_server_connection_terminate(xconn,
441 struct fsps_lease_update_state {
442 const struct file_id *id;
443 const struct smb2_lease_key *key;
446 static struct files_struct *fsps_lease_update_fn(
447 struct files_struct *fsp, void *private_data)
449 struct fsps_lease_update_state *state =
450 (struct fsps_lease_update_state *)private_data;
452 if (fsp->oplock_type != LEASE_OPLOCK) {
455 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
458 if (!file_id_equal(&fsp->file_id, state->id)) {
462 fsp_lease_update(fsp);
467 static void fsps_lease_update(struct smbd_server_connection *sconn,
468 const struct file_id *id,
469 const struct smb2_lease_key *key)
471 struct fsps_lease_update_state state = { .id = id, .key = key };
472 files_forall(sconn, fsps_lease_update_fn, &state);
475 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
476 uint32_t num_file_ids,
477 const struct file_id *ids,
478 const struct smb2_lease_key *key,
479 uint32_t lease_state)
481 struct smbd_server_connection *sconn = xconn->client->sconn;
482 const struct GUID *client_guid = NULL;
483 struct share_mode_lock *lck;
484 const struct file_id id = ids[0];
485 uint32_t current_state, breaking_to_requested, breaking_to_required;
487 uint16_t lease_version, epoch;
491 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
492 file_id_string_tos(&id), (unsigned)lease_state));
494 lck = get_existing_share_mode_lock(talloc_tos(), id);
496 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
499 client_guid = &sconn->client->connections->smb2.client.guid;
501 status = leases_db_get(client_guid,
506 &breaking_to_requested,
507 &breaking_to_required,
510 if (!NT_STATUS_IS_OK(status)) {
511 DBG_WARNING("leases_db_get returned %s\n",
518 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
519 "but we're not in breaking state\n",
520 current_state, lease_state);
522 return NT_STATUS_UNSUCCESSFUL;
526 * Can't upgrade anything: breaking_to_requested (and current_state)
527 * must be a strict bitwise superset of new_lease_state
529 if ((lease_state & breaking_to_requested) != lease_state) {
530 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
531 "- expected %"PRIu32"\n",
532 current_state, lease_state,
533 breaking_to_requested);
535 return NT_STATUS_REQUEST_NOT_ACCEPTED;
538 if (current_state != lease_state) {
539 current_state = lease_state;
542 status = NT_STATUS_OK;
544 if ((lease_state & ~breaking_to_required) != 0) {
545 struct downgrade_lease_additional_state *state;
547 DBG_INFO("lease state %"PRIu32" not fully broken from "
548 "%"PRIu32" to %"PRIu32"\n",
551 breaking_to_required);
553 breaking_to_requested = breaking_to_required;
555 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
557 * Here we break in steps, as windows does
558 * see the breaking3 and v2_breaking3 tests.
560 breaking_to_requested |= SMB2_LEASE_READ;
563 state = talloc_zero(xconn,
564 struct downgrade_lease_additional_state);
567 return NT_STATUS_NO_MEMORY;
570 state->im = tevent_create_immediate(state);
571 if (state->im == NULL) {
574 return NT_STATUS_NO_MEMORY;
577 state->xconn = xconn;
578 state->lease_key = *key;
579 state->break_from = current_state;
580 state->break_to = breaking_to_requested;
581 if (lease_version > 1) {
582 state->new_epoch = epoch;
585 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
587 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
590 * This is an async break without
591 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
593 * we need to store NONE state in the
597 breaking_to_requested = 0;
598 breaking_to_required = 0;
604 set_status = leases_db_set(
605 &sconn->client->connections->
610 breaking_to_requested,
611 breaking_to_required,
615 if (!NT_STATUS_IS_OK(set_status)) {
616 DBG_DEBUG("leases_db_set failed: %s\n",
617 nt_errstr(set_status));
623 tevent_schedule_immediate(state->im,
624 xconn->client->raw_ev_ctx,
625 downgrade_lease_additional_trigger,
628 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
630 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
631 "expected %"PRIu32"\n",
634 breaking_to_requested);
636 breaking_to_requested = 0;
637 breaking_to_required = 0;
644 set_status = leases_db_set(
649 breaking_to_requested,
650 breaking_to_required,
654 if (!NT_STATUS_IS_OK(set_status)) {
655 DBG_DEBUG("leases_db_set failed: %s\n",
656 nt_errstr(set_status));
662 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
663 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
666 * No, we did not modify the share mode array. We did modify
667 * the leases_db. But without this we don't notify a lease
668 * break waiter via dbwrap_watch_record. We need to make
669 * leases_db watched too.
671 lck->data->modified = true;
673 fsps_lease_update(sconn, &id, key);
676 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
677 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
680 * Dynamic share case. Ensure other opens are copies.
681 * This will only be breaking to NONE.
684 for (i = 1; i < num_file_ids; i++) {
685 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
687 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
690 fsps_lease_update(sconn, &ids[i], key);
692 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
693 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
701 /****************************************************************************
702 Set up an oplock break message.
703 ****************************************************************************/
705 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
707 static void new_break_message_smb1(files_struct *fsp, int cmd,
708 char result[SMB1_BREAK_MESSAGE_LENGTH])
710 memset(result,'\0',smb_size);
711 srv_set_message(result,8,0,true);
712 SCVAL(result,smb_com,SMBlockingX);
713 SSVAL(result,smb_tid,fsp->conn->cnum);
714 SSVAL(result,smb_pid,0xFFFF);
715 SSVAL(result,smb_uid,0);
716 SSVAL(result,smb_mid,0xFFFF);
717 SCVAL(result,smb_vwv0,0xFF);
718 SSVAL(result,smb_vwv2,fsp->fnum);
719 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
720 SCVAL(result,smb_vwv3+1,cmd);
723 /****************************************************************************
724 Function to do the waiting before sending a local break.
725 ****************************************************************************/
727 static void wait_before_sending_break(void)
729 long wait_time = (long)lp_oplock_break_wait_time();
732 smb_msleep(wait_time);
736 /****************************************************************************
737 Ensure that we have a valid oplock.
738 ****************************************************************************/
740 static files_struct *initial_break_processing(
741 struct smbd_server_connection *sconn, struct file_id id,
742 unsigned long file_id)
744 files_struct *fsp = NULL;
746 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
747 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
748 file_id_string_tos(&id), (int)file_id,
749 sconn->oplocks.exclusive_open,
750 sconn->oplocks.level_II_open));
753 * We need to search the file open table for the
754 * entry containing this dev and inode, and ensure
755 * we have an oplock on it.
758 fsp = file_find_dif(sconn, id, file_id);
761 /* The file could have been closed in the meantime - return success. */
762 DEBUG(3, ("initial_break_processing: cannot find open file "
763 "with file_id %s gen_id = %lu, allowing break to "
764 "succeed.\n", file_id_string_tos(&id), file_id));
768 /* Ensure we have an oplock on the file */
771 * There is a potential race condition in that an oplock could
772 * have been broken due to another udp request, and yet there are
773 * still oplock break messages being sent in the udp message
774 * queue for this file. So return true if we don't have an oplock,
775 * as we may have just freed it.
778 if(fsp->oplock_type == NO_OPLOCK) {
779 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
780 "gen_id = %lu) has no oplock. Allowing break to "
781 "succeed regardless.\n", fsp_str_dbg(fsp),
782 file_id_string_tos(&id), fsp->fh->gen_id));
789 static void oplock_timeout_handler(struct tevent_context *ctx,
790 struct tevent_timer *te,
794 files_struct *fsp = (files_struct *)private_data;
796 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
798 /* Remove the timed event handler. */
799 TALLOC_FREE(fsp->oplock_timeout);
800 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
805 /*******************************************************************
806 Add a timeout handler waiting for the client reply.
807 *******************************************************************/
809 static void add_oplock_timeout_handler(files_struct *fsp)
811 if (fsp->oplock_timeout != NULL) {
812 DEBUG(0, ("Logic problem -- have an oplock event hanging "
816 fsp->oplock_timeout =
817 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
818 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
819 oplock_timeout_handler, fsp);
821 if (fsp->oplock_timeout == NULL) {
822 DEBUG(0, ("Could not add oplock timeout handler\n"));
826 static void send_break_message_smb1(files_struct *fsp, int level)
828 struct smbXsrv_connection *xconn = NULL;
829 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
832 * For SMB1 we only have one connection
834 xconn = fsp->conn->sconn->client->connections;
836 new_break_message_smb1(fsp, level, break_msg);
839 if (!srv_send_smb(xconn,
841 IS_CONN_ENCRYPTED(fsp->conn),
843 exit_server_cleanly("send_break_message_smb1: "
844 "srv_send_smb failed.");
848 /*******************************************************************
849 This handles the generic oplock break message from another smbd.
850 *******************************************************************/
852 static void process_oplock_break_message(struct messaging_context *msg_ctx,
855 struct server_id src,
858 struct oplock_break_message *msg = NULL;
859 enum ndr_err_code ndr_err;
862 struct smbd_server_connection *sconn =
863 talloc_get_type_abort(private_data,
864 struct smbd_server_connection);
865 struct server_id self = messaging_server_id(sconn->msg_ctx);
866 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
869 bool break_needed = true;
871 msg = talloc(talloc_tos(), struct oplock_break_message);
873 DBG_WARNING("talloc failed\n");
877 ndr_err = ndr_pull_struct_blob_all(
881 (ndr_pull_flags_fn_t)ndr_pull_oplock_break_message);
882 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
883 DBG_DEBUG("ndr_pull_oplock_break_message failed: %s\n",
884 ndr_errstr(ndr_err));
888 if (DEBUGLEVEL >= 10) {
889 struct server_id_buf buf;
890 DBG_DEBUG("Got break message from %s\n",
891 server_id_str_buf(src, &buf));
892 NDR_PRINT_DEBUG(oplock_break_message, msg);
895 break_to = msg->break_to;
896 fsp = initial_break_processing(sconn, msg->id, msg->share_file_id);
901 /* We hit a race here. Break messages are sent, and before we
902 * get to process this message, we have closed the file. */
903 DEBUG(3, ("Did not find fsp\n"));
907 break_from = fsp_lease_type(fsp);
909 if (fsp->oplock_type != LEASE_OPLOCK) {
910 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
912 * Nothing to do anymore
914 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
915 fsp->sent_oplock_break));
920 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
921 DEBUG(10, ("client_caps without level2 oplocks\n"));
922 break_to &= ~SMB2_LEASE_READ;
925 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
928 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
929 break_to &= ~SMB2_LEASE_READ;
932 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
933 DEBUG(10, ("no level2 oplocks by config\n"));
934 break_to &= ~SMB2_LEASE_READ;
937 if (fsp->oplock_type == LEASE_OPLOCK) {
938 const struct GUID *client_guid = fsp_client_guid(fsp);
939 struct share_mode_lock *lck;
940 uint32_t current_state;
941 uint32_t breaking_to_requested, breaking_to_required;
943 uint16_t lease_version, epoch;
946 lck = get_existing_share_mode_lock(
947 talloc_tos(), fsp->file_id);
950 * We hit a race here. Break messages are sent, and
951 * before we get to process this message, we have closed
954 DEBUG(3, ("Did not find share_mode\n"));
958 status = leases_db_get(client_guid,
959 &fsp->lease->lease.lease_key,
963 &breaking_to_requested,
964 &breaking_to_required,
967 if (!NT_STATUS_IS_OK(status)) {
968 DBG_WARNING("leases_db_get returned %s\n",
974 break_from = current_state;
975 break_to &= current_state;
978 break_to &= breaking_to_required;
979 if (breaking_to_required != break_to) {
981 * Note we don't increment the epoch
982 * here, which might be a bug in
985 breaking_to_required = break_to;
987 break_needed = false;
988 } else if (current_state == break_to) {
989 break_needed = false;
990 } else if (current_state == SMB2_LEASE_READ) {
991 current_state = SMB2_LEASE_NONE;
992 /* Need to increment the epoch */
996 breaking_to_required = break_to;
997 breaking_to_requested = break_to;
998 /* Need to increment the epoch */
1003 NTSTATUS set_status;
1005 set_status = leases_db_set(
1007 &fsp->lease->lease.lease_key,
1010 breaking_to_requested,
1011 breaking_to_required,
1015 if (!NT_STATUS_IS_OK(set_status)) {
1016 DBG_DEBUG("leases_db_set failed: %s\n",
1017 nt_errstr(set_status));
1022 /* Ensure we're in sync with current lease state. */
1023 fsp_lease_update(fsp);
1028 if (!break_needed) {
1029 DEBUG(10,("%s: skip break\n", __func__));
1033 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1034 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1035 file_id_string_tos(&fsp->file_id),
1040 DEBUG(10, ("break_from=%u, break_to=%u\n",
1041 (unsigned)break_from, (unsigned)break_to));
1043 if ((break_from == break_to) && !break_needed) {
1044 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1046 file_id_string_tos(&fsp->file_id),
1051 /* Need to wait before sending a break
1052 message if we sent ourselves this message. */
1053 if (serverid_equal(&self, &src)) {
1054 wait_before_sending_break();
1057 if (sconn->using_smb2) {
1058 send_break_message_smb2(fsp, break_from, break_to);
1060 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1061 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1064 if ((break_from == SMB2_LEASE_READ) &&
1065 (break_to == SMB2_LEASE_NONE)) {
1067 * This is an async break without a reply and thus no timeout
1069 * leases are handled above.
1071 if (fsp->oplock_type != LEASE_OPLOCK) {
1076 if (fsp->oplock_type == LEASE_OPLOCK) {
1080 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1081 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1083 add_oplock_timeout_handler(fsp);
1086 /*******************************************************************
1087 This handles the kernel oplock break message.
1088 *******************************************************************/
1090 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1093 struct server_id src,
1097 unsigned long file_id;
1099 struct smbd_server_connection *sconn =
1100 talloc_get_type_abort(private_data,
1101 struct smbd_server_connection);
1102 struct server_id_buf tmp;
1104 if (data->data == NULL) {
1105 DEBUG(0, ("Got NULL buffer\n"));
1109 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1110 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1114 /* Pull the data from the message. */
1115 pull_file_id_24((char *)data->data, &id);
1116 file_id = (unsigned long)IVAL(data->data, 24);
1118 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1119 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1120 (unsigned int)file_id));
1122 fsp = initial_break_processing(sconn, id, file_id);
1125 DEBUG(3, ("Got a kernel oplock break message for a file "
1126 "I don't know about\n"));
1130 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1131 /* This is ok, kernel oplocks come in completely async */
1132 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1137 if (sconn->using_smb2) {
1138 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1140 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1143 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1145 add_oplock_timeout_handler(fsp);
1148 static void send_break_to_none(struct messaging_context *msg_ctx,
1149 const struct file_id *id,
1150 const struct share_mode_entry *e)
1153 status = send_break_message(msg_ctx, id, e, OPLOCK_NONE);
1154 if (!NT_STATUS_IS_OK(status)) {
1155 DBG_DEBUG("send_break_message failed: %s\n",
1159 struct break_to_none_state {
1160 struct smbd_server_connection *sconn;
1162 struct smb2_lease_key lease_key;
1163 struct GUID client_guid;
1167 static bool do_break_lease_to_none(struct share_mode_lock *lck,
1168 struct share_mode_entry *e,
1171 struct break_to_none_state *state = private_data;
1172 uint32_t current_state = 0;
1176 DBG_DEBUG("lease_key=%"PRIu64"/%"PRIu64"\n",
1177 e->lease_key.data[0],
1178 e->lease_key.data[1]);
1180 status = leases_db_get(&e->client_guid,
1184 NULL, /* breaking */
1185 NULL, /* breaking_to_requested */
1186 NULL, /* breaking_to_required */
1187 NULL, /* lease_version */
1189 if (!NT_STATUS_IS_OK(status)) {
1190 DBG_WARNING("leases_db_get failed: %s\n",
1195 if ((current_state & SMB2_LEASE_READ) == 0) {
1199 our_own = smb2_lease_equal(&state->client_guid,
1204 DEBUG(10, ("Don't break our own lease\n"));
1208 DBG_DEBUG("Breaking %"PRIu64"/%"PRIu64" to none\n",
1209 e->lease_key.data[0],
1210 e->lease_key.data[1]);
1212 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1214 state->num_broken += 1;
1219 /****************************************************************************
1220 This function is called on any file modification or lock request. If a file
1221 is level 2 oplocked then it must tell all other level 2 holders to break to
1223 ****************************************************************************/
1225 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1226 enum level2_contention_type type)
1228 struct break_to_none_state state = {
1229 .sconn = fsp->conn->sconn, .id = fsp->file_id,
1231 struct share_mode_lock *lck = NULL;
1232 struct share_mode_data *d = NULL;
1233 bool ok, has_read_lease;
1237 * If this file is level II oplocked then we need
1238 * to grab the shared memory lock and inform all
1239 * other files with a level II lock that they need
1240 * to flush their read caches. We keep the lock over
1241 * the shared memory area whilst doing this.
1244 if (fsp_lease_type_is_exclusive(fsp)) {
1246 * There can't be any level2 oplocks, we're alone.
1251 has_read_lease = file_has_read_lease(fsp);
1252 if (!has_read_lease) {
1253 DEBUG(10, ("No read oplocks around\n"));
1257 if (fsp->oplock_type == LEASE_OPLOCK) {
1258 state.client_guid = *fsp_client_guid(fsp);
1259 state.lease_key = fsp->lease->lease.lease_key;
1260 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1261 state.lease_key.data[0],
1262 state.lease_key.data[1]));
1265 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1267 DBG_WARNING("failed to lock share mode entry for file %s.\n",
1268 file_id_string_tos(&state.id));
1274 * Walk leases and oplocks separately: We have to send one break per
1275 * lease. If we have multiple share_mode_entry having a common lease,
1276 * we would break the lease twice if we don't walk the leases list
1280 ok = share_mode_forall_leases(lck, do_break_lease_to_none, &state);
1282 DBG_WARNING("share_mode_forall_leases failed\n");
1285 for(i = 0; i < d->num_share_modes; i++) {
1286 struct share_mode_entry *e = &d->share_modes[i];
1288 if (!is_valid_share_mode_entry(e)) {
1291 if (e->op_type == LEASE_OPLOCK) {
1293 * Took care of those in the loop above
1299 * As there could have been multiple writes waiting at the
1300 * lock_share_entry gate we may not be the first to
1301 * enter. Hence the state of the op_types in the share mode
1302 * entries may be partly NO_OPLOCK and partly LEVEL_II
1303 * oplock. It will do no harm to re-send break messages to
1304 * those smbd's that are still waiting their turn to remove
1305 * their LEVEL_II state, and also no harm to ignore existing
1306 * NO_OPLOCK states. JRA.
1309 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1312 if (e->op_type == NO_OPLOCK) {
1317 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1318 DEBUG(0,("%s: PANIC. "
1319 "share mode entry %d is an exclusive "
1320 "oplock !\n", __func__, i ));
1325 send_break_to_none(state.sconn->msg_ctx, &state.id, e);
1326 state.num_broken += 1;
1329 if (state.num_broken == 0) {
1331 * Lazy update here. It might be that the read lease
1332 * has gone in the meantime.
1334 d->flags &= ~SHARE_MODE_HAS_READ_LEASE;
1341 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1342 enum level2_contention_type type)
1344 contend_level2_oplocks_begin_default(fsp, type);
1347 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1348 enum level2_contention_type type)
1353 /****************************************************************************
1354 Linearize a share mode entry struct to an internal oplock break message.
1355 ****************************************************************************/
1357 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1358 const struct share_mode_entry *e)
1360 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1361 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1362 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1363 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1364 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1365 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1366 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1367 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1369 * "id" used to be part of share_mode_entry, thus the strange
1370 * place to put this. Feel free to move somewhere else :-)
1372 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1373 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1374 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1375 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1376 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1377 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1380 /****************************************************************************
1381 De-linearize an internal oplock break message to a share mode entry struct.
1382 ****************************************************************************/
1384 void message_to_share_mode_entry(struct file_id *id,
1385 struct share_mode_entry *e,
1388 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1389 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1390 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1391 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1392 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1393 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1394 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1395 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1397 * "id" used to be part of share_mode_entry, thus the strange
1398 * place to put this. Feel free to move somewhere else :-)
1400 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1401 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1402 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1403 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1404 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1405 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1408 /****************************************************************************
1409 Setup oplocks for this process.
1410 ****************************************************************************/
1412 bool init_oplocks(struct smbd_server_connection *sconn)
1414 DEBUG(3,("init_oplocks: initializing messages.\n"));
1416 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1417 process_oplock_break_message);
1418 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1419 process_kernel_oplock_break);
1423 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1425 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1427 /* only initialize once */
1428 if (koplocks == NULL) {
1429 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1430 koplocks = linux_init_kernel_oplocks(sconn);
1432 sconn->oplocks.kernel_ops = koplocks;