xref: /freebsd/sys/vm/vnode_pager.c (revision b6a05070fa77edc7ce6e60b61623fd806e807be6)
1 /*-
2  * Copyright (c) 1990 University of Utah.
3  * Copyright (c) 1991 The Regents of the University of California.
4  * All rights reserved.
5  * Copyright (c) 1993, 1994 John S. Dyson
6  * Copyright (c) 1995, David Greenman
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
41  */
42 
43 /*
44  * Page to/from files (vnodes).
45  */
46 
47 /*
48  * TODO:
49  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
50  *	greatly re-simplify the vnode_pager.
51  */
52 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 #include "opt_vm.h"
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/proc.h>
61 #include <sys/vnode.h>
62 #include <sys/mount.h>
63 #include <sys/bio.h>
64 #include <sys/buf.h>
65 #include <sys/vmmeter.h>
66 #include <sys/limits.h>
67 #include <sys/conf.h>
68 #include <sys/rwlock.h>
69 #include <sys/sf_buf.h>
70 
71 #include <machine/atomic.h>
72 
73 #include <vm/vm.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_map.h>
79 #include <vm/vnode_pager.h>
80 #include <vm/vm_extern.h>
81 
82 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
83     daddr_t *rtaddress, int *run);
84 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
85 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
86 static void vnode_pager_dealloc(vm_object_t);
87 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
88 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
89     int *, vop_getpages_iodone_t, void *);
90 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
91 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
92 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
93     vm_ooffset_t, struct ucred *cred);
94 static int vnode_pager_generic_getpages_done(struct buf *);
95 static void vnode_pager_generic_getpages_done_async(struct buf *);
96 
97 struct pagerops vnodepagerops = {
98 	.pgo_alloc =	vnode_pager_alloc,
99 	.pgo_dealloc =	vnode_pager_dealloc,
100 	.pgo_getpages =	vnode_pager_getpages,
101 	.pgo_getpages_async = vnode_pager_getpages_async,
102 	.pgo_putpages =	vnode_pager_putpages,
103 	.pgo_haspage =	vnode_pager_haspage,
104 };
105 
106 int vnode_pbuf_freecnt;
107 int vnode_async_pbuf_freecnt;
108 
109 /* Create the VM system backing object for this vnode */
110 int
111 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
112 {
113 	vm_object_t object;
114 	vm_ooffset_t size = isize;
115 	struct vattr va;
116 
117 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
118 		return (0);
119 
120 	while ((object = vp->v_object) != NULL) {
121 		VM_OBJECT_WLOCK(object);
122 		if (!(object->flags & OBJ_DEAD)) {
123 			VM_OBJECT_WUNLOCK(object);
124 			return (0);
125 		}
126 		VOP_UNLOCK(vp, 0);
127 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
128 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
129 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
130 	}
131 
132 	if (size == 0) {
133 		if (vn_isdisk(vp, NULL)) {
134 			size = IDX_TO_OFF(INT_MAX);
135 		} else {
136 			if (VOP_GETATTR(vp, &va, td->td_ucred))
137 				return (0);
138 			size = va.va_size;
139 		}
140 	}
141 
142 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
143 	/*
144 	 * Dereference the reference we just created.  This assumes
145 	 * that the object is associated with the vp.
146 	 */
147 	VM_OBJECT_WLOCK(object);
148 	object->ref_count--;
149 	VM_OBJECT_WUNLOCK(object);
150 	vrele(vp);
151 
152 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
153 
154 	return (0);
155 }
156 
157 void
158 vnode_destroy_vobject(struct vnode *vp)
159 {
160 	struct vm_object *obj;
161 
162 	obj = vp->v_object;
163 	if (obj == NULL)
164 		return;
165 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
166 	VM_OBJECT_WLOCK(obj);
167 	if (obj->ref_count == 0) {
168 		/*
169 		 * don't double-terminate the object
170 		 */
171 		if ((obj->flags & OBJ_DEAD) == 0)
172 			vm_object_terminate(obj);
173 		else
174 			VM_OBJECT_WUNLOCK(obj);
175 	} else {
176 		/*
177 		 * Woe to the process that tries to page now :-).
178 		 */
179 		vm_pager_deallocate(obj);
180 		VM_OBJECT_WUNLOCK(obj);
181 	}
182 	vp->v_object = NULL;
183 }
184 
185 
186 /*
187  * Allocate (or lookup) pager for a vnode.
188  * Handle is a vnode pointer.
189  *
190  * MPSAFE
191  */
192 vm_object_t
193 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
194     vm_ooffset_t offset, struct ucred *cred)
195 {
196 	vm_object_t object;
197 	struct vnode *vp;
198 
199 	/*
200 	 * Pageout to vnode, no can do yet.
201 	 */
202 	if (handle == NULL)
203 		return (NULL);
204 
205 	vp = (struct vnode *) handle;
206 
207 	/*
208 	 * If the object is being terminated, wait for it to
209 	 * go away.
210 	 */
211 retry:
212 	while ((object = vp->v_object) != NULL) {
213 		VM_OBJECT_WLOCK(object);
214 		if ((object->flags & OBJ_DEAD) == 0)
215 			break;
216 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
217 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
218 	}
219 
220 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
221 
222 	if (object == NULL) {
223 		/*
224 		 * Add an object of the appropriate size
225 		 */
226 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
227 
228 		object->un_pager.vnp.vnp_size = size;
229 		object->un_pager.vnp.writemappings = 0;
230 
231 		object->handle = handle;
232 		VI_LOCK(vp);
233 		if (vp->v_object != NULL) {
234 			/*
235 			 * Object has been created while we were sleeping
236 			 */
237 			VI_UNLOCK(vp);
238 			VM_OBJECT_WLOCK(object);
239 			KASSERT(object->ref_count == 1,
240 			    ("leaked ref %p %d", object, object->ref_count));
241 			object->type = OBJT_DEAD;
242 			object->ref_count = 0;
243 			VM_OBJECT_WUNLOCK(object);
244 			vm_object_destroy(object);
245 			goto retry;
246 		}
247 		vp->v_object = object;
248 		VI_UNLOCK(vp);
249 	} else {
250 		object->ref_count++;
251 #if VM_NRESERVLEVEL > 0
252 		vm_object_color(object, 0);
253 #endif
254 		VM_OBJECT_WUNLOCK(object);
255 	}
256 	vref(vp);
257 	return (object);
258 }
259 
260 /*
261  *	The object must be locked.
262  */
263 static void
264 vnode_pager_dealloc(vm_object_t object)
265 {
266 	struct vnode *vp;
267 	int refs;
268 
269 	vp = object->handle;
270 	if (vp == NULL)
271 		panic("vnode_pager_dealloc: pager already dealloced");
272 
273 	VM_OBJECT_ASSERT_WLOCKED(object);
274 	vm_object_pip_wait(object, "vnpdea");
275 	refs = object->ref_count;
276 
277 	object->handle = NULL;
278 	object->type = OBJT_DEAD;
279 	if (object->flags & OBJ_DISCONNECTWNT) {
280 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
281 		wakeup(object);
282 	}
283 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
284 	if (object->un_pager.vnp.writemappings > 0) {
285 		object->un_pager.vnp.writemappings = 0;
286 		VOP_ADD_WRITECOUNT(vp, -1);
287 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
288 		    __func__, vp, vp->v_writecount);
289 	}
290 	vp->v_object = NULL;
291 	VOP_UNSET_TEXT(vp);
292 	VM_OBJECT_WUNLOCK(object);
293 	while (refs-- > 0)
294 		vunref(vp);
295 	VM_OBJECT_WLOCK(object);
296 }
297 
298 static boolean_t
299 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
300     int *after)
301 {
302 	struct vnode *vp = object->handle;
303 	daddr_t bn;
304 	int err;
305 	daddr_t reqblock;
306 	int poff;
307 	int bsize;
308 	int pagesperblock, blocksperpage;
309 
310 	VM_OBJECT_ASSERT_WLOCKED(object);
311 	/*
312 	 * If no vp or vp is doomed or marked transparent to VM, we do not
313 	 * have the page.
314 	 */
315 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
316 		return FALSE;
317 	/*
318 	 * If the offset is beyond end of file we do
319 	 * not have the page.
320 	 */
321 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
322 		return FALSE;
323 
324 	bsize = vp->v_mount->mnt_stat.f_iosize;
325 	pagesperblock = bsize / PAGE_SIZE;
326 	blocksperpage = 0;
327 	if (pagesperblock > 0) {
328 		reqblock = pindex / pagesperblock;
329 	} else {
330 		blocksperpage = (PAGE_SIZE / bsize);
331 		reqblock = pindex * blocksperpage;
332 	}
333 	VM_OBJECT_WUNLOCK(object);
334 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
335 	VM_OBJECT_WLOCK(object);
336 	if (err)
337 		return TRUE;
338 	if (bn == -1)
339 		return FALSE;
340 	if (pagesperblock > 0) {
341 		poff = pindex - (reqblock * pagesperblock);
342 		if (before) {
343 			*before *= pagesperblock;
344 			*before += poff;
345 		}
346 		if (after) {
347 			/*
348 			 * The BMAP vop can report a partial block in the
349 			 * 'after', but must not report blocks after EOF.
350 			 * Assert the latter, and truncate 'after' in case
351 			 * of the former.
352 			 */
353 			KASSERT((reqblock + *after) * pagesperblock <
354 			    roundup2(object->size, pagesperblock),
355 			    ("%s: reqblock %jd after %d size %ju", __func__,
356 			    (intmax_t )reqblock, *after,
357 			    (uintmax_t )object->size));
358 			*after *= pagesperblock;
359 			*after += pagesperblock - (poff + 1);
360 			if (pindex + *after >= object->size)
361 				*after = object->size - 1 - pindex;
362 		}
363 	} else {
364 		if (before) {
365 			*before /= blocksperpage;
366 		}
367 
368 		if (after) {
369 			*after /= blocksperpage;
370 		}
371 	}
372 	return TRUE;
373 }
374 
375 /*
376  * Lets the VM system know about a change in size for a file.
377  * We adjust our own internal size and flush any cached pages in
378  * the associated object that are affected by the size change.
379  *
380  * Note: this routine may be invoked as a result of a pager put
381  * operation (possibly at object termination time), so we must be careful.
382  */
383 void
384 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
385 {
386 	vm_object_t object;
387 	vm_page_t m;
388 	vm_pindex_t nobjsize;
389 
390 	if ((object = vp->v_object) == NULL)
391 		return;
392 /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
393 	VM_OBJECT_WLOCK(object);
394 	if (object->type == OBJT_DEAD) {
395 		VM_OBJECT_WUNLOCK(object);
396 		return;
397 	}
398 	KASSERT(object->type == OBJT_VNODE,
399 	    ("not vnode-backed object %p", object));
400 	if (nsize == object->un_pager.vnp.vnp_size) {
401 		/*
402 		 * Hasn't changed size
403 		 */
404 		VM_OBJECT_WUNLOCK(object);
405 		return;
406 	}
407 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
408 	if (nsize < object->un_pager.vnp.vnp_size) {
409 		/*
410 		 * File has shrunk. Toss any cached pages beyond the new EOF.
411 		 */
412 		if (nobjsize < object->size)
413 			vm_object_page_remove(object, nobjsize, object->size,
414 			    0);
415 		/*
416 		 * this gets rid of garbage at the end of a page that is now
417 		 * only partially backed by the vnode.
418 		 *
419 		 * XXX for some reason (I don't know yet), if we take a
420 		 * completely invalid page and mark it partially valid
421 		 * it can screw up NFS reads, so we don't allow the case.
422 		 */
423 		if ((nsize & PAGE_MASK) &&
424 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
425 		    m->valid != 0) {
426 			int base = (int)nsize & PAGE_MASK;
427 			int size = PAGE_SIZE - base;
428 
429 			/*
430 			 * Clear out partial-page garbage in case
431 			 * the page has been mapped.
432 			 */
433 			pmap_zero_page_area(m, base, size);
434 
435 			/*
436 			 * Update the valid bits to reflect the blocks that
437 			 * have been zeroed.  Some of these valid bits may
438 			 * have already been set.
439 			 */
440 			vm_page_set_valid_range(m, base, size);
441 
442 			/*
443 			 * Round "base" to the next block boundary so that the
444 			 * dirty bit for a partially zeroed block is not
445 			 * cleared.
446 			 */
447 			base = roundup2(base, DEV_BSIZE);
448 
449 			/*
450 			 * Clear out partial-page dirty bits.
451 			 *
452 			 * note that we do not clear out the valid
453 			 * bits.  This would prevent bogus_page
454 			 * replacement from working properly.
455 			 */
456 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
457 		} else if ((nsize & PAGE_MASK) &&
458 		    vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
459 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
460 			    nobjsize);
461 		}
462 	}
463 	object->un_pager.vnp.vnp_size = nsize;
464 	object->size = nobjsize;
465 	VM_OBJECT_WUNLOCK(object);
466 }
467 
468 /*
469  * calculate the linear (byte) disk address of specified virtual
470  * file address
471  */
472 static int
473 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
474     int *run)
475 {
476 	int bsize;
477 	int err;
478 	daddr_t vblock;
479 	daddr_t voffset;
480 
481 	if (address < 0)
482 		return -1;
483 
484 	if (vp->v_iflag & VI_DOOMED)
485 		return -1;
486 
487 	bsize = vp->v_mount->mnt_stat.f_iosize;
488 	vblock = address / bsize;
489 	voffset = address % bsize;
490 
491 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
492 	if (err == 0) {
493 		if (*rtaddress != -1)
494 			*rtaddress += voffset / DEV_BSIZE;
495 		if (run) {
496 			*run += 1;
497 			*run *= bsize/PAGE_SIZE;
498 			*run -= voffset/PAGE_SIZE;
499 		}
500 	}
501 
502 	return (err);
503 }
504 
505 /*
506  * small block filesystem vnode pager input
507  */
508 static int
509 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
510 {
511 	struct vnode *vp;
512 	struct bufobj *bo;
513 	struct buf *bp;
514 	struct sf_buf *sf;
515 	daddr_t fileaddr;
516 	vm_offset_t bsize;
517 	vm_page_bits_t bits;
518 	int error, i;
519 
520 	error = 0;
521 	vp = object->handle;
522 	if (vp->v_iflag & VI_DOOMED)
523 		return VM_PAGER_BAD;
524 
525 	bsize = vp->v_mount->mnt_stat.f_iosize;
526 
527 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
528 
529 	sf = sf_buf_alloc(m, 0);
530 
531 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
532 		vm_ooffset_t address;
533 
534 		bits = vm_page_bits(i * bsize, bsize);
535 		if (m->valid & bits)
536 			continue;
537 
538 		address = IDX_TO_OFF(m->pindex) + i * bsize;
539 		if (address >= object->un_pager.vnp.vnp_size) {
540 			fileaddr = -1;
541 		} else {
542 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
543 			if (error)
544 				break;
545 		}
546 		if (fileaddr != -1) {
547 			bp = getpbuf(&vnode_pbuf_freecnt);
548 
549 			/* build a minimal buffer header */
550 			bp->b_iocmd = BIO_READ;
551 			bp->b_iodone = bdone;
552 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
553 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
554 			bp->b_rcred = crhold(curthread->td_ucred);
555 			bp->b_wcred = crhold(curthread->td_ucred);
556 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
557 			bp->b_blkno = fileaddr;
558 			pbgetbo(bo, bp);
559 			bp->b_vp = vp;
560 			bp->b_bcount = bsize;
561 			bp->b_bufsize = bsize;
562 			bp->b_runningbufspace = bp->b_bufsize;
563 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
564 
565 			/* do the input */
566 			bp->b_iooffset = dbtob(bp->b_blkno);
567 			bstrategy(bp);
568 
569 			bwait(bp, PVM, "vnsrd");
570 
571 			if ((bp->b_ioflags & BIO_ERROR) != 0)
572 				error = EIO;
573 
574 			/*
575 			 * free the buffer header back to the swap buffer pool
576 			 */
577 			bp->b_vp = NULL;
578 			pbrelbo(bp);
579 			relpbuf(bp, &vnode_pbuf_freecnt);
580 			if (error)
581 				break;
582 		} else
583 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
584 		KASSERT((m->dirty & bits) == 0,
585 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
586 		VM_OBJECT_WLOCK(object);
587 		m->valid |= bits;
588 		VM_OBJECT_WUNLOCK(object);
589 	}
590 	sf_buf_free(sf);
591 	if (error) {
592 		return VM_PAGER_ERROR;
593 	}
594 	return VM_PAGER_OK;
595 }
596 
597 /*
598  * old style vnode pager input routine
599  */
600 static int
601 vnode_pager_input_old(vm_object_t object, vm_page_t m)
602 {
603 	struct uio auio;
604 	struct iovec aiov;
605 	int error;
606 	int size;
607 	struct sf_buf *sf;
608 	struct vnode *vp;
609 
610 	VM_OBJECT_ASSERT_WLOCKED(object);
611 	error = 0;
612 
613 	/*
614 	 * Return failure if beyond current EOF
615 	 */
616 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
617 		return VM_PAGER_BAD;
618 	} else {
619 		size = PAGE_SIZE;
620 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
621 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
622 		vp = object->handle;
623 		VM_OBJECT_WUNLOCK(object);
624 
625 		/*
626 		 * Allocate a kernel virtual address and initialize so that
627 		 * we can use VOP_READ/WRITE routines.
628 		 */
629 		sf = sf_buf_alloc(m, 0);
630 
631 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
632 		aiov.iov_len = size;
633 		auio.uio_iov = &aiov;
634 		auio.uio_iovcnt = 1;
635 		auio.uio_offset = IDX_TO_OFF(m->pindex);
636 		auio.uio_segflg = UIO_SYSSPACE;
637 		auio.uio_rw = UIO_READ;
638 		auio.uio_resid = size;
639 		auio.uio_td = curthread;
640 
641 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
642 		if (!error) {
643 			int count = size - auio.uio_resid;
644 
645 			if (count == 0)
646 				error = EINVAL;
647 			else if (count != PAGE_SIZE)
648 				bzero((caddr_t)sf_buf_kva(sf) + count,
649 				    PAGE_SIZE - count);
650 		}
651 		sf_buf_free(sf);
652 
653 		VM_OBJECT_WLOCK(object);
654 	}
655 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
656 	if (!error)
657 		m->valid = VM_PAGE_BITS_ALL;
658 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
659 }
660 
661 /*
662  * generic vnode pager input routine
663  */
664 
665 /*
666  * Local media VFS's that do not implement their own VOP_GETPAGES
667  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
668  * to implement the previous behaviour.
669  *
670  * All other FS's should use the bypass to get to the local media
671  * backing vp's VOP_GETPAGES.
672  */
673 static int
674 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
675     int *rahead)
676 {
677 	struct vnode *vp;
678 	int rtval;
679 
680 	vp = object->handle;
681 	VM_OBJECT_WUNLOCK(object);
682 	rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
683 	KASSERT(rtval != EOPNOTSUPP,
684 	    ("vnode_pager: FS getpages not implemented\n"));
685 	VM_OBJECT_WLOCK(object);
686 	return rtval;
687 }
688 
689 static int
690 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
691     int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
692 {
693 	struct vnode *vp;
694 	int rtval;
695 
696 	vp = object->handle;
697 	VM_OBJECT_WUNLOCK(object);
698 	rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
699 	KASSERT(rtval != EOPNOTSUPP,
700 	    ("vnode_pager: FS getpages_async not implemented\n"));
701 	VM_OBJECT_WLOCK(object);
702 	return (rtval);
703 }
704 
705 /*
706  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
707  * local filesystems, where partially valid pages can only occur at
708  * the end of file.
709  */
710 int
711 vnode_pager_local_getpages(struct vop_getpages_args *ap)
712 {
713 
714 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
715 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
716 }
717 
718 int
719 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
720 {
721 
722 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
723 	    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
724 }
725 
726 /*
727  * This is now called from local media FS's to operate against their
728  * own vnodes if they fail to implement VOP_GETPAGES.
729  */
730 int
731 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
732     int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
733 {
734 	vm_object_t object;
735 	struct bufobj *bo;
736 	struct buf *bp;
737 	off_t foff;
738 	int bsize, pagesperblock, *freecnt;
739 	int error, before, after, rbehind, rahead, poff, i;
740 	int bytecount, secmask;
741 
742 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
743 	    ("%s does not support devices", __func__));
744 
745 	if (vp->v_iflag & VI_DOOMED)
746 		return (VM_PAGER_BAD);
747 
748 	object = vp->v_object;
749 	foff = IDX_TO_OFF(m[0]->pindex);
750 	bsize = vp->v_mount->mnt_stat.f_iosize;
751 	pagesperblock = bsize / PAGE_SIZE;
752 
753 	KASSERT(foff < object->un_pager.vnp.vnp_size,
754 	    ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
755 	KASSERT(count <= sizeof(bp->b_pages),
756 	    ("%s: requested %d pages", __func__, count));
757 
758 	/*
759 	 * The last page has valid blocks.  Invalid part can only
760 	 * exist at the end of file, and the page is made fully valid
761 	 * by zeroing in vm_pager_get_pages().
762 	 */
763 	if (m[count - 1]->valid != 0 && --count == 0) {
764 		if (iodone != NULL)
765 			iodone(arg, m, 1, 0);
766 		return (VM_PAGER_OK);
767 	}
768 
769 	/*
770 	 * Synchronous and asynchronous paging operations use different
771 	 * free pbuf counters.  This is done to avoid asynchronous requests
772 	 * to consume all pbufs.
773 	 * Allocate the pbuf at the very beginning of the function, so that
774 	 * if we are low on certain kind of pbufs don't even proceed to BMAP,
775 	 * but sleep.
776 	 */
777 	freecnt = iodone != NULL ?
778 	    &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
779 	bp = getpbuf(freecnt);
780 
781 	/*
782 	 * Get the underlying device blocks for the file with VOP_BMAP().
783 	 * If the file system doesn't support VOP_BMAP, use old way of
784 	 * getting pages via VOP_READ.
785 	 */
786 	error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
787 	if (error == EOPNOTSUPP) {
788 		relpbuf(bp, freecnt);
789 		VM_OBJECT_WLOCK(object);
790 		for (i = 0; i < count; i++) {
791 			PCPU_INC(cnt.v_vnodein);
792 			PCPU_INC(cnt.v_vnodepgsin);
793 			error = vnode_pager_input_old(object, m[i]);
794 			if (error)
795 				break;
796 		}
797 		VM_OBJECT_WUNLOCK(object);
798 		return (error);
799 	} else if (error != 0) {
800 		relpbuf(bp, freecnt);
801 		return (VM_PAGER_ERROR);
802 	}
803 
804 	/*
805 	 * If the file system supports BMAP, but blocksize is smaller
806 	 * than a page size, then use special small filesystem code.
807 	 */
808 	if (pagesperblock == 0) {
809 		for (i = 0; i < count; i++) {
810 			PCPU_INC(cnt.v_vnodein);
811 			PCPU_INC(cnt.v_vnodepgsin);
812 			error = vnode_pager_input_smlfs(object, m[i]);
813 			if (error)
814 				break;
815 		}
816 		return (error);
817 	}
818 
819 	/*
820 	 * A sparse file can be encountered only for a single page request,
821 	 * which may not be preceeded by call to vm_pager_haspage().
822 	 */
823 	if (bp->b_blkno == -1) {
824 		KASSERT(count == 1,
825 		    ("%s: array[%d] request to a sparse file %p", __func__,
826 		    count, vp));
827 		relpbuf(bp, freecnt);
828 		pmap_zero_page(m[0]);
829 		KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
830 		    __func__, m[0]));
831 		VM_OBJECT_WLOCK(object);
832 		m[0]->valid = VM_PAGE_BITS_ALL;
833 		VM_OBJECT_WUNLOCK(object);
834 		return (VM_PAGER_OK);
835 	}
836 
837 	bp->b_blkno += (foff % bsize) / DEV_BSIZE;
838 
839 	/* Recalculate blocks available after/before to pages. */
840 	poff = (foff % bsize) / PAGE_SIZE;
841 	before *= pagesperblock;
842 	before += poff;
843 	after *= pagesperblock;
844 	after += pagesperblock - (poff + 1);
845 	if (m[0]->pindex + after >= object->size)
846 		after = object->size - 1 - m[0]->pindex;
847 	KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
848 	    __func__, count, after + 1));
849 	after -= count - 1;
850 
851 	/* Trim requested rbehind/rahead to possible values. */
852 	rbehind = a_rbehind ? *a_rbehind : 0;
853 	rahead = a_rahead ? *a_rahead : 0;
854 	rbehind = min(rbehind, before);
855 	rbehind = min(rbehind, m[0]->pindex);
856 	rahead = min(rahead, after);
857 	rahead = min(rahead, object->size - m[count - 1]->pindex);
858 	KASSERT(rbehind + rahead + count <= sizeof(bp->b_pages),
859 	    ("%s: behind %d ahead %d count %d", __func__,
860 	    rbehind, rahead, count));
861 
862 	/*
863 	 * Fill in the bp->b_pages[] array with requested and optional
864 	 * read behind or read ahead pages.  Read behind pages are looked
865 	 * up in a backward direction, down to a first cached page.  Same
866 	 * for read ahead pages, but there is no need to shift the array
867 	 * in case of encountering a cached page.
868 	 */
869 	i = bp->b_npages = 0;
870 	if (rbehind) {
871 		vm_pindex_t startpindex, tpindex;
872 		vm_page_t p;
873 
874 		VM_OBJECT_WLOCK(object);
875 		startpindex = m[0]->pindex - rbehind;
876 		if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
877 		    p->pindex >= startpindex)
878 			startpindex = p->pindex + 1;
879 
880 		/* tpindex is unsigned; beware of numeric underflow. */
881 		for (tpindex = m[0]->pindex - 1;
882 		    tpindex >= startpindex && tpindex < m[0]->pindex;
883 		    tpindex--, i++) {
884 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
885 			    VM_ALLOC_IFNOTCACHED);
886 			if (p == NULL) {
887 				/* Shift the array. */
888 				for (int j = 0; j < i; j++)
889 					bp->b_pages[j] = bp->b_pages[j +
890 					    tpindex + 1 - startpindex];
891 				break;
892 			}
893 			bp->b_pages[tpindex - startpindex] = p;
894 		}
895 
896 		bp->b_pgbefore = i;
897 		bp->b_npages += i;
898 		bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
899 	} else
900 		bp->b_pgbefore = 0;
901 
902 	/* Requested pages. */
903 	for (int j = 0; j < count; j++, i++)
904 		bp->b_pages[i] = m[j];
905 	bp->b_npages += count;
906 
907 	if (rahead) {
908 		vm_pindex_t endpindex, tpindex;
909 		vm_page_t p;
910 
911 		if (!VM_OBJECT_WOWNED(object))
912 			VM_OBJECT_WLOCK(object);
913 		endpindex = m[count - 1]->pindex + rahead + 1;
914 		if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
915 		    p->pindex < endpindex)
916 			endpindex = p->pindex;
917 		if (endpindex > object->size)
918 			endpindex = object->size;
919 
920 		for (tpindex = m[count - 1]->pindex + 1;
921 		    tpindex < endpindex; i++, tpindex++) {
922 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
923 			    VM_ALLOC_IFNOTCACHED);
924 			if (p == NULL)
925 				break;
926 			bp->b_pages[i] = p;
927 		}
928 
929 		bp->b_pgafter = i - bp->b_npages;
930 		bp->b_npages = i;
931 	} else
932 		bp->b_pgafter = 0;
933 
934 	if (VM_OBJECT_WOWNED(object))
935 		VM_OBJECT_WUNLOCK(object);
936 
937 	/* Report back actual behind/ahead read. */
938 	if (a_rbehind)
939 		*a_rbehind = bp->b_pgbefore;
940 	if (a_rahead)
941 		*a_rahead = bp->b_pgafter;
942 
943 	KASSERT(bp->b_npages <= sizeof(bp->b_pages),
944 	    ("%s: buf %p overflowed", __func__, bp));
945 
946 	/*
947 	 * Recalculate first offset and bytecount with regards to read behind.
948 	 * Truncate bytecount to vnode real size and round up physical size
949 	 * for real devices.
950 	 */
951 	foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
952 	bytecount = bp->b_npages << PAGE_SHIFT;
953 	if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
954 		bytecount = object->un_pager.vnp.vnp_size - foff;
955 	secmask = bo->bo_bsize - 1;
956 	KASSERT(secmask < PAGE_SIZE && secmask > 0,
957 	    ("%s: sector size %d too large", __func__, secmask + 1));
958 	bytecount = (bytecount + secmask) & ~secmask;
959 
960 	/*
961 	 * And map the pages to be read into the kva, if the filesystem
962 	 * requires mapped buffers.
963 	 */
964 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
965 	    unmapped_buf_allowed) {
966 		bp->b_data = unmapped_buf;
967 		bp->b_offset = 0;
968 	} else {
969 		bp->b_data = bp->b_kvabase;
970 		pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
971 	}
972 
973 	/* Build a minimal buffer header. */
974 	bp->b_iocmd = BIO_READ;
975 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
976 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
977 	bp->b_rcred = crhold(curthread->td_ucred);
978 	bp->b_wcred = crhold(curthread->td_ucred);
979 	pbgetbo(bo, bp);
980 	bp->b_vp = vp;
981 	bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
982 	bp->b_iooffset = dbtob(bp->b_blkno);
983 
984 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
985 	PCPU_INC(cnt.v_vnodein);
986 	PCPU_ADD(cnt.v_vnodepgsin, bp->b_npages);
987 
988 	if (iodone != NULL) { /* async */
989 		bp->b_pgiodone = iodone;
990 		bp->b_caller1 = arg;
991 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
992 		bp->b_flags |= B_ASYNC;
993 		BUF_KERNPROC(bp);
994 		bstrategy(bp);
995 		return (VM_PAGER_OK);
996 	} else {
997 		bp->b_iodone = bdone;
998 		bstrategy(bp);
999 		bwait(bp, PVM, "vnread");
1000 		error = vnode_pager_generic_getpages_done(bp);
1001 		for (i = 0; i < bp->b_npages; i++)
1002 			bp->b_pages[i] = NULL;
1003 		bp->b_vp = NULL;
1004 		pbrelbo(bp);
1005 		relpbuf(bp, &vnode_pbuf_freecnt);
1006 		return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1007 	}
1008 }
1009 
1010 static void
1011 vnode_pager_generic_getpages_done_async(struct buf *bp)
1012 {
1013 	int error;
1014 
1015 	error = vnode_pager_generic_getpages_done(bp);
1016 	/* Run the iodone upon the requested range. */
1017 	bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1018 	    bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
1019 	for (int i = 0; i < bp->b_npages; i++)
1020 		bp->b_pages[i] = NULL;
1021 	bp->b_vp = NULL;
1022 	pbrelbo(bp);
1023 	relpbuf(bp, &vnode_async_pbuf_freecnt);
1024 }
1025 
1026 static int
1027 vnode_pager_generic_getpages_done(struct buf *bp)
1028 {
1029 	vm_object_t object;
1030 	off_t tfoff, nextoff;
1031 	int i, error;
1032 
1033 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1034 	object = bp->b_vp->v_object;
1035 
1036 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1037 		if (!buf_mapped(bp)) {
1038 			bp->b_data = bp->b_kvabase;
1039 			pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
1040 			    bp->b_npages);
1041 		}
1042 		bzero(bp->b_data + bp->b_bcount,
1043 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
1044 	}
1045 	if (buf_mapped(bp)) {
1046 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1047 		bp->b_data = unmapped_buf;
1048 	}
1049 
1050 	VM_OBJECT_WLOCK(object);
1051 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1052 	    i < bp->b_npages; i++, tfoff = nextoff) {
1053 		vm_page_t mt;
1054 
1055 		nextoff = tfoff + PAGE_SIZE;
1056 		mt = bp->b_pages[i];
1057 
1058 		if (nextoff <= object->un_pager.vnp.vnp_size) {
1059 			/*
1060 			 * Read filled up entire page.
1061 			 */
1062 			mt->valid = VM_PAGE_BITS_ALL;
1063 			KASSERT(mt->dirty == 0,
1064 			    ("%s: page %p is dirty", __func__, mt));
1065 			KASSERT(!pmap_page_is_mapped(mt),
1066 			    ("%s: page %p is mapped", __func__, mt));
1067 		} else {
1068 			/*
1069 			 * Read did not fill up entire page.
1070 			 *
1071 			 * Currently we do not set the entire page valid,
1072 			 * we just try to clear the piece that we couldn't
1073 			 * read.
1074 			 */
1075 			vm_page_set_valid_range(mt, 0,
1076 			    object->un_pager.vnp.vnp_size - tfoff);
1077 			KASSERT((mt->dirty & vm_page_bits(0,
1078 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
1079 			    ("%s: page %p is dirty", __func__, mt));
1080 		}
1081 
1082 		if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1083 			vm_page_readahead_finish(mt);
1084 	}
1085 	VM_OBJECT_WUNLOCK(object);
1086 	if (error != 0)
1087 		printf("%s: I/O read error %d\n", __func__, error);
1088 
1089 	return (error);
1090 }
1091 
1092 /*
1093  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1094  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1095  * vnode_pager_generic_putpages() to implement the previous behaviour.
1096  *
1097  * All other FS's should use the bypass to get to the local media
1098  * backing vp's VOP_PUTPAGES.
1099  */
1100 static void
1101 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1102     int flags, int *rtvals)
1103 {
1104 	int rtval;
1105 	struct vnode *vp;
1106 	int bytes = count * PAGE_SIZE;
1107 
1108 	/*
1109 	 * Force synchronous operation if we are extremely low on memory
1110 	 * to prevent a low-memory deadlock.  VOP operations often need to
1111 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1112 	 * operation ).  The swapper handles the case by limiting the amount
1113 	 * of asynchronous I/O, but that sort of solution doesn't scale well
1114 	 * for the vnode pager without a lot of work.
1115 	 *
1116 	 * Also, the backing vnode's iodone routine may not wake the pageout
1117 	 * daemon up.  This should be probably be addressed XXX.
1118 	 */
1119 
1120 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count <
1121 	    vm_cnt.v_pageout_free_min)
1122 		flags |= VM_PAGER_PUT_SYNC;
1123 
1124 	/*
1125 	 * Call device-specific putpages function
1126 	 */
1127 	vp = object->handle;
1128 	VM_OBJECT_WUNLOCK(object);
1129 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1130 	KASSERT(rtval != EOPNOTSUPP,
1131 	    ("vnode_pager: stale FS putpages\n"));
1132 	VM_OBJECT_WLOCK(object);
1133 }
1134 
1135 
1136 /*
1137  * This is now called from local media FS's to operate against their
1138  * own vnodes if they fail to implement VOP_PUTPAGES.
1139  *
1140  * This is typically called indirectly via the pageout daemon and
1141  * clustering has already typically occured, so in general we ask the
1142  * underlying filesystem to write the data out asynchronously rather
1143  * then delayed.
1144  */
1145 int
1146 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1147     int flags, int *rtvals)
1148 {
1149 	int i;
1150 	vm_object_t object;
1151 	vm_page_t m;
1152 	int count;
1153 
1154 	int maxsize, ncount;
1155 	vm_ooffset_t poffset;
1156 	struct uio auio;
1157 	struct iovec aiov;
1158 	int error;
1159 	int ioflags;
1160 	int ppscheck = 0;
1161 	static struct timeval lastfail;
1162 	static int curfail;
1163 
1164 	object = vp->v_object;
1165 	count = bytecount / PAGE_SIZE;
1166 
1167 	for (i = 0; i < count; i++)
1168 		rtvals[i] = VM_PAGER_ERROR;
1169 
1170 	if ((int64_t)ma[0]->pindex < 0) {
1171 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1172 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1173 		rtvals[0] = VM_PAGER_BAD;
1174 		return VM_PAGER_BAD;
1175 	}
1176 
1177 	maxsize = count * PAGE_SIZE;
1178 	ncount = count;
1179 
1180 	poffset = IDX_TO_OFF(ma[0]->pindex);
1181 
1182 	/*
1183 	 * If the page-aligned write is larger then the actual file we
1184 	 * have to invalidate pages occuring beyond the file EOF.  However,
1185 	 * there is an edge case where a file may not be page-aligned where
1186 	 * the last page is partially invalid.  In this case the filesystem
1187 	 * may not properly clear the dirty bits for the entire page (which
1188 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1189 	 * With the page locked we are free to fix-up the dirty bits here.
1190 	 *
1191 	 * We do not under any circumstances truncate the valid bits, as
1192 	 * this will screw up bogus page replacement.
1193 	 */
1194 	VM_OBJECT_WLOCK(object);
1195 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1196 		if (object->un_pager.vnp.vnp_size > poffset) {
1197 			int pgoff;
1198 
1199 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1200 			ncount = btoc(maxsize);
1201 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1202 				/*
1203 				 * If the object is locked and the following
1204 				 * conditions hold, then the page's dirty
1205 				 * field cannot be concurrently changed by a
1206 				 * pmap operation.
1207 				 */
1208 				m = ma[ncount - 1];
1209 				vm_page_assert_sbusied(m);
1210 				KASSERT(!pmap_page_is_write_mapped(m),
1211 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1212 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1213 				    pgoff);
1214 			}
1215 		} else {
1216 			maxsize = 0;
1217 			ncount = 0;
1218 		}
1219 		if (ncount < count) {
1220 			for (i = ncount; i < count; i++) {
1221 				rtvals[i] = VM_PAGER_BAD;
1222 			}
1223 		}
1224 	}
1225 	VM_OBJECT_WUNLOCK(object);
1226 
1227 	/*
1228 	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
1229 	 * rather then a bdwrite() to prevent paging I/O from saturating
1230 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
1231 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
1232 	 * the system decides how to cluster.
1233 	 */
1234 	ioflags = IO_VMIO;
1235 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1236 		ioflags |= IO_SYNC;
1237 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1238 		ioflags |= IO_ASYNC;
1239 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1240 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1241 
1242 	aiov.iov_base = (caddr_t) 0;
1243 	aiov.iov_len = maxsize;
1244 	auio.uio_iov = &aiov;
1245 	auio.uio_iovcnt = 1;
1246 	auio.uio_offset = poffset;
1247 	auio.uio_segflg = UIO_NOCOPY;
1248 	auio.uio_rw = UIO_WRITE;
1249 	auio.uio_resid = maxsize;
1250 	auio.uio_td = (struct thread *) 0;
1251 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1252 	PCPU_INC(cnt.v_vnodeout);
1253 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1254 
1255 	if (error) {
1256 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1257 			printf("vnode_pager_putpages: I/O error %d\n", error);
1258 	}
1259 	if (auio.uio_resid) {
1260 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1261 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1262 			    auio.uio_resid, (u_long)ma[0]->pindex);
1263 	}
1264 	for (i = 0; i < ncount; i++) {
1265 		rtvals[i] = VM_PAGER_OK;
1266 	}
1267 	return rtvals[0];
1268 }
1269 
1270 void
1271 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1272 {
1273 	vm_object_t obj;
1274 	int i, pos;
1275 
1276 	if (written == 0)
1277 		return;
1278 	obj = ma[0]->object;
1279 	VM_OBJECT_WLOCK(obj);
1280 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1281 		if (pos < trunc_page(written)) {
1282 			rtvals[i] = VM_PAGER_OK;
1283 			vm_page_undirty(ma[i]);
1284 		} else {
1285 			/* Partially written page. */
1286 			rtvals[i] = VM_PAGER_AGAIN;
1287 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1288 		}
1289 	}
1290 	VM_OBJECT_WUNLOCK(obj);
1291 }
1292 
1293 void
1294 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1295     vm_offset_t end)
1296 {
1297 	struct vnode *vp;
1298 	vm_ooffset_t old_wm;
1299 
1300 	VM_OBJECT_WLOCK(object);
1301 	if (object->type != OBJT_VNODE) {
1302 		VM_OBJECT_WUNLOCK(object);
1303 		return;
1304 	}
1305 	old_wm = object->un_pager.vnp.writemappings;
1306 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1307 	vp = object->handle;
1308 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1309 		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1310 		VOP_ADD_WRITECOUNT(vp, 1);
1311 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1312 		    __func__, vp, vp->v_writecount);
1313 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1314 		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1315 		VOP_ADD_WRITECOUNT(vp, -1);
1316 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1317 		    __func__, vp, vp->v_writecount);
1318 	}
1319 	VM_OBJECT_WUNLOCK(object);
1320 }
1321 
1322 void
1323 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1324     vm_offset_t end)
1325 {
1326 	struct vnode *vp;
1327 	struct mount *mp;
1328 	vm_offset_t inc;
1329 
1330 	VM_OBJECT_WLOCK(object);
1331 
1332 	/*
1333 	 * First, recheck the object type to account for the race when
1334 	 * the vnode is reclaimed.
1335 	 */
1336 	if (object->type != OBJT_VNODE) {
1337 		VM_OBJECT_WUNLOCK(object);
1338 		return;
1339 	}
1340 
1341 	/*
1342 	 * Optimize for the case when writemappings is not going to
1343 	 * zero.
1344 	 */
1345 	inc = end - start;
1346 	if (object->un_pager.vnp.writemappings != inc) {
1347 		object->un_pager.vnp.writemappings -= inc;
1348 		VM_OBJECT_WUNLOCK(object);
1349 		return;
1350 	}
1351 
1352 	vp = object->handle;
1353 	vhold(vp);
1354 	VM_OBJECT_WUNLOCK(object);
1355 	mp = NULL;
1356 	vn_start_write(vp, &mp, V_WAIT);
1357 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1358 
1359 	/*
1360 	 * Decrement the object's writemappings, by swapping the start
1361 	 * and end arguments for vnode_pager_update_writecount().  If
1362 	 * there was not a race with vnode reclaimation, then the
1363 	 * vnode's v_writecount is decremented.
1364 	 */
1365 	vnode_pager_update_writecount(object, end, start);
1366 	VOP_UNLOCK(vp, 0);
1367 	vdrop(vp);
1368 	if (mp != NULL)
1369 		vn_finished_write(mp);
1370 }
1371