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) and no byte-range locks in the file. Returns True
52 ****************************************************************************/
54 bool set_file_oplock(files_struct
*fsp
, int oplock_type
)
56 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
57 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
58 bool use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) && koplocks
;
60 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
62 !(koplocks
->flags
& KOPLOCKS_LEVEL2_SUPPORTED
)) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
64 "don't support them\n"));
69 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
70 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
) &&
72 !koplocks
->ops
->set_oplock(koplocks
, fsp
, oplock_type
)) {
76 fsp
->oplock_type
= oplock_type
;
77 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
78 if (oplock_type
== LEVEL_II_OPLOCK
) {
79 sconn
->oplocks
.level_II_open
++;
80 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
81 sconn
->oplocks
.exclusive_open
++;
84 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
85 "tv_sec = %x, tv_usec = %x\n",
86 fsp_str_dbg(fsp
), file_id_string_tos(&fsp
->file_id
),
87 fsp
->fh
->gen_id
, (int)fsp
->open_time
.tv_sec
,
88 (int)fsp
->open_time
.tv_usec
));
93 /****************************************************************************
94 Attempt to release an oplock on a file. Decrements oplock count.
95 ****************************************************************************/
97 void release_file_oplock(files_struct
*fsp
)
99 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
100 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
102 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
103 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
) &&
105 koplocks
->ops
->release_oplock(koplocks
, fsp
, NO_OPLOCK
);
108 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
109 sconn
->oplocks
.level_II_open
--;
110 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
111 sconn
->oplocks
.exclusive_open
--;
114 SMB_ASSERT(sconn
->oplocks
.exclusive_open
>=0);
115 SMB_ASSERT(sconn
->oplocks
.level_II_open
>=0);
117 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
118 /* This doesn't matter for close. */
119 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
121 fsp
->oplock_type
= NO_OPLOCK
;
123 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
125 flush_write_cache(fsp
, OPLOCK_RELEASE_FLUSH
);
126 delete_write_cache(fsp
);
128 TALLOC_FREE(fsp
->oplock_timeout
);
131 /****************************************************************************
132 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
133 ****************************************************************************/
135 static void downgrade_file_oplock(files_struct
*fsp
)
137 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
138 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
140 if (!EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
141 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
146 koplocks
->ops
->release_oplock(koplocks
, fsp
, LEVEL_II_OPLOCK
);
148 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
149 sconn
->oplocks
.exclusive_open
--;
150 sconn
->oplocks
.level_II_open
++;
151 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
154 /****************************************************************************
155 Remove a file oplock. Copes with level II and exclusive.
156 Locks then unlocks the share mode lock. Client can decide to go directly
157 to none even if a "break-to-level II" was sent.
158 ****************************************************************************/
160 bool remove_oplock(files_struct
*fsp
)
163 struct share_mode_lock
*lck
;
165 /* Remove the oplock flag from the sharemode. */
166 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
168 DEBUG(0,("remove_oplock: failed to lock share entry for "
169 "file %s\n", fsp_str_dbg(fsp
)));
172 ret
= remove_share_oplock(lck
, fsp
);
174 DEBUG(0,("remove_oplock: failed to remove share oplock for "
175 "file %s fnum %d, %s\n",
176 fsp_str_dbg(fsp
), fsp
->fnum
,
177 file_id_string_tos(&fsp
->file_id
)));
179 release_file_oplock(fsp
);
185 * Deal with a reply when a break-to-level II was sent.
187 bool downgrade_oplock(files_struct
*fsp
)
190 struct share_mode_lock
*lck
;
192 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
194 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
195 "file %s\n", fsp_str_dbg(fsp
)));
198 ret
= downgrade_share_oplock(lck
, fsp
);
200 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
201 "for file %s fnum %d, file_id %s\n",
202 fsp_str_dbg(fsp
), fsp
->fnum
,
203 file_id_string_tos(&fsp
->file_id
)));
206 downgrade_file_oplock(fsp
);
212 * Some kernel oplock implementations handle the notification themselves.
214 bool should_notify_deferred_opens(struct smbd_server_connection
*sconn
)
216 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
218 (koplocks
->flags
& KOPLOCKS_DEFERRED_OPEN_NOTIFICATION
));
221 /****************************************************************************
222 Set up an oplock break message.
223 ****************************************************************************/
225 static char *new_break_message_smb1(TALLOC_CTX
*mem_ctx
,
226 files_struct
*fsp
, int cmd
)
228 char *result
= talloc_array(mem_ctx
, char, smb_size
+ 8*2 + 0);
230 if (result
== NULL
) {
231 DEBUG(0, ("talloc failed\n"));
235 memset(result
,'\0',smb_size
);
236 srv_set_message(result
,8,0,true);
237 SCVAL(result
,smb_com
,SMBlockingX
);
238 SSVAL(result
,smb_tid
,fsp
->conn
->cnum
);
239 SSVAL(result
,smb_pid
,0xFFFF);
240 SSVAL(result
,smb_uid
,0);
241 SSVAL(result
,smb_mid
,0xFFFF);
242 SCVAL(result
,smb_vwv0
,0xFF);
243 SSVAL(result
,smb_vwv2
,fsp
->fnum
);
244 SCVAL(result
,smb_vwv3
,LOCKING_ANDX_OPLOCK_RELEASE
);
245 SCVAL(result
,smb_vwv3
+1,cmd
);
249 /****************************************************************************
250 Function to do the waiting before sending a local break.
251 ****************************************************************************/
253 static void wait_before_sending_break(void)
255 long wait_time
= (long)lp_oplock_break_wait_time();
258 smb_msleep(wait_time
);
262 /****************************************************************************
263 Ensure that we have a valid oplock.
264 ****************************************************************************/
266 static files_struct
*initial_break_processing(
267 struct smbd_server_connection
*sconn
, struct file_id id
,
268 unsigned long file_id
)
270 files_struct
*fsp
= NULL
;
272 if( DEBUGLVL( 3 ) ) {
273 dbgtext( "initial_break_processing: called for %s/%u\n",
274 file_id_string_tos(&id
), (int)file_id
);
275 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
276 sconn
->oplocks
.exclusive_open
,
277 sconn
->oplocks
.level_II_open
);
281 * We need to search the file open table for the
282 * entry containing this dev and inode, and ensure
283 * we have an oplock on it.
286 fsp
= file_find_dif(sconn
, id
, file_id
);
289 /* The file could have been closed in the meantime - return success. */
290 if( DEBUGLVL( 3 ) ) {
291 dbgtext( "initial_break_processing: cannot find open file with " );
292 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id
), file_id
);
293 dbgtext( "allowing break to succeed.\n" );
298 /* Ensure we have an oplock on the file */
301 * There is a potential race condition in that an oplock could
302 * have been broken due to another udp request, and yet there are
303 * still oplock break messages being sent in the udp message
304 * queue for this file. So return true if we don't have an oplock,
305 * as we may have just freed it.
308 if(fsp
->oplock_type
== NO_OPLOCK
) {
309 if( DEBUGLVL( 3 ) ) {
310 dbgtext( "initial_break_processing: file %s ",
312 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
313 file_id_string_tos(&id
), fsp
->fh
->gen_id
);
314 dbgtext( "Allowing break to succeed regardless.\n" );
322 static void oplock_timeout_handler(struct event_context
*ctx
,
323 struct timed_event
*te
,
327 files_struct
*fsp
= (files_struct
*)private_data
;
329 /* Remove the timed event handler. */
330 TALLOC_FREE(fsp
->oplock_timeout
);
331 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
334 reply_to_oplock_break_requests(fsp
);
337 /*******************************************************************
338 Add a timeout handler waiting for the client reply.
339 *******************************************************************/
341 static void add_oplock_timeout_handler(files_struct
*fsp
)
343 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
344 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
347 * If kernel oplocks already notifies smbds when an oplock break times
351 (koplocks
->flags
& KOPLOCKS_TIMEOUT_NOTIFICATION
)) {
355 if (fsp
->oplock_timeout
!= NULL
) {
356 DEBUG(0, ("Logic problem -- have an oplock event hanging "
360 fsp
->oplock_timeout
=
361 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, fsp
,
362 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0),
363 oplock_timeout_handler
, fsp
);
365 if (fsp
->oplock_timeout
== NULL
) {
366 DEBUG(0, ("Could not add oplock timeout handler\n"));
370 static void send_break_message_smb1(files_struct
*fsp
, int level
)
372 char *break_msg
= new_break_message_smb1(talloc_tos(),
375 if (break_msg
== NULL
) {
376 exit_server("Could not talloc break_msg\n");
380 if (!srv_send_smb(fsp
->conn
->sconn
,
382 IS_CONN_ENCRYPTED(fsp
->conn
),
384 exit_server_cleanly("send_break_message_smb1: "
385 "srv_send_smb failed.");
388 TALLOC_FREE(break_msg
);
391 void break_level2_to_none_async(files_struct
*fsp
)
393 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
395 if (fsp
->oplock_type
== NO_OPLOCK
) {
396 /* We already got a "break to none" message and we've handled
397 * it. just ignore. */
398 DEBUG(3, ("process_oplock_async_level2_break_message: already "
399 "broken to none, ignoring.\n"));
403 if (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
404 /* Don't tell the client, just downgrade. */
405 DEBUG(3, ("process_oplock_async_level2_break_message: "
406 "downgrading fake level 2 oplock.\n"));
411 /* Ensure we're really at level2 state. */
412 SMB_ASSERT(fsp
->oplock_type
== LEVEL_II_OPLOCK
);
414 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
415 "to none message for fid %d, file %s\n", fsp
->fnum
,
418 /* Now send a break to none message to our client. */
419 if (sconn
->using_smb2
) {
420 send_break_message_smb2(fsp
, OPLOCKLEVEL_NONE
);
422 send_break_message_smb1(fsp
, OPLOCKLEVEL_NONE
);
425 /* Async level2 request, don't send a reply, just remove the oplock. */
429 /*******************************************************************
430 This handles the case of a write triggering a break to none
431 message on a level2 oplock.
432 When we get this message we may be in any of three states :
433 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
434 the client for LEVEL2.
435 *******************************************************************/
437 static void process_oplock_async_level2_break_message(struct messaging_context
*msg_ctx
,
440 struct server_id src
,
443 struct share_mode_entry msg
;
445 struct smbd_server_connection
*sconn
=
446 talloc_get_type_abort(private_data
,
447 struct smbd_server_connection
);
449 if (data
->data
== NULL
) {
450 DEBUG(0, ("Got NULL buffer\n"));
454 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
455 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
459 /* De-linearize incoming message. */
460 message_to_share_mode_entry(&msg
, (char *)data
->data
);
462 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
463 "%s/%llu\n", server_id_str(talloc_tos(), &src
),
464 file_id_string_tos(&msg
.id
),
465 (unsigned long long)msg
.share_file_id
));
467 fsp
= initial_break_processing(sconn
, msg
.id
, msg
.share_file_id
);
470 /* We hit a race here. Break messages are sent, and before we
471 * get to process this message, we have closed the file.
472 * No need to reply as this is an async message. */
473 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
477 break_level2_to_none_async(fsp
);
480 /*******************************************************************
481 This handles the generic oplock break message from another smbd.
482 *******************************************************************/
484 static void process_oplock_break_message(struct messaging_context
*msg_ctx
,
487 struct server_id src
,
490 struct share_mode_entry msg
;
492 bool break_to_level2
= False
;
494 struct smbd_server_connection
*sconn
=
495 talloc_get_type_abort(private_data
,
496 struct smbd_server_connection
);
497 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
498 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
500 if (data
->data
== NULL
) {
501 DEBUG(0, ("Got NULL buffer\n"));
505 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
506 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
510 /* De-linearize incoming message. */
511 message_to_share_mode_entry(&msg
, (char *)data
->data
);
513 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
514 server_id_str(talloc_tos(), &src
),
515 file_id_string_tos(&msg
.id
),
516 (unsigned long long)msg
.share_file_id
));
518 fsp
= initial_break_processing(sconn
, msg
.id
, msg
.share_file_id
);
521 /* We hit a race here. Break messages are sent, and before we
522 * get to process this message, we have closed the file. Reply
523 * with 'ok, oplock broken' */
524 DEBUG(3, ("Did not find fsp\n"));
526 /* We just send the same message back. */
527 messaging_send_buf(msg_ctx
, src
, MSG_SMB_BREAK_RESPONSE
,
529 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
533 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
534 /* Remember we have to inform the requesting PID when the
537 ADD_TO_ARRAY(NULL
, struct share_mode_entry
, msg
,
538 &fsp
->pending_break_messages
,
539 &fsp
->num_pending_break_messages
);
543 if (EXCLUSIVE_OPLOCK_TYPE(msg
.op_type
) &&
544 !EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
545 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
546 file_id_string_tos(&fsp
->file_id
),
548 /* We just send the same message back. */
549 messaging_send_buf(msg_ctx
, src
, MSG_SMB_BREAK_RESPONSE
,
551 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
555 use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) && koplocks
;
557 if ((global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
558 !(msg
.op_type
& FORCE_OPLOCK_BREAK_TO_NONE
) &&
559 !(use_kernel
&& !(koplocks
->flags
& KOPLOCKS_LEVEL2_SUPPORTED
)) &&
560 lp_level2_oplocks(SNUM(fsp
->conn
))) {
561 break_to_level2
= True
;
564 /* Need to wait before sending a break
565 message if we sent ourselves this message. */
566 if (procid_equal(&self
, &src
)) {
567 wait_before_sending_break();
570 if (sconn
->using_smb2
) {
571 send_break_message_smb2(fsp
, break_to_level2
?
572 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
574 send_break_message_smb1(fsp
, break_to_level2
?
575 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
578 fsp
->sent_oplock_break
= break_to_level2
? LEVEL_II_BREAK_SENT
:BREAK_TO_NONE_SENT
;
581 ADD_TO_ARRAY(NULL
, struct share_mode_entry
, msg
,
582 &fsp
->pending_break_messages
,
583 &fsp
->num_pending_break_messages
);
585 add_oplock_timeout_handler(fsp
);
588 /*******************************************************************
589 This handles the kernel oplock break message.
590 *******************************************************************/
592 static void process_kernel_oplock_break(struct messaging_context
*msg_ctx
,
595 struct server_id src
,
599 unsigned long file_id
;
601 struct smbd_server_connection
*sconn
=
602 talloc_get_type_abort(private_data
,
603 struct smbd_server_connection
);
605 if (data
->data
== NULL
) {
606 DEBUG(0, ("Got NULL buffer\n"));
610 if (data
->length
!= MSG_SMB_KERNEL_BREAK_SIZE
) {
611 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
615 /* Pull the data from the message. */
616 pull_file_id_24((char *)data
->data
, &id
);
617 file_id
= (unsigned long)IVAL(data
->data
, 24);
619 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
620 server_id_str(talloc_tos(), &src
), file_id_string_tos(&id
),
621 (unsigned int)file_id
));
623 fsp
= initial_break_processing(sconn
, id
, file_id
);
626 DEBUG(3, ("Got a kernel oplock break message for a file "
627 "I don't know about\n"));
631 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
632 /* This is ok, kernel oplocks come in completely async */
633 DEBUG(3, ("Got a kernel oplock request while waiting for a "
638 if (sconn
->using_smb2
) {
639 send_break_message_smb2(fsp
, OPLOCKLEVEL_NONE
);
641 send_break_message_smb1(fsp
, OPLOCKLEVEL_NONE
);
644 fsp
->sent_oplock_break
= BREAK_TO_NONE_SENT
;
646 add_oplock_timeout_handler(fsp
);
649 void reply_to_oplock_break_requests(files_struct
*fsp
)
651 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
652 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
656 * If kernel oplocks already notifies smbds when oplocks are
657 * broken/removed, just return.
660 (koplocks
->flags
& KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION
)) {
664 for (i
=0; i
<fsp
->num_pending_break_messages
; i
++) {
665 struct share_mode_entry
*e
= &fsp
->pending_break_messages
[i
];
666 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
668 share_mode_entry_to_message(msg
, e
);
670 messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, e
->pid
,
671 MSG_SMB_BREAK_RESPONSE
,
673 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
676 SAFE_FREE(fsp
->pending_break_messages
);
677 fsp
->num_pending_break_messages
= 0;
678 TALLOC_FREE(fsp
->oplock_timeout
);
682 static void process_oplock_break_response(struct messaging_context
*msg_ctx
,
685 struct server_id src
,
688 struct share_mode_entry msg
;
689 struct smbd_server_connection
*sconn
=
690 talloc_get_type_abort(private_data
,
691 struct smbd_server_connection
);
693 if (data
->data
== NULL
) {
694 DEBUG(0, ("Got NULL buffer\n"));
698 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
699 DEBUG(0, ("Got invalid msg len %u\n",
700 (unsigned int)data
->length
));
704 /* De-linearize incoming message. */
705 message_to_share_mode_entry(&msg
, (char *)data
->data
);
707 DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
708 server_id_str(talloc_tos(), &src
),
709 file_id_string_tos(&msg
.id
),
710 (unsigned long long)msg
.share_file_id
,
711 (unsigned long long)msg
.op_mid
));
713 schedule_deferred_open_message_smb(sconn
, msg
.op_mid
);
716 static void process_open_retry_message(struct messaging_context
*msg_ctx
,
719 struct server_id src
,
722 struct share_mode_entry msg
;
723 struct smbd_server_connection
*sconn
=
724 talloc_get_type_abort(private_data
,
725 struct smbd_server_connection
);
727 if (data
->data
== NULL
) {
728 DEBUG(0, ("Got NULL buffer\n"));
732 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
733 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
737 /* De-linearize incoming message. */
738 message_to_share_mode_entry(&msg
, (char *)data
->data
);
740 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
741 server_id_str(talloc_tos(), &src
), file_id_string_tos(&msg
.id
),
742 (unsigned long long)msg
.op_mid
));
744 schedule_deferred_open_message_smb(sconn
, msg
.op_mid
);
747 struct break_to_none_state
{
748 struct smbd_server_connection
*sconn
;
751 static void do_break_to_none(struct tevent_req
*req
);
753 /****************************************************************************
754 This function is called on any file modification or lock request. If a file
755 is level 2 oplocked then it must tell all other level 2 holders to break to
757 ****************************************************************************/
759 static void contend_level2_oplocks_begin_default(files_struct
*fsp
,
760 enum level2_contention_type type
)
762 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
763 struct tevent_req
*req
;
764 struct break_to_none_state
*state
;
767 * If this file is level II oplocked then we need
768 * to grab the shared memory lock and inform all
769 * other files with a level II lock that they need
770 * to flush their read caches. We keep the lock over
771 * the shared memory area whilst doing this.
774 if (!LEVEL_II_OPLOCK_TYPE(fsp
->oplock_type
))
778 * When we get here we might have a brlock entry locked. Also
779 * locking the share mode entry would violate the locking
780 * order. Breaking level2 oplocks to none is asynchronous
781 * anyway, so we postpone this into an immediate timed event.
784 state
= talloc(sconn
, struct break_to_none_state
);
786 DEBUG(1, ("talloc failed\n"));
789 state
->sconn
= sconn
;
790 state
->id
= fsp
->file_id
;
792 req
= tevent_wakeup_send(state
, sconn
->ev_ctx
, timeval_set(0, 0));
794 DEBUG(1, ("tevent_wakeup_send failed\n"));
798 tevent_req_set_callback(req
, do_break_to_none
, state
);
802 static void do_break_to_none(struct tevent_req
*req
)
804 struct break_to_none_state
*state
= tevent_req_callback_data(
805 req
, struct break_to_none_state
);
806 struct server_id self
= messaging_server_id(state
->sconn
->msg_ctx
);
809 struct share_mode_lock
*lck
;
811 ret
= tevent_wakeup_recv(req
);
814 DEBUG(1, ("tevent_wakeup_recv failed\n"));
817 lck
= get_existing_share_mode_lock(talloc_tos(), state
->id
);
819 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
820 "share mode entry for file %s.\n",
821 file_id_string_tos(&state
->id
)));
825 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
826 lck
->data
->num_share_modes
));
828 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
829 struct share_mode_entry
*share_entry
= &lck
->data
->share_modes
[i
];
830 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
832 if (!is_valid_share_mode_entry(share_entry
)) {
837 * As there could have been multiple writes waiting at the
838 * lock_share_entry gate we may not be the first to
839 * enter. Hence the state of the op_types in the share mode
840 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
841 * oplock. It will do no harm to re-send break messages to
842 * those smbd's that are still waiting their turn to remove
843 * their LEVEL_II state, and also no harm to ignore existing
844 * NO_OPLOCK states. JRA.
847 DEBUG(10,("release_level_2_oplocks_on_change: "
848 "share_entry[%i]->op_type == %d\n",
849 i
, share_entry
->op_type
));
851 if (share_entry
->op_type
== NO_OPLOCK
) {
856 if (EXCLUSIVE_OPLOCK_TYPE(share_entry
->op_type
)) {
857 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
858 "share mode entry %d is an exlusive "
864 share_mode_entry_to_message(msg
, share_entry
);
867 * Deal with a race condition when breaking level2
868 * oplocks. Don't send all the messages and release
869 * the lock, this allows someone else to come in and
870 * get a level2 lock before any of the messages are
871 * processed, and thus miss getting a break message.
872 * Ensure at least one entry (the one we're breaking)
873 * is processed immediately under the lock and becomes
874 * set as NO_OPLOCK to stop any waiter getting a level2.
878 if (procid_equal(&self
, &share_entry
->pid
)) {
879 struct files_struct
*cur_fsp
=
880 initial_break_processing(state
->sconn
,
882 share_entry
->share_file_id
);
883 wait_before_sending_break();
884 if (cur_fsp
!= NULL
) {
885 break_level2_to_none_async(cur_fsp
);
887 DEBUG(3, ("release_level_2_oplocks_on_change: "
888 "Did not find fsp, ignoring\n"));
891 messaging_send_buf(state
->sconn
->msg_ctx
,
893 MSG_SMB_ASYNC_LEVEL2_BREAK
,
895 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
899 /* We let the message receivers handle removing the oplock state
900 in the share mode lock db. */
908 void smbd_contend_level2_oplocks_begin(files_struct
*fsp
,
909 enum level2_contention_type type
)
911 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
912 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
914 if (koplocks
&& koplocks
->ops
->contend_level2_oplocks_begin
) {
915 koplocks
->ops
->contend_level2_oplocks_begin(fsp
, type
);
919 contend_level2_oplocks_begin_default(fsp
, type
);
922 void smbd_contend_level2_oplocks_end(files_struct
*fsp
,
923 enum level2_contention_type type
)
925 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
926 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
928 /* Only kernel oplocks implement this so far */
929 if (koplocks
&& koplocks
->ops
->contend_level2_oplocks_end
) {
930 koplocks
->ops
->contend_level2_oplocks_end(fsp
, type
);
934 /****************************************************************************
935 Linearize a share mode entry struct to an internal oplock break message.
936 ****************************************************************************/
938 void share_mode_entry_to_message(char *msg
, const struct share_mode_entry
*e
)
940 SIVAL(msg
,OP_BREAK_MSG_PID_OFFSET
,(uint32
)e
->pid
.pid
);
941 SBVAL(msg
,OP_BREAK_MSG_MID_OFFSET
,e
->op_mid
);
942 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,e
->op_type
);
943 SIVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
,e
->access_mask
);
944 SIVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
,e
->share_access
);
945 SIVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
,e
->private_options
);
946 SIVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
,(uint32_t)e
->time
.tv_sec
);
947 SIVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
,(uint32_t)e
->time
.tv_usec
);
948 push_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
949 SIVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
,e
->share_file_id
);
950 SIVAL(msg
,OP_BREAK_MSG_UID_OFFSET
,e
->uid
);
951 SSVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
,e
->flags
);
952 SIVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
,e
->name_hash
);
953 SIVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
,e
->pid
.vnn
);
956 /****************************************************************************
957 De-linearize an internal oplock break message to a share mode entry struct.
958 ****************************************************************************/
960 void message_to_share_mode_entry(struct share_mode_entry
*e
, char *msg
)
962 e
->pid
.pid
= (pid_t
)IVAL(msg
,OP_BREAK_MSG_PID_OFFSET
);
963 e
->op_mid
= BVAL(msg
,OP_BREAK_MSG_MID_OFFSET
);
964 e
->op_type
= SVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
);
965 e
->access_mask
= IVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
);
966 e
->share_access
= IVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
);
967 e
->private_options
= IVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
);
968 e
->time
.tv_sec
= (time_t)IVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
);
969 e
->time
.tv_usec
= (int)IVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
);
970 pull_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
971 e
->share_file_id
= (unsigned long)IVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
);
972 e
->uid
= (uint32
)IVAL(msg
,OP_BREAK_MSG_UID_OFFSET
);
973 e
->flags
= (uint16
)SVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
);
974 e
->name_hash
= IVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
);
975 e
->pid
.vnn
= IVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
);
978 /****************************************************************************
979 Setup oplocks for this process.
980 ****************************************************************************/
982 bool init_oplocks(struct smbd_server_connection
*sconn
)
984 DEBUG(3,("init_oplocks: initializing messages.\n"));
986 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_BREAK_REQUEST
,
987 process_oplock_break_message
);
988 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_ASYNC_LEVEL2_BREAK
,
989 process_oplock_async_level2_break_message
);
990 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_BREAK_RESPONSE
,
991 process_oplock_break_response
);
992 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_KERNEL_BREAK
,
993 process_kernel_oplock_break
);
994 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_OPEN_RETRY
,
995 process_open_retry_message
);
1000 void init_kernel_oplocks(struct smbd_server_connection
*sconn
)
1002 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
1004 /* only initialize once */
1005 if (koplocks
== NULL
) {
1006 #if HAVE_KERNEL_OPLOCKS_IRIX
1007 koplocks
= irix_init_kernel_oplocks(sconn
);
1008 #elif HAVE_KERNEL_OPLOCKS_LINUX
1009 koplocks
= linux_init_kernel_oplocks(sconn
);
1011 sconn
->oplocks
.kernel_ops
= koplocks
;