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