xref: /freebsd/sys/vm/vnode_pager.c (revision 763df3ec5562b7ed4d9a5af2b4897e31f6127aa8)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
326f9a767SRodney W. Grimes  * Copyright (c) 1991 The Regents of the University of California.
426f9a767SRodney W. Grimes  * All rights reserved.
526f9a767SRodney W. Grimes  * Copyright (c) 1993, 1994 John S. Dyson
624a1cce3SDavid Greenman  * Copyright (c) 1995, David Greenman
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
215929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
4026f9a767SRodney W. Grimes  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes /*
44df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
45df8bae1dSRodney W. Grimes  */
46df8bae1dSRodney W. Grimes 
4726f9a767SRodney W. Grimes /*
4826f9a767SRodney W. Grimes  * TODO:
4924a1cce3SDavid Greenman  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
50f6b04d2bSDavid Greenman  *	greatly re-simplify the vnode_pager.
5126f9a767SRodney W. Grimes  */
5226f9a767SRodney W. Grimes 
53874651b1SDavid E. O'Brien #include <sys/cdefs.h>
54874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
55874651b1SDavid E. O'Brien 
563d653db0SAlan Cox #include "opt_vm.h"
573d653db0SAlan Cox 
58df8bae1dSRodney W. Grimes #include <sys/param.h>
59df8bae1dSRodney W. Grimes #include <sys/systm.h>
60df8bae1dSRodney W. Grimes #include <sys/proc.h>
61df8bae1dSRodney W. Grimes #include <sys/vnode.h>
62df8bae1dSRodney W. Grimes #include <sys/mount.h>
639626b608SPoul-Henning Kamp #include <sys/bio.h>
6424a1cce3SDavid Greenman #include <sys/buf.h>
65efeaf95aSDavid Greenman #include <sys/vmmeter.h>
66d07a6d3fSPoul-Henning Kamp #include <sys/limits.h>
6724579ca1SMatthew Dillon #include <sys/conf.h>
6889f6b863SAttilio Rao #include <sys/rwlock.h>
699e0ddbd0SAlan Cox #include <sys/sf_buf.h>
70df8bae1dSRodney W. Grimes 
714f12e0acSSuleiman Souhlal #include <machine/atomic.h>
724f12e0acSSuleiman Souhlal 
73df8bae1dSRodney W. Grimes #include <vm/vm.h>
741c771f92SKonstantin Belousov #include <vm/vm_param.h>
75efeaf95aSDavid Greenman #include <vm/vm_object.h>
76df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7724a1cce3SDavid Greenman #include <vm/vm_pager.h>
781efb74fbSJohn Dyson #include <vm/vm_map.h>
79df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
80efeaf95aSDavid Greenman #include <vm/vm_extern.h>
81df8bae1dSRodney W. Grimes 
82bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
83bff76343SAlan Cox     daddr_t *rtaddress, int *run);
8411caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
8511caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
8611caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
87b0cd2017SGleb Smirnoff static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
88b0cd2017SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
89b0cd2017SGleb Smirnoff     int *, vop_getpages_iodone_t, void *);
9033cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
9111caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
923364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
933364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *cred);
9490effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *);
9590effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *);
960b8253a7SBruce Evans 
97df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
984e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
994e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
1004e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
10190effb23SGleb Smirnoff 	.pgo_getpages_async = vnode_pager_getpages_async,
1024e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
1034e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
104df8bae1dSRodney W. Grimes };
105df8bae1dSRodney W. Grimes 
106b62b9b64SJohn Baldwin int vnode_pbuf_freecnt;
10773e9030eSGleb Smirnoff int vnode_async_pbuf_freecnt;
1081c7c3c6aSMatthew Dillon 
109d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
110d07a6d3fSPoul-Henning Kamp int
111731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
112d07a6d3fSPoul-Henning Kamp {
113d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
114d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
115d07a6d3fSPoul-Henning Kamp 	struct vattr va;
116d07a6d3fSPoul-Henning Kamp 
117d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
118d07a6d3fSPoul-Henning Kamp 		return (0);
119d07a6d3fSPoul-Henning Kamp 
120d07a6d3fSPoul-Henning Kamp 	while ((object = vp->v_object) != NULL) {
12189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
122d07a6d3fSPoul-Henning Kamp 		if (!(object->flags & OBJ_DEAD)) {
12389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
124d07a6d3fSPoul-Henning Kamp 			return (0);
125d07a6d3fSPoul-Henning Kamp 		}
12622db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
127d07a6d3fSPoul-Henning Kamp 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
1280dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
129cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
130d07a6d3fSPoul-Henning Kamp 	}
131d07a6d3fSPoul-Henning Kamp 
132d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
133d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
134d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
135d07a6d3fSPoul-Henning Kamp 		} else {
1360359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
137d07a6d3fSPoul-Henning Kamp 				return (0);
138d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
139d07a6d3fSPoul-Henning Kamp 		}
140d07a6d3fSPoul-Henning Kamp 	}
141d07a6d3fSPoul-Henning Kamp 
1423364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
143d07a6d3fSPoul-Henning Kamp 	/*
144d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
145d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
146d07a6d3fSPoul-Henning Kamp 	 */
14789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
148d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
14989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
150d07a6d3fSPoul-Henning Kamp 	vrele(vp);
151d07a6d3fSPoul-Henning Kamp 
152d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
153d07a6d3fSPoul-Henning Kamp 
154d07a6d3fSPoul-Henning Kamp 	return (0);
155d07a6d3fSPoul-Henning Kamp }
156d07a6d3fSPoul-Henning Kamp 
1577146d6cbSPoul-Henning Kamp void
1587146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1597146d6cbSPoul-Henning Kamp {
1607146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1617146d6cbSPoul-Henning Kamp 
1627146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1637146d6cbSPoul-Henning Kamp 	if (obj == NULL)
1647146d6cbSPoul-Henning Kamp 		return;
16557fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
16689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1677146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1687146d6cbSPoul-Henning Kamp 		/*
1697146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1707146d6cbSPoul-Henning Kamp 		 */
1717146d6cbSPoul-Henning Kamp 		if ((obj->flags & OBJ_DEAD) == 0)
1727146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
1737146d6cbSPoul-Henning Kamp 		else
17489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
1757146d6cbSPoul-Henning Kamp 	} else {
1767146d6cbSPoul-Henning Kamp 		/*
1777146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
1787146d6cbSPoul-Henning Kamp 		 */
1797146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
18089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
1817146d6cbSPoul-Henning Kamp 	}
1826e4b2820SJeff Roberson 	vp->v_object = NULL;
1837146d6cbSPoul-Henning Kamp }
1847146d6cbSPoul-Henning Kamp 
1857146d6cbSPoul-Henning Kamp 
186df8bae1dSRodney W. Grimes /*
187df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
188df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
189990ab7adSAlan Cox  *
190990ab7adSAlan Cox  * MPSAFE
191df8bae1dSRodney W. Grimes  */
19224a1cce3SDavid Greenman vm_object_t
1936cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
1943364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
195df8bae1dSRodney W. Grimes {
19606cb7259SDavid Greenman 	vm_object_t object;
197df8bae1dSRodney W. Grimes 	struct vnode *vp;
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes 	/*
200df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
201df8bae1dSRodney W. Grimes 	 */
202df8bae1dSRodney W. Grimes 	if (handle == NULL)
203df8bae1dSRodney W. Grimes 		return (NULL);
204df8bae1dSRodney W. Grimes 
205df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
20639d38f93SDavid Greenman 
20739d38f93SDavid Greenman 	/*
20839d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
20939d38f93SDavid Greenman 	 * go away.
21039d38f93SDavid Greenman 	 */
2112ac78f0eSStephan Uphoff retry:
2121ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
21389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
2141ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
2151ca58953SAlan Cox 			break;
21619187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
2170dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
21824a1cce3SDavid Greenman 	}
2190d94caffSDavid Greenman 
2206ded8427SKonstantin Belousov 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
2212be70f79SJohn Dyson 
22224a1cce3SDavid Greenman 	if (object == NULL) {
223df8bae1dSRodney W. Grimes 		/*
2242ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
225df8bae1dSRodney W. Grimes 		 */
2266cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
227bbc0ec52SDavid Greenman 
2286cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
22984110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
23026f9a767SRodney W. Grimes 
23124a1cce3SDavid Greenman 		object->handle = handle;
23211be8415SStephan Uphoff 		VI_LOCK(vp);
2332ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2342ac78f0eSStephan Uphoff 			/*
2352ac78f0eSStephan Uphoff 			 * Object has been created while we were sleeping
2362ac78f0eSStephan Uphoff 			 */
23711be8415SStephan Uphoff 			VI_UNLOCK(vp);
2389cddade7SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
2399cddade7SKonstantin Belousov 			KASSERT(object->ref_count == 1,
2409cddade7SKonstantin Belousov 			    ("leaked ref %p %d", object, object->ref_count));
2419cddade7SKonstantin Belousov 			object->type = OBJT_DEAD;
2429cddade7SKonstantin Belousov 			object->ref_count = 0;
2439cddade7SKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
2442ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2452ac78f0eSStephan Uphoff 			goto retry;
246df8bae1dSRodney W. Grimes 		}
2472ac78f0eSStephan Uphoff 		vp->v_object = object;
24811be8415SStephan Uphoff 		VI_UNLOCK(vp);
24911be8415SStephan Uphoff 	} else {
2502ac78f0eSStephan Uphoff 		object->ref_count++;
2513d653db0SAlan Cox #if VM_NRESERVLEVEL > 0
2523d653db0SAlan Cox 		vm_object_color(object, 0);
2533d653db0SAlan Cox #endif
25489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
25511be8415SStephan Uphoff 	}
2567747c038SJeff Roberson 	vref(vp);
25724a1cce3SDavid Greenman 	return (object);
258df8bae1dSRodney W. Grimes }
259df8bae1dSRodney W. Grimes 
260658ad5ffSAlan Cox /*
261658ad5ffSAlan Cox  *	The object must be locked.
262658ad5ffSAlan Cox  */
263f708ef1bSPoul-Henning Kamp static void
2647ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
26524a1cce3SDavid Greenman {
266b9f180d1SKonstantin Belousov 	struct vnode *vp;
267b9f180d1SKonstantin Belousov 	int refs;
268df8bae1dSRodney W. Grimes 
269b9f180d1SKonstantin Belousov 	vp = object->handle;
27024a1cce3SDavid Greenman 	if (vp == NULL)
27124a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
27224a1cce3SDavid Greenman 
27389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
27466095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
275b9f180d1SKonstantin Belousov 	refs = object->ref_count;
27624a1cce3SDavid Greenman 
27724a1cce3SDavid Greenman 	object->handle = NULL;
27895461b45SJohn Dyson 	object->type = OBJT_DEAD;
27919187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
28019187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
28119187819SAlan Cox 		wakeup(object);
28219187819SAlan Cox 	}
28357fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
28484110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
28584110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
286140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
287b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
288b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
28984110e7eSKonstantin Belousov 	}
290aa2cabb9SDavid Greenman 	vp->v_object = NULL;
291877d24acSKonstantin Belousov 	VOP_UNSET_TEXT(vp);
29289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
293b9f180d1SKonstantin Belousov 	while (refs-- > 0)
294b9f180d1SKonstantin Belousov 		vunref(vp);
29589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
296df8bae1dSRodney W. Grimes }
29726f9a767SRodney W. Grimes 
298f708ef1bSPoul-Henning Kamp static boolean_t
2997ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
3007ebba1f8SGleb Smirnoff     int *after)
301df8bae1dSRodney W. Grimes {
30224a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
30398b0c789SPoul-Henning Kamp 	daddr_t bn;
3043af76890SPoul-Henning Kamp 	int err;
305170db9c6SJohn Dyson 	daddr_t reqblock;
3062c4488fcSJohn Dyson 	int poff;
3072c4488fcSJohn Dyson 	int bsize;
308d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
309df8bae1dSRodney W. Grimes 
31089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
31124579ca1SMatthew Dillon 	/*
31224579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
31324579ca1SMatthew Dillon 	 * have the page.
31424579ca1SMatthew Dillon 	 */
315b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
31647221757SJohn Dyson 		return FALSE;
317df8bae1dSRodney W. Grimes 	/*
318b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3190d94caffSDavid Greenman 	 * not have the page.
320df8bae1dSRodney W. Grimes 	 */
321b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3224abc71c0SDavid Greenman 		return FALSE;
323df8bae1dSRodney W. Grimes 
324eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
325170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
326d63596ceSJohn Dyson 	blocksperpage = 0;
327d63596ceSJohn Dyson 	if (pagesperblock > 0) {
328a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
329d63596ceSJohn Dyson 	} else {
330d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
331d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
332d63596ceSJohn Dyson 	}
33389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
334ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
33589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
3360d94caffSDavid Greenman 	if (err)
33724a1cce3SDavid Greenman 		return TRUE;
3386eab77f2SJohn Dyson 	if (bn == -1)
339ced399eeSJohn Dyson 		return FALSE;
340d63596ceSJohn Dyson 	if (pagesperblock > 0) {
341a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
342170db9c6SJohn Dyson 		if (before) {
343170db9c6SJohn Dyson 			*before *= pagesperblock;
344170db9c6SJohn Dyson 			*before += poff;
345170db9c6SJohn Dyson 		}
346170db9c6SJohn Dyson 		if (after) {
34784d31376SGleb Smirnoff 			/*
34884d31376SGleb Smirnoff 			 * The BMAP vop can report a partial block in the
349d2596d17SGleb Smirnoff 			 * 'after', but must not report blocks after EOF.
35084d31376SGleb Smirnoff 			 * Assert the latter, and truncate 'after' in case
35184d31376SGleb Smirnoff 			 * of the former.
35284d31376SGleb Smirnoff 			 */
353d2596d17SGleb Smirnoff 			KASSERT((reqblock + *after) * pagesperblock <
354d2596d17SGleb Smirnoff 			    roundup2(object->size, pagesperblock),
35584d31376SGleb Smirnoff 			    ("%s: reqblock %jd after %d size %ju", __func__,
35684d31376SGleb Smirnoff 			    (intmax_t )reqblock, *after,
35784d31376SGleb Smirnoff 			    (uintmax_t )object->size));
358170db9c6SJohn Dyson 			*after *= pagesperblock;
35984d31376SGleb Smirnoff 			*after += pagesperblock - (poff + 1);
36084d31376SGleb Smirnoff 			if (pindex + *after >= object->size)
36184d31376SGleb Smirnoff 				*after = object->size - 1 - pindex;
362170db9c6SJohn Dyson 		}
363d63596ceSJohn Dyson 	} else {
364d63596ceSJohn Dyson 		if (before) {
365d63596ceSJohn Dyson 			*before /= blocksperpage;
366d63596ceSJohn Dyson 		}
367d63596ceSJohn Dyson 
368d63596ceSJohn Dyson 		if (after) {
369d63596ceSJohn Dyson 			*after /= blocksperpage;
370d63596ceSJohn Dyson 		}
371d63596ceSJohn Dyson 	}
372ced399eeSJohn Dyson 	return TRUE;
373df8bae1dSRodney W. Grimes }
374df8bae1dSRodney W. Grimes 
375df8bae1dSRodney W. Grimes /*
376df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
37724a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
378df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
379df8bae1dSRodney W. Grimes  *
380df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
381df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
382df8bae1dSRodney W. Grimes  */
383df8bae1dSRodney W. Grimes void
3847ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
385df8bae1dSRodney W. Grimes {
3862a8f9ab5SAlan Cox 	vm_object_t object;
3872a8f9ab5SAlan Cox 	vm_page_t m;
388c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
389df8bae1dSRodney W. Grimes 
3902a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
391df8bae1dSRodney W. Grimes 		return;
392cb61d698SKonstantin Belousov /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
39389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
3949b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
3959b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
3969b8851faSKonstantin Belousov 		return;
3979b8851faSKonstantin Belousov 	}
3989b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
3999b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
4002a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
401df8bae1dSRodney W. Grimes 		/*
402df8bae1dSRodney W. Grimes 		 * Hasn't changed size
403df8bae1dSRodney W. Grimes 		 */
40489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
405df8bae1dSRodney W. Grimes 		return;
4062a8f9ab5SAlan Cox 	}
407c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
4082a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
409df8bae1dSRodney W. Grimes 		/*
410bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
411df8bae1dSRodney W. Grimes 		 */
4122a8f9ab5SAlan Cox 		if (nobjsize < object->size)
413c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
4146bbee8e2SAlan Cox 			    0);
415bbc0ec52SDavid Greenman 		/*
416bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4173ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4183ebeaf59SMatthew Dillon 		 *
4193ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4203ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4213ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
422bbc0ec52SDavid Greenman 		 */
4232a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4241b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4251b26eb10SAlan Cox 		    m->valid != 0) {
4262b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4272b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4282b6b0df7SMatthew Dillon 
4292b6b0df7SMatthew Dillon 			/*
4302b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4312b6b0df7SMatthew Dillon 			 * the page has been mapped.
4322b6b0df7SMatthew Dillon 			 */
433fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4342b6b0df7SMatthew Dillon 
4352b6b0df7SMatthew Dillon 			/*
4363c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4373c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
4383c33df62SAlan Cox 			 * have already been set.
4393c33df62SAlan Cox 			 */
440dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
4413c33df62SAlan Cox 
4423c33df62SAlan Cox 			/*
4433c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
4443c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
4453c33df62SAlan Cox 			 * cleared.
4463c33df62SAlan Cox 			 */
4473c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
4483c33df62SAlan Cox 
4493c33df62SAlan Cox 			/*
4503c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
4513ebeaf59SMatthew Dillon 			 *
4523ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4533ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4543ebeaf59SMatthew Dillon 			 * replacement from working properly.
4552b6b0df7SMatthew Dillon 			 */
4563c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
4570ab3c7a5SAlan Cox 		} else if ((nsize & PAGE_MASK) &&
458db9ba578SAttilio Rao 		    vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
4590ab3c7a5SAlan Cox 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
4600ab3c7a5SAlan Cox 			    nobjsize);
461bbc0ec52SDavid Greenman 		}
462bbc0ec52SDavid Greenman 	}
463a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
464c576d121SLuoqi Chen 	object->size = nobjsize;
46589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
466df8bae1dSRodney W. Grimes }
467df8bae1dSRodney W. Grimes 
46826f9a767SRodney W. Grimes /*
46926f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
47026f9a767SRodney W. Grimes  * file address
47126f9a767SRodney W. Grimes  */
472bff76343SAlan Cox static int
473bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
474bff76343SAlan Cox     int *run)
47526f9a767SRodney W. Grimes {
47626f9a767SRodney W. Grimes 	int bsize;
47726f9a767SRodney W. Grimes 	int err;
478a316d390SJohn Dyson 	daddr_t vblock;
479f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
48026f9a767SRodney W. Grimes 
4812ad036b6SAlan Cox 	if (address < 0)
4820d94caffSDavid Greenman 		return -1;
4830d94caffSDavid Greenman 
484b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
4852c4488fcSJohn Dyson 		return -1;
4862c4488fcSJohn Dyson 
48726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
48826f9a767SRodney W. Grimes 	vblock = address / bsize;
48926f9a767SRodney W. Grimes 	voffset = address % bsize;
49026f9a767SRodney W. Grimes 
491bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
492bff76343SAlan Cox 	if (err == 0) {
493bff76343SAlan Cox 		if (*rtaddress != -1)
494bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
495efc68ce1SDavid Greenman 		if (run) {
496efc68ce1SDavid Greenman 			*run += 1;
497efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
498efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
499efc68ce1SDavid Greenman 		}
500efc68ce1SDavid Greenman 	}
50126f9a767SRodney W. Grimes 
502bff76343SAlan Cox 	return (err);
50326f9a767SRodney W. Grimes }
50426f9a767SRodney W. Grimes 
50526f9a767SRodney W. Grimes /*
50626f9a767SRodney W. Grimes  * small block filesystem vnode pager input
50726f9a767SRodney W. Grimes  */
508f708ef1bSPoul-Henning Kamp static int
5097ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
51026f9a767SRodney W. Grimes {
5119c83534dSPoul-Henning Kamp 	struct vnode *vp;
5129c83534dSPoul-Henning Kamp 	struct bufobj *bo;
51326f9a767SRodney W. Grimes 	struct buf *bp;
5149e0ddbd0SAlan Cox 	struct sf_buf *sf;
515f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
51626f9a767SRodney W. Grimes 	vm_offset_t bsize;
517561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
518561cc9fcSKonstantin Belousov 	int error, i;
51926f9a767SRodney W. Grimes 
520561cc9fcSKonstantin Belousov 	error = 0;
52124a1cce3SDavid Greenman 	vp = object->handle;
522b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5232c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5242c4488fcSJohn Dyson 
52526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5260bdb7528SDavid Greenman 
5279c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
52826f9a767SRodney W. Grimes 
5299e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
53026f9a767SRodney W. Grimes 
53126f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
53233c67741SMatthew Dillon 		vm_ooffset_t address;
533bbc0ec52SDavid Greenman 
5340d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5350d53a17bSAlan Cox 		if (m->valid & bits)
53626f9a767SRodney W. Grimes 			continue;
53726f9a767SRodney W. Grimes 
53833c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
53933c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
54033c67741SMatthew Dillon 			fileaddr = -1;
54133c67741SMatthew Dillon 		} else {
542bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
543bff76343SAlan Cox 			if (error)
544bff76343SAlan Cox 				break;
54533c67741SMatthew Dillon 		}
54626f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5471c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
54826f9a767SRodney W. Grimes 
54926f9a767SRodney W. Grimes 			/* build a minimal buffer header */
55021144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5516a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
552bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
553bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
554a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
555a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5569e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
557187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5589c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
5591faacf5dSKirk McKusick 			bp->b_vp = vp;
56026f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
56126f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5622b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5635bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
56426f9a767SRodney W. Grimes 
56526f9a767SRodney W. Grimes 			/* do the input */
5662c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
567b792bebeSPoul-Henning Kamp 			bstrategy(bp);
56826f9a767SRodney W. Grimes 
5696a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5706a4b5823SPoul-Henning Kamp 
571c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
57226f9a767SRodney W. Grimes 				error = EIO;
57326f9a767SRodney W. Grimes 
57426f9a767SRodney W. Grimes 			/*
57526f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
57626f9a767SRodney W. Grimes 			 */
5771faacf5dSKirk McKusick 			bp->b_vp = NULL;
5789c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5791c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
58026f9a767SRodney W. Grimes 			if (error)
58126f9a767SRodney W. Grimes 				break;
5820d53a17bSAlan Cox 		} else
5839e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
5840d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
5850d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
58689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5870d53a17bSAlan Cox 		m->valid |= bits;
58889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
58926f9a767SRodney W. Grimes 	}
5909e0ddbd0SAlan Cox 	sf_buf_free(sf);
59126f9a767SRodney W. Grimes 	if (error) {
592a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
59326f9a767SRodney W. Grimes 	}
59426f9a767SRodney W. Grimes 	return VM_PAGER_OK;
59526f9a767SRodney W. Grimes }
59626f9a767SRodney W. Grimes 
59726f9a767SRodney W. Grimes /*
598475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
59926f9a767SRodney W. Grimes  */
600f708ef1bSPoul-Henning Kamp static int
6017ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
60226f9a767SRodney W. Grimes {
603df8bae1dSRodney W. Grimes 	struct uio auio;
604df8bae1dSRodney W. Grimes 	struct iovec aiov;
60526f9a767SRodney W. Grimes 	int error;
60626f9a767SRodney W. Grimes 	int size;
6079e0ddbd0SAlan Cox 	struct sf_buf *sf;
608342a1480SJohn Baldwin 	struct vnode *vp;
609df8bae1dSRodney W. Grimes 
61089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
61126f9a767SRodney W. Grimes 	error = 0;
612bbc0ec52SDavid Greenman 
613df8bae1dSRodney W. Grimes 	/*
61426f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
61526f9a767SRodney W. Grimes 	 */
616a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
61726f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
61826f9a767SRodney W. Grimes 	} else {
61926f9a767SRodney W. Grimes 		size = PAGE_SIZE;
620a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
621a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
62252051abcSAlan Cox 		vp = object->handle;
62389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
6240bdb7528SDavid Greenman 
62526f9a767SRodney W. Grimes 		/*
626df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
627df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
628df8bae1dSRodney W. Grimes 		 */
6299e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6300bdb7528SDavid Greenman 
6319e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
632df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
633df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
634df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
635a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
636df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
63726f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
638df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
639b40ce416SJulian Elischer 		auio.uio_td = curthread;
64026f9a767SRodney W. Grimes 
641a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
642df8bae1dSRodney W. Grimes 		if (!error) {
64354d92145SMatthew Dillon 			int count = size - auio.uio_resid;
644df8bae1dSRodney W. Grimes 
645df8bae1dSRodney W. Grimes 			if (count == 0)
646df8bae1dSRodney W. Grimes 				error = EINVAL;
64726f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6489e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6499e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
650df8bae1dSRodney W. Grimes 		}
6519e0ddbd0SAlan Cox 		sf_buf_free(sf);
6521b26eb10SAlan Cox 
65389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
654df8bae1dSRodney W. Grimes 	}
6550d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
6566e3a3f38SRobert V. Baron 	if (!error)
6576e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
658a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
65926f9a767SRodney W. Grimes }
66026f9a767SRodney W. Grimes 
66126f9a767SRodney W. Grimes /*
66226f9a767SRodney W. Grimes  * generic vnode pager input routine
66326f9a767SRodney W. Grimes  */
664170db9c6SJohn Dyson 
665ce75f2c3SMike Smith /*
66623955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
66747e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
66847e151ddSRobert Drehmel  * to implement the previous behaviour.
669ce75f2c3SMike Smith  *
670ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
671ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
672ce75f2c3SMike Smith  */
673f708ef1bSPoul-Henning Kamp static int
674b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
675b0cd2017SGleb Smirnoff     int *rahead)
67624a1cce3SDavid Greenman {
677170db9c6SJohn Dyson 	struct vnode *vp;
678b0cd2017SGleb Smirnoff 	int rtval;
67995e5e988SJohn Dyson 
680170db9c6SJohn Dyson 	vp = object->handle;
68189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
682b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
68323955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
68423955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
68589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
686170db9c6SJohn Dyson 	return rtval;
687170db9c6SJohn Dyson }
688170db9c6SJohn Dyson 
68990effb23SGleb Smirnoff static int
69090effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
691b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
69290effb23SGleb Smirnoff {
69390effb23SGleb Smirnoff 	struct vnode *vp;
69490effb23SGleb Smirnoff 	int rtval;
69590effb23SGleb Smirnoff 
69690effb23SGleb Smirnoff 	vp = object->handle;
69790effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
698b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
69990effb23SGleb Smirnoff 	KASSERT(rtval != EOPNOTSUPP,
70090effb23SGleb Smirnoff 	    ("vnode_pager: FS getpages_async not implemented\n"));
70190effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
70290effb23SGleb Smirnoff 	return (rtval);
70390effb23SGleb Smirnoff }
70490effb23SGleb Smirnoff 
705ce75f2c3SMike Smith /*
70690effb23SGleb Smirnoff  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
70790effb23SGleb Smirnoff  * local filesystems, where partially valid pages can only occur at
70890effb23SGleb Smirnoff  * the end of file.
709d15b55c5SKonstantin Belousov  */
710d15b55c5SKonstantin Belousov int
711d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
712d15b55c5SKonstantin Belousov {
71390effb23SGleb Smirnoff 
714b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
715b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
71690effb23SGleb Smirnoff }
71790effb23SGleb Smirnoff 
71890effb23SGleb Smirnoff int
71990effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
72090effb23SGleb Smirnoff {
72190effb23SGleb Smirnoff 
722b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
723b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
724d15b55c5SKonstantin Belousov }
725d15b55c5SKonstantin Belousov 
726d15b55c5SKonstantin Belousov /*
727ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
728ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
729ce75f2c3SMike Smith  */
730ce75f2c3SMike Smith int
731b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
732b0cd2017SGleb Smirnoff     int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
733170db9c6SJohn Dyson {
734ce75f2c3SMike Smith 	vm_object_t object;
7359c83534dSPoul-Henning Kamp 	struct bufobj *bo;
7360bdb7528SDavid Greenman 	struct buf *bp;
737b0cd2017SGleb Smirnoff 	off_t foff;
738b0cd2017SGleb Smirnoff 	int bsize, pagesperblock, *freecnt;
739b0cd2017SGleb Smirnoff 	int error, before, after, rbehind, rahead, poff, i;
740b0cd2017SGleb Smirnoff 	int bytecount, secmask;
741ce75f2c3SMike Smith 
7429c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
743b0cd2017SGleb Smirnoff 	    ("%s does not support devices", __func__));
744b0cd2017SGleb Smirnoff 
745b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
746eac91e32SKonstantin Belousov 		return (VM_PAGER_BAD);
7472c4488fcSJohn Dyson 
748eac91e32SKonstantin Belousov 	object = vp->v_object;
749b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(m[0]->pindex);
75026f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
751b0cd2017SGleb Smirnoff 	pagesperblock = bsize / PAGE_SIZE;
752b0cd2017SGleb Smirnoff 
753b0cd2017SGleb Smirnoff 	KASSERT(foff < object->un_pager.vnp.vnp_size,
754b0cd2017SGleb Smirnoff 	    ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
755b0cd2017SGleb Smirnoff 	KASSERT(count <= sizeof(bp->b_pages),
756b0cd2017SGleb Smirnoff 	    ("%s: requested %d pages", __func__, count));
757b0cd2017SGleb Smirnoff 
758b0cd2017SGleb Smirnoff 	/*
759b0cd2017SGleb Smirnoff 	 * The last page has valid blocks.  Invalid part can only
760b0cd2017SGleb Smirnoff 	 * exist at the end of file, and the page is made fully valid
761b0cd2017SGleb Smirnoff 	 * by zeroing in vm_pager_get_pages().
762b0cd2017SGleb Smirnoff 	 */
763b0cd2017SGleb Smirnoff 	if (m[count - 1]->valid != 0 && --count == 0) {
764b0cd2017SGleb Smirnoff 		if (iodone != NULL)
765b0cd2017SGleb Smirnoff 			iodone(arg, m, 1, 0);
766b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
767b0cd2017SGleb Smirnoff 	}
768bbc0ec52SDavid Greenman 
76941c895a8SGleb Smirnoff 	/*
77041c895a8SGleb Smirnoff 	 * Synchronous and asynchronous paging operations use different
77141c895a8SGleb Smirnoff 	 * free pbuf counters.  This is done to avoid asynchronous requests
77241c895a8SGleb Smirnoff 	 * to consume all pbufs.
77341c895a8SGleb Smirnoff 	 * Allocate the pbuf at the very beginning of the function, so that
77441c895a8SGleb Smirnoff 	 * if we are low on certain kind of pbufs don't even proceed to BMAP,
77541c895a8SGleb Smirnoff 	 * but sleep.
77641c895a8SGleb Smirnoff 	 */
77773e9030eSGleb Smirnoff 	freecnt = iodone != NULL ?
77873e9030eSGleb Smirnoff 	    &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
77973e9030eSGleb Smirnoff 	bp = getpbuf(freecnt);
78073e9030eSGleb Smirnoff 
78126f9a767SRodney W. Grimes 	/*
782e122dfc1SGleb Smirnoff 	 * Get the underlying device blocks for the file with VOP_BMAP().
783e122dfc1SGleb Smirnoff 	 * If the file system doesn't support VOP_BMAP, use old way of
784e122dfc1SGleb Smirnoff 	 * getting pages via VOP_READ.
78526f9a767SRodney W. Grimes 	 */
786b0cd2017SGleb Smirnoff 	error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
7871de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
78873e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
78989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
790b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
791b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodein);
792b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodepgsin);
793b0cd2017SGleb Smirnoff 			error = vnode_pager_input_old(object, m[i]);
794b0cd2017SGleb Smirnoff 			if (error)
795b0cd2017SGleb Smirnoff 				break;
796b0cd2017SGleb Smirnoff 		}
79789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
79852051abcSAlan Cox 		return (error);
7991de11f1aSAlan Cox 	} else if (error != 0) {
80073e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
8011de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
802b0cd2017SGleb Smirnoff 	}
803bbc0ec52SDavid Greenman 
80426f9a767SRodney W. Grimes 	/*
805b0cd2017SGleb Smirnoff 	 * If the file system supports BMAP, but blocksize is smaller
806b0cd2017SGleb Smirnoff 	 * than a page size, then use special small filesystem code.
80726f9a767SRodney W. Grimes 	 */
808b0cd2017SGleb Smirnoff 	if (pagesperblock == 0) {
809ff64a90eSKonstantin Belousov 		relpbuf(bp, freecnt);
810b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
811b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodein);
812b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodepgsin);
813b0cd2017SGleb Smirnoff 			error = vnode_pager_input_smlfs(object, m[i]);
814b0cd2017SGleb Smirnoff 			if (error)
815b0cd2017SGleb Smirnoff 				break;
816b0cd2017SGleb Smirnoff 		}
817b0cd2017SGleb Smirnoff 		return (error);
81826f9a767SRodney W. Grimes 	}
8198d17e694SJulian Elischer 
82026f9a767SRodney W. Grimes 	/*
821b0cd2017SGleb Smirnoff 	 * A sparse file can be encountered only for a single page request,
822*763df3ecSPedro F. Giffuni 	 * which may not be preceded by call to vm_pager_haspage().
823a7fecb4dSAlan Cox 	 */
824b0cd2017SGleb Smirnoff 	if (bp->b_blkno == -1) {
825b0cd2017SGleb Smirnoff 		KASSERT(count == 1,
826b0cd2017SGleb Smirnoff 		    ("%s: array[%d] request to a sparse file %p", __func__,
827b0cd2017SGleb Smirnoff 		    count, vp));
82873e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
829b0cd2017SGleb Smirnoff 		pmap_zero_page(m[0]);
830b0cd2017SGleb Smirnoff 		KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
831b0cd2017SGleb Smirnoff 		    __func__, m[0]));
832a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
833b0cd2017SGleb Smirnoff 		m[0]->valid = VM_PAGE_BITS_ALL;
83489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
835f4f83da0SAlan Cox 		return (VM_PAGER_OK);
836b0cd2017SGleb Smirnoff 	}
837b0cd2017SGleb Smirnoff 
838b0cd2017SGleb Smirnoff 	bp->b_blkno += (foff % bsize) / DEV_BSIZE;
839b0cd2017SGleb Smirnoff 
840b0cd2017SGleb Smirnoff 	/* Recalculate blocks available after/before to pages. */
841b0cd2017SGleb Smirnoff 	poff = (foff % bsize) / PAGE_SIZE;
842b0cd2017SGleb Smirnoff 	before *= pagesperblock;
843b0cd2017SGleb Smirnoff 	before += poff;
844b0cd2017SGleb Smirnoff 	after *= pagesperblock;
845b0cd2017SGleb Smirnoff 	after += pagesperblock - (poff + 1);
846b0cd2017SGleb Smirnoff 	if (m[0]->pindex + after >= object->size)
847b0cd2017SGleb Smirnoff 		after = object->size - 1 - m[0]->pindex;
848b0cd2017SGleb Smirnoff 	KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
849b0cd2017SGleb Smirnoff 	    __func__, count, after + 1));
850b0cd2017SGleb Smirnoff 	after -= count - 1;
851b0cd2017SGleb Smirnoff 
852b0cd2017SGleb Smirnoff 	/* Trim requested rbehind/rahead to possible values. */
853b0cd2017SGleb Smirnoff 	rbehind = a_rbehind ? *a_rbehind : 0;
854b0cd2017SGleb Smirnoff 	rahead = a_rahead ? *a_rahead : 0;
855b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, before);
856b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, m[0]->pindex);
857b0cd2017SGleb Smirnoff 	rahead = min(rahead, after);
858b0cd2017SGleb Smirnoff 	rahead = min(rahead, object->size - m[count - 1]->pindex);
859b0cd2017SGleb Smirnoff 	KASSERT(rbehind + rahead + count <= sizeof(bp->b_pages),
860b0cd2017SGleb Smirnoff 	    ("%s: behind %d ahead %d count %d", __func__,
861b0cd2017SGleb Smirnoff 	    rbehind, rahead, count));
862b0cd2017SGleb Smirnoff 
863b0cd2017SGleb Smirnoff 	/*
864b0cd2017SGleb Smirnoff 	 * Fill in the bp->b_pages[] array with requested and optional
865b0cd2017SGleb Smirnoff 	 * read behind or read ahead pages.  Read behind pages are looked
866b0cd2017SGleb Smirnoff 	 * up in a backward direction, down to a first cached page.  Same
867b0cd2017SGleb Smirnoff 	 * for read ahead pages, but there is no need to shift the array
868b0cd2017SGleb Smirnoff 	 * in case of encountering a cached page.
869b0cd2017SGleb Smirnoff 	 */
870b0cd2017SGleb Smirnoff 	i = bp->b_npages = 0;
871b0cd2017SGleb Smirnoff 	if (rbehind) {
872b0cd2017SGleb Smirnoff 		vm_pindex_t startpindex, tpindex;
873b0cd2017SGleb Smirnoff 		vm_page_t p;
874b0cd2017SGleb Smirnoff 
875a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
876b0cd2017SGleb Smirnoff 		startpindex = m[0]->pindex - rbehind;
877b0cd2017SGleb Smirnoff 		if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
878b0cd2017SGleb Smirnoff 		    p->pindex >= startpindex)
879b0cd2017SGleb Smirnoff 			startpindex = p->pindex + 1;
880b0cd2017SGleb Smirnoff 
881b0cd2017SGleb Smirnoff 		/* tpindex is unsigned; beware of numeric underflow. */
882b0cd2017SGleb Smirnoff 		for (tpindex = m[0]->pindex - 1;
883b0cd2017SGleb Smirnoff 		    tpindex >= startpindex && tpindex < m[0]->pindex;
884b0cd2017SGleb Smirnoff 		    tpindex--, i++) {
885b0cd2017SGleb Smirnoff 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
886b0cd2017SGleb Smirnoff 			    VM_ALLOC_IFNOTCACHED);
887b0cd2017SGleb Smirnoff 			if (p == NULL) {
888b0cd2017SGleb Smirnoff 				/* Shift the array. */
889b0cd2017SGleb Smirnoff 				for (int j = 0; j < i; j++)
890b0cd2017SGleb Smirnoff 					bp->b_pages[j] = bp->b_pages[j +
891b0cd2017SGleb Smirnoff 					    tpindex + 1 - startpindex];
892b0cd2017SGleb Smirnoff 				break;
893b0cd2017SGleb Smirnoff 			}
894b0cd2017SGleb Smirnoff 			bp->b_pages[tpindex - startpindex] = p;
895a7fecb4dSAlan Cox 		}
8960bdb7528SDavid Greenman 
897b0cd2017SGleb Smirnoff 		bp->b_pgbefore = i;
898b0cd2017SGleb Smirnoff 		bp->b_npages += i;
899b0cd2017SGleb Smirnoff 		bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
900b0cd2017SGleb Smirnoff 	} else
901b0cd2017SGleb Smirnoff 		bp->b_pgbefore = 0;
902b0cd2017SGleb Smirnoff 
903b0cd2017SGleb Smirnoff 	/* Requested pages. */
904b0cd2017SGleb Smirnoff 	for (int j = 0; j < count; j++, i++)
905b0cd2017SGleb Smirnoff 		bp->b_pages[i] = m[j];
906b0cd2017SGleb Smirnoff 	bp->b_npages += count;
907b0cd2017SGleb Smirnoff 
908b0cd2017SGleb Smirnoff 	if (rahead) {
909b0cd2017SGleb Smirnoff 		vm_pindex_t endpindex, tpindex;
910b0cd2017SGleb Smirnoff 		vm_page_t p;
911b0cd2017SGleb Smirnoff 
912b0cd2017SGleb Smirnoff 		if (!VM_OBJECT_WOWNED(object))
913eac91e32SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
914b0cd2017SGleb Smirnoff 		endpindex = m[count - 1]->pindex + rahead + 1;
915b0cd2017SGleb Smirnoff 		if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
916b0cd2017SGleb Smirnoff 		    p->pindex < endpindex)
917b0cd2017SGleb Smirnoff 			endpindex = p->pindex;
918b0cd2017SGleb Smirnoff 		if (endpindex > object->size)
919b0cd2017SGleb Smirnoff 			endpindex = object->size;
920b0cd2017SGleb Smirnoff 
921b0cd2017SGleb Smirnoff 		for (tpindex = m[count - 1]->pindex + 1;
922b0cd2017SGleb Smirnoff 		    tpindex < endpindex; i++, tpindex++) {
923b0cd2017SGleb Smirnoff 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
924b0cd2017SGleb Smirnoff 			    VM_ALLOC_IFNOTCACHED);
925b0cd2017SGleb Smirnoff 			if (p == NULL)
926b0cd2017SGleb Smirnoff 				break;
927b0cd2017SGleb Smirnoff 			bp->b_pages[i] = p;
928eac91e32SKonstantin Belousov 		}
929b0cd2017SGleb Smirnoff 
930b0cd2017SGleb Smirnoff 		bp->b_pgafter = i - bp->b_npages;
931b0cd2017SGleb Smirnoff 		bp->b_npages = i;
932b0cd2017SGleb Smirnoff 	} else
933b0cd2017SGleb Smirnoff 		bp->b_pgafter = 0;
934b0cd2017SGleb Smirnoff 
935b0cd2017SGleb Smirnoff 	if (VM_OBJECT_WOWNED(object))
936eac91e32SKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
937b0cd2017SGleb Smirnoff 
938b0cd2017SGleb Smirnoff 	/* Report back actual behind/ahead read. */
939b0cd2017SGleb Smirnoff 	if (a_rbehind)
940b0cd2017SGleb Smirnoff 		*a_rbehind = bp->b_pgbefore;
941b0cd2017SGleb Smirnoff 	if (a_rahead)
942b0cd2017SGleb Smirnoff 		*a_rahead = bp->b_pgafter;
943b0cd2017SGleb Smirnoff 
944b0cd2017SGleb Smirnoff 	KASSERT(bp->b_npages <= sizeof(bp->b_pages),
945b0cd2017SGleb Smirnoff 	    ("%s: buf %p overflowed", __func__, bp));
946eac91e32SKonstantin Belousov 
9470d94caffSDavid Greenman 	/*
948b0cd2017SGleb Smirnoff 	 * Recalculate first offset and bytecount with regards to read behind.
949b0cd2017SGleb Smirnoff 	 * Truncate bytecount to vnode real size and round up physical size
950b0cd2017SGleb Smirnoff 	 * for real devices.
95126f9a767SRodney W. Grimes 	 */
952b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
953b0cd2017SGleb Smirnoff 	bytecount = bp->b_npages << PAGE_SHIFT;
954b0cd2017SGleb Smirnoff 	if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
955b0cd2017SGleb Smirnoff 		bytecount = object->un_pager.vnp.vnp_size - foff;
956eac91e32SKonstantin Belousov 	secmask = bo->bo_bsize - 1;
9576229cc50SPoul-Henning Kamp 	KASSERT(secmask < PAGE_SIZE && secmask > 0,
958b0cd2017SGleb Smirnoff 	    ("%s: sector size %d too large", __func__, secmask + 1));
959b0cd2017SGleb Smirnoff 	bytecount = (bytecount + secmask) & ~secmask;
96026f9a767SRodney W. Grimes 
96126f9a767SRodney W. Grimes 	/*
962b0cd2017SGleb Smirnoff 	 * And map the pages to be read into the kva, if the filesystem
9636ce697dcSKonstantin Belousov 	 * requires mapped buffers.
96426f9a767SRodney W. Grimes 	 */
9652a5eef69SGleb Smirnoff 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
9666ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
9676ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
9686ce697dcSKonstantin Belousov 		bp->b_offset = 0;
969fade8dd7SJeff Roberson 	} else {
970fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
971b0cd2017SGleb Smirnoff 		pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
972fade8dd7SJeff Roberson 	}
97326f9a767SRodney W. Grimes 
974b0cd2017SGleb Smirnoff 	/* Build a minimal buffer header. */
97521144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
976bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
977bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
978a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
979a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
9809c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
9811faacf5dSKirk McKusick 	bp->b_vp = vp;
982b0cd2017SGleb Smirnoff 	bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
9832c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
98490effb23SGleb Smirnoff 
985b0cd2017SGleb Smirnoff 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
986b0cd2017SGleb Smirnoff 	PCPU_INC(cnt.v_vnodein);
987b0cd2017SGleb Smirnoff 	PCPU_ADD(cnt.v_vnodepgsin, bp->b_npages);
988b0cd2017SGleb Smirnoff 
98990effb23SGleb Smirnoff 	if (iodone != NULL) { /* async */
990b0cd2017SGleb Smirnoff 		bp->b_pgiodone = iodone;
99190effb23SGleb Smirnoff 		bp->b_caller1 = arg;
99290effb23SGleb Smirnoff 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
99390effb23SGleb Smirnoff 		bp->b_flags |= B_ASYNC;
99490effb23SGleb Smirnoff 		BUF_KERNPROC(bp);
995b792bebeSPoul-Henning Kamp 		bstrategy(bp);
996b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
99790effb23SGleb Smirnoff 	} else {
99890effb23SGleb Smirnoff 		bp->b_iodone = bdone;
99990effb23SGleb Smirnoff 		bstrategy(bp);
10006a4b5823SPoul-Henning Kamp 		bwait(bp, PVM, "vnread");
100190effb23SGleb Smirnoff 		error = vnode_pager_generic_getpages_done(bp);
10021bb5ad63SGleb Smirnoff 		for (i = 0; i < bp->b_npages; i++)
10036ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
10041faacf5dSKirk McKusick 		bp->b_vp = NULL;
10059c83534dSPoul-Henning Kamp 		pbrelbo(bp);
10061c7c3c6aSMatthew Dillon 		relpbuf(bp, &vnode_pbuf_freecnt);
100790effb23SGleb Smirnoff 		return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
100890effb23SGleb Smirnoff 	}
1009b0cd2017SGleb Smirnoff }
101090effb23SGleb Smirnoff 
101190effb23SGleb Smirnoff static void
101290effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp)
101390effb23SGleb Smirnoff {
101490effb23SGleb Smirnoff 	int error;
101590effb23SGleb Smirnoff 
101690effb23SGleb Smirnoff 	error = vnode_pager_generic_getpages_done(bp);
1017b0cd2017SGleb Smirnoff 	/* Run the iodone upon the requested range. */
1018b0cd2017SGleb Smirnoff 	bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1019b0cd2017SGleb Smirnoff 	    bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
102090effb23SGleb Smirnoff 	for (int i = 0; i < bp->b_npages; i++)
102190effb23SGleb Smirnoff 		bp->b_pages[i] = NULL;
102290effb23SGleb Smirnoff 	bp->b_vp = NULL;
102390effb23SGleb Smirnoff 	pbrelbo(bp);
102473e9030eSGleb Smirnoff 	relpbuf(bp, &vnode_async_pbuf_freecnt);
102590effb23SGleb Smirnoff }
102690effb23SGleb Smirnoff 
102790effb23SGleb Smirnoff static int
102890effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp)
102990effb23SGleb Smirnoff {
103090effb23SGleb Smirnoff 	vm_object_t object;
103190effb23SGleb Smirnoff 	off_t tfoff, nextoff;
103290effb23SGleb Smirnoff 	int i, error;
103390effb23SGleb Smirnoff 
103490effb23SGleb Smirnoff 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
103590effb23SGleb Smirnoff 	object = bp->b_vp->v_object;
103690effb23SGleb Smirnoff 
103790effb23SGleb Smirnoff 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1038fade8dd7SJeff Roberson 		if (!buf_mapped(bp)) {
1039fade8dd7SJeff Roberson 			bp->b_data = bp->b_kvabase;
1040fade8dd7SJeff Roberson 			pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
104190effb23SGleb Smirnoff 			    bp->b_npages);
104290effb23SGleb Smirnoff 		}
1043fade8dd7SJeff Roberson 		bzero(bp->b_data + bp->b_bcount,
104490effb23SGleb Smirnoff 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
104590effb23SGleb Smirnoff 	}
1046fade8dd7SJeff Roberson 	if (buf_mapped(bp)) {
1047fade8dd7SJeff Roberson 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1048fade8dd7SJeff Roberson 		bp->b_data = unmapped_buf;
104990effb23SGleb Smirnoff 	}
105026f9a767SRodney W. Grimes 
105189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
105290effb23SGleb Smirnoff 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
105390effb23SGleb Smirnoff 	    i < bp->b_npages; i++, tfoff = nextoff) {
10548f9110f6SJohn Dyson 		vm_page_t mt;
10558f9110f6SJohn Dyson 
10568f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
105790effb23SGleb Smirnoff 		mt = bp->b_pages[i];
10588f9110f6SJohn Dyson 
105954746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
10608d17e694SJulian Elischer 			/*
10618d17e694SJulian Elischer 			 * Read filled up entire page.
10628d17e694SJulian Elischer 			 */
10638f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
1064016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
106579f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
1066016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
106779f0deb9SGleb Smirnoff 			    ("%s: page %p is mapped", __func__, mt));
10688f9110f6SJohn Dyson 		} else {
10698d17e694SJulian Elischer 			/*
107042eb4108SAlan Cox 			 * Read did not fill up entire page.
10718d17e694SJulian Elischer 			 *
10728d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
10738d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
10748d17e694SJulian Elischer 			 * read.
10758d17e694SJulian Elischer 			 */
1076dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
107754746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
107842eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
107942eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
108079f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
10818f9110f6SJohn Dyson 		}
10828f9110f6SJohn Dyson 
1083b0cd2017SGleb Smirnoff 		if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1084b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
108503679e23SAlan Cox 	}
108689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
108790effb23SGleb Smirnoff 	if (error != 0)
108890effb23SGleb Smirnoff 		printf("%s: I/O read error %d\n", __func__, error);
108990effb23SGleb Smirnoff 
109090effb23SGleb Smirnoff 	return (error);
109126f9a767SRodney W. Grimes }
109226f9a767SRodney W. Grimes 
1093ce75f2c3SMike Smith /*
1094ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1095ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1096ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1097ce75f2c3SMike Smith  *
1098ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1099ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1100ce75f2c3SMike Smith  */
1101e4542174SMatthew Dillon static void
11027ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
110333cad9e9SKonstantin Belousov     int flags, int *rtvals)
1104170db9c6SJohn Dyson {
1105170db9c6SJohn Dyson 	int rtval;
1106170db9c6SJohn Dyson 	struct vnode *vp;
110786ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1108ad980522SJohn Dyson 
11090e3cdf2cSAlan Cox 	/*
11100e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
11110e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
11120e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
11130e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
11140e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
11150e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
11160e3cdf2cSAlan Cox 	 *
11170e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
11180e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
11190e3cdf2cSAlan Cox 	 */
11200e3cdf2cSAlan Cox 
112133cad9e9SKonstantin Belousov 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count <
112244f1c916SBryan Drewery 	    vm_cnt.v_pageout_free_min)
112333cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
11240e3cdf2cSAlan Cox 
11250e3cdf2cSAlan Cox 	/*
11260e3cdf2cSAlan Cox 	 * Call device-specific putpages function
11270e3cdf2cSAlan Cox 	 */
1128170db9c6SJohn Dyson 	vp = object->handle;
112989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
113033cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
113123955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
113223955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
113389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1134170db9c6SJohn Dyson }
1135170db9c6SJohn Dyson 
1136ce75f2c3SMike Smith 
113726f9a767SRodney W. Grimes /*
1138ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
11394491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
11402b6b0df7SMatthew Dillon  *
11412b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
1142*763df3ecSPedro F. Giffuni  * clustering has already typically occurred, so in general we ask the
11432b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
11442b6b0df7SMatthew Dillon  * then delayed.
114526f9a767SRodney W. Grimes  */
1146ce75f2c3SMike Smith int
1147c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1148c46b90e9SAlan Cox     int flags, int *rtvals)
114926f9a767SRodney W. Grimes {
1150f6b04d2bSDavid Greenman 	int i;
1151ce75f2c3SMike Smith 	vm_object_t object;
1152c46b90e9SAlan Cox 	vm_page_t m;
1153ce75f2c3SMike Smith 	int count;
115426f9a767SRodney W. Grimes 
1155f6b04d2bSDavid Greenman 	int maxsize, ncount;
1156a316d390SJohn Dyson 	vm_ooffset_t poffset;
1157f6b04d2bSDavid Greenman 	struct uio auio;
1158f6b04d2bSDavid Greenman 	struct iovec aiov;
1159f6b04d2bSDavid Greenman 	int error;
11608f9110f6SJohn Dyson 	int ioflags;
1161dd498befSPaul Saab 	int ppscheck = 0;
1162dd498befSPaul Saab 	static struct timeval lastfail;
1163dd498befSPaul Saab 	static int curfail;
116426f9a767SRodney W. Grimes 
1165ce75f2c3SMike Smith 	object = vp->v_object;
1166ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1167ce75f2c3SMike Smith 
116826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1169031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
117026f9a767SRodney W. Grimes 
1171c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
117223562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1173c46b90e9SAlan Cox 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1174f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1175f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
11760d94caffSDavid Greenman 	}
11770bdb7528SDavid Greenman 
1178f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1179f6b04d2bSDavid Greenman 	ncount = count;
118026f9a767SRodney W. Grimes 
1181c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
118200a6f47fSMatthew Dillon 
118300a6f47fSMatthew Dillon 	/*
118400a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
1185*763df3ecSPedro F. Giffuni 	 * have to invalidate pages occurring beyond the file EOF.  However,
118600a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
118700a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
118800a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
118900a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
119000a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
11913ebeaf59SMatthew Dillon 	 *
11923ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
11933ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
119400a6f47fSMatthew Dillon 	 */
119589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1196a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
119700a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
119800a6f47fSMatthew Dillon 			int pgoff;
119900a6f47fSMatthew Dillon 
1200a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1201aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
120200a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1203c46b90e9SAlan Cox 				/*
1204c46b90e9SAlan Cox 				 * If the object is locked and the following
1205c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1206c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1207c46b90e9SAlan Cox 				 * pmap operation.
1208c46b90e9SAlan Cox 				 */
1209c46b90e9SAlan Cox 				m = ma[ncount - 1];
1210c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
12116031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1212c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1213c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1214c46b90e9SAlan Cox 				    pgoff);
121500a6f47fSMatthew Dillon 			}
121600a6f47fSMatthew Dillon 		} else {
121700a6f47fSMatthew Dillon 			maxsize = 0;
121800a6f47fSMatthew Dillon 			ncount = 0;
121900a6f47fSMatthew Dillon 		}
1220f6b04d2bSDavid Greenman 		if (ncount < count) {
1221f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1222f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1223f6b04d2bSDavid Greenman 			}
1224f6b04d2bSDavid Greenman 		}
1225f6b04d2bSDavid Greenman 	}
122689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
122726f9a767SRodney W. Grimes 
12282b6b0df7SMatthew Dillon 	/*
12292b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
12302b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
123143b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
123243b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
123343b7990eSMatthew Dillon 	 * the system decides how to cluster.
12342b6b0df7SMatthew Dillon 	 */
12358f9110f6SJohn Dyson 	ioflags = IO_VMIO;
123643b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
123743b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
123843b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
123943b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
12408f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
124143b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1242f6b04d2bSDavid Greenman 
1243f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1244f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1245f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1246f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1247a316d390SJohn Dyson 	auio.uio_offset = poffset;
1248f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1249f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1250f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1251b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1252a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1253b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodeout);
1254b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1255f6b04d2bSDavid Greenman 
1256f6b04d2bSDavid Greenman 	if (error) {
1257dd498befSPaul Saab 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
125824a1cce3SDavid Greenman 			printf("vnode_pager_putpages: I/O error %d\n", error);
1259f6b04d2bSDavid Greenman 	}
1260f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1261dd498befSPaul Saab 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
12629f80ce04SKonstantin Belousov 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1263c46b90e9SAlan Cox 			    auio.uio_resid, (u_long)ma[0]->pindex);
126426f9a767SRodney W. Grimes 	}
1265ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
126626f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
126726f9a767SRodney W. Grimes 	}
1268f6b04d2bSDavid Greenman 	return rtvals[0];
126926f9a767SRodney W. Grimes }
1270031ec8c1SKonstantin Belousov 
1271031ec8c1SKonstantin Belousov void
1272031ec8c1SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1273031ec8c1SKonstantin Belousov {
12749d17da3bSKonstantin Belousov 	vm_object_t obj;
1275031ec8c1SKonstantin Belousov 	int i, pos;
1276031ec8c1SKonstantin Belousov 
12779d17da3bSKonstantin Belousov 	if (written == 0)
12789d17da3bSKonstantin Belousov 		return;
12799d17da3bSKonstantin Belousov 	obj = ma[0]->object;
128089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1281031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1282031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1283031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1284031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1285031ec8c1SKonstantin Belousov 		} else {
1286031ec8c1SKonstantin Belousov 			/* Partially written page. */
1287031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1288031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1289031ec8c1SKonstantin Belousov 		}
1290031ec8c1SKonstantin Belousov 	}
129189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1292031ec8c1SKonstantin Belousov }
129384110e7eSKonstantin Belousov 
129484110e7eSKonstantin Belousov void
129584110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
129684110e7eSKonstantin Belousov     vm_offset_t end)
129784110e7eSKonstantin Belousov {
129884110e7eSKonstantin Belousov 	struct vnode *vp;
129984110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
130084110e7eSKonstantin Belousov 
130189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
130284110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
130389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
130484110e7eSKonstantin Belousov 		return;
130584110e7eSKonstantin Belousov 	}
130684110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
130784110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
130884110e7eSKonstantin Belousov 	vp = object->handle;
130984110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
131084110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1311140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, 1);
1312b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1313b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
131484110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
131584110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1316140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
1317b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1318b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
131984110e7eSKonstantin Belousov 	}
132089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
132184110e7eSKonstantin Belousov }
132284110e7eSKonstantin Belousov 
132384110e7eSKonstantin Belousov void
132484110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
132584110e7eSKonstantin Belousov     vm_offset_t end)
132684110e7eSKonstantin Belousov {
132784110e7eSKonstantin Belousov 	struct vnode *vp;
132884110e7eSKonstantin Belousov 	struct mount *mp;
132984110e7eSKonstantin Belousov 	vm_offset_t inc;
133084110e7eSKonstantin Belousov 
133189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
133284110e7eSKonstantin Belousov 
133384110e7eSKonstantin Belousov 	/*
133484110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
133584110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
133684110e7eSKonstantin Belousov 	 */
133784110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
133889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
133984110e7eSKonstantin Belousov 		return;
134084110e7eSKonstantin Belousov 	}
134184110e7eSKonstantin Belousov 
134284110e7eSKonstantin Belousov 	/*
134384110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
134484110e7eSKonstantin Belousov 	 * zero.
134584110e7eSKonstantin Belousov 	 */
134684110e7eSKonstantin Belousov 	inc = end - start;
134784110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
134884110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
134989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
135084110e7eSKonstantin Belousov 		return;
135184110e7eSKonstantin Belousov 	}
135284110e7eSKonstantin Belousov 
135384110e7eSKonstantin Belousov 	vp = object->handle;
135484110e7eSKonstantin Belousov 	vhold(vp);
135589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
135684110e7eSKonstantin Belousov 	mp = NULL;
135784110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
135884110e7eSKonstantin Belousov 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
135984110e7eSKonstantin Belousov 
136084110e7eSKonstantin Belousov 	/*
136184110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
136284110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
136384110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
136484110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
136584110e7eSKonstantin Belousov 	 */
136684110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
136784110e7eSKonstantin Belousov 	VOP_UNLOCK(vp, 0);
136884110e7eSKonstantin Belousov 	vdrop(vp);
136984110e7eSKonstantin Belousov 	if (mp != NULL)
137084110e7eSKonstantin Belousov 		vn_finished_write(mp);
137184110e7eSKonstantin Belousov }
1372