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