s3:smbd: fix file corruption using "write cache size != 0"
[garming/samba-autobuild/.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)
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, fsp->oplock_type))
71         {
72                 return map_nt_error_from_unix(errno);
73         }
74
75         fsp->sent_oplock_break = NO_BREAK_SENT;
76         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
77                 sconn->oplocks.level_II_open++;
78         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
79                 sconn->oplocks.exclusive_open++;
80         }
81
82         DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
83                     "tv_sec = %x, tv_usec = %x\n",
84                  fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
85                  fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
86                  (int)fsp->open_time.tv_usec ));
87
88         return NT_STATUS_OK;
89 }
90
91 /****************************************************************************
92  Attempt to release an oplock on a file. Decrements oplock count.
93 ****************************************************************************/
94
95 static void release_file_oplock(files_struct *fsp)
96 {
97         struct smbd_server_connection *sconn = fsp->conn->sconn;
98         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
99
100         if ((fsp->oplock_type != NO_OPLOCK) &&
101             koplocks) {
102                 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
103         }
104
105         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
106                 sconn->oplocks.level_II_open--;
107         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
108                 sconn->oplocks.exclusive_open--;
109         }
110
111         SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
112         SMB_ASSERT(sconn->oplocks.level_II_open>=0);
113
114         fsp->oplock_type = NO_OPLOCK;
115         fsp->sent_oplock_break = NO_BREAK_SENT;
116
117         flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
118         delete_write_cache(fsp);
119
120         TALLOC_FREE(fsp->oplock_timeout);
121 }
122
123 /****************************************************************************
124  Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
125 ****************************************************************************/
126
127 static void downgrade_file_oplock(files_struct *fsp)
128 {
129         struct smbd_server_connection *sconn = fsp->conn->sconn;
130         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
131
132         if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
133                 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
134                 return;
135         }
136
137         if (koplocks) {
138                 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
139         }
140         fsp->oplock_type = LEVEL_II_OPLOCK;
141         sconn->oplocks.exclusive_open--;
142         sconn->oplocks.level_II_open++;
143         fsp->sent_oplock_break = NO_BREAK_SENT;
144
145         flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
146         delete_write_cache(fsp);
147
148         TALLOC_FREE(fsp->oplock_timeout);
149 }
150
151 /****************************************************************************
152  Remove a file oplock. Copes with level II and exclusive.
153  Locks then unlocks the share mode lock. Client can decide to go directly
154  to none even if a "break-to-level II" was sent.
155 ****************************************************************************/
156
157 bool remove_oplock(files_struct *fsp)
158 {
159         bool ret;
160         struct share_mode_lock *lck;
161
162         DEBUG(10, ("remove_oplock called for %s\n",
163                    fsp_str_dbg(fsp)));
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
173         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
174
175                 /*
176                  * If we're the only LEVEL_II holder, we have to remove the
177                  * have_read_oplocks from the brlock entry
178                  */
179
180                 struct share_mode_data *data = lck->data;
181                 uint32_t i, num_level2;
182
183                 num_level2 = 0;
184                 for (i=0; i<data->num_share_modes; i++) {
185                         if (data->share_modes[i].op_type == LEVEL_II_OPLOCK) {
186                                 num_level2 += 1;
187                         }
188                         if (num_level2 > 1) {
189                                 /*
190                                  * No need to count them all...
191                                  */
192                                 break;
193                         }
194                 }
195
196                 if (num_level2 == 1) {
197                         /*
198                          * That's only us. We are dropping that level2 oplock,
199                          * so remove the brlock flag.
200                          */
201                         struct byte_range_lock *brl;
202
203                         brl = brl_get_locks(talloc_tos(), fsp);
204                         if (brl) {
205                                 brl_set_have_read_oplocks(brl, false);
206                                 TALLOC_FREE(brl);
207                         }
208                 }
209         }
210
211         ret = remove_share_oplock(lck, fsp);
212         if (!ret) {
213                 DEBUG(0,("remove_oplock: failed to remove share oplock for "
214                          "file %s, %s, %s\n",
215                          fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
216                          file_id_string_tos(&fsp->file_id)));
217         }
218         release_file_oplock(fsp);
219         TALLOC_FREE(lck);
220         return ret;
221 }
222
223 /*
224  * Deal with a reply when a break-to-level II was sent.
225  */
226 bool downgrade_oplock(files_struct *fsp)
227 {
228         bool ret;
229         struct share_mode_lock *lck;
230         struct byte_range_lock *brl;
231
232         DEBUG(10, ("downgrade_oplock called for %s\n",
233                    fsp_str_dbg(fsp)));
234
235         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
236         if (lck == NULL) {
237                 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
238                          "file %s\n", fsp_str_dbg(fsp)));
239                 return False;
240         }
241         ret = downgrade_share_oplock(lck, fsp);
242         if (!ret) {
243                 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
244                          "for file %s, %s, file_id %s\n",
245                          fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
246                          file_id_string_tos(&fsp->file_id)));
247         }
248
249         downgrade_file_oplock(fsp);
250
251         brl = brl_get_locks(talloc_tos(), fsp);
252         if (brl != NULL) {
253                 brl_set_have_read_oplocks(brl, true);
254                 TALLOC_FREE(brl);
255         }
256
257         TALLOC_FREE(lck);
258         return ret;
259 }
260
261 /****************************************************************************
262  Set up an oplock break message.
263 ****************************************************************************/
264
265 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
266
267 static void new_break_message_smb1(files_struct *fsp, int cmd,
268                                    char result[SMB1_BREAK_MESSAGE_LENGTH])
269 {
270         memset(result,'\0',smb_size);
271         srv_set_message(result,8,0,true);
272         SCVAL(result,smb_com,SMBlockingX);
273         SSVAL(result,smb_tid,fsp->conn->cnum);
274         SSVAL(result,smb_pid,0xFFFF);
275         SSVAL(result,smb_uid,0);
276         SSVAL(result,smb_mid,0xFFFF);
277         SCVAL(result,smb_vwv0,0xFF);
278         SSVAL(result,smb_vwv2,fsp->fnum);
279         SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
280         SCVAL(result,smb_vwv3+1,cmd);
281 }
282
283 /****************************************************************************
284  Function to do the waiting before sending a local break.
285 ****************************************************************************/
286
287 static void wait_before_sending_break(void)
288 {
289         long wait_time = (long)lp_oplock_break_wait_time();
290
291         if (wait_time) {
292                 smb_msleep(wait_time);
293         }
294 }
295
296 /****************************************************************************
297  Ensure that we have a valid oplock.
298 ****************************************************************************/
299
300 static files_struct *initial_break_processing(
301         struct smbd_server_connection *sconn, struct file_id id,
302         unsigned long file_id)
303 {
304         files_struct *fsp = NULL;
305
306         DEBUG(3, ("initial_break_processing: called for %s/%u\n"
307                   "Current oplocks_open (exclusive = %d, levelII = %d)\n",
308                   file_id_string_tos(&id), (int)file_id,
309                   sconn->oplocks.exclusive_open,
310                   sconn->oplocks.level_II_open));
311
312         /*
313          * We need to search the file open table for the
314          * entry containing this dev and inode, and ensure
315          * we have an oplock on it.
316          */
317
318         fsp = file_find_dif(sconn, id, file_id);
319
320         if(fsp == NULL) {
321                 /* The file could have been closed in the meantime - return success. */
322                 DEBUG(3, ("initial_break_processing: cannot find open file "
323                           "with file_id %s gen_id = %lu, allowing break to "
324                           "succeed.\n", file_id_string_tos(&id), file_id));
325                 return NULL;
326         }
327
328         /* Ensure we have an oplock on the file */
329
330         /*
331          * There is a potential race condition in that an oplock could
332          * have been broken due to another udp request, and yet there are
333          * still oplock break messages being sent in the udp message
334          * queue for this file. So return true if we don't have an oplock,
335          * as we may have just freed it.
336          */
337
338         if(fsp->oplock_type == NO_OPLOCK) {
339                 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
340                           "gen_id = %lu) has no oplock. Allowing break to "
341                           "succeed regardless.\n", fsp_str_dbg(fsp),
342                           file_id_string_tos(&id), fsp->fh->gen_id));
343                 return NULL;
344         }
345
346         return fsp;
347 }
348
349 static void oplock_timeout_handler(struct tevent_context *ctx,
350                                    struct tevent_timer *te,
351                                    struct timeval now,
352                                    void *private_data)
353 {
354         files_struct *fsp = (files_struct *)private_data;
355
356         SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
357
358         /* Remove the timed event handler. */
359         TALLOC_FREE(fsp->oplock_timeout);
360         DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
361                   fsp_str_dbg(fsp)));
362         remove_oplock(fsp);
363 }
364
365 /*******************************************************************
366  Add a timeout handler waiting for the client reply.
367 *******************************************************************/
368
369 static void add_oplock_timeout_handler(files_struct *fsp)
370 {
371         struct smbd_server_connection *sconn = fsp->conn->sconn;
372         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
373
374         /*
375          * If kernel oplocks already notifies smbds when an oplock break times
376          * out, just return.
377          */
378         if (koplocks &&
379             (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
380                 return;
381         }
382
383         if (fsp->oplock_timeout != NULL) {
384                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
385                           "around\n"));
386         }
387
388         fsp->oplock_timeout =
389                 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
390                                  timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
391                                  oplock_timeout_handler, fsp);
392
393         if (fsp->oplock_timeout == NULL) {
394                 DEBUG(0, ("Could not add oplock timeout handler\n"));
395         }
396 }
397
398 static void send_break_message_smb1(files_struct *fsp, int level)
399 {
400         struct smbXsrv_connection *xconn = NULL;
401         char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
402
403         /*
404          * For SMB1 we only have one connection
405          */
406         xconn = fsp->conn->sconn->client->connections;
407
408         new_break_message_smb1(fsp, level, break_msg);
409
410         show_msg(break_msg);
411         if (!srv_send_smb(xconn,
412                         break_msg, false, 0,
413                         IS_CONN_ENCRYPTED(fsp->conn),
414                         NULL)) {
415                 exit_server_cleanly("send_break_message_smb1: "
416                         "srv_send_smb failed.");
417         }
418 }
419
420 /*******************************************************************
421  This handles the generic oplock break message from another smbd.
422 *******************************************************************/
423
424 static void process_oplock_break_message(struct messaging_context *msg_ctx,
425                                          void *private_data,
426                                          uint32_t msg_type,
427                                          struct server_id src,
428                                          DATA_BLOB *data)
429 {
430         struct share_mode_entry msg;
431         files_struct *fsp;
432         bool break_to_level2 = False;
433         bool use_kernel;
434         struct smbd_server_connection *sconn =
435                 talloc_get_type_abort(private_data,
436                 struct smbd_server_connection);
437         struct server_id self = messaging_server_id(sconn->msg_ctx);
438         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
439         uint16_t break_to;
440
441         if (data->data == NULL) {
442                 DEBUG(0, ("Got NULL buffer\n"));
443                 return;
444         }
445
446         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
447                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
448                 return;
449         }
450
451         /* De-linearize incoming message. */
452         message_to_share_mode_entry(&msg, (char *)data->data);
453         break_to = msg.op_type;
454
455         DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
456                    (unsigned)break_to, 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                 DEBUG(3, ("Did not find fsp\n"));
466                 return;
467         }
468
469         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
470                 /*
471                  * Nothing to do anymore
472                  */
473                 return;
474         }
475
476         if (break_to == fsp->oplock_type) {
477                 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
478                           file_id_string_tos(&fsp->file_id),
479                           fsp_str_dbg(fsp)));
480                 return;
481         }
482
483         use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
484
485         if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
486             (break_to != NO_OPLOCK) &&
487             !(use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
488             lp_level2_oplocks(SNUM(fsp->conn))) {
489                 break_to_level2 = True;
490         }
491
492         /* Need to wait before sending a break
493            message if we sent ourselves this message. */
494         if (serverid_equal(&self, &src)) {
495                 wait_before_sending_break();
496         }
497
498         if (sconn->using_smb2) {
499                 send_break_message_smb2(fsp, break_to_level2 ?
500                         OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
501         } else {
502                 send_break_message_smb1(fsp, break_to_level2 ?
503                         OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
504         }
505
506         if ((fsp->oplock_type == LEVEL_II_OPLOCK) && (break_to == NO_OPLOCK)) {
507                 /*
508                  * This is an async break without a reply and thus no timeout
509                  */
510                 remove_oplock(fsp);
511                 return;
512         }
513         fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
514         add_oplock_timeout_handler(fsp);
515 }
516
517 /*******************************************************************
518  This handles the kernel oplock break message.
519 *******************************************************************/
520
521 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
522                                         void *private_data,
523                                         uint32_t msg_type,
524                                         struct server_id src,
525                                         DATA_BLOB *data)
526 {
527         struct file_id id;
528         unsigned long file_id;
529         files_struct *fsp;
530         struct smbd_server_connection *sconn =
531                 talloc_get_type_abort(private_data,
532                 struct smbd_server_connection);
533
534         if (data->data == NULL) {
535                 DEBUG(0, ("Got NULL buffer\n"));
536                 return;
537         }
538
539         if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
540                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
541                 return;
542         }
543
544         /* Pull the data from the message. */
545         pull_file_id_24((char *)data->data, &id);
546         file_id = (unsigned long)IVAL(data->data, 24);
547
548         DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
549                    server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
550                    (unsigned int)file_id));
551
552         fsp = initial_break_processing(sconn, id, file_id);
553
554         if (fsp == NULL) {
555                 DEBUG(3, ("Got a kernel oplock break message for a file "
556                           "I don't know about\n"));
557                 return;
558         }
559
560         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
561                 /* This is ok, kernel oplocks come in completely async */
562                 DEBUG(3, ("Got a kernel oplock request while waiting for a "
563                           "break reply\n"));
564                 return;
565         }
566
567         if (sconn->using_smb2) {
568                 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
569         } else {
570                 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
571         }
572
573         fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
574
575         add_oplock_timeout_handler(fsp);
576 }
577
578 struct break_to_none_state {
579         struct smbd_server_connection *sconn;
580         struct file_id id;
581 };
582 static void do_break_to_none(struct tevent_context *ctx,
583                              struct tevent_immediate *im,
584                              void *private_data);
585
586 /****************************************************************************
587  This function is called on any file modification or lock request. If a file
588  is level 2 oplocked then it must tell all other level 2 holders to break to
589  none.
590 ****************************************************************************/
591
592 static void contend_level2_oplocks_begin_default(files_struct *fsp,
593                                               enum level2_contention_type type)
594 {
595         struct smbd_server_connection *sconn = fsp->conn->sconn;
596         struct tevent_immediate *im;
597         struct break_to_none_state *state;
598         struct byte_range_lock *brl;
599
600         /*
601          * If this file is level II oplocked then we need
602          * to grab the shared memory lock and inform all
603          * other files with a level II lock that they need
604          * to flush their read caches. We keep the lock over
605          * the shared memory area whilst doing this.
606          */
607
608         if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
609                 /*
610                  * There can't be any level2 oplocks, we're alone.
611                  */
612                 return;
613         }
614
615         brl = brl_get_locks_readonly(fsp);
616         if ((brl != NULL) && !brl_have_read_oplocks(brl)) {
617                 DEBUG(10, ("No read oplocks around\n"));
618                 return;
619         }
620
621         /*
622          * When we get here we might have a brlock entry locked. Also
623          * locking the share mode entry would violate the locking
624          * order. Breaking level2 oplocks to none is asynchronous
625          * anyway, so we postpone this into an immediate event.
626          */
627
628         state = talloc(sconn, struct break_to_none_state);
629         if (state == NULL) {
630                 DEBUG(1, ("talloc failed\n"));
631                 return;
632         }
633         state->sconn = sconn;
634         state->id = fsp->file_id;
635
636         im = tevent_create_immediate(state);
637         if (im == NULL) {
638                 DEBUG(1, ("tevent_create_immediate failed\n"));
639                 TALLOC_FREE(state);
640                 return;
641         }
642         tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
643 }
644
645 static void do_break_to_none(struct tevent_context *ctx,
646                              struct tevent_immediate *im,
647                              void *private_data)
648 {
649         struct break_to_none_state *state = talloc_get_type_abort(
650                 private_data, struct break_to_none_state);
651         int i;
652         struct share_mode_lock *lck;
653
654         lck = get_existing_share_mode_lock(talloc_tos(), state->id);
655         if (lck == NULL) {
656                 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
657                           __func__, file_id_string_tos(&state->id)));
658                 goto done;
659         }
660
661         DEBUG(10,("%s: num_share_modes = %d\n", __func__,
662                   lck->data->num_share_modes ));
663
664         for(i = 0; i < lck->data->num_share_modes; i++) {
665                 struct share_mode_entry *share_entry = &lck->data->share_modes[i];
666                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
667
668                 if (!is_valid_share_mode_entry(share_entry)) {
669                         continue;
670                 }
671
672                 /*
673                  * As there could have been multiple writes waiting at the
674                  * lock_share_entry gate we may not be the first to
675                  * enter. Hence the state of the op_types in the share mode
676                  * entries may be partly NO_OPLOCK and partly LEVEL_II
677                  * oplock. It will do no harm to re-send break messages to
678                  * those smbd's that are still waiting their turn to remove
679                  * their LEVEL_II state, and also no harm to ignore existing
680                  * NO_OPLOCK states. JRA.
681                  */
682
683                 DEBUG(10,("%s: share_entry[%i]->op_type == %d\n", __func__,
684                           i, share_entry->op_type ));
685
686                 if (share_entry->op_type == NO_OPLOCK) {
687                         continue;
688                 }
689
690                 /* Paranoia .... */
691                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
692                         DEBUG(0,("%s: PANIC. "
693                                  "share mode entry %d is an exlusive "
694                                  "oplock !\n", __func__, i ));
695                         TALLOC_FREE(lck);
696                         abort();
697                 }
698
699                 share_mode_entry_to_message(msg, share_entry);
700                 /* Overload entry->op_type */
701                 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
702
703                 messaging_send_buf(state->sconn->msg_ctx, share_entry->pid,
704                                    MSG_SMB_BREAK_REQUEST,
705                                    (uint8 *)msg, sizeof(msg));
706         }
707
708         /* We let the message receivers handle removing the oplock state
709            in the share mode lock db. */
710
711         TALLOC_FREE(lck);
712 done:
713         TALLOC_FREE(state);
714         return;
715 }
716
717 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
718                                   enum level2_contention_type type)
719 {
720         struct smbd_server_connection *sconn = fsp->conn->sconn;
721         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
722
723         if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
724                 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
725                 return;
726         }
727
728         contend_level2_oplocks_begin_default(fsp, type);
729 }
730
731 void smbd_contend_level2_oplocks_end(files_struct *fsp,
732                                 enum level2_contention_type type)
733 {
734         struct smbd_server_connection *sconn = fsp->conn->sconn;
735         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
736
737         /* Only kernel oplocks implement this so far */
738         if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
739                 koplocks->ops->contend_level2_oplocks_end(fsp, type);
740         }
741 }
742
743 /****************************************************************************
744  Linearize a share mode entry struct to an internal oplock break message.
745 ****************************************************************************/
746
747 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
748 {
749         SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
750         SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
751         SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
752         SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
753         SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
754         SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
755         SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
756         SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
757         push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
758         SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
759         SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
760         SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
761         SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
762         SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
763 }
764
765 /****************************************************************************
766  De-linearize an internal oplock break message to a share mode entry struct.
767 ****************************************************************************/
768
769 void message_to_share_mode_entry(struct share_mode_entry *e, const char *msg)
770 {
771         e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
772         e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
773         e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
774         e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
775         e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
776         e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
777         e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
778         e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
779         pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
780         e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
781         e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
782         e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
783         e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
784         e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
785 }
786
787 /****************************************************************************
788  Setup oplocks for this process.
789 ****************************************************************************/
790
791 bool init_oplocks(struct smbd_server_connection *sconn)
792 {
793         DEBUG(3,("init_oplocks: initializing messages.\n"));
794
795         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
796                            process_oplock_break_message);
797         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
798                            process_kernel_oplock_break);
799         return true;
800 }
801
802 void init_kernel_oplocks(struct smbd_server_connection *sconn)
803 {
804         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
805
806         /* only initialize once */
807         if (koplocks == NULL) {
808 #if HAVE_KERNEL_OPLOCKS_IRIX
809                 koplocks = irix_init_kernel_oplocks(sconn);
810 #elif HAVE_KERNEL_OPLOCKS_LINUX
811                 koplocks = linux_init_kernel_oplocks(sconn);
812 #endif
813                 sconn->oplocks.kernel_ops = koplocks;
814         }
815 }