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