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.\" $Id: VOP_LOCK.9,v 1.1 1997/03/03 18:00:25 dfr Exp $ 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.Nd serialize access to a vnode 39.Sh SYNOPSIS 40.Fd #include <sys/vnode.h> 41.Ft int 42.Fn VOP_LOCK "struct vnode *vp" 43.Ft int 44.Fn VOP_UNLOCK "struct vnode *vp" 45.Ft int 46.Fn VOP_ISLOCKED "struct vnode *vp" 47.Sh DESCRIPTION 48.Pp 49The arguments are: 50.Bl -tag -width vp 51.It Ar vp 52the vnode being locked or unlocked 53.El 54.Pp 55These calls are used to serialize access to the filesystem, for 56instance to prevent two writes to the same file from happening at the 57same time. 58.Sh RETURN VALUES 59Zero is returned on success, otherwise an error is returned. 60.Sh PSEUDOCODE 61.Bd -literal 62struct vopnode { 63 int von_flag; 64 /* 65 * Other filesystem specific data. 66 */ 67 ...; 68}; 69#define VON_LOCKED 1 70#define VON_WANTED 2 71#define VTOVON(vp) ((struct vopnode *) (vp)->v_data) 72 73int 74vop_lock(struct vnode *vp) 75{ 76 struct vopnode* vop; 77 78start: 79 while (vp->v_flag & VXLOCK) { 80 vp->v_flag |= VXWANT; 81 tsleep((caddr_t)vp, PINOD, "voplk1", 0); 82 } 83 if (vp->v_tag == VT_NON) 84 return ENOENT; 85 86 vop = VTOVON(vp); 87 if (vop->von_flag & VON_LOCKED) { 88 vop->von_flag |= VON_WANTED; 89 tsleep((caddr_t) vop, PINOD, "voplk2", 0); 90 goto start; 91 } 92 93 vop->von_flag |= VON_LOCKED; 94 95 return 0; 96} 97 98int 99vop_unlock(struct vnode *vp) 100{ 101 struct vopnode *vop = VTOVON(vp); 102 103 if ((vop->von_flag & VON_LOCKED) == 0) { 104 panic("vop_unlock not locked"); 105 } 106 vop->von_flag &= ~VON_LOCKED; 107 if (vop->von_flag & VON_WANTED) { 108 vop->von_flag &= ~VON_WANTED; 109 wakeup((caddr_t) vop); 110 } 111 112 return 0; 113} 114 115int 116vop_islocked(struct vnode *vp) 117{ 118 struct vopnode *vop = VTOVON(vp); 119 120 if (vop->von_flag & VON_LOCKED) 121 return 1; 122 else 123 return 0; 124} 125.Ed 126.Sh SEE ALSO 127.Xr vnode 9 128.Sh AUTHORS 129This man page was written by Doug Rabson. 130