Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.31.2.1
      1  1.31.2.1       chs /*	$NetBSD: vfs_vnops.c,v 1.31.2.1 1998/11/09 06:06:33 chs Exp $	*/
      2      1.12       cgd 
      3      1.10       cgd /*
      4      1.11   mycroft  * Copyright (c) 1982, 1986, 1989, 1993
      5      1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
      6      1.10       cgd  * (c) UNIX System Laboratories, Inc.
      7      1.10       cgd  * All or some portions of this file are derived from material licensed
      8      1.10       cgd  * to the University of California by American Telephone and Telegraph
      9      1.10       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10      1.10       cgd  * the permission of UNIX System Laboratories, Inc.
     11      1.10       cgd  *
     12      1.10       cgd  * Redistribution and use in source and binary forms, with or without
     13      1.10       cgd  * modification, are permitted provided that the following conditions
     14      1.10       cgd  * are met:
     15      1.10       cgd  * 1. Redistributions of source code must retain the above copyright
     16      1.10       cgd  *    notice, this list of conditions and the following disclaimer.
     17      1.10       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18      1.10       cgd  *    notice, this list of conditions and the following disclaimer in the
     19      1.10       cgd  *    documentation and/or other materials provided with the distribution.
     20      1.10       cgd  * 3. All advertising materials mentioning features or use of this software
     21      1.10       cgd  *    must display the following acknowledgement:
     22      1.10       cgd  *	This product includes software developed by the University of
     23      1.10       cgd  *	California, Berkeley and its contributors.
     24      1.10       cgd  * 4. Neither the name of the University nor the names of its contributors
     25      1.10       cgd  *    may be used to endorse or promote products derived from this software
     26      1.10       cgd  *    without specific prior written permission.
     27      1.10       cgd  *
     28      1.10       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29      1.10       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30      1.10       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31      1.10       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32      1.10       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33      1.10       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34      1.10       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35      1.10       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36      1.10       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37      1.10       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38      1.10       cgd  * SUCH DAMAGE.
     39      1.10       cgd  *
     40      1.28      fvdl  *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
     41      1.10       cgd  */
     42      1.26       mrg 
     43      1.27   thorpej #include "fs_union.h"
     44      1.26       mrg #include "opt_uvm.h"
     45      1.10       cgd 
     46      1.10       cgd #include <sys/param.h>
     47      1.10       cgd #include <sys/systm.h>
     48      1.10       cgd #include <sys/kernel.h>
     49      1.10       cgd #include <sys/file.h>
     50      1.10       cgd #include <sys/stat.h>
     51      1.10       cgd #include <sys/buf.h>
     52      1.10       cgd #include <sys/proc.h>
     53      1.10       cgd #include <sys/mount.h>
     54      1.10       cgd #include <sys/namei.h>
     55      1.10       cgd #include <sys/vnode.h>
     56      1.10       cgd #include <sys/ioctl.h>
     57      1.10       cgd #include <sys/tty.h>
     58      1.21   mycroft #include <sys/poll.h>
     59      1.10       cgd 
     60      1.11   mycroft #include <vm/vm.h>
     61      1.11   mycroft 
     62      1.25       mrg #if defined(UVM)
     63      1.25       mrg #include <uvm/uvm_extern.h>
     64      1.25       mrg #endif
     65      1.25       mrg 
     66      1.28      fvdl #ifdef UNION
     67      1.28      fvdl #include <miscfs/union/union.h>
     68      1.28      fvdl #endif
     69      1.28      fvdl 
     70      1.10       cgd struct 	fileops vnops =
     71      1.21   mycroft 	{ vn_read, vn_write, vn_ioctl, vn_poll, vn_closefile };
     72      1.10       cgd 
     73      1.10       cgd /*
     74      1.10       cgd  * Common code for vnode open operations.
     75      1.10       cgd  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     76      1.10       cgd  */
     77      1.20  christos int
     78      1.18   mycroft vn_open(ndp, fmode, cmode)
     79      1.10       cgd 	register struct nameidata *ndp;
     80      1.10       cgd 	int fmode, cmode;
     81      1.10       cgd {
     82      1.10       cgd 	register struct vnode *vp;
     83      1.11   mycroft 	register struct proc *p = ndp->ni_cnd.cn_proc;
     84      1.10       cgd 	register struct ucred *cred = p->p_ucred;
     85      1.19   mycroft 	struct vattr va;
     86      1.10       cgd 	int error;
     87      1.10       cgd 
     88      1.10       cgd 	if (fmode & O_CREAT) {
     89      1.11   mycroft 		ndp->ni_cnd.cn_nameiop = CREATE;
     90      1.11   mycroft 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
     91      1.10       cgd 		if ((fmode & O_EXCL) == 0)
     92      1.11   mycroft 			ndp->ni_cnd.cn_flags |= FOLLOW;
     93      1.20  christos 		if ((error = namei(ndp)) != 0)
     94      1.10       cgd 			return (error);
     95      1.10       cgd 		if (ndp->ni_vp == NULL) {
     96      1.19   mycroft 			VATTR_NULL(&va);
     97      1.19   mycroft 			va.va_type = VREG;
     98      1.19   mycroft 			va.va_mode = cmode;
     99      1.28      fvdl 			if (fmode & O_EXCL)
    100      1.28      fvdl 				 va.va_vaflags |= VA_EXCLUSIVE;
    101      1.17   mycroft 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
    102      1.20  christos 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
    103      1.20  christos 					   &ndp->ni_cnd, &va);
    104      1.20  christos 			if (error)
    105      1.10       cgd 				return (error);
    106      1.10       cgd 			fmode &= ~O_TRUNC;
    107      1.10       cgd 			vp = ndp->ni_vp;
    108      1.10       cgd 		} else {
    109      1.11   mycroft 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    110      1.10       cgd 			if (ndp->ni_dvp == ndp->ni_vp)
    111      1.10       cgd 				vrele(ndp->ni_dvp);
    112      1.10       cgd 			else
    113      1.10       cgd 				vput(ndp->ni_dvp);
    114      1.10       cgd 			ndp->ni_dvp = NULL;
    115      1.10       cgd 			vp = ndp->ni_vp;
    116      1.10       cgd 			if (fmode & O_EXCL) {
    117      1.10       cgd 				error = EEXIST;
    118      1.10       cgd 				goto bad;
    119      1.10       cgd 			}
    120      1.10       cgd 			fmode &= ~O_CREAT;
    121      1.10       cgd 		}
    122      1.10       cgd 	} else {
    123      1.11   mycroft 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    124      1.11   mycroft 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
    125      1.20  christos 		if ((error = namei(ndp)) != 0)
    126      1.10       cgd 			return (error);
    127      1.10       cgd 		vp = ndp->ni_vp;
    128      1.10       cgd 	}
    129      1.10       cgd 	if (vp->v_type == VSOCK) {
    130      1.10       cgd 		error = EOPNOTSUPP;
    131      1.10       cgd 		goto bad;
    132      1.10       cgd 	}
    133      1.10       cgd 	if ((fmode & O_CREAT) == 0) {
    134      1.10       cgd 		if (fmode & FREAD) {
    135      1.20  christos 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
    136      1.10       cgd 				goto bad;
    137      1.10       cgd 		}
    138      1.10       cgd 		if (fmode & (FWRITE | O_TRUNC)) {
    139      1.10       cgd 			if (vp->v_type == VDIR) {
    140      1.10       cgd 				error = EISDIR;
    141      1.10       cgd 				goto bad;
    142      1.10       cgd 			}
    143      1.20  christos 			if ((error = vn_writechk(vp)) != 0 ||
    144      1.20  christos 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
    145      1.10       cgd 				goto bad;
    146      1.10       cgd 		}
    147      1.10       cgd 	}
    148      1.10       cgd 	if (fmode & O_TRUNC) {
    149      1.28      fvdl 		VOP_UNLOCK(vp, 0);			/* XXX */
    150      1.17   mycroft 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
    151      1.28      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
    152      1.19   mycroft 		VATTR_NULL(&va);
    153      1.19   mycroft 		va.va_size = 0;
    154      1.20  christos 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
    155      1.10       cgd 			goto bad;
    156      1.10       cgd 	}
    157      1.20  christos 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
    158      1.10       cgd 		goto bad;
    159      1.10       cgd 	if (fmode & FWRITE)
    160      1.10       cgd 		vp->v_writecount++;
    161  1.31.2.1       chs 
    162  1.31.2.1       chs #ifdef UBC
    163  1.31.2.1       chs 	if (vp->v_type == VREG &&
    164  1.31.2.1       chs 	    uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
    165  1.31.2.1       chs 		error = EIO;
    166  1.31.2.1       chs 		goto bad;
    167  1.31.2.1       chs 	}
    168  1.31.2.1       chs #endif
    169      1.10       cgd 	return (0);
    170      1.10       cgd bad:
    171      1.10       cgd 	vput(vp);
    172      1.10       cgd 	return (error);
    173      1.10       cgd }
    174      1.10       cgd 
    175      1.10       cgd /*
    176      1.10       cgd  * Check for write permissions on the specified vnode.
    177      1.28      fvdl  * Prototype text segments cannot be written.
    178      1.10       cgd  */
    179      1.20  christos int
    180      1.10       cgd vn_writechk(vp)
    181      1.10       cgd 	register struct vnode *vp;
    182      1.10       cgd {
    183      1.10       cgd 
    184      1.10       cgd 	/*
    185      1.10       cgd 	 * If there's shared text associated with
    186      1.10       cgd 	 * the vnode, try to free it up once.  If
    187      1.10       cgd 	 * we fail, we can't allow writing.
    188      1.10       cgd 	 */
    189      1.25       mrg #if defined(UVM)
    190      1.25       mrg 	if ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp))
    191      1.25       mrg 		return (ETXTBSY);
    192      1.25       mrg #else
    193      1.10       cgd 	if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
    194      1.10       cgd 		return (ETXTBSY);
    195      1.25       mrg #endif
    196      1.10       cgd 	return (0);
    197      1.10       cgd }
    198      1.10       cgd 
    199      1.10       cgd /*
    200      1.10       cgd  * Vnode close call
    201      1.10       cgd  */
    202      1.20  christos int
    203      1.10       cgd vn_close(vp, flags, cred, p)
    204      1.10       cgd 	register struct vnode *vp;
    205      1.10       cgd 	int flags;
    206      1.10       cgd 	struct ucred *cred;
    207      1.10       cgd 	struct proc *p;
    208      1.10       cgd {
    209      1.10       cgd 	int error;
    210      1.10       cgd 
    211      1.10       cgd 	if (flags & FWRITE)
    212      1.10       cgd 		vp->v_writecount--;
    213      1.10       cgd 	error = VOP_CLOSE(vp, flags, cred, p);
    214      1.10       cgd 	vrele(vp);
    215      1.10       cgd 	return (error);
    216      1.10       cgd }
    217      1.10       cgd 
    218      1.10       cgd /*
    219      1.10       cgd  * Package up an I/O request on a vnode into a uio and do it.
    220      1.10       cgd  */
    221      1.20  christos int
    222      1.10       cgd vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
    223      1.10       cgd 	enum uio_rw rw;
    224      1.10       cgd 	struct vnode *vp;
    225      1.10       cgd 	caddr_t base;
    226      1.10       cgd 	int len;
    227      1.10       cgd 	off_t offset;
    228      1.10       cgd 	enum uio_seg segflg;
    229      1.10       cgd 	int ioflg;
    230      1.10       cgd 	struct ucred *cred;
    231      1.30   thorpej 	size_t *aresid;
    232      1.10       cgd 	struct proc *p;
    233      1.10       cgd {
    234      1.10       cgd 	struct uio auio;
    235      1.10       cgd 	struct iovec aiov;
    236      1.10       cgd 	int error;
    237      1.10       cgd 
    238      1.10       cgd 	if ((ioflg & IO_NODELOCKED) == 0)
    239      1.28      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    240      1.10       cgd 	auio.uio_iov = &aiov;
    241      1.10       cgd 	auio.uio_iovcnt = 1;
    242      1.10       cgd 	aiov.iov_base = base;
    243      1.10       cgd 	aiov.iov_len = len;
    244      1.10       cgd 	auio.uio_resid = len;
    245      1.10       cgd 	auio.uio_offset = offset;
    246      1.10       cgd 	auio.uio_segflg = segflg;
    247      1.10       cgd 	auio.uio_rw = rw;
    248      1.10       cgd 	auio.uio_procp = p;
    249      1.11   mycroft 	if (rw == UIO_READ) {
    250      1.10       cgd 		error = VOP_READ(vp, &auio, ioflg, cred);
    251      1.11   mycroft 	} else {
    252      1.10       cgd 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    253      1.11   mycroft 	}
    254      1.10       cgd 	if (aresid)
    255      1.10       cgd 		*aresid = auio.uio_resid;
    256      1.10       cgd 	else
    257      1.10       cgd 		if (auio.uio_resid && error == 0)
    258      1.10       cgd 			error = EIO;
    259      1.10       cgd 	if ((ioflg & IO_NODELOCKED) == 0)
    260      1.28      fvdl 		VOP_UNLOCK(vp, 0);
    261      1.10       cgd 	return (error);
    262      1.23      fvdl }
    263      1.23      fvdl 
    264      1.23      fvdl int
    265      1.23      fvdl vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
    266      1.23      fvdl 	struct file *fp;
    267      1.23      fvdl 	char *buf;
    268      1.28      fvdl 	int segflg, *done, *ncookies;
    269      1.23      fvdl 	u_int count;
    270      1.23      fvdl 	struct proc *p;
    271      1.28      fvdl 	off_t **cookies;
    272      1.23      fvdl {
    273      1.23      fvdl 	struct vnode *vp = (struct vnode *)fp->f_data;
    274      1.23      fvdl 	struct iovec aiov;
    275      1.23      fvdl 	struct uio auio;
    276      1.23      fvdl 	int error, eofflag;
    277      1.23      fvdl 
    278      1.23      fvdl unionread:
    279      1.23      fvdl 	if (vp->v_type != VDIR)
    280      1.23      fvdl 		return (EINVAL);
    281      1.23      fvdl 	aiov.iov_base = buf;
    282      1.23      fvdl 	aiov.iov_len = count;
    283      1.23      fvdl 	auio.uio_iov = &aiov;
    284      1.23      fvdl 	auio.uio_iovcnt = 1;
    285      1.23      fvdl 	auio.uio_rw = UIO_READ;
    286      1.23      fvdl 	auio.uio_segflg = segflg;
    287      1.23      fvdl 	auio.uio_procp = p;
    288      1.23      fvdl 	auio.uio_resid = count;
    289      1.28      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    290      1.23      fvdl 	auio.uio_offset = fp->f_offset;
    291      1.28      fvdl 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    292      1.23      fvdl 		    ncookies);
    293      1.23      fvdl 	fp->f_offset = auio.uio_offset;
    294      1.28      fvdl 	VOP_UNLOCK(vp, 0);
    295      1.23      fvdl 	if (error)
    296      1.23      fvdl 		return (error);
    297      1.23      fvdl 
    298      1.23      fvdl #ifdef UNION
    299      1.23      fvdl {
    300      1.23      fvdl 	extern int (**union_vnodeop_p) __P((void *));
    301      1.23      fvdl 	extern struct vnode *union_dircache __P((struct vnode *));
    302      1.23      fvdl 
    303      1.23      fvdl 	if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
    304      1.23      fvdl 		struct vnode *lvp;
    305      1.23      fvdl 
    306      1.23      fvdl 		lvp = union_dircache(vp);
    307      1.23      fvdl 		if (lvp != NULLVP) {
    308      1.23      fvdl 			struct vattr va;
    309      1.23      fvdl 
    310      1.23      fvdl 			/*
    311      1.23      fvdl 			 * If the directory is opaque,
    312      1.23      fvdl 			 * then don't show lower entries
    313      1.23      fvdl 			 */
    314      1.23      fvdl 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    315      1.23      fvdl 			if (va.va_flags & OPAQUE) {
    316      1.23      fvdl 				vput(lvp);
    317      1.23      fvdl 				lvp = NULL;
    318      1.23      fvdl 			}
    319      1.23      fvdl 		}
    320      1.23      fvdl 
    321      1.23      fvdl 		if (lvp != NULLVP) {
    322      1.23      fvdl 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    323      1.23      fvdl 			if (error) {
    324      1.28      fvdl 				vput(lvp);
    325      1.23      fvdl 				return (error);
    326      1.23      fvdl 			}
    327      1.28      fvdl 			VOP_UNLOCK(lvp, 0);
    328      1.23      fvdl 			fp->f_data = (caddr_t) lvp;
    329      1.23      fvdl 			fp->f_offset = 0;
    330      1.23      fvdl 			error = vn_close(vp, FREAD, fp->f_cred, p);
    331      1.23      fvdl 			if (error)
    332      1.23      fvdl 				return (error);
    333      1.23      fvdl 			vp = lvp;
    334      1.23      fvdl 			goto unionread;
    335      1.23      fvdl 		}
    336      1.23      fvdl 	}
    337      1.23      fvdl }
    338      1.23      fvdl #endif /* UNION */
    339      1.23      fvdl 
    340      1.23      fvdl 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    341      1.23      fvdl 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    342      1.23      fvdl 		struct vnode *tvp = vp;
    343      1.23      fvdl 		vp = vp->v_mount->mnt_vnodecovered;
    344      1.23      fvdl 		VREF(vp);
    345      1.23      fvdl 		fp->f_data = (caddr_t) vp;
    346      1.23      fvdl 		fp->f_offset = 0;
    347      1.23      fvdl 		vrele(tvp);
    348      1.23      fvdl 		goto unionread;
    349      1.23      fvdl 	}
    350      1.23      fvdl 	*done = count - auio.uio_resid;
    351      1.23      fvdl 	return error;
    352      1.10       cgd }
    353      1.10       cgd 
    354      1.10       cgd /*
    355      1.10       cgd  * File table vnode read routine.
    356      1.10       cgd  */
    357      1.20  christos int
    358      1.29   thorpej vn_read(fp, offset, uio, cred, flags)
    359      1.10       cgd 	struct file *fp;
    360      1.29   thorpej 	off_t *offset;
    361      1.10       cgd 	struct uio *uio;
    362      1.10       cgd 	struct ucred *cred;
    363      1.29   thorpej 	int flags;
    364      1.10       cgd {
    365      1.28      fvdl 	struct vnode *vp = (struct vnode *)fp->f_data;
    366      1.31    kleink 	int count, error, ioflag = 0;
    367      1.10       cgd 
    368      1.17   mycroft 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    369      1.31    kleink 	if (fp->f_flag & FNONBLOCK)
    370      1.31    kleink 		ioflag |= IO_NDELAY;
    371      1.31    kleink 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    372      1.31    kleink 		ioflag |= IO_SYNC;
    373      1.28      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    374      1.29   thorpej 	uio->uio_offset = *offset;
    375      1.10       cgd 	count = uio->uio_resid;
    376      1.31    kleink 	error = VOP_READ(vp, uio, ioflag, cred);
    377      1.29   thorpej 	if (flags & FOF_UPDATE_OFFSET)
    378      1.29   thorpej 		*offset += count - uio->uio_resid;
    379      1.28      fvdl 	VOP_UNLOCK(vp, 0);
    380      1.10       cgd 	return (error);
    381      1.10       cgd }
    382      1.10       cgd 
    383      1.10       cgd /*
    384      1.10       cgd  * File table vnode write routine.
    385      1.10       cgd  */
    386      1.20  christos int
    387      1.29   thorpej vn_write(fp, offset, uio, cred, flags)
    388      1.10       cgd 	struct file *fp;
    389      1.29   thorpej 	off_t *offset;
    390      1.10       cgd 	struct uio *uio;
    391      1.10       cgd 	struct ucred *cred;
    392      1.29   thorpej 	int flags;
    393      1.10       cgd {
    394      1.28      fvdl 	struct vnode *vp = (struct vnode *)fp->f_data;
    395      1.17   mycroft 	int count, error, ioflag = IO_UNIT;
    396      1.10       cgd 
    397      1.10       cgd 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    398      1.10       cgd 		ioflag |= IO_APPEND;
    399      1.10       cgd 	if (fp->f_flag & FNONBLOCK)
    400      1.10       cgd 		ioflag |= IO_NDELAY;
    401      1.24   thorpej 	if (fp->f_flag & FFSYNC ||
    402      1.24   thorpej 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    403      1.24   thorpej 		ioflag |= IO_SYNC;
    404      1.31    kleink 	else if (fp->f_flag & FDSYNC)
    405      1.31    kleink 		ioflag |= IO_DSYNC;
    406      1.17   mycroft 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    407      1.28      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    408      1.29   thorpej 	uio->uio_offset = *offset;
    409      1.10       cgd 	count = uio->uio_resid;
    410      1.10       cgd 	error = VOP_WRITE(vp, uio, ioflag, cred);
    411      1.29   thorpej 	if (flags & FOF_UPDATE_OFFSET) {
    412      1.29   thorpej 		if (ioflag & IO_APPEND)
    413      1.29   thorpej 			*offset = uio->uio_offset;
    414      1.29   thorpej 		else
    415      1.29   thorpej 			*offset += count - uio->uio_resid;
    416      1.29   thorpej 	}
    417      1.28      fvdl 	VOP_UNLOCK(vp, 0);
    418      1.10       cgd 	return (error);
    419      1.10       cgd }
    420      1.10       cgd 
    421      1.10       cgd /*
    422      1.10       cgd  * File table vnode stat routine.
    423      1.10       cgd  */
    424      1.20  christos int
    425      1.10       cgd vn_stat(vp, sb, p)
    426      1.10       cgd 	struct vnode *vp;
    427      1.10       cgd 	register struct stat *sb;
    428      1.10       cgd 	struct proc *p;
    429      1.10       cgd {
    430      1.19   mycroft 	struct vattr va;
    431      1.10       cgd 	int error;
    432      1.10       cgd 	u_short mode;
    433      1.10       cgd 
    434      1.19   mycroft 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    435      1.10       cgd 	if (error)
    436      1.10       cgd 		return (error);
    437      1.10       cgd 	/*
    438      1.10       cgd 	 * Copy from vattr table
    439      1.10       cgd 	 */
    440      1.19   mycroft 	sb->st_dev = va.va_fsid;
    441      1.19   mycroft 	sb->st_ino = va.va_fileid;
    442      1.19   mycroft 	mode = va.va_mode;
    443      1.10       cgd 	switch (vp->v_type) {
    444      1.10       cgd 	case VREG:
    445      1.10       cgd 		mode |= S_IFREG;
    446      1.10       cgd 		break;
    447      1.10       cgd 	case VDIR:
    448      1.10       cgd 		mode |= S_IFDIR;
    449      1.10       cgd 		break;
    450      1.10       cgd 	case VBLK:
    451      1.10       cgd 		mode |= S_IFBLK;
    452      1.10       cgd 		break;
    453      1.10       cgd 	case VCHR:
    454      1.10       cgd 		mode |= S_IFCHR;
    455      1.10       cgd 		break;
    456      1.10       cgd 	case VLNK:
    457      1.10       cgd 		mode |= S_IFLNK;
    458      1.10       cgd 		break;
    459      1.10       cgd 	case VSOCK:
    460      1.10       cgd 		mode |= S_IFSOCK;
    461      1.10       cgd 		break;
    462      1.10       cgd 	case VFIFO:
    463      1.10       cgd 		mode |= S_IFIFO;
    464      1.10       cgd 		break;
    465      1.10       cgd 	default:
    466      1.10       cgd 		return (EBADF);
    467      1.10       cgd 	};
    468      1.10       cgd 	sb->st_mode = mode;
    469      1.19   mycroft 	sb->st_nlink = va.va_nlink;
    470      1.19   mycroft 	sb->st_uid = va.va_uid;
    471      1.19   mycroft 	sb->st_gid = va.va_gid;
    472      1.19   mycroft 	sb->st_rdev = va.va_rdev;
    473      1.19   mycroft 	sb->st_size = va.va_size;
    474      1.19   mycroft 	sb->st_atimespec = va.va_atime;
    475      1.19   mycroft 	sb->st_mtimespec = va.va_mtime;
    476      1.19   mycroft 	sb->st_ctimespec = va.va_ctime;
    477      1.19   mycroft 	sb->st_blksize = va.va_blocksize;
    478      1.19   mycroft 	sb->st_flags = va.va_flags;
    479      1.22   mycroft 	sb->st_gen = 0;
    480      1.19   mycroft 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    481      1.10       cgd 	return (0);
    482      1.10       cgd }
    483      1.10       cgd 
    484      1.10       cgd /*
    485      1.10       cgd  * File table vnode ioctl routine.
    486      1.10       cgd  */
    487      1.20  christos int
    488      1.10       cgd vn_ioctl(fp, com, data, p)
    489      1.10       cgd 	struct file *fp;
    490      1.15       cgd 	u_long com;
    491      1.10       cgd 	caddr_t data;
    492      1.10       cgd 	struct proc *p;
    493      1.10       cgd {
    494      1.10       cgd 	register struct vnode *vp = ((struct vnode *)fp->f_data);
    495      1.10       cgd 	struct vattr vattr;
    496      1.10       cgd 	int error;
    497      1.10       cgd 
    498      1.10       cgd 	switch (vp->v_type) {
    499      1.10       cgd 
    500      1.10       cgd 	case VREG:
    501      1.10       cgd 	case VDIR:
    502      1.10       cgd 		if (com == FIONREAD) {
    503      1.20  christos 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    504      1.20  christos 			if (error)
    505      1.10       cgd 				return (error);
    506      1.10       cgd 			*(int *)data = vattr.va_size - fp->f_offset;
    507      1.10       cgd 			return (0);
    508      1.10       cgd 		}
    509      1.10       cgd 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    510      1.10       cgd 			return (0);			/* XXX */
    511      1.10       cgd 		/* fall into ... */
    512      1.10       cgd 
    513      1.10       cgd 	default:
    514      1.10       cgd 		return (ENOTTY);
    515      1.10       cgd 
    516      1.10       cgd 	case VFIFO:
    517      1.10       cgd 	case VCHR:
    518      1.10       cgd 	case VBLK:
    519      1.10       cgd 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    520      1.10       cgd 		if (error == 0 && com == TIOCSCTTY) {
    521      1.13       cgd 			if (p->p_session->s_ttyvp)
    522      1.13       cgd 				vrele(p->p_session->s_ttyvp);
    523      1.10       cgd 			p->p_session->s_ttyvp = vp;
    524      1.10       cgd 			VREF(vp);
    525      1.10       cgd 		}
    526      1.10       cgd 		return (error);
    527      1.10       cgd 	}
    528      1.10       cgd }
    529      1.10       cgd 
    530      1.10       cgd /*
    531      1.21   mycroft  * File table vnode poll routine.
    532      1.10       cgd  */
    533      1.20  christos int
    534      1.21   mycroft vn_poll(fp, events, p)
    535      1.10       cgd 	struct file *fp;
    536      1.21   mycroft 	int events;
    537      1.10       cgd 	struct proc *p;
    538      1.10       cgd {
    539      1.10       cgd 
    540      1.21   mycroft 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    541      1.28      fvdl }
    542      1.28      fvdl 
    543      1.28      fvdl /*
    544      1.28      fvdl  * Check that the vnode is still valid, and if so
    545      1.28      fvdl  * acquire requested lock.
    546      1.28      fvdl  */
    547      1.28      fvdl int
    548      1.28      fvdl vn_lock(vp, flags)
    549      1.28      fvdl 	struct vnode *vp;
    550      1.28      fvdl 	int flags;
    551      1.28      fvdl {
    552      1.28      fvdl 	int error;
    553      1.28      fvdl 
    554      1.28      fvdl 	do {
    555      1.28      fvdl 		if ((flags & LK_INTERLOCK) == 0)
    556      1.28      fvdl 			simple_lock(&vp->v_interlock);
    557      1.28      fvdl 		if (vp->v_flag & VXLOCK) {
    558      1.28      fvdl 			vp->v_flag |= VXWANT;
    559      1.28      fvdl 			simple_unlock(&vp->v_interlock);
    560      1.28      fvdl 			tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
    561      1.28      fvdl 			error = ENOENT;
    562      1.28      fvdl 		} else {
    563      1.28      fvdl 			error = VOP_LOCK(vp, flags | LK_INTERLOCK);
    564      1.28      fvdl 			if (error == 0)
    565      1.28      fvdl 				return (error);
    566      1.28      fvdl 		}
    567      1.28      fvdl 		flags &= ~LK_INTERLOCK;
    568      1.28      fvdl 	} while (flags & LK_RETRY);
    569      1.28      fvdl 	return (error);
    570      1.10       cgd }
    571      1.10       cgd 
    572      1.10       cgd /*
    573      1.10       cgd  * File table vnode close routine.
    574      1.10       cgd  */
    575      1.20  christos int
    576      1.10       cgd vn_closefile(fp, p)
    577      1.10       cgd 	struct file *fp;
    578      1.10       cgd 	struct proc *p;
    579      1.10       cgd {
    580      1.10       cgd 
    581      1.10       cgd 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    582      1.10       cgd 		fp->f_cred, p));
    583      1.10       cgd }
    584