As the encryption is stream based there's no reason
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #define DBGC_CLASS DBGC_LOCKING
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 uint32 global_client_caps;
31
32 static struct kernel_oplocks *koplocks;
33
34 /****************************************************************************
35  Get the number of current exclusive oplocks.
36 ****************************************************************************/
37
38 int32 get_number_of_exclusive_open_oplocks(void)
39 {
40   return exclusive_oplocks_open;
41 }
42
43 /****************************************************************************
44  Return True if an oplock message is pending.
45 ****************************************************************************/
46
47 bool oplock_message_waiting(fd_set *fds)
48 {
49         if (koplocks && koplocks->msg_waiting(fds)) {
50                 return True;
51         }
52
53         return False;
54 }
55
56 /****************************************************************************
57  Find out if there are any kernel oplock messages waiting and process them
58  if so. pfds is the fd_set from the main select loop (which contains any
59  kernel oplock fd if that's what the system uses (IRIX). If may be NULL if
60  we're calling this in a shutting down state.
61 ****************************************************************************/
62
63 void process_kernel_oplocks(struct messaging_context *msg_ctx, fd_set *pfds)
64 {
65         /*
66          * We need to check for kernel oplocks before going into the select
67          * here, as the EINTR generated by the linux kernel oplock may have
68          * already been eaten. JRA.
69          */
70
71         if (!koplocks) {
72                 return;
73         }
74
75         while (koplocks->msg_waiting(pfds)) { 
76                 files_struct *fsp;
77                 char msg[MSG_SMB_KERNEL_BREAK_SIZE];
78
79                 fsp = koplocks->receive_message(pfds);
80
81                 if (fsp == NULL) {
82                         DEBUG(3, ("Kernel oplock message announced, but none "
83                                   "received\n"));
84                         return;
85                 }
86
87                 /* Put the kernel break info into the message. */
88                 push_file_id_16(msg, &fsp->file_id);
89                 SIVAL(msg,16,fsp->fh->gen_id);
90
91                 /* Don't need to be root here as we're only ever
92                    sending to ourselves. */
93
94                 messaging_send_buf(msg_ctx, procid_self(),
95                                    MSG_SMB_KERNEL_BREAK,
96                                    (uint8 *)&msg, MSG_SMB_KERNEL_BREAK_SIZE);
97         }
98 }
99
100 /****************************************************************************
101  Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
102  disabled (just sets flags). Returns True if oplock set.
103 ****************************************************************************/
104
105 bool set_file_oplock(files_struct *fsp, int oplock_type)
106 {
107         if (koplocks && !koplocks->set_oplock(fsp, oplock_type)) {
108                 return False;
109         }
110
111         fsp->oplock_type = oplock_type;
112         fsp->sent_oplock_break = NO_BREAK_SENT;
113         if (oplock_type == LEVEL_II_OPLOCK) {
114                 level_II_oplocks_open++;
115         } else {
116                 exclusive_oplocks_open++;
117         }
118
119         DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
120                     "tv_sec = %x, tv_usec = %x\n",
121                  fsp->fsp_name, file_id_string_tos(&fsp->file_id),
122                  fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
123                  (int)fsp->open_time.tv_usec ));
124
125         return True;
126 }
127
128 /****************************************************************************
129  Attempt to release an oplock on a file. Decrements oplock count.
130 ****************************************************************************/
131
132 void release_file_oplock(files_struct *fsp)
133 {
134         if ((fsp->oplock_type != NO_OPLOCK) &&
135             (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
136             koplocks) {
137                 koplocks->release_oplock(fsp);
138         }
139
140         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
141                 level_II_oplocks_open--;
142         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
143                 exclusive_oplocks_open--;
144         }
145
146         SMB_ASSERT(exclusive_oplocks_open>=0);
147         SMB_ASSERT(level_II_oplocks_open>=0);
148         
149         fsp->oplock_type = NO_OPLOCK;
150         fsp->sent_oplock_break = NO_BREAK_SENT;
151         
152         flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
153
154         TALLOC_FREE(fsp->oplock_timeout);
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_string_tos(&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_string_tos(&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         if (!srv_encryption_on()) {
256                 cli_set_message(result,8,0,true);
257         } else {
258                 char inbuf[8];
259                 smb_set_enclen(inbuf,4,srv_enc_ctx());
260                 srv_set_message(inbuf,result,8,0,true);
261         }
262         SCVAL(result,smb_com,SMBlockingX);
263         SSVAL(result,smb_tid,fsp->conn->cnum);
264         SSVAL(result,smb_pid,0xFFFF);
265         SSVAL(result,smb_uid,0);
266         SSVAL(result,smb_mid,0xFFFF);
267         SCVAL(result,smb_vwv0,0xFF);
268         SSVAL(result,smb_vwv2,fsp->fnum);
269         SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
270         SCVAL(result,smb_vwv3+1,cmd);
271         return result;
272 }
273
274 /****************************************************************************
275  Function to do the waiting before sending a local break.
276 ****************************************************************************/
277
278 static void wait_before_sending_break(void)
279 {
280         long wait_time = (long)lp_oplock_break_wait_time();
281
282         if (wait_time) {
283                 smb_msleep(wait_time);
284         }
285 }
286
287 /****************************************************************************
288  Ensure that we have a valid oplock.
289 ****************************************************************************/
290
291 static files_struct *initial_break_processing(struct file_id id, unsigned long file_id)
292 {
293         files_struct *fsp = NULL;
294
295         if( DEBUGLVL( 3 ) ) {
296                 dbgtext( "initial_break_processing: called for %s/%u\n",
297                          file_id_string_tos(&id), (int)file_id);
298                 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
299                         exclusive_oplocks_open, level_II_oplocks_open );
300         }
301
302         /*
303          * We need to search the file open table for the
304          * entry containing this dev and inode, and ensure
305          * we have an oplock on it.
306          */
307
308         fsp = file_find_dif(id, file_id);
309
310         if(fsp == NULL) {
311                 /* The file could have been closed in the meantime - return success. */
312                 if( DEBUGLVL( 3 ) ) {
313                         dbgtext( "initial_break_processing: cannot find open file with " );
314                         dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
315                         dbgtext( "allowing break to succeed.\n" );
316                 }
317                 return NULL;
318         }
319
320         /* Ensure we have an oplock on the file */
321
322         /*
323          * There is a potential race condition in that an oplock could
324          * have been broken due to another udp request, and yet there are
325          * still oplock break messages being sent in the udp message
326          * queue for this file. So return true if we don't have an oplock,
327          * as we may have just freed it.
328          */
329
330         if(fsp->oplock_type == NO_OPLOCK) {
331                 if( DEBUGLVL( 3 ) ) {
332                         dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
333                         dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
334                                  file_id_string_tos(&id), fsp->fh->gen_id );
335                         dbgtext( "Allowing break to succeed regardless.\n" );
336                 }
337                 return NULL;
338         }
339
340         return fsp;
341 }
342
343 static void oplock_timeout_handler(struct event_context *ctx,
344                                    struct timed_event *te,
345                                    const struct timeval *now,
346                                    void *private_data)
347 {
348         files_struct *fsp = (files_struct *)private_data;
349
350         /* Remove the timed event handler. */
351         TALLOC_FREE(fsp->oplock_timeout);
352         DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
353         global_client_failed_oplock_break = True;
354         remove_oplock(fsp);
355         reply_to_oplock_break_requests(fsp);
356 }
357
358 /*******************************************************************
359  Add a timeout handler waiting for the client reply.
360 *******************************************************************/
361
362 static void add_oplock_timeout_handler(files_struct *fsp)
363 {
364         if (fsp->oplock_timeout != NULL) {
365                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
366                           "around\n"));
367         }
368
369         fsp->oplock_timeout =
370                 event_add_timed(smbd_event_context(), NULL,
371                                 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
372                                 "oplock_timeout_handler",
373                                 oplock_timeout_handler, fsp);
374
375         if (fsp->oplock_timeout == NULL) {
376                 DEBUG(0, ("Could not add oplock timeout handler\n"));
377         }
378 }
379
380 /*******************************************************************
381  This handles the case of a write triggering a break to none
382  message on a level2 oplock.
383  When we get this message we may be in any of three states :
384  NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
385  the client for LEVEL2.
386 *******************************************************************/
387
388 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
389                                                       void *private_data,
390                                                       uint32_t msg_type,
391                                                       struct server_id src,
392                                                       DATA_BLOB *data)
393 {
394         struct share_mode_entry msg;
395         files_struct *fsp;
396         char *break_msg;
397         bool sign_state;
398
399         if (data->data == NULL) {
400                 DEBUG(0, ("Got NULL buffer\n"));
401                 return;
402         }
403
404         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
405                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
406                 return;
407         }
408
409         /* De-linearize incoming message. */
410         message_to_share_mode_entry(&msg, (char *)data->data);
411
412         DEBUG(10, ("Got oplock async level 2 break message from pid %d: %s/%lu\n",
413                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
414
415         fsp = initial_break_processing(msg.id, msg.share_file_id);
416
417         if (fsp == NULL) {
418                 /* We hit a race here. Break messages are sent, and before we
419                  * get to process this message, we have closed the file. 
420                  * No need to reply as this is an async message. */
421                 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
422                 return;
423         }
424
425         if (fsp->oplock_type == NO_OPLOCK) {
426                 /* We already got a "break to none" message and we've handled it.
427                  * just ignore. */
428                 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
429                 return;
430         }
431
432         if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
433                 /* Don't tell the client, just downgrade. */
434                 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
435                 remove_oplock(fsp);
436                 return;
437         }
438
439         /* Ensure we're really at level2 state. */
440         SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
441
442         /* Now send a break to none message to our client. */
443
444         break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
445         if (break_msg == NULL) {
446                 exit_server("Could not talloc break_msg\n");
447         }
448
449         /* Need to wait before sending a break message if we sent ourselves this message. */
450         if (procid_to_pid(&src) == sys_getpid()) {
451                 wait_before_sending_break();
452         }
453
454         /* Save the server smb signing state. */
455         sign_state = srv_oplock_set_signing(False);
456
457         show_msg(break_msg);
458         if (!send_smb(smbd_server_fd(), break_msg)) {
459                 exit_server_cleanly("oplock_break: send_smb failed.");
460         }
461
462         /* Restore the sign state to what it was. */
463         srv_oplock_set_signing(sign_state);
464
465         TALLOC_FREE(break_msg);
466
467         /* Async level2 request, don't send a reply, just remove the oplock. */
468         remove_oplock(fsp);
469 }
470
471 /*******************************************************************
472  This handles the generic oplock break message from another smbd.
473 *******************************************************************/
474
475 static void process_oplock_break_message(struct messaging_context *msg_ctx,
476                                          void *private_data,
477                                          uint32_t msg_type,
478                                          struct server_id src,
479                                          DATA_BLOB *data)
480 {
481         struct share_mode_entry msg;
482         files_struct *fsp;
483         char *break_msg;
484         bool break_to_level2 = False;
485         bool sign_state;
486
487         if (data->data == NULL) {
488                 DEBUG(0, ("Got NULL buffer\n"));
489                 return;
490         }
491
492         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
493                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
494                 return;
495         }
496
497         /* De-linearize incoming message. */
498         message_to_share_mode_entry(&msg, (char *)data->data);
499
500         DEBUG(10, ("Got oplock break message from pid %d: %s/%lu\n",
501                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
502
503         fsp = initial_break_processing(msg.id, msg.share_file_id);
504
505         if (fsp == NULL) {
506                 /* a We hit race here. Break messages are sent, and before we
507                  * get to process this message, we have closed the file. Reply
508                  * with 'ok, oplock broken' */
509                 DEBUG(3, ("Did not find fsp\n"));
510
511                 /* We just send the same message back. */
512                 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
513                                    (uint8 *)data->data,
514                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
515                 return;
516         }
517
518         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
519                 /* Remember we have to inform the requesting PID when the
520                  * client replies */
521                 msg.pid = src;
522                 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
523                              &fsp->pending_break_messages,
524                              &fsp->num_pending_break_messages);
525                 return;
526         }
527
528         if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
529             !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
530                 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
531                           file_id_string_tos(&fsp->file_id),
532                           fsp->fsp_name));
533                 /* We just send the same message back. */
534                 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
535                                    (uint8 *)data->data,
536                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
537                 return;
538         }
539
540         if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) && 
541             !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
542             !koplocks && /* NOTE: we force levelII off for kernel oplocks -
543                           * this will change when it is supported */
544             lp_level2_oplocks(SNUM(fsp->conn))) {
545                 break_to_level2 = True;
546         }
547
548         break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
549                                           OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
550         if (break_msg == NULL) {
551                 exit_server("Could not talloc break_msg\n");
552         }
553
554         /* Need to wait before sending a break message if we sent ourselves this message. */
555         if (procid_to_pid(&src) == sys_getpid()) {
556                 wait_before_sending_break();
557         }
558
559         /* Save the server smb signing state. */
560         sign_state = srv_oplock_set_signing(False);
561
562         show_msg(break_msg);
563         if (!send_smb(smbd_server_fd(), break_msg)) {
564                 exit_server_cleanly("oplock_break: send_smb failed.");
565         }
566
567         /* Restore the sign state to what it was. */
568         srv_oplock_set_signing(sign_state);
569
570         TALLOC_FREE(break_msg);
571
572         fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
573
574         msg.pid = src;
575         ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
576                      &fsp->pending_break_messages,
577                      &fsp->num_pending_break_messages);
578
579         add_oplock_timeout_handler(fsp);
580 }
581
582 /*******************************************************************
583  This handles the kernel oplock break message.
584 *******************************************************************/
585
586 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
587                                         void *private_data,
588                                         uint32_t msg_type,
589                                         struct server_id src,
590                                         DATA_BLOB *data)
591 {
592         struct file_id id;
593         unsigned long file_id;
594         files_struct *fsp;
595         char *break_msg;
596         bool sign_state;
597
598         if (data->data == NULL) {
599                 DEBUG(0, ("Got NULL buffer\n"));
600                 return;
601         }
602
603         if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
604                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
605                 return;
606         }
607
608         /* Pull the data from the message. */
609         pull_file_id_16((char *)data->data, &id);
610         file_id = (unsigned long)IVAL(data->data, 16);
611
612         DEBUG(10, ("Got kernel oplock break message from pid %d: %s/%u\n",
613                    (int)procid_to_pid(&src), file_id_string_tos(&id),
614                    (unsigned int)file_id));
615
616         fsp = initial_break_processing(id, 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_cleanly("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                 messaging_send_buf(smbd_messaging_context(), e->pid,
665                                    MSG_SMB_BREAK_RESPONSE,
666                                    (uint8 *)msg,
667                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
668         }
669
670         SAFE_FREE(fsp->pending_break_messages);
671         fsp->num_pending_break_messages = 0;
672         if (fsp->oplock_timeout != NULL) {
673                 /* Remove the timed event handler. */
674                 TALLOC_FREE(fsp->oplock_timeout);
675                 fsp->oplock_timeout = NULL;
676         }
677         return;
678 }
679
680 static void process_oplock_break_response(struct messaging_context *msg_ctx,
681                                           void *private_data,
682                                           uint32_t msg_type,
683                                           struct server_id src,
684                                           DATA_BLOB *data)
685 {
686         struct share_mode_entry msg;
687
688         if (data->data == NULL) {
689                 DEBUG(0, ("Got NULL buffer\n"));
690                 return;
691         }
692
693         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
694                 DEBUG(0, ("Got invalid msg len %u\n",
695                           (unsigned int)data->length));
696                 return;
697         }
698
699         /* De-linearize incoming message. */
700         message_to_share_mode_entry(&msg, (char *)data->data);
701
702         DEBUG(10, ("Got oplock break response from pid %d: %s/%lu mid %u\n",
703                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id,
704                    (unsigned int)msg.op_mid));
705
706         /* Here's the hack from open.c, store the mid in the 'port' field */
707         schedule_deferred_open_smb_message(msg.op_mid);
708 }
709
710 static void process_open_retry_message(struct messaging_context *msg_ctx,
711                                        void *private_data,
712                                        uint32_t msg_type,
713                                        struct server_id src,
714                                        DATA_BLOB *data)
715 {
716         struct share_mode_entry msg;
717         
718         if (data->data == NULL) {
719                 DEBUG(0, ("Got NULL buffer\n"));
720                 return;
721         }
722
723         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
724                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
725                 return;
726         }
727
728         /* De-linearize incoming message. */
729         message_to_share_mode_entry(&msg, (char *)data->data);
730
731         DEBUG(10, ("Got open retry msg from pid %d: %s mid %u\n",
732                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id),
733                    (unsigned int)msg.op_mid));
734
735         schedule_deferred_open_smb_message(msg.op_mid);
736 }
737
738 /****************************************************************************
739  This function is called on any file modification or lock request. If a file
740  is level 2 oplocked then it must tell all other level 2 holders to break to
741  none.
742 ****************************************************************************/
743
744 void release_level_2_oplocks_on_change(files_struct *fsp)
745 {
746         int i;
747         struct share_mode_lock *lck;
748
749         /*
750          * If this file is level II oplocked then we need
751          * to grab the shared memory lock and inform all
752          * other files with a level II lock that they need
753          * to flush their read caches. We keep the lock over
754          * the shared memory area whilst doing this.
755          */
756
757         if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
758                 return;
759
760         lck = get_share_mode_lock(NULL, fsp->file_id, NULL, NULL);
761         if (lck == NULL) {
762                 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
763                          "share mode entry for file %s.\n", fsp->fsp_name ));
764                 return;
765         }
766
767         DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n", 
768                   lck->num_share_modes ));
769
770         for(i = 0; i < lck->num_share_modes; i++) {
771                 struct share_mode_entry *share_entry = &lck->share_modes[i];
772                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
773
774                 if (!is_valid_share_mode_entry(share_entry)) {
775                         continue;
776                 }
777
778                 /*
779                  * As there could have been multiple writes waiting at the
780                  * lock_share_entry gate we may not be the first to
781                  * enter. Hence the state of the op_types in the share mode
782                  * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
783                  * oplock. It will do no harm to re-send break messages to
784                  * those smbd's that are still waiting their turn to remove
785                  * their LEVEL_II state, and also no harm to ignore existing
786                  * NO_OPLOCK states. JRA.
787                  */
788
789                 DEBUG(10,("release_level_2_oplocks_on_change: "
790                           "share_entry[%i]->op_type == %d\n",
791                           i, share_entry->op_type ));
792
793                 if (share_entry->op_type == NO_OPLOCK) {
794                         continue;
795                 }
796
797                 /* Paranoia .... */
798                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
799                         DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
800                                  "share mode entry %d is an exlusive "
801                                  "oplock !\n", i ));
802                         TALLOC_FREE(lck);
803                         abort();
804                 }
805
806                 share_mode_entry_to_message(msg, share_entry);
807
808                 messaging_send_buf(smbd_messaging_context(), share_entry->pid,
809                                    MSG_SMB_ASYNC_LEVEL2_BREAK,
810                                    (uint8 *)msg,
811                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
812         }
813
814         /* We let the message receivers handle removing the oplock state
815            in the share mode lock db. */
816
817         TALLOC_FREE(lck);
818 }
819
820 /****************************************************************************
821  Linearize a share mode entry struct to an internal oplock break message.
822 ****************************************************************************/
823
824 void share_mode_entry_to_message(char *msg, struct share_mode_entry *e)
825 {
826         SIVAL(msg,0,(uint32)e->pid.pid);
827         SSVAL(msg,4,e->op_mid);
828         SSVAL(msg,6,e->op_type);
829         SIVAL(msg,8,e->access_mask);
830         SIVAL(msg,12,e->share_access);
831         SIVAL(msg,16,e->private_options);
832         SIVAL(msg,20,(uint32)e->time.tv_sec);
833         SIVAL(msg,24,(uint32)e->time.tv_usec);
834         push_file_id_16(msg+28, &e->id);
835         SIVAL(msg,44,e->share_file_id);
836         SIVAL(msg,48,e->uid);
837         SSVAL(msg,52,e->flags);
838 #ifdef CLUSTER_SUPPORT
839         SIVAL(msg,54,e->pid.vnn);
840 #endif
841 }
842
843 /****************************************************************************
844  De-linearize an internal oplock break message to a share mode entry struct.
845 ****************************************************************************/
846
847 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
848 {
849         e->pid.pid = (pid_t)IVAL(msg,0);
850         e->op_mid = SVAL(msg,4);
851         e->op_type = SVAL(msg,6);
852         e->access_mask = IVAL(msg,8);
853         e->share_access = IVAL(msg,12);
854         e->private_options = IVAL(msg,16);
855         e->time.tv_sec = (time_t)IVAL(msg,20);
856         e->time.tv_usec = (int)IVAL(msg,24);
857         pull_file_id_16(msg+28, &e->id);
858         e->share_file_id = (unsigned long)IVAL(msg,44);
859         e->uid = (uint32)IVAL(msg,48);
860         e->flags = (uint16)SVAL(msg,52);
861 #ifdef CLUSTER_SUPPORT
862         e->pid.vnn = IVAL(msg,54);
863 #endif
864 }
865
866 /****************************************************************************
867  Setup oplocks for this process.
868 ****************************************************************************/
869
870 bool init_oplocks(struct messaging_context *msg_ctx)
871 {
872         DEBUG(3,("init_oplocks: initializing messages.\n"));
873
874         messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
875                            process_oplock_break_message);
876         messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
877                            process_oplock_async_level2_break_message);
878         messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
879                            process_oplock_break_response);
880         messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
881                            process_kernel_oplock_break);
882         messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
883                            process_open_retry_message);
884
885         if (lp_kernel_oplocks()) {
886 #if HAVE_KERNEL_OPLOCKS_IRIX
887                 koplocks = irix_init_kernel_oplocks();
888 #elif HAVE_KERNEL_OPLOCKS_LINUX
889                 koplocks = linux_init_kernel_oplocks();
890 #endif
891         }
892
893         return True;
894 }