Add some debug in s4's winbind server
[Samba/gebeck_regimport.git] / source3 / smbd / oplock.c
blob6f4d5780f3b78c85525dbadd0331cd051aa2b4b0
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"
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),
51 MSG_SMB_KERNEL_BREAK,
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 "
65 "support them\n"));
66 return false;
68 if ((fsp->oplock_type != NO_OPLOCK) &&
69 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
70 koplocks &&
71 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
72 return False;
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 ));
89 return True;
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) &&
100 koplocks) {
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;
116 } else {
117 fsp->oplock_type = NO_OPLOCK;
119 fsp->sent_oplock_break = NO_BREAK_SENT;
121 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
122 delete_write_cache(fsp);
124 TALLOC_FREE(fsp->oplock_timeout);
127 /****************************************************************************
128 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
129 ****************************************************************************/
131 static void downgrade_file_oplock(files_struct *fsp)
133 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
134 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
135 return;
138 if (koplocks) {
139 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
141 fsp->oplock_type = LEVEL_II_OPLOCK;
142 exclusive_oplocks_open--;
143 level_II_oplocks_open++;
144 fsp->sent_oplock_break = NO_BREAK_SENT;
147 /****************************************************************************
148 Remove a file oplock. Copes with level II and exclusive.
149 Locks then unlocks the share mode lock. Client can decide to go directly
150 to none even if a "break-to-level II" was sent.
151 ****************************************************************************/
153 bool remove_oplock(files_struct *fsp)
155 bool ret;
156 struct share_mode_lock *lck;
158 /* Remove the oplock flag from the sharemode. */
159 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
160 NULL);
161 if (lck == NULL) {
162 DEBUG(0,("remove_oplock: failed to lock share entry for "
163 "file %s\n", fsp_str_dbg(fsp)));
164 return False;
166 ret = remove_share_oplock(lck, fsp);
167 if (!ret) {
168 DEBUG(0,("remove_oplock: failed to remove share oplock for "
169 "file %s fnum %d, %s\n",
170 fsp_str_dbg(fsp), fsp->fnum,
171 file_id_string_tos(&fsp->file_id)));
173 release_file_oplock(fsp);
174 TALLOC_FREE(lck);
175 return ret;
179 * Deal with a reply when a break-to-level II was sent.
181 bool downgrade_oplock(files_struct *fsp)
183 bool ret;
184 struct share_mode_lock *lck;
186 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
187 NULL);
188 if (lck == NULL) {
189 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
190 "file %s\n", fsp_str_dbg(fsp)));
191 return False;
193 ret = downgrade_share_oplock(lck, fsp);
194 if (!ret) {
195 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
196 "for file %s fnum %d, file_id %s\n",
197 fsp_str_dbg(fsp), fsp->fnum,
198 file_id_string_tos(&fsp->file_id)));
201 downgrade_file_oplock(fsp);
202 TALLOC_FREE(lck);
203 return ret;
207 * Some kernel oplock implementations handle the notification themselves.
209 bool should_notify_deferred_opens()
211 return !(koplocks &&
212 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
215 /****************************************************************************
216 Set up an oplock break message.
217 ****************************************************************************/
219 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
220 files_struct *fsp, int cmd)
222 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
224 if (result == NULL) {
225 DEBUG(0, ("talloc failed\n"));
226 return NULL;
229 memset(result,'\0',smb_size);
230 srv_set_message(result,8,0,true);
231 SCVAL(result,smb_com,SMBlockingX);
232 SSVAL(result,smb_tid,fsp->conn->cnum);
233 SSVAL(result,smb_pid,0xFFFF);
234 SSVAL(result,smb_uid,0);
235 SSVAL(result,smb_mid,0xFFFF);
236 SCVAL(result,smb_vwv0,0xFF);
237 SSVAL(result,smb_vwv2,fsp->fnum);
238 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
239 SCVAL(result,smb_vwv3+1,cmd);
240 return result;
243 /****************************************************************************
244 Function to do the waiting before sending a local break.
245 ****************************************************************************/
247 static void wait_before_sending_break(void)
249 long wait_time = (long)lp_oplock_break_wait_time();
251 if (wait_time) {
252 smb_msleep(wait_time);
256 /****************************************************************************
257 Ensure that we have a valid oplock.
258 ****************************************************************************/
260 static files_struct *initial_break_processing(
261 struct smbd_server_connection *sconn, struct file_id id,
262 unsigned long file_id)
264 files_struct *fsp = NULL;
266 if( DEBUGLVL( 3 ) ) {
267 dbgtext( "initial_break_processing: called for %s/%u\n",
268 file_id_string_tos(&id), (int)file_id);
269 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
270 exclusive_oplocks_open, level_II_oplocks_open );
274 * We need to search the file open table for the
275 * entry containing this dev and inode, and ensure
276 * we have an oplock on it.
279 fsp = file_find_dif(sconn, id, file_id);
281 if(fsp == NULL) {
282 /* The file could have been closed in the meantime - return success. */
283 if( DEBUGLVL( 3 ) ) {
284 dbgtext( "initial_break_processing: cannot find open file with " );
285 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
286 dbgtext( "allowing break to succeed.\n" );
288 return NULL;
291 /* Ensure we have an oplock on the file */
294 * There is a potential race condition in that an oplock could
295 * have been broken due to another udp request, and yet there are
296 * still oplock break messages being sent in the udp message
297 * queue for this file. So return true if we don't have an oplock,
298 * as we may have just freed it.
301 if(fsp->oplock_type == NO_OPLOCK) {
302 if( DEBUGLVL( 3 ) ) {
303 dbgtext( "initial_break_processing: file %s ",
304 fsp_str_dbg(fsp));
305 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
306 file_id_string_tos(&id), fsp->fh->gen_id );
307 dbgtext( "Allowing break to succeed regardless.\n" );
309 return NULL;
312 return fsp;
315 static void oplock_timeout_handler(struct event_context *ctx,
316 struct timed_event *te,
317 struct timeval now,
318 void *private_data)
320 files_struct *fsp = (files_struct *)private_data;
322 /* Remove the timed event handler. */
323 TALLOC_FREE(fsp->oplock_timeout);
324 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
325 fsp_str_dbg(fsp)));
326 remove_oplock(fsp);
327 reply_to_oplock_break_requests(fsp);
330 /*******************************************************************
331 Add a timeout handler waiting for the client reply.
332 *******************************************************************/
334 static void add_oplock_timeout_handler(files_struct *fsp)
337 * If kernel oplocks already notifies smbds when an oplock break times
338 * out, just return.
340 if (koplocks &&
341 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
342 return;
345 if (fsp->oplock_timeout != NULL) {
346 DEBUG(0, ("Logic problem -- have an oplock event hanging "
347 "around\n"));
350 fsp->oplock_timeout =
351 event_add_timed(smbd_event_context(), fsp,
352 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
353 oplock_timeout_handler, fsp);
355 if (fsp->oplock_timeout == NULL) {
356 DEBUG(0, ("Could not add oplock timeout handler\n"));
360 static void send_break_message_smb1(files_struct *fsp, int level)
362 char *break_msg = new_break_message_smb1(talloc_tos(),
363 fsp,
364 level);
365 if (break_msg == NULL) {
366 exit_server("Could not talloc break_msg\n");
369 show_msg(break_msg);
370 if (!srv_send_smb(fsp->conn->sconn,
371 break_msg, false, 0,
372 IS_CONN_ENCRYPTED(fsp->conn),
373 NULL)) {
374 exit_server_cleanly("send_break_message_smb1: "
375 "srv_send_smb failed.");
378 TALLOC_FREE(break_msg);
381 void break_level2_to_none_async(files_struct *fsp)
383 struct smbd_server_connection *sconn = fsp->conn->sconn;
385 if (fsp->oplock_type == NO_OPLOCK) {
386 /* We already got a "break to none" message and we've handled
387 * it. just ignore. */
388 DEBUG(3, ("process_oplock_async_level2_break_message: already "
389 "broken to none, ignoring.\n"));
390 return;
393 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
394 /* Don't tell the client, just downgrade. */
395 DEBUG(3, ("process_oplock_async_level2_break_message: "
396 "downgrading fake level 2 oplock.\n"));
397 remove_oplock(fsp);
398 return;
401 /* Ensure we're really at level2 state. */
402 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
404 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
405 "to none message for fid %d, file %s\n", fsp->fnum,
406 fsp_str_dbg(fsp)));
408 /* Now send a break to none message to our client. */
409 if (sconn->using_smb2) {
410 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
411 } else {
412 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
415 /* Async level2 request, don't send a reply, just remove the oplock. */
416 remove_oplock(fsp);
419 /*******************************************************************
420 This handles the case of a write triggering a break to none
421 message on a level2 oplock.
422 When we get this message we may be in any of three states :
423 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
424 the client for LEVEL2.
425 *******************************************************************/
427 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
428 void *private_data,
429 uint32_t msg_type,
430 struct server_id src,
431 DATA_BLOB *data)
433 struct smbd_server_connection *sconn;
434 struct share_mode_entry msg;
435 files_struct *fsp;
437 if (data->data == NULL) {
438 DEBUG(0, ("Got NULL buffer\n"));
439 return;
442 sconn = msg_ctx_to_sconn(msg_ctx);
443 if (sconn == NULL) {
444 DEBUG(1, ("could not find sconn\n"));
445 return;
448 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
449 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
450 return;
453 /* De-linearize incoming message. */
454 message_to_share_mode_entry(&msg, (char *)data->data);
456 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
457 "%s/%lu\n", procid_str(talloc_tos(), &src),
458 file_id_string_tos(&msg.id), msg.share_file_id));
460 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
462 if (fsp == NULL) {
463 /* We hit a race here. Break messages are sent, and before we
464 * get to process this message, we have closed the file.
465 * No need to reply as this is an async message. */
466 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
467 return;
470 break_level2_to_none_async(fsp);
473 /*******************************************************************
474 This handles the generic oplock break message from another smbd.
475 *******************************************************************/
477 static void process_oplock_break_message(struct messaging_context *msg_ctx,
478 void *private_data,
479 uint32_t msg_type,
480 struct server_id src,
481 DATA_BLOB *data)
483 struct smbd_server_connection *sconn;
484 struct share_mode_entry msg;
485 files_struct *fsp;
486 bool break_to_level2 = False;
488 if (data->data == NULL) {
489 DEBUG(0, ("Got NULL buffer\n"));
490 return;
493 sconn = msg_ctx_to_sconn(msg_ctx);
494 if (sconn == NULL) {
495 DEBUG(1, ("could not find sconn\n"));
496 return;
499 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
500 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
501 return;
504 /* De-linearize incoming message. */
505 message_to_share_mode_entry(&msg, (char *)data->data);
507 DEBUG(10, ("Got oplock break message from pid %s: %s/%lu\n",
508 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
509 msg.share_file_id));
511 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
513 if (fsp == NULL) {
514 /* We hit a race here. Break messages are sent, and before we
515 * get to process this message, we have closed the file. Reply
516 * with 'ok, oplock broken' */
517 DEBUG(3, ("Did not find fsp\n"));
519 /* We just send the same message back. */
520 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
521 (uint8 *)data->data,
522 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
523 return;
526 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
527 /* Remember we have to inform the requesting PID when the
528 * client replies */
529 msg.pid = src;
530 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
531 &fsp->pending_break_messages,
532 &fsp->num_pending_break_messages);
533 return;
536 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
537 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
538 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
539 file_id_string_tos(&fsp->file_id),
540 fsp_str_dbg(fsp)));
541 /* We just send the same message back. */
542 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
543 (uint8 *)data->data,
544 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
545 return;
548 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
549 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
550 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
551 lp_level2_oplocks(SNUM(fsp->conn))) {
552 break_to_level2 = True;
555 /* Need to wait before sending a break
556 message if we sent ourselves this message. */
557 if (procid_is_me(&src)) {
558 wait_before_sending_break();
561 if (sconn->using_smb2) {
562 send_break_message_smb2(fsp, break_to_level2 ?
563 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
564 } else {
565 send_break_message_smb1(fsp, break_to_level2 ?
566 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
569 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
571 msg.pid = src;
572 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
573 &fsp->pending_break_messages,
574 &fsp->num_pending_break_messages);
576 add_oplock_timeout_handler(fsp);
579 /*******************************************************************
580 This handles the kernel oplock break message.
581 *******************************************************************/
583 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
584 void *private_data,
585 uint32_t msg_type,
586 struct server_id src,
587 DATA_BLOB *data)
589 struct smbd_server_connection *sconn;
590 struct file_id id;
591 unsigned long file_id;
592 files_struct *fsp;
594 if (data->data == NULL) {
595 DEBUG(0, ("Got NULL buffer\n"));
596 return;
599 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
600 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
601 return;
604 sconn = msg_ctx_to_sconn(msg_ctx);
605 if (sconn == NULL) {
606 DEBUG(1, ("could not find sconn\n"));
607 return;
610 /* Pull the data from the message. */
611 pull_file_id_24((char *)data->data, &id);
612 file_id = (unsigned long)IVAL(data->data, 24);
614 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
615 procid_str(talloc_tos(), &src), file_id_string_tos(&id),
616 (unsigned int)file_id));
618 fsp = initial_break_processing(sconn, id, file_id);
620 if (fsp == NULL) {
621 DEBUG(3, ("Got a kernel oplock break message for a file "
622 "I don't know about\n"));
623 return;
626 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
627 /* This is ok, kernel oplocks come in completely async */
628 DEBUG(3, ("Got a kernel oplock request while waiting for a "
629 "break reply\n"));
630 return;
633 if (sconn->using_smb2) {
634 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
635 } else {
636 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
639 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
641 add_oplock_timeout_handler(fsp);
644 void reply_to_oplock_break_requests(files_struct *fsp)
646 int i;
649 * If kernel oplocks already notifies smbds when oplocks are
650 * broken/removed, just return.
652 if (koplocks &&
653 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
654 return;
657 for (i=0; i<fsp->num_pending_break_messages; i++) {
658 struct share_mode_entry *e = &fsp->pending_break_messages[i];
659 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
661 share_mode_entry_to_message(msg, e);
663 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
664 MSG_SMB_BREAK_RESPONSE,
665 (uint8 *)msg,
666 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
669 SAFE_FREE(fsp->pending_break_messages);
670 fsp->num_pending_break_messages = 0;
671 if (fsp->oplock_timeout != NULL) {
672 /* Remove the timed event handler. */
673 TALLOC_FREE(fsp->oplock_timeout);
674 fsp->oplock_timeout = NULL;
676 return;
679 static void process_oplock_break_response(struct messaging_context *msg_ctx,
680 void *private_data,
681 uint32_t msg_type,
682 struct server_id src,
683 DATA_BLOB *data)
685 struct share_mode_entry msg;
687 if (data->data == NULL) {
688 DEBUG(0, ("Got NULL buffer\n"));
689 return;
692 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
693 DEBUG(0, ("Got invalid msg len %u\n",
694 (unsigned int)data->length));
695 return;
698 /* De-linearize incoming message. */
699 message_to_share_mode_entry(&msg, (char *)data->data);
701 DEBUG(10, ("Got oplock break response from pid %s: %s/%lu mid %llu\n",
702 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
703 msg.share_file_id, (unsigned long long)msg.op_mid));
705 schedule_deferred_open_message_smb(msg.op_mid);
708 static void process_open_retry_message(struct messaging_context *msg_ctx,
709 void *private_data,
710 uint32_t msg_type,
711 struct server_id src,
712 DATA_BLOB *data)
714 struct share_mode_entry msg;
716 if (data->data == NULL) {
717 DEBUG(0, ("Got NULL buffer\n"));
718 return;
721 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
722 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
723 return;
726 /* De-linearize incoming message. */
727 message_to_share_mode_entry(&msg, (char *)data->data);
729 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
730 procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
731 (unsigned long long)msg.op_mid));
733 schedule_deferred_open_message_smb(msg.op_mid);
736 /****************************************************************************
737 This function is called on any file modification or lock request. If a file
738 is level 2 oplocked then it must tell all other level 2 holders to break to
739 none.
740 ****************************************************************************/
742 static void contend_level2_oplocks_begin_default(files_struct *fsp,
743 enum level2_contention_type type)
745 int i;
746 struct share_mode_lock *lck;
749 * If this file is level II oplocked then we need
750 * to grab the shared memory lock and inform all
751 * other files with a level II lock that they need
752 * to flush their read caches. We keep the lock over
753 * the shared memory area whilst doing this.
756 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
757 return;
759 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
760 NULL);
761 if (lck == NULL) {
762 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
763 "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
764 return;
767 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
768 lck->num_share_modes ));
770 for(i = 0; i < lck->num_share_modes; i++) {
771 struct share_mode_entry *share_entry = &lck->share_modes[i];
772 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
774 if (!is_valid_share_mode_entry(share_entry)) {
775 continue;
779 * As there could have been multiple writes waiting at the
780 * lock_share_entry gate we may not be the first to
781 * enter. Hence the state of the op_types in the share mode
782 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
783 * oplock. It will do no harm to re-send break messages to
784 * those smbd's that are still waiting their turn to remove
785 * their LEVEL_II state, and also no harm to ignore existing
786 * NO_OPLOCK states. JRA.
789 DEBUG(10,("release_level_2_oplocks_on_change: "
790 "share_entry[%i]->op_type == %d\n",
791 i, share_entry->op_type ));
793 if (share_entry->op_type == NO_OPLOCK) {
794 continue;
797 /* Paranoia .... */
798 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
799 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
800 "share mode entry %d is an exlusive "
801 "oplock !\n", i ));
802 TALLOC_FREE(lck);
803 abort();
806 share_mode_entry_to_message(msg, share_entry);
809 * Deal with a race condition when breaking level2
810 * oplocks. Don't send all the messages and release
811 * the lock, this allows someone else to come in and
812 * get a level2 lock before any of the messages are
813 * processed, and thus miss getting a break message.
814 * Ensure at least one entry (the one we're breaking)
815 * is processed immediately under the lock and becomes
816 * set as NO_OPLOCK to stop any waiter getting a level2.
817 * Bugid #5980.
820 if (procid_is_me(&share_entry->pid)) {
821 wait_before_sending_break();
822 break_level2_to_none_async(fsp);
823 } else {
824 messaging_send_buf(fsp->conn->sconn->msg_ctx,
825 share_entry->pid,
826 MSG_SMB_ASYNC_LEVEL2_BREAK,
827 (uint8 *)msg,
828 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
832 /* We let the message receivers handle removing the oplock state
833 in the share mode lock db. */
835 TALLOC_FREE(lck);
838 void contend_level2_oplocks_begin(files_struct *fsp,
839 enum level2_contention_type type)
841 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
842 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
843 return;
846 contend_level2_oplocks_begin_default(fsp, type);
849 void contend_level2_oplocks_end(files_struct *fsp,
850 enum level2_contention_type type)
852 /* Only kernel oplocks implement this so far */
853 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
854 koplocks->ops->contend_level2_oplocks_end(fsp, type);
858 /****************************************************************************
859 Linearize a share mode entry struct to an internal oplock break message.
860 ****************************************************************************/
862 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
864 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
865 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
866 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
867 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
868 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
869 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
870 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
871 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
872 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
873 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
874 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
875 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
876 #ifdef CLUSTER_SUPPORT
877 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
878 #endif
881 /****************************************************************************
882 De-linearize an internal oplock break message to a share mode entry struct.
883 ****************************************************************************/
885 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
887 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
888 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
889 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
890 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
891 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
892 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
893 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
894 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
895 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
896 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
897 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
898 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
899 #ifdef CLUSTER_SUPPORT
900 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
901 #endif
904 /****************************************************************************
905 Setup oplocks for this process.
906 ****************************************************************************/
908 bool init_oplocks(struct messaging_context *msg_ctx)
910 DEBUG(3,("init_oplocks: initializing messages.\n"));
912 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
913 process_oplock_break_message);
914 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
915 process_oplock_async_level2_break_message);
916 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
917 process_oplock_break_response);
918 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
919 process_kernel_oplock_break);
920 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
921 process_open_retry_message);
923 if (lp_kernel_oplocks()) {
924 #if HAVE_KERNEL_OPLOCKS_IRIX
925 koplocks = irix_init_kernel_oplocks(NULL);
926 #elif HAVE_KERNEL_OPLOCKS_LINUX
927 koplocks = linux_init_kernel_oplocks(NULL);
928 #elif HAVE_ONEFS
929 #error Isilon, please check if the NULL context is okay here. Thanks!
930 koplocks = onefs_init_kernel_oplocks(NULL);
931 #endif
934 return True;