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