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