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