s3/docs: Fix typo.
[Samba.git] / source / smbd / oplock.c
blob261d8fd6702def7e9dec90b875adaf2dc7832662
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"
25 /* Current number of oplocks we have outstanding. */
26 static int32 exclusive_oplocks_open = 0;
27 static int32 level_II_oplocks_open = 0;
28 bool global_client_failed_oplock_break = False;
30 extern uint32 global_client_caps;
32 static struct kernel_oplocks *koplocks;
34 /****************************************************************************
35 Get the number of current exclusive oplocks.
36 ****************************************************************************/
38 int32 get_number_of_exclusive_open_oplocks(void)
40 return exclusive_oplocks_open;
43 /****************************************************************************
44 Return True if an oplock message is pending.
45 ****************************************************************************/
47 bool oplock_message_waiting(fd_set *fds)
49 if (koplocks && koplocks->msg_waiting(fds)) {
50 return True;
53 return False;
56 /****************************************************************************
57 Find out if there are any kernel oplock messages waiting and process them
58 if so. pfds is the fd_set from the main select loop (which contains any
59 kernel oplock fd if that's what the system uses (IRIX). If may be NULL if
60 we're calling this in a shutting down state.
61 ****************************************************************************/
63 void process_kernel_oplocks(struct messaging_context *msg_ctx, fd_set *pfds)
66 * We need to check for kernel oplocks before going into the select
67 * here, as the EINTR generated by the linux kernel oplock may have
68 * already been eaten. JRA.
71 if (!koplocks) {
72 return;
75 while (koplocks->msg_waiting(pfds)) {
76 files_struct *fsp;
77 char msg[MSG_SMB_KERNEL_BREAK_SIZE];
79 fsp = koplocks->receive_message(pfds);
81 if (fsp == NULL) {
82 DEBUG(3, ("Kernel oplock message announced, but none "
83 "received\n"));
84 return;
87 /* Put the kernel break info into the message. */
88 push_file_id_16(msg, &fsp->file_id);
89 SIVAL(msg,16,fsp->fh->gen_id);
91 /* Don't need to be root here as we're only ever
92 sending to ourselves. */
94 messaging_send_buf(msg_ctx, procid_self(),
95 MSG_SMB_KERNEL_BREAK,
96 (uint8 *)&msg, MSG_SMB_KERNEL_BREAK_SIZE);
100 /****************************************************************************
101 Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
102 disabled (just sets flags). Returns True if oplock set.
103 ****************************************************************************/
105 bool set_file_oplock(files_struct *fsp, int oplock_type)
107 if ((fsp->oplock_type != NO_OPLOCK) &&
108 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
109 koplocks &&
110 !koplocks->set_oplock(fsp, oplock_type)) {
111 return False;
114 fsp->oplock_type = oplock_type;
115 fsp->sent_oplock_break = NO_BREAK_SENT;
116 if (oplock_type == LEVEL_II_OPLOCK) {
117 level_II_oplocks_open++;
118 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
119 exclusive_oplocks_open++;
122 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
123 "tv_sec = %x, tv_usec = %x\n",
124 fsp->fsp_name, file_id_string_tos(&fsp->file_id),
125 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
126 (int)fsp->open_time.tv_usec ));
128 return True;
131 /****************************************************************************
132 Attempt to release an oplock on a file. Decrements oplock count.
133 ****************************************************************************/
135 void release_file_oplock(files_struct *fsp)
137 if ((fsp->oplock_type != NO_OPLOCK) &&
138 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
139 koplocks) {
140 koplocks->release_oplock(fsp);
143 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
144 level_II_oplocks_open--;
145 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
146 exclusive_oplocks_open--;
149 SMB_ASSERT(exclusive_oplocks_open>=0);
150 SMB_ASSERT(level_II_oplocks_open>=0);
152 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
153 /* This doesn't matter for close. */
154 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
155 } else {
156 fsp->oplock_type = NO_OPLOCK;
158 fsp->sent_oplock_break = NO_BREAK_SENT;
160 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
162 TALLOC_FREE(fsp->oplock_timeout);
165 /****************************************************************************
166 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
167 ****************************************************************************/
169 static void downgrade_file_oplock(files_struct *fsp)
171 if (koplocks) {
172 koplocks->release_oplock(fsp);
174 fsp->oplock_type = LEVEL_II_OPLOCK;
175 exclusive_oplocks_open--;
176 level_II_oplocks_open++;
177 fsp->sent_oplock_break = NO_BREAK_SENT;
180 /****************************************************************************
181 Remove a file oplock. Copes with level II and exclusive.
182 Locks then unlocks the share mode lock. Client can decide to go directly
183 to none even if a "break-to-level II" was sent.
184 ****************************************************************************/
186 bool remove_oplock(files_struct *fsp)
188 bool ret;
189 struct share_mode_lock *lck;
191 /* Remove the oplock flag from the sharemode. */
192 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
193 NULL);
194 if (lck == NULL) {
195 DEBUG(0,("remove_oplock: failed to lock share entry for "
196 "file %s\n", fsp->fsp_name ));
197 return False;
199 ret = remove_share_oplock(lck, fsp);
200 if (!ret) {
201 DEBUG(0,("remove_oplock: failed to remove share oplock for "
202 "file %s fnum %d, %s\n",
203 fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
205 release_file_oplock(fsp);
206 TALLOC_FREE(lck);
207 return ret;
211 * Deal with a reply when a break-to-level II was sent.
213 bool downgrade_oplock(files_struct *fsp)
215 bool ret;
216 struct share_mode_lock *lck;
218 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
219 NULL);
220 if (lck == NULL) {
221 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
222 "file %s\n", fsp->fsp_name ));
223 return False;
225 ret = downgrade_share_oplock(lck, fsp);
226 if (!ret) {
227 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
228 "for file %s fnum %d, file_id %s\n",
229 fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
232 downgrade_file_oplock(fsp);
233 TALLOC_FREE(lck);
234 return ret;
237 /****************************************************************************
238 Return the fd (if any) used for receiving oplock notifications.
239 ****************************************************************************/
241 int oplock_notify_fd(void)
243 if (koplocks) {
244 return koplocks->notification_fd;
247 return -1;
250 /****************************************************************************
251 Set up an oplock break message.
252 ****************************************************************************/
254 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
255 files_struct *fsp, uint8 cmd)
257 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
259 if (result == NULL) {
260 DEBUG(0, ("talloc failed\n"));
261 return NULL;
264 memset(result,'\0',smb_size);
265 srv_set_message(result,8,0,true);
266 SCVAL(result,smb_com,SMBlockingX);
267 SSVAL(result,smb_tid,fsp->conn->cnum);
268 SSVAL(result,smb_pid,0xFFFF);
269 SSVAL(result,smb_uid,0);
270 SSVAL(result,smb_mid,0xFFFF);
271 SCVAL(result,smb_vwv0,0xFF);
272 SSVAL(result,smb_vwv2,fsp->fnum);
273 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
274 SCVAL(result,smb_vwv3+1,cmd);
275 return result;
278 /****************************************************************************
279 Function to do the waiting before sending a local break.
280 ****************************************************************************/
282 static void wait_before_sending_break(void)
284 long wait_time = (long)lp_oplock_break_wait_time();
286 if (wait_time) {
287 smb_msleep(wait_time);
291 /****************************************************************************
292 Ensure that we have a valid oplock.
293 ****************************************************************************/
295 static files_struct *initial_break_processing(struct file_id id, unsigned long file_id)
297 files_struct *fsp = NULL;
299 if( DEBUGLVL( 3 ) ) {
300 dbgtext( "initial_break_processing: called for %s/%u\n",
301 file_id_string_tos(&id), (int)file_id);
302 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
303 exclusive_oplocks_open, level_II_oplocks_open );
307 * We need to search the file open table for the
308 * entry containing this dev and inode, and ensure
309 * we have an oplock on it.
312 fsp = file_find_dif(id, file_id);
314 if(fsp == NULL) {
315 /* The file could have been closed in the meantime - return success. */
316 if( DEBUGLVL( 3 ) ) {
317 dbgtext( "initial_break_processing: cannot find open file with " );
318 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
319 dbgtext( "allowing break to succeed.\n" );
321 return NULL;
324 /* Ensure we have an oplock on the file */
327 * There is a potential race condition in that an oplock could
328 * have been broken due to another udp request, and yet there are
329 * still oplock break messages being sent in the udp message
330 * queue for this file. So return true if we don't have an oplock,
331 * as we may have just freed it.
334 if(fsp->oplock_type == NO_OPLOCK) {
335 if( DEBUGLVL( 3 ) ) {
336 dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
337 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
338 file_id_string_tos(&id), fsp->fh->gen_id );
339 dbgtext( "Allowing break to succeed regardless.\n" );
341 return NULL;
344 return fsp;
347 static void oplock_timeout_handler(struct event_context *ctx,
348 struct timed_event *te,
349 const struct timeval *now,
350 void *private_data)
352 files_struct *fsp = (files_struct *)private_data;
354 /* Remove the timed event handler. */
355 TALLOC_FREE(fsp->oplock_timeout);
356 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
357 global_client_failed_oplock_break = True;
358 remove_oplock(fsp);
359 reply_to_oplock_break_requests(fsp);
362 /*******************************************************************
363 Add a timeout handler waiting for the client reply.
364 *******************************************************************/
366 static void add_oplock_timeout_handler(files_struct *fsp)
368 if (fsp->oplock_timeout != NULL) {
369 DEBUG(0, ("Logic problem -- have an oplock event hanging "
370 "around\n"));
373 fsp->oplock_timeout =
374 event_add_timed(smbd_event_context(), NULL,
375 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
376 "oplock_timeout_handler",
377 oplock_timeout_handler, fsp);
379 if (fsp->oplock_timeout == NULL) {
380 DEBUG(0, ("Could not add oplock timeout handler\n"));
384 /*******************************************************************
385 This handles the case of a write triggering a break to none
386 message on a level2 oplock.
387 When we get this message we may be in any of three states :
388 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
389 the client for LEVEL2.
390 *******************************************************************/
392 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
393 void *private_data,
394 uint32_t msg_type,
395 struct server_id src,
396 DATA_BLOB *data)
398 struct share_mode_entry msg;
399 files_struct *fsp;
400 char *break_msg;
401 bool sign_state;
403 if (data->data == NULL) {
404 DEBUG(0, ("Got NULL buffer\n"));
405 return;
408 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
409 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
410 return;
413 /* De-linearize incoming message. */
414 message_to_share_mode_entry(&msg, (char *)data->data);
416 DEBUG(10, ("Got oplock async level 2 break message from pid %d: %s/%lu\n",
417 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
419 fsp = initial_break_processing(msg.id, msg.share_file_id);
421 if (fsp == NULL) {
422 /* We hit a race here. Break messages are sent, and before we
423 * get to process this message, we have closed the file.
424 * No need to reply as this is an async message. */
425 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
426 return;
429 if (fsp->oplock_type == NO_OPLOCK) {
430 /* We already got a "break to none" message and we've handled it.
431 * just ignore. */
432 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
433 return;
436 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
437 /* Don't tell the client, just downgrade. */
438 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
439 remove_oplock(fsp);
440 return;
443 /* Ensure we're really at level2 state. */
444 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
446 DEBUG(10,("process_oplock_async_level2_break_message: sending break to "
447 "none message for fid %d, file %s\n",
448 fsp->fnum,
449 fsp->fsp_name));
451 /* Now send a break to none message to our client. */
453 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
454 if (break_msg == NULL) {
455 exit_server("Could not talloc break_msg\n");
458 /* Need to wait before sending a break message if we sent ourselves this message. */
459 if (procid_to_pid(&src) == sys_getpid()) {
460 wait_before_sending_break();
463 /* Save the server smb signing state. */
464 sign_state = srv_oplock_set_signing(False);
466 show_msg(break_msg);
467 if (!srv_send_smb(smbd_server_fd(),
468 break_msg,
469 IS_CONN_ENCRYPTED(fsp->conn))) {
470 exit_server_cleanly("oplock_break: srv_send_smb failed.");
473 /* Restore the sign state to what it was. */
474 srv_oplock_set_signing(sign_state);
476 TALLOC_FREE(break_msg);
478 /* Async level2 request, don't send a reply, just remove the oplock. */
479 remove_oplock(fsp);
482 /*******************************************************************
483 This handles the generic oplock break message from another smbd.
484 *******************************************************************/
486 static void process_oplock_break_message(struct messaging_context *msg_ctx,
487 void *private_data,
488 uint32_t msg_type,
489 struct server_id src,
490 DATA_BLOB *data)
492 struct share_mode_entry msg;
493 files_struct *fsp;
494 char *break_msg;
495 bool break_to_level2 = False;
496 bool sign_state;
498 if (data->data == NULL) {
499 DEBUG(0, ("Got NULL buffer\n"));
500 return;
503 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
504 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
505 return;
508 /* De-linearize incoming message. */
509 message_to_share_mode_entry(&msg, (char *)data->data);
511 DEBUG(10, ("Got oplock break message from pid %d: %s/%lu\n",
512 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
514 fsp = initial_break_processing(msg.id, msg.share_file_id);
516 if (fsp == NULL) {
517 /* a We hit race here. Break messages are sent, and before we
518 * get to process this message, we have closed the file. Reply
519 * with 'ok, oplock broken' */
520 DEBUG(3, ("Did not find fsp\n"));
522 /* We just send the same message back. */
523 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
524 (uint8 *)data->data,
525 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
526 return;
529 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
530 /* Remember we have to inform the requesting PID when the
531 * client replies */
532 msg.pid = src;
533 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
534 &fsp->pending_break_messages,
535 &fsp->num_pending_break_messages);
536 return;
539 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
540 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
541 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
542 file_id_string_tos(&fsp->file_id),
543 fsp->fsp_name));
544 /* We just send the same message back. */
545 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
546 (uint8 *)data->data,
547 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
548 return;
551 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
552 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
553 !koplocks && /* NOTE: we force levelII off for kernel oplocks -
554 * this will change when it is supported */
555 lp_level2_oplocks(SNUM(fsp->conn))) {
556 break_to_level2 = True;
559 break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
560 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
561 if (break_msg == NULL) {
562 exit_server("Could not talloc break_msg\n");
565 /* Need to wait before sending a break message if we sent ourselves this message. */
566 if (procid_to_pid(&src) == sys_getpid()) {
567 wait_before_sending_break();
570 /* Save the server smb signing state. */
571 sign_state = srv_oplock_set_signing(False);
573 show_msg(break_msg);
574 if (!srv_send_smb(smbd_server_fd(),
575 break_msg,
576 IS_CONN_ENCRYPTED(fsp->conn))) {
577 exit_server_cleanly("oplock_break: srv_send_smb failed.");
580 /* Restore the sign state to what it was. */
581 srv_oplock_set_signing(sign_state);
583 TALLOC_FREE(break_msg);
585 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
587 msg.pid = src;
588 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
589 &fsp->pending_break_messages,
590 &fsp->num_pending_break_messages);
592 add_oplock_timeout_handler(fsp);
595 /*******************************************************************
596 This handles the kernel oplock break message.
597 *******************************************************************/
599 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
600 void *private_data,
601 uint32_t msg_type,
602 struct server_id src,
603 DATA_BLOB *data)
605 struct file_id id;
606 unsigned long file_id;
607 files_struct *fsp;
608 char *break_msg;
609 bool sign_state;
611 if (data->data == NULL) {
612 DEBUG(0, ("Got NULL buffer\n"));
613 return;
616 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
617 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
618 return;
621 /* Pull the data from the message. */
622 pull_file_id_16((char *)data->data, &id);
623 file_id = (unsigned long)IVAL(data->data, 16);
625 DEBUG(10, ("Got kernel oplock break message from pid %d: %s/%u\n",
626 (int)procid_to_pid(&src), file_id_string_tos(&id),
627 (unsigned int)file_id));
629 fsp = initial_break_processing(id, file_id);
631 if (fsp == NULL) {
632 DEBUG(3, ("Got a kernel oplock break message for a file "
633 "I don't know about\n"));
634 return;
637 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
638 /* This is ok, kernel oplocks come in completely async */
639 DEBUG(3, ("Got a kernel oplock request while waiting for a "
640 "break reply\n"));
641 return;
644 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
645 if (break_msg == NULL) {
646 exit_server("Could not talloc break_msg\n");
649 /* Save the server smb signing state. */
650 sign_state = srv_oplock_set_signing(False);
652 show_msg(break_msg);
653 if (!srv_send_smb(smbd_server_fd(),
654 break_msg,
655 IS_CONN_ENCRYPTED(fsp->conn))) {
656 exit_server_cleanly("oplock_break: srv_send_smb failed.");
659 /* Restore the sign state to what it was. */
660 srv_oplock_set_signing(sign_state);
662 TALLOC_FREE(break_msg);
664 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
666 add_oplock_timeout_handler(fsp);
669 void reply_to_oplock_break_requests(files_struct *fsp)
671 int i;
673 for (i=0; i<fsp->num_pending_break_messages; i++) {
674 struct share_mode_entry *e = &fsp->pending_break_messages[i];
675 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
677 share_mode_entry_to_message(msg, e);
679 messaging_send_buf(smbd_messaging_context(), e->pid,
680 MSG_SMB_BREAK_RESPONSE,
681 (uint8 *)msg,
682 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
685 SAFE_FREE(fsp->pending_break_messages);
686 fsp->num_pending_break_messages = 0;
687 if (fsp->oplock_timeout != NULL) {
688 /* Remove the timed event handler. */
689 TALLOC_FREE(fsp->oplock_timeout);
690 fsp->oplock_timeout = NULL;
692 return;
695 static void process_oplock_break_response(struct messaging_context *msg_ctx,
696 void *private_data,
697 uint32_t msg_type,
698 struct server_id src,
699 DATA_BLOB *data)
701 struct share_mode_entry msg;
703 if (data->data == NULL) {
704 DEBUG(0, ("Got NULL buffer\n"));
705 return;
708 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
709 DEBUG(0, ("Got invalid msg len %u\n",
710 (unsigned int)data->length));
711 return;
714 /* De-linearize incoming message. */
715 message_to_share_mode_entry(&msg, (char *)data->data);
717 DEBUG(10, ("Got oplock break response from pid %d: %s/%lu mid %u\n",
718 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id,
719 (unsigned int)msg.op_mid));
721 /* Here's the hack from open.c, store the mid in the 'port' field */
722 schedule_deferred_open_smb_message(msg.op_mid);
725 static void process_open_retry_message(struct messaging_context *msg_ctx,
726 void *private_data,
727 uint32_t msg_type,
728 struct server_id src,
729 DATA_BLOB *data)
731 struct share_mode_entry msg;
733 if (data->data == NULL) {
734 DEBUG(0, ("Got NULL buffer\n"));
735 return;
738 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
739 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
740 return;
743 /* De-linearize incoming message. */
744 message_to_share_mode_entry(&msg, (char *)data->data);
746 DEBUG(10, ("Got open retry msg from pid %d: %s mid %u\n",
747 (int)procid_to_pid(&src), file_id_string_tos(&msg.id),
748 (unsigned int)msg.op_mid));
750 schedule_deferred_open_smb_message(msg.op_mid);
753 /****************************************************************************
754 This function is called on any file modification or lock request. If a file
755 is level 2 oplocked then it must tell all other level 2 holders to break to
756 none.
757 ****************************************************************************/
759 void release_level_2_oplocks_on_change(files_struct *fsp)
761 int i;
762 struct share_mode_lock *lck;
765 * If this file is level II oplocked then we need
766 * to grab the shared memory lock and inform all
767 * other files with a level II lock that they need
768 * to flush their read caches. We keep the lock over
769 * the shared memory area whilst doing this.
772 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
773 return;
775 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
776 NULL);
777 if (lck == NULL) {
778 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
779 "share mode entry for file %s.\n", fsp->fsp_name ));
780 return;
783 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
784 lck->num_share_modes ));
786 for(i = 0; i < lck->num_share_modes; i++) {
787 struct share_mode_entry *share_entry = &lck->share_modes[i];
788 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
790 if (!is_valid_share_mode_entry(share_entry)) {
791 continue;
795 * As there could have been multiple writes waiting at the
796 * lock_share_entry gate we may not be the first to
797 * enter. Hence the state of the op_types in the share mode
798 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
799 * oplock. It will do no harm to re-send break messages to
800 * those smbd's that are still waiting their turn to remove
801 * their LEVEL_II state, and also no harm to ignore existing
802 * NO_OPLOCK states. JRA.
805 DEBUG(10,("release_level_2_oplocks_on_change: "
806 "share_entry[%i]->op_type == %d\n",
807 i, share_entry->op_type ));
809 if (share_entry->op_type == NO_OPLOCK) {
810 continue;
813 /* Paranoia .... */
814 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
815 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
816 "share mode entry %d is an exlusive "
817 "oplock !\n", i ));
818 TALLOC_FREE(lck);
819 abort();
822 share_mode_entry_to_message(msg, share_entry);
825 * Deal with a race condition when breaking level2
826 * oplocks. Don't send all the messages and release
827 * the lock, this allows someone else to come in and
828 * get a level2 lock before any of the messages are
829 * processed, and thus miss getting a break message.
830 * Ensure at least one entry (the one we're breaking)
831 * is processed immediately under the lock and becomes
832 * set as NO_OPLOCK to stop any waiter getting a level2.
833 * Bugid #5980.
836 if (procid_is_me(&share_entry->pid)) {
837 DATA_BLOB blob = data_blob_const(msg,
838 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
839 process_oplock_async_level2_break_message(smbd_messaging_context(),
840 NULL,
841 MSG_SMB_ASYNC_LEVEL2_BREAK,
842 share_entry->pid,
843 &blob);
844 } else {
845 messaging_send_buf(smbd_messaging_context(),
846 share_entry->pid,
847 MSG_SMB_ASYNC_LEVEL2_BREAK,
848 (uint8 *)msg,
849 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
853 /* We let the message receivers handle removing the oplock state
854 in the share mode lock db. */
856 TALLOC_FREE(lck);
859 /****************************************************************************
860 Linearize a share mode entry struct to an internal oplock break message.
861 ****************************************************************************/
863 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
865 SIVAL(msg,0,(uint32)e->pid.pid);
866 SSVAL(msg,4,e->op_mid);
867 SSVAL(msg,6,e->op_type);
868 SIVAL(msg,8,e->access_mask);
869 SIVAL(msg,12,e->share_access);
870 SIVAL(msg,16,e->private_options);
871 SIVAL(msg,20,(uint32)e->time.tv_sec);
872 SIVAL(msg,24,(uint32)e->time.tv_usec);
873 push_file_id_16(msg+28, &e->id);
874 SIVAL(msg,44,e->share_file_id);
875 SIVAL(msg,48,e->uid);
876 SSVAL(msg,52,e->flags);
877 #ifdef CLUSTER_SUPPORT
878 SIVAL(msg,54,e->pid.vnn);
879 #endif
882 /****************************************************************************
883 De-linearize an internal oplock break message to a share mode entry struct.
884 ****************************************************************************/
886 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
888 e->pid.pid = (pid_t)IVAL(msg,0);
889 e->op_mid = SVAL(msg,4);
890 e->op_type = SVAL(msg,6);
891 e->access_mask = IVAL(msg,8);
892 e->share_access = IVAL(msg,12);
893 e->private_options = IVAL(msg,16);
894 e->time.tv_sec = (time_t)IVAL(msg,20);
895 e->time.tv_usec = (int)IVAL(msg,24);
896 pull_file_id_16(msg+28, &e->id);
897 e->share_file_id = (unsigned long)IVAL(msg,44);
898 e->uid = (uint32)IVAL(msg,48);
899 e->flags = (uint16)SVAL(msg,52);
900 #ifdef CLUSTER_SUPPORT
901 e->pid.vnn = IVAL(msg,54);
902 #endif
905 /****************************************************************************
906 Setup oplocks for this process.
907 ****************************************************************************/
909 bool init_oplocks(struct messaging_context *msg_ctx)
911 DEBUG(3,("init_oplocks: initializing messages.\n"));
913 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
914 process_oplock_break_message);
915 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
916 process_oplock_async_level2_break_message);
917 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
918 process_oplock_break_response);
919 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
920 process_kernel_oplock_break);
921 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
922 process_open_retry_message);
924 if (lp_kernel_oplocks()) {
925 #if HAVE_KERNEL_OPLOCKS_IRIX
926 koplocks = irix_init_kernel_oplocks();
927 #elif HAVE_KERNEL_OPLOCKS_LINUX
928 koplocks = linux_init_kernel_oplocks();
929 #endif
932 return True;