Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.153
      1 /*	$NetBSD: genfs_vnops.c,v 1.153 2007/05/17 07:26:22 hannken Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.153 2007/05/17 07:26:22 hannken Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/proc.h>
     39 #include <sys/kernel.h>
     40 #include <sys/mount.h>
     41 #include <sys/namei.h>
     42 #include <sys/vnode.h>
     43 #include <sys/fcntl.h>
     44 #include <sys/kmem.h>
     45 #include <sys/poll.h>
     46 #include <sys/mman.h>
     47 #include <sys/file.h>
     48 #include <sys/kauth.h>
     49 #include <sys/fstrans.h>
     50 
     51 #include <miscfs/genfs/genfs.h>
     52 #include <miscfs/genfs/genfs_node.h>
     53 #include <miscfs/specfs/specdev.h>
     54 
     55 #include <uvm/uvm.h>
     56 #include <uvm/uvm_pager.h>
     57 
     58 static int genfs_do_directio(struct vmspace *, vaddr_t, size_t, struct vnode *,
     59     off_t, enum uio_rw);
     60 static void genfs_dio_iodone(struct buf *);
     61 
     62 static int genfs_do_io(struct vnode *, off_t, vaddr_t, size_t, int, enum uio_rw,
     63     void (*)(struct buf *));
     64 static inline void genfs_rel_pages(struct vm_page **, int);
     65 static void filt_genfsdetach(struct knote *);
     66 static int filt_genfsread(struct knote *, long);
     67 static int filt_genfsvnode(struct knote *, long);
     68 
     69 #define MAX_READ_PAGES	16 	/* XXXUBC 16 */
     70 
     71 int genfs_maxdio = MAXPHYS;
     72 
     73 int
     74 genfs_poll(void *v)
     75 {
     76 	struct vop_poll_args /* {
     77 		struct vnode *a_vp;
     78 		int a_events;
     79 		struct lwp *a_l;
     80 	} */ *ap = v;
     81 
     82 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
     83 }
     84 
     85 int
     86 genfs_seek(void *v)
     87 {
     88 	struct vop_seek_args /* {
     89 		struct vnode *a_vp;
     90 		off_t a_oldoff;
     91 		off_t a_newoff;
     92 		kauth_cred_t cred;
     93 	} */ *ap = v;
     94 
     95 	if (ap->a_newoff < 0)
     96 		return (EINVAL);
     97 
     98 	return (0);
     99 }
    100 
    101 int
    102 genfs_abortop(void *v)
    103 {
    104 	struct vop_abortop_args /* {
    105 		struct vnode *a_dvp;
    106 		struct componentname *a_cnp;
    107 	} */ *ap = v;
    108 
    109 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    110 		PNBUF_PUT(ap->a_cnp->cn_pnbuf);
    111 	return (0);
    112 }
    113 
    114 int
    115 genfs_fcntl(void *v)
    116 {
    117 	struct vop_fcntl_args /* {
    118 		struct vnode *a_vp;
    119 		u_int a_command;
    120 		void *a_data;
    121 		int a_fflag;
    122 		kauth_cred_t a_cred;
    123 		struct lwp *a_l;
    124 	} */ *ap = v;
    125 
    126 	if (ap->a_command == F_SETFL)
    127 		return (0);
    128 	else
    129 		return (EOPNOTSUPP);
    130 }
    131 
    132 /*ARGSUSED*/
    133 int
    134 genfs_badop(void *v)
    135 {
    136 
    137 	panic("genfs: bad op");
    138 }
    139 
    140 /*ARGSUSED*/
    141 int
    142 genfs_nullop(void *v)
    143 {
    144 
    145 	return (0);
    146 }
    147 
    148 /*ARGSUSED*/
    149 int
    150 genfs_einval(void *v)
    151 {
    152 
    153 	return (EINVAL);
    154 }
    155 
    156 /*
    157  * Called when an fs doesn't support a particular vop.
    158  * This takes care to vrele, vput, or vunlock passed in vnodes.
    159  */
    160 int
    161 genfs_eopnotsupp(void *v)
    162 {
    163 	struct vop_generic_args /*
    164 		struct vnodeop_desc *a_desc;
    165 		/ * other random data follows, presumably * /
    166 	} */ *ap = v;
    167 	struct vnodeop_desc *desc = ap->a_desc;
    168 	struct vnode *vp, *vp_last = NULL;
    169 	int flags, i, j, offset;
    170 
    171 	flags = desc->vdesc_flags;
    172 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    173 		if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    174 			break;	/* stop at end of list */
    175 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    176 			vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
    177 
    178 			/* Skip if NULL */
    179 			if (!vp)
    180 				continue;
    181 
    182 			switch (j) {
    183 			case VDESC_VP0_WILLPUT:
    184 				/* Check for dvp == vp cases */
    185 				if (vp == vp_last)
    186 					vrele(vp);
    187 				else {
    188 					vput(vp);
    189 					vp_last = vp;
    190 				}
    191 				break;
    192 			case VDESC_VP0_WILLUNLOCK:
    193 				VOP_UNLOCK(vp, 0);
    194 				break;
    195 			case VDESC_VP0_WILLRELE:
    196 				vrele(vp);
    197 				break;
    198 			}
    199 		}
    200 	}
    201 
    202 	return (EOPNOTSUPP);
    203 }
    204 
    205 /*ARGSUSED*/
    206 int
    207 genfs_ebadf(void *v)
    208 {
    209 
    210 	return (EBADF);
    211 }
    212 
    213 /* ARGSUSED */
    214 int
    215 genfs_enoioctl(void *v)
    216 {
    217 
    218 	return (EPASSTHROUGH);
    219 }
    220 
    221 
    222 /*
    223  * Eliminate all activity associated with the requested vnode
    224  * and with all vnodes aliased to the requested vnode.
    225  */
    226 int
    227 genfs_revoke(void *v)
    228 {
    229 	struct vop_revoke_args /* {
    230 		struct vnode *a_vp;
    231 		int a_flags;
    232 	} */ *ap = v;
    233 	struct vnode *vp, *vq;
    234 	struct lwp *l = curlwp;		/* XXX */
    235 
    236 #ifdef DIAGNOSTIC
    237 	if ((ap->a_flags & REVOKEALL) == 0)
    238 		panic("genfs_revoke: not revokeall");
    239 #endif
    240 
    241 	vp = ap->a_vp;
    242 	simple_lock(&vp->v_interlock);
    243 
    244 	if (vp->v_flag & VALIASED) {
    245 		/*
    246 		 * If a vgone (or vclean) is already in progress,
    247 		 * wait until it is done and return.
    248 		 */
    249 		if (vp->v_flag & VXLOCK) {
    250 			vp->v_flag |= VXWANT;
    251 			ltsleep(vp, PINOD|PNORELOCK, "vop_revokeall", 0,
    252 				&vp->v_interlock);
    253 			return (0);
    254 		}
    255 		/*
    256 		 * Ensure that vp will not be vgone'd while we
    257 		 * are eliminating its aliases.
    258 		 */
    259 		vp->v_flag |= VXLOCK;
    260 		simple_unlock(&vp->v_interlock);
    261 		while (vp->v_flag & VALIASED) {
    262 			simple_lock(&spechash_slock);
    263 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    264 				if (vq->v_rdev != vp->v_rdev ||
    265 				    vq->v_type != vp->v_type || vp == vq)
    266 					continue;
    267 				simple_unlock(&spechash_slock);
    268 				vgone(vq);
    269 				break;
    270 			}
    271 			if (vq == NULLVP)
    272 				simple_unlock(&spechash_slock);
    273 		}
    274 		/*
    275 		 * Remove the lock so that vgone below will
    276 		 * really eliminate the vnode after which time
    277 		 * vgone will awaken any sleepers.
    278 		 */
    279 		simple_lock(&vp->v_interlock);
    280 		vp->v_flag &= ~VXLOCK;
    281 	}
    282 	vgonel(vp, l);
    283 	return (0);
    284 }
    285 
    286 /*
    287  * Lock the node.
    288  */
    289 int
    290 genfs_lock(void *v)
    291 {
    292 	struct vop_lock_args /* {
    293 		struct vnode *a_vp;
    294 		int a_flags;
    295 	} */ *ap = v;
    296 	struct vnode *vp = ap->a_vp;
    297 
    298 	return (lockmgr(vp->v_vnlock, ap->a_flags, &vp->v_interlock));
    299 }
    300 
    301 /*
    302  * Unlock the node.
    303  */
    304 int
    305 genfs_unlock(void *v)
    306 {
    307 	struct vop_unlock_args /* {
    308 		struct vnode *a_vp;
    309 		int a_flags;
    310 	} */ *ap = v;
    311 	struct vnode *vp = ap->a_vp;
    312 
    313 	return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE,
    314 	    &vp->v_interlock));
    315 }
    316 
    317 /*
    318  * Return whether or not the node is locked.
    319  */
    320 int
    321 genfs_islocked(void *v)
    322 {
    323 	struct vop_islocked_args /* {
    324 		struct vnode *a_vp;
    325 	} */ *ap = v;
    326 	struct vnode *vp = ap->a_vp;
    327 
    328 	return (lockstatus(vp->v_vnlock));
    329 }
    330 
    331 /*
    332  * Stubs to use when there is no locking to be done on the underlying object.
    333  */
    334 int
    335 genfs_nolock(void *v)
    336 {
    337 	struct vop_lock_args /* {
    338 		struct vnode *a_vp;
    339 		int a_flags;
    340 		struct lwp *a_l;
    341 	} */ *ap = v;
    342 
    343 	/*
    344 	 * Since we are not using the lock manager, we must clear
    345 	 * the interlock here.
    346 	 */
    347 	if (ap->a_flags & LK_INTERLOCK)
    348 		simple_unlock(&ap->a_vp->v_interlock);
    349 	return (0);
    350 }
    351 
    352 int
    353 genfs_nounlock(void *v)
    354 {
    355 
    356 	return (0);
    357 }
    358 
    359 int
    360 genfs_noislocked(void *v)
    361 {
    362 
    363 	return (0);
    364 }
    365 
    366 /*
    367  * Local lease check.
    368  */
    369 int
    370 genfs_lease_check(void *v)
    371 {
    372 
    373 	return (0);
    374 }
    375 
    376 int
    377 genfs_mmap(void *v)
    378 {
    379 
    380 	return (0);
    381 }
    382 
    383 static inline void
    384 genfs_rel_pages(struct vm_page **pgs, int npages)
    385 {
    386 	int i;
    387 
    388 	for (i = 0; i < npages; i++) {
    389 		struct vm_page *pg = pgs[i];
    390 
    391 		if (pg == NULL || pg == PGO_DONTCARE)
    392 			continue;
    393 		if (pg->flags & PG_FAKE) {
    394 			pg->flags |= PG_RELEASED;
    395 		}
    396 	}
    397 	uvm_lock_pageq();
    398 	uvm_page_unbusy(pgs, npages);
    399 	uvm_unlock_pageq();
    400 }
    401 
    402 /*
    403  * generic VM getpages routine.
    404  * Return PG_BUSY pages for the given range,
    405  * reading from backing store if necessary.
    406  */
    407 
    408 int
    409 genfs_getpages(void *v)
    410 {
    411 	struct vop_getpages_args /* {
    412 		struct vnode *a_vp;
    413 		voff_t a_offset;
    414 		struct vm_page **a_m;
    415 		int *a_count;
    416 		int a_centeridx;
    417 		vm_prot_t a_access_type;
    418 		int a_advice;
    419 		int a_flags;
    420 	} */ *ap = v;
    421 
    422 	off_t newsize, diskeof, memeof;
    423 	off_t offset, origoffset, startoffset, endoffset;
    424 	daddr_t lbn, blkno;
    425 	int i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
    426 	int fs_bshift, fs_bsize, dev_bshift;
    427 	int flags = ap->a_flags;
    428 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    429 	vaddr_t kva;
    430 	struct buf *bp, *mbp;
    431 	struct vnode *vp = ap->a_vp;
    432 	struct vnode *devvp;
    433 	struct genfs_node *gp = VTOG(vp);
    434 	struct uvm_object *uobj = &vp->v_uobj;
    435 	struct vm_page *pg, **pgs, *pgs_onstack[MAX_READ_PAGES];
    436 	int pgs_size;
    437 	kauth_cred_t cred = curlwp->l_cred;		/* XXXUBC curlwp */
    438 	bool async = (flags & PGO_SYNCIO) == 0;
    439 	bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
    440 	bool sawhole = false;
    441 	bool has_trans = false;
    442 	bool overwrite = (flags & PGO_OVERWRITE) != 0;
    443 	bool blockalloc = write && (flags & PGO_NOBLOCKALLOC) == 0;
    444 	voff_t origvsize;
    445 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
    446 
    447 	UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d",
    448 	    vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
    449 
    450 	KASSERT(vp->v_type == VREG || vp->v_type == VDIR ||
    451 	    vp->v_type == VLNK || vp->v_type == VBLK);
    452 
    453 	/* XXXUBC temp limit */
    454 	if (*ap->a_count > MAX_READ_PAGES) {
    455 		panic("genfs_getpages: too many pages");
    456 	}
    457 
    458 	pgs = pgs_onstack;
    459 	pgs_size = sizeof(pgs_onstack);
    460 
    461 startover:
    462 	error = 0;
    463 	origvsize = vp->v_size;
    464 	origoffset = ap->a_offset;
    465 	orignpages = *ap->a_count;
    466 	GOP_SIZE(vp, origvsize, &diskeof, 0);
    467 	if (flags & PGO_PASTEOF) {
    468 		newsize = MAX(origvsize,
    469 		    origoffset + (orignpages << PAGE_SHIFT));
    470 		GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_MEM);
    471 	} else {
    472 		GOP_SIZE(vp, origvsize, &memeof, GOP_SIZE_MEM);
    473 	}
    474 	KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
    475 	KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
    476 	KASSERT(orignpages > 0);
    477 
    478 	/*
    479 	 * Bounds-check the request.
    480 	 */
    481 
    482 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
    483 		if ((flags & PGO_LOCKED) == 0) {
    484 			simple_unlock(&uobj->vmobjlock);
    485 		}
    486 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
    487 		    origoffset, *ap->a_count, memeof,0);
    488 		error = EINVAL;
    489 		goto out_err;
    490 	}
    491 
    492 	/* uobj is locked */
    493 
    494 	if ((flags & PGO_NOTIMESTAMP) == 0 &&
    495 	    (vp->v_type != VBLK ||
    496 	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
    497 		int updflags = 0;
    498 
    499 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) {
    500 			updflags = GOP_UPDATE_ACCESSED;
    501 		}
    502 		if (write) {
    503 			updflags |= GOP_UPDATE_MODIFIED;
    504 		}
    505 		if (updflags != 0) {
    506 			GOP_MARKUPDATE(vp, updflags);
    507 		}
    508 	}
    509 
    510 	if (write) {
    511 		gp->g_dirtygen++;
    512 		if ((vp->v_flag & VONWORKLST) == 0) {
    513 			vn_syncer_add_to_worklist(vp, filedelay);
    514 		}
    515 		if ((vp->v_flag & (VWRITEMAP|VWRITEMAPDIRTY)) == VWRITEMAP) {
    516 			vp->v_flag |= VWRITEMAPDIRTY;
    517 		}
    518 	}
    519 
    520 	/*
    521 	 * For PGO_LOCKED requests, just return whatever's in memory.
    522 	 */
    523 
    524 	if (flags & PGO_LOCKED) {
    525 		int nfound;
    526 
    527 		npages = *ap->a_count;
    528 #if defined(DEBUG)
    529 		for (i = 0; i < npages; i++) {
    530 			pg = ap->a_m[i];
    531 			KASSERT(pg == NULL || pg == PGO_DONTCARE);
    532 		}
    533 #endif /* defined(DEBUG) */
    534 		nfound = uvn_findpages(uobj, origoffset, &npages,
    535 		    ap->a_m, UFP_NOWAIT|UFP_NOALLOC|(write ? UFP_NORDONLY : 0));
    536 		KASSERT(npages == *ap->a_count);
    537 		if (nfound == 0) {
    538 			error = EBUSY;
    539 			goto out_err;
    540 		}
    541 		if (!rw_tryenter(&gp->g_glock, RW_READER)) {
    542 			genfs_rel_pages(ap->a_m, npages);
    543 
    544 			/*
    545 			 * restore the array.
    546 			 */
    547 
    548 			for (i = 0; i < npages; i++) {
    549 				pg = ap->a_m[i];
    550 
    551 				if (pg != NULL || pg != PGO_DONTCARE) {
    552 					ap->a_m[i] = NULL;
    553 				}
    554 			}
    555 		} else {
    556 			rw_exit(&gp->g_glock);
    557 		}
    558 		error = (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
    559 		goto out_err;
    560 	}
    561 	simple_unlock(&uobj->vmobjlock);
    562 
    563 	/*
    564 	 * find the requested pages and make some simple checks.
    565 	 * leave space in the page array for a whole block.
    566 	 */
    567 
    568 	if (vp->v_type != VBLK) {
    569 		fs_bshift = vp->v_mount->mnt_fs_bshift;
    570 		dev_bshift = vp->v_mount->mnt_dev_bshift;
    571 	} else {
    572 		fs_bshift = DEV_BSHIFT;
    573 		dev_bshift = DEV_BSHIFT;
    574 	}
    575 	fs_bsize = 1 << fs_bshift;
    576 
    577 	orignpages = MIN(orignpages,
    578 	    round_page(memeof - origoffset) >> PAGE_SHIFT);
    579 	npages = orignpages;
    580 	startoffset = origoffset & ~(fs_bsize - 1);
    581 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT) +
    582 	    fs_bsize - 1) & ~(fs_bsize - 1));
    583 	endoffset = MIN(endoffset, round_page(memeof));
    584 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    585 
    586 	pgs_size = sizeof(struct vm_page *) *
    587 	    ((endoffset - startoffset) >> PAGE_SHIFT);
    588 	if (pgs_size > sizeof(pgs_onstack)) {
    589 		pgs = kmem_zalloc(pgs_size, async ? KM_NOSLEEP : KM_SLEEP);
    590 		if (pgs == NULL) {
    591 			pgs = pgs_onstack;
    592 			error = ENOMEM;
    593 			goto out_err;
    594 		}
    595 	} else {
    596 		/* pgs == pgs_onstack */
    597 		memset(pgs, 0, pgs_size);
    598 	}
    599 	UVMHIST_LOG(ubchist, "ridx %d npages %d startoff %ld endoff %ld",
    600 	    ridx, npages, startoffset, endoffset);
    601 
    602 	if (!has_trans) {
    603 		fstrans_start(vp->v_mount, FSTRANS_SHARED);
    604 		has_trans = true;
    605 	}
    606 
    607 	/*
    608 	 * hold g_glock to prevent a race with truncate.
    609 	 *
    610 	 * check if our idea of v_size is still valid.
    611 	 */
    612 
    613 	if (blockalloc) {
    614 		rw_enter(&gp->g_glock, RW_WRITER);
    615 	} else {
    616 		rw_enter(&gp->g_glock, RW_READER);
    617 	}
    618 	simple_lock(&uobj->vmobjlock);
    619 	if (vp->v_size < origvsize) {
    620 		rw_exit(&gp->g_glock);
    621 		if (pgs != pgs_onstack)
    622 			kmem_free(pgs, pgs_size);
    623 		goto startover;
    624 	}
    625 
    626 	if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx],
    627 	    async ? UFP_NOWAIT : UFP_ALL) != orignpages) {
    628 		rw_exit(&gp->g_glock);
    629 		KASSERT(async != 0);
    630 		genfs_rel_pages(&pgs[ridx], orignpages);
    631 		simple_unlock(&uobj->vmobjlock);
    632 		error = EBUSY;
    633 		goto out_err;
    634 	}
    635 
    636 	/*
    637 	 * if the pages are already resident, just return them.
    638 	 */
    639 
    640 	for (i = 0; i < npages; i++) {
    641 		struct vm_page *pg1 = pgs[ridx + i];
    642 
    643 		if ((pg1->flags & PG_FAKE) ||
    644 		    (blockalloc && (pg1->flags & PG_RDONLY))) {
    645 			break;
    646 		}
    647 	}
    648 	if (i == npages) {
    649 		rw_exit(&gp->g_glock);
    650 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    651 		npages += ridx;
    652 		goto out;
    653 	}
    654 
    655 	/*
    656 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    657 	 */
    658 
    659 	if (overwrite) {
    660 		rw_exit(&gp->g_glock);
    661 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    662 
    663 		for (i = 0; i < npages; i++) {
    664 			struct vm_page *pg1 = pgs[ridx + i];
    665 
    666 			pg1->flags &= ~(PG_RDONLY|PG_CLEAN);
    667 		}
    668 		npages += ridx;
    669 		goto out;
    670 	}
    671 
    672 	/*
    673 	 * the page wasn't resident and we're not overwriting,
    674 	 * so we're going to have to do some i/o.
    675 	 * find any additional pages needed to cover the expanded range.
    676 	 */
    677 
    678 	npages = (endoffset - startoffset) >> PAGE_SHIFT;
    679 	if (startoffset != origoffset || npages != orignpages) {
    680 
    681 		/*
    682 		 * we need to avoid deadlocks caused by locking
    683 		 * additional pages at lower offsets than pages we
    684 		 * already have locked.  unlock them all and start over.
    685 		 */
    686 
    687 		genfs_rel_pages(&pgs[ridx], orignpages);
    688 		memset(pgs, 0, pgs_size);
    689 
    690 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
    691 		    startoffset, endoffset, 0,0);
    692 		npgs = npages;
    693 		if (uvn_findpages(uobj, startoffset, &npgs, pgs,
    694 		    async ? UFP_NOWAIT : UFP_ALL) != npages) {
    695 			rw_exit(&gp->g_glock);
    696 			KASSERT(async != 0);
    697 			genfs_rel_pages(pgs, npages);
    698 			simple_unlock(&uobj->vmobjlock);
    699 			error = EBUSY;
    700 			goto out_err;
    701 		}
    702 	}
    703 	simple_unlock(&uobj->vmobjlock);
    704 
    705 	/*
    706 	 * read the desired page(s).
    707 	 */
    708 
    709 	totalbytes = npages << PAGE_SHIFT;
    710 	bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
    711 	tailbytes = totalbytes - bytes;
    712 	skipbytes = 0;
    713 
    714 	kva = uvm_pagermapin(pgs, npages,
    715 	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
    716 
    717 	mbp = getiobuf();
    718 	mbp->b_bufsize = totalbytes;
    719 	mbp->b_data = (void *)kva;
    720 	mbp->b_resid = mbp->b_bcount = bytes;
    721 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
    722 	mbp->b_iodone = (async ? uvm_aio_biodone : 0);
    723 	mbp->b_vp = vp;
    724 	if (async)
    725 		BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
    726 	else
    727 		BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
    728 
    729 	/*
    730 	 * if EOF is in the middle of the range, zero the part past EOF.
    731 	 * if the page including EOF is not PG_FAKE, skip over it since
    732 	 * in that case it has valid data that we need to preserve.
    733 	 */
    734 
    735 	if (tailbytes > 0) {
    736 		size_t tailstart = bytes;
    737 
    738 		if ((pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE) == 0) {
    739 			tailstart = round_page(tailstart);
    740 			tailbytes -= tailstart - bytes;
    741 		}
    742 		UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x",
    743 		    kva, tailstart, tailbytes,0);
    744 		memset((void *)(kva + tailstart), 0, tailbytes);
    745 	}
    746 
    747 	/*
    748 	 * now loop over the pages, reading as needed.
    749 	 */
    750 
    751 	bp = NULL;
    752 	for (offset = startoffset;
    753 	    bytes > 0;
    754 	    offset += iobytes, bytes -= iobytes) {
    755 
    756 		/*
    757 		 * skip pages which don't need to be read.
    758 		 */
    759 
    760 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    761 		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
    762 			size_t b;
    763 
    764 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    765 			if ((pgs[pidx]->flags & PG_RDONLY)) {
    766 				sawhole = true;
    767 			}
    768 			b = MIN(PAGE_SIZE, bytes);
    769 			offset += b;
    770 			bytes -= b;
    771 			skipbytes += b;
    772 			pidx++;
    773 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    774 			    offset, 0,0,0);
    775 			if (bytes == 0) {
    776 				goto loopdone;
    777 			}
    778 		}
    779 
    780 		/*
    781 		 * bmap the file to find out the blkno to read from and
    782 		 * how much we can read in one i/o.  if bmap returns an error,
    783 		 * skip the rest of the top-level i/o.
    784 		 */
    785 
    786 		lbn = offset >> fs_bshift;
    787 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
    788 		if (error) {
    789 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    790 			    lbn, error,0,0);
    791 			skipbytes += bytes;
    792 			goto loopdone;
    793 		}
    794 
    795 		/*
    796 		 * see how many pages can be read with this i/o.
    797 		 * reduce the i/o size if necessary to avoid
    798 		 * overwriting pages with valid data.
    799 		 */
    800 
    801 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    802 		    bytes);
    803 		if (offset + iobytes > round_page(offset)) {
    804 			pcount = 1;
    805 			while (pidx + pcount < npages &&
    806 			    pgs[pidx + pcount]->flags & PG_FAKE) {
    807 				pcount++;
    808 			}
    809 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    810 			    (offset - trunc_page(offset)));
    811 		}
    812 
    813 		/*
    814 		 * if this block isn't allocated, zero it instead of
    815 		 * reading it.  unless we are going to allocate blocks,
    816 		 * mark the pages we zeroed PG_RDONLY.
    817 		 */
    818 
    819 		if (blkno < 0) {
    820 			int holepages = (round_page(offset + iobytes) -
    821 			    trunc_page(offset)) >> PAGE_SHIFT;
    822 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    823 
    824 			sawhole = true;
    825 			memset((char *)kva + (offset - startoffset), 0,
    826 			    iobytes);
    827 			skipbytes += iobytes;
    828 
    829 			for (i = 0; i < holepages; i++) {
    830 				if (write) {
    831 					pgs[pidx + i]->flags &= ~PG_CLEAN;
    832 				}
    833 				if (!blockalloc) {
    834 					pgs[pidx + i]->flags |= PG_RDONLY;
    835 				}
    836 			}
    837 			continue;
    838 		}
    839 
    840 		/*
    841 		 * allocate a sub-buf for this piece of the i/o
    842 		 * (or just use mbp if there's only 1 piece),
    843 		 * and start it going.
    844 		 */
    845 
    846 		if (offset == startoffset && iobytes == bytes) {
    847 			bp = mbp;
    848 		} else {
    849 			bp = getiobuf();
    850 			nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
    851 		}
    852 		bp->b_lblkno = 0;
    853 
    854 		/* adjust physical blkno for partial blocks */
    855 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    856 		    dev_bshift);
    857 
    858 		UVMHIST_LOG(ubchist,
    859 		    "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    860 		    bp, offset, iobytes, bp->b_blkno);
    861 
    862 		VOP_STRATEGY(devvp, bp);
    863 	}
    864 
    865 loopdone:
    866 	nestiobuf_done(mbp, skipbytes, error);
    867 	if (async) {
    868 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
    869 		rw_exit(&gp->g_glock);
    870 		error = 0;
    871 		goto out_err;
    872 	}
    873 	if (bp != NULL) {
    874 		error = biowait(mbp);
    875 	}
    876 	putiobuf(mbp);
    877 	uvm_pagermapout(kva, npages);
    878 
    879 	/*
    880 	 * if this we encountered a hole then we have to do a little more work.
    881 	 * for read faults, we marked the page PG_RDONLY so that future
    882 	 * write accesses to the page will fault again.
    883 	 * for write faults, we must make sure that the backing store for
    884 	 * the page is completely allocated while the pages are locked.
    885 	 */
    886 
    887 	if (!error && sawhole && blockalloc) {
    888 		error = GOP_ALLOC(vp, startoffset, npages << PAGE_SHIFT, 0,
    889 		    cred);
    890 		UVMHIST_LOG(ubchist, "gop_alloc off 0x%x/0x%x -> %d",
    891 		    startoffset, npages << PAGE_SHIFT, error,0);
    892 		if (!error) {
    893 			for (i = 0; i < npages; i++) {
    894 				if (pgs[i] == NULL) {
    895 					continue;
    896 				}
    897 				pgs[i]->flags &= ~(PG_CLEAN|PG_RDONLY);
    898 				UVMHIST_LOG(ubchist, "mark dirty pg %p",
    899 				    pgs[i],0,0,0);
    900 			}
    901 		}
    902 	}
    903 	rw_exit(&gp->g_glock);
    904 	simple_lock(&uobj->vmobjlock);
    905 
    906 	/*
    907 	 * we're almost done!  release the pages...
    908 	 * for errors, we free the pages.
    909 	 * otherwise we activate them and mark them as valid and clean.
    910 	 * also, unbusy pages that were not actually requested.
    911 	 */
    912 
    913 	if (error) {
    914 		for (i = 0; i < npages; i++) {
    915 			if (pgs[i] == NULL) {
    916 				continue;
    917 			}
    918 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    919 			    pgs[i], pgs[i]->flags, 0,0);
    920 			if (pgs[i]->flags & PG_FAKE) {
    921 				pgs[i]->flags |= PG_RELEASED;
    922 			}
    923 		}
    924 		uvm_lock_pageq();
    925 		uvm_page_unbusy(pgs, npages);
    926 		uvm_unlock_pageq();
    927 		simple_unlock(&uobj->vmobjlock);
    928 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
    929 		goto out_err;
    930 	}
    931 
    932 out:
    933 	UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
    934 	error = 0;
    935 	uvm_lock_pageq();
    936 	for (i = 0; i < npages; i++) {
    937 		pg = pgs[i];
    938 		if (pg == NULL) {
    939 			continue;
    940 		}
    941 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    942 		    pg, pg->flags, 0,0);
    943 		if (pg->flags & PG_FAKE && !overwrite) {
    944 			pg->flags &= ~(PG_FAKE);
    945 			pmap_clear_modify(pgs[i]);
    946 		}
    947 		KASSERT(!write || !blockalloc || (pg->flags & PG_RDONLY) == 0);
    948 		if (i < ridx || i >= ridx + orignpages || async) {
    949 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
    950 			    pg, pg->offset,0,0);
    951 			if (pg->flags & PG_WANTED) {
    952 				wakeup(pg);
    953 			}
    954 			if (pg->flags & PG_FAKE) {
    955 				KASSERT(overwrite);
    956 				uvm_pagezero(pg);
    957 			}
    958 			if (pg->flags & PG_RELEASED) {
    959 				uvm_pagefree(pg);
    960 				continue;
    961 			}
    962 			uvm_pageenqueue(pg);
    963 			pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
    964 			UVM_PAGE_OWN(pg, NULL);
    965 		}
    966 	}
    967 	uvm_unlock_pageq();
    968 	simple_unlock(&uobj->vmobjlock);
    969 	if (ap->a_m != NULL) {
    970 		memcpy(ap->a_m, &pgs[ridx],
    971 		    orignpages * sizeof(struct vm_page *));
    972 	}
    973 
    974 out_err:
    975 	if (pgs != pgs_onstack)
    976 		kmem_free(pgs, pgs_size);
    977 	if (has_trans)
    978 		fstrans_done(vp->v_mount);
    979 	return (error);
    980 }
    981 
    982 /*
    983  * generic VM putpages routine.
    984  * Write the given range of pages to backing store.
    985  *
    986  * => "offhi == 0" means flush all pages at or after "offlo".
    987  * => object should be locked by caller.  we return with the
    988  *      object unlocked.
    989  * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
    990  *	thus, a caller might want to unlock higher level resources
    991  *	(e.g. vm_map) before calling flush.
    992  * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, we will not block
    993  * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
    994  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    995  *	that new pages are inserted on the tail end of the list.   thus,
    996  *	we can make a complete pass through the object in one go by starting
    997  *	at the head and working towards the tail (new pages are put in
    998  *	front of us).
    999  * => NOTE: we are allowed to lock the page queues, so the caller
   1000  *	must not be holding the page queue lock.
   1001  *
   1002  * note on "cleaning" object and PG_BUSY pages:
   1003  *	this routine is holding the lock on the object.   the only time
   1004  *	that it can run into a PG_BUSY page that it does not own is if
   1005  *	some other process has started I/O on the page (e.g. either
   1006  *	a pagein, or a pageout).    if the PG_BUSY page is being paged
   1007  *	in, then it can not be dirty (!PG_CLEAN) because no one has
   1008  *	had a chance to modify it yet.    if the PG_BUSY page is being
   1009  *	paged out then it means that someone else has already started
   1010  *	cleaning the page for us (how nice!).    in this case, if we
   1011  *	have syncio specified, then after we make our pass through the
   1012  *	object we need to wait for the other PG_BUSY pages to clear
   1013  *	off (i.e. we need to do an iosync).   also note that once a
   1014  *	page is PG_BUSY it must stay in its object until it is un-busyed.
   1015  *
   1016  * note on page traversal:
   1017  *	we can traverse the pages in an object either by going down the
   1018  *	linked list in "uobj->memq", or we can go over the address range
   1019  *	by page doing hash table lookups for each address.    depending
   1020  *	on how many pages are in the object it may be cheaper to do one
   1021  *	or the other.   we set "by_list" to true if we are using memq.
   1022  *	if the cost of a hash lookup was equal to the cost of the list
   1023  *	traversal we could compare the number of pages in the start->stop
   1024  *	range to the total number of pages in the object.   however, it
   1025  *	seems that a hash table lookup is more expensive than the linked
   1026  *	list traversal, so we multiply the number of pages in the
   1027  *	range by an estimate of the relatively higher cost of the hash lookup.
   1028  */
   1029 
   1030 int
   1031 genfs_putpages(void *v)
   1032 {
   1033 	struct vop_putpages_args /* {
   1034 		struct vnode *a_vp;
   1035 		voff_t a_offlo;
   1036 		voff_t a_offhi;
   1037 		int a_flags;
   1038 	} */ *ap = v;
   1039 
   1040 	return genfs_do_putpages(ap->a_vp, ap->a_offlo, ap->a_offhi,
   1041 	    ap->a_flags, NULL);
   1042 }
   1043 
   1044 int
   1045 genfs_do_putpages(struct vnode *vp, off_t startoff, off_t endoff, int flags,
   1046 	struct vm_page **busypg)
   1047 {
   1048 	struct uvm_object *uobj = &vp->v_uobj;
   1049 	struct simplelock *slock = &uobj->vmobjlock;
   1050 	off_t off;
   1051 	/* Even for strange MAXPHYS, the shift rounds down to a page */
   1052 #define maxpages (MAXPHYS >> PAGE_SHIFT)
   1053 	int i, s, error, npages, nback;
   1054 	int freeflag;
   1055 	struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp;
   1056 	bool wasclean, by_list, needs_clean, yld;
   1057 	bool async = (flags & PGO_SYNCIO) == 0;
   1058 	bool pagedaemon = curproc == uvm.pagedaemon_proc;
   1059 	struct lwp *l = curlwp ? curlwp : &lwp0;
   1060 	struct genfs_node *gp = VTOG(vp);
   1061 	int dirtygen;
   1062 	bool modified = false;
   1063 	bool has_trans = false;
   1064 	bool cleanall;
   1065 
   1066 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
   1067 
   1068 	KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
   1069 	KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
   1070 	KASSERT(startoff < endoff || endoff == 0);
   1071 
   1072 	UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
   1073 	    vp, uobj->uo_npages, startoff, endoff - startoff);
   1074 
   1075 	KASSERT((vp->v_flag & VONWORKLST) != 0 ||
   1076 	    (vp->v_flag & VWRITEMAPDIRTY) == 0);
   1077 	if (uobj->uo_npages == 0) {
   1078 		s = splbio();
   1079 		if (vp->v_flag & VONWORKLST) {
   1080 			vp->v_flag &= ~VWRITEMAPDIRTY;
   1081 			if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
   1082 				vn_syncer_remove_from_worklist(vp);
   1083 		}
   1084 		splx(s);
   1085 		simple_unlock(slock);
   1086 		return (0);
   1087 	}
   1088 
   1089 	/*
   1090 	 * the vnode has pages, set up to process the request.
   1091 	 */
   1092 
   1093 	if ((flags & PGO_CLEANIT) != 0) {
   1094 		simple_unlock(slock);
   1095 		if (pagedaemon) {
   1096 			error = fstrans_start_nowait(vp->v_mount, FSTRANS_LAZY);
   1097 			if (error)
   1098 				return error;
   1099 		} else
   1100 			fstrans_start(vp->v_mount, FSTRANS_LAZY);
   1101 		has_trans = true;
   1102 		simple_lock(slock);
   1103 	}
   1104 
   1105 	error = 0;
   1106 	s = splbio();
   1107 	simple_lock(&global_v_numoutput_slock);
   1108 	wasclean = (vp->v_numoutput == 0);
   1109 	simple_unlock(&global_v_numoutput_slock);
   1110 	splx(s);
   1111 	off = startoff;
   1112 	if (endoff == 0 || flags & PGO_ALLPAGES) {
   1113 		endoff = trunc_page(LLONG_MAX);
   1114 	}
   1115 	by_list = (uobj->uo_npages <=
   1116 	    ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
   1117 
   1118 #if !defined(DEBUG)
   1119 	/*
   1120 	 * if this vnode is known not to have dirty pages,
   1121 	 * don't bother to clean it out.
   1122 	 */
   1123 
   1124 	if ((vp->v_flag & VONWORKLST) == 0) {
   1125 		if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == 0) {
   1126 			goto skip_scan;
   1127 		}
   1128 		flags &= ~PGO_CLEANIT;
   1129 	}
   1130 #endif /* !defined(DEBUG) */
   1131 
   1132 	/*
   1133 	 * start the loop.  when scanning by list, hold the last page
   1134 	 * in the list before we start.  pages allocated after we start
   1135 	 * will be added to the end of the list, so we can stop at the
   1136 	 * current last page.
   1137 	 */
   1138 
   1139 	cleanall = (flags & PGO_CLEANIT) != 0 && wasclean &&
   1140 	    startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
   1141 	    (vp->v_flag & VONWORKLST) != 0;
   1142 	dirtygen = gp->g_dirtygen;
   1143 	freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
   1144 	if (by_list) {
   1145 		curmp.uobject = uobj;
   1146 		curmp.offset = (voff_t)-1;
   1147 		curmp.flags = PG_BUSY;
   1148 		endmp.uobject = uobj;
   1149 		endmp.offset = (voff_t)-1;
   1150 		endmp.flags = PG_BUSY;
   1151 		pg = TAILQ_FIRST(&uobj->memq);
   1152 		TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq);
   1153 		PHOLD(l);
   1154 	} else {
   1155 		pg = uvm_pagelookup(uobj, off);
   1156 	}
   1157 	nextpg = NULL;
   1158 	while (by_list || off < endoff) {
   1159 
   1160 		/*
   1161 		 * if the current page is not interesting, move on to the next.
   1162 		 */
   1163 
   1164 		KASSERT(pg == NULL || pg->uobject == uobj);
   1165 		KASSERT(pg == NULL ||
   1166 		    (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
   1167 		    (pg->flags & PG_BUSY) != 0);
   1168 		if (by_list) {
   1169 			if (pg == &endmp) {
   1170 				break;
   1171 			}
   1172 			if (pg->offset < startoff || pg->offset >= endoff ||
   1173 			    pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1174 				if (pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1175 					wasclean = false;
   1176 				}
   1177 				pg = TAILQ_NEXT(pg, listq);
   1178 				continue;
   1179 			}
   1180 			off = pg->offset;
   1181 		} else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1182 			if (pg != NULL) {
   1183 				wasclean = false;
   1184 			}
   1185 			off += PAGE_SIZE;
   1186 			if (off < endoff) {
   1187 				pg = uvm_pagelookup(uobj, off);
   1188 			}
   1189 			continue;
   1190 		}
   1191 
   1192 		/*
   1193 		 * if the current page needs to be cleaned and it's busy,
   1194 		 * wait for it to become unbusy.
   1195 		 */
   1196 
   1197 		yld = (l->l_cpu->ci_schedstate.spc_flags &
   1198 		    SPCF_SHOULDYIELD) && !pagedaemon;
   1199 		if (pg->flags & PG_BUSY || yld) {
   1200 			UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
   1201 			if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
   1202 				UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
   1203 				error = EDEADLK;
   1204 				if (busypg != NULL)
   1205 					*busypg = pg;
   1206 				break;
   1207 			}
   1208 			KASSERT(!pagedaemon);
   1209 			if (by_list) {
   1210 				TAILQ_INSERT_BEFORE(pg, &curmp, listq);
   1211 				UVMHIST_LOG(ubchist, "curmp next %p",
   1212 				    TAILQ_NEXT(&curmp, listq), 0,0,0);
   1213 			}
   1214 			if (yld) {
   1215 				simple_unlock(slock);
   1216 				preempt();
   1217 				simple_lock(slock);
   1218 			} else {
   1219 				pg->flags |= PG_WANTED;
   1220 				UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
   1221 				simple_lock(slock);
   1222 			}
   1223 			if (by_list) {
   1224 				UVMHIST_LOG(ubchist, "after next %p",
   1225 				    TAILQ_NEXT(&curmp, listq), 0,0,0);
   1226 				pg = TAILQ_NEXT(&curmp, listq);
   1227 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   1228 			} else {
   1229 				pg = uvm_pagelookup(uobj, off);
   1230 			}
   1231 			continue;
   1232 		}
   1233 
   1234 		/*
   1235 		 * if we're freeing, remove all mappings of the page now.
   1236 		 * if we're cleaning, check if the page is needs to be cleaned.
   1237 		 */
   1238 
   1239 		if (flags & PGO_FREE) {
   1240 			pmap_page_protect(pg, VM_PROT_NONE);
   1241 		} else if (flags & PGO_CLEANIT) {
   1242 
   1243 			/*
   1244 			 * if we still have some hope to pull this vnode off
   1245 			 * from the syncer queue, write-protect the page.
   1246 			 */
   1247 
   1248 			if (cleanall && wasclean &&
   1249 			    gp->g_dirtygen == dirtygen) {
   1250 
   1251 				/*
   1252 				 * uobj pages get wired only by uvm_fault
   1253 				 * where uobj is locked.
   1254 				 */
   1255 
   1256 				if (pg->wire_count == 0) {
   1257 					pmap_page_protect(pg,
   1258 					    VM_PROT_READ|VM_PROT_EXECUTE);
   1259 				} else {
   1260 					cleanall = false;
   1261 				}
   1262 			}
   1263 		}
   1264 
   1265 		if (flags & PGO_CLEANIT) {
   1266 			needs_clean = pmap_clear_modify(pg) ||
   1267 			    (pg->flags & PG_CLEAN) == 0;
   1268 			pg->flags |= PG_CLEAN;
   1269 		} else {
   1270 			needs_clean = false;
   1271 		}
   1272 
   1273 		/*
   1274 		 * if we're cleaning, build a cluster.
   1275 		 * the cluster will consist of pages which are currently dirty,
   1276 		 * but they will be returned to us marked clean.
   1277 		 * if not cleaning, just operate on the one page.
   1278 		 */
   1279 
   1280 		if (needs_clean) {
   1281 			KDASSERT((vp->v_flag & VONWORKLST));
   1282 			wasclean = false;
   1283 			memset(pgs, 0, sizeof(pgs));
   1284 			pg->flags |= PG_BUSY;
   1285 			UVM_PAGE_OWN(pg, "genfs_putpages");
   1286 
   1287 			/*
   1288 			 * first look backward.
   1289 			 */
   1290 
   1291 			npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
   1292 			nback = npages;
   1293 			uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
   1294 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
   1295 			if (nback) {
   1296 				memmove(&pgs[0], &pgs[npages - nback],
   1297 				    nback * sizeof(pgs[0]));
   1298 				if (npages - nback < nback)
   1299 					memset(&pgs[nback], 0,
   1300 					    (npages - nback) * sizeof(pgs[0]));
   1301 				else
   1302 					memset(&pgs[npages - nback], 0,
   1303 					    nback * sizeof(pgs[0]));
   1304 			}
   1305 
   1306 			/*
   1307 			 * then plug in our page of interest.
   1308 			 */
   1309 
   1310 			pgs[nback] = pg;
   1311 
   1312 			/*
   1313 			 * then look forward to fill in the remaining space in
   1314 			 * the array of pages.
   1315 			 */
   1316 
   1317 			npages = maxpages - nback - 1;
   1318 			uvn_findpages(uobj, off + PAGE_SIZE, &npages,
   1319 			    &pgs[nback + 1],
   1320 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
   1321 			npages += nback + 1;
   1322 		} else {
   1323 			pgs[0] = pg;
   1324 			npages = 1;
   1325 			nback = 0;
   1326 		}
   1327 
   1328 		/*
   1329 		 * apply FREE or DEACTIVATE options if requested.
   1330 		 */
   1331 
   1332 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1333 			uvm_lock_pageq();
   1334 		}
   1335 		for (i = 0; i < npages; i++) {
   1336 			tpg = pgs[i];
   1337 			KASSERT(tpg->uobject == uobj);
   1338 			if (by_list && tpg == TAILQ_NEXT(pg, listq))
   1339 				pg = tpg;
   1340 			if (tpg->offset < startoff || tpg->offset >= endoff)
   1341 				continue;
   1342 			if (flags & PGO_DEACTIVATE && tpg->wire_count == 0) {
   1343 				(void) pmap_clear_reference(tpg);
   1344 				uvm_pagedeactivate(tpg);
   1345 			} else if (flags & PGO_FREE) {
   1346 				pmap_page_protect(tpg, VM_PROT_NONE);
   1347 				if (tpg->flags & PG_BUSY) {
   1348 					tpg->flags |= freeflag;
   1349 					if (pagedaemon) {
   1350 						uvmexp.paging++;
   1351 						uvm_pagedequeue(tpg);
   1352 					}
   1353 				} else {
   1354 
   1355 					/*
   1356 					 * ``page is not busy''
   1357 					 * implies that npages is 1
   1358 					 * and needs_clean is false.
   1359 					 */
   1360 
   1361 					nextpg = TAILQ_NEXT(tpg, listq);
   1362 					uvm_pagefree(tpg);
   1363 					if (pagedaemon)
   1364 						uvmexp.pdfreed++;
   1365 				}
   1366 			}
   1367 		}
   1368 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1369 			uvm_unlock_pageq();
   1370 		}
   1371 		if (needs_clean) {
   1372 			modified = true;
   1373 
   1374 			/*
   1375 			 * start the i/o.  if we're traversing by list,
   1376 			 * keep our place in the list with a marker page.
   1377 			 */
   1378 
   1379 			if (by_list) {
   1380 				TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
   1381 				    listq);
   1382 			}
   1383 			simple_unlock(slock);
   1384 			error = GOP_WRITE(vp, pgs, npages, flags);
   1385 			simple_lock(slock);
   1386 			if (by_list) {
   1387 				pg = TAILQ_NEXT(&curmp, listq);
   1388 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   1389 			}
   1390 			if (error) {
   1391 				break;
   1392 			}
   1393 			if (by_list) {
   1394 				continue;
   1395 			}
   1396 		}
   1397 
   1398 		/*
   1399 		 * find the next page and continue if there was no error.
   1400 		 */
   1401 
   1402 		if (by_list) {
   1403 			if (nextpg) {
   1404 				pg = nextpg;
   1405 				nextpg = NULL;
   1406 			} else {
   1407 				pg = TAILQ_NEXT(pg, listq);
   1408 			}
   1409 		} else {
   1410 			off += (npages - nback) << PAGE_SHIFT;
   1411 			if (off < endoff) {
   1412 				pg = uvm_pagelookup(uobj, off);
   1413 			}
   1414 		}
   1415 	}
   1416 	if (by_list) {
   1417 		TAILQ_REMOVE(&uobj->memq, &endmp, listq);
   1418 		PRELE(l);
   1419 	}
   1420 
   1421 	if (modified && (vp->v_flag & VWRITEMAPDIRTY) != 0 &&
   1422 	    (vp->v_type != VBLK ||
   1423 	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
   1424 		GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
   1425 	}
   1426 
   1427 	/*
   1428 	 * if we're cleaning and there was nothing to clean,
   1429 	 * take us off the syncer list.  if we started any i/o
   1430 	 * and we're doing sync i/o, wait for all writes to finish.
   1431 	 */
   1432 
   1433 	s = splbio();
   1434 	if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
   1435 	    (vp->v_flag & VONWORKLST) != 0) {
   1436 		vp->v_flag &= ~VWRITEMAPDIRTY;
   1437 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
   1438 			vn_syncer_remove_from_worklist(vp);
   1439 	}
   1440 	splx(s);
   1441 
   1442 #if !defined(DEBUG)
   1443 skip_scan:
   1444 #endif /* !defined(DEBUG) */
   1445 	if (!wasclean && !async) {
   1446 		s = splbio();
   1447 		/*
   1448 		 * XXX - we want simple_unlock(&global_v_numoutput_slock);
   1449 		 *	 but the slot in ltsleep() is taken!
   1450 		 * XXX - try to recover from missed wakeups with a timeout..
   1451 		 *	 must think of something better.
   1452 		 */
   1453 		while (vp->v_numoutput != 0) {
   1454 			vp->v_flag |= VBWAIT;
   1455 			UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, false,
   1456 			    "genput2", hz);
   1457 			simple_lock(slock);
   1458 		}
   1459 		splx(s);
   1460 	}
   1461 	simple_unlock(slock);
   1462 
   1463 	if (has_trans)
   1464 		fstrans_done(vp->v_mount);
   1465 
   1466 	return (error);
   1467 }
   1468 
   1469 int
   1470 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
   1471 {
   1472 	off_t off;
   1473 	vaddr_t kva;
   1474 	size_t len;
   1475 	int error;
   1476 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
   1477 
   1478 	UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
   1479 	    vp, pgs, npages, flags);
   1480 
   1481 	off = pgs[0]->offset;
   1482 	kva = uvm_pagermapin(pgs, npages,
   1483 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1484 	len = npages << PAGE_SHIFT;
   1485 
   1486 	error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
   1487 			    uvm_aio_biodone);
   1488 
   1489 	return error;
   1490 }
   1491 
   1492 /*
   1493  * Backend routine for doing I/O to vnode pages.  Pages are already locked
   1494  * and mapped into kernel memory.  Here we just look up the underlying
   1495  * device block addresses and call the strategy routine.
   1496  */
   1497 
   1498 static int
   1499 genfs_do_io(struct vnode *vp, off_t off, vaddr_t kva, size_t len, int flags,
   1500     enum uio_rw rw, void (*iodone)(struct buf *))
   1501 {
   1502 	int s, error, run;
   1503 	int fs_bshift, dev_bshift;
   1504 	off_t eof, offset, startoffset;
   1505 	size_t bytes, iobytes, skipbytes;
   1506 	daddr_t lbn, blkno;
   1507 	struct buf *mbp, *bp;
   1508 	struct vnode *devvp;
   1509 	bool async = (flags & PGO_SYNCIO) == 0;
   1510 	bool write = rw == UIO_WRITE;
   1511 	int brw = write ? B_WRITE : B_READ;
   1512 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
   1513 
   1514 	UVMHIST_LOG(ubchist, "vp %p kva %p len 0x%x flags 0x%x",
   1515 	    vp, kva, len, flags);
   1516 
   1517 	GOP_SIZE(vp, vp->v_size, &eof, 0);
   1518 	if (vp->v_type != VBLK) {
   1519 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1520 		dev_bshift = vp->v_mount->mnt_dev_bshift;
   1521 	} else {
   1522 		fs_bshift = DEV_BSHIFT;
   1523 		dev_bshift = DEV_BSHIFT;
   1524 	}
   1525 	error = 0;
   1526 	startoffset = off;
   1527 	bytes = MIN(len, eof - startoffset);
   1528 	skipbytes = 0;
   1529 	KASSERT(bytes != 0);
   1530 
   1531 	if (write) {
   1532 		s = splbio();
   1533 		simple_lock(&global_v_numoutput_slock);
   1534 		vp->v_numoutput += 2;
   1535 		simple_unlock(&global_v_numoutput_slock);
   1536 		splx(s);
   1537 	}
   1538 	mbp = getiobuf();
   1539 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1540 	    vp, mbp, vp->v_numoutput, bytes);
   1541 	mbp->b_bufsize = len;
   1542 	mbp->b_data = (void *)kva;
   1543 	mbp->b_resid = mbp->b_bcount = bytes;
   1544 	mbp->b_flags = B_BUSY | brw | B_AGE | (async ? (B_CALL | B_ASYNC) : 0);
   1545 	mbp->b_iodone = iodone;
   1546 	mbp->b_vp = vp;
   1547 	if (curproc == uvm.pagedaemon_proc)
   1548 		BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
   1549 	else if (async)
   1550 		BIO_SETPRIO(mbp, BPRIO_TIMENONCRITICAL);
   1551 	else
   1552 		BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
   1553 
   1554 	bp = NULL;
   1555 	for (offset = startoffset;
   1556 	    bytes > 0;
   1557 	    offset += iobytes, bytes -= iobytes) {
   1558 		lbn = offset >> fs_bshift;
   1559 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
   1560 		if (error) {
   1561 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
   1562 			skipbytes += bytes;
   1563 			bytes = 0;
   1564 			break;
   1565 		}
   1566 
   1567 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1568 		    bytes);
   1569 		if (blkno == (daddr_t)-1) {
   1570 			if (!write) {
   1571 				memset((char *)kva + (offset - startoffset), 0,
   1572 				   iobytes);
   1573 			}
   1574 			skipbytes += iobytes;
   1575 			continue;
   1576 		}
   1577 
   1578 		/* if it's really one i/o, don't make a second buf */
   1579 		if (offset == startoffset && iobytes == bytes) {
   1580 			bp = mbp;
   1581 		} else {
   1582 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1583 			    vp, bp, vp->v_numoutput, 0);
   1584 			bp = getiobuf();
   1585 			nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
   1586 		}
   1587 		bp->b_lblkno = 0;
   1588 
   1589 		/* adjust physical blkno for partial blocks */
   1590 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1591 		    dev_bshift);
   1592 		UVMHIST_LOG(ubchist,
   1593 		    "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1594 		    vp, offset, bp->b_bcount, bp->b_blkno);
   1595 
   1596 		VOP_STRATEGY(devvp, bp);
   1597 	}
   1598 	if (skipbytes) {
   1599 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   1600 	}
   1601 	nestiobuf_done(mbp, skipbytes, error);
   1602 	if (async) {
   1603 		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
   1604 		return (0);
   1605 	}
   1606 	UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
   1607 	error = biowait(mbp);
   1608 	s = splbio();
   1609 	(*iodone)(mbp);
   1610 	splx(s);
   1611 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
   1612 	return (error);
   1613 }
   1614 
   1615 /*
   1616  * VOP_PUTPAGES() for vnodes which never have pages.
   1617  */
   1618 
   1619 int
   1620 genfs_null_putpages(void *v)
   1621 {
   1622 	struct vop_putpages_args /* {
   1623 		struct vnode *a_vp;
   1624 		voff_t a_offlo;
   1625 		voff_t a_offhi;
   1626 		int a_flags;
   1627 	} */ *ap = v;
   1628 	struct vnode *vp = ap->a_vp;
   1629 
   1630 	KASSERT(vp->v_uobj.uo_npages == 0);
   1631 	simple_unlock(&vp->v_interlock);
   1632 	return (0);
   1633 }
   1634 
   1635 void
   1636 genfs_node_init(struct vnode *vp, const struct genfs_ops *ops)
   1637 {
   1638 	struct genfs_node *gp = VTOG(vp);
   1639 
   1640 	rw_init(&gp->g_glock);
   1641 	gp->g_op = ops;
   1642 }
   1643 
   1644 void
   1645 genfs_node_destroy(struct vnode *vp)
   1646 {
   1647 	struct genfs_node *gp = VTOG(vp);
   1648 
   1649 	rw_destroy(&gp->g_glock);
   1650 }
   1651 
   1652 void
   1653 genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   1654 {
   1655 	int bsize;
   1656 
   1657 	bsize = 1 << vp->v_mount->mnt_fs_bshift;
   1658 	*eobp = (size + bsize - 1) & ~(bsize - 1);
   1659 }
   1660 
   1661 int
   1662 genfs_compat_getpages(void *v)
   1663 {
   1664 	struct vop_getpages_args /* {
   1665 		struct vnode *a_vp;
   1666 		voff_t a_offset;
   1667 		struct vm_page **a_m;
   1668 		int *a_count;
   1669 		int a_centeridx;
   1670 		vm_prot_t a_access_type;
   1671 		int a_advice;
   1672 		int a_flags;
   1673 	} */ *ap = v;
   1674 
   1675 	off_t origoffset;
   1676 	struct vnode *vp = ap->a_vp;
   1677 	struct uvm_object *uobj = &vp->v_uobj;
   1678 	struct vm_page *pg, **pgs;
   1679 	vaddr_t kva;
   1680 	int i, error, orignpages, npages;
   1681 	struct iovec iov;
   1682 	struct uio uio;
   1683 	kauth_cred_t cred = curlwp->l_cred;
   1684 	bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   1685 
   1686 	error = 0;
   1687 	origoffset = ap->a_offset;
   1688 	orignpages = *ap->a_count;
   1689 	pgs = ap->a_m;
   1690 
   1691 	if (write && (vp->v_flag & VONWORKLST) == 0) {
   1692 		vn_syncer_add_to_worklist(vp, filedelay);
   1693 	}
   1694 	if (ap->a_flags & PGO_LOCKED) {
   1695 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
   1696 		    UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
   1697 
   1698 		return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
   1699 	}
   1700 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
   1701 		simple_unlock(&uobj->vmobjlock);
   1702 		return (EINVAL);
   1703 	}
   1704 	if ((ap->a_flags & PGO_SYNCIO) == 0) {
   1705 		simple_unlock(&uobj->vmobjlock);
   1706 		return 0;
   1707 	}
   1708 	npages = orignpages;
   1709 	uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
   1710 	simple_unlock(&uobj->vmobjlock);
   1711 	kva = uvm_pagermapin(pgs, npages,
   1712 	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
   1713 	for (i = 0; i < npages; i++) {
   1714 		pg = pgs[i];
   1715 		if ((pg->flags & PG_FAKE) == 0) {
   1716 			continue;
   1717 		}
   1718 		iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
   1719 		iov.iov_len = PAGE_SIZE;
   1720 		uio.uio_iov = &iov;
   1721 		uio.uio_iovcnt = 1;
   1722 		uio.uio_offset = origoffset + (i << PAGE_SHIFT);
   1723 		uio.uio_rw = UIO_READ;
   1724 		uio.uio_resid = PAGE_SIZE;
   1725 		UIO_SETUP_SYSSPACE(&uio);
   1726 		/* XXX vn_lock */
   1727 		error = VOP_READ(vp, &uio, 0, cred);
   1728 		if (error) {
   1729 			break;
   1730 		}
   1731 		if (uio.uio_resid) {
   1732 			memset(iov.iov_base, 0, uio.uio_resid);
   1733 		}
   1734 	}
   1735 	uvm_pagermapout(kva, npages);
   1736 	simple_lock(&uobj->vmobjlock);
   1737 	uvm_lock_pageq();
   1738 	for (i = 0; i < npages; i++) {
   1739 		pg = pgs[i];
   1740 		if (error && (pg->flags & PG_FAKE) != 0) {
   1741 			pg->flags |= PG_RELEASED;
   1742 		} else {
   1743 			pmap_clear_modify(pg);
   1744 			uvm_pageactivate(pg);
   1745 		}
   1746 	}
   1747 	if (error) {
   1748 		uvm_page_unbusy(pgs, npages);
   1749 	}
   1750 	uvm_unlock_pageq();
   1751 	simple_unlock(&uobj->vmobjlock);
   1752 	return (error);
   1753 }
   1754 
   1755 int
   1756 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
   1757     int flags)
   1758 {
   1759 	off_t offset;
   1760 	struct iovec iov;
   1761 	struct uio uio;
   1762 	kauth_cred_t cred = curlwp->l_cred;
   1763 	struct buf *bp;
   1764 	vaddr_t kva;
   1765 	int s, error;
   1766 
   1767 	offset = pgs[0]->offset;
   1768 	kva = uvm_pagermapin(pgs, npages,
   1769 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1770 
   1771 	iov.iov_base = (void *)kva;
   1772 	iov.iov_len = npages << PAGE_SHIFT;
   1773 	uio.uio_iov = &iov;
   1774 	uio.uio_iovcnt = 1;
   1775 	uio.uio_offset = offset;
   1776 	uio.uio_rw = UIO_WRITE;
   1777 	uio.uio_resid = npages << PAGE_SHIFT;
   1778 	UIO_SETUP_SYSSPACE(&uio);
   1779 	/* XXX vn_lock */
   1780 	error = VOP_WRITE(vp, &uio, 0, cred);
   1781 
   1782 	s = splbio();
   1783 	V_INCR_NUMOUTPUT(vp);
   1784 	splx(s);
   1785 
   1786 	bp = getiobuf();
   1787 	bp->b_flags = B_BUSY | B_WRITE | B_AGE;
   1788 	bp->b_vp = vp;
   1789 	bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
   1790 	bp->b_data = (char *)kva;
   1791 	bp->b_bcount = npages << PAGE_SHIFT;
   1792 	bp->b_bufsize = npages << PAGE_SHIFT;
   1793 	bp->b_resid = 0;
   1794 	if (error) {
   1795 		bp->b_flags |= B_ERROR;
   1796 		bp->b_error = error;
   1797 	}
   1798 	uvm_aio_aiodone(bp);
   1799 	return (error);
   1800 }
   1801 
   1802 /*
   1803  * Process a uio using direct I/O.  If we reach a part of the request
   1804  * which cannot be processed in this fashion for some reason, just return.
   1805  * The caller must handle some additional part of the request using
   1806  * buffered I/O before trying direct I/O again.
   1807  */
   1808 
   1809 void
   1810 genfs_directio(struct vnode *vp, struct uio *uio, int ioflag)
   1811 {
   1812 	struct vmspace *vs;
   1813 	struct iovec *iov;
   1814 	vaddr_t va;
   1815 	size_t len;
   1816 	const int mask = DEV_BSIZE - 1;
   1817 	int error;
   1818 
   1819 	/*
   1820 	 * We only support direct I/O to user space for now.
   1821 	 */
   1822 
   1823 	if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
   1824 		return;
   1825 	}
   1826 
   1827 	/*
   1828 	 * If the vnode is mapped, we would need to get the getpages lock
   1829 	 * to stabilize the bmap, but then we would get into trouble whil e
   1830 	 * locking the pages if the pages belong to this same vnode (or a
   1831 	 * multi-vnode cascade to the same effect).  Just fall back to
   1832 	 * buffered I/O if the vnode is mapped to avoid this mess.
   1833 	 */
   1834 
   1835 	if (vp->v_flag & VMAPPED) {
   1836 		return;
   1837 	}
   1838 
   1839 	/*
   1840 	 * Do as much of the uio as possible with direct I/O.
   1841 	 */
   1842 
   1843 	vs = uio->uio_vmspace;
   1844 	while (uio->uio_resid) {
   1845 		iov = uio->uio_iov;
   1846 		if (iov->iov_len == 0) {
   1847 			uio->uio_iov++;
   1848 			uio->uio_iovcnt--;
   1849 			continue;
   1850 		}
   1851 		va = (vaddr_t)iov->iov_base;
   1852 		len = MIN(iov->iov_len, genfs_maxdio);
   1853 		len &= ~mask;
   1854 
   1855 		/*
   1856 		 * If the next chunk is smaller than DEV_BSIZE or extends past
   1857 		 * the current EOF, then fall back to buffered I/O.
   1858 		 */
   1859 
   1860 		if (len == 0 || uio->uio_offset + len > vp->v_size) {
   1861 			return;
   1862 		}
   1863 
   1864 		/*
   1865 		 * Check alignment.  The file offset must be at least
   1866 		 * sector-aligned.  The exact constraint on memory alignment
   1867 		 * is very hardware-dependent, but requiring sector-aligned
   1868 		 * addresses there too is safe.
   1869 		 */
   1870 
   1871 		if (uio->uio_offset & mask || va & mask) {
   1872 			return;
   1873 		}
   1874 		error = genfs_do_directio(vs, va, len, vp, uio->uio_offset,
   1875 					  uio->uio_rw);
   1876 		if (error) {
   1877 			break;
   1878 		}
   1879 		iov->iov_base = (char *)iov->iov_base + len;
   1880 		iov->iov_len -= len;
   1881 		uio->uio_offset += len;
   1882 		uio->uio_resid -= len;
   1883 	}
   1884 }
   1885 
   1886 /*
   1887  * Iodone routine for direct I/O.  We don't do much here since the request is
   1888  * always synchronous, so the caller will do most of the work after biowait().
   1889  */
   1890 
   1891 static void
   1892 genfs_dio_iodone(struct buf *bp)
   1893 {
   1894 	int s;
   1895 
   1896 	KASSERT((bp->b_flags & B_ASYNC) == 0);
   1897 	s = splbio();
   1898 	if ((bp->b_flags & (B_READ | B_AGE)) == B_AGE) {
   1899 		vwakeup(bp);
   1900 	}
   1901 	putiobuf(bp);
   1902 	splx(s);
   1903 }
   1904 
   1905 /*
   1906  * Process one chunk of a direct I/O request.
   1907  */
   1908 
   1909 static int
   1910 genfs_do_directio(struct vmspace *vs, vaddr_t uva, size_t len, struct vnode *vp,
   1911     off_t off, enum uio_rw rw)
   1912 {
   1913 	struct vm_map *map;
   1914 	struct pmap *upm, *kpm;
   1915 	size_t klen = round_page(uva + len) - trunc_page(uva);
   1916 	off_t spoff, epoff;
   1917 	vaddr_t kva, puva;
   1918 	paddr_t pa;
   1919 	vm_prot_t prot;
   1920 	int error, rv, poff, koff;
   1921 	const int pgoflags = PGO_CLEANIT | PGO_SYNCIO |
   1922 		(rw == UIO_WRITE ? PGO_FREE : 0);
   1923 
   1924 	/*
   1925 	 * For writes, verify that this range of the file already has fully
   1926 	 * allocated backing store.  If there are any holes, just punt and
   1927 	 * make the caller take the buffered write path.
   1928 	 */
   1929 
   1930 	if (rw == UIO_WRITE) {
   1931 		daddr_t lbn, elbn, blkno;
   1932 		int bsize, bshift, run;
   1933 
   1934 		bshift = vp->v_mount->mnt_fs_bshift;
   1935 		bsize = 1 << bshift;
   1936 		lbn = off >> bshift;
   1937 		elbn = (off + len + bsize - 1) >> bshift;
   1938 		while (lbn < elbn) {
   1939 			error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
   1940 			if (error) {
   1941 				return error;
   1942 			}
   1943 			if (blkno == (daddr_t)-1) {
   1944 				return ENOSPC;
   1945 			}
   1946 			lbn += 1 + run;
   1947 		}
   1948 	}
   1949 
   1950 	/*
   1951 	 * Flush any cached pages for parts of the file that we're about to
   1952 	 * access.  If we're writing, invalidate pages as well.
   1953 	 */
   1954 
   1955 	spoff = trunc_page(off);
   1956 	epoff = round_page(off + len);
   1957 	simple_lock(&vp->v_interlock);
   1958 	error = VOP_PUTPAGES(vp, spoff, epoff, pgoflags);
   1959 	if (error) {
   1960 		return error;
   1961 	}
   1962 
   1963 	/*
   1964 	 * Wire the user pages and remap them into kernel memory.
   1965 	 */
   1966 
   1967 	prot = rw == UIO_READ ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
   1968 	error = uvm_vslock(vs, (void *)uva, len, prot);
   1969 	if (error) {
   1970 		return error;
   1971 	}
   1972 
   1973 	map = &vs->vm_map;
   1974 	upm = vm_map_pmap(map);
   1975 	kpm = vm_map_pmap(kernel_map);
   1976 	kva = uvm_km_alloc(kernel_map, klen, 0,
   1977 			   UVM_KMF_VAONLY | UVM_KMF_WAITVA);
   1978 	puva = trunc_page(uva);
   1979 	for (poff = 0; poff < klen; poff += PAGE_SIZE) {
   1980 		rv = pmap_extract(upm, puva + poff, &pa);
   1981 		KASSERT(rv);
   1982 		pmap_enter(kpm, kva + poff, pa, prot, prot | PMAP_WIRED);
   1983 	}
   1984 	pmap_update(kpm);
   1985 
   1986 	/*
   1987 	 * Do the I/O.
   1988 	 */
   1989 
   1990 	koff = uva - trunc_page(uva);
   1991 	error = genfs_do_io(vp, off, kva + koff, len, PGO_SYNCIO, rw,
   1992 			    genfs_dio_iodone);
   1993 
   1994 	/*
   1995 	 * Tear down the kernel mapping.
   1996 	 */
   1997 
   1998 	pmap_remove(kpm, kva, kva + klen);
   1999 	pmap_update(kpm);
   2000 	uvm_km_free(kernel_map, kva, klen, UVM_KMF_VAONLY);
   2001 
   2002 	/*
   2003 	 * Unwire the user pages.
   2004 	 */
   2005 
   2006 	uvm_vsunlock(vs, (void *)uva, len);
   2007 	return error;
   2008 }
   2009 
   2010 
   2011 static void
   2012 filt_genfsdetach(struct knote *kn)
   2013 {
   2014 	struct vnode *vp = (struct vnode *)kn->kn_hook;
   2015 
   2016 	/* XXXLUKEM lock the struct? */
   2017 	SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
   2018 }
   2019 
   2020 static int
   2021 filt_genfsread(struct knote *kn, long hint)
   2022 {
   2023 	struct vnode *vp = (struct vnode *)kn->kn_hook;
   2024 
   2025 	/*
   2026 	 * filesystem is gone, so set the EOF flag and schedule
   2027 	 * the knote for deletion.
   2028 	 */
   2029 	if (hint == NOTE_REVOKE) {
   2030 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
   2031 		return (1);
   2032 	}
   2033 
   2034 	/* XXXLUKEM lock the struct? */
   2035 	kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
   2036         return (kn->kn_data != 0);
   2037 }
   2038 
   2039 static int
   2040 filt_genfsvnode(struct knote *kn, long hint)
   2041 {
   2042 
   2043 	if (kn->kn_sfflags & hint)
   2044 		kn->kn_fflags |= hint;
   2045 	if (hint == NOTE_REVOKE) {
   2046 		kn->kn_flags |= EV_EOF;
   2047 		return (1);
   2048 	}
   2049 	return (kn->kn_fflags != 0);
   2050 }
   2051 
   2052 static const struct filterops genfsread_filtops =
   2053 	{ 1, NULL, filt_genfsdetach, filt_genfsread };
   2054 static const struct filterops genfsvnode_filtops =
   2055 	{ 1, NULL, filt_genfsdetach, filt_genfsvnode };
   2056 
   2057 int
   2058 genfs_kqfilter(void *v)
   2059 {
   2060 	struct vop_kqfilter_args /* {
   2061 		struct vnode	*a_vp;
   2062 		struct knote	*a_kn;
   2063 	} */ *ap = v;
   2064 	struct vnode *vp;
   2065 	struct knote *kn;
   2066 
   2067 	vp = ap->a_vp;
   2068 	kn = ap->a_kn;
   2069 	switch (kn->kn_filter) {
   2070 	case EVFILT_READ:
   2071 		kn->kn_fop = &genfsread_filtops;
   2072 		break;
   2073 	case EVFILT_VNODE:
   2074 		kn->kn_fop = &genfsvnode_filtops;
   2075 		break;
   2076 	default:
   2077 		return (1);
   2078 	}
   2079 
   2080 	kn->kn_hook = vp;
   2081 
   2082 	/* XXXLUKEM lock the struct? */
   2083 	SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
   2084 
   2085 	return (0);
   2086 }
   2087 
   2088 void
   2089 genfs_node_wrlock(struct vnode *vp)
   2090 {
   2091 	struct genfs_node *gp = VTOG(vp);
   2092 
   2093 	rw_enter(&gp->g_glock, RW_WRITER);
   2094 }
   2095 
   2096 void
   2097 genfs_node_rdlock(struct vnode *vp)
   2098 {
   2099 	struct genfs_node *gp = VTOG(vp);
   2100 
   2101 	rw_enter(&gp->g_glock, RW_READER);
   2102 }
   2103 
   2104 void
   2105 genfs_node_unlock(struct vnode *vp)
   2106 {
   2107 	struct genfs_node *gp = VTOG(vp);
   2108 
   2109 	rw_exit(&gp->g_glock);
   2110 }
   2111