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