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 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
200 struct share_mode_data *d = lck->data;
201 struct byte_range_lock *br_lck;
202 uint32_t num_read_oplocks = 0;
205 if (fsp_lease_type_is_exclusive(fsp)) {
206 const struct share_mode_entry *e = NULL;
207 uint32_t e_lease_type = 0;
210 * If we're fully exclusive, we don't need a brlock entry
212 remove_stale_share_mode_entries(d);
214 e = find_share_mode_entry(lck, fsp);
216 e_lease_type = get_lease_type(d, e);
219 if (!lease_type_is_exclusive(e_lease_type)) {
220 char *timestr = NULL;
222 timestr = timeval_string(talloc_tos(),
226 NDR_PRINT_DEBUG(share_mode_data, d);
227 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
228 "open_time[%s] lease_type [0x%x] "
229 "oplock_type [0x%x]\n",
231 file_id_string_tos(&fsp->file_id),
232 fsp->fh->gen_id, timestr,
233 e_lease_type, fsp->oplock_type);
235 smb_panic("Found non-exclusive lease");
241 for (i=0; i<d->num_share_modes; i++) {
242 struct share_mode_entry *e = &d->share_modes[i];
243 uint32_t e_lease_type = get_lease_type(d, e);
245 if (e_lease_type & SMB2_LEASE_READ) {
246 num_read_oplocks += 1;
250 br_lck = brl_get_locks_readonly(fsp);
251 if (br_lck == NULL) {
254 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
258 br_lck = brl_get_locks(talloc_tos(), fsp);
259 if (br_lck == NULL) {
262 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
267 /****************************************************************************
268 Remove a file oplock with lock already held. Copes with level II and exclusive.
269 ****************************************************************************/
271 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
275 ret = remove_share_oplock(lck, fsp);
277 DBG_ERR("failed to remove share oplock for "
279 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
280 file_id_string_tos(&fsp->file_id));
282 release_file_oplock(fsp);
284 ret = update_num_read_oplocks(fsp, lck);
286 DBG_ERR("update_num_read_oplocks failed for "
288 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
289 file_id_string_tos(&fsp->file_id));
295 /****************************************************************************
296 Remove a file oplock. Copes with level II and exclusive.
297 Locks then unlocks the share mode lock. Client can decide to go directly
298 to none even if a "break-to-level II" was sent.
299 ****************************************************************************/
301 bool remove_oplock(files_struct *fsp)
304 struct share_mode_lock *lck;
306 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
308 /* Remove the oplock flag from the sharemode. */
309 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
311 DBG_ERR("failed to lock share entry for "
312 "file %s\n", fsp_str_dbg(fsp));
316 ret = remove_oplock_under_lock(fsp, lck);
323 * Deal with a reply when a break-to-level II was sent.
325 bool downgrade_oplock(files_struct *fsp)
328 struct share_mode_lock *lck;
330 DEBUG(10, ("downgrade_oplock called for %s\n",
333 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
335 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
336 "file %s\n", fsp_str_dbg(fsp)));
339 ret = downgrade_share_oplock(lck, fsp);
341 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
342 "for file %s, %s, file_id %s\n",
343 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
344 file_id_string_tos(&fsp->file_id)));
346 downgrade_file_oplock(fsp);
348 ret = update_num_read_oplocks(fsp, lck);
350 DEBUG(0, ("%s: update_num_read_oplocks failed for "
352 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
353 file_id_string_tos(&fsp->file_id)));
360 static void lease_timeout_handler(struct tevent_context *ctx,
361 struct tevent_timer *te,
365 struct fsp_lease *lease =
366 talloc_get_type_abort(private_data,
368 struct files_struct *fsp;
369 struct share_mode_lock *lck;
370 uint16_t old_epoch = lease->lease.lease_epoch;
372 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
373 &lease->lease.lease_key);
376 TALLOC_FREE(lease->timeout);
381 * Paranoia check: There can only be one fsp_lease per lease
384 SMB_ASSERT(fsp->lease == lease);
386 lck = get_existing_share_mode_lock(
387 talloc_tos(), fsp->file_id);
390 TALLOC_FREE(lease->timeout);
394 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
396 if (lease->lease.lease_epoch != old_epoch) {
398 * If the epoch changed we need to wait for
399 * the next timeout to happen.
401 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
407 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
409 * If the epoch changed we need to wait for
410 * the next timeout to happen.
412 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
418 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
420 (void)downgrade_lease(lease->sconn->client->connections,
423 &lease->lease.lease_key,
429 bool fsp_lease_update(struct share_mode_lock *lck,
430 const struct GUID *client_guid,
431 struct fsp_lease *lease)
433 uint32_t current_state;
435 uint16_t lease_version, epoch;
438 status = leases_db_get(client_guid,
439 &lease->lease.lease_key,
443 NULL, /* breaking_to_requested */
444 NULL, /* breaking_to_required */
447 if (!NT_STATUS_IS_OK(status)) {
448 DBG_WARNING("Could not find lease entry: %s\n",
450 TALLOC_FREE(lease->timeout);
451 lease->lease.lease_state = SMB2_LEASE_NONE;
452 lease->lease.lease_epoch += 1;
453 lease->lease.lease_flags = 0;
457 DEBUG(10,("%s: refresh lease state\n", __func__));
459 /* Ensure we're in sync with current lease state. */
460 if (lease->lease.lease_epoch != epoch) {
461 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
462 TALLOC_FREE(lease->timeout);
464 lease->lease.lease_epoch = epoch;
465 lease->lease.lease_state = current_state;
468 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
470 if (lease->timeout == NULL) {
471 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
473 DEBUG(10,("%s: setup timeout handler\n", __func__));
475 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
477 lease_timeout_handler,
479 if (lease->timeout == NULL) {
480 DEBUG(0, ("%s: Could not add lease timeout handler\n",
485 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
486 TALLOC_FREE(lease->timeout);
492 struct downgrade_lease_additional_state {
493 struct tevent_immediate *im;
494 struct smbXsrv_connection *xconn;
495 uint32_t break_flags;
496 struct smb2_lease_key lease_key;
502 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
503 struct tevent_immediate *im,
506 struct downgrade_lease_additional_state *state =
507 talloc_get_type_abort(private_data,
508 struct downgrade_lease_additional_state);
509 struct smbXsrv_connection *xconn = state->xconn;
512 status = smbd_smb2_send_lease_break(xconn,
519 if (!NT_STATUS_IS_OK(status)) {
520 smbd_server_connection_terminate(xconn,
526 struct downgrade_lease_fsps_state {
528 struct share_mode_lock *lck;
529 const struct smb2_lease_key *key;
532 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
535 struct downgrade_lease_fsps_state *state =
536 (struct downgrade_lease_fsps_state *)private_data;
538 if (fsp->oplock_type != LEASE_OPLOCK) {
541 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
544 if (!file_id_equal(&fsp->file_id, &state->id)) {
548 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
553 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
554 uint32_t num_file_ids,
555 const struct file_id *ids,
556 const struct smb2_lease_key *key,
557 uint32_t lease_state)
559 struct smbd_server_connection *sconn = xconn->client->sconn;
560 const struct GUID *client_guid = NULL;
561 struct share_mode_lock *lck;
562 const struct file_id id = ids[0];
563 uint32_t current_state, breaking_to_requested, breaking_to_required;
565 uint16_t lease_version, epoch;
569 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
570 file_id_string_tos(&id), (unsigned)lease_state));
572 lck = get_existing_share_mode_lock(talloc_tos(), id);
574 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
577 client_guid = &sconn->client->connections->smb2.client.guid;
579 status = leases_db_get(client_guid,
584 &breaking_to_requested,
585 &breaking_to_required,
588 if (!NT_STATUS_IS_OK(status)) {
589 DBG_WARNING("leases_db_get returned %s\n",
596 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
597 "but we're not in breaking state\n",
598 current_state, lease_state);
600 return NT_STATUS_UNSUCCESSFUL;
604 * Can't upgrade anything: breaking_to_requested (and current_state)
605 * must be a strict bitwise superset of new_lease_state
607 if ((lease_state & breaking_to_requested) != lease_state) {
608 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
609 "- expected %"PRIu32"\n",
610 current_state, lease_state,
611 breaking_to_requested);
613 return NT_STATUS_REQUEST_NOT_ACCEPTED;
616 if (current_state != lease_state) {
617 current_state = lease_state;
620 status = NT_STATUS_OK;
622 if ((lease_state & ~breaking_to_required) != 0) {
623 struct downgrade_lease_additional_state *state;
625 DBG_INFO("lease state %"PRIu32" not fully broken from "
626 "%"PRIu32" to %"PRIu32"\n",
629 breaking_to_required);
631 breaking_to_requested = breaking_to_required;
633 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
635 * Here we break in steps, as windows does
636 * see the breaking3 and v2_breaking3 tests.
638 breaking_to_requested |= SMB2_LEASE_READ;
641 state = talloc_zero(xconn,
642 struct downgrade_lease_additional_state);
645 return NT_STATUS_NO_MEMORY;
648 state->im = tevent_create_immediate(state);
649 if (state->im == NULL) {
652 return NT_STATUS_NO_MEMORY;
655 state->xconn = xconn;
656 state->lease_key = *key;
657 state->break_from = current_state;
658 state->break_to = breaking_to_requested;
659 if (lease_version > 1) {
660 state->new_epoch = epoch;
663 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
665 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
668 * This is an async break without
669 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
671 * we need to store NONE state in the
675 breaking_to_requested = 0;
676 breaking_to_required = 0;
682 set_status = leases_db_set(
683 &sconn->client->connections->
688 breaking_to_requested,
689 breaking_to_required,
693 if (!NT_STATUS_IS_OK(set_status)) {
694 DBG_DEBUG("leases_db_set failed: %s\n",
695 nt_errstr(set_status));
701 tevent_schedule_immediate(state->im,
702 xconn->client->raw_ev_ctx,
703 downgrade_lease_additional_trigger,
706 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
708 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
709 "expected %"PRIu32"\n",
712 breaking_to_requested);
714 breaking_to_requested = 0;
715 breaking_to_required = 0;
722 set_status = leases_db_set(
727 breaking_to_requested,
728 breaking_to_required,
732 if (!NT_STATUS_IS_OK(set_status)) {
733 DBG_DEBUG("leases_db_set failed: %s\n",
734 nt_errstr(set_status));
740 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
741 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
744 * No, we did not modify the share mode array. We did modify
745 * the leases_db. But without this we don't notify a lease
746 * break waiter via dbwrap_watch_record. We need to make
747 * leases_db watched too.
749 lck->data->modified = true;
752 struct downgrade_lease_fsps_state state = {
753 .id = id, .lck = lck, .key = key,
756 files_forall(sconn, downgrade_lease_fsps, &state);
760 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
761 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
764 * Dynamic share case. Ensure other opens are copies.
765 * This will only be breaking to NONE.
768 for (i = 1; i < num_file_ids; i++) {
769 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
771 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
775 struct downgrade_lease_fsps_state state = {
776 .id = ids[i], .lck = lck, .key = key,
779 files_forall(sconn, downgrade_lease_fsps, &state);
782 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
783 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
791 /****************************************************************************
792 Set up an oplock break message.
793 ****************************************************************************/
795 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
797 static void new_break_message_smb1(files_struct *fsp, int cmd,
798 char result[SMB1_BREAK_MESSAGE_LENGTH])
800 memset(result,'\0',smb_size);
801 srv_set_message(result,8,0,true);
802 SCVAL(result,smb_com,SMBlockingX);
803 SSVAL(result,smb_tid,fsp->conn->cnum);
804 SSVAL(result,smb_pid,0xFFFF);
805 SSVAL(result,smb_uid,0);
806 SSVAL(result,smb_mid,0xFFFF);
807 SCVAL(result,smb_vwv0,0xFF);
808 SSVAL(result,smb_vwv2,fsp->fnum);
809 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
810 SCVAL(result,smb_vwv3+1,cmd);
813 /****************************************************************************
814 Function to do the waiting before sending a local break.
815 ****************************************************************************/
817 static void wait_before_sending_break(void)
819 long wait_time = (long)lp_oplock_break_wait_time();
822 smb_msleep(wait_time);
826 /****************************************************************************
827 Ensure that we have a valid oplock.
828 ****************************************************************************/
830 static files_struct *initial_break_processing(
831 struct smbd_server_connection *sconn, struct file_id id,
832 unsigned long file_id)
834 files_struct *fsp = NULL;
836 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
837 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
838 file_id_string_tos(&id), (int)file_id,
839 sconn->oplocks.exclusive_open,
840 sconn->oplocks.level_II_open));
843 * We need to search the file open table for the
844 * entry containing this dev and inode, and ensure
845 * we have an oplock on it.
848 fsp = file_find_dif(sconn, id, file_id);
851 /* The file could have been closed in the meantime - return success. */
852 DEBUG(3, ("initial_break_processing: cannot find open file "
853 "with file_id %s gen_id = %lu, allowing break to "
854 "succeed.\n", file_id_string_tos(&id), file_id));
858 /* Ensure we have an oplock on the file */
861 * There is a potential race condition in that an oplock could
862 * have been broken due to another udp request, and yet there are
863 * still oplock break messages being sent in the udp message
864 * queue for this file. So return true if we don't have an oplock,
865 * as we may have just freed it.
868 if(fsp->oplock_type == NO_OPLOCK) {
869 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
870 "gen_id = %lu) has no oplock. Allowing break to "
871 "succeed regardless.\n", fsp_str_dbg(fsp),
872 file_id_string_tos(&id), fsp->fh->gen_id));
879 static void oplock_timeout_handler(struct tevent_context *ctx,
880 struct tevent_timer *te,
884 files_struct *fsp = (files_struct *)private_data;
886 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
888 /* Remove the timed event handler. */
889 TALLOC_FREE(fsp->oplock_timeout);
890 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
895 /*******************************************************************
896 Add a timeout handler waiting for the client reply.
897 *******************************************************************/
899 static void add_oplock_timeout_handler(files_struct *fsp)
901 if (fsp->oplock_timeout != NULL) {
902 DEBUG(0, ("Logic problem -- have an oplock event hanging "
906 fsp->oplock_timeout =
907 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
908 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
909 oplock_timeout_handler, fsp);
911 if (fsp->oplock_timeout == NULL) {
912 DEBUG(0, ("Could not add oplock timeout handler\n"));
916 static void send_break_message_smb1(files_struct *fsp, int level)
918 struct smbXsrv_connection *xconn = NULL;
919 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
922 * For SMB1 we only have one connection
924 xconn = fsp->conn->sconn->client->connections;
926 new_break_message_smb1(fsp, level, break_msg);
929 if (!srv_send_smb(xconn,
931 IS_CONN_ENCRYPTED(fsp->conn),
933 exit_server_cleanly("send_break_message_smb1: "
934 "srv_send_smb failed.");
938 /*******************************************************************
939 This handles the generic oplock break message from another smbd.
940 *******************************************************************/
942 static void process_oplock_break_message(struct messaging_context *msg_ctx,
945 struct server_id src,
949 struct share_mode_entry msg;
952 struct smbd_server_connection *sconn =
953 talloc_get_type_abort(private_data,
954 struct smbd_server_connection);
955 struct server_id self = messaging_server_id(sconn->msg_ctx);
956 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
959 bool break_needed = true;
960 struct server_id_buf tmp;
962 if (data->data == NULL) {
963 DEBUG(0, ("Got NULL buffer\n"));
967 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
968 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
972 /* De-linearize incoming message. */
973 message_to_share_mode_entry(&id, &msg, (char *)data->data);
974 break_to = msg.op_type;
976 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
977 (unsigned)break_to, server_id_str_buf(src, &tmp),
978 file_id_string_tos(&id),
979 (unsigned long long)msg.share_file_id));
981 fsp = initial_break_processing(sconn, id, msg.share_file_id);
984 /* We hit a race here. Break messages are sent, and before we
985 * get to process this message, we have closed the file. */
986 DEBUG(3, ("Did not find fsp\n"));
990 break_from = fsp_lease_type(fsp);
992 if (fsp->oplock_type != LEASE_OPLOCK) {
993 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
995 * Nothing to do anymore
997 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
998 fsp->sent_oplock_break));
1003 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
1004 DEBUG(10, ("client_caps without level2 oplocks\n"));
1005 break_to &= ~SMB2_LEASE_READ;
1008 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
1011 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
1012 break_to &= ~SMB2_LEASE_READ;
1015 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
1016 DEBUG(10, ("no level2 oplocks by config\n"));
1017 break_to &= ~SMB2_LEASE_READ;
1020 if (fsp->oplock_type == LEASE_OPLOCK) {
1021 const struct GUID *client_guid = fsp_client_guid(fsp);
1022 struct share_mode_lock *lck;
1023 uint32_t current_state;
1024 uint32_t breaking_to_requested, breaking_to_required;
1026 uint16_t lease_version, epoch;
1029 lck = get_existing_share_mode_lock(
1030 talloc_tos(), fsp->file_id);
1033 * We hit a race here. Break messages are sent, and
1034 * before we get to process this message, we have closed
1037 DEBUG(3, ("Did not find share_mode\n"));
1041 status = leases_db_get(client_guid,
1042 &fsp->lease->lease.lease_key,
1046 &breaking_to_requested,
1047 &breaking_to_required,
1050 if (!NT_STATUS_IS_OK(status)) {
1051 DBG_WARNING("leases_db_get returned %s\n",
1057 break_from = current_state;
1058 break_to &= current_state;
1061 break_to &= breaking_to_required;
1062 if (breaking_to_required != break_to) {
1064 * Note we don't increment the epoch
1065 * here, which might be a bug in
1068 breaking_to_required = break_to;
1070 break_needed = false;
1071 } else if (current_state == break_to) {
1072 break_needed = false;
1073 } else if (current_state == SMB2_LEASE_READ) {
1074 current_state = SMB2_LEASE_NONE;
1075 /* Need to increment the epoch */
1079 breaking_to_required = break_to;
1080 breaking_to_requested = break_to;
1081 /* Need to increment the epoch */
1086 NTSTATUS set_status;
1088 set_status = leases_db_set(
1090 &fsp->lease->lease.lease_key,
1093 breaking_to_requested,
1094 breaking_to_required,
1098 if (!NT_STATUS_IS_OK(set_status)) {
1099 DBG_DEBUG("leases_db_set failed: %s\n",
1100 nt_errstr(set_status));
1105 /* Ensure we're in sync with current lease state. */
1106 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
1111 if (!break_needed) {
1112 DEBUG(10,("%s: skip break\n", __func__));
1116 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1117 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1118 file_id_string_tos(&fsp->file_id),
1123 DEBUG(10, ("break_from=%u, break_to=%u\n",
1124 (unsigned)break_from, (unsigned)break_to));
1126 if ((break_from == break_to) && !break_needed) {
1127 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1129 file_id_string_tos(&fsp->file_id),
1134 /* Need to wait before sending a break
1135 message if we sent ourselves this message. */
1136 if (serverid_equal(&self, &src)) {
1137 wait_before_sending_break();
1140 if (sconn->using_smb2) {
1141 send_break_message_smb2(fsp, break_from, break_to);
1143 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1144 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1147 if ((break_from == SMB2_LEASE_READ) &&
1148 (break_to == SMB2_LEASE_NONE)) {
1150 * This is an async break without a reply and thus no timeout
1152 * leases are handled above.
1154 if (fsp->oplock_type != LEASE_OPLOCK) {
1159 if (fsp->oplock_type == LEASE_OPLOCK) {
1163 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1164 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1166 add_oplock_timeout_handler(fsp);
1169 /*******************************************************************
1170 This handles the kernel oplock break message.
1171 *******************************************************************/
1173 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1176 struct server_id src,
1180 unsigned long file_id;
1182 struct smbd_server_connection *sconn =
1183 talloc_get_type_abort(private_data,
1184 struct smbd_server_connection);
1185 struct server_id_buf tmp;
1187 if (data->data == NULL) {
1188 DEBUG(0, ("Got NULL buffer\n"));
1192 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1193 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1197 /* Pull the data from the message. */
1198 pull_file_id_24((char *)data->data, &id);
1199 file_id = (unsigned long)IVAL(data->data, 24);
1201 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1202 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1203 (unsigned int)file_id));
1205 fsp = initial_break_processing(sconn, id, file_id);
1208 DEBUG(3, ("Got a kernel oplock break message for a file "
1209 "I don't know about\n"));
1213 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1214 /* This is ok, kernel oplocks come in completely async */
1215 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1220 if (sconn->using_smb2) {
1221 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1223 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1226 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1228 add_oplock_timeout_handler(fsp);
1231 static bool file_has_read_oplocks(struct files_struct *fsp)
1233 struct byte_range_lock *brl;
1234 uint32_t num_read_oplocks = 0;
1236 brl = brl_get_locks_readonly(fsp);
1241 num_read_oplocks = brl_num_read_oplocks(brl);
1243 DBG_DEBUG("num_read_oplocks = %"PRIu32"\n", num_read_oplocks);
1245 return (num_read_oplocks != 0);
1248 struct break_to_none_state {
1249 struct smbd_server_connection *sconn;
1251 struct smb2_lease_key lease_key;
1252 struct GUID client_guid;
1254 static void do_break_to_none(struct tevent_context *ctx,
1255 struct tevent_immediate *im,
1256 void *private_data);
1258 /****************************************************************************
1259 This function is called on any file modification or lock request. If a file
1260 is level 2 oplocked then it must tell all other level 2 holders to break to
1262 ****************************************************************************/
1264 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1265 enum level2_contention_type type)
1267 struct smbd_server_connection *sconn = fsp->conn->sconn;
1268 struct tevent_immediate *im;
1269 struct break_to_none_state *state;
1270 bool has_read_oplocks;
1273 * If this file is level II oplocked then we need
1274 * to grab the shared memory lock and inform all
1275 * other files with a level II lock that they need
1276 * to flush their read caches. We keep the lock over
1277 * the shared memory area whilst doing this.
1280 if (fsp_lease_type_is_exclusive(fsp)) {
1282 * There can't be any level2 oplocks, we're alone.
1287 has_read_oplocks = file_has_read_oplocks(fsp);
1288 if (!has_read_oplocks) {
1289 DEBUG(10, ("No read oplocks around\n"));
1294 * When we get here we might have a brlock entry locked. Also
1295 * locking the share mode entry would violate the locking
1296 * order. Breaking level2 oplocks to none is asynchronous
1297 * anyway, so we postpone this into an immediate event.
1300 state = talloc(sconn, struct break_to_none_state);
1301 if (state == NULL) {
1302 DEBUG(1, ("talloc failed\n"));
1305 *state = (struct break_to_none_state) {
1306 .sconn = sconn, .id = fsp->file_id,
1309 if (fsp->oplock_type == LEASE_OPLOCK) {
1310 state->client_guid = *fsp_client_guid(fsp);
1311 state->lease_key = fsp->lease->lease.lease_key;
1312 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1313 state->lease_key.data[0],
1314 state->lease_key.data[1]));
1317 im = tevent_create_immediate(state);
1319 DEBUG(1, ("tevent_create_immediate failed\n"));
1323 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1326 static void send_break_to_none(struct messaging_context *msg_ctx,
1327 const struct file_id *id,
1328 const struct share_mode_entry *e)
1330 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1332 share_mode_entry_to_message(msg, id, e);
1333 /* Overload entry->op_type */
1334 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1336 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1337 (uint8_t *)msg, sizeof(msg));
1340 static bool do_break_lease_to_none(struct share_mode_lock *lck,
1341 struct share_mode_entry *e,
1344 struct break_to_none_state *state = talloc_get_type_abort(
1345 private_data, struct break_to_none_state);
1346 uint32_t current_state = 0;
1350 DBG_DEBUG("lease_key=%"PRIu64"/%"PRIu64"\n",
1351 e->lease_key.data[0],
1352 e->lease_key.data[1]);
1354 status = leases_db_get(&e->client_guid,
1358 NULL, /* breaking */
1359 NULL, /* breaking_to_requested */
1360 NULL, /* breaking_to_required */
1361 NULL, /* lease_version */
1363 if (!NT_STATUS_IS_OK(status)) {
1364 DBG_WARNING("leases_db_get failed: %s\n",
1369 if ((current_state & SMB2_LEASE_READ) == 0) {
1373 our_own = smb2_lease_equal(&state->client_guid,
1378 DEBUG(10, ("Don't break our own lease\n"));
1382 DBG_DEBUG("Breaking %"PRIu64"/%"PRIu64" to none\n",
1383 e->lease_key.data[0],
1384 e->lease_key.data[1]);
1386 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1391 static void do_break_to_none(struct tevent_context *ctx,
1392 struct tevent_immediate *im,
1395 struct break_to_none_state *state = talloc_get_type_abort(
1396 private_data, struct break_to_none_state);
1398 struct share_mode_lock *lck;
1399 struct share_mode_data *d;
1402 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1404 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1405 __func__, file_id_string_tos(&state->id)));
1411 * Walk leases and oplocks separately: We have to send one break per
1412 * lease. If we have multiple share_mode_entry having a common lease,
1413 * we would break the lease twice if we don't walk the leases list
1417 ok = share_mode_forall_leases(lck, do_break_lease_to_none, state);
1419 DBG_WARNING("share_mode_forall_leases failed\n");
1422 for(i = 0; i < d->num_share_modes; i++) {
1423 struct share_mode_entry *e = &d->share_modes[i];
1425 if (!is_valid_share_mode_entry(e)) {
1428 if (e->op_type == LEASE_OPLOCK) {
1430 * Took care of those in the loop above
1436 * As there could have been multiple writes waiting at the
1437 * lock_share_entry gate we may not be the first to
1438 * enter. Hence the state of the op_types in the share mode
1439 * entries may be partly NO_OPLOCK and partly LEVEL_II
1440 * oplock. It will do no harm to re-send break messages to
1441 * those smbd's that are still waiting their turn to remove
1442 * their LEVEL_II state, and also no harm to ignore existing
1443 * NO_OPLOCK states. JRA.
1446 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1449 if (e->op_type == NO_OPLOCK) {
1454 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1455 DEBUG(0,("%s: PANIC. "
1456 "share mode entry %d is an exclusive "
1457 "oplock !\n", __func__, i ));
1462 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1465 /* We let the message receivers handle removing the oplock state
1466 in the share mode lock db. */
1474 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1475 enum level2_contention_type type)
1477 contend_level2_oplocks_begin_default(fsp, type);
1480 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1481 enum level2_contention_type type)
1486 /****************************************************************************
1487 Linearize a share mode entry struct to an internal oplock break message.
1488 ****************************************************************************/
1490 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1491 const struct share_mode_entry *e)
1493 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1494 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1495 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1496 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1497 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1498 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1499 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1500 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1502 * "id" used to be part of share_mode_entry, thus the strange
1503 * place to put this. Feel free to move somewhere else :-)
1505 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1506 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1507 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1508 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1509 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1510 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1513 /****************************************************************************
1514 De-linearize an internal oplock break message to a share mode entry struct.
1515 ****************************************************************************/
1517 void message_to_share_mode_entry(struct file_id *id,
1518 struct share_mode_entry *e,
1521 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1522 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1523 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1524 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1525 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1526 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1527 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1528 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1530 * "id" used to be part of share_mode_entry, thus the strange
1531 * place to put this. Feel free to move somewhere else :-)
1533 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1534 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1535 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1536 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1537 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1538 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1541 /****************************************************************************
1542 Setup oplocks for this process.
1543 ****************************************************************************/
1545 bool init_oplocks(struct smbd_server_connection *sconn)
1547 DEBUG(3,("init_oplocks: initializing messages.\n"));
1549 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1550 process_oplock_break_message);
1551 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1552 process_kernel_oplock_break);
1556 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1558 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1560 /* only initialize once */
1561 if (koplocks == NULL) {
1562 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1563 koplocks = linux_init_kernel_oplocks(sconn);
1565 sconn->oplocks.kernel_ops = koplocks;