r14507: Re-disable accidentially re-enabled paranoia check. This should make
[Samba/gebeck_regimport.git] / source / smbd / oplock.c
blob61e812d43a8ac4406213b7fd388528cfbd0256fd
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 struct timeval smb_last_time;
31 extern uint32 global_client_caps;
32 extern int smb_read_error;
34 static struct kernel_oplocks *koplocks;
36 /****************************************************************************
37 Get the number of current exclusive oplocks.
38 ****************************************************************************/
40 int32 get_number_of_exclusive_open_oplocks(void)
42 return exclusive_oplocks_open;
45 /****************************************************************************
46 Return True if an oplock message is pending.
47 ****************************************************************************/
49 BOOL oplock_message_waiting(fd_set *fds)
51 if (koplocks && koplocks->msg_waiting(fds)) {
52 return True;
55 return False;
58 /****************************************************************************
59 Find out if there are any kernel oplock messages waiting and process them
60 if so. pfds is the fd_set from the main select loop (which contains any
61 kernel oplock fd if that's what the system uses (IRIX). If may be NULL if
62 we're calling this in a shutting down state.
63 ****************************************************************************/
65 void process_kernel_oplocks(fd_set *pfds)
68 * We need to check for kernel oplocks before going into the select
69 * here, as the EINTR generated by the linux kernel oplock may have
70 * already been eaten. JRA.
73 if (!koplocks) {
74 return;
77 while (koplocks->msg_waiting(pfds)) {
78 files_struct *fsp;
79 char msg[MSG_SMB_KERNEL_BREAK_SIZE];
81 fsp = koplocks->receive_message(pfds);
83 if (fsp == NULL) {
84 DEBUG(3, ("Kernel oplock message announced, but none "
85 "received\n"));
86 return;
89 /* Put the kernel break info into the message. */
90 SDEV_T_VAL(msg,0,fsp->dev);
91 SINO_T_VAL(msg,8,fsp->inode);
92 SIVAL(msg,16,fsp->file_id);
94 /* Don't need to be root here as we're only ever
95 sending to ourselves. */
97 message_send_pid(pid_to_procid(sys_getpid()),
98 MSG_SMB_KERNEL_BREAK,
99 &msg, MSG_SMB_KERNEL_BREAK_SIZE, True);
103 /****************************************************************************
104 Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
105 disabled (just sets flags). Returns True if oplock set.
106 ****************************************************************************/
108 BOOL set_file_oplock(files_struct *fsp, int oplock_type)
110 if (koplocks && !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 {
119 exclusive_oplocks_open++;
122 DEBUG(5,("set_file_oplock: granted oplock on file %s, dev = %x, inode = %.0f, file_id = %lu, \
123 tv_sec = %x, tv_usec = %x\n",
124 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id,
125 (int)fsp->open_time.tv_sec, (int)fsp->open_time.tv_usec ));
127 return True;
130 /****************************************************************************
131 Attempt to release an oplock on a file. Decrements oplock count.
132 ****************************************************************************/
134 void release_file_oplock(files_struct *fsp)
136 if ((fsp->oplock_type != NO_OPLOCK) &&
137 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
138 koplocks) {
139 koplocks->release_oplock(fsp);
142 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
143 level_II_oplocks_open--;
144 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
145 exclusive_oplocks_open--;
148 SMB_ASSERT(exclusive_oplocks_open>=0);
149 SMB_ASSERT(level_II_oplocks_open>=0);
151 fsp->oplock_type = NO_OPLOCK;
152 fsp->sent_oplock_break = NO_BREAK_SENT;
154 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
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 SMB_DEV_T dev = fsp->dev;
181 SMB_INO_T inode = fsp->inode;
182 BOOL ret;
183 struct share_mode_lock *lck;
185 /* Remove the oplock flag from the sharemode. */
186 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
187 if (lck == NULL) {
188 DEBUG(0,("remove_oplock: failed to lock share entry for "
189 "file %s\n", fsp->fsp_name ));
190 return False;
192 ret = remove_share_oplock(lck, fsp);
193 if (!ret) {
194 DEBUG(0,("remove_oplock: failed to remove share oplock for "
195 "file %s fnum %d, dev = %x, inode = %.0f\n",
196 fsp->fsp_name, fsp->fnum, (unsigned int)dev,
197 (double)inode));
199 release_file_oplock(fsp);
200 TALLOC_FREE(lck);
201 return ret;
205 * Deal with a reply when a break-to-level II was sent.
207 BOOL downgrade_oplock(files_struct *fsp)
209 SMB_DEV_T dev = fsp->dev;
210 SMB_INO_T inode = fsp->inode;
211 BOOL ret;
212 struct share_mode_lock *lck;
214 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
215 if (lck == NULL) {
216 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
217 "file %s\n", fsp->fsp_name ));
218 return False;
220 ret = downgrade_share_oplock(lck, fsp);
221 if (!ret) {
222 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
223 "for file %s fnum %d, dev = %x, inode = %.0f\n",
224 fsp->fsp_name, fsp->fnum, (unsigned int)dev,
225 (double)inode));
228 downgrade_file_oplock(fsp);
229 TALLOC_FREE(lck);
230 return ret;
233 /****************************************************************************
234 Return the fd (if any) used for receiving oplock notifications.
235 ****************************************************************************/
237 int oplock_notify_fd(void)
239 if (koplocks) {
240 return koplocks->notification_fd;
243 return -1;
246 /****************************************************************************
247 Set up an oplock break message.
248 ****************************************************************************/
250 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
251 files_struct *fsp, uint8 cmd)
253 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
255 if (result == NULL) {
256 DEBUG(0, ("talloc failed\n"));
257 return NULL;
260 memset(result,'\0',smb_size);
261 set_message(result,8,0,True);
262 SCVAL(result,smb_com,SMBlockingX);
263 SSVAL(result,smb_tid,fsp->conn->cnum);
264 SSVAL(result,smb_pid,0xFFFF);
265 SSVAL(result,smb_uid,0);
266 SSVAL(result,smb_mid,0xFFFF);
267 SCVAL(result,smb_vwv0,0xFF);
268 SSVAL(result,smb_vwv2,fsp->fnum);
269 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
270 SCVAL(result,smb_vwv3+1,cmd);
271 return result;
274 /****************************************************************************
275 Function to do the waiting before sending a local break.
276 ****************************************************************************/
278 static void wait_before_sending_break(void)
280 struct timeval cur_tv;
281 long wait_left = (long)lp_oplock_break_wait_time();
283 if (wait_left == 0) {
284 return;
287 GetTimeOfDay(&cur_tv);
289 wait_left -= ((cur_tv.tv_sec - smb_last_time.tv_sec)*1000) +
290 ((cur_tv.tv_usec - smb_last_time.tv_usec)/1000);
292 if(wait_left > 0) {
293 wait_left = MIN(wait_left, 1000);
294 sys_usleep(wait_left * 1000);
298 /****************************************************************************
299 Ensure that we have a valid oplock.
300 ****************************************************************************/
302 static files_struct *initial_break_processing(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
304 files_struct *fsp = NULL;
306 if( DEBUGLVL( 3 ) ) {
307 dbgtext( "initial_break_processing: called for dev = 0x%x, inode = %.0f file_id = %lu\n",
308 (unsigned int)dev, (double)inode, file_id);
309 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
310 exclusive_oplocks_open, level_II_oplocks_open );
314 * We need to search the file open table for the
315 * entry containing this dev and inode, and ensure
316 * we have an oplock on it.
319 fsp = file_find_dif(dev, inode, file_id);
321 if(fsp == NULL) {
322 /* The file could have been closed in the meantime - return success. */
323 if( DEBUGLVL( 3 ) ) {
324 dbgtext( "initial_break_processing: cannot find open file with " );
325 dbgtext( "dev = 0x%x, inode = %.0f file_id = %lu", (unsigned int)dev,
326 (double)inode, file_id);
327 dbgtext( "allowing break to succeed.\n" );
329 return NULL;
332 /* Ensure we have an oplock on the file */
335 * There is a potential race condition in that an oplock could
336 * have been broken due to another udp request, and yet there are
337 * still oplock break messages being sent in the udp message
338 * queue for this file. So return true if we don't have an oplock,
339 * as we may have just freed it.
342 if(fsp->oplock_type == NO_OPLOCK) {
343 if( DEBUGLVL( 3 ) ) {
344 dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
345 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu) has no oplock.\n",
346 (unsigned int)dev, (double)inode, fsp->file_id );
347 dbgtext( "Allowing break to succeed regardless.\n" );
349 return NULL;
352 return fsp;
355 static void oplock_timeout_handler(struct timed_event *te,
356 const struct timeval *now,
357 void *private_data)
359 files_struct *fsp = private_data;
361 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
362 global_client_failed_oplock_break = True;
363 remove_oplock(fsp);
364 reply_to_oplock_break_requests(fsp);
367 /*******************************************************************
368 Add a timeout handler waiting for the client reply.
369 *******************************************************************/
371 static void add_oplock_timeout_handler(files_struct *fsp)
373 if (fsp->oplock_timeout != NULL) {
374 DEBUG(0, ("Logic problem -- have an oplock event hanging "
375 "around\n"));
378 fsp->oplock_timeout =
379 add_timed_event(NULL,
380 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
381 "oplock_timeout_handler",
382 oplock_timeout_handler, fsp);
384 if (fsp->oplock_timeout == NULL) {
385 DEBUG(0, ("Could not add oplock timeout handler\n"));
389 /*******************************************************************
390 This handles the case of a write triggering a break to none
391 message on a level2 oplock.
392 When we get this message we may be in any of three states :
393 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
394 the client for LEVEL2.
395 *******************************************************************/
397 static void process_oplock_async_level2_break_message(int msg_type, struct process_id src,
398 void *buf, size_t len)
400 struct share_mode_entry msg;
401 files_struct *fsp;
402 char *break_msg;
403 BOOL sign_state;
405 if (buf == NULL) {
406 DEBUG(0, ("Got NULL buffer\n"));
407 return;
410 if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
411 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
412 return;
415 /* De-linearize incoming message. */
416 message_to_share_mode_entry(&msg, buf);
418 DEBUG(10, ("Got oplock async level 2 break message from pid %d: 0x%x/%.0f/%d\n",
419 (int)procid_to_pid(&src), (unsigned int)msg.dev, (double)msg.inode,
420 (int)msg.share_file_id));
422 fsp = initial_break_processing(msg.dev, msg.inode,
423 msg.share_file_id);
425 if (fsp == NULL) {
426 /* We hit a race here. Break messages are sent, and before we
427 * get to process this message, we have closed the file.
428 * No need to reply as this is an async message. */
429 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
430 return;
433 if (fsp->oplock_type == NO_OPLOCK) {
434 /* We already got a "break to none" message and we've handled it.
435 * just ignore. */
436 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
437 return;
440 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
441 /* Don't tell the client, just downgrade. */
442 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
443 remove_oplock(fsp);
444 return;
447 /* Ensure we're really at level2 state. */
448 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
450 /* Now send a break to none message to our client. */
452 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
453 if (break_msg == NULL) {
454 exit_server("Could not talloc break_msg\n");
457 /* Need to wait before sending a break message if we sent ourselves this message. */
458 if (procid_to_pid(&src) == sys_getpid()) {
459 wait_before_sending_break();
462 /* Save the server smb signing state. */
463 sign_state = srv_oplock_set_signing(False);
465 show_msg(break_msg);
466 if (!send_smb(smbd_server_fd(), break_msg)) {
467 exit_server("oplock_break: send_smb failed.");
470 /* Restore the sign state to what it was. */
471 srv_oplock_set_signing(sign_state);
473 TALLOC_FREE(break_msg);
475 /* Async level2 request, don't send a reply, just remove the oplock. */
476 remove_oplock(fsp);
479 /*******************************************************************
480 This handles the generic oplock break message from another smbd.
481 *******************************************************************/
483 static void process_oplock_break_message(int msg_type, struct process_id src,
484 void *buf, size_t len)
486 struct share_mode_entry msg;
487 files_struct *fsp;
488 char *break_msg;
489 BOOL break_to_level2 = False;
490 BOOL sign_state;
492 if (buf == NULL) {
493 DEBUG(0, ("Got NULL buffer\n"));
494 return;
497 if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
498 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
499 return;
502 /* De-linearize incoming message. */
503 message_to_share_mode_entry(&msg, buf);
505 DEBUG(10, ("Got oplock break message from pid %d: 0x%x/%.0f/%d\n",
506 (int)procid_to_pid(&src), (unsigned int)msg.dev, (double)msg.inode,
507 (int)msg.share_file_id));
509 fsp = initial_break_processing(msg.dev, msg.inode,
510 msg.share_file_id);
512 if (fsp == NULL) {
513 /* a We hit race here. Break messages are sent, and before we
514 * get to process this message, we have closed the file. Reply
515 * with 'ok, oplock broken' */
516 DEBUG(3, ("Did not find fsp\n"));
517 become_root();
519 /* We just send the same message back. */
520 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
521 buf, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
523 unbecome_root();
524 return;
527 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
528 /* Remember we have to inform the requesting PID when the
529 * client replies */
530 msg.pid = src;
531 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
532 &fsp->pending_break_messages,
533 &fsp->num_pending_break_messages);
534 return;
537 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
538 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
539 DEBUG(3, ("Already downgraded oplock on 0x%x/%.0f: %s\n",
540 (unsigned int)fsp->dev, (double)fsp->inode,
541 fsp->fsp_name));
542 become_root();
544 /* We just send the same message back. */
545 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
546 buf, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
548 unbecome_root();
549 return;
552 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
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 (!send_smb(smbd_server_fd(), break_msg)) {
575 exit_server("oplock_break: send_smb failed.");
578 /* Restore the sign state to what it was. */
579 srv_oplock_set_signing(sign_state);
581 TALLOC_FREE(break_msg);
583 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
585 msg.pid = src;
586 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
587 &fsp->pending_break_messages,
588 &fsp->num_pending_break_messages);
590 add_oplock_timeout_handler(fsp);
593 /*******************************************************************
594 This handles the kernel oplock break message.
595 *******************************************************************/
597 static void process_kernel_oplock_break(int msg_type, struct process_id src,
598 void *buf, size_t len)
600 SMB_DEV_T dev;
601 SMB_INO_T inode;
602 unsigned long file_id;
603 files_struct *fsp;
604 char *break_msg;
605 BOOL sign_state;
607 if (buf == NULL) {
608 DEBUG(0, ("Got NULL buffer\n"));
609 return;
612 if (len != MSG_SMB_KERNEL_BREAK_SIZE) {
613 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
614 return;
617 /* Pull the data from the message. */
618 dev = DEV_T_VAL(buf, 0);
619 inode = INO_T_VAL(buf, 8);
620 file_id = (unsigned long)IVAL(buf, 16);
622 DEBUG(10, ("Got kernel oplock break message from pid %d: 0x%x/%.0f/%u\n",
623 (int)procid_to_pid(&src), (unsigned int)dev, (double)inode,
624 (unsigned int)file_id));
626 fsp = initial_break_processing(dev, inode, file_id);
628 if (fsp == NULL) {
629 DEBUG(3, ("Got a kernel oplock break message for a file "
630 "I don't know about\n"));
631 return;
634 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
635 /* This is ok, kernel oplocks come in completely async */
636 DEBUG(3, ("Got a kernel oplock request while waiting for a "
637 "break reply\n"));
638 return;
641 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
642 if (break_msg == NULL) {
643 exit_server("Could not talloc break_msg\n");
646 /* Save the server smb signing state. */
647 sign_state = srv_oplock_set_signing(False);
649 show_msg(break_msg);
650 if (!send_smb(smbd_server_fd(), break_msg)) {
651 exit_server("oplock_break: send_smb failed.");
654 /* Restore the sign state to what it was. */
655 srv_oplock_set_signing(sign_state);
657 TALLOC_FREE(break_msg);
659 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
661 add_oplock_timeout_handler(fsp);
664 void reply_to_oplock_break_requests(files_struct *fsp)
666 int i;
668 become_root();
669 for (i=0; i<fsp->num_pending_break_messages; i++) {
670 struct share_mode_entry *e = &fsp->pending_break_messages[i];
671 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
673 share_mode_entry_to_message(msg, e);
675 message_send_pid(e->pid, MSG_SMB_BREAK_RESPONSE,
676 msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
678 unbecome_root();
680 SAFE_FREE(fsp->pending_break_messages);
681 fsp->num_pending_break_messages = 0;
682 if (fsp->oplock_timeout != NULL) {
683 /* Remove the timed event handler. */
684 TALLOC_FREE(fsp->oplock_timeout);
685 fsp->oplock_timeout = NULL;
687 return;
690 static void process_oplock_break_response(int msg_type, struct process_id src,
691 void *buf, size_t len)
693 struct share_mode_entry msg;
695 if (buf == NULL) {
696 DEBUG(0, ("Got NULL buffer\n"));
697 return;
700 if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
701 DEBUG(0, ("Got invalid msg len %u\n", (unsigned int)len));
702 return;
705 /* De-linearize incoming message. */
706 message_to_share_mode_entry(&msg, buf);
708 DEBUG(10, ("Got oplock break response from pid %d: 0x%x/%.0f/%u mid %u\n",
709 (int)procid_to_pid(&src), (unsigned int)msg.dev, (double)msg.inode,
710 (unsigned int)msg.share_file_id, (unsigned int)msg.op_mid));
712 /* Here's the hack from open.c, store the mid in the 'port' field */
713 schedule_deferred_open_smb_message(msg.op_mid);
716 static void process_open_retry_message(int msg_type, struct process_id src,
717 void *buf, size_t len)
719 struct share_mode_entry msg;
721 if (buf == NULL) {
722 DEBUG(0, ("Got NULL buffer\n"));
723 return;
726 if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
727 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
728 return;
731 /* De-linearize incoming message. */
732 message_to_share_mode_entry(&msg, buf);
734 DEBUG(10, ("Got open retry msg from pid %d: 0x%x/%.0f mid %u\n",
735 (int)procid_to_pid(&src), (unsigned int)msg.dev, (double)msg.inode,
736 (unsigned int)msg.op_mid));
738 schedule_deferred_open_smb_message(msg.op_mid);
741 /****************************************************************************
742 This function is called on any file modification or lock request. If a file
743 is level 2 oplocked then it must tell all other level 2 holders to break to
744 none.
745 ****************************************************************************/
747 void release_level_2_oplocks_on_change(files_struct *fsp)
749 int i;
750 struct share_mode_lock *lck;
753 * If this file is level II oplocked then we need
754 * to grab the shared memory lock and inform all
755 * other files with a level II lock that they need
756 * to flush their read caches. We keep the lock over
757 * the shared memory area whilst doing this.
760 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
761 return;
763 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, 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 become_root();
812 message_send_pid(share_entry->pid, MSG_SMB_ASYNC_LEVEL2_BREAK,
813 msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
814 unbecome_root();
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, 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 SDEV_T_VAL(msg,28,e->dev);
838 SINO_T_VAL(msg,36,e->inode);
839 SIVAL(msg,44,e->share_file_id);
842 /****************************************************************************
843 De-linearize an internal oplock break message to a share mode entry struct.
844 ****************************************************************************/
846 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
848 e->pid.pid = (pid_t)IVAL(msg,0);
849 e->op_mid = SVAL(msg,4);
850 e->op_type = SVAL(msg,6);
851 e->access_mask = IVAL(msg,8);
852 e->share_access = IVAL(msg,12);
853 e->private_options = IVAL(msg,16);
854 e->time.tv_sec = (time_t)IVAL(msg,20);
855 e->time.tv_usec = (int)IVAL(msg,24);
856 e->dev = DEV_T_VAL(msg,28);
857 e->inode = INO_T_VAL(msg,36);
858 e->share_file_id = (unsigned long)IVAL(msg,44);
861 /****************************************************************************
862 Setup oplocks for this process.
863 ****************************************************************************/
865 BOOL init_oplocks(void)
867 DEBUG(3,("open_oplock_ipc: initializing messages.\n"));
869 message_register(MSG_SMB_BREAK_REQUEST,
870 process_oplock_break_message);
871 message_register(MSG_SMB_ASYNC_LEVEL2_BREAK,
872 process_oplock_async_level2_break_message);
873 message_register(MSG_SMB_BREAK_RESPONSE,
874 process_oplock_break_response);
875 message_register(MSG_SMB_KERNEL_BREAK,
876 process_kernel_oplock_break);
877 message_register(MSG_SMB_OPEN_RETRY,
878 process_open_retry_message);
880 if (lp_kernel_oplocks()) {
881 #if HAVE_KERNEL_OPLOCKS_IRIX
882 koplocks = irix_init_kernel_oplocks();
883 #elif HAVE_KERNEL_OPLOCKS_LINUX
884 koplocks = linux_init_kernel_oplocks();
885 #endif
888 return True;