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