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