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