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