xref: /illumos-gate/usr/src/uts/common/fs/nbmlock.c (revision fcdb3229a31dd4ff700c69238814e326aad49098)
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 2024 RackTop Systems, Inc.
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/types.h>
38 #include <sys/fcntl.h>
39 #include <sys/vfs.h>
40 
41 /*
42  * Enter the critical region for synchronizing I/O requests with lock/share
43  * requests.  "mode" specifies whether the caller intends to update
44  * lock/share state (as opposed to just query it).
45  */
46 
47 void
48 nbl_start_crit(vnode_t *vp, krw_t mode)
49 {
50 	rw_enter(&vp->v_nbllock, mode);
51 }
52 
53 /*
54  * Leave the critical region.
55  */
56 
57 void
58 nbl_end_crit(vnode_t *vp)
59 {
60 	rw_exit(&vp->v_nbllock);
61 }
62 
63 /*
64  * Return non-zero if some thread is in the critical region.
65  * Note that this is appropriate for use in ASSERT()s only.
66  */
67 
68 int
69 nbl_in_crit(vnode_t *vp)
70 {
71 	return (RW_LOCK_HELD(&vp->v_nbllock));
72 }
73 
74 /*
75  * Returns non-zero if we need to look further for an NBMAND lock or
76  * share conflict.
77  */
78 int
79 nbl_need_check(vnode_t *vp)
80 {
81 	/*
82 	 * Currently we only check if NBMAND locks/shares are allowed on
83 	 * the filesystem.  An option for the future would be to have a
84 	 * flag on the vnode, though the locking for that can get tricky.
85 	 */
86 	return ((vp->v_vfsp) && (vp->v_vfsp->vfs_flag & VFS_NBMAND));
87 }
88 
89 /*
90  * Top-level conflict detection routine.  The arguments describe the
91  * operation that is being attempted.  If the operation conflicts with an
92  * existing lock or share reservation, a non-zero value is returned.  If
93  * the operation is allowed, zero is returned.  Note that there is an
94  * implicit argument, which is the process ID of the requester.
95  *
96  * svmand indicates that the file has System V mandatory locking enabled,
97  * so we should look at all record locks, not just NBMAND record locks.
98  * (This is to avoid a deadlock between a process making an I/O request and
99  * a process trying to release a lock.  Instead of letting the first
100  * process block in the filesystem code, we flag a conflict here.)
101  */
102 
103 int
104 nbl_conflict(vnode_t *vp,
105 		nbl_op_t op,		/* attempted operation */
106 		u_offset_t offset,	/* ignore if not I/O */
107 		ssize_t length,		/* ignore if not I/O */
108 		int svmand,		/* System V mandatory locking */
109 		caller_context_t *ct)	/* caller context */
110 {
111 	ASSERT(nbl_in_crit(vp));
112 	ASSERT(op == NBL_READ || op == NBL_WRITE || op == NBL_RENAME ||
113 	    op == NBL_REMOVE || op == NBL_READWRITE);
114 
115 	if (nbl_share_conflict(vp, op, ct)) {
116 		return (1);
117 	}
118 
119 	/*
120 	 * If this is not an I/O request, there's no need to check against
121 	 * the locks on the file.
122 	 */
123 	if (op == NBL_REMOVE || op == NBL_RENAME)
124 		return (0);
125 
126 	return (nbl_lock_conflict(vp, op, offset, length, svmand, ct));
127 }
128 
129 /*
130  * Determine if the given file has mode bits for System V mandatory locks.
131  * If there was an error, the errno value is returned.  Otherwise, zero is
132  * returned and *svp is set appropriately (non-zero for mandatory locks,
133  * zero for no mandatory locks).
134  *
135  * This is called only in read/write code paths where we know that the
136  * user has previously opened a handle on this file so we have already
137  * checked that they have access to the file attributes.  Avoid the
138  * redundant access checking here which otherwise can cause frequent
139  * and potentially expensive kidmap up-calls from ZFS.
140  */
141 
142 int
143 nbl_svmand(vnode_t *vp, cred_t *cr, int *svp)
144 {
145 	struct vattr va;
146 	int error;
147 	int flags = 0;
148 
149 	if (vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS))
150 		flags |= ATTR_NOACLCHECK;
151 
152 	va.va_mask = AT_MODE;
153 	error = VOP_GETATTR(vp, &va, flags, cr, NULL);
154 	if (error != 0)
155 		return (error);
156 
157 	*svp = MANDLOCK(vp, va.va_mode);
158 	return (0);
159 }
160