xref: /freebsd/share/man/man9/VOP_LOCK.9 (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 January 10, 2008
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"
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_CANRECURSE" -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_DOWNGRADE
72Exclusive-to-shared downgrade.
73.It Dv LK_RELEASE
74Release any type of lock.
75.It Dv LK_DRAIN
76Wait for all lock activity to end.
77.El
78.Pp
79The lock type may be
80.Em or Ns 'ed
81with these lock flags:
82.Pp
83.Bl -tag -width ".Dv LK_CANRECURSE" -offset indent -compact
84.It Dv LK_NOWAIT
85Do not sleep to wait for lock.
86.It Dv LK_SLEEPFAIL
87Sleep, then return failure.
88.It Dv LK_CANRECURSE
89Allow recursive exclusive lock.
90.El
91.Pp
92The lock type may be
93.Em or Ns 'ed
94with these control flags:
95.Pp
96.Bl -tag -width ".Dv LK_CANRECURSE" -offset indent -compact
97.It Dv LK_INTERLOCK
98Specify when the caller already has a simple lock
99.Fn ( VOP_LOCK
100will unlock the simple lock after getting the lock).
101.It Dv LK_RETRY
102Retry until locked.
103.El
104.It Fa td
105Thread context to use for the locks.
106.El
107.Pp
108Kernel code should use
109.Fn vn_lock
110to lock a vnode rather than calling
111.Fn VOP_LOCK
112directly.
113.Fn vn_lock
114also does not want a thread specified as argument but it
115assumes curthread to be used.
116.Sh RETURN VALUES
117Zero is returned on success, otherwise an error is returned.
118.Sh PSEUDOCODE
119.Bd -literal
120struct vopnode {
121    int von_flag;
122    /*
123     * Other file system specific data.
124     */
125    ...;
126};
127#define VON_LOCKED	1
128#define VON_WANTED	2
129#define VTOVON(vp)	((struct vopnode *) (vp)->v_data)
130
131int
132vop_lock(struct vnode *vp)
133{
134    struct vopnode* vop;
135
136start:
137    while (vp->v_flag & VXLOCK) {
138	vp->v_flag |= VXWANT;
139	tsleep((caddr_t)vp, PINOD, "voplk1", 0);
140    }
141    if (vp->v_tag == VT_NON)
142	return ENOENT;
143
144    vop = VTOVON(vp);
145    if (vop->von_flag & VON_LOCKED) {
146	vop->von_flag |= VON_WANTED;
147	tsleep((caddr_t) vop, PINOD, "voplk2", 0);
148	goto start;
149    }
150
151    vop->von_flag |= VON_LOCKED;
152
153    return 0;
154}
155
156int
157vop_unlock(struct vnode *vp)
158{
159    struct vopnode *vop = VTOVON(vp);
160
161    if ((vop->von_flag & VON_LOCKED) == 0) {
162	panic("vop_unlock not locked");
163    }
164    vop->von_flag &= ~VON_LOCKED;
165    if (vop->von_flag & VON_WANTED) {
166	vop->von_flag &= ~VON_WANTED;
167	wakeup((caddr_t) vop);
168    }
169
170    return 0;
171}
172
173int
174vop_islocked(struct vnode *vp)
175{
176    struct vopnode *vop = VTOVON(vp);
177
178    if (vop->von_flag & VON_LOCKED)
179	return 1;
180    else
181	return 0;
182}
183.Ed
184.Sh SEE ALSO
185.Xr vnode 9
186.Sh AUTHORS
187This manual page was written by
188.An Doug Rabson .
189