xref: /freebsd/share/man/man9/VOP_LOCK.9 (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
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.Fd #include <sys/param.h>
42.Fd #include <sys/lock.h>
43.Fd #include <sys/vnode.h>
44.Ft int
45.Fn VOP_LOCK "struct vnode *vp" "int flags" "struct proc *p"
46.Ft int
47.Fn VOP_UNLOCK "struct vnode *vp" "int flags" "struct proc *p"
48.Ft int
49.Fn VOP_ISLOCKED "struct vnode *vp" "struct proc *p"
50.Ft int
51.Fn vn_lock "struct vnode *vp" "int flags" "struct proc *p"
52.Sh DESCRIPTION
53These calls are used to serialize access to the filesystem, 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 Ar vp
60the vnode being locked or unlocked
61.It Ar flags
62One of the lock request types:
63.Bl -column LK_EXCLUPGRADE -offset indent
64.It Dv LK_SHARED Ta "Shared lock"
65.It Dv LK_EXCLUSIVE Ta "Exclusive lock"
66.It Dv LK_UPGRADE Ta "Shared-to-exclusive upgrade"
67.It Dv LK_EXCLUPGRADE Ta "First shared-to-exclusive upgrade"
68.It Dv LK_DOWNGRADE Ta "Exclusive-to-shared downgrade"
69.It Dv LK_RELEASE Ta "Release any type of lock"
70.It Dv LK_DRAIN Ta "Wait for all lock activity to end"
71.El
72.Pp
73The lock type may be
74.Em or Ns 'ed
75with these lock flags:
76.Bl -column LK_CANRECURSE -offset indent
77.It Dv LK_NOWAIT Ta "Do not sleep to wait for lock"
78.It Dv LK_SLEEPFAIL Ta "Sleep, then return failure"
79.It Dv LK_CANRECURSE Ta "Allow recursive exclusive lock"
80.It Dv LK_REENABLE Ta "Lock is to be reenabled after drain"
81.It Dv LK_NOPAUSE Ta "No spinloop"
82.El
83.Pp
84The lock type may be
85.Em or Ns 'ed
86with these control flags:
87.Bl -column LK_INTERLOCK -offset indent
88.It Dv LK_INTERLOCK Ta "Specify when the caller already has a simple lock \
89(VOP_LOCK will unlock the simple lock after getting the lock)"
90.It Dv LK_RETRY Ta "Retry until locked"
91.It Dv LK_NOOBJ Ta "Don't create object"
92.El
93.It Ar p
94process context to use for the locks
95.El
96.Pp
97Kernel code should use
98.Fn vn_lock
99to lock a vnode rather than calling
100.Fn VOP_LOCK
101directly.
102.Sh RETURN VALUES
103Zero is returned on success, otherwise an error is returned.
104.Sh PSEUDOCODE
105.Bd -literal
106struct vopnode {
107    int von_flag;
108    /*
109     * Other filesystem specific data.
110     */
111    ...;
112};
113#define VON_LOCKED	1
114#define VON_WANTED	2
115#define VTOVON(vp)	((struct vopnode *) (vp)->v_data)
116
117int
118vop_lock(struct vnode *vp)
119{
120    struct vopnode* vop;
121
122start:
123    while (vp->v_flag & VXLOCK) {
124	vp->v_flag |= VXWANT;
125	tsleep((caddr_t)vp, PINOD, "voplk1", 0);
126    }
127    if (vp->v_tag == VT_NON)
128	return ENOENT;
129
130    vop = VTOVON(vp);
131    if (vop->von_flag & VON_LOCKED) {
132	vop->von_flag |= VON_WANTED;
133	tsleep((caddr_t) vop, PINOD, "voplk2", 0);
134	goto start;
135    }
136
137    vop->von_flag |= VON_LOCKED;
138
139    return 0;
140}
141
142int
143vop_unlock(struct vnode *vp)
144{
145    struct vopnode *vop = VTOVON(vp);
146
147    if ((vop->von_flag & VON_LOCKED) == 0) {
148	panic("vop_unlock not locked");
149    }
150    vop->von_flag &= ~VON_LOCKED;
151    if (vop->von_flag & VON_WANTED) {
152	vop->von_flag &= ~VON_WANTED;
153	wakeup((caddr_t) vop);
154    }
155
156    return 0;
157}
158
159int
160vop_islocked(struct vnode *vp)
161{
162    struct vopnode *vop = VTOVON(vp);
163
164    if (vop->von_flag & VON_LOCKED)
165	return 1;
166    else
167	return 0;
168}
169.Ed
170.Sh SEE ALSO
171.Xr vnode 9
172.Sh AUTHORS
173This man page was written by
174.An Doug Rabson .
175