Remove a pointless CONST_DISCARD
[Samba/gebeck_regimport.git] / source3 / smbd / oplock.c
blob23411294df508b2bb4af2ae0db1e00c80560ebb0
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 (koplocks && !koplocks->set_oplock(fsp, oplock_type)) {
108 return False;
111 fsp->oplock_type = oplock_type;
112 fsp->sent_oplock_break = NO_BREAK_SENT;
113 if (oplock_type == LEVEL_II_OPLOCK) {
114 level_II_oplocks_open++;
115 } else {
116 exclusive_oplocks_open++;
119 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
120 "tv_sec = %x, tv_usec = %x\n",
121 fsp->fsp_name, file_id_string_tos(&fsp->file_id),
122 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
123 (int)fsp->open_time.tv_usec ));
125 return True;
128 /****************************************************************************
129 Attempt to release an oplock on a file. Decrements oplock count.
130 ****************************************************************************/
132 void release_file_oplock(files_struct *fsp)
134 if ((fsp->oplock_type != NO_OPLOCK) &&
135 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
136 koplocks) {
137 koplocks->release_oplock(fsp);
140 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
141 level_II_oplocks_open--;
142 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
143 exclusive_oplocks_open--;
146 SMB_ASSERT(exclusive_oplocks_open>=0);
147 SMB_ASSERT(level_II_oplocks_open>=0);
149 fsp->oplock_type = NO_OPLOCK;
150 fsp->sent_oplock_break = NO_BREAK_SENT;
152 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
154 TALLOC_FREE(fsp->oplock_timeout);
157 /****************************************************************************
158 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
159 ****************************************************************************/
161 static void downgrade_file_oplock(files_struct *fsp)
163 if (koplocks) {
164 koplocks->release_oplock(fsp);
166 fsp->oplock_type = LEVEL_II_OPLOCK;
167 exclusive_oplocks_open--;
168 level_II_oplocks_open++;
169 fsp->sent_oplock_break = NO_BREAK_SENT;
172 /****************************************************************************
173 Remove a file oplock. Copes with level II and exclusive.
174 Locks then unlocks the share mode lock. Client can decide to go directly
175 to none even if a "break-to-level II" was sent.
176 ****************************************************************************/
178 bool remove_oplock(files_struct *fsp)
180 bool ret;
181 struct share_mode_lock *lck;
183 /* Remove the oplock flag from the sharemode. */
184 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
185 NULL);
186 if (lck == NULL) {
187 DEBUG(0,("remove_oplock: failed to lock share entry for "
188 "file %s\n", fsp->fsp_name ));
189 return False;
191 ret = remove_share_oplock(lck, fsp);
192 if (!ret) {
193 DEBUG(0,("remove_oplock: failed to remove share oplock for "
194 "file %s fnum %d, %s\n",
195 fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
197 release_file_oplock(fsp);
198 TALLOC_FREE(lck);
199 return ret;
203 * Deal with a reply when a break-to-level II was sent.
205 bool downgrade_oplock(files_struct *fsp)
207 bool ret;
208 struct share_mode_lock *lck;
210 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
211 NULL);
212 if (lck == NULL) {
213 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
214 "file %s\n", fsp->fsp_name ));
215 return False;
217 ret = downgrade_share_oplock(lck, fsp);
218 if (!ret) {
219 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
220 "for file %s fnum %d, file_id %s\n",
221 fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
224 downgrade_file_oplock(fsp);
225 TALLOC_FREE(lck);
226 return ret;
229 /****************************************************************************
230 Return the fd (if any) used for receiving oplock notifications.
231 ****************************************************************************/
233 int oplock_notify_fd(void)
235 if (koplocks) {
236 return koplocks->notification_fd;
239 return -1;
242 /****************************************************************************
243 Set up an oplock break message.
244 ****************************************************************************/
246 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
247 files_struct *fsp, uint8 cmd)
249 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
251 if (result == NULL) {
252 DEBUG(0, ("talloc failed\n"));
253 return NULL;
256 memset(result,'\0',smb_size);
257 srv_set_message(result,8,0,true);
258 SCVAL(result,smb_com,SMBlockingX);
259 SSVAL(result,smb_tid,fsp->conn->cnum);
260 SSVAL(result,smb_pid,0xFFFF);
261 SSVAL(result,smb_uid,0);
262 SSVAL(result,smb_mid,0xFFFF);
263 SCVAL(result,smb_vwv0,0xFF);
264 SSVAL(result,smb_vwv2,fsp->fnum);
265 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
266 SCVAL(result,smb_vwv3+1,cmd);
267 return result;
270 /****************************************************************************
271 Function to do the waiting before sending a local break.
272 ****************************************************************************/
274 static void wait_before_sending_break(void)
276 long wait_time = (long)lp_oplock_break_wait_time();
278 if (wait_time) {
279 smb_msleep(wait_time);
283 /****************************************************************************
284 Ensure that we have a valid oplock.
285 ****************************************************************************/
287 static files_struct *initial_break_processing(struct file_id id, unsigned long file_id)
289 files_struct *fsp = NULL;
291 if( DEBUGLVL( 3 ) ) {
292 dbgtext( "initial_break_processing: called for %s/%u\n",
293 file_id_string_tos(&id), (int)file_id);
294 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
295 exclusive_oplocks_open, level_II_oplocks_open );
299 * We need to search the file open table for the
300 * entry containing this dev and inode, and ensure
301 * we have an oplock on it.
304 fsp = file_find_dif(id, file_id);
306 if(fsp == NULL) {
307 /* The file could have been closed in the meantime - return success. */
308 if( DEBUGLVL( 3 ) ) {
309 dbgtext( "initial_break_processing: cannot find open file with " );
310 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
311 dbgtext( "allowing break to succeed.\n" );
313 return NULL;
316 /* Ensure we have an oplock on the file */
319 * There is a potential race condition in that an oplock could
320 * have been broken due to another udp request, and yet there are
321 * still oplock break messages being sent in the udp message
322 * queue for this file. So return true if we don't have an oplock,
323 * as we may have just freed it.
326 if(fsp->oplock_type == NO_OPLOCK) {
327 if( DEBUGLVL( 3 ) ) {
328 dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
329 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
330 file_id_string_tos(&id), fsp->fh->gen_id );
331 dbgtext( "Allowing break to succeed regardless.\n" );
333 return NULL;
336 return fsp;
339 static void oplock_timeout_handler(struct event_context *ctx,
340 struct timed_event *te,
341 const struct timeval *now,
342 void *private_data)
344 files_struct *fsp = (files_struct *)private_data;
346 /* Remove the timed event handler. */
347 TALLOC_FREE(fsp->oplock_timeout);
348 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
349 global_client_failed_oplock_break = True;
350 remove_oplock(fsp);
351 reply_to_oplock_break_requests(fsp);
354 /*******************************************************************
355 Add a timeout handler waiting for the client reply.
356 *******************************************************************/
358 static void add_oplock_timeout_handler(files_struct *fsp)
360 if (fsp->oplock_timeout != NULL) {
361 DEBUG(0, ("Logic problem -- have an oplock event hanging "
362 "around\n"));
365 fsp->oplock_timeout =
366 event_add_timed(smbd_event_context(), NULL,
367 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
368 "oplock_timeout_handler",
369 oplock_timeout_handler, fsp);
371 if (fsp->oplock_timeout == NULL) {
372 DEBUG(0, ("Could not add oplock timeout handler\n"));
376 /*******************************************************************
377 This handles the case of a write triggering a break to none
378 message on a level2 oplock.
379 When we get this message we may be in any of three states :
380 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
381 the client for LEVEL2.
382 *******************************************************************/
384 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
385 void *private_data,
386 uint32_t msg_type,
387 struct server_id src,
388 DATA_BLOB *data)
390 struct share_mode_entry msg;
391 files_struct *fsp;
392 char *break_msg;
393 bool sign_state;
395 if (data->data == NULL) {
396 DEBUG(0, ("Got NULL buffer\n"));
397 return;
400 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
401 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
402 return;
405 /* De-linearize incoming message. */
406 message_to_share_mode_entry(&msg, (char *)data->data);
408 DEBUG(10, ("Got oplock async level 2 break message from pid %d: %s/%lu\n",
409 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
411 fsp = initial_break_processing(msg.id, msg.share_file_id);
413 if (fsp == NULL) {
414 /* We hit a race here. Break messages are sent, and before we
415 * get to process this message, we have closed the file.
416 * No need to reply as this is an async message. */
417 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
418 return;
421 if (fsp->oplock_type == NO_OPLOCK) {
422 /* We already got a "break to none" message and we've handled it.
423 * just ignore. */
424 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
425 return;
428 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
429 /* Don't tell the client, just downgrade. */
430 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
431 remove_oplock(fsp);
432 return;
435 /* Ensure we're really at level2 state. */
436 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
438 /* Now send a break to none message to our client. */
440 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
441 if (break_msg == NULL) {
442 exit_server("Could not talloc break_msg\n");
445 /* Need to wait before sending a break message if we sent ourselves this message. */
446 if (procid_to_pid(&src) == sys_getpid()) {
447 wait_before_sending_break();
450 /* Save the server smb signing state. */
451 sign_state = srv_oplock_set_signing(False);
453 show_msg(break_msg);
454 if (!srv_send_smb(smbd_server_fd(),
455 break_msg,
456 IS_CONN_ENCRYPTED(fsp->conn))) {
457 exit_server_cleanly("oplock_break: srv_send_smb failed.");
460 /* Restore the sign state to what it was. */
461 srv_oplock_set_signing(sign_state);
463 TALLOC_FREE(break_msg);
465 /* Async level2 request, don't send a reply, just remove the oplock. */
466 remove_oplock(fsp);
469 /*******************************************************************
470 This handles the generic oplock break message from another smbd.
471 *******************************************************************/
473 static void process_oplock_break_message(struct messaging_context *msg_ctx,
474 void *private_data,
475 uint32_t msg_type,
476 struct server_id src,
477 DATA_BLOB *data)
479 struct share_mode_entry msg;
480 files_struct *fsp;
481 char *break_msg;
482 bool break_to_level2 = False;
483 bool sign_state;
485 if (data->data == NULL) {
486 DEBUG(0, ("Got NULL buffer\n"));
487 return;
490 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
491 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
492 return;
495 /* De-linearize incoming message. */
496 message_to_share_mode_entry(&msg, (char *)data->data);
498 DEBUG(10, ("Got oplock break message from pid %d: %s/%lu\n",
499 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
501 fsp = initial_break_processing(msg.id, msg.share_file_id);
503 if (fsp == NULL) {
504 /* a We hit race here. Break messages are sent, and before we
505 * get to process this message, we have closed the file. Reply
506 * with 'ok, oplock broken' */
507 DEBUG(3, ("Did not find fsp\n"));
509 /* We just send the same message back. */
510 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
511 (uint8 *)data->data,
512 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
513 return;
516 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
517 /* Remember we have to inform the requesting PID when the
518 * client replies */
519 msg.pid = src;
520 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
521 &fsp->pending_break_messages,
522 &fsp->num_pending_break_messages);
523 return;
526 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
527 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
528 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
529 file_id_string_tos(&fsp->file_id),
530 fsp->fsp_name));
531 /* We just send the same message back. */
532 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
533 (uint8 *)data->data,
534 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
535 return;
538 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
539 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
540 !koplocks && /* NOTE: we force levelII off for kernel oplocks -
541 * this will change when it is supported */
542 lp_level2_oplocks(SNUM(fsp->conn))) {
543 break_to_level2 = True;
546 break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
547 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
548 if (break_msg == NULL) {
549 exit_server("Could not talloc break_msg\n");
552 /* Need to wait before sending a break message if we sent ourselves this message. */
553 if (procid_to_pid(&src) == sys_getpid()) {
554 wait_before_sending_break();
557 /* Save the server smb signing state. */
558 sign_state = srv_oplock_set_signing(False);
560 show_msg(break_msg);
561 if (!srv_send_smb(smbd_server_fd(),
562 break_msg,
563 IS_CONN_ENCRYPTED(fsp->conn))) {
564 exit_server_cleanly("oplock_break: srv_send_smb failed.");
567 /* Restore the sign state to what it was. */
568 srv_oplock_set_signing(sign_state);
570 TALLOC_FREE(break_msg);
572 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
574 msg.pid = src;
575 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
576 &fsp->pending_break_messages,
577 &fsp->num_pending_break_messages);
579 add_oplock_timeout_handler(fsp);
582 /*******************************************************************
583 This handles the kernel oplock break message.
584 *******************************************************************/
586 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
587 void *private_data,
588 uint32_t msg_type,
589 struct server_id src,
590 DATA_BLOB *data)
592 struct file_id id;
593 unsigned long file_id;
594 files_struct *fsp;
595 char *break_msg;
596 bool sign_state;
598 if (data->data == NULL) {
599 DEBUG(0, ("Got NULL buffer\n"));
600 return;
603 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
604 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
605 return;
608 /* Pull the data from the message. */
609 pull_file_id_16((char *)data->data, &id);
610 file_id = (unsigned long)IVAL(data->data, 16);
612 DEBUG(10, ("Got kernel oplock break message from pid %d: %s/%u\n",
613 (int)procid_to_pid(&src), file_id_string_tos(&id),
614 (unsigned int)file_id));
616 fsp = initial_break_processing(id, file_id);
618 if (fsp == NULL) {
619 DEBUG(3, ("Got a kernel oplock break message for a file "
620 "I don't know about\n"));
621 return;
624 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
625 /* This is ok, kernel oplocks come in completely async */
626 DEBUG(3, ("Got a kernel oplock request while waiting for a "
627 "break reply\n"));
628 return;
631 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
632 if (break_msg == NULL) {
633 exit_server("Could not talloc break_msg\n");
636 /* Save the server smb signing state. */
637 sign_state = srv_oplock_set_signing(False);
639 show_msg(break_msg);
640 if (!srv_send_smb(smbd_server_fd(),
641 break_msg,
642 IS_CONN_ENCRYPTED(fsp->conn))) {
643 exit_server_cleanly("oplock_break: srv_send_smb failed.");
646 /* Restore the sign state to what it was. */
647 srv_oplock_set_signing(sign_state);
649 TALLOC_FREE(break_msg);
651 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
653 add_oplock_timeout_handler(fsp);
656 void reply_to_oplock_break_requests(files_struct *fsp)
658 int i;
660 for (i=0; i<fsp->num_pending_break_messages; i++) {
661 struct share_mode_entry *e = &fsp->pending_break_messages[i];
662 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
664 share_mode_entry_to_message(msg, e);
666 messaging_send_buf(smbd_messaging_context(), e->pid,
667 MSG_SMB_BREAK_RESPONSE,
668 (uint8 *)msg,
669 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
672 SAFE_FREE(fsp->pending_break_messages);
673 fsp->num_pending_break_messages = 0;
674 if (fsp->oplock_timeout != NULL) {
675 /* Remove the timed event handler. */
676 TALLOC_FREE(fsp->oplock_timeout);
677 fsp->oplock_timeout = NULL;
679 return;
682 static void process_oplock_break_response(struct messaging_context *msg_ctx,
683 void *private_data,
684 uint32_t msg_type,
685 struct server_id src,
686 DATA_BLOB *data)
688 struct share_mode_entry msg;
690 if (data->data == NULL) {
691 DEBUG(0, ("Got NULL buffer\n"));
692 return;
695 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
696 DEBUG(0, ("Got invalid msg len %u\n",
697 (unsigned int)data->length));
698 return;
701 /* De-linearize incoming message. */
702 message_to_share_mode_entry(&msg, (char *)data->data);
704 DEBUG(10, ("Got oplock break response from pid %d: %s/%lu mid %u\n",
705 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id,
706 (unsigned int)msg.op_mid));
708 /* Here's the hack from open.c, store the mid in the 'port' field */
709 schedule_deferred_open_smb_message(msg.op_mid);
712 static void process_open_retry_message(struct messaging_context *msg_ctx,
713 void *private_data,
714 uint32_t msg_type,
715 struct server_id src,
716 DATA_BLOB *data)
718 struct share_mode_entry msg;
720 if (data->data == NULL) {
721 DEBUG(0, ("Got NULL buffer\n"));
722 return;
725 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
726 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
727 return;
730 /* De-linearize incoming message. */
731 message_to_share_mode_entry(&msg, (char *)data->data);
733 DEBUG(10, ("Got open retry msg from pid %d: %s mid %u\n",
734 (int)procid_to_pid(&src), file_id_string_tos(&msg.id),
735 (unsigned int)msg.op_mid));
737 schedule_deferred_open_smb_message(msg.op_mid);
740 /****************************************************************************
741 This function is called on any file modification or lock request. If a file
742 is level 2 oplocked then it must tell all other level 2 holders to break to
743 none.
744 ****************************************************************************/
746 void release_level_2_oplocks_on_change(files_struct *fsp)
748 int i;
749 struct share_mode_lock *lck;
752 * If this file is level II oplocked then we need
753 * to grab the shared memory lock and inform all
754 * other files with a level II lock that they need
755 * to flush their read caches. We keep the lock over
756 * the shared memory area whilst doing this.
759 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
760 return;
762 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
763 NULL);
764 if (lck == NULL) {
765 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
766 "share mode entry for file %s.\n", fsp->fsp_name ));
767 return;
770 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
771 lck->num_share_modes ));
773 for(i = 0; i < lck->num_share_modes; i++) {
774 struct share_mode_entry *share_entry = &lck->share_modes[i];
775 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
777 if (!is_valid_share_mode_entry(share_entry)) {
778 continue;
782 * As there could have been multiple writes waiting at the
783 * lock_share_entry gate we may not be the first to
784 * enter. Hence the state of the op_types in the share mode
785 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
786 * oplock. It will do no harm to re-send break messages to
787 * those smbd's that are still waiting their turn to remove
788 * their LEVEL_II state, and also no harm to ignore existing
789 * NO_OPLOCK states. JRA.
792 DEBUG(10,("release_level_2_oplocks_on_change: "
793 "share_entry[%i]->op_type == %d\n",
794 i, share_entry->op_type ));
796 if (share_entry->op_type == NO_OPLOCK) {
797 continue;
800 /* Paranoia .... */
801 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
802 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
803 "share mode entry %d is an exlusive "
804 "oplock !\n", i ));
805 TALLOC_FREE(lck);
806 abort();
809 share_mode_entry_to_message(msg, share_entry);
811 messaging_send_buf(smbd_messaging_context(), share_entry->pid,
812 MSG_SMB_ASYNC_LEVEL2_BREAK,
813 (uint8 *)msg,
814 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
817 /* We let the message receivers handle removing the oplock state
818 in the share mode lock db. */
820 TALLOC_FREE(lck);
823 /****************************************************************************
824 Linearize a share mode entry struct to an internal oplock break message.
825 ****************************************************************************/
827 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
829 SIVAL(msg,0,(uint32)e->pid.pid);
830 SSVAL(msg,4,e->op_mid);
831 SSVAL(msg,6,e->op_type);
832 SIVAL(msg,8,e->access_mask);
833 SIVAL(msg,12,e->share_access);
834 SIVAL(msg,16,e->private_options);
835 SIVAL(msg,20,(uint32)e->time.tv_sec);
836 SIVAL(msg,24,(uint32)e->time.tv_usec);
837 push_file_id_16(msg+28, &e->id);
838 SIVAL(msg,44,e->share_file_id);
839 SIVAL(msg,48,e->uid);
840 SSVAL(msg,52,e->flags);
841 #ifdef CLUSTER_SUPPORT
842 SIVAL(msg,54,e->pid.vnn);
843 #endif
846 /****************************************************************************
847 De-linearize an internal oplock break message to a share mode entry struct.
848 ****************************************************************************/
850 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
852 e->pid.pid = (pid_t)IVAL(msg,0);
853 e->op_mid = SVAL(msg,4);
854 e->op_type = SVAL(msg,6);
855 e->access_mask = IVAL(msg,8);
856 e->share_access = IVAL(msg,12);
857 e->private_options = IVAL(msg,16);
858 e->time.tv_sec = (time_t)IVAL(msg,20);
859 e->time.tv_usec = (int)IVAL(msg,24);
860 pull_file_id_16(msg+28, &e->id);
861 e->share_file_id = (unsigned long)IVAL(msg,44);
862 e->uid = (uint32)IVAL(msg,48);
863 e->flags = (uint16)SVAL(msg,52);
864 #ifdef CLUSTER_SUPPORT
865 e->pid.vnn = IVAL(msg,54);
866 #endif
869 /****************************************************************************
870 Setup oplocks for this process.
871 ****************************************************************************/
873 bool init_oplocks(struct messaging_context *msg_ctx)
875 DEBUG(3,("init_oplocks: initializing messages.\n"));
877 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
878 process_oplock_break_message);
879 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
880 process_oplock_async_level2_break_message);
881 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
882 process_oplock_break_response);
883 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
884 process_kernel_oplock_break);
885 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
886 process_open_retry_message);
888 if (lp_kernel_oplocks()) {
889 #if HAVE_KERNEL_OPLOCKS_IRIX
890 koplocks = irix_init_kernel_oplocks();
891 #elif HAVE_KERNEL_OPLOCKS_LINUX
892 koplocks = linux_init_kernel_oplocks();
893 #endif
896 return True;