Move to using 64-bit mid values in our internal open file database.
[Samba/ekacnet.git] / source3 / smbd / oplock.c
blob2289787ddd54ebca9ceece3bebf1b3739743f555
1 /*
2 Unix SMB/CIFS implementation.
3 oplock processing
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
23 #include "includes.h"
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_24((char *)msg, &fsp->file_id);
44 SIVAL(msg,24,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(),
50 MSG_SMB_KERNEL_BREAK,
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 == LEVEL_II_OPLOCK)
62 && koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks don't "
64 "support them\n"));
65 return false;
67 if ((fsp->oplock_type != NO_OPLOCK) &&
68 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
69 koplocks &&
70 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
71 return False;
74 fsp->oplock_type = oplock_type;
75 fsp->sent_oplock_break = NO_BREAK_SENT;
76 if (oplock_type == LEVEL_II_OPLOCK) {
77 level_II_oplocks_open++;
78 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
79 exclusive_oplocks_open++;
82 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
83 "tv_sec = %x, tv_usec = %x\n",
84 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
85 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
86 (int)fsp->open_time.tv_usec ));
88 return True;
91 /****************************************************************************
92 Attempt to release an oplock on a file. Decrements oplock count.
93 ****************************************************************************/
95 void release_file_oplock(files_struct *fsp)
97 if ((fsp->oplock_type != NO_OPLOCK) &&
98 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
99 koplocks) {
100 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
103 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
104 level_II_oplocks_open--;
105 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
106 exclusive_oplocks_open--;
109 SMB_ASSERT(exclusive_oplocks_open>=0);
110 SMB_ASSERT(level_II_oplocks_open>=0);
112 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
113 /* This doesn't matter for close. */
114 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
115 } else {
116 fsp->oplock_type = NO_OPLOCK;
118 fsp->sent_oplock_break = NO_BREAK_SENT;
120 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
122 TALLOC_FREE(fsp->oplock_timeout);
125 /****************************************************************************
126 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
127 ****************************************************************************/
129 static void downgrade_file_oplock(files_struct *fsp)
131 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
132 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
133 return;
136 if (koplocks) {
137 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
139 fsp->oplock_type = LEVEL_II_OPLOCK;
140 exclusive_oplocks_open--;
141 level_II_oplocks_open++;
142 fsp->sent_oplock_break = NO_BREAK_SENT;
145 /****************************************************************************
146 Remove a file oplock. Copes with level II and exclusive.
147 Locks then unlocks the share mode lock. Client can decide to go directly
148 to none even if a "break-to-level II" was sent.
149 ****************************************************************************/
151 bool remove_oplock(files_struct *fsp)
153 bool ret;
154 struct share_mode_lock *lck;
156 /* Remove the oplock flag from the sharemode. */
157 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
158 NULL);
159 if (lck == NULL) {
160 DEBUG(0,("remove_oplock: failed to lock share entry for "
161 "file %s\n", fsp_str_dbg(fsp)));
162 return False;
164 ret = remove_share_oplock(lck, fsp);
165 if (!ret) {
166 DEBUG(0,("remove_oplock: failed to remove share oplock for "
167 "file %s fnum %d, %s\n",
168 fsp_str_dbg(fsp), fsp->fnum,
169 file_id_string_tos(&fsp->file_id)));
171 release_file_oplock(fsp);
172 TALLOC_FREE(lck);
173 return ret;
177 * Deal with a reply when a break-to-level II was sent.
179 bool downgrade_oplock(files_struct *fsp)
181 bool ret;
182 struct share_mode_lock *lck;
184 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
185 NULL);
186 if (lck == NULL) {
187 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
188 "file %s\n", fsp_str_dbg(fsp)));
189 return False;
191 ret = downgrade_share_oplock(lck, fsp);
192 if (!ret) {
193 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
194 "for file %s fnum %d, file_id %s\n",
195 fsp_str_dbg(fsp), fsp->fnum,
196 file_id_string_tos(&fsp->file_id)));
199 downgrade_file_oplock(fsp);
200 TALLOC_FREE(lck);
201 return ret;
205 * Some kernel oplock implementations handle the notification themselves.
207 bool should_notify_deferred_opens()
209 return !(koplocks &&
210 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
213 /****************************************************************************
214 Set up an oplock break message.
215 ****************************************************************************/
217 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
218 files_struct *fsp, uint8 cmd)
220 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
222 if (result == NULL) {
223 DEBUG(0, ("talloc failed\n"));
224 return NULL;
227 memset(result,'\0',smb_size);
228 srv_set_message(result,8,0,true);
229 SCVAL(result,smb_com,SMBlockingX);
230 SSVAL(result,smb_tid,fsp->conn->cnum);
231 SSVAL(result,smb_pid,0xFFFF);
232 SSVAL(result,smb_uid,0);
233 SSVAL(result,smb_mid,0xFFFF);
234 SCVAL(result,smb_vwv0,0xFF);
235 SSVAL(result,smb_vwv2,fsp->fnum);
236 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
237 SCVAL(result,smb_vwv3+1,cmd);
238 return result;
241 /****************************************************************************
242 Function to do the waiting before sending a local break.
243 ****************************************************************************/
245 static void wait_before_sending_break(void)
247 long wait_time = (long)lp_oplock_break_wait_time();
249 if (wait_time) {
250 smb_msleep(wait_time);
254 /****************************************************************************
255 Ensure that we have a valid oplock.
256 ****************************************************************************/
258 static files_struct *initial_break_processing(struct file_id id, unsigned long file_id)
260 files_struct *fsp = NULL;
262 if( DEBUGLVL( 3 ) ) {
263 dbgtext( "initial_break_processing: called for %s/%u\n",
264 file_id_string_tos(&id), (int)file_id);
265 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
266 exclusive_oplocks_open, level_II_oplocks_open );
270 * We need to search the file open table for the
271 * entry containing this dev and inode, and ensure
272 * we have an oplock on it.
275 fsp = file_find_dif(id, file_id);
277 if(fsp == NULL) {
278 /* The file could have been closed in the meantime - return success. */
279 if( DEBUGLVL( 3 ) ) {
280 dbgtext( "initial_break_processing: cannot find open file with " );
281 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
282 dbgtext( "allowing break to succeed.\n" );
284 return NULL;
287 /* Ensure we have an oplock on the file */
290 * There is a potential race condition in that an oplock could
291 * have been broken due to another udp request, and yet there are
292 * still oplock break messages being sent in the udp message
293 * queue for this file. So return true if we don't have an oplock,
294 * as we may have just freed it.
297 if(fsp->oplock_type == NO_OPLOCK) {
298 if( DEBUGLVL( 3 ) ) {
299 dbgtext( "initial_break_processing: file %s ",
300 fsp_str_dbg(fsp));
301 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
302 file_id_string_tos(&id), fsp->fh->gen_id );
303 dbgtext( "Allowing break to succeed regardless.\n" );
305 return NULL;
308 return fsp;
311 static void oplock_timeout_handler(struct event_context *ctx,
312 struct timed_event *te,
313 struct timeval now,
314 void *private_data)
316 files_struct *fsp = (files_struct *)private_data;
318 /* Remove the timed event handler. */
319 TALLOC_FREE(fsp->oplock_timeout);
320 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
321 fsp_str_dbg(fsp)));
322 global_client_failed_oplock_break = True;
323 remove_oplock(fsp);
324 reply_to_oplock_break_requests(fsp);
327 /*******************************************************************
328 Add a timeout handler waiting for the client reply.
329 *******************************************************************/
331 static void add_oplock_timeout_handler(files_struct *fsp)
334 * If kernel oplocks already notifies smbds when an oplock break times
335 * out, just return.
337 if (koplocks &&
338 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
339 return;
342 if (fsp->oplock_timeout != NULL) {
343 DEBUG(0, ("Logic problem -- have an oplock event hanging "
344 "around\n"));
347 fsp->oplock_timeout =
348 event_add_timed(smbd_event_context(), NULL,
349 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
350 oplock_timeout_handler, fsp);
352 if (fsp->oplock_timeout == NULL) {
353 DEBUG(0, ("Could not add oplock timeout handler\n"));
357 static void send_break_message_smb1(files_struct *fsp, uint8_t level)
359 char *break_msg = new_break_message_smb1(talloc_tos(),
360 fsp,
361 level);
362 if (break_msg == NULL) {
363 exit_server("Could not talloc break_msg\n");
366 show_msg(break_msg);
367 if (!srv_send_smb(smbd_server_fd(),
368 break_msg, false, 0,
369 IS_CONN_ENCRYPTED(fsp->conn),
370 NULL)) {
371 exit_server_cleanly("send_break_message_smb1: "
372 "srv_send_smb failed.");
375 TALLOC_FREE(break_msg);
378 void break_level2_to_none_async(files_struct *fsp)
380 struct smbd_server_connection *sconn = smbd_server_conn;
382 if (fsp->oplock_type == NO_OPLOCK) {
383 /* We already got a "break to none" message and we've handled
384 * it. just ignore. */
385 DEBUG(3, ("process_oplock_async_level2_break_message: already "
386 "broken to none, ignoring.\n"));
387 return;
390 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
391 /* Don't tell the client, just downgrade. */
392 DEBUG(3, ("process_oplock_async_level2_break_message: "
393 "downgrading fake level 2 oplock.\n"));
394 remove_oplock(fsp);
395 return;
398 /* Ensure we're really at level2 state. */
399 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
401 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
402 "to none message for fid %d, file %s\n", fsp->fnum,
403 fsp_str_dbg(fsp)));
405 /* Now send a break to none message to our client. */
406 if (sconn->allow_smb2) {
407 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
408 } else {
409 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
412 /* Async level2 request, don't send a reply, just remove the oplock. */
413 remove_oplock(fsp);
416 /*******************************************************************
417 This handles the case of a write triggering a break to none
418 message on a level2 oplock.
419 When we get this message we may be in any of three states :
420 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
421 the client for LEVEL2.
422 *******************************************************************/
424 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
425 void *private_data,
426 uint32_t msg_type,
427 struct server_id src,
428 DATA_BLOB *data)
430 struct share_mode_entry msg;
431 files_struct *fsp;
433 if (data->data == NULL) {
434 DEBUG(0, ("Got NULL buffer\n"));
435 return;
438 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
439 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
440 return;
443 /* De-linearize incoming message. */
444 message_to_share_mode_entry(&msg, (char *)data->data);
446 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
447 "%s/%lu\n", procid_str(talloc_tos(), &src),
448 file_id_string_tos(&msg.id), msg.share_file_id));
450 fsp = initial_break_processing(msg.id, msg.share_file_id);
452 if (fsp == NULL) {
453 /* We hit a race here. Break messages are sent, and before we
454 * get to process this message, we have closed the file.
455 * No need to reply as this is an async message. */
456 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
457 return;
460 break_level2_to_none_async(fsp);
463 /*******************************************************************
464 This handles the generic oplock break message from another smbd.
465 *******************************************************************/
467 static void process_oplock_break_message(struct messaging_context *msg_ctx,
468 void *private_data,
469 uint32_t msg_type,
470 struct server_id src,
471 DATA_BLOB *data)
473 struct smbd_server_connection *sconn = smbd_server_conn;
474 struct share_mode_entry msg;
475 files_struct *fsp;
476 bool break_to_level2 = False;
478 if (data->data == NULL) {
479 DEBUG(0, ("Got NULL buffer\n"));
480 return;
483 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
484 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
485 return;
488 /* De-linearize incoming message. */
489 message_to_share_mode_entry(&msg, (char *)data->data);
491 DEBUG(10, ("Got oplock break message from pid %s: %s/%lu\n",
492 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
493 msg.share_file_id));
495 fsp = initial_break_processing(msg.id, msg.share_file_id);
497 if (fsp == NULL) {
498 /* We hit a race here. Break messages are sent, and before we
499 * get to process this message, we have closed the file. Reply
500 * with 'ok, oplock broken' */
501 DEBUG(3, ("Did not find fsp\n"));
503 /* We just send the same message back. */
504 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
505 (uint8 *)data->data,
506 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
507 return;
510 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
511 /* Remember we have to inform the requesting PID when the
512 * client replies */
513 msg.pid = src;
514 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
515 &fsp->pending_break_messages,
516 &fsp->num_pending_break_messages);
517 return;
520 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
521 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
522 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
523 file_id_string_tos(&fsp->file_id),
524 fsp_str_dbg(fsp)));
525 /* We just send the same message back. */
526 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
527 (uint8 *)data->data,
528 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
529 return;
532 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
533 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
534 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
535 lp_level2_oplocks(SNUM(fsp->conn))) {
536 break_to_level2 = True;
539 /* Need to wait before sending a break
540 message if we sent ourselves this message. */
541 if (procid_is_me(&src)) {
542 wait_before_sending_break();
545 if (sconn->allow_smb2) {
546 send_break_message_smb2(fsp, break_to_level2 ?
547 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
548 } else {
549 send_break_message_smb1(fsp, break_to_level2 ?
550 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
553 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
555 msg.pid = src;
556 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
557 &fsp->pending_break_messages,
558 &fsp->num_pending_break_messages);
560 add_oplock_timeout_handler(fsp);
563 /*******************************************************************
564 This handles the kernel oplock break message.
565 *******************************************************************/
567 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
568 void *private_data,
569 uint32_t msg_type,
570 struct server_id src,
571 DATA_BLOB *data)
573 struct smbd_server_connection *sconn = smbd_server_conn;
574 struct file_id id;
575 unsigned long file_id;
576 files_struct *fsp;
578 if (data->data == NULL) {
579 DEBUG(0, ("Got NULL buffer\n"));
580 return;
583 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
584 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
585 return;
588 /* Pull the data from the message. */
589 pull_file_id_24((char *)data->data, &id);
590 file_id = (unsigned long)IVAL(data->data, 24);
592 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
593 procid_str(talloc_tos(), &src), file_id_string_tos(&id),
594 (unsigned int)file_id));
596 fsp = initial_break_processing(id, file_id);
598 if (fsp == NULL) {
599 DEBUG(3, ("Got a kernel oplock break message for a file "
600 "I don't know about\n"));
601 return;
604 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
605 /* This is ok, kernel oplocks come in completely async */
606 DEBUG(3, ("Got a kernel oplock request while waiting for a "
607 "break reply\n"));
608 return;
611 if (sconn->allow_smb2) {
612 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
613 } else {
614 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
617 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
619 add_oplock_timeout_handler(fsp);
622 void reply_to_oplock_break_requests(files_struct *fsp)
624 int i;
627 * If kernel oplocks already notifies smbds when oplocks are
628 * broken/removed, just return.
630 if (koplocks &&
631 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
632 return;
635 for (i=0; i<fsp->num_pending_break_messages; i++) {
636 struct share_mode_entry *e = &fsp->pending_break_messages[i];
637 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
639 share_mode_entry_to_message(msg, e);
641 messaging_send_buf(smbd_messaging_context(), e->pid,
642 MSG_SMB_BREAK_RESPONSE,
643 (uint8 *)msg,
644 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
647 SAFE_FREE(fsp->pending_break_messages);
648 fsp->num_pending_break_messages = 0;
649 if (fsp->oplock_timeout != NULL) {
650 /* Remove the timed event handler. */
651 TALLOC_FREE(fsp->oplock_timeout);
652 fsp->oplock_timeout = NULL;
654 return;
657 static void process_oplock_break_response(struct messaging_context *msg_ctx,
658 void *private_data,
659 uint32_t msg_type,
660 struct server_id src,
661 DATA_BLOB *data)
663 struct share_mode_entry msg;
665 if (data->data == NULL) {
666 DEBUG(0, ("Got NULL buffer\n"));
667 return;
670 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
671 DEBUG(0, ("Got invalid msg len %u\n",
672 (unsigned int)data->length));
673 return;
676 /* De-linearize incoming message. */
677 message_to_share_mode_entry(&msg, (char *)data->data);
679 DEBUG(10, ("Got oplock break response from pid %s: %s/%lu mid %llu\n",
680 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
681 msg.share_file_id, (unsigned long long)msg.op_mid));
683 schedule_deferred_open_message_smb(msg.op_mid);
686 static void process_open_retry_message(struct messaging_context *msg_ctx,
687 void *private_data,
688 uint32_t msg_type,
689 struct server_id src,
690 DATA_BLOB *data)
692 struct share_mode_entry msg;
694 if (data->data == NULL) {
695 DEBUG(0, ("Got NULL buffer\n"));
696 return;
699 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
700 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
701 return;
704 /* De-linearize incoming message. */
705 message_to_share_mode_entry(&msg, (char *)data->data);
707 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
708 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
709 (unsigned long long)msg.op_mid));
711 schedule_deferred_open_message_smb(msg.op_mid);
714 /****************************************************************************
715 This function is called on any file modification or lock request. If a file
716 is level 2 oplocked then it must tell all other level 2 holders to break to
717 none.
718 ****************************************************************************/
720 static void contend_level2_oplocks_begin_default(files_struct *fsp,
721 enum level2_contention_type type)
723 int i;
724 struct share_mode_lock *lck;
727 * If this file is level II oplocked then we need
728 * to grab the shared memory lock and inform all
729 * other files with a level II lock that they need
730 * to flush their read caches. We keep the lock over
731 * the shared memory area whilst doing this.
734 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
735 return;
737 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
738 NULL);
739 if (lck == NULL) {
740 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
741 "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
742 return;
745 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
746 lck->num_share_modes ));
748 for(i = 0; i < lck->num_share_modes; i++) {
749 struct share_mode_entry *share_entry = &lck->share_modes[i];
750 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
752 if (!is_valid_share_mode_entry(share_entry)) {
753 continue;
757 * As there could have been multiple writes waiting at the
758 * lock_share_entry gate we may not be the first to
759 * enter. Hence the state of the op_types in the share mode
760 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
761 * oplock. It will do no harm to re-send break messages to
762 * those smbd's that are still waiting their turn to remove
763 * their LEVEL_II state, and also no harm to ignore existing
764 * NO_OPLOCK states. JRA.
767 DEBUG(10,("release_level_2_oplocks_on_change: "
768 "share_entry[%i]->op_type == %d\n",
769 i, share_entry->op_type ));
771 if (share_entry->op_type == NO_OPLOCK) {
772 continue;
775 /* Paranoia .... */
776 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
777 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
778 "share mode entry %d is an exlusive "
779 "oplock !\n", i ));
780 TALLOC_FREE(lck);
781 abort();
784 share_mode_entry_to_message(msg, share_entry);
787 * Deal with a race condition when breaking level2
788 * oplocks. Don't send all the messages and release
789 * the lock, this allows someone else to come in and
790 * get a level2 lock before any of the messages are
791 * processed, and thus miss getting a break message.
792 * Ensure at least one entry (the one we're breaking)
793 * is processed immediately under the lock and becomes
794 * set as NO_OPLOCK to stop any waiter getting a level2.
795 * Bugid #5980.
798 if (procid_is_me(&share_entry->pid)) {
799 wait_before_sending_break();
800 break_level2_to_none_async(fsp);
801 } else {
802 messaging_send_buf(smbd_messaging_context(),
803 share_entry->pid,
804 MSG_SMB_ASYNC_LEVEL2_BREAK,
805 (uint8 *)msg,
806 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
810 /* We let the message receivers handle removing the oplock state
811 in the share mode lock db. */
813 TALLOC_FREE(lck);
816 void contend_level2_oplocks_begin(files_struct *fsp,
817 enum level2_contention_type type)
819 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
820 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
821 return;
824 contend_level2_oplocks_begin_default(fsp, type);
827 void contend_level2_oplocks_end(files_struct *fsp,
828 enum level2_contention_type type)
830 /* Only kernel oplocks implement this so far */
831 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
832 koplocks->ops->contend_level2_oplocks_end(fsp, type);
836 /****************************************************************************
837 Linearize a share mode entry struct to an internal oplock break message.
838 ****************************************************************************/
840 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
842 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
843 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
844 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
845 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
846 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
847 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
848 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
849 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
850 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
851 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
852 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
853 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
854 #ifdef CLUSTER_SUPPORT
855 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
856 #endif
859 /****************************************************************************
860 De-linearize an internal oplock break message to a share mode entry struct.
861 ****************************************************************************/
863 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
865 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
866 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
867 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
868 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
869 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
870 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
871 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
872 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
873 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
874 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
875 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
876 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
877 #ifdef CLUSTER_SUPPORT
878 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
879 #endif
882 /****************************************************************************
883 Setup oplocks for this process.
884 ****************************************************************************/
886 bool init_oplocks(struct messaging_context *msg_ctx)
888 DEBUG(3,("init_oplocks: initializing messages.\n"));
890 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
891 process_oplock_break_message);
892 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
893 process_oplock_async_level2_break_message);
894 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
895 process_oplock_break_response);
896 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
897 process_kernel_oplock_break);
898 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
899 process_open_retry_message);
901 if (lp_kernel_oplocks()) {
902 #if HAVE_KERNEL_OPLOCKS_IRIX
903 koplocks = irix_init_kernel_oplocks(talloc_autofree_context());
904 #elif HAVE_KERNEL_OPLOCKS_LINUX
905 koplocks = linux_init_kernel_oplocks(talloc_autofree_context());
906 #elif HAVE_ONEFS
907 koplocks = onefs_init_kernel_oplocks(talloc_autofree_context());
908 #endif
911 return True;