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