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