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