2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998 - 2001
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define DBGC_CLASS DBGC_LOCKING
24 #include "lib/util/server_id.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
28 #include "../librpc/gen_ndr/open_files.h"
29 #include "../librpc/gen_ndr/ndr_open_files.h"
32 * helper function used by the kernel oplock backends to post the break message
34 void break_kernel_oplock(struct messaging_context
*msg_ctx
, files_struct
*fsp
)
36 uint8_t msg
[MSG_SMB_KERNEL_BREAK_SIZE
];
38 /* Put the kernel break info into the message. */
39 push_file_id_24((char *)msg
, &fsp
->file_id
);
40 SIVAL(msg
,24,fsp
->fh
->gen_id
);
42 /* Don't need to be root here as we're only ever
43 sending to ourselves. */
45 messaging_send_buf(msg_ctx
, messaging_server_id(msg_ctx
),
47 msg
, MSG_SMB_KERNEL_BREAK_SIZE
);
50 /****************************************************************************
51 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
52 disabled (just sets flags).
53 ****************************************************************************/
55 NTSTATUS
set_file_oplock(files_struct
*fsp
)
57 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
58 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
59 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
62 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
64 !(koplocks
->flags
& KOPLOCKS_LEVEL2_SUPPORTED
)) {
65 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
66 "don't support them\n"));
67 return NT_STATUS_NOT_SUPPORTED
;
71 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
73 !koplocks
->ops
->set_oplock(koplocks
, fsp
, fsp
->oplock_type
))
75 return map_nt_error_from_unix(errno
);
78 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
79 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
80 sconn
->oplocks
.level_II_open
++;
81 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
82 sconn
->oplocks
.exclusive_open
++;
85 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
86 "tv_sec = %x, tv_usec = %x\n",
87 fsp_str_dbg(fsp
), file_id_string_tos(&fsp
->file_id
),
88 fsp
->fh
->gen_id
, (int)fsp
->open_time
.tv_sec
,
89 (int)fsp
->open_time
.tv_usec
));
94 /****************************************************************************
95 Attempt to release an oplock on a file. Decrements oplock count.
96 ****************************************************************************/
98 static void release_file_oplock(files_struct
*fsp
)
100 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
101 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
102 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
105 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
107 koplocks
->ops
->release_oplock(koplocks
, fsp
, NO_OPLOCK
);
110 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
111 sconn
->oplocks
.level_II_open
--;
112 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
113 sconn
->oplocks
.exclusive_open
--;
116 SMB_ASSERT(sconn
->oplocks
.exclusive_open
>=0);
117 SMB_ASSERT(sconn
->oplocks
.level_II_open
>=0);
119 fsp
->oplock_type
= NO_OPLOCK
;
120 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
122 flush_write_cache(fsp
, SAMBA_OPLOCK_RELEASE_FLUSH
);
123 delete_write_cache(fsp
);
125 TALLOC_FREE(fsp
->oplock_timeout
);
128 /****************************************************************************
129 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
130 ****************************************************************************/
132 static void downgrade_file_oplock(files_struct
*fsp
)
134 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
135 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
136 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
139 if (!EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
140 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
145 koplocks
->ops
->release_oplock(koplocks
, fsp
, LEVEL_II_OPLOCK
);
147 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
148 sconn
->oplocks
.exclusive_open
--;
149 sconn
->oplocks
.level_II_open
++;
150 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
152 flush_write_cache(fsp
, SAMBA_OPLOCK_RELEASE_FLUSH
);
153 delete_write_cache(fsp
);
155 TALLOC_FREE(fsp
->oplock_timeout
);
158 uint32_t get_lease_type(const struct share_mode_data
*d
,
159 const struct share_mode_entry
*e
)
161 if (e
->op_type
== LEASE_OPLOCK
) {
162 return d
->leases
[e
->lease_idx
].current_state
;
164 return map_oplock_to_lease_type(e
->op_type
);
167 bool update_num_read_oplocks(files_struct
*fsp
, struct share_mode_lock
*lck
)
169 struct share_mode_data
*d
= lck
->data
;
170 struct byte_range_lock
*br_lck
;
171 uint32_t num_read_oplocks
= 0;
174 if (fsp_lease_type_is_exclusive(fsp
)) {
175 const struct share_mode_entry
*e
= NULL
;
176 uint32_t e_lease_type
= 0;
179 * If we're fully exclusive, we don't need a brlock entry
181 remove_stale_share_mode_entries(d
);
183 e
= find_share_mode_entry(lck
, fsp
);
185 e_lease_type
= get_lease_type(d
, e
);
188 if (!lease_type_is_exclusive(e_lease_type
)) {
189 char *timestr
= NULL
;
191 timestr
= timeval_string(talloc_tos(),
195 NDR_PRINT_DEBUG(share_mode_data
, d
);
196 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
197 "open_time[%s] lease_type [0x%x] "
198 "oplock_type [0x%x]\n",
200 file_id_string_tos(&fsp
->file_id
),
201 fsp
->fh
->gen_id
, timestr
,
202 e_lease_type
, fsp
->oplock_type
);
204 smb_panic("Found non-exclusive lease");
210 for (i
=0; i
<d
->num_share_modes
; i
++) {
211 struct share_mode_entry
*e
= &d
->share_modes
[i
];
212 uint32_t e_lease_type
= get_lease_type(d
, e
);
214 if (e_lease_type
& SMB2_LEASE_READ
) {
215 num_read_oplocks
+= 1;
219 br_lck
= brl_get_locks_readonly(fsp
);
220 if (br_lck
== NULL
) {
223 if (brl_num_read_oplocks(br_lck
) == num_read_oplocks
) {
227 br_lck
= brl_get_locks(talloc_tos(), fsp
);
228 if (br_lck
== NULL
) {
231 brl_set_num_read_oplocks(br_lck
, num_read_oplocks
);
236 /****************************************************************************
237 Remove a file oplock with lock already held. Copes with level II and exclusive.
238 ****************************************************************************/
240 bool remove_oplock_under_lock(files_struct
*fsp
, struct share_mode_lock
*lck
)
244 ret
= remove_share_oplock(lck
, fsp
);
246 DBG_ERR("failed to remove share oplock for "
248 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
249 file_id_string_tos(&fsp
->file_id
));
251 release_file_oplock(fsp
);
253 ret
= update_num_read_oplocks(fsp
, lck
);
255 DBG_ERR("update_num_read_oplocks failed for "
257 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
258 file_id_string_tos(&fsp
->file_id
));
264 /****************************************************************************
265 Remove a file oplock. Copes with level II and exclusive.
266 Locks then unlocks the share mode lock. Client can decide to go directly
267 to none even if a "break-to-level II" was sent.
268 ****************************************************************************/
270 bool remove_oplock(files_struct
*fsp
)
273 struct share_mode_lock
*lck
;
275 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp
));
277 /* Remove the oplock flag from the sharemode. */
278 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
280 DBG_ERR("failed to lock share entry for "
281 "file %s\n", fsp_str_dbg(fsp
));
285 ret
= remove_oplock_under_lock(fsp
, lck
);
292 * Deal with a reply when a break-to-level II was sent.
294 bool downgrade_oplock(files_struct
*fsp
)
297 struct share_mode_lock
*lck
;
299 DEBUG(10, ("downgrade_oplock called for %s\n",
302 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
304 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
305 "file %s\n", fsp_str_dbg(fsp
)));
308 ret
= downgrade_share_oplock(lck
, fsp
);
310 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
311 "for file %s, %s, file_id %s\n",
312 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
313 file_id_string_tos(&fsp
->file_id
)));
315 downgrade_file_oplock(fsp
);
317 ret
= update_num_read_oplocks(fsp
, lck
);
319 DEBUG(0, ("%s: update_num_read_oplocks failed for "
321 __func__
, fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
322 file_id_string_tos(&fsp
->file_id
)));
329 static void lease_timeout_handler(struct tevent_context
*ctx
,
330 struct tevent_timer
*te
,
334 struct fsp_lease
*lease
=
335 talloc_get_type_abort(private_data
,
337 struct files_struct
*fsp
;
338 struct share_mode_lock
*lck
;
339 uint16_t old_epoch
= lease
->lease
.lease_epoch
;
341 fsp
= file_find_one_fsp_from_lease_key(lease
->sconn
,
342 &lease
->lease
.lease_key
);
345 TALLOC_FREE(lease
->timeout
);
349 lck
= get_existing_share_mode_lock(
350 talloc_tos(), fsp
->file_id
);
353 TALLOC_FREE(lease
->timeout
);
357 fsp_lease_update(lck
, fsp_client_guid(fsp
), lease
);
359 if (lease
->lease
.lease_epoch
!= old_epoch
) {
361 * If the epoch changed we need to wait for
362 * the next timeout to happen.
364 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
370 if (!(lease
->lease
.lease_flags
& SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
)) {
372 * If the epoch changed we need to wait for
373 * the next timeout to happen.
375 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
381 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
383 (void)downgrade_lease(lease
->sconn
->client
->connections
,
386 &lease
->lease
.lease_key
,
392 bool fsp_lease_update(struct share_mode_lock
*lck
,
393 const struct GUID
*client_guid
,
394 struct fsp_lease
*lease
)
396 struct share_mode_data
*d
= lck
->data
;
398 struct share_mode_lease
*l
= NULL
;
400 idx
= find_share_mode_lease(d
, client_guid
, &lease
->lease
.lease_key
);
406 DEBUG(1, ("%s: Could not find lease entry\n", __func__
));
407 TALLOC_FREE(lease
->timeout
);
408 lease
->lease
.lease_state
= SMB2_LEASE_NONE
;
409 lease
->lease
.lease_epoch
+= 1;
410 lease
->lease
.lease_flags
= 0;
414 DEBUG(10,("%s: refresh lease state\n", __func__
));
416 /* Ensure we're in sync with current lease state. */
417 if (lease
->lease
.lease_epoch
!= l
->epoch
) {
418 DEBUG(10,("%s: cancel outdated timeout\n", __func__
));
419 TALLOC_FREE(lease
->timeout
);
421 lease
->lease
.lease_epoch
= l
->epoch
;
422 lease
->lease
.lease_state
= l
->current_state
;
425 lease
->lease
.lease_flags
|= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
;
427 if (lease
->timeout
== NULL
) {
428 struct timeval t
= timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0);
430 DEBUG(10,("%s: setup timeout handler\n", __func__
));
432 lease
->timeout
= tevent_add_timer(lease
->sconn
->ev_ctx
,
434 lease_timeout_handler
,
436 if (lease
->timeout
== NULL
) {
437 DEBUG(0, ("%s: Could not add lease timeout handler\n",
442 lease
->lease
.lease_flags
&= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
;
443 TALLOC_FREE(lease
->timeout
);
449 struct downgrade_lease_additional_state
{
450 struct tevent_immediate
*im
;
451 struct smbXsrv_connection
*xconn
;
452 uint32_t break_flags
;
453 struct smb2_lease_key lease_key
;
459 static void downgrade_lease_additional_trigger(struct tevent_context
*ev
,
460 struct tevent_immediate
*im
,
463 struct downgrade_lease_additional_state
*state
=
464 talloc_get_type_abort(private_data
,
465 struct downgrade_lease_additional_state
);
466 struct smbXsrv_connection
*xconn
= state
->xconn
;
469 status
= smbd_smb2_send_lease_break(xconn
,
476 if (!NT_STATUS_IS_OK(status
)) {
477 smbd_server_connection_terminate(xconn
,
483 struct downgrade_lease_fsps_state
{
485 struct share_mode_lock
*lck
;
486 const struct smb2_lease_key
*key
;
489 static struct files_struct
*downgrade_lease_fsps(struct files_struct
*fsp
,
492 struct downgrade_lease_fsps_state
*state
=
493 (struct downgrade_lease_fsps_state
*)private_data
;
495 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
498 if (!smb2_lease_key_equal(&fsp
->lease
->lease
.lease_key
, state
->key
)) {
501 if (!file_id_equal(&fsp
->file_id
, &state
->id
)) {
505 fsp_lease_update(state
->lck
, fsp_client_guid(fsp
), fsp
->lease
);
510 NTSTATUS
downgrade_lease(struct smbXsrv_connection
*xconn
,
511 uint32_t num_file_ids
,
512 const struct file_id
*ids
,
513 const struct smb2_lease_key
*key
,
514 uint32_t lease_state
)
516 struct smbd_server_connection
*sconn
= xconn
->client
->sconn
;
517 struct share_mode_lock
*lck
;
518 struct share_mode_lease
*l
= NULL
;
519 const struct file_id id
= ids
[0];
523 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__
,
524 file_id_string_tos(&id
), (unsigned)lease_state
));
526 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
528 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
530 status
= downgrade_share_lease(sconn
, lck
, key
, lease_state
, &l
);
532 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__
,
533 file_id_string_tos(&id
), (unsigned)lease_state
, nt_errstr(status
)));
535 if (NT_STATUS_EQUAL(status
, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS
)) {
536 struct downgrade_lease_additional_state
*state
;
538 state
= talloc_zero(xconn
,
539 struct downgrade_lease_additional_state
);
542 return NT_STATUS_NO_MEMORY
;
545 state
->im
= tevent_create_immediate(state
);
546 if (state
->im
== NULL
) {
549 return NT_STATUS_NO_MEMORY
;
552 state
->xconn
= xconn
;
553 if (l
->current_state
& (~SMB2_LEASE_READ
)) {
554 state
->break_flags
= SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
;
556 state
->lease_key
= l
->lease_key
;
557 state
->break_from
= l
->current_state
;
558 state
->break_to
= l
->breaking_to_requested
;
559 if (l
->lease_version
> 1) {
560 state
->new_epoch
= l
->epoch
;
563 if (state
->break_flags
== 0) {
565 * This is an async break without
566 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
568 * we need to store NONE state in the
571 l
->current_state
= 0;
572 l
->breaking_to_requested
= 0;
573 l
->breaking_to_required
= 0;
576 lck
->data
->modified
= true;
579 tevent_schedule_immediate(state
->im
, xconn
->ev_ctx
,
580 downgrade_lease_additional_trigger
,
585 struct downgrade_lease_fsps_state state
= {
586 .id
= id
, .lck
= lck
, .key
= key
,
589 files_forall(sconn
, downgrade_lease_fsps
, &state
);
593 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__
,
594 file_id_string_tos(&id
), (unsigned)lease_state
, nt_errstr(status
)));
597 * Dynamic share case. Ensure other opens are copies.
598 * This will only be breaking to NONE.
601 for (i
= 1; i
< num_file_ids
; i
++) {
602 lck
= get_existing_share_mode_lock(talloc_tos(), ids
[i
]);
604 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
608 struct downgrade_lease_fsps_state state
= {
609 .id
= ids
[i
], .lck
= lck
, .key
= key
,
612 files_forall(sconn
, downgrade_lease_fsps
, &state
);
615 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__
,
616 file_id_string_tos(&ids
[i
]), (unsigned)lease_state
, nt_errstr(status
)));
624 /****************************************************************************
625 Set up an oplock break message.
626 ****************************************************************************/
628 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
630 static void new_break_message_smb1(files_struct
*fsp
, int cmd
,
631 char result
[SMB1_BREAK_MESSAGE_LENGTH
])
633 memset(result
,'\0',smb_size
);
634 srv_set_message(result
,8,0,true);
635 SCVAL(result
,smb_com
,SMBlockingX
);
636 SSVAL(result
,smb_tid
,fsp
->conn
->cnum
);
637 SSVAL(result
,smb_pid
,0xFFFF);
638 SSVAL(result
,smb_uid
,0);
639 SSVAL(result
,smb_mid
,0xFFFF);
640 SCVAL(result
,smb_vwv0
,0xFF);
641 SSVAL(result
,smb_vwv2
,fsp
->fnum
);
642 SCVAL(result
,smb_vwv3
,LOCKING_ANDX_OPLOCK_RELEASE
);
643 SCVAL(result
,smb_vwv3
+1,cmd
);
646 /****************************************************************************
647 Function to do the waiting before sending a local break.
648 ****************************************************************************/
650 static void wait_before_sending_break(void)
652 long wait_time
= (long)lp_oplock_break_wait_time();
655 smb_msleep(wait_time
);
659 /****************************************************************************
660 Ensure that we have a valid oplock.
661 ****************************************************************************/
663 static files_struct
*initial_break_processing(
664 struct smbd_server_connection
*sconn
, struct file_id id
,
665 unsigned long file_id
)
667 files_struct
*fsp
= NULL
;
669 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
670 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
671 file_id_string_tos(&id
), (int)file_id
,
672 sconn
->oplocks
.exclusive_open
,
673 sconn
->oplocks
.level_II_open
));
676 * We need to search the file open table for the
677 * entry containing this dev and inode, and ensure
678 * we have an oplock on it.
681 fsp
= file_find_dif(sconn
, id
, file_id
);
684 /* The file could have been closed in the meantime - return success. */
685 DEBUG(3, ("initial_break_processing: cannot find open file "
686 "with file_id %s gen_id = %lu, allowing break to "
687 "succeed.\n", file_id_string_tos(&id
), file_id
));
691 /* Ensure we have an oplock on the file */
694 * There is a potential race condition in that an oplock could
695 * have been broken due to another udp request, and yet there are
696 * still oplock break messages being sent in the udp message
697 * queue for this file. So return true if we don't have an oplock,
698 * as we may have just freed it.
701 if(fsp
->oplock_type
== NO_OPLOCK
) {
702 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
703 "gen_id = %lu) has no oplock. Allowing break to "
704 "succeed regardless.\n", fsp_str_dbg(fsp
),
705 file_id_string_tos(&id
), fsp
->fh
->gen_id
));
712 static void oplock_timeout_handler(struct tevent_context
*ctx
,
713 struct tevent_timer
*te
,
717 files_struct
*fsp
= (files_struct
*)private_data
;
719 SMB_ASSERT(fsp
->sent_oplock_break
!= NO_BREAK_SENT
);
721 /* Remove the timed event handler. */
722 TALLOC_FREE(fsp
->oplock_timeout
);
723 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
728 /*******************************************************************
729 Add a timeout handler waiting for the client reply.
730 *******************************************************************/
732 static void add_oplock_timeout_handler(files_struct
*fsp
)
734 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
735 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
736 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
740 * If kernel oplocks already notifies smbds when an oplock break times
744 (koplocks
->flags
& KOPLOCKS_TIMEOUT_NOTIFICATION
)) {
748 if (fsp
->oplock_timeout
!= NULL
) {
749 DEBUG(0, ("Logic problem -- have an oplock event hanging "
753 fsp
->oplock_timeout
=
754 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, fsp
,
755 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0),
756 oplock_timeout_handler
, fsp
);
758 if (fsp
->oplock_timeout
== NULL
) {
759 DEBUG(0, ("Could not add oplock timeout handler\n"));
763 static void send_break_message_smb1(files_struct
*fsp
, int level
)
765 struct smbXsrv_connection
*xconn
= NULL
;
766 char break_msg
[SMB1_BREAK_MESSAGE_LENGTH
];
769 * For SMB1 we only have one connection
771 xconn
= fsp
->conn
->sconn
->client
->connections
;
773 new_break_message_smb1(fsp
, level
, break_msg
);
776 if (!srv_send_smb(xconn
,
778 IS_CONN_ENCRYPTED(fsp
->conn
),
780 exit_server_cleanly("send_break_message_smb1: "
781 "srv_send_smb failed.");
785 /*******************************************************************
786 This handles the generic oplock break message from another smbd.
787 *******************************************************************/
789 static void process_oplock_break_message(struct messaging_context
*msg_ctx
,
792 struct server_id src
,
795 struct share_mode_entry msg
;
798 struct smbd_server_connection
*sconn
=
799 talloc_get_type_abort(private_data
,
800 struct smbd_server_connection
);
801 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
802 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
805 bool break_needed
= true;
806 struct server_id_buf tmp
;
808 if (data
->data
== NULL
) {
809 DEBUG(0, ("Got NULL buffer\n"));
813 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
814 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
818 /* De-linearize incoming message. */
819 message_to_share_mode_entry(&msg
, (char *)data
->data
);
820 break_to
= msg
.op_type
;
822 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
823 (unsigned)break_to
, server_id_str_buf(src
, &tmp
),
824 file_id_string_tos(&msg
.id
),
825 (unsigned long long)msg
.share_file_id
));
827 fsp
= initial_break_processing(sconn
, msg
.id
, msg
.share_file_id
);
830 /* We hit a race here. Break messages are sent, and before we
831 * get to process this message, we have closed the file. */
832 DEBUG(3, ("Did not find fsp\n"));
836 break_from
= fsp_lease_type(fsp
);
838 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
839 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
841 * Nothing to do anymore
843 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
844 fsp
->sent_oplock_break
));
849 if (!(global_client_caps
& CAP_LEVEL_II_OPLOCKS
)) {
850 DEBUG(10, ("client_caps without level2 oplocks\n"));
851 break_to
&= ~SMB2_LEASE_READ
;
854 use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
856 if (use_kernel
&& !(koplocks
->flags
& KOPLOCKS_LEVEL2_SUPPORTED
)) {
857 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
858 break_to
&= ~SMB2_LEASE_READ
;
861 if (!lp_level2_oplocks(SNUM(fsp
->conn
))) {
862 DEBUG(10, ("no level2 oplocks by config\n"));
863 break_to
&= ~SMB2_LEASE_READ
;
866 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
867 struct share_mode_lock
*lck
;
870 lck
= get_existing_share_mode_lock(
871 talloc_tos(), fsp
->file_id
);
874 * We hit a race here. Break messages are sent, and
875 * before we get to process this message, we have closed
878 DEBUG(3, ("Did not find share_mode\n"));
882 idx
= find_share_mode_lease(
884 fsp_client_guid(fsp
),
885 &fsp
->lease
->lease
.lease_key
);
887 struct share_mode_lease
*l
;
888 l
= &lck
->data
->leases
[idx
];
890 break_from
= l
->current_state
;
891 break_to
&= l
->current_state
;
894 break_to
&= l
->breaking_to_required
;
895 if (l
->breaking_to_required
!= break_to
) {
897 * Note we don't increment the epoch
898 * here, which might be a bug in
901 l
->breaking_to_required
= break_to
;
902 lck
->data
->modified
= true;
904 break_needed
= false;
905 } else if (l
->current_state
== break_to
) {
906 break_needed
= false;
907 } else if (l
->current_state
== SMB2_LEASE_READ
) {
908 l
->current_state
= SMB2_LEASE_NONE
;
909 /* Need to increment the epoch */
911 lck
->data
->modified
= true;
914 l
->breaking_to_required
= break_to
;
915 l
->breaking_to_requested
= break_to
;
916 /* Need to increment the epoch */
918 lck
->data
->modified
= true;
921 /* Ensure we're in sync with current lease state. */
922 fsp_lease_update(lck
, fsp_client_guid(fsp
), fsp
->lease
);
929 DEBUG(10,("%s: skip break\n", __func__
));
933 if ((break_from
== SMB2_LEASE_NONE
) && !break_needed
) {
934 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
935 file_id_string_tos(&fsp
->file_id
),
940 DEBUG(10, ("break_from=%u, break_to=%u\n",
941 (unsigned)break_from
, (unsigned)break_to
));
943 if ((break_from
== break_to
) && !break_needed
) {
944 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
946 file_id_string_tos(&fsp
->file_id
),
951 /* Need to wait before sending a break
952 message if we sent ourselves this message. */
953 if (serverid_equal(&self
, &src
)) {
954 wait_before_sending_break();
957 if (sconn
->using_smb2
) {
958 send_break_message_smb2(fsp
, break_from
, break_to
);
960 send_break_message_smb1(fsp
, (break_to
& SMB2_LEASE_READ
) ?
961 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
964 if ((break_from
== SMB2_LEASE_READ
) &&
965 (break_to
== SMB2_LEASE_NONE
)) {
967 * This is an async break without a reply and thus no timeout
969 * leases are handled above.
971 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
976 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
980 fsp
->sent_oplock_break
= (break_to
& SMB2_LEASE_READ
) ?
981 LEVEL_II_BREAK_SENT
:BREAK_TO_NONE_SENT
;
983 add_oplock_timeout_handler(fsp
);
986 /*******************************************************************
987 This handles the kernel oplock break message.
988 *******************************************************************/
990 static void process_kernel_oplock_break(struct messaging_context
*msg_ctx
,
993 struct server_id src
,
997 unsigned long file_id
;
999 struct smbd_server_connection
*sconn
=
1000 talloc_get_type_abort(private_data
,
1001 struct smbd_server_connection
);
1002 struct server_id_buf tmp
;
1004 if (data
->data
== NULL
) {
1005 DEBUG(0, ("Got NULL buffer\n"));
1009 if (data
->length
!= MSG_SMB_KERNEL_BREAK_SIZE
) {
1010 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
1014 /* Pull the data from the message. */
1015 pull_file_id_24((char *)data
->data
, &id
);
1016 file_id
= (unsigned long)IVAL(data
->data
, 24);
1018 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1019 server_id_str_buf(src
, &tmp
), file_id_string_tos(&id
),
1020 (unsigned int)file_id
));
1022 fsp
= initial_break_processing(sconn
, id
, file_id
);
1025 DEBUG(3, ("Got a kernel oplock break message for a file "
1026 "I don't know about\n"));
1030 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
1031 /* This is ok, kernel oplocks come in completely async */
1032 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1037 if (sconn
->using_smb2
) {
1038 send_break_message_smb2(fsp
, 0, OPLOCKLEVEL_NONE
);
1040 send_break_message_smb1(fsp
, OPLOCKLEVEL_NONE
);
1043 fsp
->sent_oplock_break
= BREAK_TO_NONE_SENT
;
1045 add_oplock_timeout_handler(fsp
);
1048 struct break_to_none_state
{
1049 struct smbd_server_connection
*sconn
;
1051 struct smb2_lease_key lease_key
;
1052 struct GUID client_guid
;
1054 static void do_break_to_none(struct tevent_context
*ctx
,
1055 struct tevent_immediate
*im
,
1056 void *private_data
);
1058 /****************************************************************************
1059 This function is called on any file modification or lock request. If a file
1060 is level 2 oplocked then it must tell all other level 2 holders to break to
1062 ****************************************************************************/
1064 static void contend_level2_oplocks_begin_default(files_struct
*fsp
,
1065 enum level2_contention_type type
)
1067 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1068 struct tevent_immediate
*im
;
1069 struct break_to_none_state
*state
;
1070 struct byte_range_lock
*brl
;
1071 uint32_t num_read_oplocks
= 0;
1074 * If this file is level II oplocked then we need
1075 * to grab the shared memory lock and inform all
1076 * other files with a level II lock that they need
1077 * to flush their read caches. We keep the lock over
1078 * the shared memory area whilst doing this.
1081 if (fsp_lease_type_is_exclusive(fsp
)) {
1083 * There can't be any level2 oplocks, we're alone.
1088 brl
= brl_get_locks_readonly(fsp
);
1090 num_read_oplocks
= brl_num_read_oplocks(brl
);
1093 DEBUG(10, ("num_read_oplocks = %"PRIu32
"\n", num_read_oplocks
));
1095 if (num_read_oplocks
== 0) {
1096 DEBUG(10, ("No read oplocks around\n"));
1101 * When we get here we might have a brlock entry locked. Also
1102 * locking the share mode entry would violate the locking
1103 * order. Breaking level2 oplocks to none is asynchronous
1104 * anyway, so we postpone this into an immediate event.
1107 state
= talloc_zero(sconn
, struct break_to_none_state
);
1108 if (state
== NULL
) {
1109 DEBUG(1, ("talloc failed\n"));
1112 state
->sconn
= sconn
;
1113 state
->id
= fsp
->file_id
;
1115 if (fsp
->oplock_type
== LEASE_OPLOCK
) {
1116 state
->client_guid
= *fsp_client_guid(fsp
);
1117 state
->lease_key
= fsp
->lease
->lease
.lease_key
;
1118 DEBUG(10, ("Breaking through lease key %"PRIu64
"/%"PRIu64
"\n",
1119 state
->lease_key
.data
[0],
1120 state
->lease_key
.data
[1]));
1123 im
= tevent_create_immediate(state
);
1125 DEBUG(1, ("tevent_create_immediate failed\n"));
1129 tevent_schedule_immediate(im
, sconn
->ev_ctx
, do_break_to_none
, state
);
1132 static void send_break_to_none(struct messaging_context
*msg_ctx
,
1133 const struct share_mode_entry
*e
)
1135 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1137 share_mode_entry_to_message(msg
, e
);
1138 /* Overload entry->op_type */
1139 SSVAL(msg
, OP_BREAK_MSG_OP_TYPE_OFFSET
, NO_OPLOCK
);
1141 messaging_send_buf(msg_ctx
, e
->pid
, MSG_SMB_BREAK_REQUEST
,
1142 (uint8_t *)msg
, sizeof(msg
));
1145 static void do_break_to_none(struct tevent_context
*ctx
,
1146 struct tevent_immediate
*im
,
1149 struct break_to_none_state
*state
= talloc_get_type_abort(
1150 private_data
, struct break_to_none_state
);
1152 struct share_mode_lock
*lck
;
1153 struct share_mode_data
*d
;
1155 lck
= get_existing_share_mode_lock(talloc_tos(), state
->id
);
1157 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1158 __func__
, file_id_string_tos(&state
->id
)));
1164 * Walk leases and oplocks separately: We have to send one break per
1165 * lease. If we have multiple share_mode_entry having a common lease,
1166 * we would break the lease twice if we don't walk the leases list
1170 for (i
=0; i
<d
->num_leases
; i
++) {
1171 struct share_mode_lease
*l
= &d
->leases
[i
];
1172 struct share_mode_entry
*e
= NULL
;
1175 if ((l
->current_state
& SMB2_LEASE_READ
) == 0) {
1178 if (smb2_lease_equal(&state
->client_guid
,
1182 DEBUG(10, ("Don't break our own lease\n"));
1186 for (j
=0; j
<d
->num_share_modes
; j
++) {
1187 e
= &d
->share_modes
[j
];
1189 if (!is_valid_share_mode_entry(e
)) {
1192 if (e
->lease_idx
== i
) {
1196 if (j
== d
->num_share_modes
) {
1197 DEBUG(0, ("leases[%"PRIu32
"] has no share mode\n",
1202 DEBUG(10, ("Breaking lease# %"PRIu32
" with share_entry# "
1203 "%"PRIu32
"\n", i
, j
));
1205 send_break_to_none(state
->sconn
->msg_ctx
, e
);
1208 for(i
= 0; i
< d
->num_share_modes
; i
++) {
1209 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1211 if (!is_valid_share_mode_entry(e
)) {
1214 if (e
->op_type
== LEASE_OPLOCK
) {
1216 * Took care of those in the loop above
1222 * As there could have been multiple writes waiting at the
1223 * lock_share_entry gate we may not be the first to
1224 * enter. Hence the state of the op_types in the share mode
1225 * entries may be partly NO_OPLOCK and partly LEVEL_II
1226 * oplock. It will do no harm to re-send break messages to
1227 * those smbd's that are still waiting their turn to remove
1228 * their LEVEL_II state, and also no harm to ignore existing
1229 * NO_OPLOCK states. JRA.
1232 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__
,
1235 if (e
->op_type
== NO_OPLOCK
) {
1240 if (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
)) {
1241 DEBUG(0,("%s: PANIC. "
1242 "share mode entry %d is an exclusive "
1243 "oplock !\n", __func__
, i
));
1248 send_break_to_none(state
->sconn
->msg_ctx
, e
);
1251 /* We let the message receivers handle removing the oplock state
1252 in the share mode lock db. */
1260 void smbd_contend_level2_oplocks_begin(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
;
1265 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
1268 if (use_kernel
&& koplocks
->ops
->contend_level2_oplocks_begin
) {
1269 koplocks
->ops
->contend_level2_oplocks_begin(fsp
, type
);
1273 contend_level2_oplocks_begin_default(fsp
, type
);
1276 void smbd_contend_level2_oplocks_end(files_struct
*fsp
,
1277 enum level2_contention_type type
)
1279 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1280 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
1281 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) &&
1284 /* Only kernel oplocks implement this so far */
1285 if (use_kernel
&& koplocks
->ops
->contend_level2_oplocks_end
) {
1286 koplocks
->ops
->contend_level2_oplocks_end(fsp
, type
);
1290 /****************************************************************************
1291 Linearize a share mode entry struct to an internal oplock break message.
1292 ****************************************************************************/
1294 void share_mode_entry_to_message(char *msg
, const struct share_mode_entry
*e
)
1296 SIVAL(msg
,OP_BREAK_MSG_PID_OFFSET
,(uint32_t)e
->pid
.pid
);
1297 SBVAL(msg
,OP_BREAK_MSG_MID_OFFSET
,e
->op_mid
);
1298 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,e
->op_type
);
1299 SIVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
,e
->access_mask
);
1300 SIVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
,e
->share_access
);
1301 SIVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
,e
->private_options
);
1302 SIVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
,(uint32_t)e
->time
.tv_sec
);
1303 SIVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
,(uint32_t)e
->time
.tv_usec
);
1304 push_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
1305 SIVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
,e
->share_file_id
);
1306 SIVAL(msg
,OP_BREAK_MSG_UID_OFFSET
,e
->uid
);
1307 SSVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
,e
->flags
);
1308 SIVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
,e
->name_hash
);
1309 SIVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
,e
->pid
.vnn
);
1312 /****************************************************************************
1313 De-linearize an internal oplock break message to a share mode entry struct.
1314 ****************************************************************************/
1316 void message_to_share_mode_entry(struct share_mode_entry
*e
, const char *msg
)
1318 e
->pid
.pid
= (pid_t
)IVAL(msg
,OP_BREAK_MSG_PID_OFFSET
);
1319 e
->op_mid
= BVAL(msg
,OP_BREAK_MSG_MID_OFFSET
);
1320 e
->op_type
= SVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
);
1321 e
->access_mask
= IVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
);
1322 e
->share_access
= IVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
);
1323 e
->private_options
= IVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
);
1324 e
->time
.tv_sec
= (time_t)IVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
);
1325 e
->time
.tv_usec
= (int)IVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
);
1326 pull_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
1327 e
->share_file_id
= (unsigned long)IVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
);
1328 e
->uid
= (uint32_t)IVAL(msg
,OP_BREAK_MSG_UID_OFFSET
);
1329 e
->flags
= (uint16_t)SVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
);
1330 e
->name_hash
= IVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
);
1331 e
->pid
.vnn
= IVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
);
1334 /****************************************************************************
1335 Setup oplocks for this process.
1336 ****************************************************************************/
1338 bool init_oplocks(struct smbd_server_connection
*sconn
)
1340 DEBUG(3,("init_oplocks: initializing messages.\n"));
1342 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_BREAK_REQUEST
,
1343 process_oplock_break_message
);
1344 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_KERNEL_BREAK
,
1345 process_kernel_oplock_break
);
1349 void init_kernel_oplocks(struct smbd_server_connection
*sconn
)
1351 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
1353 /* only initialize once */
1354 if (koplocks
== NULL
) {
1355 #if HAVE_KERNEL_OPLOCKS_IRIX
1356 koplocks
= irix_init_kernel_oplocks(sconn
);
1357 #elif HAVE_KERNEL_OPLOCKS_LINUX
1358 koplocks
= linux_init_kernel_oplocks(sconn
);
1360 sconn
->oplocks
.kernel_ops
= koplocks
;