r6284: Starting on 3.0.14:
[Samba/gbeck.git] / source / smbd / oplock.c
blobb9e38e30fcc41494771195ff7a3d8a343387e575
1 /*
2 Unix SMB/CIFS implementation.
3 oplock processing
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998 - 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 /* Oplock ipc UDP socket. */
25 static int oplock_sock = -1;
26 uint16 global_oplock_port = 0;
28 /* Current number of oplocks we have outstanding. */
29 static int32 exclusive_oplocks_open = 0;
30 static int32 level_II_oplocks_open = 0;
31 BOOL global_client_failed_oplock_break = False;
32 BOOL global_oplock_break = False;
34 extern int smb_read_error;
36 static struct kernel_oplocks *koplocks;
38 static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, BOOL local);
40 /****************************************************************************
41 Get the number of current exclusive oplocks.
42 ****************************************************************************/
44 int32 get_number_of_exclusive_open_oplocks(void)
46 return exclusive_oplocks_open;
49 /****************************************************************************
50 Return True if an oplock message is pending.
51 ****************************************************************************/
53 BOOL oplock_message_waiting(fd_set *fds)
55 if (koplocks && koplocks->msg_waiting(fds))
56 return True;
58 if (FD_ISSET(oplock_sock, fds))
59 return True;
61 return False;
64 /****************************************************************************
65 Read an oplock break message from either the oplock UDP fd or the
66 kernel (if kernel oplocks are supported).
68 If timeout is zero then *fds contains the file descriptors that
69 are ready to be read and acted upon. If timeout is non-zero then
70 *fds contains the file descriptors to be selected on for read.
71 The timeout is in milliseconds
73 ****************************************************************************/
75 BOOL receive_local_message( char *buffer, int buffer_len, int timeout)
77 struct sockaddr_in from;
78 socklen_t fromlen = sizeof(from);
79 int32 msg_len = 0;
80 fd_set fds;
81 int selrtn = -1;
83 FD_ZERO(&fds);
84 smb_read_error = 0;
87 * We need to check for kernel oplocks before going into the select
88 * here, as the EINTR generated by the linux kernel oplock may have
89 * already been eaten. JRA.
92 if (koplocks && koplocks->msg_waiting(&fds)) {
93 return koplocks->receive_message(&fds, buffer, buffer_len);
96 while (timeout > 0 && selrtn == -1) {
97 struct timeval to;
98 int maxfd = oplock_sock;
99 time_t starttime = time(NULL);
101 FD_ZERO(&fds);
102 maxfd = setup_oplock_select_set(&fds);
104 to.tv_sec = timeout / 1000;
105 to.tv_usec = (timeout % 1000) * 1000;
107 DEBUG(5,("receive_local_message: doing select with timeout of %d ms\n", timeout));
109 selrtn = sys_select(maxfd+1,&fds,NULL,NULL,&to);
111 if (selrtn == -1 && errno == EINTR) {
113 /* could be a kernel oplock interrupt */
114 if (koplocks && koplocks->msg_waiting(&fds)) {
115 return koplocks->receive_message(&fds, buffer, buffer_len);
119 * Linux 2.0.x seems to have a bug in that
120 * it can return -1, EINTR with a timeout of zero.
121 * Make sure we bail out here with a read timeout
122 * if we got EINTR on a timeout of 1 or less.
125 if (timeout <= 1) {
126 smb_read_error = READ_TIMEOUT;
127 return False;
130 /* Not a kernel interrupt - could be a SIGUSR1 message. We must restart. */
131 /* We need to decrement the timeout here. */
132 timeout -= ((time(NULL) - starttime)*1000);
133 if (timeout < 0)
134 timeout = 1;
136 DEBUG(5,("receive_local_message: EINTR : new timeout %d ms\n", timeout));
137 continue;
140 /* Check if error */
141 if(selrtn == -1) {
142 /* something is wrong. Maybe the socket is dead? */
143 smb_read_error = READ_ERROR;
144 return False;
147 /* Did we timeout ? */
148 if (selrtn == 0) {
149 smb_read_error = READ_TIMEOUT;
150 return False;
154 if (koplocks && koplocks->msg_waiting(&fds)) {
155 return koplocks->receive_message(&fds, buffer, buffer_len);
158 if (!FD_ISSET(oplock_sock, &fds))
159 return False;
162 * From here down we deal with the smbd <--> smbd
163 * oplock break protocol only.
167 * Read a loopback udp message.
169 msg_len = sys_recvfrom(oplock_sock, &buffer[OPBRK_CMD_HEADER_LEN],
170 buffer_len - OPBRK_CMD_HEADER_LEN, 0, (struct sockaddr *)&from, &fromlen);
172 if(msg_len < 0) {
173 DEBUG(0,("receive_local_message. Error in recvfrom. (%s).\n",strerror(errno)));
174 return False;
177 /* Validate message length. */
178 if(msg_len > (buffer_len - OPBRK_CMD_HEADER_LEN)) {
179 DEBUG(0,("receive_local_message: invalid msg_len (%d) max can be %d\n", msg_len,
180 buffer_len - OPBRK_CMD_HEADER_LEN));
181 return False;
184 /* Validate message from address (must be localhost). */
185 if(from.sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
186 DEBUG(0,("receive_local_message: invalid 'from' address \
187 (was %lx should be 127.0.0.1)\n", (long)from.sin_addr.s_addr));
188 return False;
191 /* Setup the message header */
192 SIVAL(buffer,OPBRK_CMD_LEN_OFFSET,msg_len);
193 SSVAL(buffer,OPBRK_CMD_PORT_OFFSET,ntohs(from.sin_port));
195 return True;
198 /****************************************************************************
199 Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
200 disabled (just sets flags). Returns True if oplock set.
201 ****************************************************************************/
203 BOOL set_file_oplock(files_struct *fsp, int oplock_type)
205 if (koplocks && !koplocks->set_oplock(fsp, oplock_type))
206 return False;
208 fsp->oplock_type = oplock_type;
209 fsp->sent_oplock_break = NO_BREAK_SENT;
210 if (oplock_type == LEVEL_II_OPLOCK)
211 level_II_oplocks_open++;
212 else
213 exclusive_oplocks_open++;
215 DEBUG(5,("set_file_oplock: granted oplock on file %s, dev = %x, inode = %.0f, file_id = %lu, \
216 tv_sec = %x, tv_usec = %x\n",
217 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id,
218 (int)fsp->open_time.tv_sec, (int)fsp->open_time.tv_usec ));
220 return True;
223 /****************************************************************************
224 Attempt to release an oplock on a file. Decrements oplock count.
225 ****************************************************************************/
227 void release_file_oplock(files_struct *fsp)
229 if ((fsp->oplock_type != NO_OPLOCK) && koplocks)
230 koplocks->release_oplock(fsp);
232 if (fsp->oplock_type == LEVEL_II_OPLOCK)
233 level_II_oplocks_open--;
234 else if (fsp->oplock_type)
235 exclusive_oplocks_open--;
237 fsp->oplock_type = NO_OPLOCK;
238 fsp->sent_oplock_break = NO_BREAK_SENT;
240 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
243 /****************************************************************************
244 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
245 ****************************************************************************/
247 static void downgrade_file_oplock(files_struct *fsp)
249 if (koplocks)
250 koplocks->release_oplock(fsp);
251 fsp->oplock_type = LEVEL_II_OPLOCK;
252 exclusive_oplocks_open--;
253 level_II_oplocks_open++;
254 fsp->sent_oplock_break = NO_BREAK_SENT;
257 /****************************************************************************
258 Remove a file oplock. Copes with level II and exclusive.
259 Locks then unlocks the share mode lock. Client can decide to go directly
260 to none even if a "break-to-level II" was sent.
261 ****************************************************************************/
263 BOOL remove_oplock(files_struct *fsp, BOOL break_to_none)
265 SMB_DEV_T dev = fsp->dev;
266 SMB_INO_T inode = fsp->inode;
267 BOOL ret = True;
269 /* Remove the oplock flag from the sharemode. */
270 if (lock_share_entry_fsp(fsp) == False) {
271 DEBUG(0,("remove_oplock: failed to lock share entry for file %s\n",
272 fsp->fsp_name ));
273 return False;
276 if (fsp->sent_oplock_break == EXCLUSIVE_BREAK_SENT || break_to_none) {
278 * Deal with a reply when a break-to-none was sent.
281 if(remove_share_oplock(fsp)==False) {
282 DEBUG(0,("remove_oplock: failed to remove share oplock for file %s fnum %d, \
283 dev = %x, inode = %.0f\n", fsp->fsp_name, fsp->fnum, (unsigned int)dev, (double)inode));
284 ret = False;
287 release_file_oplock(fsp);
288 } else {
290 * Deal with a reply when a break-to-level II was sent.
292 if(downgrade_share_oplock(fsp)==False) {
293 DEBUG(0,("remove_oplock: failed to downgrade share oplock for file %s fnum %d, \
294 dev = %x, inode = %.0f\n", fsp->fsp_name, fsp->fnum, (unsigned int)dev, (double)inode));
295 ret = False;
298 downgrade_file_oplock(fsp);
301 unlock_share_entry_fsp(fsp);
302 return ret;
305 /****************************************************************************
306 Setup the listening set of file descriptors for an oplock break
307 message either from the UDP socket or from the kernel. Returns the maximum
308 fd used.
309 ****************************************************************************/
311 int setup_oplock_select_set( fd_set *fds)
313 int maxfd = oplock_sock;
315 if(oplock_sock == -1)
316 return 0;
318 FD_SET(oplock_sock,fds);
320 if (koplocks && koplocks->notification_fd != -1) {
321 FD_SET(koplocks->notification_fd, fds);
322 maxfd = MAX(maxfd, koplocks->notification_fd);
325 return maxfd;
328 /****************************************************************************
329 Process an oplock break message - whether it came from the UDP socket
330 or from the kernel.
331 ****************************************************************************/
333 BOOL process_local_message(char *buffer, int buf_size)
335 int32 msg_len;
336 uint16 from_port;
337 char *msg_start;
338 pid_t remotepid;
339 SMB_DEV_T dev;
340 SMB_INO_T inode;
341 unsigned long file_id;
342 uint16 break_cmd_type;
344 msg_len = IVAL(buffer,OPBRK_CMD_LEN_OFFSET);
345 from_port = SVAL(buffer,OPBRK_CMD_PORT_OFFSET);
347 msg_start = &buffer[OPBRK_CMD_HEADER_LEN];
349 DEBUG(5,("process_local_message: Got a message of length %d from port (%d)\n",
350 msg_len, from_port));
353 * Pull the info out of the requesting packet.
356 break_cmd_type = SVAL(msg_start,OPBRK_MESSAGE_CMD_OFFSET);
358 switch(break_cmd_type) {
359 case KERNEL_OPLOCK_BREAK_CMD:
360 if (!koplocks) {
361 DEBUG(0,("unexpected kernel oplock break!\n"));
362 break;
364 if (!koplocks->parse_message(msg_start, msg_len, &inode, &dev, &file_id)) {
365 DEBUG(0,("kernel oplock break parse failure!\n"));
367 break;
369 case OPLOCK_BREAK_CMD:
370 case LEVEL_II_OPLOCK_BREAK_CMD:
371 case ASYNC_LEVEL_II_OPLOCK_BREAK_CMD:
373 /* Ensure that the msg length is correct. */
374 if(msg_len != OPLOCK_BREAK_MSG_LEN) {
375 DEBUG(0,("process_local_message: incorrect length for OPLOCK_BREAK_CMD (was %d, should be %d).\n",
376 (int)msg_len, (int)OPLOCK_BREAK_MSG_LEN));
377 return False;
380 memcpy((char *)&remotepid, msg_start+OPLOCK_BREAK_PID_OFFSET,sizeof(remotepid));
381 memcpy((char *)&inode, msg_start+OPLOCK_BREAK_INODE_OFFSET,sizeof(inode));
382 memcpy((char *)&dev, msg_start+OPLOCK_BREAK_DEV_OFFSET,sizeof(dev));
383 memcpy((char *)&file_id, msg_start+OPLOCK_BREAK_FILEID_OFFSET,sizeof(file_id));
385 DEBUG(5,("process_local_message: (%s) oplock break request from \
386 pid %d, port %d, dev = %x, inode = %.0f, file_id = %lu\n",
387 (break_cmd_type == OPLOCK_BREAK_CMD) ? "exclusive" : "level II",
388 (int)remotepid, from_port, (unsigned int)dev, (double)inode, file_id));
389 break;
391 case RETRY_DEFERRED_OPEN_CMD:
393 /* Request to retry and open that would return SHARING_VIOLATION. */
394 if (msg_len != DEFERRED_OPEN_MSG_LEN) {
395 DEBUG(0,("process_local_message: incorrect length for RETRY_DEFERRED_OPEN_CMD (was %d, should be %d).\n",
396 (int)msg_len, (int)DEFERRED_OPEN_MSG_LEN));
397 return False;
400 uint16 mid;
402 memcpy((char *)&remotepid, msg_start+DEFERRED_OPEN_PID_OFFSET,sizeof(remotepid));
403 memcpy((char *)&inode, msg_start+DEFERRED_OPEN_INODE_OFFSET,sizeof(inode));
404 memcpy((char *)&dev, msg_start+DEFERRED_OPEN_DEV_OFFSET,sizeof(dev));
405 memcpy((char *)&mid, msg_start+DEFERRED_OPEN_MID_OFFSET,sizeof(mid));
407 DEBUG(5,("process_local_message: RETRY_DEFERRED_OPEN from \
408 pid %d, port %d, dev = %x, inode = %.0f, mid = %u\n",
409 (int)remotepid, from_port, (unsigned int)dev, (double)inode, (unsigned int)mid));
411 schedule_sharing_violation_open_smb_message(mid);
413 return True;
416 * Keep this as a debug case - eventually we can remove it.
418 case 0x8001:
419 DEBUG(0,("process_local_message: Received unsolicited break \
420 reply - dumping info.\n"));
422 if(msg_len != OPLOCK_BREAK_MSG_LEN) {
423 DEBUG(0,("process_local_message: ubr: incorrect length for reply \
424 (was %d, should be %d).\n", (int)msg_len, (int)OPLOCK_BREAK_MSG_LEN));
425 return False;
428 memcpy((char *)&inode, msg_start+OPLOCK_BREAK_INODE_OFFSET,sizeof(inode));
429 memcpy((char *)&remotepid, msg_start+OPLOCK_BREAK_PID_OFFSET,sizeof(remotepid));
430 memcpy((char *)&dev, msg_start+OPLOCK_BREAK_DEV_OFFSET,sizeof(dev));
431 memcpy((char *)&file_id, msg_start+OPLOCK_BREAK_FILEID_OFFSET,sizeof(file_id));
433 DEBUG(0,("process_local_message: unsolicited oplock break reply from \
434 pid %d, port %d, dev = %x, inode = %.0f, file_id = %lu\n",
435 (int)remotepid, from_port, (unsigned int)dev, (double)inode, file_id));
437 return False;
439 default:
440 DEBUG(0,("process_local_message: unknown UDP message command code (%x) - ignoring.\n",
441 (unsigned int)SVAL(msg_start,0)));
442 return False;
446 * Now actually process the break request.
449 if((exclusive_oplocks_open + level_II_oplocks_open) != 0) {
450 if (oplock_break(dev, inode, file_id, False) == False) {
451 DEBUG(0,("process_local_message: oplock break failed.\n"));
452 return False;
454 } else {
456 * If we have no record of any currently open oplocks,
457 * it's not an error, as a close command may have
458 * just been issued on the file that was oplocked.
459 * Just log a message and return success in this case.
461 DEBUG(3,("process_local_message: oplock break requested with no outstanding \
462 oplocks. Returning success.\n"));
466 * Do the appropriate reply - none in the kernel or async level II case.
469 if(break_cmd_type == OPLOCK_BREAK_CMD || break_cmd_type == LEVEL_II_OPLOCK_BREAK_CMD) {
470 struct sockaddr_in toaddr;
472 /* Send the message back after OR'ing in the 'REPLY' bit. */
473 SSVAL(msg_start,OPBRK_MESSAGE_CMD_OFFSET,break_cmd_type | CMD_REPLY);
475 memset((char *)&toaddr,'\0',sizeof(toaddr));
476 toaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
477 toaddr.sin_port = htons(from_port);
478 toaddr.sin_family = AF_INET;
480 if(sys_sendto( oplock_sock, msg_start, OPLOCK_BREAK_MSG_LEN, 0,
481 (struct sockaddr *)&toaddr, sizeof(toaddr)) < 0) {
482 DEBUG(0,("process_local_message: sendto process %d failed. Errno was %s\n",
483 (int)remotepid, strerror(errno)));
484 return False;
487 DEBUG(5,("process_local_message: oplock break reply sent to \
488 pid %d, port %d, for file dev = %x, inode = %.0f, file_id = %lu\n",
489 (int)remotepid, from_port, (unsigned int)dev, (double)inode, file_id));
492 return True;
495 /****************************************************************************
496 Set up an oplock break message.
497 ****************************************************************************/
499 static void prepare_break_message(char *outbuf, files_struct *fsp, BOOL level2)
501 memset(outbuf,'\0',smb_size);
502 set_message(outbuf,8,0,True);
504 SCVAL(outbuf,smb_com,SMBlockingX);
505 SSVAL(outbuf,smb_tid,fsp->conn->cnum);
506 SSVAL(outbuf,smb_pid,0xFFFF);
507 SSVAL(outbuf,smb_uid,0);
508 SSVAL(outbuf,smb_mid,0xFFFF);
509 SCVAL(outbuf,smb_vwv0,0xFF);
510 SSVAL(outbuf,smb_vwv2,fsp->fnum);
511 SCVAL(outbuf,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
512 SCVAL(outbuf,smb_vwv3+1,level2 ? OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
515 /****************************************************************************
516 Function to do the waiting before sending a local break.
517 ****************************************************************************/
519 static void wait_before_sending_break(BOOL local_request)
521 extern struct timeval smb_last_time;
523 if(local_request) {
524 struct timeval cur_tv;
525 long wait_left = (long)lp_oplock_break_wait_time();
527 if (wait_left == 0)
528 return;
530 GetTimeOfDay(&cur_tv);
532 wait_left -= ((cur_tv.tv_sec - smb_last_time.tv_sec)*1000) +
533 ((cur_tv.tv_usec - smb_last_time.tv_usec)/1000);
535 if(wait_left > 0) {
536 wait_left = MIN(wait_left, 1000);
537 sys_usleep(wait_left * 1000);
542 /****************************************************************************
543 Ensure that we have a valid oplock.
544 ****************************************************************************/
546 static files_struct *initial_break_processing(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
548 files_struct *fsp = NULL;
550 if( DEBUGLVL( 3 ) ) {
551 dbgtext( "initial_break_processing: called for dev = %x, inode = %.0f file_id = %lu\n",
552 (unsigned int)dev, (double)inode, file_id);
553 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
554 exclusive_oplocks_open, level_II_oplocks_open );
558 * We need to search the file open table for the
559 * entry containing this dev and inode, and ensure
560 * we have an oplock on it.
563 fsp = file_find_dif(dev, inode, file_id);
565 if(fsp == NULL) {
566 /* The file could have been closed in the meantime - return success. */
567 if( DEBUGLVL( 3 ) ) {
568 dbgtext( "initial_break_processing: cannot find open file with " );
569 dbgtext( "dev = %x, inode = %.0f file_id = %lu", (unsigned int)dev,
570 (double)inode, file_id);
571 dbgtext( "allowing break to succeed.\n" );
573 return NULL;
576 /* Ensure we have an oplock on the file */
579 * There is a potential race condition in that an oplock could
580 * have been broken due to another udp request, and yet there are
581 * still oplock break messages being sent in the udp message
582 * queue for this file. So return true if we don't have an oplock,
583 * as we may have just freed it.
586 if(fsp->oplock_type == NO_OPLOCK) {
587 if( DEBUGLVL( 3 ) ) {
588 dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
589 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu) has no oplock.\n",
590 (unsigned int)dev, (double)inode, fsp->file_id );
591 dbgtext( "Allowing break to succeed regardless.\n" );
593 return NULL;
596 return fsp;
599 /****************************************************************************
600 Process a level II oplock break directly.
601 We must call this function with the share mode entry locked.
602 ****************************************************************************/
604 static BOOL oplock_break_level2(files_struct *fsp, BOOL local_request)
606 extern uint32 global_client_caps;
607 char outbuf[128];
608 SMB_DEV_T dev = fsp->dev;
609 SMB_INO_T inode = fsp->inode;
612 * We can have a level II oplock even if the client is not
613 * level II oplock aware. In this case just remove the
614 * flags and don't send the break-to-none message to
615 * the client.
618 if (global_client_caps & CAP_LEVEL_II_OPLOCKS) {
619 BOOL sign_state;
622 * If we are sending an oplock break due to an SMB sent
623 * by our own client we ensure that we wait at leat
624 * lp_oplock_break_wait_time() milliseconds before sending
625 * the packet. Sending the packet sooner can break Win9x
626 * and has reported to cause problems on NT. JRA.
629 wait_before_sending_break(local_request);
631 /* Prepare the SMBlockingX message. */
632 prepare_break_message( outbuf, fsp, False);
634 /* Save the server smb signing state. */
635 sign_state = srv_oplock_set_signing(False);
637 if (!send_smb(smbd_server_fd(), outbuf))
638 exit_server("oplock_break_level2: send_smb failed.");
640 /* Restore the sign state to what it was. */
641 srv_oplock_set_signing(sign_state);
645 * Now we must update the shared memory structure to tell
646 * everyone else we no longer have a level II oplock on
647 * this open file. We must call this function with the share mode
648 * entry locked so we can change the entry directly.
651 if(remove_share_oplock(fsp)==False) {
652 DEBUG(0,("oplock_break_level2: unable to remove level II oplock for file %s\n", fsp->fsp_name ));
655 release_file_oplock(fsp);
657 if(level_II_oplocks_open < 0) {
658 DEBUG(0,("oplock_break_level2: level_II_oplocks_open < 0 (%d). PANIC ERROR\n",
659 level_II_oplocks_open));
660 abort();
663 if( DEBUGLVL( 3 ) ) {
664 dbgtext( "oplock_break_level2: returning success for " );
665 dbgtext( "dev = %x, inode = %.0f, file_id = %lu\n", (unsigned int)dev, (double)inode, fsp->file_id );
666 dbgtext( "Current level II oplocks_open = %d\n", level_II_oplocks_open );
669 return True;
672 /****************************************************************************
673 Process an oplock break directly.
674 This is always called with the share mode lock *NOT* held.
675 ****************************************************************************/
677 static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, BOOL local_request)
679 extern uint32 global_client_caps;
680 extern struct current_user current_user;
681 char *inbuf = NULL;
682 char *outbuf = NULL;
683 files_struct *fsp = NULL;
684 time_t start_time;
685 BOOL shutdown_server = False;
686 BOOL oplock_timeout = False;
687 BOOL sign_state;
688 connection_struct *saved_user_conn;
689 connection_struct *saved_fsp_conn;
690 int saved_vuid;
691 pstring saved_dir;
692 int timeout = (OPLOCK_BREAK_TIMEOUT * 1000);
693 pstring file_name;
694 BOOL using_levelII;
696 if((fsp = initial_break_processing(dev, inode, file_id)) == NULL)
697 return True;
700 * Deal with a level II oplock going break to none separately.
703 if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
704 BOOL ret;
705 /* We must always call oplock_break_level2() with
706 the share mode entry locked. */
707 if (lock_share_entry_fsp(fsp) == False) {
708 DEBUG(0,("oplock_break: unable to lock share entry for file %s\n", fsp->fsp_name ));
709 return False;
711 ret = oplock_break_level2(fsp, local_request);
712 unlock_share_entry_fsp(fsp);
713 return ret;
716 /* Mark the oplock break as sent - we don't want to send twice! */
717 if (fsp->sent_oplock_break) {
718 if( DEBUGLVL( 0 ) ) {
719 dbgtext( "oplock_break: ERROR: oplock_break already sent for " );
720 dbgtext( "file %s ", fsp->fsp_name);
721 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu)\n", (unsigned int)dev, (double)inode, fsp->file_id );
725 * We have to fail the open here as we cannot send another oplock break on
726 * this file whilst we are awaiting a response from the client - neither
727 * can we allow another open to succeed while we are waiting for the client.
729 return False;
732 if(global_oplock_break) {
733 DEBUG(0,("ABORT : ABORT : recursion in oplock_break !!!!!\n"));
734 abort();
738 * Now comes the horrid part. We must send an oplock break to the client,
739 * and then process incoming messages until we get a close or oplock release.
740 * At this point we know we need a new inbuf/outbuf buffer pair.
741 * We cannot use these staticaly as we may recurse into here due to
742 * messages crossing on the wire.
745 if((inbuf = (char *)SMB_MALLOC(BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE + SAFETY_MARGIN))==NULL) {
746 DEBUG(0,("oplock_break: malloc fail for input buffer.\n"));
747 return False;
750 if((outbuf = (char *)SMB_MALLOC(BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE + SAFETY_MARGIN))==NULL) {
751 DEBUG(0,("oplock_break: malloc fail for output buffer.\n"));
752 SAFE_FREE(inbuf);
753 return False;
757 * If we are sending an oplock break due to an SMB sent
758 * by our own client we ensure that we wait at leat
759 * lp_oplock_break_wait_time() milliseconds before sending
760 * the packet. Sending the packet sooner can break Win9x
761 * and has reported to cause problems on NT. JRA.
764 wait_before_sending_break(local_request);
766 /* Prepare the SMBlockingX message. */
768 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
769 !koplocks && /* NOTE: we force levelII off for kernel oplocks - this will change when it is supported */
770 lp_level2_oplocks(SNUM(fsp->conn))) {
771 using_levelII = True;
772 } else {
773 using_levelII = False;
776 prepare_break_message( outbuf, fsp, using_levelII);
777 /* Remember if we just sent a break to level II on this file. */
778 fsp->sent_oplock_break = using_levelII? LEVEL_II_BREAK_SENT:EXCLUSIVE_BREAK_SENT;
780 /* Save the server smb signing state. */
781 sign_state = srv_oplock_set_signing(False);
783 if (!send_smb(smbd_server_fd(), outbuf)) {
784 srv_oplock_set_signing(sign_state);
785 exit_server("oplock_break: send_smb failed.");
788 /* Restore the sign state to what it was. */
789 srv_oplock_set_signing(sign_state);
791 /* We need this in case a readraw crosses on the wire. */
792 global_oplock_break = True;
794 /* Process incoming messages. */
797 * JRA - If we don't get a break from the client in OPLOCK_BREAK_TIMEOUT
798 * seconds we should just die....
801 start_time = time(NULL);
804 * Save the information we need to re-become the
805 * user, then unbecome the user whilst we're doing this.
807 saved_user_conn = current_user.conn;
808 saved_vuid = current_user.vuid;
809 saved_fsp_conn = fsp->conn;
810 change_to_root_user();
812 * Initialize saved_dir to something sensible: vfs_GetWd may not work well
813 * for root: the directory may be NFS-mounted and exported with root_squash
814 * (so has no root access).
816 pstrcpy(saved_dir,saved_fsp_conn->connectpath);
817 vfs_GetWd(saved_fsp_conn,saved_dir);
818 /* Save the chain fnum. */
819 file_chain_save();
822 * From Charles Hoch <hoch@exemplary.com>. If the break processing
823 * code closes the file (as it often does), then the fsp pointer here
824 * points to free()'d memory. We *must* revalidate fsp each time
825 * around the loop.
828 pstrcpy(file_name, fsp->fsp_name);
830 while((fsp = initial_break_processing(dev, inode, file_id)) &&
831 OPEN_FSP(fsp) && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
832 if(receive_smb(smbd_server_fd(),inbuf, timeout) == False) {
834 * Die if we got an error.
837 if (smb_read_error == READ_EOF) {
838 DEBUG( 0, ( "oplock_break: end of file from client\n" ) );
839 shutdown_server = True;
840 } else if (smb_read_error == READ_ERROR) {
841 DEBUG( 0, ("oplock_break: receive_smb error (%s)\n", strerror(errno)) );
842 shutdown_server = True;
843 } else if (smb_read_error == READ_BAD_SIG) {
844 DEBUG( 0, ("oplock_break: bad signature from client\n" ));
845 shutdown_server = True;
846 } else if (smb_read_error == READ_TIMEOUT) {
847 DEBUG( 0, ( "oplock_break: receive_smb timed out after %d seconds.\n", OPLOCK_BREAK_TIMEOUT ) );
848 oplock_timeout = True;
851 DEBUGADD( 0, ( "oplock_break failed for file %s ", file_name ) );
852 DEBUGADD( 0, ( "(dev = %x, inode = %.0f, file_id = %lu).\n",
853 (unsigned int)dev, (double)inode, file_id));
855 break;
859 * There are certain SMB requests that we shouldn't allow
860 * to recurse. opens, renames and deletes are the obvious
861 * ones. This is handled in the switch_message() function.
862 * If global_oplock_break is set they will push the packet onto
863 * the pending smb queue and return -1 (no reply).
864 * JRA.
867 process_smb(inbuf, outbuf);
870 * Die if we go over the time limit.
873 if((time(NULL) - start_time) > OPLOCK_BREAK_TIMEOUT) {
874 if( DEBUGLVL( 0 ) ) {
875 dbgtext( "oplock_break: no break received from client " );
876 dbgtext( "within %d seconds.\n", OPLOCK_BREAK_TIMEOUT );
877 dbgtext( "oplock_break failed for file %s ", fsp->fsp_name );
878 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu).\n",
879 (unsigned int)dev, (double)inode, file_id );
881 oplock_timeout = True;
882 break;
887 * Go back to being the user who requested the oplock
888 * break.
890 if((saved_user_conn != NULL) && (saved_vuid != UID_FIELD_INVALID) && !change_to_user(saved_user_conn, saved_vuid)) {
891 DEBUG( 0, ( "oplock_break: unable to re-become user!" ) );
892 DEBUGADD( 0, ( "Shutting down server\n" ) );
893 close(oplock_sock);
894 exit_server("unable to re-become user");
897 /* Including the directory. */
898 vfs_ChDir(saved_fsp_conn,saved_dir);
900 /* Restore the chain fnum. */
901 file_chain_restore();
903 /* Free the buffers we've been using to recurse. */
904 SAFE_FREE(inbuf);
905 SAFE_FREE(outbuf);
907 /* We need this in case a readraw crossed on the wire. */
908 if(global_oplock_break)
909 global_oplock_break = False;
912 * If the client timed out then clear the oplock (or go to level II)
913 * and continue. This seems to be what NT does and is better than dropping
914 * the connection.
917 if(oplock_timeout && (fsp = initial_break_processing(dev, inode, file_id)) &&
918 OPEN_FSP(fsp) && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
919 DEBUG(0,("oplock_break: client failure in oplock break in file %s\n", fsp->fsp_name));
920 remove_oplock(fsp,True);
921 #if FASCIST_OPLOCK_BACKOFF
922 global_client_failed_oplock_break = True; /* Never grant this client an oplock again. */
923 #endif
927 * If the client had an error we must die.
930 if(shutdown_server) {
931 DEBUG( 0, ( "oplock_break: client failure in break - " ) );
932 DEBUGADD( 0, ( "shutting down this smbd.\n" ) );
933 close(oplock_sock);
934 exit_server("oplock break failure");
937 /* Santity check - remove this later. JRA */
938 if(exclusive_oplocks_open < 0) {
939 DEBUG(0,("oplock_break: exclusive_oplocks_open < 0 (%d). PANIC ERROR\n", exclusive_oplocks_open));
940 abort();
943 if( DEBUGLVL( 3 ) ) {
944 dbgtext( "oplock_break: returning success for " );
945 dbgtext( "dev = %x, inode = %.0f, file_id = %lu\n", (unsigned int)dev, (double)inode, file_id );
946 dbgtext( "Current exclusive_oplocks_open = %d\n", exclusive_oplocks_open );
949 return True;
952 /****************************************************************************
953 Send an oplock break message to another smbd process. If the oplock is held
954 by the local smbd then call the oplock break function directly.
955 This function is called with no share locks held.
956 ****************************************************************************/
958 BOOL request_oplock_break(share_mode_entry *share_entry)
960 char op_break_msg[OPLOCK_BREAK_MSG_LEN];
961 struct sockaddr_in addr_out;
962 pid_t pid = sys_getpid();
963 time_t start_time;
964 int time_left;
965 SMB_DEV_T dev = share_entry->dev;
966 SMB_INO_T inode = share_entry->inode;
967 unsigned long file_id = share_entry->share_file_id;
968 uint16 break_cmd_type;
970 if(pid == share_entry->pid) {
971 /* We are breaking our own oplock, make sure it's us. */
972 if(share_entry->op_port != global_oplock_port) {
973 DEBUG(0,("request_oplock_break: corrupt share mode entry - pid = %d, port = %d \
974 should be %d\n", (int)pid, share_entry->op_port, global_oplock_port));
975 return False;
978 DEBUG(5,("request_oplock_break: breaking our own oplock\n"));
980 #if 1 /* JRA PARANOIA TEST.... */
982 files_struct *fsp = file_find_dif(dev, inode, file_id);
983 if (!fsp) {
984 DEBUG(0,("request_oplock_break: PANIC : breaking our own oplock requested for \
985 dev = %x, inode = %.0f, file_id = %lu and no fsp found !\n",
986 (unsigned int)dev, (double)inode, file_id ));
987 smb_panic("request_oplock_break: no fsp found for our own oplock\n");
990 #endif /* END JRA PARANOIA TEST... */
992 /* Call oplock break direct. */
993 return oplock_break(dev, inode, file_id, True);
996 /* We need to send a OPLOCK_BREAK_CMD message to the port in the share mode entry. */
998 if (LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) {
999 break_cmd_type = LEVEL_II_OPLOCK_BREAK_CMD;
1000 } else {
1001 break_cmd_type = OPLOCK_BREAK_CMD;
1004 SSVAL(op_break_msg,OPBRK_MESSAGE_CMD_OFFSET,break_cmd_type);
1005 memcpy(op_break_msg+OPLOCK_BREAK_PID_OFFSET,(char *)&pid,sizeof(pid));
1006 memcpy(op_break_msg+OPLOCK_BREAK_DEV_OFFSET,(char *)&dev,sizeof(dev));
1007 memcpy(op_break_msg+OPLOCK_BREAK_INODE_OFFSET,(char *)&inode,sizeof(inode));
1008 memcpy(op_break_msg+OPLOCK_BREAK_FILEID_OFFSET,(char *)&file_id,sizeof(file_id));
1010 /* Set the address and port. */
1011 memset((char *)&addr_out,'\0',sizeof(addr_out));
1012 addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1013 addr_out.sin_port = htons( share_entry->op_port );
1014 addr_out.sin_family = AF_INET;
1016 if( DEBUGLVL( 3 ) ) {
1017 dbgtext( "request_oplock_break: sending a synchronous oplock break message to " );
1018 dbgtext( "pid %d on port %d ", (int)share_entry->pid, share_entry->op_port );
1019 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1020 (unsigned int)dev, (double)inode, file_id );
1023 if(sys_sendto(oplock_sock,op_break_msg,OPLOCK_BREAK_MSG_LEN,0,
1024 (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0) {
1025 if( DEBUGLVL( 0 ) ) {
1026 dbgtext( "request_oplock_break: failed when sending a oplock " );
1027 dbgtext( "break message to pid %d ", (int)share_entry->pid );
1028 dbgtext( "on port %d ", share_entry->op_port );
1029 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1030 (unsigned int)dev, (double)inode, file_id );
1031 dbgtext( "Error was %s\n", strerror(errno) );
1033 return False;
1037 * Now we must await the oplock broken message coming back
1038 * from the target smbd process. Timeout if it fails to
1039 * return in (OPLOCK_BREAK_TIMEOUT + OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR) seconds.
1040 * While we get messages that aren't ours, loop.
1043 start_time = time(NULL);
1044 time_left = OPLOCK_BREAK_TIMEOUT+OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR;
1046 while(time_left >= 0) {
1047 char op_break_reply[OPBRK_CMD_HEADER_LEN+OPLOCK_BREAK_MSG_LEN];
1048 uint16 reply_from_port;
1049 char *reply_msg_start;
1051 if(receive_local_message(op_break_reply, sizeof(op_break_reply),
1052 time_left ? time_left * 1000 : 1) == False) {
1053 if(smb_read_error == READ_TIMEOUT) {
1054 if( DEBUGLVL( 0 ) ) {
1055 dbgtext( "request_oplock_break: no response received to oplock " );
1056 dbgtext( "break request to pid %d ", (int)share_entry->pid );
1057 dbgtext( "on port %d ", share_entry->op_port );
1058 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1059 (unsigned int)dev, (double)inode, file_id );
1063 * This is a hack to make handling of failing clients more robust.
1064 * If a oplock break response message is not received in the timeout
1065 * period we may assume that the smbd servicing that client holding
1066 * the oplock has died and the client changes were lost anyway, so
1067 * we should continue to try and open the file.
1069 break;
1070 } else {
1071 if( DEBUGLVL( 0 ) ) {
1072 dbgtext( "request_oplock_break: error in response received " );
1073 dbgtext( "to oplock break request to pid %d ", (int)share_entry->pid );
1074 dbgtext( "on port %d ", share_entry->op_port );
1075 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1076 (unsigned int)dev, (double)inode, file_id );
1077 dbgtext( "Error was (%s).\n", strerror(errno) );
1080 return False;
1083 reply_from_port = SVAL(op_break_reply,OPBRK_CMD_PORT_OFFSET);
1084 reply_msg_start = &op_break_reply[OPBRK_CMD_HEADER_LEN];
1087 * Test to see if this is the reply we are awaiting (ie. the one we sent with the CMD_REPLY flag OR'ed in).
1089 if((SVAL(reply_msg_start,OPBRK_MESSAGE_CMD_OFFSET) & CMD_REPLY) &&
1090 ((SVAL(reply_msg_start,OPBRK_MESSAGE_CMD_OFFSET) & ~CMD_REPLY) == break_cmd_type) &&
1091 (reply_from_port == share_entry->op_port) &&
1092 (memcmp(&reply_msg_start[OPLOCK_BREAK_PID_OFFSET], &op_break_msg[OPLOCK_BREAK_PID_OFFSET],
1093 OPLOCK_BREAK_MSG_LEN - OPLOCK_BREAK_PID_OFFSET) == 0)) {
1096 * This is the reply we've been waiting for.
1098 break;
1099 } else {
1101 * This is another message - a break request.
1102 * Note that both kernel oplock break requests
1103 * and UDP inter-smbd oplock break requests will
1104 * be processed here.
1106 * Process it to prevent potential deadlock.
1107 * Note that the code in switch_message() prevents
1108 * us from recursing into here as any SMB requests
1109 * we might process that would cause another oplock
1110 * break request to be made will be queued.
1111 * JRA.
1114 process_local_message(op_break_reply, sizeof(op_break_reply));
1117 time_left -= (time(NULL) - start_time);
1120 DEBUG(3,("request_oplock_break: broke oplock.\n"));
1122 return True;
1125 /****************************************************************************
1126 Attempt to break an oplock on a file (if oplocked).
1127 Returns True if the file was closed as a result of
1128 the oplock break, False otherwise.
1129 Used as a last ditch attempt to free a space in the
1130 file table when we have run out.
1131 ****************************************************************************/
1133 BOOL attempt_close_oplocked_file(files_struct *fsp)
1135 DEBUG(5,("attempt_close_oplocked_file: checking file %s.\n", fsp->fsp_name));
1137 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !fsp->sent_oplock_break && (fsp->fd != -1)) {
1138 /* Try and break the oplock. */
1139 if (oplock_break(fsp->dev, fsp->inode, fsp->file_id, True)) {
1140 if(file_find_fsp(fsp) == NULL) /* Did the oplock break close the file ? */
1141 return True;
1145 return False;
1148 /****************************************************************************
1149 Send an asynchronous oplock break message to another smbd process.
1150 ****************************************************************************/
1152 static BOOL request_remote_level2_async_oplock_break(share_mode_entry *share_entry)
1154 char op_break_msg[OPLOCK_BREAK_MSG_LEN];
1155 struct sockaddr_in addr_out;
1156 pid_t pid = sys_getpid();
1157 SMB_DEV_T dev = share_entry->dev;
1158 SMB_INO_T inode = share_entry->inode;
1159 unsigned long file_id = share_entry->share_file_id;
1161 /* We need to send a ASYNC_LEVEL_II_OPLOCK_BREAK_CMD message to the port in the share mode entry. */
1163 SSVAL(op_break_msg,OPBRK_MESSAGE_CMD_OFFSET,ASYNC_LEVEL_II_OPLOCK_BREAK_CMD);
1164 memcpy(op_break_msg+OPLOCK_BREAK_PID_OFFSET,(char *)&pid,sizeof(pid));
1165 memcpy(op_break_msg+OPLOCK_BREAK_DEV_OFFSET,(char *)&dev,sizeof(dev));
1166 memcpy(op_break_msg+OPLOCK_BREAK_INODE_OFFSET,(char *)&inode,sizeof(inode));
1167 memcpy(op_break_msg+OPLOCK_BREAK_FILEID_OFFSET,(char *)&file_id,sizeof(file_id));
1169 /* Set the address and port. */
1170 memset((char *)&addr_out,'\0',sizeof(addr_out));
1171 addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1172 addr_out.sin_port = htons( share_entry->op_port );
1173 addr_out.sin_family = AF_INET;
1175 if( DEBUGLVL( 3 ) ) {
1176 dbgtext( "request_remote_level2_async_oplock_break: sending an asynchronous oplock break message to ");
1177 dbgtext( "pid %d on port %d ", (int)share_entry->pid, share_entry->op_port );
1178 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1179 (unsigned int)dev, (double)inode, file_id );
1182 if(sys_sendto(oplock_sock,op_break_msg,OPLOCK_BREAK_MSG_LEN,0,
1183 (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0) {
1184 if( DEBUGLVL( 0 ) ) {
1185 dbgtext( "request_remote_level2_async_oplock_break: failed when sending a oplock " );
1186 dbgtext( "break message to pid %d ", (int)share_entry->pid );
1187 dbgtext( "on port %d ", share_entry->op_port );
1188 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1189 (unsigned int)dev, (double)inode, file_id );
1190 dbgtext( "Error was %s\n", strerror(errno) );
1192 return False;
1195 DEBUG(3,("request_remote_level2_async_oplock_break: sent async break message to level II entry.\n"));
1196 return True;
1199 /****************************************************************************
1200 This function is called on any file modification or lock request. If a file
1201 is level 2 oplocked then it must tell all other level 2 holders to break to none.
1202 ****************************************************************************/
1204 void release_level_2_oplocks_on_change(files_struct *fsp)
1206 share_mode_entry *share_list = NULL;
1207 pid_t pid = sys_getpid();
1208 int num_share_modes = 0;
1209 int i;
1212 * If this file is level II oplocked then we need
1213 * to grab the shared memory lock and inform all
1214 * other files with a level II lock that they need
1215 * to flush their read caches. We keep the lock over
1216 * the shared memory area whilst doing this.
1219 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1220 return;
1222 if (lock_share_entry_fsp(fsp) == False) {
1223 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock share mode entry for file %s.\n", fsp->fsp_name ));
1226 num_share_modes = get_share_modes(fsp->conn, fsp->dev, fsp->inode, &share_list);
1228 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
1229 num_share_modes ));
1231 for(i = 0; i < num_share_modes; i++) {
1232 share_mode_entry *share_entry = &share_list[i];
1235 * As there could have been multiple writes waiting at the lock_share_entry
1236 * gate we may not be the first to enter. Hence the state of the op_types
1237 * in the share mode entries may be partly NO_OPLOCK and partly LEVEL_II
1238 * oplock. It will do no harm to re-send break messages to those smbd's
1239 * that are still waiting their turn to remove their LEVEL_II state, and
1240 * also no harm to ignore existing NO_OPLOCK states. JRA.
1243 DEBUG(10,("release_level_2_oplocks_on_change: share_entry[%i]->op_type == %d\n",
1244 i, share_entry->op_type ));
1246 if (share_entry->op_type == NO_OPLOCK)
1247 continue;
1249 /* Paranoia .... */
1250 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
1251 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. share mode entry %d is an exlusive oplock !\n", i ));
1252 unlock_share_entry(fsp->conn, fsp->dev, fsp->inode);
1253 abort();
1257 * Check if this is a file we have open (including the
1258 * file we've been called to do write_file on. If so
1259 * then break it directly without releasing the lock.
1262 if (pid == share_entry->pid) {
1263 files_struct *new_fsp = file_find_dif(share_entry->dev, share_entry->inode, share_entry->share_file_id);
1265 /* Paranoia check... */
1266 if(new_fsp == NULL) {
1267 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. share mode entry %d is not a local file !\n", i ));
1268 unlock_share_entry(fsp->conn, fsp->dev, fsp->inode);
1269 abort();
1272 DEBUG(10,("release_level_2_oplocks_on_change: breaking our own oplock.\n"));
1274 oplock_break_level2(new_fsp, True);
1276 } else {
1279 * This is a remote file and so we send an asynchronous
1280 * message.
1283 DEBUG(10,("release_level_2_oplocks_on_change: breaking remote oplock (async).\n"));
1284 request_remote_level2_async_oplock_break(share_entry);
1288 SAFE_FREE(share_list);
1289 unlock_share_entry_fsp(fsp);
1291 /* Paranoia check... */
1292 if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
1293 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. File %s still has a level II oplock.\n", fsp->fsp_name));
1294 smb_panic("release_level_2_oplocks_on_change");
1298 /****************************************************************************
1299 Send a 'retry your open' message to a process with a deferred open entry.
1300 ****************************************************************************/
1302 BOOL send_deferred_open_retry_message(deferred_open_entry *entry)
1304 char de_msg[DEFERRED_OPEN_MSG_LEN];
1305 struct sockaddr_in addr_out;
1306 pid_t pid = sys_getpid();
1308 memset(de_msg, '\0', DEFERRED_OPEN_MSG_LEN);
1309 SSVAL(de_msg,DEFERRED_OPEN_CMD_OFFSET,RETRY_DEFERRED_OPEN_CMD);
1310 memcpy(de_msg+DEFERRED_OPEN_PID_OFFSET,(char *)&pid,sizeof(pid));
1311 memcpy(de_msg+DEFERRED_OPEN_DEV_OFFSET,(char *)&entry->dev,sizeof(entry->dev));
1312 memcpy(de_msg+DEFERRED_OPEN_INODE_OFFSET,(char *)&entry->inode,sizeof(entry->inode));
1313 memcpy(de_msg+DEFERRED_OPEN_MID_OFFSET,(char *)&entry->mid,sizeof(entry->mid));
1315 /* Set the address and port. */
1316 memset((char *)&addr_out,'\0',sizeof(addr_out));
1317 addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1318 addr_out.sin_port = htons( entry->port );
1319 addr_out.sin_family = AF_INET;
1321 if( DEBUGLVL( 3 ) ) {
1322 dbgtext( "send_deferred_open_retry_message: sending a message to ");
1323 dbgtext( "pid %d on port %d ", (int)entry->pid, entry->port );
1324 dbgtext( "for dev = %x, inode = %.0f, mid = %u\n",
1325 (unsigned int)entry->dev, (double)entry->inode, (unsigned int)entry->mid );
1328 if(sys_sendto(oplock_sock,de_msg,DEFERRED_OPEN_MSG_LEN,0,
1329 (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0) {
1330 if( DEBUGLVL( 0 ) ) {
1331 dbgtext( "send_deferred_open_retry_message: failed sending a message to ");
1332 dbgtext( "pid %d on port %d ", (int)entry->pid, entry->port );
1333 dbgtext( "for dev = %x, inode = %.0f, mid = %u\n",
1334 (unsigned int)entry->dev, (double)entry->inode, (unsigned int)entry->mid );
1335 dbgtext( "Error was %s\n", strerror(errno) );
1337 return False;
1339 return True;
1342 /****************************************************************************
1343 Setup oplocks for this process.
1344 ****************************************************************************/
1346 BOOL init_oplocks(void)
1348 struct sockaddr_in sock_name;
1349 socklen_t len = sizeof(sock_name);
1351 DEBUG(3,("open_oplock_ipc: opening loopback UDP socket.\n"));
1353 /* Open a lookback UDP socket on a random port. */
1354 oplock_sock = open_socket_in(SOCK_DGRAM, 0, 0, htonl(INADDR_LOOPBACK),False);
1355 if (oplock_sock == -1) {
1356 DEBUG(0,("open_oplock_ipc: Failed to get local UDP socket for \
1357 address %lx. Error was %s\n", (long)htonl(INADDR_LOOPBACK), strerror(errno)));
1358 global_oplock_port = 0;
1359 return(False);
1362 /* Find out the transient UDP port we have been allocated. */
1363 if(getsockname(oplock_sock, (struct sockaddr *)&sock_name, &len)<0) {
1364 DEBUG(0,("open_oplock_ipc: Failed to get local UDP port. Error was %s\n",
1365 strerror(errno)));
1366 close(oplock_sock);
1367 oplock_sock = -1;
1368 global_oplock_port = 0;
1369 return False;
1371 global_oplock_port = ntohs(sock_name.sin_port);
1373 if (lp_kernel_oplocks()) {
1374 #if HAVE_KERNEL_OPLOCKS_IRIX
1375 koplocks = irix_init_kernel_oplocks();
1376 #elif HAVE_KERNEL_OPLOCKS_LINUX
1377 koplocks = linux_init_kernel_oplocks();
1378 #endif
1381 DEBUG(3,("open_oplock ipc: pid = %d, global_oplock_port = %u\n",
1382 (int)sys_getpid(), global_oplock_port));
1384 return True;