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 "smbd/smbd.h"
25 #include "smbd/globals.h"
27 #include "../librpc/gen_ndr/open_files.h"
30 * helper function used by the kernel oplock backends to post the break message
32 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
34 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
36 /* Put the kernel break info into the message. */
37 push_file_id_24((char *)msg, &fsp->file_id);
38 SIVAL(msg,24,fsp->fh->gen_id);
40 /* Don't need to be root here as we're only ever
41 sending to ourselves. */
43 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
45 msg, MSG_SMB_KERNEL_BREAK_SIZE);
48 /****************************************************************************
49 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
50 disabled (just sets flags).
51 ****************************************************************************/
53 NTSTATUS set_file_oplock(files_struct *fsp)
55 struct smbd_server_connection *sconn = fsp->conn->sconn;
56 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
57 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
59 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
61 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
62 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
63 "don't support them\n"));
64 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)) && koplocks;
101 if ((fsp->oplock_type != NO_OPLOCK) &&
103 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
106 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
107 sconn->oplocks.level_II_open--;
108 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
109 sconn->oplocks.exclusive_open--;
112 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
113 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
115 fsp->oplock_type = NO_OPLOCK;
116 fsp->sent_oplock_break = NO_BREAK_SENT;
118 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
119 delete_write_cache(fsp);
121 TALLOC_FREE(fsp->oplock_timeout);
124 /****************************************************************************
125 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
126 ****************************************************************************/
128 static void downgrade_file_oplock(files_struct *fsp)
130 struct smbd_server_connection *sconn = fsp->conn->sconn;
131 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
133 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
134 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
139 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
141 fsp->oplock_type = LEVEL_II_OPLOCK;
142 sconn->oplocks.exclusive_open--;
143 sconn->oplocks.level_II_open++;
144 fsp->sent_oplock_break = NO_BREAK_SENT;
146 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
147 delete_write_cache(fsp);
149 TALLOC_FREE(fsp->oplock_timeout);
152 uint32_t map_oplock_to_lease_type(uint16_t op_type)
158 case BATCH_OPLOCK|EXCLUSIVE_OPLOCK:
159 ret = SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE;
161 case EXCLUSIVE_OPLOCK:
162 ret = SMB2_LEASE_READ|SMB2_LEASE_WRITE;
164 case LEVEL_II_OPLOCK:
165 ret = SMB2_LEASE_READ;
168 ret = SMB2_LEASE_NONE;
174 uint32_t get_lease_type(struct share_mode_data *d, struct share_mode_entry *e)
176 if (e->op_type == LEASE_OPLOCK) {
177 return d->leases[e->lease_idx].current_state;
179 return map_oplock_to_lease_type(e->op_type);
182 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
184 struct share_mode_data *d = lck->data;
185 struct byte_range_lock *br_lck;
186 uint32_t num_read_oplocks = 0;
189 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
191 * If we're the only one, we don't need a brlock entry
193 SMB_ASSERT(d->num_share_modes == 1);
194 SMB_ASSERT(EXCLUSIVE_OPLOCK_TYPE(d->share_modes[0].op_type));
198 for (i=0; i<d->num_share_modes; i++) {
199 struct share_mode_entry *e = &d->share_modes[i];
200 uint32_t e_lease_type = get_lease_type(d, e);
202 if (e_lease_type & SMB2_LEASE_READ) {
203 num_read_oplocks += 1;
207 br_lck = brl_get_locks_readonly(fsp);
208 if (br_lck == NULL) {
211 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
215 br_lck = brl_get_locks(talloc_tos(), fsp);
216 if (br_lck == NULL) {
219 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
224 /****************************************************************************
225 Remove a file oplock. Copes with level II and exclusive.
226 Locks then unlocks the share mode lock. Client can decide to go directly
227 to none even if a "break-to-level II" was sent.
228 ****************************************************************************/
230 bool remove_oplock(files_struct *fsp)
233 struct share_mode_lock *lck;
235 DEBUG(10, ("remove_oplock called for %s\n",
238 /* Remove the oplock flag from the sharemode. */
239 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
241 DEBUG(0,("remove_oplock: failed to lock share entry for "
242 "file %s\n", fsp_str_dbg(fsp)));
246 ret = remove_share_oplock(lck, fsp);
248 DEBUG(0,("remove_oplock: failed to remove share oplock for "
250 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
251 file_id_string_tos(&fsp->file_id)));
253 release_file_oplock(fsp);
255 ret = update_num_read_oplocks(fsp, lck);
257 DEBUG(0, ("%s: update_num_read_oplocks failed for "
259 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
260 file_id_string_tos(&fsp->file_id)));
268 * Deal with a reply when a break-to-level II was sent.
270 bool downgrade_oplock(files_struct *fsp)
273 struct share_mode_lock *lck;
275 DEBUG(10, ("downgrade_oplock called for %s\n",
278 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
280 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
281 "file %s\n", fsp_str_dbg(fsp)));
284 ret = downgrade_share_oplock(lck, fsp);
286 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
287 "for file %s, %s, file_id %s\n",
288 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
289 file_id_string_tos(&fsp->file_id)));
291 downgrade_file_oplock(fsp);
293 ret = update_num_read_oplocks(fsp, lck);
295 DEBUG(0, ("%s: update_num_read_oplocks failed for "
297 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
298 file_id_string_tos(&fsp->file_id)));
305 static void lease_timeout_handler(struct tevent_context *ctx,
306 struct tevent_timer *te,
310 struct fsp_lease *lease =
311 talloc_get_type_abort(private_data,
313 struct files_struct *fsp;
314 struct share_mode_lock *lck;
315 uint16_t old_epoch = lease->lease.lease_epoch;
317 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
318 &lease->lease.lease_key);
321 TALLOC_FREE(lease->timeout);
325 lck = get_existing_share_mode_lock(
326 talloc_tos(), fsp->file_id);
329 TALLOC_FREE(lease->timeout);
333 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
335 if (lease->lease.lease_epoch != old_epoch) {
337 * If the epoch changed we need to wait for
338 * the next timeout to happen.
340 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
346 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
348 * If the epoch changed we need to wait for
349 * the next timeout to happen.
351 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
357 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
359 (void)downgrade_lease(lease->sconn->client->connections,
362 &lease->lease.lease_key,
368 bool fsp_lease_update(struct share_mode_lock *lck,
369 const struct GUID *client_guid,
370 struct fsp_lease *lease)
372 struct share_mode_data *d = lck->data;
374 struct share_mode_lease *l = NULL;
376 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
382 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
383 TALLOC_FREE(lease->timeout);
384 lease->lease.lease_state = SMB2_LEASE_NONE;
385 lease->lease.lease_epoch += 1;
386 lease->lease.lease_flags = 0;
390 DEBUG(10,("%s: refresh lease state\n", __func__));
392 /* Ensure we're in sync with current lease state. */
393 if (lease->lease.lease_epoch != l->epoch) {
394 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
395 TALLOC_FREE(lease->timeout);
397 lease->lease.lease_epoch = l->epoch;
398 lease->lease.lease_state = l->current_state;
401 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
403 if (lease->timeout == NULL) {
404 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
406 DEBUG(10,("%s: setup timeout handler\n", __func__));
408 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
410 lease_timeout_handler,
412 if (lease->timeout == NULL) {
413 DEBUG(0, ("%s: Could not add lease timeout handler\n",
418 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
419 TALLOC_FREE(lease->timeout);
425 struct downgrade_lease_additional_state {
426 struct tevent_immediate *im;
427 struct smbXsrv_connection *xconn;
428 uint32_t break_flags;
429 struct smb2_lease_key lease_key;
435 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
436 struct tevent_immediate *im,
439 struct downgrade_lease_additional_state *state =
440 talloc_get_type_abort(private_data,
441 struct downgrade_lease_additional_state);
442 struct smbXsrv_connection *xconn = state->xconn;
445 status = smbd_smb2_send_lease_break(xconn,
452 if (!NT_STATUS_IS_OK(status)) {
453 smbd_server_connection_terminate(xconn,
459 struct downgrade_lease_fsps_state {
461 struct share_mode_lock *lck;
462 const struct smb2_lease_key *key;
465 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
468 struct downgrade_lease_fsps_state *state =
469 (struct downgrade_lease_fsps_state *)private_data;
471 if (fsp->oplock_type != LEASE_OPLOCK) {
474 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
477 if (!file_id_equal(&fsp->file_id, &state->id)) {
481 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
486 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
487 uint32_t num_file_ids,
488 const struct file_id *ids,
489 const struct smb2_lease_key *key,
490 uint32_t lease_state)
492 struct smbd_server_connection *sconn = xconn->client->sconn;
493 struct share_mode_lock *lck;
494 struct share_mode_lease *l = NULL;
495 const struct file_id id = ids[0];
499 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
500 file_id_string_tos(&id), (unsigned)lease_state));
502 lck = get_existing_share_mode_lock(talloc_tos(), id);
504 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
506 status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
508 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
509 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
511 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
512 struct downgrade_lease_additional_state *state;
514 state = talloc_zero(xconn,
515 struct downgrade_lease_additional_state);
518 return NT_STATUS_NO_MEMORY;
521 state->im = tevent_create_immediate(state);
522 if (state->im == NULL) {
525 return NT_STATUS_NO_MEMORY;
528 state->xconn = xconn;
529 if (l->current_state & (~SMB2_LEASE_READ)) {
530 state->break_flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
532 state->lease_key = l->lease_key;
533 state->break_from = l->current_state;
534 state->break_to = l->breaking_to_requested;
535 if (l->lease_version > 1) {
536 state->new_epoch = l->epoch;
539 if (state->break_flags == 0) {
541 * This is an async break without
542 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
544 * we need to store NONE state in the
547 l->current_state = 0;
548 l->breaking_to_requested = 0;
549 l->breaking_to_required = 0;
552 lck->data->modified = true;
555 tevent_schedule_immediate(state->im, xconn->ev_ctx,
556 downgrade_lease_additional_trigger,
561 struct downgrade_lease_fsps_state state = {
562 .id = id, .lck = lck, .key = key,
565 files_forall(sconn, downgrade_lease_fsps, &state);
569 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
570 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
573 * Dynamic share case. Ensure other opens are copies.
574 * This will only be breaking to NONE.
577 for (i = 1; i < num_file_ids; i++) {
578 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
580 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
584 struct downgrade_lease_fsps_state state = {
585 .id = ids[i], .lck = lck, .key = key,
588 files_forall(sconn, downgrade_lease_fsps, &state);
591 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
592 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
600 /****************************************************************************
601 Set up an oplock break message.
602 ****************************************************************************/
604 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
606 static void new_break_message_smb1(files_struct *fsp, int cmd,
607 char result[SMB1_BREAK_MESSAGE_LENGTH])
609 memset(result,'\0',smb_size);
610 srv_set_message(result,8,0,true);
611 SCVAL(result,smb_com,SMBlockingX);
612 SSVAL(result,smb_tid,fsp->conn->cnum);
613 SSVAL(result,smb_pid,0xFFFF);
614 SSVAL(result,smb_uid,0);
615 SSVAL(result,smb_mid,0xFFFF);
616 SCVAL(result,smb_vwv0,0xFF);
617 SSVAL(result,smb_vwv2,fsp->fnum);
618 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
619 SCVAL(result,smb_vwv3+1,cmd);
622 /****************************************************************************
623 Function to do the waiting before sending a local break.
624 ****************************************************************************/
626 static void wait_before_sending_break(void)
628 long wait_time = (long)lp_oplock_break_wait_time();
631 smb_msleep(wait_time);
635 /****************************************************************************
636 Ensure that we have a valid oplock.
637 ****************************************************************************/
639 static files_struct *initial_break_processing(
640 struct smbd_server_connection *sconn, struct file_id id,
641 unsigned long file_id)
643 files_struct *fsp = NULL;
645 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
646 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
647 file_id_string_tos(&id), (int)file_id,
648 sconn->oplocks.exclusive_open,
649 sconn->oplocks.level_II_open));
652 * We need to search the file open table for the
653 * entry containing this dev and inode, and ensure
654 * we have an oplock on it.
657 fsp = file_find_dif(sconn, id, file_id);
660 /* The file could have been closed in the meantime - return success. */
661 DEBUG(3, ("initial_break_processing: cannot find open file "
662 "with file_id %s gen_id = %lu, allowing break to "
663 "succeed.\n", file_id_string_tos(&id), file_id));
667 /* Ensure we have an oplock on the file */
670 * There is a potential race condition in that an oplock could
671 * have been broken due to another udp request, and yet there are
672 * still oplock break messages being sent in the udp message
673 * queue for this file. So return true if we don't have an oplock,
674 * as we may have just freed it.
677 if(fsp->oplock_type == NO_OPLOCK) {
678 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
679 "gen_id = %lu) has no oplock. Allowing break to "
680 "succeed regardless.\n", fsp_str_dbg(fsp),
681 file_id_string_tos(&id), fsp->fh->gen_id));
688 static void oplock_timeout_handler(struct tevent_context *ctx,
689 struct tevent_timer *te,
693 files_struct *fsp = (files_struct *)private_data;
695 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
697 /* Remove the timed event handler. */
698 TALLOC_FREE(fsp->oplock_timeout);
699 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
704 /*******************************************************************
705 Add a timeout handler waiting for the client reply.
706 *******************************************************************/
708 static void add_oplock_timeout_handler(files_struct *fsp)
710 struct smbd_server_connection *sconn = fsp->conn->sconn;
711 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
714 * If kernel oplocks already notifies smbds when an oplock break times
718 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
722 if (fsp->oplock_timeout != NULL) {
723 DEBUG(0, ("Logic problem -- have an oplock event hanging "
727 fsp->oplock_timeout =
728 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
729 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
730 oplock_timeout_handler, fsp);
732 if (fsp->oplock_timeout == NULL) {
733 DEBUG(0, ("Could not add oplock timeout handler\n"));
737 static void send_break_message_smb1(files_struct *fsp, int level)
739 struct smbXsrv_connection *xconn = NULL;
740 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
743 * For SMB1 we only have one connection
745 xconn = fsp->conn->sconn->client->connections;
747 new_break_message_smb1(fsp, level, break_msg);
750 if (!srv_send_smb(xconn,
752 IS_CONN_ENCRYPTED(fsp->conn),
754 exit_server_cleanly("send_break_message_smb1: "
755 "srv_send_smb failed.");
759 /*******************************************************************
760 This handles the generic oplock break message from another smbd.
761 *******************************************************************/
763 static void process_oplock_break_message(struct messaging_context *msg_ctx,
766 struct server_id src,
769 struct share_mode_entry msg;
772 struct smbd_server_connection *sconn =
773 talloc_get_type_abort(private_data,
774 struct smbd_server_connection);
775 struct server_id self = messaging_server_id(sconn->msg_ctx);
776 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
779 bool break_needed = true;
780 struct server_id_buf tmp;
782 if (data->data == NULL) {
783 DEBUG(0, ("Got NULL buffer\n"));
787 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
788 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
792 /* De-linearize incoming message. */
793 message_to_share_mode_entry(&msg, (char *)data->data);
794 break_to = msg.op_type;
796 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
797 (unsigned)break_to, server_id_str_buf(src, &tmp),
798 file_id_string_tos(&msg.id),
799 (unsigned long long)msg.share_file_id));
801 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
804 /* We hit a race here. Break messages are sent, and before we
805 * get to process this message, we have closed the file. */
806 DEBUG(3, ("Did not find fsp\n"));
810 break_from = fsp_lease_type(fsp);
812 if (fsp->oplock_type != LEASE_OPLOCK) {
813 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
815 * Nothing to do anymore
817 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
818 fsp->sent_oplock_break));
823 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
824 DEBUG(10, ("client_caps without level2 oplocks\n"));
825 break_to &= ~SMB2_LEASE_READ;
828 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
829 if (use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
830 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
831 break_to &= ~SMB2_LEASE_READ;
834 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
835 DEBUG(10, ("no level2 oplocks by config\n"));
836 break_to &= ~SMB2_LEASE_READ;
839 if (fsp->oplock_type == LEASE_OPLOCK) {
840 struct share_mode_lock *lck;
843 lck = get_existing_share_mode_lock(
844 talloc_tos(), fsp->file_id);
847 * We hit a race here. Break messages are sent, and
848 * before we get to process this message, we have closed
851 DEBUG(3, ("Did not find share_mode\n"));
855 idx = find_share_mode_lease(
857 fsp_client_guid(fsp),
858 &fsp->lease->lease.lease_key);
860 struct share_mode_lease *l;
861 l = &lck->data->leases[idx];
863 break_from = l->current_state;
864 break_to &= l->current_state;
867 break_to &= l->breaking_to_required;
868 if (l->breaking_to_required != break_to) {
870 * Note we don't increment the epoch
871 * here, which might be a bug in
874 l->breaking_to_required = break_to;
875 lck->data->modified = true;
877 break_needed = false;
878 } else if (l->current_state == break_to) {
879 break_needed = false;
880 } else if (l->current_state == SMB2_LEASE_READ) {
881 l->current_state = SMB2_LEASE_NONE;
882 /* Need to increment the epoch */
884 lck->data->modified = true;
887 l->breaking_to_required = break_to;
888 l->breaking_to_requested = break_to;
889 /* Need to increment the epoch */
891 lck->data->modified = true;
894 /* Ensure we're in sync with current lease state. */
895 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
902 DEBUG(10,("%s: skip break\n", __func__));
906 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
907 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
908 file_id_string_tos(&fsp->file_id),
913 DEBUG(10, ("break_from=%u, break_to=%u\n",
914 (unsigned)break_from, (unsigned)break_to));
916 if ((break_from == break_to) && !break_needed) {
917 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
919 file_id_string_tos(&fsp->file_id),
924 /* Need to wait before sending a break
925 message if we sent ourselves this message. */
926 if (serverid_equal(&self, &src)) {
927 wait_before_sending_break();
930 if (sconn->using_smb2) {
931 send_break_message_smb2(fsp, break_from, break_to);
933 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
934 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
937 if ((break_from == SMB2_LEASE_READ) &&
938 (break_to == SMB2_LEASE_NONE)) {
940 * This is an async break without a reply and thus no timeout
942 * leases are handled above.
944 if (fsp->oplock_type != LEASE_OPLOCK) {
949 if (fsp->oplock_type == LEASE_OPLOCK) {
953 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
954 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
956 add_oplock_timeout_handler(fsp);
959 /*******************************************************************
960 This handles the kernel oplock break message.
961 *******************************************************************/
963 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
966 struct server_id src,
970 unsigned long file_id;
972 struct smbd_server_connection *sconn =
973 talloc_get_type_abort(private_data,
974 struct smbd_server_connection);
975 struct server_id_buf tmp;
977 if (data->data == NULL) {
978 DEBUG(0, ("Got NULL buffer\n"));
982 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
983 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
987 /* Pull the data from the message. */
988 pull_file_id_24((char *)data->data, &id);
989 file_id = (unsigned long)IVAL(data->data, 24);
991 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
992 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
993 (unsigned int)file_id));
995 fsp = initial_break_processing(sconn, id, file_id);
998 DEBUG(3, ("Got a kernel oplock break message for a file "
999 "I don't know about\n"));
1003 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1004 /* This is ok, kernel oplocks come in completely async */
1005 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1010 if (sconn->using_smb2) {
1011 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1013 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1016 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1018 add_oplock_timeout_handler(fsp);
1021 struct break_to_none_state {
1022 struct smbd_server_connection *sconn;
1024 struct smb2_lease_key lease_key;
1025 struct GUID client_guid;
1027 static void do_break_to_none(struct tevent_context *ctx,
1028 struct tevent_immediate *im,
1029 void *private_data);
1031 /****************************************************************************
1032 This function is called on any file modification or lock request. If a file
1033 is level 2 oplocked then it must tell all other level 2 holders to break to
1035 ****************************************************************************/
1037 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1038 enum level2_contention_type type)
1040 struct smbd_server_connection *sconn = fsp->conn->sconn;
1041 struct tevent_immediate *im;
1042 struct break_to_none_state *state;
1043 struct byte_range_lock *brl;
1044 uint32_t num_read_oplocks = 0;
1047 * If this file is level II oplocked then we need
1048 * to grab the shared memory lock and inform all
1049 * other files with a level II lock that they need
1050 * to flush their read caches. We keep the lock over
1051 * the shared memory area whilst doing this.
1054 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1056 * There can't be any level2 oplocks, we're alone.
1061 brl = brl_get_locks_readonly(fsp);
1063 num_read_oplocks = brl_num_read_oplocks(brl);
1066 DEBUG(10, ("num_read_oplocks = %"PRIu32"\n", num_read_oplocks));
1068 if (num_read_oplocks == 0) {
1069 DEBUG(10, ("No read oplocks around\n"));
1074 * When we get here we might have a brlock entry locked. Also
1075 * locking the share mode entry would violate the locking
1076 * order. Breaking level2 oplocks to none is asynchronous
1077 * anyway, so we postpone this into an immediate event.
1080 state = talloc_zero(sconn, struct break_to_none_state);
1081 if (state == NULL) {
1082 DEBUG(1, ("talloc failed\n"));
1085 state->sconn = sconn;
1086 state->id = fsp->file_id;
1088 if (fsp->oplock_type == LEASE_OPLOCK) {
1089 state->client_guid = *fsp_client_guid(fsp);
1090 state->lease_key = fsp->lease->lease.lease_key;
1091 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1092 state->lease_key.data[0],
1093 state->lease_key.data[1]));
1096 im = tevent_create_immediate(state);
1098 DEBUG(1, ("tevent_create_immediate failed\n"));
1102 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1105 static void send_break_to_none(struct messaging_context *msg_ctx,
1106 const struct share_mode_entry *e)
1108 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1110 share_mode_entry_to_message(msg, e);
1111 /* Overload entry->op_type */
1112 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1114 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1115 (uint8_t *)msg, sizeof(msg));
1118 static void do_break_to_none(struct tevent_context *ctx,
1119 struct tevent_immediate *im,
1122 struct break_to_none_state *state = talloc_get_type_abort(
1123 private_data, struct break_to_none_state);
1125 struct share_mode_lock *lck;
1126 struct share_mode_data *d;
1128 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1130 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1131 __func__, file_id_string_tos(&state->id)));
1137 * Walk leases and oplocks separately: We have to send one break per
1138 * lease. If we have multiple share_mode_entry having a common lease,
1139 * we would break the lease twice if we don't walk the leases list
1143 for (i=0; i<d->num_leases; i++) {
1144 struct share_mode_lease *l = &d->leases[i];
1145 struct share_mode_entry *e;
1148 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1151 if (smb2_lease_equal(&state->client_guid,
1155 DEBUG(10, ("Don't break our own lease\n"));
1159 for (j=0; j<d->num_share_modes; j++) {
1160 e = &d->share_modes[j];
1162 if (!is_valid_share_mode_entry(e)) {
1165 if (e->lease_idx == i) {
1169 if (j == d->num_share_modes) {
1170 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1175 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1176 "%"PRIu32"\n", i, j));
1178 send_break_to_none(state->sconn->msg_ctx, e);
1181 for(i = 0; i < d->num_share_modes; i++) {
1182 struct share_mode_entry *e = &d->share_modes[i];
1184 if (!is_valid_share_mode_entry(e)) {
1187 if (e->op_type == LEASE_OPLOCK) {
1189 * Took care of those in the loop above
1195 * As there could have been multiple writes waiting at the
1196 * lock_share_entry gate we may not be the first to
1197 * enter. Hence the state of the op_types in the share mode
1198 * entries may be partly NO_OPLOCK and partly LEVEL_II
1199 * oplock. It will do no harm to re-send break messages to
1200 * those smbd's that are still waiting their turn to remove
1201 * their LEVEL_II state, and also no harm to ignore existing
1202 * NO_OPLOCK states. JRA.
1205 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1208 if (e->op_type == NO_OPLOCK) {
1213 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1214 DEBUG(0,("%s: PANIC. "
1215 "share mode entry %d is an exclusive "
1216 "oplock !\n", __func__, i ));
1221 send_break_to_none(state->sconn->msg_ctx, e);
1224 /* We let the message receivers handle removing the oplock state
1225 in the share mode lock db. */
1233 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1234 enum level2_contention_type type)
1236 struct smbd_server_connection *sconn = fsp->conn->sconn;
1237 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1239 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
1240 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
1244 contend_level2_oplocks_begin_default(fsp, type);
1247 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1248 enum level2_contention_type type)
1250 struct smbd_server_connection *sconn = fsp->conn->sconn;
1251 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1253 /* Only kernel oplocks implement this so far */
1254 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
1255 koplocks->ops->contend_level2_oplocks_end(fsp, type);
1259 /****************************************************************************
1260 Linearize a share mode entry struct to an internal oplock break message.
1261 ****************************************************************************/
1263 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
1265 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1266 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1267 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1268 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1269 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1270 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1271 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1272 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1273 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
1274 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1275 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1276 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1277 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1278 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1281 /****************************************************************************
1282 De-linearize an internal oplock break message to a share mode entry struct.
1283 ****************************************************************************/
1285 void message_to_share_mode_entry(struct share_mode_entry *e, const char *msg)
1287 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1288 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1289 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1290 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1291 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1292 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1293 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1294 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1295 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
1296 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1297 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1298 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1299 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1300 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1303 /****************************************************************************
1304 Setup oplocks for this process.
1305 ****************************************************************************/
1307 bool init_oplocks(struct smbd_server_connection *sconn)
1309 DEBUG(3,("init_oplocks: initializing messages.\n"));
1311 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1312 process_oplock_break_message);
1313 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1314 process_kernel_oplock_break);
1318 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1320 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1322 /* only initialize once */
1323 if (koplocks == NULL) {
1324 #if HAVE_KERNEL_OPLOCKS_IRIX
1325 koplocks = irix_init_kernel_oplocks(sconn);
1326 #elif HAVE_KERNEL_OPLOCKS_LINUX
1327 koplocks = linux_init_kernel_oplocks(sconn);
1329 sconn->oplocks.kernel_ops = koplocks;