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