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
->allow_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
->allow_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
)));
132 for (blr
= sconn
->smb1
.locks
.blocking_lock_queue
; blr
; blr
= blr
->next
) {
133 bs
= (struct onefs_cbrl_blr_state
*)blr
->blr_private
;
135 /* We don't control all of the BLRs on the BLQ. */
140 DEBUG(10, ("found %s\n",
141 onefs_cbrl_blr_state_str(blr
)));
148 DEBUG(5, ("Could not find CBRL BLR for id %llu\n", id
));
155 static void onefs_cbrl_async_success(uint64_t id
)
157 struct blocking_lock_record
*blr
;
158 struct onefs_cbrl_blr_state
*bs
;
161 DEBUG(10, ("CBRL async success!\n"));
163 /* Find BLR with id. Its okay not to find one (race with cancel) */
164 blr
= onefs_cbrl_find_blr(id
);
168 bs
= (struct onefs_cbrl_blr_state
*)blr
->blr_private
;
170 SMB_ASSERT(bs
->state
== ONEFS_CBRL_ASYNC
);
174 num_locks
= SVAL(blr
->req
->vwv
+7, 0);
176 if (blr
->lock_num
== num_locks
)
177 bs
->state
= ONEFS_CBRL_DONE
;
179 bs
->state
= ONEFS_CBRL_NONE
;
181 /* Self contend our own level 2 oplock. The kernel handles
182 * contention of other opener's level 2 oplocks. */
183 contend_level2_oplocks_begin(blr
->fsp
,
184 LEVEL2_CONTEND_WINDOWS_BRL
);
186 /* Process the queue, to try the next lock or finish up. */
187 process_blocking_lock_queue();
190 static void onefs_cbrl_async_failure(uint64_t id
)
192 struct blocking_lock_record
*blr
;
193 struct onefs_cbrl_blr_state
*bs
;
195 DEBUG(10, ("CBRL async failure!\n"));
197 /* Find BLR with id. Its okay not to find one (race with cancel) */
198 blr
= onefs_cbrl_find_blr(id
);
202 bs
= (struct onefs_cbrl_blr_state
*)blr
->blr_private
;
205 SMB_ASSERT(bs
->state
== ONEFS_CBRL_ASYNC
);
206 bs
->state
= ONEFS_CBRL_ERROR
;
208 /* Process the queue. It will end up trying to retake the same lock,
209 * see the error in onefs_cbrl_lock_windows() and fail. */
210 process_blocking_lock_queue();
213 static struct cbrl_event_ops cbrl_ops
=
214 {.cbrl_async_success
= onefs_cbrl_async_success
,
215 .cbrl_async_failure
= onefs_cbrl_async_failure
};
217 static void onefs_cbrl_events_handler(struct event_context
*ev
,
218 struct fd_event
*fde
,
222 DEBUG(10, ("onefs_cbrl_events_handler\n"));
224 if (cbrl_event_dispatcher(&cbrl_ops
)) {
225 DEBUG(0, ("cbrl_event_dispatcher failed: %s\n",
230 static void onefs_init_cbrl(void)
232 static bool init_done
= false;
233 static int cbrl_event_fd
;
234 static struct fd_event
*cbrl_fde
;
239 DEBUG(10, ("onefs_init_cbrl\n"));
241 /* Register the event channel for CBRL. */
242 cbrl_event_fd
= cbrl_event_register();
243 if (cbrl_event_fd
== -1) {
244 DEBUG(0, ("cbrl_event_register failed: %s\n",
249 DEBUG(10, ("cbrl_event_fd = %d\n", cbrl_event_fd
));
251 /* Register the CBRL event_fd with samba's event system */
252 cbrl_fde
= event_add_fd(smbd_event_context(),
256 onefs_cbrl_events_handler
,
264 * Blocking PID. As far as I can tell, the blocking_pid is only used to tell
265 * whether a posix lock or a CIFS lock blocked us. If it was a posix lock,
266 * Samba polls every 10 seconds, which we don't want. -zkirsch
268 #define ONEFS_BLOCKING_PID 0xABCDABCD
271 * @param[in] br_lck Contains the fsp.
272 * @param[in] plock Lock request.
273 * @param[in] blocking_lock Only used for figuring out the error.
274 * @param[in,out] blr The BLR for the already-deferred operation.
276 NTSTATUS
onefs_brl_lock_windows(vfs_handle_struct
*handle
,
277 struct byte_range_lock
*br_lck
,
278 struct lock_struct
*plock
,
280 struct blocking_lock_record
*blr
)
282 int fd
= br_lck
->fsp
->fh
->fd
;
284 enum cbrl_lock_type type
;
286 bool pending
= false;
287 bool pending_async
= false;
289 struct onefs_cbrl_blr_state
*bs
;
292 START_PROFILE(syscall_brl_lock
);
294 SMB_ASSERT(plock
->lock_flav
== WINDOWS_LOCK
);
295 SMB_ASSERT(plock
->lock_type
!= UNLOCK_LOCK
);
297 onefs_cbrl_enumerate_blq("onefs_brl_lock_windows");
299 /* Will only initialize the first time its called. */
302 switch (plock
->lock_type
) {
309 case PENDING_WRITE_LOCK
:
310 /* Called when a blocking lock request is added - do an
316 case PENDING_READ_LOCK
:
317 /* Called when a blocking lock request is added - do an
324 /* UNLOCK_LOCK: should only be used for a POSIX_LOCK */
325 smb_panic("Invalid plock->lock_type passed in to "
326 "onefs_brl_lock_windows");
329 /* Figure out if we're actually doing the lock or a no-op. We need to
330 * do a no-op when process_blocking_lock_queue calls back into us.
332 * We know process_* is calling into us if a blr is passed in and
333 * pending is false. */
334 if (!pending
&& blr
) {
335 /* Check the BLR state. */
336 bs
= (struct onefs_cbrl_blr_state
*)blr
->blr_private
;
339 /* ASYNC still in progress: The process_* calls will keep
340 * calling even if we haven't gotten the lock. Keep erroring
341 * without calling ifs_cbrl, or getting/setting an id. */
342 if (bs
->state
== ONEFS_CBRL_ASYNC
) {
345 else if (bs
->state
== ONEFS_CBRL_ERROR
) {
346 END_PROFILE(syscall_brl_lock
);
347 return NT_STATUS_NO_MEMORY
;
350 SMB_ASSERT(bs
->state
== ONEFS_CBRL_NONE
);
355 SMB_ASSERT(blocking_lock
);
357 id
= onefs_get_new_id();
360 DEBUG(10, ("Calling ifs_cbrl(LOCK)...\n"));
361 error
= ifs_cbrl(fd
, CBRL_OP_LOCK
, type
, plock
->start
,
362 plock
->size
, async
, id
, plock
->context
.smbpid
, plock
->context
.tid
,
366 } else if (errno
== EWOULDBLOCK
) {
368 } else if (errno
== EINPROGRESS
) {
372 /* Talloc a new BLR private state. */
373 blr
->blr_private
= talloc(blr
, struct onefs_cbrl_blr_state
);
374 pending_async
= true;
377 /* Store the new id in the BLR private state. */
378 bs
= (struct onefs_cbrl_blr_state
*)blr
->blr_private
;
380 bs
->state
= ONEFS_CBRL_ASYNC
;
382 DEBUG(0, ("onefs_brl_lock_windows failure: error=%d (%s).\n",
383 errno
, strerror(errno
)));
388 END_PROFILE(syscall_brl_lock
);
390 /* Failure - error or async. */
391 plock
->context
.smbpid
= (uint32
) ONEFS_BLOCKING_PID
;
394 status
= NT_STATUS_OK
;
396 status
= brl_lock_failed(br_lck
->fsp
, plock
, blocking_lock
);
398 DEBUG(10, ("returning %s.\n", nt_errstr(status
)));
402 /* Self contend our own level 2 oplock. The kernel handles
403 * contention of other opener's level 2 oplocks. */
404 contend_level2_oplocks_begin(br_lck
->fsp
,
405 LEVEL2_CONTEND_WINDOWS_BRL
);
407 END_PROFILE(syscall_brl_lock
);
410 onefs_cbrl_enumerate_blq("onefs_brl_unlock_windows");
411 DEBUG(10, ("returning NT_STATUS_OK.\n"));
415 bool onefs_brl_unlock_windows(vfs_handle_struct
*handle
,
416 struct messaging_context
*msg_ctx
,
417 struct byte_range_lock
*br_lck
,
418 const struct lock_struct
*plock
)
421 int fd
= br_lck
->fsp
->fh
->fd
;
423 START_PROFILE(syscall_brl_unlock
);
425 SMB_ASSERT(plock
->lock_flav
== WINDOWS_LOCK
);
426 SMB_ASSERT(plock
->lock_type
== UNLOCK_LOCK
);
428 DEBUG(10, ("Calling ifs_cbrl(UNLOCK)...\n"));
429 error
= ifs_cbrl(fd
, CBRL_OP_UNLOCK
, CBRL_LK_SH
,
430 plock
->start
, plock
->size
, false, 0, plock
->context
.smbpid
,
431 plock
->context
.tid
, plock
->fnum
);
433 END_PROFILE(syscall_brl_unlock
);
436 DEBUG(10, ("returning false.\n"));
440 /* For symmetry purposes, end our oplock contention even though its
441 * currently a no-op. */
442 contend_level2_oplocks_end(br_lck
->fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
444 DEBUG(10, ("returning true.\n"));
447 /* Problem with storing things in TDB: I won't know what BRL to unlock in the TDB.
449 * - I could send Samba a message with which lock is being unlocked?
450 * - I could *easily* make the "id" something you always pass in to
451 * lock, unlock or cancel -- it identifies a lock. Makes sense!
455 /* Default implementation only calls this on PENDING locks. */
456 bool onefs_brl_cancel_windows(vfs_handle_struct
*handle
,
457 struct byte_range_lock
*br_lck
,
458 struct lock_struct
*plock
,
459 struct blocking_lock_record
*blr
)
462 int fd
= br_lck
->fsp
->fh
->fd
;
463 struct onefs_cbrl_blr_state
*bs
;
465 START_PROFILE(syscall_brl_cancel
);
468 SMB_ASSERT(plock
->lock_flav
== WINDOWS_LOCK
);
471 onefs_cbrl_enumerate_blq("onefs_brl_cancel_windows");
473 bs
= ((struct onefs_cbrl_blr_state
*)blr
->blr_private
);
476 if (bs
->state
== ONEFS_CBRL_DONE
|| bs
->state
== ONEFS_CBRL_ERROR
) {
478 DEBUG(10, ("State=%d, returning true\n", bs
->state
));
479 END_PROFILE(syscall_brl_cancel
);
483 SMB_ASSERT(bs
->state
== ONEFS_CBRL_NONE
||
484 bs
->state
== ONEFS_CBRL_ASYNC
);
487 DEBUG(10, ("Calling ifs_cbrl(CANCEL)...\n"));
488 error
= ifs_cbrl(fd
, CBRL_OP_CANCEL
, CBRL_LK_UNSPEC
, plock
->start
,
489 plock
->size
, false, bs
->id
, plock
->context
.smbpid
,
490 plock
->context
.tid
, plock
->fnum
);
492 END_PROFILE(syscall_brl_cancel
);
495 DEBUG(10, ("returning false\n"));
496 bs
->state
= ONEFS_CBRL_ERROR
;
500 bs
->state
= ONEFS_CBRL_DONE
;
501 onefs_cbrl_enumerate_blq("onefs_brl_cancel_windows");
502 DEBUG(10, ("returning true\n"));
506 bool onefs_strict_lock(vfs_handle_struct
*handle
,
508 struct lock_struct
*plock
)
512 START_PROFILE(syscall_strict_lock
);
514 SMB_ASSERT(plock
->lock_type
== READ_LOCK
||
515 plock
->lock_type
== WRITE_LOCK
);
517 if (!lp_locking(handle
->conn
->params
) ||
518 !lp_strict_locking(handle
->conn
->params
)) {
519 END_PROFILE(syscall_strict_lock
);
523 if (plock
->lock_flav
== POSIX_LOCK
) {
524 END_PROFILE(syscall_strict_lock
);
525 return SMB_VFS_NEXT_STRICT_LOCK(handle
, fsp
, plock
);
528 if (plock
->size
== 0) {
529 END_PROFILE(syscall_strict_lock
);
533 error
= ifs_cbrl(fsp
->fh
->fd
, CBRL_OP_LOCK
,
534 plock
->lock_type
== READ_LOCK
? CBRL_LK_RD
: CBRL_LK_WR
,
535 plock
->start
, plock
->size
, 0, 0, plock
->context
.smbpid
,
536 plock
->context
.tid
, plock
->fnum
);
538 END_PROFILE(syscall_strict_lock
);
543 void onefs_strict_unlock(vfs_handle_struct
*handle
,
545 struct lock_struct
*plock
)
547 START_PROFILE(syscall_strict_unlock
);
549 SMB_ASSERT(plock
->lock_type
== READ_LOCK
||
550 plock
->lock_type
== WRITE_LOCK
);
552 if (!lp_locking(handle
->conn
->params
) ||
553 !lp_strict_locking(handle
->conn
->params
)) {
554 END_PROFILE(syscall_strict_unlock
);
558 if (plock
->lock_flav
== POSIX_LOCK
) {
559 SMB_VFS_NEXT_STRICT_UNLOCK(handle
, fsp
, plock
);
560 END_PROFILE(syscall_strict_unlock
);
564 if (plock
->size
== 0) {
565 END_PROFILE(syscall_strict_unlock
);
570 ifs_cbrl(fsp
->fh
->fd
, CBRL_OP_UNLOCK
,
571 plock
->lock_type
== READ_LOCK
? CBRL_LK_RD
: CBRL_LK_WR
,
572 plock
->start
, plock
->size
, 0, 0, plock
->context
.smbpid
,
573 plock
->context
.tid
, plock
->fnum
);
576 END_PROFILE(syscall_strict_unlock
);
579 /* TODO Optimization: Abstract out brl_get_locks() in the Windows case.
580 * We'll malloc some memory or whatever (can't return NULL), but not actually
583 /* XXX brl_locktest: CBRL does not support calling this, but its only for
584 * strict locking. Add empty VOP? */
586 /* XXX brl_lockquery: CBRL does not support calling this for WINDOWS LOCKS, but
587 * its only called for POSIX LOCKS. Add empty VOP? */
589 /* XXX brl_close_fnum: CBRL will do this automatically. I think this is a NO-OP
590 * for us, we could add an empty VOP. */