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