Merge commit '7e934d3acc051b7ee3ef0d11571fd1225800a607'
[unleashed.git] / kernel / fs / nbmlock.c
blobb7cf2765cb95ab92cef55e716a0c85cdcea9459e
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Utility routines and top-level conflict detection code for NBMAND
28 * locks.
31 #include <sys/nbmlock.h>
32 #include <sys/rwlock.h>
33 #include <sys/vnode.h>
34 #include <sys/cmn_err.h>
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
37 #include <sys/vfs.h>
40 * Enter the critical region for synchronizing I/O requests with lock/share
41 * requests. "mode" specifies whether the caller intends to update
42 * lock/share state (as opposed to just query it).
45 void
46 nbl_start_crit(vnode_t *vp, krw_t mode)
48 rw_enter(&vp->v_nbllock, mode);
52 * Leave the critical region.
55 void
56 nbl_end_crit(vnode_t *vp)
58 rw_exit(&vp->v_nbllock);
62 * Return non-zero if some thread is in the critical region.
63 * Note that this is appropriate for use in ASSERT()s only.
66 int
67 nbl_in_crit(vnode_t *vp)
69 return (RW_LOCK_HELD(&vp->v_nbllock));
73 * Returns non-zero if we need to look further for an NBMAND lock or
74 * share conflict.
76 int
77 nbl_need_check(vnode_t *vp)
80 * Currently we only check if NBMAND locks/shares are allowed on
81 * the filesystem. An option for the future would be to have a
82 * flag on the vnode, though the locking for that can get tricky.
84 return ((vp->v_vfsp) && (vp->v_vfsp->vfs_flag & VFS_NBMAND));
88 * Top-level conflict detection routine. The arguments describe the
89 * operation that is being attempted. If the operation conflicts with an
90 * existing lock or share reservation, a non-zero value is returned. If
91 * the operation is allowed, zero is returned. Note that there is an
92 * implicit argument, which is the process ID of the requester.
94 * svmand indicates that the file has System V mandatory locking enabled,
95 * so we should look at all record locks, not just NBMAND record locks.
96 * (This is to avoid a deadlock between a process making an I/O request and
97 * a process trying to release a lock. Instead of letting the first
98 * process block in the filesystem code, we flag a conflict here.)
102 nbl_conflict(vnode_t *vp,
103 nbl_op_t op, /* attempted operation */
104 uoff_t offset, /* ignore if not I/O */
105 ssize_t length, /* ignore if not I/O */
106 int svmand, /* System V mandatory locking */
107 caller_context_t *ct) /* caller context */
109 ASSERT(nbl_in_crit(vp));
110 ASSERT(op == NBL_READ || op == NBL_WRITE || op == NBL_RENAME ||
111 op == NBL_REMOVE || op == NBL_READWRITE);
113 if (nbl_share_conflict(vp, op, ct)) {
114 return (1);
118 * If this is not an I/O request, there's no need to check against
119 * the locks on the file.
121 if (op == NBL_REMOVE || op == NBL_RENAME)
122 return (0);
124 return (nbl_lock_conflict(vp, op, offset, length, svmand, ct));
128 * Determine if the given file has mode bits for System V mandatory locks.
129 * If there was an error, the errno value is returned. Otherwise, zero is
130 * returned and *svp is set appropriately (non-zero for mandatory locks,
131 * zero for no mandatory locks).
135 nbl_svmand(vnode_t *vp, cred_t *cr, int *svp)
137 struct vattr va;
138 int error;
140 va.va_mask = AT_MODE;
141 error = fop_getattr(vp, &va, 0, cr, NULL);
142 if (error != 0)
143 return (error);
145 *svp = MANDLOCK(vp, va.va_mode);
146 return (0);