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) {
64 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
65 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
66 "don't support them\n"));
67 return NT_STATUS_NOT_SUPPORTED;
71 if ((fsp->oplock_type != NO_OPLOCK) &&
73 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
75 return map_nt_error_from_unix(errno);
78 fsp->sent_oplock_break = NO_BREAK_SENT;
79 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
80 sconn->oplocks.level_II_open++;
81 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
82 sconn->oplocks.exclusive_open++;
85 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
86 "tv_sec = %x, tv_usec = %x\n",
87 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
88 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
89 (int)fsp->open_time.tv_usec ));
94 /****************************************************************************
95 Attempt to release an oplock on a file. Decrements oplock count.
96 ****************************************************************************/
98 static void release_file_oplock(files_struct *fsp)
100 struct smbd_server_connection *sconn = fsp->conn->sconn;
101 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
102 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
105 if ((fsp->oplock_type != NO_OPLOCK) &&
107 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
110 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
111 sconn->oplocks.level_II_open--;
112 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
113 sconn->oplocks.exclusive_open--;
116 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
117 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
119 fsp->oplock_type = NO_OPLOCK;
120 fsp->sent_oplock_break = NO_BREAK_SENT;
122 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
123 delete_write_cache(fsp);
125 TALLOC_FREE(fsp->oplock_timeout);
128 /****************************************************************************
129 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
130 ****************************************************************************/
132 static void downgrade_file_oplock(files_struct *fsp)
134 struct smbd_server_connection *sconn = fsp->conn->sconn;
135 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
136 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
139 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
140 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
145 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
147 fsp->oplock_type = LEVEL_II_OPLOCK;
148 sconn->oplocks.exclusive_open--;
149 sconn->oplocks.level_II_open++;
150 fsp->sent_oplock_break = NO_BREAK_SENT;
152 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
153 delete_write_cache(fsp);
155 TALLOC_FREE(fsp->oplock_timeout);
158 uint32_t get_lease_type(const struct share_mode_data *d,
159 const struct share_mode_entry *e)
161 if (e->op_type == LEASE_OPLOCK) {
162 return d->leases[e->lease_idx].current_state;
164 return map_oplock_to_lease_type(e->op_type);
167 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
169 struct share_mode_data *d = lck->data;
170 struct byte_range_lock *br_lck;
171 uint32_t num_read_oplocks = 0;
174 if (fsp_lease_type_is_exclusive(fsp)) {
175 const struct share_mode_entry *e = NULL;
176 uint32_t e_lease_type = 0;
179 * If we're fully exclusive, we don't need a brlock entry
181 remove_stale_share_mode_entries(d);
183 e = find_share_mode_entry(lck, fsp);
185 e_lease_type = get_lease_type(d, e);
188 if (!lease_type_is_exclusive(e_lease_type)) {
189 char *timestr = NULL;
191 timestr = timeval_string(talloc_tos(),
195 NDR_PRINT_DEBUG(share_mode_data, d);
196 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
197 "open_time[%s] lease_type [0x%x] "
198 "oplock_type [0x%x]\n",
200 file_id_string_tos(&fsp->file_id),
201 fsp->fh->gen_id, timestr,
202 e_lease_type, fsp->oplock_type);
204 smb_panic("Found non-exclusive lease");
210 for (i=0; i<d->num_share_modes; i++) {
211 struct share_mode_entry *e = &d->share_modes[i];
212 uint32_t e_lease_type = get_lease_type(d, e);
214 if (e_lease_type & SMB2_LEASE_READ) {
215 num_read_oplocks += 1;
219 br_lck = brl_get_locks_readonly(fsp);
220 if (br_lck == NULL) {
223 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
227 br_lck = brl_get_locks(talloc_tos(), fsp);
228 if (br_lck == NULL) {
231 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
236 /****************************************************************************
237 Remove a file oplock with lock already held. Copes with level II and exclusive.
238 ****************************************************************************/
240 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
244 ret = remove_share_oplock(lck, fsp);
246 DBG_ERR("failed to remove share oplock for "
248 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
249 file_id_string_tos(&fsp->file_id));
251 release_file_oplock(fsp);
253 ret = update_num_read_oplocks(fsp, lck);
255 DBG_ERR("update_num_read_oplocks failed for "
257 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
258 file_id_string_tos(&fsp->file_id));
264 /****************************************************************************
265 Remove a file oplock. Copes with level II and exclusive.
266 Locks then unlocks the share mode lock. Client can decide to go directly
267 to none even if a "break-to-level II" was sent.
268 ****************************************************************************/
270 bool remove_oplock(files_struct *fsp)
273 struct share_mode_lock *lck;
275 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
277 /* Remove the oplock flag from the sharemode. */
278 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
280 DBG_ERR("failed to lock share entry for "
281 "file %s\n", fsp_str_dbg(fsp));
285 ret = remove_oplock_under_lock(fsp, lck);
292 * Deal with a reply when a break-to-level II was sent.
294 bool downgrade_oplock(files_struct *fsp)
297 struct share_mode_lock *lck;
299 DEBUG(10, ("downgrade_oplock called for %s\n",
302 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
304 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
305 "file %s\n", fsp_str_dbg(fsp)));
308 ret = downgrade_share_oplock(lck, fsp);
310 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
311 "for file %s, %s, file_id %s\n",
312 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
313 file_id_string_tos(&fsp->file_id)));
315 downgrade_file_oplock(fsp);
317 ret = update_num_read_oplocks(fsp, lck);
319 DEBUG(0, ("%s: update_num_read_oplocks failed for "
321 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
322 file_id_string_tos(&fsp->file_id)));
329 static void lease_timeout_handler(struct tevent_context *ctx,
330 struct tevent_timer *te,
334 struct fsp_lease *lease =
335 talloc_get_type_abort(private_data,
337 struct files_struct *fsp;
338 struct share_mode_lock *lck;
339 uint16_t old_epoch = lease->lease.lease_epoch;
341 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
342 &lease->lease.lease_key);
345 TALLOC_FREE(lease->timeout);
349 lck = get_existing_share_mode_lock(
350 talloc_tos(), fsp->file_id);
353 TALLOC_FREE(lease->timeout);
357 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
359 if (lease->lease.lease_epoch != old_epoch) {
361 * If the epoch changed we need to wait for
362 * the next timeout to happen.
364 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
370 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
372 * If the epoch changed we need to wait for
373 * the next timeout to happen.
375 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
381 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
383 (void)downgrade_lease(lease->sconn->client->connections,
386 &lease->lease.lease_key,
392 bool fsp_lease_update(struct share_mode_lock *lck,
393 const struct GUID *client_guid,
394 struct fsp_lease *lease)
396 struct share_mode_data *d = lck->data;
398 struct share_mode_lease *l = NULL;
400 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
406 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
407 TALLOC_FREE(lease->timeout);
408 lease->lease.lease_state = SMB2_LEASE_NONE;
409 lease->lease.lease_epoch += 1;
410 lease->lease.lease_flags = 0;
414 DEBUG(10,("%s: refresh lease state\n", __func__));
416 /* Ensure we're in sync with current lease state. */
417 if (lease->lease.lease_epoch != l->epoch) {
418 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
419 TALLOC_FREE(lease->timeout);
421 lease->lease.lease_epoch = l->epoch;
422 lease->lease.lease_state = l->current_state;
425 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
427 if (lease->timeout == NULL) {
428 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
430 DEBUG(10,("%s: setup timeout handler\n", __func__));
432 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
434 lease_timeout_handler,
436 if (lease->timeout == NULL) {
437 DEBUG(0, ("%s: Could not add lease timeout handler\n",
442 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
443 TALLOC_FREE(lease->timeout);
449 struct downgrade_lease_additional_state {
450 struct tevent_immediate *im;
451 struct smbXsrv_connection *xconn;
452 uint32_t break_flags;
453 struct smb2_lease_key lease_key;
459 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
460 struct tevent_immediate *im,
463 struct downgrade_lease_additional_state *state =
464 talloc_get_type_abort(private_data,
465 struct downgrade_lease_additional_state);
466 struct smbXsrv_connection *xconn = state->xconn;
469 status = smbd_smb2_send_lease_break(xconn,
476 if (!NT_STATUS_IS_OK(status)) {
477 smbd_server_connection_terminate(xconn,
483 struct downgrade_lease_fsps_state {
485 struct share_mode_lock *lck;
486 const struct smb2_lease_key *key;
489 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
492 struct downgrade_lease_fsps_state *state =
493 (struct downgrade_lease_fsps_state *)private_data;
495 if (fsp->oplock_type != LEASE_OPLOCK) {
498 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
501 if (!file_id_equal(&fsp->file_id, &state->id)) {
505 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
510 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
511 uint32_t num_file_ids,
512 const struct file_id *ids,
513 const struct smb2_lease_key *key,
514 uint32_t lease_state)
516 struct smbd_server_connection *sconn = xconn->client->sconn;
517 struct share_mode_lock *lck;
518 struct share_mode_lease *l = NULL;
519 const struct file_id id = ids[0];
523 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
524 file_id_string_tos(&id), (unsigned)lease_state));
526 lck = get_existing_share_mode_lock(talloc_tos(), id);
528 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
530 status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
532 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
533 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
535 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
536 struct downgrade_lease_additional_state *state;
538 state = talloc_zero(xconn,
539 struct downgrade_lease_additional_state);
542 return NT_STATUS_NO_MEMORY;
545 state->im = tevent_create_immediate(state);
546 if (state->im == NULL) {
549 return NT_STATUS_NO_MEMORY;
552 state->xconn = xconn;
553 if (l->current_state & (~SMB2_LEASE_READ)) {
554 state->break_flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
556 state->lease_key = l->lease_key;
557 state->break_from = l->current_state;
558 state->break_to = l->breaking_to_requested;
559 if (l->lease_version > 1) {
560 state->new_epoch = l->epoch;
563 if (state->break_flags == 0) {
565 * This is an async break without
566 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
568 * we need to store NONE state in the
571 l->current_state = 0;
572 l->breaking_to_requested = 0;
573 l->breaking_to_required = 0;
576 lck->data->modified = true;
579 tevent_schedule_immediate(state->im,
580 xconn->client->raw_ev_ctx,
581 downgrade_lease_additional_trigger,
586 struct downgrade_lease_fsps_state state = {
587 .id = id, .lck = lck, .key = key,
590 files_forall(sconn, downgrade_lease_fsps, &state);
594 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
595 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
598 * Dynamic share case. Ensure other opens are copies.
599 * This will only be breaking to NONE.
602 for (i = 1; i < num_file_ids; i++) {
603 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
605 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
609 struct downgrade_lease_fsps_state state = {
610 .id = ids[i], .lck = lck, .key = key,
613 files_forall(sconn, downgrade_lease_fsps, &state);
616 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
617 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
625 /****************************************************************************
626 Set up an oplock break message.
627 ****************************************************************************/
629 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
631 static void new_break_message_smb1(files_struct *fsp, int cmd,
632 char result[SMB1_BREAK_MESSAGE_LENGTH])
634 memset(result,'\0',smb_size);
635 srv_set_message(result,8,0,true);
636 SCVAL(result,smb_com,SMBlockingX);
637 SSVAL(result,smb_tid,fsp->conn->cnum);
638 SSVAL(result,smb_pid,0xFFFF);
639 SSVAL(result,smb_uid,0);
640 SSVAL(result,smb_mid,0xFFFF);
641 SCVAL(result,smb_vwv0,0xFF);
642 SSVAL(result,smb_vwv2,fsp->fnum);
643 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
644 SCVAL(result,smb_vwv3+1,cmd);
647 /****************************************************************************
648 Function to do the waiting before sending a local break.
649 ****************************************************************************/
651 static void wait_before_sending_break(void)
653 long wait_time = (long)lp_oplock_break_wait_time();
656 smb_msleep(wait_time);
660 /****************************************************************************
661 Ensure that we have a valid oplock.
662 ****************************************************************************/
664 static files_struct *initial_break_processing(
665 struct smbd_server_connection *sconn, struct file_id id,
666 unsigned long file_id)
668 files_struct *fsp = NULL;
670 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
671 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
672 file_id_string_tos(&id), (int)file_id,
673 sconn->oplocks.exclusive_open,
674 sconn->oplocks.level_II_open));
677 * We need to search the file open table for the
678 * entry containing this dev and inode, and ensure
679 * we have an oplock on it.
682 fsp = file_find_dif(sconn, id, file_id);
685 /* The file could have been closed in the meantime - return success. */
686 DEBUG(3, ("initial_break_processing: cannot find open file "
687 "with file_id %s gen_id = %lu, allowing break to "
688 "succeed.\n", file_id_string_tos(&id), file_id));
692 /* Ensure we have an oplock on the file */
695 * There is a potential race condition in that an oplock could
696 * have been broken due to another udp request, and yet there are
697 * still oplock break messages being sent in the udp message
698 * queue for this file. So return true if we don't have an oplock,
699 * as we may have just freed it.
702 if(fsp->oplock_type == NO_OPLOCK) {
703 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
704 "gen_id = %lu) has no oplock. Allowing break to "
705 "succeed regardless.\n", fsp_str_dbg(fsp),
706 file_id_string_tos(&id), fsp->fh->gen_id));
713 static void oplock_timeout_handler(struct tevent_context *ctx,
714 struct tevent_timer *te,
718 files_struct *fsp = (files_struct *)private_data;
720 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
722 /* Remove the timed event handler. */
723 TALLOC_FREE(fsp->oplock_timeout);
724 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
729 /*******************************************************************
730 Add a timeout handler waiting for the client reply.
731 *******************************************************************/
733 static void add_oplock_timeout_handler(files_struct *fsp)
735 struct smbd_server_connection *sconn = fsp->conn->sconn;
736 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
737 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
741 * If kernel oplocks already notifies smbds when an oplock break times
745 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
749 if (fsp->oplock_timeout != NULL) {
750 DEBUG(0, ("Logic problem -- have an oplock event hanging "
754 fsp->oplock_timeout =
755 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
756 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
757 oplock_timeout_handler, fsp);
759 if (fsp->oplock_timeout == NULL) {
760 DEBUG(0, ("Could not add oplock timeout handler\n"));
764 static void send_break_message_smb1(files_struct *fsp, int level)
766 struct smbXsrv_connection *xconn = NULL;
767 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
770 * For SMB1 we only have one connection
772 xconn = fsp->conn->sconn->client->connections;
774 new_break_message_smb1(fsp, level, break_msg);
777 if (!srv_send_smb(xconn,
779 IS_CONN_ENCRYPTED(fsp->conn),
781 exit_server_cleanly("send_break_message_smb1: "
782 "srv_send_smb failed.");
786 /*******************************************************************
787 This handles the generic oplock break message from another smbd.
788 *******************************************************************/
790 static void process_oplock_break_message(struct messaging_context *msg_ctx,
793 struct server_id src,
797 struct share_mode_entry msg;
800 struct smbd_server_connection *sconn =
801 talloc_get_type_abort(private_data,
802 struct smbd_server_connection);
803 struct server_id self = messaging_server_id(sconn->msg_ctx);
804 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
807 bool break_needed = true;
808 struct server_id_buf tmp;
810 if (data->data == NULL) {
811 DEBUG(0, ("Got NULL buffer\n"));
815 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
816 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
820 /* De-linearize incoming message. */
821 message_to_share_mode_entry(&id, &msg, (char *)data->data);
822 break_to = msg.op_type;
824 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
825 (unsigned)break_to, server_id_str_buf(src, &tmp),
826 file_id_string_tos(&id),
827 (unsigned long long)msg.share_file_id));
829 fsp = initial_break_processing(sconn, id, msg.share_file_id);
832 /* We hit a race here. Break messages are sent, and before we
833 * get to process this message, we have closed the file. */
834 DEBUG(3, ("Did not find fsp\n"));
838 break_from = fsp_lease_type(fsp);
840 if (fsp->oplock_type != LEASE_OPLOCK) {
841 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
843 * Nothing to do anymore
845 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
846 fsp->sent_oplock_break));
851 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
852 DEBUG(10, ("client_caps without level2 oplocks\n"));
853 break_to &= ~SMB2_LEASE_READ;
856 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
858 if (use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
859 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
860 break_to &= ~SMB2_LEASE_READ;
863 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
864 DEBUG(10, ("no level2 oplocks by config\n"));
865 break_to &= ~SMB2_LEASE_READ;
868 if (fsp->oplock_type == LEASE_OPLOCK) {
869 struct share_mode_lock *lck;
872 lck = get_existing_share_mode_lock(
873 talloc_tos(), fsp->file_id);
876 * We hit a race here. Break messages are sent, and
877 * before we get to process this message, we have closed
880 DEBUG(3, ("Did not find share_mode\n"));
884 idx = find_share_mode_lease(
886 fsp_client_guid(fsp),
887 &fsp->lease->lease.lease_key);
889 struct share_mode_lease *l;
890 l = &lck->data->leases[idx];
892 break_from = l->current_state;
893 break_to &= l->current_state;
896 break_to &= l->breaking_to_required;
897 if (l->breaking_to_required != break_to) {
899 * Note we don't increment the epoch
900 * here, which might be a bug in
903 l->breaking_to_required = break_to;
904 lck->data->modified = true;
906 break_needed = false;
907 } else if (l->current_state == break_to) {
908 break_needed = false;
909 } else if (l->current_state == SMB2_LEASE_READ) {
910 l->current_state = SMB2_LEASE_NONE;
911 /* Need to increment the epoch */
913 lck->data->modified = true;
916 l->breaking_to_required = break_to;
917 l->breaking_to_requested = break_to;
918 /* Need to increment the epoch */
920 lck->data->modified = true;
923 /* Ensure we're in sync with current lease state. */
924 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
931 DEBUG(10,("%s: skip break\n", __func__));
935 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
936 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
937 file_id_string_tos(&fsp->file_id),
942 DEBUG(10, ("break_from=%u, break_to=%u\n",
943 (unsigned)break_from, (unsigned)break_to));
945 if ((break_from == break_to) && !break_needed) {
946 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
948 file_id_string_tos(&fsp->file_id),
953 /* Need to wait before sending a break
954 message if we sent ourselves this message. */
955 if (serverid_equal(&self, &src)) {
956 wait_before_sending_break();
959 if (sconn->using_smb2) {
960 send_break_message_smb2(fsp, break_from, break_to);
962 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
963 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
966 if ((break_from == SMB2_LEASE_READ) &&
967 (break_to == SMB2_LEASE_NONE)) {
969 * This is an async break without a reply and thus no timeout
971 * leases are handled above.
973 if (fsp->oplock_type != LEASE_OPLOCK) {
978 if (fsp->oplock_type == LEASE_OPLOCK) {
982 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
983 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
985 add_oplock_timeout_handler(fsp);
988 /*******************************************************************
989 This handles the kernel oplock break message.
990 *******************************************************************/
992 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
995 struct server_id src,
999 unsigned long file_id;
1001 struct smbd_server_connection *sconn =
1002 talloc_get_type_abort(private_data,
1003 struct smbd_server_connection);
1004 struct server_id_buf tmp;
1006 if (data->data == NULL) {
1007 DEBUG(0, ("Got NULL buffer\n"));
1011 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1012 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1016 /* Pull the data from the message. */
1017 pull_file_id_24((char *)data->data, &id);
1018 file_id = (unsigned long)IVAL(data->data, 24);
1020 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1021 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1022 (unsigned int)file_id));
1024 fsp = initial_break_processing(sconn, id, file_id);
1027 DEBUG(3, ("Got a kernel oplock break message for a file "
1028 "I don't know about\n"));
1032 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1033 /* This is ok, kernel oplocks come in completely async */
1034 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1039 if (sconn->using_smb2) {
1040 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1042 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1045 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1047 add_oplock_timeout_handler(fsp);
1050 struct break_to_none_state {
1051 struct smbd_server_connection *sconn;
1053 struct smb2_lease_key lease_key;
1054 struct GUID client_guid;
1056 static void do_break_to_none(struct tevent_context *ctx,
1057 struct tevent_immediate *im,
1058 void *private_data);
1060 /****************************************************************************
1061 This function is called on any file modification or lock request. If a file
1062 is level 2 oplocked then it must tell all other level 2 holders to break to
1064 ****************************************************************************/
1066 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1067 enum level2_contention_type type)
1069 struct smbd_server_connection *sconn = fsp->conn->sconn;
1070 struct tevent_immediate *im;
1071 struct break_to_none_state *state;
1072 struct byte_range_lock *brl;
1073 uint32_t num_read_oplocks = 0;
1076 * If this file is level II oplocked then we need
1077 * to grab the shared memory lock and inform all
1078 * other files with a level II lock that they need
1079 * to flush their read caches. We keep the lock over
1080 * the shared memory area whilst doing this.
1083 if (fsp_lease_type_is_exclusive(fsp)) {
1085 * There can't be any level2 oplocks, we're alone.
1090 brl = brl_get_locks_readonly(fsp);
1092 num_read_oplocks = brl_num_read_oplocks(brl);
1095 DEBUG(10, ("num_read_oplocks = %"PRIu32"\n", num_read_oplocks));
1097 if (num_read_oplocks == 0) {
1098 DEBUG(10, ("No read oplocks around\n"));
1103 * When we get here we might have a brlock entry locked. Also
1104 * locking the share mode entry would violate the locking
1105 * order. Breaking level2 oplocks to none is asynchronous
1106 * anyway, so we postpone this into an immediate event.
1109 state = talloc_zero(sconn, struct break_to_none_state);
1110 if (state == NULL) {
1111 DEBUG(1, ("talloc failed\n"));
1114 state->sconn = sconn;
1115 state->id = fsp->file_id;
1117 if (fsp->oplock_type == LEASE_OPLOCK) {
1118 state->client_guid = *fsp_client_guid(fsp);
1119 state->lease_key = fsp->lease->lease.lease_key;
1120 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1121 state->lease_key.data[0],
1122 state->lease_key.data[1]));
1125 im = tevent_create_immediate(state);
1127 DEBUG(1, ("tevent_create_immediate failed\n"));
1131 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1134 static void send_break_to_none(struct messaging_context *msg_ctx,
1135 const struct file_id *id,
1136 const struct share_mode_entry *e)
1138 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1140 share_mode_entry_to_message(msg, id, e);
1141 /* Overload entry->op_type */
1142 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1144 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1145 (uint8_t *)msg, sizeof(msg));
1148 static void do_break_to_none(struct tevent_context *ctx,
1149 struct tevent_immediate *im,
1152 struct break_to_none_state *state = talloc_get_type_abort(
1153 private_data, struct break_to_none_state);
1155 struct share_mode_lock *lck;
1156 struct share_mode_data *d;
1158 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1160 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1161 __func__, file_id_string_tos(&state->id)));
1167 * Walk leases and oplocks separately: We have to send one break per
1168 * lease. If we have multiple share_mode_entry having a common lease,
1169 * we would break the lease twice if we don't walk the leases list
1173 for (i=0; i<d->num_leases; i++) {
1174 struct share_mode_lease *l = &d->leases[i];
1175 struct share_mode_entry *e = NULL;
1178 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1181 if (smb2_lease_equal(&state->client_guid,
1185 DEBUG(10, ("Don't break our own lease\n"));
1189 for (j=0; j<d->num_share_modes; j++) {
1190 e = &d->share_modes[j];
1192 if (!is_valid_share_mode_entry(e)) {
1195 if (e->lease_idx == i) {
1199 if (j == d->num_share_modes) {
1200 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1205 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1206 "%"PRIu32"\n", i, j));
1208 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1211 for(i = 0; i < d->num_share_modes; i++) {
1212 struct share_mode_entry *e = &d->share_modes[i];
1214 if (!is_valid_share_mode_entry(e)) {
1217 if (e->op_type == LEASE_OPLOCK) {
1219 * Took care of those in the loop above
1225 * As there could have been multiple writes waiting at the
1226 * lock_share_entry gate we may not be the first to
1227 * enter. Hence the state of the op_types in the share mode
1228 * entries may be partly NO_OPLOCK and partly LEVEL_II
1229 * oplock. It will do no harm to re-send break messages to
1230 * those smbd's that are still waiting their turn to remove
1231 * their LEVEL_II state, and also no harm to ignore existing
1232 * NO_OPLOCK states. JRA.
1235 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1238 if (e->op_type == NO_OPLOCK) {
1243 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1244 DEBUG(0,("%s: PANIC. "
1245 "share mode entry %d is an exclusive "
1246 "oplock !\n", __func__, i ));
1251 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1254 /* We let the message receivers handle removing the oplock state
1255 in the share mode lock db. */
1263 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1264 enum level2_contention_type type)
1266 struct smbd_server_connection *sconn = fsp->conn->sconn;
1267 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1268 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
1271 if (use_kernel && koplocks->ops->contend_level2_oplocks_begin) {
1272 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
1276 contend_level2_oplocks_begin_default(fsp, type);
1279 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1280 enum level2_contention_type type)
1282 struct smbd_server_connection *sconn = fsp->conn->sconn;
1283 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1284 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
1287 /* Only kernel oplocks implement this so far */
1288 if (use_kernel && koplocks->ops->contend_level2_oplocks_end) {
1289 koplocks->ops->contend_level2_oplocks_end(fsp, type);
1293 /****************************************************************************
1294 Linearize a share mode entry struct to an internal oplock break message.
1295 ****************************************************************************/
1297 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1298 const struct share_mode_entry *e)
1300 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1301 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1302 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1303 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1304 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1305 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1306 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1307 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1309 * "id" used to be part of share_mode_entry, thus the strange
1310 * place to put this. Feel free to move somewhere else :-)
1312 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1313 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1314 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1315 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1316 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1317 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1320 /****************************************************************************
1321 De-linearize an internal oplock break message to a share mode entry struct.
1322 ****************************************************************************/
1324 void message_to_share_mode_entry(struct file_id *id,
1325 struct share_mode_entry *e,
1328 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1329 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1330 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1331 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1332 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1333 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1334 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1335 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1337 * "id" used to be part of share_mode_entry, thus the strange
1338 * place to put this. Feel free to move somewhere else :-)
1340 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1341 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1342 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1343 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1344 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1345 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1348 /****************************************************************************
1349 Setup oplocks for this process.
1350 ****************************************************************************/
1352 bool init_oplocks(struct smbd_server_connection *sconn)
1354 DEBUG(3,("init_oplocks: initializing messages.\n"));
1356 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1357 process_oplock_break_message);
1358 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1359 process_kernel_oplock_break);
1363 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1365 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1367 /* only initialize once */
1368 if (koplocks == NULL) {
1369 #if HAVE_KERNEL_OPLOCKS_IRIX
1370 koplocks = irix_init_kernel_oplocks(sconn);
1371 #elif HAVE_KERNEL_OPLOCKS_LINUX
1372 koplocks = linux_init_kernel_oplocks(sconn);
1374 sconn->oplocks.kernel_ops = koplocks;