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