2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998 - 2001
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define DBGC_CLASS DBGC_LOCKING
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
27 #include "../librpc/gen_ndr/open_files.h"
30 * helper function used by the kernel oplock backends to post the break message
32 void break_kernel_oplock(struct messaging_context
*msg_ctx
, files_struct
*fsp
)
34 uint8_t msg
[MSG_SMB_KERNEL_BREAK_SIZE
];
36 /* Put the kernel break info into the message. */
37 push_file_id_24((char *)msg
, &fsp
->file_id
);
38 SIVAL(msg
,24,fsp
->fh
->gen_id
);
40 /* Don't need to be root here as we're only ever
41 sending to ourselves. */
43 messaging_send_buf(msg_ctx
, messaging_server_id(msg_ctx
),
45 msg
, MSG_SMB_KERNEL_BREAK_SIZE
);
48 /****************************************************************************
49 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
50 disabled (just sets flags).
51 ****************************************************************************/
53 NTSTATUS
set_file_oplock(files_struct
*fsp
)
55 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
56 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
57 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) && koplocks
;
59 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
61 !(koplocks
->flags
& KOPLOCKS_LEVEL2_SUPPORTED
)) {
62 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
63 "don't support them\n"));
64 return NT_STATUS_NOT_SUPPORTED
;
68 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
70 !koplocks
->ops
->set_oplock(koplocks
, fsp
, fsp
->oplock_type
))
72 return map_nt_error_from_unix(errno
);
75 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
76 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
77 sconn
->oplocks
.level_II_open
++;
78 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
79 sconn
->oplocks
.exclusive_open
++;
82 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
83 "tv_sec = %x, tv_usec = %x\n",
84 fsp_str_dbg(fsp
), file_id_string_tos(&fsp
->file_id
),
85 fsp
->fh
->gen_id
, (int)fsp
->open_time
.tv_sec
,
86 (int)fsp
->open_time
.tv_usec
));
91 /****************************************************************************
92 Attempt to release an oplock on a file. Decrements oplock count.
93 ****************************************************************************/
95 static void release_file_oplock(files_struct
*fsp
)
97 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
98 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
99 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) && koplocks
;
101 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
103 koplocks
->ops
->release_oplock(koplocks
, fsp
, NO_OPLOCK
);
106 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
107 sconn
->oplocks
.level_II_open
--;
108 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
109 sconn
->oplocks
.exclusive_open
--;
112 SMB_ASSERT(sconn
->oplocks
.exclusive_open
>=0);
113 SMB_ASSERT(sconn
->oplocks
.level_II_open
>=0);
115 fsp
->oplock_type
= NO_OPLOCK
;
116 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
118 flush_write_cache(fsp
, SAMBA_OPLOCK_RELEASE_FLUSH
);
119 delete_write_cache(fsp
);
121 TALLOC_FREE(fsp
->oplock_timeout
);
124 /****************************************************************************
125 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
126 ****************************************************************************/
128 static void downgrade_file_oplock(files_struct
*fsp
)
130 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
131 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
133 if (!EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
134 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
139 koplocks
->ops
->release_oplock(koplocks
, fsp
, LEVEL_II_OPLOCK
);
141 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
142 sconn
->oplocks
.exclusive_open
--;
143 sconn
->oplocks
.level_II_open
++;
144 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
146 flush_write_cache(fsp
, SAMBA_OPLOCK_RELEASE_FLUSH
);
147 delete_write_cache(fsp
);
149 TALLOC_FREE(fsp
->oplock_timeout
);
152 uint32_t map_oplock_to_lease_type(uint16_t op_type
)
158 case BATCH_OPLOCK
|EXCLUSIVE_OPLOCK
:
159 ret
= SMB2_LEASE_READ
|SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
;
161 case EXCLUSIVE_OPLOCK
:
162 ret
= SMB2_LEASE_READ
|SMB2_LEASE_WRITE
;
164 case LEVEL_II_OPLOCK
:
165 ret
= SMB2_LEASE_READ
;
168 ret
= SMB2_LEASE_NONE
;
174 uint32_t get_lease_type(struct share_mode_data
*d
, struct share_mode_entry
*e
)
176 if (e
->op_type
== LEASE_OPLOCK
) {
177 return d
->leases
[e
->lease_idx
].current_state
;
179 return map_oplock_to_lease_type(e
->op_type
);
182 bool update_num_read_oplocks(files_struct
*fsp
, struct share_mode_lock
*lck
)
184 struct share_mode_data
*d
= lck
->data
;
185 struct byte_range_lock
*br_lck
;
186 uint32_t num_read_oplocks
= 0;
189 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
191 * If we're the only one, we don't need a brlock entry
193 remove_stale_share_mode_entries(d
);
194 SMB_ASSERT(d
->num_share_modes
== 1);
195 SMB_ASSERT(EXCLUSIVE_OPLOCK_TYPE(d
->share_modes
[0].op_type
));
199 for (i
=0; i
<d
->num_share_modes
; i
++) {
200 struct share_mode_entry
*e
= &d
->share_modes
[i
];
201 uint32_t e_lease_type
= get_lease_type(d
, e
);
203 if (e_lease_type
& SMB2_LEASE_READ
) {
204 num_read_oplocks
+= 1;
208 br_lck
= brl_get_locks_readonly(fsp
);
209 if (br_lck
== NULL
) {
212 if (brl_num_read_oplocks(br_lck
) == num_read_oplocks
) {
216 br_lck
= brl_get_locks(talloc_tos(), fsp
);
217 if (br_lck
== NULL
) {
220 brl_set_num_read_oplocks(br_lck
, num_read_oplocks
);
225 /****************************************************************************
226 Remove a file oplock with lock already held. Copes with level II and exclusive.
227 ****************************************************************************/
229 bool remove_oplock_under_lock(files_struct
*fsp
, struct share_mode_lock
*lck
)
233 ret
= remove_share_oplock(lck
, fsp
);
235 DBG_ERR("failed to remove share oplock for "
237 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
238 file_id_string_tos(&fsp
->file_id
));
240 release_file_oplock(fsp
);
242 ret
= update_num_read_oplocks(fsp
, lck
);
244 DBG_ERR("update_num_read_oplocks failed for "
246 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
247 file_id_string_tos(&fsp
->file_id
));
253 /****************************************************************************
254 Remove a file oplock. Copes with level II and exclusive.
255 Locks then unlocks the share mode lock. Client can decide to go directly
256 to none even if a "break-to-level II" was sent.
257 ****************************************************************************/
259 bool remove_oplock(files_struct
*fsp
)
262 struct share_mode_lock
*lck
;
264 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp
));
266 /* Remove the oplock flag from the sharemode. */
267 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
269 DBG_ERR("failed to lock share entry for "
270 "file %s\n", fsp_str_dbg(fsp
));
274 ret
= remove_oplock_under_lock(fsp
, lck
);
281 * Deal with a reply when a break-to-level II was sent.
283 bool downgrade_oplock(files_struct
*fsp
)
286 struct share_mode_lock
*lck
;
288 DEBUG(10, ("downgrade_oplock called for %s\n",
291 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
293 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
294 "file %s\n", fsp_str_dbg(fsp
)));
297 ret
= downgrade_share_oplock(lck
, fsp
);
299 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
300 "for file %s, %s, file_id %s\n",
301 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
302 file_id_string_tos(&fsp
->file_id
)));
304 downgrade_file_oplock(fsp
);
306 ret
= update_num_read_oplocks(fsp
, lck
);
308 DEBUG(0, ("%s: update_num_read_oplocks failed for "
310 __func__
, fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
311 file_id_string_tos(&fsp
->file_id
)));
318 static void lease_timeout_handler(struct tevent_context
*ctx
,
319 struct tevent_timer
*te
,
323 struct fsp_lease
*lease
=
324 talloc_get_type_abort(private_data
,
326 struct files_struct
*fsp
;
327 struct share_mode_lock
*lck
;
328 uint16_t old_epoch
= lease
->lease
.lease_epoch
;
330 fsp
= file_find_one_fsp_from_lease_key(lease
->sconn
,
331 &lease
->lease
.lease_key
);
334 TALLOC_FREE(lease
->timeout
);
338 lck
= get_existing_share_mode_lock(
339 talloc_tos(), fsp
->file_id
);
342 TALLOC_FREE(lease
->timeout
);
346 fsp_lease_update(lck
, fsp_client_guid(fsp
), lease
);
348 if (lease
->lease
.lease_epoch
!= old_epoch
) {
350 * If the epoch changed we need to wait for
351 * the next timeout to happen.
353 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
359 if (!(lease
->lease
.lease_flags
& SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
)) {
361 * If the epoch changed we need to wait for
362 * the next timeout to happen.
364 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
370 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
372 (void)downgrade_lease(lease
->sconn
->client
->connections
,
375 &lease
->lease
.lease_key
,
381 bool fsp_lease_update(struct share_mode_lock
*lck
,
382 const struct GUID
*client_guid
,
383 struct fsp_lease
*lease
)
385 struct share_mode_data
*d
= lck
->data
;
387 struct share_mode_lease
*l
= NULL
;
389 idx
= find_share_mode_lease(d
, client_guid
, &lease
->lease
.lease_key
);
395 DEBUG(1, ("%s: Could not find lease entry\n", __func__
));
396 TALLOC_FREE(lease
->timeout
);
397 lease
->lease
.lease_state
= SMB2_LEASE_NONE
;
398 lease
->lease
.lease_epoch
+= 1;
399 lease
->lease
.lease_flags
= 0;
403 DEBUG(10,("%s: refresh lease state\n", __func__
));
405 /* Ensure we're in sync with current lease state. */
406 if (lease
->lease
.lease_epoch
!= l
->epoch
) {
407 DEBUG(10,("%s: cancel outdated timeout\n", __func__
));
408 TALLOC_FREE(lease
->timeout
);
410 lease
->lease
.lease_epoch
= l
->epoch
;
411 lease
->lease
.lease_state
= l
->current_state
;
414 lease
->lease
.lease_flags
|= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
;
416 if (lease
->timeout
== NULL
) {
417 struct timeval t
= timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0);
419 DEBUG(10,("%s: setup timeout handler\n", __func__
));
421 lease
->timeout
= tevent_add_timer(lease
->sconn
->ev_ctx
,
423 lease_timeout_handler
,
425 if (lease
->timeout
== NULL
) {
426 DEBUG(0, ("%s: Could not add lease timeout handler\n",
431 lease
->lease
.lease_flags
&= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
;
432 TALLOC_FREE(lease
->timeout
);
438 struct downgrade_lease_additional_state
{
439 struct tevent_immediate
*im
;
440 struct smbXsrv_connection
*xconn
;
441 uint32_t break_flags
;
442 struct smb2_lease_key lease_key
;
448 static void downgrade_lease_additional_trigger(struct tevent_context
*ev
,
449 struct tevent_immediate
*im
,
452 struct downgrade_lease_additional_state
*state
=
453 talloc_get_type_abort(private_data
,
454 struct downgrade_lease_additional_state
);
455 struct smbXsrv_connection
*xconn
= state
->xconn
;
458 status
= smbd_smb2_send_lease_break(xconn
,
465 if (!NT_STATUS_IS_OK(status
)) {
466 smbd_server_connection_terminate(xconn
,
472 struct downgrade_lease_fsps_state
{
474 struct share_mode_lock
*lck
;
475 const struct smb2_lease_key
*key
;
478 static struct files_struct
*downgrade_lease_fsps(struct files_struct
*fsp
,
481 struct downgrade_lease_fsps_state
*state
=
482 (struct downgrade_lease_fsps_state
*)private_data
;
484 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
487 if (!smb2_lease_key_equal(&fsp
->lease
->lease
.lease_key
, state
->key
)) {
490 if (!file_id_equal(&fsp
->file_id
, &state
->id
)) {
494 fsp_lease_update(state
->lck
, fsp_client_guid(fsp
), fsp
->lease
);
499 NTSTATUS
downgrade_lease(struct smbXsrv_connection
*xconn
,
500 uint32_t num_file_ids
,
501 const struct file_id
*ids
,
502 const struct smb2_lease_key
*key
,
503 uint32_t lease_state
)
505 struct smbd_server_connection
*sconn
= xconn
->client
->sconn
;
506 struct share_mode_lock
*lck
;
507 struct share_mode_lease
*l
= NULL
;
508 const struct file_id id
= ids
[0];
512 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__
,
513 file_id_string_tos(&id
), (unsigned)lease_state
));
515 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
517 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
519 status
= downgrade_share_lease(sconn
, lck
, key
, lease_state
, &l
);
521 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__
,
522 file_id_string_tos(&id
), (unsigned)lease_state
, nt_errstr(status
)));
524 if (NT_STATUS_EQUAL(status
, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS
)) {
525 struct downgrade_lease_additional_state
*state
;
527 state
= talloc_zero(xconn
,
528 struct downgrade_lease_additional_state
);
531 return NT_STATUS_NO_MEMORY
;
534 state
->im
= tevent_create_immediate(state
);
535 if (state
->im
== NULL
) {
538 return NT_STATUS_NO_MEMORY
;
541 state
->xconn
= xconn
;
542 if (l
->current_state
& (~SMB2_LEASE_READ
)) {
543 state
->break_flags
= SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
;
545 state
->lease_key
= l
->lease_key
;
546 state
->break_from
= l
->current_state
;
547 state
->break_to
= l
->breaking_to_requested
;
548 if (l
->lease_version
> 1) {
549 state
->new_epoch
= l
->epoch
;
552 if (state
->break_flags
== 0) {
554 * This is an async break without
555 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
557 * we need to store NONE state in the
560 l
->current_state
= 0;
561 l
->breaking_to_requested
= 0;
562 l
->breaking_to_required
= 0;
565 lck
->data
->modified
= true;
568 tevent_schedule_immediate(state
->im
, xconn
->ev_ctx
,
569 downgrade_lease_additional_trigger
,
574 struct downgrade_lease_fsps_state state
= {
575 .id
= id
, .lck
= lck
, .key
= key
,
578 files_forall(sconn
, downgrade_lease_fsps
, &state
);
582 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__
,
583 file_id_string_tos(&id
), (unsigned)lease_state
, nt_errstr(status
)));
586 * Dynamic share case. Ensure other opens are copies.
587 * This will only be breaking to NONE.
590 for (i
= 1; i
< num_file_ids
; i
++) {
591 lck
= get_existing_share_mode_lock(talloc_tos(), ids
[i
]);
593 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
597 struct downgrade_lease_fsps_state state
= {
598 .id
= ids
[i
], .lck
= lck
, .key
= key
,
601 files_forall(sconn
, downgrade_lease_fsps
, &state
);
604 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__
,
605 file_id_string_tos(&ids
[i
]), (unsigned)lease_state
, nt_errstr(status
)));
613 /****************************************************************************
614 Set up an oplock break message.
615 ****************************************************************************/
617 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
619 static void new_break_message_smb1(files_struct
*fsp
, int cmd
,
620 char result
[SMB1_BREAK_MESSAGE_LENGTH
])
622 memset(result
,'\0',smb_size
);
623 srv_set_message(result
,8,0,true);
624 SCVAL(result
,smb_com
,SMBlockingX
);
625 SSVAL(result
,smb_tid
,fsp
->conn
->cnum
);
626 SSVAL(result
,smb_pid
,0xFFFF);
627 SSVAL(result
,smb_uid
,0);
628 SSVAL(result
,smb_mid
,0xFFFF);
629 SCVAL(result
,smb_vwv0
,0xFF);
630 SSVAL(result
,smb_vwv2
,fsp
->fnum
);
631 SCVAL(result
,smb_vwv3
,LOCKING_ANDX_OPLOCK_RELEASE
);
632 SCVAL(result
,smb_vwv3
+1,cmd
);
635 /****************************************************************************
636 Function to do the waiting before sending a local break.
637 ****************************************************************************/
639 static void wait_before_sending_break(void)
641 long wait_time
= (long)lp_oplock_break_wait_time();
644 smb_msleep(wait_time
);
648 /****************************************************************************
649 Ensure that we have a valid oplock.
650 ****************************************************************************/
652 static files_struct
*initial_break_processing(
653 struct smbd_server_connection
*sconn
, struct file_id id
,
654 unsigned long file_id
)
656 files_struct
*fsp
= NULL
;
658 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
659 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
660 file_id_string_tos(&id
), (int)file_id
,
661 sconn
->oplocks
.exclusive_open
,
662 sconn
->oplocks
.level_II_open
));
665 * We need to search the file open table for the
666 * entry containing this dev and inode, and ensure
667 * we have an oplock on it.
670 fsp
= file_find_dif(sconn
, id
, file_id
);
673 /* The file could have been closed in the meantime - return success. */
674 DEBUG(3, ("initial_break_processing: cannot find open file "
675 "with file_id %s gen_id = %lu, allowing break to "
676 "succeed.\n", file_id_string_tos(&id
), file_id
));
680 /* Ensure we have an oplock on the file */
683 * There is a potential race condition in that an oplock could
684 * have been broken due to another udp request, and yet there are
685 * still oplock break messages being sent in the udp message
686 * queue for this file. So return true if we don't have an oplock,
687 * as we may have just freed it.
690 if(fsp
->oplock_type
== NO_OPLOCK
) {
691 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
692 "gen_id = %lu) has no oplock. Allowing break to "
693 "succeed regardless.\n", fsp_str_dbg(fsp
),
694 file_id_string_tos(&id
), fsp
->fh
->gen_id
));
701 static void oplock_timeout_handler(struct tevent_context
*ctx
,
702 struct tevent_timer
*te
,
706 files_struct
*fsp
= (files_struct
*)private_data
;
708 SMB_ASSERT(fsp
->sent_oplock_break
!= NO_BREAK_SENT
);
710 /* Remove the timed event handler. */
711 TALLOC_FREE(fsp
->oplock_timeout
);
712 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
717 /*******************************************************************
718 Add a timeout handler waiting for the client reply.
719 *******************************************************************/
721 static void add_oplock_timeout_handler(files_struct
*fsp
)
723 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
724 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
727 * If kernel oplocks already notifies smbds when an oplock break times
731 (koplocks
->flags
& KOPLOCKS_TIMEOUT_NOTIFICATION
)) {
735 if (fsp
->oplock_timeout
!= NULL
) {
736 DEBUG(0, ("Logic problem -- have an oplock event hanging "
740 fsp
->oplock_timeout
=
741 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, fsp
,
742 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0),
743 oplock_timeout_handler
, fsp
);
745 if (fsp
->oplock_timeout
== NULL
) {
746 DEBUG(0, ("Could not add oplock timeout handler\n"));
750 static void send_break_message_smb1(files_struct
*fsp
, int level
)
752 struct smbXsrv_connection
*xconn
= NULL
;
753 char break_msg
[SMB1_BREAK_MESSAGE_LENGTH
];
756 * For SMB1 we only have one connection
758 xconn
= fsp
->conn
->sconn
->client
->connections
;
760 new_break_message_smb1(fsp
, level
, break_msg
);
763 if (!srv_send_smb(xconn
,
765 IS_CONN_ENCRYPTED(fsp
->conn
),
767 exit_server_cleanly("send_break_message_smb1: "
768 "srv_send_smb failed.");
772 /*******************************************************************
773 This handles the generic oplock break message from another smbd.
774 *******************************************************************/
776 static void process_oplock_break_message(struct messaging_context
*msg_ctx
,
779 struct server_id src
,
782 struct share_mode_entry msg
;
785 struct smbd_server_connection
*sconn
=
786 talloc_get_type_abort(private_data
,
787 struct smbd_server_connection
);
788 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
789 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
792 bool break_needed
= true;
793 struct server_id_buf tmp
;
795 if (data
->data
== NULL
) {
796 DEBUG(0, ("Got NULL buffer\n"));
800 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
801 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
805 /* De-linearize incoming message. */
806 message_to_share_mode_entry(&msg
, (char *)data
->data
);
807 break_to
= msg
.op_type
;
809 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
810 (unsigned)break_to
, server_id_str_buf(src
, &tmp
),
811 file_id_string_tos(&msg
.id
),
812 (unsigned long long)msg
.share_file_id
));
814 fsp
= initial_break_processing(sconn
, msg
.id
, msg
.share_file_id
);
817 /* We hit a race here. Break messages are sent, and before we
818 * get to process this message, we have closed the file. */
819 DEBUG(3, ("Did not find fsp\n"));
823 break_from
= fsp_lease_type(fsp
);
825 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
826 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
828 * Nothing to do anymore
830 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
831 fsp
->sent_oplock_break
));
836 if (!(global_client_caps
& CAP_LEVEL_II_OPLOCKS
)) {
837 DEBUG(10, ("client_caps without level2 oplocks\n"));
838 break_to
&= ~SMB2_LEASE_READ
;
841 use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) && koplocks
;
842 if (use_kernel
&& !(koplocks
->flags
& KOPLOCKS_LEVEL2_SUPPORTED
)) {
843 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
844 break_to
&= ~SMB2_LEASE_READ
;
847 if (!lp_level2_oplocks(SNUM(fsp
->conn
))) {
848 DEBUG(10, ("no level2 oplocks by config\n"));
849 break_to
&= ~SMB2_LEASE_READ
;
852 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
853 struct share_mode_lock
*lck
;
856 lck
= get_existing_share_mode_lock(
857 talloc_tos(), fsp
->file_id
);
860 * We hit a race here. Break messages are sent, and
861 * before we get to process this message, we have closed
864 DEBUG(3, ("Did not find share_mode\n"));
868 idx
= find_share_mode_lease(
870 fsp_client_guid(fsp
),
871 &fsp
->lease
->lease
.lease_key
);
873 struct share_mode_lease
*l
;
874 l
= &lck
->data
->leases
[idx
];
876 break_from
= l
->current_state
;
877 break_to
&= l
->current_state
;
880 break_to
&= l
->breaking_to_required
;
881 if (l
->breaking_to_required
!= break_to
) {
883 * Note we don't increment the epoch
884 * here, which might be a bug in
887 l
->breaking_to_required
= break_to
;
888 lck
->data
->modified
= true;
890 break_needed
= false;
891 } else if (l
->current_state
== break_to
) {
892 break_needed
= false;
893 } else if (l
->current_state
== SMB2_LEASE_READ
) {
894 l
->current_state
= SMB2_LEASE_NONE
;
895 /* Need to increment the epoch */
897 lck
->data
->modified
= true;
900 l
->breaking_to_required
= break_to
;
901 l
->breaking_to_requested
= break_to
;
902 /* Need to increment the epoch */
904 lck
->data
->modified
= true;
907 /* Ensure we're in sync with current lease state. */
908 fsp_lease_update(lck
, fsp_client_guid(fsp
), fsp
->lease
);
915 DEBUG(10,("%s: skip break\n", __func__
));
919 if ((break_from
== SMB2_LEASE_NONE
) && !break_needed
) {
920 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
921 file_id_string_tos(&fsp
->file_id
),
926 DEBUG(10, ("break_from=%u, break_to=%u\n",
927 (unsigned)break_from
, (unsigned)break_to
));
929 if ((break_from
== break_to
) && !break_needed
) {
930 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
932 file_id_string_tos(&fsp
->file_id
),
937 /* Need to wait before sending a break
938 message if we sent ourselves this message. */
939 if (serverid_equal(&self
, &src
)) {
940 wait_before_sending_break();
943 if (sconn
->using_smb2
) {
944 send_break_message_smb2(fsp
, break_from
, break_to
);
946 send_break_message_smb1(fsp
, (break_to
& SMB2_LEASE_READ
) ?
947 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
950 if ((break_from
== SMB2_LEASE_READ
) &&
951 (break_to
== SMB2_LEASE_NONE
)) {
953 * This is an async break without a reply and thus no timeout
955 * leases are handled above.
957 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
962 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
966 fsp
->sent_oplock_break
= (break_to
& SMB2_LEASE_READ
) ?
967 LEVEL_II_BREAK_SENT
:BREAK_TO_NONE_SENT
;
969 add_oplock_timeout_handler(fsp
);
972 /*******************************************************************
973 This handles the kernel oplock break message.
974 *******************************************************************/
976 static void process_kernel_oplock_break(struct messaging_context
*msg_ctx
,
979 struct server_id src
,
983 unsigned long file_id
;
985 struct smbd_server_connection
*sconn
=
986 talloc_get_type_abort(private_data
,
987 struct smbd_server_connection
);
988 struct server_id_buf tmp
;
990 if (data
->data
== NULL
) {
991 DEBUG(0, ("Got NULL buffer\n"));
995 if (data
->length
!= MSG_SMB_KERNEL_BREAK_SIZE
) {
996 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
1000 /* Pull the data from the message. */
1001 pull_file_id_24((char *)data
->data
, &id
);
1002 file_id
= (unsigned long)IVAL(data
->data
, 24);
1004 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1005 server_id_str_buf(src
, &tmp
), file_id_string_tos(&id
),
1006 (unsigned int)file_id
));
1008 fsp
= initial_break_processing(sconn
, id
, file_id
);
1011 DEBUG(3, ("Got a kernel oplock break message for a file "
1012 "I don't know about\n"));
1016 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
1017 /* This is ok, kernel oplocks come in completely async */
1018 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1023 if (sconn
->using_smb2
) {
1024 send_break_message_smb2(fsp
, 0, OPLOCKLEVEL_NONE
);
1026 send_break_message_smb1(fsp
, OPLOCKLEVEL_NONE
);
1029 fsp
->sent_oplock_break
= BREAK_TO_NONE_SENT
;
1031 add_oplock_timeout_handler(fsp
);
1034 struct break_to_none_state
{
1035 struct smbd_server_connection
*sconn
;
1037 struct smb2_lease_key lease_key
;
1038 struct GUID client_guid
;
1040 static void do_break_to_none(struct tevent_context
*ctx
,
1041 struct tevent_immediate
*im
,
1042 void *private_data
);
1044 /****************************************************************************
1045 This function is called on any file modification or lock request. If a file
1046 is level 2 oplocked then it must tell all other level 2 holders to break to
1048 ****************************************************************************/
1050 static void contend_level2_oplocks_begin_default(files_struct
*fsp
,
1051 enum level2_contention_type type
)
1053 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1054 struct tevent_immediate
*im
;
1055 struct break_to_none_state
*state
;
1056 struct byte_range_lock
*brl
;
1057 uint32_t num_read_oplocks
= 0;
1060 * If this file is level II oplocked then we need
1061 * to grab the shared memory lock and inform all
1062 * other files with a level II lock that they need
1063 * to flush their read caches. We keep the lock over
1064 * the shared memory area whilst doing this.
1067 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1069 * There can't be any level2 oplocks, we're alone.
1074 brl
= brl_get_locks_readonly(fsp
);
1076 num_read_oplocks
= brl_num_read_oplocks(brl
);
1079 DEBUG(10, ("num_read_oplocks = %"PRIu32
"\n", num_read_oplocks
));
1081 if (num_read_oplocks
== 0) {
1082 DEBUG(10, ("No read oplocks around\n"));
1087 * When we get here we might have a brlock entry locked. Also
1088 * locking the share mode entry would violate the locking
1089 * order. Breaking level2 oplocks to none is asynchronous
1090 * anyway, so we postpone this into an immediate event.
1093 state
= talloc_zero(sconn
, struct break_to_none_state
);
1094 if (state
== NULL
) {
1095 DEBUG(1, ("talloc failed\n"));
1098 state
->sconn
= sconn
;
1099 state
->id
= fsp
->file_id
;
1101 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
1102 state
->client_guid
= *fsp_client_guid(fsp
);
1103 state
->lease_key
= fsp
->lease
->lease
.lease_key
;
1104 DEBUG(10, ("Breaking through lease key %"PRIu64
"/%"PRIu64
"\n",
1105 state
->lease_key
.data
[0],
1106 state
->lease_key
.data
[1]));
1109 im
= tevent_create_immediate(state
);
1111 DEBUG(1, ("tevent_create_immediate failed\n"));
1115 tevent_schedule_immediate(im
, sconn
->ev_ctx
, do_break_to_none
, state
);
1118 static void send_break_to_none(struct messaging_context
*msg_ctx
,
1119 const struct share_mode_entry
*e
)
1121 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1123 share_mode_entry_to_message(msg
, e
);
1124 /* Overload entry->op_type */
1125 SSVAL(msg
, OP_BREAK_MSG_OP_TYPE_OFFSET
, NO_OPLOCK
);
1127 messaging_send_buf(msg_ctx
, e
->pid
, MSG_SMB_BREAK_REQUEST
,
1128 (uint8_t *)msg
, sizeof(msg
));
1131 static void do_break_to_none(struct tevent_context
*ctx
,
1132 struct tevent_immediate
*im
,
1135 struct break_to_none_state
*state
= talloc_get_type_abort(
1136 private_data
, struct break_to_none_state
);
1138 struct share_mode_lock
*lck
;
1139 struct share_mode_data
*d
;
1141 lck
= get_existing_share_mode_lock(talloc_tos(), state
->id
);
1143 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1144 __func__
, file_id_string_tos(&state
->id
)));
1150 * Walk leases and oplocks separately: We have to send one break per
1151 * lease. If we have multiple share_mode_entry having a common lease,
1152 * we would break the lease twice if we don't walk the leases list
1156 for (i
=0; i
<d
->num_leases
; i
++) {
1157 struct share_mode_lease
*l
= &d
->leases
[i
];
1158 struct share_mode_entry
*e
= NULL
;
1161 if ((l
->current_state
& SMB2_LEASE_READ
) == 0) {
1164 if (smb2_lease_equal(&state
->client_guid
,
1168 DEBUG(10, ("Don't break our own lease\n"));
1172 for (j
=0; j
<d
->num_share_modes
; j
++) {
1173 e
= &d
->share_modes
[j
];
1175 if (!is_valid_share_mode_entry(e
)) {
1178 if (e
->lease_idx
== i
) {
1182 if (j
== d
->num_share_modes
) {
1183 DEBUG(0, ("leases[%"PRIu32
"] has no share mode\n",
1188 DEBUG(10, ("Breaking lease# %"PRIu32
" with share_entry# "
1189 "%"PRIu32
"\n", i
, j
));
1191 send_break_to_none(state
->sconn
->msg_ctx
, e
);
1194 for(i
= 0; i
< d
->num_share_modes
; i
++) {
1195 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1197 if (!is_valid_share_mode_entry(e
)) {
1200 if (e
->op_type
== LEASE_OPLOCK
) {
1202 * Took care of those in the loop above
1208 * As there could have been multiple writes waiting at the
1209 * lock_share_entry gate we may not be the first to
1210 * enter. Hence the state of the op_types in the share mode
1211 * entries may be partly NO_OPLOCK and partly LEVEL_II
1212 * oplock. It will do no harm to re-send break messages to
1213 * those smbd's that are still waiting their turn to remove
1214 * their LEVEL_II state, and also no harm to ignore existing
1215 * NO_OPLOCK states. JRA.
1218 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__
,
1221 if (e
->op_type
== NO_OPLOCK
) {
1226 if (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
)) {
1227 DEBUG(0,("%s: PANIC. "
1228 "share mode entry %d is an exclusive "
1229 "oplock !\n", __func__
, i
));
1234 send_break_to_none(state
->sconn
->msg_ctx
, e
);
1237 /* We let the message receivers handle removing the oplock state
1238 in the share mode lock db. */
1246 void smbd_contend_level2_oplocks_begin(files_struct
*fsp
,
1247 enum level2_contention_type type
)
1249 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1250 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
1252 if (koplocks
&& koplocks
->ops
->contend_level2_oplocks_begin
) {
1253 koplocks
->ops
->contend_level2_oplocks_begin(fsp
, type
);
1257 contend_level2_oplocks_begin_default(fsp
, type
);
1260 void smbd_contend_level2_oplocks_end(files_struct
*fsp
,
1261 enum level2_contention_type type
)
1263 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1264 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
1266 /* Only kernel oplocks implement this so far */
1267 if (koplocks
&& koplocks
->ops
->contend_level2_oplocks_end
) {
1268 koplocks
->ops
->contend_level2_oplocks_end(fsp
, type
);
1272 /****************************************************************************
1273 Linearize a share mode entry struct to an internal oplock break message.
1274 ****************************************************************************/
1276 void share_mode_entry_to_message(char *msg
, const struct share_mode_entry
*e
)
1278 SIVAL(msg
,OP_BREAK_MSG_PID_OFFSET
,(uint32_t)e
->pid
.pid
);
1279 SBVAL(msg
,OP_BREAK_MSG_MID_OFFSET
,e
->op_mid
);
1280 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,e
->op_type
);
1281 SIVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
,e
->access_mask
);
1282 SIVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
,e
->share_access
);
1283 SIVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
,e
->private_options
);
1284 SIVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
,(uint32_t)e
->time
.tv_sec
);
1285 SIVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
,(uint32_t)e
->time
.tv_usec
);
1286 push_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
1287 SIVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
,e
->share_file_id
);
1288 SIVAL(msg
,OP_BREAK_MSG_UID_OFFSET
,e
->uid
);
1289 SSVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
,e
->flags
);
1290 SIVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
,e
->name_hash
);
1291 SIVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
,e
->pid
.vnn
);
1294 /****************************************************************************
1295 De-linearize an internal oplock break message to a share mode entry struct.
1296 ****************************************************************************/
1298 void message_to_share_mode_entry(struct share_mode_entry
*e
, const char *msg
)
1300 e
->pid
.pid
= (pid_t
)IVAL(msg
,OP_BREAK_MSG_PID_OFFSET
);
1301 e
->op_mid
= BVAL(msg
,OP_BREAK_MSG_MID_OFFSET
);
1302 e
->op_type
= SVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
);
1303 e
->access_mask
= IVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
);
1304 e
->share_access
= IVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
);
1305 e
->private_options
= IVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
);
1306 e
->time
.tv_sec
= (time_t)IVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
);
1307 e
->time
.tv_usec
= (int)IVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
);
1308 pull_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
1309 e
->share_file_id
= (unsigned long)IVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
);
1310 e
->uid
= (uint32_t)IVAL(msg
,OP_BREAK_MSG_UID_OFFSET
);
1311 e
->flags
= (uint16_t)SVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
);
1312 e
->name_hash
= IVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
);
1313 e
->pid
.vnn
= IVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
);
1316 /****************************************************************************
1317 Setup oplocks for this process.
1318 ****************************************************************************/
1320 bool init_oplocks(struct smbd_server_connection
*sconn
)
1322 DEBUG(3,("init_oplocks: initializing messages.\n"));
1324 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_BREAK_REQUEST
,
1325 process_oplock_break_message
);
1326 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_KERNEL_BREAK
,
1327 process_kernel_oplock_break
);
1331 void init_kernel_oplocks(struct smbd_server_connection
*sconn
)
1333 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
1335 /* only initialize once */
1336 if (koplocks
== NULL
) {
1337 #if HAVE_KERNEL_OPLOCKS_IRIX
1338 koplocks
= irix_init_kernel_oplocks(sconn
);
1339 #elif HAVE_KERNEL_OPLOCKS_LINUX
1340 koplocks
= linux_init_kernel_oplocks(sconn
);
1342 sconn
->oplocks
.kernel_ops
= koplocks
;