1.\" $NetBSD: vnsubr.9,v 1.6 2002/02/13 08:18:56 ross Exp $ 2.\" 3.\" Copyright (c) 2001 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.\" 3. All advertising materials mentioning features or use of this software 18.\" must display the following acknowledgement: 19.\" This product includes software developed by the NetBSD 20.\" Foundation, Inc. and its contributors. 21.\" 4. Neither the name of The NetBSD Foundation nor the names of its 22.\" contributors may be used to endorse or promote products derived 23.\" from this software without specific prior written permission. 24.\" 25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35.\" POSSIBILITY OF SUCH DAMAGE. 36.\" 37.Dd October 22, 2001 38.Dt VNSUBR 9 39.Os 40.Sh NAME 41.Nm vnsubr , 42.Nm vn_bwrite , 43.Nm vn_close , 44.Nm vn_closefile , 45.Nm vn_default_error , 46.Nm vn_fcntl , 47.Nm vn_ioctl , 48.Nm vn_isunder , 49.Nm vn_lock , 50.Nm vn_markexec , 51.Nm vn_setrecurse , 52.Nm vn_restorerecurse , 53.Nm vn_open , 54.Nm vn_read , 55.Nm vn_poll , 56.Nm vn_stat , 57.Nm vn_write , 58.Nm vn_writechk 59.Nd high-level convenience functions for vnode operations 60.Sh SYNOPSIS 61.Fd #include \*[Lt]sys/param.h\*[Gt] 62.Fd #include \*[Lt]sys/lock.h\*[Gt] 63.Fd #include \*[Lt]sys/vnode.h\*[Gt] 64.Ft int 65.Fn vn_bwrite "void *ap" 66.Ft int 67.Fn vn_close "struct vnode *vp" "int flags" "struct ucred *cred" "struct proc *p" 68.Ft int 69.Fn vn_closefile "struct file *fp" "struct proc *p" 70.Ft int 71.Fn vn_default_error "void *v" 72.Ft int 73.Fn vn_fcntl "struct file *fp" "u_int com" "caddr_t data" "struct proc *p" 74.Ft int 75.Fn vn_ioctl "struct file *fp" "u_long com" "caddr_t data" "struct proc *p" 76.Ft int 77.Fn vn_isunder "struct vnode *dvp" "struct vnode *rvp" "struct proc *p" 78.Ft int 79.Fn vn_lock "struct vnode *vp" "int flags" 80.Ft void 81.Fn vn_markexec "struct vnode *vp" 82.Ft u_int 83.Fn vn_setrecurse "struct vnode *vp" 84.Ft void 85.Fn vn_restorerecurse "struct vnode *vp" "u_int flags" 86.Ft int 87.Fn vn_open "struct nameidata *ndp" "int fmode" "int cmode" 88.Ft int 89.Fo vn_rdwr 90.Fa "enum uio_rw rw" "struct vnode *vp" "caddr_t base" 91.Fa "int len" "off_t offset" "enum uio_seg segflg" "int ioflg" 92.Fa "struct ucred *cred" "size_t *aresid" "struct proc *p" 93.Fc 94.Ft int 95.Fn vn_read "struct file *fp" "off_t *offset" "struct uio *uio" "struct ucred *cred" "int flags" 96.Ft int 97.Fn vn_readdir "struct file *fp" "char *buf" "int segflg" "u_int count" "int *done" "struct proc *p" "off_t **cookies" "int *ncookies" 98.Ft int 99.Fn vn_poll "struct file *fp" "int events" "struct proc *p" 100.Ft int 101.Fn vn_stat "void *fdata" "struct stat *sb" "struct proc *p" 102.Ft int 103.Fn vn_write "struct file *fp" "off_t *offset" "struct uio *uio" "struct ucred *cred" "int flags" 104.Ft int 105.Fn vn_writechk "struct vnode *vp" 106.Sh DESCRIPTION 107The high-level functions described in this page are convenience 108functions for simplied access to the vnode operations described in 109.Xr vnodeops 9 . 110.Sh FUNCTIONS 111.Bl -tag -width compact 112.It Fn vn_bwrite "ap" 113Common code for block write operations. 114.It Fn vn_close "vp" "flags" "cred" "p" 115Common code for a vnode close. The argument 116.Fa vp 117is the locked vnode of the vnode to close. 118.Fn vn_close 119simply locks the vnode, invokes the vnode operation 120.Xr VOP_CLOSE 9 121and calls 122.Fn vput 123to return the vnode to the freelist or holdlist. Note that 124.Fn vn_close 125expects an unlocked, referenced vnode and will dereference the vnode 126prior to returning. If the operation is successful zero is returned, 127otherwise an appropriate error is returned. 128.It Fn vn_closefile "fp" "p" 129Common code for a file table vnode close operation. The file is 130described by 131.Fa fp 132and 133.Fa p 134is the calling process. 135.Fn vn_closefile 136simply calls 137.Fn vn_close 138with the appropriate arguments. 139.It Fn vn_default_error "v" 140A generic "default" routine that just returns error. It is used by a 141file system to specify unsupported operations in the vnode operations 142vector. 143.It Fn vn_fcntl "fp" "com" "data" "p" 144Common code for a file table vnode 145.Xr fcntl 2 146operation. The file is 147specified by 148.Fa fp . 149The argument 150.Fa p 151is the calling process. 152.Fn vn_fcntl 153simply locks the vnode and invokes the vnode operation 154.Xr VOP_FCNTL 9 155with the command 156.Fa com 157and buffer 158.Fa data . 159The vnode is unlocked on return. If the operation is successful zero 160is returned, otherwise an appropriate error is returned. 161.It Fn vn_ioctl "fp" "com" "data" "p" 162Common code for a file table vnode ioctl operation. The file is 163specified by 164.Fa fp . 165The argument 166.Fa p 167is the calling process. 168.Fn vn_ioctl 169simply locks the vnode and invokes the vnode operation 170.Xr VOP_IOCTL 9 171with the command 172.Fa com 173and buffer 174.Fa data . 175The vnode is unlocked on return. If the operation is successful zero 176is returned, otherwise an appropriate error is returned. 177.It Fn vn_isunder "dvp" "rvp" "p" 178Common code to check if one directory specified by the vnode 179.Fa rvp 180can be found inside the directory specified by the vnode 181.Fa dvp . 182The argument 183.Fa p 184is the calling process. 185.Fn vn_isunder 186is intended to be used in 187.Xr chroot 2 , 188.Xr chdir 2 , 189.Xr fchdir 2 , 190etc., to ensure that 191.Xr chroot 2 192actually means something. If the operation is successful zero is 193returned, otherwise 1 is returned. 194.It Fn vn_lock "vp" "flags" 195Common code to acquire the lock for vnode 196.Fa vp . 197The argument 198.Fa flags 199specifies the 200.Xr lockmgr 9 201flags used to lock the vnode. If the operation is successful zero is 202returned, otherwise an appropriate error code is returned. The vnode 203interlock 204.Em v_interlock 205is releases on return. 206.Pp 207.Fn vn_lock 208must not be called when the vnode's reference count is zero. Instead, 209.Xr vget 9 210should be used. 211.It Fn vn_markexec "vp" 212Common code to mark the vnode 213.Fa vp 214as containing executable code of a running process. 215.It Fn vn_setrecurse "vp" 216Common code to enable LK_CANRECURSE on the vnode lock for vnode 217.Fa vp . 218.Fn vn_setrecurse 219returns the new 220.Xr lockmgr 9 221flags after the update. 222.It Fn vn_restorerecurse "vp" "flags" 223Common code to restore the vnode lock flags for the vnode 224.Fa vp . 225It is called when done with 226.Fn vn_setrecurse . 227.It Fn vn_open "ndp" "fmode" "cmode" 228Common code for vnode open operations. The pathname is described in 229the nameidata pointer (see 230.Xr namei 9 ) . 231The arguments 232.Fa fmode 233and 234.Fa cmode 235specify the 236.Xr open 2 237file mode and the access permissions for creation. 238.Fn vn_open 239checks permissions and invokes the 240.Xr VOP_OPEN 9 241or 242.Xr VOP_CREATE 9 243vnode operations. If the operation is successful zero is returned, 244otherwise an appropriate error code is returned. 245.It Fo vn_rdwr 246.Fa "rw" "vp" "base" "len" "offset" "segflg" "ioflg" 247.Fa "cred" "aresid" "p" 248.Fc 249Common code to package up an I/O request on a vnode into a uio and 250then perform the I/O. The argument 251.Fa rw 252specifies whether the I/O is a read (UIO_READ) or write (UIO_WRITE) 253operation. The unlocked vnode is specified by 254.Fa vp . 255The arguments 256.Fa p 257and 258.Fa cred 259are the calling process and its credentials. The remaining arguments 260specify the uio parameters. For further information on these 261parameters see 262.Xr uiomove 9 . 263.It Fn vn_read "fp" "offset" "uio" "cred" "flags" 264Common code for a file table vnode read. The argument 265.Fa fp 266is the file structure, The argument 267.Fa offset 268is the offset into the file. The argument 269.Fa uio 270is the uio structure describing the memory to read into. The caller's 271credentials are specified in 272.Fa cred . 273The 274.Fa flags 275argument can define FOF_UPDATE_OFFSET to update the read position in 276the file. If the operation is successful zero is returned, otherwise 277an appropriate error is returned. 278.It Fo vn_readdir 279.Fa "fp" "buf" "segflg" "count" "done" "p" "cookies" 280.Fa "ncookies" 281.Fc 282Common code for reading the contents of a directory. The argument 283.Fa fp 284is the file structure, 285.Fa buf 286is the buffer for placing the struct dirent structures. 287The arguments 288.Fa cookies 289and 290.Fa ncookies 291specify the addresses for the list and number of directory seek 292cookies generated for NFS. Both 293.Fa cookies 294and 295.Fa ncookies 296should be NULL is they aren't required to be returned by 297.Fn vn_readdir . 298If the operation is successful zero is returned, otherwise an 299appropriate error code is returned. 300.It Fn vn_poll "fp" "events" "p" 301Common code for a file table vnode poll operation. 302.Fn vn_poll 303simply calls 304.Xr VOP_POLL 9 305with the events 306.Fa events 307and the calling process 308.Fa p . 309If the operation is success zero is returned, otherwise an appropriate 310error code is returned. 311.It Fn vn_stat "fdata" "sb" "p" 312Common code for a vnode stat operation. The vnode is specified by the 313argument 314.Fa fdata 315and 316.Fa sb 317is the buffer to return the stat information. The argument 318.Fa p 319is the calling process. 320.Fn vn_stat 321basically calls the vnode operation 322.Xr VOP_GETATTR 9 323and transfer the contents of a vattr structure into a struct stat. 324If the operation is successful zero is returned, otherwise an 325appropriate error code is returned. 326.It Fn vn_write "fp" "offset" "uio" "cred" "flags" 327Common code for a file table vnode write. The argument 328.Fa fp 329is the file structure, The argument 330.Fa offset 331is the offset into the file. The argument 332.Fa uio 333is the uio structure describing the memory to read from. The caller's 334credentials are specified in 335.Fa cred . 336The 337.Fa flags 338argument can define FOF_UPDATE_OFFSET to update the read position in 339the file. If the operation is successful zero is returned, otherwise 340an appropriate error is returned. 341.It Fn vn_writechk "vp" 342Common code to check for write permission on the vnode 343.Fa vp . 344A vnode is read-only if it is in use as a process's text image. 345If the vnode is read-only ETEXTBSY is returned, otherwise zero is 346returned to indicate that the vnode can be written to. 347.El 348.Sh ERRORS 349.Bl -tag -width Er 350.It Bq Er ETXTBSY 351Cannot write to a vnode since is a process's text image. 352.It Bq Er ENOENT 353The vnode has been reclaimed and is dead. This error is only returned 354if the LK_RETRY flag is not passed to 355.Fn vn_lock . 356.It Bq Er EBUSY 357The LK_NOWAIT flag was set and 358.Fn vn_lock 359would have slept. 360.El 361.Sh CODE REFERENCES 362This section describes places within the 363.Nx 364source tree where actual code implementing or utilising the vnode 365framework can be found. All pathnames are relative to 366.Pa /usr/src . 367.Pp 368The high-level convenience functions are implemented within the file 369.Pa sys/kern/vfs_vnops.c . 370.Sh SEE ALSO 371.Xr intro 9 , 372.Xr lock 9 , 373.Xr namei 9 , 374.Xr vattr 9 , 375.Xr vfs 9 , 376.Xr vnode 9 , 377.Xr vnodeops 9 378