xref: /freebsd/share/man/man9/VOP_LOCK.9 (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
1.\" -*- nroff -*-
2.\"
3.\" Copyright (c) 1996 Doug Rabson
4.\"
5.\" All rights reserved.
6.\"
7.\" This program is free software.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
19.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
22.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28.\"
29.\" $FreeBSD$
30.\"
31.Dd July 24, 1996
32.Os
33.Dt VOP_LOCK 9
34.Sh NAME
35.Nm VOP_LOCK ,
36.Nm VOP_UNLOCK ,
37.Nm VOP_ISLOCKED ,
38.Nm vn_lock
39.Nd serialize access to a vnode
40.Sh SYNOPSIS
41.In sys/param.h
42.In sys/lock.h
43.In sys/vnode.h
44.Ft int
45.Fn VOP_LOCK "struct vnode *vp" "int flags" "struct thread *td"
46.Ft int
47.Fn VOP_UNLOCK "struct vnode *vp" "int flags" "struct thread *td"
48.Ft int
49.Fn VOP_ISLOCKED "struct vnode *vp" "struct thread *td"
50.Ft int
51.Fn vn_lock "struct vnode *vp" "int flags" "struct thread *td"
52.Sh DESCRIPTION
53These calls are used to serialize access to the file system, such as
54to prevent two writes to the same file from happening at the
55same time.
56.Pp
57The arguments are:
58.Bl -tag -width flags
59.It Fa vp
60The vnode being locked or unlocked.
61.It Fa flags
62One of the lock request types:
63.Pp
64.Bl -tag -width ".Dv LK_EXCLUPGRADE" -offset indent -compact
65.It Dv LK_SHARED
66Shared lock.
67.It Dv LK_EXCLUSIVE
68Exclusive lock.
69.It Dv LK_UPGRADE
70Shared-to-exclusive upgrade.
71.It Dv LK_EXCLUPGRADE
72First shared-to-exclusive upgrade.
73.It Dv LK_DOWNGRADE
74Exclusive-to-shared downgrade.
75.It Dv LK_RELEASE
76Release any type of lock.
77.It Dv LK_DRAIN
78Wait for all lock activity to end.
79.El
80.Pp
81The lock type may be
82.Em or Ns 'ed
83with these lock flags:
84.Pp
85.Bl -tag -width ".Dv LK_EXCLUPGRADE" -offset indent -compact
86.It Dv LK_NOWAIT
87Do not sleep to wait for lock.
88.It Dv LK_SLEEPFAIL
89Sleep, then return failure.
90.It Dv LK_CANRECURSE
91Allow recursive exclusive lock.
92.It Dv LK_REENABLE
93Lock is to be reenabled after drain.
94.It Dv LK_NOPAUSE
95No spinloop.
96.El
97.Pp
98The lock type may be
99.Em or Ns 'ed
100with these control flags:
101.Pp
102.Bl -tag -width ".Dv LK_EXCLUPGRADE" -offset indent -compact
103.It Dv LK_INTERLOCK
104Specify when the caller already has a simple lock
105.Fn ( VOP_LOCK
106will unlock the simple lock after getting the lock).
107.It Dv LK_RETRY
108Retry until locked.
109.It Dv LK_NOOBJ
110Do not create object.
111.El
112.It Fa td
113Thread context to use for the locks.
114.El
115.Pp
116Kernel code should use
117.Fn vn_lock
118to lock a vnode rather than calling
119.Fn VOP_LOCK
120directly.
121.Sh RETURN VALUES
122Zero is returned on success, otherwise an error is returned.
123.Sh PSEUDOCODE
124.Bd -literal
125struct vopnode {
126    int von_flag;
127    /*
128     * Other file system specific data.
129     */
130    ...;
131};
132#define VON_LOCKED	1
133#define VON_WANTED	2
134#define VTOVON(vp)	((struct vopnode *) (vp)->v_data)
135
136int
137vop_lock(struct vnode *vp)
138{
139    struct vopnode* vop;
140
141start:
142    while (vp->v_flag & VXLOCK) {
143	vp->v_flag |= VXWANT;
144	tsleep((caddr_t)vp, PINOD, "voplk1", 0);
145    }
146    if (vp->v_tag == VT_NON)
147	return ENOENT;
148
149    vop = VTOVON(vp);
150    if (vop->von_flag & VON_LOCKED) {
151	vop->von_flag |= VON_WANTED;
152	tsleep((caddr_t) vop, PINOD, "voplk2", 0);
153	goto start;
154    }
155
156    vop->von_flag |= VON_LOCKED;
157
158    return 0;
159}
160
161int
162vop_unlock(struct vnode *vp)
163{
164    struct vopnode *vop = VTOVON(vp);
165
166    if ((vop->von_flag & VON_LOCKED) == 0) {
167	panic("vop_unlock not locked");
168    }
169    vop->von_flag &= ~VON_LOCKED;
170    if (vop->von_flag & VON_WANTED) {
171	vop->von_flag &= ~VON_WANTED;
172	wakeup((caddr_t) vop);
173    }
174
175    return 0;
176}
177
178int
179vop_islocked(struct vnode *vp)
180{
181    struct vopnode *vop = VTOVON(vp);
182
183    if (vop->von_flag & VON_LOCKED)
184	return 1;
185    else
186	return 0;
187}
188.Ed
189.Sh SEE ALSO
190.Xr vnode 9
191.Sh AUTHORS
192This manual page was written by
193.An Doug Rabson .
194