Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.185.2.3
      1 /*	$NetBSD: vfs_vnops.c,v 1.185.2.3 2014/08/20 00:04:29 tls 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.185.2.3 2014/08/20 00:04:29 tls 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 		/* We have to release the locks ourselves */
    176 		if (fmode & O_CREAT) {
    177 			if (vp == NULL) {
    178 				vput(ndp->ni_dvp);
    179 			} else {
    180 				VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    181 				if (ndp->ni_dvp == ndp->ni_vp)
    182 					vrele(ndp->ni_dvp);
    183 				else
    184 					vput(ndp->ni_dvp);
    185 				ndp->ni_dvp = NULL;
    186 				vput(vp);
    187 			}
    188 		} else {
    189 			vput(vp);
    190 		}
    191 		goto out;
    192 	}
    193 #endif /* NVERIEXEC > 0 */
    194 
    195 	if (fmode & O_CREAT) {
    196 		if (ndp->ni_vp == NULL) {
    197 			vattr_null(&va);
    198 			va.va_type = VREG;
    199 			va.va_mode = cmode;
    200 			if (fmode & O_EXCL)
    201 				 va.va_vaflags |= VA_EXCLUSIVE;
    202 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
    203 					   &ndp->ni_cnd, &va);
    204 			vput(ndp->ni_dvp);
    205 			if (error)
    206 				goto out;
    207 			fmode &= ~O_TRUNC;
    208 			vp = ndp->ni_vp;
    209 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    210 		} else {
    211 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    212 			if (ndp->ni_dvp == ndp->ni_vp)
    213 				vrele(ndp->ni_dvp);
    214 			else
    215 				vput(ndp->ni_dvp);
    216 			ndp->ni_dvp = NULL;
    217 			vp = ndp->ni_vp;
    218 			if (fmode & O_EXCL) {
    219 				error = EEXIST;
    220 				goto bad;
    221 			}
    222 			fmode &= ~O_CREAT;
    223 		}
    224 	} else {
    225 		vp = ndp->ni_vp;
    226 	}
    227 	if (vp->v_type == VSOCK) {
    228 		error = EOPNOTSUPP;
    229 		goto bad;
    230 	}
    231 	if (ndp->ni_vp->v_type == VLNK) {
    232 		error = EFTYPE;
    233 		goto bad;
    234 	}
    235 
    236 	if ((fmode & O_CREAT) == 0) {
    237 		error = vn_openchk(vp, cred, fmode);
    238 		if (error != 0)
    239 			goto bad;
    240 	}
    241 
    242 	if (fmode & O_TRUNC) {
    243 		vattr_null(&va);
    244 		va.va_size = 0;
    245 		error = VOP_SETATTR(vp, &va, cred);
    246 		if (error != 0)
    247 			goto bad;
    248 	}
    249 	if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
    250 		goto bad;
    251 	if (fmode & FWRITE) {
    252 		mutex_enter(vp->v_interlock);
    253 		vp->v_writecount++;
    254 		mutex_exit(vp->v_interlock);
    255 	}
    256 
    257 bad:
    258 	if (error)
    259 		vput(vp);
    260 out:
    261 	pathbuf_stringcopy_put(ndp->ni_pathbuf, pathstring);
    262 	return (error);
    263 }
    264 
    265 /*
    266  * Check for write permissions on the specified vnode.
    267  * Prototype text segments cannot be written.
    268  */
    269 int
    270 vn_writechk(struct vnode *vp)
    271 {
    272 
    273 	/*
    274 	 * If the vnode is in use as a process's text,
    275 	 * we can't allow writing.
    276 	 */
    277 	if (vp->v_iflag & VI_TEXT)
    278 		return (ETXTBSY);
    279 	return (0);
    280 }
    281 
    282 int
    283 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
    284 {
    285 	int permbits = 0;
    286 	int error;
    287 
    288 	if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR)
    289 		return ENOTDIR;
    290 
    291 	if ((fflags & FREAD) != 0) {
    292 		permbits = VREAD;
    293 	}
    294 	if ((fflags & (FWRITE | O_TRUNC)) != 0) {
    295 		permbits |= VWRITE;
    296 		if (vp->v_type == VDIR) {
    297 			error = EISDIR;
    298 			goto bad;
    299 		}
    300 		error = vn_writechk(vp);
    301 		if (error != 0)
    302 			goto bad;
    303 	}
    304 	error = VOP_ACCESS(vp, permbits, cred);
    305 bad:
    306 	return error;
    307 }
    308 
    309 /*
    310  * Mark a vnode as having executable mappings.
    311  */
    312 void
    313 vn_markexec(struct vnode *vp)
    314 {
    315 
    316 	if ((vp->v_iflag & VI_EXECMAP) != 0) {
    317 		/* Safe unlocked, as long as caller holds a reference. */
    318 		return;
    319 	}
    320 
    321 	mutex_enter(vp->v_interlock);
    322 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    323 		atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
    324 		atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
    325 		vp->v_iflag |= VI_EXECMAP;
    326 	}
    327 	mutex_exit(vp->v_interlock);
    328 }
    329 
    330 /*
    331  * Mark a vnode as being the text of a process.
    332  * Fail if the vnode is currently writable.
    333  */
    334 int
    335 vn_marktext(struct vnode *vp)
    336 {
    337 
    338 	if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
    339 		/* Safe unlocked, as long as caller holds a reference. */
    340 		return (0);
    341 	}
    342 
    343 	mutex_enter(vp->v_interlock);
    344 	if (vp->v_writecount != 0) {
    345 		KASSERT((vp->v_iflag & VI_TEXT) == 0);
    346 		mutex_exit(vp->v_interlock);
    347 		return (ETXTBSY);
    348 	}
    349 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    350 		atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
    351 		atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
    352 	}
    353 	vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
    354 	mutex_exit(vp->v_interlock);
    355 	return (0);
    356 }
    357 
    358 /*
    359  * Vnode close call
    360  *
    361  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    362  */
    363 int
    364 vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
    365 {
    366 	int error;
    367 
    368 	if (flags & FWRITE) {
    369 		mutex_enter(vp->v_interlock);
    370 		KASSERT(vp->v_writecount > 0);
    371 		vp->v_writecount--;
    372 		mutex_exit(vp->v_interlock);
    373 	}
    374 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    375 	error = VOP_CLOSE(vp, flags, cred);
    376 	vput(vp);
    377 	return (error);
    378 }
    379 
    380 static int
    381 enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag)
    382 {
    383 	struct lwp *l = curlwp;
    384 	off_t testoff;
    385 
    386 	if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG)
    387 		return 0;
    388 
    389 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    390 	if (ioflag & IO_APPEND)
    391 		testoff = vp->v_size;
    392 	else
    393 		testoff = uio->uio_offset;
    394 
    395 	if (testoff + uio->uio_resid >
    396 	    l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    397 		mutex_enter(proc_lock);
    398 		psignal(l->l_proc, SIGXFSZ);
    399 		mutex_exit(proc_lock);
    400 		return EFBIG;
    401 	}
    402 
    403 	return 0;
    404 }
    405 
    406 /*
    407  * Package up an I/O request on a vnode into a uio and do it.
    408  */
    409 int
    410 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
    411     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
    412     struct lwp *l)
    413 {
    414 	struct uio auio;
    415 	struct iovec aiov;
    416 	int error;
    417 
    418 	if ((ioflg & IO_NODELOCKED) == 0) {
    419 		if (rw == UIO_READ) {
    420 			vn_lock(vp, LK_SHARED | LK_RETRY);
    421 		} else /* UIO_WRITE */ {
    422 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    423 		}
    424 	}
    425 	auio.uio_iov = &aiov;
    426 	auio.uio_iovcnt = 1;
    427 	aiov.iov_base = base;
    428 	aiov.iov_len = len;
    429 	auio.uio_resid = len;
    430 	auio.uio_offset = offset;
    431 	auio.uio_rw = rw;
    432 	if (segflg == UIO_SYSSPACE) {
    433 		UIO_SETUP_SYSSPACE(&auio);
    434 	} else {
    435 		auio.uio_vmspace = l->l_proc->p_vmspace;
    436 	}
    437 
    438 	if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0)
    439 		goto out;
    440 
    441 	if (rw == UIO_READ) {
    442 		error = VOP_READ(vp, &auio, ioflg, cred);
    443 	} else {
    444 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    445 	}
    446 
    447 	if (aresid)
    448 		*aresid = auio.uio_resid;
    449 	else
    450 		if (auio.uio_resid && error == 0)
    451 			error = EIO;
    452 
    453  out:
    454 	if ((ioflg & IO_NODELOCKED) == 0) {
    455 		VOP_UNLOCK(vp);
    456 	}
    457 	return (error);
    458 }
    459 
    460 int
    461 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
    462     struct lwp *l, off_t **cookies, int *ncookies)
    463 {
    464 	struct vnode *vp = (struct vnode *)fp->f_data;
    465 	struct iovec aiov;
    466 	struct uio auio;
    467 	int error, eofflag;
    468 
    469 	/* Limit the size on any kernel buffers used by VOP_READDIR */
    470 	count = min(MAXBSIZE, count);
    471 
    472 unionread:
    473 	if (vp->v_type != VDIR)
    474 		return (EINVAL);
    475 	aiov.iov_base = bf;
    476 	aiov.iov_len = count;
    477 	auio.uio_iov = &aiov;
    478 	auio.uio_iovcnt = 1;
    479 	auio.uio_rw = UIO_READ;
    480 	if (segflg == UIO_SYSSPACE) {
    481 		UIO_SETUP_SYSSPACE(&auio);
    482 	} else {
    483 		KASSERT(l == curlwp);
    484 		auio.uio_vmspace = l->l_proc->p_vmspace;
    485 	}
    486 	auio.uio_resid = count;
    487 	vn_lock(vp, LK_SHARED | LK_RETRY);
    488 	auio.uio_offset = fp->f_offset;
    489 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    490 		    ncookies);
    491 	mutex_enter(&fp->f_lock);
    492 	fp->f_offset = auio.uio_offset;
    493 	mutex_exit(&fp->f_lock);
    494 	VOP_UNLOCK(vp);
    495 	if (error)
    496 		return (error);
    497 
    498 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    499 		struct vnode *ovp = vp;
    500 
    501 		error = (*vn_union_readdir_hook)(&vp, fp, l);
    502 		if (error)
    503 			return (error);
    504 		if (vp != ovp)
    505 			goto unionread;
    506 	}
    507 
    508 	if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
    509 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    510 		struct vnode *tvp = vp;
    511 		vp = vp->v_mount->mnt_vnodecovered;
    512 		vref(vp);
    513 		mutex_enter(&fp->f_lock);
    514 		fp->f_data = vp;
    515 		fp->f_offset = 0;
    516 		mutex_exit(&fp->f_lock);
    517 		vrele(tvp);
    518 		goto unionread;
    519 	}
    520 	*done = count - auio.uio_resid;
    521 	return error;
    522 }
    523 
    524 /*
    525  * File table vnode read routine.
    526  */
    527 static int
    528 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    529     int flags)
    530 {
    531 	struct vnode *vp = (struct vnode *)fp->f_data;
    532 	int error, ioflag, fflag;
    533 	size_t count;
    534 
    535 	ioflag = IO_ADV_ENCODE(fp->f_advice);
    536 	fflag = fp->f_flag;
    537 	if (fflag & FNONBLOCK)
    538 		ioflag |= IO_NDELAY;
    539 	if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    540 		ioflag |= IO_SYNC;
    541 	if (fflag & FALTIO)
    542 		ioflag |= IO_ALTSEMANTICS;
    543 	if (fflag & FDIRECT)
    544 		ioflag |= IO_DIRECT;
    545 	vn_lock(vp, LK_SHARED | LK_RETRY);
    546 	uio->uio_offset = *offset;
    547 	count = uio->uio_resid;
    548 	error = VOP_READ(vp, uio, ioflag, cred);
    549 	if (flags & FOF_UPDATE_OFFSET)
    550 		*offset += count - uio->uio_resid;
    551 	VOP_UNLOCK(vp);
    552 	return (error);
    553 }
    554 
    555 /*
    556  * File table vnode write routine.
    557  */
    558 static int
    559 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    560     int flags)
    561 {
    562 	struct vnode *vp = (struct vnode *)fp->f_data;
    563 	int error, ioflag, fflag;
    564 	size_t count;
    565 
    566 	ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
    567 	fflag = fp->f_flag;
    568 	if (vp->v_type == VREG && (fflag & O_APPEND))
    569 		ioflag |= IO_APPEND;
    570 	if (fflag & FNONBLOCK)
    571 		ioflag |= IO_NDELAY;
    572 	if (fflag & FFSYNC ||
    573 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    574 		ioflag |= IO_SYNC;
    575 	else if (fflag & FDSYNC)
    576 		ioflag |= IO_DSYNC;
    577 	if (fflag & FALTIO)
    578 		ioflag |= IO_ALTSEMANTICS;
    579 	if (fflag & FDIRECT)
    580 		ioflag |= IO_DIRECT;
    581 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    582 	uio->uio_offset = *offset;
    583 	count = uio->uio_resid;
    584 
    585 	if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
    586 		goto out;
    587 
    588 	error = VOP_WRITE(vp, uio, ioflag, cred);
    589 
    590 	if (flags & FOF_UPDATE_OFFSET) {
    591 		if (ioflag & IO_APPEND) {
    592 			/*
    593 			 * SUSv3 describes behaviour for count = 0 as following:
    594 			 * "Before any action ... is taken, and if nbyte is zero
    595 			 * and the file is a regular file, the write() function
    596 			 * ... in the absence of errors ... shall return zero
    597 			 * and have no other results."
    598 			 */
    599 			if (count)
    600 				*offset = uio->uio_offset;
    601 		} else
    602 			*offset += count - uio->uio_resid;
    603 	}
    604 
    605  out:
    606 	VOP_UNLOCK(vp);
    607 	return (error);
    608 }
    609 
    610 /*
    611  * File table vnode stat routine.
    612  */
    613 static int
    614 vn_statfile(file_t *fp, struct stat *sb)
    615 {
    616 	struct vnode *vp = fp->f_data;
    617 	int error;
    618 
    619 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    620 	error = vn_stat(vp, sb);
    621 	VOP_UNLOCK(vp);
    622 	return error;
    623 }
    624 
    625 int
    626 vn_stat(struct vnode *vp, struct stat *sb)
    627 {
    628 	struct vattr va;
    629 	int error;
    630 	mode_t mode;
    631 
    632 	memset(&va, 0, sizeof(va));
    633 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
    634 	if (error)
    635 		return (error);
    636 	/*
    637 	 * Copy from vattr table
    638 	 */
    639 	memset(sb, 0, sizeof(*sb));
    640 	sb->st_dev = va.va_fsid;
    641 	sb->st_ino = va.va_fileid;
    642 	mode = va.va_mode;
    643 	switch (vp->v_type) {
    644 	case VREG:
    645 		mode |= S_IFREG;
    646 		break;
    647 	case VDIR:
    648 		mode |= S_IFDIR;
    649 		break;
    650 	case VBLK:
    651 		mode |= S_IFBLK;
    652 		break;
    653 	case VCHR:
    654 		mode |= S_IFCHR;
    655 		break;
    656 	case VLNK:
    657 		mode |= S_IFLNK;
    658 		break;
    659 	case VSOCK:
    660 		mode |= S_IFSOCK;
    661 		break;
    662 	case VFIFO:
    663 		mode |= S_IFIFO;
    664 		break;
    665 	default:
    666 		return (EBADF);
    667 	};
    668 	sb->st_mode = mode;
    669 	sb->st_nlink = va.va_nlink;
    670 	sb->st_uid = va.va_uid;
    671 	sb->st_gid = va.va_gid;
    672 	sb->st_rdev = va.va_rdev;
    673 	sb->st_size = va.va_size;
    674 	sb->st_atimespec = va.va_atime;
    675 	sb->st_mtimespec = va.va_mtime;
    676 	sb->st_ctimespec = va.va_ctime;
    677 	sb->st_birthtimespec = va.va_birthtime;
    678 	sb->st_blksize = va.va_blocksize;
    679 	sb->st_flags = va.va_flags;
    680 	sb->st_gen = 0;
    681 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    682 	return (0);
    683 }
    684 
    685 /*
    686  * File table vnode fcntl routine.
    687  */
    688 static int
    689 vn_fcntl(file_t *fp, u_int com, void *data)
    690 {
    691 	struct vnode *vp = fp->f_data;
    692 	int error;
    693 
    694 	error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
    695 	return (error);
    696 }
    697 
    698 /*
    699  * File table vnode ioctl routine.
    700  */
    701 static int
    702 vn_ioctl(file_t *fp, u_long com, void *data)
    703 {
    704 	struct vnode *vp = fp->f_data, *ovp;
    705 	struct vattr vattr;
    706 	int error;
    707 
    708 	switch (vp->v_type) {
    709 
    710 	case VREG:
    711 	case VDIR:
    712 		if (com == FIONREAD) {
    713 			vn_lock(vp, LK_SHARED | LK_RETRY);
    714 			error = VOP_GETATTR(vp, &vattr, kauth_cred_get());
    715 			VOP_UNLOCK(vp);
    716 			if (error)
    717 				return (error);
    718 			*(int *)data = vattr.va_size - fp->f_offset;
    719 			return (0);
    720 		}
    721 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
    722 			/*
    723 			 * Files don't have send queues, so there never
    724 			 * are any bytes in them, nor is there any
    725 			 * open space in them.
    726 			 */
    727 			*(int *)data = 0;
    728 			return (0);
    729 		}
    730 		if (com == FIOGETBMAP) {
    731 			daddr_t *block;
    732 
    733 			if (*(daddr_t *)data < 0)
    734 				return (EINVAL);
    735 			block = (daddr_t *)data;
    736 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
    737 		}
    738 		if (com == OFIOGETBMAP) {
    739 			daddr_t ibn, obn;
    740 
    741 			if (*(int32_t *)data < 0)
    742 				return (EINVAL);
    743 			ibn = (daddr_t)*(int32_t *)data;
    744 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    745 			*(int32_t *)data = (int32_t)obn;
    746 			return error;
    747 		}
    748 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    749 			return (0);			/* XXX */
    750 		/* fall into ... */
    751 	case VFIFO:
    752 	case VCHR:
    753 	case VBLK:
    754 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
    755 		    kauth_cred_get());
    756 		if (error == 0 && com == TIOCSCTTY) {
    757 			vref(vp);
    758 			mutex_enter(proc_lock);
    759 			ovp = curproc->p_session->s_ttyvp;
    760 			curproc->p_session->s_ttyvp = vp;
    761 			mutex_exit(proc_lock);
    762 			if (ovp != NULL)
    763 				vrele(ovp);
    764 		}
    765 		return (error);
    766 
    767 	default:
    768 		return (EPASSTHROUGH);
    769 	}
    770 }
    771 
    772 /*
    773  * File table vnode poll routine.
    774  */
    775 static int
    776 vn_poll(file_t *fp, int events)
    777 {
    778 
    779 	return (VOP_POLL(fp->f_data, events));
    780 }
    781 
    782 /*
    783  * File table vnode kqfilter routine.
    784  */
    785 int
    786 vn_kqfilter(file_t *fp, struct knote *kn)
    787 {
    788 
    789 	return (VOP_KQFILTER(fp->f_data, kn));
    790 }
    791 
    792 /*
    793  * Check that the vnode is still valid, and if so
    794  * acquire requested lock.
    795  */
    796 int
    797 vn_lock(struct vnode *vp, int flags)
    798 {
    799 	int error;
    800 
    801 #if 0
    802 	KASSERT(vp->v_usecount > 0 || (vp->v_iflag & VI_ONWORKLST) != 0);
    803 #endif
    804 	KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY)) == 0);
    805 	KASSERT(!mutex_owned(vp->v_interlock));
    806 
    807 #ifdef DIAGNOSTIC
    808 	if (wapbl_vphaswapbl(vp))
    809 		WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
    810 #endif
    811 
    812 	error = VOP_LOCK(vp, flags);
    813 	if ((flags & LK_RETRY) != 0 && error == ENOENT)
    814 		error = VOP_LOCK(vp, flags);
    815 
    816 	KASSERT((flags & LK_RETRY) == 0 || (flags & LK_NOWAIT) != 0 ||
    817 	    error == 0);
    818 
    819 	return error;
    820 }
    821 
    822 /*
    823  * File table vnode close routine.
    824  */
    825 static int
    826 vn_closefile(file_t *fp)
    827 {
    828 
    829 	return vn_close(fp->f_data, fp->f_flag, fp->f_cred);
    830 }
    831 
    832 /*
    833  * Simplified in-kernel wrapper calls for extended attribute access.
    834  * Both calls pass in a NULL credential, authorizing a "kernel" access.
    835  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
    836  */
    837 int
    838 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
    839     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
    840 {
    841 	struct uio auio;
    842 	struct iovec aiov;
    843 	int error;
    844 
    845 	aiov.iov_len = *buflen;
    846 	aiov.iov_base = bf;
    847 
    848 	auio.uio_iov = &aiov;
    849 	auio.uio_iovcnt = 1;
    850 	auio.uio_rw = UIO_READ;
    851 	auio.uio_offset = 0;
    852 	auio.uio_resid = *buflen;
    853 	UIO_SETUP_SYSSPACE(&auio);
    854 
    855 	if ((ioflg & IO_NODELOCKED) == 0)
    856 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    857 
    858 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL);
    859 
    860 	if ((ioflg & IO_NODELOCKED) == 0)
    861 		VOP_UNLOCK(vp);
    862 
    863 	if (error == 0)
    864 		*buflen = *buflen - auio.uio_resid;
    865 
    866 	return (error);
    867 }
    868 
    869 /*
    870  * XXX Failure mode if partially written?
    871  */
    872 int
    873 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
    874     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
    875 {
    876 	struct uio auio;
    877 	struct iovec aiov;
    878 	int error;
    879 
    880 	aiov.iov_len = buflen;
    881 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
    882 
    883 	auio.uio_iov = &aiov;
    884 	auio.uio_iovcnt = 1;
    885 	auio.uio_rw = UIO_WRITE;
    886 	auio.uio_offset = 0;
    887 	auio.uio_resid = buflen;
    888 	UIO_SETUP_SYSSPACE(&auio);
    889 
    890 	if ((ioflg & IO_NODELOCKED) == 0) {
    891 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    892 	}
    893 
    894 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL);
    895 
    896 	if ((ioflg & IO_NODELOCKED) == 0) {
    897 		VOP_UNLOCK(vp);
    898 	}
    899 
    900 	return (error);
    901 }
    902 
    903 int
    904 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
    905     const char *attrname, struct lwp *l)
    906 {
    907 	int error;
    908 
    909 	if ((ioflg & IO_NODELOCKED) == 0) {
    910 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    911 	}
    912 
    913 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL);
    914 	if (error == EOPNOTSUPP)
    915 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL);
    916 
    917 	if ((ioflg & IO_NODELOCKED) == 0) {
    918 		VOP_UNLOCK(vp);
    919 	}
    920 
    921 	return (error);
    922 }
    923 
    924 void
    925 vn_ra_allocctx(struct vnode *vp)
    926 {
    927 	struct uvm_ractx *ra = NULL;
    928 
    929 	KASSERT(mutex_owned(vp->v_interlock));
    930 
    931 	if (vp->v_type != VREG) {
    932 		return;
    933 	}
    934 	if (vp->v_ractx != NULL) {
    935 		return;
    936 	}
    937 	if (vp->v_ractx == NULL) {
    938 		mutex_exit(vp->v_interlock);
    939 		ra = uvm_ra_allocctx();
    940 		mutex_enter(vp->v_interlock);
    941 		if (ra != NULL && vp->v_ractx == NULL) {
    942 			size_t iochunk = 512, ioc = 512;
    943 
    944 			while(1) {
    945 				size_t mp_mp = MAX(vp->v_mount->mnt_maxphys,
    946 						   MAXPHYS); /* XXX NFS */
    947 				size_t ra_max = MIN(mp_mp, UVM_RA_WINSIZE_MAX);
    948 				if (ioc > ra_max) {
    949 					ra->ra_iochunk = iochunk;
    950 					vp->v_ractx = ra;
    951 					ra = NULL;
    952 					break;
    953 				} else {
    954 					iochunk = ioc;
    955 					ioc *= 2;
    956 				}
    957 			}
    958 		}
    959 	}
    960 	if (ra != NULL) {
    961 		uvm_ra_freectx(ra);
    962 	}
    963 }
    964 
    965 int
    966 vn_fifo_bypass(void *v)
    967 {
    968 	struct vop_generic_args *ap = v;
    969 
    970 	return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v);
    971 }
    972