xref: /freebsd/sys/vm/vnode_pager.c (revision b068bb09a1a82d9fef0e939ad6135443a959e290)
160727d8bSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
526f9a767SRodney W. Grimes  * Copyright (c) 1991 The Regents of the University of California.
626f9a767SRodney W. Grimes  * All rights reserved.
726f9a767SRodney W. Grimes  * Copyright (c) 1993, 1994 John S. Dyson
824a1cce3SDavid Greenman  * Copyright (c) 1995, David Greenman
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
12df8bae1dSRodney W. Grimes  * Science Department.
13df8bae1dSRodney W. Grimes  *
14df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
15df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
16df8bae1dSRodney W. Grimes  * are met:
17df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
19df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
20df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
21df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
22df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
235929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
24df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
25df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
26df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
27df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
28df8bae1dSRodney W. Grimes  *    without specific prior written permission.
29df8bae1dSRodney W. Grimes  *
30df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
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>
543d653db0SAlan Cox #include "opt_vm.h"
553d653db0SAlan Cox 
56df8bae1dSRodney W. Grimes #include <sys/param.h>
57756a5412SGleb Smirnoff #include <sys/kernel.h>
58df8bae1dSRodney W. Grimes #include <sys/systm.h>
59e5818a53SJeff Roberson #include <sys/sysctl.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>
66daec9284SConrad Meyer #include <sys/ktr.h>
67d07a6d3fSPoul-Henning Kamp #include <sys/limits.h>
6824579ca1SMatthew Dillon #include <sys/conf.h>
6951df5321SJeff Roberson #include <sys/refcount.h>
7089f6b863SAttilio Rao #include <sys/rwlock.h>
719e0ddbd0SAlan Cox #include <sys/sf_buf.h>
72e5818a53SJeff Roberson #include <sys/domainset.h>
7300a3fe96SKonstantin Belousov #include <sys/user.h>
74df8bae1dSRodney W. Grimes 
754f12e0acSSuleiman Souhlal #include <machine/atomic.h>
764f12e0acSSuleiman Souhlal 
77df8bae1dSRodney W. Grimes #include <vm/vm.h>
781c771f92SKonstantin Belousov #include <vm/vm_param.h>
79efeaf95aSDavid Greenman #include <vm/vm_object.h>
80df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8124a1cce3SDavid Greenman #include <vm/vm_pager.h>
821efb74fbSJohn Dyson #include <vm/vm_map.h>
83df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
84efeaf95aSDavid Greenman #include <vm/vm_extern.h>
85756a5412SGleb Smirnoff #include <vm/uma.h>
86df8bae1dSRodney W. Grimes 
87bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
88bff76343SAlan Cox     daddr_t *rtaddress, int *run);
8911caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
9011caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
9111caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
92b0cd2017SGleb Smirnoff static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
93b0cd2017SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
94b0cd2017SGleb Smirnoff     int *, vop_getpages_iodone_t, void *);
9533cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
9611caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
973364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
983364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *cred);
9990effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *);
10090effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *);
101fe7bcbafSKyle Evans static void vnode_pager_update_writecount(vm_object_t, vm_offset_t,
102fe7bcbafSKyle Evans     vm_offset_t);
103fe7bcbafSKyle Evans static void vnode_pager_release_writecount(vm_object_t, vm_offset_t,
104fe7bcbafSKyle Evans     vm_offset_t);
105192112b7SKonstantin Belousov static void vnode_pager_getvp(vm_object_t, struct vnode **, bool *);
1060b8253a7SBruce Evans 
107d474440aSKonstantin Belousov const struct pagerops vnodepagerops = {
10800a3fe96SKonstantin Belousov 	.pgo_kvme_type = KVME_TYPE_VNODE,
1094e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
1104e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
1114e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
11290effb23SGleb Smirnoff 	.pgo_getpages_async = vnode_pager_getpages_async,
1134e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
1144e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
115fe7bcbafSKyle Evans 	.pgo_update_writecount = vnode_pager_update_writecount,
116fe7bcbafSKyle Evans 	.pgo_release_writecount = vnode_pager_release_writecount,
117180bcaa4SKonstantin Belousov 	.pgo_set_writeable_dirty = vm_object_set_writeable_dirty_,
118c23c555bSKonstantin Belousov 	.pgo_mightbedirty = vm_object_mightbedirty_,
119192112b7SKonstantin Belousov 	.pgo_getvp = vnode_pager_getvp,
120df8bae1dSRodney W. Grimes };
121df8bae1dSRodney W. Grimes 
122e5818a53SJeff Roberson static struct domainset *vnode_domainset = NULL;
123e5818a53SJeff Roberson 
124a314aba8SMateusz Guzik SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset,
125a314aba8SMateusz Guzik     CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_RW, &vnode_domainset, 0,
126a314aba8SMateusz Guzik     sysctl_handle_domainset, "A", "Default vnode NUMA policy");
127e5818a53SJeff Roberson 
12866fb0b1aSGleb Smirnoff static int nvnpbufs;
12966fb0b1aSGleb Smirnoff SYSCTL_INT(_vm, OID_AUTO, vnode_pbufs, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
13066fb0b1aSGleb Smirnoff     &nvnpbufs, 0, "number of physical buffers allocated for vnode pager");
13166fb0b1aSGleb Smirnoff 
132756a5412SGleb Smirnoff static uma_zone_t vnode_pbuf_zone;
133756a5412SGleb Smirnoff 
134756a5412SGleb Smirnoff static void
135756a5412SGleb Smirnoff vnode_pager_init(void *dummy)
136756a5412SGleb Smirnoff {
137756a5412SGleb Smirnoff 
13866fb0b1aSGleb Smirnoff #ifdef __LP64__
13966fb0b1aSGleb Smirnoff 	nvnpbufs = nswbuf * 2;
14066fb0b1aSGleb Smirnoff #else
14166fb0b1aSGleb Smirnoff 	nvnpbufs = nswbuf / 2;
14266fb0b1aSGleb Smirnoff #endif
14366fb0b1aSGleb Smirnoff 	TUNABLE_INT_FETCH("vm.vnode_pbufs", &nvnpbufs);
14466fb0b1aSGleb Smirnoff 	vnode_pbuf_zone = pbuf_zsecond_create("vnpbuf", nvnpbufs);
145756a5412SGleb Smirnoff }
146756a5412SGleb Smirnoff SYSINIT(vnode_pager, SI_SUB_CPU, SI_ORDER_ANY, vnode_pager_init, NULL);
147756a5412SGleb Smirnoff 
148d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
149d07a6d3fSPoul-Henning Kamp int
150731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
151d07a6d3fSPoul-Henning Kamp {
152d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
153d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
154a67d5408SJeff Roberson 	bool last;
155d07a6d3fSPoul-Henning Kamp 
1567ad2a82dSMateusz Guzik 	if (!vn_isdisk(vp) && vn_canvmio(vp) == FALSE)
157d07a6d3fSPoul-Henning Kamp 		return (0);
158d07a6d3fSPoul-Henning Kamp 
1596470c8d3SKonstantin Belousov 	object = vp->v_object;
1606470c8d3SKonstantin Belousov 	if (object != NULL)
161d07a6d3fSPoul-Henning Kamp 		return (0);
162d07a6d3fSPoul-Henning Kamp 
163d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
1647ad2a82dSMateusz Guzik 		if (vn_isdisk(vp)) {
165d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
166d07a6d3fSPoul-Henning Kamp 		} else {
167f45feecfSMateusz Guzik 			if (vn_getsize_locked(vp, &size, td->td_ucred) != 0)
168d07a6d3fSPoul-Henning Kamp 				return (0);
169d07a6d3fSPoul-Henning Kamp 		}
170d07a6d3fSPoul-Henning Kamp 	}
171d07a6d3fSPoul-Henning Kamp 
1723364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
173d07a6d3fSPoul-Henning Kamp 	/*
174d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
175a67d5408SJeff Roberson 	 * that the object is associated with the vp.  We still have
176a67d5408SJeff Roberson 	 * to serialize with vnode_pager_dealloc() for the last
177a67d5408SJeff Roberson 	 * potential reference.
178d07a6d3fSPoul-Henning Kamp 	 */
17951df5321SJeff Roberson 	VM_OBJECT_RLOCK(object);
180a67d5408SJeff Roberson 	last = refcount_release(&object->ref_count);
18151df5321SJeff Roberson 	VM_OBJECT_RUNLOCK(object);
182a67d5408SJeff Roberson 	if (last)
183d07a6d3fSPoul-Henning Kamp 		vrele(vp);
184d07a6d3fSPoul-Henning Kamp 
185d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
186d07a6d3fSPoul-Henning Kamp 
187d07a6d3fSPoul-Henning Kamp 	return (0);
188d07a6d3fSPoul-Henning Kamp }
189d07a6d3fSPoul-Henning Kamp 
1907146d6cbSPoul-Henning Kamp void
1917146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1927146d6cbSPoul-Henning Kamp {
1937146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1947146d6cbSPoul-Henning Kamp 
1957146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1966470c8d3SKonstantin Belousov 	if (obj == NULL || obj->handle != vp)
1977146d6cbSPoul-Henning Kamp 		return;
19857fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
19989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
2006470c8d3SKonstantin Belousov 	MPASS(obj->type == OBJT_VNODE);
2012a339d9eSKonstantin Belousov 	umtx_shm_object_terminated(obj);
2027146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
2039c83ff2dSJeff Roberson 		KASSERT((obj->flags & OBJ_DEAD) == 0,
2049c83ff2dSJeff Roberson 		   ("vnode_destroy_vobject: Terminating dead object"));
205783a68aaSKonstantin Belousov 		vm_object_set_flag(obj, OBJ_DEAD);
206783a68aaSKonstantin Belousov 
207783a68aaSKonstantin Belousov 		/*
208783a68aaSKonstantin Belousov 		 * Clean pages and flush buffers.
209783a68aaSKonstantin Belousov 		 */
210783a68aaSKonstantin Belousov 		vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
211783a68aaSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
212783a68aaSKonstantin Belousov 
213783a68aaSKonstantin Belousov 		vinvalbuf(vp, V_SAVE, 0, 0);
214783a68aaSKonstantin Belousov 
215783a68aaSKonstantin Belousov 		BO_LOCK(&vp->v_bufobj);
216783a68aaSKonstantin Belousov 		vp->v_bufobj.bo_flag |= BO_DEAD;
217783a68aaSKonstantin Belousov 		BO_UNLOCK(&vp->v_bufobj);
218783a68aaSKonstantin Belousov 
219783a68aaSKonstantin Belousov 		VM_OBJECT_WLOCK(obj);
2207146d6cbSPoul-Henning Kamp 		vm_object_terminate(obj);
22190880a1bSKonstantin Belousov 	} else {
22290880a1bSKonstantin Belousov 		/*
2237146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
2247146d6cbSPoul-Henning Kamp 		 */
2257146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
22689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
2277146d6cbSPoul-Henning Kamp 	}
22890880a1bSKonstantin Belousov 	KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
2297146d6cbSPoul-Henning Kamp }
2307146d6cbSPoul-Henning Kamp 
231df8bae1dSRodney W. Grimes /*
232df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
233df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
234df8bae1dSRodney W. Grimes  */
23524a1cce3SDavid Greenman vm_object_t
2366cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
2373364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
238df8bae1dSRodney W. Grimes {
23906cb7259SDavid Greenman 	vm_object_t object;
240df8bae1dSRodney W. Grimes 	struct vnode *vp;
241df8bae1dSRodney W. Grimes 
242df8bae1dSRodney W. Grimes 	/*
243df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
244df8bae1dSRodney W. Grimes 	 */
245df8bae1dSRodney W. Grimes 	if (handle == NULL)
246df8bae1dSRodney W. Grimes 		return (NULL);
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes 	vp = (struct vnode *)handle;
2496470c8d3SKonstantin Belousov 	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
250f1fa1ba3SMateusz Guzik 	VNPASS(vp->v_usecount > 0, vp);
2516470c8d3SKonstantin Belousov retry:
2526470c8d3SKonstantin Belousov 	object = vp->v_object;
2532be70f79SJohn Dyson 
25424a1cce3SDavid Greenman 	if (object == NULL) {
255df8bae1dSRodney W. Grimes 		/*
2562ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
257df8bae1dSRodney W. Grimes 		 */
2586470c8d3SKonstantin Belousov 		object = vm_object_allocate(OBJT_VNODE,
2596470c8d3SKonstantin Belousov 		    OFF_TO_IDX(round_page(size)));
260bbc0ec52SDavid Greenman 
2616cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
26284110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
263e5818a53SJeff Roberson 		object->domain.dr_policy = vnode_domainset;
26424a1cce3SDavid Greenman 		object->handle = handle;
265208b81bbSKonstantin Belousov 		if ((vp->v_vflag & VV_VMSIZEVNLOCK) != 0) {
266208b81bbSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
267208b81bbSKonstantin Belousov 			vm_object_set_flag(object, OBJ_SIZEVNLOCK);
268208b81bbSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
269208b81bbSKonstantin Belousov 		}
27011be8415SStephan Uphoff 		VI_LOCK(vp);
2712ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2722ac78f0eSStephan Uphoff 			/*
2736470c8d3SKonstantin Belousov 			 * Object has been created while we were allocating.
2742ac78f0eSStephan Uphoff 			 */
27511be8415SStephan Uphoff 			VI_UNLOCK(vp);
2769cddade7SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
2779cddade7SKonstantin Belousov 			KASSERT(object->ref_count == 1,
2789cddade7SKonstantin Belousov 			    ("leaked ref %p %d", object, object->ref_count));
2799cddade7SKonstantin Belousov 			object->type = OBJT_DEAD;
28051df5321SJeff Roberson 			refcount_init(&object->ref_count, 0);
2819cddade7SKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
2822ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2832ac78f0eSStephan Uphoff 			goto retry;
284df8bae1dSRodney W. Grimes 		}
2852ac78f0eSStephan Uphoff 		vp->v_object = object;
28611be8415SStephan Uphoff 		VI_UNLOCK(vp);
287a67d5408SJeff Roberson 		vrefact(vp);
28811be8415SStephan Uphoff 	} else {
289a67d5408SJeff Roberson 		vm_object_reference(object);
2903d653db0SAlan Cox #if VM_NRESERVLEVEL > 0
291a67d5408SJeff Roberson 		if ((object->flags & OBJ_COLORED) == 0) {
292a67d5408SJeff Roberson 			VM_OBJECT_WLOCK(object);
2933d653db0SAlan Cox 			vm_object_color(object, 0);
29489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
29511be8415SStephan Uphoff 		}
296a67d5408SJeff Roberson #endif
297a67d5408SJeff Roberson 	}
29824a1cce3SDavid Greenman 	return (object);
299df8bae1dSRodney W. Grimes }
300df8bae1dSRodney W. Grimes 
301658ad5ffSAlan Cox /*
302658ad5ffSAlan Cox  *	The object must be locked.
303658ad5ffSAlan Cox  */
304f708ef1bSPoul-Henning Kamp static void
3057ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
30624a1cce3SDavid Greenman {
307b9f180d1SKonstantin Belousov 	struct vnode *vp;
308b9f180d1SKonstantin Belousov 	int refs;
309df8bae1dSRodney W. Grimes 
310b9f180d1SKonstantin Belousov 	vp = object->handle;
31124a1cce3SDavid Greenman 	if (vp == NULL)
31224a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
31324a1cce3SDavid Greenman 
31489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
31566095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
316b9f180d1SKonstantin Belousov 	refs = object->ref_count;
31724a1cce3SDavid Greenman 
31824a1cce3SDavid Greenman 	object->handle = NULL;
31995461b45SJohn Dyson 	object->type = OBJT_DEAD;
32057fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
32184110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
32284110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
32378022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
324b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
325b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
32684110e7eSKonstantin Belousov 	}
327aa2cabb9SDavid Greenman 	vp->v_object = NULL;
32878022527SKonstantin Belousov 	VI_LOCK(vp);
32978022527SKonstantin Belousov 
33078022527SKonstantin Belousov 	/*
33178022527SKonstantin Belousov 	 * vm_map_entry_set_vnode_text() cannot reach this vnode by
33278022527SKonstantin Belousov 	 * following object->handle.  Clear all text references now.
33378022527SKonstantin Belousov 	 * This also clears the transient references from
33478022527SKonstantin Belousov 	 * kern_execve(), which is fine because dead_vnodeops uses nop
33578022527SKonstantin Belousov 	 * for VOP_UNSET_TEXT().
33678022527SKonstantin Belousov 	 */
33778022527SKonstantin Belousov 	if (vp->v_writecount < 0)
33878022527SKonstantin Belousov 		vp->v_writecount = 0;
33978022527SKonstantin Belousov 	VI_UNLOCK(vp);
34089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
341a67d5408SJeff Roberson 	if (refs > 0)
342b9f180d1SKonstantin Belousov 		vunref(vp);
34389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
344df8bae1dSRodney W. Grimes }
34526f9a767SRodney W. Grimes 
346f708ef1bSPoul-Henning Kamp static boolean_t
3477ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
3487ebba1f8SGleb Smirnoff     int *after)
349df8bae1dSRodney W. Grimes {
35024a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
35198b0c789SPoul-Henning Kamp 	daddr_t bn;
3524153054aSJeff Roberson 	uintptr_t lockstate;
3533af76890SPoul-Henning Kamp 	int err;
354170db9c6SJohn Dyson 	daddr_t reqblock;
3552c4488fcSJohn Dyson 	int poff;
3562c4488fcSJohn Dyson 	int bsize;
357d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
358df8bae1dSRodney W. Grimes 
3594153054aSJeff Roberson 	VM_OBJECT_ASSERT_LOCKED(object);
36024579ca1SMatthew Dillon 	/*
36124579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
36224579ca1SMatthew Dillon 	 * have the page.
36324579ca1SMatthew Dillon 	 */
364abd80ddbSMateusz Guzik 	if (vp == NULL || VN_IS_DOOMED(vp))
36547221757SJohn Dyson 		return FALSE;
366df8bae1dSRodney W. Grimes 	/*
367b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3680d94caffSDavid Greenman 	 * not have the page.
369df8bae1dSRodney W. Grimes 	 */
370b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3714abc71c0SDavid Greenman 		return FALSE;
372df8bae1dSRodney W. Grimes 
373eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
374170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
375d63596ceSJohn Dyson 	blocksperpage = 0;
376d63596ceSJohn Dyson 	if (pagesperblock > 0) {
377a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
378d63596ceSJohn Dyson 	} else {
379d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
380d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
381d63596ceSJohn Dyson 	}
3824153054aSJeff Roberson 	lockstate = VM_OBJECT_DROP(object);
383ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
3844153054aSJeff Roberson 	VM_OBJECT_PICKUP(object, lockstate);
3850d94caffSDavid Greenman 	if (err)
38624a1cce3SDavid Greenman 		return TRUE;
3876eab77f2SJohn Dyson 	if (bn == -1)
388ced399eeSJohn Dyson 		return FALSE;
389d63596ceSJohn Dyson 	if (pagesperblock > 0) {
390a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
391170db9c6SJohn Dyson 		if (before) {
392170db9c6SJohn Dyson 			*before *= pagesperblock;
393170db9c6SJohn Dyson 			*before += poff;
394170db9c6SJohn Dyson 		}
395170db9c6SJohn Dyson 		if (after) {
39684d31376SGleb Smirnoff 			/*
39784d31376SGleb Smirnoff 			 * The BMAP vop can report a partial block in the
398d2596d17SGleb Smirnoff 			 * 'after', but must not report blocks after EOF.
39984d31376SGleb Smirnoff 			 * Assert the latter, and truncate 'after' in case
40084d31376SGleb Smirnoff 			 * of the former.
40184d31376SGleb Smirnoff 			 */
402d2596d17SGleb Smirnoff 			KASSERT((reqblock + *after) * pagesperblock <
403d2596d17SGleb Smirnoff 			    roundup2(object->size, pagesperblock),
40484d31376SGleb Smirnoff 			    ("%s: reqblock %jd after %d size %ju", __func__,
40584d31376SGleb Smirnoff 			    (intmax_t )reqblock, *after,
40684d31376SGleb Smirnoff 			    (uintmax_t )object->size));
407170db9c6SJohn Dyson 			*after *= pagesperblock;
40884d31376SGleb Smirnoff 			*after += pagesperblock - (poff + 1);
40984d31376SGleb Smirnoff 			if (pindex + *after >= object->size)
41084d31376SGleb Smirnoff 				*after = object->size - 1 - pindex;
411170db9c6SJohn Dyson 		}
412d63596ceSJohn Dyson 	} else {
413d63596ceSJohn Dyson 		if (before) {
414d63596ceSJohn Dyson 			*before /= blocksperpage;
415d63596ceSJohn Dyson 		}
416d63596ceSJohn Dyson 
417d63596ceSJohn Dyson 		if (after) {
418d63596ceSJohn Dyson 			*after /= blocksperpage;
419d63596ceSJohn Dyson 		}
420d63596ceSJohn Dyson 	}
421ced399eeSJohn Dyson 	return TRUE;
422df8bae1dSRodney W. Grimes }
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes /*
425de2e1529SKa Ho Ng  * Internal routine clearing partial-page content
426de2e1529SKa Ho Ng  */
427de2e1529SKa Ho Ng static void
428de2e1529SKa Ho Ng vnode_pager_subpage_purge(struct vm_page *m, int base, int end)
429de2e1529SKa Ho Ng {
430de2e1529SKa Ho Ng 	int size;
431de2e1529SKa Ho Ng 
432de2e1529SKa Ho Ng 	KASSERT(end > base && end <= PAGE_SIZE,
433de2e1529SKa Ho Ng 	    ("%s: start %d end %d", __func__, base, end));
434de2e1529SKa Ho Ng 	size = end - base;
435de2e1529SKa Ho Ng 
436de2e1529SKa Ho Ng 	/*
437de2e1529SKa Ho Ng 	 * Clear out partial-page garbage in case
438de2e1529SKa Ho Ng 	 * the page has been mapped.
439de2e1529SKa Ho Ng 	 */
440de2e1529SKa Ho Ng 	pmap_zero_page_area(m, base, size);
441de2e1529SKa Ho Ng 
442de2e1529SKa Ho Ng 	/*
443de2e1529SKa Ho Ng 	 * Update the valid bits to reflect the blocks
444de2e1529SKa Ho Ng 	 * that have been zeroed.  Some of these valid
445de2e1529SKa Ho Ng 	 * bits may have already been set.
446de2e1529SKa Ho Ng 	 */
447de2e1529SKa Ho Ng 	vm_page_set_valid_range(m, base, size);
448de2e1529SKa Ho Ng 
449de2e1529SKa Ho Ng 	/*
450de2e1529SKa Ho Ng 	 * Round up "base" to the next block boundary so
451de2e1529SKa Ho Ng 	 * that the dirty bit for a partially zeroed
452de2e1529SKa Ho Ng 	 * block is not cleared.
453de2e1529SKa Ho Ng 	 */
454de2e1529SKa Ho Ng 	base = roundup2(base, DEV_BSIZE);
455de2e1529SKa Ho Ng 	end = rounddown2(end, DEV_BSIZE);
456de2e1529SKa Ho Ng 
457de2e1529SKa Ho Ng 	if (end > base) {
458de2e1529SKa Ho Ng 		/*
459de2e1529SKa Ho Ng 		 * Clear out partial-page dirty bits.
460de2e1529SKa Ho Ng 		 *
461de2e1529SKa Ho Ng 		 * note that we do not clear out the
462de2e1529SKa Ho Ng 		 * valid bits.  This would prevent
463de2e1529SKa Ho Ng 		 * bogus_page replacement from working
464de2e1529SKa Ho Ng 		 * properly.
465de2e1529SKa Ho Ng 		 */
466de2e1529SKa Ho Ng 		vm_page_clear_dirty(m, base, end - base);
467de2e1529SKa Ho Ng 	}
468de2e1529SKa Ho Ng 
469de2e1529SKa Ho Ng }
470de2e1529SKa Ho Ng 
471de2e1529SKa Ho Ng /*
472df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
47324a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
474df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
475df8bae1dSRodney W. Grimes  *
476df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
477df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
478df8bae1dSRodney W. Grimes  */
479df8bae1dSRodney W. Grimes void
4807ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
481df8bae1dSRodney W. Grimes {
4822a8f9ab5SAlan Cox 	vm_object_t object;
4832a8f9ab5SAlan Cox 	vm_page_t m;
484c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
485df8bae1dSRodney W. Grimes 
4862a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
487df8bae1dSRodney W. Grimes 		return;
4885b87ecc6SKonstantin Belousov #ifdef DEBUG_VFS_LOCKS
4895b87ecc6SKonstantin Belousov 	{
4905b87ecc6SKonstantin Belousov 		struct mount *mp;
4915b87ecc6SKonstantin Belousov 
4925b87ecc6SKonstantin Belousov 		mp = vp->v_mount;
4935b87ecc6SKonstantin Belousov 		if (mp != NULL && (mp->mnt_kern_flag & MNTK_VMSETSIZE_BUG) == 0)
4945b87ecc6SKonstantin Belousov 			assert_vop_elocked(vp,
4955b87ecc6SKonstantin Belousov 			    "vnode_pager_setsize and not locked vnode");
4965b87ecc6SKonstantin Belousov 	}
4975b87ecc6SKonstantin Belousov #endif
49889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
4999b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
5009b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
5019b8851faSKonstantin Belousov 		return;
5029b8851faSKonstantin Belousov 	}
5039b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
5049b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
5052a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
506df8bae1dSRodney W. Grimes 		/*
507df8bae1dSRodney W. Grimes 		 * Hasn't changed size
508df8bae1dSRodney W. Grimes 		 */
50989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
510df8bae1dSRodney W. Grimes 		return;
5112a8f9ab5SAlan Cox 	}
512c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
5132a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
514df8bae1dSRodney W. Grimes 		/*
515bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
516df8bae1dSRodney W. Grimes 		 */
5172a8f9ab5SAlan Cox 		if (nobjsize < object->size)
518c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
5196bbee8e2SAlan Cox 			    0);
520bbc0ec52SDavid Greenman 		/*
521bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
5223ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
5233ebeaf59SMatthew Dillon 		 *
5243ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
5253ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
5263ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
527bbc0ec52SDavid Greenman 		 */
5280012f373SJeff Roberson 		if (!(nsize & PAGE_MASK))
5290012f373SJeff Roberson 			goto out;
5300012f373SJeff Roberson 		m = vm_page_grab(object, OFF_TO_IDX(nsize), VM_ALLOC_NOCREAT);
5310012f373SJeff Roberson 		if (m == NULL)
5320012f373SJeff Roberson 			goto out;
533de2e1529SKa Ho Ng 		if (!vm_page_none_valid(m))
534de2e1529SKa Ho Ng 			vnode_pager_subpage_purge(m, (int)nsize & PAGE_MASK,
535de2e1529SKa Ho Ng 			    PAGE_SIZE);
5360012f373SJeff Roberson 		vm_page_xunbusy(m);
537bbc0ec52SDavid Greenman 	}
5380012f373SJeff Roberson out:
539419e5698SKonstantin Belousov #if defined(__powerpc__) && !defined(__powerpc64__)
540a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
541419e5698SKonstantin Belousov #else
542419e5698SKonstantin Belousov 	atomic_store_64(&object->un_pager.vnp.vnp_size, nsize);
543419e5698SKonstantin Belousov #endif
544c576d121SLuoqi Chen 	object->size = nobjsize;
54589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
546df8bae1dSRodney W. Grimes }
547df8bae1dSRodney W. Grimes 
54826f9a767SRodney W. Grimes /*
549de2e1529SKa Ho Ng  * Lets the VM system know about the purged range for a file. We toss away any
550de2e1529SKa Ho Ng  * cached pages in the associated object that are affected by the purge
551de2e1529SKa Ho Ng  * operation. Partial-page area not aligned to page boundaries will be zeroed
552de2e1529SKa Ho Ng  * and the dirty blocks in DEV_BSIZE unit within a page will not be flushed.
553de2e1529SKa Ho Ng  */
554de2e1529SKa Ho Ng void
555de2e1529SKa Ho Ng vnode_pager_purge_range(struct vnode *vp, vm_ooffset_t start, vm_ooffset_t end)
556de2e1529SKa Ho Ng {
557de2e1529SKa Ho Ng 	struct vm_page *m;
558de2e1529SKa Ho Ng 	struct vm_object *object;
559de2e1529SKa Ho Ng 	vm_pindex_t pi, pistart, piend;
560de2e1529SKa Ho Ng 	bool same_page;
561de2e1529SKa Ho Ng 	int base, pend;
562de2e1529SKa Ho Ng 
563de2e1529SKa Ho Ng 	ASSERT_VOP_LOCKED(vp, "vnode_pager_purge_range");
564de2e1529SKa Ho Ng 
565de2e1529SKa Ho Ng 	object = vp->v_object;
566de2e1529SKa Ho Ng 	pi = start + PAGE_MASK < start ? OBJ_MAX_SIZE :
567de2e1529SKa Ho Ng 	    OFF_TO_IDX(start + PAGE_MASK);
568de2e1529SKa Ho Ng 	pistart = OFF_TO_IDX(start);
569de2e1529SKa Ho Ng 	piend = end == 0 ? OBJ_MAX_SIZE : OFF_TO_IDX(end);
570de2e1529SKa Ho Ng 	same_page = pistart == piend;
571de2e1529SKa Ho Ng 	if ((end != 0 && end <= start) || object == NULL)
572de2e1529SKa Ho Ng 		return;
573de2e1529SKa Ho Ng 
574de2e1529SKa Ho Ng 	VM_OBJECT_WLOCK(object);
575de2e1529SKa Ho Ng 
576de2e1529SKa Ho Ng 	if (pi < piend)
577de2e1529SKa Ho Ng 		vm_object_page_remove(object, pi, piend, 0);
578de2e1529SKa Ho Ng 
579de2e1529SKa Ho Ng 	if ((start & PAGE_MASK) != 0) {
580de2e1529SKa Ho Ng 		base = (int)start & PAGE_MASK;
581de2e1529SKa Ho Ng 		pend = same_page ? (int)end & PAGE_MASK : PAGE_SIZE;
582de2e1529SKa Ho Ng 		m = vm_page_grab(object, pistart, VM_ALLOC_NOCREAT);
583de2e1529SKa Ho Ng 		if (m != NULL) {
584de2e1529SKa Ho Ng 			if (!vm_page_none_valid(m))
585de2e1529SKa Ho Ng 				vnode_pager_subpage_purge(m, base, pend);
586de2e1529SKa Ho Ng 			vm_page_xunbusy(m);
587de2e1529SKa Ho Ng 		}
588de2e1529SKa Ho Ng 		if (same_page)
589de2e1529SKa Ho Ng 			goto out;
590de2e1529SKa Ho Ng 	}
591de2e1529SKa Ho Ng 	if ((end & PAGE_MASK) != 0) {
592de2e1529SKa Ho Ng 		base = same_page ? (int)start & PAGE_MASK : 0 ;
593de2e1529SKa Ho Ng 		pend = (int)end & PAGE_MASK;
594de2e1529SKa Ho Ng 		m = vm_page_grab(object, piend, VM_ALLOC_NOCREAT);
595de2e1529SKa Ho Ng 		if (m != NULL) {
596de2e1529SKa Ho Ng 			if (!vm_page_none_valid(m))
597de2e1529SKa Ho Ng 				vnode_pager_subpage_purge(m, base, pend);
598de2e1529SKa Ho Ng 			vm_page_xunbusy(m);
599de2e1529SKa Ho Ng 		}
600de2e1529SKa Ho Ng 	}
601de2e1529SKa Ho Ng out:
602de2e1529SKa Ho Ng 	VM_OBJECT_WUNLOCK(object);
603de2e1529SKa Ho Ng }
604de2e1529SKa Ho Ng 
605de2e1529SKa Ho Ng /*
60626f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
60726f9a767SRodney W. Grimes  * file address
60826f9a767SRodney W. Grimes  */
609bff76343SAlan Cox static int
610bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
611bff76343SAlan Cox     int *run)
61226f9a767SRodney W. Grimes {
61326f9a767SRodney W. Grimes 	int bsize;
61426f9a767SRodney W. Grimes 	int err;
615a316d390SJohn Dyson 	daddr_t vblock;
616f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
61726f9a767SRodney W. Grimes 
618abd80ddbSMateusz Guzik 	if (VN_IS_DOOMED(vp))
6192c4488fcSJohn Dyson 		return -1;
6202c4488fcSJohn Dyson 
62126f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
62226f9a767SRodney W. Grimes 	vblock = address / bsize;
62326f9a767SRodney W. Grimes 	voffset = address % bsize;
62426f9a767SRodney W. Grimes 
625bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
626bff76343SAlan Cox 	if (err == 0) {
627bff76343SAlan Cox 		if (*rtaddress != -1)
628bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
629efc68ce1SDavid Greenman 		if (run) {
630efc68ce1SDavid Greenman 			*run += 1;
631efc68ce1SDavid Greenman 			*run *= bsize / PAGE_SIZE;
632efc68ce1SDavid Greenman 			*run -= voffset / PAGE_SIZE;
633efc68ce1SDavid Greenman 		}
634efc68ce1SDavid Greenman 	}
63526f9a767SRodney W. Grimes 
636bff76343SAlan Cox 	return (err);
63726f9a767SRodney W. Grimes }
63826f9a767SRodney W. Grimes 
63928f957b8SKonstantin Belousov static void
64028f957b8SKonstantin Belousov vnode_pager_input_bdone(struct buf *bp)
64128f957b8SKonstantin Belousov {
64228f957b8SKonstantin Belousov 	runningbufwakeup(bp);
64328f957b8SKonstantin Belousov 	bdone(bp);
64428f957b8SKonstantin Belousov }
64528f957b8SKonstantin Belousov 
64626f9a767SRodney W. Grimes /*
64726f9a767SRodney W. Grimes  * small block filesystem vnode pager input
64826f9a767SRodney W. Grimes  */
649f708ef1bSPoul-Henning Kamp static int
6507ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
65126f9a767SRodney W. Grimes {
6529c83534dSPoul-Henning Kamp 	struct vnode *vp;
6539c83534dSPoul-Henning Kamp 	struct bufobj *bo;
65426f9a767SRodney W. Grimes 	struct buf *bp;
6559e0ddbd0SAlan Cox 	struct sf_buf *sf;
656f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
65726f9a767SRodney W. Grimes 	vm_offset_t bsize;
658561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
659561cc9fcSKonstantin Belousov 	int error, i;
66026f9a767SRodney W. Grimes 
661561cc9fcSKonstantin Belousov 	error = 0;
66224a1cce3SDavid Greenman 	vp = object->handle;
663abd80ddbSMateusz Guzik 	if (VN_IS_DOOMED(vp))
6642c4488fcSJohn Dyson 		return VM_PAGER_BAD;
6652c4488fcSJohn Dyson 
66626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
6670bdb7528SDavid Greenman 
6689c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
66926f9a767SRodney W. Grimes 
6709e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
67126f9a767SRodney W. Grimes 
67226f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
67333c67741SMatthew Dillon 		vm_ooffset_t address;
674bbc0ec52SDavid Greenman 
6750d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
6760d53a17bSAlan Cox 		if (m->valid & bits)
67726f9a767SRodney W. Grimes 			continue;
67826f9a767SRodney W. Grimes 
67933c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
68033c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
68133c67741SMatthew Dillon 			fileaddr = -1;
68233c67741SMatthew Dillon 		} else {
683bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
684bff76343SAlan Cox 			if (error)
685bff76343SAlan Cox 				break;
68633c67741SMatthew Dillon 		}
68726f9a767SRodney W. Grimes 		if (fileaddr != -1) {
688756a5412SGleb Smirnoff 			bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
68926f9a767SRodney W. Grimes 
69026f9a767SRodney W. Grimes 			/* build a minimal buffer header */
69121144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
69228f957b8SKonstantin Belousov 			bp->b_iodone = vnode_pager_input_bdone;
693bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
694bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
695a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
696a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
6979e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
698187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
6999c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
7001faacf5dSKirk McKusick 			bp->b_vp = vp;
70126f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
70226f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
7032b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
7045bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
70526f9a767SRodney W. Grimes 
70626f9a767SRodney W. Grimes 			/* do the input */
7072c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
708b792bebeSPoul-Henning Kamp 			bstrategy(bp);
70926f9a767SRodney W. Grimes 
7106a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
7116a4b5823SPoul-Henning Kamp 
712cafbf0c6SWarner Losh 			if ((bp->b_ioflags & BIO_ERROR) != 0) {
713cafbf0c6SWarner Losh 				KASSERT(bp->b_error != 0,
714cafbf0c6SWarner Losh 				    ("%s: buf error but b_error == 0\n", __func__));
715cafbf0c6SWarner Losh 				error = bp->b_error;
716cafbf0c6SWarner Losh 			}
71726f9a767SRodney W. Grimes 
71826f9a767SRodney W. Grimes 			/*
71926f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
72026f9a767SRodney W. Grimes 			 */
7211faacf5dSKirk McKusick 			bp->b_vp = NULL;
7229c83534dSPoul-Henning Kamp 			pbrelbo(bp);
723756a5412SGleb Smirnoff 			uma_zfree(vnode_pbuf_zone, bp);
72426f9a767SRodney W. Grimes 			if (error)
72526f9a767SRodney W. Grimes 				break;
7260d53a17bSAlan Cox 		} else
7279e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
7280d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
7290d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
7307f935055SJeff Roberson 		vm_page_bits_set(m, &m->valid, bits);
73126f9a767SRodney W. Grimes 	}
7329e0ddbd0SAlan Cox 	sf_buf_free(sf);
73326f9a767SRodney W. Grimes 	if (error) {
734a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
73526f9a767SRodney W. Grimes 	}
73626f9a767SRodney W. Grimes 	return VM_PAGER_OK;
73726f9a767SRodney W. Grimes }
73826f9a767SRodney W. Grimes 
73926f9a767SRodney W. Grimes /*
740475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
74126f9a767SRodney W. Grimes  */
742f708ef1bSPoul-Henning Kamp static int
7437ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
74426f9a767SRodney W. Grimes {
745df8bae1dSRodney W. Grimes 	struct uio auio;
746df8bae1dSRodney W. Grimes 	struct iovec aiov;
74726f9a767SRodney W. Grimes 	int error;
74826f9a767SRodney W. Grimes 	int size;
7499e0ddbd0SAlan Cox 	struct sf_buf *sf;
750342a1480SJohn Baldwin 	struct vnode *vp;
751df8bae1dSRodney W. Grimes 
75289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
75326f9a767SRodney W. Grimes 	error = 0;
754bbc0ec52SDavid Greenman 
755df8bae1dSRodney W. Grimes 	/*
75626f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
75726f9a767SRodney W. Grimes 	 */
758a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
75926f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
76026f9a767SRodney W. Grimes 	} else {
76126f9a767SRodney W. Grimes 		size = PAGE_SIZE;
762a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
763a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
76452051abcSAlan Cox 		vp = object->handle;
76589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
7660bdb7528SDavid Greenman 
76726f9a767SRodney W. Grimes 		/*
768df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
769df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
770df8bae1dSRodney W. Grimes 		 */
7719e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
7720bdb7528SDavid Greenman 
7739e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
774df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
775df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
776df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
777a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
778df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
77926f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
780df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
781b40ce416SJulian Elischer 		auio.uio_td = curthread;
78226f9a767SRodney W. Grimes 
783a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
784df8bae1dSRodney W. Grimes 		if (!error) {
78554d92145SMatthew Dillon 			int count = size - auio.uio_resid;
786df8bae1dSRodney W. Grimes 
787df8bae1dSRodney W. Grimes 			if (count == 0)
788df8bae1dSRodney W. Grimes 				error = EINVAL;
78926f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
7909e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
7919e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
792df8bae1dSRodney W. Grimes 		}
7939e0ddbd0SAlan Cox 		sf_buf_free(sf);
7941b26eb10SAlan Cox 
79589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
796df8bae1dSRodney W. Grimes 	}
7970d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
7986e3a3f38SRobert V. Baron 	if (!error)
7990012f373SJeff Roberson 		vm_page_valid(m);
800a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
80126f9a767SRodney W. Grimes }
80226f9a767SRodney W. Grimes 
80326f9a767SRodney W. Grimes /*
80426f9a767SRodney W. Grimes  * generic vnode pager input routine
80526f9a767SRodney W. Grimes  */
806170db9c6SJohn Dyson 
807ce75f2c3SMike Smith /*
80823955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
80947e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
81047e151ddSRobert Drehmel  * to implement the previous behaviour.
811ce75f2c3SMike Smith  *
812ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
813ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
814ce75f2c3SMike Smith  */
815f708ef1bSPoul-Henning Kamp static int
816b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
817b0cd2017SGleb Smirnoff     int *rahead)
81824a1cce3SDavid Greenman {
819170db9c6SJohn Dyson 	struct vnode *vp;
820b0cd2017SGleb Smirnoff 	int rtval;
82195e5e988SJohn Dyson 
822d6e13f3bSJeff Roberson 	/* Handle is stable with paging in progress. */
823170db9c6SJohn Dyson 	vp = object->handle;
824b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
82523955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
82623955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
827170db9c6SJohn Dyson 	return rtval;
828170db9c6SJohn Dyson }
829170db9c6SJohn Dyson 
83090effb23SGleb Smirnoff static int
83190effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
832b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
83390effb23SGleb Smirnoff {
83490effb23SGleb Smirnoff 	struct vnode *vp;
83590effb23SGleb Smirnoff 	int rtval;
83690effb23SGleb Smirnoff 
83790effb23SGleb Smirnoff 	vp = object->handle;
838b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
83990effb23SGleb Smirnoff 	KASSERT(rtval != EOPNOTSUPP,
84090effb23SGleb Smirnoff 	    ("vnode_pager: FS getpages_async not implemented\n"));
84190effb23SGleb Smirnoff 	return (rtval);
84290effb23SGleb Smirnoff }
84390effb23SGleb Smirnoff 
844ce75f2c3SMike Smith /*
84590effb23SGleb Smirnoff  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
84690effb23SGleb Smirnoff  * local filesystems, where partially valid pages can only occur at
84790effb23SGleb Smirnoff  * the end of file.
848d15b55c5SKonstantin Belousov  */
849d15b55c5SKonstantin Belousov int
850d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
851d15b55c5SKonstantin Belousov {
85290effb23SGleb Smirnoff 
853b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
854b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
85590effb23SGleb Smirnoff }
85690effb23SGleb Smirnoff 
85790effb23SGleb Smirnoff int
85890effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
85990effb23SGleb Smirnoff {
860abfdf767SKonstantin Belousov 	int error;
86190effb23SGleb Smirnoff 
862abfdf767SKonstantin Belousov 	error = vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
863abfdf767SKonstantin Belousov 	    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
864abfdf767SKonstantin Belousov 	if (error != 0 && ap->a_iodone != NULL)
865abfdf767SKonstantin Belousov 		ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
866abfdf767SKonstantin Belousov 	return (error);
867d15b55c5SKonstantin Belousov }
868d15b55c5SKonstantin Belousov 
869d15b55c5SKonstantin Belousov /*
870ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
871ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
872ce75f2c3SMike Smith  */
873ce75f2c3SMike Smith int
874b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
875b0cd2017SGleb Smirnoff     int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
876170db9c6SJohn Dyson {
877ce75f2c3SMike Smith 	vm_object_t object;
8789c83534dSPoul-Henning Kamp 	struct bufobj *bo;
8790bdb7528SDavid Greenman 	struct buf *bp;
880b0cd2017SGleb Smirnoff 	off_t foff;
881e48b82bdSGleb Smirnoff #ifdef INVARIANTS
882e48b82bdSGleb Smirnoff 	off_t blkno0;
883e48b82bdSGleb Smirnoff #endif
884756a5412SGleb Smirnoff 	int bsize, pagesperblock;
885b0cd2017SGleb Smirnoff 	int error, before, after, rbehind, rahead, poff, i;
886b0cd2017SGleb Smirnoff 	int bytecount, secmask;
887ce75f2c3SMike Smith 
8889c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
889b0cd2017SGleb Smirnoff 	    ("%s does not support devices", __func__));
890b0cd2017SGleb Smirnoff 
891abd80ddbSMateusz Guzik 	if (VN_IS_DOOMED(vp))
892eac91e32SKonstantin Belousov 		return (VM_PAGER_BAD);
8932c4488fcSJohn Dyson 
894eac91e32SKonstantin Belousov 	object = vp->v_object;
895b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(m[0]->pindex);
89626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
897b0cd2017SGleb Smirnoff 	pagesperblock = bsize / PAGE_SIZE;
898b0cd2017SGleb Smirnoff 
899b0cd2017SGleb Smirnoff 	KASSERT(foff < object->un_pager.vnp.vnp_size,
900b0cd2017SGleb Smirnoff 	    ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
901cd853791SKonstantin Belousov 	KASSERT(count <= atop(maxphys),
902b0cd2017SGleb Smirnoff 	    ("%s: requested %d pages", __func__, count));
903b0cd2017SGleb Smirnoff 
904b0cd2017SGleb Smirnoff 	/*
905b0cd2017SGleb Smirnoff 	 * The last page has valid blocks.  Invalid part can only
906b0cd2017SGleb Smirnoff 	 * exist at the end of file, and the page is made fully valid
907b0cd2017SGleb Smirnoff 	 * by zeroing in vm_pager_get_pages().
908b0cd2017SGleb Smirnoff 	 */
9090012f373SJeff Roberson 	if (!vm_page_none_valid(m[count - 1]) && --count == 0) {
910b0cd2017SGleb Smirnoff 		if (iodone != NULL)
911b0cd2017SGleb Smirnoff 			iodone(arg, m, 1, 0);
912b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
913b0cd2017SGleb Smirnoff 	}
914bbc0ec52SDavid Greenman 
915756a5412SGleb Smirnoff 	bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
916cd853791SKonstantin Belousov 	MPASS((bp->b_flags & B_MAXPHYS) != 0);
91773e9030eSGleb Smirnoff 
91826f9a767SRodney W. Grimes 	/*
919e122dfc1SGleb Smirnoff 	 * Get the underlying device blocks for the file with VOP_BMAP().
920e122dfc1SGleb Smirnoff 	 * If the file system doesn't support VOP_BMAP, use old way of
921e122dfc1SGleb Smirnoff 	 * getting pages via VOP_READ.
92226f9a767SRodney W. Grimes 	 */
923b0cd2017SGleb Smirnoff 	error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
9241de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
925756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
92689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
927b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
92883c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodein);
92983c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodepgsin);
930b0cd2017SGleb Smirnoff 			error = vnode_pager_input_old(object, m[i]);
931b0cd2017SGleb Smirnoff 			if (error)
932b0cd2017SGleb Smirnoff 				break;
933b0cd2017SGleb Smirnoff 		}
93489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
93552051abcSAlan Cox 		return (error);
9361de11f1aSAlan Cox 	} else if (error != 0) {
937756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
9381de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
939b0cd2017SGleb Smirnoff 	}
940bbc0ec52SDavid Greenman 
94126f9a767SRodney W. Grimes 	/*
942b0cd2017SGleb Smirnoff 	 * If the file system supports BMAP, but blocksize is smaller
943b0cd2017SGleb Smirnoff 	 * than a page size, then use special small filesystem code.
94426f9a767SRodney W. Grimes 	 */
945b0cd2017SGleb Smirnoff 	if (pagesperblock == 0) {
946756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
947b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
94883c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodein);
94983c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodepgsin);
950b0cd2017SGleb Smirnoff 			error = vnode_pager_input_smlfs(object, m[i]);
951b0cd2017SGleb Smirnoff 			if (error)
952b0cd2017SGleb Smirnoff 				break;
953b0cd2017SGleb Smirnoff 		}
954b0cd2017SGleb Smirnoff 		return (error);
95526f9a767SRodney W. Grimes 	}
9568d17e694SJulian Elischer 
95726f9a767SRodney W. Grimes 	/*
958b0cd2017SGleb Smirnoff 	 * A sparse file can be encountered only for a single page request,
959763df3ecSPedro F. Giffuni 	 * which may not be preceded by call to vm_pager_haspage().
960a7fecb4dSAlan Cox 	 */
961b0cd2017SGleb Smirnoff 	if (bp->b_blkno == -1) {
962b0cd2017SGleb Smirnoff 		KASSERT(count == 1,
963b0cd2017SGleb Smirnoff 		    ("%s: array[%d] request to a sparse file %p", __func__,
964b0cd2017SGleb Smirnoff 		    count, vp));
965756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
966b0cd2017SGleb Smirnoff 		pmap_zero_page(m[0]);
967b0cd2017SGleb Smirnoff 		KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
968b0cd2017SGleb Smirnoff 		    __func__, m[0]));
9690012f373SJeff Roberson 		vm_page_valid(m[0]);
970f4f83da0SAlan Cox 		return (VM_PAGER_OK);
971b0cd2017SGleb Smirnoff 	}
972b0cd2017SGleb Smirnoff 
973e48b82bdSGleb Smirnoff #ifdef INVARIANTS
974e48b82bdSGleb Smirnoff 	blkno0 = bp->b_blkno;
975e48b82bdSGleb Smirnoff #endif
976b0cd2017SGleb Smirnoff 	bp->b_blkno += (foff % bsize) / DEV_BSIZE;
977b0cd2017SGleb Smirnoff 
978b0cd2017SGleb Smirnoff 	/* Recalculate blocks available after/before to pages. */
979b0cd2017SGleb Smirnoff 	poff = (foff % bsize) / PAGE_SIZE;
980b0cd2017SGleb Smirnoff 	before *= pagesperblock;
981b0cd2017SGleb Smirnoff 	before += poff;
982b0cd2017SGleb Smirnoff 	after *= pagesperblock;
983b0cd2017SGleb Smirnoff 	after += pagesperblock - (poff + 1);
984b0cd2017SGleb Smirnoff 	if (m[0]->pindex + after >= object->size)
985b0cd2017SGleb Smirnoff 		after = object->size - 1 - m[0]->pindex;
986b0cd2017SGleb Smirnoff 	KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
987b0cd2017SGleb Smirnoff 	    __func__, count, after + 1));
988b0cd2017SGleb Smirnoff 	after -= count - 1;
989b0cd2017SGleb Smirnoff 
990b0cd2017SGleb Smirnoff 	/* Trim requested rbehind/rahead to possible values. */
991b0cd2017SGleb Smirnoff 	rbehind = a_rbehind ? *a_rbehind : 0;
992b0cd2017SGleb Smirnoff 	rahead = a_rahead ? *a_rahead : 0;
993b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, before);
994b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, m[0]->pindex);
995b0cd2017SGleb Smirnoff 	rahead = min(rahead, after);
996b0cd2017SGleb Smirnoff 	rahead = min(rahead, object->size - m[count - 1]->pindex);
997e48b82bdSGleb Smirnoff 	/*
998e48b82bdSGleb Smirnoff 	 * Check that total amount of pages fit into buf.  Trim rbehind and
999e48b82bdSGleb Smirnoff 	 * rahead evenly if not.
1000e48b82bdSGleb Smirnoff 	 */
1001cd853791SKonstantin Belousov 	if (rbehind + rahead + count > atop(maxphys)) {
1002e48b82bdSGleb Smirnoff 		int trim, sum;
1003e48b82bdSGleb Smirnoff 
1004cd853791SKonstantin Belousov 		trim = rbehind + rahead + count - atop(maxphys) + 1;
1005e48b82bdSGleb Smirnoff 		sum = rbehind + rahead;
1006e48b82bdSGleb Smirnoff 		if (rbehind == before) {
1007e48b82bdSGleb Smirnoff 			/* Roundup rbehind trim to block size. */
1008e48b82bdSGleb Smirnoff 			rbehind -= roundup(trim * rbehind / sum, pagesperblock);
1009e48b82bdSGleb Smirnoff 			if (rbehind < 0)
1010e48b82bdSGleb Smirnoff 				rbehind = 0;
1011e48b82bdSGleb Smirnoff 		} else
1012e48b82bdSGleb Smirnoff 			rbehind -= trim * rbehind / sum;
1013e48b82bdSGleb Smirnoff 		rahead -= trim * rahead / sum;
1014e48b82bdSGleb Smirnoff 	}
1015cd853791SKonstantin Belousov 	KASSERT(rbehind + rahead + count <= atop(maxphys),
1016cd853791SKonstantin Belousov 	    ("%s: behind %d ahead %d count %d maxphys %lu", __func__,
1017cd853791SKonstantin Belousov 	    rbehind, rahead, count, maxphys));
1018b0cd2017SGleb Smirnoff 
1019b0cd2017SGleb Smirnoff 	/*
1020b0cd2017SGleb Smirnoff 	 * Fill in the bp->b_pages[] array with requested and optional
1021b0cd2017SGleb Smirnoff 	 * read behind or read ahead pages.  Read behind pages are looked
1022b0cd2017SGleb Smirnoff 	 * up in a backward direction, down to a first cached page.  Same
1023b0cd2017SGleb Smirnoff 	 * for read ahead pages, but there is no need to shift the array
1024b0cd2017SGleb Smirnoff 	 * in case of encountering a cached page.
1025b0cd2017SGleb Smirnoff 	 */
1026b0cd2017SGleb Smirnoff 	i = bp->b_npages = 0;
1027b0cd2017SGleb Smirnoff 	if (rbehind) {
1028b0cd2017SGleb Smirnoff 		vm_pindex_t startpindex, tpindex;
1029b0cd2017SGleb Smirnoff 		vm_page_t p;
1030b0cd2017SGleb Smirnoff 
1031a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
1032b0cd2017SGleb Smirnoff 		startpindex = m[0]->pindex - rbehind;
1033b0cd2017SGleb Smirnoff 		if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
1034b0cd2017SGleb Smirnoff 		    p->pindex >= startpindex)
1035b0cd2017SGleb Smirnoff 			startpindex = p->pindex + 1;
1036b0cd2017SGleb Smirnoff 
1037b0cd2017SGleb Smirnoff 		/* tpindex is unsigned; beware of numeric underflow. */
1038b0cd2017SGleb Smirnoff 		for (tpindex = m[0]->pindex - 1;
1039b0cd2017SGleb Smirnoff 		    tpindex >= startpindex && tpindex < m[0]->pindex;
1040b0cd2017SGleb Smirnoff 		    tpindex--, i++) {
10417667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1042b0cd2017SGleb Smirnoff 			if (p == NULL) {
1043b0cd2017SGleb Smirnoff 				/* Shift the array. */
1044b0cd2017SGleb Smirnoff 				for (int j = 0; j < i; j++)
1045b0cd2017SGleb Smirnoff 					bp->b_pages[j] = bp->b_pages[j +
1046b0cd2017SGleb Smirnoff 					    tpindex + 1 - startpindex];
1047b0cd2017SGleb Smirnoff 				break;
1048b0cd2017SGleb Smirnoff 			}
1049b0cd2017SGleb Smirnoff 			bp->b_pages[tpindex - startpindex] = p;
1050a7fecb4dSAlan Cox 		}
10510bdb7528SDavid Greenman 
1052b0cd2017SGleb Smirnoff 		bp->b_pgbefore = i;
1053b0cd2017SGleb Smirnoff 		bp->b_npages += i;
1054b0cd2017SGleb Smirnoff 		bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
1055b0cd2017SGleb Smirnoff 	} else
1056b0cd2017SGleb Smirnoff 		bp->b_pgbefore = 0;
1057b0cd2017SGleb Smirnoff 
1058b0cd2017SGleb Smirnoff 	/* Requested pages. */
1059b0cd2017SGleb Smirnoff 	for (int j = 0; j < count; j++, i++)
1060b0cd2017SGleb Smirnoff 		bp->b_pages[i] = m[j];
1061b0cd2017SGleb Smirnoff 	bp->b_npages += count;
1062b0cd2017SGleb Smirnoff 
1063b0cd2017SGleb Smirnoff 	if (rahead) {
1064b0cd2017SGleb Smirnoff 		vm_pindex_t endpindex, tpindex;
1065b0cd2017SGleb Smirnoff 		vm_page_t p;
1066b0cd2017SGleb Smirnoff 
1067b0cd2017SGleb Smirnoff 		if (!VM_OBJECT_WOWNED(object))
1068eac91e32SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
1069b0cd2017SGleb Smirnoff 		endpindex = m[count - 1]->pindex + rahead + 1;
1070b0cd2017SGleb Smirnoff 		if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
1071b0cd2017SGleb Smirnoff 		    p->pindex < endpindex)
1072b0cd2017SGleb Smirnoff 			endpindex = p->pindex;
1073b0cd2017SGleb Smirnoff 		if (endpindex > object->size)
1074b0cd2017SGleb Smirnoff 			endpindex = object->size;
1075b0cd2017SGleb Smirnoff 
1076b0cd2017SGleb Smirnoff 		for (tpindex = m[count - 1]->pindex + 1;
1077b0cd2017SGleb Smirnoff 		    tpindex < endpindex; i++, tpindex++) {
10787667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1079b0cd2017SGleb Smirnoff 			if (p == NULL)
1080b0cd2017SGleb Smirnoff 				break;
1081b0cd2017SGleb Smirnoff 			bp->b_pages[i] = p;
1082eac91e32SKonstantin Belousov 		}
1083b0cd2017SGleb Smirnoff 
1084b0cd2017SGleb Smirnoff 		bp->b_pgafter = i - bp->b_npages;
1085b0cd2017SGleb Smirnoff 		bp->b_npages = i;
1086b0cd2017SGleb Smirnoff 	} else
1087b0cd2017SGleb Smirnoff 		bp->b_pgafter = 0;
1088b0cd2017SGleb Smirnoff 
1089b0cd2017SGleb Smirnoff 	if (VM_OBJECT_WOWNED(object))
1090eac91e32SKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
1091b0cd2017SGleb Smirnoff 
1092b0cd2017SGleb Smirnoff 	/* Report back actual behind/ahead read. */
1093b0cd2017SGleb Smirnoff 	if (a_rbehind)
1094b0cd2017SGleb Smirnoff 		*a_rbehind = bp->b_pgbefore;
1095b0cd2017SGleb Smirnoff 	if (a_rahead)
1096b0cd2017SGleb Smirnoff 		*a_rahead = bp->b_pgafter;
1097b0cd2017SGleb Smirnoff 
1098e48b82bdSGleb Smirnoff #ifdef INVARIANTS
1099cd853791SKonstantin Belousov 	KASSERT(bp->b_npages <= atop(maxphys),
1100b0cd2017SGleb Smirnoff 	    ("%s: buf %p overflowed", __func__, bp));
11014f56243aSGleb Smirnoff 	for (int j = 1, prev = 0; j < bp->b_npages; j++) {
11021e0c121fSGleb Smirnoff 		if (bp->b_pages[j] == bogus_page)
11031e0c121fSGleb Smirnoff 			continue;
11041e0c121fSGleb Smirnoff 		KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
11051e0c121fSGleb Smirnoff 		    j - prev, ("%s: pages array not consecutive, bp %p",
11061e0c121fSGleb Smirnoff 		     __func__, bp));
11071e0c121fSGleb Smirnoff 		prev = j;
11081e0c121fSGleb Smirnoff 	}
1109e48b82bdSGleb Smirnoff #endif
1110eac91e32SKonstantin Belousov 
11110d94caffSDavid Greenman 	/*
1112b0cd2017SGleb Smirnoff 	 * Recalculate first offset and bytecount with regards to read behind.
1113b0cd2017SGleb Smirnoff 	 * Truncate bytecount to vnode real size and round up physical size
1114b0cd2017SGleb Smirnoff 	 * for real devices.
111526f9a767SRodney W. Grimes 	 */
1116b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1117b0cd2017SGleb Smirnoff 	bytecount = bp->b_npages << PAGE_SHIFT;
1118b0cd2017SGleb Smirnoff 	if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
1119b0cd2017SGleb Smirnoff 		bytecount = object->un_pager.vnp.vnp_size - foff;
1120eac91e32SKonstantin Belousov 	secmask = bo->bo_bsize - 1;
11216229cc50SPoul-Henning Kamp 	KASSERT(secmask < PAGE_SIZE && secmask > 0,
1122b0cd2017SGleb Smirnoff 	    ("%s: sector size %d too large", __func__, secmask + 1));
1123b0cd2017SGleb Smirnoff 	bytecount = (bytecount + secmask) & ~secmask;
112426f9a767SRodney W. Grimes 
112526f9a767SRodney W. Grimes 	/*
1126b0cd2017SGleb Smirnoff 	 * And map the pages to be read into the kva, if the filesystem
11276ce697dcSKonstantin Belousov 	 * requires mapped buffers.
112826f9a767SRodney W. Grimes 	 */
11292a5eef69SGleb Smirnoff 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
11306ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
11316ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
11326ce697dcSKonstantin Belousov 		bp->b_offset = 0;
1133fade8dd7SJeff Roberson 	} else {
1134fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
1135b0cd2017SGleb Smirnoff 		pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1136fade8dd7SJeff Roberson 	}
113726f9a767SRodney W. Grimes 
1138b0cd2017SGleb Smirnoff 	/* Build a minimal buffer header. */
113921144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
1140bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1141bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1142a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
1143a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
11449c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
11451faacf5dSKirk McKusick 	bp->b_vp = vp;
1146b0cd2017SGleb Smirnoff 	bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
11472c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
1148e48b82bdSGleb Smirnoff 	KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1149e48b82bdSGleb Smirnoff 	    (blkno0 - bp->b_blkno) * DEV_BSIZE +
1150e48b82bdSGleb Smirnoff 	    IDX_TO_OFF(m[0]->pindex) % bsize,
1151e48b82bdSGleb Smirnoff 	    ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1152e48b82bdSGleb Smirnoff 	    "blkno0 %ju b_blkno %ju", bsize,
1153e48b82bdSGleb Smirnoff 	    (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1154e48b82bdSGleb Smirnoff 	    (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
115590effb23SGleb Smirnoff 
1156b0cd2017SGleb Smirnoff 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
115783c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodein);
115883c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1159b0cd2017SGleb Smirnoff 
116090effb23SGleb Smirnoff 	if (iodone != NULL) { /* async */
1161b0cd2017SGleb Smirnoff 		bp->b_pgiodone = iodone;
116290effb23SGleb Smirnoff 		bp->b_caller1 = arg;
116390effb23SGleb Smirnoff 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
116490effb23SGleb Smirnoff 		bp->b_flags |= B_ASYNC;
116590effb23SGleb Smirnoff 		BUF_KERNPROC(bp);
1166b792bebeSPoul-Henning Kamp 		bstrategy(bp);
1167b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
116890effb23SGleb Smirnoff 	} else {
116990effb23SGleb Smirnoff 		bp->b_iodone = bdone;
117090effb23SGleb Smirnoff 		bstrategy(bp);
11716a4b5823SPoul-Henning Kamp 		bwait(bp, PVM, "vnread");
117290effb23SGleb Smirnoff 		error = vnode_pager_generic_getpages_done(bp);
11731bb5ad63SGleb Smirnoff 		for (i = 0; i < bp->b_npages; i++)
11746ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
11751faacf5dSKirk McKusick 		bp->b_vp = NULL;
11769c83534dSPoul-Henning Kamp 		pbrelbo(bp);
1177756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
117890effb23SGleb Smirnoff 		return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
117990effb23SGleb Smirnoff 	}
1180b0cd2017SGleb Smirnoff }
118190effb23SGleb Smirnoff 
118290effb23SGleb Smirnoff static void
118390effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp)
118490effb23SGleb Smirnoff {
118590effb23SGleb Smirnoff 	int error;
118690effb23SGleb Smirnoff 
118790effb23SGleb Smirnoff 	error = vnode_pager_generic_getpages_done(bp);
1188b0cd2017SGleb Smirnoff 	/* Run the iodone upon the requested range. */
1189b0cd2017SGleb Smirnoff 	bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1190b0cd2017SGleb Smirnoff 	    bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
119190effb23SGleb Smirnoff 	for (int i = 0; i < bp->b_npages; i++)
119290effb23SGleb Smirnoff 		bp->b_pages[i] = NULL;
119390effb23SGleb Smirnoff 	bp->b_vp = NULL;
119490effb23SGleb Smirnoff 	pbrelbo(bp);
1195756a5412SGleb Smirnoff 	uma_zfree(vnode_pbuf_zone, bp);
119690effb23SGleb Smirnoff }
119790effb23SGleb Smirnoff 
119890effb23SGleb Smirnoff static int
119990effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp)
120090effb23SGleb Smirnoff {
120190effb23SGleb Smirnoff 	vm_object_t object;
120290effb23SGleb Smirnoff 	off_t tfoff, nextoff;
120390effb23SGleb Smirnoff 	int i, error;
120490effb23SGleb Smirnoff 
1205cafbf0c6SWarner Losh 	KASSERT((bp->b_ioflags & BIO_ERROR) == 0 || bp->b_error != 0,
1206cafbf0c6SWarner Losh 	    ("%s: buf error but b_error == 0\n", __func__));
1207cafbf0c6SWarner Losh 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? bp->b_error : 0;
120890effb23SGleb Smirnoff 	object = bp->b_vp->v_object;
120990effb23SGleb Smirnoff 
121028f957b8SKonstantin Belousov 	runningbufwakeup(bp);
121128f957b8SKonstantin Belousov 
121290effb23SGleb Smirnoff 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1213fade8dd7SJeff Roberson 		if (!buf_mapped(bp)) {
1214fade8dd7SJeff Roberson 			bp->b_data = bp->b_kvabase;
1215fade8dd7SJeff Roberson 			pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
121690effb23SGleb Smirnoff 			    bp->b_npages);
121790effb23SGleb Smirnoff 		}
1218fade8dd7SJeff Roberson 		bzero(bp->b_data + bp->b_bcount,
121990effb23SGleb Smirnoff 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
122090effb23SGleb Smirnoff 	}
1221fade8dd7SJeff Roberson 	if (buf_mapped(bp)) {
1222fade8dd7SJeff Roberson 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1223fade8dd7SJeff Roberson 		bp->b_data = unmapped_buf;
122490effb23SGleb Smirnoff 	}
122526f9a767SRodney W. Grimes 
12261bd12a3bSChuck Silvers 	/*
12271bd12a3bSChuck Silvers 	 * If the read failed, we must free any read ahead/behind pages here.
12281bd12a3bSChuck Silvers 	 * The requested pages are freed by the caller (for sync requests)
12291bd12a3bSChuck Silvers 	 * or by the bp->b_pgiodone callback (for async requests).
12301bd12a3bSChuck Silvers 	 */
12311bd12a3bSChuck Silvers 	if (error != 0) {
12321bd12a3bSChuck Silvers 		VM_OBJECT_WLOCK(object);
12331bd12a3bSChuck Silvers 		for (i = 0; i < bp->b_pgbefore; i++)
12341bd12a3bSChuck Silvers 			vm_page_free_invalid(bp->b_pages[i]);
12351bd12a3bSChuck Silvers 		for (i = bp->b_npages - bp->b_pgafter; i < bp->b_npages; i++)
12361bd12a3bSChuck Silvers 			vm_page_free_invalid(bp->b_pages[i]);
12371bd12a3bSChuck Silvers 		VM_OBJECT_WUNLOCK(object);
12381bd12a3bSChuck Silvers 		return (error);
12391bd12a3bSChuck Silvers 	}
12401bd12a3bSChuck Silvers 
12417f935055SJeff Roberson 	/* Read lock to protect size. */
12427f935055SJeff Roberson 	VM_OBJECT_RLOCK(object);
124390effb23SGleb Smirnoff 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
124490effb23SGleb Smirnoff 	    i < bp->b_npages; i++, tfoff = nextoff) {
12458f9110f6SJohn Dyson 		vm_page_t mt;
12468f9110f6SJohn Dyson 
12478f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
124890effb23SGleb Smirnoff 		mt = bp->b_pages[i];
12492f81c92eSMark Johnston 		if (mt == bogus_page)
12502f81c92eSMark Johnston 			continue;
12518f9110f6SJohn Dyson 
125254746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
12538d17e694SJulian Elischer 			/*
12548d17e694SJulian Elischer 			 * Read filled up entire page.
12558d17e694SJulian Elischer 			 */
12560012f373SJeff Roberson 			vm_page_valid(mt);
1257016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
125879f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
1259016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
126079f0deb9SGleb Smirnoff 			    ("%s: page %p is mapped", __func__, mt));
12618f9110f6SJohn Dyson 		} else {
12628d17e694SJulian Elischer 			/*
126342eb4108SAlan Cox 			 * Read did not fill up entire page.
12648d17e694SJulian Elischer 			 *
1265c3dbadc1SChuck Silvers 			 * Currently we do not set the entire page valid,
1266c3dbadc1SChuck Silvers 			 * we just try to clear the piece that we couldn't
1267c3dbadc1SChuck Silvers 			 * read.
12688d17e694SJulian Elischer 			 */
1269dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
127054746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
127142eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
1272c3dbadc1SChuck Silvers 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
1273c3dbadc1SChuck Silvers 			    ("%s: page %p is dirty", __func__, mt));
12748f9110f6SJohn Dyson 		}
12758f9110f6SJohn Dyson 
1276b0cd2017SGleb Smirnoff 		if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1277b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
127803679e23SAlan Cox 	}
12797f935055SJeff Roberson 	VM_OBJECT_RUNLOCK(object);
128090effb23SGleb Smirnoff 
128190effb23SGleb Smirnoff 	return (error);
128226f9a767SRodney W. Grimes }
128326f9a767SRodney W. Grimes 
1284ce75f2c3SMike Smith /*
1285ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1286ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1287ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1288ce75f2c3SMike Smith  *
1289ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1290ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1291ce75f2c3SMike Smith  */
1292e4542174SMatthew Dillon static void
12937ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
129433cad9e9SKonstantin Belousov     int flags, int *rtvals)
1295170db9c6SJohn Dyson {
1296b8ebd99aSJohn Baldwin 	int rtval __diagused;
1297170db9c6SJohn Dyson 	struct vnode *vp;
129886ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1299ad980522SJohn Dyson 
13000e3cdf2cSAlan Cox 	/*
13010e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
13020e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
13030e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
13040e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
13050e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
13060e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
13070e3cdf2cSAlan Cox 	 *
13080e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
13090e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
13100e3cdf2cSAlan Cox 	 */
13110e3cdf2cSAlan Cox 
1312e2068d0bSJeff Roberson 	if (vm_page_count_min())
131333cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
13140e3cdf2cSAlan Cox 
13150e3cdf2cSAlan Cox 	/*
13160e3cdf2cSAlan Cox 	 * Call device-specific putpages function
13170e3cdf2cSAlan Cox 	 */
1318170db9c6SJohn Dyson 	vp = object->handle;
131989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
132033cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
132123955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
132223955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
132389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1324170db9c6SJohn Dyson }
1325170db9c6SJohn Dyson 
132605877a85SKonstantin Belousov static int
132705877a85SKonstantin Belousov vn_off2bidx(vm_ooffset_t offset)
132805877a85SKonstantin Belousov {
132905877a85SKonstantin Belousov 
133005877a85SKonstantin Belousov 	return ((offset & PAGE_MASK) / DEV_BSIZE);
133105877a85SKonstantin Belousov }
133205877a85SKonstantin Belousov 
133305877a85SKonstantin Belousov static bool
133405877a85SKonstantin Belousov vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
133505877a85SKonstantin Belousov {
133605877a85SKonstantin Belousov 
133705877a85SKonstantin Belousov 	KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
133805877a85SKonstantin Belousov 	    offset < IDX_TO_OFF(m->pindex + 1),
133905877a85SKonstantin Belousov 	    ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
134005877a85SKonstantin Belousov 	    (uintmax_t)offset));
134105877a85SKonstantin Belousov 	return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
134205877a85SKonstantin Belousov }
1343ce75f2c3SMike Smith 
134426f9a767SRodney W. Grimes /*
1345ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
13464491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
13472b6b0df7SMatthew Dillon  *
13482b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
1349763df3ecSPedro F. Giffuni  * clustering has already typically occurred, so in general we ask the
13502b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
13512b6b0df7SMatthew Dillon  * then delayed.
135226f9a767SRodney W. Grimes  */
1353ce75f2c3SMike Smith int
1354c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1355c46b90e9SAlan Cox     int flags, int *rtvals)
135626f9a767SRodney W. Grimes {
1357ce75f2c3SMike Smith 	vm_object_t object;
1358c46b90e9SAlan Cox 	vm_page_t m;
1359ed1a88a3SKonstantin Belousov 	vm_ooffset_t max_offset, next_offset, poffset, prev_offset;
1360f6b04d2bSDavid Greenman 	struct uio auio;
1361f6b04d2bSDavid Greenman 	struct iovec aiov;
136205877a85SKonstantin Belousov 	off_t prev_resid, wrsz;
1363e6c44f65SKonstantin Belousov 	int count, error, i, maxsize, ncount, pgoff, ppscheck;
136405877a85SKonstantin Belousov 	bool in_hole;
1365dd498befSPaul Saab 	static struct timeval lastfail;
1366dd498befSPaul Saab 	static int curfail;
136726f9a767SRodney W. Grimes 
1368ce75f2c3SMike Smith 	object = vp->v_object;
1369ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1370ce75f2c3SMike Smith 
137126f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1372031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
137326f9a767SRodney W. Grimes 
1374c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
1375e6c44f65SKonstantin Belousov 		printf("vnode_pager_generic_putpages: "
1376e6c44f65SKonstantin Belousov 		    "attempt to write meta-data 0x%jx(%lx)\n",
1377e6c44f65SKonstantin Belousov 		    (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1378f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1379e6c44f65SKonstantin Belousov 		return (VM_PAGER_BAD);
13800d94caffSDavid Greenman 	}
13810bdb7528SDavid Greenman 
1382f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1383f6b04d2bSDavid Greenman 	ncount = count;
138426f9a767SRodney W. Grimes 
1385c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
138600a6f47fSMatthew Dillon 
138700a6f47fSMatthew Dillon 	/*
138800a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
1389763df3ecSPedro F. Giffuni 	 * have to invalidate pages occurring beyond the file EOF.  However,
139000a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
139100a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
139200a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
139300a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1394efec381dSMark Johnston 	 * With the page busied we are free to fix up the dirty bits here.
13953ebeaf59SMatthew Dillon 	 *
13963ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
13973ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
139800a6f47fSMatthew Dillon 	 */
1399b3d4ab66SKonstantin Belousov 	VM_OBJECT_RLOCK(object);
1400a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
140100a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
1402a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1403aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
140400a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1405938cdc42SKonstantin Belousov 				pgoff = roundup2(pgoff, DEV_BSIZE);
1406938cdc42SKonstantin Belousov 
1407c46b90e9SAlan Cox 				/*
14087f935055SJeff Roberson 				 * If the page is busy and the following
1409c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1410c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1411c46b90e9SAlan Cox 				 * pmap operation.
1412c46b90e9SAlan Cox 				 */
1413c46b90e9SAlan Cox 				m = ma[ncount - 1];
1414c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
14156031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1416c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1417e6c44f65SKonstantin Belousov 				MPASS(m->dirty != 0);
1418c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1419c46b90e9SAlan Cox 				    pgoff);
142000a6f47fSMatthew Dillon 			}
142100a6f47fSMatthew Dillon 		} else {
142200a6f47fSMatthew Dillon 			maxsize = 0;
142300a6f47fSMatthew Dillon 			ncount = 0;
142400a6f47fSMatthew Dillon 		}
1425e6c44f65SKonstantin Belousov 		for (i = ncount; i < count; i++)
1426f6b04d2bSDavid Greenman 			rtvals[i] = VM_PAGER_BAD;
1427f6b04d2bSDavid Greenman 	}
14287f935055SJeff Roberson 	VM_OBJECT_RUNLOCK(object);
142926f9a767SRodney W. Grimes 
1430f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1431f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1432f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1433e6c44f65SKonstantin Belousov 	auio.uio_td = NULL;
1434ed1a88a3SKonstantin Belousov 	max_offset = roundup2(poffset + maxsize, DEV_BSIZE);
143505877a85SKonstantin Belousov 
1436ed1a88a3SKonstantin Belousov 	for (prev_offset = poffset; prev_offset < max_offset;) {
143705877a85SKonstantin Belousov 		/* Skip clean blocks. */
1438ed1a88a3SKonstantin Belousov 		for (in_hole = true; in_hole && prev_offset < max_offset;) {
143905877a85SKonstantin Belousov 			m = ma[OFF_TO_IDX(prev_offset - poffset)];
144005877a85SKonstantin Belousov 			for (i = vn_off2bidx(prev_offset);
144105877a85SKonstantin Belousov 			    i < sizeof(vm_page_bits_t) * NBBY &&
1442ed1a88a3SKonstantin Belousov 			    prev_offset < max_offset; i++) {
144305877a85SKonstantin Belousov 				if (vn_dirty_blk(m, prev_offset)) {
144405877a85SKonstantin Belousov 					in_hole = false;
144505877a85SKonstantin Belousov 					break;
144605877a85SKonstantin Belousov 				}
144705877a85SKonstantin Belousov 				prev_offset += DEV_BSIZE;
144805877a85SKonstantin Belousov 			}
144905877a85SKonstantin Belousov 		}
145005877a85SKonstantin Belousov 		if (in_hole)
145105877a85SKonstantin Belousov 			goto write_done;
145205877a85SKonstantin Belousov 
145305877a85SKonstantin Belousov 		/* Find longest run of dirty blocks. */
1454ed1a88a3SKonstantin Belousov 		for (next_offset = prev_offset; next_offset < max_offset;) {
145505877a85SKonstantin Belousov 			m = ma[OFF_TO_IDX(next_offset - poffset)];
145605877a85SKonstantin Belousov 			for (i = vn_off2bidx(next_offset);
145705877a85SKonstantin Belousov 			    i < sizeof(vm_page_bits_t) * NBBY &&
1458ed1a88a3SKonstantin Belousov 			    next_offset < max_offset; i++) {
145905877a85SKonstantin Belousov 				if (!vn_dirty_blk(m, next_offset))
146005877a85SKonstantin Belousov 					goto start_write;
146105877a85SKonstantin Belousov 				next_offset += DEV_BSIZE;
146205877a85SKonstantin Belousov 			}
146305877a85SKonstantin Belousov 		}
146405877a85SKonstantin Belousov start_write:
146505877a85SKonstantin Belousov 		if (next_offset > poffset + maxsize)
146605877a85SKonstantin Belousov 			next_offset = poffset + maxsize;
1467bdb46c21SKonstantin Belousov 		if (prev_offset == next_offset)
1468bdb46c21SKonstantin Belousov 			goto write_done;
146905877a85SKonstantin Belousov 
147005877a85SKonstantin Belousov 		/*
147105877a85SKonstantin Belousov 		 * Getting here requires finding a dirty block in the
147205877a85SKonstantin Belousov 		 * 'skip clean blocks' loop.
147305877a85SKonstantin Belousov 		 */
147405877a85SKonstantin Belousov 
147505877a85SKonstantin Belousov 		aiov.iov_base = NULL;
147605877a85SKonstantin Belousov 		auio.uio_iovcnt = 1;
147705877a85SKonstantin Belousov 		auio.uio_offset = prev_offset;
147805877a85SKonstantin Belousov 		prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
147905877a85SKonstantin Belousov 		    prev_offset;
148005877a85SKonstantin Belousov 		error = VOP_WRITE(vp, &auio,
148105877a85SKonstantin Belousov 		    vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
148205877a85SKonstantin Belousov 
148305877a85SKonstantin Belousov 		wrsz = prev_resid - auio.uio_resid;
148405877a85SKonstantin Belousov 		if (wrsz == 0) {
148505877a85SKonstantin Belousov 			if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
148605877a85SKonstantin Belousov 				vn_printf(vp, "vnode_pager_putpages: "
148705877a85SKonstantin Belousov 				    "zero-length write at %ju resid %zd\n",
148805877a85SKonstantin Belousov 				    auio.uio_offset, auio.uio_resid);
148905877a85SKonstantin Belousov 			}
149005877a85SKonstantin Belousov 			break;
149105877a85SKonstantin Belousov 		}
149205877a85SKonstantin Belousov 
149305877a85SKonstantin Belousov 		/* Adjust the starting offset for next iteration. */
149405877a85SKonstantin Belousov 		prev_offset += wrsz;
149505877a85SKonstantin Belousov 		MPASS(auio.uio_offset == prev_offset);
1496f6b04d2bSDavid Greenman 
14973dbb0ca6SKonstantin Belousov 		ppscheck = 0;
149805877a85SKonstantin Belousov 		if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
149905877a85SKonstantin Belousov 		    &curfail, 1)) != 0)
150005877a85SKonstantin Belousov 			vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
150105877a85SKonstantin Belousov 			    error);
1502e6c44f65SKonstantin Belousov 		if (auio.uio_resid != 0 && (ppscheck != 0 ||
1503e6c44f65SKonstantin Belousov 		    ppsratecheck(&lastfail, &curfail, 1) != 0))
150405877a85SKonstantin Belousov 			vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
150505877a85SKonstantin Belousov 			    "at %ju\n", auio.uio_resid,
150605877a85SKonstantin Belousov 			    (uintmax_t)ma[0]->pindex);
150705877a85SKonstantin Belousov 		if (error != 0 || auio.uio_resid != 0)
150805877a85SKonstantin Belousov 			break;
150905877a85SKonstantin Belousov 	}
151005877a85SKonstantin Belousov write_done:
151105877a85SKonstantin Belousov 	/* Mark completely processed pages. */
151205877a85SKonstantin Belousov 	for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
151326f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
151405877a85SKonstantin Belousov 	/* Mark partial EOF page. */
151505877a85SKonstantin Belousov 	if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
151605877a85SKonstantin Belousov 		rtvals[i++] = VM_PAGER_OK;
151705877a85SKonstantin Belousov 	/* Unwritten pages in range, free bonus if the page is clean. */
151805877a85SKonstantin Belousov 	for (; i < ncount; i++)
151905877a85SKonstantin Belousov 		rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
152005877a85SKonstantin Belousov 	VM_CNT_ADD(v_vnodepgsout, i);
152105877a85SKonstantin Belousov 	VM_CNT_INC(v_vnodeout);
1522e6c44f65SKonstantin Belousov 	return (rtvals[0]);
152326f9a767SRodney W. Grimes }
1524031ec8c1SKonstantin Belousov 
152565b9599aSKonstantin Belousov int
152665b9599aSKonstantin Belousov vnode_pager_putpages_ioflags(int pager_flags)
152765b9599aSKonstantin Belousov {
152865b9599aSKonstantin Belousov 	int ioflags;
152965b9599aSKonstantin Belousov 
153065b9599aSKonstantin Belousov 	/*
153165b9599aSKonstantin Belousov 	 * Pageouts are already clustered, use IO_ASYNC to force a
153265b9599aSKonstantin Belousov 	 * bawrite() rather then a bdwrite() to prevent paging I/O
153365b9599aSKonstantin Belousov 	 * from saturating the buffer cache.  Dummy-up the sequential
153465b9599aSKonstantin Belousov 	 * heuristic to cause large ranges to cluster.  If neither
153565b9599aSKonstantin Belousov 	 * IO_SYNC or IO_ASYNC is set, the system decides how to
153665b9599aSKonstantin Belousov 	 * cluster.
153765b9599aSKonstantin Belousov 	 */
153865b9599aSKonstantin Belousov 	ioflags = IO_VMIO;
153965b9599aSKonstantin Belousov 	if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0)
154065b9599aSKonstantin Belousov 		ioflags |= IO_SYNC;
154165b9599aSKonstantin Belousov 	else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0)
154265b9599aSKonstantin Belousov 		ioflags |= IO_ASYNC;
154365b9599aSKonstantin Belousov 	ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0;
154465b9599aSKonstantin Belousov 	ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0;
154565b9599aSKonstantin Belousov 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
154665b9599aSKonstantin Belousov 	return (ioflags);
154765b9599aSKonstantin Belousov }
154865b9599aSKonstantin Belousov 
1549555b7bb4SKonstantin Belousov /*
1550555b7bb4SKonstantin Belousov  * vnode_pager_undirty_pages().
1551555b7bb4SKonstantin Belousov  *
1552555b7bb4SKonstantin Belousov  * A helper to mark pages as clean after pageout that was possibly
1553555b7bb4SKonstantin Belousov  * done with a short write.  The lpos argument specifies the page run
1554555b7bb4SKonstantin Belousov  * length in bytes, and the written argument specifies how many bytes
1555555b7bb4SKonstantin Belousov  * were actually written.  eof is the offset past the last valid byte
1556555b7bb4SKonstantin Belousov  * in the vnode using the absolute file position of the first byte in
1557555b7bb4SKonstantin Belousov  * the run as the base from which it is computed.
1558555b7bb4SKonstantin Belousov  */
1559031ec8c1SKonstantin Belousov void
1560555b7bb4SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof,
1561555b7bb4SKonstantin Belousov     int lpos)
1562031ec8c1SKonstantin Belousov {
1563555b7bb4SKonstantin Belousov 	int i, pos, pos_devb;
1564031ec8c1SKonstantin Belousov 
1565555b7bb4SKonstantin Belousov 	if (written == 0 && eof >= lpos)
15669d17da3bSKonstantin Belousov 		return;
1567031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1568031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1569031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1570031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1571031ec8c1SKonstantin Belousov 		} else {
1572031ec8c1SKonstantin Belousov 			/* Partially written page. */
1573031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1574031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1575031ec8c1SKonstantin Belousov 		}
1576031ec8c1SKonstantin Belousov 	}
1577555b7bb4SKonstantin Belousov 	if (eof >= lpos) /* avoid truncation */
15787f935055SJeff Roberson 		return;
1579555b7bb4SKonstantin Belousov 	for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1580555b7bb4SKonstantin Belousov 		if (pos != trunc_page(pos)) {
1581555b7bb4SKonstantin Belousov 			/*
1582555b7bb4SKonstantin Belousov 			 * The page contains the last valid byte in
1583555b7bb4SKonstantin Belousov 			 * the vnode, mark the rest of the page as
1584555b7bb4SKonstantin Belousov 			 * clean, potentially making the whole page
1585555b7bb4SKonstantin Belousov 			 * clean.
1586555b7bb4SKonstantin Belousov 			 */
1587555b7bb4SKonstantin Belousov 			pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1588555b7bb4SKonstantin Belousov 			vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1589555b7bb4SKonstantin Belousov 			    pos_devb);
1590555b7bb4SKonstantin Belousov 
1591555b7bb4SKonstantin Belousov 			/*
1592555b7bb4SKonstantin Belousov 			 * If the page was cleaned, report the pageout
1593555b7bb4SKonstantin Belousov 			 * on it as successful.  msync() no longer
1594555b7bb4SKonstantin Belousov 			 * needs to write out the page, endlessly
1595555b7bb4SKonstantin Belousov 			 * creating write requests and dirty buffers.
1596555b7bb4SKonstantin Belousov 			 */
1597555b7bb4SKonstantin Belousov 			if (ma[i]->dirty == 0)
1598555b7bb4SKonstantin Belousov 				rtvals[i] = VM_PAGER_OK;
1599555b7bb4SKonstantin Belousov 
1600555b7bb4SKonstantin Belousov 			pos = round_page(pos);
1601555b7bb4SKonstantin Belousov 		} else {
1602555b7bb4SKonstantin Belousov 			/* vm_pageout_flush() clears dirty */
1603555b7bb4SKonstantin Belousov 			rtvals[i] = VM_PAGER_BAD;
1604555b7bb4SKonstantin Belousov 			pos += PAGE_SIZE;
1605555b7bb4SKonstantin Belousov 		}
1606555b7bb4SKonstantin Belousov 	}
1607031ec8c1SKonstantin Belousov }
160884110e7eSKonstantin Belousov 
1609fe7bcbafSKyle Evans static void
161084110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
161184110e7eSKonstantin Belousov     vm_offset_t end)
161284110e7eSKonstantin Belousov {
161384110e7eSKonstantin Belousov 	struct vnode *vp;
161484110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
161584110e7eSKonstantin Belousov 
161689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
161784110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
161889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
161984110e7eSKonstantin Belousov 		return;
162084110e7eSKonstantin Belousov 	}
162184110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
162284110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
162384110e7eSKonstantin Belousov 	vp = object->handle;
162484110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
162578022527SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "v_writecount inc");
162678022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, 1);
1627b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1628b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
162984110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
163078022527SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "v_writecount dec");
163178022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
1632b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1633b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
163484110e7eSKonstantin Belousov 	}
163589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
163684110e7eSKonstantin Belousov }
163784110e7eSKonstantin Belousov 
1638fe7bcbafSKyle Evans static void
163984110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
164084110e7eSKonstantin Belousov     vm_offset_t end)
164184110e7eSKonstantin Belousov {
164284110e7eSKonstantin Belousov 	struct vnode *vp;
164384110e7eSKonstantin Belousov 	struct mount *mp;
164484110e7eSKonstantin Belousov 	vm_offset_t inc;
164584110e7eSKonstantin Belousov 
164689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
164784110e7eSKonstantin Belousov 
164884110e7eSKonstantin Belousov 	/*
164984110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
165084110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
165184110e7eSKonstantin Belousov 	 */
165284110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
165389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
165484110e7eSKonstantin Belousov 		return;
165584110e7eSKonstantin Belousov 	}
165684110e7eSKonstantin Belousov 
165784110e7eSKonstantin Belousov 	/*
165884110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
165984110e7eSKonstantin Belousov 	 * zero.
166084110e7eSKonstantin Belousov 	 */
166184110e7eSKonstantin Belousov 	inc = end - start;
166284110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
166384110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
166489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
166584110e7eSKonstantin Belousov 		return;
166684110e7eSKonstantin Belousov 	}
166784110e7eSKonstantin Belousov 
166884110e7eSKonstantin Belousov 	vp = object->handle;
166984110e7eSKonstantin Belousov 	vhold(vp);
167089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
167184110e7eSKonstantin Belousov 	mp = NULL;
167284110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
167378022527SKonstantin Belousov 	vn_lock(vp, LK_SHARED | LK_RETRY);
167484110e7eSKonstantin Belousov 
167584110e7eSKonstantin Belousov 	/*
167684110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
167784110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
167884110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
167984110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
168084110e7eSKonstantin Belousov 	 */
168184110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
1682b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
168384110e7eSKonstantin Belousov 	vdrop(vp);
168484110e7eSKonstantin Belousov 	if (mp != NULL)
168584110e7eSKonstantin Belousov 		vn_finished_write(mp);
168684110e7eSKonstantin Belousov }
1687192112b7SKonstantin Belousov 
1688192112b7SKonstantin Belousov static void
1689192112b7SKonstantin Belousov vnode_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp)
1690192112b7SKonstantin Belousov {
1691192112b7SKonstantin Belousov 	*vpp = object->handle;
1692192112b7SKonstantin Belousov }
1693*b068bb09SKonstantin Belousov 
1694*b068bb09SKonstantin Belousov static void
1695*b068bb09SKonstantin Belousov vnode_pager_clean1(struct vnode *vp, int sync_flags)
1696*b068bb09SKonstantin Belousov {
1697*b068bb09SKonstantin Belousov 	struct vm_object *obj;
1698*b068bb09SKonstantin Belousov 
1699*b068bb09SKonstantin Belousov 	ASSERT_VOP_LOCKED(vp, "needs lock for writes");
1700*b068bb09SKonstantin Belousov 	obj = vp->v_object;
1701*b068bb09SKonstantin Belousov 	if (obj == NULL)
1702*b068bb09SKonstantin Belousov 		return;
1703*b068bb09SKonstantin Belousov 
1704*b068bb09SKonstantin Belousov 	VM_OBJECT_WLOCK(obj);
1705*b068bb09SKonstantin Belousov 	vm_object_page_clean(obj, 0, 0, sync_flags);
1706*b068bb09SKonstantin Belousov 	VM_OBJECT_WUNLOCK(obj);
1707*b068bb09SKonstantin Belousov }
1708*b068bb09SKonstantin Belousov 
1709*b068bb09SKonstantin Belousov void
1710*b068bb09SKonstantin Belousov vnode_pager_clean_sync(struct vnode *vp)
1711*b068bb09SKonstantin Belousov {
1712*b068bb09SKonstantin Belousov 	vnode_pager_clean1(vp, OBJPC_SYNC);
1713*b068bb09SKonstantin Belousov }
1714*b068bb09SKonstantin Belousov 
1715*b068bb09SKonstantin Belousov void
1716*b068bb09SKonstantin Belousov vnode_pager_clean_async(struct vnode *vp)
1717*b068bb09SKonstantin Belousov {
1718*b068bb09SKonstantin Belousov 	vnode_pager_clean1(vp, 0);
1719*b068bb09SKonstantin Belousov }
1720