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