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