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