Home | History | Annotate | Line # | Download | only in genfs
genfs_io.c revision 1.55.2.4
      1 /*	$NetBSD: genfs_io.c,v 1.55.2.4 2017/12/03 11:38:47 jdolecek 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_io.c,v 1.55.2.4 2017/12/03 11:38:47 jdolecek 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/vnode.h>
     42 #include <sys/kmem.h>
     43 #include <sys/kauth.h>
     44 #include <sys/fstrans.h>
     45 #include <sys/buf.h>
     46 
     47 #include <miscfs/genfs/genfs.h>
     48 #include <miscfs/genfs/genfs_node.h>
     49 #include <miscfs/specfs/specdev.h>
     50 
     51 #include <uvm/uvm.h>
     52 #include <uvm/uvm_pager.h>
     53 
     54 static int genfs_do_directio(struct vmspace *, vaddr_t, size_t, struct vnode *,
     55     off_t, enum uio_rw);
     56 static void genfs_dio_iodone(struct buf *);
     57 
     58 static int genfs_getpages_read(struct vnode *, struct vm_page **, int, off_t,
     59     off_t, bool, bool, bool, bool);
     60 static int genfs_do_io(struct vnode *, off_t, vaddr_t, size_t, int, enum uio_rw,
     61     void (*)(struct buf *));
     62 static void genfs_rel_pages(struct vm_page **, unsigned int);
     63 static void genfs_markdirty(struct vnode *);
     64 
     65 int genfs_maxdio = MAXPHYS;
     66 
     67 static void
     68 genfs_rel_pages(struct vm_page **pgs, unsigned int npages)
     69 {
     70 	unsigned int i;
     71 
     72 	for (i = 0; i < npages; i++) {
     73 		struct vm_page *pg = pgs[i];
     74 
     75 		if (pg == NULL || pg == PGO_DONTCARE)
     76 			continue;
     77 		KASSERT(uvm_page_locked_p(pg));
     78 		if (pg->flags & PG_FAKE) {
     79 			pg->flags |= PG_RELEASED;
     80 		}
     81 	}
     82 	mutex_enter(&uvm_pageqlock);
     83 	uvm_page_unbusy(pgs, npages);
     84 	mutex_exit(&uvm_pageqlock);
     85 }
     86 
     87 static void
     88 genfs_markdirty(struct vnode *vp)
     89 {
     90 	struct genfs_node * const gp = VTOG(vp);
     91 
     92 	KASSERT(mutex_owned(vp->v_interlock));
     93 	gp->g_dirtygen++;
     94 	if ((vp->v_iflag & VI_ONWORKLST) == 0) {
     95 		vn_syncer_add_to_worklist(vp, filedelay);
     96 	}
     97 	if ((vp->v_iflag & (VI_WRMAP|VI_WRMAPDIRTY)) == VI_WRMAP) {
     98 		vp->v_iflag |= VI_WRMAPDIRTY;
     99 	}
    100 }
    101 
    102 /*
    103  * generic VM getpages routine.
    104  * Return PG_BUSY pages for the given range,
    105  * reading from backing store if necessary.
    106  */
    107 
    108 int
    109 genfs_getpages(void *v)
    110 {
    111 	struct vop_getpages_args /* {
    112 		struct vnode *a_vp;
    113 		voff_t a_offset;
    114 		struct vm_page **a_m;
    115 		int *a_count;
    116 		int a_centeridx;
    117 		vm_prot_t a_access_type;
    118 		int a_advice;
    119 		int a_flags;
    120 	} */ * const ap = v;
    121 
    122 	off_t diskeof, memeof;
    123 	int i, error, npages;
    124 	const int flags = ap->a_flags;
    125 	struct vnode * const vp = ap->a_vp;
    126 	struct uvm_object * const uobj = &vp->v_uobj;
    127 	const bool async = (flags & PGO_SYNCIO) == 0;
    128 	const bool memwrite = (ap->a_access_type & VM_PROT_WRITE) != 0;
    129 	const bool overwrite = (flags & PGO_OVERWRITE) != 0;
    130 	const bool blockalloc = memwrite && (flags & PGO_NOBLOCKALLOC) == 0;
    131 	const bool glocked = (flags & PGO_GLOCKHELD) != 0;
    132 	bool holds_wapbl = false;
    133 	struct mount *trans_mount = NULL;
    134 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
    135 
    136 	UVMHIST_LOG(ubchist, "vp %#jx off 0x%jx/%jx count %jd",
    137 	    (uintptr_t)vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
    138 
    139 	KASSERT(vp->v_type == VREG || vp->v_type == VDIR ||
    140 	    vp->v_type == VLNK || vp->v_type == VBLK);
    141 
    142 	error = vdead_check(vp, VDEAD_NOWAIT);
    143 	if (error) {
    144 		if ((flags & PGO_LOCKED) == 0)
    145 			mutex_exit(uobj->vmobjlock);
    146 		return error;
    147 	}
    148 
    149 startover:
    150 	error = 0;
    151 	const voff_t origvsize = vp->v_size;
    152 	const off_t origoffset = ap->a_offset;
    153 	const int orignpages = *ap->a_count;
    154 
    155 	GOP_SIZE(vp, origvsize, &diskeof, 0);
    156 	if (flags & PGO_PASTEOF) {
    157 		off_t newsize;
    158 #if defined(DIAGNOSTIC)
    159 		off_t writeeof;
    160 #endif /* defined(DIAGNOSTIC) */
    161 
    162 		newsize = MAX(origvsize,
    163 		    origoffset + (orignpages << PAGE_SHIFT));
    164 		GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_MEM);
    165 #if defined(DIAGNOSTIC)
    166 		GOP_SIZE(vp, vp->v_writesize, &writeeof, GOP_SIZE_MEM);
    167 		if (newsize > round_page(writeeof)) {
    168 			panic("%s: past eof: %" PRId64 " vs. %" PRId64,
    169 			    __func__, newsize, round_page(writeeof));
    170 		}
    171 #endif /* defined(DIAGNOSTIC) */
    172 	} else {
    173 		GOP_SIZE(vp, origvsize, &memeof, GOP_SIZE_MEM);
    174 	}
    175 	KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
    176 	KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
    177 	KASSERT(orignpages > 0);
    178 
    179 	/*
    180 	 * Bounds-check the request.
    181 	 */
    182 
    183 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
    184 		if ((flags & PGO_LOCKED) == 0) {
    185 			mutex_exit(uobj->vmobjlock);
    186 		}
    187 		UVMHIST_LOG(ubchist, "off 0x%jx count %jd goes past EOF 0x%jx",
    188 		    origoffset, *ap->a_count, memeof,0);
    189 		error = EINVAL;
    190 		goto out_err;
    191 	}
    192 
    193 	/* uobj is locked */
    194 
    195 	if ((flags & PGO_NOTIMESTAMP) == 0 &&
    196 	    (vp->v_type != VBLK ||
    197 	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
    198 		int updflags = 0;
    199 
    200 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) {
    201 			updflags = GOP_UPDATE_ACCESSED;
    202 		}
    203 		if (memwrite) {
    204 			updflags |= GOP_UPDATE_MODIFIED;
    205 		}
    206 		if (updflags != 0) {
    207 			GOP_MARKUPDATE(vp, updflags);
    208 		}
    209 	}
    210 
    211 	/*
    212 	 * For PGO_LOCKED requests, just return whatever's in memory.
    213 	 */
    214 
    215 	if (flags & PGO_LOCKED) {
    216 		int nfound;
    217 		struct vm_page *pg;
    218 
    219 		KASSERT(!glocked);
    220 		npages = *ap->a_count;
    221 #if defined(DEBUG)
    222 		for (i = 0; i < npages; i++) {
    223 			pg = ap->a_m[i];
    224 			KASSERT(pg == NULL || pg == PGO_DONTCARE);
    225 		}
    226 #endif /* defined(DEBUG) */
    227 		nfound = uvn_findpages(uobj, origoffset, &npages,
    228 		    ap->a_m, UFP_NOWAIT|UFP_NOALLOC|(memwrite ? UFP_NORDONLY : 0));
    229 		KASSERT(npages == *ap->a_count);
    230 		if (nfound == 0) {
    231 			error = EBUSY;
    232 			goto out_err;
    233 		}
    234 		if (!genfs_node_rdtrylock(vp)) {
    235 			genfs_rel_pages(ap->a_m, npages);
    236 
    237 			/*
    238 			 * restore the array.
    239 			 */
    240 
    241 			for (i = 0; i < npages; i++) {
    242 				pg = ap->a_m[i];
    243 
    244 				if (pg != NULL && pg != PGO_DONTCARE) {
    245 					ap->a_m[i] = NULL;
    246 				}
    247 				KASSERT(ap->a_m[i] == NULL ||
    248 				    ap->a_m[i] == PGO_DONTCARE);
    249 			}
    250 		} else {
    251 			genfs_node_unlock(vp);
    252 		}
    253 		error = (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
    254 		if (error == 0 && memwrite) {
    255 			genfs_markdirty(vp);
    256 		}
    257 		goto out_err;
    258 	}
    259 	mutex_exit(uobj->vmobjlock);
    260 
    261 	/*
    262 	 * find the requested pages and make some simple checks.
    263 	 * leave space in the page array for a whole block.
    264 	 */
    265 
    266 	const int fs_bshift = (vp->v_type != VBLK) ?
    267 	    vp->v_mount->mnt_fs_bshift : DEV_BSHIFT;
    268 	const int fs_bsize = 1 << fs_bshift;
    269 #define	blk_mask	(fs_bsize - 1)
    270 #define	trunc_blk(x)	((x) & ~blk_mask)
    271 #define	round_blk(x)	(((x) + blk_mask) & ~blk_mask)
    272 
    273 	const int orignmempages = MIN(orignpages,
    274 	    round_page(memeof - origoffset) >> PAGE_SHIFT);
    275 	npages = orignmempages;
    276 	const off_t startoffset = trunc_blk(origoffset);
    277 	const off_t endoffset = MIN(
    278 	    round_page(round_blk(origoffset + (npages << PAGE_SHIFT))),
    279 	    round_page(memeof));
    280 	const int ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    281 
    282 	const int pgs_size = sizeof(struct vm_page *) *
    283 	    ((endoffset - startoffset) >> PAGE_SHIFT);
    284 	struct vm_page **pgs, *pgs_onstack[UBC_MAX_PAGES];
    285 
    286 	if (pgs_size > sizeof(pgs_onstack)) {
    287 		pgs = kmem_zalloc(pgs_size, async ? KM_NOSLEEP : KM_SLEEP);
    288 		if (pgs == NULL) {
    289 			pgs = pgs_onstack;
    290 			error = ENOMEM;
    291 			goto out_err;
    292 		}
    293 	} else {
    294 		pgs = pgs_onstack;
    295 		(void)memset(pgs, 0, pgs_size);
    296 	}
    297 
    298 	UVMHIST_LOG(ubchist, "ridx %jd npages %jd startoff %jd endoff %jd",
    299 	    ridx, npages, startoffset, endoffset);
    300 
    301 	if (trans_mount == NULL) {
    302 		trans_mount = vp->v_mount;
    303 		fstrans_start(trans_mount);
    304 		/*
    305 		 * check if this vnode is still valid.
    306 		 */
    307 		mutex_enter(vp->v_interlock);
    308 		error = vdead_check(vp, 0);
    309 		mutex_exit(vp->v_interlock);
    310 		if (error)
    311 			goto out_err_free;
    312 		/*
    313 		 * XXX: This assumes that we come here only via
    314 		 * the mmio path
    315 		 */
    316 		if (blockalloc && vp->v_mount->mnt_wapbl) {
    317 			error = WAPBL_BEGIN(trans_mount);
    318 			if (error)
    319 				goto out_err_free;
    320 			holds_wapbl = true;
    321 		}
    322 	}
    323 
    324 	/*
    325 	 * hold g_glock to prevent a race with truncate.
    326 	 *
    327 	 * check if our idea of v_size is still valid.
    328 	 */
    329 
    330 	KASSERT(!glocked || genfs_node_wrlocked(vp));
    331 	if (!glocked) {
    332 		if (blockalloc) {
    333 			genfs_node_wrlock(vp);
    334 		} else {
    335 			genfs_node_rdlock(vp);
    336 		}
    337 	}
    338 	mutex_enter(uobj->vmobjlock);
    339 	if (vp->v_size < origvsize) {
    340 		if (!glocked) {
    341 			genfs_node_unlock(vp);
    342 		}
    343 		if (pgs != pgs_onstack)
    344 			kmem_free(pgs, pgs_size);
    345 		goto startover;
    346 	}
    347 
    348 	if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx],
    349 	    async ? UFP_NOWAIT : UFP_ALL) != orignmempages) {
    350 		if (!glocked) {
    351 			genfs_node_unlock(vp);
    352 		}
    353 		KASSERT(async != 0);
    354 		genfs_rel_pages(&pgs[ridx], orignmempages);
    355 		mutex_exit(uobj->vmobjlock);
    356 		error = EBUSY;
    357 		goto out_err_free;
    358 	}
    359 
    360 	/*
    361 	 * if the pages are already resident, just return them.
    362 	 */
    363 
    364 	for (i = 0; i < npages; i++) {
    365 		struct vm_page *pg = pgs[ridx + i];
    366 
    367 		if ((pg->flags & PG_FAKE) ||
    368 		    (blockalloc && (pg->flags & PG_RDONLY))) {
    369 			break;
    370 		}
    371 	}
    372 	if (i == npages) {
    373 		if (!glocked) {
    374 			genfs_node_unlock(vp);
    375 		}
    376 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    377 		npages += ridx;
    378 		goto out;
    379 	}
    380 
    381 	/*
    382 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    383 	 */
    384 
    385 	if (overwrite) {
    386 		if (!glocked) {
    387 			genfs_node_unlock(vp);
    388 		}
    389 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    390 
    391 		for (i = 0; i < npages; i++) {
    392 			struct vm_page *pg = pgs[ridx + i];
    393 
    394 			pg->flags &= ~(PG_RDONLY|PG_CLEAN);
    395 		}
    396 		npages += ridx;
    397 		goto out;
    398 	}
    399 
    400 	/*
    401 	 * the page wasn't resident and we're not overwriting,
    402 	 * so we're going to have to do some i/o.
    403 	 * find any additional pages needed to cover the expanded range.
    404 	 */
    405 
    406 	npages = (endoffset - startoffset) >> PAGE_SHIFT;
    407 	if (startoffset != origoffset || npages != orignmempages) {
    408 		int npgs;
    409 
    410 		/*
    411 		 * we need to avoid deadlocks caused by locking
    412 		 * additional pages at lower offsets than pages we
    413 		 * already have locked.  unlock them all and start over.
    414 		 */
    415 
    416 		genfs_rel_pages(&pgs[ridx], orignmempages);
    417 		memset(pgs, 0, pgs_size);
    418 
    419 		UVMHIST_LOG(ubchist, "reset npages start 0x%jx end 0x%jx",
    420 		    startoffset, endoffset, 0,0);
    421 		npgs = npages;
    422 		if (uvn_findpages(uobj, startoffset, &npgs, pgs,
    423 		    async ? UFP_NOWAIT : UFP_ALL) != npages) {
    424 			if (!glocked) {
    425 				genfs_node_unlock(vp);
    426 			}
    427 			KASSERT(async != 0);
    428 			genfs_rel_pages(pgs, npages);
    429 			mutex_exit(uobj->vmobjlock);
    430 			error = EBUSY;
    431 			goto out_err_free;
    432 		}
    433 	}
    434 
    435 	mutex_exit(uobj->vmobjlock);
    436 	error = genfs_getpages_read(vp, pgs, npages, startoffset, diskeof,
    437 	    async, memwrite, blockalloc, glocked);
    438 	if (!glocked) {
    439 		genfs_node_unlock(vp);
    440 	}
    441 	if (error == 0 && async)
    442 		goto out_err_free;
    443 	mutex_enter(uobj->vmobjlock);
    444 
    445 	/*
    446 	 * we're almost done!  release the pages...
    447 	 * for errors, we free the pages.
    448 	 * otherwise we activate them and mark them as valid and clean.
    449 	 * also, unbusy pages that were not actually requested.
    450 	 */
    451 
    452 	if (error) {
    453 		genfs_rel_pages(pgs, npages);
    454 		mutex_exit(uobj->vmobjlock);
    455 		UVMHIST_LOG(ubchist, "returning error %jd", error,0,0,0);
    456 		goto out_err_free;
    457 	}
    458 
    459 out:
    460 	UVMHIST_LOG(ubchist, "succeeding, npages %jd", npages,0,0,0);
    461 	error = 0;
    462 	mutex_enter(&uvm_pageqlock);
    463 	for (i = 0; i < npages; i++) {
    464 		struct vm_page *pg = pgs[i];
    465 		if (pg == NULL) {
    466 			continue;
    467 		}
    468 		UVMHIST_LOG(ubchist, "examining pg %#jx flags 0x%jx",
    469 		    (uintptr_t)pg, pg->flags, 0,0);
    470 		if (pg->flags & PG_FAKE && !overwrite) {
    471 			pg->flags &= ~(PG_FAKE);
    472 			pmap_clear_modify(pgs[i]);
    473 		}
    474 		KASSERT(!memwrite || !blockalloc || (pg->flags & PG_RDONLY) == 0);
    475 		if (i < ridx || i >= ridx + orignmempages || async) {
    476 			UVMHIST_LOG(ubchist, "unbusy pg %#jx offset 0x%jx",
    477 			    (uintptr_t)pg, pg->offset,0,0);
    478 			if (pg->flags & PG_WANTED) {
    479 				wakeup(pg);
    480 			}
    481 			if (pg->flags & PG_FAKE) {
    482 				KASSERT(overwrite);
    483 				uvm_pagezero(pg);
    484 			}
    485 			if (pg->flags & PG_RELEASED) {
    486 				uvm_pagefree(pg);
    487 				continue;
    488 			}
    489 			uvm_pageenqueue(pg);
    490 			pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
    491 			UVM_PAGE_OWN(pg, NULL);
    492 		}
    493 	}
    494 	mutex_exit(&uvm_pageqlock);
    495 	if (memwrite) {
    496 		genfs_markdirty(vp);
    497 	}
    498 	mutex_exit(uobj->vmobjlock);
    499 	if (ap->a_m != NULL) {
    500 		memcpy(ap->a_m, &pgs[ridx],
    501 		    orignmempages * sizeof(struct vm_page *));
    502 	}
    503 
    504 out_err_free:
    505 	if (pgs != NULL && pgs != pgs_onstack)
    506 		kmem_free(pgs, pgs_size);
    507 out_err:
    508 	if (trans_mount != NULL) {
    509 		if (holds_wapbl)
    510 			WAPBL_END(trans_mount);
    511 		fstrans_done(trans_mount);
    512 	}
    513 	return error;
    514 }
    515 
    516 /*
    517  * genfs_getpages_read: Read the pages in with VOP_BMAP/VOP_STRATEGY.
    518  *
    519  * "glocked" (which is currently not actually used) tells us not whether
    520  * the genfs_node is locked on entry (it always is) but whether it was
    521  * locked on entry to genfs_getpages.
    522  */
    523 static int
    524 genfs_getpages_read(struct vnode *vp, struct vm_page **pgs, int npages,
    525     off_t startoffset, off_t diskeof,
    526     bool async, bool memwrite, bool blockalloc, bool glocked)
    527 {
    528 	struct uvm_object * const uobj = &vp->v_uobj;
    529 	const int fs_bshift = (vp->v_type != VBLK) ?
    530 	    vp->v_mount->mnt_fs_bshift : DEV_BSHIFT;
    531 	const int dev_bshift = (vp->v_type != VBLK) ?
    532 	    vp->v_mount->mnt_dev_bshift : DEV_BSHIFT;
    533 	kauth_cred_t const cred = curlwp->l_cred;		/* XXXUBC curlwp */
    534 	size_t bytes, iobytes, tailstart, tailbytes, totalbytes, skipbytes;
    535 	vaddr_t kva;
    536 	struct buf *bp, *mbp;
    537 	bool sawhole = false;
    538 	int i;
    539 	int error = 0;
    540 
    541 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
    542 
    543 	/*
    544 	 * read the desired page(s).
    545 	 */
    546 
    547 	totalbytes = npages << PAGE_SHIFT;
    548 	bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
    549 	tailbytes = totalbytes - bytes;
    550 	skipbytes = 0;
    551 
    552 	kva = uvm_pagermapin(pgs, npages,
    553 	    UVMPAGER_MAPIN_READ | (async ? 0 : UVMPAGER_MAPIN_WAITOK));
    554 	if (kva == 0)
    555 		return EBUSY;
    556 
    557 	mbp = getiobuf(vp, true);
    558 	mbp->b_bufsize = totalbytes;
    559 	mbp->b_data = (void *)kva;
    560 	mbp->b_resid = mbp->b_bcount = bytes;
    561 	mbp->b_cflags = BC_BUSY;
    562 	if (async) {
    563 		mbp->b_flags = B_READ | B_ASYNC;
    564 		mbp->b_iodone = uvm_aio_biodone;
    565 	} else {
    566 		mbp->b_flags = B_READ;
    567 		mbp->b_iodone = NULL;
    568 	}
    569 	if (async)
    570 		BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
    571 	else
    572 		BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
    573 
    574 	/*
    575 	 * if EOF is in the middle of the range, zero the part past EOF.
    576 	 * skip over pages which are not PG_FAKE since in that case they have
    577 	 * valid data that we need to preserve.
    578 	 */
    579 
    580 	tailstart = bytes;
    581 	while (tailbytes > 0) {
    582 		const int len = PAGE_SIZE - (tailstart & PAGE_MASK);
    583 
    584 		KASSERT(len <= tailbytes);
    585 		if ((pgs[tailstart >> PAGE_SHIFT]->flags & PG_FAKE) != 0) {
    586 			memset((void *)(kva + tailstart), 0, len);
    587 			UVMHIST_LOG(ubchist, "tailbytes %#jx 0x%jx 0x%jx",
    588 			    (uintptr_t)kva, tailstart, len, 0);
    589 		}
    590 		tailstart += len;
    591 		tailbytes -= len;
    592 	}
    593 
    594 	/*
    595 	 * now loop over the pages, reading as needed.
    596 	 */
    597 
    598 	bp = NULL;
    599 	off_t offset;
    600 	for (offset = startoffset;
    601 	    bytes > 0;
    602 	    offset += iobytes, bytes -= iobytes) {
    603 		int run;
    604 		daddr_t lbn, blkno;
    605 		int pidx;
    606 		struct vnode *devvp;
    607 
    608 		/*
    609 		 * skip pages which don't need to be read.
    610 		 */
    611 
    612 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    613 		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
    614 			size_t b;
    615 
    616 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    617 			if ((pgs[pidx]->flags & PG_RDONLY)) {
    618 				sawhole = true;
    619 			}
    620 			b = MIN(PAGE_SIZE, bytes);
    621 			offset += b;
    622 			bytes -= b;
    623 			skipbytes += b;
    624 			pidx++;
    625 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%jx",
    626 			    offset, 0,0,0);
    627 			if (bytes == 0) {
    628 				goto loopdone;
    629 			}
    630 		}
    631 
    632 		/*
    633 		 * bmap the file to find out the blkno to read from and
    634 		 * how much we can read in one i/o.  if bmap returns an error,
    635 		 * skip the rest of the top-level i/o.
    636 		 */
    637 
    638 		lbn = offset >> fs_bshift;
    639 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
    640 		if (error) {
    641 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%jx -> %jd\n",
    642 			    lbn,error,0,0);
    643 			skipbytes += bytes;
    644 			bytes = 0;
    645 			goto loopdone;
    646 		}
    647 
    648 		/*
    649 		 * see how many pages can be read with this i/o.
    650 		 * reduce the i/o size if necessary to avoid
    651 		 * overwriting pages with valid data.
    652 		 */
    653 
    654 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    655 		    bytes);
    656 		if (offset + iobytes > round_page(offset)) {
    657 			int pcount;
    658 
    659 			pcount = 1;
    660 			while (pidx + pcount < npages &&
    661 			    pgs[pidx + pcount]->flags & PG_FAKE) {
    662 				pcount++;
    663 			}
    664 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    665 			    (offset - trunc_page(offset)));
    666 		}
    667 
    668 		/*
    669 		 * if this block isn't allocated, zero it instead of
    670 		 * reading it.  unless we are going to allocate blocks,
    671 		 * mark the pages we zeroed PG_RDONLY.
    672 		 */
    673 
    674 		if (blkno == (daddr_t)-1) {
    675 			int holepages = (round_page(offset + iobytes) -
    676 			    trunc_page(offset)) >> PAGE_SHIFT;
    677 			UVMHIST_LOG(ubchist, "lbn 0x%jx -> HOLE", lbn,0,0,0);
    678 
    679 			sawhole = true;
    680 			memset((char *)kva + (offset - startoffset), 0,
    681 			    iobytes);
    682 			skipbytes += iobytes;
    683 
    684 			mutex_enter(uobj->vmobjlock);
    685 			for (i = 0; i < holepages; i++) {
    686 				if (memwrite) {
    687 					pgs[pidx + i]->flags &= ~PG_CLEAN;
    688 				}
    689 				if (!blockalloc) {
    690 					pgs[pidx + i]->flags |= PG_RDONLY;
    691 				}
    692 			}
    693 			mutex_exit(uobj->vmobjlock);
    694 			continue;
    695 		}
    696 
    697 		/*
    698 		 * allocate a sub-buf for this piece of the i/o
    699 		 * (or just use mbp if there's only 1 piece),
    700 		 * and start it going.
    701 		 */
    702 
    703 		if (offset == startoffset && iobytes == bytes) {
    704 			bp = mbp;
    705 		} else {
    706 			UVMHIST_LOG(ubchist, "vp %#jx bp %#jx num now %jd",
    707 			    (uintptr_t)vp, (uintptr_t)bp, vp->v_numoutput, 0);
    708 			bp = getiobuf(vp, true);
    709 			nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
    710 		}
    711 		bp->b_lblkno = 0;
    712 
    713 		/* adjust physical blkno for partial blocks */
    714 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    715 		    dev_bshift);
    716 
    717 		UVMHIST_LOG(ubchist,
    718 		    "bp %#jx offset 0x%x bcount 0x%x blkno 0x%x",
    719 		    (uintptr_t)bp, offset, bp->b_bcount, bp->b_blkno);
    720 
    721 		VOP_STRATEGY(devvp, bp);
    722 	}
    723 
    724 loopdone:
    725 	nestiobuf_done(mbp, skipbytes, error);
    726 	if (async) {
    727 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
    728 		return 0;
    729 	}
    730 	if (bp != NULL) {
    731 		error = biowait(mbp);
    732 	}
    733 
    734 	/* Remove the mapping (make KVA available as soon as possible) */
    735 	uvm_pagermapout(kva, npages);
    736 
    737 	/*
    738 	 * if this we encountered a hole then we have to do a little more work.
    739 	 * for read faults, we marked the page PG_RDONLY so that future
    740 	 * write accesses to the page will fault again.
    741 	 * for write faults, we must make sure that the backing store for
    742 	 * the page is completely allocated while the pages are locked.
    743 	 */
    744 
    745 	if (!error && sawhole && blockalloc) {
    746 		error = GOP_ALLOC(vp, startoffset,
    747 		    npages << PAGE_SHIFT, 0, cred);
    748 		UVMHIST_LOG(ubchist, "gop_alloc off 0x%jx/0x%jx -> %jd",
    749 		    startoffset, npages << PAGE_SHIFT, error,0);
    750 		if (!error) {
    751 			mutex_enter(uobj->vmobjlock);
    752 			for (i = 0; i < npages; i++) {
    753 				struct vm_page *pg = pgs[i];
    754 
    755 				if (pg == NULL) {
    756 					continue;
    757 				}
    758 				pg->flags &= ~(PG_CLEAN|PG_RDONLY);
    759 				UVMHIST_LOG(ubchist, "mark dirty pg %#jx",
    760 				    (uintptr_t)pg, 0, 0, 0);
    761 			}
    762 			mutex_exit(uobj->vmobjlock);
    763 		}
    764 	}
    765 
    766 	putiobuf(mbp);
    767 	return error;
    768 }
    769 
    770 /*
    771  * generic VM putpages routine.
    772  * Write the given range of pages to backing store.
    773  *
    774  * => "offhi == 0" means flush all pages at or after "offlo".
    775  * => object should be locked by caller.  we return with the
    776  *      object unlocked.
    777  * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
    778  *	thus, a caller might want to unlock higher level resources
    779  *	(e.g. vm_map) before calling flush.
    780  * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, we will not block
    781  * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
    782  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    783  *	that new pages are inserted on the tail end of the list.   thus,
    784  *	we can make a complete pass through the object in one go by starting
    785  *	at the head and working towards the tail (new pages are put in
    786  *	front of us).
    787  * => NOTE: we are allowed to lock the page queues, so the caller
    788  *	must not be holding the page queue lock.
    789  *
    790  * note on "cleaning" object and PG_BUSY pages:
    791  *	this routine is holding the lock on the object.   the only time
    792  *	that it can run into a PG_BUSY page that it does not own is if
    793  *	some other process has started I/O on the page (e.g. either
    794  *	a pagein, or a pageout).    if the PG_BUSY page is being paged
    795  *	in, then it can not be dirty (!PG_CLEAN) because no one has
    796  *	had a chance to modify it yet.    if the PG_BUSY page is being
    797  *	paged out then it means that someone else has already started
    798  *	cleaning the page for us (how nice!).    in this case, if we
    799  *	have syncio specified, then after we make our pass through the
    800  *	object we need to wait for the other PG_BUSY pages to clear
    801  *	off (i.e. we need to do an iosync).   also note that once a
    802  *	page is PG_BUSY it must stay in its object until it is un-busyed.
    803  *
    804  * note on page traversal:
    805  *	we can traverse the pages in an object either by going down the
    806  *	linked list in "uobj->memq", or we can go over the address range
    807  *	by page doing hash table lookups for each address.    depending
    808  *	on how many pages are in the object it may be cheaper to do one
    809  *	or the other.   we set "by_list" to true if we are using memq.
    810  *	if the cost of a hash lookup was equal to the cost of the list
    811  *	traversal we could compare the number of pages in the start->stop
    812  *	range to the total number of pages in the object.   however, it
    813  *	seems that a hash table lookup is more expensive than the linked
    814  *	list traversal, so we multiply the number of pages in the
    815  *	range by an estimate of the relatively higher cost of the hash lookup.
    816  */
    817 
    818 int
    819 genfs_putpages(void *v)
    820 {
    821 	struct vop_putpages_args /* {
    822 		struct vnode *a_vp;
    823 		voff_t a_offlo;
    824 		voff_t a_offhi;
    825 		int a_flags;
    826 	} */ * const ap = v;
    827 
    828 	return genfs_do_putpages(ap->a_vp, ap->a_offlo, ap->a_offhi,
    829 	    ap->a_flags, NULL);
    830 }
    831 
    832 int
    833 genfs_do_putpages(struct vnode *vp, off_t startoff, off_t endoff,
    834     int origflags, struct vm_page **busypg)
    835 {
    836 	struct uvm_object * const uobj = &vp->v_uobj;
    837 	kmutex_t * const slock = uobj->vmobjlock;
    838 	off_t off;
    839 	int i, error, npages, nback;
    840 	int freeflag;
    841 #if 1
    842 	unsigned int maxpages;
    843 	struct vm_page *pgs[MACHINE_MAXPHYS >> PAGE_SHIFT];
    844 #else
    845 	unsigned int maxpages = 64;
    846 	struct vm_page *pgs[64];
    847 #endif
    848 	struct vm_page *pg, *nextpg, *tpg, curmp, endmp;
    849 	bool wasclean, by_list, needs_clean, yld;
    850 	bool async = (origflags & PGO_SYNCIO) == 0;
    851 	bool pagedaemon = curlwp == uvm.pagedaemon_lwp;
    852 	struct lwp * const l = curlwp ? curlwp : &lwp0;
    853 	struct genfs_node * const gp = VTOG(vp);
    854 	struct mount *trans_mp;
    855 	int flags;
    856 	int dirtygen;
    857 	bool modified;
    858 	bool holds_wapbl;
    859 	bool cleanall;
    860 	bool onworklst;
    861 	static int printed;
    862 
    863         if (vp->v_mount && vp->v_mount->mnt_maxphys) {
    864                 maxpages = vp->v_mount->mnt_maxphys >> PAGE_SHIFT;
    865         } else {
    866                 maxpages = MAXPHYS >> PAGE_SHIFT;
    867         }
    868 
    869 	if (!printed || maxpages > printed ) {
    870 		printf("putpages: maxpages %d\n", maxpages);
    871 		printed = maxpages;
    872 	}
    873 
    874 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
    875 
    876 	KASSERT(origflags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
    877 	KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
    878 	KASSERT(startoff < endoff || endoff == 0);
    879 
    880 	UVMHIST_LOG(ubchist, "vp %#jx pages %jd off 0x%jx len 0x%jx",
    881 	    (uintptr_t)vp, uobj->uo_npages, startoff, endoff - startoff);
    882 
    883 	trans_mp = NULL;
    884 	holds_wapbl = false;
    885 
    886 retry:
    887 	modified = false;
    888 	flags = origflags;
    889 	KASSERT((vp->v_iflag & VI_ONWORKLST) != 0 ||
    890 	    (vp->v_iflag & VI_WRMAPDIRTY) == 0);
    891 	if (uobj->uo_npages == 0) {
    892 		if (vp->v_iflag & VI_ONWORKLST) {
    893 			vp->v_iflag &= ~VI_WRMAPDIRTY;
    894 			if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
    895 				vn_syncer_remove_from_worklist(vp);
    896 		}
    897 		if (trans_mp) {
    898 			if (holds_wapbl)
    899 				WAPBL_END(trans_mp);
    900 			fstrans_done(trans_mp);
    901 		}
    902 		mutex_exit(slock);
    903 		return (0);
    904 	}
    905 
    906 	/*
    907 	 * the vnode has pages, set up to process the request.
    908 	 */
    909 
    910 	if (trans_mp == NULL && (flags & PGO_CLEANIT) != 0) {
    911 		if (pagedaemon) {
    912 			/* Pagedaemon must not sleep here. */
    913 			trans_mp = vp->v_mount;
    914 			error = fstrans_start_nowait(trans_mp);
    915 			if (error) {
    916 				mutex_exit(slock);
    917 				return error;
    918 			}
    919 		} else {
    920 			/*
    921 			 * Cannot use vdeadcheck() here as this operation
    922 			 * usually gets used from VOP_RECLAIM().  Test for
    923 			 * change of v_mount instead and retry on change.
    924 			 */
    925 			mutex_exit(slock);
    926 			trans_mp = vp->v_mount;
    927 			fstrans_start(trans_mp);
    928 			if (vp->v_mount != trans_mp) {
    929 				fstrans_done(trans_mp);
    930 				trans_mp = NULL;
    931 			} else {
    932 				holds_wapbl = (trans_mp->mnt_wapbl &&
    933 				    (origflags & PGO_JOURNALLOCKED) == 0);
    934 				if (holds_wapbl) {
    935 					error = WAPBL_BEGIN(trans_mp);
    936 					if (error) {
    937 						fstrans_done(trans_mp);
    938 						return error;
    939 					}
    940 				}
    941 			}
    942 			mutex_enter(slock);
    943 			goto retry;
    944 		}
    945 	}
    946 
    947 	error = 0;
    948 	wasclean = (vp->v_numoutput == 0);
    949 	off = startoff;
    950 	if (endoff == 0 || flags & PGO_ALLPAGES) {
    951 		endoff = trunc_page(LLONG_MAX);
    952 	}
    953 	by_list = (uobj->uo_npages <=
    954 	    ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_TREE_PENALTY);
    955 
    956 	/*
    957 	 * if this vnode is known not to have dirty pages,
    958 	 * don't bother to clean it out.
    959 	 */
    960 
    961 	if ((vp->v_iflag & VI_ONWORKLST) == 0) {
    962 #if !defined(DEBUG)
    963 		if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == 0) {
    964 			goto skip_scan;
    965 		}
    966 #endif /* !defined(DEBUG) */
    967 		flags &= ~PGO_CLEANIT;
    968 	}
    969 
    970 	/*
    971 	 * start the loop.  when scanning by list, hold the last page
    972 	 * in the list before we start.  pages allocated after we start
    973 	 * will be added to the end of the list, so we can stop at the
    974 	 * current last page.
    975 	 */
    976 
    977 	cleanall = (flags & PGO_CLEANIT) != 0 && wasclean &&
    978 	    startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
    979 	    (vp->v_iflag & VI_ONWORKLST) != 0;
    980 	dirtygen = gp->g_dirtygen;
    981 	freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
    982 	if (by_list) {
    983 		curmp.flags = PG_MARKER;
    984 		endmp.flags = PG_MARKER;
    985 		pg = TAILQ_FIRST(&uobj->memq);
    986 		TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq.queue);
    987 	} else {
    988 		pg = uvm_pagelookup(uobj, off);
    989 	}
    990 	nextpg = NULL;
    991 	while (by_list || off < endoff) {
    992 
    993 		/*
    994 		 * if the current page is not interesting, move on to the next.
    995 		 */
    996 
    997 		KASSERT(pg == NULL || pg->uobject == uobj ||
    998 		    (pg->flags & PG_MARKER) != 0);
    999 		KASSERT(pg == NULL ||
   1000 		    (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
   1001 		    (pg->flags & (PG_BUSY|PG_MARKER)) != 0);
   1002 		if (by_list) {
   1003 			if (pg == &endmp) {
   1004 				break;
   1005 			}
   1006 			if (pg->flags & PG_MARKER) {
   1007 				pg = TAILQ_NEXT(pg, listq.queue);
   1008 				continue;
   1009 			}
   1010 			if (pg->offset < startoff || pg->offset >= endoff ||
   1011 			    pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1012 				if (pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1013 					wasclean = false;
   1014 				}
   1015 				pg = TAILQ_NEXT(pg, listq.queue);
   1016 				continue;
   1017 			}
   1018 			off = pg->offset;
   1019 		} else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1020 			if (pg != NULL) {
   1021 				wasclean = false;
   1022 			}
   1023 			off += PAGE_SIZE;
   1024 			if (off < endoff) {
   1025 				pg = uvm_pagelookup(uobj, off);
   1026 			}
   1027 			continue;
   1028 		}
   1029 
   1030 		/*
   1031 		 * if the current page needs to be cleaned and it's busy,
   1032 		 * wait for it to become unbusy.
   1033 		 */
   1034 
   1035 		yld = (l->l_cpu->ci_schedstate.spc_flags &
   1036 		    SPCF_SHOULDYIELD) && !pagedaemon;
   1037 		if (pg->flags & PG_BUSY || yld) {
   1038 			UVMHIST_LOG(ubchist, "busy %#jx", (uintptr_t)pg,
   1039 			   0, 0, 0);
   1040 			if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
   1041 				UVMHIST_LOG(ubchist, "busyfail %#jx",
   1042 				    (uintptr_t)pg, 0, 0, 0);
   1043 				error = EDEADLK;
   1044 				if (busypg != NULL)
   1045 					*busypg = pg;
   1046 				break;
   1047 			}
   1048 			if (pagedaemon) {
   1049 				/*
   1050 				 * someone has taken the page while we
   1051 				 * dropped the lock for fstrans_start.
   1052 				 */
   1053 				break;
   1054 			}
   1055 			if (by_list) {
   1056 				TAILQ_INSERT_BEFORE(pg, &curmp, listq.queue);
   1057 				UVMHIST_LOG(ubchist, "curmp next %#jx",
   1058 				    (uintptr_t)TAILQ_NEXT(&curmp, listq.queue),
   1059 				    0, 0, 0);
   1060 			}
   1061 			if (yld) {
   1062 				mutex_exit(slock);
   1063 				preempt();
   1064 				mutex_enter(slock);
   1065 			} else {
   1066 				pg->flags |= PG_WANTED;
   1067 				UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
   1068 				mutex_enter(slock);
   1069 			}
   1070 			if (by_list) {
   1071 				UVMHIST_LOG(ubchist, "after next %#jx",
   1072 				    (uintptr_t)TAILQ_NEXT(&curmp, listq.queue),
   1073 				    0, 0, 0);
   1074 				pg = TAILQ_NEXT(&curmp, listq.queue);
   1075 				TAILQ_REMOVE(&uobj->memq, &curmp, listq.queue);
   1076 			} else {
   1077 				pg = uvm_pagelookup(uobj, off);
   1078 			}
   1079 			continue;
   1080 		}
   1081 
   1082 		/*
   1083 		 * if we're freeing, remove all mappings of the page now.
   1084 		 * if we're cleaning, check if the page is needs to be cleaned.
   1085 		 */
   1086 
   1087 		if (flags & PGO_FREE) {
   1088 			pmap_page_protect(pg, VM_PROT_NONE);
   1089 		} else if (flags & PGO_CLEANIT) {
   1090 
   1091 			/*
   1092 			 * if we still have some hope to pull this vnode off
   1093 			 * from the syncer queue, write-protect the page.
   1094 			 */
   1095 
   1096 			if (cleanall && wasclean &&
   1097 			    gp->g_dirtygen == dirtygen) {
   1098 
   1099 				/*
   1100 				 * uobj pages get wired only by uvm_fault
   1101 				 * where uobj is locked.
   1102 				 */
   1103 
   1104 				if (pg->wire_count == 0) {
   1105 					pmap_page_protect(pg,
   1106 					    VM_PROT_READ|VM_PROT_EXECUTE);
   1107 				} else {
   1108 					cleanall = false;
   1109 				}
   1110 			}
   1111 		}
   1112 
   1113 		if (flags & PGO_CLEANIT) {
   1114 			needs_clean = pmap_clear_modify(pg) ||
   1115 			    (pg->flags & PG_CLEAN) == 0;
   1116 			pg->flags |= PG_CLEAN;
   1117 		} else {
   1118 			needs_clean = false;
   1119 		}
   1120 
   1121 		/*
   1122 		 * if we're cleaning, build a cluster.
   1123 		 * the cluster will consist of pages which are currently dirty,
   1124 		 * but they will be returned to us marked clean.
   1125 		 * if not cleaning, just operate on the one page.
   1126 		 */
   1127 
   1128 		if (needs_clean) {
   1129 			KDASSERT((vp->v_iflag & VI_ONWORKLST));
   1130 			wasclean = false;
   1131 			memset(pgs, 0, sizeof(pgs));
   1132 			pg->flags |= PG_BUSY;
   1133 			UVM_PAGE_OWN(pg, "genfs_putpages");
   1134 
   1135 			/*
   1136 			 * first look backward.
   1137 			 */
   1138 
   1139 			npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
   1140 			nback = npages;
   1141 			uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
   1142 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
   1143 			if (nback) {
   1144 				memmove(&pgs[0], &pgs[npages - nback],
   1145 				    nback * sizeof(pgs[0]));
   1146 				if (npages - nback < nback)
   1147 					memset(&pgs[nback], 0,
   1148 					    (npages - nback) * sizeof(pgs[0]));
   1149 				else
   1150 					memset(&pgs[npages - nback], 0,
   1151 					    nback * sizeof(pgs[0]));
   1152 			}
   1153 
   1154 			/*
   1155 			 * then plug in our page of interest.
   1156 			 */
   1157 
   1158 			pgs[nback] = pg;
   1159 
   1160 			/*
   1161 			 * then look forward to fill in the remaining space in
   1162 			 * the array of pages.
   1163 			 */
   1164 
   1165 			npages = maxpages - nback - 1;
   1166 			uvn_findpages(uobj, off + PAGE_SIZE, &npages,
   1167 			    &pgs[nback + 1],
   1168 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
   1169 			npages += nback + 1;
   1170 		} else {
   1171 			pgs[0] = pg;
   1172 			npages = 1;
   1173 			nback = 0;
   1174 		}
   1175 
   1176 		/*
   1177 		 * apply FREE or DEACTIVATE options if requested.
   1178 		 */
   1179 
   1180 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1181 			mutex_enter(&uvm_pageqlock);
   1182 		}
   1183 		for (i = 0; i < npages; i++) {
   1184 			tpg = pgs[i];
   1185 			KASSERT(tpg->uobject == uobj);
   1186 			if (by_list && tpg == TAILQ_NEXT(pg, listq.queue))
   1187 				pg = tpg;
   1188 			if (tpg->offset < startoff || tpg->offset >= endoff)
   1189 				continue;
   1190 			if (flags & PGO_DEACTIVATE && tpg->wire_count == 0) {
   1191 				uvm_pagedeactivate(tpg);
   1192 			} else if (flags & PGO_FREE) {
   1193 				pmap_page_protect(tpg, VM_PROT_NONE);
   1194 				if (tpg->flags & PG_BUSY) {
   1195 					tpg->flags |= freeflag;
   1196 					if (pagedaemon) {
   1197 						uvm_pageout_start(1);
   1198 						uvm_pagedequeue(tpg);
   1199 					}
   1200 				} else {
   1201 
   1202 					/*
   1203 					 * ``page is not busy''
   1204 					 * implies that npages is 1
   1205 					 * and needs_clean is false.
   1206 					 */
   1207 
   1208 					nextpg = TAILQ_NEXT(tpg, listq.queue);
   1209 					uvm_pagefree(tpg);
   1210 					if (pagedaemon)
   1211 						uvmexp.pdfreed++;
   1212 				}
   1213 			}
   1214 		}
   1215 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1216 			mutex_exit(&uvm_pageqlock);
   1217 		}
   1218 		if (needs_clean) {
   1219 			modified = true;
   1220 
   1221 			/*
   1222 			 * start the i/o.  if we're traversing by list,
   1223 			 * keep our place in the list with a marker page.
   1224 			 */
   1225 
   1226 			if (by_list) {
   1227 				TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
   1228 				    listq.queue);
   1229 			}
   1230 			mutex_exit(slock);
   1231 			error = GOP_WRITE(vp, pgs, npages, flags);
   1232 			mutex_enter(slock);
   1233 			if (by_list) {
   1234 				pg = TAILQ_NEXT(&curmp, listq.queue);
   1235 				TAILQ_REMOVE(&uobj->memq, &curmp, listq.queue);
   1236 			}
   1237 			if (error) {
   1238 				break;
   1239 			}
   1240 			if (by_list) {
   1241 				continue;
   1242 			}
   1243 		}
   1244 
   1245 		/*
   1246 		 * find the next page and continue if there was no error.
   1247 		 */
   1248 
   1249 		if (by_list) {
   1250 			if (nextpg) {
   1251 				pg = nextpg;
   1252 				nextpg = NULL;
   1253 			} else {
   1254 				pg = TAILQ_NEXT(pg, listq.queue);
   1255 			}
   1256 		} else {
   1257 			off += (npages - nback) << PAGE_SHIFT;
   1258 			if (off < endoff) {
   1259 				pg = uvm_pagelookup(uobj, off);
   1260 			}
   1261 		}
   1262 	}
   1263 	if (by_list) {
   1264 		TAILQ_REMOVE(&uobj->memq, &endmp, listq.queue);
   1265 	}
   1266 
   1267 	if (modified && (vp->v_iflag & VI_WRMAPDIRTY) != 0 &&
   1268 	    (vp->v_type != VBLK ||
   1269 	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
   1270 		GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
   1271 	}
   1272 
   1273 	/*
   1274 	 * if we're cleaning and there was nothing to clean,
   1275 	 * take us off the syncer list.  if we started any i/o
   1276 	 * and we're doing sync i/o, wait for all writes to finish.
   1277 	 */
   1278 
   1279 	if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
   1280 	    (vp->v_iflag & VI_ONWORKLST) != 0) {
   1281 #if defined(DEBUG)
   1282 		TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
   1283 			if ((pg->flags & (PG_FAKE | PG_MARKER)) != 0) {
   1284 				continue;
   1285 			}
   1286 			if ((pg->flags & PG_CLEAN) == 0) {
   1287 				printf("%s: %p: !CLEAN\n", __func__, pg);
   1288 			}
   1289 			if (pmap_is_modified(pg)) {
   1290 				printf("%s: %p: modified\n", __func__, pg);
   1291 			}
   1292 		}
   1293 #endif /* defined(DEBUG) */
   1294 		vp->v_iflag &= ~VI_WRMAPDIRTY;
   1295 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
   1296 			vn_syncer_remove_from_worklist(vp);
   1297 	}
   1298 
   1299 #if !defined(DEBUG)
   1300 skip_scan:
   1301 #endif /* !defined(DEBUG) */
   1302 
   1303 	/* Wait for output to complete. */
   1304 	if (!wasclean && !async && vp->v_numoutput != 0) {
   1305 		while (vp->v_numoutput != 0)
   1306 			cv_wait(&vp->v_cv, slock);
   1307 	}
   1308 	onworklst = (vp->v_iflag & VI_ONWORKLST) != 0;
   1309 	mutex_exit(slock);
   1310 
   1311 	if ((flags & PGO_RECLAIM) != 0 && onworklst) {
   1312 		/*
   1313 		 * in the case of PGO_RECLAIM, ensure to make the vnode clean.
   1314 		 * retrying is not a big deal because, in many cases,
   1315 		 * uobj->uo_npages is already 0 here.
   1316 		 */
   1317 		mutex_enter(slock);
   1318 		goto retry;
   1319 	}
   1320 
   1321 	if (trans_mp) {
   1322 		if (holds_wapbl)
   1323 			WAPBL_END(trans_mp);
   1324 		fstrans_done(trans_mp);
   1325 	}
   1326 
   1327 	return (error);
   1328 }
   1329 
   1330 int
   1331 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
   1332 {
   1333 	off_t off;
   1334 	vaddr_t kva;
   1335 	size_t len;
   1336 	int error;
   1337 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
   1338 
   1339 	UVMHIST_LOG(ubchist, "vp %#jx pgs %#jx npages %jd flags 0x%jx",
   1340 	    (uintptr_t)vp, (uintptr_t)pgs, npages, flags);
   1341 
   1342 	off = pgs[0]->offset;
   1343 	kva = uvm_pagermapin(pgs, npages,
   1344 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1345 	len = npages << PAGE_SHIFT;
   1346 
   1347 	error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
   1348 			    uvm_aio_biodone);
   1349 
   1350 	return error;
   1351 }
   1352 
   1353 int
   1354 genfs_gop_write_rwmap(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
   1355 {
   1356 	off_t off;
   1357 	vaddr_t kva;
   1358 	size_t len;
   1359 	int error;
   1360 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
   1361 
   1362 	UVMHIST_LOG(ubchist, "vp %#jx pgs %#jx npages %jd flags 0x%jx",
   1363 	    (uintptr_t)vp, (uintptr_t)pgs, npages, flags);
   1364 
   1365 	off = pgs[0]->offset;
   1366 	kva = uvm_pagermapin(pgs, npages,
   1367 	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
   1368 	len = npages << PAGE_SHIFT;
   1369 
   1370 	error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
   1371 			    uvm_aio_biodone);
   1372 
   1373 	return error;
   1374 }
   1375 
   1376 /*
   1377  * Backend routine for doing I/O to vnode pages.  Pages are already locked
   1378  * and mapped into kernel memory.  Here we just look up the underlying
   1379  * device block addresses and call the strategy routine.
   1380  */
   1381 
   1382 static int
   1383 genfs_do_io(struct vnode *vp, off_t off, vaddr_t kva, size_t len, int flags,
   1384     enum uio_rw rw, void (*iodone)(struct buf *))
   1385 {
   1386 	int s, error;
   1387 	int fs_bshift, dev_bshift;
   1388 	off_t eof, offset, startoffset;
   1389 	size_t bytes, iobytes, skipbytes;
   1390 	struct buf *mbp, *bp;
   1391 	const bool async = (flags & PGO_SYNCIO) == 0;
   1392 	const bool lazy = (flags & PGO_LAZY) == 0;
   1393 	const bool iowrite = rw == UIO_WRITE;
   1394 	const int brw = iowrite ? B_WRITE : B_READ;
   1395 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
   1396 
   1397 	UVMHIST_LOG(ubchist, "vp %#jx kva %#jx len 0x%jx flags 0x%jx",
   1398 	    (uintptr_t)vp, (uintptr_t)kva, len, flags);
   1399 
   1400 	KASSERT(vp->v_size <= vp->v_writesize);
   1401 	GOP_SIZE(vp, vp->v_writesize, &eof, 0);
   1402 	if (vp->v_type != VBLK) {
   1403 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1404 		dev_bshift = vp->v_mount->mnt_dev_bshift;
   1405 	} else {
   1406 		fs_bshift = DEV_BSHIFT;
   1407 		dev_bshift = DEV_BSHIFT;
   1408 	}
   1409 	error = 0;
   1410 	startoffset = off;
   1411 	bytes = MIN(len, eof - startoffset);
   1412 	skipbytes = 0;
   1413 	KASSERT(bytes != 0);
   1414 
   1415 	if (iowrite) {
   1416 		mutex_enter(vp->v_interlock);
   1417 		vp->v_numoutput += 2;
   1418 		mutex_exit(vp->v_interlock);
   1419 	}
   1420 	mbp = getiobuf(vp, true);
   1421 	UVMHIST_LOG(ubchist, "vp %#jx mbp %#jx num now %jd bytes 0x%jx",
   1422 	    (uintptr_t)vp, (uintptr_t)mbp, vp->v_numoutput, bytes);
   1423 	mbp->b_bufsize = len;
   1424 	mbp->b_data = (void *)kva;
   1425 	mbp->b_resid = mbp->b_bcount = bytes;
   1426 	mbp->b_cflags = BC_BUSY | BC_AGE;
   1427 	if (async) {
   1428 		mbp->b_flags = brw | B_ASYNC;
   1429 		mbp->b_iodone = iodone;
   1430 	} else {
   1431 		mbp->b_flags = brw;
   1432 		mbp->b_iodone = NULL;
   1433 	}
   1434 	if (curlwp == uvm.pagedaemon_lwp)
   1435 		BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
   1436 	else if (async || lazy)
   1437 		BIO_SETPRIO(mbp, BPRIO_TIMENONCRITICAL);
   1438 	else
   1439 		BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
   1440 
   1441 	bp = NULL;
   1442 	for (offset = startoffset;
   1443 	    bytes > 0;
   1444 	    offset += iobytes, bytes -= iobytes) {
   1445 		int run;
   1446 		daddr_t lbn, blkno;
   1447 		struct vnode *devvp;
   1448 
   1449 		/*
   1450 		 * bmap the file to find out the blkno to read from and
   1451 		 * how much we can read in one i/o.  if bmap returns an error,
   1452 		 * skip the rest of the top-level i/o.
   1453 		 */
   1454 
   1455 		lbn = offset >> fs_bshift;
   1456 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
   1457 		if (error) {
   1458 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%jx -> %jd\n",
   1459 			    lbn, error, 0, 0);
   1460 			skipbytes += bytes;
   1461 			bytes = 0;
   1462 			goto loopdone;
   1463 		}
   1464 
   1465 		/*
   1466 		 * see how many pages can be read with this i/o.
   1467 		 * reduce the i/o size if necessary to avoid
   1468 		 * overwriting pages with valid data.
   1469 		 */
   1470 
   1471 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1472 		    bytes);
   1473 
   1474 		/*
   1475 		 * if this block isn't allocated, zero it instead of
   1476 		 * reading it.  unless we are going to allocate blocks,
   1477 		 * mark the pages we zeroed PG_RDONLY.
   1478 		 */
   1479 
   1480 		if (blkno == (daddr_t)-1) {
   1481 			if (!iowrite) {
   1482 				memset((char *)kva + (offset - startoffset), 0,
   1483 				    iobytes);
   1484 			}
   1485 			skipbytes += iobytes;
   1486 			continue;
   1487 		}
   1488 
   1489 		/*
   1490 		 * allocate a sub-buf for this piece of the i/o
   1491 		 * (or just use mbp if there's only 1 piece),
   1492 		 * and start it going.
   1493 		 */
   1494 
   1495 		if (offset == startoffset && iobytes == bytes) {
   1496 			bp = mbp;
   1497 		} else {
   1498 			UVMHIST_LOG(ubchist, "vp %#jx bp %#jx num now %jd",
   1499 			    (uintptr_t)vp, (uintptr_t)bp, vp->v_numoutput, 0);
   1500 			bp = getiobuf(vp, true);
   1501 			nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
   1502 		}
   1503 		bp->b_lblkno = 0;
   1504 
   1505 		/* adjust physical blkno for partial blocks */
   1506 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1507 		    dev_bshift);
   1508 
   1509 		UVMHIST_LOG(ubchist,
   1510 		    "bp %#jx offset 0x%jx bcount 0x%jx blkno 0x%jx",
   1511 		    (uintptr_t)bp, offset, bp->b_bcount, bp->b_blkno);
   1512 
   1513 		VOP_STRATEGY(devvp, bp);
   1514 	}
   1515 
   1516 loopdone:
   1517 	if (skipbytes) {
   1518 		UVMHIST_LOG(ubchist, "skipbytes %jd", skipbytes, 0,0,0);
   1519 	}
   1520 	nestiobuf_done(mbp, skipbytes, error);
   1521 	if (async) {
   1522 		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
   1523 		return (0);
   1524 	}
   1525 	UVMHIST_LOG(ubchist, "waiting for mbp %#jx", (uintptr_t)mbp, 0, 0, 0);
   1526 	error = biowait(mbp);
   1527 	s = splbio();
   1528 	(*iodone)(mbp);
   1529 	splx(s);
   1530 	UVMHIST_LOG(ubchist, "returning, error %jd", error, 0, 0, 0);
   1531 	return (error);
   1532 }
   1533 
   1534 int
   1535 genfs_compat_getpages(void *v)
   1536 {
   1537 	struct vop_getpages_args /* {
   1538 		struct vnode *a_vp;
   1539 		voff_t a_offset;
   1540 		struct vm_page **a_m;
   1541 		int *a_count;
   1542 		int a_centeridx;
   1543 		vm_prot_t a_access_type;
   1544 		int a_advice;
   1545 		int a_flags;
   1546 	} */ *ap = v;
   1547 
   1548 	off_t origoffset;
   1549 	struct vnode *vp = ap->a_vp;
   1550 	struct uvm_object *uobj = &vp->v_uobj;
   1551 	struct vm_page *pg, **pgs;
   1552 	vaddr_t kva;
   1553 	int i, error, orignpages, npages;
   1554 	struct iovec iov;
   1555 	struct uio uio;
   1556 	kauth_cred_t cred = curlwp->l_cred;
   1557 	const bool memwrite = (ap->a_access_type & VM_PROT_WRITE) != 0;
   1558 
   1559 	error = 0;
   1560 	origoffset = ap->a_offset;
   1561 	orignpages = *ap->a_count;
   1562 	pgs = ap->a_m;
   1563 
   1564 	if (ap->a_flags & PGO_LOCKED) {
   1565 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
   1566 		    UFP_NOWAIT|UFP_NOALLOC| (memwrite ? UFP_NORDONLY : 0));
   1567 
   1568 		error = ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
   1569 		if (error == 0 && memwrite) {
   1570 			genfs_markdirty(vp);
   1571 		}
   1572 		return error;
   1573 	}
   1574 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
   1575 		mutex_exit(uobj->vmobjlock);
   1576 		return EINVAL;
   1577 	}
   1578 	if ((ap->a_flags & PGO_SYNCIO) == 0) {
   1579 		mutex_exit(uobj->vmobjlock);
   1580 		return 0;
   1581 	}
   1582 	npages = orignpages;
   1583 	uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
   1584 	mutex_exit(uobj->vmobjlock);
   1585 	kva = uvm_pagermapin(pgs, npages,
   1586 	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
   1587 	for (i = 0; i < npages; i++) {
   1588 		pg = pgs[i];
   1589 		if ((pg->flags & PG_FAKE) == 0) {
   1590 			continue;
   1591 		}
   1592 		iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
   1593 		iov.iov_len = PAGE_SIZE;
   1594 		uio.uio_iov = &iov;
   1595 		uio.uio_iovcnt = 1;
   1596 		uio.uio_offset = origoffset + (i << PAGE_SHIFT);
   1597 		uio.uio_rw = UIO_READ;
   1598 		uio.uio_resid = PAGE_SIZE;
   1599 		UIO_SETUP_SYSSPACE(&uio);
   1600 		/* XXX vn_lock */
   1601 		error = VOP_READ(vp, &uio, 0, cred);
   1602 		if (error) {
   1603 			break;
   1604 		}
   1605 		if (uio.uio_resid) {
   1606 			memset(iov.iov_base, 0, uio.uio_resid);
   1607 		}
   1608 	}
   1609 	uvm_pagermapout(kva, npages);
   1610 	mutex_enter(uobj->vmobjlock);
   1611 	mutex_enter(&uvm_pageqlock);
   1612 	for (i = 0; i < npages; i++) {
   1613 		pg = pgs[i];
   1614 		if (error && (pg->flags & PG_FAKE) != 0) {
   1615 			pg->flags |= PG_RELEASED;
   1616 		} else {
   1617 			pmap_clear_modify(pg);
   1618 			uvm_pageactivate(pg);
   1619 		}
   1620 	}
   1621 	if (error) {
   1622 		uvm_page_unbusy(pgs, npages);
   1623 	}
   1624 	mutex_exit(&uvm_pageqlock);
   1625 	if (error == 0 && memwrite) {
   1626 		genfs_markdirty(vp);
   1627 	}
   1628 	mutex_exit(uobj->vmobjlock);
   1629 	return error;
   1630 }
   1631 
   1632 int
   1633 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
   1634     int flags)
   1635 {
   1636 	off_t offset;
   1637 	struct iovec iov;
   1638 	struct uio uio;
   1639 	kauth_cred_t cred = curlwp->l_cred;
   1640 	struct buf *bp;
   1641 	vaddr_t kva;
   1642 	int error;
   1643 
   1644 	offset = pgs[0]->offset;
   1645 	kva = uvm_pagermapin(pgs, npages,
   1646 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1647 
   1648 	iov.iov_base = (void *)kva;
   1649 	iov.iov_len = npages << PAGE_SHIFT;
   1650 	uio.uio_iov = &iov;
   1651 	uio.uio_iovcnt = 1;
   1652 	uio.uio_offset = offset;
   1653 	uio.uio_rw = UIO_WRITE;
   1654 	uio.uio_resid = npages << PAGE_SHIFT;
   1655 	UIO_SETUP_SYSSPACE(&uio);
   1656 	/* XXX vn_lock */
   1657 	error = VOP_WRITE(vp, &uio, 0, cred);
   1658 
   1659 	mutex_enter(vp->v_interlock);
   1660 	vp->v_numoutput++;
   1661 	mutex_exit(vp->v_interlock);
   1662 
   1663 	bp = getiobuf(vp, true);
   1664 	bp->b_cflags = BC_BUSY | BC_AGE;
   1665 	bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
   1666 	bp->b_data = (char *)kva;
   1667 	bp->b_bcount = npages << PAGE_SHIFT;
   1668 	bp->b_bufsize = npages << PAGE_SHIFT;
   1669 	bp->b_resid = 0;
   1670 	bp->b_error = error;
   1671 	uvm_aio_aiodone(bp);
   1672 	return (error);
   1673 }
   1674 
   1675 /*
   1676  * Process a uio using direct I/O.  If we reach a part of the request
   1677  * which cannot be processed in this fashion for some reason, just return.
   1678  * The caller must handle some additional part of the request using
   1679  * buffered I/O before trying direct I/O again.
   1680  */
   1681 
   1682 void
   1683 genfs_directio(struct vnode *vp, struct uio *uio, int ioflag)
   1684 {
   1685 	struct vmspace *vs;
   1686 	struct iovec *iov;
   1687 	vaddr_t va;
   1688 	size_t len;
   1689 	const int mask = DEV_BSIZE - 1;
   1690 	int error;
   1691 	bool need_wapbl = (vp->v_mount && vp->v_mount->mnt_wapbl &&
   1692 	    (ioflag & IO_JOURNALLOCKED) == 0);
   1693 
   1694 	/*
   1695 	 * We only support direct I/O to user space for now.
   1696 	 */
   1697 
   1698 	if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
   1699 		return;
   1700 	}
   1701 
   1702 	/*
   1703 	 * If the vnode is mapped, we would need to get the getpages lock
   1704 	 * to stabilize the bmap, but then we would get into trouble while
   1705 	 * locking the pages if the pages belong to this same vnode (or a
   1706 	 * multi-vnode cascade to the same effect).  Just fall back to
   1707 	 * buffered I/O if the vnode is mapped to avoid this mess.
   1708 	 */
   1709 
   1710 	if (vp->v_vflag & VV_MAPPED) {
   1711 		return;
   1712 	}
   1713 
   1714 	if (need_wapbl) {
   1715 		error = WAPBL_BEGIN(vp->v_mount);
   1716 		if (error)
   1717 			return;
   1718 	}
   1719 
   1720 	/*
   1721 	 * Do as much of the uio as possible with direct I/O.
   1722 	 */
   1723 
   1724 	vs = uio->uio_vmspace;
   1725 	while (uio->uio_resid) {
   1726 		iov = uio->uio_iov;
   1727 		if (iov->iov_len == 0) {
   1728 			uio->uio_iov++;
   1729 			uio->uio_iovcnt--;
   1730 			continue;
   1731 		}
   1732 		va = (vaddr_t)iov->iov_base;
   1733 		len = MIN(iov->iov_len, genfs_maxdio);
   1734 		len &= ~mask;
   1735 
   1736 		/*
   1737 		 * If the next chunk is smaller than DEV_BSIZE or extends past
   1738 		 * the current EOF, then fall back to buffered I/O.
   1739 		 */
   1740 
   1741 		if (len == 0 || uio->uio_offset + len > vp->v_size) {
   1742 			break;
   1743 		}
   1744 
   1745 		/*
   1746 		 * Check alignment.  The file offset must be at least
   1747 		 * sector-aligned.  The exact constraint on memory alignment
   1748 		 * is very hardware-dependent, but requiring sector-aligned
   1749 		 * addresses there too is safe.
   1750 		 */
   1751 
   1752 		if (uio->uio_offset & mask || va & mask) {
   1753 			break;
   1754 		}
   1755 		error = genfs_do_directio(vs, va, len, vp, uio->uio_offset,
   1756 					  uio->uio_rw);
   1757 		if (error) {
   1758 			break;
   1759 		}
   1760 		iov->iov_base = (char *)iov->iov_base + len;
   1761 		iov->iov_len -= len;
   1762 		uio->uio_offset += len;
   1763 		uio->uio_resid -= len;
   1764 	}
   1765 
   1766 	if (need_wapbl)
   1767 		WAPBL_END(vp->v_mount);
   1768 }
   1769 
   1770 /*
   1771  * Iodone routine for direct I/O.  We don't do much here since the request is
   1772  * always synchronous, so the caller will do most of the work after biowait().
   1773  */
   1774 
   1775 static void
   1776 genfs_dio_iodone(struct buf *bp)
   1777 {
   1778 
   1779 	KASSERT((bp->b_flags & B_ASYNC) == 0);
   1780 	if ((bp->b_flags & B_READ) == 0 && (bp->b_cflags & BC_AGE) != 0) {
   1781 		mutex_enter(bp->b_objlock);
   1782 		vwakeup(bp);
   1783 		mutex_exit(bp->b_objlock);
   1784 	}
   1785 	putiobuf(bp);
   1786 }
   1787 
   1788 /*
   1789  * Process one chunk of a direct I/O request.
   1790  */
   1791 
   1792 static int
   1793 genfs_do_directio(struct vmspace *vs, vaddr_t uva, size_t len, struct vnode *vp,
   1794     off_t off, enum uio_rw rw)
   1795 {
   1796 	struct vm_map *map;
   1797 	struct pmap *upm, *kpm __unused;
   1798 	size_t klen = round_page(uva + len) - trunc_page(uva);
   1799 	off_t spoff, epoff;
   1800 	vaddr_t kva, puva;
   1801 	paddr_t pa;
   1802 	vm_prot_t prot;
   1803 	int error, rv __diagused, poff, koff;
   1804 	const int pgoflags = PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED |
   1805 		(rw == UIO_WRITE ? PGO_FREE : 0);
   1806 
   1807 	/*
   1808 	 * For writes, verify that this range of the file already has fully
   1809 	 * allocated backing store.  If there are any holes, just punt and
   1810 	 * make the caller take the buffered write path.
   1811 	 */
   1812 
   1813 	if (rw == UIO_WRITE) {
   1814 		daddr_t lbn, elbn, blkno;
   1815 		int bsize, bshift, run;
   1816 
   1817 		bshift = vp->v_mount->mnt_fs_bshift;
   1818 		bsize = 1 << bshift;
   1819 		lbn = off >> bshift;
   1820 		elbn = (off + len + bsize - 1) >> bshift;
   1821 		while (lbn < elbn) {
   1822 			error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
   1823 			if (error) {
   1824 				return error;
   1825 			}
   1826 			if (blkno == (daddr_t)-1) {
   1827 				return ENOSPC;
   1828 			}
   1829 			lbn += 1 + run;
   1830 		}
   1831 	}
   1832 
   1833 	/*
   1834 	 * Flush any cached pages for parts of the file that we're about to
   1835 	 * access.  If we're writing, invalidate pages as well.
   1836 	 */
   1837 
   1838 	spoff = trunc_page(off);
   1839 	epoff = round_page(off + len);
   1840 	mutex_enter(vp->v_interlock);
   1841 	error = VOP_PUTPAGES(vp, spoff, epoff, pgoflags);
   1842 	if (error) {
   1843 		return error;
   1844 	}
   1845 
   1846 	/*
   1847 	 * Wire the user pages and remap them into kernel memory.
   1848 	 */
   1849 
   1850 	prot = rw == UIO_READ ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
   1851 	error = uvm_vslock(vs, (void *)uva, len, prot);
   1852 	if (error) {
   1853 		return error;
   1854 	}
   1855 
   1856 	map = &vs->vm_map;
   1857 	upm = vm_map_pmap(map);
   1858 	kpm = vm_map_pmap(kernel_map);
   1859 	puva = trunc_page(uva);
   1860 	kva = uvm_km_alloc(kernel_map, klen, atop(puva) & uvmexp.colormask,
   1861 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_COLORMATCH);
   1862 	for (poff = 0; poff < klen; poff += PAGE_SIZE) {
   1863 		rv = pmap_extract(upm, puva + poff, &pa);
   1864 		KASSERT(rv);
   1865 		pmap_kenter_pa(kva + poff, pa, prot, PMAP_WIRED);
   1866 	}
   1867 	pmap_update(kpm);
   1868 
   1869 	/*
   1870 	 * Do the I/O.
   1871 	 */
   1872 
   1873 	koff = uva - trunc_page(uva);
   1874 	error = genfs_do_io(vp, off, kva + koff, len, PGO_SYNCIO, rw,
   1875 			    genfs_dio_iodone);
   1876 
   1877 	/*
   1878 	 * Tear down the kernel mapping.
   1879 	 */
   1880 
   1881 	pmap_kremove(kva, klen);
   1882 	pmap_update(kpm);
   1883 	uvm_km_free(kernel_map, kva, klen, UVM_KMF_VAONLY);
   1884 
   1885 	/*
   1886 	 * Unwire the user pages.
   1887 	 */
   1888 
   1889 	uvm_vsunlock(vs, (void *)uva, len);
   1890 	return error;
   1891 }
   1892