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/globals.h"
26 /****************************************************************************
27 Get the number of current exclusive oplocks.
28 ****************************************************************************/
30 int32
get_number_of_exclusive_open_oplocks(void)
32 return exclusive_oplocks_open
;
36 * helper function used by the kernel oplock backends to post the break message
38 void break_kernel_oplock(struct messaging_context
*msg_ctx
, files_struct
*fsp
)
40 uint8_t msg
[MSG_SMB_KERNEL_BREAK_SIZE
];
42 /* Put the kernel break info into the message. */
43 push_file_id_16((char *)msg
, &fsp
->file_id
);
44 SIVAL(msg
,16,fsp
->fh
->gen_id
);
46 /* Don't need to be root here as we're only ever
47 sending to ourselves. */
49 messaging_send_buf(msg_ctx
, procid_self(),
51 msg
, MSG_SMB_KERNEL_BREAK_SIZE
);
54 /****************************************************************************
55 Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
56 disabled (just sets flags). Returns True if oplock set.
57 ****************************************************************************/
59 bool set_file_oplock(files_struct
*fsp
, int oplock_type
)
61 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
62 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
) &&
64 !koplocks
->ops
->set_oplock(koplocks
, fsp
, oplock_type
)) {
68 fsp
->oplock_type
= oplock_type
;
69 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
70 if (oplock_type
== LEVEL_II_OPLOCK
) {
71 level_II_oplocks_open
++;
72 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
73 exclusive_oplocks_open
++;
76 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
77 "tv_sec = %x, tv_usec = %x\n",
78 fsp
->fsp_name
, file_id_string_tos(&fsp
->file_id
),
79 fsp
->fh
->gen_id
, (int)fsp
->open_time
.tv_sec
,
80 (int)fsp
->open_time
.tv_usec
));
85 /****************************************************************************
86 Attempt to release an oplock on a file. Decrements oplock count.
87 ****************************************************************************/
89 void release_file_oplock(files_struct
*fsp
)
91 if ((fsp
->oplock_type
!= NO_OPLOCK
) &&
92 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
) &&
94 koplocks
->ops
->release_oplock(koplocks
, fsp
, NO_OPLOCK
);
97 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
98 level_II_oplocks_open
--;
99 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
100 exclusive_oplocks_open
--;
103 SMB_ASSERT(exclusive_oplocks_open
>=0);
104 SMB_ASSERT(level_II_oplocks_open
>=0);
106 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
107 /* This doesn't matter for close. */
108 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
110 fsp
->oplock_type
= NO_OPLOCK
;
112 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
114 flush_write_cache(fsp
, OPLOCK_RELEASE_FLUSH
);
116 TALLOC_FREE(fsp
->oplock_timeout
);
119 /****************************************************************************
120 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
121 ****************************************************************************/
123 static void downgrade_file_oplock(files_struct
*fsp
)
126 koplocks
->ops
->release_oplock(koplocks
, fsp
, LEVEL_II_OPLOCK
);
128 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
129 exclusive_oplocks_open
--;
130 level_II_oplocks_open
++;
131 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
134 /****************************************************************************
135 Remove a file oplock. Copes with level II and exclusive.
136 Locks then unlocks the share mode lock. Client can decide to go directly
137 to none even if a "break-to-level II" was sent.
138 ****************************************************************************/
140 bool remove_oplock(files_struct
*fsp
)
143 struct share_mode_lock
*lck
;
145 /* Remove the oplock flag from the sharemode. */
146 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
149 DEBUG(0,("remove_oplock: failed to lock share entry for "
150 "file %s\n", fsp
->fsp_name
));
153 ret
= remove_share_oplock(lck
, fsp
);
155 DEBUG(0,("remove_oplock: failed to remove share oplock for "
156 "file %s fnum %d, %s\n",
157 fsp
->fsp_name
, fsp
->fnum
, file_id_string_tos(&fsp
->file_id
)));
159 release_file_oplock(fsp
);
165 * Deal with a reply when a break-to-level II was sent.
167 bool downgrade_oplock(files_struct
*fsp
)
170 struct share_mode_lock
*lck
;
172 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
175 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
176 "file %s\n", fsp
->fsp_name
));
179 ret
= downgrade_share_oplock(lck
, fsp
);
181 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
182 "for file %s fnum %d, file_id %s\n",
183 fsp
->fsp_name
, fsp
->fnum
, file_id_string_tos(&fsp
->file_id
)));
186 downgrade_file_oplock(fsp
);
191 /****************************************************************************
192 Set up an oplock break message.
193 ****************************************************************************/
195 static char *new_break_smb_message(TALLOC_CTX
*mem_ctx
,
196 files_struct
*fsp
, uint8 cmd
)
198 char *result
= TALLOC_ARRAY(mem_ctx
, char, smb_size
+ 8*2 + 0);
200 if (result
== NULL
) {
201 DEBUG(0, ("talloc failed\n"));
205 memset(result
,'\0',smb_size
);
206 srv_set_message(result
,8,0,true);
207 SCVAL(result
,smb_com
,SMBlockingX
);
208 SSVAL(result
,smb_tid
,fsp
->conn
->cnum
);
209 SSVAL(result
,smb_pid
,0xFFFF);
210 SSVAL(result
,smb_uid
,0);
211 SSVAL(result
,smb_mid
,0xFFFF);
212 SCVAL(result
,smb_vwv0
,0xFF);
213 SSVAL(result
,smb_vwv2
,fsp
->fnum
);
214 SCVAL(result
,smb_vwv3
,LOCKING_ANDX_OPLOCK_RELEASE
);
215 SCVAL(result
,smb_vwv3
+1,cmd
);
219 /****************************************************************************
220 Function to do the waiting before sending a local break.
221 ****************************************************************************/
223 static void wait_before_sending_break(void)
225 long wait_time
= (long)lp_oplock_break_wait_time();
228 smb_msleep(wait_time
);
232 /****************************************************************************
233 Ensure that we have a valid oplock.
234 ****************************************************************************/
236 static files_struct
*initial_break_processing(struct file_id id
, unsigned long file_id
)
238 files_struct
*fsp
= NULL
;
240 if( DEBUGLVL( 3 ) ) {
241 dbgtext( "initial_break_processing: called for %s/%u\n",
242 file_id_string_tos(&id
), (int)file_id
);
243 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
244 exclusive_oplocks_open
, level_II_oplocks_open
);
248 * We need to search the file open table for the
249 * entry containing this dev and inode, and ensure
250 * we have an oplock on it.
253 fsp
= file_find_dif(id
, file_id
);
256 /* The file could have been closed in the meantime - return success. */
257 if( DEBUGLVL( 3 ) ) {
258 dbgtext( "initial_break_processing: cannot find open file with " );
259 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id
), file_id
);
260 dbgtext( "allowing break to succeed.\n" );
265 /* Ensure we have an oplock on the file */
268 * There is a potential race condition in that an oplock could
269 * have been broken due to another udp request, and yet there are
270 * still oplock break messages being sent in the udp message
271 * queue for this file. So return true if we don't have an oplock,
272 * as we may have just freed it.
275 if(fsp
->oplock_type
== NO_OPLOCK
) {
276 if( DEBUGLVL( 3 ) ) {
277 dbgtext( "initial_break_processing: file %s ", fsp
->fsp_name
);
278 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
279 file_id_string_tos(&id
), fsp
->fh
->gen_id
);
280 dbgtext( "Allowing break to succeed regardless.\n" );
288 static void oplock_timeout_handler(struct event_context
*ctx
,
289 struct timed_event
*te
,
293 files_struct
*fsp
= (files_struct
*)private_data
;
295 /* Remove the timed event handler. */
296 TALLOC_FREE(fsp
->oplock_timeout
);
297 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp
->fsp_name
));
298 global_client_failed_oplock_break
= True
;
300 reply_to_oplock_break_requests(fsp
);
303 /*******************************************************************
304 Add a timeout handler waiting for the client reply.
305 *******************************************************************/
307 static void add_oplock_timeout_handler(files_struct
*fsp
)
309 if (fsp
->oplock_timeout
!= NULL
) {
310 DEBUG(0, ("Logic problem -- have an oplock event hanging "
314 fsp
->oplock_timeout
=
315 event_add_timed(smbd_event_context(), NULL
,
316 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0),
317 oplock_timeout_handler
, fsp
);
319 if (fsp
->oplock_timeout
== NULL
) {
320 DEBUG(0, ("Could not add oplock timeout handler\n"));
324 /*******************************************************************
325 This handles the case of a write triggering a break to none
326 message on a level2 oplock.
327 When we get this message we may be in any of three states :
328 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
329 the client for LEVEL2.
330 *******************************************************************/
332 static void process_oplock_async_level2_break_message(struct messaging_context
*msg_ctx
,
335 struct server_id src
,
338 struct share_mode_entry msg
;
343 if (data
->data
== NULL
) {
344 DEBUG(0, ("Got NULL buffer\n"));
348 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
349 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
353 /* De-linearize incoming message. */
354 message_to_share_mode_entry(&msg
, (char *)data
->data
);
356 DEBUG(10, ("Got oplock async level 2 break message from pid %d: %s/%lu\n",
357 (int)procid_to_pid(&src
), file_id_string_tos(&msg
.id
), msg
.share_file_id
));
359 fsp
= initial_break_processing(msg
.id
, msg
.share_file_id
);
362 /* We hit a race here. Break messages are sent, and before we
363 * get to process this message, we have closed the file.
364 * No need to reply as this is an async message. */
365 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
369 if (fsp
->oplock_type
== NO_OPLOCK
) {
370 /* We already got a "break to none" message and we've handled it.
372 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
376 if (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
377 /* Don't tell the client, just downgrade. */
378 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
383 /* Ensure we're really at level2 state. */
384 SMB_ASSERT(fsp
->oplock_type
== LEVEL_II_OPLOCK
);
386 DEBUG(10,("process_oplock_async_level2_break_message: sending break to "
387 "none message for fid %d, file %s\n",
391 /* Now send a break to none message to our client. */
393 break_msg
= new_break_smb_message(NULL
, fsp
, OPLOCKLEVEL_NONE
);
394 if (break_msg
== NULL
) {
395 exit_server("Could not talloc break_msg\n");
398 /* Need to wait before sending a break message if we sent ourselves this message. */
399 if (procid_to_pid(&src
) == sys_getpid()) {
400 wait_before_sending_break();
403 /* Save the server smb signing state. */
404 sign_state
= srv_oplock_set_signing(False
);
407 if (!srv_send_smb(smbd_server_fd(),
409 IS_CONN_ENCRYPTED(fsp
->conn
),
411 exit_server_cleanly("oplock_break: srv_send_smb failed.");
414 /* Restore the sign state to what it was. */
415 srv_oplock_set_signing(sign_state
);
417 TALLOC_FREE(break_msg
);
419 /* Async level2 request, don't send a reply, just remove the oplock. */
423 /*******************************************************************
424 This handles the generic oplock break message from another smbd.
425 *******************************************************************/
427 static void process_oplock_break_message(struct messaging_context
*msg_ctx
,
430 struct server_id src
,
433 struct share_mode_entry msg
;
436 bool break_to_level2
= False
;
439 if (data
->data
== NULL
) {
440 DEBUG(0, ("Got NULL buffer\n"));
444 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
445 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
449 /* De-linearize incoming message. */
450 message_to_share_mode_entry(&msg
, (char *)data
->data
);
452 DEBUG(10, ("Got oplock break message from pid %d: %s/%lu\n",
453 (int)procid_to_pid(&src
), file_id_string_tos(&msg
.id
), msg
.share_file_id
));
455 fsp
= initial_break_processing(msg
.id
, msg
.share_file_id
);
458 /* a We hit race here. Break messages are sent, and before we
459 * get to process this message, we have closed the file. Reply
460 * with 'ok, oplock broken' */
461 DEBUG(3, ("Did not find fsp\n"));
463 /* We just send the same message back. */
464 messaging_send_buf(msg_ctx
, src
, MSG_SMB_BREAK_RESPONSE
,
466 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
470 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
471 /* Remember we have to inform the requesting PID when the
474 ADD_TO_ARRAY(NULL
, struct share_mode_entry
, msg
,
475 &fsp
->pending_break_messages
,
476 &fsp
->num_pending_break_messages
);
480 if (EXCLUSIVE_OPLOCK_TYPE(msg
.op_type
) &&
481 !EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
482 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
483 file_id_string_tos(&fsp
->file_id
),
485 /* We just send the same message back. */
486 messaging_send_buf(msg_ctx
, src
, MSG_SMB_BREAK_RESPONSE
,
488 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
492 if ((global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
493 !(msg
.op_type
& FORCE_OPLOCK_BREAK_TO_NONE
) &&
494 !koplocks
&& /* NOTE: we force levelII off for kernel oplocks -
495 * this will change when it is supported */
496 lp_level2_oplocks(SNUM(fsp
->conn
))) {
497 break_to_level2
= True
;
500 break_msg
= new_break_smb_message(NULL
, fsp
, break_to_level2
?
501 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
502 if (break_msg
== NULL
) {
503 exit_server("Could not talloc break_msg\n");
506 /* Need to wait before sending a break message if we sent ourselves this message. */
507 if (procid_to_pid(&src
) == sys_getpid()) {
508 wait_before_sending_break();
511 /* Save the server smb signing state. */
512 sign_state
= srv_oplock_set_signing(False
);
515 if (!srv_send_smb(smbd_server_fd(),
517 IS_CONN_ENCRYPTED(fsp
->conn
),
519 exit_server_cleanly("oplock_break: srv_send_smb failed.");
522 /* Restore the sign state to what it was. */
523 srv_oplock_set_signing(sign_state
);
525 TALLOC_FREE(break_msg
);
527 fsp
->sent_oplock_break
= break_to_level2
? LEVEL_II_BREAK_SENT
:BREAK_TO_NONE_SENT
;
530 ADD_TO_ARRAY(NULL
, struct share_mode_entry
, msg
,
531 &fsp
->pending_break_messages
,
532 &fsp
->num_pending_break_messages
);
534 add_oplock_timeout_handler(fsp
);
537 /*******************************************************************
538 This handles the kernel oplock break message.
539 *******************************************************************/
541 static void process_kernel_oplock_break(struct messaging_context
*msg_ctx
,
544 struct server_id src
,
548 unsigned long file_id
;
553 if (data
->data
== NULL
) {
554 DEBUG(0, ("Got NULL buffer\n"));
558 if (data
->length
!= MSG_SMB_KERNEL_BREAK_SIZE
) {
559 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
563 /* Pull the data from the message. */
564 pull_file_id_16((char *)data
->data
, &id
);
565 file_id
= (unsigned long)IVAL(data
->data
, 16);
567 DEBUG(10, ("Got kernel oplock break message from pid %d: %s/%u\n",
568 (int)procid_to_pid(&src
), file_id_string_tos(&id
),
569 (unsigned int)file_id
));
571 fsp
= initial_break_processing(id
, file_id
);
574 DEBUG(3, ("Got a kernel oplock break message for a file "
575 "I don't know about\n"));
579 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
580 /* This is ok, kernel oplocks come in completely async */
581 DEBUG(3, ("Got a kernel oplock request while waiting for a "
586 break_msg
= new_break_smb_message(NULL
, fsp
, OPLOCKLEVEL_NONE
);
587 if (break_msg
== NULL
) {
588 exit_server("Could not talloc break_msg\n");
591 /* Save the server smb signing state. */
592 sign_state
= srv_oplock_set_signing(False
);
595 if (!srv_send_smb(smbd_server_fd(),
597 IS_CONN_ENCRYPTED(fsp
->conn
),
599 exit_server_cleanly("oplock_break: srv_send_smb failed.");
602 /* Restore the sign state to what it was. */
603 srv_oplock_set_signing(sign_state
);
605 TALLOC_FREE(break_msg
);
607 fsp
->sent_oplock_break
= BREAK_TO_NONE_SENT
;
609 add_oplock_timeout_handler(fsp
);
612 void reply_to_oplock_break_requests(files_struct
*fsp
)
616 for (i
=0; i
<fsp
->num_pending_break_messages
; i
++) {
617 struct share_mode_entry
*e
= &fsp
->pending_break_messages
[i
];
618 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
620 share_mode_entry_to_message(msg
, e
);
622 messaging_send_buf(smbd_messaging_context(), e
->pid
,
623 MSG_SMB_BREAK_RESPONSE
,
625 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
628 SAFE_FREE(fsp
->pending_break_messages
);
629 fsp
->num_pending_break_messages
= 0;
630 if (fsp
->oplock_timeout
!= NULL
) {
631 /* Remove the timed event handler. */
632 TALLOC_FREE(fsp
->oplock_timeout
);
633 fsp
->oplock_timeout
= NULL
;
638 static void process_oplock_break_response(struct messaging_context
*msg_ctx
,
641 struct server_id src
,
644 struct share_mode_entry msg
;
646 if (data
->data
== NULL
) {
647 DEBUG(0, ("Got NULL buffer\n"));
651 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
652 DEBUG(0, ("Got invalid msg len %u\n",
653 (unsigned int)data
->length
));
657 /* De-linearize incoming message. */
658 message_to_share_mode_entry(&msg
, (char *)data
->data
);
660 DEBUG(10, ("Got oplock break response from pid %d: %s/%lu mid %u\n",
661 (int)procid_to_pid(&src
), file_id_string_tos(&msg
.id
), msg
.share_file_id
,
662 (unsigned int)msg
.op_mid
));
664 /* Here's the hack from open.c, store the mid in the 'port' field */
665 schedule_deferred_open_smb_message(msg
.op_mid
);
668 static void process_open_retry_message(struct messaging_context
*msg_ctx
,
671 struct server_id src
,
674 struct share_mode_entry msg
;
676 if (data
->data
== NULL
) {
677 DEBUG(0, ("Got NULL buffer\n"));
681 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
682 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
686 /* De-linearize incoming message. */
687 message_to_share_mode_entry(&msg
, (char *)data
->data
);
689 DEBUG(10, ("Got open retry msg from pid %d: %s mid %u\n",
690 (int)procid_to_pid(&src
), file_id_string_tos(&msg
.id
),
691 (unsigned int)msg
.op_mid
));
693 schedule_deferred_open_smb_message(msg
.op_mid
);
696 /****************************************************************************
697 This function is called on any file modification or lock request. If a file
698 is level 2 oplocked then it must tell all other level 2 holders to break to
700 ****************************************************************************/
702 void release_level_2_oplocks_on_change(files_struct
*fsp
)
705 struct share_mode_lock
*lck
;
708 * If this file is level II oplocked then we need
709 * to grab the shared memory lock and inform all
710 * other files with a level II lock that they need
711 * to flush their read caches. We keep the lock over
712 * the shared memory area whilst doing this.
715 if (!LEVEL_II_OPLOCK_TYPE(fsp
->oplock_type
))
718 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
721 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
722 "share mode entry for file %s.\n", fsp
->fsp_name
));
726 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
727 lck
->num_share_modes
));
729 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
730 struct share_mode_entry
*share_entry
= &lck
->share_modes
[i
];
731 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
733 if (!is_valid_share_mode_entry(share_entry
)) {
738 * As there could have been multiple writes waiting at the
739 * lock_share_entry gate we may not be the first to
740 * enter. Hence the state of the op_types in the share mode
741 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
742 * oplock. It will do no harm to re-send break messages to
743 * those smbd's that are still waiting their turn to remove
744 * their LEVEL_II state, and also no harm to ignore existing
745 * NO_OPLOCK states. JRA.
748 DEBUG(10,("release_level_2_oplocks_on_change: "
749 "share_entry[%i]->op_type == %d\n",
750 i
, share_entry
->op_type
));
752 if (share_entry
->op_type
== NO_OPLOCK
) {
757 if (EXCLUSIVE_OPLOCK_TYPE(share_entry
->op_type
)) {
758 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
759 "share mode entry %d is an exlusive "
765 share_mode_entry_to_message(msg
, share_entry
);
768 * Deal with a race condition when breaking level2
769 * oplocks. Don't send all the messages and release
770 * the lock, this allows someone else to come in and
771 * get a level2 lock before any of the messages are
772 * processed, and thus miss getting a break message.
773 * Ensure at least one entry (the one we're breaking)
774 * is processed immediately under the lock and becomes
775 * set as NO_OPLOCK to stop any waiter getting a level2.
779 if (procid_is_me(&share_entry
->pid
)) {
780 DATA_BLOB blob
= data_blob_const(msg
,
781 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
782 process_oplock_async_level2_break_message(smbd_messaging_context(),
784 MSG_SMB_ASYNC_LEVEL2_BREAK
,
788 messaging_send_buf(smbd_messaging_context(),
790 MSG_SMB_ASYNC_LEVEL2_BREAK
,
792 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
796 /* We let the message receivers handle removing the oplock state
797 in the share mode lock db. */
802 /****************************************************************************
803 Linearize a share mode entry struct to an internal oplock break message.
804 ****************************************************************************/
806 void share_mode_entry_to_message(char *msg
, const struct share_mode_entry
*e
)
808 SIVAL(msg
,0,(uint32
)e
->pid
.pid
);
809 SSVAL(msg
,4,e
->op_mid
);
810 SSVAL(msg
,6,e
->op_type
);
811 SIVAL(msg
,8,e
->access_mask
);
812 SIVAL(msg
,12,e
->share_access
);
813 SIVAL(msg
,16,e
->private_options
);
814 SIVAL(msg
,20,(uint32
)e
->time
.tv_sec
);
815 SIVAL(msg
,24,(uint32
)e
->time
.tv_usec
);
816 push_file_id_16(msg
+28, &e
->id
);
817 SIVAL(msg
,44,e
->share_file_id
);
818 SIVAL(msg
,48,e
->uid
);
819 SSVAL(msg
,52,e
->flags
);
820 #ifdef CLUSTER_SUPPORT
821 SIVAL(msg
,54,e
->pid
.vnn
);
825 /****************************************************************************
826 De-linearize an internal oplock break message to a share mode entry struct.
827 ****************************************************************************/
829 void message_to_share_mode_entry(struct share_mode_entry
*e
, char *msg
)
831 e
->pid
.pid
= (pid_t
)IVAL(msg
,0);
832 e
->op_mid
= SVAL(msg
,4);
833 e
->op_type
= SVAL(msg
,6);
834 e
->access_mask
= IVAL(msg
,8);
835 e
->share_access
= IVAL(msg
,12);
836 e
->private_options
= IVAL(msg
,16);
837 e
->time
.tv_sec
= (time_t)IVAL(msg
,20);
838 e
->time
.tv_usec
= (int)IVAL(msg
,24);
839 pull_file_id_16(msg
+28, &e
->id
);
840 e
->share_file_id
= (unsigned long)IVAL(msg
,44);
841 e
->uid
= (uint32
)IVAL(msg
,48);
842 e
->flags
= (uint16
)SVAL(msg
,52);
843 #ifdef CLUSTER_SUPPORT
844 e
->pid
.vnn
= IVAL(msg
,54);
848 /****************************************************************************
849 Setup oplocks for this process.
850 ****************************************************************************/
852 bool init_oplocks(struct messaging_context
*msg_ctx
)
854 DEBUG(3,("init_oplocks: initializing messages.\n"));
856 messaging_register(msg_ctx
, NULL
, MSG_SMB_BREAK_REQUEST
,
857 process_oplock_break_message
);
858 messaging_register(msg_ctx
, NULL
, MSG_SMB_ASYNC_LEVEL2_BREAK
,
859 process_oplock_async_level2_break_message
);
860 messaging_register(msg_ctx
, NULL
, MSG_SMB_BREAK_RESPONSE
,
861 process_oplock_break_response
);
862 messaging_register(msg_ctx
, NULL
, MSG_SMB_KERNEL_BREAK
,
863 process_kernel_oplock_break
);
864 messaging_register(msg_ctx
, NULL
, MSG_SMB_OPEN_RETRY
,
865 process_open_retry_message
);
867 if (lp_kernel_oplocks()) {
868 #if HAVE_KERNEL_OPLOCKS_IRIX
869 koplocks
= irix_init_kernel_oplocks(talloc_autofree_context());
870 #elif HAVE_KERNEL_OPLOCKS_LINUX
871 koplocks
= linux_init_kernel_oplocks(talloc_autofree_context());