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