xref: /titanic_50/usr/src/uts/common/fs/nbmlock.c (revision 080575042aba2197b425ebfd52061dea061a9aa1)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Utility routines and top-level conflict detection code for NBMAND
31  * locks.
32  */
33 
34 #include <sys/nbmlock.h>
35 #include <sys/rwlock.h>
36 #include <sys/vnode.h>
37 #include <sys/cmn_err.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
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
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
70 nbl_in_crit(vnode_t *vp)
71 {
72 	return (RW_LOCK_HELD(&vp->v_nbllock));
73 }
74 
75 /*
76  * Return the nbl_op_t that corresponds to the given lock type (read or
77  * write).
78  */
79 
80 nbl_op_t
81 nbl_lock_to_op(int lock_type)
82 {
83 	int level;
84 
85 	switch (lock_type) {
86 	case F_WRLCK:
87 		return (NBL_READWRITE);
88 	case F_RDLCK:
89 		return (NBL_READ);
90 	default:
91 #ifdef DEBUG
92 		level = CE_PANIC;
93 #else
94 		level = CE_WARN;
95 #endif
96 		cmn_err(level, "unexpected lock type: %d\n", lock_type);
97 		return (NBL_WRITE);	/* pick something restrictive */
98 	}
99 	/*NOTREACHED*/
100 }
101 
102 /*
103  * Returns non-zero if we need to look further for an NBMAND lock or
104  * share conflict.
105  */
106 int
107 nbl_need_check(vnode_t *vp)
108 {
109 	/*
110 	 * Currently we only check if NBMAND locks/shares are allowed on
111 	 * the filesystem.  An option for the future would be to have a
112 	 * flag on the vnode, though the locking for that can get tricky.
113 	 */
114 	return ((vp->v_vfsp) && (vp->v_vfsp->vfs_flag & VFS_NBMAND));
115 }
116 
117 /*
118  * Top-level conflict detection routine.  The arguments describe the
119  * operation that is being attempted.  If the operation conflicts with an
120  * existing lock or share reservation, a non-zero value is returned.  If
121  * the operation is allowed, zero is returned.  Note that there is an
122  * implicit argument, which is the process ID of the requester.
123  *
124  * svmand indicates that the file has System V mandatory locking enabled,
125  * so we should look at all record locks, not just NBMAND record locks.
126  * (This is to avoid a deadlock between a process making an I/O request and
127  * a process trying to release a lock.  Instead of letting the first
128  * process block in the filesystem code, we flag a conflict here.)
129  */
130 
131 int
132 nbl_conflict(vnode_t *vp,
133 		nbl_op_t op,		/* attempted operation */
134 		u_offset_t offset,	/* ignore if not I/O */
135 		ssize_t length,		/* ignore if not I/O */
136 		int svmand)		/* System V mandatory locking */
137 {
138 	ASSERT(nbl_in_crit(vp));
139 	ASSERT(op == NBL_READ || op == NBL_WRITE || op == NBL_RENAME ||
140 	    op == NBL_REMOVE || op == NBL_READWRITE);
141 
142 	if (nbl_share_conflict(vp, op)) {
143 		return (1);
144 	}
145 
146 	/*
147 	 * If this is not an I/O request, there's no need to check against
148 	 * the locks on the file.
149 	 */
150 	if (op == NBL_REMOVE || op == NBL_RENAME)
151 		return (0);
152 
153 	return (nbl_lock_conflict(vp, op, offset, length, svmand));
154 }
155 
156 /*
157  * Determine if the given file has mode bits for System V mandatory locks.
158  * If there was an error, the errno value is returned.  Otherwise, zero is
159  * returned and *svp is set appropriately (non-zero for mandatory locks,
160  * zero for no mandatory locks).
161  */
162 
163 int
164 nbl_svmand(vnode_t *vp, cred_t *cr, int *svp)
165 {
166 	struct vattr va;
167 	int error;
168 
169 	va.va_mask = AT_MODE;
170 	error = VOP_GETATTR(vp, &va, 0, cr);
171 	if (error != 0)
172 		return (error);
173 
174 	*svp = MANDLOCK(vp, va.va_mode);
175 	return (0);
176 }
177