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