b14c37002070a1b0951e539dfbce11ad55fc629b
[kai/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  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, NULL, NULL,
166                                   NULL);
167         if (lck == NULL) {
168                 DEBUG(0,("remove_oplock: failed to lock share entry for "
169                          "file %s\n", fsp_str_dbg(fsp)));
170                 return False;
171         }
172         ret = remove_share_oplock(lck, fsp);
173         if (!ret) {
174                 DEBUG(0,("remove_oplock: failed to remove share oplock for "
175                          "file %s fnum %d, %s\n",
176                          fsp_str_dbg(fsp), fsp->fnum,
177                          file_id_string_tos(&fsp->file_id)));
178         }
179         release_file_oplock(fsp);
180         TALLOC_FREE(lck);
181         return ret;
182 }
183
184 /*
185  * Deal with a reply when a break-to-level II was sent.
186  */
187 bool downgrade_oplock(files_struct *fsp)
188 {
189         bool ret;
190         struct share_mode_lock *lck;
191
192         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
193                                   NULL);
194         if (lck == NULL) {
195                 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
196                          "file %s\n", fsp_str_dbg(fsp)));
197                 return False;
198         }
199         ret = downgrade_share_oplock(lck, fsp);
200         if (!ret) {
201                 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
202                          "for file %s fnum %d, file_id %s\n",
203                          fsp_str_dbg(fsp), fsp->fnum,
204                          file_id_string_tos(&fsp->file_id)));
205         }
206
207         downgrade_file_oplock(fsp);
208         TALLOC_FREE(lck);
209         return ret;
210 }
211
212 /*
213  * Some kernel oplock implementations handle the notification themselves.
214  */
215 bool should_notify_deferred_opens()
216 {
217         return !(koplocks &&
218                 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
219 }
220
221 /****************************************************************************
222  Set up an oplock break message.
223 ****************************************************************************/
224
225 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
226                                    files_struct *fsp, int cmd)
227 {
228         char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
229
230         if (result == NULL) {
231                 DEBUG(0, ("talloc failed\n"));
232                 return NULL;
233         }
234
235         memset(result,'\0',smb_size);
236         srv_set_message(result,8,0,true);
237         SCVAL(result,smb_com,SMBlockingX);
238         SSVAL(result,smb_tid,fsp->conn->cnum);
239         SSVAL(result,smb_pid,0xFFFF);
240         SSVAL(result,smb_uid,0);
241         SSVAL(result,smb_mid,0xFFFF);
242         SCVAL(result,smb_vwv0,0xFF);
243         SSVAL(result,smb_vwv2,fsp->fnum);
244         SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
245         SCVAL(result,smb_vwv3+1,cmd);
246         return result;
247 }
248
249 /****************************************************************************
250  Function to do the waiting before sending a local break.
251 ****************************************************************************/
252
253 static void wait_before_sending_break(void)
254 {
255         long wait_time = (long)lp_oplock_break_wait_time();
256
257         if (wait_time) {
258                 smb_msleep(wait_time);
259         }
260 }
261
262 /****************************************************************************
263  Ensure that we have a valid oplock.
264 ****************************************************************************/
265
266 static files_struct *initial_break_processing(
267         struct smbd_server_connection *sconn, struct file_id id,
268         unsigned long file_id)
269 {
270         files_struct *fsp = NULL;
271
272         if( DEBUGLVL( 3 ) ) {
273                 dbgtext( "initial_break_processing: called for %s/%u\n",
274                          file_id_string_tos(&id), (int)file_id);
275                 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
276                         exclusive_oplocks_open, level_II_oplocks_open );
277         }
278
279         /*
280          * We need to search the file open table for the
281          * entry containing this dev and inode, and ensure
282          * we have an oplock on it.
283          */
284
285         fsp = file_find_dif(sconn, id, file_id);
286
287         if(fsp == NULL) {
288                 /* The file could have been closed in the meantime - return success. */
289                 if( DEBUGLVL( 3 ) ) {
290                         dbgtext( "initial_break_processing: cannot find open file with " );
291                         dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
292                         dbgtext( "allowing break to succeed.\n" );
293                 }
294                 return NULL;
295         }
296
297         /* Ensure we have an oplock on the file */
298
299         /*
300          * There is a potential race condition in that an oplock could
301          * have been broken due to another udp request, and yet there are
302          * still oplock break messages being sent in the udp message
303          * queue for this file. So return true if we don't have an oplock,
304          * as we may have just freed it.
305          */
306
307         if(fsp->oplock_type == NO_OPLOCK) {
308                 if( DEBUGLVL( 3 ) ) {
309                         dbgtext( "initial_break_processing: file %s ",
310                                  fsp_str_dbg(fsp));
311                         dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
312                                  file_id_string_tos(&id), fsp->fh->gen_id );
313                         dbgtext( "Allowing break to succeed regardless.\n" );
314                 }
315                 return NULL;
316         }
317
318         return fsp;
319 }
320
321 static void oplock_timeout_handler(struct event_context *ctx,
322                                    struct timed_event *te,
323                                    struct timeval now,
324                                    void *private_data)
325 {
326         files_struct *fsp = (files_struct *)private_data;
327
328         /* Remove the timed event handler. */
329         TALLOC_FREE(fsp->oplock_timeout);
330         DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
331                   fsp_str_dbg(fsp)));
332         remove_oplock(fsp);
333         reply_to_oplock_break_requests(fsp);
334 }
335
336 /*******************************************************************
337  Add a timeout handler waiting for the client reply.
338 *******************************************************************/
339
340 static void add_oplock_timeout_handler(files_struct *fsp)
341 {
342         /*
343          * If kernel oplocks already notifies smbds when an oplock break times
344          * out, just return.
345          */
346         if (koplocks &&
347             (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
348                 return;
349         }
350
351         if (fsp->oplock_timeout != NULL) {
352                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
353                           "around\n"));
354         }
355
356         fsp->oplock_timeout =
357                 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
358                                  timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
359                                  oplock_timeout_handler, fsp);
360
361         if (fsp->oplock_timeout == NULL) {
362                 DEBUG(0, ("Could not add oplock timeout handler\n"));
363         }
364 }
365
366 static void send_break_message_smb1(files_struct *fsp, int level)
367 {
368         char *break_msg = new_break_message_smb1(talloc_tos(),
369                                         fsp,
370                                         level);
371         if (break_msg == NULL) {
372                 exit_server("Could not talloc break_msg\n");
373         }
374
375         show_msg(break_msg);
376         if (!srv_send_smb(fsp->conn->sconn,
377                         break_msg, false, 0,
378                         IS_CONN_ENCRYPTED(fsp->conn),
379                         NULL)) {
380                 exit_server_cleanly("send_break_message_smb1: "
381                         "srv_send_smb failed.");
382         }
383
384         TALLOC_FREE(break_msg);
385 }
386
387 void break_level2_to_none_async(files_struct *fsp)
388 {
389         struct smbd_server_connection *sconn = fsp->conn->sconn;
390
391         if (fsp->oplock_type == NO_OPLOCK) {
392                 /* We already got a "break to none" message and we've handled
393                  * it.  just ignore. */
394                 DEBUG(3, ("process_oplock_async_level2_break_message: already "
395                           "broken to none, ignoring.\n"));
396                 return;
397         }
398
399         if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
400                 /* Don't tell the client, just downgrade. */
401                 DEBUG(3, ("process_oplock_async_level2_break_message: "
402                           "downgrading fake level 2 oplock.\n"));
403                 remove_oplock(fsp);
404                 return;
405         }
406
407         /* Ensure we're really at level2 state. */
408         SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
409
410         DEBUG(10,("process_oplock_async_level2_break_message: sending break "
411                   "to none message for fid %d, file %s\n", fsp->fnum,
412                   fsp_str_dbg(fsp)));
413
414         /* Now send a break to none message to our client. */
415         if (sconn->using_smb2) {
416                 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
417         } else {
418                 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
419         }
420
421         /* Async level2 request, don't send a reply, just remove the oplock. */
422         remove_oplock(fsp);
423 }
424
425 /*******************************************************************
426  This handles the case of a write triggering a break to none
427  message on a level2 oplock.
428  When we get this message we may be in any of three states :
429  NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
430  the client for LEVEL2.
431 *******************************************************************/
432
433 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
434                                                       void *private_data,
435                                                       uint32_t msg_type,
436                                                       struct server_id src,
437                                                       DATA_BLOB *data)
438 {
439         struct share_mode_entry msg;
440         files_struct *fsp;
441         struct smbd_server_connection *sconn =
442                 talloc_get_type_abort(private_data,
443                 struct smbd_server_connection);
444
445         if (data->data == NULL) {
446                 DEBUG(0, ("Got NULL buffer\n"));
447                 return;
448         }
449
450         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
451                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
452                 return;
453         }
454
455         /* De-linearize incoming message. */
456         message_to_share_mode_entry(&msg, (char *)data->data);
457
458         DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
459                    "%s/%llu\n", server_id_str(talloc_tos(), &src),
460                    file_id_string_tos(&msg.id),
461                    (unsigned long long)msg.share_file_id));
462
463         fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
464
465         if (fsp == NULL) {
466                 /* We hit a race here. Break messages are sent, and before we
467                  * get to process this message, we have closed the file. 
468                  * No need to reply as this is an async message. */
469                 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
470                 return;
471         }
472
473         break_level2_to_none_async(fsp);
474 }
475
476 /*******************************************************************
477  This handles the generic oplock break message from another smbd.
478 *******************************************************************/
479
480 static void process_oplock_break_message(struct messaging_context *msg_ctx,
481                                          void *private_data,
482                                          uint32_t msg_type,
483                                          struct server_id src,
484                                          DATA_BLOB *data)
485 {
486         struct share_mode_entry msg;
487         files_struct *fsp;
488         bool break_to_level2 = False;
489         struct smbd_server_connection *sconn =
490                 talloc_get_type_abort(private_data,
491                 struct smbd_server_connection);
492
493         if (data->data == NULL) {
494                 DEBUG(0, ("Got NULL buffer\n"));
495                 return;
496         }
497
498         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
499                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
500                 return;
501         }
502
503         /* De-linearize incoming message. */
504         message_to_share_mode_entry(&msg, (char *)data->data);
505
506         DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
507                    server_id_str(talloc_tos(), &src),
508                    file_id_string_tos(&msg.id),
509                    (unsigned long long)msg.share_file_id));
510
511         fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
512
513         if (fsp == NULL) {
514                 /* We hit a race here. Break messages are sent, and before we
515                  * get to process this message, we have closed the file. Reply
516                  * with 'ok, oplock broken' */
517                 DEBUG(3, ("Did not find fsp\n"));
518
519                 /* We just send the same message back. */
520                 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
521                                    (uint8 *)data->data,
522                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
523                 return;
524         }
525
526         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
527                 /* Remember we have to inform the requesting PID when the
528                  * client replies */
529                 msg.pid = src;
530                 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
531                              &fsp->pending_break_messages,
532                              &fsp->num_pending_break_messages);
533                 return;
534         }
535
536         if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
537             !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
538                 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
539                           file_id_string_tos(&fsp->file_id),
540                           fsp_str_dbg(fsp)));
541                 /* We just send the same message back. */
542                 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
543                                    (uint8 *)data->data,
544                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
545                 return;
546         }
547
548         if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) && 
549             !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
550             !(koplocks && !(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         if (fsp->oplock_timeout != NULL) {
668                 /* Remove the timed event handler. */
669                 TALLOC_FREE(fsp->oplock_timeout);
670                 fsp->oplock_timeout = NULL;
671         }
672         return;
673 }
674
675 static void process_oplock_break_response(struct messaging_context *msg_ctx,
676                                           void *private_data,
677                                           uint32_t msg_type,
678                                           struct server_id src,
679                                           DATA_BLOB *data)
680 {
681         struct share_mode_entry msg;
682         struct smbd_server_connection *sconn =
683                 talloc_get_type_abort(private_data,
684                 struct smbd_server_connection);
685
686         if (data->data == NULL) {
687                 DEBUG(0, ("Got NULL buffer\n"));
688                 return;
689         }
690
691         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
692                 DEBUG(0, ("Got invalid msg len %u\n",
693                           (unsigned int)data->length));
694                 return;
695         }
696
697         /* De-linearize incoming message. */
698         message_to_share_mode_entry(&msg, (char *)data->data);
699
700         DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
701                    server_id_str(talloc_tos(), &src),
702                    file_id_string_tos(&msg.id),
703                    (unsigned long long)msg.share_file_id,
704                    (unsigned long long)msg.op_mid));
705
706         schedule_deferred_open_message_smb(sconn, msg.op_mid);
707 }
708
709 static void process_open_retry_message(struct messaging_context *msg_ctx,
710                                        void *private_data,
711                                        uint32_t msg_type,
712                                        struct server_id src,
713                                        DATA_BLOB *data)
714 {
715         struct share_mode_entry msg;
716         struct smbd_server_connection *sconn =
717                 talloc_get_type_abort(private_data,
718                 struct smbd_server_connection);
719
720         if (data->data == NULL) {
721                 DEBUG(0, ("Got NULL buffer\n"));
722                 return;
723         }
724
725         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
726                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
727                 return;
728         }
729
730         /* De-linearize incoming message. */
731         message_to_share_mode_entry(&msg, (char *)data->data);
732
733         DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
734                    server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
735                    (unsigned long long)msg.op_mid));
736
737         schedule_deferred_open_message_smb(sconn, msg.op_mid);
738 }
739
740 /****************************************************************************
741  This function is called on any file modification or lock request. If a file
742  is level 2 oplocked then it must tell all other level 2 holders to break to
743  none.
744 ****************************************************************************/
745
746 static void contend_level2_oplocks_begin_default(files_struct *fsp,
747                                               enum level2_contention_type type)
748 {
749         int i;
750         struct share_mode_lock *lck;
751
752         /*
753          * If this file is level II oplocked then we need
754          * to grab the shared memory lock and inform all
755          * other files with a level II lock that they need
756          * to flush their read caches. We keep the lock over
757          * the shared memory area whilst doing this.
758          */
759
760         if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
761                 return;
762
763         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
764                                   NULL);
765         if (lck == NULL) {
766                 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
767                          "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
768                 return;
769         }
770
771         DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n", 
772                   lck->num_share_modes ));
773
774         for(i = 0; i < lck->num_share_modes; i++) {
775                 struct share_mode_entry *share_entry = &lck->share_modes[i];
776                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
777
778                 if (!is_valid_share_mode_entry(share_entry)) {
779                         continue;
780                 }
781
782                 /*
783                  * As there could have been multiple writes waiting at the
784                  * lock_share_entry gate we may not be the first to
785                  * enter. Hence the state of the op_types in the share mode
786                  * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
787                  * oplock. It will do no harm to re-send break messages to
788                  * those smbd's that are still waiting their turn to remove
789                  * their LEVEL_II state, and also no harm to ignore existing
790                  * NO_OPLOCK states. JRA.
791                  */
792
793                 DEBUG(10,("release_level_2_oplocks_on_change: "
794                           "share_entry[%i]->op_type == %d\n",
795                           i, share_entry->op_type ));
796
797                 if (share_entry->op_type == NO_OPLOCK) {
798                         continue;
799                 }
800
801                 /* Paranoia .... */
802                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
803                         DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
804                                  "share mode entry %d is an exlusive "
805                                  "oplock !\n", i ));
806                         TALLOC_FREE(lck);
807                         abort();
808                 }
809
810                 share_mode_entry_to_message(msg, share_entry);
811
812                 /*
813                  * Deal with a race condition when breaking level2
814                  * oplocks. Don't send all the messages and release
815                  * the lock, this allows someone else to come in and
816                  * get a level2 lock before any of the messages are
817                  * processed, and thus miss getting a break message.
818                  * Ensure at least one entry (the one we're breaking)
819                  * is processed immediately under the lock and becomes
820                  * set as NO_OPLOCK to stop any waiter getting a level2.
821                  * Bugid #5980.
822                  */
823
824                 if (procid_is_me(&share_entry->pid)) {
825                         struct files_struct *cur_fsp =
826                                 initial_break_processing(fsp->conn->sconn,
827                                         share_entry->id,
828                                         share_entry->share_file_id);
829                         wait_before_sending_break();
830                         if (cur_fsp != NULL) {
831                                 break_level2_to_none_async(cur_fsp);
832                         } else {
833                                 DEBUG(3, ("release_level_2_oplocks_on_change: "
834                                 "Did not find fsp, ignoring\n"));
835                         }
836                 } else {
837                         messaging_send_buf(fsp->conn->sconn->msg_ctx,
838                                         share_entry->pid,
839                                         MSG_SMB_ASYNC_LEVEL2_BREAK,
840                                         (uint8 *)msg,
841                                         MSG_SMB_SHARE_MODE_ENTRY_SIZE);
842                 }
843         }
844
845         /* We let the message receivers handle removing the oplock state
846            in the share mode lock db. */
847
848         TALLOC_FREE(lck);
849 }
850
851 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
852                                   enum level2_contention_type type)
853 {
854         if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
855                 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
856                 return;
857         }
858
859         contend_level2_oplocks_begin_default(fsp, type);
860 }
861
862 void smbd_contend_level2_oplocks_end(files_struct *fsp,
863                                 enum level2_contention_type type)
864 {
865         /* Only kernel oplocks implement this so far */
866         if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
867                 koplocks->ops->contend_level2_oplocks_end(fsp, type);
868         }
869 }
870
871 /****************************************************************************
872  Linearize a share mode entry struct to an internal oplock break message.
873 ****************************************************************************/
874
875 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
876 {
877         SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
878         SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
879         SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
880         SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
881         SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
882         SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
883         SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
884         SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
885         push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
886         SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
887         SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
888         SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
889         SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
890         SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
891 }
892
893 /****************************************************************************
894  De-linearize an internal oplock break message to a share mode entry struct.
895 ****************************************************************************/
896
897 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
898 {
899         e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
900         e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
901         e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
902         e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
903         e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
904         e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
905         e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
906         e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
907         pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
908         e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
909         e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
910         e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
911         e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
912         e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
913 }
914
915 /****************************************************************************
916  Setup oplocks for this process.
917 ****************************************************************************/
918
919 bool init_oplocks(struct smbd_server_connection *sconn)
920 {
921         DEBUG(3,("init_oplocks: initializing messages.\n"));
922
923         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
924                            process_oplock_break_message);
925         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
926                            process_oplock_async_level2_break_message);
927         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_RESPONSE,
928                            process_oplock_break_response);
929         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
930                            process_kernel_oplock_break);
931         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_OPEN_RETRY,
932                            process_open_retry_message);
933
934         if (lp_kernel_oplocks()) {
935 #if HAVE_KERNEL_OPLOCKS_IRIX
936                 koplocks = irix_init_kernel_oplocks(sconn);
937 #elif HAVE_KERNEL_OPLOCKS_LINUX
938                 koplocks = linux_init_kernel_oplocks(sconn);
939 #elif HAVE_ONEFS
940 #error Isilon, please check if the NULL context is okay here. Thanks!
941                 koplocks = onefs_init_kernel_oplocks(sconn);
942 #endif
943         }
944
945         return True;
946 }