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