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