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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 * Utility routines and top-level conflict detection code for NBMAND
33 #include <sys/types.h>
34 #include <sys/cmn_err.h>
35 #include <sys/debug.h>
36 #include <sys/fcntl.h>
37 #include <sys/rwlock.h>
38 #include <sys/vnode.h>
40 #include <sys/nbmlock.h>
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).
49 nbl_start_crit(vnode_t
*vp
, krw_t mode
)
51 rw_enter(&vp
->v_nbllock
, mode
);
55 * Leave the critical region.
59 nbl_end_crit(vnode_t
*vp
)
61 rw_exit(&vp
->v_nbllock
);
65 * Return non-zero if some thread is in the critical region.
66 * Note that this is appropriate for use in ASSERT()s only.
70 nbl_in_crit(vnode_t
*vp
)
72 return (RW_LOCK_HELD(&vp
->v_nbllock
));
76 * Returns non-zero if we need to look further for an NBMAND lock or
81 nbl_need_check(vnode_t
*vp
)
84 * Currently we only check if NBMAND locks/shares are allowed on
85 * the filesystem. An option for the future would be to have a
86 * flag on the vnode, though the locking for that can get tricky.
88 /* (vp->v_vfsp) && (vp->v_vfsp->vfs_flag & VFS_NBMAND)) */
93 * Top-level conflict detection routine. The arguments describe the
94 * operation that is being attempted. If the operation conflicts with an
95 * existing lock or share reservation, a non-zero value is returned. If
96 * the operation is allowed, zero is returned. Note that there is an
97 * implicit argument, which is the process ID of the requester.
99 * svmand indicates that the file has System V mandatory locking enabled,
100 * so we should look at all record locks, not just NBMAND record locks.
101 * (This is to avoid a deadlock between a process making an I/O request and
102 * a process trying to release a lock. Instead of letting the first
103 * process block in the filesystem code, we flag a conflict here.)
107 nbl_conflict(vnode_t
*vp
,
108 nbl_op_t op
, /* attempted operation */
109 u_offset_t offset
, /* ignore if not I/O */
110 ssize_t length
, /* ignore if not I/O */
111 int svmand
, /* System V mandatory locking */
112 caller_context_t
*ct
) /* caller context */
114 ASSERT(nbl_in_crit(vp
));
115 ASSERT(op
== NBL_READ
|| op
== NBL_WRITE
|| op
== NBL_RENAME
||
116 op
== NBL_REMOVE
|| op
== NBL_READWRITE
);
118 if (nbl_share_conflict(vp
, op
, ct
)) {
123 * If this is not an I/O request, there's no need to check against
124 * the locks on the file.
126 if (op
== NBL_REMOVE
|| op
== NBL_RENAME
)
129 return (nbl_lock_conflict(vp
, op
, offset
, length
, svmand
, ct
));
133 * Determine if the given file has mode bits for System V mandatory locks.
134 * If there was an error, the errno value is returned. Otherwise, zero is
135 * returned and *svp is set appropriately (non-zero for mandatory locks,
136 * zero for no mandatory locks).
140 nbl_svmand(vnode_t
*vp
, cred_t
*cr
, int *svp
)
145 va
.va_mask
= AT_MODE
;
146 error
= VOP_GETATTR(vp
, &va
, 0, cr
, NULL
);
150 *svp
= MANDLOCK(vp
, va
.va_mode
);
155 * The kernel handles this for us when we actually try I/O.
159 nbl_share_conflict(vnode_t
*vp
, nbl_op_t op
, caller_context_t
*ct
)
165 * The kernel handles this for us when we actually try I/O.
169 nbl_lock_conflict(vnode_t
*vp
, nbl_op_t op
, u_offset_t offset
,
170 ssize_t length
, int svmand
, caller_context_t
*ct
)