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