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 "../librpc/gen_ndr/open_files.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 /****************************************************************************
92 Attempt to release an oplock on a file. Decrements oplock count.
93 ****************************************************************************/
95 static void release_file_oplock(files_struct *fsp)
97 struct smbd_server_connection *sconn = fsp->conn->sconn;
98 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
99 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
102 if ((fsp->oplock_type != NO_OPLOCK) &&
104 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
107 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
108 sconn->oplocks.level_II_open--;
109 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
110 sconn->oplocks.exclusive_open--;
113 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
114 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
116 fsp->oplock_type = NO_OPLOCK;
117 fsp->sent_oplock_break = NO_BREAK_SENT;
119 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
120 delete_write_cache(fsp);
122 TALLOC_FREE(fsp->oplock_timeout);
125 /****************************************************************************
126 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
127 ****************************************************************************/
129 static void downgrade_file_oplock(files_struct *fsp)
131 struct smbd_server_connection *sconn = fsp->conn->sconn;
132 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
133 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
136 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
137 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
142 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
144 fsp->oplock_type = LEVEL_II_OPLOCK;
145 sconn->oplocks.exclusive_open--;
146 sconn->oplocks.level_II_open++;
147 fsp->sent_oplock_break = NO_BREAK_SENT;
149 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
150 delete_write_cache(fsp);
152 TALLOC_FREE(fsp->oplock_timeout);
155 uint32_t get_lease_type(const struct share_mode_data *d,
156 const struct share_mode_entry *e)
158 if (e->op_type == LEASE_OPLOCK) {
159 return d->leases[e->lease_idx].current_state;
161 return map_oplock_to_lease_type(e->op_type);
164 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
166 struct share_mode_data *d = lck->data;
167 struct byte_range_lock *br_lck;
168 uint32_t num_read_oplocks = 0;
171 if (fsp_lease_type_is_exclusive(fsp)) {
172 const struct share_mode_entry *e = NULL;
173 uint32_t e_lease_type = 0;
176 * If we're fully exclusive, we don't need a brlock entry
178 remove_stale_share_mode_entries(d);
180 e = find_share_mode_entry(lck, fsp);
182 e_lease_type = get_lease_type(d, e);
185 if (!lease_type_is_exclusive(e_lease_type)) {
186 char *timestr = NULL;
188 timestr = timeval_string(talloc_tos(),
192 NDR_PRINT_DEBUG(share_mode_data, d);
193 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
194 "open_time[%s] lease_type [0x%x] "
195 "oplock_type [0x%x]\n",
197 file_id_string_tos(&fsp->file_id),
198 fsp->fh->gen_id, timestr,
199 e_lease_type, fsp->oplock_type);
201 smb_panic("Found non-exclusive lease");
207 for (i=0; i<d->num_share_modes; i++) {
208 struct share_mode_entry *e = &d->share_modes[i];
209 uint32_t e_lease_type = get_lease_type(d, e);
211 if (e_lease_type & SMB2_LEASE_READ) {
212 num_read_oplocks += 1;
216 br_lck = brl_get_locks_readonly(fsp);
217 if (br_lck == NULL) {
220 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
224 br_lck = brl_get_locks(talloc_tos(), fsp);
225 if (br_lck == NULL) {
228 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
233 /****************************************************************************
234 Remove a file oplock with lock already held. Copes with level II and exclusive.
235 ****************************************************************************/
237 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
241 ret = remove_share_oplock(lck, fsp);
243 DBG_ERR("failed to remove share oplock for "
245 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
246 file_id_string_tos(&fsp->file_id));
248 release_file_oplock(fsp);
250 ret = update_num_read_oplocks(fsp, lck);
252 DBG_ERR("update_num_read_oplocks failed for "
254 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
255 file_id_string_tos(&fsp->file_id));
261 /****************************************************************************
262 Remove a file oplock. Copes with level II and exclusive.
263 Locks then unlocks the share mode lock. Client can decide to go directly
264 to none even if a "break-to-level II" was sent.
265 ****************************************************************************/
267 bool remove_oplock(files_struct *fsp)
270 struct share_mode_lock *lck;
272 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
274 /* Remove the oplock flag from the sharemode. */
275 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
277 DBG_ERR("failed to lock share entry for "
278 "file %s\n", fsp_str_dbg(fsp));
282 ret = remove_oplock_under_lock(fsp, lck);
289 * Deal with a reply when a break-to-level II was sent.
291 bool downgrade_oplock(files_struct *fsp)
294 struct share_mode_lock *lck;
296 DEBUG(10, ("downgrade_oplock called for %s\n",
299 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
301 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
302 "file %s\n", fsp_str_dbg(fsp)));
305 ret = downgrade_share_oplock(lck, fsp);
307 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
308 "for file %s, %s, file_id %s\n",
309 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
310 file_id_string_tos(&fsp->file_id)));
312 downgrade_file_oplock(fsp);
314 ret = update_num_read_oplocks(fsp, lck);
316 DEBUG(0, ("%s: update_num_read_oplocks failed for "
318 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
319 file_id_string_tos(&fsp->file_id)));
326 static void lease_timeout_handler(struct tevent_context *ctx,
327 struct tevent_timer *te,
331 struct fsp_lease *lease =
332 talloc_get_type_abort(private_data,
334 struct files_struct *fsp;
335 struct share_mode_lock *lck;
336 uint16_t old_epoch = lease->lease.lease_epoch;
339 * This function runs without any specific impersonation
340 * and must not call any SMB_VFS operations!
343 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
344 &lease->lease.lease_key);
347 TALLOC_FREE(lease->timeout);
351 lck = get_existing_share_mode_lock(
352 talloc_tos(), fsp->file_id);
355 TALLOC_FREE(lease->timeout);
359 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
361 if (lease->lease.lease_epoch != old_epoch) {
363 * If the epoch changed we need to wait for
364 * the next timeout to happen.
366 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
372 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
374 * If the epoch changed we need to wait for
375 * the next timeout to happen.
377 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
383 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
385 (void)downgrade_lease(lease->sconn->client->connections,
388 &lease->lease.lease_key,
394 bool fsp_lease_update(struct share_mode_lock *lck,
395 const struct GUID *client_guid,
396 struct fsp_lease *lease)
398 struct share_mode_data *d = lck->data;
400 struct share_mode_lease *l = NULL;
402 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
408 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
409 TALLOC_FREE(lease->timeout);
410 lease->lease.lease_state = SMB2_LEASE_NONE;
411 lease->lease.lease_epoch += 1;
412 lease->lease.lease_flags = 0;
416 DEBUG(10,("%s: refresh lease state\n", __func__));
418 /* Ensure we're in sync with current lease state. */
419 if (lease->lease.lease_epoch != l->epoch) {
420 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
421 TALLOC_FREE(lease->timeout);
423 lease->lease.lease_epoch = l->epoch;
424 lease->lease.lease_state = l->current_state;
427 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
429 if (lease->timeout == NULL) {
430 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
432 DEBUG(10,("%s: setup timeout handler\n", __func__));
435 * lease_timeout_handler() only accesses locking.tdb
436 * so we don't use any impersonation and use
437 * the raw tevent context.
439 lease->timeout = tevent_add_timer(lease->sconn->raw_ev_ctx,
441 lease_timeout_handler,
443 if (lease->timeout == NULL) {
444 DEBUG(0, ("%s: Could not add lease timeout handler\n",
449 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
450 TALLOC_FREE(lease->timeout);
456 struct downgrade_lease_additional_state {
457 struct tevent_immediate *im;
458 struct smbXsrv_connection *xconn;
459 uint32_t break_flags;
460 struct smb2_lease_key lease_key;
466 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
467 struct tevent_immediate *im,
470 struct downgrade_lease_additional_state *state =
471 talloc_get_type_abort(private_data,
472 struct downgrade_lease_additional_state);
473 struct smbXsrv_connection *xconn = state->xconn;
476 status = smbd_smb2_send_lease_break(xconn,
483 if (!NT_STATUS_IS_OK(status)) {
484 smbd_server_connection_terminate(xconn,
490 struct downgrade_lease_fsps_state {
492 struct share_mode_lock *lck;
493 const struct smb2_lease_key *key;
496 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
499 struct downgrade_lease_fsps_state *state =
500 (struct downgrade_lease_fsps_state *)private_data;
502 if (fsp->oplock_type != LEASE_OPLOCK) {
505 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
508 if (!file_id_equal(&fsp->file_id, &state->id)) {
512 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
517 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
518 uint32_t num_file_ids,
519 const struct file_id *ids,
520 const struct smb2_lease_key *key,
521 uint32_t lease_state)
523 struct smbd_server_connection *sconn = xconn->client->sconn;
524 struct share_mode_lock *lck;
525 struct share_mode_lease *l = NULL;
526 const struct file_id id = ids[0];
530 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
531 file_id_string_tos(&id), (unsigned)lease_state));
533 lck = get_existing_share_mode_lock(talloc_tos(), id);
535 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
537 status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
539 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
540 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
542 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
543 struct downgrade_lease_additional_state *state;
545 state = talloc_zero(xconn,
546 struct downgrade_lease_additional_state);
549 return NT_STATUS_NO_MEMORY;
552 state->im = tevent_create_immediate(state);
553 if (state->im == NULL) {
556 return NT_STATUS_NO_MEMORY;
559 state->xconn = xconn;
560 if (l->current_state & (~SMB2_LEASE_READ)) {
561 state->break_flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
563 state->lease_key = l->lease_key;
564 state->break_from = l->current_state;
565 state->break_to = l->breaking_to_requested;
566 if (l->lease_version > 1) {
567 state->new_epoch = l->epoch;
570 if (state->break_flags == 0) {
572 * This is an async break without
573 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
575 * we need to store NONE state in the
578 l->current_state = 0;
579 l->breaking_to_requested = 0;
580 l->breaking_to_required = 0;
583 lck->data->modified = true;
586 tevent_schedule_immediate(state->im,
587 xconn->client->raw_ev_ctx,
588 downgrade_lease_additional_trigger,
593 struct downgrade_lease_fsps_state state = {
594 .id = id, .lck = lck, .key = key,
597 files_forall(sconn, downgrade_lease_fsps, &state);
601 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
602 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
605 * Dynamic share case. Ensure other opens are copies.
606 * This will only be breaking to NONE.
609 for (i = 1; i < num_file_ids; i++) {
610 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
612 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
616 struct downgrade_lease_fsps_state state = {
617 .id = ids[i], .lck = lck, .key = key,
620 files_forall(sconn, downgrade_lease_fsps, &state);
623 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
624 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
632 /****************************************************************************
633 Set up an oplock break message.
634 ****************************************************************************/
636 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
638 static void new_break_message_smb1(files_struct *fsp, int cmd,
639 char result[SMB1_BREAK_MESSAGE_LENGTH])
641 memset(result,'\0',smb_size);
642 srv_set_message(result,8,0,true);
643 SCVAL(result,smb_com,SMBlockingX);
644 SSVAL(result,smb_tid,fsp->conn->cnum);
645 SSVAL(result,smb_pid,0xFFFF);
646 SSVAL(result,smb_uid,0);
647 SSVAL(result,smb_mid,0xFFFF);
648 SCVAL(result,smb_vwv0,0xFF);
649 SSVAL(result,smb_vwv2,fsp->fnum);
650 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
651 SCVAL(result,smb_vwv3+1,cmd);
654 /****************************************************************************
655 Function to do the waiting before sending a local break.
656 ****************************************************************************/
658 static void wait_before_sending_break(void)
660 long wait_time = (long)lp_oplock_break_wait_time();
663 smb_msleep(wait_time);
667 /****************************************************************************
668 Ensure that we have a valid oplock.
669 ****************************************************************************/
671 static files_struct *initial_break_processing(
672 struct smbd_server_connection *sconn, struct file_id id,
673 unsigned long file_id)
675 files_struct *fsp = NULL;
677 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
678 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
679 file_id_string_tos(&id), (int)file_id,
680 sconn->oplocks.exclusive_open,
681 sconn->oplocks.level_II_open));
684 * We need to search the file open table for the
685 * entry containing this dev and inode, and ensure
686 * we have an oplock on it.
689 fsp = file_find_dif(sconn, id, file_id);
692 /* The file could have been closed in the meantime - return success. */
693 DEBUG(3, ("initial_break_processing: cannot find open file "
694 "with file_id %s gen_id = %lu, allowing break to "
695 "succeed.\n", file_id_string_tos(&id), file_id));
699 /* Ensure we have an oplock on the file */
702 * There is a potential race condition in that an oplock could
703 * have been broken due to another udp request, and yet there are
704 * still oplock break messages being sent in the udp message
705 * queue for this file. So return true if we don't have an oplock,
706 * as we may have just freed it.
709 if(fsp->oplock_type == NO_OPLOCK) {
710 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
711 "gen_id = %lu) has no oplock. Allowing break to "
712 "succeed regardless.\n", fsp_str_dbg(fsp),
713 file_id_string_tos(&id), fsp->fh->gen_id));
720 static void oplock_timeout_handler(struct tevent_context *ctx,
721 struct tevent_timer *te,
725 files_struct *fsp = (files_struct *)private_data;
728 * Note this function doesn't run under any specific impersonation and
729 * is not expected to call any SMB_VFS operation!
732 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
734 /* Remove the timed event handler. */
735 TALLOC_FREE(fsp->oplock_timeout);
736 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
741 /*******************************************************************
742 Add a timeout handler waiting for the client reply.
743 *******************************************************************/
745 static void add_oplock_timeout_handler(files_struct *fsp)
747 if (fsp->oplock_timeout != NULL) {
748 DEBUG(0, ("Logic problem -- have an oplock event hanging "
753 * For now we keep the logic and use the
754 * raw event context. We're called from
755 * the messaging system from a raw event context.
756 * Also oplock_timeout_handler doesn't invoke
759 fsp->oplock_timeout =
760 tevent_add_timer(fsp->conn->sconn->raw_ev_ctx, fsp,
761 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
762 oplock_timeout_handler, fsp);
764 if (fsp->oplock_timeout == NULL) {
765 DEBUG(0, ("Could not add oplock timeout handler\n"));
769 static void send_break_message_smb1(files_struct *fsp, int level)
771 struct smbXsrv_connection *xconn = NULL;
772 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
775 * For SMB1 we only have one connection
777 xconn = fsp->conn->sconn->client->connections;
779 new_break_message_smb1(fsp, level, break_msg);
782 if (!srv_send_smb(xconn,
784 IS_CONN_ENCRYPTED(fsp->conn),
786 exit_server_cleanly("send_break_message_smb1: "
787 "srv_send_smb failed.");
791 /*******************************************************************
792 This handles the generic oplock break message from another smbd.
793 *******************************************************************/
795 static void process_oplock_break_message(struct messaging_context *msg_ctx,
798 struct server_id src,
802 struct share_mode_entry msg;
805 struct smbd_server_connection *sconn =
806 talloc_get_type_abort(private_data,
807 struct smbd_server_connection);
808 struct server_id self = messaging_server_id(sconn->msg_ctx);
809 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
812 bool break_needed = true;
813 struct server_id_buf tmp;
815 if (data->data == NULL) {
816 DEBUG(0, ("Got NULL buffer\n"));
820 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
821 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
825 /* De-linearize incoming message. */
826 message_to_share_mode_entry(&id, &msg, (char *)data->data);
827 break_to = msg.op_type;
829 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
830 (unsigned)break_to, server_id_str_buf(src, &tmp),
831 file_id_string_tos(&id),
832 (unsigned long long)msg.share_file_id));
834 fsp = initial_break_processing(sconn, id, msg.share_file_id);
837 /* We hit a race here. Break messages are sent, and before we
838 * get to process this message, we have closed the file. */
839 DEBUG(3, ("Did not find fsp\n"));
843 break_from = fsp_lease_type(fsp);
845 if (fsp->oplock_type != LEASE_OPLOCK) {
846 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
848 * Nothing to do anymore
850 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
851 fsp->sent_oplock_break));
856 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
857 DEBUG(10, ("client_caps without level2 oplocks\n"));
858 break_to &= ~SMB2_LEASE_READ;
861 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
864 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
865 break_to &= ~SMB2_LEASE_READ;
868 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
869 DEBUG(10, ("no level2 oplocks by config\n"));
870 break_to &= ~SMB2_LEASE_READ;
873 if (fsp->oplock_type == LEASE_OPLOCK) {
874 struct share_mode_lock *lck;
877 lck = get_existing_share_mode_lock(
878 talloc_tos(), fsp->file_id);
881 * We hit a race here. Break messages are sent, and
882 * before we get to process this message, we have closed
885 DEBUG(3, ("Did not find share_mode\n"));
889 idx = find_share_mode_lease(
891 fsp_client_guid(fsp),
892 &fsp->lease->lease.lease_key);
894 struct share_mode_lease *l;
895 l = &lck->data->leases[idx];
897 break_from = l->current_state;
898 break_to &= l->current_state;
901 break_to &= l->breaking_to_required;
902 if (l->breaking_to_required != break_to) {
904 * Note we don't increment the epoch
905 * here, which might be a bug in
908 l->breaking_to_required = break_to;
909 lck->data->modified = true;
911 break_needed = false;
912 } else if (l->current_state == break_to) {
913 break_needed = false;
914 } else if (l->current_state == SMB2_LEASE_READ) {
915 l->current_state = SMB2_LEASE_NONE;
916 /* Need to increment the epoch */
918 lck->data->modified = true;
921 l->breaking_to_required = break_to;
922 l->breaking_to_requested = break_to;
923 /* Need to increment the epoch */
925 lck->data->modified = true;
928 /* Ensure we're in sync with current lease state. */
929 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
936 DEBUG(10,("%s: skip break\n", __func__));
940 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
941 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
942 file_id_string_tos(&fsp->file_id),
947 DEBUG(10, ("break_from=%u, break_to=%u\n",
948 (unsigned)break_from, (unsigned)break_to));
950 if ((break_from == break_to) && !break_needed) {
951 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
953 file_id_string_tos(&fsp->file_id),
958 /* Need to wait before sending a break
959 message if we sent ourselves this message. */
960 if (serverid_equal(&self, &src)) {
961 wait_before_sending_break();
964 if (sconn->using_smb2) {
965 send_break_message_smb2(fsp, break_from, break_to);
967 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
968 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
971 if ((break_from == SMB2_LEASE_READ) &&
972 (break_to == SMB2_LEASE_NONE)) {
974 * This is an async break without a reply and thus no timeout
976 * leases are handled above.
978 if (fsp->oplock_type != LEASE_OPLOCK) {
983 if (fsp->oplock_type == LEASE_OPLOCK) {
987 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
988 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
990 add_oplock_timeout_handler(fsp);
993 /*******************************************************************
994 This handles the kernel oplock break message.
995 *******************************************************************/
997 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1000 struct server_id src,
1004 unsigned long file_id;
1006 struct smbd_server_connection *sconn =
1007 talloc_get_type_abort(private_data,
1008 struct smbd_server_connection);
1009 struct server_id_buf tmp;
1011 if (data->data == NULL) {
1012 DEBUG(0, ("Got NULL buffer\n"));
1016 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1017 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1021 /* Pull the data from the message. */
1022 pull_file_id_24((char *)data->data, &id);
1023 file_id = (unsigned long)IVAL(data->data, 24);
1025 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1026 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1027 (unsigned int)file_id));
1029 fsp = initial_break_processing(sconn, id, file_id);
1032 DEBUG(3, ("Got a kernel oplock break message for a file "
1033 "I don't know about\n"));
1037 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1038 /* This is ok, kernel oplocks come in completely async */
1039 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1044 if (sconn->using_smb2) {
1045 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1047 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1050 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1052 add_oplock_timeout_handler(fsp);
1055 static bool file_has_read_oplocks(struct files_struct *fsp)
1057 struct byte_range_lock *brl;
1058 uint32_t num_read_oplocks = 0;
1060 brl = brl_get_locks_readonly(fsp);
1065 num_read_oplocks = brl_num_read_oplocks(brl);
1067 DBG_DEBUG("num_read_oplocks = %"PRIu32"\n", num_read_oplocks);
1069 return (num_read_oplocks != 0);
1072 struct break_to_none_state {
1073 struct smbd_server_connection *sconn;
1075 struct smb2_lease_key lease_key;
1076 struct GUID client_guid;
1078 static void do_break_to_none(struct tevent_context *ctx,
1079 struct tevent_immediate *im,
1080 void *private_data);
1082 /****************************************************************************
1083 This function is called on any file modification or lock request. If a file
1084 is level 2 oplocked then it must tell all other level 2 holders to break to
1086 ****************************************************************************/
1088 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1089 enum level2_contention_type type)
1091 struct smbd_server_connection *sconn = fsp->conn->sconn;
1092 struct tevent_immediate *im;
1093 struct break_to_none_state *state;
1094 bool has_read_oplocks;
1097 * If this file is level II oplocked then we need
1098 * to grab the shared memory lock and inform all
1099 * other files with a level II lock that they need
1100 * to flush their read caches. We keep the lock over
1101 * the shared memory area whilst doing this.
1104 if (fsp_lease_type_is_exclusive(fsp)) {
1106 * There can't be any level2 oplocks, we're alone.
1111 has_read_oplocks = file_has_read_oplocks(fsp);
1112 if (!has_read_oplocks) {
1113 DEBUG(10, ("No read oplocks around\n"));
1118 * When we get here we might have a brlock entry locked. Also
1119 * locking the share mode entry would violate the locking
1120 * order. Breaking level2 oplocks to none is asynchronous
1121 * anyway, so we postpone this into an immediate event.
1124 state = talloc_zero(sconn, struct break_to_none_state);
1125 if (state == NULL) {
1126 DEBUG(1, ("talloc failed\n"));
1129 state->sconn = sconn;
1130 state->id = fsp->file_id;
1132 if (fsp->oplock_type == LEASE_OPLOCK) {
1133 state->client_guid = *fsp_client_guid(fsp);
1134 state->lease_key = fsp->lease->lease.lease_key;
1135 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1136 state->lease_key.data[0],
1137 state->lease_key.data[1]));
1140 im = tevent_create_immediate(state);
1142 DEBUG(1, ("tevent_create_immediate failed\n"));
1148 * do_break_to_none() only operates on the
1149 * locking.tdb and sends network packets to
1150 * the client. That doesn't require any
1151 * impersonation, so we just use the
1152 * raw tevent context here.
1154 tevent_schedule_immediate(im, sconn->raw_ev_ctx, do_break_to_none, state);
1157 static void send_break_to_none(struct messaging_context *msg_ctx,
1158 const struct file_id *id,
1159 const struct share_mode_entry *e)
1161 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1163 share_mode_entry_to_message(msg, id, e);
1164 /* Overload entry->op_type */
1165 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1167 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1168 (uint8_t *)msg, sizeof(msg));
1171 static void do_break_to_none(struct tevent_context *ctx,
1172 struct tevent_immediate *im,
1175 struct break_to_none_state *state = talloc_get_type_abort(
1176 private_data, struct break_to_none_state);
1178 struct share_mode_lock *lck;
1179 struct share_mode_data *d;
1182 * Note this function doesn't run under any specific impersonation and
1183 * is not expected to call any SMB_VFS operation!
1186 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1188 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1189 __func__, file_id_string_tos(&state->id)));
1195 * Walk leases and oplocks separately: We have to send one break per
1196 * lease. If we have multiple share_mode_entry having a common lease,
1197 * we would break the lease twice if we don't walk the leases list
1201 for (i=0; i<d->num_leases; i++) {
1202 struct share_mode_lease *l = &d->leases[i];
1203 struct share_mode_entry *e = NULL;
1206 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1209 if (smb2_lease_equal(&state->client_guid,
1213 DEBUG(10, ("Don't break our own lease\n"));
1217 for (j=0; j<d->num_share_modes; j++) {
1218 e = &d->share_modes[j];
1220 if (!is_valid_share_mode_entry(e)) {
1223 if (e->lease_idx == i) {
1227 if (j == d->num_share_modes) {
1228 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1233 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1234 "%"PRIu32"\n", i, j));
1236 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1239 for(i = 0; i < d->num_share_modes; i++) {
1240 struct share_mode_entry *e = &d->share_modes[i];
1242 if (!is_valid_share_mode_entry(e)) {
1245 if (e->op_type == LEASE_OPLOCK) {
1247 * Took care of those in the loop above
1253 * As there could have been multiple writes waiting at the
1254 * lock_share_entry gate we may not be the first to
1255 * enter. Hence the state of the op_types in the share mode
1256 * entries may be partly NO_OPLOCK and partly LEVEL_II
1257 * oplock. It will do no harm to re-send break messages to
1258 * those smbd's that are still waiting their turn to remove
1259 * their LEVEL_II state, and also no harm to ignore existing
1260 * NO_OPLOCK states. JRA.
1263 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1266 if (e->op_type == NO_OPLOCK) {
1271 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1272 DEBUG(0,("%s: PANIC. "
1273 "share mode entry %d is an exclusive "
1274 "oplock !\n", __func__, i ));
1279 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1282 /* We let the message receivers handle removing the oplock state
1283 in the share mode lock db. */
1291 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1292 enum level2_contention_type type)
1294 contend_level2_oplocks_begin_default(fsp, type);
1297 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1298 enum level2_contention_type type)
1303 /****************************************************************************
1304 Linearize a share mode entry struct to an internal oplock break message.
1305 ****************************************************************************/
1307 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1308 const struct share_mode_entry *e)
1310 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1311 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1312 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1313 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1314 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1315 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1316 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1317 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1319 * "id" used to be part of share_mode_entry, thus the strange
1320 * place to put this. Feel free to move somewhere else :-)
1322 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1323 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1324 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1325 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1326 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1327 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1330 /****************************************************************************
1331 De-linearize an internal oplock break message to a share mode entry struct.
1332 ****************************************************************************/
1334 void message_to_share_mode_entry(struct file_id *id,
1335 struct share_mode_entry *e,
1338 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1339 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1340 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1341 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1342 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1343 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1344 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1345 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1347 * "id" used to be part of share_mode_entry, thus the strange
1348 * place to put this. Feel free to move somewhere else :-)
1350 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1351 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1352 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1353 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1354 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1355 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1358 /****************************************************************************
1359 Setup oplocks for this process.
1360 ****************************************************************************/
1362 bool init_oplocks(struct smbd_server_connection *sconn)
1364 DEBUG(3,("init_oplocks: initializing messages.\n"));
1366 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1367 process_oplock_break_message);
1368 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1369 process_kernel_oplock_break);
1373 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1375 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1377 /* only initialize once */
1378 if (koplocks == NULL) {
1379 #if HAVE_KERNEL_OPLOCKS_LINUX
1380 koplocks = linux_init_kernel_oplocks(sconn);
1382 sconn->oplocks.kernel_ops = koplocks;