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