2 * Unix SMB/CIFS implementation.
3 * Support for OneFS kernel oplocks
5 * Copyright (C) Volker Lendecke 2007
6 * Copyright (C) Tim Prouty, 2009
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.
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.
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/>.
22 #define DBGC_CLASS DBGC_LOCKING
27 #include "oplock_onefs.h"
28 #include "smbd/smbd.h"
29 #include "smbd/globals.h"
31 #include <ifs/ifs_syscalls.h>
32 #include <isi_ecs/isi_ecs_oplocks.h>
35 struct onefs_oplocks_context {
36 struct kernel_oplocks *ctx;
37 struct smbd_server_connection *sconn;
38 const struct oplocks_event_ops *onefs_ops;
40 struct fd_event *read_fde;
43 enum onefs_callback_state {
45 ONEFS_WAITING_FOR_OPLOCK
48 struct onefs_callback_record {
49 struct onefs_callback_record *prev, *next;
51 enum onefs_callback_state state;
53 files_struct *fsp; /* ONEFS_OPEN_FILE */
54 uint64_t mid; /* ONEFS_WAITING_FOR_OPLOCK */
59 * Internal list of files (along with additional state) that have outstanding
60 * oplocks or requests for oplocks.
62 struct onefs_callback_record *callback_recs;
65 * Convert a onefs_callback_record to a debug string using the dbg_ctx().
67 const char *onefs_cb_record_str_dbg(const struct onefs_callback_record *r)
72 result = talloc_strdup(talloc_tos(), "NULL callback record");
78 result = talloc_asprintf(talloc_tos(), "cb record %llu for "
80 fsp_str_dbg(r->data.fsp));
81 case ONEFS_WAITING_FOR_OPLOCK:
82 result = talloc_asprintf(talloc_tos(), "cb record %llu for "
83 "pending mid %llu", r->id,
84 (unsigned long long)r->data.mid);
87 result = talloc_asprintf(talloc_tos(), "cb record %llu unknown "
88 "state %d", r->id, r->state);
96 * Traverse the list of onefs_callback_records and print all entries.
98 static void debug_cb_records(const char *fn)
100 struct onefs_callback_record *rec;
105 DEBUG(10, ("cb records (%s):\n", fn));
107 for (rec = callback_recs; rec; rec = rec->next) {
108 DEBUGADD(10, ("%s\n", onefs_cb_record_str_dbg(rec)));
113 * Find a callback record in the list of outstanding oplock operations.
115 * Once n ifs_createfile requests an oplock on a file, the kernel communicates
116 * with samba via the oplock event channel by sending events that reference an
117 * id. This function maps that id to the onefs_callback_record that was
118 * created for it during the initial setup on open (onefs_oplock_wait_record).
119 * When a matching id is found in the onefs_callback_record list, the
120 * callback_type is checked to make sure the record is in in the correct
123 static struct onefs_callback_record *onefs_find_cb(uint64_t id,
124 enum onefs_callback_state expected_state)
126 struct onefs_callback_record *rec;
128 debug_cb_records("onefs_find_cb");
130 for (rec = callback_recs; rec; rec = rec->next) {
132 DEBUG(10, ("found %s\n",
133 onefs_cb_record_str_dbg(rec)));
139 DEBUG(5, ("Could not find callback record for id %llu\n", id));
143 if (rec->state != expected_state) {
144 DEBUG(0, ("Expected cb type %d, got %s", expected_state,
145 onefs_cb_record_str_dbg(rec)));
154 * Remove and free a callback record from the callback record list.
156 void destroy_onefs_callback_record(uint64_t id)
158 struct onefs_callback_record *rec;
160 debug_cb_records("destroy_onefs_callback_record");
163 DEBUG(10, ("destroy_onefs_callback_record: Nothing to "
168 for (rec = callback_recs; rec; rec = rec->next) {
170 DLIST_REMOVE(callback_recs, rec);
172 DEBUG(10, ("removed cb rec %llu\n", id));
177 DEBUG(0, ("Could not find cb rec %llu to delete", id));
182 * Initialize a callback record and add it to the list of outstanding callback
185 * This is called in the open path before ifs_createfile so an id can be
186 * passed in. Each callback record can be in one of two states:
188 * 1. WAITING_FOR_OPLOCK: This is the initial state for all callback
189 * records. If ifs_createfile can be completed syncronously without needing
190 * to break any level I oplocks, the state is transitioned to OPEN_FILE.
191 * Otherwise ifs_createfile will finish asynchronously and the open is
192 * deferred. When the necessary level I opocks have been broken, and the
193 * open can be done, an event is sent by the kernel on the oplock event
194 * channel, which is handled by semlock_available_handler. At this point
195 * the deferred open is retried. Unless a level I oplock was acquired by
196 * another client, ifs_createfile will now complete synchronously.
198 * 2. OPEN_FILE: Once ifs_createfile completes, the callback record is
199 * transitioned to this state via onefs_set_oplock_callback.
201 uint64_t onefs_oplock_wait_record(uint64_t mid)
203 struct onefs_callback_record *result;
204 static uint64_t id_generator = 0;
206 if (!(result = SMB_MALLOC_P(struct onefs_callback_record))) {
207 DEBUG(0, ("talloc failed\n"));
211 memset(result, '\0', sizeof(result));
214 if (id_generator == 0) {
215 /* Wow, that's a long-running smbd... */
219 result->id = id_generator;
221 result->state = ONEFS_WAITING_FOR_OPLOCK;
222 result->data.mid = mid;
223 DLIST_ADD(callback_recs, result);
225 DEBUG(10, ("New cb rec %llu created\n", result->id));
231 * Transition the callback record state to OPEN_FILE.
233 * This is called after the file is opened and an fsp struct has been
234 * allocated. The mid is dropped in favor of storing the fsp.
236 void onefs_set_oplock_callback(uint64_t id, files_struct *fsp)
238 struct onefs_callback_record *cb;
241 DEBUG(10, ("onefs_set_oplock_callback called for cb rec %llu\n", id));
243 if (!(cb = onefs_find_cb(id, ONEFS_WAITING_FOR_OPLOCK))) {
244 if (asprintf(&msg, "Got invalid callback %lld\n", id) != -1) {
247 smb_panic("Got invalid callback id\n");
253 if (open_was_deferred(cb->data.mid)) {
254 if (asprintf(&msg, "Trying to upgrade callback for deferred "
255 "open mid=%llu\n", (unsigned long long)cb->data.mid) != -1) {
258 smb_panic("Trying to upgrade callback for deferred open "
262 cb->state = ONEFS_OPEN_FILE;
267 * Using a callback record, initialize a share mode entry to pass to
268 * share_mode_entry_to_message to send samba IPC messages.
270 static void init_share_mode_entry(struct share_mode_entry *sme,
271 struct onefs_callback_record *cb,
276 sme->pid = procid_self();
277 sme->op_type = op_type;
278 sme->id = cb->data.fsp->file_id;
279 sme->share_file_id = cb->data.fsp->fh->gen_id;
283 * Callback when a break-to-none event is received from the kernel.
285 * On OneFS level 1 oplocks are always broken to level 2 first, therefore an
286 * async level 2 break message is always sent when breaking to none. The
287 * downside of this is that OneFS currently has no way to express breaking
288 * directly from level 1 to none.
290 static void oplock_break_to_none_handler(uint64_t id)
292 struct onefs_callback_record *cb;
293 struct share_mode_entry sme;
294 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
296 DEBUG(10, ("oplock_break_to_none_handler called for id %llu\n", id));
298 if (!(cb = onefs_find_cb(id, ONEFS_OPEN_FILE))) {
299 DEBUG(3, ("oplock_break_to_none_handler: could not find "
300 "callback id %llu\n", id));
304 DEBUG(10, ("oplock_break_to_none_handler called for file %s\n",
305 fsp_str_dbg(cb->data.fsp)));
307 init_share_mode_entry(&sme, cb, FORCE_OPLOCK_BREAK_TO_NONE);
308 share_mode_entry_to_message(msg, &sme);
309 messaging_send_buf(smbd_messaging_context(),
311 MSG_SMB_ASYNC_LEVEL2_BREAK,
313 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
316 * We could still receive an OPLOCK_REVOKED message, so keep the
317 * oplock_callback_id around.
322 * Callback when a break-to-level2 event is received from the kernel.
324 * Breaks from level 1 to level 2.
326 static void oplock_break_to_level_two_handler(uint64_t id)
328 struct onefs_callback_record *cb;
329 struct share_mode_entry sme;
330 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
332 DEBUG(10, ("oplock_break_to_level_two_handler called for id %llu\n",
335 if (!(cb = onefs_find_cb(id, ONEFS_OPEN_FILE))) {
336 DEBUG(3, ("oplock_break_to_level_two_handler: could not find "
337 "callback id %llu\n", id));
341 DEBUG(10, ("oplock_break_to_level_two_handler called for file %s\n",
342 fsp_str_dbg(cb->data.fsp)));
344 init_share_mode_entry(&sme, cb, LEVEL_II_OPLOCK);
345 share_mode_entry_to_message(msg, &sme);
346 messaging_send_buf(smbd_messaging_context(),
348 MSG_SMB_BREAK_REQUEST,
350 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
353 * We could still receive an OPLOCK_REVOKED or OPLOCK_BREAK_TO_NONE
354 * message, so keep the oplock_callback_id around.
359 * Revoke an oplock from an unresponsive client.
361 * The kernel will send this message when it times out waiting for a level 1
362 * oplock break to be acknowledged by the client. The oplock is then
363 * immediately removed.
365 static void oplock_revoked_handler(uint64_t id)
367 struct onefs_callback_record *cb;
368 files_struct *fsp = NULL;
370 DEBUG(10, ("oplock_revoked_handler called for id %llu\n", id));
372 if (!(cb = onefs_find_cb(id, ONEFS_OPEN_FILE))) {
373 DEBUG(3, ("oplock_revoked_handler: could not find "
374 "callback id %llu\n", id));
380 SMB_ASSERT(fsp->oplock_timeout == NULL);
382 DEBUG(0,("Level 1 oplock break failed for file %s. Forcefully "
383 "revoking oplock\n", fsp_str_dbg(fsp)));
388 * cb record is cleaned up in fsp ext data destructor on close, so
389 * leave it in the list.
394 * Asynchronous ifs_createfile callback
396 * If ifs_createfile had to asynchronously break any oplocks, this function is
397 * called when the kernel sends an event that the open can be retried.
399 static void semlock_available_handler(uint64_t id)
401 struct onefs_callback_record *cb;
403 DEBUG(10, ("semlock_available_handler called: %llu\n", id));
405 if (!(cb = onefs_find_cb(id, ONEFS_WAITING_FOR_OPLOCK))) {
406 DEBUG(5, ("semlock_available_handler: Did not find callback "
411 DEBUG(10, ("Got semlock available for mid %llu\n",
412 (unsigned long long)cb->data.mid));
415 if (!(open_was_deferred(cb->data.mid))) {
417 if (asprintf(&msg, "Semlock available on an open that wasn't "
419 onefs_cb_record_str_dbg(cb)) != -1) {
422 smb_panic("Semlock available on an open that wasn't "
426 schedule_deferred_open_smb_message(cb->data.mid);
428 /* Cleanup the callback record since the open will be retried. */
429 destroy_onefs_callback_record(id);
435 * Asynchronous ifs_createfile failure callback
437 * If ifs_createfile had to asynchronously break any oplocks, but an error was
438 * encountered in the kernel, the open will be retried with the state->failed
439 * set to true. This will prompt the open path to send an INTERNAL_ERROR
440 * error message to the client.
442 static void semlock_async_failure_handler(uint64_t id)
444 struct onefs_callback_record *cb;
445 struct deferred_open_record *state;
447 DEBUG(1, ("semlock_async_failure_handler called: %llu\n", id));
449 if (!(cb = onefs_find_cb(id, ONEFS_WAITING_FOR_OPLOCK))) {
450 DEBUG(5, ("semlock_async_failure_handler: Did not find callback "
455 DEBUG(1, ("Got semlock_async_failure message for mid %llu\n",
456 (unsigned long long)cb->data.mid));
459 if (!(open_was_deferred(cb->data.mid))) {
461 if (asprintf(&msg, "Semlock failure on an open that wasn't "
463 onefs_cb_record_str_dbg(cb)) != -1) {
466 smb_panic("Semlock failure on an open that wasn't deferred\n");
469 /* Find the actual deferred open record. */
470 if (!get_open_deferred_message_state(cb->data.mid, NULL, &state)) {
471 DEBUG(0, ("Could not find deferred request for "
472 "mid %d\n", cb->data.mid));
473 destroy_onefs_callback_record(id);
477 /* Update to failed so the client can be notified on retried open. */
478 state->failed = true;
480 /* Schedule deferred open for immediate retry. */
481 schedule_deferred_open_smb_message(cb->data.mid);
483 /* Cleanup the callback record here since the open will be retried. */
484 destroy_onefs_callback_record(id);
490 * OneFS acquires all oplocks via ifs_createfile, so this is a no-op.
492 static bool onefs_set_kernel_oplock(struct kernel_oplocks *_ctx,
493 files_struct *fsp, int oplock_type) {
498 * Release the kernel oplock.
500 static void onefs_release_kernel_oplock(struct kernel_oplocks *_ctx,
501 files_struct *fsp, int oplock_type)
503 enum oplock_type oplock = onefs_samba_oplock_to_oplock(oplock_type);
505 DEBUG(10, ("onefs_release_kernel_oplock: Releasing %s to type %s\n",
506 fsp_str_dbg(fsp), onefs_oplock_str(oplock)));
508 if (fsp->fh->fd == -1) {
509 DEBUG(1, ("no fd\n"));
513 /* Downgrade oplock to either SHARED or NONE. */
514 if (ifs_oplock_downgrade(fsp->fh->fd, oplock)) {
515 DEBUG(1,("ifs_oplock_downgrade failed: %s\n",
521 * Wrap ifs_semlock_write so it is only called on operations that aren't
522 * already contended in the kernel.
524 static void onefs_semlock_write(int fd, enum level2_contention_type type,
525 enum semlock_operation semlock_op)
530 case LEVEL2_CONTEND_ALLOC_GROW:
531 case LEVEL2_CONTEND_POSIX_BRL:
532 DEBUG(10, ("Taking %d write semlock for cmd %d on fd: %d\n",
533 semlock_op, type, fd));
534 ret = ifs_semlock_write(fd, semlock_op);
536 DEBUG(0,("ifs_semlock_write failed taking %d write "
537 "semlock for cmd %d on fd: %d: %s",
538 semlock_op, type, fd, strerror(errno)));
542 DEBUG(10, ("Skipping write semlock for cmd %d on fd: %d\n",
548 * Contend level 2 oplocks in the kernel and smbd.
550 * Taking a write semlock will contend all level 2 oplocks in all smbds across
551 * the cluster except the fsp's own level 2 oplock. This lack of
552 * self-contention is a limitation of the current OneFS kernel oplocks
553 * implementation. Luckily it is easy to contend our own level 2 oplock by
554 * checking the the fsp's oplock_type. If it's a level2, send a break message
555 * to the client and remove the oplock.
557 static void onefs_contend_level2_oplocks_begin(files_struct *fsp,
558 enum level2_contention_type type)
560 /* Take care of level 2 kernel contention. */
561 onefs_semlock_write(fsp->fh->fd, type, SEMLOCK_LOCK);
563 /* Take care of level 2 self contention. */
564 if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
565 break_level2_to_none_async(fsp);
569 * Unlock the write semlock when the level 2 contending operation ends.
571 static void onefs_contend_level2_oplocks_end(files_struct *fsp,
572 enum level2_contention_type type)
574 /* Take care of level 2 kernel contention. */
575 onefs_semlock_write(fsp->fh->fd, type, SEMLOCK_UNLOCK);
579 * Return string value of onefs oplock types.
581 const char *onefs_oplock_str(enum oplock_type onefs_oplock_type)
583 switch (onefs_oplock_type) {
585 return "OPLOCK_NONE";
586 case OPLOCK_EXCLUSIVE:
587 return "OPLOCK_EXCLUSIVE";
589 return "OPLOCK_BATCH";
591 return "OPLOCK_SHARED";
599 * Convert from onefs to samba oplock.
601 int onefs_oplock_to_samba_oplock(enum oplock_type onefs_oplock)
603 switch (onefs_oplock) {
606 case OPLOCK_EXCLUSIVE:
607 return EXCLUSIVE_OPLOCK;
611 return LEVEL_II_OPLOCK;
613 DEBUG(0, ("unknown oplock type %d found\n", onefs_oplock));
620 * Convert from samba to onefs oplock.
622 enum oplock_type onefs_samba_oplock_to_oplock(int samba_oplock_type)
624 if (BATCH_OPLOCK_TYPE(samba_oplock_type)) return OPLOCK_BATCH;
625 if (EXCLUSIVE_OPLOCK_TYPE(samba_oplock_type)) return OPLOCK_EXCLUSIVE;
626 if (LEVEL_II_OPLOCK_TYPE(samba_oplock_type)) return OPLOCK_SHARED;
631 * Oplock event handler.
633 * Call into the event system dispatcher to handle each event.
635 static void onefs_oplocks_read_fde_handler(struct event_context *ev,
636 struct fd_event *fde,
640 struct onefs_oplocks_context *ctx =
641 talloc_get_type(private_data, struct onefs_oplocks_context);
643 if (oplocks_event_dispatcher(ctx->onefs_ops)) {
644 DEBUG(0, ("oplocks_event_dispatcher failed: %s\n",
650 * Setup kernel oplocks
652 static const struct kernel_oplocks_ops onefs_koplocks_ops = {
653 .set_oplock = onefs_set_kernel_oplock,
654 .release_oplock = onefs_release_kernel_oplock,
655 .contend_level2_oplocks_begin = onefs_contend_level2_oplocks_begin,
656 .contend_level2_oplocks_end = onefs_contend_level2_oplocks_end,
659 static const struct oplocks_event_ops onefs_dispatch_ops = {
660 .oplock_break_to_none = oplock_break_to_none_handler,
661 .oplock_break_to_level_two = oplock_break_to_level_two_handler,
662 .oplock_revoked = oplock_revoked_handler,
663 .semlock_available = semlock_available_handler,
664 .semlock_async_failure = semlock_async_failure_handler,
667 struct kernel_oplocks *onefs_init_kernel_oplocks(struct smbd_server_connection *sconn)
669 struct kernel_oplocks *_ctx = NULL;
670 struct onefs_oplocks_context *ctx = NULL;
671 struct procoptions po = PROCOPTIONS_INIT;
673 DEBUG(10, ("onefs_init_kernel_oplocks called\n"));
675 /* Set the non-blocking proc flag */
676 po.po_flags_on |= P_NON_BLOCKING_SEMLOCK;
677 if (setprocoptions(&po) != 0) {
678 DEBUG(0, ("setprocoptions failed: %s.\n", strerror(errno)));
682 /* Setup the oplock contexts */
683 _ctx = talloc_zero(mem_ctx, struct kernel_oplocks);
688 ctx = talloc_zero(_ctx, struct onefs_oplocks_context);
694 _ctx->ops = &onefs_koplocks_ops;
695 _ctx->flags = (KOPLOCKS_LEVEL2_SUPPORTED |
696 KOPLOCKS_DEFERRED_OPEN_NOTIFICATION |
697 KOPLOCKS_TIMEOUT_NOTIFICATION |
698 KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION);
699 _ctx->private_data = ctx;
701 ctx->onefs_ops = &onefs_dispatch_ops;
703 /* Register an kernel event channel for oplocks */
704 ctx->onefs_event_fd = oplocks_event_register();
705 if (ctx->onefs_event_fd == -1) {
706 DEBUG(0, ("oplocks_event_register failed: %s\n",
711 DEBUG(10, ("oplock event_fd = %d\n", ctx->onefs_event_fd));
713 /* Register the oplock event_fd with samba's event system */
714 ctx->read_fde = event_add_fd(sconn->ev_ctx,
718 onefs_oplocks_read_fde_handler,
728 void oplock_onefs_dummy(void);
729 void oplock_onefs_dummy(void) {}
730 #endif /* HAVE_ONEFS */