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