1*8329232eSGordon Ross /*
2*8329232eSGordon Ross  * CDDL HEADER START
3*8329232eSGordon Ross  *
4*8329232eSGordon Ross  * The contents of this file are subject to the terms of the
5*8329232eSGordon Ross  * Common Development and Distribution License (the "License").
6*8329232eSGordon Ross  * You may not use this file except in compliance with the License.
7*8329232eSGordon Ross  *
8*8329232eSGordon Ross  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*8329232eSGordon Ross  * or http://www.opensolaris.org/os/licensing.
10*8329232eSGordon Ross  * See the License for the specific language governing permissions
11*8329232eSGordon Ross  * and limitations under the License.
12*8329232eSGordon Ross  *
13*8329232eSGordon Ross  * When distributing Covered Code, include this CDDL HEADER in each
14*8329232eSGordon Ross  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*8329232eSGordon Ross  * If applicable, add the following below this CDDL HEADER, with the
16*8329232eSGordon Ross  * fields enclosed by brackets "[]" replaced with your own identifying
17*8329232eSGordon Ross  * information: Portions Copyright [yyyy] [name of copyright owner]
18*8329232eSGordon Ross  *
19*8329232eSGordon Ross  * CDDL HEADER END
20*8329232eSGordon Ross  */
21*8329232eSGordon Ross /*
22*8329232eSGordon Ross  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*8329232eSGordon Ross  * Use is subject to license terms.
24*8329232eSGordon Ross  *
25*8329232eSGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26*8329232eSGordon Ross  */
27*8329232eSGordon Ross 
28*8329232eSGordon Ross /*
29*8329232eSGordon Ross  * Utility routines and top-level conflict detection code for NBMAND
30*8329232eSGordon Ross  * locks.
31*8329232eSGordon Ross  */
32*8329232eSGordon Ross 
33*8329232eSGordon Ross #include <sys/nbmlock.h>
34*8329232eSGordon Ross #include <sys/rwlock.h>
35*8329232eSGordon Ross #include <sys/vnode.h>
36*8329232eSGordon Ross #include <sys/cmn_err.h>
37*8329232eSGordon Ross #include <sys/debug.h>
38*8329232eSGordon Ross #include <sys/types.h>
39*8329232eSGordon Ross #include <sys/fcntl.h>
40*8329232eSGordon Ross #include <sys/vfs.h>
41*8329232eSGordon Ross 
42*8329232eSGordon Ross /*
43*8329232eSGordon Ross  * Enter the critical region for synchronizing I/O requests with lock/share
44*8329232eSGordon Ross  * requests.  "mode" specifies whether the caller intends to update
45*8329232eSGordon Ross  * lock/share state (as opposed to just query it).
46*8329232eSGordon Ross  */
47*8329232eSGordon Ross 
48*8329232eSGordon Ross void
nbl_start_crit(vnode_t * vp,krw_t mode)49*8329232eSGordon Ross nbl_start_crit(vnode_t *vp, krw_t mode)
50*8329232eSGordon Ross {
51*8329232eSGordon Ross 	rw_enter(&vp->v_nbllock, mode);
52*8329232eSGordon Ross }
53*8329232eSGordon Ross 
54*8329232eSGordon Ross /*
55*8329232eSGordon Ross  * Leave the critical region.
56*8329232eSGordon Ross  */
57*8329232eSGordon Ross 
58*8329232eSGordon Ross void
nbl_end_crit(vnode_t * vp)59*8329232eSGordon Ross nbl_end_crit(vnode_t *vp)
60*8329232eSGordon Ross {
61*8329232eSGordon Ross 	rw_exit(&vp->v_nbllock);
62*8329232eSGordon Ross }
63*8329232eSGordon Ross 
64*8329232eSGordon Ross /*
65*8329232eSGordon Ross  * Return non-zero if some thread is in the critical region.
66*8329232eSGordon Ross  * Note that this is appropriate for use in ASSERT()s only.
67*8329232eSGordon Ross  */
68*8329232eSGordon Ross 
69*8329232eSGordon Ross int
nbl_in_crit(vnode_t * vp)70*8329232eSGordon Ross nbl_in_crit(vnode_t *vp)
71*8329232eSGordon Ross {
72*8329232eSGordon Ross 	return (RW_LOCK_HELD(&vp->v_nbllock));
73*8329232eSGordon Ross }
74*8329232eSGordon Ross 
75*8329232eSGordon Ross /*
76*8329232eSGordon Ross  * Returns non-zero if we need to look further for an NBMAND lock or
77*8329232eSGordon Ross  * share conflict.
78*8329232eSGordon Ross  */
79*8329232eSGordon Ross int
nbl_need_check(vnode_t * vp)80*8329232eSGordon Ross nbl_need_check(vnode_t *vp)
81*8329232eSGordon Ross {
82*8329232eSGordon Ross 	/*
83*8329232eSGordon Ross 	 * Currently we only check if NBMAND locks/shares are allowed on
84*8329232eSGordon Ross 	 * the filesystem.  An option for the future would be to have a
85*8329232eSGordon Ross 	 * flag on the vnode, though the locking for that can get tricky.
86*8329232eSGordon Ross 	 */
87*8329232eSGordon Ross 	return ((vp->v_vfsp) && (vp->v_vfsp->vfs_flag & VFS_NBMAND));
88*8329232eSGordon Ross }
89*8329232eSGordon Ross 
90*8329232eSGordon Ross /* No locks, so no conflicts. */
91*8329232eSGordon Ross int
nbl_conflict(vnode_t * vp,nbl_op_t op,u_offset_t offset,ssize_t length,int svmand,caller_context_t * ct)92*8329232eSGordon Ross nbl_conflict(vnode_t *vp,
93*8329232eSGordon Ross 	nbl_op_t op,		/* attempted operation */
94*8329232eSGordon Ross 	u_offset_t offset,	/* ignore if not I/O */
95*8329232eSGordon Ross 	ssize_t length,		/* ignore if not I/O */
96*8329232eSGordon Ross 	int svmand,		/* System V mandatory locking */
97*8329232eSGordon Ross 	caller_context_t *ct)	/* caller context */
98*8329232eSGordon Ross {
99*8329232eSGordon Ross 	ASSERT(nbl_in_crit(vp));
100*8329232eSGordon Ross 
101*8329232eSGordon Ross 	return (0);
102*8329232eSGordon Ross }
103*8329232eSGordon Ross 
104*8329232eSGordon Ross /*
105*8329232eSGordon Ross  * Determine if the given file has mode bits for System V mandatory locks.
106*8329232eSGordon Ross  * If there was an error, the errno value is returned.  Otherwise, zero is
107*8329232eSGordon Ross  * returned and *svp is set appropriately (non-zero for mandatory locks,
108*8329232eSGordon Ross  * zero for no mandatory locks).
109*8329232eSGordon Ross  */
110*8329232eSGordon Ross 
111*8329232eSGordon Ross int
nbl_svmand(vnode_t * vp,cred_t * cr,int * svp)112*8329232eSGordon Ross nbl_svmand(vnode_t *vp, cred_t *cr, int *svp)
113*8329232eSGordon Ross {
114*8329232eSGordon Ross 	struct vattr va;
115*8329232eSGordon Ross 	int error;
116*8329232eSGordon Ross 
117*8329232eSGordon Ross 	va.va_mask = AT_MODE;
118*8329232eSGordon Ross 	error = VOP_GETATTR(vp, &va, 0, cr, NULL);
119*8329232eSGordon Ross 	if (error != 0)
120*8329232eSGordon Ross 		return (error);
121*8329232eSGordon Ross 
122*8329232eSGordon Ross 	*svp = MANDLOCK(vp, va.va_mode);
123*8329232eSGordon Ross 	return (0);
124*8329232eSGordon Ross }