1.\" $NetBSD: vnsubr.9,v 1.40 2010/12/02 12:54:13 wiz Exp $ 2.\" 3.\" Copyright (c) 2001, 2005, 2006 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Gregory McGarry. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd June 18, 2010 31.Dt VNSUBR 9 32.Os 33.Sh NAME 34.Nm vnsubr , 35.Nm vn_bwrite , 36.Nm vn_close , 37.Nm vn_default_error , 38.Nm vn_isunder , 39.Nm vn_lock , 40.Nm vn_markexec , 41.Nm vn_marktext , 42.Nm vn_rdwr , 43.Nm vn_open , 44.Nm vn_stat , 45.Nm vn_writechk 46.Nd high-level convenience functions for vnode operations 47.Sh SYNOPSIS 48.In sys/param.h 49.In sys/lock.h 50.In sys/vnode.h 51.Ft int 52.Fn vn_bwrite "void *ap" 53.Ft int 54.Fn vn_close "struct vnode *vp" "int flags" "kauth_cred_t cred" 55.Ft int 56.Fn vn_default_error "void *v" 57.Ft int 58.Fn vn_isunder "struct vnode *dvp" "struct vnode *rvp" "struct lwp *l" 59.Ft int 60.Fn vn_lock "struct vnode *vp" "int flags" 61.Ft void 62.Fn vn_markexec "struct vnode *vp" 63.Ft void 64.Fn vn_marktext "struct vnode *vp" 65.Ft int 66.Fn vn_open "struct nameidata *ndp" "int fmode" "int cmode" 67.Ft int 68.Fo vn_rdwr 69.Fa "enum uio_rw rw" "struct vnode *vp" "void *base" 70.Fa "int len" "off_t offset" "enum uio_seg segflg" "int ioflg" 71.Fa "kauth_cred_t cred" "size_t *aresid" "struct lwp *l" 72.Fc 73.Ft int 74.Fn vn_readdir "file_t *fp" "char *buf" "int segflg" "u_int count" "int *done" "struct lwp *l" "off_t **cookies" "int *ncookies" 75.Ft int 76.Fn vn_stat "struct vnode *vp" "struct stat *sb" 77.Ft int 78.Fn vn_writechk "struct vnode *vp" 79.Sh DESCRIPTION 80The high-level functions described in this page are convenience 81functions for simplified access to the vnode operations described in 82.Xr vnodeops 9 . 83.Sh FUNCTIONS 84.Bl -tag -width compact 85.It Fn vn_bwrite "ap" 86Common code for block write operations. 87.It Fn vn_close "vp" "flags" "cred" 88Common code for a vnode close. 89The argument 90.Fa vp 91is the unlocked vnode of the vnode to close. 92.Fn vn_close 93simply locks the vnode, invokes the vnode operation 94.Xr VOP_CLOSE 9 95and calls 96.Fn vput 97to return the vnode to the freelist or holdlist. 98Note that 99.Fn vn_close 100expects an unlocked, referenced vnode and will dereference the vnode 101prior to returning. 102If the operation is successful zero is returned, 103otherwise an appropriate error is returned. 104.It Fn vn_default_error "v" 105A generic "default" routine that just returns error. 106It is used by a file system to specify unsupported operations in 107the vnode operations vector. 108.It Fn vn_isunder "dvp" "rvp" "l" 109Common code to check if one directory specified by the vnode 110.Fa rvp 111can be found inside the directory specified by the vnode 112.Fa dvp . 113The argument 114.Fa l 115is the calling process. 116.Fn vn_isunder 117is intended to be used in 118.Xr chroot 2 , 119.Xr chdir 2 , 120.Xr fchdir 2 , 121etc., to ensure that 122.Xr chroot 2 123actually means something. 124If the operation is successful zero is returned, otherwise 1 is returned. 125.It Fn vn_lock "vp" "flags" 126Common code to acquire the lock for vnode 127.Fa vp . 128The argument 129.Fa flags 130specifies the 131.Xr lockmgr 9 132flags used to lock the vnode. 133If the operation is successful zero is returned, otherwise an 134appropriate error code is returned. 135The vnode interlock 136.Em v_interlock 137is released on return. 138.Pp 139.Fn vn_lock 140must not be called when the vnode's reference count is zero. 141Instead, 142.Xr vget 9 143should be used. 144.It Fn vn_markexec "vp" 145Common code to mark the vnode 146.Fa vp 147as containing executable code of a running process. 148.It Fn vn_marktext "vp" 149Common code to mark the vnode 150.Fa vp 151as being the text of a running process. 152.It Fn vn_open "ndp" "fmode" "cmode" 153Common code for vnode open operations. 154The pathname is described in the nameidata pointer (see 155.Xr namei 9 ) . 156The arguments 157.Fa fmode 158and 159.Fa cmode 160specify the 161.Xr open 2 162file mode and the access permissions for creation. 163.Fn vn_open 164checks permissions and invokes the 165.Xr VOP_OPEN 9 166or 167.Xr VOP_CREATE 9 168vnode operations. 169If the operation is successful zero is returned and the vnode is locked, 170otherwise an appropriate error code is returned. 171.It Fn vn_rdwr "rw" "vp" "base" "len" "offset" "segflg" "ioflg" "cred" "aresid" "l" 172Common code to package up an I/O request on a vnode into a uio and 173then perform the I/O. 174The argument 175.Fa rw 176specifies whether the I/O is a read 177.Pq Dv UIO_READ 178or write 179.Pq Dv UIO_WRITE 180operation. 181The vnode is specified by 182.Fa vp . 183The arguments 184.Fa l 185and 186.Fa cred 187are the calling lwp and its credentials. 188If 189.Fa ioflg 190contains 191.Dv IO_NODELOCKED , 192it is expected that the vnode is locked. 193.Fa ioflg 194will be passed to 195.Fn VOP_READ Ns No / Ns Fn VOP_WRITE . 196The remaining arguments specify the uio parameters. 197For further information on these parameters see 198.Xr uiomove 9 . 199.It Fn vn_readdir "fp" "buf" "segflg" "count" "done" "l" "cookies" "ncookies" 200Common code for reading the contents of a directory. 201The argument 202.Fa fp 203is the file structure, 204.Fa buf 205is the buffer for placing the struct dirent structures. 206The arguments 207.Fa cookies 208and 209.Fa ncookies 210specify the addresses for the list and number of directory seek 211cookies generated for NFS. 212Both 213.Fa cookies 214and 215.Fa ncookies 216should be 217.Dv NULL 218if they aren't required to be returned by 219.Fn vn_readdir . 220If the operation is successful zero is returned, otherwise an 221appropriate error code is returned. 222.It Fn vn_stat "vp" "sb" 223Common code for a vnode stat operation. 224The vnode is specified by the argument 225.Fa vp , 226and 227.Fa sb 228is the buffer to return the stat information. 229.Fn vn_stat 230basically calls the vnode operation 231.Xr VOP_GETATTR 9 232and transfers the contents of a vattr structure into a struct stat. 233If the operation is successful zero is returned, otherwise an 234appropriate error code is returned. 235.It Fn vn_writechk "vp" 236Common code to check for write permission on the vnode 237.Fa vp . 238A vnode is read-only if it is in use as a process's text image. 239If the vnode is read-only ETEXTBSY is returned, otherwise zero is 240returned to indicate that the vnode can be written to. 241.El 242.Sh ERRORS 243.Bl -tag -width Er 244.It Bq Er EBUSY 245The LK_NOWAIT flag was set and 246.Fn vn_lock 247would have slept. 248.It Bq Er ENOENT 249The vnode has been reclaimed and is dead. 250This error is only returned if the LK_RETRY flag is not passed to 251.Fn vn_lock . 252.It Bq Er ETXTBSY 253Cannot write to a vnode since is a process's text image. 254.El 255.Sh CODE REFERENCES 256The high-level convenience functions are implemented within the files 257.Pa sys/kern/vfs_vnops.c 258and 259.Pa sys/sys/vnode.h . 260.Sh SEE ALSO 261.Xr file 9 , 262.Xr intro 9 , 263.Xr lock 9 , 264.Xr namei 9 , 265.Xr vattr 9 , 266.Xr vfs 9 , 267.Xr vnode 9 , 268.Xr vnodeops 9 269