xref: /freebsd/sys/vm/vnode_pager.c (revision 6470c8d3db6d04a44682b6cc898d446dcccc0ef0)
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  *
4226f9a767SRodney W. Grimes  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes 
45df8bae1dSRodney W. Grimes /*
46df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
47df8bae1dSRodney W. Grimes  */
48df8bae1dSRodney W. Grimes 
4926f9a767SRodney W. Grimes /*
5026f9a767SRodney W. Grimes  * TODO:
5124a1cce3SDavid Greenman  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
52f6b04d2bSDavid Greenman  *	greatly re-simplify the vnode_pager.
5326f9a767SRodney W. Grimes  */
5426f9a767SRodney W. Grimes 
55874651b1SDavid E. O'Brien #include <sys/cdefs.h>
56874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
57874651b1SDavid E. O'Brien 
583d653db0SAlan Cox #include "opt_vm.h"
593d653db0SAlan Cox 
60df8bae1dSRodney W. Grimes #include <sys/param.h>
61756a5412SGleb Smirnoff #include <sys/kernel.h>
62df8bae1dSRodney W. Grimes #include <sys/systm.h>
63e5818a53SJeff Roberson #include <sys/sysctl.h>
64df8bae1dSRodney W. Grimes #include <sys/proc.h>
65df8bae1dSRodney W. Grimes #include <sys/vnode.h>
66df8bae1dSRodney W. Grimes #include <sys/mount.h>
679626b608SPoul-Henning Kamp #include <sys/bio.h>
6824a1cce3SDavid Greenman #include <sys/buf.h>
69efeaf95aSDavid Greenman #include <sys/vmmeter.h>
70daec9284SConrad Meyer #include <sys/ktr.h>
71d07a6d3fSPoul-Henning Kamp #include <sys/limits.h>
7224579ca1SMatthew Dillon #include <sys/conf.h>
7389f6b863SAttilio Rao #include <sys/rwlock.h>
749e0ddbd0SAlan Cox #include <sys/sf_buf.h>
75e5818a53SJeff Roberson #include <sys/domainset.h>
76df8bae1dSRodney W. Grimes 
774f12e0acSSuleiman Souhlal #include <machine/atomic.h>
784f12e0acSSuleiman Souhlal 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
801c771f92SKonstantin Belousov #include <vm/vm_param.h>
81efeaf95aSDavid Greenman #include <vm/vm_object.h>
82df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8324a1cce3SDavid Greenman #include <vm/vm_pager.h>
841efb74fbSJohn Dyson #include <vm/vm_map.h>
85df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
86efeaf95aSDavid Greenman #include <vm/vm_extern.h>
87756a5412SGleb Smirnoff #include <vm/uma.h>
88df8bae1dSRodney W. Grimes 
89bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
90bff76343SAlan Cox     daddr_t *rtaddress, int *run);
9111caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
9211caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
9311caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
94b0cd2017SGleb Smirnoff static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
95b0cd2017SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
96b0cd2017SGleb Smirnoff     int *, vop_getpages_iodone_t, void *);
9733cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
9811caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
993364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
1003364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *cred);
10190effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *);
10290effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *);
1030b8253a7SBruce Evans 
104df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
1054e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
1064e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
1074e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
10890effb23SGleb Smirnoff 	.pgo_getpages_async = vnode_pager_getpages_async,
1094e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
1104e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
111df8bae1dSRodney W. Grimes };
112df8bae1dSRodney W. Grimes 
113e5818a53SJeff Roberson static struct domainset *vnode_domainset = NULL;
114e5818a53SJeff Roberson 
115e5818a53SJeff Roberson SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset, CTLTYPE_STRING | CTLFLAG_RW,
116e5818a53SJeff Roberson     &vnode_domainset, 0, sysctl_handle_domainset, "A",
117e5818a53SJeff Roberson     "Default vnode NUMA policy");
118e5818a53SJeff Roberson 
11966fb0b1aSGleb Smirnoff static int nvnpbufs;
12066fb0b1aSGleb Smirnoff SYSCTL_INT(_vm, OID_AUTO, vnode_pbufs, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
12166fb0b1aSGleb Smirnoff     &nvnpbufs, 0, "number of physical buffers allocated for vnode pager");
12266fb0b1aSGleb Smirnoff 
123756a5412SGleb Smirnoff static uma_zone_t vnode_pbuf_zone;
124756a5412SGleb Smirnoff 
125756a5412SGleb Smirnoff static void
126756a5412SGleb Smirnoff vnode_pager_init(void *dummy)
127756a5412SGleb Smirnoff {
128756a5412SGleb Smirnoff 
12966fb0b1aSGleb Smirnoff #ifdef __LP64__
13066fb0b1aSGleb Smirnoff 	nvnpbufs = nswbuf * 2;
13166fb0b1aSGleb Smirnoff #else
13266fb0b1aSGleb Smirnoff 	nvnpbufs = nswbuf / 2;
13366fb0b1aSGleb Smirnoff #endif
13466fb0b1aSGleb Smirnoff 	TUNABLE_INT_FETCH("vm.vnode_pbufs", &nvnpbufs);
13566fb0b1aSGleb Smirnoff 	vnode_pbuf_zone = pbuf_zsecond_create("vnpbuf", nvnpbufs);
136756a5412SGleb Smirnoff }
137756a5412SGleb Smirnoff SYSINIT(vnode_pager, SI_SUB_CPU, SI_ORDER_ANY, vnode_pager_init, NULL);
138756a5412SGleb Smirnoff 
139d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
140d07a6d3fSPoul-Henning Kamp int
141731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
142d07a6d3fSPoul-Henning Kamp {
143d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
144d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
145d07a6d3fSPoul-Henning Kamp 	struct vattr va;
146d07a6d3fSPoul-Henning Kamp 
147d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
148d07a6d3fSPoul-Henning Kamp 		return (0);
149d07a6d3fSPoul-Henning Kamp 
150*6470c8d3SKonstantin Belousov 	object = vp->v_object;
151*6470c8d3SKonstantin Belousov 	if (object != NULL)
152d07a6d3fSPoul-Henning Kamp 		return (0);
153d07a6d3fSPoul-Henning Kamp 
154d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
155d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
156d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
157d07a6d3fSPoul-Henning Kamp 		} else {
1580359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
159d07a6d3fSPoul-Henning Kamp 				return (0);
160d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
161d07a6d3fSPoul-Henning Kamp 		}
162d07a6d3fSPoul-Henning Kamp 	}
163d07a6d3fSPoul-Henning Kamp 
1643364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
165d07a6d3fSPoul-Henning Kamp 	/*
166d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
167d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
168d07a6d3fSPoul-Henning Kamp 	 */
16989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
170d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
17189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
172d07a6d3fSPoul-Henning Kamp 	vrele(vp);
173d07a6d3fSPoul-Henning Kamp 
174d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
175d07a6d3fSPoul-Henning Kamp 
176d07a6d3fSPoul-Henning Kamp 	return (0);
177d07a6d3fSPoul-Henning Kamp }
178d07a6d3fSPoul-Henning Kamp 
1797146d6cbSPoul-Henning Kamp void
1807146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1817146d6cbSPoul-Henning Kamp {
1827146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1837146d6cbSPoul-Henning Kamp 
1847146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
185*6470c8d3SKonstantin Belousov 	if (obj == NULL || obj->handle != vp)
1867146d6cbSPoul-Henning Kamp 		return;
18757fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
18889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
189*6470c8d3SKonstantin Belousov 	MPASS(obj->type == OBJT_VNODE);
1902a339d9eSKonstantin Belousov 	umtx_shm_object_terminated(obj);
1917146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1927146d6cbSPoul-Henning Kamp 		/*
1937146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1947146d6cbSPoul-Henning Kamp 		 */
19590880a1bSKonstantin Belousov 		if ((obj->flags & OBJ_DEAD) == 0) {
196783a68aaSKonstantin Belousov 			vm_object_set_flag(obj, OBJ_DEAD);
197783a68aaSKonstantin Belousov 
198783a68aaSKonstantin Belousov 			/*
199783a68aaSKonstantin Belousov 			 * Clean pages and flush buffers.
200783a68aaSKonstantin Belousov 			 */
201783a68aaSKonstantin Belousov 			vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
202783a68aaSKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
203783a68aaSKonstantin Belousov 
204783a68aaSKonstantin Belousov 			vinvalbuf(vp, V_SAVE, 0, 0);
205783a68aaSKonstantin Belousov 
206783a68aaSKonstantin Belousov 			BO_LOCK(&vp->v_bufobj);
207783a68aaSKonstantin Belousov 			vp->v_bufobj.bo_flag |= BO_DEAD;
208783a68aaSKonstantin Belousov 			BO_UNLOCK(&vp->v_bufobj);
209783a68aaSKonstantin Belousov 
210783a68aaSKonstantin Belousov 			VM_OBJECT_WLOCK(obj);
2117146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
21290880a1bSKonstantin Belousov 		} else {
21390880a1bSKonstantin Belousov 			/*
21490880a1bSKonstantin Belousov 			 * Waiters were already handled during object
21590880a1bSKonstantin Belousov 			 * termination.  The exclusive vnode lock hopefully
21690880a1bSKonstantin Belousov 			 * prevented new waiters from referencing the dying
21790880a1bSKonstantin Belousov 			 * object.
21890880a1bSKonstantin Belousov 			 */
21990880a1bSKonstantin Belousov 			vp->v_object = NULL;
22089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
22190880a1bSKonstantin Belousov 		}
2227146d6cbSPoul-Henning Kamp 	} else {
2237146d6cbSPoul-Henning Kamp 		/*
2247146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
2257146d6cbSPoul-Henning Kamp 		 */
2267146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
22789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
2287146d6cbSPoul-Henning Kamp 	}
22990880a1bSKonstantin Belousov 	KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
2307146d6cbSPoul-Henning Kamp }
2317146d6cbSPoul-Henning Kamp 
2327146d6cbSPoul-Henning Kamp 
233df8bae1dSRodney W. Grimes /*
234df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
235df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
236df8bae1dSRodney W. Grimes  */
23724a1cce3SDavid Greenman vm_object_t
2386cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
2393364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
240df8bae1dSRodney W. Grimes {
24106cb7259SDavid Greenman 	vm_object_t object;
242df8bae1dSRodney W. Grimes 	struct vnode *vp;
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes 	/*
245df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
246df8bae1dSRodney W. Grimes 	 */
247df8bae1dSRodney W. Grimes 	if (handle == NULL)
248df8bae1dSRodney W. Grimes 		return (NULL);
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes 	vp = (struct vnode *)handle;
251*6470c8d3SKonstantin Belousov 	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
2526ded8427SKonstantin Belousov 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
253*6470c8d3SKonstantin Belousov retry:
254*6470c8d3SKonstantin Belousov 	object = vp->v_object;
2552be70f79SJohn Dyson 
25624a1cce3SDavid Greenman 	if (object == NULL) {
257df8bae1dSRodney W. Grimes 		/*
2582ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
259df8bae1dSRodney W. Grimes 		 */
260*6470c8d3SKonstantin Belousov 		object = vm_object_allocate(OBJT_VNODE,
261*6470c8d3SKonstantin Belousov 		    OFF_TO_IDX(round_page(size)));
262bbc0ec52SDavid Greenman 
2636cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
26484110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
265e5818a53SJeff Roberson 		object->domain.dr_policy = vnode_domainset;
26626f9a767SRodney W. Grimes 
26724a1cce3SDavid Greenman 		object->handle = handle;
26811be8415SStephan Uphoff 		VI_LOCK(vp);
2692ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2702ac78f0eSStephan Uphoff 			/*
271*6470c8d3SKonstantin Belousov 			 * Object has been created while we were allocating.
2722ac78f0eSStephan Uphoff 			 */
27311be8415SStephan Uphoff 			VI_UNLOCK(vp);
2749cddade7SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
2759cddade7SKonstantin Belousov 			KASSERT(object->ref_count == 1,
2769cddade7SKonstantin Belousov 			    ("leaked ref %p %d", object, object->ref_count));
2779cddade7SKonstantin Belousov 			object->type = OBJT_DEAD;
2789cddade7SKonstantin Belousov 			object->ref_count = 0;
2799cddade7SKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
2802ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2812ac78f0eSStephan Uphoff 			goto retry;
282df8bae1dSRodney W. Grimes 		}
2832ac78f0eSStephan Uphoff 		vp->v_object = object;
28411be8415SStephan Uphoff 		VI_UNLOCK(vp);
28511be8415SStephan Uphoff 	} else {
286*6470c8d3SKonstantin Belousov 		VM_OBJECT_WLOCK(object);
2872ac78f0eSStephan Uphoff 		object->ref_count++;
2883d653db0SAlan Cox #if VM_NRESERVLEVEL > 0
2893d653db0SAlan Cox 		vm_object_color(object, 0);
2903d653db0SAlan Cox #endif
29189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
29211be8415SStephan Uphoff 	}
2936ff51a36SMateusz Guzik 	vrefact(vp);
29424a1cce3SDavid Greenman 	return (object);
295df8bae1dSRodney W. Grimes }
296df8bae1dSRodney W. Grimes 
297658ad5ffSAlan Cox /*
298658ad5ffSAlan Cox  *	The object must be locked.
299658ad5ffSAlan Cox  */
300f708ef1bSPoul-Henning Kamp static void
3017ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
30224a1cce3SDavid Greenman {
303b9f180d1SKonstantin Belousov 	struct vnode *vp;
304b9f180d1SKonstantin Belousov 	int refs;
305df8bae1dSRodney W. Grimes 
306b9f180d1SKonstantin Belousov 	vp = object->handle;
30724a1cce3SDavid Greenman 	if (vp == NULL)
30824a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
30924a1cce3SDavid Greenman 
31089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
31166095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
312b9f180d1SKonstantin Belousov 	refs = object->ref_count;
31324a1cce3SDavid Greenman 
31424a1cce3SDavid Greenman 	object->handle = NULL;
31595461b45SJohn Dyson 	object->type = OBJT_DEAD;
31657fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
31784110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
31884110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
31978022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
320b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
321b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
32284110e7eSKonstantin Belousov 	}
323aa2cabb9SDavid Greenman 	vp->v_object = NULL;
32478022527SKonstantin Belousov 	VI_LOCK(vp);
32578022527SKonstantin Belousov 
32678022527SKonstantin Belousov 	/*
32778022527SKonstantin Belousov 	 * vm_map_entry_set_vnode_text() cannot reach this vnode by
32878022527SKonstantin Belousov 	 * following object->handle.  Clear all text references now.
32978022527SKonstantin Belousov 	 * This also clears the transient references from
33078022527SKonstantin Belousov 	 * kern_execve(), which is fine because dead_vnodeops uses nop
33178022527SKonstantin Belousov 	 * for VOP_UNSET_TEXT().
33278022527SKonstantin Belousov 	 */
33378022527SKonstantin Belousov 	if (vp->v_writecount < 0)
33478022527SKonstantin Belousov 		vp->v_writecount = 0;
33578022527SKonstantin Belousov 	VI_UNLOCK(vp);
33689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
337b9f180d1SKonstantin Belousov 	while (refs-- > 0)
338b9f180d1SKonstantin Belousov 		vunref(vp);
33989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
340df8bae1dSRodney W. Grimes }
34126f9a767SRodney W. Grimes 
342f708ef1bSPoul-Henning Kamp static boolean_t
3437ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
3447ebba1f8SGleb Smirnoff     int *after)
345df8bae1dSRodney W. Grimes {
34624a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
34798b0c789SPoul-Henning Kamp 	daddr_t bn;
3484153054aSJeff Roberson 	uintptr_t lockstate;
3493af76890SPoul-Henning Kamp 	int err;
350170db9c6SJohn Dyson 	daddr_t reqblock;
3512c4488fcSJohn Dyson 	int poff;
3522c4488fcSJohn Dyson 	int bsize;
353d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
354df8bae1dSRodney W. Grimes 
3554153054aSJeff Roberson 	VM_OBJECT_ASSERT_LOCKED(object);
35624579ca1SMatthew Dillon 	/*
35724579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
35824579ca1SMatthew Dillon 	 * have the page.
35924579ca1SMatthew Dillon 	 */
360b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
36147221757SJohn Dyson 		return FALSE;
362df8bae1dSRodney W. Grimes 	/*
363b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3640d94caffSDavid Greenman 	 * not have the page.
365df8bae1dSRodney W. Grimes 	 */
366b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3674abc71c0SDavid Greenman 		return FALSE;
368df8bae1dSRodney W. Grimes 
369eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
370170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
371d63596ceSJohn Dyson 	blocksperpage = 0;
372d63596ceSJohn Dyson 	if (pagesperblock > 0) {
373a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
374d63596ceSJohn Dyson 	} else {
375d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
376d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
377d63596ceSJohn Dyson 	}
3784153054aSJeff Roberson 	lockstate = VM_OBJECT_DROP(object);
379ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
3804153054aSJeff Roberson 	VM_OBJECT_PICKUP(object, lockstate);
3810d94caffSDavid Greenman 	if (err)
38224a1cce3SDavid Greenman 		return TRUE;
3836eab77f2SJohn Dyson 	if (bn == -1)
384ced399eeSJohn Dyson 		return FALSE;
385d63596ceSJohn Dyson 	if (pagesperblock > 0) {
386a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
387170db9c6SJohn Dyson 		if (before) {
388170db9c6SJohn Dyson 			*before *= pagesperblock;
389170db9c6SJohn Dyson 			*before += poff;
390170db9c6SJohn Dyson 		}
391170db9c6SJohn Dyson 		if (after) {
39284d31376SGleb Smirnoff 			/*
39384d31376SGleb Smirnoff 			 * The BMAP vop can report a partial block in the
394d2596d17SGleb Smirnoff 			 * 'after', but must not report blocks after EOF.
39584d31376SGleb Smirnoff 			 * Assert the latter, and truncate 'after' in case
39684d31376SGleb Smirnoff 			 * of the former.
39784d31376SGleb Smirnoff 			 */
398d2596d17SGleb Smirnoff 			KASSERT((reqblock + *after) * pagesperblock <
399d2596d17SGleb Smirnoff 			    roundup2(object->size, pagesperblock),
40084d31376SGleb Smirnoff 			    ("%s: reqblock %jd after %d size %ju", __func__,
40184d31376SGleb Smirnoff 			    (intmax_t )reqblock, *after,
40284d31376SGleb Smirnoff 			    (uintmax_t )object->size));
403170db9c6SJohn Dyson 			*after *= pagesperblock;
40484d31376SGleb Smirnoff 			*after += pagesperblock - (poff + 1);
40584d31376SGleb Smirnoff 			if (pindex + *after >= object->size)
40684d31376SGleb Smirnoff 				*after = object->size - 1 - pindex;
407170db9c6SJohn Dyson 		}
408d63596ceSJohn Dyson 	} else {
409d63596ceSJohn Dyson 		if (before) {
410d63596ceSJohn Dyson 			*before /= blocksperpage;
411d63596ceSJohn Dyson 		}
412d63596ceSJohn Dyson 
413d63596ceSJohn Dyson 		if (after) {
414d63596ceSJohn Dyson 			*after /= blocksperpage;
415d63596ceSJohn Dyson 		}
416d63596ceSJohn Dyson 	}
417ced399eeSJohn Dyson 	return TRUE;
418df8bae1dSRodney W. Grimes }
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes /*
421df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
42224a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
423df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
424df8bae1dSRodney W. Grimes  *
425df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
426df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
427df8bae1dSRodney W. Grimes  */
428df8bae1dSRodney W. Grimes void
4297ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
430df8bae1dSRodney W. Grimes {
4312a8f9ab5SAlan Cox 	vm_object_t object;
4322a8f9ab5SAlan Cox 	vm_page_t m;
433c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
434df8bae1dSRodney W. Grimes 
4352a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
436df8bae1dSRodney W. Grimes 		return;
437cb61d698SKonstantin Belousov /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
43889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
4399b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
4409b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
4419b8851faSKonstantin Belousov 		return;
4429b8851faSKonstantin Belousov 	}
4439b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
4449b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
4452a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
446df8bae1dSRodney W. Grimes 		/*
447df8bae1dSRodney W. Grimes 		 * Hasn't changed size
448df8bae1dSRodney W. Grimes 		 */
44989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
450df8bae1dSRodney W. Grimes 		return;
4512a8f9ab5SAlan Cox 	}
452c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
4532a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
454df8bae1dSRodney W. Grimes 		/*
455bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
456df8bae1dSRodney W. Grimes 		 */
4572a8f9ab5SAlan Cox 		if (nobjsize < object->size)
458c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
4596bbee8e2SAlan Cox 			    0);
460bbc0ec52SDavid Greenman 		/*
461bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4623ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4633ebeaf59SMatthew Dillon 		 *
4643ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4653ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4663ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
467bbc0ec52SDavid Greenman 		 */
4682a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4691b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4701b26eb10SAlan Cox 		    m->valid != 0) {
4712b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4722b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4732b6b0df7SMatthew Dillon 
4742b6b0df7SMatthew Dillon 			/*
4752b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4762b6b0df7SMatthew Dillon 			 * the page has been mapped.
4772b6b0df7SMatthew Dillon 			 */
478fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4792b6b0df7SMatthew Dillon 
4802b6b0df7SMatthew Dillon 			/*
4813c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4823c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
4833c33df62SAlan Cox 			 * have already been set.
4843c33df62SAlan Cox 			 */
485dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
4863c33df62SAlan Cox 
4873c33df62SAlan Cox 			/*
4883c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
4893c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
4903c33df62SAlan Cox 			 * cleared.
4913c33df62SAlan Cox 			 */
4923c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
4933c33df62SAlan Cox 
4943c33df62SAlan Cox 			/*
4953c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
4963ebeaf59SMatthew Dillon 			 *
4973ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4983ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4993ebeaf59SMatthew Dillon 			 * replacement from working properly.
5002b6b0df7SMatthew Dillon 			 */
5013c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
502bbc0ec52SDavid Greenman 		}
503bbc0ec52SDavid Greenman 	}
504a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
505c576d121SLuoqi Chen 	object->size = nobjsize;
50689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
507df8bae1dSRodney W. Grimes }
508df8bae1dSRodney W. Grimes 
50926f9a767SRodney W. Grimes /*
51026f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
51126f9a767SRodney W. Grimes  * file address
51226f9a767SRodney W. Grimes  */
513bff76343SAlan Cox static int
514bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
515bff76343SAlan Cox     int *run)
51626f9a767SRodney W. Grimes {
51726f9a767SRodney W. Grimes 	int bsize;
51826f9a767SRodney W. Grimes 	int err;
519a316d390SJohn Dyson 	daddr_t vblock;
520f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
52126f9a767SRodney W. Grimes 
5222ad036b6SAlan Cox 	if (address < 0)
5230d94caffSDavid Greenman 		return -1;
5240d94caffSDavid Greenman 
525b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5262c4488fcSJohn Dyson 		return -1;
5272c4488fcSJohn Dyson 
52826f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
52926f9a767SRodney W. Grimes 	vblock = address / bsize;
53026f9a767SRodney W. Grimes 	voffset = address % bsize;
53126f9a767SRodney W. Grimes 
532bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
533bff76343SAlan Cox 	if (err == 0) {
534bff76343SAlan Cox 		if (*rtaddress != -1)
535bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
536efc68ce1SDavid Greenman 		if (run) {
537efc68ce1SDavid Greenman 			*run += 1;
538efc68ce1SDavid Greenman 			*run *= bsize / PAGE_SIZE;
539efc68ce1SDavid Greenman 			*run -= voffset / PAGE_SIZE;
540efc68ce1SDavid Greenman 		}
541efc68ce1SDavid Greenman 	}
54226f9a767SRodney W. Grimes 
543bff76343SAlan Cox 	return (err);
54426f9a767SRodney W. Grimes }
54526f9a767SRodney W. Grimes 
54626f9a767SRodney W. Grimes /*
54726f9a767SRodney W. Grimes  * small block filesystem vnode pager input
54826f9a767SRodney W. Grimes  */
549f708ef1bSPoul-Henning Kamp static int
5507ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
55126f9a767SRodney W. Grimes {
5529c83534dSPoul-Henning Kamp 	struct vnode *vp;
5539c83534dSPoul-Henning Kamp 	struct bufobj *bo;
55426f9a767SRodney W. Grimes 	struct buf *bp;
5559e0ddbd0SAlan Cox 	struct sf_buf *sf;
556f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
55726f9a767SRodney W. Grimes 	vm_offset_t bsize;
558561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
559561cc9fcSKonstantin Belousov 	int error, i;
56026f9a767SRodney W. Grimes 
561561cc9fcSKonstantin Belousov 	error = 0;
56224a1cce3SDavid Greenman 	vp = object->handle;
563b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5642c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5652c4488fcSJohn Dyson 
56626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5670bdb7528SDavid Greenman 
5689c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
56926f9a767SRodney W. Grimes 
5709e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
57126f9a767SRodney W. Grimes 
57226f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
57333c67741SMatthew Dillon 		vm_ooffset_t address;
574bbc0ec52SDavid Greenman 
5750d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5760d53a17bSAlan Cox 		if (m->valid & bits)
57726f9a767SRodney W. Grimes 			continue;
57826f9a767SRodney W. Grimes 
57933c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
58033c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
58133c67741SMatthew Dillon 			fileaddr = -1;
58233c67741SMatthew Dillon 		} else {
583bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
584bff76343SAlan Cox 			if (error)
585bff76343SAlan Cox 				break;
58633c67741SMatthew Dillon 		}
58726f9a767SRodney W. Grimes 		if (fileaddr != -1) {
588756a5412SGleb Smirnoff 			bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
58926f9a767SRodney W. Grimes 
59026f9a767SRodney W. Grimes 			/* build a minimal buffer header */
59121144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5926a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
593bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
594bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
595a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
596a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5979e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
598187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5999c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
6001faacf5dSKirk McKusick 			bp->b_vp = vp;
60126f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
60226f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
6032b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
6045bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
60526f9a767SRodney W. Grimes 
60626f9a767SRodney W. Grimes 			/* do the input */
6072c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
608b792bebeSPoul-Henning Kamp 			bstrategy(bp);
60926f9a767SRodney W. Grimes 
6106a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
6116a4b5823SPoul-Henning Kamp 
612c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
61326f9a767SRodney W. Grimes 				error = EIO;
61426f9a767SRodney W. Grimes 
61526f9a767SRodney W. Grimes 			/*
61626f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
61726f9a767SRodney W. Grimes 			 */
6181faacf5dSKirk McKusick 			bp->b_vp = NULL;
6199c83534dSPoul-Henning Kamp 			pbrelbo(bp);
620756a5412SGleb Smirnoff 			uma_zfree(vnode_pbuf_zone, bp);
62126f9a767SRodney W. Grimes 			if (error)
62226f9a767SRodney W. Grimes 				break;
6230d53a17bSAlan Cox 		} else
6249e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
6250d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
6260d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
62789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
6280d53a17bSAlan Cox 		m->valid |= bits;
62989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
63026f9a767SRodney W. Grimes 	}
6319e0ddbd0SAlan Cox 	sf_buf_free(sf);
63226f9a767SRodney W. Grimes 	if (error) {
633a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
63426f9a767SRodney W. Grimes 	}
63526f9a767SRodney W. Grimes 	return VM_PAGER_OK;
63626f9a767SRodney W. Grimes }
63726f9a767SRodney W. Grimes 
63826f9a767SRodney W. Grimes /*
639475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
64026f9a767SRodney W. Grimes  */
641f708ef1bSPoul-Henning Kamp static int
6427ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
64326f9a767SRodney W. Grimes {
644df8bae1dSRodney W. Grimes 	struct uio auio;
645df8bae1dSRodney W. Grimes 	struct iovec aiov;
64626f9a767SRodney W. Grimes 	int error;
64726f9a767SRodney W. Grimes 	int size;
6489e0ddbd0SAlan Cox 	struct sf_buf *sf;
649342a1480SJohn Baldwin 	struct vnode *vp;
650df8bae1dSRodney W. Grimes 
65189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
65226f9a767SRodney W. Grimes 	error = 0;
653bbc0ec52SDavid Greenman 
654df8bae1dSRodney W. Grimes 	/*
65526f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
65626f9a767SRodney W. Grimes 	 */
657a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
65826f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
65926f9a767SRodney W. Grimes 	} else {
66026f9a767SRodney W. Grimes 		size = PAGE_SIZE;
661a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
662a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
66352051abcSAlan Cox 		vp = object->handle;
66489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
6650bdb7528SDavid Greenman 
66626f9a767SRodney W. Grimes 		/*
667df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
668df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
669df8bae1dSRodney W. Grimes 		 */
6709e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6710bdb7528SDavid Greenman 
6729e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
673df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
674df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
675df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
676a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
677df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
67826f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
679df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
680b40ce416SJulian Elischer 		auio.uio_td = curthread;
68126f9a767SRodney W. Grimes 
682a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
683df8bae1dSRodney W. Grimes 		if (!error) {
68454d92145SMatthew Dillon 			int count = size - auio.uio_resid;
685df8bae1dSRodney W. Grimes 
686df8bae1dSRodney W. Grimes 			if (count == 0)
687df8bae1dSRodney W. Grimes 				error = EINVAL;
68826f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6899e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6909e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
691df8bae1dSRodney W. Grimes 		}
6929e0ddbd0SAlan Cox 		sf_buf_free(sf);
6931b26eb10SAlan Cox 
69489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
695df8bae1dSRodney W. Grimes 	}
6960d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
6976e3a3f38SRobert V. Baron 	if (!error)
6986e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
699a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
70026f9a767SRodney W. Grimes }
70126f9a767SRodney W. Grimes 
70226f9a767SRodney W. Grimes /*
70326f9a767SRodney W. Grimes  * generic vnode pager input routine
70426f9a767SRodney W. Grimes  */
705170db9c6SJohn Dyson 
706ce75f2c3SMike Smith /*
70723955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
70847e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
70947e151ddSRobert Drehmel  * to implement the previous behaviour.
710ce75f2c3SMike Smith  *
711ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
712ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
713ce75f2c3SMike Smith  */
714f708ef1bSPoul-Henning Kamp static int
715b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
716b0cd2017SGleb Smirnoff     int *rahead)
71724a1cce3SDavid Greenman {
718170db9c6SJohn Dyson 	struct vnode *vp;
719b0cd2017SGleb Smirnoff 	int rtval;
72095e5e988SJohn Dyson 
721170db9c6SJohn Dyson 	vp = object->handle;
72289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
723b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
72423955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
72523955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
72689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
727170db9c6SJohn Dyson 	return rtval;
728170db9c6SJohn Dyson }
729170db9c6SJohn Dyson 
73090effb23SGleb Smirnoff static int
73190effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
732b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
73390effb23SGleb Smirnoff {
73490effb23SGleb Smirnoff 	struct vnode *vp;
73590effb23SGleb Smirnoff 	int rtval;
73690effb23SGleb Smirnoff 
73790effb23SGleb Smirnoff 	vp = object->handle;
73890effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
739b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
74090effb23SGleb Smirnoff 	KASSERT(rtval != EOPNOTSUPP,
74190effb23SGleb Smirnoff 	    ("vnode_pager: FS getpages_async not implemented\n"));
74290effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
74390effb23SGleb Smirnoff 	return (rtval);
74490effb23SGleb Smirnoff }
74590effb23SGleb Smirnoff 
746ce75f2c3SMike Smith /*
74790effb23SGleb Smirnoff  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
74890effb23SGleb Smirnoff  * local filesystems, where partially valid pages can only occur at
74990effb23SGleb Smirnoff  * the end of file.
750d15b55c5SKonstantin Belousov  */
751d15b55c5SKonstantin Belousov int
752d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
753d15b55c5SKonstantin Belousov {
75490effb23SGleb Smirnoff 
755b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
756b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
75790effb23SGleb Smirnoff }
75890effb23SGleb Smirnoff 
75990effb23SGleb Smirnoff int
76090effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
76190effb23SGleb Smirnoff {
76290effb23SGleb Smirnoff 
763b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
764b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
765d15b55c5SKonstantin Belousov }
766d15b55c5SKonstantin Belousov 
767d15b55c5SKonstantin Belousov /*
768ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
769ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
770ce75f2c3SMike Smith  */
771ce75f2c3SMike Smith int
772b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
773b0cd2017SGleb Smirnoff     int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
774170db9c6SJohn Dyson {
775ce75f2c3SMike Smith 	vm_object_t object;
7769c83534dSPoul-Henning Kamp 	struct bufobj *bo;
7770bdb7528SDavid Greenman 	struct buf *bp;
778b0cd2017SGleb Smirnoff 	off_t foff;
779e48b82bdSGleb Smirnoff #ifdef INVARIANTS
780e48b82bdSGleb Smirnoff 	off_t blkno0;
781e48b82bdSGleb Smirnoff #endif
782756a5412SGleb Smirnoff 	int bsize, pagesperblock;
783b0cd2017SGleb Smirnoff 	int error, before, after, rbehind, rahead, poff, i;
784b0cd2017SGleb Smirnoff 	int bytecount, secmask;
785ce75f2c3SMike Smith 
7869c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
787b0cd2017SGleb Smirnoff 	    ("%s does not support devices", __func__));
788b0cd2017SGleb Smirnoff 
789b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
790eac91e32SKonstantin Belousov 		return (VM_PAGER_BAD);
7912c4488fcSJohn Dyson 
792eac91e32SKonstantin Belousov 	object = vp->v_object;
793b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(m[0]->pindex);
79426f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
795b0cd2017SGleb Smirnoff 	pagesperblock = bsize / PAGE_SIZE;
796b0cd2017SGleb Smirnoff 
797b0cd2017SGleb Smirnoff 	KASSERT(foff < object->un_pager.vnp.vnp_size,
798b0cd2017SGleb Smirnoff 	    ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
79940a51684SJason A. Harmening 	KASSERT(count <= nitems(bp->b_pages),
800b0cd2017SGleb Smirnoff 	    ("%s: requested %d pages", __func__, count));
801b0cd2017SGleb Smirnoff 
802b0cd2017SGleb Smirnoff 	/*
803b0cd2017SGleb Smirnoff 	 * The last page has valid blocks.  Invalid part can only
804b0cd2017SGleb Smirnoff 	 * exist at the end of file, and the page is made fully valid
805b0cd2017SGleb Smirnoff 	 * by zeroing in vm_pager_get_pages().
806b0cd2017SGleb Smirnoff 	 */
807b0cd2017SGleb Smirnoff 	if (m[count - 1]->valid != 0 && --count == 0) {
808b0cd2017SGleb Smirnoff 		if (iodone != NULL)
809b0cd2017SGleb Smirnoff 			iodone(arg, m, 1, 0);
810b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
811b0cd2017SGleb Smirnoff 	}
812bbc0ec52SDavid Greenman 
813756a5412SGleb Smirnoff 	bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
81473e9030eSGleb Smirnoff 
81526f9a767SRodney W. Grimes 	/*
816e122dfc1SGleb Smirnoff 	 * Get the underlying device blocks for the file with VOP_BMAP().
817e122dfc1SGleb Smirnoff 	 * If the file system doesn't support VOP_BMAP, use old way of
818e122dfc1SGleb Smirnoff 	 * getting pages via VOP_READ.
81926f9a767SRodney W. Grimes 	 */
820b0cd2017SGleb Smirnoff 	error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
8211de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
822756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
82389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
824b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
82583c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodein);
82683c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodepgsin);
827b0cd2017SGleb Smirnoff 			error = vnode_pager_input_old(object, m[i]);
828b0cd2017SGleb Smirnoff 			if (error)
829b0cd2017SGleb Smirnoff 				break;
830b0cd2017SGleb Smirnoff 		}
83189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
83252051abcSAlan Cox 		return (error);
8331de11f1aSAlan Cox 	} else if (error != 0) {
834756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
8351de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
836b0cd2017SGleb Smirnoff 	}
837bbc0ec52SDavid Greenman 
83826f9a767SRodney W. Grimes 	/*
839b0cd2017SGleb Smirnoff 	 * If the file system supports BMAP, but blocksize is smaller
840b0cd2017SGleb Smirnoff 	 * than a page size, then use special small filesystem code.
84126f9a767SRodney W. Grimes 	 */
842b0cd2017SGleb Smirnoff 	if (pagesperblock == 0) {
843756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
844b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
84583c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodein);
84683c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodepgsin);
847b0cd2017SGleb Smirnoff 			error = vnode_pager_input_smlfs(object, m[i]);
848b0cd2017SGleb Smirnoff 			if (error)
849b0cd2017SGleb Smirnoff 				break;
850b0cd2017SGleb Smirnoff 		}
851b0cd2017SGleb Smirnoff 		return (error);
85226f9a767SRodney W. Grimes 	}
8538d17e694SJulian Elischer 
85426f9a767SRodney W. Grimes 	/*
855b0cd2017SGleb Smirnoff 	 * A sparse file can be encountered only for a single page request,
856763df3ecSPedro F. Giffuni 	 * which may not be preceded by call to vm_pager_haspage().
857a7fecb4dSAlan Cox 	 */
858b0cd2017SGleb Smirnoff 	if (bp->b_blkno == -1) {
859b0cd2017SGleb Smirnoff 		KASSERT(count == 1,
860b0cd2017SGleb Smirnoff 		    ("%s: array[%d] request to a sparse file %p", __func__,
861b0cd2017SGleb Smirnoff 		    count, vp));
862756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
863b0cd2017SGleb Smirnoff 		pmap_zero_page(m[0]);
864b0cd2017SGleb Smirnoff 		KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
865b0cd2017SGleb Smirnoff 		    __func__, m[0]));
866a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
867b0cd2017SGleb Smirnoff 		m[0]->valid = VM_PAGE_BITS_ALL;
86889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
869f4f83da0SAlan Cox 		return (VM_PAGER_OK);
870b0cd2017SGleb Smirnoff 	}
871b0cd2017SGleb Smirnoff 
872e48b82bdSGleb Smirnoff #ifdef INVARIANTS
873e48b82bdSGleb Smirnoff 	blkno0 = bp->b_blkno;
874e48b82bdSGleb Smirnoff #endif
875b0cd2017SGleb Smirnoff 	bp->b_blkno += (foff % bsize) / DEV_BSIZE;
876b0cd2017SGleb Smirnoff 
877b0cd2017SGleb Smirnoff 	/* Recalculate blocks available after/before to pages. */
878b0cd2017SGleb Smirnoff 	poff = (foff % bsize) / PAGE_SIZE;
879b0cd2017SGleb Smirnoff 	before *= pagesperblock;
880b0cd2017SGleb Smirnoff 	before += poff;
881b0cd2017SGleb Smirnoff 	after *= pagesperblock;
882b0cd2017SGleb Smirnoff 	after += pagesperblock - (poff + 1);
883b0cd2017SGleb Smirnoff 	if (m[0]->pindex + after >= object->size)
884b0cd2017SGleb Smirnoff 		after = object->size - 1 - m[0]->pindex;
885b0cd2017SGleb Smirnoff 	KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
886b0cd2017SGleb Smirnoff 	    __func__, count, after + 1));
887b0cd2017SGleb Smirnoff 	after -= count - 1;
888b0cd2017SGleb Smirnoff 
889b0cd2017SGleb Smirnoff 	/* Trim requested rbehind/rahead to possible values. */
890b0cd2017SGleb Smirnoff 	rbehind = a_rbehind ? *a_rbehind : 0;
891b0cd2017SGleb Smirnoff 	rahead = a_rahead ? *a_rahead : 0;
892b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, before);
893b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, m[0]->pindex);
894b0cd2017SGleb Smirnoff 	rahead = min(rahead, after);
895b0cd2017SGleb Smirnoff 	rahead = min(rahead, object->size - m[count - 1]->pindex);
896e48b82bdSGleb Smirnoff 	/*
897e48b82bdSGleb Smirnoff 	 * Check that total amount of pages fit into buf.  Trim rbehind and
898e48b82bdSGleb Smirnoff 	 * rahead evenly if not.
899e48b82bdSGleb Smirnoff 	 */
900e48b82bdSGleb Smirnoff 	if (rbehind + rahead + count > nitems(bp->b_pages)) {
901e48b82bdSGleb Smirnoff 		int trim, sum;
902e48b82bdSGleb Smirnoff 
903e48b82bdSGleb Smirnoff 		trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
904e48b82bdSGleb Smirnoff 		sum = rbehind + rahead;
905e48b82bdSGleb Smirnoff 		if (rbehind == before) {
906e48b82bdSGleb Smirnoff 			/* Roundup rbehind trim to block size. */
907e48b82bdSGleb Smirnoff 			rbehind -= roundup(trim * rbehind / sum, pagesperblock);
908e48b82bdSGleb Smirnoff 			if (rbehind < 0)
909e48b82bdSGleb Smirnoff 				rbehind = 0;
910e48b82bdSGleb Smirnoff 		} else
911e48b82bdSGleb Smirnoff 			rbehind -= trim * rbehind / sum;
912e48b82bdSGleb Smirnoff 		rahead -= trim * rahead / sum;
913e48b82bdSGleb Smirnoff 	}
914e48b82bdSGleb Smirnoff 	KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
915b0cd2017SGleb Smirnoff 	    ("%s: behind %d ahead %d count %d", __func__,
916b0cd2017SGleb Smirnoff 	    rbehind, rahead, count));
917b0cd2017SGleb Smirnoff 
918b0cd2017SGleb Smirnoff 	/*
919b0cd2017SGleb Smirnoff 	 * Fill in the bp->b_pages[] array with requested and optional
920b0cd2017SGleb Smirnoff 	 * read behind or read ahead pages.  Read behind pages are looked
921b0cd2017SGleb Smirnoff 	 * up in a backward direction, down to a first cached page.  Same
922b0cd2017SGleb Smirnoff 	 * for read ahead pages, but there is no need to shift the array
923b0cd2017SGleb Smirnoff 	 * in case of encountering a cached page.
924b0cd2017SGleb Smirnoff 	 */
925b0cd2017SGleb Smirnoff 	i = bp->b_npages = 0;
926b0cd2017SGleb Smirnoff 	if (rbehind) {
927b0cd2017SGleb Smirnoff 		vm_pindex_t startpindex, tpindex;
928b0cd2017SGleb Smirnoff 		vm_page_t p;
929b0cd2017SGleb Smirnoff 
930a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
931b0cd2017SGleb Smirnoff 		startpindex = m[0]->pindex - rbehind;
932b0cd2017SGleb Smirnoff 		if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
933b0cd2017SGleb Smirnoff 		    p->pindex >= startpindex)
934b0cd2017SGleb Smirnoff 			startpindex = p->pindex + 1;
935b0cd2017SGleb Smirnoff 
936b0cd2017SGleb Smirnoff 		/* tpindex is unsigned; beware of numeric underflow. */
937b0cd2017SGleb Smirnoff 		for (tpindex = m[0]->pindex - 1;
938b0cd2017SGleb Smirnoff 		    tpindex >= startpindex && tpindex < m[0]->pindex;
939b0cd2017SGleb Smirnoff 		    tpindex--, i++) {
9407667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
941b0cd2017SGleb Smirnoff 			if (p == NULL) {
942b0cd2017SGleb Smirnoff 				/* Shift the array. */
943b0cd2017SGleb Smirnoff 				for (int j = 0; j < i; j++)
944b0cd2017SGleb Smirnoff 					bp->b_pages[j] = bp->b_pages[j +
945b0cd2017SGleb Smirnoff 					    tpindex + 1 - startpindex];
946b0cd2017SGleb Smirnoff 				break;
947b0cd2017SGleb Smirnoff 			}
948b0cd2017SGleb Smirnoff 			bp->b_pages[tpindex - startpindex] = p;
949a7fecb4dSAlan Cox 		}
9500bdb7528SDavid Greenman 
951b0cd2017SGleb Smirnoff 		bp->b_pgbefore = i;
952b0cd2017SGleb Smirnoff 		bp->b_npages += i;
953b0cd2017SGleb Smirnoff 		bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
954b0cd2017SGleb Smirnoff 	} else
955b0cd2017SGleb Smirnoff 		bp->b_pgbefore = 0;
956b0cd2017SGleb Smirnoff 
957b0cd2017SGleb Smirnoff 	/* Requested pages. */
958b0cd2017SGleb Smirnoff 	for (int j = 0; j < count; j++, i++)
959b0cd2017SGleb Smirnoff 		bp->b_pages[i] = m[j];
960b0cd2017SGleb Smirnoff 	bp->b_npages += count;
961b0cd2017SGleb Smirnoff 
962b0cd2017SGleb Smirnoff 	if (rahead) {
963b0cd2017SGleb Smirnoff 		vm_pindex_t endpindex, tpindex;
964b0cd2017SGleb Smirnoff 		vm_page_t p;
965b0cd2017SGleb Smirnoff 
966b0cd2017SGleb Smirnoff 		if (!VM_OBJECT_WOWNED(object))
967eac91e32SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
968b0cd2017SGleb Smirnoff 		endpindex = m[count - 1]->pindex + rahead + 1;
969b0cd2017SGleb Smirnoff 		if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
970b0cd2017SGleb Smirnoff 		    p->pindex < endpindex)
971b0cd2017SGleb Smirnoff 			endpindex = p->pindex;
972b0cd2017SGleb Smirnoff 		if (endpindex > object->size)
973b0cd2017SGleb Smirnoff 			endpindex = object->size;
974b0cd2017SGleb Smirnoff 
975b0cd2017SGleb Smirnoff 		for (tpindex = m[count - 1]->pindex + 1;
976b0cd2017SGleb Smirnoff 		    tpindex < endpindex; i++, tpindex++) {
9777667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
978b0cd2017SGleb Smirnoff 			if (p == NULL)
979b0cd2017SGleb Smirnoff 				break;
980b0cd2017SGleb Smirnoff 			bp->b_pages[i] = p;
981eac91e32SKonstantin Belousov 		}
982b0cd2017SGleb Smirnoff 
983b0cd2017SGleb Smirnoff 		bp->b_pgafter = i - bp->b_npages;
984b0cd2017SGleb Smirnoff 		bp->b_npages = i;
985b0cd2017SGleb Smirnoff 	} else
986b0cd2017SGleb Smirnoff 		bp->b_pgafter = 0;
987b0cd2017SGleb Smirnoff 
988b0cd2017SGleb Smirnoff 	if (VM_OBJECT_WOWNED(object))
989eac91e32SKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
990b0cd2017SGleb Smirnoff 
991b0cd2017SGleb Smirnoff 	/* Report back actual behind/ahead read. */
992b0cd2017SGleb Smirnoff 	if (a_rbehind)
993b0cd2017SGleb Smirnoff 		*a_rbehind = bp->b_pgbefore;
994b0cd2017SGleb Smirnoff 	if (a_rahead)
995b0cd2017SGleb Smirnoff 		*a_rahead = bp->b_pgafter;
996b0cd2017SGleb Smirnoff 
997e48b82bdSGleb Smirnoff #ifdef INVARIANTS
998dcc0ff5aSGleb Smirnoff 	KASSERT(bp->b_npages <= nitems(bp->b_pages),
999b0cd2017SGleb Smirnoff 	    ("%s: buf %p overflowed", __func__, bp));
10004f56243aSGleb Smirnoff 	for (int j = 1, prev = 0; j < bp->b_npages; j++) {
10011e0c121fSGleb Smirnoff 		if (bp->b_pages[j] == bogus_page)
10021e0c121fSGleb Smirnoff 			continue;
10031e0c121fSGleb Smirnoff 		KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
10041e0c121fSGleb Smirnoff 		    j - prev, ("%s: pages array not consecutive, bp %p",
10051e0c121fSGleb Smirnoff 		     __func__, bp));
10061e0c121fSGleb Smirnoff 		prev = j;
10071e0c121fSGleb Smirnoff 	}
1008e48b82bdSGleb Smirnoff #endif
1009eac91e32SKonstantin Belousov 
10100d94caffSDavid Greenman 	/*
1011b0cd2017SGleb Smirnoff 	 * Recalculate first offset and bytecount with regards to read behind.
1012b0cd2017SGleb Smirnoff 	 * Truncate bytecount to vnode real size and round up physical size
1013b0cd2017SGleb Smirnoff 	 * for real devices.
101426f9a767SRodney W. Grimes 	 */
1015b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1016b0cd2017SGleb Smirnoff 	bytecount = bp->b_npages << PAGE_SHIFT;
1017b0cd2017SGleb Smirnoff 	if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
1018b0cd2017SGleb Smirnoff 		bytecount = object->un_pager.vnp.vnp_size - foff;
1019eac91e32SKonstantin Belousov 	secmask = bo->bo_bsize - 1;
10206229cc50SPoul-Henning Kamp 	KASSERT(secmask < PAGE_SIZE && secmask > 0,
1021b0cd2017SGleb Smirnoff 	    ("%s: sector size %d too large", __func__, secmask + 1));
1022b0cd2017SGleb Smirnoff 	bytecount = (bytecount + secmask) & ~secmask;
102326f9a767SRodney W. Grimes 
102426f9a767SRodney W. Grimes 	/*
1025b0cd2017SGleb Smirnoff 	 * And map the pages to be read into the kva, if the filesystem
10266ce697dcSKonstantin Belousov 	 * requires mapped buffers.
102726f9a767SRodney W. Grimes 	 */
10282a5eef69SGleb Smirnoff 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
10296ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
10306ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
10316ce697dcSKonstantin Belousov 		bp->b_offset = 0;
1032fade8dd7SJeff Roberson 	} else {
1033fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
1034b0cd2017SGleb Smirnoff 		pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1035fade8dd7SJeff Roberson 	}
103626f9a767SRodney W. Grimes 
1037b0cd2017SGleb Smirnoff 	/* Build a minimal buffer header. */
103821144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
1039bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1040bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1041a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
1042a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
10439c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
10441faacf5dSKirk McKusick 	bp->b_vp = vp;
1045b0cd2017SGleb Smirnoff 	bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
10462c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
1047e48b82bdSGleb Smirnoff 	KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1048e48b82bdSGleb Smirnoff 	    (blkno0 - bp->b_blkno) * DEV_BSIZE +
1049e48b82bdSGleb Smirnoff 	    IDX_TO_OFF(m[0]->pindex) % bsize,
1050e48b82bdSGleb Smirnoff 	    ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1051e48b82bdSGleb Smirnoff 	    "blkno0 %ju b_blkno %ju", bsize,
1052e48b82bdSGleb Smirnoff 	    (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1053e48b82bdSGleb Smirnoff 	    (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
105490effb23SGleb Smirnoff 
1055b0cd2017SGleb Smirnoff 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
105683c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodein);
105783c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1058b0cd2017SGleb Smirnoff 
105990effb23SGleb Smirnoff 	if (iodone != NULL) { /* async */
1060b0cd2017SGleb Smirnoff 		bp->b_pgiodone = iodone;
106190effb23SGleb Smirnoff 		bp->b_caller1 = arg;
106290effb23SGleb Smirnoff 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
106390effb23SGleb Smirnoff 		bp->b_flags |= B_ASYNC;
106490effb23SGleb Smirnoff 		BUF_KERNPROC(bp);
1065b792bebeSPoul-Henning Kamp 		bstrategy(bp);
1066b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
106790effb23SGleb Smirnoff 	} else {
106890effb23SGleb Smirnoff 		bp->b_iodone = bdone;
106990effb23SGleb Smirnoff 		bstrategy(bp);
10706a4b5823SPoul-Henning Kamp 		bwait(bp, PVM, "vnread");
107190effb23SGleb Smirnoff 		error = vnode_pager_generic_getpages_done(bp);
10721bb5ad63SGleb Smirnoff 		for (i = 0; i < bp->b_npages; i++)
10736ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
10741faacf5dSKirk McKusick 		bp->b_vp = NULL;
10759c83534dSPoul-Henning Kamp 		pbrelbo(bp);
1076756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
107790effb23SGleb Smirnoff 		return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
107890effb23SGleb Smirnoff 	}
1079b0cd2017SGleb Smirnoff }
108090effb23SGleb Smirnoff 
108190effb23SGleb Smirnoff static void
108290effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp)
108390effb23SGleb Smirnoff {
108490effb23SGleb Smirnoff 	int error;
108590effb23SGleb Smirnoff 
108690effb23SGleb Smirnoff 	error = vnode_pager_generic_getpages_done(bp);
1087b0cd2017SGleb Smirnoff 	/* Run the iodone upon the requested range. */
1088b0cd2017SGleb Smirnoff 	bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1089b0cd2017SGleb Smirnoff 	    bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
109090effb23SGleb Smirnoff 	for (int i = 0; i < bp->b_npages; i++)
109190effb23SGleb Smirnoff 		bp->b_pages[i] = NULL;
109290effb23SGleb Smirnoff 	bp->b_vp = NULL;
109390effb23SGleb Smirnoff 	pbrelbo(bp);
1094756a5412SGleb Smirnoff 	uma_zfree(vnode_pbuf_zone, bp);
109590effb23SGleb Smirnoff }
109690effb23SGleb Smirnoff 
109790effb23SGleb Smirnoff static int
109890effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp)
109990effb23SGleb Smirnoff {
110090effb23SGleb Smirnoff 	vm_object_t object;
110190effb23SGleb Smirnoff 	off_t tfoff, nextoff;
110290effb23SGleb Smirnoff 	int i, error;
110390effb23SGleb Smirnoff 
110490effb23SGleb Smirnoff 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
110590effb23SGleb Smirnoff 	object = bp->b_vp->v_object;
110690effb23SGleb Smirnoff 
110790effb23SGleb Smirnoff 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1108fade8dd7SJeff Roberson 		if (!buf_mapped(bp)) {
1109fade8dd7SJeff Roberson 			bp->b_data = bp->b_kvabase;
1110fade8dd7SJeff Roberson 			pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
111190effb23SGleb Smirnoff 			    bp->b_npages);
111290effb23SGleb Smirnoff 		}
1113fade8dd7SJeff Roberson 		bzero(bp->b_data + bp->b_bcount,
111490effb23SGleb Smirnoff 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
111590effb23SGleb Smirnoff 	}
1116fade8dd7SJeff Roberson 	if (buf_mapped(bp)) {
1117fade8dd7SJeff Roberson 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1118fade8dd7SJeff Roberson 		bp->b_data = unmapped_buf;
111990effb23SGleb Smirnoff 	}
112026f9a767SRodney W. Grimes 
112189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
112290effb23SGleb Smirnoff 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
112390effb23SGleb Smirnoff 	    i < bp->b_npages; i++, tfoff = nextoff) {
11248f9110f6SJohn Dyson 		vm_page_t mt;
11258f9110f6SJohn Dyson 
11268f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
112790effb23SGleb Smirnoff 		mt = bp->b_pages[i];
11288f9110f6SJohn Dyson 
112954746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
11308d17e694SJulian Elischer 			/*
11318d17e694SJulian Elischer 			 * Read filled up entire page.
11328d17e694SJulian Elischer 			 */
11338f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
1134016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
113579f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
1136016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
113779f0deb9SGleb Smirnoff 			    ("%s: page %p is mapped", __func__, mt));
11388f9110f6SJohn Dyson 		} else {
11398d17e694SJulian Elischer 			/*
114042eb4108SAlan Cox 			 * Read did not fill up entire page.
11418d17e694SJulian Elischer 			 *
11428d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
11438d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
11448d17e694SJulian Elischer 			 * read.
11458d17e694SJulian Elischer 			 */
1146dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
114754746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
114842eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
114942eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
115079f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
11518f9110f6SJohn Dyson 		}
11528f9110f6SJohn Dyson 
1153b0cd2017SGleb Smirnoff 		if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1154b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
115503679e23SAlan Cox 	}
115689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
115790effb23SGleb Smirnoff 	if (error != 0)
115890effb23SGleb Smirnoff 		printf("%s: I/O read error %d\n", __func__, error);
115990effb23SGleb Smirnoff 
116090effb23SGleb Smirnoff 	return (error);
116126f9a767SRodney W. Grimes }
116226f9a767SRodney W. Grimes 
1163ce75f2c3SMike Smith /*
1164ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1165ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1166ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1167ce75f2c3SMike Smith  *
1168ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1169ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1170ce75f2c3SMike Smith  */
1171e4542174SMatthew Dillon static void
11727ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
117333cad9e9SKonstantin Belousov     int flags, int *rtvals)
1174170db9c6SJohn Dyson {
1175170db9c6SJohn Dyson 	int rtval;
1176170db9c6SJohn Dyson 	struct vnode *vp;
117786ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1178ad980522SJohn Dyson 
11790e3cdf2cSAlan Cox 	/*
11800e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
11810e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
11820e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
11830e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
11840e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
11850e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
11860e3cdf2cSAlan Cox 	 *
11870e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
11880e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
11890e3cdf2cSAlan Cox 	 */
11900e3cdf2cSAlan Cox 
1191e2068d0bSJeff Roberson 	if (vm_page_count_min())
119233cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
11930e3cdf2cSAlan Cox 
11940e3cdf2cSAlan Cox 	/*
11950e3cdf2cSAlan Cox 	 * Call device-specific putpages function
11960e3cdf2cSAlan Cox 	 */
1197170db9c6SJohn Dyson 	vp = object->handle;
119889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
119933cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
120023955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
120123955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
120289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1203170db9c6SJohn Dyson }
1204170db9c6SJohn Dyson 
120505877a85SKonstantin Belousov static int
120605877a85SKonstantin Belousov vn_off2bidx(vm_ooffset_t offset)
120705877a85SKonstantin Belousov {
120805877a85SKonstantin Belousov 
120905877a85SKonstantin Belousov 	return ((offset & PAGE_MASK) / DEV_BSIZE);
121005877a85SKonstantin Belousov }
121105877a85SKonstantin Belousov 
121205877a85SKonstantin Belousov static bool
121305877a85SKonstantin Belousov vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
121405877a85SKonstantin Belousov {
121505877a85SKonstantin Belousov 
121605877a85SKonstantin Belousov 	KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
121705877a85SKonstantin Belousov 	    offset < IDX_TO_OFF(m->pindex + 1),
121805877a85SKonstantin Belousov 	    ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
121905877a85SKonstantin Belousov 	    (uintmax_t)offset));
122005877a85SKonstantin Belousov 	return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
122105877a85SKonstantin Belousov }
1222ce75f2c3SMike Smith 
122326f9a767SRodney W. Grimes /*
1224ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
12254491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
12262b6b0df7SMatthew Dillon  *
12272b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
1228763df3ecSPedro F. Giffuni  * clustering has already typically occurred, so in general we ask the
12292b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
12302b6b0df7SMatthew Dillon  * then delayed.
123126f9a767SRodney W. Grimes  */
1232ce75f2c3SMike Smith int
1233c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1234c46b90e9SAlan Cox     int flags, int *rtvals)
123526f9a767SRodney W. Grimes {
1236ce75f2c3SMike Smith 	vm_object_t object;
1237c46b90e9SAlan Cox 	vm_page_t m;
123805877a85SKonstantin Belousov 	vm_ooffset_t maxblksz, next_offset, poffset, prev_offset;
1239f6b04d2bSDavid Greenman 	struct uio auio;
1240f6b04d2bSDavid Greenman 	struct iovec aiov;
124105877a85SKonstantin Belousov 	off_t prev_resid, wrsz;
1242e6c44f65SKonstantin Belousov 	int count, error, i, maxsize, ncount, pgoff, ppscheck;
124305877a85SKonstantin Belousov 	bool in_hole;
1244dd498befSPaul Saab 	static struct timeval lastfail;
1245dd498befSPaul Saab 	static int curfail;
124626f9a767SRodney W. Grimes 
1247ce75f2c3SMike Smith 	object = vp->v_object;
1248ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1249ce75f2c3SMike Smith 
125026f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1251031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
125226f9a767SRodney W. Grimes 
1253c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
1254e6c44f65SKonstantin Belousov 		printf("vnode_pager_generic_putpages: "
1255e6c44f65SKonstantin Belousov 		    "attempt to write meta-data 0x%jx(%lx)\n",
1256e6c44f65SKonstantin Belousov 		    (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1257f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1258e6c44f65SKonstantin Belousov 		return (VM_PAGER_BAD);
12590d94caffSDavid Greenman 	}
12600bdb7528SDavid Greenman 
1261f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1262f6b04d2bSDavid Greenman 	ncount = count;
126326f9a767SRodney W. Grimes 
1264c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
126500a6f47fSMatthew Dillon 
126600a6f47fSMatthew Dillon 	/*
126700a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
1268763df3ecSPedro F. Giffuni 	 * have to invalidate pages occurring beyond the file EOF.  However,
126900a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
127000a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
127100a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
127200a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
127300a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
12743ebeaf59SMatthew Dillon 	 *
12753ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
12763ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
127700a6f47fSMatthew Dillon 	 */
1278b3d4ab66SKonstantin Belousov 	VM_OBJECT_RLOCK(object);
1279a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1280b3d4ab66SKonstantin Belousov 		if (!VM_OBJECT_TRYUPGRADE(object)) {
1281b3d4ab66SKonstantin Belousov 			VM_OBJECT_RUNLOCK(object);
1282b3d4ab66SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
1283b3d4ab66SKonstantin Belousov 			if (maxsize + poffset <= object->un_pager.vnp.vnp_size)
1284b3d4ab66SKonstantin Belousov 				goto downgrade;
1285b3d4ab66SKonstantin Belousov 		}
128600a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
1287a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1288aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
128900a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1290938cdc42SKonstantin Belousov 				pgoff = roundup2(pgoff, DEV_BSIZE);
1291938cdc42SKonstantin Belousov 
1292c46b90e9SAlan Cox 				/*
1293c46b90e9SAlan Cox 				 * If the object is locked and the following
1294c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1295c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1296c46b90e9SAlan Cox 				 * pmap operation.
1297c46b90e9SAlan Cox 				 */
1298c46b90e9SAlan Cox 				m = ma[ncount - 1];
1299c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
13006031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1301c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1302e6c44f65SKonstantin Belousov 				MPASS(m->dirty != 0);
1303c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1304c46b90e9SAlan Cox 				    pgoff);
130500a6f47fSMatthew Dillon 			}
130600a6f47fSMatthew Dillon 		} else {
130700a6f47fSMatthew Dillon 			maxsize = 0;
130800a6f47fSMatthew Dillon 			ncount = 0;
130900a6f47fSMatthew Dillon 		}
1310e6c44f65SKonstantin Belousov 		for (i = ncount; i < count; i++)
1311f6b04d2bSDavid Greenman 			rtvals[i] = VM_PAGER_BAD;
1312b3d4ab66SKonstantin Belousov downgrade:
1313b3d4ab66SKonstantin Belousov 		VM_OBJECT_LOCK_DOWNGRADE(object);
1314f6b04d2bSDavid Greenman 	}
131526f9a767SRodney W. Grimes 
1316f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1317f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1318f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1319e6c44f65SKonstantin Belousov 	auio.uio_td = NULL;
132005877a85SKonstantin Belousov 	maxblksz = roundup2(poffset + maxsize, DEV_BSIZE);
132105877a85SKonstantin Belousov 
132205877a85SKonstantin Belousov 	for (prev_offset = poffset; prev_offset < maxblksz;) {
132305877a85SKonstantin Belousov 		/* Skip clean blocks. */
132405877a85SKonstantin Belousov 		for (in_hole = true; in_hole && prev_offset < maxblksz;) {
132505877a85SKonstantin Belousov 			m = ma[OFF_TO_IDX(prev_offset - poffset)];
132605877a85SKonstantin Belousov 			for (i = vn_off2bidx(prev_offset);
132705877a85SKonstantin Belousov 			    i < sizeof(vm_page_bits_t) * NBBY &&
132805877a85SKonstantin Belousov 			    prev_offset < maxblksz; i++) {
132905877a85SKonstantin Belousov 				if (vn_dirty_blk(m, prev_offset)) {
133005877a85SKonstantin Belousov 					in_hole = false;
133105877a85SKonstantin Belousov 					break;
133205877a85SKonstantin Belousov 				}
133305877a85SKonstantin Belousov 				prev_offset += DEV_BSIZE;
133405877a85SKonstantin Belousov 			}
133505877a85SKonstantin Belousov 		}
133605877a85SKonstantin Belousov 		if (in_hole)
133705877a85SKonstantin Belousov 			goto write_done;
133805877a85SKonstantin Belousov 
133905877a85SKonstantin Belousov 		/* Find longest run of dirty blocks. */
134005877a85SKonstantin Belousov 		for (next_offset = prev_offset; next_offset < maxblksz;) {
134105877a85SKonstantin Belousov 			m = ma[OFF_TO_IDX(next_offset - poffset)];
134205877a85SKonstantin Belousov 			for (i = vn_off2bidx(next_offset);
134305877a85SKonstantin Belousov 			    i < sizeof(vm_page_bits_t) * NBBY &&
134405877a85SKonstantin Belousov 			    next_offset < maxblksz; i++) {
134505877a85SKonstantin Belousov 				if (!vn_dirty_blk(m, next_offset))
134605877a85SKonstantin Belousov 					goto start_write;
134705877a85SKonstantin Belousov 				next_offset += DEV_BSIZE;
134805877a85SKonstantin Belousov 			}
134905877a85SKonstantin Belousov 		}
135005877a85SKonstantin Belousov start_write:
135105877a85SKonstantin Belousov 		if (next_offset > poffset + maxsize)
135205877a85SKonstantin Belousov 			next_offset = poffset + maxsize;
135305877a85SKonstantin Belousov 
135405877a85SKonstantin Belousov 		/*
135505877a85SKonstantin Belousov 		 * Getting here requires finding a dirty block in the
135605877a85SKonstantin Belousov 		 * 'skip clean blocks' loop.
135705877a85SKonstantin Belousov 		 */
135805877a85SKonstantin Belousov 		MPASS(prev_offset < next_offset);
135905877a85SKonstantin Belousov 
1360b3d4ab66SKonstantin Belousov 		VM_OBJECT_RUNLOCK(object);
136105877a85SKonstantin Belousov 		aiov.iov_base = NULL;
136205877a85SKonstantin Belousov 		auio.uio_iovcnt = 1;
136305877a85SKonstantin Belousov 		auio.uio_offset = prev_offset;
136405877a85SKonstantin Belousov 		prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
136505877a85SKonstantin Belousov 		    prev_offset;
136605877a85SKonstantin Belousov 		error = VOP_WRITE(vp, &auio,
136705877a85SKonstantin Belousov 		    vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
136805877a85SKonstantin Belousov 
136905877a85SKonstantin Belousov 		wrsz = prev_resid - auio.uio_resid;
137005877a85SKonstantin Belousov 		if (wrsz == 0) {
137105877a85SKonstantin Belousov 			if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
137205877a85SKonstantin Belousov 				vn_printf(vp, "vnode_pager_putpages: "
137305877a85SKonstantin Belousov 				    "zero-length write at %ju resid %zd\n",
137405877a85SKonstantin Belousov 				    auio.uio_offset, auio.uio_resid);
137505877a85SKonstantin Belousov 			}
1376b3d4ab66SKonstantin Belousov 			VM_OBJECT_RLOCK(object);
137705877a85SKonstantin Belousov 			break;
137805877a85SKonstantin Belousov 		}
137905877a85SKonstantin Belousov 
138005877a85SKonstantin Belousov 		/* Adjust the starting offset for next iteration. */
138105877a85SKonstantin Belousov 		prev_offset += wrsz;
138205877a85SKonstantin Belousov 		MPASS(auio.uio_offset == prev_offset);
1383f6b04d2bSDavid Greenman 
13843dbb0ca6SKonstantin Belousov 		ppscheck = 0;
138505877a85SKonstantin Belousov 		if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
138605877a85SKonstantin Belousov 		    &curfail, 1)) != 0)
138705877a85SKonstantin Belousov 			vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
138805877a85SKonstantin Belousov 			    error);
1389e6c44f65SKonstantin Belousov 		if (auio.uio_resid != 0 && (ppscheck != 0 ||
1390e6c44f65SKonstantin Belousov 		    ppsratecheck(&lastfail, &curfail, 1) != 0))
139105877a85SKonstantin Belousov 			vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
139205877a85SKonstantin Belousov 			    "at %ju\n", auio.uio_resid,
139305877a85SKonstantin Belousov 			    (uintmax_t)ma[0]->pindex);
1394b3d4ab66SKonstantin Belousov 		VM_OBJECT_RLOCK(object);
139505877a85SKonstantin Belousov 		if (error != 0 || auio.uio_resid != 0)
139605877a85SKonstantin Belousov 			break;
139705877a85SKonstantin Belousov 	}
139805877a85SKonstantin Belousov write_done:
139905877a85SKonstantin Belousov 	/* Mark completely processed pages. */
140005877a85SKonstantin Belousov 	for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
140126f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
140205877a85SKonstantin Belousov 	/* Mark partial EOF page. */
140305877a85SKonstantin Belousov 	if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
140405877a85SKonstantin Belousov 		rtvals[i++] = VM_PAGER_OK;
140505877a85SKonstantin Belousov 	/* Unwritten pages in range, free bonus if the page is clean. */
140605877a85SKonstantin Belousov 	for (; i < ncount; i++)
140705877a85SKonstantin Belousov 		rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
1408b3d4ab66SKonstantin Belousov 	VM_OBJECT_RUNLOCK(object);
140905877a85SKonstantin Belousov 	VM_CNT_ADD(v_vnodepgsout, i);
141005877a85SKonstantin Belousov 	VM_CNT_INC(v_vnodeout);
1411e6c44f65SKonstantin Belousov 	return (rtvals[0]);
141226f9a767SRodney W. Grimes }
1413031ec8c1SKonstantin Belousov 
141465b9599aSKonstantin Belousov int
141565b9599aSKonstantin Belousov vnode_pager_putpages_ioflags(int pager_flags)
141665b9599aSKonstantin Belousov {
141765b9599aSKonstantin Belousov 	int ioflags;
141865b9599aSKonstantin Belousov 
141965b9599aSKonstantin Belousov 	/*
142065b9599aSKonstantin Belousov 	 * Pageouts are already clustered, use IO_ASYNC to force a
142165b9599aSKonstantin Belousov 	 * bawrite() rather then a bdwrite() to prevent paging I/O
142265b9599aSKonstantin Belousov 	 * from saturating the buffer cache.  Dummy-up the sequential
142365b9599aSKonstantin Belousov 	 * heuristic to cause large ranges to cluster.  If neither
142465b9599aSKonstantin Belousov 	 * IO_SYNC or IO_ASYNC is set, the system decides how to
142565b9599aSKonstantin Belousov 	 * cluster.
142665b9599aSKonstantin Belousov 	 */
142765b9599aSKonstantin Belousov 	ioflags = IO_VMIO;
142865b9599aSKonstantin Belousov 	if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0)
142965b9599aSKonstantin Belousov 		ioflags |= IO_SYNC;
143065b9599aSKonstantin Belousov 	else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0)
143165b9599aSKonstantin Belousov 		ioflags |= IO_ASYNC;
143265b9599aSKonstantin Belousov 	ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0;
143365b9599aSKonstantin Belousov 	ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0;
143465b9599aSKonstantin Belousov 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
143565b9599aSKonstantin Belousov 	return (ioflags);
143665b9599aSKonstantin Belousov }
143765b9599aSKonstantin Belousov 
1438555b7bb4SKonstantin Belousov /*
1439555b7bb4SKonstantin Belousov  * vnode_pager_undirty_pages().
1440555b7bb4SKonstantin Belousov  *
1441555b7bb4SKonstantin Belousov  * A helper to mark pages as clean after pageout that was possibly
1442555b7bb4SKonstantin Belousov  * done with a short write.  The lpos argument specifies the page run
1443555b7bb4SKonstantin Belousov  * length in bytes, and the written argument specifies how many bytes
1444555b7bb4SKonstantin Belousov  * were actually written.  eof is the offset past the last valid byte
1445555b7bb4SKonstantin Belousov  * in the vnode using the absolute file position of the first byte in
1446555b7bb4SKonstantin Belousov  * the run as the base from which it is computed.
1447555b7bb4SKonstantin Belousov  */
1448031ec8c1SKonstantin Belousov void
1449555b7bb4SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof,
1450555b7bb4SKonstantin Belousov     int lpos)
1451031ec8c1SKonstantin Belousov {
14529d17da3bSKonstantin Belousov 	vm_object_t obj;
1453555b7bb4SKonstantin Belousov 	int i, pos, pos_devb;
1454031ec8c1SKonstantin Belousov 
1455555b7bb4SKonstantin Belousov 	if (written == 0 && eof >= lpos)
14569d17da3bSKonstantin Belousov 		return;
14579d17da3bSKonstantin Belousov 	obj = ma[0]->object;
145889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1459031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1460031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1461031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1462031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1463031ec8c1SKonstantin Belousov 		} else {
1464031ec8c1SKonstantin Belousov 			/* Partially written page. */
1465031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1466031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1467031ec8c1SKonstantin Belousov 		}
1468031ec8c1SKonstantin Belousov 	}
1469555b7bb4SKonstantin Belousov 	if (eof >= lpos) /* avoid truncation */
1470555b7bb4SKonstantin Belousov 		goto done;
1471555b7bb4SKonstantin Belousov 	for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1472555b7bb4SKonstantin Belousov 		if (pos != trunc_page(pos)) {
1473555b7bb4SKonstantin Belousov 			/*
1474555b7bb4SKonstantin Belousov 			 * The page contains the last valid byte in
1475555b7bb4SKonstantin Belousov 			 * the vnode, mark the rest of the page as
1476555b7bb4SKonstantin Belousov 			 * clean, potentially making the whole page
1477555b7bb4SKonstantin Belousov 			 * clean.
1478555b7bb4SKonstantin Belousov 			 */
1479555b7bb4SKonstantin Belousov 			pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1480555b7bb4SKonstantin Belousov 			vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1481555b7bb4SKonstantin Belousov 			    pos_devb);
1482555b7bb4SKonstantin Belousov 
1483555b7bb4SKonstantin Belousov 			/*
1484555b7bb4SKonstantin Belousov 			 * If the page was cleaned, report the pageout
1485555b7bb4SKonstantin Belousov 			 * on it as successful.  msync() no longer
1486555b7bb4SKonstantin Belousov 			 * needs to write out the page, endlessly
1487555b7bb4SKonstantin Belousov 			 * creating write requests and dirty buffers.
1488555b7bb4SKonstantin Belousov 			 */
1489555b7bb4SKonstantin Belousov 			if (ma[i]->dirty == 0)
1490555b7bb4SKonstantin Belousov 				rtvals[i] = VM_PAGER_OK;
1491555b7bb4SKonstantin Belousov 
1492555b7bb4SKonstantin Belousov 			pos = round_page(pos);
1493555b7bb4SKonstantin Belousov 		} else {
1494555b7bb4SKonstantin Belousov 			/* vm_pageout_flush() clears dirty */
1495555b7bb4SKonstantin Belousov 			rtvals[i] = VM_PAGER_BAD;
1496555b7bb4SKonstantin Belousov 			pos += PAGE_SIZE;
1497555b7bb4SKonstantin Belousov 		}
1498555b7bb4SKonstantin Belousov 	}
1499555b7bb4SKonstantin Belousov done:
150089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1501031ec8c1SKonstantin Belousov }
150284110e7eSKonstantin Belousov 
150384110e7eSKonstantin Belousov void
150484110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
150584110e7eSKonstantin Belousov     vm_offset_t end)
150684110e7eSKonstantin Belousov {
150784110e7eSKonstantin Belousov 	struct vnode *vp;
150884110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
150984110e7eSKonstantin Belousov 
151089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
151184110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
151289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
151384110e7eSKonstantin Belousov 		return;
151484110e7eSKonstantin Belousov 	}
151584110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
151684110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
151784110e7eSKonstantin Belousov 	vp = object->handle;
151884110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
151978022527SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "v_writecount inc");
152078022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, 1);
1521b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1522b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
152384110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
152478022527SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "v_writecount dec");
152578022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
1526b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1527b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
152884110e7eSKonstantin Belousov 	}
152989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
153084110e7eSKonstantin Belousov }
153184110e7eSKonstantin Belousov 
153284110e7eSKonstantin Belousov void
153384110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
153484110e7eSKonstantin Belousov     vm_offset_t end)
153584110e7eSKonstantin Belousov {
153684110e7eSKonstantin Belousov 	struct vnode *vp;
153784110e7eSKonstantin Belousov 	struct mount *mp;
153884110e7eSKonstantin Belousov 	vm_offset_t inc;
153984110e7eSKonstantin Belousov 
154089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
154184110e7eSKonstantin Belousov 
154284110e7eSKonstantin Belousov 	/*
154384110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
154484110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
154584110e7eSKonstantin Belousov 	 */
154684110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
154789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
154884110e7eSKonstantin Belousov 		return;
154984110e7eSKonstantin Belousov 	}
155084110e7eSKonstantin Belousov 
155184110e7eSKonstantin Belousov 	/*
155284110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
155384110e7eSKonstantin Belousov 	 * zero.
155484110e7eSKonstantin Belousov 	 */
155584110e7eSKonstantin Belousov 	inc = end - start;
155684110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
155784110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
155889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
155984110e7eSKonstantin Belousov 		return;
156084110e7eSKonstantin Belousov 	}
156184110e7eSKonstantin Belousov 
156284110e7eSKonstantin Belousov 	vp = object->handle;
156384110e7eSKonstantin Belousov 	vhold(vp);
156489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
156584110e7eSKonstantin Belousov 	mp = NULL;
156684110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
156778022527SKonstantin Belousov 	vn_lock(vp, LK_SHARED | LK_RETRY);
156884110e7eSKonstantin Belousov 
156984110e7eSKonstantin Belousov 	/*
157084110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
157184110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
157284110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
157384110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
157484110e7eSKonstantin Belousov 	 */
157584110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
157684110e7eSKonstantin Belousov 	VOP_UNLOCK(vp, 0);
157784110e7eSKonstantin Belousov 	vdrop(vp);
157884110e7eSKonstantin Belousov 	if (mp != NULL)
157984110e7eSKonstantin Belousov 		vn_finished_write(mp);
158084110e7eSKonstantin Belousov }
1581