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