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