xref: /freebsd/sys/vm/vnode_pager.c (revision 90effb23412fae2a397a34f4983e15dfc522e34b)
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 
56df8bae1dSRodney W. Grimes #include <sys/param.h>
57df8bae1dSRodney W. Grimes #include <sys/systm.h>
58df8bae1dSRodney W. Grimes #include <sys/proc.h>
59df8bae1dSRodney W. Grimes #include <sys/vnode.h>
60df8bae1dSRodney W. Grimes #include <sys/mount.h>
619626b608SPoul-Henning Kamp #include <sys/bio.h>
6224a1cce3SDavid Greenman #include <sys/buf.h>
63efeaf95aSDavid Greenman #include <sys/vmmeter.h>
64d07a6d3fSPoul-Henning Kamp #include <sys/limits.h>
6524579ca1SMatthew Dillon #include <sys/conf.h>
6689f6b863SAttilio Rao #include <sys/rwlock.h>
679e0ddbd0SAlan Cox #include <sys/sf_buf.h>
68df8bae1dSRodney W. Grimes 
694f12e0acSSuleiman Souhlal #include <machine/atomic.h>
704f12e0acSSuleiman Souhlal 
71df8bae1dSRodney W. Grimes #include <vm/vm.h>
721c771f92SKonstantin Belousov #include <vm/vm_param.h>
73efeaf95aSDavid Greenman #include <vm/vm_object.h>
74df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7524a1cce3SDavid Greenman #include <vm/vm_pager.h>
761efb74fbSJohn Dyson #include <vm/vm_map.h>
77df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
78efeaf95aSDavid Greenman #include <vm/vm_extern.h>
79df8bae1dSRodney W. Grimes 
80bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
81bff76343SAlan Cox     daddr_t *rtaddress, int *run);
8211caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
8311caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
8411caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
85*90effb23SGleb Smirnoff static int vnode_pager_local_getpages0(struct vnode *, vm_page_t *, int, int,
86*90effb23SGleb Smirnoff     vop_getpages_iodone_t, void *);
8711caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
88*90effb23SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int,
89*90effb23SGleb Smirnoff     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);
94*90effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *);
95*90effb23SGleb 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,
101*90effb23SGleb 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;
1071c7c3c6aSMatthew Dillon 
108d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
109d07a6d3fSPoul-Henning Kamp int
110731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
111d07a6d3fSPoul-Henning Kamp {
112d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
113d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
114d07a6d3fSPoul-Henning Kamp 	struct vattr va;
115d07a6d3fSPoul-Henning Kamp 
116d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
117d07a6d3fSPoul-Henning Kamp 		return (0);
118d07a6d3fSPoul-Henning Kamp 
119d07a6d3fSPoul-Henning Kamp 	while ((object = vp->v_object) != NULL) {
12089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
121d07a6d3fSPoul-Henning Kamp 		if (!(object->flags & OBJ_DEAD)) {
12289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
123d07a6d3fSPoul-Henning Kamp 			return (0);
124d07a6d3fSPoul-Henning Kamp 		}
12522db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
126d07a6d3fSPoul-Henning Kamp 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
1270dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
128cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
129d07a6d3fSPoul-Henning Kamp 	}
130d07a6d3fSPoul-Henning Kamp 
131d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
132d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
133d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
134d07a6d3fSPoul-Henning Kamp 		} else {
1350359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
136d07a6d3fSPoul-Henning Kamp 				return (0);
137d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
138d07a6d3fSPoul-Henning Kamp 		}
139d07a6d3fSPoul-Henning Kamp 	}
140d07a6d3fSPoul-Henning Kamp 
1413364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
142d07a6d3fSPoul-Henning Kamp 	/*
143d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
144d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
145d07a6d3fSPoul-Henning Kamp 	 */
14689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
147d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
14889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
149d07a6d3fSPoul-Henning Kamp 	vrele(vp);
150d07a6d3fSPoul-Henning Kamp 
151d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
152d07a6d3fSPoul-Henning Kamp 
153d07a6d3fSPoul-Henning Kamp 	return (0);
154d07a6d3fSPoul-Henning Kamp }
155d07a6d3fSPoul-Henning Kamp 
1567146d6cbSPoul-Henning Kamp void
1577146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1587146d6cbSPoul-Henning Kamp {
1597146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1607146d6cbSPoul-Henning Kamp 
1617146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1627146d6cbSPoul-Henning Kamp 	if (obj == NULL)
1637146d6cbSPoul-Henning Kamp 		return;
16457fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
16589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1667146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1677146d6cbSPoul-Henning Kamp 		/*
1687146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1697146d6cbSPoul-Henning Kamp 		 */
1707146d6cbSPoul-Henning Kamp 		if ((obj->flags & OBJ_DEAD) == 0)
1717146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
1727146d6cbSPoul-Henning Kamp 		else
17389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
1747146d6cbSPoul-Henning Kamp 	} else {
1757146d6cbSPoul-Henning Kamp 		/*
1767146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
1777146d6cbSPoul-Henning Kamp 		 */
1787146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
17989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
1807146d6cbSPoul-Henning Kamp 	}
1816e4b2820SJeff Roberson 	vp->v_object = NULL;
1827146d6cbSPoul-Henning Kamp }
1837146d6cbSPoul-Henning Kamp 
1847146d6cbSPoul-Henning Kamp 
185df8bae1dSRodney W. Grimes /*
186df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
187df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
188990ab7adSAlan Cox  *
189990ab7adSAlan Cox  * MPSAFE
190df8bae1dSRodney W. Grimes  */
19124a1cce3SDavid Greenman vm_object_t
1926cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
1933364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
194df8bae1dSRodney W. Grimes {
19506cb7259SDavid Greenman 	vm_object_t object;
196df8bae1dSRodney W. Grimes 	struct vnode *vp;
197df8bae1dSRodney W. Grimes 
198df8bae1dSRodney W. Grimes 	/*
199df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
200df8bae1dSRodney W. Grimes 	 */
201df8bae1dSRodney W. Grimes 	if (handle == NULL)
202df8bae1dSRodney W. Grimes 		return (NULL);
203df8bae1dSRodney W. Grimes 
204df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
20539d38f93SDavid Greenman 
20639d38f93SDavid Greenman 	/*
20739d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
20839d38f93SDavid Greenman 	 * go away.
20939d38f93SDavid Greenman 	 */
2102ac78f0eSStephan Uphoff retry:
2111ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
21289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
2131ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
2141ca58953SAlan Cox 			break;
21519187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
2160dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
21724a1cce3SDavid Greenman 	}
2180d94caffSDavid Greenman 
2196ded8427SKonstantin Belousov 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
2202be70f79SJohn Dyson 
22124a1cce3SDavid Greenman 	if (object == NULL) {
222df8bae1dSRodney W. Grimes 		/*
2232ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
224df8bae1dSRodney W. Grimes 		 */
2256cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
226bbc0ec52SDavid Greenman 
2276cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
22884110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
22926f9a767SRodney W. Grimes 
23024a1cce3SDavid Greenman 		object->handle = handle;
23111be8415SStephan Uphoff 		VI_LOCK(vp);
2322ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2332ac78f0eSStephan Uphoff 			/*
2342ac78f0eSStephan Uphoff 			 * Object has been created while we were sleeping
2352ac78f0eSStephan Uphoff 			 */
23611be8415SStephan Uphoff 			VI_UNLOCK(vp);
2372ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2382ac78f0eSStephan Uphoff 			goto retry;
239df8bae1dSRodney W. Grimes 		}
2402ac78f0eSStephan Uphoff 		vp->v_object = object;
24111be8415SStephan Uphoff 		VI_UNLOCK(vp);
24211be8415SStephan Uphoff 	} else {
2432ac78f0eSStephan Uphoff 		object->ref_count++;
24489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
24511be8415SStephan Uphoff 	}
2467747c038SJeff Roberson 	vref(vp);
24724a1cce3SDavid Greenman 	return (object);
248df8bae1dSRodney W. Grimes }
249df8bae1dSRodney W. Grimes 
250658ad5ffSAlan Cox /*
251658ad5ffSAlan Cox  *	The object must be locked.
252658ad5ffSAlan Cox  */
253f708ef1bSPoul-Henning Kamp static void
2547ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
25524a1cce3SDavid Greenman {
256b9f180d1SKonstantin Belousov 	struct vnode *vp;
257b9f180d1SKonstantin Belousov 	int refs;
258df8bae1dSRodney W. Grimes 
259b9f180d1SKonstantin Belousov 	vp = object->handle;
26024a1cce3SDavid Greenman 	if (vp == NULL)
26124a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
26224a1cce3SDavid Greenman 
26389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
26466095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
265b9f180d1SKonstantin Belousov 	refs = object->ref_count;
26624a1cce3SDavid Greenman 
26724a1cce3SDavid Greenman 	object->handle = NULL;
26895461b45SJohn Dyson 	object->type = OBJT_DEAD;
26919187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
27019187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
27119187819SAlan Cox 		wakeup(object);
27219187819SAlan Cox 	}
27357fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
27484110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
27584110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
276140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
277b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
278b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
27984110e7eSKonstantin Belousov 	}
280aa2cabb9SDavid Greenman 	vp->v_object = NULL;
281877d24acSKonstantin Belousov 	VOP_UNSET_TEXT(vp);
28289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
283b9f180d1SKonstantin Belousov 	while (refs-- > 0)
284b9f180d1SKonstantin Belousov 		vunref(vp);
28589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
286df8bae1dSRodney W. Grimes }
28726f9a767SRodney W. Grimes 
288f708ef1bSPoul-Henning Kamp static boolean_t
2897ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
2907ebba1f8SGleb Smirnoff     int *after)
291df8bae1dSRodney W. Grimes {
29224a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
29398b0c789SPoul-Henning Kamp 	daddr_t bn;
2943af76890SPoul-Henning Kamp 	int err;
295170db9c6SJohn Dyson 	daddr_t reqblock;
2962c4488fcSJohn Dyson 	int poff;
2972c4488fcSJohn Dyson 	int bsize;
298d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
299df8bae1dSRodney W. Grimes 
30089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
30124579ca1SMatthew Dillon 	/*
30224579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
30324579ca1SMatthew Dillon 	 * have the page.
30424579ca1SMatthew Dillon 	 */
305b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
30647221757SJohn Dyson 		return FALSE;
307df8bae1dSRodney W. Grimes 	/*
308b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3090d94caffSDavid Greenman 	 * not have the page.
310df8bae1dSRodney W. Grimes 	 */
311b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3124abc71c0SDavid Greenman 		return FALSE;
313df8bae1dSRodney W. Grimes 
314eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
315170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
316d63596ceSJohn Dyson 	blocksperpage = 0;
317d63596ceSJohn Dyson 	if (pagesperblock > 0) {
318a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
319d63596ceSJohn Dyson 	} else {
320d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
321d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
322d63596ceSJohn Dyson 	}
32389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
324ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
32589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
3260d94caffSDavid Greenman 	if (err)
32724a1cce3SDavid Greenman 		return TRUE;
3286eab77f2SJohn Dyson 	if (bn == -1)
329ced399eeSJohn Dyson 		return FALSE;
330d63596ceSJohn Dyson 	if (pagesperblock > 0) {
331a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
332170db9c6SJohn Dyson 		if (before) {
333170db9c6SJohn Dyson 			*before *= pagesperblock;
334170db9c6SJohn Dyson 			*before += poff;
335170db9c6SJohn Dyson 		}
336170db9c6SJohn Dyson 		if (after) {
337b1fc01b7SJohn Dyson 			int numafter;
338170db9c6SJohn Dyson 			*after *= pagesperblock;
339b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
34047e151ddSRobert Drehmel 			if (IDX_TO_OFF(pindex + numafter) >
34147e151ddSRobert Drehmel 			    object->un_pager.vnp.vnp_size) {
34247e151ddSRobert Drehmel 				numafter =
34347e151ddSRobert Drehmel 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
34447e151ddSRobert Drehmel 				    pindex;
345b1fc01b7SJohn Dyson 			}
346b1fc01b7SJohn Dyson 			*after += numafter;
347170db9c6SJohn Dyson 		}
348d63596ceSJohn Dyson 	} else {
349d63596ceSJohn Dyson 		if (before) {
350d63596ceSJohn Dyson 			*before /= blocksperpage;
351d63596ceSJohn Dyson 		}
352d63596ceSJohn Dyson 
353d63596ceSJohn Dyson 		if (after) {
354d63596ceSJohn Dyson 			*after /= blocksperpage;
355d63596ceSJohn Dyson 		}
356d63596ceSJohn Dyson 	}
357ced399eeSJohn Dyson 	return TRUE;
358df8bae1dSRodney W. Grimes }
359df8bae1dSRodney W. Grimes 
360df8bae1dSRodney W. Grimes /*
361df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
36224a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
363df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
364df8bae1dSRodney W. Grimes  *
365df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
366df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
367df8bae1dSRodney W. Grimes  */
368df8bae1dSRodney W. Grimes void
3697ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
370df8bae1dSRodney W. Grimes {
3712a8f9ab5SAlan Cox 	vm_object_t object;
3722a8f9ab5SAlan Cox 	vm_page_t m;
373c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
374df8bae1dSRodney W. Grimes 
3752a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
376df8bae1dSRodney W. Grimes 		return;
377cb61d698SKonstantin Belousov /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
37889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
3799b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
3809b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
3819b8851faSKonstantin Belousov 		return;
3829b8851faSKonstantin Belousov 	}
3839b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
3849b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
3852a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
386df8bae1dSRodney W. Grimes 		/*
387df8bae1dSRodney W. Grimes 		 * Hasn't changed size
388df8bae1dSRodney W. Grimes 		 */
38989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
390df8bae1dSRodney W. Grimes 		return;
3912a8f9ab5SAlan Cox 	}
392c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
3932a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
394df8bae1dSRodney W. Grimes 		/*
395bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
396df8bae1dSRodney W. Grimes 		 */
3972a8f9ab5SAlan Cox 		if (nobjsize < object->size)
398c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
3996bbee8e2SAlan Cox 			    0);
400bbc0ec52SDavid Greenman 		/*
401bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4023ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4033ebeaf59SMatthew Dillon 		 *
4043ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4053ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4063ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
407bbc0ec52SDavid Greenman 		 */
4082a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4091b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4101b26eb10SAlan Cox 		    m->valid != 0) {
4112b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4122b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4132b6b0df7SMatthew Dillon 
4142b6b0df7SMatthew Dillon 			/*
4152b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4162b6b0df7SMatthew Dillon 			 * the page has been mapped.
4172b6b0df7SMatthew Dillon 			 */
418fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4192b6b0df7SMatthew Dillon 
4202b6b0df7SMatthew Dillon 			/*
4213c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4223c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
4233c33df62SAlan Cox 			 * have already been set.
4243c33df62SAlan Cox 			 */
425dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
4263c33df62SAlan Cox 
4273c33df62SAlan Cox 			/*
4283c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
4293c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
4303c33df62SAlan Cox 			 * cleared.
4313c33df62SAlan Cox 			 */
4323c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
4333c33df62SAlan Cox 
4343c33df62SAlan Cox 			/*
4353c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
4363ebeaf59SMatthew Dillon 			 *
4373ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4383ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4393ebeaf59SMatthew Dillon 			 * replacement from working properly.
4402b6b0df7SMatthew Dillon 			 */
4413c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
4420ab3c7a5SAlan Cox 		} else if ((nsize & PAGE_MASK) &&
443db9ba578SAttilio Rao 		    vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
4440ab3c7a5SAlan Cox 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
4450ab3c7a5SAlan Cox 			    nobjsize);
446bbc0ec52SDavid Greenman 		}
447bbc0ec52SDavid Greenman 	}
448a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
449c576d121SLuoqi Chen 	object->size = nobjsize;
45089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
451df8bae1dSRodney W. Grimes }
452df8bae1dSRodney W. Grimes 
45326f9a767SRodney W. Grimes /*
45426f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
45526f9a767SRodney W. Grimes  * file address
45626f9a767SRodney W. Grimes  */
457bff76343SAlan Cox static int
458bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
459bff76343SAlan Cox     int *run)
46026f9a767SRodney W. Grimes {
46126f9a767SRodney W. Grimes 	int bsize;
46226f9a767SRodney W. Grimes 	int err;
463a316d390SJohn Dyson 	daddr_t vblock;
464f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
46526f9a767SRodney W. Grimes 
4662ad036b6SAlan Cox 	if (address < 0)
4670d94caffSDavid Greenman 		return -1;
4680d94caffSDavid Greenman 
469b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
4702c4488fcSJohn Dyson 		return -1;
4712c4488fcSJohn Dyson 
47226f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
47326f9a767SRodney W. Grimes 	vblock = address / bsize;
47426f9a767SRodney W. Grimes 	voffset = address % bsize;
47526f9a767SRodney W. Grimes 
476bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
477bff76343SAlan Cox 	if (err == 0) {
478bff76343SAlan Cox 		if (*rtaddress != -1)
479bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
480efc68ce1SDavid Greenman 		if (run) {
481efc68ce1SDavid Greenman 			*run += 1;
482efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
483efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
484efc68ce1SDavid Greenman 		}
485efc68ce1SDavid Greenman 	}
48626f9a767SRodney W. Grimes 
487bff76343SAlan Cox 	return (err);
48826f9a767SRodney W. Grimes }
48926f9a767SRodney W. Grimes 
49026f9a767SRodney W. Grimes /*
49126f9a767SRodney W. Grimes  * small block filesystem vnode pager input
49226f9a767SRodney W. Grimes  */
493f708ef1bSPoul-Henning Kamp static int
4947ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
49526f9a767SRodney W. Grimes {
4969c83534dSPoul-Henning Kamp 	struct vnode *vp;
4979c83534dSPoul-Henning Kamp 	struct bufobj *bo;
49826f9a767SRodney W. Grimes 	struct buf *bp;
4999e0ddbd0SAlan Cox 	struct sf_buf *sf;
500f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
50126f9a767SRodney W. Grimes 	vm_offset_t bsize;
502561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
503561cc9fcSKonstantin Belousov 	int error, i;
50426f9a767SRodney W. Grimes 
505561cc9fcSKonstantin Belousov 	error = 0;
50624a1cce3SDavid Greenman 	vp = object->handle;
507b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5082c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5092c4488fcSJohn Dyson 
51026f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5110bdb7528SDavid Greenman 
5129c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
51326f9a767SRodney W. Grimes 
5149e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
51526f9a767SRodney W. Grimes 
51626f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
51733c67741SMatthew Dillon 		vm_ooffset_t address;
518bbc0ec52SDavid Greenman 
5190d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5200d53a17bSAlan Cox 		if (m->valid & bits)
52126f9a767SRodney W. Grimes 			continue;
52226f9a767SRodney W. Grimes 
52333c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
52433c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
52533c67741SMatthew Dillon 			fileaddr = -1;
52633c67741SMatthew Dillon 		} else {
527bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
528bff76343SAlan Cox 			if (error)
529bff76343SAlan Cox 				break;
53033c67741SMatthew Dillon 		}
53126f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5321c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
53326f9a767SRodney W. Grimes 
53426f9a767SRodney W. Grimes 			/* build a minimal buffer header */
53521144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5366a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
537bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
538bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
539a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
540a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5419e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
542187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5439c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
5441faacf5dSKirk McKusick 			bp->b_vp = vp;
54526f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
54626f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5472b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5485bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
54926f9a767SRodney W. Grimes 
55026f9a767SRodney W. Grimes 			/* do the input */
5512c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
552b792bebeSPoul-Henning Kamp 			bstrategy(bp);
55326f9a767SRodney W. Grimes 
5546a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5556a4b5823SPoul-Henning Kamp 
556c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
55726f9a767SRodney W. Grimes 				error = EIO;
55826f9a767SRodney W. Grimes 
55926f9a767SRodney W. Grimes 			/*
56026f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
56126f9a767SRodney W. Grimes 			 */
5621faacf5dSKirk McKusick 			bp->b_vp = NULL;
5639c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5641c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
56526f9a767SRodney W. Grimes 			if (error)
56626f9a767SRodney W. Grimes 				break;
5670d53a17bSAlan Cox 		} else
5689e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
5690d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
5700d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
57189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5720d53a17bSAlan Cox 		m->valid |= bits;
57389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
57426f9a767SRodney W. Grimes 	}
5759e0ddbd0SAlan Cox 	sf_buf_free(sf);
57626f9a767SRodney W. Grimes 	if (error) {
577a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
57826f9a767SRodney W. Grimes 	}
57926f9a767SRodney W. Grimes 	return VM_PAGER_OK;
58026f9a767SRodney W. Grimes }
58126f9a767SRodney W. Grimes 
58226f9a767SRodney W. Grimes /*
583475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
58426f9a767SRodney W. Grimes  */
585f708ef1bSPoul-Henning Kamp static int
5867ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
58726f9a767SRodney W. Grimes {
588df8bae1dSRodney W. Grimes 	struct uio auio;
589df8bae1dSRodney W. Grimes 	struct iovec aiov;
59026f9a767SRodney W. Grimes 	int error;
59126f9a767SRodney W. Grimes 	int size;
5929e0ddbd0SAlan Cox 	struct sf_buf *sf;
593342a1480SJohn Baldwin 	struct vnode *vp;
594df8bae1dSRodney W. Grimes 
59589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
59626f9a767SRodney W. Grimes 	error = 0;
597bbc0ec52SDavid Greenman 
598df8bae1dSRodney W. Grimes 	/*
59926f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
60026f9a767SRodney W. Grimes 	 */
601a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
60226f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
60326f9a767SRodney W. Grimes 	} else {
60426f9a767SRodney W. Grimes 		size = PAGE_SIZE;
605a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
606a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
60752051abcSAlan Cox 		vp = object->handle;
60889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
6090bdb7528SDavid Greenman 
61026f9a767SRodney W. Grimes 		/*
611df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
612df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
613df8bae1dSRodney W. Grimes 		 */
6149e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6150bdb7528SDavid Greenman 
6169e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
617df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
618df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
619df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
620a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
621df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
62226f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
623df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
624b40ce416SJulian Elischer 		auio.uio_td = curthread;
62526f9a767SRodney W. Grimes 
626a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
627df8bae1dSRodney W. Grimes 		if (!error) {
62854d92145SMatthew Dillon 			int count = size - auio.uio_resid;
629df8bae1dSRodney W. Grimes 
630df8bae1dSRodney W. Grimes 			if (count == 0)
631df8bae1dSRodney W. Grimes 				error = EINVAL;
63226f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6339e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6349e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
635df8bae1dSRodney W. Grimes 		}
6369e0ddbd0SAlan Cox 		sf_buf_free(sf);
6371b26eb10SAlan Cox 
63889f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
639df8bae1dSRodney W. Grimes 	}
6400d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
6416e3a3f38SRobert V. Baron 	if (!error)
6426e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
643a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
64426f9a767SRodney W. Grimes }
64526f9a767SRodney W. Grimes 
64626f9a767SRodney W. Grimes /*
64726f9a767SRodney W. Grimes  * generic vnode pager input routine
64826f9a767SRodney W. Grimes  */
649170db9c6SJohn Dyson 
650ce75f2c3SMike Smith /*
65123955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
65247e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
65347e151ddSRobert Drehmel  * to implement the previous behaviour.
654ce75f2c3SMike Smith  *
655ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
656ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
657ce75f2c3SMike Smith  */
658f708ef1bSPoul-Henning Kamp static int
6597ebba1f8SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
66024a1cce3SDavid Greenman {
661170db9c6SJohn Dyson 	int rtval;
662170db9c6SJohn Dyson 	struct vnode *vp;
66386ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
66495e5e988SJohn Dyson 
665170db9c6SJohn Dyson 	vp = object->handle;
66689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
66727ad26d8SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage);
66823955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
66923955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
67089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
671170db9c6SJohn Dyson 	return rtval;
672170db9c6SJohn Dyson }
673170db9c6SJohn Dyson 
674*90effb23SGleb Smirnoff static int
675*90effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
676*90effb23SGleb Smirnoff     int reqpage, vop_getpages_iodone_t iodone, void *arg)
677*90effb23SGleb Smirnoff {
678*90effb23SGleb Smirnoff 	struct vnode *vp;
679*90effb23SGleb Smirnoff 	int rtval;
680*90effb23SGleb Smirnoff 
681*90effb23SGleb Smirnoff 	vp = object->handle;
682*90effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
683*90effb23SGleb Smirnoff 	rtval = VOP_GETPAGES_ASYNC(vp, m, count * PAGE_SIZE, reqpage, 0,
684*90effb23SGleb Smirnoff 	    iodone, arg);
685*90effb23SGleb Smirnoff 	KASSERT(rtval != EOPNOTSUPP,
686*90effb23SGleb Smirnoff 	    ("vnode_pager: FS getpages_async not implemented\n"));
687*90effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
688*90effb23SGleb Smirnoff 	return (rtval);
689*90effb23SGleb Smirnoff }
690*90effb23SGleb Smirnoff 
691ce75f2c3SMike Smith /*
692*90effb23SGleb Smirnoff  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
693*90effb23SGleb Smirnoff  * local filesystems, where partially valid pages can only occur at
694*90effb23SGleb Smirnoff  * the end of file.
695d15b55c5SKonstantin Belousov  */
696d15b55c5SKonstantin Belousov int
697d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
698d15b55c5SKonstantin Belousov {
699*90effb23SGleb Smirnoff 
700*90effb23SGleb Smirnoff 	return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count,
701*90effb23SGleb Smirnoff 	    ap->a_reqpage, NULL, NULL));
702*90effb23SGleb Smirnoff }
703*90effb23SGleb Smirnoff 
704*90effb23SGleb Smirnoff int
705*90effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
706*90effb23SGleb Smirnoff {
707*90effb23SGleb Smirnoff 
708*90effb23SGleb Smirnoff 	return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count,
709*90effb23SGleb Smirnoff 	    ap->a_reqpage, ap->a_iodone, ap->a_arg));
710*90effb23SGleb Smirnoff }
711*90effb23SGleb Smirnoff 
712*90effb23SGleb Smirnoff static int
713*90effb23SGleb Smirnoff vnode_pager_local_getpages0(struct vnode *vp, vm_page_t *m, int bytecount,
714*90effb23SGleb Smirnoff     int reqpage, vop_getpages_iodone_t iodone, void *arg)
715*90effb23SGleb Smirnoff {
716d15b55c5SKonstantin Belousov 	vm_page_t mreq;
717d15b55c5SKonstantin Belousov 
718*90effb23SGleb Smirnoff 	mreq = m[reqpage];
719d15b55c5SKonstantin Belousov 
720d15b55c5SKonstantin Belousov 	/*
721d15b55c5SKonstantin Belousov 	 * Since the caller has busied the requested page, that page's valid
722d15b55c5SKonstantin Belousov 	 * field will not be changed by other threads.
723d15b55c5SKonstantin Belousov 	 */
724d15b55c5SKonstantin Belousov 	vm_page_assert_xbusied(mreq);
725d15b55c5SKonstantin Belousov 
726d15b55c5SKonstantin Belousov 	/*
727d15b55c5SKonstantin Belousov 	 * The requested page has valid blocks.  Invalid part can only
728d15b55c5SKonstantin Belousov 	 * exist at the end of file, and the page is made fully valid
729d15b55c5SKonstantin Belousov 	 * by zeroing in vm_pager_getpages().  Free non-requested
730d15b55c5SKonstantin Belousov 	 * pages, since no i/o is done to read its content.
731d15b55c5SKonstantin Belousov 	 */
732d15b55c5SKonstantin Belousov 	if (mreq->valid != 0) {
733*90effb23SGleb Smirnoff 		vm_pager_free_nonreq(mreq->object, m, reqpage,
734*90effb23SGleb Smirnoff 		    round_page(bytecount) / PAGE_SIZE);
735*90effb23SGleb Smirnoff 		if (iodone != NULL)
736*90effb23SGleb Smirnoff 			iodone(arg, m, reqpage, 0);
737d15b55c5SKonstantin Belousov 		return (VM_PAGER_OK);
738d15b55c5SKonstantin Belousov 	}
739d15b55c5SKonstantin Belousov 
740*90effb23SGleb Smirnoff 	return (vnode_pager_generic_getpages(vp, m, bytecount, reqpage,
741*90effb23SGleb Smirnoff 	    iodone, arg));
742d15b55c5SKonstantin Belousov }
743d15b55c5SKonstantin Belousov 
744d15b55c5SKonstantin Belousov /*
745ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
746ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
747ce75f2c3SMike Smith  */
748ce75f2c3SMike Smith int
7497ebba1f8SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount,
750*90effb23SGleb Smirnoff     int reqpage, vop_getpages_iodone_t iodone, void *arg)
751170db9c6SJohn Dyson {
752ce75f2c3SMike Smith 	vm_object_t object;
753*90effb23SGleb Smirnoff 	off_t foff;
754f3aad9a6SBjoern A. Zeeb 	int i, j, size, bsize, first;
755f4f83da0SAlan Cox 	daddr_t firstaddr, reqblock;
7569c83534dSPoul-Henning Kamp 	struct bufobj *bo;
757efc68ce1SDavid Greenman 	int runpg;
758efc68ce1SDavid Greenman 	int runend;
7590bdb7528SDavid Greenman 	struct buf *bp;
760ce75f2c3SMike Smith 	int count;
7611de11f1aSAlan Cox 	int error;
76226f9a767SRodney W. Grimes 
763ce75f2c3SMike Smith 	object = vp->v_object;
764ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
765ce75f2c3SMike Smith 
7669c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
7679c83534dSPoul-Henning Kamp 	    ("vnode_pager_generic_getpages does not support devices"));
768b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
7692c4488fcSJohn Dyson 		return VM_PAGER_BAD;
7702c4488fcSJohn Dyson 
77126f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
772a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
773bbc0ec52SDavid Greenman 
77426f9a767SRodney W. Grimes 	/*
775e122dfc1SGleb Smirnoff 	 * Get the underlying device blocks for the file with VOP_BMAP().
776e122dfc1SGleb Smirnoff 	 * If the file system doesn't support VOP_BMAP, use old way of
777e122dfc1SGleb Smirnoff 	 * getting pages via VOP_READ.
77826f9a767SRodney W. Grimes 	 */
7791de11f1aSAlan Cox 	error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
7801de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
78189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
7822965a453SKip Macy 
783e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7842965a453SKip Macy 			if (i != reqpage) {
7852965a453SKip Macy 				vm_page_lock(m[i]);
786d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7872965a453SKip Macy 				vm_page_unlock(m[i]);
7882965a453SKip Macy 			}
789b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodein);
790b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodepgsin);
79152051abcSAlan Cox 		error = vnode_pager_input_old(object, m[reqpage]);
79289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
79352051abcSAlan Cox 		return (error);
7941de11f1aSAlan Cox 	} else if (error != 0) {
795396b3e34SAlan Cox 		vm_pager_free_nonreq(object, m, reqpage, count);
7961de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
797bbc0ec52SDavid Greenman 
79826f9a767SRodney W. Grimes 		/*
79926f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
80026f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
80126f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
80226f9a767SRodney W. Grimes 		 */
80326f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
804500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
805396b3e34SAlan Cox 		vm_pager_free_nonreq(object, m, reqpage, count);
806b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodein);
807b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodepgsin);
80824a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
80926f9a767SRodney W. Grimes 	}
8108d17e694SJulian Elischer 
81126f9a767SRodney W. Grimes 	/*
812a7fecb4dSAlan Cox 	 * Since the caller has busied the requested page, that page's valid
813a7fecb4dSAlan Cox 	 * field will not be changed by other threads.
814a7fecb4dSAlan Cox 	 */
815a7fecb4dSAlan Cox 	vm_page_assert_xbusied(m[reqpage]);
816a7fecb4dSAlan Cox 
817a7fecb4dSAlan Cox 	/*
8188d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
8198d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
8208d17e694SJulian Elischer 	 * media.
8210d94caffSDavid Greenman 	 */
8228b575f6cSAlan Cox 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
823a7fecb4dSAlan Cox 		vm_pager_free_nonreq(object, m, reqpage, count);
824a7fecb4dSAlan Cox 		return (VM_PAGER_OK);
825f4f83da0SAlan Cox 	} else if (reqblock == -1) {
826f4f83da0SAlan Cox 		pmap_zero_page(m[reqpage]);
82712aa4fdcSAlan Cox 		KASSERT(m[reqpage]->dirty == 0,
82812aa4fdcSAlan Cox 		    ("vnode_pager_generic_getpages: page %p is dirty", m));
829a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
830f4f83da0SAlan Cox 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
831f4f83da0SAlan Cox 		for (i = 0; i < count; i++)
8322965a453SKip Macy 			if (i != reqpage) {
8332965a453SKip Macy 				vm_page_lock(m[i]);
834f4f83da0SAlan Cox 				vm_page_free(m[i]);
8352965a453SKip Macy 				vm_page_unlock(m[i]);
8362965a453SKip Macy 			}
83789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
838f4f83da0SAlan Cox 		return (VM_PAGER_OK);
839a7fecb4dSAlan Cox 	} else if (m[reqpage]->valid != 0) {
840a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
8418d17e694SJulian Elischer 		m[reqpage]->valid = 0;
84289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
843a7fecb4dSAlan Cox 	}
8440bdb7528SDavid Greenman 
8450d94caffSDavid Greenman 	/*
84626f9a767SRodney W. Grimes 	 * here on direct device I/O
84726f9a767SRodney W. Grimes 	 */
848efc68ce1SDavid Greenman 	firstaddr = -1;
849a1287949SEivind Eklund 
85026f9a767SRodney W. Grimes 	/*
851efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
85226f9a767SRodney W. Grimes 	 */
853efc68ce1SDavid Greenman 	for (first = 0, i = 0; i < count; i = runend) {
854bff76343SAlan Cox 		if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
855bff76343SAlan Cox 		    &runpg) != 0) {
85689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
857bff76343SAlan Cox 			for (; i < count; i++)
8582965a453SKip Macy 				if (i != reqpage) {
8592965a453SKip Macy 					vm_page_lock(m[i]);
860bff76343SAlan Cox 					vm_page_free(m[i]);
8612965a453SKip Macy 					vm_page_unlock(m[i]);
8622965a453SKip Macy 				}
86389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
864bff76343SAlan Cox 			return (VM_PAGER_ERROR);
865bff76343SAlan Cox 		}
866efc68ce1SDavid Greenman 		if (firstaddr == -1) {
86789f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
86824a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
869f3aad9a6SBjoern A. Zeeb 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
870f3aad9a6SBjoern A. Zeeb 				    (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
871bf1001faSMaxime Henrion 				    (uintmax_t)foff,
872bf1001faSMaxime Henrion 				    (uintmax_t)
873fc62ef1fSBruce Evans 				    (object->un_pager.vnp.vnp_size >> 32),
874bf1001faSMaxime Henrion 				    (uintmax_t)object->un_pager.vnp.vnp_size);
875efc68ce1SDavid Greenman 			}
8762965a453SKip Macy 			vm_page_lock(m[i]);
877d8d5fa88SAlfred Perlstein 			vm_page_free(m[i]);
8782965a453SKip Macy 			vm_page_unlock(m[i]);
87989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
880efc68ce1SDavid Greenman 			runend = i + 1;
881efc68ce1SDavid Greenman 			first = runend;
882efc68ce1SDavid Greenman 			continue;
883efc68ce1SDavid Greenman 		}
884efc68ce1SDavid Greenman 		runend = i + runpg;
885efc68ce1SDavid Greenman 		if (runend <= reqpage) {
88689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
8872965a453SKip Macy 			for (j = i; j < runend; j++) {
8882965a453SKip Macy 				vm_page_lock(m[j]);
889d8d5fa88SAlfred Perlstein 				vm_page_free(m[j]);
8902965a453SKip Macy 				vm_page_unlock(m[j]);
8912965a453SKip Macy 			}
89289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
89326f9a767SRodney W. Grimes 		} else {
894efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
89589f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
8962965a453SKip Macy 				for (i = first + runpg; i < count; i++) {
8972965a453SKip Macy 					vm_page_lock(m[i]);
898d8d5fa88SAlfred Perlstein 					vm_page_free(m[i]);
8992965a453SKip Macy 					vm_page_unlock(m[i]);
9002965a453SKip Macy 				}
90189f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
902efc68ce1SDavid Greenman 				count = first + runpg;
90326f9a767SRodney W. Grimes 			}
904efc68ce1SDavid Greenman 			break;
90526f9a767SRodney W. Grimes 		}
906efc68ce1SDavid Greenman 		first = runend;
907efc68ce1SDavid Greenman 	}
90826f9a767SRodney W. Grimes 
90926f9a767SRodney W. Grimes 	/*
910bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
911bbc0ec52SDavid Greenman 	 * to be zero based...
91226f9a767SRodney W. Grimes 	 */
91326f9a767SRodney W. Grimes 	if (first != 0) {
9147e2393ffSAlan Cox 		m += first;
91526f9a767SRodney W. Grimes 		count -= first;
91626f9a767SRodney W. Grimes 		reqpage -= first;
91726f9a767SRodney W. Grimes 	}
918efc68ce1SDavid Greenman 
91926f9a767SRodney W. Grimes 	/*
92026f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
92126f9a767SRodney W. Grimes 	 */
922a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
92326f9a767SRodney W. Grimes 
92426f9a767SRodney W. Grimes 	/*
92526f9a767SRodney W. Grimes 	 * calculate the size of the transfer
92626f9a767SRodney W. Grimes 	 */
92726f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
9281a31a6c3SPoul-Henning Kamp 	KASSERT(count > 0, ("zero count"));
92924a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
93024a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
9311a31a6c3SPoul-Henning Kamp 	KASSERT(size > 0, ("zero size"));
93226f9a767SRodney W. Grimes 
93326f9a767SRodney W. Grimes 	/*
93424579ca1SMatthew Dillon 	 * round up physical size for real devices.
93526f9a767SRodney W. Grimes 	 */
9369c83534dSPoul-Henning Kamp 	if (1) {
9379c83534dSPoul-Henning Kamp 		int secmask = bo->bo_bsize - 1;
9386229cc50SPoul-Henning Kamp 		KASSERT(secmask < PAGE_SIZE && secmask > 0,
9396229cc50SPoul-Henning Kamp 		    ("vnode_pager_generic_getpages: sector size %d too large",
9406229cc50SPoul-Henning Kamp 		    secmask + 1));
94124579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
94224579ca1SMatthew Dillon 	}
94326f9a767SRodney W. Grimes 
9441c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
945*90effb23SGleb Smirnoff 	bp->b_kvaalloc = bp->b_data;
94616f62314SDavid Greenman 
94726f9a767SRodney W. Grimes 	/*
9486ce697dcSKonstantin Belousov 	 * and map the pages to be read into the kva, if the filesystem
9496ce697dcSKonstantin Belousov 	 * requires mapped buffers.
95026f9a767SRodney W. Grimes 	 */
9512a5eef69SGleb Smirnoff 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
9526ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
9536ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
9546ce697dcSKonstantin Belousov 		bp->b_kvabase = unmapped_buf;
9556ce697dcSKonstantin Belousov 		bp->b_offset = 0;
9566ce697dcSKonstantin Belousov 		bp->b_flags |= B_UNMAPPED;
9576ce697dcSKonstantin Belousov 	} else
958*90effb23SGleb Smirnoff 		pmap_qenter((vm_offset_t)bp->b_kvaalloc, m, count);
95926f9a767SRodney W. Grimes 
96026f9a767SRodney W. Grimes 	/* build a minimal buffer header */
96121144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
962bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
963bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
964a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
965a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
966187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
9679c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
9681faacf5dSKirk McKusick 	bp->b_vp = vp;
96926f9a767SRodney W. Grimes 	bp->b_bcount = size;
97026f9a767SRodney W. Grimes 	bp->b_bufsize = size;
9712b6b0df7SMatthew Dillon 	bp->b_runningbufspace = bp->b_bufsize;
972*90effb23SGleb Smirnoff 	for (i = 0; i < count; i++)
973*90effb23SGleb Smirnoff 		bp->b_pages[i] = m[i];
974*90effb23SGleb Smirnoff 	bp->b_npages = count;
975*90effb23SGleb Smirnoff 	bp->b_pager.pg_reqpage = reqpage;
9765bd65606SJohn Baldwin 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
97726f9a767SRodney W. Grimes 
978b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodein);
979b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsin, count);
980976e77fcSDavid Greenman 
98126f9a767SRodney W. Grimes 	/* do the input */
9822c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
983*90effb23SGleb Smirnoff 
984*90effb23SGleb Smirnoff 	if (iodone != NULL) { /* async */
985*90effb23SGleb Smirnoff 		bp->b_pager.pg_iodone = iodone;
986*90effb23SGleb Smirnoff 		bp->b_caller1 = arg;
987*90effb23SGleb Smirnoff 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
988*90effb23SGleb Smirnoff 		bp->b_flags |= B_ASYNC;
989*90effb23SGleb Smirnoff 		BUF_KERNPROC(bp);
990b792bebeSPoul-Henning Kamp 		bstrategy(bp);
991*90effb23SGleb Smirnoff 		/* Good bye! */
992*90effb23SGleb Smirnoff 	} else {
993*90effb23SGleb Smirnoff 		bp->b_iodone = bdone;
994*90effb23SGleb Smirnoff 		bstrategy(bp);
9956a4b5823SPoul-Henning Kamp 		bwait(bp, PVM, "vnread");
996*90effb23SGleb Smirnoff 		error = vnode_pager_generic_getpages_done(bp);
997*90effb23SGleb Smirnoff 		for (int i = 0; i < bp->b_npages; i++)
9986ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
9991faacf5dSKirk McKusick 		bp->b_vp = NULL;
10009c83534dSPoul-Henning Kamp 		pbrelbo(bp);
10011c7c3c6aSMatthew Dillon 		relpbuf(bp, &vnode_pbuf_freecnt);
1002*90effb23SGleb Smirnoff 	}
1003*90effb23SGleb Smirnoff 
1004*90effb23SGleb Smirnoff 	return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1005*90effb23SGleb Smirnoff }
1006*90effb23SGleb Smirnoff 
1007*90effb23SGleb Smirnoff static void
1008*90effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp)
1009*90effb23SGleb Smirnoff {
1010*90effb23SGleb Smirnoff 	int error;
1011*90effb23SGleb Smirnoff 
1012*90effb23SGleb Smirnoff 	error = vnode_pager_generic_getpages_done(bp);
1013*90effb23SGleb Smirnoff 	bp->b_pager.pg_iodone(bp->b_caller1, bp->b_pages,
1014*90effb23SGleb Smirnoff 	  bp->b_pager.pg_reqpage, error);
1015*90effb23SGleb Smirnoff 	for (int i = 0; i < bp->b_npages; i++)
1016*90effb23SGleb Smirnoff 		bp->b_pages[i] = NULL;
1017*90effb23SGleb Smirnoff 	bp->b_vp = NULL;
1018*90effb23SGleb Smirnoff 	pbrelbo(bp);
1019*90effb23SGleb Smirnoff 	relpbuf(bp, &vnode_pbuf_freecnt);
1020*90effb23SGleb Smirnoff }
1021*90effb23SGleb Smirnoff 
1022*90effb23SGleb Smirnoff static int
1023*90effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp)
1024*90effb23SGleb Smirnoff {
1025*90effb23SGleb Smirnoff 	vm_object_t object;
1026*90effb23SGleb Smirnoff 	off_t tfoff, nextoff;
1027*90effb23SGleb Smirnoff 	int i, error;
1028*90effb23SGleb Smirnoff 
1029*90effb23SGleb Smirnoff 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1030*90effb23SGleb Smirnoff 	object = bp->b_vp->v_object;
1031*90effb23SGleb Smirnoff 
1032*90effb23SGleb Smirnoff 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1033*90effb23SGleb Smirnoff 		if ((bp->b_flags & B_UNMAPPED) != 0) {
1034*90effb23SGleb Smirnoff 			bp->b_flags &= ~B_UNMAPPED;
1035*90effb23SGleb Smirnoff 			pmap_qenter((vm_offset_t)bp->b_kvaalloc, bp->b_pages,
1036*90effb23SGleb Smirnoff 			    bp->b_npages);
1037*90effb23SGleb Smirnoff 		}
1038*90effb23SGleb Smirnoff 		bzero(bp->b_kvaalloc + bp->b_bcount,
1039*90effb23SGleb Smirnoff 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
1040*90effb23SGleb Smirnoff 	}
1041*90effb23SGleb Smirnoff 	if ((bp->b_flags & B_UNMAPPED) == 0)
1042*90effb23SGleb Smirnoff 		pmap_qremove((vm_offset_t)bp->b_kvaalloc, bp->b_npages);
1043*90effb23SGleb Smirnoff 	if ((bp->b_vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0) {
1044*90effb23SGleb Smirnoff 		bp->b_data = bp->b_kvaalloc;
1045*90effb23SGleb Smirnoff 		bp->b_kvabase = bp->b_kvaalloc;
1046*90effb23SGleb Smirnoff 		bp->b_flags &= ~B_UNMAPPED;
1047*90effb23SGleb Smirnoff 	}
104826f9a767SRodney W. Grimes 
104989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1050*90effb23SGleb Smirnoff 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1051*90effb23SGleb Smirnoff 	    i < bp->b_npages; i++, tfoff = nextoff) {
10528f9110f6SJohn Dyson 		vm_page_t mt;
10538f9110f6SJohn Dyson 
10548f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
1055*90effb23SGleb Smirnoff 		mt = bp->b_pages[i];
10568f9110f6SJohn Dyson 
105754746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
10588d17e694SJulian Elischer 			/*
10598d17e694SJulian Elischer 			 * Read filled up entire page.
10608d17e694SJulian Elischer 			 */
10618f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
1062016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
106379f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
1064016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
106579f0deb9SGleb Smirnoff 			    ("%s: page %p is mapped", __func__, mt));
10668f9110f6SJohn Dyson 		} else {
10678d17e694SJulian Elischer 			/*
106842eb4108SAlan Cox 			 * Read did not fill up entire page.
10698d17e694SJulian Elischer 			 *
10708d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
10718d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
10728d17e694SJulian Elischer 			 * read.
10738d17e694SJulian Elischer 			 */
1074dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
107554746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
107642eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
107742eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
107879f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
10798f9110f6SJohn Dyson 		}
10808f9110f6SJohn Dyson 
1081*90effb23SGleb Smirnoff 		if (i != bp->b_pager.pg_reqpage)
1082b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
108303679e23SAlan Cox 	}
108489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1085*90effb23SGleb Smirnoff 	if (error != 0)
1086*90effb23SGleb Smirnoff 		printf("%s: I/O read error %d\n", __func__, error);
1087*90effb23SGleb Smirnoff 
1088*90effb23SGleb Smirnoff 	return (error);
108926f9a767SRodney W. Grimes }
109026f9a767SRodney W. Grimes 
1091ce75f2c3SMike Smith /*
1092ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1093ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1094ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1095ce75f2c3SMike Smith  *
1096ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1097ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1098ce75f2c3SMike Smith  */
1099e4542174SMatthew Dillon static void
11007ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
110133cad9e9SKonstantin Belousov     int flags, int *rtvals)
1102170db9c6SJohn Dyson {
1103170db9c6SJohn Dyson 	int rtval;
1104170db9c6SJohn Dyson 	struct vnode *vp;
110586ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1106ad980522SJohn Dyson 
11070e3cdf2cSAlan Cox 	/*
11080e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
11090e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
11100e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
11110e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
11120e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
11130e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
11140e3cdf2cSAlan Cox 	 *
11150e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
11160e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
11170e3cdf2cSAlan Cox 	 */
11180e3cdf2cSAlan Cox 
111933cad9e9SKonstantin Belousov 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count <
112044f1c916SBryan Drewery 	    vm_cnt.v_pageout_free_min)
112133cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
11220e3cdf2cSAlan Cox 
11230e3cdf2cSAlan Cox 	/*
11240e3cdf2cSAlan Cox 	 * Call device-specific putpages function
11250e3cdf2cSAlan Cox 	 */
1126170db9c6SJohn Dyson 	vp = object->handle;
112789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
112833cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
112923955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
113023955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
113189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1132170db9c6SJohn Dyson }
1133170db9c6SJohn Dyson 
1134ce75f2c3SMike Smith 
113526f9a767SRodney W. Grimes /*
1136ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
11374491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
11382b6b0df7SMatthew Dillon  *
11392b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
11402b6b0df7SMatthew Dillon  * clustering has already typically occured, so in general we ask the
11412b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
11422b6b0df7SMatthew Dillon  * then delayed.
114326f9a767SRodney W. Grimes  */
1144ce75f2c3SMike Smith int
1145c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1146c46b90e9SAlan Cox     int flags, int *rtvals)
114726f9a767SRodney W. Grimes {
1148f6b04d2bSDavid Greenman 	int i;
1149ce75f2c3SMike Smith 	vm_object_t object;
1150c46b90e9SAlan Cox 	vm_page_t m;
1151ce75f2c3SMike Smith 	int count;
115226f9a767SRodney W. Grimes 
1153f6b04d2bSDavid Greenman 	int maxsize, ncount;
1154a316d390SJohn Dyson 	vm_ooffset_t poffset;
1155f6b04d2bSDavid Greenman 	struct uio auio;
1156f6b04d2bSDavid Greenman 	struct iovec aiov;
1157f6b04d2bSDavid Greenman 	int error;
11588f9110f6SJohn Dyson 	int ioflags;
1159dd498befSPaul Saab 	int ppscheck = 0;
1160dd498befSPaul Saab 	static struct timeval lastfail;
1161dd498befSPaul Saab 	static int curfail;
116226f9a767SRodney W. Grimes 
1163ce75f2c3SMike Smith 	object = vp->v_object;
1164ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1165ce75f2c3SMike Smith 
116626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1167031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
116826f9a767SRodney W. Grimes 
1169c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
117023562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1171c46b90e9SAlan Cox 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1172f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1173f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
11740d94caffSDavid Greenman 	}
11750bdb7528SDavid Greenman 
1176f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1177f6b04d2bSDavid Greenman 	ncount = count;
117826f9a767SRodney W. Grimes 
1179c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
118000a6f47fSMatthew Dillon 
118100a6f47fSMatthew Dillon 	/*
118200a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
118300a6f47fSMatthew Dillon 	 * have to invalidate pages occuring beyond the file EOF.  However,
118400a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
118500a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
118600a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
118700a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
118800a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
11893ebeaf59SMatthew Dillon 	 *
11903ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
11913ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
119200a6f47fSMatthew Dillon 	 */
119389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1194a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
119500a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
119600a6f47fSMatthew Dillon 			int pgoff;
119700a6f47fSMatthew Dillon 
1198a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1199aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
120000a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1201c46b90e9SAlan Cox 				/*
1202c46b90e9SAlan Cox 				 * If the object is locked and the following
1203c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1204c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1205c46b90e9SAlan Cox 				 * pmap operation.
1206c46b90e9SAlan Cox 				 */
1207c46b90e9SAlan Cox 				m = ma[ncount - 1];
1208c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
12096031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1210c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1211c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1212c46b90e9SAlan Cox 				    pgoff);
121300a6f47fSMatthew Dillon 			}
121400a6f47fSMatthew Dillon 		} else {
121500a6f47fSMatthew Dillon 			maxsize = 0;
121600a6f47fSMatthew Dillon 			ncount = 0;
121700a6f47fSMatthew Dillon 		}
1218f6b04d2bSDavid Greenman 		if (ncount < count) {
1219f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1220f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1221f6b04d2bSDavid Greenman 			}
1222f6b04d2bSDavid Greenman 		}
1223f6b04d2bSDavid Greenman 	}
122489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
122526f9a767SRodney W. Grimes 
12262b6b0df7SMatthew Dillon 	/*
12272b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
12282b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
122943b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
123043b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
123143b7990eSMatthew Dillon 	 * the system decides how to cluster.
12322b6b0df7SMatthew Dillon 	 */
12338f9110f6SJohn Dyson 	ioflags = IO_VMIO;
123443b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
123543b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
123643b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
123743b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
12388f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
123943b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1240f6b04d2bSDavid Greenman 
1241f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1242f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1243f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1244f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1245a316d390SJohn Dyson 	auio.uio_offset = poffset;
1246f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1247f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1248f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1249b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1250a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1251b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodeout);
1252b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1253f6b04d2bSDavid Greenman 
1254f6b04d2bSDavid Greenman 	if (error) {
1255dd498befSPaul Saab 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
125624a1cce3SDavid Greenman 			printf("vnode_pager_putpages: I/O error %d\n", error);
1257f6b04d2bSDavid Greenman 	}
1258f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1259dd498befSPaul Saab 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
12609f80ce04SKonstantin Belousov 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1261c46b90e9SAlan Cox 			    auio.uio_resid, (u_long)ma[0]->pindex);
126226f9a767SRodney W. Grimes 	}
1263ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
126426f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
126526f9a767SRodney W. Grimes 	}
1266f6b04d2bSDavid Greenman 	return rtvals[0];
126726f9a767SRodney W. Grimes }
1268031ec8c1SKonstantin Belousov 
1269031ec8c1SKonstantin Belousov void
1270031ec8c1SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1271031ec8c1SKonstantin Belousov {
12729d17da3bSKonstantin Belousov 	vm_object_t obj;
1273031ec8c1SKonstantin Belousov 	int i, pos;
1274031ec8c1SKonstantin Belousov 
12759d17da3bSKonstantin Belousov 	if (written == 0)
12769d17da3bSKonstantin Belousov 		return;
12779d17da3bSKonstantin Belousov 	obj = ma[0]->object;
127889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1279031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1280031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1281031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1282031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1283031ec8c1SKonstantin Belousov 		} else {
1284031ec8c1SKonstantin Belousov 			/* Partially written page. */
1285031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1286031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1287031ec8c1SKonstantin Belousov 		}
1288031ec8c1SKonstantin Belousov 	}
128989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1290031ec8c1SKonstantin Belousov }
129184110e7eSKonstantin Belousov 
129284110e7eSKonstantin Belousov void
129384110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
129484110e7eSKonstantin Belousov     vm_offset_t end)
129584110e7eSKonstantin Belousov {
129684110e7eSKonstantin Belousov 	struct vnode *vp;
129784110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
129884110e7eSKonstantin Belousov 
129989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
130084110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
130189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
130284110e7eSKonstantin Belousov 		return;
130384110e7eSKonstantin Belousov 	}
130484110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
130584110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
130684110e7eSKonstantin Belousov 	vp = object->handle;
130784110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
130884110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1309140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, 1);
1310b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1311b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
131284110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
131384110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1314140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
1315b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1316b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
131784110e7eSKonstantin Belousov 	}
131889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
131984110e7eSKonstantin Belousov }
132084110e7eSKonstantin Belousov 
132184110e7eSKonstantin Belousov void
132284110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
132384110e7eSKonstantin Belousov     vm_offset_t end)
132484110e7eSKonstantin Belousov {
132584110e7eSKonstantin Belousov 	struct vnode *vp;
132684110e7eSKonstantin Belousov 	struct mount *mp;
132784110e7eSKonstantin Belousov 	vm_offset_t inc;
132884110e7eSKonstantin Belousov 
132989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
133084110e7eSKonstantin Belousov 
133184110e7eSKonstantin Belousov 	/*
133284110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
133384110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
133484110e7eSKonstantin Belousov 	 */
133584110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
133689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
133784110e7eSKonstantin Belousov 		return;
133884110e7eSKonstantin Belousov 	}
133984110e7eSKonstantin Belousov 
134084110e7eSKonstantin Belousov 	/*
134184110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
134284110e7eSKonstantin Belousov 	 * zero.
134384110e7eSKonstantin Belousov 	 */
134484110e7eSKonstantin Belousov 	inc = end - start;
134584110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
134684110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
134789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
134884110e7eSKonstantin Belousov 		return;
134984110e7eSKonstantin Belousov 	}
135084110e7eSKonstantin Belousov 
135184110e7eSKonstantin Belousov 	vp = object->handle;
135284110e7eSKonstantin Belousov 	vhold(vp);
135389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
135484110e7eSKonstantin Belousov 	mp = NULL;
135584110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
135684110e7eSKonstantin Belousov 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
135784110e7eSKonstantin Belousov 
135884110e7eSKonstantin Belousov 	/*
135984110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
136084110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
136184110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
136284110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
136384110e7eSKonstantin Belousov 	 */
136484110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
136584110e7eSKonstantin Belousov 	VOP_UNLOCK(vp, 0);
136684110e7eSKonstantin Belousov 	vdrop(vp);
136784110e7eSKonstantin Belousov 	if (mp != NULL)
136884110e7eSKonstantin Belousov 		vn_finished_write(mp);
136984110e7eSKonstantin Belousov }
1370