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