r6252: Re-order position of become_root() to allow directory to be
[tprouty/samba.git] / source / smbd / oplock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    oplock processing
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 1998 - 2001
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 /* Oplock ipc UDP socket. */
25 static int oplock_sock = -1;
26 uint16 global_oplock_port = 0;
27
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;
33
34 extern struct timeval smb_last_time;
35 extern uint32 global_client_caps;
36 extern struct current_user current_user;
37 extern int smb_read_error;
38
39 static struct kernel_oplocks *koplocks;
40
41 static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, BOOL local);
42
43 /****************************************************************************
44  Get the number of current exclusive oplocks.
45 ****************************************************************************/
46
47 int32 get_number_of_exclusive_open_oplocks(void)
48 {
49   return exclusive_oplocks_open;
50 }
51
52 /****************************************************************************
53  Return True if an oplock message is pending.
54 ****************************************************************************/
55
56 BOOL oplock_message_waiting(fd_set *fds)
57 {
58         if (koplocks && koplocks->msg_waiting(fds))
59                 return True;
60
61         if (FD_ISSET(oplock_sock, fds))
62                 return True;
63
64         return False;
65 }
66
67 /****************************************************************************
68  Read an oplock break message from either the oplock UDP fd or the
69  kernel (if kernel oplocks are supported).
70
71  If timeout is zero then *fds contains the file descriptors that
72  are ready to be read and acted upon. If timeout is non-zero then
73  *fds contains the file descriptors to be selected on for read.
74  The timeout is in milliseconds
75
76 ****************************************************************************/
77
78 BOOL receive_local_message( char *buffer, int buffer_len, int timeout)
79 {
80         struct sockaddr_in from;
81         socklen_t fromlen = sizeof(from);
82         int32 msg_len = 0;
83         fd_set fds;
84         int selrtn = -1;
85
86         FD_ZERO(&fds);
87         smb_read_error = 0;
88
89         /*
90          * We need to check for kernel oplocks before going into the select
91          * here, as the EINTR generated by the linux kernel oplock may have
92          * already been eaten. JRA.
93          */
94
95         if (koplocks && koplocks->msg_waiting(&fds)) {
96                 return koplocks->receive_message(&fds, buffer, buffer_len);
97         }
98
99         while (timeout > 0 && selrtn == -1) {
100                 struct timeval to;
101                 int maxfd = oplock_sock;
102                 time_t starttime = time(NULL);
103
104                 FD_ZERO(&fds);
105                 maxfd = setup_oplock_select_set(&fds);
106
107                 to.tv_sec = timeout / 1000;
108                 to.tv_usec = (timeout % 1000) * 1000;
109
110                 DEBUG(5,("receive_local_message: doing select with timeout of %d ms\n", timeout));
111
112                 selrtn = sys_select(maxfd+1,&fds,NULL,NULL,&to);
113
114                 if (selrtn == -1 && errno == EINTR) {
115
116                         /* could be a kernel oplock interrupt */
117                         if (koplocks && koplocks->msg_waiting(&fds)) {
118                                 return koplocks->receive_message(&fds, buffer, buffer_len);
119                         }
120
121                         /*
122                          * Linux 2.0.x seems to have a bug in that
123                          * it can return -1, EINTR with a timeout of zero.
124                          * Make sure we bail out here with a read timeout
125                          * if we got EINTR on a timeout of 1 or less.
126                          */
127
128                         if (timeout <= 1) {
129                                 smb_read_error = READ_TIMEOUT;
130                                 return False;
131                         }
132
133                         /* Not a kernel interrupt - could be a SIGUSR1 message. We must restart. */
134                         /* We need to decrement the timeout here. */
135                         timeout -= ((time(NULL) - starttime)*1000);
136                         if (timeout < 0)
137                                 timeout = 1;
138
139                         DEBUG(5,("receive_local_message: EINTR : new timeout %d ms\n", timeout));
140                         continue;
141                 }
142
143                 /* Check if error */
144                 if(selrtn == -1) {
145                         /* something is wrong. Maybe the socket is dead? */
146                         smb_read_error = READ_ERROR;
147                         return False;
148                 }
149
150                 /* Did we timeout ? */
151                 if (selrtn == 0) {
152                         smb_read_error = READ_TIMEOUT;
153                         return False;
154                 }
155         }
156
157         if (koplocks && koplocks->msg_waiting(&fds)) {
158                 return koplocks->receive_message(&fds, buffer, buffer_len);
159         }
160
161         if (!FD_ISSET(oplock_sock, &fds))
162                 return False;
163
164         /*
165          * From here down we deal with the smbd <--> smbd
166          * oplock break protocol only.
167          */
168
169         /*
170          * Read a loopback udp message.
171          */
172         msg_len = sys_recvfrom(oplock_sock, &buffer[OPBRK_CMD_HEADER_LEN],
173                                                 buffer_len - OPBRK_CMD_HEADER_LEN, 0, (struct sockaddr *)&from, &fromlen);
174
175         if(msg_len < 0) {
176                 DEBUG(0,("receive_local_message. Error in recvfrom. (%s).\n",strerror(errno)));
177                 return False;
178         }
179
180         /* Validate message length. */
181         if(msg_len > (buffer_len - OPBRK_CMD_HEADER_LEN)) {
182                 DEBUG(0,("receive_local_message: invalid msg_len (%d) max can be %d\n", msg_len,
183                         buffer_len  - OPBRK_CMD_HEADER_LEN));
184                 return False;
185         }
186
187         /* Validate message from address (must be localhost). */
188         if(from.sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
189                 DEBUG(0,("receive_local_message: invalid 'from' address \
190 (was %lx should be 127.0.0.1)\n", (long)from.sin_addr.s_addr));
191                 return False;
192         }
193
194         /* Setup the message header */
195         SIVAL(buffer,OPBRK_CMD_LEN_OFFSET,msg_len);
196         SSVAL(buffer,OPBRK_CMD_PORT_OFFSET,ntohs(from.sin_port));
197
198         return True;
199 }
200
201 /****************************************************************************
202  Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
203  disabled (just sets flags). Returns True if oplock set.
204 ****************************************************************************/
205
206 BOOL set_file_oplock(files_struct *fsp, int oplock_type)
207 {
208         if (koplocks && !koplocks->set_oplock(fsp, oplock_type))
209                 return False;
210
211         fsp->oplock_type = oplock_type;
212         fsp->sent_oplock_break = NO_BREAK_SENT;
213         if (oplock_type == LEVEL_II_OPLOCK)
214                 level_II_oplocks_open++;
215         else
216                 exclusive_oplocks_open++;
217
218         DEBUG(5,("set_file_oplock: granted oplock on file %s, dev = %x, inode = %.0f, file_id = %lu, \
219 tv_sec = %x, tv_usec = %x\n",
220                  fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id,
221                  (int)fsp->open_time.tv_sec, (int)fsp->open_time.tv_usec ));
222
223         return True;
224 }
225
226 /****************************************************************************
227  Attempt to release an oplock on a file. Decrements oplock count.
228 ****************************************************************************/
229
230 void release_file_oplock(files_struct *fsp)
231 {
232         if ((fsp->oplock_type != NO_OPLOCK) && koplocks)
233                 koplocks->release_oplock(fsp);
234
235         if (fsp->oplock_type == LEVEL_II_OPLOCK)
236                 level_II_oplocks_open--;
237         else if (fsp->oplock_type)
238                 exclusive_oplocks_open--;
239         
240         fsp->oplock_type = NO_OPLOCK;
241         fsp->sent_oplock_break = NO_BREAK_SENT;
242         
243         flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
244 }
245
246 /****************************************************************************
247  Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
248 ****************************************************************************/
249
250 static void downgrade_file_oplock(files_struct *fsp)
251 {
252         if (koplocks)
253                 koplocks->release_oplock(fsp);
254         fsp->oplock_type = LEVEL_II_OPLOCK;
255         exclusive_oplocks_open--;
256         level_II_oplocks_open++;
257         fsp->sent_oplock_break = NO_BREAK_SENT;
258 }
259
260 /****************************************************************************
261  Remove a file oplock. Copes with level II and exclusive.
262  Locks then unlocks the share mode lock. Client can decide to go directly
263  to none even if a "break-to-level II" was sent.
264 ****************************************************************************/
265
266 BOOL remove_oplock(files_struct *fsp, BOOL break_to_none)
267 {
268         SMB_DEV_T dev = fsp->dev;
269         SMB_INO_T inode = fsp->inode;
270         BOOL ret = True;
271
272         /* Remove the oplock flag from the sharemode. */
273         if (lock_share_entry_fsp(fsp) == False) {
274                 DEBUG(0,("remove_oplock: failed to lock share entry for file %s\n",
275                          fsp->fsp_name ));
276                 return False;
277         }
278
279         if (fsp->sent_oplock_break == EXCLUSIVE_BREAK_SENT || break_to_none) {
280                 /*
281                  * Deal with a reply when a break-to-none was sent.
282                  */
283
284                 if(remove_share_oplock(fsp)==False) {
285                         DEBUG(0,("remove_oplock: failed to remove share oplock for file %s fnum %d, \
286 dev = %x, inode = %.0f\n", fsp->fsp_name, fsp->fnum, (unsigned int)dev, (double)inode));
287                         ret = False;
288                 }
289
290                 release_file_oplock(fsp);
291         } else {
292                 /*
293                  * Deal with a reply when a break-to-level II was sent.
294                  */
295                 if(downgrade_share_oplock(fsp)==False) {
296                         DEBUG(0,("remove_oplock: failed to downgrade share oplock for file %s fnum %d, \
297 dev = %x, inode = %.0f\n", fsp->fsp_name, fsp->fnum, (unsigned int)dev, (double)inode));
298                         ret = False;
299                 }
300                 
301                 downgrade_file_oplock(fsp);
302         }
303
304         unlock_share_entry_fsp(fsp);
305         return ret;
306 }
307
308 /****************************************************************************
309  Setup the listening set of file descriptors for an oplock break
310  message either from the UDP socket or from the kernel. Returns the maximum
311  fd used.
312 ****************************************************************************/
313
314 int setup_oplock_select_set( fd_set *fds)
315 {
316         int maxfd = oplock_sock;
317
318         if(oplock_sock == -1)
319                 return 0;
320
321         FD_SET(oplock_sock,fds);
322
323         if (koplocks && koplocks->notification_fd != -1) {
324                 FD_SET(koplocks->notification_fd, fds);
325                 maxfd = MAX(maxfd, koplocks->notification_fd);
326         }
327
328         return maxfd;
329 }
330
331 /****************************************************************************
332  Process an oplock break message - whether it came from the UDP socket
333  or from the kernel.
334 ****************************************************************************/
335
336 BOOL process_local_message(char *buffer, int buf_size)
337 {
338         int32 msg_len;
339         uint16 from_port;
340         char *msg_start;
341         pid_t remotepid;
342         SMB_DEV_T dev;
343         SMB_INO_T inode;
344         unsigned long file_id;
345         uint16 break_cmd_type;
346
347         msg_len = IVAL(buffer,OPBRK_CMD_LEN_OFFSET);
348         from_port = SVAL(buffer,OPBRK_CMD_PORT_OFFSET);
349
350         msg_start = &buffer[OPBRK_CMD_HEADER_LEN];
351
352         DEBUG(5,("process_local_message: Got a message of length %d from port (%d)\n", 
353                 msg_len, from_port));
354
355         /* 
356          * Pull the info out of the requesting packet.
357          */
358
359         break_cmd_type = SVAL(msg_start,OPBRK_MESSAGE_CMD_OFFSET);
360
361         switch(break_cmd_type) {
362                 case KERNEL_OPLOCK_BREAK_CMD:
363                         if (!koplocks) {
364                                 DEBUG(0,("unexpected kernel oplock break!\n"));
365                                 break;
366                         } 
367                         if (!koplocks->parse_message(msg_start, msg_len, &inode, &dev, &file_id)) {
368                                 DEBUG(0,("kernel oplock break parse failure!\n"));
369                         }
370                         break;
371
372                 case OPLOCK_BREAK_CMD:
373                 case LEVEL_II_OPLOCK_BREAK_CMD:
374                 case ASYNC_LEVEL_II_OPLOCK_BREAK_CMD:
375
376                         /* Ensure that the msg length is correct. */
377                         if(msg_len != OPLOCK_BREAK_MSG_LEN) {
378                                 DEBUG(0,("process_local_message: incorrect length for OPLOCK_BREAK_CMD (was %d, should be %d).\n",
379                                         (int)msg_len, (int)OPLOCK_BREAK_MSG_LEN));
380                                 return False;
381                         }
382
383                         memcpy((char *)&remotepid, msg_start+OPLOCK_BREAK_PID_OFFSET,sizeof(remotepid));
384                         memcpy((char *)&inode, msg_start+OPLOCK_BREAK_INODE_OFFSET,sizeof(inode));
385                         memcpy((char *)&dev, msg_start+OPLOCK_BREAK_DEV_OFFSET,sizeof(dev));
386                         memcpy((char *)&file_id, msg_start+OPLOCK_BREAK_FILEID_OFFSET,sizeof(file_id));
387
388                         DEBUG(5,("process_local_message: (%s) oplock break request from \
389 pid %d, port %d, dev = %x, inode = %.0f, file_id = %lu\n",
390                                 (break_cmd_type == OPLOCK_BREAK_CMD) ? "exclusive" : "level II",
391                                 (int)remotepid, from_port, (unsigned int)dev, (double)inode, file_id));
392                         break;
393
394                 case RETRY_DEFERRED_OPEN_CMD:
395
396                         /* Request to retry and open that would return SHARING_VIOLATION. */
397                         if (msg_len != DEFERRED_OPEN_MSG_LEN) {
398                                 DEBUG(0,("process_local_message: incorrect length for RETRY_DEFERRED_OPEN_CMD (was %d, should be %d).\n",
399                                         (int)msg_len, (int)DEFERRED_OPEN_MSG_LEN));
400                                 return False;
401                         }
402                         {
403                                 uint16 mid;
404
405                                 memcpy((char *)&remotepid, msg_start+DEFERRED_OPEN_PID_OFFSET,sizeof(remotepid));
406                                 memcpy((char *)&inode, msg_start+DEFERRED_OPEN_INODE_OFFSET,sizeof(inode));
407                                 memcpy((char *)&dev, msg_start+DEFERRED_OPEN_DEV_OFFSET,sizeof(dev));
408                                 memcpy((char *)&mid, msg_start+DEFERRED_OPEN_MID_OFFSET,sizeof(mid));
409
410                                 DEBUG(5,("process_local_message: RETRY_DEFERRED_OPEN from \
411 pid %d, port %d, dev = %x, inode = %.0f, mid = %u\n",
412                                         (int)remotepid, from_port, (unsigned int)dev, (double)inode, (unsigned int)mid));
413
414                                 schedule_sharing_violation_open_smb_message(mid);
415                         }
416                         return True;
417
418                 /* 
419                  * Keep this as a debug case - eventually we can remove it.
420                  */
421                 case 0x8001:
422                         DEBUG(0,("process_local_message: Received unsolicited break \
423 reply - dumping info.\n"));
424
425                         if(msg_len != OPLOCK_BREAK_MSG_LEN) {
426                                 DEBUG(0,("process_local_message: ubr: incorrect length for reply \
427 (was %d, should be %d).\n", (int)msg_len, (int)OPLOCK_BREAK_MSG_LEN));
428                                 return False;
429                         }
430
431                         memcpy((char *)&inode, msg_start+OPLOCK_BREAK_INODE_OFFSET,sizeof(inode));
432                         memcpy((char *)&remotepid, msg_start+OPLOCK_BREAK_PID_OFFSET,sizeof(remotepid));
433                         memcpy((char *)&dev, msg_start+OPLOCK_BREAK_DEV_OFFSET,sizeof(dev));
434                         memcpy((char *)&file_id, msg_start+OPLOCK_BREAK_FILEID_OFFSET,sizeof(file_id));
435
436                         DEBUG(0,("process_local_message: unsolicited oplock break reply from \
437 pid %d, port %d, dev = %x, inode = %.0f, file_id = %lu\n",
438                                 (int)remotepid, from_port, (unsigned int)dev, (double)inode, file_id));
439
440                         return False;
441
442                 default:
443                         DEBUG(0,("process_local_message: unknown UDP message command code (%x) - ignoring.\n",
444                                 (unsigned int)SVAL(msg_start,0)));
445                         return False;
446         }
447
448         /*
449          * Now actually process the break request.
450          */
451
452         if((exclusive_oplocks_open + level_II_oplocks_open) != 0) {
453                 if (oplock_break(dev, inode, file_id, False) == False) {
454                         DEBUG(0,("process_local_message: oplock break failed.\n"));
455                         return False;
456                 }
457         } else {
458                 /*
459                  * If we have no record of any currently open oplocks,
460                  * it's not an error, as a close command may have
461                  * just been issued on the file that was oplocked.
462                  * Just log a message and return success in this case.
463                  */
464                 DEBUG(3,("process_local_message: oplock break requested with no outstanding \
465 oplocks. Returning success.\n"));
466         }
467
468         /* 
469          * Do the appropriate reply - none in the kernel or async level II case.
470          */
471
472         if(break_cmd_type == OPLOCK_BREAK_CMD || break_cmd_type == LEVEL_II_OPLOCK_BREAK_CMD) {
473                 struct sockaddr_in toaddr;
474
475                 /* Send the message back after OR'ing in the 'REPLY' bit. */
476                 SSVAL(msg_start,OPBRK_MESSAGE_CMD_OFFSET,break_cmd_type | CMD_REPLY);
477
478                 memset((char *)&toaddr,'\0',sizeof(toaddr));
479                 toaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
480                 toaddr.sin_port = htons(from_port);
481                 toaddr.sin_family = AF_INET;
482
483                 if(sys_sendto( oplock_sock, msg_start, OPLOCK_BREAK_MSG_LEN, 0,
484                                 (struct sockaddr *)&toaddr, sizeof(toaddr)) < 0) {
485                         DEBUG(0,("process_local_message: sendto process %d failed. Errno was %s\n",
486                                 (int)remotepid, strerror(errno)));
487                         return False;
488                 }
489
490                 DEBUG(5,("process_local_message: oplock break reply sent to \
491 pid %d, port %d, for file dev = %x, inode = %.0f, file_id = %lu\n",
492                         (int)remotepid, from_port, (unsigned int)dev, (double)inode, file_id));
493         }
494
495         return True;
496 }
497
498 /****************************************************************************
499  Set up an oplock break message.
500 ****************************************************************************/
501
502 static void prepare_break_message(char *outbuf, files_struct *fsp, BOOL level2)
503 {
504         memset(outbuf,'\0',smb_size);
505         set_message(outbuf,8,0,True);
506
507         SCVAL(outbuf,smb_com,SMBlockingX);
508         SSVAL(outbuf,smb_tid,fsp->conn->cnum);
509         SSVAL(outbuf,smb_pid,0xFFFF);
510         SSVAL(outbuf,smb_uid,0);
511         SSVAL(outbuf,smb_mid,0xFFFF);
512         SCVAL(outbuf,smb_vwv0,0xFF);
513         SSVAL(outbuf,smb_vwv2,fsp->fnum);
514         SCVAL(outbuf,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
515         SCVAL(outbuf,smb_vwv3+1,level2 ? OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
516 }
517
518 /****************************************************************************
519  Function to do the waiting before sending a local break.
520 ****************************************************************************/
521
522 static void wait_before_sending_break(BOOL local_request)
523 {
524         if(local_request) {
525                 struct timeval cur_tv;
526                 long wait_left = (long)lp_oplock_break_wait_time();
527
528                 if (wait_left == 0)
529                         return;
530
531                 GetTimeOfDay(&cur_tv);
532
533                 wait_left -= ((cur_tv.tv_sec - smb_last_time.tv_sec)*1000) +
534                 ((cur_tv.tv_usec - smb_last_time.tv_usec)/1000);
535
536                 if(wait_left > 0) {
537                         wait_left = MIN(wait_left, 1000);
538                         sys_usleep(wait_left * 1000);
539                 }
540         }
541 }
542
543 /****************************************************************************
544  Ensure that we have a valid oplock.
545 ****************************************************************************/
546
547 static files_struct *initial_break_processing(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
548 {
549         files_struct *fsp = NULL;
550
551         if( DEBUGLVL( 3 ) ) {
552                 dbgtext( "initial_break_processing: called for dev = %x, inode = %.0f file_id = %lu\n",
553                         (unsigned int)dev, (double)inode, file_id);
554                 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
555                         exclusive_oplocks_open, level_II_oplocks_open );
556         }
557
558         /*
559          * We need to search the file open table for the
560          * entry containing this dev and inode, and ensure
561          * we have an oplock on it.
562          */
563
564         fsp = file_find_dif(dev, inode, file_id);
565
566         if(fsp == NULL) {
567                 /* The file could have been closed in the meantime - return success. */
568                 if( DEBUGLVL( 3 ) ) {
569                         dbgtext( "initial_break_processing: cannot find open file with " );
570                         dbgtext( "dev = %x, inode = %.0f file_id = %lu", (unsigned int)dev,
571                                 (double)inode, file_id);
572                         dbgtext( "allowing break to succeed.\n" );
573                 }
574                 return NULL;
575         }
576
577         /* Ensure we have an oplock on the file */
578
579         /*
580          * There is a potential race condition in that an oplock could
581          * have been broken due to another udp request, and yet there are
582          * still oplock break messages being sent in the udp message
583          * queue for this file. So return true if we don't have an oplock,
584          * as we may have just freed it.
585          */
586
587         if(fsp->oplock_type == NO_OPLOCK) {
588                 if( DEBUGLVL( 3 ) ) {
589                         dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
590                         dbgtext( "(dev = %x, inode = %.0f, file_id = %lu) has no oplock.\n",
591                                 (unsigned int)dev, (double)inode, fsp->file_id );
592                         dbgtext( "Allowing break to succeed regardless.\n" );
593                 }
594                 return NULL;
595         }
596
597         return fsp;
598 }
599
600 /****************************************************************************
601  Process a level II oplock break directly.
602  We must call this function with the share mode entry locked.
603 ****************************************************************************/
604
605 static BOOL oplock_break_level2(files_struct *fsp, BOOL local_request)
606 {
607         char outbuf[128];
608         SMB_DEV_T dev = fsp->dev;
609         SMB_INO_T inode = fsp->inode;
610
611         /*
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.
616          */
617
618         if (global_client_caps & CAP_LEVEL_II_OPLOCKS) {
619                 BOOL sign_state;
620
621                 /*
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.
627                  */
628
629                 wait_before_sending_break(local_request);
630
631                 /* Prepare the SMBlockingX message. */
632                 prepare_break_message( outbuf, fsp, False);
633
634                 /* Save the server smb signing state. */
635                 sign_state = srv_oplock_set_signing(False);
636
637                 if (!send_smb(smbd_server_fd(), outbuf))
638                         exit_server("oplock_break_level2: send_smb failed.");
639
640                 /* Restore the sign state to what it was. */
641                 srv_oplock_set_signing(sign_state);
642         }
643
644         /*
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.
649          */
650
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 ));
653         }
654
655         release_file_oplock(fsp);
656
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();
661         }
662
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 );
667         }
668
669         return True;
670 }
671
672 /****************************************************************************
673  Process an oplock break directly.
674  This is always called with the share mode lock *NOT* held.
675 ****************************************************************************/
676
677 static BOOL oplock_break(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id, BOOL local_request)
678 {
679         char *inbuf = NULL;
680         char *outbuf = NULL;
681         files_struct *fsp = NULL;
682         time_t start_time;
683         BOOL shutdown_server = False;
684         BOOL oplock_timeout = False;
685         BOOL sign_state;
686         connection_struct *saved_user_conn;
687         connection_struct *saved_fsp_conn;
688         int saved_vuid;
689         pstring saved_dir; 
690         int timeout = (OPLOCK_BREAK_TIMEOUT * 1000);
691         pstring file_name;
692         BOOL using_levelII;
693
694         if((fsp = initial_break_processing(dev, inode, file_id)) == NULL)
695                 return True;
696
697         /*
698          * Deal with a level II oplock going break to none separately.
699          */
700
701         if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
702                 BOOL ret;
703                 /* We must always call oplock_break_level2() with
704                    the share mode entry locked. */
705                 if (lock_share_entry_fsp(fsp) == False) {
706                         DEBUG(0,("oplock_break: unable to lock share entry for file %s\n", fsp->fsp_name ));
707                         return False;
708                 }
709                 ret = oplock_break_level2(fsp, local_request);
710                 unlock_share_entry_fsp(fsp);
711                 return ret;
712         }
713
714         /* Mark the oplock break as sent - we don't want to send twice! */
715         if (fsp->sent_oplock_break) {
716                 if( DEBUGLVL( 0 ) ) {
717                         dbgtext( "oplock_break: ERROR: oplock_break already sent for " );
718                         dbgtext( "file %s ", fsp->fsp_name);
719                         dbgtext( "(dev = %x, inode = %.0f, file_id = %lu)\n", (unsigned int)dev, (double)inode, fsp->file_id );
720                 }
721
722                 /*
723                  * We have to fail the open here as we cannot send another oplock break on
724                  * this file whilst we are awaiting a response from the client - neither
725                  * can we allow another open to succeed while we are waiting for the client.
726                  */
727                 return False;
728         }
729
730         if(global_oplock_break) {
731                 DEBUG(0,("ABORT : ABORT : recursion in oplock_break !!!!!\n"));
732                 abort();
733         }
734
735         /*
736          * Now comes the horrid part. We must send an oplock break to the client,
737          * and then process incoming messages until we get a close or oplock release.
738          * At this point we know we need a new inbuf/outbuf buffer pair.
739          * We cannot use these staticaly as we may recurse into here due to
740          * messages crossing on the wire.
741          */
742
743         if((inbuf = (char *)SMB_MALLOC(BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE + SAFETY_MARGIN))==NULL) {
744                 DEBUG(0,("oplock_break: malloc fail for input buffer.\n"));
745                 return False;
746         }
747
748         if((outbuf = (char *)SMB_MALLOC(BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE + SAFETY_MARGIN))==NULL) {
749                 DEBUG(0,("oplock_break: malloc fail for output buffer.\n"));
750                 SAFE_FREE(inbuf);
751                 return False;
752         }
753
754         /*
755          * If we are sending an oplock break due to an SMB sent
756          * by our own client we ensure that we wait at leat
757          * lp_oplock_break_wait_time() milliseconds before sending
758          * the packet. Sending the packet sooner can break Win9x
759          * and has reported to cause problems on NT. JRA.
760          */
761
762         wait_before_sending_break(local_request);
763
764         /* Prepare the SMBlockingX message. */
765
766         if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) && 
767                         !koplocks && /* NOTE: we force levelII off for kernel oplocks - this will change when it is supported */
768                         lp_level2_oplocks(SNUM(fsp->conn))) {
769                 using_levelII = True;
770         } else {
771                 using_levelII = False;
772         }
773
774         prepare_break_message( outbuf, fsp, using_levelII);
775         /* Remember if we just sent a break to level II on this file. */
776         fsp->sent_oplock_break = using_levelII? LEVEL_II_BREAK_SENT:EXCLUSIVE_BREAK_SENT;
777
778         /* Save the server smb signing state. */
779         sign_state = srv_oplock_set_signing(False);
780
781         if (!send_smb(smbd_server_fd(), outbuf)) {
782                 srv_oplock_set_signing(sign_state);
783                 exit_server("oplock_break: send_smb failed.");
784         }
785
786         /* Restore the sign state to what it was. */
787         srv_oplock_set_signing(sign_state);
788
789         /* We need this in case a readraw crosses on the wire. */
790         global_oplock_break = True;
791  
792         /* Process incoming messages. */
793
794         /*
795          * JRA - If we don't get a break from the client in OPLOCK_BREAK_TIMEOUT
796          * seconds we should just die....
797          */
798
799         start_time = time(NULL);
800
801         /*
802          * Save the information we need to re-become the
803          * user, then unbecome the user whilst we're doing this.
804          */
805         saved_user_conn = current_user.conn;
806         saved_vuid = current_user.vuid;
807         saved_fsp_conn = fsp->conn;
808         /*
809          * Initialize saved_dir to something sensible: vfs_GetWd may not work well
810          * for root: the directory may be NFS-mounted and exported with root_squash
811          * (so has no root access).
812          */
813         pstrcpy(saved_dir,saved_fsp_conn->connectpath);
814         vfs_GetWd(saved_fsp_conn,saved_dir);
815         /* Save the chain fnum. */
816         file_chain_save();
817
818         pstrcpy(file_name, fsp->fsp_name);
819
820         change_to_root_user();
821
822         /*
823          * From Charles Hoch <hoch@exemplary.com>. If the break processing
824          * code closes the file (as it often does), then the fsp pointer here
825          * points to free()'d memory. We *must* revalidate fsp each time
826          * around the loop.
827          */
828
829         while((fsp = initial_break_processing(dev, inode, file_id)) &&
830                         OPEN_FSP(fsp) && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
831                 if(receive_smb(smbd_server_fd(),inbuf, timeout) == False) {
832                         /*
833                          * Die if we got an error.
834                          */
835
836                         if (smb_read_error == READ_EOF) {
837                                 DEBUG( 0, ( "oplock_break: end of file from client\n" ) );
838                                 shutdown_server = True;
839                         } else if (smb_read_error == READ_ERROR) {
840                                 DEBUG( 0, ("oplock_break: receive_smb error (%s)\n", strerror(errno)) );
841                                 shutdown_server = True;
842                         } else if (smb_read_error == READ_BAD_SIG) {
843                                 DEBUG( 0, ("oplock_break: bad signature from client\n" ));
844                                 shutdown_server = True;
845                         } else if (smb_read_error == READ_TIMEOUT) {
846                                 DEBUG( 0, ( "oplock_break: receive_smb timed out after %d seconds.\n", OPLOCK_BREAK_TIMEOUT ) );
847                                 oplock_timeout = True;
848                         }
849
850                         DEBUGADD( 0, ( "oplock_break failed for file %s ", file_name ) );
851                         DEBUGADD( 0, ( "(dev = %x, inode = %.0f, file_id = %lu).\n",
852                                 (unsigned int)dev, (double)inode, file_id));
853
854                         break;
855                 }
856
857                 /*
858                  * There are certain SMB requests that we shouldn't allow
859                  * to recurse. opens, renames and deletes are the obvious
860                  * ones. This is handled in the switch_message() function.
861                  * If global_oplock_break is set they will push the packet onto
862                  * the pending smb queue and return -1 (no reply).
863                  * JRA.
864                  */
865
866                 process_smb(inbuf, outbuf);
867
868                 /*
869                  * Die if we go over the time limit.
870                  */
871
872                 if((time(NULL) - start_time) > OPLOCK_BREAK_TIMEOUT) {
873                         if( DEBUGLVL( 0 ) ) {
874                                 dbgtext( "oplock_break: no break received from client " );
875                                 dbgtext( "within %d seconds.\n", OPLOCK_BREAK_TIMEOUT );
876                                 dbgtext( "oplock_break failed for file %s ", fsp->fsp_name );
877                                 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu).\n",  
878                                         (unsigned int)dev, (double)inode, file_id );
879                         }
880                         oplock_timeout = True;
881                         break;
882                 }
883         }
884
885         /*
886          * Go back to being the user who requested the oplock
887          * break.
888          */
889         if((saved_user_conn != NULL) && (saved_vuid != UID_FIELD_INVALID) && !change_to_user(saved_user_conn, saved_vuid)) {
890                 DEBUG( 0, ( "oplock_break: unable to re-become user!" ) );
891                 DEBUGADD( 0, ( "Shutting down server\n" ) );
892                 close(oplock_sock);
893                 exit_server("unable to re-become user");
894         }
895
896         /* Including the directory. */
897         vfs_ChDir(saved_fsp_conn,saved_dir);
898
899         /* Restore the chain fnum. */
900         file_chain_restore();
901
902         /* Free the buffers we've been using to recurse. */
903         SAFE_FREE(inbuf);
904         SAFE_FREE(outbuf);
905
906         /* We need this in case a readraw crossed on the wire. */
907         if(global_oplock_break)
908                 global_oplock_break = False;
909
910         /*
911          * If the client timed out then clear the oplock (or go to level II)
912          * and continue. This seems to be what NT does and is better than dropping
913          * the connection.
914          */
915
916         if(oplock_timeout && (fsp = initial_break_processing(dev, inode, file_id)) &&
917                         OPEN_FSP(fsp) && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
918                 DEBUG(0,("oplock_break: client failure in oplock break in file %s\n", fsp->fsp_name));
919                 remove_oplock(fsp,True);
920 #if FASCIST_OPLOCK_BACKOFF
921                 global_client_failed_oplock_break = True; /* Never grant this client an oplock again. */
922 #endif
923         }
924
925         /*
926          * If the client had an error we must die.
927          */
928
929         if(shutdown_server) {
930                 DEBUG( 0, ( "oplock_break: client failure in break - " ) );
931                 DEBUGADD( 0, ( "shutting down this smbd.\n" ) );
932                 close(oplock_sock);
933                 exit_server("oplock break failure");
934         }
935
936         /* Santity check - remove this later. JRA */
937         if(exclusive_oplocks_open < 0) {
938                 DEBUG(0,("oplock_break: exclusive_oplocks_open < 0 (%d). PANIC ERROR\n", exclusive_oplocks_open));
939                 abort();
940         }
941
942         /* We know we have no saved errors here. */
943         set_saved_error_triple(0, 0, NT_STATUS_OK);
944
945         if( DEBUGLVL( 3 ) ) {
946                 dbgtext( "oplock_break: returning success for " );
947                 dbgtext( "dev = %x, inode = %.0f, file_id = %lu\n", (unsigned int)dev, (double)inode, file_id );
948                 dbgtext( "Current exclusive_oplocks_open = %d\n", exclusive_oplocks_open );
949         }
950
951         return True;
952 }
953
954 /****************************************************************************
955  Send an oplock break message to another smbd process. If the oplock is held 
956  by the local smbd then call the oplock break function directly.
957  This function is called with no share locks held.
958 ****************************************************************************/
959
960 BOOL request_oplock_break(share_mode_entry *share_entry)
961 {
962         char op_break_msg[OPLOCK_BREAK_MSG_LEN];
963         struct sockaddr_in addr_out;
964         pid_t pid = sys_getpid();
965         time_t start_time;
966         int time_left;
967         SMB_DEV_T dev = share_entry->dev;
968         SMB_INO_T inode = share_entry->inode;
969         unsigned long file_id = share_entry->share_file_id;
970         uint16 break_cmd_type;
971
972         if(pid == share_entry->pid) {
973                 /* We are breaking our own oplock, make sure it's us. */
974                 if(share_entry->op_port != global_oplock_port) {
975                         DEBUG(0,("request_oplock_break: corrupt share mode entry - pid = %d, port = %d \
976 should be %d\n", (int)pid, share_entry->op_port, global_oplock_port));
977                         return False;
978                 }
979
980                 DEBUG(5,("request_oplock_break: breaking our own oplock\n"));
981
982 #if 1 /* JRA PARANOIA TEST.... */
983                 {
984                         files_struct *fsp = file_find_dif(dev, inode, file_id);
985                         if (!fsp) {
986                                 DEBUG(0,("request_oplock_break: PANIC : breaking our own oplock requested for \
987 dev = %x, inode = %.0f, file_id = %lu and no fsp found !\n",
988             (unsigned int)dev, (double)inode, file_id ));
989                                 smb_panic("request_oplock_break: no fsp found for our own oplock\n");
990                         }
991                 }
992 #endif /* END JRA PARANOIA TEST... */
993
994                 /* Call oplock break direct. */
995                 return oplock_break(dev, inode, file_id, True);
996         }
997
998         /* We need to send a OPLOCK_BREAK_CMD message to the port in the share mode entry. */
999
1000         if (LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) {
1001                 break_cmd_type = LEVEL_II_OPLOCK_BREAK_CMD;
1002         } else {
1003                 break_cmd_type = OPLOCK_BREAK_CMD;
1004         }
1005
1006         SSVAL(op_break_msg,OPBRK_MESSAGE_CMD_OFFSET,break_cmd_type);
1007         memcpy(op_break_msg+OPLOCK_BREAK_PID_OFFSET,(char *)&pid,sizeof(pid));
1008         memcpy(op_break_msg+OPLOCK_BREAK_DEV_OFFSET,(char *)&dev,sizeof(dev));
1009         memcpy(op_break_msg+OPLOCK_BREAK_INODE_OFFSET,(char *)&inode,sizeof(inode));
1010         memcpy(op_break_msg+OPLOCK_BREAK_FILEID_OFFSET,(char *)&file_id,sizeof(file_id));
1011
1012         /* Set the address and port. */
1013         memset((char *)&addr_out,'\0',sizeof(addr_out));
1014         addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1015         addr_out.sin_port = htons( share_entry->op_port );
1016         addr_out.sin_family = AF_INET;
1017    
1018         if( DEBUGLVL( 3 ) ) {
1019                 dbgtext( "request_oplock_break: sending a synchronous oplock break message to " );
1020                 dbgtext( "pid %d on port %d ", (int)share_entry->pid, share_entry->op_port );
1021                 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1022             (unsigned int)dev, (double)inode, file_id );
1023         }
1024
1025         if(sys_sendto(oplock_sock,op_break_msg,OPLOCK_BREAK_MSG_LEN,0,
1026                         (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0) {
1027                 if( DEBUGLVL( 0 ) ) {
1028                         dbgtext( "request_oplock_break: failed when sending a oplock " );
1029                         dbgtext( "break message to pid %d ", (int)share_entry->pid );
1030                         dbgtext( "on port %d ", share_entry->op_port );
1031                         dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1032                                 (unsigned int)dev, (double)inode, file_id );
1033                         dbgtext( "Error was %s\n", strerror(errno) );
1034                 }
1035                 return False;
1036         }
1037
1038         /*
1039          * Now we must await the oplock broken message coming back
1040          * from the target smbd process. Timeout if it fails to
1041          * return in (OPLOCK_BREAK_TIMEOUT + OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR) seconds.
1042          * While we get messages that aren't ours, loop.
1043          */
1044
1045         start_time = time(NULL);
1046         time_left = OPLOCK_BREAK_TIMEOUT+OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR;
1047
1048         while(time_left >= 0) {
1049                 char op_break_reply[OPBRK_CMD_HEADER_LEN+OPLOCK_BREAK_MSG_LEN];
1050                 uint16 reply_from_port;
1051                 char *reply_msg_start;
1052
1053                 if(receive_local_message(op_break_reply, sizeof(op_break_reply),
1054                                 time_left ? time_left * 1000 : 1) == False) {
1055                         if(smb_read_error == READ_TIMEOUT) {
1056                                 if( DEBUGLVL( 0 ) ) {
1057                                         dbgtext( "request_oplock_break: no response received to oplock " );
1058                                         dbgtext( "break request to pid %d ", (int)share_entry->pid );
1059                                         dbgtext( "on port %d ", share_entry->op_port );
1060                                         dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1061                                                         (unsigned int)dev, (double)inode, file_id );
1062                                 }
1063
1064                                 /*
1065                                  * This is a hack to make handling of failing clients more robust.
1066                                  * If a oplock break response message is not received in the timeout
1067                                  * period we may assume that the smbd servicing that client holding
1068                                  * the oplock has died and the client changes were lost anyway, so
1069                                  * we should continue to try and open the file.
1070                                  */
1071                                 break;
1072                         } else {
1073                                 if( DEBUGLVL( 0 ) ) {
1074                                         dbgtext( "request_oplock_break: error in response received " );
1075                                         dbgtext( "to oplock break request to pid %d ", (int)share_entry->pid );
1076                                         dbgtext( "on port %d ", share_entry->op_port );
1077                                         dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1078                                                 (unsigned int)dev, (double)inode, file_id );
1079                                         dbgtext( "Error was (%s).\n", strerror(errno) );
1080                                 }
1081                         }
1082                         return False;
1083                 }
1084
1085                 reply_from_port = SVAL(op_break_reply,OPBRK_CMD_PORT_OFFSET);
1086                 reply_msg_start = &op_break_reply[OPBRK_CMD_HEADER_LEN];
1087
1088                 /*
1089                  * Test to see if this is the reply we are awaiting (ie. the one we sent with the CMD_REPLY flag OR'ed in).
1090                  */
1091                 if((SVAL(reply_msg_start,OPBRK_MESSAGE_CMD_OFFSET) & CMD_REPLY) &&
1092                         ((SVAL(reply_msg_start,OPBRK_MESSAGE_CMD_OFFSET) & ~CMD_REPLY) == break_cmd_type) &&
1093                         (reply_from_port == share_entry->op_port) && 
1094                         (memcmp(&reply_msg_start[OPLOCK_BREAK_PID_OFFSET], &op_break_msg[OPLOCK_BREAK_PID_OFFSET],
1095                                 OPLOCK_BREAK_MSG_LEN - OPLOCK_BREAK_PID_OFFSET) == 0)) {
1096
1097                         /*
1098                          * This is the reply we've been waiting for.
1099                          */
1100                         break;
1101                 } else {
1102                         /*
1103                          * This is another message - a break request.
1104                          * Note that both kernel oplock break requests
1105                          * and UDP inter-smbd oplock break requests will
1106                          * be processed here.
1107                          *
1108                          * Process it to prevent potential deadlock.
1109                          * Note that the code in switch_message() prevents
1110                          * us from recursing into here as any SMB requests
1111                          * we might process that would cause another oplock
1112                          * break request to be made will be queued.
1113                          * JRA.
1114                          */
1115
1116                         process_local_message(op_break_reply, sizeof(op_break_reply));
1117                 }
1118
1119                 time_left -= (time(NULL) - start_time);
1120         }
1121
1122         DEBUG(3,("request_oplock_break: broke oplock.\n"));
1123
1124         return True;
1125 }
1126
1127 /****************************************************************************
1128   Attempt to break an oplock on a file (if oplocked).
1129   Returns True if the file was closed as a result of
1130   the oplock break, False otherwise.
1131   Used as a last ditch attempt to free a space in the 
1132   file table when we have run out.
1133 ****************************************************************************/
1134
1135 BOOL attempt_close_oplocked_file(files_struct *fsp)
1136 {
1137         DEBUG(5,("attempt_close_oplocked_file: checking file %s.\n", fsp->fsp_name));
1138
1139         if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !fsp->sent_oplock_break && (fsp->fd != -1)) {
1140                 /* Try and break the oplock. */
1141                 if (oplock_break(fsp->dev, fsp->inode, fsp->file_id, True)) {
1142                         if(file_find_fsp(fsp) == NULL) /* Did the oplock break close the file ? */
1143                                 return True;
1144                 }
1145         }
1146
1147         return False;
1148 }
1149
1150 /****************************************************************************
1151  Send an asynchronous oplock break message to another smbd process.
1152 ****************************************************************************/
1153
1154 static BOOL request_remote_level2_async_oplock_break(share_mode_entry *share_entry)
1155 {
1156         char op_break_msg[OPLOCK_BREAK_MSG_LEN];
1157         struct sockaddr_in addr_out;
1158         pid_t pid = sys_getpid();
1159         SMB_DEV_T dev = share_entry->dev;
1160         SMB_INO_T inode = share_entry->inode;
1161         unsigned long file_id = share_entry->share_file_id;
1162
1163         /* We need to send a ASYNC_LEVEL_II_OPLOCK_BREAK_CMD message to the port in the share mode entry. */
1164
1165         SSVAL(op_break_msg,OPBRK_MESSAGE_CMD_OFFSET,ASYNC_LEVEL_II_OPLOCK_BREAK_CMD);
1166         memcpy(op_break_msg+OPLOCK_BREAK_PID_OFFSET,(char *)&pid,sizeof(pid));
1167         memcpy(op_break_msg+OPLOCK_BREAK_DEV_OFFSET,(char *)&dev,sizeof(dev));
1168         memcpy(op_break_msg+OPLOCK_BREAK_INODE_OFFSET,(char *)&inode,sizeof(inode));
1169         memcpy(op_break_msg+OPLOCK_BREAK_FILEID_OFFSET,(char *)&file_id,sizeof(file_id));
1170
1171         /* Set the address and port. */
1172         memset((char *)&addr_out,'\0',sizeof(addr_out));
1173         addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1174         addr_out.sin_port = htons( share_entry->op_port );
1175         addr_out.sin_family = AF_INET;
1176    
1177         if( DEBUGLVL( 3 ) ) {
1178                 dbgtext( "request_remote_level2_async_oplock_break: sending an asynchronous oplock break message to ");
1179                 dbgtext( "pid %d on port %d ", (int)share_entry->pid, share_entry->op_port );
1180                 dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1181             (unsigned int)dev, (double)inode, file_id );
1182         }
1183
1184         if(sys_sendto(oplock_sock,op_break_msg,OPLOCK_BREAK_MSG_LEN,0,
1185                         (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0) {
1186                 if( DEBUGLVL( 0 ) ) {
1187                         dbgtext( "request_remote_level2_async_oplock_break: failed when sending a oplock " );
1188                         dbgtext( "break message to pid %d ", (int)share_entry->pid );
1189                         dbgtext( "on port %d ", share_entry->op_port );
1190                         dbgtext( "for dev = %x, inode = %.0f, file_id = %lu\n",
1191                                 (unsigned int)dev, (double)inode, file_id );
1192                         dbgtext( "Error was %s\n", strerror(errno) );
1193                 }
1194                 return False;
1195         }
1196
1197         DEBUG(3,("request_remote_level2_async_oplock_break: sent async break message to level II entry.\n"));
1198         return True;
1199 }
1200
1201 /****************************************************************************
1202  This function is called on any file modification or lock request. If a file
1203  is level 2 oplocked then it must tell all other level 2 holders to break to none.
1204 ****************************************************************************/
1205
1206 void release_level_2_oplocks_on_change(files_struct *fsp)
1207 {
1208         share_mode_entry *share_list = NULL;
1209         pid_t pid = sys_getpid();
1210         int num_share_modes = 0;
1211         int i;
1212
1213         /*
1214          * If this file is level II oplocked then we need
1215          * to grab the shared memory lock and inform all
1216          * other files with a level II lock that they need
1217          * to flush their read caches. We keep the lock over
1218          * the shared memory area whilst doing this.
1219          */
1220
1221         if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1222                 return;
1223
1224         if (lock_share_entry_fsp(fsp) == False) {
1225                 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock share mode entry for file %s.\n", fsp->fsp_name ));
1226         }
1227
1228         num_share_modes = get_share_modes(fsp->conn, fsp->dev, fsp->inode, &share_list);
1229
1230         DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n", 
1231                         num_share_modes ));
1232
1233         for(i = 0; i < num_share_modes; i++) {
1234                 share_mode_entry *share_entry = &share_list[i];
1235
1236                 /*
1237                  * As there could have been multiple writes waiting at the lock_share_entry
1238                  * gate we may not be the first to enter. Hence the state of the op_types
1239                  * in the share mode entries may be partly NO_OPLOCK and partly LEVEL_II
1240                  * oplock. It will do no harm to re-send break messages to those smbd's
1241                  * that are still waiting their turn to remove their LEVEL_II state, and
1242                  * also no harm to ignore existing NO_OPLOCK states. JRA.
1243                  */
1244
1245                 DEBUG(10,("release_level_2_oplocks_on_change: share_entry[%i]->op_type == %d\n",
1246                                 i, share_entry->op_type ));
1247
1248                 if (share_entry->op_type == NO_OPLOCK)
1249                         continue;
1250
1251                 /* Paranoia .... */
1252                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
1253                         DEBUG(0,("release_level_2_oplocks_on_change: PANIC. share mode entry %d is an exlusive oplock !\n", i ));
1254                         unlock_share_entry(fsp->conn, fsp->dev, fsp->inode);
1255                         abort();
1256                 }
1257
1258                 /*
1259                  * Check if this is a file we have open (including the
1260                  * file we've been called to do write_file on. If so
1261                  * then break it directly without releasing the lock.
1262                  */
1263
1264                 if (pid == share_entry->pid) {
1265                         files_struct *new_fsp = file_find_dif(share_entry->dev, share_entry->inode, share_entry->share_file_id);
1266
1267                         /* Paranoia check... */
1268                         if(new_fsp == NULL) {
1269                                 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. share mode entry %d is not a local file !\n", i ));
1270                                 unlock_share_entry(fsp->conn, fsp->dev, fsp->inode);
1271                                 abort();
1272                         }
1273
1274                         DEBUG(10,("release_level_2_oplocks_on_change: breaking our own oplock.\n"));
1275
1276                         oplock_break_level2(new_fsp, True);
1277
1278                 } else {
1279
1280                         /*
1281                          * This is a remote file and so we send an asynchronous
1282                          * message.
1283                          */
1284
1285                         DEBUG(10,("release_level_2_oplocks_on_change: breaking remote oplock (async).\n"));
1286                         request_remote_level2_async_oplock_break(share_entry);
1287                 }
1288         }
1289
1290         SAFE_FREE(share_list);
1291         unlock_share_entry_fsp(fsp);
1292
1293         /* Paranoia check... */
1294         if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
1295                 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. File %s still has a level II oplock.\n", fsp->fsp_name));
1296                 smb_panic("release_level_2_oplocks_on_change");
1297         }
1298 }
1299
1300 /****************************************************************************
1301  Send a 'retry your open' message to a process with a deferred open entry.
1302 ****************************************************************************/
1303
1304 BOOL send_deferred_open_retry_message(deferred_open_entry *entry)
1305 {
1306         char de_msg[DEFERRED_OPEN_MSG_LEN];
1307         struct sockaddr_in addr_out;
1308         pid_t pid = sys_getpid();
1309
1310         memset(de_msg, '\0', DEFERRED_OPEN_MSG_LEN);
1311         SSVAL(de_msg,DEFERRED_OPEN_CMD_OFFSET,RETRY_DEFERRED_OPEN_CMD);
1312         memcpy(de_msg+DEFERRED_OPEN_PID_OFFSET,(char *)&pid,sizeof(pid));
1313         memcpy(de_msg+DEFERRED_OPEN_DEV_OFFSET,(char *)&entry->dev,sizeof(entry->dev));
1314         memcpy(de_msg+DEFERRED_OPEN_INODE_OFFSET,(char *)&entry->inode,sizeof(entry->inode));
1315         memcpy(de_msg+DEFERRED_OPEN_MID_OFFSET,(char *)&entry->mid,sizeof(entry->mid));
1316
1317         /* Set the address and port. */
1318         memset((char *)&addr_out,'\0',sizeof(addr_out));
1319         addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1320         addr_out.sin_port = htons( entry->port );
1321         addr_out.sin_family = AF_INET;
1322    
1323         if( DEBUGLVL( 3 ) ) {
1324                 dbgtext( "send_deferred_open_retry_message: sending a message to ");
1325                 dbgtext( "pid %d on port %d ", (int)entry->pid, entry->port );
1326                 dbgtext( "for dev = %x, inode = %.0f, mid = %u\n",
1327                         (unsigned int)entry->dev, (double)entry->inode, (unsigned int)entry->mid );
1328         }
1329
1330         if(sys_sendto(oplock_sock,de_msg,DEFERRED_OPEN_MSG_LEN,0,
1331                         (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0) {
1332                 if( DEBUGLVL( 0 ) ) {
1333                         dbgtext( "send_deferred_open_retry_message: failed sending a message to ");
1334                         dbgtext( "pid %d on port %d ", (int)entry->pid, entry->port );
1335                         dbgtext( "for dev = %x, inode = %.0f, mid = %u\n",
1336                                 (unsigned int)entry->dev, (double)entry->inode, (unsigned int)entry->mid );
1337                         dbgtext( "Error was %s\n", strerror(errno) );
1338                 }
1339                 return False;
1340         }
1341         return True;
1342 }
1343
1344 /****************************************************************************
1345  Setup oplocks for this process.
1346 ****************************************************************************/
1347
1348 BOOL init_oplocks(void)
1349 {
1350         struct sockaddr_in sock_name;
1351         socklen_t len = sizeof(sock_name);
1352
1353         DEBUG(3,("open_oplock_ipc: opening loopback UDP socket.\n"));
1354
1355         /* Open a lookback UDP socket on a random port. */
1356         oplock_sock = open_socket_in(SOCK_DGRAM, 0, 0, htonl(INADDR_LOOPBACK),False);
1357         if (oplock_sock == -1) {
1358                 DEBUG(0,("open_oplock_ipc: Failed to get local UDP socket for \
1359 address %lx. Error was %s\n", (long)htonl(INADDR_LOOPBACK), strerror(errno)));
1360                 global_oplock_port = 0;
1361                 return(False);
1362         }
1363
1364         /* Find out the transient UDP port we have been allocated. */
1365         if(getsockname(oplock_sock, (struct sockaddr *)&sock_name, &len)<0) {
1366                 DEBUG(0,("open_oplock_ipc: Failed to get local UDP port. Error was %s\n",
1367                          strerror(errno)));
1368                 close(oplock_sock);
1369                 oplock_sock = -1;
1370                 global_oplock_port = 0;
1371                 return False;
1372         }
1373         global_oplock_port = ntohs(sock_name.sin_port);
1374
1375         if (lp_kernel_oplocks()) {
1376 #if HAVE_KERNEL_OPLOCKS_IRIX
1377                 koplocks = irix_init_kernel_oplocks();
1378 #elif HAVE_KERNEL_OPLOCKS_LINUX
1379                 koplocks = linux_init_kernel_oplocks();
1380 #endif
1381         }
1382
1383         DEBUG(3,("open_oplock ipc: pid = %d, global_oplock_port = %u\n", 
1384                  (int)sys_getpid(), global_oplock_port));
1385
1386         return True;
1387 }