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