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