r13198: Fix issues exposed by Jerry's testing on 64-bit Solaris
[ira/wip.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    Copyright (C) Volker Lendecke 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /* Current number of oplocks we have outstanding. */
26 static int32 exclusive_oplocks_open = 0;
27 static int32 level_II_oplocks_open = 0;
28 BOOL global_client_failed_oplock_break = False;
29
30 extern struct timeval smb_last_time;
31 extern uint32 global_client_caps;
32 extern int smb_read_error;
33
34 static struct kernel_oplocks *koplocks;
35
36 /****************************************************************************
37  Get the number of current exclusive oplocks.
38 ****************************************************************************/
39
40 int32 get_number_of_exclusive_open_oplocks(void)
41 {
42   return exclusive_oplocks_open;
43 }
44
45 /****************************************************************************
46  Return True if an oplock message is pending.
47 ****************************************************************************/
48
49 BOOL oplock_message_waiting(fd_set *fds)
50 {
51         if (koplocks && koplocks->msg_waiting(fds)) {
52                 return True;
53         }
54
55         return False;
56 }
57
58 /****************************************************************************
59  Read an oplock break message from either the oplock UDP fd or the
60  kernel (if kernel oplocks are supported).
61
62  If timeout is zero then *fds contains the file descriptors that
63  are ready to be read and acted upon. If timeout is non-zero then
64  *fds contains the file descriptors to be selected on for read.
65  The timeout is in milliseconds
66
67 ****************************************************************************/
68
69 void process_kernel_oplocks(void)
70 {
71         fd_set fds;
72
73         FD_ZERO(&fds);
74         smb_read_error = 0;
75
76         /*
77          * We need to check for kernel oplocks before going into the select
78          * here, as the EINTR generated by the linux kernel oplock may have
79          * already been eaten. JRA.
80          */
81
82         if (!koplocks) {
83                 return;
84         }
85
86         while (koplocks->msg_waiting(&fds)) { 
87                 files_struct *fsp;
88                 char msg[MSG_SMB_KERNEL_BREAK_SIZE];
89
90                 fsp = koplocks->receive_message(&fds);
91
92                 if (fsp == NULL) {
93                         DEBUG(3, ("Kernel oplock message announced, but none "
94                                   "received\n"));
95                         return;
96                 }
97
98                 /* Put the kernel break info into the message. */
99                 SDEV_T_VAL(msg,0,fsp->dev);
100                 SINO_T_VAL(msg,8,fsp->inode);
101                 SIVAL(msg,16,fsp->file_id);
102
103                 /* Don't need to be root here as we're only ever
104                    sending to ourselves. */
105
106                 message_send_pid(pid_to_procid(sys_getpid()),
107                                  MSG_SMB_KERNEL_BREAK,
108                                  &msg, MSG_SMB_KERNEL_BREAK_SIZE, True);
109         }
110 }
111
112 /****************************************************************************
113  Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
114  disabled (just sets flags). Returns True if oplock set.
115 ****************************************************************************/
116
117 BOOL set_file_oplock(files_struct *fsp, int oplock_type)
118 {
119         if (koplocks && !koplocks->set_oplock(fsp, oplock_type)) {
120                 return False;
121         }
122
123         fsp->oplock_type = oplock_type;
124         fsp->sent_oplock_break = NO_BREAK_SENT;
125         if (oplock_type == LEVEL_II_OPLOCK) {
126                 level_II_oplocks_open++;
127         } else {
128                 exclusive_oplocks_open++;
129         }
130
131         DEBUG(5,("set_file_oplock: granted oplock on file %s, dev = %x, inode = %.0f, file_id = %lu, \
132 tv_sec = %x, tv_usec = %x\n",
133                  fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id,
134                  (int)fsp->open_time.tv_sec, (int)fsp->open_time.tv_usec ));
135
136         return True;
137 }
138
139 /****************************************************************************
140  Attempt to release an oplock on a file. Decrements oplock count.
141 ****************************************************************************/
142
143 void release_file_oplock(files_struct *fsp)
144 {
145         if ((fsp->oplock_type != NO_OPLOCK) &&
146             (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
147             koplocks) {
148                 koplocks->release_oplock(fsp);
149         }
150
151         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
152                 level_II_oplocks_open--;
153         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
154                 exclusive_oplocks_open--;
155         }
156
157         SMB_ASSERT(exclusive_oplocks_open>=0);
158         SMB_ASSERT(level_II_oplocks_open>=0);
159         
160         fsp->oplock_type = NO_OPLOCK;
161         fsp->sent_oplock_break = NO_BREAK_SENT;
162         
163         flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
164 }
165
166 /****************************************************************************
167  Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
168 ****************************************************************************/
169
170 static void downgrade_file_oplock(files_struct *fsp)
171 {
172         if (koplocks) {
173                 koplocks->release_oplock(fsp);
174         }
175         fsp->oplock_type = LEVEL_II_OPLOCK;
176         exclusive_oplocks_open--;
177         level_II_oplocks_open++;
178         fsp->sent_oplock_break = NO_BREAK_SENT;
179 }
180
181 /****************************************************************************
182  Remove a file oplock. Copes with level II and exclusive.
183  Locks then unlocks the share mode lock. Client can decide to go directly
184  to none even if a "break-to-level II" was sent.
185 ****************************************************************************/
186
187 BOOL remove_oplock(files_struct *fsp)
188 {
189         SMB_DEV_T dev = fsp->dev;
190         SMB_INO_T inode = fsp->inode;
191         BOOL ret;
192         struct share_mode_lock *lck;
193
194         /* Remove the oplock flag from the sharemode. */
195         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
196         if (lck == NULL) {
197                 DEBUG(0,("remove_oplock: failed to lock share entry for "
198                          "file %s\n", fsp->fsp_name ));
199                 return False;
200         }
201         ret = remove_share_oplock(lck, fsp);
202         if (!ret) {
203                 DEBUG(0,("remove_oplock: failed to remove share oplock for "
204                          "file %s fnum %d, dev = %x, inode = %.0f\n",
205                          fsp->fsp_name, fsp->fnum, (unsigned int)dev,
206                          (double)inode));
207         }
208         release_file_oplock(fsp);
209         talloc_free(lck);
210         return ret;
211 }
212
213 /*
214  * Deal with a reply when a break-to-level II was sent.
215  */
216 BOOL downgrade_oplock(files_struct *fsp)
217 {
218         SMB_DEV_T dev = fsp->dev;
219         SMB_INO_T inode = fsp->inode;
220         BOOL ret;
221         struct share_mode_lock *lck;
222
223         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
224         if (lck == NULL) {
225                 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
226                          "file %s\n", fsp->fsp_name ));
227                 return False;
228         }
229         ret = downgrade_share_oplock(lck, fsp);
230         if (!ret) {
231                 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
232                          "for file %s fnum %d, dev = %x, inode = %.0f\n",
233                          fsp->fsp_name, fsp->fnum, (unsigned int)dev,
234                          (double)inode));
235         }
236
237         downgrade_file_oplock(fsp);
238         talloc_free(lck);
239         return ret;
240 }
241
242 /****************************************************************************
243  Setup the listening set of file descriptors for an oplock break
244  message either from the UDP socket or from the kernel. Returns the maximum
245  fd used.
246 ****************************************************************************/
247
248 int setup_oplock_select_set( fd_set *fds)
249 {
250         int maxfd = 0;
251
252         if (koplocks && koplocks->notification_fd != -1) {
253                 FD_SET(koplocks->notification_fd, fds);
254                 maxfd = MAX(maxfd, koplocks->notification_fd);
255         }
256
257         return maxfd;
258 }
259
260 /****************************************************************************
261  Set up an oplock break message.
262 ****************************************************************************/
263
264 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
265                                    files_struct *fsp, uint8 cmd)
266 {
267         char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
268
269         if (result == NULL) {
270                 DEBUG(0, ("talloc failed\n"));
271                 return NULL;
272         }
273
274         memset(result,'\0',smb_size);
275         set_message(result,8,0,True);
276         SCVAL(result,smb_com,SMBlockingX);
277         SSVAL(result,smb_tid,fsp->conn->cnum);
278         SSVAL(result,smb_pid,0xFFFF);
279         SSVAL(result,smb_uid,0);
280         SSVAL(result,smb_mid,0xFFFF);
281         SCVAL(result,smb_vwv0,0xFF);
282         SSVAL(result,smb_vwv2,fsp->fnum);
283         SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
284         SCVAL(result,smb_vwv3+1,cmd);
285         return result;
286 }
287
288 /****************************************************************************
289  Function to do the waiting before sending a local break.
290 ****************************************************************************/
291
292 static void wait_before_sending_break(void)
293 {
294         struct timeval cur_tv;
295         long wait_left = (long)lp_oplock_break_wait_time();
296
297         if (wait_left == 0) {
298                 return;
299         }
300
301         GetTimeOfDay(&cur_tv);
302
303         wait_left -= ((cur_tv.tv_sec - smb_last_time.tv_sec)*1000) +
304                 ((cur_tv.tv_usec - smb_last_time.tv_usec)/1000);
305
306         if(wait_left > 0) {
307                 wait_left = MIN(wait_left, 1000);
308                 sys_usleep(wait_left * 1000);
309         }
310 }
311
312 /****************************************************************************
313  Ensure that we have a valid oplock.
314 ****************************************************************************/
315
316 static files_struct *initial_break_processing(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
317 {
318         files_struct *fsp = NULL;
319
320         if( DEBUGLVL( 3 ) ) {
321                 dbgtext( "initial_break_processing: called for dev = 0x%x, inode = %.0f file_id = %lu\n",
322                         (unsigned int)dev, (double)inode, file_id);
323                 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
324                         exclusive_oplocks_open, level_II_oplocks_open );
325         }
326
327         /*
328          * We need to search the file open table for the
329          * entry containing this dev and inode, and ensure
330          * we have an oplock on it.
331          */
332
333         fsp = file_find_dif(dev, inode, file_id);
334
335         if(fsp == NULL) {
336                 /* The file could have been closed in the meantime - return success. */
337                 if( DEBUGLVL( 3 ) ) {
338                         dbgtext( "initial_break_processing: cannot find open file with " );
339                         dbgtext( "dev = 0x%x, inode = %.0f file_id = %lu", (unsigned int)dev,
340                                 (double)inode, file_id);
341                         dbgtext( "allowing break to succeed.\n" );
342                 }
343                 return NULL;
344         }
345
346         /* Ensure we have an oplock on the file */
347
348         /*
349          * There is a potential race condition in that an oplock could
350          * have been broken due to another udp request, and yet there are
351          * still oplock break messages being sent in the udp message
352          * queue for this file. So return true if we don't have an oplock,
353          * as we may have just freed it.
354          */
355
356         if(fsp->oplock_type == NO_OPLOCK) {
357                 if( DEBUGLVL( 3 ) ) {
358                         dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
359                         dbgtext( "(dev = %x, inode = %.0f, file_id = %lu) has no oplock.\n",
360                                 (unsigned int)dev, (double)inode, fsp->file_id );
361                         dbgtext( "Allowing break to succeed regardless.\n" );
362                 }
363                 return NULL;
364         }
365
366         return fsp;
367 }
368
369 static void oplock_timeout_handler(struct timed_event *te,
370                                    const struct timeval *now,
371                                    void *private_data)
372 {
373         files_struct *fsp = private_data;
374
375         DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
376         global_client_failed_oplock_break = True;
377         remove_oplock(fsp);
378         reply_to_oplock_break_requests(fsp);
379 }
380
381 /*******************************************************************
382  Add a timeout handler waiting for the client reply.
383 *******************************************************************/
384
385 static void add_oplock_timeout_handler(files_struct *fsp)
386 {
387         if (fsp->oplock_timeout != NULL) {
388                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
389                           "around\n"));
390         }
391
392         fsp->oplock_timeout =
393                 add_timed_event(NULL,
394                                 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
395                                 "oplock_timeout_handler",
396                                 oplock_timeout_handler, fsp);
397
398         if (fsp->oplock_timeout == NULL) {
399                 DEBUG(0, ("Could not add oplock timeout handler\n"));
400         }
401 }
402
403 /*******************************************************************
404  This handles the case of a write triggering a break to none
405  message on a level2 oplock.
406  When we get this message we may be in any of three states :
407  NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
408  the client for LEVEL2.
409 *******************************************************************/
410
411 static void process_oplock_async_level2_break_message(int msg_type, struct process_id src,
412                                          void *buf, size_t len)
413 {
414         struct share_mode_entry msg;
415         files_struct *fsp;
416         char *break_msg;
417         BOOL break_to_level2 = False;
418         BOOL sign_state;
419
420         if (buf == NULL) {
421                 DEBUG(0, ("Got NULL buffer\n"));
422                 return;
423         }
424
425         if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
426                 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
427                 return;
428         }
429
430         /* De-linearize incoming message. */
431         message_to_share_mode_entry(&msg, buf);
432
433         DEBUG(10, ("Got oplock async level 2 break message from pid %d: 0x%x/%.0f/%d\n",
434                    (int)procid_to_pid(&src), (unsigned int)msg.dev, (double)msg.inode,
435                    (int)msg.share_file_id));
436
437         fsp = initial_break_processing(msg.dev, msg.inode,
438                                        msg.share_file_id);
439
440         if (fsp == NULL) {
441                 /* We hit a race here. Break messages are sent, and before we
442                  * get to process this message, we have closed the file. 
443                  * No need to reply as this is an async message. */
444                 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
445                 return;
446         }
447
448         if (fsp->oplock_type == NO_OPLOCK) {
449                 /* We already got a "break to none" message and we've handled it.
450                  * just ignore. */
451                 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
452                 return;
453         }
454
455         if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
456                 /* Don't tell the client, just downgrade. */
457                 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
458                 remove_oplock(fsp);
459                 return;
460         }
461
462         /* Ensure we're really at level2 state. */
463         SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
464
465         /* Now send a break to none message to our client. */
466
467         break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
468         if (break_msg == NULL) {
469                 exit_server("Could not talloc break_msg\n");
470         }
471
472         /* Need to wait before sending a break message if we sent ourselves this message. */
473         if (procid_to_pid(&src) == sys_getpid()) {
474                 wait_before_sending_break();
475         }
476
477         /* Save the server smb signing state. */
478         sign_state = srv_oplock_set_signing(False);
479
480         show_msg(break_msg);
481         if (!send_smb(smbd_server_fd(), break_msg)) {
482                 exit_server("oplock_break: send_smb failed.");
483         }
484
485         /* Restore the sign state to what it was. */
486         srv_oplock_set_signing(sign_state);
487
488         talloc_free(break_msg);
489
490         /* Async level2 request, don't send a reply, just remove the oplock. */
491         remove_oplock(fsp);
492 }
493
494 /*******************************************************************
495  This handles the generic oplock break message from another smbd.
496 *******************************************************************/
497
498 static void process_oplock_break_message(int msg_type, struct process_id src,
499                                          void *buf, size_t len)
500 {
501         struct share_mode_entry msg;
502         files_struct *fsp;
503         char *break_msg;
504         BOOL break_to_level2 = False;
505         BOOL sign_state;
506
507         if (buf == NULL) {
508                 DEBUG(0, ("Got NULL buffer\n"));
509                 return;
510         }
511
512         if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
513                 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
514                 return;
515         }
516
517         /* De-linearize incoming message. */
518         message_to_share_mode_entry(&msg, buf);
519
520         DEBUG(10, ("Got oplock break message from pid %d: 0x%x/%.0f/%d\n",
521                    (int)procid_to_pid(&src), (unsigned int)msg.dev, (double)msg.inode,
522                    (int)msg.share_file_id));
523
524         fsp = initial_break_processing(msg.dev, msg.inode,
525                                        msg.share_file_id);
526
527         if (fsp == NULL) {
528                 /* a We hit race here. Break messages are sent, and before we
529                  * get to process this message, we have closed the file. Reply
530                  * with 'ok, oplock broken' */
531                 DEBUG(3, ("Did not find fsp\n"));
532                 become_root();
533
534                 /* We just send the same message back. */
535                 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
536                                  buf, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
537
538                 unbecome_root();
539                 return;
540         }
541
542         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
543                 /* Remember we have to inform the requesting PID when the
544                  * client replies */
545                 msg.pid = src;
546                 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
547                              &fsp->pending_break_messages,
548                              &fsp->num_pending_break_messages);
549                 return;
550         }
551
552         if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
553             !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
554                 DEBUG(3, ("Already downgraded oplock on 0x%x/%.0f: %s\n",
555                           (unsigned int)fsp->dev, (double)fsp->inode,
556                           fsp->fsp_name));
557                 become_root();
558
559                 /* We just send the same message back. */
560                 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
561                                  buf, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
562
563                 unbecome_root();
564                 return;
565         }
566
567         if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) && 
568             !koplocks && /* NOTE: we force levelII off for kernel oplocks -
569                           * this will change when it is supported */
570             lp_level2_oplocks(SNUM(fsp->conn))) {
571                 break_to_level2 = True;
572         }
573
574         break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
575                                           OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
576         if (break_msg == NULL) {
577                 exit_server("Could not talloc break_msg\n");
578         }
579
580         /* Need to wait before sending a break message if we sent ourselves this message. */
581         if (procid_to_pid(&src) == sys_getpid()) {
582                 wait_before_sending_break();
583         }
584
585         /* Save the server smb signing state. */
586         sign_state = srv_oplock_set_signing(False);
587
588         show_msg(break_msg);
589         if (!send_smb(smbd_server_fd(), break_msg)) {
590                 exit_server("oplock_break: send_smb failed.");
591         }
592
593         /* Restore the sign state to what it was. */
594         srv_oplock_set_signing(sign_state);
595
596         talloc_free(break_msg);
597
598         fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
599
600         msg.pid = src;
601         ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
602                      &fsp->pending_break_messages,
603                      &fsp->num_pending_break_messages);
604
605         add_oplock_timeout_handler(fsp);
606 }
607
608 /*******************************************************************
609  This handles the kernel oplock break message.
610 *******************************************************************/
611
612 static void process_kernel_oplock_break(int msg_type, struct process_id src,
613                                         void *buf, size_t len)
614 {
615         SMB_DEV_T dev;
616         SMB_INO_T inode;
617         unsigned long file_id;
618         files_struct *fsp;
619         char *break_msg;
620         BOOL sign_state;
621
622         if (buf == NULL) {
623                 DEBUG(0, ("Got NULL buffer\n"));
624                 return;
625         }
626
627         if (len != MSG_SMB_KERNEL_BREAK_SIZE) {
628                 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
629                 return;
630         }
631
632         /* Pull the data from the message. */
633         dev = DEV_T_VAL(buf, 0);
634         inode = INO_T_VAL(buf, 8);
635         file_id = (unsigned long)IVAL(buf, 16);
636
637         DEBUG(10, ("Got kernel oplock break message from pid %d: 0x%x/%.0f/%u\n",
638                    (int)procid_to_pid(&src), (unsigned int)dev, (double)inode,
639                    (unsigned int)file_id));
640
641         fsp = initial_break_processing(dev, inode, file_id);
642
643         if (fsp == NULL) {
644                 DEBUG(3, ("Got a kernel oplock break message for a file "
645                           "I don't know about\n"));
646                 return;
647         }
648
649         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
650                 /* This is ok, kernel oplocks come in completely async */
651                 DEBUG(3, ("Got a kernel oplock request while waiting for a "
652                           "break reply\n"));
653                 return;
654         }
655
656         break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
657         if (break_msg == NULL) {
658                 exit_server("Could not talloc break_msg\n");
659         }
660
661         /* Save the server smb signing state. */
662         sign_state = srv_oplock_set_signing(False);
663
664         show_msg(break_msg);
665         if (!send_smb(smbd_server_fd(), break_msg)) {
666                 exit_server("oplock_break: send_smb failed.");
667         }
668
669         /* Restore the sign state to what it was. */
670         srv_oplock_set_signing(sign_state);
671
672         talloc_free(break_msg);
673
674         fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
675
676         add_oplock_timeout_handler(fsp);
677 }
678
679 void reply_to_oplock_break_requests(files_struct *fsp)
680 {
681         int i;
682
683         become_root();
684         for (i=0; i<fsp->num_pending_break_messages; i++) {
685                 struct share_mode_entry *e = &fsp->pending_break_messages[i];
686                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
687
688                 share_mode_entry_to_message(msg, e);
689
690                 message_send_pid(e->pid, MSG_SMB_BREAK_RESPONSE,
691                                  msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
692         }
693         unbecome_root();
694
695         SAFE_FREE(fsp->pending_break_messages);
696         fsp->num_pending_break_messages = 0;
697         if (fsp->oplock_timeout != NULL) {
698                 /* Remove the timed event handler. */
699                 talloc_free(fsp->oplock_timeout);
700                 fsp->oplock_timeout = NULL;
701         }
702         return;
703 }
704
705 static void process_oplock_break_response(int msg_type, struct process_id src,
706                                           void *buf, size_t len)
707 {
708         struct share_mode_entry msg;
709
710         if (buf == NULL) {
711                 DEBUG(0, ("Got NULL buffer\n"));
712                 return;
713         }
714
715         if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
716                 DEBUG(0, ("Got invalid msg len %u\n", (unsigned int)len));
717                 return;
718         }
719
720         /* De-linearize incoming message. */
721         message_to_share_mode_entry(&msg, buf);
722
723         DEBUG(10, ("Got oplock break response from pid %d: 0x%x/%.0f/%u mid %u\n",
724                    (int)procid_to_pid(&src), (unsigned int)msg.dev, (double)msg.inode,
725                    (unsigned int)msg.share_file_id, (unsigned int)msg.op_mid));
726
727         /* Here's the hack from open.c, store the mid in the 'port' field */
728         schedule_deferred_open_smb_message(msg.op_mid);
729 }
730
731 static void process_open_retry_message(int msg_type, struct process_id src,
732                                        void *buf, size_t len)
733 {
734         struct share_mode_entry msg;
735         
736         if (buf == NULL) {
737                 DEBUG(0, ("Got NULL buffer\n"));
738                 return;
739         }
740
741         if (len != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
742                 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
743                 return;
744         }
745
746         /* De-linearize incoming message. */
747         message_to_share_mode_entry(&msg, buf);
748
749         DEBUG(10, ("Got open retry msg from pid %d: 0x%x/%.0f mid %u\n",
750                    (int)procid_to_pid(&src), (unsigned int)msg.dev, (double)msg.inode,
751                    (unsigned int)msg.op_mid));
752
753         schedule_deferred_open_smb_message(msg.op_mid);
754 }
755
756 /****************************************************************************
757  This function is called on any file modification or lock request. If a file
758  is level 2 oplocked then it must tell all other level 2 holders to break to
759  none.
760 ****************************************************************************/
761
762 void release_level_2_oplocks_on_change(files_struct *fsp)
763 {
764         int i;
765         struct share_mode_lock *lck;
766
767         /*
768          * If this file is level II oplocked then we need
769          * to grab the shared memory lock and inform all
770          * other files with a level II lock that they need
771          * to flush their read caches. We keep the lock over
772          * the shared memory area whilst doing this.
773          */
774
775         if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
776                 return;
777
778         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
779         if (lck == NULL) {
780                 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
781                          "share mode entry for file %s.\n", fsp->fsp_name ));
782         }
783
784         DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n", 
785                   lck->num_share_modes ));
786
787         for(i = 0; i < lck->num_share_modes; i++) {
788                 struct share_mode_entry *share_entry = &lck->share_modes[i];
789                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
790
791                 if (!is_valid_share_mode_entry(share_entry)) {
792                         continue;
793                 }
794
795                 /*
796                  * As there could have been multiple writes waiting at the
797                  * lock_share_entry gate we may not be the first to
798                  * enter. Hence the state of the op_types in the share mode
799                  * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
800                  * oplock. It will do no harm to re-send break messages to
801                  * those smbd's that are still waiting their turn to remove
802                  * their LEVEL_II state, and also no harm to ignore existing
803                  * NO_OPLOCK states. JRA.
804                  */
805
806                 DEBUG(10,("release_level_2_oplocks_on_change: "
807                           "share_entry[%i]->op_type == %d\n",
808                           i, share_entry->op_type ));
809
810                 if (share_entry->op_type == NO_OPLOCK) {
811                         continue;
812                 }
813
814                 /* Paranoia .... */
815                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
816                         DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
817                                  "share mode entry %d is an exlusive "
818                                  "oplock !\n", i ));
819                         talloc_free(lck);
820                         abort();
821                 }
822
823                 share_mode_entry_to_message(msg, share_entry);
824
825                 become_root();
826                 message_send_pid(share_entry->pid, MSG_SMB_ASYNC_LEVEL2_BREAK,
827                                  msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
828                 unbecome_root();
829         }
830
831         /* We let the message receivers handle removing the oplock state
832            in the share mode lock db. */
833
834         talloc_free(lck);
835 }
836
837 /****************************************************************************
838  Linearize a share mode entry struct to an internal oplock break message.
839 ****************************************************************************/
840
841 void share_mode_entry_to_message(char *msg, struct share_mode_entry *e)
842 {
843         SIVAL(msg,0,(uint32)e->pid.pid);
844         SSVAL(msg,4,e->op_mid);
845         SSVAL(msg,6,e->op_type);
846         SIVAL(msg,8,e->access_mask);
847         SIVAL(msg,12,e->share_access);
848         SIVAL(msg,16,e->private_options);
849         SIVAL(msg,20,(uint32)e->time.tv_sec);
850         SIVAL(msg,24,(uint32)e->time.tv_usec);
851         SDEV_T_VAL(msg,28,e->dev);
852         SINO_T_VAL(msg,36,e->inode);
853         SIVAL(msg,44,e->share_file_id);
854 }
855
856 /****************************************************************************
857  De-linearize an internal oplock break message to a share mode entry struct.
858 ****************************************************************************/
859
860 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
861 {
862         e->pid.pid = (pid_t)IVAL(msg,0);
863         e->op_mid = SVAL(msg,4);
864         e->op_type = SVAL(msg,6);
865         e->access_mask = IVAL(msg,8);
866         e->share_access = IVAL(msg,12);
867         e->private_options = IVAL(msg,16);
868         e->time.tv_sec = (time_t)IVAL(msg,20);
869         e->time.tv_usec = (int)IVAL(msg,24);
870         e->dev = DEV_T_VAL(msg,28);
871         e->inode = INO_T_VAL(msg,36);
872         e->share_file_id = (unsigned long)IVAL(msg,44);
873 }
874
875 /****************************************************************************
876  Setup oplocks for this process.
877 ****************************************************************************/
878
879 BOOL init_oplocks(void)
880 {
881         DEBUG(3,("open_oplock_ipc: initializing messages.\n"));
882
883         message_register(MSG_SMB_BREAK_REQUEST,
884                          process_oplock_break_message);
885         message_register(MSG_SMB_ASYNC_LEVEL2_BREAK,
886                          process_oplock_async_level2_break_message);
887         message_register(MSG_SMB_BREAK_RESPONSE,
888                          process_oplock_break_response);
889         message_register(MSG_SMB_KERNEL_BREAK,
890                          process_kernel_oplock_break);
891         message_register(MSG_SMB_OPEN_RETRY,
892                          process_open_retry_message);
893
894         if (lp_kernel_oplocks()) {
895 #if HAVE_KERNEL_OPLOCKS_IRIX
896                 koplocks = irix_init_kernel_oplocks();
897 #elif HAVE_KERNEL_OPLOCKS_LINUX
898                 koplocks = linux_init_kernel_oplocks();
899 #endif
900         }
901
902         return True;
903 }