Remove the smb_read_error global variable and replace
[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         set_message(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_string_tos(&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_string_tos(&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_string_tos(&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         /* Remove the timed event handler. */
345         TALLOC_FREE(fsp->oplock_timeout);
346         DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
347         global_client_failed_oplock_break = True;
348         remove_oplock(fsp);
349         reply_to_oplock_break_requests(fsp);
350 }
351
352 /*******************************************************************
353  Add a timeout handler waiting for the client reply.
354 *******************************************************************/
355
356 static void add_oplock_timeout_handler(files_struct *fsp)
357 {
358         if (fsp->oplock_timeout != NULL) {
359                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
360                           "around\n"));
361         }
362
363         fsp->oplock_timeout =
364                 event_add_timed(smbd_event_context(), NULL,
365                                 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
366                                 "oplock_timeout_handler",
367                                 oplock_timeout_handler, fsp);
368
369         if (fsp->oplock_timeout == NULL) {
370                 DEBUG(0, ("Could not add oplock timeout handler\n"));
371         }
372 }
373
374 /*******************************************************************
375  This handles the case of a write triggering a break to none
376  message on a level2 oplock.
377  When we get this message we may be in any of three states :
378  NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
379  the client for LEVEL2.
380 *******************************************************************/
381
382 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
383                                                       void *private_data,
384                                                       uint32_t msg_type,
385                                                       struct server_id src,
386                                                       DATA_BLOB *data)
387 {
388         struct share_mode_entry msg;
389         files_struct *fsp;
390         char *break_msg;
391         bool sign_state;
392
393         if (data->data == NULL) {
394                 DEBUG(0, ("Got NULL buffer\n"));
395                 return;
396         }
397
398         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
399                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
400                 return;
401         }
402
403         /* De-linearize incoming message. */
404         message_to_share_mode_entry(&msg, (char *)data->data);
405
406         DEBUG(10, ("Got oplock async level 2 break message from pid %d: %s/%lu\n",
407                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
408
409         fsp = initial_break_processing(msg.id, msg.share_file_id);
410
411         if (fsp == NULL) {
412                 /* We hit a race here. Break messages are sent, and before we
413                  * get to process this message, we have closed the file. 
414                  * No need to reply as this is an async message. */
415                 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
416                 return;
417         }
418
419         if (fsp->oplock_type == NO_OPLOCK) {
420                 /* We already got a "break to none" message and we've handled it.
421                  * just ignore. */
422                 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
423                 return;
424         }
425
426         if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
427                 /* Don't tell the client, just downgrade. */
428                 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
429                 remove_oplock(fsp);
430                 return;
431         }
432
433         /* Ensure we're really at level2 state. */
434         SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
435
436         /* Now send a break to none message to our client. */
437
438         break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
439         if (break_msg == NULL) {
440                 exit_server("Could not talloc break_msg\n");
441         }
442
443         /* Need to wait before sending a break message if we sent ourselves this message. */
444         if (procid_to_pid(&src) == sys_getpid()) {
445                 wait_before_sending_break();
446         }
447
448         /* Save the server smb signing state. */
449         sign_state = srv_oplock_set_signing(False);
450
451         show_msg(break_msg);
452         if (!send_smb(smbd_server_fd(), break_msg)) {
453                 exit_server_cleanly("oplock_break: send_smb failed.");
454         }
455
456         /* Restore the sign state to what it was. */
457         srv_oplock_set_signing(sign_state);
458
459         TALLOC_FREE(break_msg);
460
461         /* Async level2 request, don't send a reply, just remove the oplock. */
462         remove_oplock(fsp);
463 }
464
465 /*******************************************************************
466  This handles the generic oplock break message from another smbd.
467 *******************************************************************/
468
469 static void process_oplock_break_message(struct messaging_context *msg_ctx,
470                                          void *private_data,
471                                          uint32_t msg_type,
472                                          struct server_id src,
473                                          DATA_BLOB *data)
474 {
475         struct share_mode_entry msg;
476         files_struct *fsp;
477         char *break_msg;
478         bool break_to_level2 = False;
479         bool sign_state;
480
481         if (data->data == NULL) {
482                 DEBUG(0, ("Got NULL buffer\n"));
483                 return;
484         }
485
486         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
487                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
488                 return;
489         }
490
491         /* De-linearize incoming message. */
492         message_to_share_mode_entry(&msg, (char *)data->data);
493
494         DEBUG(10, ("Got oplock break message from pid %d: %s/%lu\n",
495                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
496
497         fsp = initial_break_processing(msg.id, msg.share_file_id);
498
499         if (fsp == NULL) {
500                 /* a We hit race here. Break messages are sent, and before we
501                  * get to process this message, we have closed the file. Reply
502                  * with 'ok, oplock broken' */
503                 DEBUG(3, ("Did not find fsp\n"));
504
505                 /* We just send the same message back. */
506                 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
507                                    (uint8 *)data->data,
508                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
509                 return;
510         }
511
512         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
513                 /* Remember we have to inform the requesting PID when the
514                  * client replies */
515                 msg.pid = src;
516                 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
517                              &fsp->pending_break_messages,
518                              &fsp->num_pending_break_messages);
519                 return;
520         }
521
522         if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
523             !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
524                 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
525                           file_id_string_tos(&fsp->file_id),
526                           fsp->fsp_name));
527                 /* We just send the same message back. */
528                 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
529                                    (uint8 *)data->data,
530                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
531                 return;
532         }
533
534         if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) && 
535             !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
536             !koplocks && /* NOTE: we force levelII off for kernel oplocks -
537                           * this will change when it is supported */
538             lp_level2_oplocks(SNUM(fsp->conn))) {
539                 break_to_level2 = True;
540         }
541
542         break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
543                                           OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
544         if (break_msg == NULL) {
545                 exit_server("Could not talloc break_msg\n");
546         }
547
548         /* Need to wait before sending a break message if we sent ourselves this message. */
549         if (procid_to_pid(&src) == sys_getpid()) {
550                 wait_before_sending_break();
551         }
552
553         /* Save the server smb signing state. */
554         sign_state = srv_oplock_set_signing(False);
555
556         show_msg(break_msg);
557         if (!send_smb(smbd_server_fd(), break_msg)) {
558                 exit_server_cleanly("oplock_break: send_smb failed.");
559         }
560
561         /* Restore the sign state to what it was. */
562         srv_oplock_set_signing(sign_state);
563
564         TALLOC_FREE(break_msg);
565
566         fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
567
568         msg.pid = src;
569         ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
570                      &fsp->pending_break_messages,
571                      &fsp->num_pending_break_messages);
572
573         add_oplock_timeout_handler(fsp);
574 }
575
576 /*******************************************************************
577  This handles the kernel oplock break message.
578 *******************************************************************/
579
580 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
581                                         void *private_data,
582                                         uint32_t msg_type,
583                                         struct server_id src,
584                                         DATA_BLOB *data)
585 {
586         struct file_id id;
587         unsigned long file_id;
588         files_struct *fsp;
589         char *break_msg;
590         bool sign_state;
591
592         if (data->data == NULL) {
593                 DEBUG(0, ("Got NULL buffer\n"));
594                 return;
595         }
596
597         if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
598                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
599                 return;
600         }
601
602         /* Pull the data from the message. */
603         pull_file_id_16((char *)data->data, &id);
604         file_id = (unsigned long)IVAL(data->data, 16);
605
606         DEBUG(10, ("Got kernel oplock break message from pid %d: %s/%u\n",
607                    (int)procid_to_pid(&src), file_id_string_tos(&id),
608                    (unsigned int)file_id));
609
610         fsp = initial_break_processing(id, file_id);
611
612         if (fsp == NULL) {
613                 DEBUG(3, ("Got a kernel oplock break message for a file "
614                           "I don't know about\n"));
615                 return;
616         }
617
618         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
619                 /* This is ok, kernel oplocks come in completely async */
620                 DEBUG(3, ("Got a kernel oplock request while waiting for a "
621                           "break reply\n"));
622                 return;
623         }
624
625         break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
626         if (break_msg == NULL) {
627                 exit_server("Could not talloc break_msg\n");
628         }
629
630         /* Save the server smb signing state. */
631         sign_state = srv_oplock_set_signing(False);
632
633         show_msg(break_msg);
634         if (!send_smb(smbd_server_fd(), break_msg)) {
635                 exit_server_cleanly("oplock_break: send_smb failed.");
636         }
637
638         /* Restore the sign state to what it was. */
639         srv_oplock_set_signing(sign_state);
640
641         TALLOC_FREE(break_msg);
642
643         fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
644
645         add_oplock_timeout_handler(fsp);
646 }
647
648 void reply_to_oplock_break_requests(files_struct *fsp)
649 {
650         int i;
651
652         for (i=0; i<fsp->num_pending_break_messages; i++) {
653                 struct share_mode_entry *e = &fsp->pending_break_messages[i];
654                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
655
656                 share_mode_entry_to_message(msg, e);
657
658                 messaging_send_buf(smbd_messaging_context(), e->pid,
659                                    MSG_SMB_BREAK_RESPONSE,
660                                    (uint8 *)msg,
661                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
662         }
663
664         SAFE_FREE(fsp->pending_break_messages);
665         fsp->num_pending_break_messages = 0;
666         if (fsp->oplock_timeout != NULL) {
667                 /* Remove the timed event handler. */
668                 TALLOC_FREE(fsp->oplock_timeout);
669                 fsp->oplock_timeout = NULL;
670         }
671         return;
672 }
673
674 static void process_oplock_break_response(struct messaging_context *msg_ctx,
675                                           void *private_data,
676                                           uint32_t msg_type,
677                                           struct server_id src,
678                                           DATA_BLOB *data)
679 {
680         struct share_mode_entry msg;
681
682         if (data->data == NULL) {
683                 DEBUG(0, ("Got NULL buffer\n"));
684                 return;
685         }
686
687         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
688                 DEBUG(0, ("Got invalid msg len %u\n",
689                           (unsigned int)data->length));
690                 return;
691         }
692
693         /* De-linearize incoming message. */
694         message_to_share_mode_entry(&msg, (char *)data->data);
695
696         DEBUG(10, ("Got oplock break response from pid %d: %s/%lu mid %u\n",
697                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id,
698                    (unsigned int)msg.op_mid));
699
700         /* Here's the hack from open.c, store the mid in the 'port' field */
701         schedule_deferred_open_smb_message(msg.op_mid);
702 }
703
704 static void process_open_retry_message(struct messaging_context *msg_ctx,
705                                        void *private_data,
706                                        uint32_t msg_type,
707                                        struct server_id src,
708                                        DATA_BLOB *data)
709 {
710         struct share_mode_entry msg;
711         
712         if (data->data == NULL) {
713                 DEBUG(0, ("Got NULL buffer\n"));
714                 return;
715         }
716
717         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
718                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
719                 return;
720         }
721
722         /* De-linearize incoming message. */
723         message_to_share_mode_entry(&msg, (char *)data->data);
724
725         DEBUG(10, ("Got open retry msg from pid %d: %s mid %u\n",
726                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id),
727                    (unsigned int)msg.op_mid));
728
729         schedule_deferred_open_smb_message(msg.op_mid);
730 }
731
732 /****************************************************************************
733  This function is called on any file modification or lock request. If a file
734  is level 2 oplocked then it must tell all other level 2 holders to break to
735  none.
736 ****************************************************************************/
737
738 void release_level_2_oplocks_on_change(files_struct *fsp)
739 {
740         int i;
741         struct share_mode_lock *lck;
742
743         /*
744          * If this file is level II oplocked then we need
745          * to grab the shared memory lock and inform all
746          * other files with a level II lock that they need
747          * to flush their read caches. We keep the lock over
748          * the shared memory area whilst doing this.
749          */
750
751         if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
752                 return;
753
754         lck = get_share_mode_lock(NULL, fsp->file_id, NULL, NULL);
755         if (lck == NULL) {
756                 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
757                          "share mode entry for file %s.\n", fsp->fsp_name ));
758                 return;
759         }
760
761         DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n", 
762                   lck->num_share_modes ));
763
764         for(i = 0; i < lck->num_share_modes; i++) {
765                 struct share_mode_entry *share_entry = &lck->share_modes[i];
766                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
767
768                 if (!is_valid_share_mode_entry(share_entry)) {
769                         continue;
770                 }
771
772                 /*
773                  * As there could have been multiple writes waiting at the
774                  * lock_share_entry gate we may not be the first to
775                  * enter. Hence the state of the op_types in the share mode
776                  * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
777                  * oplock. It will do no harm to re-send break messages to
778                  * those smbd's that are still waiting their turn to remove
779                  * their LEVEL_II state, and also no harm to ignore existing
780                  * NO_OPLOCK states. JRA.
781                  */
782
783                 DEBUG(10,("release_level_2_oplocks_on_change: "
784                           "share_entry[%i]->op_type == %d\n",
785                           i, share_entry->op_type ));
786
787                 if (share_entry->op_type == NO_OPLOCK) {
788                         continue;
789                 }
790
791                 /* Paranoia .... */
792                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
793                         DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
794                                  "share mode entry %d is an exlusive "
795                                  "oplock !\n", i ));
796                         TALLOC_FREE(lck);
797                         abort();
798                 }
799
800                 share_mode_entry_to_message(msg, share_entry);
801
802                 messaging_send_buf(smbd_messaging_context(), share_entry->pid,
803                                    MSG_SMB_ASYNC_LEVEL2_BREAK,
804                                    (uint8 *)msg,
805                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
806         }
807
808         /* We let the message receivers handle removing the oplock state
809            in the share mode lock db. */
810
811         TALLOC_FREE(lck);
812 }
813
814 /****************************************************************************
815  Linearize a share mode entry struct to an internal oplock break message.
816 ****************************************************************************/
817
818 void share_mode_entry_to_message(char *msg, struct share_mode_entry *e)
819 {
820         SIVAL(msg,0,(uint32)e->pid.pid);
821         SSVAL(msg,4,e->op_mid);
822         SSVAL(msg,6,e->op_type);
823         SIVAL(msg,8,e->access_mask);
824         SIVAL(msg,12,e->share_access);
825         SIVAL(msg,16,e->private_options);
826         SIVAL(msg,20,(uint32)e->time.tv_sec);
827         SIVAL(msg,24,(uint32)e->time.tv_usec);
828         push_file_id_16(msg+28, &e->id);
829         SIVAL(msg,44,e->share_file_id);
830         SIVAL(msg,48,e->uid);
831         SSVAL(msg,52,e->flags);
832 #ifdef CLUSTER_SUPPORT
833         SIVAL(msg,54,e->pid.vnn);
834 #endif
835 }
836
837 /****************************************************************************
838  De-linearize an internal oplock break message to a share mode entry struct.
839 ****************************************************************************/
840
841 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
842 {
843         e->pid.pid = (pid_t)IVAL(msg,0);
844         e->op_mid = SVAL(msg,4);
845         e->op_type = SVAL(msg,6);
846         e->access_mask = IVAL(msg,8);
847         e->share_access = IVAL(msg,12);
848         e->private_options = IVAL(msg,16);
849         e->time.tv_sec = (time_t)IVAL(msg,20);
850         e->time.tv_usec = (int)IVAL(msg,24);
851         pull_file_id_16(msg+28, &e->id);
852         e->share_file_id = (unsigned long)IVAL(msg,44);
853         e->uid = (uint32)IVAL(msg,48);
854         e->flags = (uint16)SVAL(msg,52);
855 #ifdef CLUSTER_SUPPORT
856         e->pid.vnn = IVAL(msg,54);
857 #endif
858 }
859
860 /****************************************************************************
861  Setup oplocks for this process.
862 ****************************************************************************/
863
864 bool init_oplocks(struct messaging_context *msg_ctx)
865 {
866         DEBUG(3,("init_oplocks: initializing messages.\n"));
867
868         messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
869                            process_oplock_break_message);
870         messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
871                            process_oplock_async_level2_break_message);
872         messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
873                            process_oplock_break_response);
874         messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
875                            process_kernel_oplock_break);
876         messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
877                            process_open_retry_message);
878
879         if (lp_kernel_oplocks()) {
880 #if HAVE_KERNEL_OPLOCKS_IRIX
881                 koplocks = irix_init_kernel_oplocks();
882 #elif HAVE_KERNEL_OPLOCKS_LINUX
883                 koplocks = linux_init_kernel_oplocks();
884 #endif
885         }
886
887         return True;
888 }