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