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