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