xref: /freebsd/sys/vm/vnode_pager.c (revision 90880a1b29da0f504ffba54b19d53fdea9b06090)
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);
1672a339d9eSKonstantin Belousov 	umtx_shm_object_terminated(obj);
1687146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1697146d6cbSPoul-Henning Kamp 		/*
1707146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1717146d6cbSPoul-Henning Kamp 		 */
172*90880a1bSKonstantin Belousov 		if ((obj->flags & OBJ_DEAD) == 0) {
1737146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
174*90880a1bSKonstantin Belousov 		} else {
175*90880a1bSKonstantin Belousov 			/*
176*90880a1bSKonstantin Belousov 			 * Waiters were already handled during object
177*90880a1bSKonstantin Belousov 			 * termination.  The exclusive vnode lock hopefully
178*90880a1bSKonstantin Belousov 			 * prevented new waiters from referencing the dying
179*90880a1bSKonstantin Belousov 			 * object.
180*90880a1bSKonstantin Belousov 			 */
181*90880a1bSKonstantin Belousov 			KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0,
182*90880a1bSKonstantin Belousov 			    ("OBJ_DISCONNECTWNT set obj %p flags %x",
183*90880a1bSKonstantin Belousov 			    obj, obj->flags));
184*90880a1bSKonstantin Belousov 			vp->v_object = NULL;
18589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
186*90880a1bSKonstantin Belousov 		}
1877146d6cbSPoul-Henning Kamp 	} else {
1887146d6cbSPoul-Henning Kamp 		/*
1897146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
1907146d6cbSPoul-Henning Kamp 		 */
1917146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
19289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
1937146d6cbSPoul-Henning Kamp 	}
194*90880a1bSKonstantin Belousov 	KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
1957146d6cbSPoul-Henning Kamp }
1967146d6cbSPoul-Henning Kamp 
1977146d6cbSPoul-Henning Kamp 
198df8bae1dSRodney W. Grimes /*
199df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
200df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
201990ab7adSAlan Cox  *
202990ab7adSAlan Cox  * MPSAFE
203df8bae1dSRodney W. Grimes  */
20424a1cce3SDavid Greenman vm_object_t
2056cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
2063364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
207df8bae1dSRodney W. Grimes {
20806cb7259SDavid Greenman 	vm_object_t object;
209df8bae1dSRodney W. Grimes 	struct vnode *vp;
210df8bae1dSRodney W. Grimes 
211df8bae1dSRodney W. Grimes 	/*
212df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
213df8bae1dSRodney W. Grimes 	 */
214df8bae1dSRodney W. Grimes 	if (handle == NULL)
215df8bae1dSRodney W. Grimes 		return (NULL);
216df8bae1dSRodney W. Grimes 
217df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
21839d38f93SDavid Greenman 
21939d38f93SDavid Greenman 	/*
22039d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
22139d38f93SDavid Greenman 	 * go away.
22239d38f93SDavid Greenman 	 */
2232ac78f0eSStephan Uphoff retry:
2241ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
22589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
2261ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
2271ca58953SAlan Cox 			break;
22819187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
2290dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
23024a1cce3SDavid Greenman 	}
2310d94caffSDavid Greenman 
2326ded8427SKonstantin Belousov 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
2332be70f79SJohn Dyson 
23424a1cce3SDavid Greenman 	if (object == NULL) {
235df8bae1dSRodney W. Grimes 		/*
2362ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
237df8bae1dSRodney W. Grimes 		 */
2386cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
239bbc0ec52SDavid Greenman 
2406cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
24184110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
24226f9a767SRodney W. Grimes 
24324a1cce3SDavid Greenman 		object->handle = handle;
24411be8415SStephan Uphoff 		VI_LOCK(vp);
2452ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2462ac78f0eSStephan Uphoff 			/*
2472ac78f0eSStephan Uphoff 			 * Object has been created while we were sleeping
2482ac78f0eSStephan Uphoff 			 */
24911be8415SStephan Uphoff 			VI_UNLOCK(vp);
2509cddade7SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
2519cddade7SKonstantin Belousov 			KASSERT(object->ref_count == 1,
2529cddade7SKonstantin Belousov 			    ("leaked ref %p %d", object, object->ref_count));
2539cddade7SKonstantin Belousov 			object->type = OBJT_DEAD;
2549cddade7SKonstantin Belousov 			object->ref_count = 0;
2559cddade7SKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
2562ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2572ac78f0eSStephan Uphoff 			goto retry;
258df8bae1dSRodney W. Grimes 		}
2592ac78f0eSStephan Uphoff 		vp->v_object = object;
26011be8415SStephan Uphoff 		VI_UNLOCK(vp);
26111be8415SStephan Uphoff 	} else {
2622ac78f0eSStephan Uphoff 		object->ref_count++;
2633d653db0SAlan Cox #if VM_NRESERVLEVEL > 0
2643d653db0SAlan Cox 		vm_object_color(object, 0);
2653d653db0SAlan Cox #endif
26689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
26711be8415SStephan Uphoff 	}
2687747c038SJeff Roberson 	vref(vp);
26924a1cce3SDavid Greenman 	return (object);
270df8bae1dSRodney W. Grimes }
271df8bae1dSRodney W. Grimes 
272658ad5ffSAlan Cox /*
273658ad5ffSAlan Cox  *	The object must be locked.
274658ad5ffSAlan Cox  */
275f708ef1bSPoul-Henning Kamp static void
2767ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
27724a1cce3SDavid Greenman {
278b9f180d1SKonstantin Belousov 	struct vnode *vp;
279b9f180d1SKonstantin Belousov 	int refs;
280df8bae1dSRodney W. Grimes 
281b9f180d1SKonstantin Belousov 	vp = object->handle;
28224a1cce3SDavid Greenman 	if (vp == NULL)
28324a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
28424a1cce3SDavid Greenman 
28589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
28666095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
287b9f180d1SKonstantin Belousov 	refs = object->ref_count;
28824a1cce3SDavid Greenman 
28924a1cce3SDavid Greenman 	object->handle = NULL;
29095461b45SJohn Dyson 	object->type = OBJT_DEAD;
29119187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
29219187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
29319187819SAlan Cox 		wakeup(object);
29419187819SAlan Cox 	}
29557fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
29684110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
29784110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
298140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
299b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
300b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
30184110e7eSKonstantin Belousov 	}
302aa2cabb9SDavid Greenman 	vp->v_object = NULL;
303877d24acSKonstantin Belousov 	VOP_UNSET_TEXT(vp);
30489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
305b9f180d1SKonstantin Belousov 	while (refs-- > 0)
306b9f180d1SKonstantin Belousov 		vunref(vp);
30789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
308df8bae1dSRodney W. Grimes }
30926f9a767SRodney W. Grimes 
310f708ef1bSPoul-Henning Kamp static boolean_t
3117ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
3127ebba1f8SGleb Smirnoff     int *after)
313df8bae1dSRodney W. Grimes {
31424a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
31598b0c789SPoul-Henning Kamp 	daddr_t bn;
3163af76890SPoul-Henning Kamp 	int err;
317170db9c6SJohn Dyson 	daddr_t reqblock;
3182c4488fcSJohn Dyson 	int poff;
3192c4488fcSJohn Dyson 	int bsize;
320d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
321df8bae1dSRodney W. Grimes 
32289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
32324579ca1SMatthew Dillon 	/*
32424579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
32524579ca1SMatthew Dillon 	 * have the page.
32624579ca1SMatthew Dillon 	 */
327b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
32847221757SJohn Dyson 		return FALSE;
329df8bae1dSRodney W. Grimes 	/*
330b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3310d94caffSDavid Greenman 	 * not have the page.
332df8bae1dSRodney W. Grimes 	 */
333b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3344abc71c0SDavid Greenman 		return FALSE;
335df8bae1dSRodney W. Grimes 
336eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
337170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
338d63596ceSJohn Dyson 	blocksperpage = 0;
339d63596ceSJohn Dyson 	if (pagesperblock > 0) {
340a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
341d63596ceSJohn Dyson 	} else {
342d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
343d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
344d63596ceSJohn Dyson 	}
34589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
346ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
34789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
3480d94caffSDavid Greenman 	if (err)
34924a1cce3SDavid Greenman 		return TRUE;
3506eab77f2SJohn Dyson 	if (bn == -1)
351ced399eeSJohn Dyson 		return FALSE;
352d63596ceSJohn Dyson 	if (pagesperblock > 0) {
353a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
354170db9c6SJohn Dyson 		if (before) {
355170db9c6SJohn Dyson 			*before *= pagesperblock;
356170db9c6SJohn Dyson 			*before += poff;
357170db9c6SJohn Dyson 		}
358170db9c6SJohn Dyson 		if (after) {
35984d31376SGleb Smirnoff 			/*
36084d31376SGleb Smirnoff 			 * The BMAP vop can report a partial block in the
361d2596d17SGleb Smirnoff 			 * 'after', but must not report blocks after EOF.
36284d31376SGleb Smirnoff 			 * Assert the latter, and truncate 'after' in case
36384d31376SGleb Smirnoff 			 * of the former.
36484d31376SGleb Smirnoff 			 */
365d2596d17SGleb Smirnoff 			KASSERT((reqblock + *after) * pagesperblock <
366d2596d17SGleb Smirnoff 			    roundup2(object->size, pagesperblock),
36784d31376SGleb Smirnoff 			    ("%s: reqblock %jd after %d size %ju", __func__,
36884d31376SGleb Smirnoff 			    (intmax_t )reqblock, *after,
36984d31376SGleb Smirnoff 			    (uintmax_t )object->size));
370170db9c6SJohn Dyson 			*after *= pagesperblock;
37184d31376SGleb Smirnoff 			*after += pagesperblock - (poff + 1);
37284d31376SGleb Smirnoff 			if (pindex + *after >= object->size)
37384d31376SGleb Smirnoff 				*after = object->size - 1 - pindex;
374170db9c6SJohn Dyson 		}
375d63596ceSJohn Dyson 	} else {
376d63596ceSJohn Dyson 		if (before) {
377d63596ceSJohn Dyson 			*before /= blocksperpage;
378d63596ceSJohn Dyson 		}
379d63596ceSJohn Dyson 
380d63596ceSJohn Dyson 		if (after) {
381d63596ceSJohn Dyson 			*after /= blocksperpage;
382d63596ceSJohn Dyson 		}
383d63596ceSJohn Dyson 	}
384ced399eeSJohn Dyson 	return TRUE;
385df8bae1dSRodney W. Grimes }
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes /*
388df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
38924a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
390df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
391df8bae1dSRodney W. Grimes  *
392df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
393df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
394df8bae1dSRodney W. Grimes  */
395df8bae1dSRodney W. Grimes void
3967ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
397df8bae1dSRodney W. Grimes {
3982a8f9ab5SAlan Cox 	vm_object_t object;
3992a8f9ab5SAlan Cox 	vm_page_t m;
400c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
401df8bae1dSRodney W. Grimes 
4022a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
403df8bae1dSRodney W. Grimes 		return;
404cb61d698SKonstantin Belousov /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
40589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
4069b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
4079b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
4089b8851faSKonstantin Belousov 		return;
4099b8851faSKonstantin Belousov 	}
4109b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
4119b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
4122a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
413df8bae1dSRodney W. Grimes 		/*
414df8bae1dSRodney W. Grimes 		 * Hasn't changed size
415df8bae1dSRodney W. Grimes 		 */
41689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
417df8bae1dSRodney W. Grimes 		return;
4182a8f9ab5SAlan Cox 	}
419c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
4202a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
421df8bae1dSRodney W. Grimes 		/*
422bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
423df8bae1dSRodney W. Grimes 		 */
4242a8f9ab5SAlan Cox 		if (nobjsize < object->size)
425c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
4266bbee8e2SAlan Cox 			    0);
427bbc0ec52SDavid Greenman 		/*
428bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4293ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4303ebeaf59SMatthew Dillon 		 *
4313ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4323ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4333ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
434bbc0ec52SDavid Greenman 		 */
4352a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4361b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4371b26eb10SAlan Cox 		    m->valid != 0) {
4382b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4392b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4402b6b0df7SMatthew Dillon 
4412b6b0df7SMatthew Dillon 			/*
4422b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4432b6b0df7SMatthew Dillon 			 * the page has been mapped.
4442b6b0df7SMatthew Dillon 			 */
445fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4462b6b0df7SMatthew Dillon 
4472b6b0df7SMatthew Dillon 			/*
4483c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4493c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
4503c33df62SAlan Cox 			 * have already been set.
4513c33df62SAlan Cox 			 */
452dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
4533c33df62SAlan Cox 
4543c33df62SAlan Cox 			/*
4553c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
4563c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
4573c33df62SAlan Cox 			 * cleared.
4583c33df62SAlan Cox 			 */
4593c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
4603c33df62SAlan Cox 
4613c33df62SAlan Cox 			/*
4623c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
4633ebeaf59SMatthew Dillon 			 *
4643ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4653ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4663ebeaf59SMatthew Dillon 			 * replacement from working properly.
4672b6b0df7SMatthew Dillon 			 */
4683c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
4690ab3c7a5SAlan Cox 		} else if ((nsize & PAGE_MASK) &&
470db9ba578SAttilio Rao 		    vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
4710ab3c7a5SAlan Cox 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
4720ab3c7a5SAlan Cox 			    nobjsize);
473bbc0ec52SDavid Greenman 		}
474bbc0ec52SDavid Greenman 	}
475a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
476c576d121SLuoqi Chen 	object->size = nobjsize;
47789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
478df8bae1dSRodney W. Grimes }
479df8bae1dSRodney W. Grimes 
48026f9a767SRodney W. Grimes /*
48126f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
48226f9a767SRodney W. Grimes  * file address
48326f9a767SRodney W. Grimes  */
484bff76343SAlan Cox static int
485bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
486bff76343SAlan Cox     int *run)
48726f9a767SRodney W. Grimes {
48826f9a767SRodney W. Grimes 	int bsize;
48926f9a767SRodney W. Grimes 	int err;
490a316d390SJohn Dyson 	daddr_t vblock;
491f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
49226f9a767SRodney W. Grimes 
4932ad036b6SAlan Cox 	if (address < 0)
4940d94caffSDavid Greenman 		return -1;
4950d94caffSDavid Greenman 
496b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
4972c4488fcSJohn Dyson 		return -1;
4982c4488fcSJohn Dyson 
49926f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
50026f9a767SRodney W. Grimes 	vblock = address / bsize;
50126f9a767SRodney W. Grimes 	voffset = address % bsize;
50226f9a767SRodney W. Grimes 
503bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
504bff76343SAlan Cox 	if (err == 0) {
505bff76343SAlan Cox 		if (*rtaddress != -1)
506bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
507efc68ce1SDavid Greenman 		if (run) {
508efc68ce1SDavid Greenman 			*run += 1;
509efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
510efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
511efc68ce1SDavid Greenman 		}
512efc68ce1SDavid Greenman 	}
51326f9a767SRodney W. Grimes 
514bff76343SAlan Cox 	return (err);
51526f9a767SRodney W. Grimes }
51626f9a767SRodney W. Grimes 
51726f9a767SRodney W. Grimes /*
51826f9a767SRodney W. Grimes  * small block filesystem vnode pager input
51926f9a767SRodney W. Grimes  */
520f708ef1bSPoul-Henning Kamp static int
5217ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
52226f9a767SRodney W. Grimes {
5239c83534dSPoul-Henning Kamp 	struct vnode *vp;
5249c83534dSPoul-Henning Kamp 	struct bufobj *bo;
52526f9a767SRodney W. Grimes 	struct buf *bp;
5269e0ddbd0SAlan Cox 	struct sf_buf *sf;
527f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
52826f9a767SRodney W. Grimes 	vm_offset_t bsize;
529561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
530561cc9fcSKonstantin Belousov 	int error, i;
53126f9a767SRodney W. Grimes 
532561cc9fcSKonstantin Belousov 	error = 0;
53324a1cce3SDavid Greenman 	vp = object->handle;
534b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5352c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5362c4488fcSJohn Dyson 
53726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5380bdb7528SDavid Greenman 
5399c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
54026f9a767SRodney W. Grimes 
5419e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
54226f9a767SRodney W. Grimes 
54326f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
54433c67741SMatthew Dillon 		vm_ooffset_t address;
545bbc0ec52SDavid Greenman 
5460d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5470d53a17bSAlan Cox 		if (m->valid & bits)
54826f9a767SRodney W. Grimes 			continue;
54926f9a767SRodney W. Grimes 
55033c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
55133c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
55233c67741SMatthew Dillon 			fileaddr = -1;
55333c67741SMatthew Dillon 		} else {
554bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
555bff76343SAlan Cox 			if (error)
556bff76343SAlan Cox 				break;
55733c67741SMatthew Dillon 		}
55826f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5591c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
56026f9a767SRodney W. Grimes 
56126f9a767SRodney W. Grimes 			/* build a minimal buffer header */
56221144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5636a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
564bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
565bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
566a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
567a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5689e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
569187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5709c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
5711faacf5dSKirk McKusick 			bp->b_vp = vp;
57226f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
57326f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5742b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5755bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
57626f9a767SRodney W. Grimes 
57726f9a767SRodney W. Grimes 			/* do the input */
5782c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
579b792bebeSPoul-Henning Kamp 			bstrategy(bp);
58026f9a767SRodney W. Grimes 
5816a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5826a4b5823SPoul-Henning Kamp 
583c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
58426f9a767SRodney W. Grimes 				error = EIO;
58526f9a767SRodney W. Grimes 
58626f9a767SRodney W. Grimes 			/*
58726f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
58826f9a767SRodney W. Grimes 			 */
5891faacf5dSKirk McKusick 			bp->b_vp = NULL;
5909c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5911c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
59226f9a767SRodney W. Grimes 			if (error)
59326f9a767SRodney W. Grimes 				break;
5940d53a17bSAlan Cox 		} else
5959e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
5960d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
5970d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
59889f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5990d53a17bSAlan Cox 		m->valid |= bits;
60089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
60126f9a767SRodney W. Grimes 	}
6029e0ddbd0SAlan Cox 	sf_buf_free(sf);
60326f9a767SRodney W. Grimes 	if (error) {
604a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
60526f9a767SRodney W. Grimes 	}
60626f9a767SRodney W. Grimes 	return VM_PAGER_OK;
60726f9a767SRodney W. Grimes }
60826f9a767SRodney W. Grimes 
60926f9a767SRodney W. Grimes /*
610475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
61126f9a767SRodney W. Grimes  */
612f708ef1bSPoul-Henning Kamp static int
6137ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
61426f9a767SRodney W. Grimes {
615df8bae1dSRodney W. Grimes 	struct uio auio;
616df8bae1dSRodney W. Grimes 	struct iovec aiov;
61726f9a767SRodney W. Grimes 	int error;
61826f9a767SRodney W. Grimes 	int size;
6199e0ddbd0SAlan Cox 	struct sf_buf *sf;
620342a1480SJohn Baldwin 	struct vnode *vp;
621df8bae1dSRodney W. Grimes 
62289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
62326f9a767SRodney W. Grimes 	error = 0;
624bbc0ec52SDavid Greenman 
625df8bae1dSRodney W. Grimes 	/*
62626f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
62726f9a767SRodney W. Grimes 	 */
628a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
62926f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
63026f9a767SRodney W. Grimes 	} else {
63126f9a767SRodney W. Grimes 		size = PAGE_SIZE;
632a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
633a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
63452051abcSAlan Cox 		vp = object->handle;
63589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
6360bdb7528SDavid Greenman 
63726f9a767SRodney W. Grimes 		/*
638df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
639df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
640df8bae1dSRodney W. Grimes 		 */
6419e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6420bdb7528SDavid Greenman 
6439e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
644df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
645df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
646df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
647a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
648df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
64926f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
650df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
651b40ce416SJulian Elischer 		auio.uio_td = curthread;
65226f9a767SRodney W. Grimes 
653a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
654df8bae1dSRodney W. Grimes 		if (!error) {
65554d92145SMatthew Dillon 			int count = size - auio.uio_resid;
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 			if (count == 0)
658df8bae1dSRodney W. Grimes 				error = EINVAL;
65926f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6609e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6619e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
662df8bae1dSRodney W. Grimes 		}
6639e0ddbd0SAlan Cox 		sf_buf_free(sf);
6641b26eb10SAlan Cox 
66589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
666df8bae1dSRodney W. Grimes 	}
6670d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
6686e3a3f38SRobert V. Baron 	if (!error)
6696e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
670a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
67126f9a767SRodney W. Grimes }
67226f9a767SRodney W. Grimes 
67326f9a767SRodney W. Grimes /*
67426f9a767SRodney W. Grimes  * generic vnode pager input routine
67526f9a767SRodney W. Grimes  */
676170db9c6SJohn Dyson 
677ce75f2c3SMike Smith /*
67823955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
67947e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
68047e151ddSRobert Drehmel  * to implement the previous behaviour.
681ce75f2c3SMike Smith  *
682ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
683ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
684ce75f2c3SMike Smith  */
685f708ef1bSPoul-Henning Kamp static int
686b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
687b0cd2017SGleb Smirnoff     int *rahead)
68824a1cce3SDavid Greenman {
689170db9c6SJohn Dyson 	struct vnode *vp;
690b0cd2017SGleb Smirnoff 	int rtval;
69195e5e988SJohn Dyson 
692170db9c6SJohn Dyson 	vp = object->handle;
69389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
694b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
69523955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
69623955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
69789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
698170db9c6SJohn Dyson 	return rtval;
699170db9c6SJohn Dyson }
700170db9c6SJohn Dyson 
70190effb23SGleb Smirnoff static int
70290effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
703b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
70490effb23SGleb Smirnoff {
70590effb23SGleb Smirnoff 	struct vnode *vp;
70690effb23SGleb Smirnoff 	int rtval;
70790effb23SGleb Smirnoff 
70890effb23SGleb Smirnoff 	vp = object->handle;
70990effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
710b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
71190effb23SGleb Smirnoff 	KASSERT(rtval != EOPNOTSUPP,
71290effb23SGleb Smirnoff 	    ("vnode_pager: FS getpages_async not implemented\n"));
71390effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
71490effb23SGleb Smirnoff 	return (rtval);
71590effb23SGleb Smirnoff }
71690effb23SGleb Smirnoff 
717ce75f2c3SMike Smith /*
71890effb23SGleb Smirnoff  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
71990effb23SGleb Smirnoff  * local filesystems, where partially valid pages can only occur at
72090effb23SGleb Smirnoff  * the end of file.
721d15b55c5SKonstantin Belousov  */
722d15b55c5SKonstantin Belousov int
723d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
724d15b55c5SKonstantin Belousov {
72590effb23SGleb Smirnoff 
726b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
727b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
72890effb23SGleb Smirnoff }
72990effb23SGleb Smirnoff 
73090effb23SGleb Smirnoff int
73190effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
73290effb23SGleb Smirnoff {
73390effb23SGleb Smirnoff 
734b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
735b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
736d15b55c5SKonstantin Belousov }
737d15b55c5SKonstantin Belousov 
738d15b55c5SKonstantin Belousov /*
739ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
740ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
741ce75f2c3SMike Smith  */
742ce75f2c3SMike Smith int
743b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
744b0cd2017SGleb Smirnoff     int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
745170db9c6SJohn Dyson {
746ce75f2c3SMike Smith 	vm_object_t object;
7479c83534dSPoul-Henning Kamp 	struct bufobj *bo;
7480bdb7528SDavid Greenman 	struct buf *bp;
749b0cd2017SGleb Smirnoff 	off_t foff;
750b0cd2017SGleb Smirnoff 	int bsize, pagesperblock, *freecnt;
751b0cd2017SGleb Smirnoff 	int error, before, after, rbehind, rahead, poff, i;
752b0cd2017SGleb Smirnoff 	int bytecount, secmask;
753ce75f2c3SMike Smith 
7549c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
755b0cd2017SGleb Smirnoff 	    ("%s does not support devices", __func__));
756b0cd2017SGleb Smirnoff 
757b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
758eac91e32SKonstantin Belousov 		return (VM_PAGER_BAD);
7592c4488fcSJohn Dyson 
760eac91e32SKonstantin Belousov 	object = vp->v_object;
761b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(m[0]->pindex);
76226f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
763b0cd2017SGleb Smirnoff 	pagesperblock = bsize / PAGE_SIZE;
764b0cd2017SGleb Smirnoff 
765b0cd2017SGleb Smirnoff 	KASSERT(foff < object->un_pager.vnp.vnp_size,
766b0cd2017SGleb Smirnoff 	    ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
767b0cd2017SGleb Smirnoff 	KASSERT(count <= sizeof(bp->b_pages),
768b0cd2017SGleb Smirnoff 	    ("%s: requested %d pages", __func__, count));
769b0cd2017SGleb Smirnoff 
770b0cd2017SGleb Smirnoff 	/*
771b0cd2017SGleb Smirnoff 	 * The last page has valid blocks.  Invalid part can only
772b0cd2017SGleb Smirnoff 	 * exist at the end of file, and the page is made fully valid
773b0cd2017SGleb Smirnoff 	 * by zeroing in vm_pager_get_pages().
774b0cd2017SGleb Smirnoff 	 */
775b0cd2017SGleb Smirnoff 	if (m[count - 1]->valid != 0 && --count == 0) {
776b0cd2017SGleb Smirnoff 		if (iodone != NULL)
777b0cd2017SGleb Smirnoff 			iodone(arg, m, 1, 0);
778b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
779b0cd2017SGleb Smirnoff 	}
780bbc0ec52SDavid Greenman 
78141c895a8SGleb Smirnoff 	/*
78241c895a8SGleb Smirnoff 	 * Synchronous and asynchronous paging operations use different
78341c895a8SGleb Smirnoff 	 * free pbuf counters.  This is done to avoid asynchronous requests
78441c895a8SGleb Smirnoff 	 * to consume all pbufs.
78541c895a8SGleb Smirnoff 	 * Allocate the pbuf at the very beginning of the function, so that
78641c895a8SGleb Smirnoff 	 * if we are low on certain kind of pbufs don't even proceed to BMAP,
78741c895a8SGleb Smirnoff 	 * but sleep.
78841c895a8SGleb Smirnoff 	 */
78973e9030eSGleb Smirnoff 	freecnt = iodone != NULL ?
79073e9030eSGleb Smirnoff 	    &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
79173e9030eSGleb Smirnoff 	bp = getpbuf(freecnt);
79273e9030eSGleb Smirnoff 
79326f9a767SRodney W. Grimes 	/*
794e122dfc1SGleb Smirnoff 	 * Get the underlying device blocks for the file with VOP_BMAP().
795e122dfc1SGleb Smirnoff 	 * If the file system doesn't support VOP_BMAP, use old way of
796e122dfc1SGleb Smirnoff 	 * getting pages via VOP_READ.
79726f9a767SRodney W. Grimes 	 */
798b0cd2017SGleb Smirnoff 	error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
7991de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
80073e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
80189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
802b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
803b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodein);
804b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodepgsin);
805b0cd2017SGleb Smirnoff 			error = vnode_pager_input_old(object, m[i]);
806b0cd2017SGleb Smirnoff 			if (error)
807b0cd2017SGleb Smirnoff 				break;
808b0cd2017SGleb Smirnoff 		}
80989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
81052051abcSAlan Cox 		return (error);
8111de11f1aSAlan Cox 	} else if (error != 0) {
81273e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
8131de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
814b0cd2017SGleb Smirnoff 	}
815bbc0ec52SDavid Greenman 
81626f9a767SRodney W. Grimes 	/*
817b0cd2017SGleb Smirnoff 	 * If the file system supports BMAP, but blocksize is smaller
818b0cd2017SGleb Smirnoff 	 * than a page size, then use special small filesystem code.
81926f9a767SRodney W. Grimes 	 */
820b0cd2017SGleb Smirnoff 	if (pagesperblock == 0) {
821ff64a90eSKonstantin Belousov 		relpbuf(bp, freecnt);
822b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
823b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodein);
824b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodepgsin);
825b0cd2017SGleb Smirnoff 			error = vnode_pager_input_smlfs(object, m[i]);
826b0cd2017SGleb Smirnoff 			if (error)
827b0cd2017SGleb Smirnoff 				break;
828b0cd2017SGleb Smirnoff 		}
829b0cd2017SGleb Smirnoff 		return (error);
83026f9a767SRodney W. Grimes 	}
8318d17e694SJulian Elischer 
83226f9a767SRodney W. Grimes 	/*
833b0cd2017SGleb Smirnoff 	 * A sparse file can be encountered only for a single page request,
834763df3ecSPedro F. Giffuni 	 * which may not be preceded by call to vm_pager_haspage().
835a7fecb4dSAlan Cox 	 */
836b0cd2017SGleb Smirnoff 	if (bp->b_blkno == -1) {
837b0cd2017SGleb Smirnoff 		KASSERT(count == 1,
838b0cd2017SGleb Smirnoff 		    ("%s: array[%d] request to a sparse file %p", __func__,
839b0cd2017SGleb Smirnoff 		    count, vp));
84073e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
841b0cd2017SGleb Smirnoff 		pmap_zero_page(m[0]);
842b0cd2017SGleb Smirnoff 		KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
843b0cd2017SGleb Smirnoff 		    __func__, m[0]));
844a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
845b0cd2017SGleb Smirnoff 		m[0]->valid = VM_PAGE_BITS_ALL;
84689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
847f4f83da0SAlan Cox 		return (VM_PAGER_OK);
848b0cd2017SGleb Smirnoff 	}
849b0cd2017SGleb Smirnoff 
850b0cd2017SGleb Smirnoff 	bp->b_blkno += (foff % bsize) / DEV_BSIZE;
851b0cd2017SGleb Smirnoff 
852b0cd2017SGleb Smirnoff 	/* Recalculate blocks available after/before to pages. */
853b0cd2017SGleb Smirnoff 	poff = (foff % bsize) / PAGE_SIZE;
854b0cd2017SGleb Smirnoff 	before *= pagesperblock;
855b0cd2017SGleb Smirnoff 	before += poff;
856b0cd2017SGleb Smirnoff 	after *= pagesperblock;
857b0cd2017SGleb Smirnoff 	after += pagesperblock - (poff + 1);
858b0cd2017SGleb Smirnoff 	if (m[0]->pindex + after >= object->size)
859b0cd2017SGleb Smirnoff 		after = object->size - 1 - m[0]->pindex;
860b0cd2017SGleb Smirnoff 	KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
861b0cd2017SGleb Smirnoff 	    __func__, count, after + 1));
862b0cd2017SGleb Smirnoff 	after -= count - 1;
863b0cd2017SGleb Smirnoff 
864b0cd2017SGleb Smirnoff 	/* Trim requested rbehind/rahead to possible values. */
865b0cd2017SGleb Smirnoff 	rbehind = a_rbehind ? *a_rbehind : 0;
866b0cd2017SGleb Smirnoff 	rahead = a_rahead ? *a_rahead : 0;
867b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, before);
868b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, m[0]->pindex);
869b0cd2017SGleb Smirnoff 	rahead = min(rahead, after);
870b0cd2017SGleb Smirnoff 	rahead = min(rahead, object->size - m[count - 1]->pindex);
871b0cd2017SGleb Smirnoff 	KASSERT(rbehind + rahead + count <= sizeof(bp->b_pages),
872b0cd2017SGleb Smirnoff 	    ("%s: behind %d ahead %d count %d", __func__,
873b0cd2017SGleb Smirnoff 	    rbehind, rahead, count));
874b0cd2017SGleb Smirnoff 
875b0cd2017SGleb Smirnoff 	/*
876b0cd2017SGleb Smirnoff 	 * Fill in the bp->b_pages[] array with requested and optional
877b0cd2017SGleb Smirnoff 	 * read behind or read ahead pages.  Read behind pages are looked
878b0cd2017SGleb Smirnoff 	 * up in a backward direction, down to a first cached page.  Same
879b0cd2017SGleb Smirnoff 	 * for read ahead pages, but there is no need to shift the array
880b0cd2017SGleb Smirnoff 	 * in case of encountering a cached page.
881b0cd2017SGleb Smirnoff 	 */
882b0cd2017SGleb Smirnoff 	i = bp->b_npages = 0;
883b0cd2017SGleb Smirnoff 	if (rbehind) {
884b0cd2017SGleb Smirnoff 		vm_pindex_t startpindex, tpindex;
885b0cd2017SGleb Smirnoff 		vm_page_t p;
886b0cd2017SGleb Smirnoff 
887a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
888b0cd2017SGleb Smirnoff 		startpindex = m[0]->pindex - rbehind;
889b0cd2017SGleb Smirnoff 		if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
890b0cd2017SGleb Smirnoff 		    p->pindex >= startpindex)
891b0cd2017SGleb Smirnoff 			startpindex = p->pindex + 1;
892b0cd2017SGleb Smirnoff 
893b0cd2017SGleb Smirnoff 		/* tpindex is unsigned; beware of numeric underflow. */
894b0cd2017SGleb Smirnoff 		for (tpindex = m[0]->pindex - 1;
895b0cd2017SGleb Smirnoff 		    tpindex >= startpindex && tpindex < m[0]->pindex;
896b0cd2017SGleb Smirnoff 		    tpindex--, i++) {
897b0cd2017SGleb Smirnoff 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
898b0cd2017SGleb Smirnoff 			    VM_ALLOC_IFNOTCACHED);
899b0cd2017SGleb Smirnoff 			if (p == NULL) {
900b0cd2017SGleb Smirnoff 				/* Shift the array. */
901b0cd2017SGleb Smirnoff 				for (int j = 0; j < i; j++)
902b0cd2017SGleb Smirnoff 					bp->b_pages[j] = bp->b_pages[j +
903b0cd2017SGleb Smirnoff 					    tpindex + 1 - startpindex];
904b0cd2017SGleb Smirnoff 				break;
905b0cd2017SGleb Smirnoff 			}
906b0cd2017SGleb Smirnoff 			bp->b_pages[tpindex - startpindex] = p;
907a7fecb4dSAlan Cox 		}
9080bdb7528SDavid Greenman 
909b0cd2017SGleb Smirnoff 		bp->b_pgbefore = i;
910b0cd2017SGleb Smirnoff 		bp->b_npages += i;
911b0cd2017SGleb Smirnoff 		bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
912b0cd2017SGleb Smirnoff 	} else
913b0cd2017SGleb Smirnoff 		bp->b_pgbefore = 0;
914b0cd2017SGleb Smirnoff 
915b0cd2017SGleb Smirnoff 	/* Requested pages. */
916b0cd2017SGleb Smirnoff 	for (int j = 0; j < count; j++, i++)
917b0cd2017SGleb Smirnoff 		bp->b_pages[i] = m[j];
918b0cd2017SGleb Smirnoff 	bp->b_npages += count;
919b0cd2017SGleb Smirnoff 
920b0cd2017SGleb Smirnoff 	if (rahead) {
921b0cd2017SGleb Smirnoff 		vm_pindex_t endpindex, tpindex;
922b0cd2017SGleb Smirnoff 		vm_page_t p;
923b0cd2017SGleb Smirnoff 
924b0cd2017SGleb Smirnoff 		if (!VM_OBJECT_WOWNED(object))
925eac91e32SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
926b0cd2017SGleb Smirnoff 		endpindex = m[count - 1]->pindex + rahead + 1;
927b0cd2017SGleb Smirnoff 		if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
928b0cd2017SGleb Smirnoff 		    p->pindex < endpindex)
929b0cd2017SGleb Smirnoff 			endpindex = p->pindex;
930b0cd2017SGleb Smirnoff 		if (endpindex > object->size)
931b0cd2017SGleb Smirnoff 			endpindex = object->size;
932b0cd2017SGleb Smirnoff 
933b0cd2017SGleb Smirnoff 		for (tpindex = m[count - 1]->pindex + 1;
934b0cd2017SGleb Smirnoff 		    tpindex < endpindex; i++, tpindex++) {
935b0cd2017SGleb Smirnoff 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
936b0cd2017SGleb Smirnoff 			    VM_ALLOC_IFNOTCACHED);
937b0cd2017SGleb Smirnoff 			if (p == NULL)
938b0cd2017SGleb Smirnoff 				break;
939b0cd2017SGleb Smirnoff 			bp->b_pages[i] = p;
940eac91e32SKonstantin Belousov 		}
941b0cd2017SGleb Smirnoff 
942b0cd2017SGleb Smirnoff 		bp->b_pgafter = i - bp->b_npages;
943b0cd2017SGleb Smirnoff 		bp->b_npages = i;
944b0cd2017SGleb Smirnoff 	} else
945b0cd2017SGleb Smirnoff 		bp->b_pgafter = 0;
946b0cd2017SGleb Smirnoff 
947b0cd2017SGleb Smirnoff 	if (VM_OBJECT_WOWNED(object))
948eac91e32SKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
949b0cd2017SGleb Smirnoff 
950b0cd2017SGleb Smirnoff 	/* Report back actual behind/ahead read. */
951b0cd2017SGleb Smirnoff 	if (a_rbehind)
952b0cd2017SGleb Smirnoff 		*a_rbehind = bp->b_pgbefore;
953b0cd2017SGleb Smirnoff 	if (a_rahead)
954b0cd2017SGleb Smirnoff 		*a_rahead = bp->b_pgafter;
955b0cd2017SGleb Smirnoff 
956b0cd2017SGleb Smirnoff 	KASSERT(bp->b_npages <= sizeof(bp->b_pages),
957b0cd2017SGleb Smirnoff 	    ("%s: buf %p overflowed", __func__, bp));
958eac91e32SKonstantin Belousov 
9590d94caffSDavid Greenman 	/*
960b0cd2017SGleb Smirnoff 	 * Recalculate first offset and bytecount with regards to read behind.
961b0cd2017SGleb Smirnoff 	 * Truncate bytecount to vnode real size and round up physical size
962b0cd2017SGleb Smirnoff 	 * for real devices.
96326f9a767SRodney W. Grimes 	 */
964b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
965b0cd2017SGleb Smirnoff 	bytecount = bp->b_npages << PAGE_SHIFT;
966b0cd2017SGleb Smirnoff 	if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
967b0cd2017SGleb Smirnoff 		bytecount = object->un_pager.vnp.vnp_size - foff;
968eac91e32SKonstantin Belousov 	secmask = bo->bo_bsize - 1;
9696229cc50SPoul-Henning Kamp 	KASSERT(secmask < PAGE_SIZE && secmask > 0,
970b0cd2017SGleb Smirnoff 	    ("%s: sector size %d too large", __func__, secmask + 1));
971b0cd2017SGleb Smirnoff 	bytecount = (bytecount + secmask) & ~secmask;
97226f9a767SRodney W. Grimes 
97326f9a767SRodney W. Grimes 	/*
974b0cd2017SGleb Smirnoff 	 * And map the pages to be read into the kva, if the filesystem
9756ce697dcSKonstantin Belousov 	 * requires mapped buffers.
97626f9a767SRodney W. Grimes 	 */
9772a5eef69SGleb Smirnoff 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
9786ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
9796ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
9806ce697dcSKonstantin Belousov 		bp->b_offset = 0;
981fade8dd7SJeff Roberson 	} else {
982fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
983b0cd2017SGleb Smirnoff 		pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
984fade8dd7SJeff Roberson 	}
98526f9a767SRodney W. Grimes 
986b0cd2017SGleb Smirnoff 	/* Build a minimal buffer header. */
98721144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
988bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
989bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
990a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
991a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
9929c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
9931faacf5dSKirk McKusick 	bp->b_vp = vp;
994b0cd2017SGleb Smirnoff 	bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
9952c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
99690effb23SGleb Smirnoff 
997b0cd2017SGleb Smirnoff 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
998b0cd2017SGleb Smirnoff 	PCPU_INC(cnt.v_vnodein);
999b0cd2017SGleb Smirnoff 	PCPU_ADD(cnt.v_vnodepgsin, bp->b_npages);
1000b0cd2017SGleb Smirnoff 
100190effb23SGleb Smirnoff 	if (iodone != NULL) { /* async */
1002b0cd2017SGleb Smirnoff 		bp->b_pgiodone = iodone;
100390effb23SGleb Smirnoff 		bp->b_caller1 = arg;
100490effb23SGleb Smirnoff 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
100590effb23SGleb Smirnoff 		bp->b_flags |= B_ASYNC;
100690effb23SGleb Smirnoff 		BUF_KERNPROC(bp);
1007b792bebeSPoul-Henning Kamp 		bstrategy(bp);
1008b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
100990effb23SGleb Smirnoff 	} else {
101090effb23SGleb Smirnoff 		bp->b_iodone = bdone;
101190effb23SGleb Smirnoff 		bstrategy(bp);
10126a4b5823SPoul-Henning Kamp 		bwait(bp, PVM, "vnread");
101390effb23SGleb Smirnoff 		error = vnode_pager_generic_getpages_done(bp);
10141bb5ad63SGleb Smirnoff 		for (i = 0; i < bp->b_npages; i++)
10156ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
10161faacf5dSKirk McKusick 		bp->b_vp = NULL;
10179c83534dSPoul-Henning Kamp 		pbrelbo(bp);
10181c7c3c6aSMatthew Dillon 		relpbuf(bp, &vnode_pbuf_freecnt);
101990effb23SGleb Smirnoff 		return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
102090effb23SGleb Smirnoff 	}
1021b0cd2017SGleb Smirnoff }
102290effb23SGleb Smirnoff 
102390effb23SGleb Smirnoff static void
102490effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp)
102590effb23SGleb Smirnoff {
102690effb23SGleb Smirnoff 	int error;
102790effb23SGleb Smirnoff 
102890effb23SGleb Smirnoff 	error = vnode_pager_generic_getpages_done(bp);
1029b0cd2017SGleb Smirnoff 	/* Run the iodone upon the requested range. */
1030b0cd2017SGleb Smirnoff 	bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1031b0cd2017SGleb Smirnoff 	    bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
103290effb23SGleb Smirnoff 	for (int i = 0; i < bp->b_npages; i++)
103390effb23SGleb Smirnoff 		bp->b_pages[i] = NULL;
103490effb23SGleb Smirnoff 	bp->b_vp = NULL;
103590effb23SGleb Smirnoff 	pbrelbo(bp);
103673e9030eSGleb Smirnoff 	relpbuf(bp, &vnode_async_pbuf_freecnt);
103790effb23SGleb Smirnoff }
103890effb23SGleb Smirnoff 
103990effb23SGleb Smirnoff static int
104090effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp)
104190effb23SGleb Smirnoff {
104290effb23SGleb Smirnoff 	vm_object_t object;
104390effb23SGleb Smirnoff 	off_t tfoff, nextoff;
104490effb23SGleb Smirnoff 	int i, error;
104590effb23SGleb Smirnoff 
104690effb23SGleb Smirnoff 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
104790effb23SGleb Smirnoff 	object = bp->b_vp->v_object;
104890effb23SGleb Smirnoff 
104990effb23SGleb Smirnoff 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1050fade8dd7SJeff Roberson 		if (!buf_mapped(bp)) {
1051fade8dd7SJeff Roberson 			bp->b_data = bp->b_kvabase;
1052fade8dd7SJeff Roberson 			pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
105390effb23SGleb Smirnoff 			    bp->b_npages);
105490effb23SGleb Smirnoff 		}
1055fade8dd7SJeff Roberson 		bzero(bp->b_data + bp->b_bcount,
105690effb23SGleb Smirnoff 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
105790effb23SGleb Smirnoff 	}
1058fade8dd7SJeff Roberson 	if (buf_mapped(bp)) {
1059fade8dd7SJeff Roberson 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1060fade8dd7SJeff Roberson 		bp->b_data = unmapped_buf;
106190effb23SGleb Smirnoff 	}
106226f9a767SRodney W. Grimes 
106389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
106490effb23SGleb Smirnoff 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
106590effb23SGleb Smirnoff 	    i < bp->b_npages; i++, tfoff = nextoff) {
10668f9110f6SJohn Dyson 		vm_page_t mt;
10678f9110f6SJohn Dyson 
10688f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
106990effb23SGleb Smirnoff 		mt = bp->b_pages[i];
10708f9110f6SJohn Dyson 
107154746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
10728d17e694SJulian Elischer 			/*
10738d17e694SJulian Elischer 			 * Read filled up entire page.
10748d17e694SJulian Elischer 			 */
10758f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
1076016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
107779f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
1078016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
107979f0deb9SGleb Smirnoff 			    ("%s: page %p is mapped", __func__, mt));
10808f9110f6SJohn Dyson 		} else {
10818d17e694SJulian Elischer 			/*
108242eb4108SAlan Cox 			 * Read did not fill up entire page.
10838d17e694SJulian Elischer 			 *
10848d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
10858d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
10868d17e694SJulian Elischer 			 * read.
10878d17e694SJulian Elischer 			 */
1088dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
108954746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
109042eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
109142eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
109279f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
10938f9110f6SJohn Dyson 		}
10948f9110f6SJohn Dyson 
1095b0cd2017SGleb Smirnoff 		if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1096b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
109703679e23SAlan Cox 	}
109889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
109990effb23SGleb Smirnoff 	if (error != 0)
110090effb23SGleb Smirnoff 		printf("%s: I/O read error %d\n", __func__, error);
110190effb23SGleb Smirnoff 
110290effb23SGleb Smirnoff 	return (error);
110326f9a767SRodney W. Grimes }
110426f9a767SRodney W. Grimes 
1105ce75f2c3SMike Smith /*
1106ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1107ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1108ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1109ce75f2c3SMike Smith  *
1110ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1111ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1112ce75f2c3SMike Smith  */
1113e4542174SMatthew Dillon static void
11147ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
111533cad9e9SKonstantin Belousov     int flags, int *rtvals)
1116170db9c6SJohn Dyson {
1117170db9c6SJohn Dyson 	int rtval;
1118170db9c6SJohn Dyson 	struct vnode *vp;
111986ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1120ad980522SJohn Dyson 
11210e3cdf2cSAlan Cox 	/*
11220e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
11230e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
11240e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
11250e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
11260e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
11270e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
11280e3cdf2cSAlan Cox 	 *
11290e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
11300e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
11310e3cdf2cSAlan Cox 	 */
11320e3cdf2cSAlan Cox 
113333cad9e9SKonstantin Belousov 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count <
113444f1c916SBryan Drewery 	    vm_cnt.v_pageout_free_min)
113533cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
11360e3cdf2cSAlan Cox 
11370e3cdf2cSAlan Cox 	/*
11380e3cdf2cSAlan Cox 	 * Call device-specific putpages function
11390e3cdf2cSAlan Cox 	 */
1140170db9c6SJohn Dyson 	vp = object->handle;
114189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
114233cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
114323955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
114423955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
114589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1146170db9c6SJohn Dyson }
1147170db9c6SJohn Dyson 
1148ce75f2c3SMike Smith 
114926f9a767SRodney W. Grimes /*
1150ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
11514491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
11522b6b0df7SMatthew Dillon  *
11532b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
1154763df3ecSPedro F. Giffuni  * clustering has already typically occurred, so in general we ask the
11552b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
11562b6b0df7SMatthew Dillon  * then delayed.
115726f9a767SRodney W. Grimes  */
1158ce75f2c3SMike Smith int
1159c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1160c46b90e9SAlan Cox     int flags, int *rtvals)
116126f9a767SRodney W. Grimes {
1162f6b04d2bSDavid Greenman 	int i;
1163ce75f2c3SMike Smith 	vm_object_t object;
1164c46b90e9SAlan Cox 	vm_page_t m;
1165ce75f2c3SMike Smith 	int count;
116626f9a767SRodney W. Grimes 
1167f6b04d2bSDavid Greenman 	int maxsize, ncount;
1168a316d390SJohn Dyson 	vm_ooffset_t poffset;
1169f6b04d2bSDavid Greenman 	struct uio auio;
1170f6b04d2bSDavid Greenman 	struct iovec aiov;
1171f6b04d2bSDavid Greenman 	int error;
11728f9110f6SJohn Dyson 	int ioflags;
1173dd498befSPaul Saab 	int ppscheck = 0;
1174dd498befSPaul Saab 	static struct timeval lastfail;
1175dd498befSPaul Saab 	static int curfail;
117626f9a767SRodney W. Grimes 
1177ce75f2c3SMike Smith 	object = vp->v_object;
1178ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1179ce75f2c3SMike Smith 
118026f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1181031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
118226f9a767SRodney W. Grimes 
1183c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
118423562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1185c46b90e9SAlan Cox 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1186f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1187f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
11880d94caffSDavid Greenman 	}
11890bdb7528SDavid Greenman 
1190f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1191f6b04d2bSDavid Greenman 	ncount = count;
119226f9a767SRodney W. Grimes 
1193c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
119400a6f47fSMatthew Dillon 
119500a6f47fSMatthew Dillon 	/*
119600a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
1197763df3ecSPedro F. Giffuni 	 * have to invalidate pages occurring beyond the file EOF.  However,
119800a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
119900a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
120000a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
120100a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
120200a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
12033ebeaf59SMatthew Dillon 	 *
12043ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
12053ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
120600a6f47fSMatthew Dillon 	 */
120789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1208a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
120900a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
121000a6f47fSMatthew Dillon 			int pgoff;
121100a6f47fSMatthew Dillon 
1212a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1213aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
121400a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1215c46b90e9SAlan Cox 				/*
1216c46b90e9SAlan Cox 				 * If the object is locked and the following
1217c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1218c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1219c46b90e9SAlan Cox 				 * pmap operation.
1220c46b90e9SAlan Cox 				 */
1221c46b90e9SAlan Cox 				m = ma[ncount - 1];
1222c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
12236031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1224c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1225c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1226c46b90e9SAlan Cox 				    pgoff);
122700a6f47fSMatthew Dillon 			}
122800a6f47fSMatthew Dillon 		} else {
122900a6f47fSMatthew Dillon 			maxsize = 0;
123000a6f47fSMatthew Dillon 			ncount = 0;
123100a6f47fSMatthew Dillon 		}
1232f6b04d2bSDavid Greenman 		if (ncount < count) {
1233f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1234f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1235f6b04d2bSDavid Greenman 			}
1236f6b04d2bSDavid Greenman 		}
1237f6b04d2bSDavid Greenman 	}
123889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
123926f9a767SRodney W. Grimes 
12402b6b0df7SMatthew Dillon 	/*
12412b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
12422b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
124343b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
124443b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
124543b7990eSMatthew Dillon 	 * the system decides how to cluster.
12462b6b0df7SMatthew Dillon 	 */
12478f9110f6SJohn Dyson 	ioflags = IO_VMIO;
124843b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
124943b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
125043b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
125143b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
12528f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
125343b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1254f6b04d2bSDavid Greenman 
1255f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1256f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1257f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1258f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1259a316d390SJohn Dyson 	auio.uio_offset = poffset;
1260f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1261f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1262f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1263b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1264a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1265b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodeout);
1266b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1267f6b04d2bSDavid Greenman 
1268f6b04d2bSDavid Greenman 	if (error) {
1269dd498befSPaul Saab 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
127024a1cce3SDavid Greenman 			printf("vnode_pager_putpages: I/O error %d\n", error);
1271f6b04d2bSDavid Greenman 	}
1272f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1273dd498befSPaul Saab 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
12749f80ce04SKonstantin Belousov 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1275c46b90e9SAlan Cox 			    auio.uio_resid, (u_long)ma[0]->pindex);
127626f9a767SRodney W. Grimes 	}
1277ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
127826f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
127926f9a767SRodney W. Grimes 	}
1280f6b04d2bSDavid Greenman 	return rtvals[0];
128126f9a767SRodney W. Grimes }
1282031ec8c1SKonstantin Belousov 
1283031ec8c1SKonstantin Belousov void
1284031ec8c1SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1285031ec8c1SKonstantin Belousov {
12869d17da3bSKonstantin Belousov 	vm_object_t obj;
1287031ec8c1SKonstantin Belousov 	int i, pos;
1288031ec8c1SKonstantin Belousov 
12899d17da3bSKonstantin Belousov 	if (written == 0)
12909d17da3bSKonstantin Belousov 		return;
12919d17da3bSKonstantin Belousov 	obj = ma[0]->object;
129289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1293031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1294031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1295031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1296031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1297031ec8c1SKonstantin Belousov 		} else {
1298031ec8c1SKonstantin Belousov 			/* Partially written page. */
1299031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1300031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1301031ec8c1SKonstantin Belousov 		}
1302031ec8c1SKonstantin Belousov 	}
130389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1304031ec8c1SKonstantin Belousov }
130584110e7eSKonstantin Belousov 
130684110e7eSKonstantin Belousov void
130784110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
130884110e7eSKonstantin Belousov     vm_offset_t end)
130984110e7eSKonstantin Belousov {
131084110e7eSKonstantin Belousov 	struct vnode *vp;
131184110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
131284110e7eSKonstantin Belousov 
131389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
131484110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
131589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
131684110e7eSKonstantin Belousov 		return;
131784110e7eSKonstantin Belousov 	}
131884110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
131984110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
132084110e7eSKonstantin Belousov 	vp = object->handle;
132184110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
132284110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1323140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, 1);
1324b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1325b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
132684110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
132784110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1328140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
1329b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1330b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
133184110e7eSKonstantin Belousov 	}
133289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
133384110e7eSKonstantin Belousov }
133484110e7eSKonstantin Belousov 
133584110e7eSKonstantin Belousov void
133684110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
133784110e7eSKonstantin Belousov     vm_offset_t end)
133884110e7eSKonstantin Belousov {
133984110e7eSKonstantin Belousov 	struct vnode *vp;
134084110e7eSKonstantin Belousov 	struct mount *mp;
134184110e7eSKonstantin Belousov 	vm_offset_t inc;
134284110e7eSKonstantin Belousov 
134389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
134484110e7eSKonstantin Belousov 
134584110e7eSKonstantin Belousov 	/*
134684110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
134784110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
134884110e7eSKonstantin Belousov 	 */
134984110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
135089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
135184110e7eSKonstantin Belousov 		return;
135284110e7eSKonstantin Belousov 	}
135384110e7eSKonstantin Belousov 
135484110e7eSKonstantin Belousov 	/*
135584110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
135684110e7eSKonstantin Belousov 	 * zero.
135784110e7eSKonstantin Belousov 	 */
135884110e7eSKonstantin Belousov 	inc = end - start;
135984110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
136084110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
136189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
136284110e7eSKonstantin Belousov 		return;
136384110e7eSKonstantin Belousov 	}
136484110e7eSKonstantin Belousov 
136584110e7eSKonstantin Belousov 	vp = object->handle;
136684110e7eSKonstantin Belousov 	vhold(vp);
136789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
136884110e7eSKonstantin Belousov 	mp = NULL;
136984110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
137084110e7eSKonstantin Belousov 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
137184110e7eSKonstantin Belousov 
137284110e7eSKonstantin Belousov 	/*
137384110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
137484110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
137584110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
137684110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
137784110e7eSKonstantin Belousov 	 */
137884110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
137984110e7eSKonstantin Belousov 	VOP_UNLOCK(vp, 0);
138084110e7eSKonstantin Belousov 	vdrop(vp);
138184110e7eSKonstantin Belousov 	if (mp != NULL)
138284110e7eSKonstantin Belousov 		vn_finished_write(mp);
138384110e7eSKonstantin Belousov }
1384