2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998 - 2001
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define DBGC_CLASS DBGC_LOCKING
24 #include "lib/util/server_id.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
28 #include "locking/leases_db.h"
29 #include "../librpc/gen_ndr/ndr_open_files.h"
32 * helper function used by the kernel oplock backends to post the break message
34 void break_kernel_oplock(struct messaging_context
*msg_ctx
, files_struct
*fsp
)
36 uint8_t msg
[MSG_SMB_KERNEL_BREAK_SIZE
];
38 /* Put the kernel break info into the message. */
39 push_file_id_24((char *)msg
, &fsp
->file_id
);
40 SIVAL(msg
,24,fsp
->fh
->gen_id
);
42 /* Don't need to be root here as we're only ever
43 sending to ourselves. */
45 messaging_send_buf(msg_ctx
, messaging_server_id(msg_ctx
),
47 msg
, MSG_SMB_KERNEL_BREAK_SIZE
);
50 /****************************************************************************
51 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
52 disabled (just sets flags).
53 ****************************************************************************/
55 NTSTATUS
set_file_oplock(files_struct
*fsp
)
57 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
58 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
59 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
61 struct file_id_buf buf
;
63 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& use_kernel
) {
64 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
65 "don't support them\n"));
66 return NT_STATUS_NOT_SUPPORTED
;
69 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
71 !koplocks
->ops
->set_oplock(koplocks
, fsp
, fsp
->oplock_type
))
73 return map_nt_error_from_unix(errno
);
76 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
77 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
78 sconn
->oplocks
.level_II_open
++;
79 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
80 sconn
->oplocks
.exclusive_open
++;
83 DBG_INFO("granted oplock on file %s, %s/%"PRIu64
", "
84 "tv_sec = %x, tv_usec = %x\n",
86 file_id_str_buf(fsp
->file_id
, &buf
),
88 (int)fsp
->open_time
.tv_sec
,
89 (int)fsp
->open_time
.tv_usec
);
94 static void release_fsp_kernel_oplock(files_struct
*fsp
)
96 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
97 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
100 if (koplocks
== NULL
) {
103 use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
));
107 if (fsp
->oplock_type
== NO_OPLOCK
) {
110 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
112 * For leases we don't touch kernel oplocks at all
117 koplocks
->ops
->release_oplock(koplocks
, fsp
, NO_OPLOCK
);
120 /****************************************************************************
121 Attempt to release an oplock on a file. Decrements oplock count.
122 ****************************************************************************/
124 static void release_file_oplock(files_struct
*fsp
)
126 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
128 release_fsp_kernel_oplock(fsp
);
130 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
131 sconn
->oplocks
.level_II_open
--;
132 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
133 sconn
->oplocks
.exclusive_open
--;
136 SMB_ASSERT(sconn
->oplocks
.exclusive_open
>=0);
137 SMB_ASSERT(sconn
->oplocks
.level_II_open
>=0);
139 fsp
->oplock_type
= NO_OPLOCK
;
140 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
142 TALLOC_FREE(fsp
->oplock_timeout
);
145 /****************************************************************************
146 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
147 ****************************************************************************/
149 static void downgrade_file_oplock(files_struct
*fsp
)
151 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
152 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
153 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
156 if (!EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
157 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
162 koplocks
->ops
->release_oplock(koplocks
, fsp
, LEVEL_II_OPLOCK
);
164 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
165 sconn
->oplocks
.exclusive_open
--;
166 sconn
->oplocks
.level_II_open
++;
167 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
169 TALLOC_FREE(fsp
->oplock_timeout
);
172 uint32_t get_lease_type(const struct share_mode_entry
*e
, struct file_id id
)
174 if (e
->op_type
== LEASE_OPLOCK
) {
176 uint32_t current_state
;
178 status
= leases_db_get(
184 NULL
, /* breaking_to_requested */
185 NULL
, /* breaking_to_required */
186 NULL
, /* lease_version */
188 SMB_ASSERT(NT_STATUS_IS_OK(status
));
189 return current_state
;
191 return map_oplock_to_lease_type(e
->op_type
);
194 /****************************************************************************
195 Remove a file oplock. Copes with level II and exclusive.
196 Locks then unlocks the share mode lock. Client can decide to go directly
197 to none even if a "break-to-level II" was sent.
198 ****************************************************************************/
200 bool remove_oplock(files_struct
*fsp
)
203 struct share_mode_lock
*lck
;
205 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp
));
207 /* Remove the oplock flag from the sharemode. */
208 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
210 DBG_ERR("failed to lock share entry for "
211 "file %s\n", fsp_str_dbg(fsp
));
215 ret
= remove_share_oplock(lck
, fsp
);
217 struct file_id_buf buf
;
219 DBG_ERR("failed to remove share oplock for "
221 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
222 file_id_str_buf(fsp
->file_id
, &buf
));
224 release_file_oplock(fsp
);
231 * Deal with a reply when a break-to-level II was sent.
233 bool downgrade_oplock(files_struct
*fsp
)
236 struct share_mode_lock
*lck
;
238 DEBUG(10, ("downgrade_oplock called for %s\n",
241 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
243 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
244 "file %s\n", fsp_str_dbg(fsp
)));
247 ret
= downgrade_share_oplock(lck
, fsp
);
249 struct file_id_buf idbuf
;
250 DBG_ERR("failed to downgrade share oplock "
251 "for file %s, %s, file_id %s\n",
254 file_id_str_buf(fsp
->file_id
, &idbuf
));
256 downgrade_file_oplock(fsp
);
262 static void lease_timeout_handler(struct tevent_context
*ctx
,
263 struct tevent_timer
*te
,
267 struct fsp_lease
*lease
=
268 talloc_get_type_abort(private_data
,
270 struct files_struct
*fsp
;
271 struct share_mode_lock
*lck
;
272 uint16_t old_epoch
= lease
->lease
.lease_epoch
;
274 fsp
= file_find_one_fsp_from_lease_key(lease
->sconn
,
275 &lease
->lease
.lease_key
);
278 TALLOC_FREE(lease
->timeout
);
283 * Paranoia check: There can only be one fsp_lease per lease
286 SMB_ASSERT(fsp
->lease
== lease
);
288 lck
= get_existing_share_mode_lock(
289 talloc_tos(), fsp
->file_id
);
292 TALLOC_FREE(lease
->timeout
);
296 fsp_lease_update(fsp
);
298 if (lease
->lease
.lease_epoch
!= old_epoch
) {
300 * If the epoch changed we need to wait for
301 * the next timeout to happen.
303 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
309 if (!(lease
->lease
.lease_flags
& SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
)) {
311 * If the epoch changed we need to wait for
312 * the next timeout to happen.
314 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
320 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
322 (void)downgrade_lease(lease
->sconn
->client
->connections
,
325 &lease
->lease
.lease_key
,
331 bool fsp_lease_update(struct files_struct
*fsp
)
333 const struct GUID
*client_guid
= fsp_client_guid(fsp
);
334 struct fsp_lease
*lease
= fsp
->lease
;
335 uint32_t current_state
;
337 uint16_t lease_version
, epoch
;
340 status
= leases_db_get(client_guid
,
341 &lease
->lease
.lease_key
,
345 NULL
, /* breaking_to_requested */
346 NULL
, /* breaking_to_required */
349 if (!NT_STATUS_IS_OK(status
)) {
350 DBG_WARNING("Could not find lease entry: %s\n",
352 TALLOC_FREE(lease
->timeout
);
353 lease
->lease
.lease_state
= SMB2_LEASE_NONE
;
354 lease
->lease
.lease_epoch
+= 1;
355 lease
->lease
.lease_flags
= 0;
359 DEBUG(10,("%s: refresh lease state\n", __func__
));
361 /* Ensure we're in sync with current lease state. */
362 if (lease
->lease
.lease_epoch
!= epoch
) {
363 DEBUG(10,("%s: cancel outdated timeout\n", __func__
));
364 TALLOC_FREE(lease
->timeout
);
366 lease
->lease
.lease_epoch
= epoch
;
367 lease
->lease
.lease_state
= current_state
;
370 lease
->lease
.lease_flags
|= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
;
372 if (lease
->timeout
== NULL
) {
373 struct timeval t
= timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0);
375 DEBUG(10,("%s: setup timeout handler\n", __func__
));
377 lease
->timeout
= tevent_add_timer(lease
->sconn
->ev_ctx
,
379 lease_timeout_handler
,
381 if (lease
->timeout
== NULL
) {
382 DEBUG(0, ("%s: Could not add lease timeout handler\n",
387 lease
->lease
.lease_flags
&= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
;
388 TALLOC_FREE(lease
->timeout
);
394 struct downgrade_lease_additional_state
{
395 struct tevent_immediate
*im
;
396 struct smbXsrv_connection
*xconn
;
397 uint32_t break_flags
;
398 struct smb2_lease_key lease_key
;
404 static void downgrade_lease_additional_trigger(struct tevent_context
*ev
,
405 struct tevent_immediate
*im
,
408 struct downgrade_lease_additional_state
*state
=
409 talloc_get_type_abort(private_data
,
410 struct downgrade_lease_additional_state
);
411 struct smbXsrv_connection
*xconn
= state
->xconn
;
414 status
= smbd_smb2_send_lease_break(xconn
,
421 if (!NT_STATUS_IS_OK(status
)) {
422 smbd_server_connection_terminate(xconn
,
428 struct fsps_lease_update_state
{
429 const struct file_id
*id
;
430 const struct smb2_lease_key
*key
;
433 static struct files_struct
*fsps_lease_update_fn(
434 struct files_struct
*fsp
, void *private_data
)
436 struct fsps_lease_update_state
*state
=
437 (struct fsps_lease_update_state
*)private_data
;
439 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
442 if (!smb2_lease_key_equal(&fsp
->lease
->lease
.lease_key
, state
->key
)) {
445 if (!file_id_equal(&fsp
->file_id
, state
->id
)) {
449 fsp_lease_update(fsp
);
454 static void fsps_lease_update(struct smbd_server_connection
*sconn
,
455 const struct file_id
*id
,
456 const struct smb2_lease_key
*key
)
458 struct fsps_lease_update_state state
= { .id
= id
, .key
= key
};
459 files_forall(sconn
, fsps_lease_update_fn
, &state
);
462 NTSTATUS
downgrade_lease(struct smbXsrv_connection
*xconn
,
463 uint32_t num_file_ids
,
464 const struct file_id
*ids
,
465 const struct smb2_lease_key
*key
,
466 uint32_t lease_state
)
468 struct smbd_server_connection
*sconn
= xconn
->client
->sconn
;
469 const struct GUID
*client_guid
= NULL
;
470 struct share_mode_lock
*lck
;
471 const struct file_id id
= ids
[0];
472 uint32_t current_state
, breaking_to_requested
, breaking_to_required
;
474 uint16_t lease_version
, epoch
;
477 struct file_id_buf idbuf
;
479 DBG_DEBUG("Downgrading %s to %"PRIu32
"\n",
480 file_id_str_buf(id
, &idbuf
),
483 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
485 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
488 client_guid
= &sconn
->client
->connections
->smb2
.client
.guid
;
490 status
= leases_db_get(client_guid
,
495 &breaking_to_requested
,
496 &breaking_to_required
,
499 if (!NT_STATUS_IS_OK(status
)) {
500 DBG_WARNING("leases_db_get returned %s\n",
507 DBG_WARNING("Attempt to break from %"PRIu32
" to %"PRIu32
" - "
508 "but we're not in breaking state\n",
509 current_state
, lease_state
);
511 return NT_STATUS_UNSUCCESSFUL
;
515 * Can't upgrade anything: breaking_to_requested (and current_state)
516 * must be a strict bitwise superset of new_lease_state
518 if ((lease_state
& breaking_to_requested
) != lease_state
) {
519 DBG_WARNING("Attempt to upgrade from %"PRIu32
" to %"PRIu32
" "
520 "- expected %"PRIu32
"\n",
521 current_state
, lease_state
,
522 breaking_to_requested
);
524 return NT_STATUS_REQUEST_NOT_ACCEPTED
;
527 if (current_state
!= lease_state
) {
528 current_state
= lease_state
;
531 status
= NT_STATUS_OK
;
533 if ((lease_state
& ~breaking_to_required
) != 0) {
534 struct downgrade_lease_additional_state
*state
;
536 DBG_INFO("lease state %"PRIu32
" not fully broken from "
537 "%"PRIu32
" to %"PRIu32
"\n",
540 breaking_to_required
);
542 breaking_to_requested
= breaking_to_required
;
544 if (current_state
& (SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
)) {
546 * Here we break in steps, as windows does
547 * see the breaking3 and v2_breaking3 tests.
549 breaking_to_requested
|= SMB2_LEASE_READ
;
552 state
= talloc_zero(xconn
,
553 struct downgrade_lease_additional_state
);
556 return NT_STATUS_NO_MEMORY
;
559 state
->im
= tevent_create_immediate(state
);
560 if (state
->im
== NULL
) {
563 return NT_STATUS_NO_MEMORY
;
566 state
->xconn
= xconn
;
567 state
->lease_key
= *key
;
568 state
->break_from
= current_state
;
569 state
->break_to
= breaking_to_requested
;
570 if (lease_version
> 1) {
571 state
->new_epoch
= epoch
;
574 if (current_state
& (SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
)) {
576 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
;
579 * This is an async break without
580 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
582 * we need to store NONE state in the
586 breaking_to_requested
= 0;
587 breaking_to_required
= 0;
593 set_status
= leases_db_set(
594 &sconn
->client
->connections
->
599 breaking_to_requested
,
600 breaking_to_required
,
604 if (!NT_STATUS_IS_OK(set_status
)) {
605 DBG_DEBUG("leases_db_set failed: %s\n",
606 nt_errstr(set_status
));
612 tevent_schedule_immediate(state
->im
,
613 xconn
->client
->raw_ev_ctx
,
614 downgrade_lease_additional_trigger
,
617 status
= NT_STATUS_OPLOCK_BREAK_IN_PROGRESS
;
619 DBG_DEBUG("breaking from %"PRIu32
" to %"PRIu32
" - "
620 "expected %"PRIu32
"\n",
623 breaking_to_requested
);
625 breaking_to_requested
= 0;
626 breaking_to_required
= 0;
633 set_status
= leases_db_set(
638 breaking_to_requested
,
639 breaking_to_required
,
643 if (!NT_STATUS_IS_OK(set_status
)) {
644 DBG_DEBUG("leases_db_set failed: %s\n",
645 nt_errstr(set_status
));
651 DBG_DEBUG("Downgrading %s to %"PRIu32
" => %s\n",
652 file_id_str_buf(id
, &idbuf
),
656 share_mode_wakeup_waiters(id
);
658 fsps_lease_update(sconn
, &id
, key
);
662 DBG_DEBUG("Downgrading %s to %"PRIu32
" => %s\n",
663 file_id_str_buf(id
, &idbuf
),
668 * Dynamic share case. Ensure other opens are copies.
669 * This will only be breaking to NONE.
672 for (i
= 1; i
< num_file_ids
; i
++) {
673 lck
= get_existing_share_mode_lock(talloc_tos(), ids
[i
]);
675 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
678 fsps_lease_update(sconn
, &ids
[i
], key
);
680 DBG_DEBUG("Downgrading %s to %"PRIu32
" => %s\n",
681 file_id_str_buf(ids
[i
], &idbuf
),
691 /****************************************************************************
692 Set up an oplock break message.
693 ****************************************************************************/
695 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
697 static void new_break_message_smb1(files_struct
*fsp
, int cmd
,
698 char result
[SMB1_BREAK_MESSAGE_LENGTH
])
700 memset(result
,'\0',smb_size
);
701 srv_set_message(result
,8,0,true);
702 SCVAL(result
,smb_com
,SMBlockingX
);
703 SSVAL(result
,smb_tid
,fsp
->conn
->cnum
);
704 SSVAL(result
,smb_pid
,0xFFFF);
705 SSVAL(result
,smb_uid
,0);
706 SSVAL(result
,smb_mid
,0xFFFF);
707 SCVAL(result
,smb_vwv0
,0xFF);
708 SSVAL(result
,smb_vwv2
,fsp
->fnum
);
709 SCVAL(result
,smb_vwv3
,LOCKING_ANDX_OPLOCK_RELEASE
);
710 SCVAL(result
,smb_vwv3
+1,cmd
);
713 /****************************************************************************
714 Function to do the waiting before sending a local break.
715 ****************************************************************************/
717 static void wait_before_sending_break(void)
719 long wait_time
= (long)lp_oplock_break_wait_time();
722 smb_msleep(wait_time
);
726 /****************************************************************************
727 Ensure that we have a valid oplock.
728 ****************************************************************************/
730 static files_struct
*initial_break_processing(
731 struct smbd_server_connection
*sconn
, struct file_id id
,
732 unsigned long file_id
)
734 files_struct
*fsp
= NULL
;
735 struct file_id_buf idbuf
;
737 DBG_NOTICE("called for %s/%u\n"
738 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
739 file_id_str_buf(id
, &idbuf
),
741 sconn
->oplocks
.exclusive_open
,
742 sconn
->oplocks
.level_II_open
);
745 * We need to search the file open table for the
746 * entry containing this dev and inode, and ensure
747 * we have an oplock on it.
750 fsp
= file_find_dif(sconn
, id
, file_id
);
753 /* The file could have been closed in the meantime - return success. */
754 DBG_NOTICE("cannot find open file "
755 "with file_id %s gen_id = %lu, allowing break to "
757 file_id_str_buf(id
, &idbuf
),
762 /* Ensure we have an oplock on the file */
765 * There is a potential race condition in that an oplock could
766 * have been broken due to another udp request, and yet there are
767 * still oplock break messages being sent in the udp message
768 * queue for this file. So return true if we don't have an oplock,
769 * as we may have just freed it.
772 if(fsp
->oplock_type
== NO_OPLOCK
) {
773 DBG_NOTICE("file %s (file_id = %s gen_id = %"PRIu64
") "
775 "Allowing break to succeed regardless.\n",
777 file_id_str_buf(id
, &idbuf
),
785 static void oplock_timeout_handler(struct tevent_context
*ctx
,
786 struct tevent_timer
*te
,
790 files_struct
*fsp
= (files_struct
*)private_data
;
792 SMB_ASSERT(fsp
->sent_oplock_break
!= NO_BREAK_SENT
);
794 /* Remove the timed event handler. */
795 TALLOC_FREE(fsp
->oplock_timeout
);
796 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
801 /*******************************************************************
802 Add a timeout handler waiting for the client reply.
803 *******************************************************************/
805 static void add_oplock_timeout_handler(files_struct
*fsp
)
807 if (fsp
->oplock_timeout
!= NULL
) {
808 DEBUG(0, ("Logic problem -- have an oplock event hanging "
812 fsp
->oplock_timeout
=
813 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, fsp
,
814 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0),
815 oplock_timeout_handler
, fsp
);
817 if (fsp
->oplock_timeout
== NULL
) {
818 DEBUG(0, ("Could not add oplock timeout handler\n"));
822 static void send_break_message_smb1(files_struct
*fsp
, int level
)
824 struct smbXsrv_connection
*xconn
= NULL
;
825 char break_msg
[SMB1_BREAK_MESSAGE_LENGTH
];
828 * For SMB1 we only have one connection
830 xconn
= fsp
->conn
->sconn
->client
->connections
;
832 new_break_message_smb1(fsp
, level
, break_msg
);
835 if (!srv_send_smb(xconn
,
837 IS_CONN_ENCRYPTED(fsp
->conn
),
839 exit_server_cleanly("send_break_message_smb1: "
840 "srv_send_smb failed.");
844 /*******************************************************************
845 This handles the generic oplock break message from another smbd.
846 *******************************************************************/
848 static void process_oplock_break_message(struct messaging_context
*msg_ctx
,
851 struct server_id src
,
854 struct oplock_break_message
*msg
= NULL
;
855 enum ndr_err_code ndr_err
;
858 struct smbd_server_connection
*sconn
=
859 talloc_get_type_abort(private_data
,
860 struct smbd_server_connection
);
861 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
862 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
865 bool break_needed
= true;
867 msg
= talloc(talloc_tos(), struct oplock_break_message
);
869 DBG_WARNING("talloc failed\n");
873 ndr_err
= ndr_pull_struct_blob_all(
877 (ndr_pull_flags_fn_t
)ndr_pull_oplock_break_message
);
878 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
879 DBG_DEBUG("ndr_pull_oplock_break_message failed: %s\n",
880 ndr_errstr(ndr_err
));
884 if (DEBUGLEVEL
>= 10) {
885 struct server_id_buf buf
;
886 DBG_DEBUG("Got break message from %s\n",
887 server_id_str_buf(src
, &buf
));
888 NDR_PRINT_DEBUG(oplock_break_message
, msg
);
891 break_to
= msg
->break_to
;
892 fsp
= initial_break_processing(sconn
, msg
->id
, msg
->share_file_id
);
897 /* We hit a race here. Break messages are sent, and before we
898 * get to process this message, we have closed the file. */
899 DEBUG(3, ("Did not find fsp\n"));
903 break_from
= fsp_lease_type(fsp
);
905 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
906 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
908 * Nothing to do anymore
910 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
911 fsp
->sent_oplock_break
));
916 if (!(global_client_caps
& CAP_LEVEL_II_OPLOCKS
)) {
917 DEBUG(10, ("client_caps without level2 oplocks\n"));
918 break_to
&= ~SMB2_LEASE_READ
;
921 use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
924 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
925 break_to
&= ~SMB2_LEASE_READ
;
928 if (!lp_level2_oplocks(SNUM(fsp
->conn
))) {
929 DEBUG(10, ("no level2 oplocks by config\n"));
930 break_to
&= ~SMB2_LEASE_READ
;
933 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
934 const struct GUID
*client_guid
= fsp_client_guid(fsp
);
935 struct share_mode_lock
*lck
;
936 uint32_t current_state
;
937 uint32_t breaking_to_requested
, breaking_to_required
;
939 uint16_t lease_version
, epoch
;
942 lck
= get_existing_share_mode_lock(
943 talloc_tos(), fsp
->file_id
);
946 * We hit a race here. Break messages are sent, and
947 * before we get to process this message, we have closed
950 DEBUG(3, ("Did not find share_mode\n"));
954 status
= leases_db_get(client_guid
,
955 &fsp
->lease
->lease
.lease_key
,
959 &breaking_to_requested
,
960 &breaking_to_required
,
963 if (!NT_STATUS_IS_OK(status
)) {
964 DBG_WARNING("leases_db_get returned %s\n",
970 break_from
= current_state
;
971 break_to
&= current_state
;
974 break_to
&= breaking_to_required
;
975 if (breaking_to_required
!= break_to
) {
977 * Note we don't increment the epoch
978 * here, which might be a bug in
981 breaking_to_required
= break_to
;
983 break_needed
= false;
984 } else if (current_state
== break_to
) {
985 break_needed
= false;
986 } else if (current_state
== SMB2_LEASE_READ
) {
987 current_state
= SMB2_LEASE_NONE
;
988 /* Need to increment the epoch */
992 breaking_to_required
= break_to
;
993 breaking_to_requested
= break_to
;
994 /* Need to increment the epoch */
1001 set_status
= leases_db_set(
1003 &fsp
->lease
->lease
.lease_key
,
1006 breaking_to_requested
,
1007 breaking_to_required
,
1011 if (!NT_STATUS_IS_OK(set_status
)) {
1012 DBG_DEBUG("leases_db_set failed: %s\n",
1013 nt_errstr(set_status
));
1018 /* Ensure we're in sync with current lease state. */
1019 fsp_lease_update(fsp
);
1024 if (!break_needed
) {
1025 DEBUG(10,("%s: skip break\n", __func__
));
1029 if (break_from
== SMB2_LEASE_NONE
) {
1030 struct file_id_buf idbuf
;
1031 DBG_NOTICE("Already downgraded oplock to none on %s: %s\n",
1032 file_id_str_buf(fsp
->file_id
, &idbuf
),
1037 DEBUG(10, ("break_from=%u, break_to=%u\n",
1038 (unsigned)break_from
, (unsigned)break_to
));
1040 if (break_from
== break_to
) {
1041 struct file_id_buf idbuf
;
1042 DBG_NOTICE("Already downgraded oplock to %u on %s: %s\n",
1044 file_id_str_buf(fsp
->file_id
, &idbuf
),
1049 /* Need to wait before sending a break
1050 message if we sent ourselves this message. */
1051 if (server_id_equal(&self
, &src
)) {
1052 wait_before_sending_break();
1055 if (sconn
->using_smb2
) {
1056 send_break_message_smb2(fsp
, break_from
, break_to
);
1058 send_break_message_smb1(fsp
, (break_to
& SMB2_LEASE_READ
) ?
1059 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
1062 if ((break_from
== SMB2_LEASE_READ
) &&
1063 (break_to
== SMB2_LEASE_NONE
)) {
1065 * This is an async break without a reply and thus no timeout
1067 * leases are handled above.
1069 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
1074 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
1078 fsp
->sent_oplock_break
= (break_to
& SMB2_LEASE_READ
) ?
1079 LEVEL_II_BREAK_SENT
:BREAK_TO_NONE_SENT
;
1081 add_oplock_timeout_handler(fsp
);
1084 /*******************************************************************
1085 This handles the kernel oplock break message.
1086 *******************************************************************/
1088 static void process_kernel_oplock_break(struct messaging_context
*msg_ctx
,
1091 struct server_id src
,
1095 struct file_id_buf idbuf
;
1096 unsigned long file_id
;
1098 struct smbd_server_connection
*sconn
=
1099 talloc_get_type_abort(private_data
,
1100 struct smbd_server_connection
);
1101 struct server_id_buf tmp
;
1103 if (data
->data
== NULL
) {
1104 DEBUG(0, ("Got NULL buffer\n"));
1108 if (data
->length
!= MSG_SMB_KERNEL_BREAK_SIZE
) {
1109 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
1113 /* Pull the data from the message. */
1114 pull_file_id_24((char *)data
->data
, &id
);
1115 file_id
= (unsigned long)IVAL(data
->data
, 24);
1117 DBG_DEBUG("Got kernel oplock break message from pid %s: %s/%u\n",
1118 server_id_str_buf(src
, &tmp
),
1119 file_id_str_buf(id
, &idbuf
),
1120 (unsigned int)file_id
);
1122 fsp
= initial_break_processing(sconn
, id
, file_id
);
1125 DEBUG(3, ("Got a kernel oplock break message for a file "
1126 "I don't know about\n"));
1130 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
1131 /* This is ok, kernel oplocks come in completely async */
1132 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1137 if (sconn
->using_smb2
) {
1138 send_break_message_smb2(fsp
, 0, OPLOCKLEVEL_NONE
);
1140 send_break_message_smb1(fsp
, OPLOCKLEVEL_NONE
);
1143 fsp
->sent_oplock_break
= BREAK_TO_NONE_SENT
;
1145 add_oplock_timeout_handler(fsp
);
1148 static void send_break_to_none(struct messaging_context
*msg_ctx
,
1149 const struct file_id
*id
,
1150 const struct share_mode_entry
*e
)
1153 status
= send_break_message(msg_ctx
, id
, e
, OPLOCK_NONE
);
1154 if (!NT_STATUS_IS_OK(status
)) {
1155 DBG_DEBUG("send_break_message failed: %s\n",
1159 struct break_to_none_state
{
1160 struct smbd_server_connection
*sconn
;
1162 struct smb2_lease_key lease_key
;
1163 struct GUID client_guid
;
1167 static bool do_break_lease_to_none(struct share_mode_entry
*e
,
1170 struct break_to_none_state
*state
= private_data
;
1171 uint32_t current_state
= 0;
1175 DBG_DEBUG("lease_key=%"PRIu64
"/%"PRIu64
"\n",
1176 e
->lease_key
.data
[0],
1177 e
->lease_key
.data
[1]);
1179 status
= leases_db_get(&e
->client_guid
,
1183 NULL
, /* breaking */
1184 NULL
, /* breaking_to_requested */
1185 NULL
, /* breaking_to_required */
1186 NULL
, /* lease_version */
1188 if (!NT_STATUS_IS_OK(status
)) {
1189 DBG_WARNING("leases_db_get failed: %s\n",
1194 if ((current_state
& SMB2_LEASE_READ
) == 0) {
1198 our_own
= smb2_lease_equal(&state
->client_guid
,
1203 DEBUG(10, ("Don't break our own lease\n"));
1207 DBG_DEBUG("Breaking %"PRIu64
"/%"PRIu64
" to none\n",
1208 e
->lease_key
.data
[0],
1209 e
->lease_key
.data
[1]);
1211 send_break_to_none(state
->sconn
->msg_ctx
, &state
->id
, e
);
1213 state
->num_broken
+= 1;
1218 static bool do_break_oplock_to_none(struct share_mode_entry
*e
,
1222 struct break_to_none_state
*state
= private_data
;
1224 if (e
->op_type
== LEASE_OPLOCK
) {
1226 * Already being taken care of
1232 * As there could have been multiple writes waiting at the
1233 * lock_share_entry gate we may not be the first to
1234 * enter. Hence the state of the op_types in the share mode
1235 * entries may be partly NO_OPLOCK and partly LEVEL_II
1236 * oplock. It will do no harm to re-send break messages to
1237 * those smbd's that are still waiting their turn to remove
1238 * their LEVEL_II state, and also no harm to ignore existing
1239 * NO_OPLOCK states. JRA.
1242 DBG_DEBUG("e->op_type == %d\n", e
->op_type
);
1244 if (e
->op_type
== NO_OPLOCK
) {
1249 SMB_ASSERT(!EXCLUSIVE_OPLOCK_TYPE(e
->op_type
));
1251 send_break_to_none(state
->sconn
->msg_ctx
, &state
->id
, e
);
1252 state
->num_broken
+= 1;
1257 /****************************************************************************
1258 This function is called on any file modification or lock request. If a file
1259 is level 2 oplocked then it must tell all other level 2 holders to break to
1261 ****************************************************************************/
1263 static void contend_level2_oplocks_begin_default(files_struct
*fsp
,
1264 enum level2_contention_type type
)
1266 struct break_to_none_state state
= {
1267 .sconn
= fsp
->conn
->sconn
, .id
= fsp
->file_id
,
1269 struct share_mode_lock
*lck
= NULL
;
1270 struct share_mode_data
*d
= NULL
;
1271 bool ok
, has_read_lease
;
1274 * If this file is level II oplocked then we need
1275 * to grab the shared memory lock and inform all
1276 * other files with a level II lock that they need
1277 * to flush their read caches. We keep the lock over
1278 * the shared memory area whilst doing this.
1281 if (fsp_lease_type_is_exclusive(fsp
)) {
1283 * There can't be any level2 oplocks, we're alone.
1288 has_read_lease
= file_has_read_lease(fsp
);
1289 if (!has_read_lease
) {
1290 DEBUG(10, ("No read oplocks around\n"));
1294 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
1295 state
.client_guid
= *fsp_client_guid(fsp
);
1296 state
.lease_key
= fsp
->lease
->lease
.lease_key
;
1297 DEBUG(10, ("Breaking through lease key %"PRIu64
"/%"PRIu64
"\n",
1298 state
.lease_key
.data
[0],
1299 state
.lease_key
.data
[1]));
1302 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
1304 struct file_id_buf idbuf
;
1305 DBG_WARNING("failed to lock share mode entry for file %s.\n",
1306 file_id_str_buf(state
.id
, &idbuf
));
1312 * Walk leases and oplocks separately: We have to send one break per
1313 * lease. If we have multiple share_mode_entry having a common lease,
1314 * we would break the lease twice if we don't walk the leases list
1318 ok
= share_mode_forall_leases(lck
, do_break_lease_to_none
, &state
);
1320 DBG_WARNING("share_mode_forall_leases failed\n");
1323 ok
= share_mode_forall_entries(lck
, do_break_oplock_to_none
, &state
);
1325 DBG_WARNING("share_mode_forall_entries failed\n");
1328 if (state
.num_broken
== 0) {
1330 * Lazy update here. It might be that the read lease
1331 * has gone in the meantime.
1333 d
->flags
&= ~SHARE_MODE_LEASE_READ
;
1340 void smbd_contend_level2_oplocks_begin(files_struct
*fsp
,
1341 enum level2_contention_type type
)
1343 contend_level2_oplocks_begin_default(fsp
, type
);
1346 void smbd_contend_level2_oplocks_end(files_struct
*fsp
,
1347 enum level2_contention_type type
)
1352 /****************************************************************************
1353 Linearize a share mode entry struct to an internal oplock break message.
1354 ****************************************************************************/
1356 void share_mode_entry_to_message(char *msg
, const struct file_id
*id
,
1357 const struct share_mode_entry
*e
)
1359 SIVAL(msg
,OP_BREAK_MSG_PID_OFFSET
,(uint32_t)e
->pid
.pid
);
1360 SBVAL(msg
,OP_BREAK_MSG_MID_OFFSET
,e
->op_mid
);
1361 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,e
->op_type
);
1362 SIVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
,e
->access_mask
);
1363 SIVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
,e
->share_access
);
1364 SIVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
,e
->private_options
);
1365 SIVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
,(uint32_t)e
->time
.tv_sec
);
1366 SIVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
,(uint32_t)e
->time
.tv_usec
);
1368 * "id" used to be part of share_mode_entry, thus the strange
1369 * place to put this. Feel free to move somewhere else :-)
1371 push_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, id
);
1372 SIVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
,e
->share_file_id
);
1373 SIVAL(msg
,OP_BREAK_MSG_UID_OFFSET
,e
->uid
);
1374 SSVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
,e
->flags
);
1375 SIVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
,e
->name_hash
);
1376 SIVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
,e
->pid
.vnn
);
1379 /****************************************************************************
1380 De-linearize an internal oplock break message to a share mode entry struct.
1381 ****************************************************************************/
1383 void message_to_share_mode_entry(struct file_id
*id
,
1384 struct share_mode_entry
*e
,
1387 e
->pid
.pid
= (pid_t
)IVAL(msg
,OP_BREAK_MSG_PID_OFFSET
);
1388 e
->op_mid
= BVAL(msg
,OP_BREAK_MSG_MID_OFFSET
);
1389 e
->op_type
= SVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
);
1390 e
->access_mask
= IVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
);
1391 e
->share_access
= IVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
);
1392 e
->private_options
= IVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
);
1393 e
->time
.tv_sec
= (time_t)IVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
);
1394 e
->time
.tv_usec
= (int)IVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
);
1396 * "id" used to be part of share_mode_entry, thus the strange
1397 * place to put this. Feel free to move somewhere else :-)
1399 pull_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, id
);
1400 e
->share_file_id
= (unsigned long)IVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
);
1401 e
->uid
= (uint32_t)IVAL(msg
,OP_BREAK_MSG_UID_OFFSET
);
1402 e
->flags
= (uint16_t)SVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
);
1403 e
->name_hash
= IVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
);
1404 e
->pid
.vnn
= IVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
);
1407 /****************************************************************************
1408 Setup oplocks for this process.
1409 ****************************************************************************/
1411 bool init_oplocks(struct smbd_server_connection
*sconn
)
1413 DEBUG(3,("init_oplocks: initializing messages.\n"));
1415 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_BREAK_REQUEST
,
1416 process_oplock_break_message
);
1417 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_KERNEL_BREAK
,
1418 process_kernel_oplock_break
);
1422 void init_kernel_oplocks(struct smbd_server_connection
*sconn
)
1424 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
1426 /* only initialize once */
1427 if (koplocks
== NULL
) {
1428 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1429 koplocks
= linux_init_kernel_oplocks(sconn
);
1431 sconn
->oplocks
.kernel_ops
= koplocks
;