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