2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Jeremy Allison 1998 - 2001
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.
25 /* Oplock ipc UDP socket. */
26 static int oplock_sock
= -1;
27 uint16 global_oplock_port
= 0;
29 /* Current number of oplocks we have outstanding. */
30 static int32 exclusive_oplocks_open
= 0;
31 static int32 level_II_oplocks_open
= 0;
32 BOOL global_client_failed_oplock_break
= False
;
33 BOOL global_oplock_break
= False
;
35 extern int smb_read_error
;
37 static struct kernel_oplocks
*koplocks
;
39 static BOOL
oplock_break(SMB_DEV_T dev
, SMB_INO_T inode
, unsigned long file_id
, BOOL local
);
41 /****************************************************************************
42 Get the number of current exclusive oplocks.
43 ****************************************************************************/
45 int32
get_number_of_exclusive_open_oplocks(void)
47 return exclusive_oplocks_open
;
50 /****************************************************************************
51 Return True if an oplock message is pending.
52 ****************************************************************************/
54 BOOL
oplock_message_waiting(fd_set
*fds
)
56 if (koplocks
&& koplocks
->msg_waiting(fds
))
59 if (FD_ISSET(oplock_sock
, fds
))
65 /****************************************************************************
66 Read an oplock break message from either the oplock UDP fd or the
67 kernel (if kernel oplocks are supported).
69 If timeout is zero then *fds contains the file descriptors that
70 are ready to be read and acted upon. If timeout is non-zero then
71 *fds contains the file descriptors to be selected on for read.
72 The timeout is in milliseconds
74 ****************************************************************************/
76 BOOL
receive_local_message( char *buffer
, int buffer_len
, int timeout
)
78 struct sockaddr_in from
;
79 socklen_t fromlen
= sizeof(from
);
88 * We need to check for kernel oplocks before going into the select
89 * here, as the EINTR generated by the linux kernel oplock may have
90 * already been eaten. JRA.
93 if (koplocks
&& koplocks
->msg_waiting(&fds
)) {
94 return koplocks
->receive_message(&fds
, buffer
, buffer_len
);
97 while (timeout
> 0 && selrtn
== -1) {
99 int maxfd
= oplock_sock
;
100 time_t starttime
= time(NULL
);
103 maxfd
= setup_oplock_select_set(&fds
);
105 to
.tv_sec
= timeout
/ 1000;
106 to
.tv_usec
= (timeout
% 1000) * 1000;
108 DEBUG(5,("receive_local_message: doing select with timeout of %d ms\n", timeout
));
110 selrtn
= sys_select(maxfd
+1,&fds
,NULL
,NULL
,&to
);
112 if (selrtn
== -1 && errno
== EINTR
) {
114 /* could be a kernel oplock interrupt */
115 if (koplocks
&& koplocks
->msg_waiting(&fds
)) {
116 return koplocks
->receive_message(&fds
, buffer
, buffer_len
);
120 * Linux 2.0.x seems to have a bug in that
121 * it can return -1, EINTR with a timeout of zero.
122 * Make sure we bail out here with a read timeout
123 * if we got EINTR on a timeout of 1 or less.
127 smb_read_error
= READ_TIMEOUT
;
131 /* Not a kernel interrupt - could be a SIGUSR1 message. We must restart. */
132 /* We need to decrement the timeout here. */
133 timeout
-= ((time(NULL
) - starttime
)*1000);
137 DEBUG(5,("receive_local_message: EINTR : new timeout %d ms\n", timeout
));
143 /* something is wrong. Maybe the socket is dead? */
144 smb_read_error
= READ_ERROR
;
148 /* Did we timeout ? */
150 smb_read_error
= READ_TIMEOUT
;
155 if (koplocks
&& koplocks
->msg_waiting(&fds
)) {
156 return koplocks
->receive_message(&fds
, buffer
, buffer_len
);
159 if (!FD_ISSET(oplock_sock
, &fds
))
163 * From here down we deal with the smbd <--> smbd
164 * oplock break protocol only.
168 * Read a loopback udp message.
170 msg_len
= sys_recvfrom(oplock_sock
, &buffer
[OPBRK_CMD_HEADER_LEN
],
171 buffer_len
- OPBRK_CMD_HEADER_LEN
, 0, (struct sockaddr
*)&from
, &fromlen
);
174 DEBUG(0,("receive_local_message. Error in recvfrom. (%s).\n",strerror(errno
)));
178 /* Validate message length. */
179 if(msg_len
> (buffer_len
- OPBRK_CMD_HEADER_LEN
)) {
180 DEBUG(0,("receive_local_message: invalid msg_len (%d) max can be %d\n", msg_len
,
181 buffer_len
- OPBRK_CMD_HEADER_LEN
));
185 /* Validate message from address (must be localhost). */
186 if(from
.sin_addr
.s_addr
!= htonl(INADDR_LOOPBACK
)) {
187 DEBUG(0,("receive_local_message: invalid 'from' address \
188 (was %lx should be 127.0.0.1)\n", (long)from
.sin_addr
.s_addr
));
192 /* Setup the message header */
193 SIVAL(buffer
,OPBRK_CMD_LEN_OFFSET
,msg_len
);
194 SSVAL(buffer
,OPBRK_CMD_PORT_OFFSET
,ntohs(from
.sin_port
));
199 /****************************************************************************
200 Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
201 disabled (just sets flags). Returns True if oplock set.
202 ****************************************************************************/
204 BOOL
set_file_oplock(files_struct
*fsp
, int oplock_type
)
206 if (koplocks
&& !koplocks
->set_oplock(fsp
, oplock_type
))
209 fsp
->oplock_type
= oplock_type
;
210 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
211 if (oplock_type
== LEVEL_II_OPLOCK
)
212 level_II_oplocks_open
++;
214 exclusive_oplocks_open
++;
216 DEBUG(5,("set_file_oplock: granted oplock on file %s, dev = %x, inode = %.0f, file_id = %lu, \
217 tv_sec = %x, tv_usec = %x\n",
218 fsp
->fsp_name
, (unsigned int)fsp
->dev
, (double)fsp
->inode
, fsp
->file_id
,
219 (int)fsp
->open_time
.tv_sec
, (int)fsp
->open_time
.tv_usec
));
224 /****************************************************************************
225 Attempt to release an oplock on a file. Decrements oplock count.
226 ****************************************************************************/
228 void release_file_oplock(files_struct
*fsp
)
231 koplocks
->release_oplock(fsp
);
233 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
)
234 level_II_oplocks_open
--;
236 exclusive_oplocks_open
--;
238 fsp
->oplock_type
= NO_OPLOCK
;
239 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
241 flush_write_cache(fsp
, OPLOCK_RELEASE_FLUSH
);
244 /****************************************************************************
245 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
246 ****************************************************************************/
248 static void downgrade_file_oplock(files_struct
*fsp
)
251 koplocks
->release_oplock(fsp
);
252 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
253 exclusive_oplocks_open
--;
254 level_II_oplocks_open
++;
255 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
258 /****************************************************************************
259 Remove a file oplock. Copes with level II and exclusive.
260 Locks then unlocks the share mode lock. Client can decide to go directly
261 to none even if a "break-to-level II" was sent.
262 ****************************************************************************/
264 BOOL
remove_oplock(files_struct
*fsp
, BOOL break_to_none
)
266 SMB_DEV_T dev
= fsp
->dev
;
267 SMB_INO_T inode
= fsp
->inode
;
270 /* Remove the oplock flag from the sharemode. */
271 if (lock_share_entry_fsp(fsp
) == False
) {
272 DEBUG(0,("remove_oplock: failed to lock share entry for file %s\n",
277 if (fsp
->sent_oplock_break
== EXCLUSIVE_BREAK_SENT
|| break_to_none
) {
279 * Deal with a reply when a break-to-none was sent.
282 if(remove_share_oplock(fsp
)==False
) {
283 DEBUG(0,("remove_oplock: failed to remove share oplock for file %s fnum %d, \
284 dev = %x, inode = %.0f\n", fsp
->fsp_name
, fsp
->fnum
, (unsigned int)dev
, (double)inode
));
288 release_file_oplock(fsp
);
291 * Deal with a reply when a break-to-level II was sent.
293 if(downgrade_share_oplock(fsp
)==False
) {
294 DEBUG(0,("remove_oplock: failed to downgrade share oplock for file %s fnum %d, \
295 dev = %x, inode = %.0f\n", fsp
->fsp_name
, fsp
->fnum
, (unsigned int)dev
, (double)inode
));
299 downgrade_file_oplock(fsp
);
302 unlock_share_entry_fsp(fsp
);
306 /****************************************************************************
307 Setup the listening set of file descriptors for an oplock break
308 message either from the UDP socket or from the kernel. Returns the maximum
310 ****************************************************************************/
312 int setup_oplock_select_set( fd_set
*fds
)
314 int maxfd
= oplock_sock
;
316 if(oplock_sock
== -1)
319 FD_SET(oplock_sock
,fds
);
321 if (koplocks
&& koplocks
->notification_fd
!= -1) {
322 FD_SET(koplocks
->notification_fd
, fds
);
323 maxfd
= MAX(maxfd
, koplocks
->notification_fd
);
329 /****************************************************************************
330 Process an oplock break message - whether it came from the UDP socket
332 ****************************************************************************/
334 BOOL
process_local_message(char *buffer
, int buf_size
)
342 unsigned long file_id
;
343 uint16 break_cmd_type
;
345 msg_len
= IVAL(buffer
,OPBRK_CMD_LEN_OFFSET
);
346 from_port
= SVAL(buffer
,OPBRK_CMD_PORT_OFFSET
);
348 msg_start
= &buffer
[OPBRK_CMD_HEADER_LEN
];
350 DEBUG(5,("process_local_message: Got a message of length %d from port (%d)\n",
351 msg_len
, from_port
));
354 * Pull the info out of the requesting packet.
357 break_cmd_type
= SVAL(msg_start
,OPBRK_MESSAGE_CMD_OFFSET
);
359 switch(break_cmd_type
) {
360 case KERNEL_OPLOCK_BREAK_CMD
:
362 DEBUG(0,("unexpected kernel oplock break!\n"));
365 if (!koplocks
->parse_message(msg_start
, msg_len
, &inode
, &dev
, &file_id
)) {
366 DEBUG(0,("kernel oplock break parse failure!\n"));
370 case OPLOCK_BREAK_CMD
:
371 case 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
));
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
));
392 * Keep this as a debug case - eventually we can remove it.
395 DEBUG(0,("process_local_message: Received unsolicited break \
396 reply - dumping info.\n"));
398 if(msg_len
!= OPLOCK_BREAK_MSG_LEN
) {
399 DEBUG(0,("process_local_message: ubr: incorrect length for reply \
400 (was %d, should be %d).\n", (int)msg_len
, (int)OPLOCK_BREAK_MSG_LEN
));
404 memcpy((char *)&inode
, msg_start
+OPLOCK_BREAK_INODE_OFFSET
,sizeof(inode
));
405 memcpy((char *)&remotepid
, msg_start
+OPLOCK_BREAK_PID_OFFSET
,sizeof(remotepid
));
406 memcpy((char *)&dev
, msg_start
+OPLOCK_BREAK_DEV_OFFSET
,sizeof(dev
));
407 memcpy((char *)&file_id
, msg_start
+OPLOCK_BREAK_FILEID_OFFSET
,sizeof(file_id
));
409 DEBUG(0,("process_local_message: unsolicited oplock break reply from \
410 pid %d, port %d, dev = %x, inode = %.0f, file_id = %lu\n",
411 (int)remotepid
, from_port
, (unsigned int)dev
, (double)inode
, file_id
));
416 DEBUG(0,("process_local_message: unknown UDP message command code (%x) - ignoring.\n",
417 (unsigned int)SVAL(msg_start
,0)));
422 * Now actually process the break request.
425 if((exclusive_oplocks_open
+ level_II_oplocks_open
) != 0) {
426 if (oplock_break(dev
, inode
, file_id
, False
) == False
) {
427 DEBUG(0,("process_local_message: oplock break failed.\n"));
432 * If we have no record of any currently open oplocks,
433 * it's not an error, as a close command may have
434 * just been issued on the file that was oplocked.
435 * Just log a message and return success in this case.
437 DEBUG(3,("process_local_message: oplock break requested with no outstanding \
438 oplocks. Returning success.\n"));
442 * Do the appropriate reply - none in the kernel or level II case.
445 if(SVAL(msg_start
,OPBRK_MESSAGE_CMD_OFFSET
) == OPLOCK_BREAK_CMD
) {
446 struct sockaddr_in toaddr
;
448 /* Send the message back after OR'ing in the 'REPLY' bit. */
449 SSVAL(msg_start
,OPBRK_MESSAGE_CMD_OFFSET
,OPLOCK_BREAK_CMD
| CMD_REPLY
);
451 memset((char *)&toaddr
,'\0',sizeof(toaddr
));
452 toaddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
453 toaddr
.sin_port
= htons(from_port
);
454 toaddr
.sin_family
= AF_INET
;
456 if(sys_sendto( oplock_sock
, msg_start
, OPLOCK_BREAK_MSG_LEN
, 0,
457 (struct sockaddr
*)&toaddr
, sizeof(toaddr
)) < 0) {
458 DEBUG(0,("process_local_message: sendto process %d failed. Errno was %s\n",
459 (int)remotepid
, strerror(errno
)));
463 DEBUG(5,("process_local_message: oplock break reply sent to \
464 pid %d, port %d, for file dev = %x, inode = %.0f, file_id = %lu\n",
465 (int)remotepid
, from_port
, (unsigned int)dev
, (double)inode
, file_id
));
471 /****************************************************************************
472 Set up an oplock break message.
473 ****************************************************************************/
475 static void prepare_break_message(char *outbuf
, files_struct
*fsp
, BOOL level2
)
477 memset(outbuf
,'\0',smb_size
);
478 set_message(outbuf
,8,0,True
);
480 SCVAL(outbuf
,smb_com
,SMBlockingX
);
481 SSVAL(outbuf
,smb_tid
,fsp
->conn
->cnum
);
482 SSVAL(outbuf
,smb_pid
,0xFFFF);
483 SSVAL(outbuf
,smb_uid
,0);
484 SSVAL(outbuf
,smb_mid
,0xFFFF);
485 SCVAL(outbuf
,smb_vwv0
,0xFF);
486 SSVAL(outbuf
,smb_vwv2
,fsp
->fnum
);
487 SCVAL(outbuf
,smb_vwv3
,LOCKING_ANDX_OPLOCK_RELEASE
);
488 SCVAL(outbuf
,smb_vwv3
+1,level2
? OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
491 /****************************************************************************
492 Function to do the waiting before sending a local break.
493 ****************************************************************************/
495 static void wait_before_sending_break(BOOL local_request
)
497 extern struct timeval smb_last_time
;
500 struct timeval cur_tv
;
501 long wait_left
= (long)lp_oplock_break_wait_time();
506 GetTimeOfDay(&cur_tv
);
508 wait_left
-= ((cur_tv
.tv_sec
- smb_last_time
.tv_sec
)*1000) +
509 ((cur_tv
.tv_usec
- smb_last_time
.tv_usec
)/1000);
512 wait_left
= MIN(wait_left
, 1000);
513 sys_usleep(wait_left
* 1000);
518 /****************************************************************************
519 Ensure that we have a valid oplock.
520 ****************************************************************************/
522 static files_struct
*initial_break_processing(SMB_DEV_T dev
, SMB_INO_T inode
, unsigned long file_id
)
524 files_struct
*fsp
= NULL
;
526 if( DEBUGLVL( 3 ) ) {
527 dbgtext( "initial_break_processing: called for dev = %x, inode = %.0f file_id = %lu\n",
528 (unsigned int)dev
, (double)inode
, file_id
);
529 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
530 exclusive_oplocks_open
, level_II_oplocks_open
);
534 * We need to search the file open table for the
535 * entry containing this dev and inode, and ensure
536 * we have an oplock on it.
539 fsp
= file_find_dif(dev
, inode
, file_id
);
542 /* The file could have been closed in the meantime - return success. */
543 if( DEBUGLVL( 3 ) ) {
544 dbgtext( "initial_break_processing: cannot find open file with " );
545 dbgtext( "dev = %x, inode = %.0f file_id = %lu", (unsigned int)dev
,
546 (double)inode
, file_id
);
547 dbgtext( "allowing break to succeed.\n" );
552 /* Ensure we have an oplock on the file */
555 * There is a potential race condition in that an oplock could
556 * have been broken due to another udp request, and yet there are
557 * still oplock break messages being sent in the udp message
558 * queue for this file. So return true if we don't have an oplock,
559 * as we may have just freed it.
562 if(fsp
->oplock_type
== NO_OPLOCK
) {
563 if( DEBUGLVL( 3 ) ) {
564 dbgtext( "initial_break_processing: file %s ", fsp
->fsp_name
);
565 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu) has no oplock.\n",
566 (unsigned int)dev
, (double)inode
, fsp
->file_id
);
567 dbgtext( "Allowing break to succeed regardless.\n" );
575 /****************************************************************************
576 Process a level II oplock break directly.
577 ****************************************************************************/
579 BOOL
oplock_break_level2(files_struct
*fsp
, BOOL local_request
, int token
)
581 extern uint32 global_client_caps
;
583 BOOL got_lock
= False
;
584 SMB_DEV_T dev
= fsp
->dev
;
585 SMB_INO_T inode
= fsp
->inode
;
588 * We can have a level II oplock even if the client is not
589 * level II oplock aware. In this case just remove the
590 * flags and don't send the break-to-none message to
594 if (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) {
596 * If we are sending an oplock break due to an SMB sent
597 * by our own client we ensure that we wait at leat
598 * lp_oplock_break_wait_time() milliseconds before sending
599 * the packet. Sending the packet sooner can break Win9x
600 * and has reported to cause problems on NT. JRA.
603 wait_before_sending_break(local_request
);
605 /* Prepare the SMBlockingX message. */
607 prepare_break_message( outbuf
, fsp
, False
);
608 if (!send_smb(smbd_server_fd(), outbuf
))
609 exit_server("oplock_break_level2: send_smb failed.\n");
613 * Now we must update the shared memory structure to tell
614 * everyone else we no longer have a level II oplock on
615 * this open file. If local_request is true then token is
616 * the existing lock on the shared memory area.
619 if(!local_request
&& lock_share_entry_fsp(fsp
) == False
) {
620 DEBUG(0,("oplock_break_level2: unable to lock share entry for file %s\n", fsp
->fsp_name
));
625 if(remove_share_oplock(fsp
)==False
) {
626 DEBUG(0,("oplock_break_level2: unable to remove level II oplock for file %s\n", fsp
->fsp_name
));
629 if (!local_request
&& got_lock
)
630 unlock_share_entry_fsp(fsp
);
632 fsp
->oplock_type
= NO_OPLOCK
;
633 level_II_oplocks_open
--;
635 if(level_II_oplocks_open
< 0) {
636 DEBUG(0,("oplock_break_level2: level_II_oplocks_open < 0 (%d). PANIC ERROR\n",
637 level_II_oplocks_open
));
641 if( DEBUGLVL( 3 ) ) {
642 dbgtext( "oplock_break_level2: returning success for " );
643 dbgtext( "dev = %x, inode = %.0f, file_id = %lu\n", (unsigned int)dev
, (double)inode
, fsp
->file_id
);
644 dbgtext( "Current level II oplocks_open = %d\n", level_II_oplocks_open
);
650 /****************************************************************************
651 Process an oplock break directly.
652 ****************************************************************************/
654 static BOOL
oplock_break(SMB_DEV_T dev
, SMB_INO_T inode
, unsigned long file_id
, BOOL local_request
)
656 extern uint32 global_client_caps
;
657 extern struct current_user current_user
;
660 files_struct
*fsp
= NULL
;
662 BOOL shutdown_server
= False
;
663 BOOL oplock_timeout
= False
;
664 connection_struct
*saved_user_conn
;
665 connection_struct
*saved_fsp_conn
;
668 int timeout
= (OPLOCK_BREAK_TIMEOUT
* 1000);
672 if((fsp
= initial_break_processing(dev
, inode
, file_id
)) == NULL
)
676 * Deal with a level II oplock going break to none separately.
679 if (LEVEL_II_OPLOCK_TYPE(fsp
->oplock_type
))
680 return oplock_break_level2(fsp
, local_request
, -1);
682 /* Mark the oplock break as sent - we don't want to send twice! */
683 if (fsp
->sent_oplock_break
) {
684 if( DEBUGLVL( 0 ) ) {
685 dbgtext( "oplock_break: ERROR: oplock_break already sent for " );
686 dbgtext( "file %s ", fsp
->fsp_name
);
687 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu)\n", (unsigned int)dev
, (double)inode
, fsp
->file_id
);
691 * We have to fail the open here as we cannot send another oplock break on
692 * this file whilst we are awaiting a response from the client - neither
693 * can we allow another open to succeed while we are waiting for the client.
698 if(global_oplock_break
) {
699 DEBUG(0,("ABORT : ABORT : recursion in oplock_break !!!!!\n"));
704 * Now comes the horrid part. We must send an oplock break to the client,
705 * and then process incoming messages until we get a close or oplock release.
706 * At this point we know we need a new inbuf/outbuf buffer pair.
707 * We cannot use these staticaly as we may recurse into here due to
708 * messages crossing on the wire.
711 if((inbuf
= (char *)malloc(BUFFER_SIZE
+ LARGE_WRITEX_HDR_SIZE
+ SAFETY_MARGIN
))==NULL
) {
712 DEBUG(0,("oplock_break: malloc fail for input buffer.\n"));
716 if((outbuf
= (char *)malloc(BUFFER_SIZE
+ LARGE_WRITEX_HDR_SIZE
+ SAFETY_MARGIN
))==NULL
) {
717 DEBUG(0,("oplock_break: malloc fail for output buffer.\n"));
723 * If we are sending an oplock break due to an SMB sent
724 * by our own client we ensure that we wait at leat
725 * lp_oplock_break_wait_time() milliseconds before sending
726 * the packet. Sending the packet sooner can break Win9x
727 * and has reported to cause problems on NT. JRA.
730 wait_before_sending_break(local_request
);
732 /* Prepare the SMBlockingX message. */
734 if ((global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
735 !koplocks
&& /* NOTE: we force levelII off for kernel oplocks - this will change when it is supported */
736 lp_level2_oplocks(SNUM(fsp
->conn
))) {
737 using_levelII
= True
;
739 using_levelII
= False
;
742 prepare_break_message( outbuf
, fsp
, using_levelII
);
743 /* Remember if we just sent a break to level II on this file. */
744 fsp
->sent_oplock_break
= using_levelII
? LEVEL_II_BREAK_SENT
:EXCLUSIVE_BREAK_SENT
;
746 if (!send_smb(smbd_server_fd(), outbuf
))
747 exit_server("oplock_break: send_smb failed.\n");
749 /* We need this in case a readraw crosses on the wire. */
750 global_oplock_break
= True
;
752 /* Process incoming messages. */
755 * JRA - If we don't get a break from the client in OPLOCK_BREAK_TIMEOUT
756 * seconds we should just die....
759 start_time
= time(NULL
);
762 * Save the information we need to re-become the
763 * user, then unbecome the user whilst we're doing this.
765 saved_user_conn
= current_user
.conn
;
766 saved_vuid
= current_user
.vuid
;
767 saved_fsp_conn
= fsp
->conn
;
768 change_to_root_user();
769 vfs_GetWd(saved_fsp_conn
,saved_dir
);
770 /* Save the chain fnum. */
774 * From Charles Hoch <hoch@exemplary.com>. If the break processing
775 * code closes the file (as it often does), then the fsp pointer here
776 * points to free()'d memory. We *must* revalidate fsp each time
780 pstrcpy(file_name
, fsp
->fsp_name
);
782 while((fsp
= initial_break_processing(dev
, inode
, file_id
)) &&
783 OPEN_FSP(fsp
) && EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
784 if(receive_smb(smbd_server_fd(),inbuf
, timeout
) == False
) {
786 * Die if we got an error.
789 if (smb_read_error
== READ_EOF
) {
790 DEBUG( 0, ( "oplock_break: end of file from client\n" ) );
791 shutdown_server
= True
;
792 } else if (smb_read_error
== READ_ERROR
) {
793 DEBUG( 0, ("oplock_break: receive_smb error (%s)\n", strerror(errno
)) );
794 shutdown_server
= True
;
795 } else if (smb_read_error
== READ_TIMEOUT
) {
796 DEBUG( 0, ( "oplock_break: receive_smb timed out after %d seconds.\n", OPLOCK_BREAK_TIMEOUT
) );
797 oplock_timeout
= True
;
800 DEBUGADD( 0, ( "oplock_break failed for file %s ", file_name
) );
801 DEBUGADD( 0, ( "(dev = %x, inode = %.0f, file_id = %lu).\n",
802 (unsigned int)dev
, (double)inode
, file_id
));
808 * There are certain SMB requests that we shouldn't allow
809 * to recurse. opens, renames and deletes are the obvious
810 * ones. This is handled in the switch_message() function.
811 * If global_oplock_break is set they will push the packet onto
812 * the pending smb queue and return -1 (no reply).
816 process_smb(inbuf
, outbuf
);
819 * Die if we go over the time limit.
822 if((time(NULL
) - start_time
) > OPLOCK_BREAK_TIMEOUT
) {
823 if( DEBUGLVL( 0 ) ) {
824 dbgtext( "oplock_break: no break received from client " );
825 dbgtext( "within %d seconds.\n", OPLOCK_BREAK_TIMEOUT
);
826 dbgtext( "oplock_break failed for file %s ", fsp
->fsp_name
);
827 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu).\n",
828 (unsigned int)dev
, (double)inode
, file_id
);
830 oplock_timeout
= True
;
836 * Go back to being the user who requested the oplock
839 if((saved_user_conn
!= NULL
) && (saved_vuid
!= UID_FIELD_INVALID
) && !change_to_user(saved_user_conn
, saved_vuid
)) {
840 DEBUG( 0, ( "oplock_break: unable to re-become user!" ) );
841 DEBUGADD( 0, ( "Shutting down server\n" ) );
843 exit_server("unable to re-become user");
846 /* Including the directory. */
847 vfs_ChDir(saved_fsp_conn
,saved_dir
);
849 /* Restore the chain fnum. */
850 file_chain_restore();
852 /* Free the buffers we've been using to recurse. */
856 /* We need this in case a readraw crossed on the wire. */
857 if(global_oplock_break
)
858 global_oplock_break
= False
;
861 * If the client timed out then clear the oplock (or go to level II)
862 * and continue. This seems to be what NT does and is better than dropping
866 if(oplock_timeout
&& (fsp
= initial_break_processing(dev
, inode
, file_id
)) &&
867 OPEN_FSP(fsp
) && EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
868 DEBUG(0,("oplock_break: client failure in oplock break in file %s\n", fsp
->fsp_name
));
869 remove_oplock(fsp
,True
);
870 global_client_failed_oplock_break
= True
; /* Never grant this client an oplock again. */
874 * If the client had an error we must die.
877 if(shutdown_server
) {
878 DEBUG( 0, ( "oplock_break: client failure in break - " ) );
879 DEBUGADD( 0, ( "shutting down this smbd.\n" ) );
881 exit_server("oplock break failure");
884 /* Santity check - remove this later. JRA */
885 if(exclusive_oplocks_open
< 0) {
886 DEBUG(0,("oplock_break: exclusive_oplocks_open < 0 (%d). PANIC ERROR\n", exclusive_oplocks_open
));
890 if( DEBUGLVL( 3 ) ) {
891 dbgtext( "oplock_break: returning success for " );
892 dbgtext( "dev = %x, inode = %.0f, file_id = %lu\n", (unsigned int)dev
, (double)inode
, file_id
);
893 dbgtext( "Current exclusive_oplocks_open = %d\n", exclusive_oplocks_open
);
899 /****************************************************************************
900 Send an oplock break message to another smbd process. If the oplock is held
901 by the local smbd then call the oplock break function directly.
902 ****************************************************************************/
904 BOOL
request_oplock_break(share_mode_entry
*share_entry
)
906 char op_break_msg
[OPLOCK_BREAK_MSG_LEN
];
907 struct sockaddr_in addr_out
;
908 pid_t pid
= sys_getpid();
911 SMB_DEV_T dev
= share_entry
->dev
;
912 SMB_INO_T inode
= share_entry
->inode
;
913 unsigned long file_id
= share_entry
->share_file_id
;
915 if(pid
== share_entry
->pid
) {
916 /* We are breaking our own oplock, make sure it's us. */
917 if(share_entry
->op_port
!= global_oplock_port
) {
918 DEBUG(0,("request_oplock_break: corrupt share mode entry - pid = %d, port = %d \
919 should be %d\n", (int)pid
, share_entry
->op_port
, global_oplock_port
));
923 DEBUG(5,("request_oplock_break: breaking our own oplock\n"));
925 #if 1 /* JRA PARANOIA TEST.... */
927 files_struct
*fsp
= file_find_dif(dev
, inode
, file_id
);
929 DEBUG(0,("request_oplock_break: PANIC : breaking our own oplock requested for \
930 dev = %x, inode = %.0f, file_id = %lu and no fsp found !\n",
931 (unsigned int)dev
, (double)inode
, file_id
));
932 smb_panic("request_oplock_break: no fsp found for our own oplock\n");
935 #endif /* END JRA PARANOIA TEST... */
937 /* Call oplock break direct. */
938 return oplock_break(dev
, inode
, file_id
, True
);
941 /* We need to send a OPLOCK_BREAK_CMD message to the port in the share mode entry. */
943 if (LEVEL_II_OPLOCK_TYPE(share_entry
->op_type
)) {
944 SSVAL(op_break_msg
,OPBRK_MESSAGE_CMD_OFFSET
,LEVEL_II_OPLOCK_BREAK_CMD
);
946 SSVAL(op_break_msg
,OPBRK_MESSAGE_CMD_OFFSET
,OPLOCK_BREAK_CMD
);
949 memcpy(op_break_msg
+OPLOCK_BREAK_PID_OFFSET
,(char *)&pid
,sizeof(pid
));
950 memcpy(op_break_msg
+OPLOCK_BREAK_DEV_OFFSET
,(char *)&dev
,sizeof(dev
));
951 memcpy(op_break_msg
+OPLOCK_BREAK_INODE_OFFSET
,(char *)&inode
,sizeof(inode
));
952 memcpy(op_break_msg
+OPLOCK_BREAK_FILEID_OFFSET
,(char *)&file_id
,sizeof(file_id
));
954 /* Set the address and port. */
955 memset((char *)&addr_out
,'\0',sizeof(addr_out
));
956 addr_out
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
957 addr_out
.sin_port
= htons( share_entry
->op_port
);
958 addr_out
.sin_family
= AF_INET
;
960 if( DEBUGLVL( 3 ) ) {
961 dbgtext( "request_oplock_break: sending a oplock break message to " );
962 dbgtext( "pid %d on port %d ", (int)share_entry
->pid
, share_entry
->op_port
);
963 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
964 (unsigned int)dev
, (double)inode
, file_id
);
967 if(sys_sendto(oplock_sock
,op_break_msg
,OPLOCK_BREAK_MSG_LEN
,0,
968 (struct sockaddr
*)&addr_out
,sizeof(addr_out
)) < 0) {
969 if( DEBUGLVL( 0 ) ) {
970 dbgtext( "request_oplock_break: failed when sending a oplock " );
971 dbgtext( "break message to pid %d ", (int)share_entry
->pid
);
972 dbgtext( "on port %d ", share_entry
->op_port
);
973 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
974 (unsigned int)dev
, (double)inode
, file_id
);
975 dbgtext( "Error was %s\n", strerror(errno
) );
981 * If we just sent a message to a level II oplock share entry then
982 * we are done and may return.
985 if (LEVEL_II_OPLOCK_TYPE(share_entry
->op_type
)) {
986 DEBUG(3,("request_oplock_break: sent break message to level II entry.\n"));
991 * Now we must await the oplock broken message coming back
992 * from the target smbd process. Timeout if it fails to
993 * return in (OPLOCK_BREAK_TIMEOUT + OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR) seconds.
994 * While we get messages that aren't ours, loop.
997 start_time
= time(NULL
);
998 time_left
= OPLOCK_BREAK_TIMEOUT
+OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR
;
1000 while(time_left
>= 0) {
1001 char op_break_reply
[OPBRK_CMD_HEADER_LEN
+OPLOCK_BREAK_MSG_LEN
];
1002 uint16 reply_from_port
;
1003 char *reply_msg_start
;
1005 if(receive_local_message(op_break_reply
, sizeof(op_break_reply
),
1006 time_left
? time_left
* 1000 : 1) == False
) {
1007 if(smb_read_error
== READ_TIMEOUT
) {
1008 if( DEBUGLVL( 0 ) ) {
1009 dbgtext( "request_oplock_break: no response received to oplock " );
1010 dbgtext( "break request to pid %d ", (int)share_entry
->pid
);
1011 dbgtext( "on port %d ", share_entry
->op_port
);
1012 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1013 (unsigned int)dev
, (double)inode
, file_id
);
1017 * This is a hack to make handling of failing clients more robust.
1018 * If a oplock break response message is not received in the timeout
1019 * period we may assume that the smbd servicing that client holding
1020 * the oplock has died and the client changes were lost anyway, so
1021 * we should continue to try and open the file.
1025 if( DEBUGLVL( 0 ) ) {
1026 dbgtext( "request_oplock_break: error in response received " );
1027 dbgtext( "to oplock break request 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
) );
1037 reply_from_port
= SVAL(op_break_reply
,OPBRK_CMD_PORT_OFFSET
);
1038 reply_msg_start
= &op_break_reply
[OPBRK_CMD_HEADER_LEN
];
1041 * Test to see if this is the reply we are awaiting.
1043 if((SVAL(reply_msg_start
,OPBRK_MESSAGE_CMD_OFFSET
) & CMD_REPLY
) &&
1044 ((SVAL(reply_msg_start
,OPBRK_MESSAGE_CMD_OFFSET
) & ~CMD_REPLY
) == OPLOCK_BREAK_CMD
) &&
1045 (reply_from_port
== share_entry
->op_port
) &&
1046 (memcmp(&reply_msg_start
[OPLOCK_BREAK_PID_OFFSET
], &op_break_msg
[OPLOCK_BREAK_PID_OFFSET
],
1047 OPLOCK_BREAK_MSG_LEN
- OPLOCK_BREAK_PID_OFFSET
) == 0)) {
1050 * This is the reply we've been waiting for.
1055 * This is another message - a break request.
1056 * Note that both kernel oplock break requests
1057 * and UDP inter-smbd oplock break requests will
1058 * be processed here.
1060 * Process it to prevent potential deadlock.
1061 * Note that the code in switch_message() prevents
1062 * us from recursing into here as any SMB requests
1063 * we might process that would cause another oplock
1064 * break request to be made will be queued.
1068 process_local_message(op_break_reply
, sizeof(op_break_reply
));
1071 time_left
-= (time(NULL
) - start_time
);
1074 DEBUG(3,("request_oplock_break: broke oplock.\n"));
1079 /****************************************************************************
1080 Attempt to break an oplock on a file (if oplocked).
1081 Returns True if the file was closed as a result of
1082 the oplock break, False otherwise.
1083 Used as a last ditch attempt to free a space in the
1084 file table when we have run out.
1085 ****************************************************************************/
1087 BOOL
attempt_close_oplocked_file(files_struct
*fsp
)
1089 DEBUG(5,("attempt_close_oplocked_file: checking file %s.\n", fsp
->fsp_name
));
1091 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !fsp
->sent_oplock_break
&& (fsp
->fd
!= -1)) {
1092 /* Try and break the oplock. */
1093 if (oplock_break(fsp
->dev
, fsp
->inode
, fsp
->file_id
, True
)) {
1094 if(file_find_fsp(fsp
) == NULL
) /* Did the oplock break close the file ? */
1102 /****************************************************************************
1103 This function is called on any file modification or lock request. If a file
1104 is level 2 oplocked then it must tell all other level 2 holders to break to none.
1105 ****************************************************************************/
1107 void release_level_2_oplocks_on_change(files_struct
*fsp
)
1109 share_mode_entry
*share_list
= NULL
;
1110 pid_t pid
= sys_getpid();
1112 int num_share_modes
= 0;
1116 * If this file is level II oplocked then we need
1117 * to grab the shared memory lock and inform all
1118 * other files with a level II lock that they need
1119 * to flush their read caches. We keep the lock over
1120 * the shared memory area whilst doing this.
1123 if (!LEVEL_II_OPLOCK_TYPE(fsp
->oplock_type
))
1126 if (lock_share_entry_fsp(fsp
) == False
) {
1127 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock share mode entry for file %s.\n", fsp
->fsp_name
));
1130 num_share_modes
= get_share_modes(fsp
->conn
, fsp
->dev
, fsp
->inode
, &share_list
);
1132 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
1135 for(i
= 0; i
< num_share_modes
; i
++) {
1136 share_mode_entry
*share_entry
= &share_list
[i
];
1139 * As there could have been multiple writes waiting at the lock_share_entry
1140 * gate we may not be the first to enter. Hence the state of the op_types
1141 * in the share mode entries may be partly NO_OPLOCK and partly LEVEL_II
1142 * oplock. It will do no harm to re-send break messages to those smbd's
1143 * that are still waiting their turn to remove their LEVEL_II state, and
1144 * also no harm to ignore existing NO_OPLOCK states. JRA.
1147 DEBUG(10,("release_level_2_oplocks_on_change: share_entry[%i]->op_type == %d\n",
1148 i
, share_entry
->op_type
));
1150 if (share_entry
->op_type
== NO_OPLOCK
)
1154 if (EXCLUSIVE_OPLOCK_TYPE(share_entry
->op_type
)) {
1155 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. share mode entry %d is an exlusive oplock !\n", i
));
1156 unlock_share_entry(fsp
->conn
, fsp
->dev
, fsp
->inode
);
1161 * Check if this is a file we have open (including the
1162 * file we've been called to do write_file on. If so
1163 * then break it directly without releasing the lock.
1166 if (pid
== share_entry
->pid
) {
1167 files_struct
*new_fsp
= file_find_dif(share_entry
->dev
, share_entry
->inode
, share_entry
->share_file_id
);
1169 /* Paranoia check... */
1170 if(new_fsp
== NULL
) {
1171 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. share mode entry %d is not a local file !\n", i
));
1172 unlock_share_entry(fsp
->conn
, fsp
->dev
, fsp
->inode
);
1176 DEBUG(10,("release_level_2_oplocks_on_change: breaking our own oplock.\n"));
1178 oplock_break_level2(new_fsp
, True
, token
);
1183 * This is a remote file and so we send an asynchronous
1187 DEBUG(10,("release_level_2_oplocks_on_change: breaking remote oplock.\n"));
1188 request_oplock_break(share_entry
);
1192 SAFE_FREE(share_list
);
1193 unlock_share_entry_fsp(fsp
);
1195 /* Paranoia check... */
1196 if (LEVEL_II_OPLOCK_TYPE(fsp
->oplock_type
)) {
1197 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. File %s still has a level II oplock.\n", fsp
->fsp_name
));
1198 smb_panic("release_level_2_oplocks_on_change");
1202 /****************************************************************************
1203 setup oplocks for this process
1204 ****************************************************************************/
1206 BOOL
init_oplocks(void)
1208 struct sockaddr_in sock_name
;
1209 socklen_t len
= sizeof(sock_name
);
1211 DEBUG(3,("open_oplock_ipc: opening loopback UDP socket.\n"));
1213 /* Open a lookback UDP socket on a random port. */
1214 oplock_sock
= open_socket_in(SOCK_DGRAM
, 0, 0, htonl(INADDR_LOOPBACK
),False
);
1215 if (oplock_sock
== -1) {
1216 DEBUG(0,("open_oplock_ipc: Failed to get local UDP socket for \
1217 address %lx. Error was %s\n", (long)htonl(INADDR_LOOPBACK
), strerror(errno
)));
1218 global_oplock_port
= 0;
1222 /* Find out the transient UDP port we have been allocated. */
1223 if(getsockname(oplock_sock
, (struct sockaddr
*)&sock_name
, &len
)<0) {
1224 DEBUG(0,("open_oplock_ipc: Failed to get local UDP port. Error was %s\n",
1228 global_oplock_port
= 0;
1231 global_oplock_port
= ntohs(sock_name
.sin_port
);
1233 if (lp_kernel_oplocks()) {
1234 #if HAVE_KERNEL_OPLOCKS_IRIX
1235 koplocks
= irix_init_kernel_oplocks();
1236 #elif HAVE_KERNEL_OPLOCKS_LINUX
1237 koplocks
= linux_init_kernel_oplocks();
1241 DEBUG(3,("open_oplock ipc: pid = %d, global_oplock_port = %u\n",
1242 (int)sys_getpid(), global_oplock_port
));