Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.217
      1 /*	$NetBSD: vfs_vnops.c,v 1.217 2021/06/30 11:20:32 christos 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.217 2021/06/30 11:20:32 christos 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 #include <sys/mman.h>
     93 
     94 #include <miscfs/specfs/specdev.h>
     95 #include <miscfs/fifofs/fifo.h>
     96 
     97 #include <uvm/uvm_extern.h>
     98 #include <uvm/uvm_readahead.h>
     99 #include <uvm/uvm_device.h>
    100 
    101 #ifdef UNION
    102 #include <fs/union/union.h>
    103 #endif
    104 
    105 #ifndef COMPAT_ZERODEV
    106 #define COMPAT_ZERODEV(dev)	(0)
    107 #endif
    108 
    109 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
    110 
    111 #include <sys/verified_exec.h>
    112 
    113 static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
    114 	    kauth_cred_t cred, int flags);
    115 static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
    116 	    kauth_cred_t cred, int flags);
    117 static int vn_closefile(file_t *fp);
    118 static int vn_poll(file_t *fp, int events);
    119 static int vn_fcntl(file_t *fp, u_int com, void *data);
    120 static int vn_statfile(file_t *fp, struct stat *sb);
    121 static int vn_ioctl(file_t *fp, u_long com, void *data);
    122 static int vn_mmap(struct file *, off_t *, size_t, int, int *, int *,
    123 		   struct uvm_object **, int *);
    124 
    125 const struct fileops vnops = {
    126 	.fo_name = "vn",
    127 	.fo_read = vn_read,
    128 	.fo_write = vn_write,
    129 	.fo_ioctl = vn_ioctl,
    130 	.fo_fcntl = vn_fcntl,
    131 	.fo_poll = vn_poll,
    132 	.fo_stat = vn_statfile,
    133 	.fo_close = vn_closefile,
    134 	.fo_kqfilter = vn_kqfilter,
    135 	.fo_restart = fnullop_restart,
    136 	.fo_mmap = vn_mmap,
    137 };
    138 
    139 /*
    140  * Common code for vnode open operations.
    141  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
    142  *
    143  * at_dvp is the directory for openat(), if any.
    144  * pb is the path.
    145  * nmode is additional namei flags, restricted to TRYEMULROOT and NOCHROOT.
    146  * fmode is the open flags, converted from O_* to F*
    147  * cmode is the creation file permissions.
    148  *
    149  * XXX shouldn't cmode be mode_t?
    150  *
    151  * On success produces either a vnode in *ret_vp, or if that is NULL,
    152  * a file descriptor number in ret_fd.
    153  *
    154  * The caller may pass NULL for ret_fd (and ret_domove), in which case
    155  * EOPNOTSUPP will be produced in the cases that would otherwise return
    156  * a file descriptor.
    157  *
    158  * Note that callers that want NOFOLLOW should pass O_NOFOLLOW in fmode,
    159  * not NOFOLLOW in nmode.
    160  */
    161 int
    162 vn_open(struct vnode *at_dvp, struct pathbuf *pb,
    163 	int nmode, int fmode, int cmode,
    164 	struct vnode **ret_vp, bool *ret_domove, int *ret_fd)
    165 {
    166 	struct nameidata nd;
    167 	struct vnode *vp;
    168 	struct lwp *l = curlwp;
    169 	kauth_cred_t cred = l->l_cred;
    170 	struct vattr va;
    171 	int error;
    172 	const char *pathstring;
    173 
    174 	KASSERT((nmode & (TRYEMULROOT | NOCHROOT)) == nmode);
    175 
    176 	if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY))
    177 		return EINVAL;
    178 
    179 	NDINIT(&nd, LOOKUP, FOLLOW | nmode, pb);
    180 	if (at_dvp != NULL)
    181 		NDAT(&nd, at_dvp);
    182 
    183 	nd.ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT;
    184 
    185 	if (fmode & O_CREAT) {
    186 		nd.ni_cnd.cn_nameiop = CREATE;
    187 		nd.ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
    188 		if ((fmode & O_EXCL) == 0 &&
    189 		    ((fmode & O_NOFOLLOW) == 0))
    190 			nd.ni_cnd.cn_flags |= FOLLOW;
    191 		if ((fmode & O_EXCL) == 0)
    192 			nd.ni_cnd.cn_flags |= NONEXCLHACK;
    193 	} else {
    194 		nd.ni_cnd.cn_nameiop = LOOKUP;
    195 		nd.ni_cnd.cn_flags |= LOCKLEAF;
    196 		if ((fmode & O_NOFOLLOW) == 0)
    197 			nd.ni_cnd.cn_flags |= FOLLOW;
    198 	}
    199 
    200 	pathstring = pathbuf_stringcopy_get(nd.ni_pathbuf);
    201 	if (pathstring == NULL) {
    202 		return ENOMEM;
    203 	}
    204 
    205 	/*
    206 	 * When this "interface" was exposed to do_open() it used
    207 	 * to initialize l_dupfd to -newfd-1 (thus passing in the
    208 	 * new file handle number to use)... but nothing in the
    209 	 * kernel uses that value. So just send 0.
    210 	 */
    211 	l->l_dupfd = 0;
    212 
    213 	error = namei(&nd);
    214 	if (error)
    215 		goto out;
    216 
    217 	vp = nd.ni_vp;
    218 
    219 #if NVERIEXEC > 0
    220 	error = veriexec_openchk(l, nd.ni_vp, pathstring, fmode);
    221 	if (error) {
    222 		/* We have to release the locks ourselves */
    223 		/*
    224 		 * 20210604 dholland passing NONEXCLHACK means we can
    225 		 * get ni_dvp == NULL back if ni_vp exists, and we should
    226 		 * treat that like the non-O_CREAT case.
    227 		 */
    228 		if ((fmode & O_CREAT) != 0 && nd.ni_dvp != NULL) {
    229 			if (vp == NULL) {
    230 				vput(nd.ni_dvp);
    231 			} else {
    232 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    233 				if (nd.ni_dvp == nd.ni_vp)
    234 					vrele(nd.ni_dvp);
    235 				else
    236 					vput(nd.ni_dvp);
    237 				nd.ni_dvp = NULL;
    238 				vput(vp);
    239 			}
    240 		} else {
    241 			vput(vp);
    242 		}
    243 		goto out;
    244 	}
    245 #endif /* NVERIEXEC > 0 */
    246 
    247 	/*
    248 	 * 20210604 dholland ditto
    249 	 */
    250 	if ((fmode & O_CREAT) != 0 && nd.ni_dvp != NULL) {
    251 		if (nd.ni_vp == NULL) {
    252 			vattr_null(&va);
    253 			va.va_type = VREG;
    254 			va.va_mode = cmode;
    255 			if (fmode & O_EXCL)
    256 				 va.va_vaflags |= VA_EXCLUSIVE;
    257 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
    258 					   &nd.ni_cnd, &va);
    259 			if (error) {
    260 				vput(nd.ni_dvp);
    261 				goto out;
    262 			}
    263 			fmode &= ~O_TRUNC;
    264 			vp = nd.ni_vp;
    265 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    266 			vput(nd.ni_dvp);
    267 		} else {
    268 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    269 			if (nd.ni_dvp == nd.ni_vp)
    270 				vrele(nd.ni_dvp);
    271 			else
    272 				vput(nd.ni_dvp);
    273 			nd.ni_dvp = NULL;
    274 			vp = nd.ni_vp;
    275 			if (fmode & O_EXCL) {
    276 				error = EEXIST;
    277 				goto bad;
    278 			}
    279 			fmode &= ~O_CREAT;
    280 		}
    281 	} else if ((fmode & O_CREAT) != 0) {
    282 		/*
    283 		 * 20210606 dholland passing NONEXCLHACK means this
    284 		 * case exists; it is the same as the following one
    285 		 * but also needs to do things in the second (exists)
    286 		 * half of the following block. (Besides handle
    287 		 * ni_dvp, anyway.)
    288 		 */
    289 		vp = nd.ni_vp;
    290 		KASSERT((fmode & O_EXCL) == 0);
    291 		fmode &= ~O_CREAT;
    292 	} else {
    293 		vp = nd.ni_vp;
    294 	}
    295 	if (vp->v_type == VSOCK) {
    296 		error = EOPNOTSUPP;
    297 		goto bad;
    298 	}
    299 	if (nd.ni_vp->v_type == VLNK) {
    300 		error = EFTYPE;
    301 		goto bad;
    302 	}
    303 
    304 	if ((fmode & O_CREAT) == 0) {
    305 		error = vn_openchk(vp, cred, fmode);
    306 		if (error != 0)
    307 			goto bad;
    308 	}
    309 
    310 	if (fmode & O_TRUNC) {
    311 		vattr_null(&va);
    312 		va.va_size = 0;
    313 		error = VOP_SETATTR(vp, &va, cred);
    314 		if (error != 0)
    315 			goto bad;
    316 	}
    317 	if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
    318 		goto bad;
    319 	if (fmode & FWRITE) {
    320 		mutex_enter(vp->v_interlock);
    321 		vp->v_writecount++;
    322 		mutex_exit(vp->v_interlock);
    323 	}
    324 
    325 bad:
    326 	if (error)
    327 		vput(vp);
    328 out:
    329 	pathbuf_stringcopy_put(nd.ni_pathbuf, pathstring);
    330 
    331 	switch (error) {
    332 	case EDUPFD:
    333 	case EMOVEFD:
    334 		/* if the caller isn't prepared to handle fds, fail for them */
    335 		KASSERTMSG(ret_domove && ret_fd,
    336 		    "caller did not supply ret_domove and ret_fd for %d",
    337 		    error);
    338 		*ret_vp = NULL;
    339 		*ret_domove = error == EMOVEFD;
    340 		*ret_fd = l->l_dupfd;
    341 		break;
    342 	case 0:
    343 		*ret_vp = vp;
    344 		break;
    345 	}
    346 	l->l_dupfd = 0;
    347 	return error;
    348 }
    349 
    350 /*
    351  * Check for write permissions on the specified vnode.
    352  * Prototype text segments cannot be written.
    353  */
    354 int
    355 vn_writechk(struct vnode *vp)
    356 {
    357 
    358 	/*
    359 	 * If the vnode is in use as a process's text,
    360 	 * we can't allow writing.
    361 	 */
    362 	if (vp->v_iflag & VI_TEXT)
    363 		return (ETXTBSY);
    364 	return (0);
    365 }
    366 
    367 int
    368 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
    369 {
    370 	int permbits = 0;
    371 	int error;
    372 
    373 	if (vp->v_type == VNON || vp->v_type == VBAD)
    374 		return ENXIO;
    375 
    376 	if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR)
    377 		return ENOTDIR;
    378 
    379 	if ((fflags & O_REGULAR) != 0 && vp->v_type != VREG)
    380 		return EFTYPE;
    381 
    382 	if ((fflags & FREAD) != 0) {
    383 		permbits = VREAD;
    384 	}
    385 	if ((fflags & FEXEC) != 0) {
    386 		permbits |= VEXEC;
    387 	}
    388 	if ((fflags & (FWRITE | O_TRUNC)) != 0) {
    389 		permbits |= VWRITE;
    390 		if (vp->v_type == VDIR) {
    391 			error = EISDIR;
    392 			goto bad;
    393 		}
    394 		error = vn_writechk(vp);
    395 		if (error != 0)
    396 			goto bad;
    397 	}
    398 	error = VOP_ACCESS(vp, permbits, cred);
    399 bad:
    400 	return error;
    401 }
    402 
    403 /*
    404  * Mark a vnode as having executable mappings.
    405  */
    406 void
    407 vn_markexec(struct vnode *vp)
    408 {
    409 
    410 	if ((vp->v_iflag & VI_EXECMAP) != 0) {
    411 		/* Safe unlocked, as long as caller holds a reference. */
    412 		return;
    413 	}
    414 
    415 	rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    416 	mutex_enter(vp->v_interlock);
    417 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    418 		cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
    419 		vp->v_iflag |= VI_EXECMAP;
    420 	}
    421 	mutex_exit(vp->v_interlock);
    422 	rw_exit(vp->v_uobj.vmobjlock);
    423 }
    424 
    425 /*
    426  * Mark a vnode as being the text of a process.
    427  * Fail if the vnode is currently writable.
    428  */
    429 int
    430 vn_marktext(struct vnode *vp)
    431 {
    432 
    433 	if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
    434 		/* Safe unlocked, as long as caller holds a reference. */
    435 		return (0);
    436 	}
    437 
    438 	rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    439 	mutex_enter(vp->v_interlock);
    440 	if (vp->v_writecount != 0) {
    441 		KASSERT((vp->v_iflag & VI_TEXT) == 0);
    442 		mutex_exit(vp->v_interlock);
    443 		rw_exit(vp->v_uobj.vmobjlock);
    444 		return (ETXTBSY);
    445 	}
    446 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    447 		cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
    448 	}
    449 	vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
    450 	mutex_exit(vp->v_interlock);
    451 	rw_exit(vp->v_uobj.vmobjlock);
    452 	return (0);
    453 }
    454 
    455 /*
    456  * Vnode close call
    457  *
    458  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    459  */
    460 int
    461 vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
    462 {
    463 	int error;
    464 
    465 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    466 	if (flags & FWRITE) {
    467 		mutex_enter(vp->v_interlock);
    468 		KASSERT(vp->v_writecount > 0);
    469 		vp->v_writecount--;
    470 		mutex_exit(vp->v_interlock);
    471 	}
    472 	error = VOP_CLOSE(vp, flags, cred);
    473 	vput(vp);
    474 	return (error);
    475 }
    476 
    477 static int
    478 enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag)
    479 {
    480 	struct lwp *l = curlwp;
    481 	off_t testoff;
    482 
    483 	if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG)
    484 		return 0;
    485 
    486 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    487 	if (ioflag & IO_APPEND)
    488 		testoff = vp->v_size;
    489 	else
    490 		testoff = uio->uio_offset;
    491 
    492 	if (testoff + uio->uio_resid >
    493 	    l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    494 		mutex_enter(&proc_lock);
    495 		psignal(l->l_proc, SIGXFSZ);
    496 		mutex_exit(&proc_lock);
    497 		return EFBIG;
    498 	}
    499 
    500 	return 0;
    501 }
    502 
    503 /*
    504  * Package up an I/O request on a vnode into a uio and do it.
    505  */
    506 int
    507 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
    508     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
    509     struct lwp *l)
    510 {
    511 	struct uio auio;
    512 	struct iovec aiov;
    513 	int error;
    514 
    515 	if ((ioflg & IO_NODELOCKED) == 0) {
    516 		if (rw == UIO_READ) {
    517 			vn_lock(vp, LK_SHARED | LK_RETRY);
    518 		} else /* UIO_WRITE */ {
    519 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    520 		}
    521 	}
    522 	auio.uio_iov = &aiov;
    523 	auio.uio_iovcnt = 1;
    524 	aiov.iov_base = base;
    525 	aiov.iov_len = len;
    526 	auio.uio_resid = len;
    527 	auio.uio_offset = offset;
    528 	auio.uio_rw = rw;
    529 	if (segflg == UIO_SYSSPACE) {
    530 		UIO_SETUP_SYSSPACE(&auio);
    531 	} else {
    532 		auio.uio_vmspace = l->l_proc->p_vmspace;
    533 	}
    534 
    535 	if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0)
    536 		goto out;
    537 
    538 	if (rw == UIO_READ) {
    539 		error = VOP_READ(vp, &auio, ioflg, cred);
    540 	} else {
    541 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    542 	}
    543 
    544 	if (aresid)
    545 		*aresid = auio.uio_resid;
    546 	else
    547 		if (auio.uio_resid && error == 0)
    548 			error = EIO;
    549 
    550  out:
    551 	if ((ioflg & IO_NODELOCKED) == 0) {
    552 		VOP_UNLOCK(vp);
    553 	}
    554 	return (error);
    555 }
    556 
    557 int
    558 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
    559     struct lwp *l, off_t **cookies, int *ncookies)
    560 {
    561 	struct vnode *vp = fp->f_vnode;
    562 	struct iovec aiov;
    563 	struct uio auio;
    564 	int error, eofflag;
    565 
    566 	/* Limit the size on any kernel buffers used by VOP_READDIR */
    567 	count = uimin(MAXBSIZE, count);
    568 
    569 unionread:
    570 	if (vp->v_type != VDIR)
    571 		return (EINVAL);
    572 	aiov.iov_base = bf;
    573 	aiov.iov_len = count;
    574 	auio.uio_iov = &aiov;
    575 	auio.uio_iovcnt = 1;
    576 	auio.uio_rw = UIO_READ;
    577 	if (segflg == UIO_SYSSPACE) {
    578 		UIO_SETUP_SYSSPACE(&auio);
    579 	} else {
    580 		KASSERT(l == curlwp);
    581 		auio.uio_vmspace = l->l_proc->p_vmspace;
    582 	}
    583 	auio.uio_resid = count;
    584 	vn_lock(vp, LK_SHARED | LK_RETRY);
    585 	auio.uio_offset = fp->f_offset;
    586 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    587 		    ncookies);
    588 	mutex_enter(&fp->f_lock);
    589 	fp->f_offset = auio.uio_offset;
    590 	mutex_exit(&fp->f_lock);
    591 	VOP_UNLOCK(vp);
    592 	if (error)
    593 		return (error);
    594 
    595 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    596 		struct vnode *ovp = vp;
    597 
    598 		error = (*vn_union_readdir_hook)(&vp, fp, l);
    599 		if (error)
    600 			return (error);
    601 		if (vp != ovp)
    602 			goto unionread;
    603 	}
    604 
    605 	if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
    606 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    607 		struct vnode *tvp = vp;
    608 		vp = vp->v_mount->mnt_vnodecovered;
    609 		vref(vp);
    610 		mutex_enter(&fp->f_lock);
    611 		fp->f_vnode = vp;
    612 		fp->f_offset = 0;
    613 		mutex_exit(&fp->f_lock);
    614 		vrele(tvp);
    615 		goto unionread;
    616 	}
    617 	*done = count - auio.uio_resid;
    618 	return error;
    619 }
    620 
    621 /*
    622  * File table vnode read routine.
    623  */
    624 static int
    625 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    626     int flags)
    627 {
    628 	struct vnode *vp = fp->f_vnode;
    629 	int error, ioflag, fflag;
    630 	size_t count;
    631 
    632 	ioflag = IO_ADV_ENCODE(fp->f_advice);
    633 	fflag = fp->f_flag;
    634 	if (fflag & FNONBLOCK)
    635 		ioflag |= IO_NDELAY;
    636 	if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    637 		ioflag |= IO_SYNC;
    638 	if (fflag & FALTIO)
    639 		ioflag |= IO_ALTSEMANTICS;
    640 	if (fflag & FDIRECT)
    641 		ioflag |= IO_DIRECT;
    642 	vn_lock(vp, LK_SHARED | LK_RETRY);
    643 	uio->uio_offset = *offset;
    644 	count = uio->uio_resid;
    645 	error = VOP_READ(vp, uio, ioflag, cred);
    646 	if (flags & FOF_UPDATE_OFFSET)
    647 		*offset += count - uio->uio_resid;
    648 	VOP_UNLOCK(vp);
    649 	return (error);
    650 }
    651 
    652 /*
    653  * File table vnode write routine.
    654  */
    655 static int
    656 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    657     int flags)
    658 {
    659 	struct vnode *vp = fp->f_vnode;
    660 	int error, ioflag, fflag;
    661 	size_t count;
    662 
    663 	ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
    664 	fflag = fp->f_flag;
    665 	if (vp->v_type == VREG && (fflag & O_APPEND))
    666 		ioflag |= IO_APPEND;
    667 	if (fflag & FNONBLOCK)
    668 		ioflag |= IO_NDELAY;
    669 	if (fflag & FFSYNC ||
    670 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    671 		ioflag |= IO_SYNC;
    672 	else if (fflag & FDSYNC)
    673 		ioflag |= IO_DSYNC;
    674 	if (fflag & FALTIO)
    675 		ioflag |= IO_ALTSEMANTICS;
    676 	if (fflag & FDIRECT)
    677 		ioflag |= IO_DIRECT;
    678 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    679 	uio->uio_offset = *offset;
    680 	count = uio->uio_resid;
    681 
    682 	if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
    683 		goto out;
    684 
    685 	error = VOP_WRITE(vp, uio, ioflag, cred);
    686 
    687 	if (flags & FOF_UPDATE_OFFSET) {
    688 		if (ioflag & IO_APPEND) {
    689 			/*
    690 			 * SUSv3 describes behaviour for count = 0 as following:
    691 			 * "Before any action ... is taken, and if nbyte is zero
    692 			 * and the file is a regular file, the write() function
    693 			 * ... in the absence of errors ... shall return zero
    694 			 * and have no other results."
    695 			 */
    696 			if (count)
    697 				*offset = uio->uio_offset;
    698 		} else
    699 			*offset += count - uio->uio_resid;
    700 	}
    701 
    702  out:
    703 	VOP_UNLOCK(vp);
    704 	return (error);
    705 }
    706 
    707 /*
    708  * File table vnode stat routine.
    709  */
    710 static int
    711 vn_statfile(file_t *fp, struct stat *sb)
    712 {
    713 	struct vnode *vp = fp->f_vnode;
    714 	int error;
    715 
    716 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    717 	error = vn_stat(vp, sb);
    718 	VOP_UNLOCK(vp);
    719 	return error;
    720 }
    721 
    722 int
    723 vn_stat(struct vnode *vp, struct stat *sb)
    724 {
    725 	struct vattr va;
    726 	int error;
    727 	mode_t mode;
    728 
    729 	memset(&va, 0, sizeof(va));
    730 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
    731 	if (error)
    732 		return (error);
    733 	/*
    734 	 * Copy from vattr table
    735 	 */
    736 	memset(sb, 0, sizeof(*sb));
    737 	sb->st_dev = va.va_fsid;
    738 	sb->st_ino = va.va_fileid;
    739 	mode = va.va_mode;
    740 	switch (vp->v_type) {
    741 	case VREG:
    742 		mode |= S_IFREG;
    743 		break;
    744 	case VDIR:
    745 		mode |= S_IFDIR;
    746 		break;
    747 	case VBLK:
    748 		mode |= S_IFBLK;
    749 		break;
    750 	case VCHR:
    751 		mode |= S_IFCHR;
    752 		break;
    753 	case VLNK:
    754 		mode |= S_IFLNK;
    755 		break;
    756 	case VSOCK:
    757 		mode |= S_IFSOCK;
    758 		break;
    759 	case VFIFO:
    760 		mode |= S_IFIFO;
    761 		break;
    762 	default:
    763 		return (EBADF);
    764 	}
    765 	sb->st_mode = mode;
    766 	sb->st_nlink = va.va_nlink;
    767 	sb->st_uid = va.va_uid;
    768 	sb->st_gid = va.va_gid;
    769 	sb->st_rdev = va.va_rdev;
    770 	sb->st_size = va.va_size;
    771 	sb->st_atimespec = va.va_atime;
    772 	sb->st_mtimespec = va.va_mtime;
    773 	sb->st_ctimespec = va.va_ctime;
    774 	sb->st_birthtimespec = va.va_birthtime;
    775 	sb->st_blksize = va.va_blocksize;
    776 	sb->st_flags = va.va_flags;
    777 	sb->st_gen = 0;
    778 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    779 	return (0);
    780 }
    781 
    782 /*
    783  * File table vnode fcntl routine.
    784  */
    785 static int
    786 vn_fcntl(file_t *fp, u_int com, void *data)
    787 {
    788 	struct vnode *vp = fp->f_vnode;
    789 	int error;
    790 
    791 	error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
    792 	return (error);
    793 }
    794 
    795 /*
    796  * File table vnode ioctl routine.
    797  */
    798 static int
    799 vn_ioctl(file_t *fp, u_long com, void *data)
    800 {
    801 	struct vnode *vp = fp->f_vnode, *ovp;
    802 	struct vattr vattr;
    803 	int error;
    804 
    805 	switch (vp->v_type) {
    806 
    807 	case VREG:
    808 	case VDIR:
    809 		if (com == FIONREAD) {
    810 			vn_lock(vp, LK_SHARED | LK_RETRY);
    811 			error = VOP_GETATTR(vp, &vattr, kauth_cred_get());
    812 			VOP_UNLOCK(vp);
    813 			if (error)
    814 				return (error);
    815 			*(int *)data = vattr.va_size - fp->f_offset;
    816 			return (0);
    817 		}
    818 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
    819 			/*
    820 			 * Files don't have send queues, so there never
    821 			 * are any bytes in them, nor is there any
    822 			 * open space in them.
    823 			 */
    824 			*(int *)data = 0;
    825 			return (0);
    826 		}
    827 		if (com == FIOGETBMAP) {
    828 			daddr_t *block;
    829 
    830 			if (*(daddr_t *)data < 0)
    831 				return (EINVAL);
    832 			block = (daddr_t *)data;
    833 			vn_lock(vp, LK_SHARED | LK_RETRY);
    834 			error = VOP_BMAP(vp, *block, NULL, block, NULL);
    835 			VOP_UNLOCK(vp);
    836 			return error;
    837 		}
    838 		if (com == OFIOGETBMAP) {
    839 			daddr_t ibn, obn;
    840 
    841 			if (*(int32_t *)data < 0)
    842 				return (EINVAL);
    843 			ibn = (daddr_t)*(int32_t *)data;
    844 			vn_lock(vp, LK_SHARED | LK_RETRY);
    845 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    846 			VOP_UNLOCK(vp);
    847 			*(int32_t *)data = (int32_t)obn;
    848 			return error;
    849 		}
    850 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    851 			return (0);			/* XXX */
    852 		/* FALLTHROUGH */
    853 	case VFIFO:
    854 	case VCHR:
    855 	case VBLK:
    856 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
    857 		    kauth_cred_get());
    858 		if (error == 0 && com == TIOCSCTTY) {
    859 			vref(vp);
    860 			mutex_enter(&proc_lock);
    861 			ovp = curproc->p_session->s_ttyvp;
    862 			curproc->p_session->s_ttyvp = vp;
    863 			mutex_exit(&proc_lock);
    864 			if (ovp != NULL)
    865 				vrele(ovp);
    866 		}
    867 		return (error);
    868 
    869 	default:
    870 		return (EPASSTHROUGH);
    871 	}
    872 }
    873 
    874 /*
    875  * File table vnode poll routine.
    876  */
    877 static int
    878 vn_poll(file_t *fp, int events)
    879 {
    880 
    881 	return (VOP_POLL(fp->f_vnode, events));
    882 }
    883 
    884 /*
    885  * File table vnode kqfilter routine.
    886  */
    887 int
    888 vn_kqfilter(file_t *fp, struct knote *kn)
    889 {
    890 
    891 	return (VOP_KQFILTER(fp->f_vnode, kn));
    892 }
    893 
    894 static int
    895 vn_mmap(struct file *fp, off_t *offp, size_t size, int prot, int *flagsp,
    896 	int *advicep, struct uvm_object **uobjp, int *maxprotp)
    897 {
    898 	struct uvm_object *uobj;
    899 	struct vnode *vp;
    900 	struct vattr va;
    901 	struct lwp *l;
    902 	vm_prot_t maxprot;
    903 	off_t off;
    904 	int error, flags;
    905 	bool needwritemap;
    906 
    907 	l = curlwp;
    908 
    909 	off = *offp;
    910 	flags = *flagsp;
    911 	maxprot = VM_PROT_EXECUTE;
    912 
    913 	vp = fp->f_vnode;
    914 	if (vp->v_type != VREG && vp->v_type != VCHR &&
    915 	    vp->v_type != VBLK) {
    916 		/* only REG/CHR/BLK support mmap */
    917 		return ENODEV;
    918 	}
    919 	if (vp->v_type != VCHR && off < 0) {
    920 		return EINVAL;
    921 	}
    922 	if (vp->v_type != VCHR && (off_t)(off + size) < off) {
    923 		/* no offset wrapping */
    924 		return EOVERFLOW;
    925 	}
    926 
    927 	/* special case: catch SunOS style /dev/zero */
    928 	if (vp->v_type == VCHR &&
    929 	    (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
    930 		*uobjp = NULL;
    931 		*maxprotp = VM_PROT_ALL;
    932 		return 0;
    933 	}
    934 
    935 	/*
    936 	 * Old programs may not select a specific sharing type, so
    937 	 * default to an appropriate one.
    938 	 *
    939 	 * XXX: how does MAP_ANON fit in the picture?
    940 	 */
    941 	if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
    942 #if defined(DEBUG)
    943 		struct proc *p = l->l_proc;
    944 		printf("WARNING: defaulted mmap() share type to "
    945 		       "%s (pid %d command %s)\n", vp->v_type == VCHR ?
    946 		       "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
    947 		       p->p_comm);
    948 #endif
    949 		if (vp->v_type == VCHR)
    950 			flags |= MAP_SHARED;	/* for a device */
    951 		else
    952 			flags |= MAP_PRIVATE;	/* for a file */
    953 	}
    954 
    955 	/*
    956 	 * MAP_PRIVATE device mappings don't make sense (and aren't
    957 	 * supported anyway).  However, some programs rely on this,
    958 	 * so just change it to MAP_SHARED.
    959 	 */
    960 	if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
    961 		flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
    962 	}
    963 
    964 	/*
    965 	 * now check protection
    966 	 */
    967 
    968 	/* check read access */
    969 	if (fp->f_flag & FREAD)
    970 		maxprot |= VM_PROT_READ;
    971 	else if (prot & PROT_READ) {
    972 		return EACCES;
    973 	}
    974 
    975 	/* check write access, shared case first */
    976 	if (flags & MAP_SHARED) {
    977 		/*
    978 		 * if the file is writable, only add PROT_WRITE to
    979 		 * maxprot if the file is not immutable, append-only.
    980 		 * otherwise, if we have asked for PROT_WRITE, return
    981 		 * EPERM.
    982 		 */
    983 		if (fp->f_flag & FWRITE) {
    984 			vn_lock(vp, LK_SHARED | LK_RETRY);
    985 			error = VOP_GETATTR(vp, &va, l->l_cred);
    986 			VOP_UNLOCK(vp);
    987 			if (error) {
    988 				return error;
    989 			}
    990 			if ((va.va_flags &
    991 			     (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
    992 				maxprot |= VM_PROT_WRITE;
    993 			else if (prot & PROT_WRITE) {
    994 				return EPERM;
    995 			}
    996 		} else if (prot & PROT_WRITE) {
    997 			return EACCES;
    998 		}
    999 	} else {
   1000 		/* MAP_PRIVATE mappings can always write to */
   1001 		maxprot |= VM_PROT_WRITE;
   1002 	}
   1003 
   1004 	/*
   1005 	 * Don't allow mmap for EXEC if the file system
   1006 	 * is mounted NOEXEC.
   1007 	 */
   1008 	if ((prot & PROT_EXEC) != 0 &&
   1009 	    (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
   1010 		return EACCES;
   1011 	}
   1012 
   1013 	if (vp->v_type != VCHR) {
   1014 		error = VOP_MMAP(vp, prot, curlwp->l_cred);
   1015 		if (error) {
   1016 			return error;
   1017 		}
   1018 		vref(vp);
   1019 		uobj = &vp->v_uobj;
   1020 
   1021 		/*
   1022 		 * If the vnode is being mapped with PROT_EXEC,
   1023 		 * then mark it as text.
   1024 		 */
   1025 		if (prot & PROT_EXEC) {
   1026 			vn_markexec(vp);
   1027 		}
   1028 	} else {
   1029 		int i = maxprot;
   1030 
   1031 		/*
   1032 		 * XXX Some devices don't like to be mapped with
   1033 		 * XXX PROT_EXEC or PROT_WRITE, but we don't really
   1034 		 * XXX have a better way of handling this, right now
   1035 		 */
   1036 		do {
   1037 			uobj = udv_attach(vp->v_rdev,
   1038 					  (flags & MAP_SHARED) ? i :
   1039 					  (i & ~VM_PROT_WRITE), off, size);
   1040 			i--;
   1041 		} while ((uobj == NULL) && (i > 0));
   1042 		if (uobj == NULL) {
   1043 			return EINVAL;
   1044 		}
   1045 		*advicep = UVM_ADV_RANDOM;
   1046 	}
   1047 
   1048 	/*
   1049 	 * Set vnode flags to indicate the new kinds of mapping.
   1050 	 * We take the vnode lock in exclusive mode here to serialize
   1051 	 * with direct I/O.
   1052 	 *
   1053 	 * Safe to check for these flag values without a lock, as
   1054 	 * long as a reference to the vnode is held.
   1055 	 */
   1056 	needwritemap = (vp->v_iflag & VI_WRMAP) == 0 &&
   1057 		(flags & MAP_SHARED) != 0 &&
   1058 		(maxprot & VM_PROT_WRITE) != 0;
   1059 	if ((vp->v_vflag & VV_MAPPED) == 0 || needwritemap) {
   1060 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1061 		vp->v_vflag |= VV_MAPPED;
   1062 		if (needwritemap) {
   1063 			rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
   1064 			mutex_enter(vp->v_interlock);
   1065 			vp->v_iflag |= VI_WRMAP;
   1066 			mutex_exit(vp->v_interlock);
   1067 			rw_exit(vp->v_uobj.vmobjlock);
   1068 		}
   1069 		VOP_UNLOCK(vp);
   1070 	}
   1071 
   1072 #if NVERIEXEC > 0
   1073 
   1074 	/*
   1075 	 * Check if the file can be executed indirectly.
   1076 	 *
   1077 	 * XXX: This gives false warnings about "Incorrect access type"
   1078 	 * XXX: if the mapping is not executable. Harmless, but will be
   1079 	 * XXX: fixed as part of other changes.
   1080 	 */
   1081 	if (veriexec_verify(l, vp, "(mmap)", VERIEXEC_INDIRECT,
   1082 			    NULL)) {
   1083 
   1084 		/*
   1085 		 * Don't allow executable mappings if we can't
   1086 		 * indirectly execute the file.
   1087 		 */
   1088 		if (prot & VM_PROT_EXECUTE) {
   1089 			return EPERM;
   1090 		}
   1091 
   1092 		/*
   1093 		 * Strip the executable bit from 'maxprot' to make sure
   1094 		 * it can't be made executable later.
   1095 		 */
   1096 		maxprot &= ~VM_PROT_EXECUTE;
   1097 	}
   1098 #endif /* NVERIEXEC > 0 */
   1099 
   1100 	*uobjp = uobj;
   1101 	*maxprotp = maxprot;
   1102 	*flagsp = flags;
   1103 
   1104 	return 0;
   1105 }
   1106 
   1107 
   1108 
   1109 /*
   1110  * Check that the vnode is still valid, and if so
   1111  * acquire requested lock.
   1112  */
   1113 int
   1114 vn_lock(struct vnode *vp, int flags)
   1115 {
   1116 	struct lwp *l;
   1117 	int error;
   1118 
   1119 #if 0
   1120 	KASSERT(vrefcnt(vp) > 0 || (vp->v_iflag & VI_ONWORKLST) != 0);
   1121 #endif
   1122 	KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY|
   1123 	    LK_UPGRADE|LK_DOWNGRADE)) == 0);
   1124 	KASSERT((flags & LK_NOWAIT) != 0 || !mutex_owned(vp->v_interlock));
   1125 
   1126 #ifdef DIAGNOSTIC
   1127 	if (wapbl_vphaswapbl(vp))
   1128 		WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
   1129 #endif
   1130 
   1131 	/* Get a more useful report for lockstat. */
   1132 	l = curlwp;
   1133 	KASSERT(l->l_rwcallsite == 0);
   1134 	l->l_rwcallsite = (uintptr_t)__builtin_return_address(0);
   1135 
   1136 	error = VOP_LOCK(vp, flags);
   1137 	if ((flags & LK_RETRY) != 0 && error == ENOENT)
   1138 		error = VOP_LOCK(vp, flags);
   1139 
   1140 	l->l_rwcallsite = 0;
   1141 
   1142 	KASSERT((flags & LK_RETRY) == 0 || (flags & LK_NOWAIT) != 0 ||
   1143 	    error == 0);
   1144 
   1145 	return error;
   1146 }
   1147 
   1148 /*
   1149  * File table vnode close routine.
   1150  */
   1151 static int
   1152 vn_closefile(file_t *fp)
   1153 {
   1154 
   1155 	return vn_close(fp->f_vnode, fp->f_flag, fp->f_cred);
   1156 }
   1157 
   1158 /*
   1159  * Simplified in-kernel wrapper calls for extended attribute access.
   1160  * Both calls pass in a NULL credential, authorizing a "kernel" access.
   1161  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
   1162  */
   1163 int
   1164 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
   1165     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
   1166 {
   1167 	struct uio auio;
   1168 	struct iovec aiov;
   1169 	int error;
   1170 
   1171 	aiov.iov_len = *buflen;
   1172 	aiov.iov_base = bf;
   1173 
   1174 	auio.uio_iov = &aiov;
   1175 	auio.uio_iovcnt = 1;
   1176 	auio.uio_rw = UIO_READ;
   1177 	auio.uio_offset = 0;
   1178 	auio.uio_resid = *buflen;
   1179 	UIO_SETUP_SYSSPACE(&auio);
   1180 
   1181 	if ((ioflg & IO_NODELOCKED) == 0)
   1182 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1183 
   1184 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL,
   1185 	    NOCRED);
   1186 
   1187 	if ((ioflg & IO_NODELOCKED) == 0)
   1188 		VOP_UNLOCK(vp);
   1189 
   1190 	if (error == 0)
   1191 		*buflen = *buflen - auio.uio_resid;
   1192 
   1193 	return (error);
   1194 }
   1195 
   1196 /*
   1197  * XXX Failure mode if partially written?
   1198  */
   1199 int
   1200 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
   1201     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
   1202 {
   1203 	struct uio auio;
   1204 	struct iovec aiov;
   1205 	int error;
   1206 
   1207 	aiov.iov_len = buflen;
   1208 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
   1209 
   1210 	auio.uio_iov = &aiov;
   1211 	auio.uio_iovcnt = 1;
   1212 	auio.uio_rw = UIO_WRITE;
   1213 	auio.uio_offset = 0;
   1214 	auio.uio_resid = buflen;
   1215 	UIO_SETUP_SYSSPACE(&auio);
   1216 
   1217 	if ((ioflg & IO_NODELOCKED) == 0) {
   1218 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1219 	}
   1220 
   1221 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NOCRED);
   1222 
   1223 	if ((ioflg & IO_NODELOCKED) == 0) {
   1224 		VOP_UNLOCK(vp);
   1225 	}
   1226 
   1227 	return (error);
   1228 }
   1229 
   1230 int
   1231 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
   1232     const char *attrname, struct lwp *l)
   1233 {
   1234 	int error;
   1235 
   1236 	if ((ioflg & IO_NODELOCKED) == 0) {
   1237 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1238 	}
   1239 
   1240 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NOCRED);
   1241 	if (error == EOPNOTSUPP)
   1242 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
   1243 		    NOCRED);
   1244 
   1245 	if ((ioflg & IO_NODELOCKED) == 0) {
   1246 		VOP_UNLOCK(vp);
   1247 	}
   1248 
   1249 	return (error);
   1250 }
   1251 
   1252 int
   1253 vn_fifo_bypass(void *v)
   1254 {
   1255 	struct vop_generic_args *ap = v;
   1256 
   1257 	return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v);
   1258 }
   1259 
   1260 /*
   1261  * Open block device by device number
   1262  */
   1263 int
   1264 vn_bdev_open(dev_t dev, struct vnode **vpp, struct lwp *l)
   1265 {
   1266 	int     error;
   1267 
   1268 	if ((error = bdevvp(dev, vpp)) != 0)
   1269 		return error;
   1270 
   1271 	if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
   1272 		vrele(*vpp);
   1273 		return error;
   1274 	}
   1275 	mutex_enter((*vpp)->v_interlock);
   1276 	(*vpp)->v_writecount++;
   1277 	mutex_exit((*vpp)->v_interlock);
   1278 
   1279 	return 0;
   1280 }
   1281 
   1282 /*
   1283  * Lookup the provided name in the filesystem.  If the file exists,
   1284  * is a valid block device, and isn't being used by anyone else,
   1285  * set *vpp to the file's vnode.
   1286  */
   1287 int
   1288 vn_bdev_openpath(struct pathbuf *pb, struct vnode **vpp, struct lwp *l)
   1289 {
   1290 	struct vnode *vp;
   1291 	dev_t dev;
   1292 	enum vtype vt;
   1293 	int     error;
   1294 
   1295 	error = vn_open(NULL, pb, 0, FREAD | FWRITE, 0, &vp, NULL, NULL);
   1296 	if (error != 0)
   1297 		return error;
   1298 
   1299 	dev = vp->v_rdev;
   1300 	vt = vp->v_type;
   1301 
   1302 	VOP_UNLOCK(vp);
   1303 	(void) vn_close(vp, FREAD | FWRITE, l->l_cred);
   1304 
   1305 	if (vt != VBLK)
   1306 		return ENOTBLK;
   1307 
   1308 	return vn_bdev_open(dev, vpp, l);
   1309 }
   1310