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