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/ndr_open_files.h"
31 * helper function used by the kernel oplock backends to post the break message
33 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
35 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
37 /* Put the kernel break info into the message. */
38 push_file_id_24((char *)msg, &fsp->file_id);
39 SIVAL(msg,24,fsp->fh->gen_id);
41 /* Don't need to be root here as we're only ever
42 sending to ourselves. */
44 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
46 msg, MSG_SMB_KERNEL_BREAK_SIZE);
49 /****************************************************************************
50 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
51 disabled (just sets flags).
52 ****************************************************************************/
54 NTSTATUS set_file_oplock(files_struct *fsp)
56 struct smbd_server_connection *sconn = fsp->conn->sconn;
57 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
58 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
61 if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
62 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
63 "don't support them\n"));
64 return NT_STATUS_NOT_SUPPORTED;
67 if ((fsp->oplock_type != NO_OPLOCK) &&
69 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
71 return map_nt_error_from_unix(errno);
74 fsp->sent_oplock_break = NO_BREAK_SENT;
75 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
76 sconn->oplocks.level_II_open++;
77 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
78 sconn->oplocks.exclusive_open++;
81 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
82 "tv_sec = %x, tv_usec = %x\n",
83 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
84 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
85 (int)fsp->open_time.tv_usec ));
90 /****************************************************************************
91 Attempt to release an oplock on a file. Decrements oplock count.
92 ****************************************************************************/
94 static void release_file_oplock(files_struct *fsp)
96 struct smbd_server_connection *sconn = fsp->conn->sconn;
97 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
98 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
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;
132 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
135 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
136 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
141 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
143 fsp->oplock_type = LEVEL_II_OPLOCK;
144 sconn->oplocks.exclusive_open--;
145 sconn->oplocks.level_II_open++;
146 fsp->sent_oplock_break = NO_BREAK_SENT;
148 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
149 delete_write_cache(fsp);
151 TALLOC_FREE(fsp->oplock_timeout);
154 uint32_t get_lease_type(const struct share_mode_data *d,
155 const struct share_mode_entry *e)
157 if (e->op_type == LEASE_OPLOCK) {
158 return d->leases[e->lease_idx].current_state;
160 return map_oplock_to_lease_type(e->op_type);
163 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
165 struct share_mode_data *d = lck->data;
166 struct byte_range_lock *br_lck;
167 uint32_t num_read_oplocks = 0;
170 if (fsp_lease_type_is_exclusive(fsp)) {
171 const struct share_mode_entry *e = NULL;
172 uint32_t e_lease_type = 0;
175 * If we're fully exclusive, we don't need a brlock entry
177 remove_stale_share_mode_entries(d);
179 e = find_share_mode_entry(lck, fsp);
181 e_lease_type = get_lease_type(d, e);
184 if (!lease_type_is_exclusive(e_lease_type)) {
185 char *timestr = NULL;
187 timestr = timeval_string(talloc_tos(),
191 NDR_PRINT_DEBUG(share_mode_data, d);
192 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
193 "open_time[%s] lease_type [0x%x] "
194 "oplock_type [0x%x]\n",
196 file_id_string_tos(&fsp->file_id),
197 fsp->fh->gen_id, timestr,
198 e_lease_type, fsp->oplock_type);
200 smb_panic("Found non-exclusive lease");
206 for (i=0; i<d->num_share_modes; i++) {
207 struct share_mode_entry *e = &d->share_modes[i];
208 uint32_t e_lease_type = get_lease_type(d, e);
210 if (e_lease_type & SMB2_LEASE_READ) {
211 num_read_oplocks += 1;
215 br_lck = brl_get_locks_readonly(fsp);
216 if (br_lck == NULL) {
219 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
223 br_lck = brl_get_locks(talloc_tos(), fsp);
224 if (br_lck == NULL) {
227 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
232 /****************************************************************************
233 Remove a file oplock with lock already held. Copes with level II and exclusive.
234 ****************************************************************************/
236 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
240 ret = remove_share_oplock(lck, fsp);
242 DBG_ERR("failed to remove share oplock for "
244 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
245 file_id_string_tos(&fsp->file_id));
247 release_file_oplock(fsp);
249 ret = update_num_read_oplocks(fsp, lck);
251 DBG_ERR("update_num_read_oplocks failed for "
253 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
254 file_id_string_tos(&fsp->file_id));
260 /****************************************************************************
261 Remove a file oplock. Copes with level II and exclusive.
262 Locks then unlocks the share mode lock. Client can decide to go directly
263 to none even if a "break-to-level II" was sent.
264 ****************************************************************************/
266 bool remove_oplock(files_struct *fsp)
269 struct share_mode_lock *lck;
271 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
273 /* Remove the oplock flag from the sharemode. */
274 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
276 DBG_ERR("failed to lock share entry for "
277 "file %s\n", fsp_str_dbg(fsp));
281 ret = remove_oplock_under_lock(fsp, lck);
288 * Deal with a reply when a break-to-level II was sent.
290 bool downgrade_oplock(files_struct *fsp)
293 struct share_mode_lock *lck;
295 DEBUG(10, ("downgrade_oplock called for %s\n",
298 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
300 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
301 "file %s\n", fsp_str_dbg(fsp)));
304 ret = downgrade_share_oplock(lck, fsp);
306 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
307 "for file %s, %s, file_id %s\n",
308 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
309 file_id_string_tos(&fsp->file_id)));
311 downgrade_file_oplock(fsp);
313 ret = update_num_read_oplocks(fsp, lck);
315 DEBUG(0, ("%s: update_num_read_oplocks failed for "
317 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
318 file_id_string_tos(&fsp->file_id)));
325 static void lease_timeout_handler(struct tevent_context *ctx,
326 struct tevent_timer *te,
330 struct fsp_lease *lease =
331 talloc_get_type_abort(private_data,
333 struct files_struct *fsp;
334 struct share_mode_lock *lck;
335 uint16_t old_epoch = lease->lease.lease_epoch;
338 * This function runs without any specific impersonation
339 * and must not call any SMB_VFS operations!
342 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
343 &lease->lease.lease_key);
346 TALLOC_FREE(lease->timeout);
351 * Paranoia check: There can only be one fsp_lease per lease
354 SMB_ASSERT(fsp->lease == lease);
356 lck = get_existing_share_mode_lock(
357 talloc_tos(), fsp->file_id);
360 TALLOC_FREE(lease->timeout);
364 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
366 if (lease->lease.lease_epoch != old_epoch) {
368 * If the epoch changed we need to wait for
369 * the next timeout to happen.
371 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
377 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
379 * If the epoch changed we need to wait for
380 * the next timeout to happen.
382 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
388 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
390 (void)downgrade_lease(lease->sconn->client->connections,
393 &lease->lease.lease_key,
399 bool fsp_lease_update(struct share_mode_lock *lck,
400 const struct GUID *client_guid,
401 struct fsp_lease *lease)
403 struct share_mode_data *d = lck->data;
405 struct share_mode_lease *l = NULL;
407 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
409 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
410 TALLOC_FREE(lease->timeout);
411 lease->lease.lease_state = SMB2_LEASE_NONE;
412 lease->lease.lease_epoch += 1;
413 lease->lease.lease_flags = 0;
419 DEBUG(10,("%s: refresh lease state\n", __func__));
421 /* Ensure we're in sync with current lease state. */
422 if (lease->lease.lease_epoch != l->epoch) {
423 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
424 TALLOC_FREE(lease->timeout);
426 lease->lease.lease_epoch = l->epoch;
427 lease->lease.lease_state = l->current_state;
430 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
432 if (lease->timeout == NULL) {
433 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
435 DEBUG(10,("%s: setup timeout handler\n", __func__));
438 * lease_timeout_handler() only accesses locking.tdb
439 * so we don't use any impersonation and use
440 * the raw tevent context.
442 lease->timeout = tevent_add_timer(lease->sconn->raw_ev_ctx,
444 lease_timeout_handler,
446 if (lease->timeout == NULL) {
447 DEBUG(0, ("%s: Could not add lease timeout handler\n",
452 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
453 TALLOC_FREE(lease->timeout);
459 struct downgrade_lease_additional_state {
460 struct tevent_immediate *im;
461 struct smbXsrv_connection *xconn;
462 uint32_t break_flags;
463 struct smb2_lease_key lease_key;
469 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
470 struct tevent_immediate *im,
473 struct downgrade_lease_additional_state *state =
474 talloc_get_type_abort(private_data,
475 struct downgrade_lease_additional_state);
476 struct smbXsrv_connection *xconn = state->xconn;
479 status = smbd_smb2_send_lease_break(xconn,
486 if (!NT_STATUS_IS_OK(status)) {
487 smbd_server_connection_terminate(xconn,
493 struct downgrade_lease_fsps_state {
495 struct share_mode_lock *lck;
496 const struct smb2_lease_key *key;
499 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
502 struct downgrade_lease_fsps_state *state =
503 (struct downgrade_lease_fsps_state *)private_data;
505 if (fsp->oplock_type != LEASE_OPLOCK) {
508 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
511 if (!file_id_equal(&fsp->file_id, &state->id)) {
515 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
520 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
521 uint32_t num_file_ids,
522 const struct file_id *ids,
523 const struct smb2_lease_key *key,
524 uint32_t lease_state)
526 struct smbd_server_connection *sconn = xconn->client->sconn;
527 struct share_mode_lock *lck;
528 struct share_mode_data *d = NULL;
529 struct share_mode_lease *l = NULL;
530 const struct file_id id = ids[0];
535 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
536 file_id_string_tos(&id), (unsigned)lease_state));
538 lck = get_existing_share_mode_lock(talloc_tos(), id);
540 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
544 idx = find_share_mode_lease(
545 d, &sconn->client->connections->smb2.client.guid, key);
547 DEBUG(10, ("lease not found\n"));
548 return NT_STATUS_INVALID_PARAMETER;
553 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
554 "but we're not in breaking state\n",
555 l->current_state, lease_state);
557 return NT_STATUS_UNSUCCESSFUL;
561 * Can't upgrade anything: l->breaking_to_requested (and l->current_state)
562 * must be a strict bitwise superset of new_lease_state
564 if ((lease_state & l->breaking_to_requested) != lease_state) {
565 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
566 "- expected %"PRIu32"\n",
567 l->current_state, lease_state,
568 l->breaking_to_requested);
570 return NT_STATUS_REQUEST_NOT_ACCEPTED;
573 if (l->current_state != lease_state) {
574 l->current_state = lease_state;
578 status = NT_STATUS_OK;
582 if ((lease_state & ~l->breaking_to_required) != 0) {
583 struct downgrade_lease_additional_state *state;
585 DBG_INFO("lease state %"PRIu32" not fully broken from "
586 "%"PRIu32" to %"PRIu32"\n",
589 l->breaking_to_required);
591 l->breaking_to_requested = l->breaking_to_required;
593 if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
595 * Here we break in steps, as windows does
596 * see the breaking3 and v2_breaking3 tests.
598 l->breaking_to_requested |= SMB2_LEASE_READ;
601 state = talloc_zero(xconn,
602 struct downgrade_lease_additional_state);
605 return NT_STATUS_NO_MEMORY;
608 state->im = tevent_create_immediate(state);
609 if (state->im == NULL) {
612 return NT_STATUS_NO_MEMORY;
615 state->xconn = xconn;
616 state->lease_key = l->lease_key;
617 state->break_from = l->current_state;
618 state->break_to = l->breaking_to_requested;
619 if (l->lease_version > 1) {
620 state->new_epoch = l->epoch;
623 if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
625 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
628 * This is an async break without
629 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
631 * we need to store NONE state in the
634 l->current_state = 0;
635 l->breaking_to_requested = 0;
636 l->breaking_to_required = 0;
639 lck->data->modified = true;
642 tevent_schedule_immediate(state->im,
643 xconn->client->raw_ev_ctx,
644 downgrade_lease_additional_trigger,
647 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
649 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
650 "expected %"PRIu32"\n",
653 l->breaking_to_requested);
655 l->breaking_to_requested = 0;
656 l->breaking_to_required = 0;
662 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
663 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
666 struct downgrade_lease_fsps_state state = {
667 .id = id, .lck = lck, .key = key,
670 files_forall(sconn, downgrade_lease_fsps, &state);
674 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
675 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
678 * Dynamic share case. Ensure other opens are copies.
679 * This will only be breaking to NONE.
682 for (i = 1; i < num_file_ids; i++) {
683 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
685 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
689 struct downgrade_lease_fsps_state state = {
690 .id = ids[i], .lck = lck, .key = key,
693 files_forall(sconn, downgrade_lease_fsps, &state);
696 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
697 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
705 /****************************************************************************
706 Set up an oplock break message.
707 ****************************************************************************/
709 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
711 static void new_break_message_smb1(files_struct *fsp, int cmd,
712 char result[SMB1_BREAK_MESSAGE_LENGTH])
714 memset(result,'\0',smb_size);
715 srv_set_message(result,8,0,true);
716 SCVAL(result,smb_com,SMBlockingX);
717 SSVAL(result,smb_tid,fsp->conn->cnum);
718 SSVAL(result,smb_pid,0xFFFF);
719 SSVAL(result,smb_uid,0);
720 SSVAL(result,smb_mid,0xFFFF);
721 SCVAL(result,smb_vwv0,0xFF);
722 SSVAL(result,smb_vwv2,fsp->fnum);
723 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
724 SCVAL(result,smb_vwv3+1,cmd);
727 /****************************************************************************
728 Function to do the waiting before sending a local break.
729 ****************************************************************************/
731 static void wait_before_sending_break(void)
733 long wait_time = (long)lp_oplock_break_wait_time();
736 smb_msleep(wait_time);
740 /****************************************************************************
741 Ensure that we have a valid oplock.
742 ****************************************************************************/
744 static files_struct *initial_break_processing(
745 struct smbd_server_connection *sconn, struct file_id id,
746 unsigned long file_id)
748 files_struct *fsp = NULL;
750 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
751 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
752 file_id_string_tos(&id), (int)file_id,
753 sconn->oplocks.exclusive_open,
754 sconn->oplocks.level_II_open));
757 * We need to search the file open table for the
758 * entry containing this dev and inode, and ensure
759 * we have an oplock on it.
762 fsp = file_find_dif(sconn, id, file_id);
765 /* The file could have been closed in the meantime - return success. */
766 DEBUG(3, ("initial_break_processing: cannot find open file "
767 "with file_id %s gen_id = %lu, allowing break to "
768 "succeed.\n", file_id_string_tos(&id), file_id));
772 /* Ensure we have an oplock on the file */
775 * There is a potential race condition in that an oplock could
776 * have been broken due to another udp request, and yet there are
777 * still oplock break messages being sent in the udp message
778 * queue for this file. So return true if we don't have an oplock,
779 * as we may have just freed it.
782 if(fsp->oplock_type == NO_OPLOCK) {
783 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
784 "gen_id = %lu) has no oplock. Allowing break to "
785 "succeed regardless.\n", fsp_str_dbg(fsp),
786 file_id_string_tos(&id), fsp->fh->gen_id));
793 static void oplock_timeout_handler(struct tevent_context *ctx,
794 struct tevent_timer *te,
798 files_struct *fsp = (files_struct *)private_data;
801 * Note this function doesn't run under any specific impersonation and
802 * is not expected to call any SMB_VFS operation!
805 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
807 /* Remove the timed event handler. */
808 TALLOC_FREE(fsp->oplock_timeout);
809 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
814 /*******************************************************************
815 Add a timeout handler waiting for the client reply.
816 *******************************************************************/
818 static void add_oplock_timeout_handler(files_struct *fsp)
820 if (fsp->oplock_timeout != NULL) {
821 DEBUG(0, ("Logic problem -- have an oplock event hanging "
826 * For now we keep the logic and use the
827 * raw event context. We're called from
828 * the messaging system from a raw event context.
829 * Also oplock_timeout_handler doesn't invoke
832 fsp->oplock_timeout =
833 tevent_add_timer(fsp->conn->sconn->raw_ev_ctx, fsp,
834 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
835 oplock_timeout_handler, fsp);
837 if (fsp->oplock_timeout == NULL) {
838 DEBUG(0, ("Could not add oplock timeout handler\n"));
842 static void send_break_message_smb1(files_struct *fsp, int level)
844 struct smbXsrv_connection *xconn = NULL;
845 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
848 * For SMB1 we only have one connection
850 xconn = fsp->conn->sconn->client->connections;
852 new_break_message_smb1(fsp, level, break_msg);
855 if (!srv_send_smb(xconn,
857 IS_CONN_ENCRYPTED(fsp->conn),
859 exit_server_cleanly("send_break_message_smb1: "
860 "srv_send_smb failed.");
864 /*******************************************************************
865 This handles the generic oplock break message from another smbd.
866 *******************************************************************/
868 static void process_oplock_break_message(struct messaging_context *msg_ctx,
871 struct server_id src,
875 struct share_mode_entry msg;
878 struct smbd_server_connection *sconn =
879 talloc_get_type_abort(private_data,
880 struct smbd_server_connection);
881 struct server_id self = messaging_server_id(sconn->msg_ctx);
882 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
885 bool break_needed = true;
886 struct server_id_buf tmp;
888 if (data->data == NULL) {
889 DEBUG(0, ("Got NULL buffer\n"));
893 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
894 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
898 /* De-linearize incoming message. */
899 message_to_share_mode_entry(&id, &msg, (char *)data->data);
900 break_to = msg.op_type;
902 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
903 (unsigned)break_to, server_id_str_buf(src, &tmp),
904 file_id_string_tos(&id),
905 (unsigned long long)msg.share_file_id));
907 fsp = initial_break_processing(sconn, id, msg.share_file_id);
910 /* We hit a race here. Break messages are sent, and before we
911 * get to process this message, we have closed the file. */
912 DEBUG(3, ("Did not find fsp\n"));
916 break_from = fsp_lease_type(fsp);
918 if (fsp->oplock_type != LEASE_OPLOCK) {
919 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
921 * Nothing to do anymore
923 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
924 fsp->sent_oplock_break));
929 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
930 DEBUG(10, ("client_caps without level2 oplocks\n"));
931 break_to &= ~SMB2_LEASE_READ;
934 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
937 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
938 break_to &= ~SMB2_LEASE_READ;
941 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
942 DEBUG(10, ("no level2 oplocks by config\n"));
943 break_to &= ~SMB2_LEASE_READ;
946 if (fsp->oplock_type == LEASE_OPLOCK) {
947 struct share_mode_lock *lck;
950 lck = get_existing_share_mode_lock(
951 talloc_tos(), fsp->file_id);
954 * We hit a race here. Break messages are sent, and
955 * before we get to process this message, we have closed
958 DEBUG(3, ("Did not find share_mode\n"));
962 idx = find_share_mode_lease(
964 fsp_client_guid(fsp),
965 &fsp->lease->lease.lease_key);
967 struct share_mode_lease *l;
968 l = &lck->data->leases[idx];
970 break_from = l->current_state;
971 break_to &= l->current_state;
974 break_to &= l->breaking_to_required;
975 if (l->breaking_to_required != break_to) {
977 * Note we don't increment the epoch
978 * here, which might be a bug in
981 l->breaking_to_required = break_to;
982 lck->data->modified = true;
984 break_needed = false;
985 } else if (l->current_state == break_to) {
986 break_needed = false;
987 } else if (l->current_state == SMB2_LEASE_READ) {
988 l->current_state = SMB2_LEASE_NONE;
989 /* Need to increment the epoch */
991 lck->data->modified = true;
994 l->breaking_to_required = break_to;
995 l->breaking_to_requested = break_to;
996 /* Need to increment the epoch */
998 lck->data->modified = true;
1001 /* Ensure we're in sync with current lease state. */
1002 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
1008 if (!break_needed) {
1009 DEBUG(10,("%s: skip break\n", __func__));
1013 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1014 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1015 file_id_string_tos(&fsp->file_id),
1020 DEBUG(10, ("break_from=%u, break_to=%u\n",
1021 (unsigned)break_from, (unsigned)break_to));
1023 if ((break_from == break_to) && !break_needed) {
1024 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1026 file_id_string_tos(&fsp->file_id),
1031 /* Need to wait before sending a break
1032 message if we sent ourselves this message. */
1033 if (serverid_equal(&self, &src)) {
1034 wait_before_sending_break();
1037 if (sconn->using_smb2) {
1038 send_break_message_smb2(fsp, break_from, break_to);
1040 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1041 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1044 if ((break_from == SMB2_LEASE_READ) &&
1045 (break_to == SMB2_LEASE_NONE)) {
1047 * This is an async break without a reply and thus no timeout
1049 * leases are handled above.
1051 if (fsp->oplock_type != LEASE_OPLOCK) {
1056 if (fsp->oplock_type == LEASE_OPLOCK) {
1060 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1061 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1063 add_oplock_timeout_handler(fsp);
1066 /*******************************************************************
1067 This handles the kernel oplock break message.
1068 *******************************************************************/
1070 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1073 struct server_id src,
1077 unsigned long file_id;
1079 struct smbd_server_connection *sconn =
1080 talloc_get_type_abort(private_data,
1081 struct smbd_server_connection);
1082 struct server_id_buf tmp;
1084 if (data->data == NULL) {
1085 DEBUG(0, ("Got NULL buffer\n"));
1089 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1090 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1094 /* Pull the data from the message. */
1095 pull_file_id_24((char *)data->data, &id);
1096 file_id = (unsigned long)IVAL(data->data, 24);
1098 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1099 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1100 (unsigned int)file_id));
1102 fsp = initial_break_processing(sconn, id, file_id);
1105 DEBUG(3, ("Got a kernel oplock break message for a file "
1106 "I don't know about\n"));
1110 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1111 /* This is ok, kernel oplocks come in completely async */
1112 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1117 if (sconn->using_smb2) {
1118 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1120 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1123 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1125 add_oplock_timeout_handler(fsp);
1128 static bool file_has_read_oplocks(struct files_struct *fsp)
1130 struct byte_range_lock *brl;
1131 uint32_t num_read_oplocks = 0;
1133 brl = brl_get_locks_readonly(fsp);
1138 num_read_oplocks = brl_num_read_oplocks(brl);
1140 DBG_DEBUG("num_read_oplocks = %"PRIu32"\n", num_read_oplocks);
1142 return (num_read_oplocks != 0);
1145 struct break_to_none_state {
1146 struct smbd_server_connection *sconn;
1148 struct smb2_lease_key lease_key;
1149 struct GUID client_guid;
1151 static void do_break_to_none(struct tevent_context *ctx,
1152 struct tevent_immediate *im,
1153 void *private_data);
1155 /****************************************************************************
1156 This function is called on any file modification or lock request. If a file
1157 is level 2 oplocked then it must tell all other level 2 holders to break to
1159 ****************************************************************************/
1161 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1162 enum level2_contention_type type)
1164 struct smbd_server_connection *sconn = fsp->conn->sconn;
1165 struct tevent_immediate *im;
1166 struct break_to_none_state *state;
1167 bool has_read_oplocks;
1170 * If this file is level II oplocked then we need
1171 * to grab the shared memory lock and inform all
1172 * other files with a level II lock that they need
1173 * to flush their read caches. We keep the lock over
1174 * the shared memory area whilst doing this.
1177 if (fsp_lease_type_is_exclusive(fsp)) {
1179 * There can't be any level2 oplocks, we're alone.
1184 has_read_oplocks = file_has_read_oplocks(fsp);
1185 if (!has_read_oplocks) {
1186 DEBUG(10, ("No read oplocks around\n"));
1191 * When we get here we might have a brlock entry locked. Also
1192 * locking the share mode entry would violate the locking
1193 * order. Breaking level2 oplocks to none is asynchronous
1194 * anyway, so we postpone this into an immediate event.
1197 state = talloc_zero(sconn, struct break_to_none_state);
1198 if (state == NULL) {
1199 DEBUG(1, ("talloc failed\n"));
1202 state->sconn = sconn;
1203 state->id = fsp->file_id;
1205 if (fsp->oplock_type == LEASE_OPLOCK) {
1206 state->client_guid = *fsp_client_guid(fsp);
1207 state->lease_key = fsp->lease->lease.lease_key;
1208 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1209 state->lease_key.data[0],
1210 state->lease_key.data[1]));
1213 im = tevent_create_immediate(state);
1215 DEBUG(1, ("tevent_create_immediate failed\n"));
1221 * do_break_to_none() only operates on the
1222 * locking.tdb and sends network packets to
1223 * the client. That doesn't require any
1224 * impersonation, so we just use the
1225 * raw tevent context here.
1227 tevent_schedule_immediate(im, sconn->raw_ev_ctx, do_break_to_none, state);
1230 static void send_break_to_none(struct messaging_context *msg_ctx,
1231 const struct file_id *id,
1232 const struct share_mode_entry *e)
1234 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1236 share_mode_entry_to_message(msg, id, e);
1237 /* Overload entry->op_type */
1238 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1240 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1241 (uint8_t *)msg, sizeof(msg));
1244 static void do_break_to_none(struct tevent_context *ctx,
1245 struct tevent_immediate *im,
1248 struct break_to_none_state *state = talloc_get_type_abort(
1249 private_data, struct break_to_none_state);
1251 struct share_mode_lock *lck;
1252 struct share_mode_data *d;
1255 * Note this function doesn't run under any specific impersonation and
1256 * is not expected to call any SMB_VFS operation!
1259 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1261 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1262 __func__, file_id_string_tos(&state->id)));
1268 * Walk leases and oplocks separately: We have to send one break per
1269 * lease. If we have multiple share_mode_entry having a common lease,
1270 * we would break the lease twice if we don't walk the leases list
1274 for (i=0; i<d->num_leases; i++) {
1275 struct share_mode_lease *l = &d->leases[i];
1276 struct share_mode_entry *e = NULL;
1279 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1282 if (smb2_lease_equal(&state->client_guid,
1286 DEBUG(10, ("Don't break our own lease\n"));
1290 for (j=0; j<d->num_share_modes; j++) {
1291 e = &d->share_modes[j];
1293 if (!is_valid_share_mode_entry(e)) {
1296 if (e->lease_idx == i) {
1300 if (j == d->num_share_modes) {
1301 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1306 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1307 "%"PRIu32"\n", i, j));
1309 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1312 for(i = 0; i < d->num_share_modes; i++) {
1313 struct share_mode_entry *e = &d->share_modes[i];
1315 if (!is_valid_share_mode_entry(e)) {
1318 if (e->op_type == LEASE_OPLOCK) {
1320 * Took care of those in the loop above
1326 * As there could have been multiple writes waiting at the
1327 * lock_share_entry gate we may not be the first to
1328 * enter. Hence the state of the op_types in the share mode
1329 * entries may be partly NO_OPLOCK and partly LEVEL_II
1330 * oplock. It will do no harm to re-send break messages to
1331 * those smbd's that are still waiting their turn to remove
1332 * their LEVEL_II state, and also no harm to ignore existing
1333 * NO_OPLOCK states. JRA.
1336 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1339 if (e->op_type == NO_OPLOCK) {
1344 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1345 DEBUG(0,("%s: PANIC. "
1346 "share mode entry %d is an exclusive "
1347 "oplock !\n", __func__, i ));
1352 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1355 /* We let the message receivers handle removing the oplock state
1356 in the share mode lock db. */
1364 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1365 enum level2_contention_type type)
1367 contend_level2_oplocks_begin_default(fsp, type);
1370 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1371 enum level2_contention_type type)
1376 /****************************************************************************
1377 Linearize a share mode entry struct to an internal oplock break message.
1378 ****************************************************************************/
1380 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1381 const struct share_mode_entry *e)
1383 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1384 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1385 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1386 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1387 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1388 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1389 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1390 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1392 * "id" used to be part of share_mode_entry, thus the strange
1393 * place to put this. Feel free to move somewhere else :-)
1395 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1396 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1397 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1398 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1399 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1400 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1403 /****************************************************************************
1404 De-linearize an internal oplock break message to a share mode entry struct.
1405 ****************************************************************************/
1407 void message_to_share_mode_entry(struct file_id *id,
1408 struct share_mode_entry *e,
1411 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1412 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1413 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1414 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1415 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1416 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1417 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1418 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1420 * "id" used to be part of share_mode_entry, thus the strange
1421 * place to put this. Feel free to move somewhere else :-)
1423 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1424 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1425 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1426 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1427 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1428 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1431 /****************************************************************************
1432 Setup oplocks for this process.
1433 ****************************************************************************/
1435 bool init_oplocks(struct smbd_server_connection *sconn)
1437 DEBUG(3,("init_oplocks: initializing messages.\n"));
1439 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1440 process_oplock_break_message);
1441 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1442 process_kernel_oplock_break);
1446 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1448 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1450 /* only initialize once */
1451 if (koplocks == NULL) {
1452 #if HAVE_KERNEL_OPLOCKS_LINUX
1453 koplocks = linux_init_kernel_oplocks(sconn);
1455 sconn->oplocks.kernel_ops = koplocks;