]> git.ipfire.org Git - thirdparty/samba.git/blob
b3da84b1269
[thirdparty/samba.git] /
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 "lib/util/server_id.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "messages.h"
28 #include "locking/leases_db.h"
29 #include "../librpc/gen_ndr/ndr_open_files.h"
30
31 /*
32 * helper function used by the kernel oplock backends to post the break message
33 */
34 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
35 {
36 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
37
38 /* Put the kernel break info into the message. */
39 push_file_id_24((char *)msg, &fsp->file_id);
40 SIVAL(msg,24,fsp->fh->gen_id);
41
42 /* Don't need to be root here as we're only ever
43 sending to ourselves. */
44
45 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
46 MSG_SMB_KERNEL_BREAK,
47 msg, MSG_SMB_KERNEL_BREAK_SIZE);
48 }
49
50 /****************************************************************************
51 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
52 disabled (just sets flags).
53 ****************************************************************************/
54
55 NTSTATUS set_file_oplock(files_struct *fsp)
56 {
57 struct smbd_server_connection *sconn = fsp->conn->sconn;
58 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
59 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
60 (koplocks != NULL);
61
62 if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
64 "don't support them\n"));
65 return NT_STATUS_NOT_SUPPORTED;
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 DBG_INFO("granted oplock on file %s, %s/%"PRIu64", "
83 "tv_sec = %x, tv_usec = %x\n",
84 fsp_str_dbg(fsp),
85 file_id_string_tos(&fsp->file_id),
86 fsp->fh->gen_id,
87 (int)fsp->open_time.tv_sec,
88 (int)fsp->open_time.tv_usec);
89
90 return NT_STATUS_OK;
91 }
92
93 static void release_fsp_kernel_oplock(files_struct *fsp)
94 {
95 struct smbd_server_connection *sconn = fsp->conn->sconn;
96 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
97 bool use_kernel;
98
99 if (koplocks == NULL) {
100 return;
101 }
102 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
103 if (!use_kernel) {
104 return;
105 }
106 if (fsp->oplock_type == NO_OPLOCK) {
107 return;
108 }
109 if (fsp->oplock_type == LEASE_OPLOCK) {
110 /*
111 * For leases we don't touch kernel oplocks at all
112 */
113 return;
114 }
115
116 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
117 }
118
119 /****************************************************************************
120 Attempt to release an oplock on a file. Decrements oplock count.
121 ****************************************************************************/
122
123 static void release_file_oplock(files_struct *fsp)
124 {
125 struct smbd_server_connection *sconn = fsp->conn->sconn;
126
127 release_fsp_kernel_oplock(fsp);
128
129 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
130 sconn->oplocks.level_II_open--;
131 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
132 sconn->oplocks.exclusive_open--;
133 }
134
135 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
136 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
137
138 fsp->oplock_type = NO_OPLOCK;
139 fsp->sent_oplock_break = NO_BREAK_SENT;
140
141 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
142 delete_write_cache(fsp);
143
144 TALLOC_FREE(fsp->oplock_timeout);
145 }
146
147 /****************************************************************************
148 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
149 ****************************************************************************/
150
151 static void downgrade_file_oplock(files_struct *fsp)
152 {
153 struct smbd_server_connection *sconn = fsp->conn->sconn;
154 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
155 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
156 (koplocks != NULL);
157
158 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
159 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
160 return;
161 }
162
163 if (use_kernel) {
164 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
165 }
166 fsp->oplock_type = LEVEL_II_OPLOCK;
167 sconn->oplocks.exclusive_open--;
168 sconn->oplocks.level_II_open++;
169 fsp->sent_oplock_break = NO_BREAK_SENT;
170
171 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
172 delete_write_cache(fsp);
173
174 TALLOC_FREE(fsp->oplock_timeout);
175 }
176
177 uint32_t get_lease_type(const struct share_mode_entry *e, struct file_id id)
178 {
179 if (e->op_type == LEASE_OPLOCK) {
180 NTSTATUS status;
181 uint32_t current_state;
182
183 status = leases_db_get(
184 &e->client_guid,
185 &e->lease_key,
186 &id,
187 &current_state,
188 NULL, /* breaking */
189 NULL, /* breaking_to_requested */
190 NULL, /* breaking_to_required */
191 NULL, /* lease_version */
192 NULL); /* epoch */
193 SMB_ASSERT(NT_STATUS_IS_OK(status));
194 return current_state;
195 }
196 return map_oplock_to_lease_type(e->op_type);
197 }
198
199 /****************************************************************************
200 Remove a file oplock. Copes with level II and exclusive.
201 Locks then unlocks the share mode lock. Client can decide to go directly
202 to none even if a "break-to-level II" was sent.
203 ****************************************************************************/
204
205 bool remove_oplock(files_struct *fsp)
206 {
207 bool ret;
208 struct share_mode_lock *lck;
209
210 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
211
212 /* Remove the oplock flag from the sharemode. */
213 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
214 if (lck == NULL) {
215 DBG_ERR("failed to lock share entry for "
216 "file %s\n", fsp_str_dbg(fsp));
217 return false;
218 }
219
220 ret = remove_share_oplock(lck, fsp);
221 if (!ret) {
222 DBG_ERR("failed to remove share oplock for "
223 "file %s, %s, %s\n",
224 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
225 file_id_string_tos(&fsp->file_id));
226 }
227 release_file_oplock(fsp);
228
229 TALLOC_FREE(lck);
230 return ret;
231 }
232
233 /*
234 * Deal with a reply when a break-to-level II was sent.
235 */
236 bool downgrade_oplock(files_struct *fsp)
237 {
238 bool ret;
239 struct share_mode_lock *lck;
240
241 DEBUG(10, ("downgrade_oplock called for %s\n",
242 fsp_str_dbg(fsp)));
243
244 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
245 if (lck == NULL) {
246 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
247 "file %s\n", fsp_str_dbg(fsp)));
248 return False;
249 }
250 ret = downgrade_share_oplock(lck, fsp);
251 if (!ret) {
252 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
253 "for file %s, %s, file_id %s\n",
254 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
255 file_id_string_tos(&fsp->file_id)));
256 }
257 downgrade_file_oplock(fsp);
258
259 TALLOC_FREE(lck);
260 return ret;
261 }
262
263 static void lease_timeout_handler(struct tevent_context *ctx,
264 struct tevent_timer *te,
265 struct timeval now,
266 void *private_data)
267 {
268 struct fsp_lease *lease =
269 talloc_get_type_abort(private_data,
270 struct fsp_lease);
271 struct files_struct *fsp;
272 struct share_mode_lock *lck;
273 uint16_t old_epoch = lease->lease.lease_epoch;
274
275 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
276 &lease->lease.lease_key);
277 if (fsp == NULL) {
278 /* race? */
279 TALLOC_FREE(lease->timeout);
280 return;
281 }
282
283 /*
284 * Paranoia check: There can only be one fsp_lease per lease
285 * key
286 */
287 SMB_ASSERT(fsp->lease == lease);
288
289 lck = get_existing_share_mode_lock(
290 talloc_tos(), fsp->file_id);
291 if (lck == NULL) {
292 /* race? */
293 TALLOC_FREE(lease->timeout);
294 return;
295 }
296
297 fsp_lease_update(fsp);
298
299 if (lease->lease.lease_epoch != old_epoch) {
300 /*
301 * If the epoch changed we need to wait for
302 * the next timeout to happen.
303 */
304 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
305 fsp_str_dbg(fsp)));
306 TALLOC_FREE(lck);
307 return;
308 }
309
310 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
311 /*
312 * If the epoch changed we need to wait for
313 * the next timeout to happen.
314 */
315 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
316 fsp_str_dbg(fsp)));
317 TALLOC_FREE(lck);
318 return;
319 }
320
321 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
322 fsp_str_dbg(fsp)));
323 (void)downgrade_lease(lease->sconn->client->connections,
324 1,
325 &fsp->file_id,
326 &lease->lease.lease_key,
327 SMB2_LEASE_NONE);
328
329 TALLOC_FREE(lck);
330 }
331
332 bool fsp_lease_update(struct files_struct *fsp)
333 {
334 const struct GUID *client_guid = fsp_client_guid(fsp);
335 struct fsp_lease *lease = fsp->lease;
336 uint32_t current_state;
337 bool breaking;
338 uint16_t lease_version, epoch;
339 NTSTATUS status;
340
341 status = leases_db_get(client_guid,
342 &lease->lease.lease_key,
343 &fsp->file_id,
344 &current_state,
345 &breaking,
346 NULL, /* breaking_to_requested */
347 NULL, /* breaking_to_required */
348 &lease_version,
349 &epoch);
350 if (!NT_STATUS_IS_OK(status)) {
351 DBG_WARNING("Could not find lease entry: %s\n",
352 nt_errstr(status));
353 TALLOC_FREE(lease->timeout);
354 lease->lease.lease_state = SMB2_LEASE_NONE;
355 lease->lease.lease_epoch += 1;
356 lease->lease.lease_flags = 0;
357 return false;
358 }
359
360 DEBUG(10,("%s: refresh lease state\n", __func__));
361
362 /* Ensure we're in sync with current lease state. */
363 if (lease->lease.lease_epoch != epoch) {
364 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
365 TALLOC_FREE(lease->timeout);
366 }
367 lease->lease.lease_epoch = epoch;
368 lease->lease.lease_state = current_state;
369
370 if (breaking) {
371 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
372
373 if (lease->timeout == NULL) {
374 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
375
376 DEBUG(10,("%s: setup timeout handler\n", __func__));
377
378 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
379 lease, t,
380 lease_timeout_handler,
381 lease);
382 if (lease->timeout == NULL) {
383 DEBUG(0, ("%s: Could not add lease timeout handler\n",
384 __func__));
385 }
386 }
387 } else {
388 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
389 TALLOC_FREE(lease->timeout);
390 }
391
392 return true;
393 }
394
395 struct downgrade_lease_additional_state {
396 struct tevent_immediate *im;
397 struct smbXsrv_connection *xconn;
398 uint32_t break_flags;
399 struct smb2_lease_key lease_key;
400 uint32_t break_from;
401 uint32_t break_to;
402 uint16_t new_epoch;
403 };
404
405 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
406 struct tevent_immediate *im,
407 void *private_data)
408 {
409 struct downgrade_lease_additional_state *state =
410 talloc_get_type_abort(private_data,
411 struct downgrade_lease_additional_state);
412 struct smbXsrv_connection *xconn = state->xconn;
413 NTSTATUS status;
414
415 status = smbd_smb2_send_lease_break(xconn,
416 state->new_epoch,
417 state->break_flags,
418 &state->lease_key,
419 state->break_from,
420 state->break_to);
421 TALLOC_FREE(state);
422 if (!NT_STATUS_IS_OK(status)) {
423 smbd_server_connection_terminate(xconn,
424 nt_errstr(status));
425 return;
426 }
427 }
428
429 struct fsps_lease_update_state {
430 const struct file_id *id;
431 const struct smb2_lease_key *key;
432 };
433
434 static struct files_struct *fsps_lease_update_fn(
435 struct files_struct *fsp, void *private_data)
436 {
437 struct fsps_lease_update_state *state =
438 (struct fsps_lease_update_state *)private_data;
439
440 if (fsp->oplock_type != LEASE_OPLOCK) {
441 return NULL;
442 }
443 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
444 return NULL;
445 }
446 if (!file_id_equal(&fsp->file_id, state->id)) {
447 return NULL;
448 }
449
450 fsp_lease_update(fsp);
451
452 return NULL;
453 }
454
455 static void fsps_lease_update(struct smbd_server_connection *sconn,
456 const struct file_id *id,
457 const struct smb2_lease_key *key)
458 {
459 struct fsps_lease_update_state state = { .id = id, .key = key };
460 files_forall(sconn, fsps_lease_update_fn, &state);
461 }
462
463 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
464 uint32_t num_file_ids,
465 const struct file_id *ids,
466 const struct smb2_lease_key *key,
467 uint32_t lease_state)
468 {
469 struct smbd_server_connection *sconn = xconn->client->sconn;
470 const struct GUID *client_guid = NULL;
471 struct share_mode_lock *lck;
472 const struct file_id id = ids[0];
473 uint32_t current_state, breaking_to_requested, breaking_to_required;
474 bool breaking;
475 uint16_t lease_version, epoch;
476 NTSTATUS status;
477 uint32_t i;
478
479 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
480 file_id_string_tos(&id), (unsigned)lease_state));
481
482 lck = get_existing_share_mode_lock(talloc_tos(), id);
483 if (lck == NULL) {
484 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
485 }
486
487 client_guid = &sconn->client->connections->smb2.client.guid;
488
489 status = leases_db_get(client_guid,
490 key,
491 &id,
492 &current_state,
493 &breaking,
494 &breaking_to_requested,
495 &breaking_to_required,
496 &lease_version,
497 &epoch);
498 if (!NT_STATUS_IS_OK(status)) {
499 DBG_WARNING("leases_db_get returned %s\n",
500 nt_errstr(status));
501 TALLOC_FREE(lck);
502 return status;
503 }
504
505 if (!breaking) {
506 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
507 "but we're not in breaking state\n",
508 current_state, lease_state);
509 TALLOC_FREE(lck);
510 return NT_STATUS_UNSUCCESSFUL;
511 }
512
513 /*
514 * Can't upgrade anything: breaking_to_requested (and current_state)
515 * must be a strict bitwise superset of new_lease_state
516 */
517 if ((lease_state & breaking_to_requested) != lease_state) {
518 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
519 "- expected %"PRIu32"\n",
520 current_state, lease_state,
521 breaking_to_requested);
522 TALLOC_FREE(lck);
523 return NT_STATUS_REQUEST_NOT_ACCEPTED;
524 }
525
526 if (current_state != lease_state) {
527 current_state = lease_state;
528 }
529
530 status = NT_STATUS_OK;
531
532 if ((lease_state & ~breaking_to_required) != 0) {
533 struct downgrade_lease_additional_state *state;
534
535 DBG_INFO("lease state %"PRIu32" not fully broken from "
536 "%"PRIu32" to %"PRIu32"\n",
537 lease_state,
538 current_state,
539 breaking_to_required);
540
541 breaking_to_requested = breaking_to_required;
542
543 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
544 /*
545 * Here we break in steps, as windows does
546 * see the breaking3 and v2_breaking3 tests.
547 */
548 breaking_to_requested |= SMB2_LEASE_READ;
549 }
550
551 state = talloc_zero(xconn,
552 struct downgrade_lease_additional_state);
553 if (state == NULL) {
554 TALLOC_FREE(lck);
555 return NT_STATUS_NO_MEMORY;
556 }
557
558 state->im = tevent_create_immediate(state);
559 if (state->im == NULL) {
560 TALLOC_FREE(state);
561 TALLOC_FREE(lck);
562 return NT_STATUS_NO_MEMORY;
563 }
564
565 state->xconn = xconn;
566 state->lease_key = *key;
567 state->break_from = current_state;
568 state->break_to = breaking_to_requested;
569 if (lease_version > 1) {
570 state->new_epoch = epoch;
571 }
572
573 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
574 state->break_flags =
575 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
576 } else {
577 /*
578 * This is an async break without
579 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
580 *
581 * we need to store NONE state in the
582 * database.
583 */
584 current_state = 0;
585 breaking_to_requested = 0;
586 breaking_to_required = 0;
587 breaking = false;
588
589 {
590 NTSTATUS set_status;
591
592 set_status = leases_db_set(
593 &sconn->client->connections->
594 smb2.client.guid,
595 key,
596 current_state,
597 breaking,
598 breaking_to_requested,
599 breaking_to_required,
600 lease_version,
601 epoch);
602
603 if (!NT_STATUS_IS_OK(set_status)) {
604 DBG_DEBUG("leases_db_set failed: %s\n",
605 nt_errstr(set_status));
606 return set_status;
607 }
608 }
609 }
610
611 tevent_schedule_immediate(state->im,
612 xconn->client->raw_ev_ctx,
613 downgrade_lease_additional_trigger,
614 state);
615
616 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
617 } else {
618 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
619 "expected %"PRIu32"\n",
620 current_state,
621 lease_state,
622 breaking_to_requested);
623
624 breaking_to_requested = 0;
625 breaking_to_required = 0;
626 breaking = false;
627 }
628
629 {
630 NTSTATUS set_status;
631
632 set_status = leases_db_set(
633 client_guid,
634 key,
635 current_state,
636 breaking,
637 breaking_to_requested,
638 breaking_to_required,
639 lease_version,
640 epoch);
641
642 if (!NT_STATUS_IS_OK(set_status)) {
643 DBG_DEBUG("leases_db_set failed: %s\n",
644 nt_errstr(set_status));
645 TALLOC_FREE(lck);
646 return set_status;
647 }
648 }
649
650 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
651 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
652
653 /*
654 * No, we did not modify the share mode array. We did modify
655 * the leases_db. But without this we don't notify a lease
656 * break waiter via dbwrap_watch_record. We need to make
657 * leases_db watched too.
658 */
659 lck->data->modified = true;
660
661 fsps_lease_update(sconn, &id, key);
662
663 TALLOC_FREE(lck);
664 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
665 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
666
667 /*
668 * Dynamic share case. Ensure other opens are copies.
669 * This will only be breaking to NONE.
670 */
671
672 for (i = 1; i < num_file_ids; i++) {
673 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
674 if (lck == NULL) {
675 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
676 }
677
678 fsps_lease_update(sconn, &ids[i], key);
679
680 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
681 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
682
683 TALLOC_FREE(lck);
684 }
685
686 return status;
687 }
688
689 /****************************************************************************
690 Set up an oplock break message.
691 ****************************************************************************/
692
693 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
694
695 static void new_break_message_smb1(files_struct *fsp, int cmd,
696 char result[SMB1_BREAK_MESSAGE_LENGTH])
697 {
698 memset(result,'\0',smb_size);
699 srv_set_message(result,8,0,true);
700 SCVAL(result,smb_com,SMBlockingX);
701 SSVAL(result,smb_tid,fsp->conn->cnum);
702 SSVAL(result,smb_pid,0xFFFF);
703 SSVAL(result,smb_uid,0);
704 SSVAL(result,smb_mid,0xFFFF);
705 SCVAL(result,smb_vwv0,0xFF);
706 SSVAL(result,smb_vwv2,fsp->fnum);
707 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
708 SCVAL(result,smb_vwv3+1,cmd);
709 }
710
711 /****************************************************************************
712 Function to do the waiting before sending a local break.
713 ****************************************************************************/
714
715 static void wait_before_sending_break(void)
716 {
717 long wait_time = (long)lp_oplock_break_wait_time();
718
719 if (wait_time) {
720 smb_msleep(wait_time);
721 }
722 }
723
724 /****************************************************************************
725 Ensure that we have a valid oplock.
726 ****************************************************************************/
727
728 static files_struct *initial_break_processing(
729 struct smbd_server_connection *sconn, struct file_id id,
730 unsigned long file_id)
731 {
732 files_struct *fsp = NULL;
733
734 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
735 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
736 file_id_string_tos(&id), (int)file_id,
737 sconn->oplocks.exclusive_open,
738 sconn->oplocks.level_II_open));
739
740 /*
741 * We need to search the file open table for the
742 * entry containing this dev and inode, and ensure
743 * we have an oplock on it.
744 */
745
746 fsp = file_find_dif(sconn, id, file_id);
747
748 if(fsp == NULL) {
749 /* The file could have been closed in the meantime - return success. */
750 DEBUG(3, ("initial_break_processing: cannot find open file "
751 "with file_id %s gen_id = %lu, allowing break to "
752 "succeed.\n", file_id_string_tos(&id), file_id));
753 return NULL;
754 }
755
756 /* Ensure we have an oplock on the file */
757
758 /*
759 * There is a potential race condition in that an oplock could
760 * have been broken due to another udp request, and yet there are
761 * still oplock break messages being sent in the udp message
762 * queue for this file. So return true if we don't have an oplock,
763 * as we may have just freed it.
764 */
765
766 if(fsp->oplock_type == NO_OPLOCK) {
767 DBG_NOTICE("file %s (file_id = %s gen_id = %"PRIu64") "
768 "has no oplock. "
769 "Allowing break to succeed regardless.\n",
770 fsp_str_dbg(fsp),
771 file_id_string_tos(&id),
772 fsp->fh->gen_id);
773 return NULL;
774 }
775
776 return fsp;
777 }
778
779 static void oplock_timeout_handler(struct tevent_context *ctx,
780 struct tevent_timer *te,
781 struct timeval now,
782 void *private_data)
783 {
784 files_struct *fsp = (files_struct *)private_data;
785
786 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
787
788 /* Remove the timed event handler. */
789 TALLOC_FREE(fsp->oplock_timeout);
790 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
791 fsp_str_dbg(fsp)));
792 remove_oplock(fsp);
793 }
794
795 /*******************************************************************
796 Add a timeout handler waiting for the client reply.
797 *******************************************************************/
798
799 static void add_oplock_timeout_handler(files_struct *fsp)
800 {
801 if (fsp->oplock_timeout != NULL) {
802 DEBUG(0, ("Logic problem -- have an oplock event hanging "
803 "around\n"));
804 }
805
806 fsp->oplock_timeout =
807 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
808 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
809 oplock_timeout_handler, fsp);
810
811 if (fsp->oplock_timeout == NULL) {
812 DEBUG(0, ("Could not add oplock timeout handler\n"));
813 }
814 }
815
816 static void send_break_message_smb1(files_struct *fsp, int level)
817 {
818 struct smbXsrv_connection *xconn = NULL;
819 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
820
821 /*
822 * For SMB1 we only have one connection
823 */
824 xconn = fsp->conn->sconn->client->connections;
825
826 new_break_message_smb1(fsp, level, break_msg);
827
828 show_msg(break_msg);
829 if (!srv_send_smb(xconn,
830 break_msg, false, 0,
831 IS_CONN_ENCRYPTED(fsp->conn),
832 NULL)) {
833 exit_server_cleanly("send_break_message_smb1: "
834 "srv_send_smb failed.");
835 }
836 }
837
838 /*******************************************************************
839 This handles the generic oplock break message from another smbd.
840 *******************************************************************/
841
842 static void process_oplock_break_message(struct messaging_context *msg_ctx,
843 void *private_data,
844 uint32_t msg_type,
845 struct server_id src,
846 DATA_BLOB *data)
847 {
848 struct oplock_break_message *msg = NULL;
849 enum ndr_err_code ndr_err;
850 files_struct *fsp;
851 bool use_kernel;
852 struct smbd_server_connection *sconn =
853 talloc_get_type_abort(private_data,
854 struct smbd_server_connection);
855 struct server_id self = messaging_server_id(sconn->msg_ctx);
856 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
857 uint16_t break_from;
858 uint16_t break_to;
859 bool break_needed = true;
860
861 msg = talloc(talloc_tos(), struct oplock_break_message);
862 if (msg == NULL) {
863 DBG_WARNING("talloc failed\n");
864 return;
865 }
866
867 ndr_err = ndr_pull_struct_blob_all(
868 data,
869 msg,
870 msg,
871 (ndr_pull_flags_fn_t)ndr_pull_oplock_break_message);
872 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
873 DBG_DEBUG("ndr_pull_oplock_break_message failed: %s\n",
874 ndr_errstr(ndr_err));
875 TALLOC_FREE(msg);
876 return;
877 }
878 if (DEBUGLEVEL >= 10) {
879 struct server_id_buf buf;
880 DBG_DEBUG("Got break message from %s\n",
881 server_id_str_buf(src, &buf));
882 NDR_PRINT_DEBUG(oplock_break_message, msg);
883 }
884
885 break_to = msg->break_to;
886 fsp = initial_break_processing(sconn, msg->id, msg->share_file_id);
887
888 TALLOC_FREE(msg);
889
890 if (fsp == NULL) {
891 /* We hit a race here. Break messages are sent, and before we
892 * get to process this message, we have closed the file. */
893 DEBUG(3, ("Did not find fsp\n"));
894 return;
895 }
896
897 break_from = fsp_lease_type(fsp);
898
899 if (fsp->oplock_type != LEASE_OPLOCK) {
900 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
901 /*
902 * Nothing to do anymore
903 */
904 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
905 fsp->sent_oplock_break));
906 return;
907 }
908 }
909
910 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
911 DEBUG(10, ("client_caps without level2 oplocks\n"));
912 break_to &= ~SMB2_LEASE_READ;
913 }
914
915 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
916 (koplocks != NULL);
917 if (use_kernel) {
918 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
919 break_to &= ~SMB2_LEASE_READ;
920 }
921
922 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
923 DEBUG(10, ("no level2 oplocks by config\n"));
924 break_to &= ~SMB2_LEASE_READ;
925 }
926
927 if (fsp->oplock_type == LEASE_OPLOCK) {
928 const struct GUID *client_guid = fsp_client_guid(fsp);
929 struct share_mode_lock *lck;
930 uint32_t current_state;
931 uint32_t breaking_to_requested, breaking_to_required;
932 bool breaking;
933 uint16_t lease_version, epoch;
934 NTSTATUS status;
935
936 lck = get_existing_share_mode_lock(
937 talloc_tos(), fsp->file_id);
938 if (lck == NULL) {
939 /*
940 * We hit a race here. Break messages are sent, and
941 * before we get to process this message, we have closed
942 * the file.
943 */
944 DEBUG(3, ("Did not find share_mode\n"));
945 return;
946 }
947
948 status = leases_db_get(client_guid,
949 &fsp->lease->lease.lease_key,
950 &fsp->file_id,
951 &current_state,
952 &breaking,
953 &breaking_to_requested,
954 &breaking_to_required,
955 &lease_version,
956 &epoch);
957 if (!NT_STATUS_IS_OK(status)) {
958 DBG_WARNING("leases_db_get returned %s\n",
959 nt_errstr(status));
960 TALLOC_FREE(lck);
961 return;
962 }
963
964 break_from = current_state;
965 break_to &= current_state;
966
967 if (breaking) {
968 break_to &= breaking_to_required;
969 if (breaking_to_required != break_to) {
970 /*
971 * Note we don't increment the epoch
972 * here, which might be a bug in
973 * Windows too...
974 */
975 breaking_to_required = break_to;
976 }
977 break_needed = false;
978 } else if (current_state == break_to) {
979 break_needed = false;
980 } else if (current_state == SMB2_LEASE_READ) {
981 current_state = SMB2_LEASE_NONE;
982 /* Need to increment the epoch */
983 epoch += 1;
984 } else {
985 breaking = true;
986 breaking_to_required = break_to;
987 breaking_to_requested = break_to;
988 /* Need to increment the epoch */
989 epoch += 1;
990 }
991
992 {
993 NTSTATUS set_status;
994
995 set_status = leases_db_set(
996 client_guid,
997 &fsp->lease->lease.lease_key,
998 current_state,
999 breaking,
1000 breaking_to_requested,
1001 breaking_to_required,
1002 lease_version,
1003 epoch);
1004
1005 if (!NT_STATUS_IS_OK(set_status)) {
1006 DBG_DEBUG("leases_db_set failed: %s\n",
1007 nt_errstr(set_status));
1008 return;
1009 }
1010 }
1011
1012 /* Ensure we're in sync with current lease state. */
1013 fsp_lease_update(fsp);
1014
1015 TALLOC_FREE(lck);
1016 }
1017
1018 if (!break_needed) {
1019 DEBUG(10,("%s: skip break\n", __func__));
1020 return;
1021 }
1022
1023 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1024 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1025 file_id_string_tos(&fsp->file_id),
1026 fsp_str_dbg(fsp)));
1027 return;
1028 }
1029
1030 DEBUG(10, ("break_from=%u, break_to=%u\n",
1031 (unsigned)break_from, (unsigned)break_to));
1032
1033 if ((break_from == break_to) && !break_needed) {
1034 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1035 (unsigned)break_to,
1036 file_id_string_tos(&fsp->file_id),
1037 fsp_str_dbg(fsp)));
1038 return;
1039 }
1040
1041 /* Need to wait before sending a break
1042 message if we sent ourselves this message. */
1043 if (serverid_equal(&self, &src)) {
1044 wait_before_sending_break();
1045 }
1046
1047 if (sconn->using_smb2) {
1048 send_break_message_smb2(fsp, break_from, break_to);
1049 } else {
1050 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1051 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1052 }
1053
1054 if ((break_from == SMB2_LEASE_READ) &&
1055 (break_to == SMB2_LEASE_NONE)) {
1056 /*
1057 * This is an async break without a reply and thus no timeout
1058 *
1059 * leases are handled above.
1060 */
1061 if (fsp->oplock_type != LEASE_OPLOCK) {
1062 remove_oplock(fsp);
1063 }
1064 return;
1065 }
1066 if (fsp->oplock_type == LEASE_OPLOCK) {
1067 return;
1068 }
1069
1070 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1071 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1072
1073 add_oplock_timeout_handler(fsp);
1074 }
1075
1076 /*******************************************************************
1077 This handles the kernel oplock break message.
1078 *******************************************************************/
1079
1080 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1081 void *private_data,
1082 uint32_t msg_type,
1083 struct server_id src,
1084 DATA_BLOB *data)
1085 {
1086 struct file_id id;
1087 unsigned long file_id;
1088 files_struct *fsp;
1089 struct smbd_server_connection *sconn =
1090 talloc_get_type_abort(private_data,
1091 struct smbd_server_connection);
1092 struct server_id_buf tmp;
1093
1094 if (data->data == NULL) {
1095 DEBUG(0, ("Got NULL buffer\n"));
1096 return;
1097 }
1098
1099 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1100 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1101 return;
1102 }
1103
1104 /* Pull the data from the message. */
1105 pull_file_id_24((char *)data->data, &id);
1106 file_id = (unsigned long)IVAL(data->data, 24);
1107
1108 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1109 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1110 (unsigned int)file_id));
1111
1112 fsp = initial_break_processing(sconn, id, file_id);
1113
1114 if (fsp == NULL) {
1115 DEBUG(3, ("Got a kernel oplock break message for a file "
1116 "I don't know about\n"));
1117 return;
1118 }
1119
1120 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1121 /* This is ok, kernel oplocks come in completely async */
1122 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1123 "break reply\n"));
1124 return;
1125 }
1126
1127 if (sconn->using_smb2) {
1128 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1129 } else {
1130 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1131 }
1132
1133 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1134
1135 add_oplock_timeout_handler(fsp);
1136 }
1137
1138 static void send_break_to_none(struct messaging_context *msg_ctx,
1139 const struct file_id *id,
1140 const struct share_mode_entry *e)
1141 {
1142 NTSTATUS status;
1143 status = send_break_message(msg_ctx, id, e, OPLOCK_NONE);
1144 if (!NT_STATUS_IS_OK(status)) {
1145 DBG_DEBUG("send_break_message failed: %s\n",
1146 nt_errstr(status));
1147 }
1148 }
1149 struct break_to_none_state {
1150 struct smbd_server_connection *sconn;
1151 struct file_id id;
1152 struct smb2_lease_key lease_key;
1153 struct GUID client_guid;
1154 size_t num_broken;
1155 };
1156
1157 static bool do_break_lease_to_none(struct share_mode_entry *e,
1158 void *private_data)
1159 {
1160 struct break_to_none_state *state = private_data;
1161 uint32_t current_state = 0;
1162 bool our_own;
1163 NTSTATUS status;
1164
1165 DBG_DEBUG("lease_key=%"PRIu64"/%"PRIu64"\n",
1166 e->lease_key.data[0],
1167 e->lease_key.data[1]);
1168
1169 status = leases_db_get(&e->client_guid,
1170 &e->lease_key,
1171 &state->id,
1172 &current_state,
1173 NULL, /* breaking */
1174 NULL, /* breaking_to_requested */
1175 NULL, /* breaking_to_required */
1176 NULL, /* lease_version */
1177 NULL); /* epoch */
1178 if (!NT_STATUS_IS_OK(status)) {
1179 DBG_WARNING("leases_db_get failed: %s\n",
1180 nt_errstr(status));
1181 return false;
1182 }
1183
1184 if ((current_state & SMB2_LEASE_READ) == 0) {
1185 return false;
1186 }
1187
1188 our_own = smb2_lease_equal(&state->client_guid,
1189 &state->lease_key,
1190 &e->client_guid,
1191 &e->lease_key);
1192 if (our_own) {
1193 DEBUG(10, ("Don't break our own lease\n"));
1194 return false;
1195 }
1196
1197 DBG_DEBUG("Breaking %"PRIu64"/%"PRIu64" to none\n",
1198 e->lease_key.data[0],
1199 e->lease_key.data[1]);
1200
1201 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1202
1203 state->num_broken += 1;
1204
1205 return false;
1206 }
1207
1208 static bool do_break_oplock_to_none(struct share_mode_entry *e,
1209 bool *modified,
1210 void *private_data)
1211 {
1212 struct break_to_none_state *state = private_data;
1213
1214 if (e->op_type == LEASE_OPLOCK) {
1215 /*
1216 * Already being taken care of
1217 */
1218 return false;
1219 }
1220
1221 /*
1222 * As there could have been multiple writes waiting at the
1223 * lock_share_entry gate we may not be the first to
1224 * enter. Hence the state of the op_types in the share mode
1225 * entries may be partly NO_OPLOCK and partly LEVEL_II
1226 * oplock. It will do no harm to re-send break messages to
1227 * those smbd's that are still waiting their turn to remove
1228 * their LEVEL_II state, and also no harm to ignore existing
1229 * NO_OPLOCK states. JRA.
1230 */
1231
1232 DBG_DEBUG("e->op_type == %d\n", e->op_type);
1233
1234 if (e->op_type == NO_OPLOCK) {
1235 return false;
1236 }
1237
1238 /* Paranoia .... */
1239 SMB_ASSERT(!EXCLUSIVE_OPLOCK_TYPE(e->op_type));
1240
1241 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1242 state->num_broken += 1;
1243
1244 return false;
1245 }
1246
1247 /****************************************************************************
1248 This function is called on any file modification or lock request. If a file
1249 is level 2 oplocked then it must tell all other level 2 holders to break to
1250 none.
1251 ****************************************************************************/
1252
1253 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1254 enum level2_contention_type type)
1255 {
1256 struct break_to_none_state state = {
1257 .sconn = fsp->conn->sconn, .id = fsp->file_id,
1258 };
1259 struct share_mode_lock *lck = NULL;
1260 struct share_mode_data *d = NULL;
1261 bool ok, has_read_lease;
1262
1263 /*
1264 * If this file is level II oplocked then we need
1265 * to grab the shared memory lock and inform all
1266 * other files with a level II lock that they need
1267 * to flush their read caches. We keep the lock over
1268 * the shared memory area whilst doing this.
1269 */
1270
1271 if (fsp_lease_type_is_exclusive(fsp)) {
1272 /*
1273 * There can't be any level2 oplocks, we're alone.
1274 */
1275 return;
1276 }
1277
1278 has_read_lease = file_has_read_lease(fsp);
1279 if (!has_read_lease) {
1280 DEBUG(10, ("No read oplocks around\n"));
1281 return;
1282 }
1283
1284 if (fsp->oplock_type == LEASE_OPLOCK) {
1285 state.client_guid = *fsp_client_guid(fsp);
1286 state.lease_key = fsp->lease->lease.lease_key;
1287 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1288 state.lease_key.data[0],
1289 state.lease_key.data[1]));
1290 }
1291
1292 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1293 if (lck == NULL) {
1294 DBG_WARNING("failed to lock share mode entry for file %s.\n",
1295 file_id_string_tos(&state.id));
1296 return;
1297 }
1298 d = lck->data;
1299
1300 /*
1301 * Walk leases and oplocks separately: We have to send one break per
1302 * lease. If we have multiple share_mode_entry having a common lease,
1303 * we would break the lease twice if we don't walk the leases list
1304 * separately.
1305 */
1306
1307 ok = share_mode_forall_leases(lck, do_break_lease_to_none, &state);
1308 if (!ok) {
1309 DBG_WARNING("share_mode_forall_leases failed\n");
1310 }
1311
1312 ok = share_mode_forall_entries(lck, do_break_oplock_to_none, &state);
1313 if (!ok) {
1314 DBG_WARNING("share_mode_forall_entries failed\n");
1315 }
1316
1317 if (state.num_broken == 0) {
1318 /*
1319 * Lazy update here. It might be that the read lease
1320 * has gone in the meantime.
1321 */
1322 d->flags &= ~SHARE_MODE_LEASE_READ;
1323 d->modified = true;
1324 }
1325
1326 TALLOC_FREE(lck);
1327 }
1328
1329 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1330 enum level2_contention_type type)
1331 {
1332 contend_level2_oplocks_begin_default(fsp, type);
1333 }
1334
1335 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1336 enum level2_contention_type type)
1337 {
1338 return;
1339 }
1340
1341 /****************************************************************************
1342 Linearize a share mode entry struct to an internal oplock break message.
1343 ****************************************************************************/
1344
1345 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1346 const struct share_mode_entry *e)
1347 {
1348 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1349 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1350 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1351 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1352 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1353 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1354 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1355 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1356 /*
1357 * "id" used to be part of share_mode_entry, thus the strange
1358 * place to put this. Feel free to move somewhere else :-)
1359 */
1360 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1361 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1362 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1363 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1364 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1365 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1366 }
1367
1368 /****************************************************************************
1369 De-linearize an internal oplock break message to a share mode entry struct.
1370 ****************************************************************************/
1371
1372 void message_to_share_mode_entry(struct file_id *id,
1373 struct share_mode_entry *e,
1374 const char *msg)
1375 {
1376 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1377 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1378 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1379 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1380 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1381 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1382 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1383 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1384 /*
1385 * "id" used to be part of share_mode_entry, thus the strange
1386 * place to put this. Feel free to move somewhere else :-)
1387 */
1388 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1389 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1390 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1391 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1392 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1393 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1394 }
1395
1396 /****************************************************************************
1397 Setup oplocks for this process.
1398 ****************************************************************************/
1399
1400 bool init_oplocks(struct smbd_server_connection *sconn)
1401 {
1402 DEBUG(3,("init_oplocks: initializing messages.\n"));
1403
1404 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1405 process_oplock_break_message);
1406 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1407 process_kernel_oplock_break);
1408 return true;
1409 }
1410
1411 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1412 {
1413 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1414
1415 /* only initialize once */
1416 if (koplocks == NULL) {
1417 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1418 koplocks = linux_init_kernel_oplocks(sconn);
1419 #endif
1420 sconn->oplocks.kernel_ops = koplocks;
1421 }
1422 }