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 NTSTATUS
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"));
65 return NT_STATUS_NOT_SUPPORTED
;
69 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
70 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
) &&
72 !koplocks
->ops
->set_oplock(koplocks
, fsp
, oplock_type
))
74 return map_nt_error_from_unix(errno
);
77 fsp
->oplock_type
= oplock_type
;
78 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
79 if (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 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
;
103 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
104 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
) &&
106 koplocks
->ops
->release_oplock(koplocks
, fsp
, NO_OPLOCK
);
109 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
110 sconn
->oplocks
.level_II_open
--;
111 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
112 sconn
->oplocks
.exclusive_open
--;
115 SMB_ASSERT(sconn
->oplocks
.exclusive_open
>=0);
116 SMB_ASSERT(sconn
->oplocks
.level_II_open
>=0);
118 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
119 /* This doesn't matter for close. */
120 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
122 fsp
->oplock_type
= NO_OPLOCK
;
124 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
126 flush_write_cache(fsp
, OPLOCK_RELEASE_FLUSH
);
127 delete_write_cache(fsp
);
129 TALLOC_FREE(fsp
->oplock_timeout
);
132 /****************************************************************************
133 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
134 ****************************************************************************/
136 static void downgrade_file_oplock(files_struct
*fsp
)
138 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
139 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
141 if (!EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
142 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
147 koplocks
->ops
->release_oplock(koplocks
, fsp
, LEVEL_II_OPLOCK
);
149 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
150 sconn
->oplocks
.exclusive_open
--;
151 sconn
->oplocks
.level_II_open
++;
152 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
155 /****************************************************************************
156 Remove a file oplock. Copes with level II and exclusive.
157 Locks then unlocks the share mode lock. Client can decide to go directly
158 to none even if a "break-to-level II" was sent.
159 ****************************************************************************/
161 bool remove_oplock(files_struct
*fsp
)
164 struct share_mode_lock
*lck
;
166 /* Remove the oplock flag from the sharemode. */
167 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
169 DEBUG(0,("remove_oplock: failed to lock share entry for "
170 "file %s\n", fsp_str_dbg(fsp
)));
173 ret
= remove_share_oplock(lck
, fsp
);
175 DEBUG(0,("remove_oplock: failed to remove share oplock for "
177 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
178 file_id_string_tos(&fsp
->file_id
)));
180 release_file_oplock(fsp
);
186 * Deal with a reply when a break-to-level II was sent.
188 bool downgrade_oplock(files_struct
*fsp
)
191 struct share_mode_lock
*lck
;
193 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
195 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
196 "file %s\n", fsp_str_dbg(fsp
)));
199 ret
= downgrade_share_oplock(lck
, fsp
);
201 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
202 "for file %s, %s, file_id %s\n",
203 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
204 file_id_string_tos(&fsp
->file_id
)));
207 downgrade_file_oplock(fsp
);
213 * Some kernel oplock implementations handle the notification themselves.
215 bool should_notify_deferred_opens(struct smbd_server_connection
*sconn
)
217 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
219 (koplocks
->flags
& KOPLOCKS_DEFERRED_OPEN_NOTIFICATION
));
222 /****************************************************************************
223 Set up an oplock break message.
224 ****************************************************************************/
226 static char *new_break_message_smb1(TALLOC_CTX
*mem_ctx
,
227 files_struct
*fsp
, int cmd
)
229 char *result
= talloc_array(mem_ctx
, char, smb_size
+ 8*2 + 0);
231 if (result
== NULL
) {
232 DEBUG(0, ("talloc failed\n"));
236 memset(result
,'\0',smb_size
);
237 srv_set_message(result
,8,0,true);
238 SCVAL(result
,smb_com
,SMBlockingX
);
239 SSVAL(result
,smb_tid
,fsp
->conn
->cnum
);
240 SSVAL(result
,smb_pid
,0xFFFF);
241 SSVAL(result
,smb_uid
,0);
242 SSVAL(result
,smb_mid
,0xFFFF);
243 SCVAL(result
,smb_vwv0
,0xFF);
244 SSVAL(result
,smb_vwv2
,fsp
->fnum
);
245 SCVAL(result
,smb_vwv3
,LOCKING_ANDX_OPLOCK_RELEASE
);
246 SCVAL(result
,smb_vwv3
+1,cmd
);
250 /****************************************************************************
251 Function to do the waiting before sending a local break.
252 ****************************************************************************/
254 static void wait_before_sending_break(void)
256 long wait_time
= (long)lp_oplock_break_wait_time();
259 smb_msleep(wait_time
);
263 /****************************************************************************
264 Ensure that we have a valid oplock.
265 ****************************************************************************/
267 static files_struct
*initial_break_processing(
268 struct smbd_server_connection
*sconn
, struct file_id id
,
269 unsigned long file_id
)
271 files_struct
*fsp
= NULL
;
273 if( DEBUGLVL( 3 ) ) {
274 dbgtext( "initial_break_processing: called for %s/%u\n",
275 file_id_string_tos(&id
), (int)file_id
);
276 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
277 sconn
->oplocks
.exclusive_open
,
278 sconn
->oplocks
.level_II_open
);
282 * We need to search the file open table for the
283 * entry containing this dev and inode, and ensure
284 * we have an oplock on it.
287 fsp
= file_find_dif(sconn
, id
, file_id
);
290 /* The file could have been closed in the meantime - return success. */
291 if( DEBUGLVL( 3 ) ) {
292 dbgtext( "initial_break_processing: cannot find open file with " );
293 dbgtext( "file_id %s gen_id = %lu, ", file_id_string_tos(&id
), file_id
);
294 dbgtext( "allowing break to succeed.\n" );
299 /* Ensure we have an oplock on the file */
302 * There is a potential race condition in that an oplock could
303 * have been broken due to another udp request, and yet there are
304 * still oplock break messages being sent in the udp message
305 * queue for this file. So return true if we don't have an oplock,
306 * as we may have just freed it.
309 if(fsp
->oplock_type
== NO_OPLOCK
) {
310 if( DEBUGLVL( 3 ) ) {
311 dbgtext( "initial_break_processing: file %s ",
313 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
314 file_id_string_tos(&id
), fsp
->fh
->gen_id
);
315 dbgtext( "Allowing break to succeed regardless.\n" );
323 static void oplock_timeout_handler(struct tevent_context
*ctx
,
324 struct tevent_timer
*te
,
328 files_struct
*fsp
= (files_struct
*)private_data
;
330 /* Remove the timed event handler. */
331 TALLOC_FREE(fsp
->oplock_timeout
);
332 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
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 %s, file %s\n", fsp_fnum_dbg(fsp
),
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. */
523 DEBUG(3, ("Did not find fsp\n"));
527 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
528 /* Remember we have to inform the requesting PID when the
531 ADD_TO_ARRAY(NULL
, struct share_mode_entry
, msg
,
532 &fsp
->pending_break_messages
,
533 &fsp
->num_pending_break_messages
);
537 if (EXCLUSIVE_OPLOCK_TYPE(msg
.op_type
) &&
538 !EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
539 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
540 file_id_string_tos(&fsp
->file_id
),
545 use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) && koplocks
;
547 if ((global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
548 !(msg
.op_type
& FORCE_OPLOCK_BREAK_TO_NONE
) &&
549 !(use_kernel
&& !(koplocks
->flags
& KOPLOCKS_LEVEL2_SUPPORTED
)) &&
550 lp_level2_oplocks(SNUM(fsp
->conn
))) {
551 break_to_level2
= True
;
554 /* Need to wait before sending a break
555 message if we sent ourselves this message. */
556 if (serverid_equal(&self
, &src
)) {
557 wait_before_sending_break();
560 if (sconn
->using_smb2
) {
561 send_break_message_smb2(fsp
, break_to_level2
?
562 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
564 send_break_message_smb1(fsp
, break_to_level2
?
565 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
568 fsp
->sent_oplock_break
= break_to_level2
? LEVEL_II_BREAK_SENT
:BREAK_TO_NONE_SENT
;
571 ADD_TO_ARRAY(NULL
, struct share_mode_entry
, msg
,
572 &fsp
->pending_break_messages
,
573 &fsp
->num_pending_break_messages
);
575 add_oplock_timeout_handler(fsp
);
578 /*******************************************************************
579 This handles the kernel oplock break message.
580 *******************************************************************/
582 static void process_kernel_oplock_break(struct messaging_context
*msg_ctx
,
585 struct server_id src
,
589 unsigned long file_id
;
591 struct smbd_server_connection
*sconn
=
592 talloc_get_type_abort(private_data
,
593 struct smbd_server_connection
);
595 if (data
->data
== NULL
) {
596 DEBUG(0, ("Got NULL buffer\n"));
600 if (data
->length
!= MSG_SMB_KERNEL_BREAK_SIZE
) {
601 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
605 /* Pull the data from the message. */
606 pull_file_id_24((char *)data
->data
, &id
);
607 file_id
= (unsigned long)IVAL(data
->data
, 24);
609 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
610 server_id_str(talloc_tos(), &src
), file_id_string_tos(&id
),
611 (unsigned int)file_id
));
613 fsp
= initial_break_processing(sconn
, id
, file_id
);
616 DEBUG(3, ("Got a kernel oplock break message for a file "
617 "I don't know about\n"));
621 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
622 /* This is ok, kernel oplocks come in completely async */
623 DEBUG(3, ("Got a kernel oplock request while waiting for a "
628 if (sconn
->using_smb2
) {
629 send_break_message_smb2(fsp
, OPLOCKLEVEL_NONE
);
631 send_break_message_smb1(fsp
, OPLOCKLEVEL_NONE
);
634 fsp
->sent_oplock_break
= BREAK_TO_NONE_SENT
;
636 add_oplock_timeout_handler(fsp
);
639 static void process_open_retry_message(struct messaging_context
*msg_ctx
,
642 struct server_id src
,
645 struct share_mode_entry msg
;
646 struct smbd_server_connection
*sconn
=
647 talloc_get_type_abort(private_data
,
648 struct smbd_server_connection
);
650 if (data
->data
== NULL
) {
651 DEBUG(0, ("Got NULL buffer\n"));
655 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
656 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
660 /* De-linearize incoming message. */
661 message_to_share_mode_entry(&msg
, (char *)data
->data
);
663 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
664 server_id_str(talloc_tos(), &src
), file_id_string_tos(&msg
.id
),
665 (unsigned long long)msg
.op_mid
));
667 schedule_deferred_open_message_smb(sconn
, msg
.op_mid
);
670 struct break_to_none_state
{
671 struct smbd_server_connection
*sconn
;
674 static void do_break_to_none(struct tevent_req
*req
);
676 /****************************************************************************
677 This function is called on any file modification or lock request. If a file
678 is level 2 oplocked then it must tell all other level 2 holders to break to
680 ****************************************************************************/
682 static void contend_level2_oplocks_begin_default(files_struct
*fsp
,
683 enum level2_contention_type type
)
685 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
686 struct tevent_req
*req
;
687 struct break_to_none_state
*state
;
690 * If this file is level II oplocked then we need
691 * to grab the shared memory lock and inform all
692 * other files with a level II lock that they need
693 * to flush their read caches. We keep the lock over
694 * the shared memory area whilst doing this.
697 if (!LEVEL_II_OPLOCK_TYPE(fsp
->oplock_type
))
701 * When we get here we might have a brlock entry locked. Also
702 * locking the share mode entry would violate the locking
703 * order. Breaking level2 oplocks to none is asynchronous
704 * anyway, so we postpone this into an immediate timed event.
707 state
= talloc(sconn
, struct break_to_none_state
);
709 DEBUG(1, ("talloc failed\n"));
712 state
->sconn
= sconn
;
713 state
->id
= fsp
->file_id
;
715 req
= tevent_wakeup_send(state
, sconn
->ev_ctx
, timeval_set(0, 0));
717 DEBUG(1, ("tevent_wakeup_send failed\n"));
721 tevent_req_set_callback(req
, do_break_to_none
, state
);
725 static void do_break_to_none(struct tevent_req
*req
)
727 struct break_to_none_state
*state
= tevent_req_callback_data(
728 req
, struct break_to_none_state
);
729 struct server_id self
= messaging_server_id(state
->sconn
->msg_ctx
);
732 struct share_mode_lock
*lck
;
734 ret
= tevent_wakeup_recv(req
);
737 DEBUG(1, ("tevent_wakeup_recv failed\n"));
740 lck
= get_existing_share_mode_lock(talloc_tos(), state
->id
);
742 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
743 "share mode entry for file %s.\n",
744 file_id_string_tos(&state
->id
)));
748 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
749 lck
->data
->num_share_modes
));
751 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
752 struct share_mode_entry
*share_entry
= &lck
->data
->share_modes
[i
];
753 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
755 if (!is_valid_share_mode_entry(share_entry
)) {
760 * As there could have been multiple writes waiting at the
761 * lock_share_entry gate we may not be the first to
762 * enter. Hence the state of the op_types in the share mode
763 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
764 * oplock. It will do no harm to re-send break messages to
765 * those smbd's that are still waiting their turn to remove
766 * their LEVEL_II state, and also no harm to ignore existing
767 * NO_OPLOCK states. JRA.
770 DEBUG(10,("release_level_2_oplocks_on_change: "
771 "share_entry[%i]->op_type == %d\n",
772 i
, share_entry
->op_type
));
774 if (share_entry
->op_type
== NO_OPLOCK
) {
779 if (EXCLUSIVE_OPLOCK_TYPE(share_entry
->op_type
)) {
780 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
781 "share mode entry %d is an exlusive "
787 share_mode_entry_to_message(msg
, share_entry
);
790 * Deal with a race condition when breaking level2
791 * oplocks. Don't send all the messages and release
792 * the lock, this allows someone else to come in and
793 * get a level2 lock before any of the messages are
794 * processed, and thus miss getting a break message.
795 * Ensure at least one entry (the one we're breaking)
796 * is processed immediately under the lock and becomes
797 * set as NO_OPLOCK to stop any waiter getting a level2.
801 if (serverid_equal(&self
, &share_entry
->pid
)) {
802 struct files_struct
*cur_fsp
=
803 initial_break_processing(state
->sconn
,
805 share_entry
->share_file_id
);
806 wait_before_sending_break();
807 if (cur_fsp
!= NULL
) {
808 break_level2_to_none_async(cur_fsp
);
810 DEBUG(3, ("release_level_2_oplocks_on_change: "
811 "Did not find fsp, ignoring\n"));
814 messaging_send_buf(state
->sconn
->msg_ctx
,
816 MSG_SMB_ASYNC_LEVEL2_BREAK
,
818 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
822 /* We let the message receivers handle removing the oplock state
823 in the share mode lock db. */
831 void smbd_contend_level2_oplocks_begin(files_struct
*fsp
,
832 enum level2_contention_type type
)
834 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
835 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
837 if (koplocks
&& koplocks
->ops
->contend_level2_oplocks_begin
) {
838 koplocks
->ops
->contend_level2_oplocks_begin(fsp
, type
);
842 contend_level2_oplocks_begin_default(fsp
, type
);
845 void smbd_contend_level2_oplocks_end(files_struct
*fsp
,
846 enum level2_contention_type type
)
848 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
849 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
851 /* Only kernel oplocks implement this so far */
852 if (koplocks
&& koplocks
->ops
->contend_level2_oplocks_end
) {
853 koplocks
->ops
->contend_level2_oplocks_end(fsp
, type
);
857 /****************************************************************************
858 Linearize a share mode entry struct to an internal oplock break message.
859 ****************************************************************************/
861 void share_mode_entry_to_message(char *msg
, const struct share_mode_entry
*e
)
863 SIVAL(msg
,OP_BREAK_MSG_PID_OFFSET
,(uint32
)e
->pid
.pid
);
864 SBVAL(msg
,OP_BREAK_MSG_MID_OFFSET
,e
->op_mid
);
865 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,e
->op_type
);
866 SIVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
,e
->access_mask
);
867 SIVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
,e
->share_access
);
868 SIVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
,e
->private_options
);
869 SIVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
,(uint32_t)e
->time
.tv_sec
);
870 SIVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
,(uint32_t)e
->time
.tv_usec
);
871 push_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
872 SIVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
,e
->share_file_id
);
873 SIVAL(msg
,OP_BREAK_MSG_UID_OFFSET
,e
->uid
);
874 SSVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
,e
->flags
);
875 SIVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
,e
->name_hash
);
876 SIVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
,e
->pid
.vnn
);
879 /****************************************************************************
880 De-linearize an internal oplock break message to a share mode entry struct.
881 ****************************************************************************/
883 void message_to_share_mode_entry(struct share_mode_entry
*e
, char *msg
)
885 e
->pid
.pid
= (pid_t
)IVAL(msg
,OP_BREAK_MSG_PID_OFFSET
);
886 e
->op_mid
= BVAL(msg
,OP_BREAK_MSG_MID_OFFSET
);
887 e
->op_type
= SVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
);
888 e
->access_mask
= IVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
);
889 e
->share_access
= IVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
);
890 e
->private_options
= IVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
);
891 e
->time
.tv_sec
= (time_t)IVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
);
892 e
->time
.tv_usec
= (int)IVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
);
893 pull_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
894 e
->share_file_id
= (unsigned long)IVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
);
895 e
->uid
= (uint32
)IVAL(msg
,OP_BREAK_MSG_UID_OFFSET
);
896 e
->flags
= (uint16
)SVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
);
897 e
->name_hash
= IVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
);
898 e
->pid
.vnn
= IVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
);
901 /****************************************************************************
902 Setup oplocks for this process.
903 ****************************************************************************/
905 bool init_oplocks(struct smbd_server_connection
*sconn
)
907 DEBUG(3,("init_oplocks: initializing messages.\n"));
909 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_BREAK_REQUEST
,
910 process_oplock_break_message
);
911 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_ASYNC_LEVEL2_BREAK
,
912 process_oplock_async_level2_break_message
);
913 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_KERNEL_BREAK
,
914 process_kernel_oplock_break
);
916 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_OPEN_RETRY
,
917 process_open_retry_message
);
923 void init_kernel_oplocks(struct smbd_server_connection
*sconn
)
925 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
927 /* only initialize once */
928 if (koplocks
== NULL
) {
929 #if HAVE_KERNEL_OPLOCKS_IRIX
930 koplocks
= irix_init_kernel_oplocks(sconn
);
931 #elif HAVE_KERNEL_OPLOCKS_LINUX
932 koplocks
= linux_init_kernel_oplocks(sconn
);
934 sconn
->oplocks
.kernel_ops
= koplocks
;