smbd: Remove FORCE_OPLOCK_BREAK_TO_NONE
[metze/samba/wip.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 NTSTATUS 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 NT_STATUS_NOT_SUPPORTED;
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         {
74                 return map_nt_error_from_unix(errno);
75         }
76
77         fsp->oplock_type = oplock_type;
78         fsp->sent_oplock_break = NO_BREAK_SENT;
79         if (oplock_type == LEVEL_II_OPLOCK) {
80                 sconn->oplocks.level_II_open++;
81         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
82                 sconn->oplocks.exclusive_open++;
83         }
84
85         DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
86                     "tv_sec = %x, tv_usec = %x\n",
87                  fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
88                  fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
89                  (int)fsp->open_time.tv_usec ));
90
91         return NT_STATUS_OK;
92 }
93
94 /****************************************************************************
95  Attempt to release an oplock on a file. Decrements oplock count.
96 ****************************************************************************/
97
98 void release_file_oplock(files_struct *fsp)
99 {
100         struct smbd_server_connection *sconn = fsp->conn->sconn;
101         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
102
103         if ((fsp->oplock_type != NO_OPLOCK) &&
104             (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
105             koplocks) {
106                 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
107         }
108
109         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
110                 sconn->oplocks.level_II_open--;
111         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
112                 sconn->oplocks.exclusive_open--;
113         }
114
115         SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
116         SMB_ASSERT(sconn->oplocks.level_II_open>=0);
117
118         if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
119                 /* This doesn't matter for close. */
120                 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
121         } else {
122                 fsp->oplock_type = NO_OPLOCK;
123         }
124         fsp->sent_oplock_break = NO_BREAK_SENT;
125
126         flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
127         delete_write_cache(fsp);
128
129         TALLOC_FREE(fsp->oplock_timeout);
130 }
131
132 /****************************************************************************
133  Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
134 ****************************************************************************/
135
136 static void downgrade_file_oplock(files_struct *fsp)
137 {
138         struct smbd_server_connection *sconn = fsp->conn->sconn;
139         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
140
141         if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
142                 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
143                 return;
144         }
145
146         if (koplocks) {
147                 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
148         }
149         fsp->oplock_type = LEVEL_II_OPLOCK;
150         sconn->oplocks.exclusive_open--;
151         sconn->oplocks.level_II_open++;
152         fsp->sent_oplock_break = NO_BREAK_SENT;
153
154         TALLOC_FREE(fsp->oplock_timeout);
155 }
156
157 /****************************************************************************
158  Remove a file oplock. Copes with level II and exclusive.
159  Locks then unlocks the share mode lock. Client can decide to go directly
160  to none even if a "break-to-level II" was sent.
161 ****************************************************************************/
162
163 bool remove_oplock(files_struct *fsp)
164 {
165         bool ret;
166         struct share_mode_lock *lck;
167
168         /* Remove the oplock flag from the sharemode. */
169         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
170         if (lck == NULL) {
171                 DEBUG(0,("remove_oplock: failed to lock share entry for "
172                          "file %s\n", fsp_str_dbg(fsp)));
173                 return False;
174         }
175         ret = remove_share_oplock(lck, fsp);
176         if (!ret) {
177                 DEBUG(0,("remove_oplock: failed to remove share oplock for "
178                          "file %s, %s, %s\n",
179                          fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
180                          file_id_string_tos(&fsp->file_id)));
181         }
182         release_file_oplock(fsp);
183         TALLOC_FREE(lck);
184         return ret;
185 }
186
187 /*
188  * Deal with a reply when a break-to-level II was sent.
189  */
190 bool downgrade_oplock(files_struct *fsp)
191 {
192         bool ret;
193         struct share_mode_lock *lck;
194
195         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
196         if (lck == NULL) {
197                 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
198                          "file %s\n", fsp_str_dbg(fsp)));
199                 return False;
200         }
201         ret = downgrade_share_oplock(lck, fsp);
202         if (!ret) {
203                 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
204                          "for file %s, %s, file_id %s\n",
205                          fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
206                          file_id_string_tos(&fsp->file_id)));
207         }
208
209         downgrade_file_oplock(fsp);
210         TALLOC_FREE(lck);
211         return ret;
212 }
213
214 /*
215  * Some kernel oplock implementations handle the notification themselves.
216  */
217 bool should_notify_deferred_opens(struct smbd_server_connection *sconn)
218 {
219         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
220         return !(koplocks &&
221                 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
222 }
223
224 /****************************************************************************
225  Set up an oplock break message.
226 ****************************************************************************/
227
228 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
229
230 static void new_break_message_smb1(files_struct *fsp, int cmd,
231                                    char result[SMB1_BREAK_MESSAGE_LENGTH])
232 {
233         memset(result,'\0',smb_size);
234         srv_set_message(result,8,0,true);
235         SCVAL(result,smb_com,SMBlockingX);
236         SSVAL(result,smb_tid,fsp->conn->cnum);
237         SSVAL(result,smb_pid,0xFFFF);
238         SSVAL(result,smb_uid,0);
239         SSVAL(result,smb_mid,0xFFFF);
240         SCVAL(result,smb_vwv0,0xFF);
241         SSVAL(result,smb_vwv2,fsp->fnum);
242         SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
243         SCVAL(result,smb_vwv3+1,cmd);
244 }
245
246 /****************************************************************************
247  Function to do the waiting before sending a local break.
248 ****************************************************************************/
249
250 static void wait_before_sending_break(void)
251 {
252         long wait_time = (long)lp_oplock_break_wait_time();
253
254         if (wait_time) {
255                 smb_msleep(wait_time);
256         }
257 }
258
259 /****************************************************************************
260  Ensure that we have a valid oplock.
261 ****************************************************************************/
262
263 static files_struct *initial_break_processing(
264         struct smbd_server_connection *sconn, struct file_id id,
265         unsigned long file_id)
266 {
267         files_struct *fsp = NULL;
268
269         if( DEBUGLVL( 3 ) ) {
270                 dbgtext( "initial_break_processing: called for %s/%u\n",
271                          file_id_string_tos(&id), (int)file_id);
272                 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
273                         sconn->oplocks.exclusive_open,
274                         sconn->oplocks.level_II_open);
275         }
276
277         /*
278          * We need to search the file open table for the
279          * entry containing this dev and inode, and ensure
280          * we have an oplock on it.
281          */
282
283         fsp = file_find_dif(sconn, id, file_id);
284
285         if(fsp == NULL) {
286                 /* The file could have been closed in the meantime - return success. */
287                 if( DEBUGLVL( 3 ) ) {
288                         dbgtext( "initial_break_processing: cannot find open file with " );
289                         dbgtext( "file_id %s gen_id = %lu, ", file_id_string_tos(&id), file_id);
290                         dbgtext( "allowing break to succeed.\n" );
291                 }
292                 return NULL;
293         }
294
295         /* Ensure we have an oplock on the file */
296
297         /*
298          * There is a potential race condition in that an oplock could
299          * have been broken due to another udp request, and yet there are
300          * still oplock break messages being sent in the udp message
301          * queue for this file. So return true if we don't have an oplock,
302          * as we may have just freed it.
303          */
304
305         if(fsp->oplock_type == NO_OPLOCK) {
306                 if( DEBUGLVL( 3 ) ) {
307                         dbgtext( "initial_break_processing: file %s ",
308                                  fsp_str_dbg(fsp));
309                         dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
310                                  file_id_string_tos(&id), fsp->fh->gen_id );
311                         dbgtext( "Allowing break to succeed regardless.\n" );
312                 }
313                 return NULL;
314         }
315
316         return fsp;
317 }
318
319 static void oplock_timeout_handler(struct tevent_context *ctx,
320                                    struct tevent_timer *te,
321                                    struct timeval now,
322                                    void *private_data)
323 {
324         files_struct *fsp = (files_struct *)private_data;
325
326         SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
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 }
334
335 /*******************************************************************
336  Add a timeout handler waiting for the client reply.
337 *******************************************************************/
338
339 static void add_oplock_timeout_handler(files_struct *fsp)
340 {
341         struct smbd_server_connection *sconn = fsp->conn->sconn;
342         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
343
344         /*
345          * If kernel oplocks already notifies smbds when an oplock break times
346          * out, just return.
347          */
348         if (koplocks &&
349             (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
350                 return;
351         }
352
353         if (fsp->oplock_timeout != NULL) {
354                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
355                           "around\n"));
356         }
357
358         fsp->oplock_timeout =
359                 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
360                                  timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
361                                  oplock_timeout_handler, fsp);
362
363         if (fsp->oplock_timeout == NULL) {
364                 DEBUG(0, ("Could not add oplock timeout handler\n"));
365         }
366 }
367
368 static void send_break_message_smb1(files_struct *fsp, int level)
369 {
370         char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
371
372         new_break_message_smb1(fsp, level, break_msg);
373
374         show_msg(break_msg);
375         if (!srv_send_smb(fsp->conn->sconn,
376                         break_msg, false, 0,
377                         IS_CONN_ENCRYPTED(fsp->conn),
378                         NULL)) {
379                 exit_server_cleanly("send_break_message_smb1: "
380                         "srv_send_smb failed.");
381         }
382 }
383
384 static 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 %s, file %s\n", fsp_fnum_dbg(fsp),
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         struct server_id self = messaging_server_id(sconn->msg_ctx);
491         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
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. */
516                 DEBUG(3, ("Did not find fsp\n"));
517                 return;
518         }
519
520         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
521                 /*
522                  * Nothing to do anymore
523                  */
524                 return;
525         }
526
527         if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
528             !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
529                 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
530                           file_id_string_tos(&fsp->file_id),
531                           fsp_str_dbg(fsp)));
532                 return;
533         }
534
535         use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
536
537         if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
538             !(use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
539             lp_level2_oplocks(SNUM(fsp->conn))) {
540                 break_to_level2 = True;
541         }
542
543         /* Need to wait before sending a break
544            message if we sent ourselves this message. */
545         if (serverid_equal(&self, &src)) {
546                 wait_before_sending_break();
547         }
548
549         if (sconn->using_smb2) {
550                 send_break_message_smb2(fsp, break_to_level2 ?
551                         OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
552         } else {
553                 send_break_message_smb1(fsp, break_to_level2 ?
554                         OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
555         }
556
557         fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
558
559         add_oplock_timeout_handler(fsp);
560 }
561
562 /*******************************************************************
563  This handles the kernel oplock break message.
564 *******************************************************************/
565
566 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
567                                         void *private_data,
568                                         uint32_t msg_type,
569                                         struct server_id src,
570                                         DATA_BLOB *data)
571 {
572         struct file_id id;
573         unsigned long file_id;
574         files_struct *fsp;
575         struct smbd_server_connection *sconn =
576                 talloc_get_type_abort(private_data,
577                 struct smbd_server_connection);
578
579         if (data->data == NULL) {
580                 DEBUG(0, ("Got NULL buffer\n"));
581                 return;
582         }
583
584         if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
585                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
586                 return;
587         }
588
589         /* Pull the data from the message. */
590         pull_file_id_24((char *)data->data, &id);
591         file_id = (unsigned long)IVAL(data->data, 24);
592
593         DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
594                    server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
595                    (unsigned int)file_id));
596
597         fsp = initial_break_processing(sconn, id, file_id);
598
599         if (fsp == NULL) {
600                 DEBUG(3, ("Got a kernel oplock break message for a file "
601                           "I don't know about\n"));
602                 return;
603         }
604
605         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
606                 /* This is ok, kernel oplocks come in completely async */
607                 DEBUG(3, ("Got a kernel oplock request while waiting for a "
608                           "break reply\n"));
609                 return;
610         }
611
612         if (sconn->using_smb2) {
613                 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
614         } else {
615                 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
616         }
617
618         fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
619
620         add_oplock_timeout_handler(fsp);
621 }
622
623 struct break_to_none_state {
624         struct smbd_server_connection *sconn;
625         struct file_id id;
626 };
627 static void do_break_to_none(struct tevent_context *ctx,
628                              struct tevent_immediate *im,
629                              void *private_data);
630
631 /****************************************************************************
632  This function is called on any file modification or lock request. If a file
633  is level 2 oplocked then it must tell all other level 2 holders to break to
634  none.
635 ****************************************************************************/
636
637 static void contend_level2_oplocks_begin_default(files_struct *fsp,
638                                               enum level2_contention_type type)
639 {
640         struct smbd_server_connection *sconn = fsp->conn->sconn;
641         struct tevent_immediate *im;
642         struct break_to_none_state *state;
643
644         /*
645          * If this file is level II oplocked then we need
646          * to grab the shared memory lock and inform all
647          * other files with a level II lock that they need
648          * to flush their read caches. We keep the lock over
649          * the shared memory area whilst doing this.
650          */
651
652         if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
653                 return;
654
655         /*
656          * When we get here we might have a brlock entry locked. Also
657          * locking the share mode entry would violate the locking
658          * order. Breaking level2 oplocks to none is asynchronous
659          * anyway, so we postpone this into an immediate timed event.
660          */
661
662         state = talloc(sconn, struct break_to_none_state);
663         if (state == NULL) {
664                 DEBUG(1, ("talloc failed\n"));
665                 return;
666         }
667         state->sconn = sconn;
668         state->id = fsp->file_id;
669
670         im = tevent_create_immediate(state);
671         if (im == NULL) {
672                 DEBUG(1, ("tevent_create_immediate failed\n"));
673                 TALLOC_FREE(state);
674                 return;
675         }
676         tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
677 }
678
679 static void do_break_to_none(struct tevent_context *ctx,
680                              struct tevent_immediate *im,
681                              void *private_data)
682 {
683         struct break_to_none_state *state = talloc_get_type_abort(
684                 private_data, struct break_to_none_state);
685         struct server_id self = messaging_server_id(state->sconn->msg_ctx);
686         int i;
687         struct share_mode_lock *lck;
688
689         lck = get_existing_share_mode_lock(talloc_tos(), state->id);
690         if (lck == NULL) {
691                 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
692                           "share mode entry for file %s.\n",
693                           file_id_string_tos(&state->id)));
694                 goto done;
695         }
696
697         DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n", 
698                   lck->data->num_share_modes ));
699
700         for(i = 0; i < lck->data->num_share_modes; i++) {
701                 struct share_mode_entry *share_entry = &lck->data->share_modes[i];
702                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
703
704                 if (!is_valid_share_mode_entry(share_entry)) {
705                         continue;
706                 }
707
708                 /*
709                  * As there could have been multiple writes waiting at the
710                  * lock_share_entry gate we may not be the first to
711                  * enter. Hence the state of the op_types in the share mode
712                  * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
713                  * oplock. It will do no harm to re-send break messages to
714                  * those smbd's that are still waiting their turn to remove
715                  * their LEVEL_II state, and also no harm to ignore existing
716                  * NO_OPLOCK states. JRA.
717                  */
718
719                 DEBUG(10,("release_level_2_oplocks_on_change: "
720                           "share_entry[%i]->op_type == %d\n",
721                           i, share_entry->op_type ));
722
723                 if (share_entry->op_type == NO_OPLOCK) {
724                         continue;
725                 }
726
727                 /* Paranoia .... */
728                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
729                         DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
730                                  "share mode entry %d is an exlusive "
731                                  "oplock !\n", i ));
732                         TALLOC_FREE(lck);
733                         abort();
734                 }
735
736                 share_mode_entry_to_message(msg, share_entry);
737
738                 /*
739                  * Deal with a race condition when breaking level2
740                  * oplocks. Don't send all the messages and release
741                  * the lock, this allows someone else to come in and
742                  * get a level2 lock before any of the messages are
743                  * processed, and thus miss getting a break message.
744                  * Ensure at least one entry (the one we're breaking)
745                  * is processed immediately under the lock and becomes
746                  * set as NO_OPLOCK to stop any waiter getting a level2.
747                  * Bugid #5980.
748                  */
749
750                 if (serverid_equal(&self, &share_entry->pid)) {
751                         struct files_struct *cur_fsp =
752                                 initial_break_processing(state->sconn,
753                                         share_entry->id,
754                                         share_entry->share_file_id);
755                         if (cur_fsp != NULL) {
756                                 wait_before_sending_break();
757                                 break_level2_to_none_async(cur_fsp);
758                         } else {
759                                 DEBUG(3, ("release_level_2_oplocks_on_change: "
760                                 "Did not find fsp, ignoring\n"));
761                         }
762                 } else {
763                         messaging_send_buf(state->sconn->msg_ctx,
764                                         share_entry->pid,
765                                         MSG_SMB_ASYNC_LEVEL2_BREAK,
766                                         (uint8 *)msg,
767                                         MSG_SMB_SHARE_MODE_ENTRY_SIZE);
768                 }
769         }
770
771         /* We let the message receivers handle removing the oplock state
772            in the share mode lock db. */
773
774         TALLOC_FREE(lck);
775 done:
776         TALLOC_FREE(state);
777         return;
778 }
779
780 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
781                                   enum level2_contention_type type)
782 {
783         struct smbd_server_connection *sconn = fsp->conn->sconn;
784         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
785
786         if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
787                 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
788                 return;
789         }
790
791         contend_level2_oplocks_begin_default(fsp, type);
792 }
793
794 void smbd_contend_level2_oplocks_end(files_struct *fsp,
795                                 enum level2_contention_type type)
796 {
797         struct smbd_server_connection *sconn = fsp->conn->sconn;
798         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
799
800         /* Only kernel oplocks implement this so far */
801         if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
802                 koplocks->ops->contend_level2_oplocks_end(fsp, type);
803         }
804 }
805
806 /****************************************************************************
807  Linearize a share mode entry struct to an internal oplock break message.
808 ****************************************************************************/
809
810 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
811 {
812         SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
813         SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
814         SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
815         SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
816         SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
817         SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
818         SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
819         SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
820         push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
821         SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
822         SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
823         SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
824         SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
825         SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
826 }
827
828 /****************************************************************************
829  De-linearize an internal oplock break message to a share mode entry struct.
830 ****************************************************************************/
831
832 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
833 {
834         e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
835         e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
836         e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
837         e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
838         e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
839         e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
840         e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
841         e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
842         pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
843         e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
844         e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
845         e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
846         e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
847         e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
848 }
849
850 /****************************************************************************
851  Setup oplocks for this process.
852 ****************************************************************************/
853
854 bool init_oplocks(struct smbd_server_connection *sconn)
855 {
856         DEBUG(3,("init_oplocks: initializing messages.\n"));
857
858         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
859                            process_oplock_break_message);
860         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
861                            process_oplock_async_level2_break_message);
862         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
863                            process_kernel_oplock_break);
864         return true;
865 }
866
867 void init_kernel_oplocks(struct smbd_server_connection *sconn)
868 {
869         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
870
871         /* only initialize once */
872         if (koplocks == NULL) {
873 #if HAVE_KERNEL_OPLOCKS_IRIX
874                 koplocks = irix_init_kernel_oplocks(sconn);
875 #elif HAVE_KERNEL_OPLOCKS_LINUX
876                 koplocks = linux_init_kernel_oplocks(sconn);
877 #endif
878                 sconn->oplocks.kernel_ops = koplocks;
879         }
880 }