s3-includes: enable TYPESAFE_QSORT() in s3
[Samba/gebeck_regimport.git] / source3 / smbd / oplock.c
blobf3034bd739687254b1f8a88fe5fd914038278579
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_smb_message(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 void break_level2_to_none_async(files_struct *fsp)
359 char *break_msg;
361 if (fsp->oplock_type == NO_OPLOCK) {
362 /* We already got a "break to none" message and we've handled
363 * it. just ignore. */
364 DEBUG(3, ("process_oplock_async_level2_break_message: already "
365 "broken to none, ignoring.\n"));
366 return;
369 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
370 /* Don't tell the client, just downgrade. */
371 DEBUG(3, ("process_oplock_async_level2_break_message: "
372 "downgrading fake level 2 oplock.\n"));
373 remove_oplock(fsp);
374 return;
377 /* Ensure we're really at level2 state. */
378 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
380 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
381 "to none message for fid %d, file %s\n", fsp->fnum,
382 fsp_str_dbg(fsp)));
384 /* Now send a break to none message to our client. */
385 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
386 if (break_msg == NULL) {
387 exit_server("Could not talloc break_msg\n");
390 show_msg(break_msg);
391 if (!srv_send_smb(smbd_server_fd(),
392 break_msg, false, 0,
393 IS_CONN_ENCRYPTED(fsp->conn),
394 NULL)) {
395 exit_server_cleanly("oplock_break: srv_send_smb failed.");
398 TALLOC_FREE(break_msg);
400 /* Async level2 request, don't send a reply, just remove the oplock. */
401 remove_oplock(fsp);
405 /*******************************************************************
406 This handles the case of a write triggering a break to none
407 message on a level2 oplock.
408 When we get this message we may be in any of three states :
409 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
410 the client for LEVEL2.
411 *******************************************************************/
413 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
414 void *private_data,
415 uint32_t msg_type,
416 struct server_id src,
417 DATA_BLOB *data)
419 struct share_mode_entry msg;
420 files_struct *fsp;
422 if (data->data == NULL) {
423 DEBUG(0, ("Got NULL buffer\n"));
424 return;
427 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
428 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
429 return;
432 /* De-linearize incoming message. */
433 message_to_share_mode_entry(&msg, (char *)data->data);
435 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
436 "%s/%lu\n", procid_str(talloc_tos(), &src),
437 file_id_string_tos(&msg.id), msg.share_file_id));
439 fsp = initial_break_processing(msg.id, msg.share_file_id);
441 if (fsp == NULL) {
442 /* We hit a race here. Break messages are sent, and before we
443 * get to process this message, we have closed the file.
444 * No need to reply as this is an async message. */
445 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
446 return;
449 break_level2_to_none_async(fsp);
452 /*******************************************************************
453 This handles the generic oplock break message from another smbd.
454 *******************************************************************/
456 static void process_oplock_break_message(struct messaging_context *msg_ctx,
457 void *private_data,
458 uint32_t msg_type,
459 struct server_id src,
460 DATA_BLOB *data)
462 struct share_mode_entry msg;
463 files_struct *fsp;
464 char *break_msg;
465 bool break_to_level2 = False;
467 if (data->data == NULL) {
468 DEBUG(0, ("Got NULL buffer\n"));
469 return;
472 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
473 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
474 return;
477 /* De-linearize incoming message. */
478 message_to_share_mode_entry(&msg, (char *)data->data);
480 DEBUG(10, ("Got oplock break message from pid %s: %s/%lu\n",
481 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
482 msg.share_file_id));
484 fsp = initial_break_processing(msg.id, msg.share_file_id);
486 if (fsp == NULL) {
487 /* We hit a race here. Break messages are sent, and before we
488 * get to process this message, we have closed the file. Reply
489 * with 'ok, oplock broken' */
490 DEBUG(3, ("Did not find fsp\n"));
492 /* We just send the same message back. */
493 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
494 (uint8 *)data->data,
495 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
496 return;
499 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
500 /* Remember we have to inform the requesting PID when the
501 * client replies */
502 msg.pid = src;
503 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
504 &fsp->pending_break_messages,
505 &fsp->num_pending_break_messages);
506 return;
509 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
510 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
511 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
512 file_id_string_tos(&fsp->file_id),
513 fsp_str_dbg(fsp)));
514 /* We just send the same message back. */
515 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
516 (uint8 *)data->data,
517 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
518 return;
521 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
522 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
523 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
524 lp_level2_oplocks(SNUM(fsp->conn))) {
525 break_to_level2 = True;
528 break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
529 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
530 if (break_msg == NULL) {
531 exit_server("Could not talloc break_msg\n");
534 /* Need to wait before sending a break message if we sent ourselves this message. */
535 if (procid_is_me(&src)) {
536 wait_before_sending_break();
539 show_msg(break_msg);
540 if (!srv_send_smb(smbd_server_fd(),
541 break_msg, false, 0,
542 IS_CONN_ENCRYPTED(fsp->conn),
543 NULL)) {
544 exit_server_cleanly("oplock_break: srv_send_smb failed.");
547 TALLOC_FREE(break_msg);
549 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
551 msg.pid = src;
552 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
553 &fsp->pending_break_messages,
554 &fsp->num_pending_break_messages);
556 add_oplock_timeout_handler(fsp);
559 /*******************************************************************
560 This handles the kernel oplock break message.
561 *******************************************************************/
563 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
564 void *private_data,
565 uint32_t msg_type,
566 struct server_id src,
567 DATA_BLOB *data)
569 struct file_id id;
570 unsigned long file_id;
571 files_struct *fsp;
572 char *break_msg;
574 if (data->data == NULL) {
575 DEBUG(0, ("Got NULL buffer\n"));
576 return;
579 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
580 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
581 return;
584 /* Pull the data from the message. */
585 pull_file_id_24((char *)data->data, &id);
586 file_id = (unsigned long)IVAL(data->data, 24);
588 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
589 procid_str(talloc_tos(), &src), file_id_string_tos(&id),
590 (unsigned int)file_id));
592 fsp = initial_break_processing(id, file_id);
594 if (fsp == NULL) {
595 DEBUG(3, ("Got a kernel oplock break message for a file "
596 "I don't know about\n"));
597 return;
600 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
601 /* This is ok, kernel oplocks come in completely async */
602 DEBUG(3, ("Got a kernel oplock request while waiting for a "
603 "break reply\n"));
604 return;
607 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
608 if (break_msg == NULL) {
609 exit_server("Could not talloc break_msg\n");
612 show_msg(break_msg);
613 if (!srv_send_smb(smbd_server_fd(),
614 break_msg, false, 0,
615 IS_CONN_ENCRYPTED(fsp->conn),
616 NULL)) {
617 exit_server_cleanly("oplock_break: srv_send_smb failed.");
620 TALLOC_FREE(break_msg);
622 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
624 add_oplock_timeout_handler(fsp);
627 void reply_to_oplock_break_requests(files_struct *fsp)
629 int i;
632 * If kernel oplocks already notifies smbds when oplocks are
633 * broken/removed, just return.
635 if (koplocks &&
636 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
637 return;
640 for (i=0; i<fsp->num_pending_break_messages; i++) {
641 struct share_mode_entry *e = &fsp->pending_break_messages[i];
642 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
644 share_mode_entry_to_message(msg, e);
646 messaging_send_buf(smbd_messaging_context(), e->pid,
647 MSG_SMB_BREAK_RESPONSE,
648 (uint8 *)msg,
649 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
652 SAFE_FREE(fsp->pending_break_messages);
653 fsp->num_pending_break_messages = 0;
654 if (fsp->oplock_timeout != NULL) {
655 /* Remove the timed event handler. */
656 TALLOC_FREE(fsp->oplock_timeout);
657 fsp->oplock_timeout = NULL;
659 return;
662 static void process_oplock_break_response(struct messaging_context *msg_ctx,
663 void *private_data,
664 uint32_t msg_type,
665 struct server_id src,
666 DATA_BLOB *data)
668 struct share_mode_entry msg;
670 if (data->data == NULL) {
671 DEBUG(0, ("Got NULL buffer\n"));
672 return;
675 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
676 DEBUG(0, ("Got invalid msg len %u\n",
677 (unsigned int)data->length));
678 return;
681 /* De-linearize incoming message. */
682 message_to_share_mode_entry(&msg, (char *)data->data);
684 DEBUG(10, ("Got oplock break response from pid %s: %s/%lu mid %u\n",
685 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
686 msg.share_file_id, (unsigned int)msg.op_mid));
688 schedule_deferred_open_smb_message(msg.op_mid);
691 static void process_open_retry_message(struct messaging_context *msg_ctx,
692 void *private_data,
693 uint32_t msg_type,
694 struct server_id src,
695 DATA_BLOB *data)
697 struct share_mode_entry msg;
699 if (data->data == NULL) {
700 DEBUG(0, ("Got NULL buffer\n"));
701 return;
704 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
705 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
706 return;
709 /* De-linearize incoming message. */
710 message_to_share_mode_entry(&msg, (char *)data->data);
712 DEBUG(10, ("Got open retry msg from pid %s: %s mid %u\n",
713 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
714 (unsigned int)msg.op_mid));
716 schedule_deferred_open_smb_message(msg.op_mid);
719 /****************************************************************************
720 This function is called on any file modification or lock request. If a file
721 is level 2 oplocked then it must tell all other level 2 holders to break to
722 none.
723 ****************************************************************************/
725 static void contend_level2_oplocks_begin_default(files_struct *fsp,
726 enum level2_contention_type type)
728 int i;
729 struct share_mode_lock *lck;
732 * If this file is level II oplocked then we need
733 * to grab the shared memory lock and inform all
734 * other files with a level II lock that they need
735 * to flush their read caches. We keep the lock over
736 * the shared memory area whilst doing this.
739 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
740 return;
742 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
743 NULL);
744 if (lck == NULL) {
745 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
746 "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
747 return;
750 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
751 lck->num_share_modes ));
753 for(i = 0; i < lck->num_share_modes; i++) {
754 struct share_mode_entry *share_entry = &lck->share_modes[i];
755 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
757 if (!is_valid_share_mode_entry(share_entry)) {
758 continue;
762 * As there could have been multiple writes waiting at the
763 * lock_share_entry gate we may not be the first to
764 * enter. Hence the state of the op_types in the share mode
765 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
766 * oplock. It will do no harm to re-send break messages to
767 * those smbd's that are still waiting their turn to remove
768 * their LEVEL_II state, and also no harm to ignore existing
769 * NO_OPLOCK states. JRA.
772 DEBUG(10,("release_level_2_oplocks_on_change: "
773 "share_entry[%i]->op_type == %d\n",
774 i, share_entry->op_type ));
776 if (share_entry->op_type == NO_OPLOCK) {
777 continue;
780 /* Paranoia .... */
781 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
782 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
783 "share mode entry %d is an exlusive "
784 "oplock !\n", i ));
785 TALLOC_FREE(lck);
786 abort();
789 share_mode_entry_to_message(msg, share_entry);
792 * Deal with a race condition when breaking level2
793 * oplocks. Don't send all the messages and release
794 * the lock, this allows someone else to come in and
795 * get a level2 lock before any of the messages are
796 * processed, and thus miss getting a break message.
797 * Ensure at least one entry (the one we're breaking)
798 * is processed immediately under the lock and becomes
799 * set as NO_OPLOCK to stop any waiter getting a level2.
800 * Bugid #5980.
803 if (procid_is_me(&share_entry->pid)) {
804 wait_before_sending_break();
805 break_level2_to_none_async(fsp);
806 } else {
807 messaging_send_buf(smbd_messaging_context(),
808 share_entry->pid,
809 MSG_SMB_ASYNC_LEVEL2_BREAK,
810 (uint8 *)msg,
811 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
815 /* We let the message receivers handle removing the oplock state
816 in the share mode lock db. */
818 TALLOC_FREE(lck);
821 void contend_level2_oplocks_begin(files_struct *fsp,
822 enum level2_contention_type type)
824 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
825 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
826 return;
829 contend_level2_oplocks_begin_default(fsp, type);
832 void contend_level2_oplocks_end(files_struct *fsp,
833 enum level2_contention_type type)
835 /* Only kernel oplocks implement this so far */
836 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
837 koplocks->ops->contend_level2_oplocks_end(fsp, type);
841 /****************************************************************************
842 Linearize a share mode entry struct to an internal oplock break message.
843 ****************************************************************************/
845 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
847 SIVAL(msg,0,(uint32)e->pid.pid);
848 SSVAL(msg,4,e->op_mid);
849 SSVAL(msg,6,e->op_type);
850 SIVAL(msg,8,e->access_mask);
851 SIVAL(msg,12,e->share_access);
852 SIVAL(msg,16,e->private_options);
853 SIVAL(msg,20,(uint32)e->time.tv_sec);
854 SIVAL(msg,24,(uint32)e->time.tv_usec);
855 push_file_id_24(msg+28, &e->id);
856 SIVAL(msg,52,e->share_file_id);
857 SIVAL(msg,56,e->uid);
858 SSVAL(msg,60,e->flags);
859 #ifdef CLUSTER_SUPPORT
860 SIVAL(msg,62,e->pid.vnn);
861 #endif
864 /****************************************************************************
865 De-linearize an internal oplock break message to a share mode entry struct.
866 ****************************************************************************/
868 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
870 e->pid.pid = (pid_t)IVAL(msg,0);
871 e->op_mid = SVAL(msg,4);
872 e->op_type = SVAL(msg,6);
873 e->access_mask = IVAL(msg,8);
874 e->share_access = IVAL(msg,12);
875 e->private_options = IVAL(msg,16);
876 e->time.tv_sec = (time_t)IVAL(msg,20);
877 e->time.tv_usec = (int)IVAL(msg,24);
878 pull_file_id_24(msg+28, &e->id);
879 e->share_file_id = (unsigned long)IVAL(msg,52);
880 e->uid = (uint32)IVAL(msg,56);
881 e->flags = (uint16)SVAL(msg,60);
882 #ifdef CLUSTER_SUPPORT
883 e->pid.vnn = IVAL(msg,62);
884 #endif
887 /****************************************************************************
888 Setup oplocks for this process.
889 ****************************************************************************/
891 bool init_oplocks(struct messaging_context *msg_ctx)
893 DEBUG(3,("init_oplocks: initializing messages.\n"));
895 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
896 process_oplock_break_message);
897 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
898 process_oplock_async_level2_break_message);
899 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
900 process_oplock_break_response);
901 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
902 process_kernel_oplock_break);
903 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
904 process_open_retry_message);
906 if (lp_kernel_oplocks()) {
907 #if HAVE_KERNEL_OPLOCKS_IRIX
908 koplocks = irix_init_kernel_oplocks(talloc_autofree_context());
909 #elif HAVE_KERNEL_OPLOCKS_LINUX
910 koplocks = linux_init_kernel_oplocks(talloc_autofree_context());
911 #elif HAVE_ONEFS
912 koplocks = onefs_init_kernel_oplocks(talloc_autofree_context());
913 #endif
916 return True;