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