Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / smbd / oplock.c
blobe4f5c434b092ae77da0ee14dccb675b65b4a89aa
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 #define DBGC_CLASS DBGC_LOCKING
24 #include "includes.h"
26 /* Current number of oplocks we have outstanding. */
27 static int32 exclusive_oplocks_open = 0;
28 static int32 level_II_oplocks_open = 0;
29 BOOL global_client_failed_oplock_break = False;
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->fh->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, 0x%x/%.0f/%lu, "
123 "tv_sec = %x, tv_usec = %x\n",
124 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode,
125 fsp->fh->file_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 fsp->oplock_type = NO_OPLOCK;
153 fsp->sent_oplock_break = NO_BREAK_SENT;
155 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
158 /****************************************************************************
159 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
160 ****************************************************************************/
162 static void downgrade_file_oplock(files_struct *fsp)
164 if (koplocks) {
165 koplocks->release_oplock(fsp);
167 fsp->oplock_type = LEVEL_II_OPLOCK;
168 exclusive_oplocks_open--;
169 level_II_oplocks_open++;
170 fsp->sent_oplock_break = NO_BREAK_SENT;
173 /****************************************************************************
174 Remove a file oplock. Copes with level II and exclusive.
175 Locks then unlocks the share mode lock. Client can decide to go directly
176 to none even if a "break-to-level II" was sent.
177 ****************************************************************************/
179 BOOL remove_oplock(files_struct *fsp)
181 SMB_DEV_T dev = fsp->dev;
182 SMB_INO_T inode = fsp->inode;
183 BOOL ret;
184 struct share_mode_lock *lck;
186 /* Remove the oplock flag from the sharemode. */
187 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
188 if (lck == NULL) {
189 DEBUG(0,("remove_oplock: failed to lock share entry for "
190 "file %s\n", fsp->fsp_name ));
191 return False;
193 ret = remove_share_oplock(lck, fsp);
194 if (!ret) {
195 DEBUG(0,("remove_oplock: failed to remove share oplock for "
196 "file %s fnum %d, 0x%x/%.0f\n",
197 fsp->fsp_name, fsp->fnum, (unsigned int)dev,
198 (double)inode));
200 release_file_oplock(fsp);
201 TALLOC_FREE(lck);
202 return ret;
206 * Deal with a reply when a break-to-level II was sent.
208 BOOL downgrade_oplock(files_struct *fsp)
210 SMB_DEV_T dev = fsp->dev;
211 SMB_INO_T inode = fsp->inode;
212 BOOL ret;
213 struct share_mode_lock *lck;
215 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
216 if (lck == NULL) {
217 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
218 "file %s\n", fsp->fsp_name ));
219 return False;
221 ret = downgrade_share_oplock(lck, fsp);
222 if (!ret) {
223 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
224 "for file %s fnum %d, dev = %x, inode = %.0f\n",
225 fsp->fsp_name, fsp->fnum, (unsigned int)dev,
226 (double)inode));
229 downgrade_file_oplock(fsp);
230 TALLOC_FREE(lck);
231 return ret;
234 /****************************************************************************
235 Return the fd (if any) used for receiving oplock notifications.
236 ****************************************************************************/
238 int oplock_notify_fd(void)
240 if (koplocks) {
241 return koplocks->notification_fd;
244 return -1;
247 /****************************************************************************
248 Set up an oplock break message.
249 ****************************************************************************/
251 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
252 files_struct *fsp, uint8 cmd)
254 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
256 if (result == NULL) {
257 DEBUG(0, ("talloc failed\n"));
258 return NULL;
261 memset(result,'\0',smb_size);
262 set_message(result,8,0,True);
263 SCVAL(result,smb_com,SMBlockingX);
264 SSVAL(result,smb_tid,fsp->conn->cnum);
265 SSVAL(result,smb_pid,0xFFFF);
266 SSVAL(result,smb_uid,0);
267 SSVAL(result,smb_mid,0xFFFF);
268 SCVAL(result,smb_vwv0,0xFF);
269 SSVAL(result,smb_vwv2,fsp->fnum);
270 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
271 SCVAL(result,smb_vwv3+1,cmd);
272 return result;
275 /****************************************************************************
276 Function to do the waiting before sending a local break.
277 ****************************************************************************/
279 static void wait_before_sending_break(void)
281 long wait_time = (long)lp_oplock_break_wait_time();
283 if (wait_time) {
284 smb_msleep(wait_time);
288 /****************************************************************************
289 Ensure that we have a valid oplock.
290 ****************************************************************************/
292 static files_struct *initial_break_processing(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
294 files_struct *fsp = NULL;
296 if( DEBUGLVL( 3 ) ) {
297 dbgtext( "initial_break_processing: called for 0x%x/%.0f/%u\n",
298 (unsigned int)dev, (double)inode, (int)file_id);
299 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
300 exclusive_oplocks_open, level_II_oplocks_open );
304 * We need to search the file open table for the
305 * entry containing this dev and inode, and ensure
306 * we have an oplock on it.
309 fsp = file_find_dif(dev, inode, file_id);
311 if(fsp == NULL) {
312 /* The file could have been closed in the meantime - return success. */
313 if( DEBUGLVL( 3 ) ) {
314 dbgtext( "initial_break_processing: cannot find open file with " );
315 dbgtext( "dev = 0x%x, inode = %.0f file_id = %lu", (unsigned int)dev,
316 (double)inode, file_id);
317 dbgtext( "allowing break to succeed.\n" );
319 return NULL;
322 /* Ensure we have an oplock on the file */
325 * There is a potential race condition in that an oplock could
326 * have been broken due to another udp request, and yet there are
327 * still oplock break messages being sent in the udp message
328 * queue for this file. So return true if we don't have an oplock,
329 * as we may have just freed it.
332 if(fsp->oplock_type == NO_OPLOCK) {
333 if( DEBUGLVL( 3 ) ) {
334 dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
335 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu) has no oplock.\n",
336 (unsigned int)dev, (double)inode, fsp->fh->file_id );
337 dbgtext( "Allowing break to succeed regardless.\n" );
339 return NULL;
342 return fsp;
345 static void oplock_timeout_handler(struct timed_event *te,
346 const struct timeval *now,
347 void *private_data)
349 files_struct *fsp = private_data;
351 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
352 global_client_failed_oplock_break = True;
353 remove_oplock(fsp);
354 reply_to_oplock_break_requests(fsp);
357 /*******************************************************************
358 Add a timeout handler waiting for the client reply.
359 *******************************************************************/
361 static void add_oplock_timeout_handler(files_struct *fsp)
363 if (fsp->oplock_timeout != NULL) {
364 DEBUG(0, ("Logic problem -- have an oplock event hanging "
365 "around\n"));
368 fsp->oplock_timeout =
369 add_timed_event(NULL,
370 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
371 "oplock_timeout_handler",
372 oplock_timeout_handler, fsp);
374 if (fsp->oplock_timeout == NULL) {
375 DEBUG(0, ("Could not add oplock timeout handler\n"));
379 /*******************************************************************
380 This handles the case of a write triggering a break to none
381 message on a level2 oplock.
382 When we get this message we may be in any of three states :
383 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
384 the client for LEVEL2.
385 *******************************************************************/
387 static void process_oplock_async_level2_break_message(int msg_type, struct process_id src,
388 void *buf, size_t len)
390 struct share_mode_entry msg;
391 files_struct *fsp;
392 char *break_msg;
393 BOOL sign_state;
395 if (buf == NULL) {
396 DEBUG(0, ("Got NULL buffer\n"));
397 return;
400 if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
401 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
402 return;
405 /* De-linearize incoming message. */
406 message_to_share_mode_entry(&msg, buf);
408 DEBUG(10, ("Got oplock async level 2 break message from pid %d: 0x%x/%.0f/%lu\n",
409 (int)procid_to_pid(&src), (unsigned int)msg.dev,
410 (double)msg.inode, msg.share_file_id));
412 fsp = initial_break_processing(msg.dev, msg.inode,
413 msg.share_file_id);
415 if (fsp == NULL) {
416 /* We hit a race here. Break messages are sent, and before we
417 * get to process this message, we have closed the file.
418 * No need to reply as this is an async message. */
419 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
420 return;
423 if (fsp->oplock_type == NO_OPLOCK) {
424 /* We already got a "break to none" message and we've handled it.
425 * just ignore. */
426 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
427 return;
430 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
431 /* Don't tell the client, just downgrade. */
432 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
433 remove_oplock(fsp);
434 return;
437 /* Ensure we're really at level2 state. */
438 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
440 /* Now send a break to none message to our client. */
442 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
443 if (break_msg == NULL) {
444 exit_server("Could not talloc break_msg\n");
447 /* Need to wait before sending a break message if we sent ourselves this message. */
448 if (procid_to_pid(&src) == sys_getpid()) {
449 wait_before_sending_break();
452 /* Save the server smb signing state. */
453 sign_state = srv_oplock_set_signing(False);
455 show_msg(break_msg);
456 if (!send_smb(smbd_server_fd(), break_msg)) {
457 exit_server("oplock_break: 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(int msg_type, struct process_id src,
474 void *buf, size_t len)
476 struct share_mode_entry msg;
477 files_struct *fsp;
478 char *break_msg;
479 BOOL break_to_level2 = False;
480 BOOL sign_state;
482 if (buf == NULL) {
483 DEBUG(0, ("Got NULL buffer\n"));
484 return;
487 if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
488 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
489 return;
492 /* De-linearize incoming message. */
493 message_to_share_mode_entry(&msg, buf);
495 DEBUG(10, ("Got oplock break message from pid %d: 0x%x/%.0f/%lu\n",
496 (int)procid_to_pid(&src), (unsigned int)msg.dev,
497 (double)msg.inode, msg.share_file_id));
499 fsp = initial_break_processing(msg.dev, msg.inode,
500 msg.share_file_id);
502 if (fsp == NULL) {
503 /* a We hit race here. Break messages are sent, and before we
504 * get to process this message, we have closed the file. Reply
505 * with 'ok, oplock broken' */
506 DEBUG(3, ("Did not find fsp\n"));
507 become_root();
509 /* We just send the same message back. */
510 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
511 buf, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
513 unbecome_root();
514 return;
517 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
518 /* Remember we have to inform the requesting PID when the
519 * client replies */
520 msg.pid = src;
521 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
522 &fsp->pending_break_messages,
523 &fsp->num_pending_break_messages);
524 return;
527 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
528 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
529 DEBUG(3, ("Already downgraded oplock on 0x%x/%.0f: %s\n",
530 (unsigned int)fsp->dev, (double)fsp->inode,
531 fsp->fsp_name));
532 become_root();
534 /* We just send the same message back. */
535 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
536 buf, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
538 unbecome_root();
539 return;
542 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
543 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
544 !koplocks && /* NOTE: we force levelII off for kernel oplocks -
545 * this will change when it is supported */
546 lp_level2_oplocks(SNUM(fsp->conn))) {
547 break_to_level2 = True;
550 break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
551 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
552 if (break_msg == NULL) {
553 exit_server("Could not talloc break_msg\n");
556 /* Need to wait before sending a break message if we sent ourselves this message. */
557 if (procid_to_pid(&src) == sys_getpid()) {
558 wait_before_sending_break();
561 /* Save the server smb signing state. */
562 sign_state = srv_oplock_set_signing(False);
564 show_msg(break_msg);
565 if (!send_smb(smbd_server_fd(), break_msg)) {
566 exit_server("oplock_break: send_smb failed.");
569 /* Restore the sign state to what it was. */
570 srv_oplock_set_signing(sign_state);
572 TALLOC_FREE(break_msg);
574 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
576 msg.pid = src;
577 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
578 &fsp->pending_break_messages,
579 &fsp->num_pending_break_messages);
581 add_oplock_timeout_handler(fsp);
584 /*******************************************************************
585 This handles the kernel oplock break message.
586 *******************************************************************/
588 static void process_kernel_oplock_break(int msg_type, struct process_id src,
589 void *buf, size_t len)
591 SMB_DEV_T dev;
592 SMB_INO_T inode;
593 unsigned long file_id;
594 files_struct *fsp;
595 char *break_msg;
596 BOOL sign_state;
598 if (buf == NULL) {
599 DEBUG(0, ("Got NULL buffer\n"));
600 return;
603 if (len != MSG_SMB_KERNEL_BREAK_SIZE) {
604 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
605 return;
608 /* Pull the data from the message. */
609 dev = DEV_T_VAL(buf, 0);
610 inode = INO_T_VAL(buf, 8);
611 file_id = (unsigned long)IVAL(buf, 16);
613 DEBUG(10, ("Got kernel oplock break message from pid %d: 0x%x/%.0f/%u\n",
614 (int)procid_to_pid(&src), (unsigned int)dev, (double)inode,
615 (unsigned int)file_id));
617 fsp = initial_break_processing(dev, inode, file_id);
619 if (fsp == NULL) {
620 DEBUG(3, ("Got a kernel oplock break message for a file "
621 "I don't know about\n"));
622 return;
625 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
626 /* This is ok, kernel oplocks come in completely async */
627 DEBUG(3, ("Got a kernel oplock request while waiting for a "
628 "break reply\n"));
629 return;
632 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
633 if (break_msg == NULL) {
634 exit_server("Could not talloc break_msg\n");
637 /* Save the server smb signing state. */
638 sign_state = srv_oplock_set_signing(False);
640 show_msg(break_msg);
641 if (!send_smb(smbd_server_fd(), break_msg)) {
642 exit_server("oplock_break: send_smb failed.");
645 /* Restore the sign state to what it was. */
646 srv_oplock_set_signing(sign_state);
648 TALLOC_FREE(break_msg);
650 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
652 add_oplock_timeout_handler(fsp);
655 void reply_to_oplock_break_requests(files_struct *fsp)
657 int i;
659 become_root();
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 message_send_pid(e->pid, MSG_SMB_BREAK_RESPONSE,
667 msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
669 unbecome_root();
671 SAFE_FREE(fsp->pending_break_messages);
672 fsp->num_pending_break_messages = 0;
673 if (fsp->oplock_timeout != NULL) {
674 /* Remove the timed event handler. */
675 TALLOC_FREE(fsp->oplock_timeout);
676 fsp->oplock_timeout = NULL;
678 return;
681 static void process_oplock_break_response(int msg_type, struct process_id src,
682 void *buf, size_t len)
684 struct share_mode_entry msg;
686 if (buf == NULL) {
687 DEBUG(0, ("Got NULL buffer\n"));
688 return;
691 if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
692 DEBUG(0, ("Got invalid msg len %u\n", (unsigned int)len));
693 return;
696 /* De-linearize incoming message. */
697 message_to_share_mode_entry(&msg, buf);
699 DEBUG(10, ("Got oplock break response from pid %d: 0x%x/%.0f/%lu mid %u\n",
700 (int)procid_to_pid(&src), (unsigned int)msg.dev,
701 (double)msg.inode, msg.share_file_id,
702 (unsigned int)msg.op_mid));
704 /* Here's the hack from open.c, store the mid in the 'port' field */
705 schedule_deferred_open_smb_message(msg.op_mid);
708 static void process_open_retry_message(int msg_type, struct process_id src,
709 void *buf, size_t len)
711 struct share_mode_entry msg;
713 if (buf == NULL) {
714 DEBUG(0, ("Got NULL buffer\n"));
715 return;
718 if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
719 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
720 return;
723 /* De-linearize incoming message. */
724 message_to_share_mode_entry(&msg, buf);
726 DEBUG(10, ("Got open retry msg from pid %d: 0x%x/%.0f/%lu mid %u\n",
727 (int)procid_to_pid(&src), (unsigned int)msg.dev,
728 (double)msg.inode, msg.share_file_id,
729 (unsigned int)msg.op_mid));
731 schedule_deferred_open_smb_message(msg.op_mid);
734 /****************************************************************************
735 This function is called on any file modification or lock request. If a file
736 is level 2 oplocked then it must tell all other level 2 holders to break to
737 none.
738 ****************************************************************************/
740 void release_level_2_oplocks_on_change(files_struct *fsp)
742 int i;
743 struct share_mode_lock *lck;
746 * If this file is level II oplocked then we need
747 * to grab the shared memory lock and inform all
748 * other files with a level II lock that they need
749 * to flush their read caches. We keep the lock over
750 * the shared memory area whilst doing this.
753 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
754 return;
756 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
757 if (lck == NULL) {
758 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
759 "share mode entry for file %s.\n", fsp->fsp_name ));
760 return;
763 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
764 lck->num_share_modes ));
766 for(i = 0; i < lck->num_share_modes; i++) {
767 struct share_mode_entry *share_entry = &lck->share_modes[i];
768 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
770 if (!is_valid_share_mode_entry(share_entry)) {
771 continue;
775 * As there could have been multiple writes waiting at the
776 * lock_share_entry gate we may not be the first to
777 * enter. Hence the state of the op_types in the share mode
778 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
779 * oplock. It will do no harm to re-send break messages to
780 * those smbd's that are still waiting their turn to remove
781 * their LEVEL_II state, and also no harm to ignore existing
782 * NO_OPLOCK states. JRA.
785 DEBUG(10,("release_level_2_oplocks_on_change: "
786 "share_entry[%i]->op_type == %d\n",
787 i, share_entry->op_type ));
789 if (share_entry->op_type == NO_OPLOCK) {
790 continue;
793 /* Paranoia .... */
794 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
795 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
796 "share mode entry %d is an exlusive "
797 "oplock !\n", i ));
798 TALLOC_FREE(lck);
799 abort();
802 share_mode_entry_to_message(msg, share_entry);
804 become_root();
805 message_send_pid(share_entry->pid, MSG_SMB_ASYNC_LEVEL2_BREAK,
806 msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
807 unbecome_root();
810 /* We let the message receivers handle removing the oplock state
811 in the share mode lock db. */
813 TALLOC_FREE(lck);
816 /****************************************************************************
817 Linearize a share mode entry struct to an internal oplock break message.
818 ****************************************************************************/
820 void share_mode_entry_to_message(char *msg, struct share_mode_entry *e)
822 SIVAL(msg,0,(uint32)e->pid.pid);
823 SSVAL(msg,4,e->op_mid);
824 SSVAL(msg,6,e->op_type);
825 SIVAL(msg,8,e->access_mask);
826 SIVAL(msg,12,e->share_access);
827 SIVAL(msg,16,e->private_options);
828 SIVAL(msg,20,(uint32)e->time.tv_sec);
829 SIVAL(msg,24,(uint32)e->time.tv_usec);
830 SDEV_T_VAL(msg,28,e->dev);
831 SINO_T_VAL(msg,36,e->inode);
832 SIVAL(msg,44,e->share_file_id);
833 SIVAL(msg,48,e->uid);
836 /****************************************************************************
837 De-linearize an internal oplock break message to a share mode entry struct.
838 ****************************************************************************/
840 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
842 e->pid.pid = (pid_t)IVAL(msg,0);
843 e->op_mid = SVAL(msg,4);
844 e->op_type = SVAL(msg,6);
845 e->access_mask = IVAL(msg,8);
846 e->share_access = IVAL(msg,12);
847 e->private_options = IVAL(msg,16);
848 e->time.tv_sec = (time_t)IVAL(msg,20);
849 e->time.tv_usec = (int)IVAL(msg,24);
850 e->dev = DEV_T_VAL(msg,28);
851 e->inode = INO_T_VAL(msg,36);
852 e->share_file_id = (unsigned long)IVAL(msg,44);
853 e->uid = (uint32)IVAL(msg,48);
856 /****************************************************************************
857 Setup oplocks for this process.
858 ****************************************************************************/
860 BOOL init_oplocks(void)
862 DEBUG(3,("open_oplock_ipc: initializing messages.\n"));
864 message_register(MSG_SMB_BREAK_REQUEST,
865 process_oplock_break_message);
866 message_register(MSG_SMB_ASYNC_LEVEL2_BREAK,
867 process_oplock_async_level2_break_message);
868 message_register(MSG_SMB_BREAK_RESPONSE,
869 process_oplock_break_response);
870 message_register(MSG_SMB_KERNEL_BREAK,
871 process_kernel_oplock_break);
872 message_register(MSG_SMB_OPEN_RETRY,
873 process_open_retry_message);
875 if (lp_kernel_oplocks()) {
876 #if HAVE_KERNEL_OPLOCKS_IRIX
877 koplocks = irix_init_kernel_oplocks();
878 #elif HAVE_KERNEL_OPLOCKS_LINUX
879 koplocks = linux_init_kernel_oplocks();
880 #endif
883 return True;