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