s3: Use autogenerated open_files.idl
[Samba.git] / source3 / smbd / oplock.c
blob1bf957d810efc87d0e872637f0503ff33c12dc1f
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/smbd.h"
25 #include "smbd/globals.h"
26 #include "messages.h"
27 #include "../librpc/gen_ndr/open_files.h"
29 /****************************************************************************
30 Get the number of current exclusive oplocks.
31 ****************************************************************************/
33 int32 get_number_of_exclusive_open_oplocks(void)
35 return exclusive_oplocks_open;
39 * helper function used by the kernel oplock backends to post the break message
41 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
43 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
45 /* Put the kernel break info into the message. */
46 push_file_id_24((char *)msg, &fsp->file_id);
47 SIVAL(msg,24,fsp->fh->gen_id);
49 /* Don't need to be root here as we're only ever
50 sending to ourselves. */
52 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
53 MSG_SMB_KERNEL_BREAK,
54 msg, MSG_SMB_KERNEL_BREAK_SIZE);
57 /****************************************************************************
58 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
59 disabled (just sets flags) and no byte-range locks in the file. Returns True
60 if oplock set.
61 ****************************************************************************/
63 bool set_file_oplock(files_struct *fsp, int oplock_type)
65 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
66 if (koplocks &&
67 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
68 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
69 "don't support them\n"));
70 return false;
74 if ((fsp->oplock_type != NO_OPLOCK) &&
75 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
76 koplocks &&
77 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
78 return False;
81 fsp->oplock_type = oplock_type;
82 fsp->sent_oplock_break = NO_BREAK_SENT;
83 if (oplock_type == LEVEL_II_OPLOCK) {
84 level_II_oplocks_open++;
85 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
86 exclusive_oplocks_open++;
89 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
90 "tv_sec = %x, tv_usec = %x\n",
91 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
92 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
93 (int)fsp->open_time.tv_usec ));
95 return True;
98 /****************************************************************************
99 Attempt to release an oplock on a file. Decrements oplock count.
100 ****************************************************************************/
102 void release_file_oplock(files_struct *fsp)
104 if ((fsp->oplock_type != NO_OPLOCK) &&
105 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
106 koplocks) {
107 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
110 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
111 level_II_oplocks_open--;
112 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
113 exclusive_oplocks_open--;
116 SMB_ASSERT(exclusive_oplocks_open>=0);
117 SMB_ASSERT(level_II_oplocks_open>=0);
119 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
120 /* This doesn't matter for close. */
121 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
122 } else {
123 fsp->oplock_type = NO_OPLOCK;
125 fsp->sent_oplock_break = NO_BREAK_SENT;
127 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
128 delete_write_cache(fsp);
130 TALLOC_FREE(fsp->oplock_timeout);
133 /****************************************************************************
134 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
135 ****************************************************************************/
137 static void downgrade_file_oplock(files_struct *fsp)
139 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
140 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
141 return;
144 if (koplocks) {
145 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
147 fsp->oplock_type = LEVEL_II_OPLOCK;
148 exclusive_oplocks_open--;
149 level_II_oplocks_open++;
150 fsp->sent_oplock_break = NO_BREAK_SENT;
153 /****************************************************************************
154 Remove a file oplock. Copes with level II and exclusive.
155 Locks then unlocks the share mode lock. Client can decide to go directly
156 to none even if a "break-to-level II" was sent.
157 ****************************************************************************/
159 bool remove_oplock(files_struct *fsp)
161 bool ret;
162 struct share_mode_lock *lck;
164 /* Remove the oplock flag from the sharemode. */
165 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
166 NULL);
167 if (lck == NULL) {
168 DEBUG(0,("remove_oplock: failed to lock share entry for "
169 "file %s\n", fsp_str_dbg(fsp)));
170 return False;
172 ret = remove_share_oplock(lck, fsp);
173 if (!ret) {
174 DEBUG(0,("remove_oplock: failed to remove share oplock for "
175 "file %s fnum %d, %s\n",
176 fsp_str_dbg(fsp), fsp->fnum,
177 file_id_string_tos(&fsp->file_id)));
179 release_file_oplock(fsp);
180 TALLOC_FREE(lck);
181 return ret;
185 * Deal with a reply when a break-to-level II was sent.
187 bool downgrade_oplock(files_struct *fsp)
189 bool ret;
190 struct share_mode_lock *lck;
192 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
193 NULL);
194 if (lck == NULL) {
195 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
196 "file %s\n", fsp_str_dbg(fsp)));
197 return False;
199 ret = downgrade_share_oplock(lck, fsp);
200 if (!ret) {
201 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
202 "for file %s fnum %d, file_id %s\n",
203 fsp_str_dbg(fsp), fsp->fnum,
204 file_id_string_tos(&fsp->file_id)));
207 downgrade_file_oplock(fsp);
208 TALLOC_FREE(lck);
209 return ret;
213 * Some kernel oplock implementations handle the notification themselves.
215 bool should_notify_deferred_opens()
217 return !(koplocks &&
218 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
221 /****************************************************************************
222 Set up an oplock break message.
223 ****************************************************************************/
225 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
226 files_struct *fsp, int cmd)
228 char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
230 if (result == NULL) {
231 DEBUG(0, ("talloc failed\n"));
232 return NULL;
235 memset(result,'\0',smb_size);
236 srv_set_message(result,8,0,true);
237 SCVAL(result,smb_com,SMBlockingX);
238 SSVAL(result,smb_tid,fsp->conn->cnum);
239 SSVAL(result,smb_pid,0xFFFF);
240 SSVAL(result,smb_uid,0);
241 SSVAL(result,smb_mid,0xFFFF);
242 SCVAL(result,smb_vwv0,0xFF);
243 SSVAL(result,smb_vwv2,fsp->fnum);
244 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
245 SCVAL(result,smb_vwv3+1,cmd);
246 return result;
249 /****************************************************************************
250 Function to do the waiting before sending a local break.
251 ****************************************************************************/
253 static void wait_before_sending_break(void)
255 long wait_time = (long)lp_oplock_break_wait_time();
257 if (wait_time) {
258 smb_msleep(wait_time);
262 /****************************************************************************
263 Ensure that we have a valid oplock.
264 ****************************************************************************/
266 static files_struct *initial_break_processing(
267 struct smbd_server_connection *sconn, struct file_id id,
268 unsigned long file_id)
270 files_struct *fsp = NULL;
272 if( DEBUGLVL( 3 ) ) {
273 dbgtext( "initial_break_processing: called for %s/%u\n",
274 file_id_string_tos(&id), (int)file_id);
275 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
276 exclusive_oplocks_open, level_II_oplocks_open );
280 * We need to search the file open table for the
281 * entry containing this dev and inode, and ensure
282 * we have an oplock on it.
285 fsp = file_find_dif(sconn, id, file_id);
287 if(fsp == NULL) {
288 /* The file could have been closed in the meantime - return success. */
289 if( DEBUGLVL( 3 ) ) {
290 dbgtext( "initial_break_processing: cannot find open file with " );
291 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
292 dbgtext( "allowing break to succeed.\n" );
294 return NULL;
297 /* Ensure we have an oplock on the file */
300 * There is a potential race condition in that an oplock could
301 * have been broken due to another udp request, and yet there are
302 * still oplock break messages being sent in the udp message
303 * queue for this file. So return true if we don't have an oplock,
304 * as we may have just freed it.
307 if(fsp->oplock_type == NO_OPLOCK) {
308 if( DEBUGLVL( 3 ) ) {
309 dbgtext( "initial_break_processing: file %s ",
310 fsp_str_dbg(fsp));
311 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
312 file_id_string_tos(&id), fsp->fh->gen_id );
313 dbgtext( "Allowing break to succeed regardless.\n" );
315 return NULL;
318 return fsp;
321 static void oplock_timeout_handler(struct event_context *ctx,
322 struct timed_event *te,
323 struct timeval now,
324 void *private_data)
326 files_struct *fsp = (files_struct *)private_data;
328 /* Remove the timed event handler. */
329 TALLOC_FREE(fsp->oplock_timeout);
330 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
331 fsp_str_dbg(fsp)));
332 remove_oplock(fsp);
333 reply_to_oplock_break_requests(fsp);
336 /*******************************************************************
337 Add a timeout handler waiting for the client reply.
338 *******************************************************************/
340 static void add_oplock_timeout_handler(files_struct *fsp)
343 * If kernel oplocks already notifies smbds when an oplock break times
344 * out, just return.
346 if (koplocks &&
347 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
348 return;
351 if (fsp->oplock_timeout != NULL) {
352 DEBUG(0, ("Logic problem -- have an oplock event hanging "
353 "around\n"));
356 fsp->oplock_timeout =
357 event_add_timed(server_event_context(), fsp,
358 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
359 oplock_timeout_handler, fsp);
361 if (fsp->oplock_timeout == NULL) {
362 DEBUG(0, ("Could not add oplock timeout handler\n"));
366 static void send_break_message_smb1(files_struct *fsp, int level)
368 char *break_msg = new_break_message_smb1(talloc_tos(),
369 fsp,
370 level);
371 if (break_msg == NULL) {
372 exit_server("Could not talloc break_msg\n");
375 show_msg(break_msg);
376 if (!srv_send_smb(fsp->conn->sconn,
377 break_msg, false, 0,
378 IS_CONN_ENCRYPTED(fsp->conn),
379 NULL)) {
380 exit_server_cleanly("send_break_message_smb1: "
381 "srv_send_smb failed.");
384 TALLOC_FREE(break_msg);
387 void break_level2_to_none_async(files_struct *fsp)
389 struct smbd_server_connection *sconn = fsp->conn->sconn;
391 if (fsp->oplock_type == NO_OPLOCK) {
392 /* We already got a "break to none" message and we've handled
393 * it. just ignore. */
394 DEBUG(3, ("process_oplock_async_level2_break_message: already "
395 "broken to none, ignoring.\n"));
396 return;
399 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
400 /* Don't tell the client, just downgrade. */
401 DEBUG(3, ("process_oplock_async_level2_break_message: "
402 "downgrading fake level 2 oplock.\n"));
403 remove_oplock(fsp);
404 return;
407 /* Ensure we're really at level2 state. */
408 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
410 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
411 "to none message for fid %d, file %s\n", fsp->fnum,
412 fsp_str_dbg(fsp)));
414 /* Now send a break to none message to our client. */
415 if (sconn->using_smb2) {
416 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
417 } else {
418 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
421 /* Async level2 request, don't send a reply, just remove the oplock. */
422 remove_oplock(fsp);
425 /*******************************************************************
426 This handles the case of a write triggering a break to none
427 message on a level2 oplock.
428 When we get this message we may be in any of three states :
429 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
430 the client for LEVEL2.
431 *******************************************************************/
433 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
434 void *private_data,
435 uint32_t msg_type,
436 struct server_id src,
437 DATA_BLOB *data)
439 struct smbd_server_connection *sconn;
440 struct share_mode_entry msg;
441 files_struct *fsp;
443 if (data->data == NULL) {
444 DEBUG(0, ("Got NULL buffer\n"));
445 return;
448 sconn = msg_ctx_to_sconn(msg_ctx);
449 if (sconn == NULL) {
450 DEBUG(1, ("could not find sconn\n"));
451 return;
454 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
455 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
456 return;
459 /* De-linearize incoming message. */
460 message_to_share_mode_entry(&msg, (char *)data->data);
462 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
463 "%s/%lu\n", server_id_str(talloc_tos(), &src),
464 file_id_string_tos(&msg.id), msg.share_file_id));
466 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
468 if (fsp == NULL) {
469 /* We hit a race here. Break messages are sent, and before we
470 * get to process this message, we have closed the file.
471 * No need to reply as this is an async message. */
472 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
473 return;
476 break_level2_to_none_async(fsp);
479 /*******************************************************************
480 This handles the generic oplock break message from another smbd.
481 *******************************************************************/
483 static void process_oplock_break_message(struct messaging_context *msg_ctx,
484 void *private_data,
485 uint32_t msg_type,
486 struct server_id src,
487 DATA_BLOB *data)
489 struct smbd_server_connection *sconn;
490 struct share_mode_entry msg;
491 files_struct *fsp;
492 bool break_to_level2 = False;
494 if (data->data == NULL) {
495 DEBUG(0, ("Got NULL buffer\n"));
496 return;
499 sconn = msg_ctx_to_sconn(msg_ctx);
500 if (sconn == NULL) {
501 DEBUG(1, ("could not find sconn\n"));
502 return;
505 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
506 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
507 return;
510 /* De-linearize incoming message. */
511 message_to_share_mode_entry(&msg, (char *)data->data);
513 DEBUG(10, ("Got oplock break message from pid %s: %s/%lu\n",
514 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
515 msg.share_file_id));
517 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
519 if (fsp == NULL) {
520 /* We hit a race here. Break messages are sent, and before we
521 * get to process this message, we have closed the file. Reply
522 * with 'ok, oplock broken' */
523 DEBUG(3, ("Did not find fsp\n"));
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 (fsp->sent_oplock_break != NO_BREAK_SENT) {
533 /* Remember we have to inform the requesting PID when the
534 * client replies */
535 msg.pid = src;
536 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
537 &fsp->pending_break_messages,
538 &fsp->num_pending_break_messages);
539 return;
542 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
543 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
544 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
545 file_id_string_tos(&fsp->file_id),
546 fsp_str_dbg(fsp)));
547 /* We just send the same message back. */
548 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
549 (uint8 *)data->data,
550 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
551 return;
554 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
555 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
556 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
557 lp_level2_oplocks(SNUM(fsp->conn))) {
558 break_to_level2 = True;
561 /* Need to wait before sending a break
562 message if we sent ourselves this message. */
563 if (procid_is_me(&src)) {
564 wait_before_sending_break();
567 if (sconn->using_smb2) {
568 send_break_message_smb2(fsp, break_to_level2 ?
569 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
570 } else {
571 send_break_message_smb1(fsp, break_to_level2 ?
572 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
575 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
577 msg.pid = src;
578 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
579 &fsp->pending_break_messages,
580 &fsp->num_pending_break_messages);
582 add_oplock_timeout_handler(fsp);
585 /*******************************************************************
586 This handles the kernel oplock break message.
587 *******************************************************************/
589 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
590 void *private_data,
591 uint32_t msg_type,
592 struct server_id src,
593 DATA_BLOB *data)
595 struct smbd_server_connection *sconn;
596 struct file_id id;
597 unsigned long file_id;
598 files_struct *fsp;
600 if (data->data == NULL) {
601 DEBUG(0, ("Got NULL buffer\n"));
602 return;
605 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
606 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
607 return;
610 sconn = msg_ctx_to_sconn(msg_ctx);
611 if (sconn == NULL) {
612 DEBUG(1, ("could not find sconn\n"));
613 return;
616 /* Pull the data from the message. */
617 pull_file_id_24((char *)data->data, &id);
618 file_id = (unsigned long)IVAL(data->data, 24);
620 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
621 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
622 (unsigned int)file_id));
624 fsp = initial_break_processing(sconn, id, file_id);
626 if (fsp == NULL) {
627 DEBUG(3, ("Got a kernel oplock break message for a file "
628 "I don't know about\n"));
629 return;
632 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
633 /* This is ok, kernel oplocks come in completely async */
634 DEBUG(3, ("Got a kernel oplock request while waiting for a "
635 "break reply\n"));
636 return;
639 if (sconn->using_smb2) {
640 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
641 } else {
642 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
645 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
647 add_oplock_timeout_handler(fsp);
650 void reply_to_oplock_break_requests(files_struct *fsp)
652 int i;
655 * If kernel oplocks already notifies smbds when oplocks are
656 * broken/removed, just return.
658 if (koplocks &&
659 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
660 return;
663 for (i=0; i<fsp->num_pending_break_messages; i++) {
664 struct share_mode_entry *e = &fsp->pending_break_messages[i];
665 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
667 share_mode_entry_to_message(msg, e);
669 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
670 MSG_SMB_BREAK_RESPONSE,
671 (uint8 *)msg,
672 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
675 SAFE_FREE(fsp->pending_break_messages);
676 fsp->num_pending_break_messages = 0;
677 if (fsp->oplock_timeout != NULL) {
678 /* Remove the timed event handler. */
679 TALLOC_FREE(fsp->oplock_timeout);
680 fsp->oplock_timeout = NULL;
682 return;
685 static void process_oplock_break_response(struct messaging_context *msg_ctx,
686 void *private_data,
687 uint32_t msg_type,
688 struct server_id src,
689 DATA_BLOB *data)
691 struct share_mode_entry msg;
692 struct smbd_server_connection *sconn;
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 %u\n",
701 (unsigned int)data->length));
702 return;
705 /* De-linearize incoming message. */
706 message_to_share_mode_entry(&msg, (char *)data->data);
708 DEBUG(10, ("Got oplock break response from pid %s: %s/%lu mid %llu\n",
709 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
710 msg.share_file_id, (unsigned long long)msg.op_mid));
712 sconn = msg_ctx_to_sconn(msg_ctx);
713 if (sconn != NULL) {
714 schedule_deferred_open_message_smb(sconn, msg.op_mid);
718 static void process_open_retry_message(struct messaging_context *msg_ctx,
719 void *private_data,
720 uint32_t msg_type,
721 struct server_id src,
722 DATA_BLOB *data)
724 struct share_mode_entry msg;
725 struct smbd_server_connection *sconn;
727 if (data->data == NULL) {
728 DEBUG(0, ("Got NULL buffer\n"));
729 return;
732 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
733 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
734 return;
737 /* De-linearize incoming message. */
738 message_to_share_mode_entry(&msg, (char *)data->data);
740 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
741 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
742 (unsigned long long)msg.op_mid));
744 sconn = msg_ctx_to_sconn(msg_ctx);
745 if (sconn != NULL) {
746 schedule_deferred_open_message_smb(sconn, msg.op_mid);
750 /****************************************************************************
751 This function is called on any file modification or lock request. If a file
752 is level 2 oplocked then it must tell all other level 2 holders to break to
753 none.
754 ****************************************************************************/
756 static void contend_level2_oplocks_begin_default(files_struct *fsp,
757 enum level2_contention_type type)
759 int i;
760 struct share_mode_lock *lck;
763 * If this file is level II oplocked then we need
764 * to grab the shared memory lock and inform all
765 * other files with a level II lock that they need
766 * to flush their read caches. We keep the lock over
767 * the shared memory area whilst doing this.
770 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
771 return;
773 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
774 NULL);
775 if (lck == NULL) {
776 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
777 "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
778 return;
781 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
782 lck->num_share_modes ));
784 for(i = 0; i < lck->num_share_modes; i++) {
785 struct share_mode_entry *share_entry = &lck->share_modes[i];
786 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
788 if (!is_valid_share_mode_entry(share_entry)) {
789 continue;
793 * As there could have been multiple writes waiting at the
794 * lock_share_entry gate we may not be the first to
795 * enter. Hence the state of the op_types in the share mode
796 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
797 * oplock. It will do no harm to re-send break messages to
798 * those smbd's that are still waiting their turn to remove
799 * their LEVEL_II state, and also no harm to ignore existing
800 * NO_OPLOCK states. JRA.
803 DEBUG(10,("release_level_2_oplocks_on_change: "
804 "share_entry[%i]->op_type == %d\n",
805 i, share_entry->op_type ));
807 if (share_entry->op_type == NO_OPLOCK) {
808 continue;
811 /* Paranoia .... */
812 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
813 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
814 "share mode entry %d is an exlusive "
815 "oplock !\n", i ));
816 TALLOC_FREE(lck);
817 abort();
820 share_mode_entry_to_message(msg, share_entry);
823 * Deal with a race condition when breaking level2
824 * oplocks. Don't send all the messages and release
825 * the lock, this allows someone else to come in and
826 * get a level2 lock before any of the messages are
827 * processed, and thus miss getting a break message.
828 * Ensure at least one entry (the one we're breaking)
829 * is processed immediately under the lock and becomes
830 * set as NO_OPLOCK to stop any waiter getting a level2.
831 * Bugid #5980.
834 if (procid_is_me(&share_entry->pid)) {
835 struct files_struct *cur_fsp =
836 initial_break_processing(fsp->conn->sconn,
837 share_entry->id,
838 share_entry->share_file_id);
839 wait_before_sending_break();
840 if (cur_fsp != NULL) {
841 break_level2_to_none_async(cur_fsp);
842 } else {
843 DEBUG(3, ("release_level_2_oplocks_on_change: "
844 "Did not find fsp, ignoring\n"));
846 } else {
847 messaging_send_buf(fsp->conn->sconn->msg_ctx,
848 share_entry->pid,
849 MSG_SMB_ASYNC_LEVEL2_BREAK,
850 (uint8 *)msg,
851 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
855 /* We let the message receivers handle removing the oplock state
856 in the share mode lock db. */
858 TALLOC_FREE(lck);
861 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
862 enum level2_contention_type type)
864 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
865 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
866 return;
869 contend_level2_oplocks_begin_default(fsp, type);
872 void smbd_contend_level2_oplocks_end(files_struct *fsp,
873 enum level2_contention_type type)
875 /* Only kernel oplocks implement this so far */
876 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
877 koplocks->ops->contend_level2_oplocks_end(fsp, type);
881 /****************************************************************************
882 Linearize a share mode entry struct to an internal oplock break message.
883 ****************************************************************************/
885 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
887 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
888 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
889 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
890 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
891 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
892 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
893 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
894 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
895 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
896 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
897 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
898 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
899 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
900 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
903 /****************************************************************************
904 De-linearize an internal oplock break message to a share mode entry struct.
905 ****************************************************************************/
907 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
909 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
910 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
911 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
912 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
913 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
914 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
915 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
916 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
917 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
918 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
919 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
920 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
921 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
922 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
925 /****************************************************************************
926 Setup oplocks for this process.
927 ****************************************************************************/
929 bool init_oplocks(struct messaging_context *msg_ctx)
931 DEBUG(3,("init_oplocks: initializing messages.\n"));
933 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
934 process_oplock_break_message);
935 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
936 process_oplock_async_level2_break_message);
937 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
938 process_oplock_break_response);
939 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
940 process_kernel_oplock_break);
941 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
942 process_open_retry_message);
944 if (lp_kernel_oplocks()) {
945 #if HAVE_KERNEL_OPLOCKS_IRIX
946 koplocks = irix_init_kernel_oplocks(NULL);
947 #elif HAVE_KERNEL_OPLOCKS_LINUX
948 koplocks = linux_init_kernel_oplocks(NULL);
949 #elif HAVE_ONEFS
950 #error Isilon, please check if the NULL context is okay here. Thanks!
951 koplocks = onefs_init_kernel_oplocks(NULL);
952 #endif
955 return True;