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