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