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