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