2 * Unix SMB/CIFS implementation.
3 * Support for OneFS system interfaces.
5 * Copyright (C) Zack Kirsch, 2009
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include <ifs/ifs_syscalls.h>
25 #include <sys/isi_cifs_brl.h>
26 #include <isi_ecs/isi_ecs_cbrl.h>
29 #define DBGC_CLASS DBGC_LOCKING
31 static uint64_t onefs_get_new_id(void) {
32 static uint64_t id = 0;
39 enum onefs_cbrl_lock_state {ONEFS_CBRL_NONE, ONEFS_CBRL_ASYNC, ONEFS_CBRL_DONE,
42 struct onefs_cbrl_blr_state {
44 enum onefs_cbrl_lock_state state;
47 static char *onefs_cbrl_blr_state_str(const struct blocking_lock_record *blr)
49 static fstring result;
50 struct onefs_cbrl_blr_state *bs;
54 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
57 fstrcpy(result, "NULL CBRL BLR state - Posix lock?");
63 fstr_sprintf(result, "CBRL BLR id=%llu: state=NONE", bs->id);
65 case ONEFS_CBRL_ASYNC:
66 fstr_sprintf(result, "CBRL BLR id=%llu: state=ASYNC", bs->id);
69 fstr_sprintf(result, "CBRL BLR id=%llu: state=DONE", bs->id);
71 case ONEFS_CBRL_ERROR:
72 fstr_sprintf(result, "CBRL BLR id=%llu: state=ERROR", bs->id);
75 fstr_sprintf(result, "CBRL BLR id=%llu: unknown state %d",
83 static void onefs_cbrl_enumerate_blq(const char *fn)
85 struct smbd_server_connection *sconn = smbd_server_conn;
86 struct blocking_lock_record *blr;
91 DEBUG(10, ("CBRL BLR records (%s):\n", fn));
93 if (sconn->using_smb2) {
94 struct smbd_smb2_request *smb2req;
95 for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
96 blr = get_pending_smb2req_blr(smb2req);
98 DEBUGADD(10, ("%s\n", onefs_cbrl_blr_state_str(blr)));
102 for (blr = sconn->smb1.locks.blocking_lock_queue; blr; blr = blr->next)
103 DEBUGADD(10, ("%s\n", onefs_cbrl_blr_state_str(blr)));
107 static struct blocking_lock_record *onefs_cbrl_find_blr(uint64_t id)
109 struct smbd_server_connection *sconn = smbd_server_conn;
110 struct blocking_lock_record *blr;
111 struct onefs_cbrl_blr_state *bs;
113 onefs_cbrl_enumerate_blq("onefs_cbrl_find_blr");
115 if (sconn->using_smb2) {
116 struct smbd_smb2_request *smb2req;
117 for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
118 blr = get_pending_smb2req_blr(smb2req);
122 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
127 DEBUG(10, ("found %s\n",
128 onefs_cbrl_blr_state_str(blr)));
133 for (blr = sconn->smb1.locks.blocking_lock_queue; blr; blr = blr->next) {
134 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
136 /* We don't control all of the BLRs on the BLQ. */
141 DEBUG(10, ("found %s\n",
142 onefs_cbrl_blr_state_str(blr)));
149 DEBUG(5, ("Could not find CBRL BLR for id %llu\n", id));
156 static void onefs_cbrl_async_success(uint64_t id)
158 struct blocking_lock_record *blr;
159 struct onefs_cbrl_blr_state *bs;
162 DEBUG(10, ("CBRL async success!\n"));
164 /* Find BLR with id. Its okay not to find one (race with cancel) */
165 blr = onefs_cbrl_find_blr(id);
169 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
171 SMB_ASSERT(bs->state == ONEFS_CBRL_ASYNC);
175 num_locks = SVAL(blr->req->vwv+7, 0);
177 if (blr->lock_num == num_locks)
178 bs->state = ONEFS_CBRL_DONE;
180 bs->state = ONEFS_CBRL_NONE;
182 /* Self contend our own level 2 oplock. The kernel handles
183 * contention of other opener's level 2 oplocks. */
184 contend_level2_oplocks_begin(blr->fsp,
185 LEVEL2_CONTEND_WINDOWS_BRL);
187 /* Process the queue, to try the next lock or finish up. */
188 process_blocking_lock_queue(smbd_server_conn);
191 static void onefs_cbrl_async_failure(uint64_t id)
193 struct blocking_lock_record *blr;
194 struct onefs_cbrl_blr_state *bs;
196 DEBUG(10, ("CBRL async failure!\n"));
198 /* Find BLR with id. Its okay not to find one (race with cancel) */
199 blr = onefs_cbrl_find_blr(id);
203 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
206 SMB_ASSERT(bs->state == ONEFS_CBRL_ASYNC);
207 bs->state = ONEFS_CBRL_ERROR;
209 /* Process the queue. It will end up trying to retake the same lock,
210 * see the error in onefs_cbrl_lock_windows() and fail. */
211 process_blocking_lock_queue(smbd_server_conn);
214 static struct cbrl_event_ops cbrl_ops =
215 {.cbrl_async_success = onefs_cbrl_async_success,
216 .cbrl_async_failure = onefs_cbrl_async_failure};
218 static void onefs_cbrl_events_handler(struct event_context *ev,
219 struct fd_event *fde,
223 DEBUG(10, ("onefs_cbrl_events_handler\n"));
225 if (cbrl_event_dispatcher(&cbrl_ops)) {
226 DEBUG(0, ("cbrl_event_dispatcher failed: %s\n",
231 static void onefs_init_cbrl(void)
233 static bool init_done = false;
234 static int cbrl_event_fd;
235 static struct fd_event *cbrl_fde;
240 DEBUG(10, ("onefs_init_cbrl\n"));
242 /* Register the event channel for CBRL. */
243 cbrl_event_fd = cbrl_event_register();
244 if (cbrl_event_fd == -1) {
245 DEBUG(0, ("cbrl_event_register failed: %s\n",
250 DEBUG(10, ("cbrl_event_fd = %d\n", cbrl_event_fd));
252 /* Register the CBRL event_fd with samba's event system */
253 cbrl_fde = event_add_fd(smbd_event_context(),
257 onefs_cbrl_events_handler,
265 * Blocking PID. As far as I can tell, the blocking_pid is only used to tell
266 * whether a posix lock or a CIFS lock blocked us. If it was a posix lock,
267 * Samba polls every 10 seconds, which we don't want. -zkirsch
269 #define ONEFS_BLOCKING_PID 0xABCDABCD
272 * @param[in] br_lck Contains the fsp.
273 * @param[in] plock Lock request.
274 * @param[in] blocking_lock Only used for figuring out the error.
275 * @param[in,out] blr The BLR for the already-deferred operation.
277 NTSTATUS onefs_brl_lock_windows(vfs_handle_struct *handle,
278 struct byte_range_lock *br_lck,
279 struct lock_struct *plock,
281 struct blocking_lock_record *blr)
283 int fd = br_lck->fsp->fh->fd;
285 enum cbrl_lock_type type;
287 bool pending = false;
288 bool pending_async = false;
290 struct onefs_cbrl_blr_state *bs;
293 START_PROFILE(syscall_brl_lock);
295 SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
296 SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
298 onefs_cbrl_enumerate_blq("onefs_brl_lock_windows");
300 /* Will only initialize the first time its called. */
303 switch (plock->lock_type) {
310 case PENDING_WRITE_LOCK:
311 /* Called when a blocking lock request is added - do an
317 case PENDING_READ_LOCK:
318 /* Called when a blocking lock request is added - do an
325 /* UNLOCK_LOCK: should only be used for a POSIX_LOCK */
326 smb_panic("Invalid plock->lock_type passed in to "
327 "onefs_brl_lock_windows");
330 /* Figure out if we're actually doing the lock or a no-op. We need to
331 * do a no-op when process_blocking_lock_queue calls back into us.
333 * We know process_* is calling into us if a blr is passed in and
334 * pending is false. */
335 if (!pending && blr) {
336 /* Check the BLR state. */
337 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
340 /* ASYNC still in progress: The process_* calls will keep
341 * calling even if we haven't gotten the lock. Keep erroring
342 * without calling ifs_cbrl, or getting/setting an id. */
343 if (bs->state == ONEFS_CBRL_ASYNC) {
346 else if (bs->state == ONEFS_CBRL_ERROR) {
347 END_PROFILE(syscall_brl_lock);
348 return NT_STATUS_NO_MEMORY;
351 SMB_ASSERT(bs->state == ONEFS_CBRL_NONE);
356 SMB_ASSERT(blocking_lock);
358 id = onefs_get_new_id();
361 DEBUG(10, ("Calling ifs_cbrl(LOCK)...\n"));
362 error = ifs_cbrl(fd, CBRL_OP_LOCK, type, plock->start,
363 plock->size, async, id, plock->context.smbpid, plock->context.tid,
367 } else if (errno == EWOULDBLOCK) {
369 } else if (errno == EINPROGRESS) {
373 /* Talloc a new BLR private state. */
374 blr->blr_private = talloc(blr, struct onefs_cbrl_blr_state);
375 pending_async = true;
378 /* Store the new id in the BLR private state. */
379 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
381 bs->state = ONEFS_CBRL_ASYNC;
383 DEBUG(0, ("onefs_brl_lock_windows failure: error=%d (%s).\n",
384 errno, strerror(errno)));
389 END_PROFILE(syscall_brl_lock);
391 /* Failure - error or async. */
392 plock->context.smbpid = (uint32) ONEFS_BLOCKING_PID;
395 status = NT_STATUS_OK;
397 status = brl_lock_failed(br_lck->fsp, plock, blocking_lock);
399 DEBUG(10, ("returning %s.\n", nt_errstr(status)));
403 /* Self contend our own level 2 oplock. The kernel handles
404 * contention of other opener's level 2 oplocks. */
405 contend_level2_oplocks_begin(br_lck->fsp,
406 LEVEL2_CONTEND_WINDOWS_BRL);
408 END_PROFILE(syscall_brl_lock);
411 onefs_cbrl_enumerate_blq("onefs_brl_unlock_windows");
412 DEBUG(10, ("returning NT_STATUS_OK.\n"));
416 bool onefs_brl_unlock_windows(vfs_handle_struct *handle,
417 struct messaging_context *msg_ctx,
418 struct byte_range_lock *br_lck,
419 const struct lock_struct *plock)
422 int fd = br_lck->fsp->fh->fd;
424 START_PROFILE(syscall_brl_unlock);
426 SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
427 SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
429 DEBUG(10, ("Calling ifs_cbrl(UNLOCK)...\n"));
430 error = ifs_cbrl(fd, CBRL_OP_UNLOCK, CBRL_LK_SH,
431 plock->start, plock->size, false, 0, plock->context.smbpid,
432 plock->context.tid, plock->fnum);
434 END_PROFILE(syscall_brl_unlock);
437 DEBUG(10, ("returning false.\n"));
441 /* For symmetry purposes, end our oplock contention even though its
442 * currently a no-op. */
443 contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
445 DEBUG(10, ("returning true.\n"));
448 /* Problem with storing things in TDB: I won't know what BRL to unlock in the TDB.
450 * - I could send Samba a message with which lock is being unlocked?
451 * - I could *easily* make the "id" something you always pass in to
452 * lock, unlock or cancel -- it identifies a lock. Makes sense!
456 /* Default implementation only calls this on PENDING locks. */
457 bool onefs_brl_cancel_windows(vfs_handle_struct *handle,
458 struct byte_range_lock *br_lck,
459 struct lock_struct *plock,
460 struct blocking_lock_record *blr)
463 int fd = br_lck->fsp->fh->fd;
464 struct onefs_cbrl_blr_state *bs;
466 START_PROFILE(syscall_brl_cancel);
469 SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
472 onefs_cbrl_enumerate_blq("onefs_brl_cancel_windows");
474 bs = ((struct onefs_cbrl_blr_state *)blr->blr_private);
477 if (bs->state == ONEFS_CBRL_DONE || bs->state == ONEFS_CBRL_ERROR) {
479 DEBUG(10, ("State=%d, returning true\n", bs->state));
480 END_PROFILE(syscall_brl_cancel);
484 SMB_ASSERT(bs->state == ONEFS_CBRL_NONE ||
485 bs->state == ONEFS_CBRL_ASYNC);
488 DEBUG(10, ("Calling ifs_cbrl(CANCEL)...\n"));
489 error = ifs_cbrl(fd, CBRL_OP_CANCEL, CBRL_LK_UNSPEC, plock->start,
490 plock->size, false, bs->id, plock->context.smbpid,
491 plock->context.tid, plock->fnum);
493 END_PROFILE(syscall_brl_cancel);
496 DEBUG(10, ("returning false\n"));
497 bs->state = ONEFS_CBRL_ERROR;
501 bs->state = ONEFS_CBRL_DONE;
502 onefs_cbrl_enumerate_blq("onefs_brl_cancel_windows");
503 DEBUG(10, ("returning true\n"));
507 bool onefs_strict_lock(vfs_handle_struct *handle,
509 struct lock_struct *plock)
513 START_PROFILE(syscall_strict_lock);
515 SMB_ASSERT(plock->lock_type == READ_LOCK ||
516 plock->lock_type == WRITE_LOCK);
518 if (!lp_locking(handle->conn->params) ||
519 !lp_strict_locking(handle->conn->params)) {
520 END_PROFILE(syscall_strict_lock);
524 if (plock->lock_flav == POSIX_LOCK) {
525 END_PROFILE(syscall_strict_lock);
526 return SMB_VFS_NEXT_STRICT_LOCK(handle, fsp, plock);
529 if (plock->size == 0) {
530 END_PROFILE(syscall_strict_lock);
534 error = ifs_cbrl(fsp->fh->fd, CBRL_OP_LOCK,
535 plock->lock_type == READ_LOCK ? CBRL_LK_RD : CBRL_LK_WR,
536 plock->start, plock->size, 0, 0, plock->context.smbpid,
537 plock->context.tid, plock->fnum);
539 END_PROFILE(syscall_strict_lock);
544 void onefs_strict_unlock(vfs_handle_struct *handle,
546 struct lock_struct *plock)
548 START_PROFILE(syscall_strict_unlock);
550 SMB_ASSERT(plock->lock_type == READ_LOCK ||
551 plock->lock_type == WRITE_LOCK);
553 if (!lp_locking(handle->conn->params) ||
554 !lp_strict_locking(handle->conn->params)) {
555 END_PROFILE(syscall_strict_unlock);
559 if (plock->lock_flav == POSIX_LOCK) {
560 SMB_VFS_NEXT_STRICT_UNLOCK(handle, fsp, plock);
561 END_PROFILE(syscall_strict_unlock);
565 if (plock->size == 0) {
566 END_PROFILE(syscall_strict_unlock);
571 ifs_cbrl(fsp->fh->fd, CBRL_OP_UNLOCK,
572 plock->lock_type == READ_LOCK ? CBRL_LK_RD : CBRL_LK_WR,
573 plock->start, plock->size, 0, 0, plock->context.smbpid,
574 plock->context.tid, plock->fnum);
577 END_PROFILE(syscall_strict_unlock);
580 /* TODO Optimization: Abstract out brl_get_locks() in the Windows case.
581 * We'll malloc some memory or whatever (can't return NULL), but not actually
584 /* XXX brl_locktest: CBRL does not support calling this, but its only for
585 * strict locking. Add empty VOP? */
587 /* XXX brl_lockquery: CBRL does not support calling this for WINDOWS LOCKS, but
588 * its only called for POSIX LOCKS. Add empty VOP? */
590 /* XXX brl_close_fnum: CBRL will do this automatically. I think this is a NO-OP
591 * for us, we could add an empty VOP. */