Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.169.4.4
      1 /*	$NetBSD: vfs_vnops.c,v 1.169.4.4 2011/03/05 20:55:27 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  * (c) UNIX System Laboratories, Inc.
     36  * All or some portions of this file are derived from material licensed
     37  * to the University of California by American Telephone and Telegraph
     38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39  * the permission of UNIX System Laboratories, Inc.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.169.4.4 2011/03/05 20:55:27 rmind Exp $");
     70 
     71 #include "veriexec.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/kernel.h>
     76 #include <sys/file.h>
     77 #include <sys/stat.h>
     78 #include <sys/buf.h>
     79 #include <sys/proc.h>
     80 #include <sys/mount.h>
     81 #include <sys/namei.h>
     82 #include <sys/vnode.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/tty.h>
     85 #include <sys/poll.h>
     86 #include <sys/kauth.h>
     87 #include <sys/syslog.h>
     88 #include <sys/fstrans.h>
     89 #include <sys/atomic.h>
     90 #include <sys/filedesc.h>
     91 #include <sys/wapbl.h>
     92 
     93 #include <miscfs/specfs/specdev.h>
     94 #include <miscfs/fifofs/fifo.h>
     95 
     96 #include <uvm/uvm_extern.h>
     97 #include <uvm/uvm_readahead.h>
     98 
     99 #ifdef UNION
    100 #include <fs/union/union.h>
    101 #endif
    102 
    103 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
    104 
    105 #include <sys/verified_exec.h>
    106 
    107 static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
    108 	    kauth_cred_t cred, int flags);
    109 static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
    110 	    kauth_cred_t cred, int flags);
    111 static int vn_closefile(file_t *fp);
    112 static int vn_poll(file_t *fp, int events);
    113 static int vn_fcntl(file_t *fp, u_int com, void *data);
    114 static int vn_statfile(file_t *fp, struct stat *sb);
    115 static int vn_ioctl(file_t *fp, u_long com, void *data);
    116 
    117 const struct fileops vnops = {
    118 	.fo_read = vn_read,
    119 	.fo_write = vn_write,
    120 	.fo_ioctl = vn_ioctl,
    121 	.fo_fcntl = vn_fcntl,
    122 	.fo_poll = vn_poll,
    123 	.fo_stat = vn_statfile,
    124 	.fo_close = vn_closefile,
    125 	.fo_kqfilter = vn_kqfilter,
    126 	.fo_restart = fnullop_restart,
    127 };
    128 
    129 /*
    130  * Common code for vnode open operations.
    131  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
    132  */
    133 int
    134 vn_open(struct nameidata *ndp, int fmode, int cmode)
    135 {
    136 	struct vnode *vp;
    137 	struct lwp *l = curlwp;
    138 	kauth_cred_t cred = l->l_cred;
    139 	struct vattr va;
    140 	int error;
    141 	const char *pathstring;
    142 
    143 	if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY))
    144 		return EINVAL;
    145 
    146 	ndp->ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT;
    147 
    148 	if (fmode & O_CREAT) {
    149 		ndp->ni_cnd.cn_nameiop = CREATE;
    150 		ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
    151 		if ((fmode & O_EXCL) == 0 &&
    152 		    ((fmode & O_NOFOLLOW) == 0))
    153 			ndp->ni_cnd.cn_flags |= FOLLOW;
    154 	} else {
    155 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    156 		ndp->ni_cnd.cn_flags |= LOCKLEAF;
    157 		if ((fmode & O_NOFOLLOW) == 0)
    158 			ndp->ni_cnd.cn_flags |= FOLLOW;
    159 	}
    160 
    161 	pathstring = pathbuf_stringcopy_get(ndp->ni_pathbuf);
    162 	if (pathstring == NULL) {
    163 		return ENOMEM;
    164 	}
    165 
    166 	error = namei(ndp);
    167 	if (error)
    168 		goto out;
    169 
    170 	vp = ndp->ni_vp;
    171 
    172 #if NVERIEXEC > 0
    173 	error = veriexec_openchk(l, ndp->ni_vp, pathstring, fmode);
    174 	if (error)
    175 		goto bad;
    176 #endif /* NVERIEXEC > 0 */
    177 
    178 	if (fmode & O_CREAT) {
    179 		if (ndp->ni_vp == NULL) {
    180 			vattr_null(&va);
    181 			va.va_type = VREG;
    182 			va.va_mode = cmode;
    183 			if (fmode & O_EXCL)
    184 				 va.va_vaflags |= VA_EXCLUSIVE;
    185 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
    186 					   &ndp->ni_cnd, &va);
    187 			if (error)
    188 				goto out;
    189 			fmode &= ~O_TRUNC;
    190 			vp = ndp->ni_vp;
    191 		} else {
    192 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    193 			if (ndp->ni_dvp == ndp->ni_vp)
    194 				vrele(ndp->ni_dvp);
    195 			else
    196 				vput(ndp->ni_dvp);
    197 			ndp->ni_dvp = NULL;
    198 			vp = ndp->ni_vp;
    199 			if (fmode & O_EXCL) {
    200 				error = EEXIST;
    201 				goto bad;
    202 			}
    203 			fmode &= ~O_CREAT;
    204 		}
    205 	} else {
    206 		vp = ndp->ni_vp;
    207 	}
    208 	if (vp->v_type == VSOCK) {
    209 		error = EOPNOTSUPP;
    210 		goto bad;
    211 	}
    212 	if (ndp->ni_vp->v_type == VLNK) {
    213 		error = EFTYPE;
    214 		goto bad;
    215 	}
    216 
    217 	if ((fmode & O_CREAT) == 0) {
    218 		error = vn_openchk(vp, cred, fmode);
    219 		if (error != 0)
    220 			goto bad;
    221 	}
    222 
    223 	if (fmode & O_TRUNC) {
    224 		vattr_null(&va);
    225 		va.va_size = 0;
    226 		error = VOP_SETATTR(vp, &va, cred);
    227 		if (error != 0)
    228 			goto bad;
    229 	}
    230 	if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
    231 		goto bad;
    232 	if (fmode & FWRITE) {
    233 		mutex_enter(vp->v_interlock);
    234 		vp->v_writecount++;
    235 		mutex_exit(vp->v_interlock);
    236 	}
    237 
    238 bad:
    239 	if (error)
    240 		vput(vp);
    241 out:
    242 	pathbuf_stringcopy_put(ndp->ni_pathbuf, pathstring);
    243 	return (error);
    244 }
    245 
    246 /*
    247  * Check for write permissions on the specified vnode.
    248  * Prototype text segments cannot be written.
    249  */
    250 int
    251 vn_writechk(struct vnode *vp)
    252 {
    253 
    254 	/*
    255 	 * If the vnode is in use as a process's text,
    256 	 * we can't allow writing.
    257 	 */
    258 	if (vp->v_iflag & VI_TEXT)
    259 		return (ETXTBSY);
    260 	return (0);
    261 }
    262 
    263 int
    264 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
    265 {
    266 	int permbits = 0;
    267 	int error;
    268 
    269 	if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR)
    270 		return ENOTDIR;
    271 
    272 	if ((fflags & FREAD) != 0) {
    273 		permbits = VREAD;
    274 	}
    275 	if ((fflags & (FWRITE | O_TRUNC)) != 0) {
    276 		permbits |= VWRITE;
    277 		if (vp->v_type == VDIR) {
    278 			error = EISDIR;
    279 			goto bad;
    280 		}
    281 		error = vn_writechk(vp);
    282 		if (error != 0)
    283 			goto bad;
    284 	}
    285 	error = VOP_ACCESS(vp, permbits, cred);
    286 bad:
    287 	return error;
    288 }
    289 
    290 /*
    291  * Mark a vnode as having executable mappings.
    292  */
    293 void
    294 vn_markexec(struct vnode *vp)
    295 {
    296 
    297 	if ((vp->v_iflag & VI_EXECMAP) != 0) {
    298 		/* Safe unlocked, as long as caller holds a reference. */
    299 		return;
    300 	}
    301 
    302 	mutex_enter(vp->v_interlock);
    303 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    304 		atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
    305 		atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
    306 		vp->v_iflag |= VI_EXECMAP;
    307 	}
    308 	mutex_exit(vp->v_interlock);
    309 }
    310 
    311 /*
    312  * Mark a vnode as being the text of a process.
    313  * Fail if the vnode is currently writable.
    314  */
    315 int
    316 vn_marktext(struct vnode *vp)
    317 {
    318 
    319 	if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
    320 		/* Safe unlocked, as long as caller holds a reference. */
    321 		return (0);
    322 	}
    323 
    324 	mutex_enter(vp->v_interlock);
    325 	if (vp->v_writecount != 0) {
    326 		KASSERT((vp->v_iflag & VI_TEXT) == 0);
    327 		mutex_exit(vp->v_interlock);
    328 		return (ETXTBSY);
    329 	}
    330 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    331 		atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
    332 		atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
    333 	}
    334 	vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
    335 	mutex_exit(vp->v_interlock);
    336 	return (0);
    337 }
    338 
    339 /*
    340  * Vnode close call
    341  *
    342  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    343  */
    344 int
    345 vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
    346 {
    347 	int error;
    348 
    349 	if (flags & FWRITE) {
    350 		mutex_enter(vp->v_interlock);
    351 		vp->v_writecount--;
    352 		mutex_exit(vp->v_interlock);
    353 	}
    354 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    355 	error = VOP_CLOSE(vp, flags, cred);
    356 	vput(vp);
    357 	return (error);
    358 }
    359 
    360 static int
    361 enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag)
    362 {
    363 	struct lwp *l = curlwp;
    364 	off_t testoff;
    365 
    366 	if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG)
    367 		return 0;
    368 
    369 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    370 	if (ioflag & IO_APPEND)
    371 		testoff = vp->v_size;
    372 	else
    373 		testoff = uio->uio_offset;
    374 
    375 	if (testoff + uio->uio_resid >
    376 	    l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    377 		mutex_enter(proc_lock);
    378 		psignal(l->l_proc, SIGXFSZ);
    379 		mutex_exit(proc_lock);
    380 		return EFBIG;
    381 	}
    382 
    383 	return 0;
    384 }
    385 
    386 /*
    387  * Package up an I/O request on a vnode into a uio and do it.
    388  */
    389 int
    390 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
    391     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
    392     struct lwp *l)
    393 {
    394 	struct uio auio;
    395 	struct iovec aiov;
    396 	int error;
    397 
    398 	if ((ioflg & IO_NODELOCKED) == 0) {
    399 		if (rw == UIO_READ) {
    400 			vn_lock(vp, LK_SHARED | LK_RETRY);
    401 		} else /* UIO_WRITE */ {
    402 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    403 		}
    404 	}
    405 	auio.uio_iov = &aiov;
    406 	auio.uio_iovcnt = 1;
    407 	aiov.iov_base = base;
    408 	aiov.iov_len = len;
    409 	auio.uio_resid = len;
    410 	auio.uio_offset = offset;
    411 	auio.uio_rw = rw;
    412 	if (segflg == UIO_SYSSPACE) {
    413 		UIO_SETUP_SYSSPACE(&auio);
    414 	} else {
    415 		auio.uio_vmspace = l->l_proc->p_vmspace;
    416 	}
    417 
    418 	if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0)
    419 		goto out;
    420 
    421 	if (rw == UIO_READ) {
    422 		error = VOP_READ(vp, &auio, ioflg, cred);
    423 	} else {
    424 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    425 	}
    426 
    427 	if (aresid)
    428 		*aresid = auio.uio_resid;
    429 	else
    430 		if (auio.uio_resid && error == 0)
    431 			error = EIO;
    432 
    433  out:
    434 	if ((ioflg & IO_NODELOCKED) == 0) {
    435 		VOP_UNLOCK(vp);
    436 	}
    437 	return (error);
    438 }
    439 
    440 int
    441 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
    442     struct lwp *l, off_t **cookies, int *ncookies)
    443 {
    444 	struct vnode *vp = (struct vnode *)fp->f_data;
    445 	struct iovec aiov;
    446 	struct uio auio;
    447 	int error, eofflag;
    448 
    449 	/* Limit the size on any kernel buffers used by VOP_READDIR */
    450 	count = min(MAXBSIZE, count);
    451 
    452 unionread:
    453 	if (vp->v_type != VDIR)
    454 		return (EINVAL);
    455 	aiov.iov_base = bf;
    456 	aiov.iov_len = count;
    457 	auio.uio_iov = &aiov;
    458 	auio.uio_iovcnt = 1;
    459 	auio.uio_rw = UIO_READ;
    460 	if (segflg == UIO_SYSSPACE) {
    461 		UIO_SETUP_SYSSPACE(&auio);
    462 	} else {
    463 		KASSERT(l == curlwp);
    464 		auio.uio_vmspace = l->l_proc->p_vmspace;
    465 	}
    466 	auio.uio_resid = count;
    467 	vn_lock(vp, LK_SHARED | LK_RETRY);
    468 	auio.uio_offset = fp->f_offset;
    469 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    470 		    ncookies);
    471 	mutex_enter(&fp->f_lock);
    472 	fp->f_offset = auio.uio_offset;
    473 	mutex_exit(&fp->f_lock);
    474 	VOP_UNLOCK(vp);
    475 	if (error)
    476 		return (error);
    477 
    478 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    479 		struct vnode *ovp = vp;
    480 
    481 		error = (*vn_union_readdir_hook)(&vp, fp, l);
    482 		if (error)
    483 			return (error);
    484 		if (vp != ovp)
    485 			goto unionread;
    486 	}
    487 
    488 	if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
    489 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    490 		struct vnode *tvp = vp;
    491 		vp = vp->v_mount->mnt_vnodecovered;
    492 		vref(vp);
    493 		mutex_enter(&fp->f_lock);
    494 		fp->f_data = vp;
    495 		fp->f_offset = 0;
    496 		mutex_exit(&fp->f_lock);
    497 		vrele(tvp);
    498 		goto unionread;
    499 	}
    500 	*done = count - auio.uio_resid;
    501 	return error;
    502 }
    503 
    504 /*
    505  * File table vnode read routine.
    506  */
    507 static int
    508 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    509     int flags)
    510 {
    511 	struct vnode *vp = (struct vnode *)fp->f_data;
    512 	int count, error, ioflag, fflag;
    513 
    514 	ioflag = IO_ADV_ENCODE(fp->f_advice);
    515 	fflag = fp->f_flag;
    516 	if (fflag & FNONBLOCK)
    517 		ioflag |= IO_NDELAY;
    518 	if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    519 		ioflag |= IO_SYNC;
    520 	if (fflag & FALTIO)
    521 		ioflag |= IO_ALTSEMANTICS;
    522 	if (fflag & FDIRECT)
    523 		ioflag |= IO_DIRECT;
    524 	vn_lock(vp, LK_SHARED | LK_RETRY);
    525 	uio->uio_offset = *offset;
    526 	count = uio->uio_resid;
    527 	error = VOP_READ(vp, uio, ioflag, cred);
    528 	if (flags & FOF_UPDATE_OFFSET)
    529 		*offset += count - uio->uio_resid;
    530 	VOP_UNLOCK(vp);
    531 	return (error);
    532 }
    533 
    534 /*
    535  * File table vnode write routine.
    536  */
    537 static int
    538 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    539     int flags)
    540 {
    541 	struct vnode *vp = (struct vnode *)fp->f_data;
    542 	int count, error, ioflag, fflag;
    543 
    544 	ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
    545 	fflag = fp->f_flag;
    546 	if (vp->v_type == VREG && (fflag & O_APPEND))
    547 		ioflag |= IO_APPEND;
    548 	if (fflag & FNONBLOCK)
    549 		ioflag |= IO_NDELAY;
    550 	if (fflag & FFSYNC ||
    551 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    552 		ioflag |= IO_SYNC;
    553 	else if (fflag & FDSYNC)
    554 		ioflag |= IO_DSYNC;
    555 	if (fflag & FALTIO)
    556 		ioflag |= IO_ALTSEMANTICS;
    557 	if (fflag & FDIRECT)
    558 		ioflag |= IO_DIRECT;
    559 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    560 	uio->uio_offset = *offset;
    561 	count = uio->uio_resid;
    562 
    563 	if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
    564 		goto out;
    565 
    566 	error = VOP_WRITE(vp, uio, ioflag, cred);
    567 
    568 	if (flags & FOF_UPDATE_OFFSET) {
    569 		if (ioflag & IO_APPEND) {
    570 			/*
    571 			 * SUSv3 describes behaviour for count = 0 as following:
    572 			 * "Before any action ... is taken, and if nbyte is zero
    573 			 * and the file is a regular file, the write() function
    574 			 * ... in the absence of errors ... shall return zero
    575 			 * and have no other results."
    576 			 */
    577 			if (count)
    578 				*offset = uio->uio_offset;
    579 		} else
    580 			*offset += count - uio->uio_resid;
    581 	}
    582 
    583  out:
    584 	VOP_UNLOCK(vp);
    585 	return (error);
    586 }
    587 
    588 /*
    589  * File table vnode stat routine.
    590  */
    591 static int
    592 vn_statfile(file_t *fp, struct stat *sb)
    593 {
    594 	struct vnode *vp = fp->f_data;
    595 	int error;
    596 
    597 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    598 	error = vn_stat(vp, sb);
    599 	VOP_UNLOCK(vp);
    600 	return error;
    601 }
    602 
    603 int
    604 vn_stat(struct vnode *vp, struct stat *sb)
    605 {
    606 	struct vattr va;
    607 	int error;
    608 	mode_t mode;
    609 
    610 	memset(&va, 0, sizeof(va));
    611 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
    612 	if (error)
    613 		return (error);
    614 	/*
    615 	 * Copy from vattr table
    616 	 */
    617 	memset(sb, 0, sizeof(*sb));
    618 	sb->st_dev = va.va_fsid;
    619 	sb->st_ino = va.va_fileid;
    620 	mode = va.va_mode;
    621 	switch (vp->v_type) {
    622 	case VREG:
    623 		mode |= S_IFREG;
    624 		break;
    625 	case VDIR:
    626 		mode |= S_IFDIR;
    627 		break;
    628 	case VBLK:
    629 		mode |= S_IFBLK;
    630 		break;
    631 	case VCHR:
    632 		mode |= S_IFCHR;
    633 		break;
    634 	case VLNK:
    635 		mode |= S_IFLNK;
    636 		break;
    637 	case VSOCK:
    638 		mode |= S_IFSOCK;
    639 		break;
    640 	case VFIFO:
    641 		mode |= S_IFIFO;
    642 		break;
    643 	default:
    644 		return (EBADF);
    645 	};
    646 	sb->st_mode = mode;
    647 	sb->st_nlink = va.va_nlink;
    648 	sb->st_uid = va.va_uid;
    649 	sb->st_gid = va.va_gid;
    650 	sb->st_rdev = va.va_rdev;
    651 	sb->st_size = va.va_size;
    652 	sb->st_atimespec = va.va_atime;
    653 	sb->st_mtimespec = va.va_mtime;
    654 	sb->st_ctimespec = va.va_ctime;
    655 	sb->st_birthtimespec = va.va_birthtime;
    656 	sb->st_blksize = va.va_blocksize;
    657 	sb->st_flags = va.va_flags;
    658 	sb->st_gen = 0;
    659 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    660 	return (0);
    661 }
    662 
    663 /*
    664  * File table vnode fcntl routine.
    665  */
    666 static int
    667 vn_fcntl(file_t *fp, u_int com, void *data)
    668 {
    669 	struct vnode *vp = fp->f_data;
    670 	int error;
    671 
    672 	error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
    673 	return (error);
    674 }
    675 
    676 /*
    677  * File table vnode ioctl routine.
    678  */
    679 static int
    680 vn_ioctl(file_t *fp, u_long com, void *data)
    681 {
    682 	struct vnode *vp = fp->f_data, *ovp;
    683 	struct vattr vattr;
    684 	int error;
    685 
    686 	switch (vp->v_type) {
    687 
    688 	case VREG:
    689 	case VDIR:
    690 		if (com == FIONREAD) {
    691 			error = VOP_GETATTR(vp, &vattr,
    692 			    kauth_cred_get());
    693 			if (error)
    694 				return (error);
    695 			*(int *)data = vattr.va_size - fp->f_offset;
    696 			return (0);
    697 		}
    698 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
    699 			/*
    700 			 * Files don't have send queues, so there never
    701 			 * are any bytes in them, nor is there any
    702 			 * open space in them.
    703 			 */
    704 			*(int *)data = 0;
    705 			return (0);
    706 		}
    707 		if (com == FIOGETBMAP) {
    708 			daddr_t *block;
    709 
    710 			if (*(daddr_t *)data < 0)
    711 				return (EINVAL);
    712 			block = (daddr_t *)data;
    713 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
    714 		}
    715 		if (com == OFIOGETBMAP) {
    716 			daddr_t ibn, obn;
    717 
    718 			if (*(int32_t *)data < 0)
    719 				return (EINVAL);
    720 			ibn = (daddr_t)*(int32_t *)data;
    721 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    722 			*(int32_t *)data = (int32_t)obn;
    723 			return error;
    724 		}
    725 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    726 			return (0);			/* XXX */
    727 		/* fall into ... */
    728 	case VFIFO:
    729 	case VCHR:
    730 	case VBLK:
    731 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
    732 		    kauth_cred_get());
    733 		if (error == 0 && com == TIOCSCTTY) {
    734 			vref(vp);
    735 			mutex_enter(proc_lock);
    736 			ovp = curproc->p_session->s_ttyvp;
    737 			curproc->p_session->s_ttyvp = vp;
    738 			mutex_exit(proc_lock);
    739 			if (ovp != NULL)
    740 				vrele(ovp);
    741 		}
    742 		return (error);
    743 
    744 	default:
    745 		return (EPASSTHROUGH);
    746 	}
    747 }
    748 
    749 /*
    750  * File table vnode poll routine.
    751  */
    752 static int
    753 vn_poll(file_t *fp, int events)
    754 {
    755 
    756 	return (VOP_POLL(fp->f_data, events));
    757 }
    758 
    759 /*
    760  * File table vnode kqfilter routine.
    761  */
    762 int
    763 vn_kqfilter(file_t *fp, struct knote *kn)
    764 {
    765 
    766 	return (VOP_KQFILTER(fp->f_data, kn));
    767 }
    768 
    769 /*
    770  * Check that the vnode is still valid, and if so
    771  * acquire requested lock.
    772  */
    773 int
    774 vn_lock(struct vnode *vp, int flags)
    775 {
    776 	int error;
    777 
    778 #if 0
    779 	KASSERT(vp->v_usecount > 0 || (vp->v_iflag & VI_ONWORKLST) != 0);
    780 #endif
    781 	KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY)) == 0);
    782 	KASSERT(!mutex_owned(vp->v_interlock));
    783 
    784 #ifdef DIAGNOSTIC
    785 	if (wapbl_vphaswapbl(vp))
    786 		WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
    787 #endif
    788 
    789 	do {
    790 		/*
    791 		 * XXX PR 37706 forced unmount of file systems is unsafe.
    792 		 * Race between vclean() and this the remaining problem.
    793 		 */
    794 		mutex_enter(vp->v_interlock);
    795 		if (vp->v_iflag & VI_XLOCK) {
    796 			if (flags & LK_NOWAIT) {
    797 				mutex_exit(vp->v_interlock);
    798 				return EBUSY;
    799 			}
    800 			vwait(vp, VI_XLOCK);
    801 			mutex_exit(vp->v_interlock);
    802 			error = ENOENT;
    803 		} else {
    804 			mutex_exit(vp->v_interlock);
    805 			error = VOP_LOCK(vp, (flags & ~LK_RETRY));
    806 			if (error == 0 || error == EDEADLK || error == EBUSY)
    807 				return (error);
    808 		}
    809 	} while (flags & LK_RETRY);
    810 	return (error);
    811 }
    812 
    813 /*
    814  * File table vnode close routine.
    815  */
    816 static int
    817 vn_closefile(file_t *fp)
    818 {
    819 
    820 	return vn_close(fp->f_data, fp->f_flag, fp->f_cred);
    821 }
    822 
    823 /*
    824  * Simplified in-kernel wrapper calls for extended attribute access.
    825  * Both calls pass in a NULL credential, authorizing a "kernel" access.
    826  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
    827  */
    828 int
    829 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
    830     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
    831 {
    832 	struct uio auio;
    833 	struct iovec aiov;
    834 	int error;
    835 
    836 	aiov.iov_len = *buflen;
    837 	aiov.iov_base = bf;
    838 
    839 	auio.uio_iov = &aiov;
    840 	auio.uio_iovcnt = 1;
    841 	auio.uio_rw = UIO_READ;
    842 	auio.uio_offset = 0;
    843 	auio.uio_resid = *buflen;
    844 	UIO_SETUP_SYSSPACE(&auio);
    845 
    846 	if ((ioflg & IO_NODELOCKED) == 0)
    847 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    848 
    849 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL);
    850 
    851 	if ((ioflg & IO_NODELOCKED) == 0)
    852 		VOP_UNLOCK(vp);
    853 
    854 	if (error == 0)
    855 		*buflen = *buflen - auio.uio_resid;
    856 
    857 	return (error);
    858 }
    859 
    860 /*
    861  * XXX Failure mode if partially written?
    862  */
    863 int
    864 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
    865     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
    866 {
    867 	struct uio auio;
    868 	struct iovec aiov;
    869 	int error;
    870 
    871 	aiov.iov_len = buflen;
    872 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
    873 
    874 	auio.uio_iov = &aiov;
    875 	auio.uio_iovcnt = 1;
    876 	auio.uio_rw = UIO_WRITE;
    877 	auio.uio_offset = 0;
    878 	auio.uio_resid = buflen;
    879 	UIO_SETUP_SYSSPACE(&auio);
    880 
    881 	if ((ioflg & IO_NODELOCKED) == 0) {
    882 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    883 	}
    884 
    885 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL);
    886 
    887 	if ((ioflg & IO_NODELOCKED) == 0) {
    888 		VOP_UNLOCK(vp);
    889 	}
    890 
    891 	return (error);
    892 }
    893 
    894 int
    895 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
    896     const char *attrname, struct lwp *l)
    897 {
    898 	int error;
    899 
    900 	if ((ioflg & IO_NODELOCKED) == 0) {
    901 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    902 	}
    903 
    904 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL);
    905 	if (error == EOPNOTSUPP)
    906 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL);
    907 
    908 	if ((ioflg & IO_NODELOCKED) == 0) {
    909 		VOP_UNLOCK(vp);
    910 	}
    911 
    912 	return (error);
    913 }
    914 
    915 void
    916 vn_ra_allocctx(struct vnode *vp)
    917 {
    918 	struct uvm_ractx *ra = NULL;
    919 
    920 	KASSERT(mutex_owned(vp->v_interlock));
    921 
    922 	if (vp->v_type != VREG) {
    923 		return;
    924 	}
    925 	if (vp->v_ractx != NULL) {
    926 		return;
    927 	}
    928 	if (vp->v_ractx == NULL) {
    929 		mutex_exit(vp->v_interlock);
    930 		ra = uvm_ra_allocctx();
    931 		mutex_enter(vp->v_interlock);
    932 		if (ra != NULL && vp->v_ractx == NULL) {
    933 			vp->v_ractx = ra;
    934 			ra = NULL;
    935 		}
    936 	}
    937 	if (ra != NULL) {
    938 		uvm_ra_freectx(ra);
    939 	}
    940 }
    941 
    942 int
    943 vn_fifo_bypass(void *v)
    944 {
    945 	struct vop_generic_args *ap = v;
    946 
    947 	return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v);
    948 }
    949