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