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"
25 #include "librpc/gen_ndr/messaging.h"
27 /****************************************************************************
28 Get the number of current exclusive oplocks.
29 ****************************************************************************/
31 int32 get_number_of_exclusive_open_oplocks(void)
33 return exclusive_oplocks_open;
37 * helper function used by the kernel oplock backends to post the break message
39 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
41 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
43 /* Put the kernel break info into the message. */
44 push_file_id_24((char *)msg, &fsp->file_id);
45 SIVAL(msg,24,fsp->fh->gen_id);
47 /* Don't need to be root here as we're only ever
48 sending to ourselves. */
50 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
52 msg, MSG_SMB_KERNEL_BREAK_SIZE);
55 /****************************************************************************
56 Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
57 disabled (just sets flags). Returns True if oplock set.
58 ****************************************************************************/
60 bool set_file_oplock(files_struct *fsp, int oplock_type)
62 if ((fsp->oplock_type == LEVEL_II_OPLOCK)
63 && koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
64 DEBUG(10, ("Refusing level2 oplock, kernel oplocks don't "
68 if ((fsp->oplock_type != NO_OPLOCK) &&
69 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
71 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
75 fsp->oplock_type = oplock_type;
76 fsp->sent_oplock_break = NO_BREAK_SENT;
77 if (oplock_type == LEVEL_II_OPLOCK) {
78 level_II_oplocks_open++;
79 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
80 exclusive_oplocks_open++;
83 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
84 "tv_sec = %x, tv_usec = %x\n",
85 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
86 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
87 (int)fsp->open_time.tv_usec ));
92 /****************************************************************************
93 Attempt to release an oplock on a file. Decrements oplock count.
94 ****************************************************************************/
96 void release_file_oplock(files_struct *fsp)
98 if ((fsp->oplock_type != NO_OPLOCK) &&
99 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
101 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
104 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
105 level_II_oplocks_open--;
106 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
107 exclusive_oplocks_open--;
110 SMB_ASSERT(exclusive_oplocks_open>=0);
111 SMB_ASSERT(level_II_oplocks_open>=0);
113 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
114 /* This doesn't matter for close. */
115 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
117 fsp->oplock_type = NO_OPLOCK;
119 fsp->sent_oplock_break = NO_BREAK_SENT;
121 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
123 TALLOC_FREE(fsp->oplock_timeout);
126 /****************************************************************************
127 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
128 ****************************************************************************/
130 static void downgrade_file_oplock(files_struct *fsp)
132 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
133 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
138 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
140 fsp->oplock_type = LEVEL_II_OPLOCK;
141 exclusive_oplocks_open--;
142 level_II_oplocks_open++;
143 fsp->sent_oplock_break = NO_BREAK_SENT;
146 /****************************************************************************
147 Remove a file oplock. Copes with level II and exclusive.
148 Locks then unlocks the share mode lock. Client can decide to go directly
149 to none even if a "break-to-level II" was sent.
150 ****************************************************************************/
152 bool remove_oplock(files_struct *fsp)
155 struct share_mode_lock *lck;
157 /* Remove the oplock flag from the sharemode. */
158 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
161 DEBUG(0,("remove_oplock: failed to lock share entry for "
162 "file %s\n", fsp_str_dbg(fsp)));
165 ret = remove_share_oplock(lck, fsp);
167 DEBUG(0,("remove_oplock: failed to remove share oplock for "
168 "file %s fnum %d, %s\n",
169 fsp_str_dbg(fsp), fsp->fnum,
170 file_id_string_tos(&fsp->file_id)));
172 release_file_oplock(fsp);
178 * Deal with a reply when a break-to-level II was sent.
180 bool downgrade_oplock(files_struct *fsp)
183 struct share_mode_lock *lck;
185 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
188 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
189 "file %s\n", fsp_str_dbg(fsp)));
192 ret = downgrade_share_oplock(lck, fsp);
194 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
195 "for file %s fnum %d, file_id %s\n",
196 fsp_str_dbg(fsp), fsp->fnum,
197 file_id_string_tos(&fsp->file_id)));
200 downgrade_file_oplock(fsp);
206 * Some kernel oplock implementations handle the notification themselves.
208 bool should_notify_deferred_opens()
211 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
214 /****************************************************************************
215 Set up an oplock break message.
216 ****************************************************************************/
218 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
219 files_struct *fsp, int cmd)
221 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
223 if (result == NULL) {
224 DEBUG(0, ("talloc failed\n"));
228 memset(result,'\0',smb_size);
229 srv_set_message(result,8,0,true);
230 SCVAL(result,smb_com,SMBlockingX);
231 SSVAL(result,smb_tid,fsp->conn->cnum);
232 SSVAL(result,smb_pid,0xFFFF);
233 SSVAL(result,smb_uid,0);
234 SSVAL(result,smb_mid,0xFFFF);
235 SCVAL(result,smb_vwv0,0xFF);
236 SSVAL(result,smb_vwv2,fsp->fnum);
237 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
238 SCVAL(result,smb_vwv3+1,cmd);
242 /****************************************************************************
243 Function to do the waiting before sending a local break.
244 ****************************************************************************/
246 static void wait_before_sending_break(void)
248 long wait_time = (long)lp_oplock_break_wait_time();
251 smb_msleep(wait_time);
255 /****************************************************************************
256 Ensure that we have a valid oplock.
257 ****************************************************************************/
259 static files_struct *initial_break_processing(
260 struct smbd_server_connection *sconn, struct file_id id,
261 unsigned long file_id)
263 files_struct *fsp = NULL;
265 if( DEBUGLVL( 3 ) ) {
266 dbgtext( "initial_break_processing: called for %s/%u\n",
267 file_id_string_tos(&id), (int)file_id);
268 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
269 exclusive_oplocks_open, level_II_oplocks_open );
273 * We need to search the file open table for the
274 * entry containing this dev and inode, and ensure
275 * we have an oplock on it.
278 fsp = file_find_dif(sconn, id, file_id);
281 /* The file could have been closed in the meantime - return success. */
282 if( DEBUGLVL( 3 ) ) {
283 dbgtext( "initial_break_processing: cannot find open file with " );
284 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
285 dbgtext( "allowing break to succeed.\n" );
290 /* Ensure we have an oplock on the file */
293 * There is a potential race condition in that an oplock could
294 * have been broken due to another udp request, and yet there are
295 * still oplock break messages being sent in the udp message
296 * queue for this file. So return true if we don't have an oplock,
297 * as we may have just freed it.
300 if(fsp->oplock_type == NO_OPLOCK) {
301 if( DEBUGLVL( 3 ) ) {
302 dbgtext( "initial_break_processing: file %s ",
304 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
305 file_id_string_tos(&id), fsp->fh->gen_id );
306 dbgtext( "Allowing break to succeed regardless.\n" );
314 static void oplock_timeout_handler(struct event_context *ctx,
315 struct timed_event *te,
319 files_struct *fsp = (files_struct *)private_data;
321 /* Remove the timed event handler. */
322 TALLOC_FREE(fsp->oplock_timeout);
323 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
326 reply_to_oplock_break_requests(fsp);
329 /*******************************************************************
330 Add a timeout handler waiting for the client reply.
331 *******************************************************************/
333 static void add_oplock_timeout_handler(files_struct *fsp)
336 * If kernel oplocks already notifies smbds when an oplock break times
340 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
344 if (fsp->oplock_timeout != NULL) {
345 DEBUG(0, ("Logic problem -- have an oplock event hanging "
349 fsp->oplock_timeout =
350 event_add_timed(smbd_event_context(), fsp,
351 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
352 oplock_timeout_handler, fsp);
354 if (fsp->oplock_timeout == NULL) {
355 DEBUG(0, ("Could not add oplock timeout handler\n"));
359 static void send_break_message_smb1(files_struct *fsp, int level)
361 char *break_msg = new_break_message_smb1(talloc_tos(),
364 if (break_msg == NULL) {
365 exit_server("Could not talloc break_msg\n");
369 if (!srv_send_smb(fsp->conn->sconn,
371 IS_CONN_ENCRYPTED(fsp->conn),
373 exit_server_cleanly("send_break_message_smb1: "
374 "srv_send_smb failed.");
377 TALLOC_FREE(break_msg);
380 void break_level2_to_none_async(files_struct *fsp)
382 struct smbd_server_connection *sconn = fsp->conn->sconn;
384 if (fsp->oplock_type == NO_OPLOCK) {
385 /* We already got a "break to none" message and we've handled
386 * it. just ignore. */
387 DEBUG(3, ("process_oplock_async_level2_break_message: already "
388 "broken to none, ignoring.\n"));
392 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
393 /* Don't tell the client, just downgrade. */
394 DEBUG(3, ("process_oplock_async_level2_break_message: "
395 "downgrading fake level 2 oplock.\n"));
400 /* Ensure we're really at level2 state. */
401 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
403 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
404 "to none message for fid %d, file %s\n", fsp->fnum,
407 /* Now send a break to none message to our client. */
408 if (sconn->using_smb2) {
409 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
411 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
414 /* Async level2 request, don't send a reply, just remove the oplock. */
418 /*******************************************************************
419 This handles the case of a write triggering a break to none
420 message on a level2 oplock.
421 When we get this message we may be in any of three states :
422 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
423 the client for LEVEL2.
424 *******************************************************************/
426 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
429 struct server_id src,
432 struct smbd_server_connection *sconn;
433 struct share_mode_entry msg;
436 if (data->data == NULL) {
437 DEBUG(0, ("Got NULL buffer\n"));
441 sconn = msg_ctx_to_sconn(msg_ctx);
443 DEBUG(1, ("could not find sconn\n"));
447 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
448 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
452 /* De-linearize incoming message. */
453 message_to_share_mode_entry(&msg, (char *)data->data);
455 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
456 "%s/%lu\n", procid_str(talloc_tos(), &src),
457 file_id_string_tos(&msg.id), msg.share_file_id));
459 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
462 /* We hit a race here. Break messages are sent, and before we
463 * get to process this message, we have closed the file.
464 * No need to reply as this is an async message. */
465 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
469 break_level2_to_none_async(fsp);
472 /*******************************************************************
473 This handles the generic oplock break message from another smbd.
474 *******************************************************************/
476 static void process_oplock_break_message(struct messaging_context *msg_ctx,
479 struct server_id src,
482 struct smbd_server_connection *sconn = smbd_server_conn;
483 struct share_mode_entry msg;
485 bool break_to_level2 = False;
487 if (data->data == NULL) {
488 DEBUG(0, ("Got NULL buffer\n"));
492 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
493 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
497 /* De-linearize incoming message. */
498 message_to_share_mode_entry(&msg, (char *)data->data);
500 DEBUG(10, ("Got oplock break message from pid %s: %s/%lu\n",
501 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
504 fsp = initial_break_processing(smbd_server_conn, msg.id,
508 /* We hit a race here. Break messages are sent, and before we
509 * get to process this message, we have closed the file. Reply
510 * with 'ok, oplock broken' */
511 DEBUG(3, ("Did not find fsp\n"));
513 /* We just send the same message back. */
514 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
516 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
520 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
521 /* Remember we have to inform the requesting PID when the
524 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
525 &fsp->pending_break_messages,
526 &fsp->num_pending_break_messages);
530 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
531 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
532 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
533 file_id_string_tos(&fsp->file_id),
535 /* We just send the same message back. */
536 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
538 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
542 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
543 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
544 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
545 lp_level2_oplocks(SNUM(fsp->conn))) {
546 break_to_level2 = True;
549 /* Need to wait before sending a break
550 message if we sent ourselves this message. */
551 if (procid_is_me(&src)) {
552 wait_before_sending_break();
555 if (sconn->using_smb2) {
556 send_break_message_smb2(fsp, break_to_level2 ?
557 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
559 send_break_message_smb1(fsp, break_to_level2 ?
560 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
563 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
566 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
567 &fsp->pending_break_messages,
568 &fsp->num_pending_break_messages);
570 add_oplock_timeout_handler(fsp);
573 /*******************************************************************
574 This handles the kernel oplock break message.
575 *******************************************************************/
577 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
580 struct server_id src,
583 struct smbd_server_connection *sconn = smbd_server_conn;
585 unsigned long file_id;
588 if (data->data == NULL) {
589 DEBUG(0, ("Got NULL buffer\n"));
593 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
594 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
598 /* Pull the data from the message. */
599 pull_file_id_24((char *)data->data, &id);
600 file_id = (unsigned long)IVAL(data->data, 24);
602 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
603 procid_str(talloc_tos(), &src), file_id_string_tos(&id),
604 (unsigned int)file_id));
606 fsp = initial_break_processing(smbd_server_conn, id, file_id);
609 DEBUG(3, ("Got a kernel oplock break message for a file "
610 "I don't know about\n"));
614 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
615 /* This is ok, kernel oplocks come in completely async */
616 DEBUG(3, ("Got a kernel oplock request while waiting for a "
621 if (sconn->using_smb2) {
622 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
624 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
627 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
629 add_oplock_timeout_handler(fsp);
632 void reply_to_oplock_break_requests(files_struct *fsp)
637 * If kernel oplocks already notifies smbds when oplocks are
638 * broken/removed, just return.
641 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
645 for (i=0; i<fsp->num_pending_break_messages; i++) {
646 struct share_mode_entry *e = &fsp->pending_break_messages[i];
647 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
649 share_mode_entry_to_message(msg, e);
651 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
652 MSG_SMB_BREAK_RESPONSE,
654 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
657 SAFE_FREE(fsp->pending_break_messages);
658 fsp->num_pending_break_messages = 0;
659 if (fsp->oplock_timeout != NULL) {
660 /* Remove the timed event handler. */
661 TALLOC_FREE(fsp->oplock_timeout);
662 fsp->oplock_timeout = NULL;
667 static void process_oplock_break_response(struct messaging_context *msg_ctx,
670 struct server_id src,
673 struct share_mode_entry msg;
675 if (data->data == NULL) {
676 DEBUG(0, ("Got NULL buffer\n"));
680 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
681 DEBUG(0, ("Got invalid msg len %u\n",
682 (unsigned int)data->length));
686 /* De-linearize incoming message. */
687 message_to_share_mode_entry(&msg, (char *)data->data);
689 DEBUG(10, ("Got oplock break response from pid %s: %s/%lu mid %llu\n",
690 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
691 msg.share_file_id, (unsigned long long)msg.op_mid));
693 schedule_deferred_open_message_smb(msg.op_mid);
696 static void process_open_retry_message(struct messaging_context *msg_ctx,
699 struct server_id src,
702 struct share_mode_entry msg;
704 if (data->data == NULL) {
705 DEBUG(0, ("Got NULL buffer\n"));
709 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
710 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
714 /* De-linearize incoming message. */
715 message_to_share_mode_entry(&msg, (char *)data->data);
717 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
718 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
719 (unsigned long long)msg.op_mid));
721 schedule_deferred_open_message_smb(msg.op_mid);
724 /****************************************************************************
725 This function is called on any file modification or lock request. If a file
726 is level 2 oplocked then it must tell all other level 2 holders to break to
728 ****************************************************************************/
730 static void contend_level2_oplocks_begin_default(files_struct *fsp,
731 enum level2_contention_type type)
734 struct share_mode_lock *lck;
737 * If this file is level II oplocked then we need
738 * to grab the shared memory lock and inform all
739 * other files with a level II lock that they need
740 * to flush their read caches. We keep the lock over
741 * the shared memory area whilst doing this.
744 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
747 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
750 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
751 "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
755 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
756 lck->num_share_modes ));
758 for(i = 0; i < lck->num_share_modes; i++) {
759 struct share_mode_entry *share_entry = &lck->share_modes[i];
760 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
762 if (!is_valid_share_mode_entry(share_entry)) {
767 * As there could have been multiple writes waiting at the
768 * lock_share_entry gate we may not be the first to
769 * enter. Hence the state of the op_types in the share mode
770 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
771 * oplock. It will do no harm to re-send break messages to
772 * those smbd's that are still waiting their turn to remove
773 * their LEVEL_II state, and also no harm to ignore existing
774 * NO_OPLOCK states. JRA.
777 DEBUG(10,("release_level_2_oplocks_on_change: "
778 "share_entry[%i]->op_type == %d\n",
779 i, share_entry->op_type ));
781 if (share_entry->op_type == NO_OPLOCK) {
786 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
787 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
788 "share mode entry %d is an exlusive "
794 share_mode_entry_to_message(msg, share_entry);
797 * Deal with a race condition when breaking level2
798 * oplocks. Don't send all the messages and release
799 * the lock, this allows someone else to come in and
800 * get a level2 lock before any of the messages are
801 * processed, and thus miss getting a break message.
802 * Ensure at least one entry (the one we're breaking)
803 * is processed immediately under the lock and becomes
804 * set as NO_OPLOCK to stop any waiter getting a level2.
808 if (procid_is_me(&share_entry->pid)) {
809 wait_before_sending_break();
810 break_level2_to_none_async(fsp);
812 messaging_send_buf(fsp->conn->sconn->msg_ctx,
814 MSG_SMB_ASYNC_LEVEL2_BREAK,
816 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
820 /* We let the message receivers handle removing the oplock state
821 in the share mode lock db. */
826 void contend_level2_oplocks_begin(files_struct *fsp,
827 enum level2_contention_type type)
829 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
830 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
834 contend_level2_oplocks_begin_default(fsp, type);
837 void contend_level2_oplocks_end(files_struct *fsp,
838 enum level2_contention_type type)
840 /* Only kernel oplocks implement this so far */
841 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
842 koplocks->ops->contend_level2_oplocks_end(fsp, type);
846 /****************************************************************************
847 Linearize a share mode entry struct to an internal oplock break message.
848 ****************************************************************************/
850 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
852 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
853 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
854 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
855 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
856 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
857 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
858 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
859 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
860 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
861 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
862 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
863 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
864 #ifdef CLUSTER_SUPPORT
865 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
869 /****************************************************************************
870 De-linearize an internal oplock break message to a share mode entry struct.
871 ****************************************************************************/
873 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
875 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
876 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
877 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
878 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
879 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
880 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
881 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
882 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
883 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
884 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
885 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
886 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
887 #ifdef CLUSTER_SUPPORT
888 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
892 /****************************************************************************
893 Setup oplocks for this process.
894 ****************************************************************************/
896 bool init_oplocks(struct messaging_context *msg_ctx)
898 DEBUG(3,("init_oplocks: initializing messages.\n"));
900 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
901 process_oplock_break_message);
902 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
903 process_oplock_async_level2_break_message);
904 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
905 process_oplock_break_response);
906 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
907 process_kernel_oplock_break);
908 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
909 process_open_retry_message);
911 if (lp_kernel_oplocks()) {
912 #if HAVE_KERNEL_OPLOCKS_IRIX
913 koplocks = irix_init_kernel_oplocks(talloc_autofree_context());
914 #elif HAVE_KERNEL_OPLOCKS_LINUX
915 koplocks = linux_init_kernel_oplocks(talloc_autofree_context());
917 koplocks = onefs_init_kernel_oplocks(talloc_autofree_context());