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