xref: /freebsd/sys/vm/vnode_pager.c (revision 2f81c92e555669e578aaf639fba908640dbb85e5)
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 *);
103fe7bcbafSKyle Evans static void vnode_pager_update_writecount(vm_object_t, vm_offset_t,
104fe7bcbafSKyle Evans     vm_offset_t);
105fe7bcbafSKyle Evans static void vnode_pager_release_writecount(vm_object_t, vm_offset_t,
106fe7bcbafSKyle Evans     vm_offset_t);
1070b8253a7SBruce Evans 
108df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
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,
117df8bae1dSRodney W. Grimes };
118df8bae1dSRodney W. Grimes 
119e5818a53SJeff Roberson static struct domainset *vnode_domainset = NULL;
120e5818a53SJeff Roberson 
121e5818a53SJeff Roberson SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset, CTLTYPE_STRING | CTLFLAG_RW,
122e5818a53SJeff Roberson     &vnode_domainset, 0, sysctl_handle_domainset, "A",
123e5818a53SJeff Roberson     "Default vnode NUMA policy");
124e5818a53SJeff Roberson 
12566fb0b1aSGleb Smirnoff static int nvnpbufs;
12666fb0b1aSGleb Smirnoff SYSCTL_INT(_vm, OID_AUTO, vnode_pbufs, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
12766fb0b1aSGleb Smirnoff     &nvnpbufs, 0, "number of physical buffers allocated for vnode pager");
12866fb0b1aSGleb Smirnoff 
129756a5412SGleb Smirnoff static uma_zone_t vnode_pbuf_zone;
130756a5412SGleb Smirnoff 
131756a5412SGleb Smirnoff static void
132756a5412SGleb Smirnoff vnode_pager_init(void *dummy)
133756a5412SGleb Smirnoff {
134756a5412SGleb Smirnoff 
13566fb0b1aSGleb Smirnoff #ifdef __LP64__
13666fb0b1aSGleb Smirnoff 	nvnpbufs = nswbuf * 2;
13766fb0b1aSGleb Smirnoff #else
13866fb0b1aSGleb Smirnoff 	nvnpbufs = nswbuf / 2;
13966fb0b1aSGleb Smirnoff #endif
14066fb0b1aSGleb Smirnoff 	TUNABLE_INT_FETCH("vm.vnode_pbufs", &nvnpbufs);
14166fb0b1aSGleb Smirnoff 	vnode_pbuf_zone = pbuf_zsecond_create("vnpbuf", nvnpbufs);
142756a5412SGleb Smirnoff }
143756a5412SGleb Smirnoff SYSINIT(vnode_pager, SI_SUB_CPU, SI_ORDER_ANY, vnode_pager_init, NULL);
144756a5412SGleb Smirnoff 
145d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
146d07a6d3fSPoul-Henning Kamp int
147731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
148d07a6d3fSPoul-Henning Kamp {
149d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
150d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
151d07a6d3fSPoul-Henning Kamp 	struct vattr va;
152d07a6d3fSPoul-Henning Kamp 
153d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
154d07a6d3fSPoul-Henning Kamp 		return (0);
155d07a6d3fSPoul-Henning Kamp 
1566470c8d3SKonstantin Belousov 	object = vp->v_object;
1576470c8d3SKonstantin Belousov 	if (object != NULL)
158d07a6d3fSPoul-Henning Kamp 		return (0);
159d07a6d3fSPoul-Henning Kamp 
160d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
161d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
162d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
163d07a6d3fSPoul-Henning Kamp 		} else {
1640359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
165d07a6d3fSPoul-Henning Kamp 				return (0);
166d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
167d07a6d3fSPoul-Henning Kamp 		}
168d07a6d3fSPoul-Henning Kamp 	}
169d07a6d3fSPoul-Henning Kamp 
1703364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
171d07a6d3fSPoul-Henning Kamp 	/*
172d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
173d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
174d07a6d3fSPoul-Henning Kamp 	 */
17589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
176d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
17789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
178d07a6d3fSPoul-Henning Kamp 	vrele(vp);
179d07a6d3fSPoul-Henning Kamp 
180d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
181d07a6d3fSPoul-Henning Kamp 
182d07a6d3fSPoul-Henning Kamp 	return (0);
183d07a6d3fSPoul-Henning Kamp }
184d07a6d3fSPoul-Henning Kamp 
1857146d6cbSPoul-Henning Kamp void
1867146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1877146d6cbSPoul-Henning Kamp {
1887146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1897146d6cbSPoul-Henning Kamp 
1907146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1916470c8d3SKonstantin Belousov 	if (obj == NULL || obj->handle != vp)
1927146d6cbSPoul-Henning Kamp 		return;
19357fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
19489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1956470c8d3SKonstantin Belousov 	MPASS(obj->type == OBJT_VNODE);
1962a339d9eSKonstantin Belousov 	umtx_shm_object_terminated(obj);
1977146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1987146d6cbSPoul-Henning Kamp 		/*
1997146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
2007146d6cbSPoul-Henning Kamp 		 */
20190880a1bSKonstantin Belousov 		if ((obj->flags & OBJ_DEAD) == 0) {
202783a68aaSKonstantin Belousov 			vm_object_set_flag(obj, OBJ_DEAD);
203783a68aaSKonstantin Belousov 
204783a68aaSKonstantin Belousov 			/*
205783a68aaSKonstantin Belousov 			 * Clean pages and flush buffers.
206783a68aaSKonstantin Belousov 			 */
207783a68aaSKonstantin Belousov 			vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
208783a68aaSKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
209783a68aaSKonstantin Belousov 
210783a68aaSKonstantin Belousov 			vinvalbuf(vp, V_SAVE, 0, 0);
211783a68aaSKonstantin Belousov 
212783a68aaSKonstantin Belousov 			BO_LOCK(&vp->v_bufobj);
213783a68aaSKonstantin Belousov 			vp->v_bufobj.bo_flag |= BO_DEAD;
214783a68aaSKonstantin Belousov 			BO_UNLOCK(&vp->v_bufobj);
215783a68aaSKonstantin Belousov 
216783a68aaSKonstantin Belousov 			VM_OBJECT_WLOCK(obj);
2177146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
21890880a1bSKonstantin Belousov 		} else {
21990880a1bSKonstantin Belousov 			/*
22090880a1bSKonstantin Belousov 			 * Waiters were already handled during object
22190880a1bSKonstantin Belousov 			 * termination.  The exclusive vnode lock hopefully
22290880a1bSKonstantin Belousov 			 * prevented new waiters from referencing the dying
22390880a1bSKonstantin Belousov 			 * object.
22490880a1bSKonstantin Belousov 			 */
22590880a1bSKonstantin Belousov 			vp->v_object = NULL;
22689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
22790880a1bSKonstantin Belousov 		}
2287146d6cbSPoul-Henning Kamp 	} else {
2297146d6cbSPoul-Henning Kamp 		/*
2307146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
2317146d6cbSPoul-Henning Kamp 		 */
2327146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
23389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
2347146d6cbSPoul-Henning Kamp 	}
23590880a1bSKonstantin Belousov 	KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
2367146d6cbSPoul-Henning Kamp }
2377146d6cbSPoul-Henning Kamp 
2387146d6cbSPoul-Henning Kamp 
239df8bae1dSRodney W. Grimes /*
240df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
241df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
242df8bae1dSRodney W. Grimes  */
24324a1cce3SDavid Greenman vm_object_t
2446cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
2453364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
246df8bae1dSRodney W. Grimes {
24706cb7259SDavid Greenman 	vm_object_t object;
248df8bae1dSRodney W. Grimes 	struct vnode *vp;
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes 	/*
251df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
252df8bae1dSRodney W. Grimes 	 */
253df8bae1dSRodney W. Grimes 	if (handle == NULL)
254df8bae1dSRodney W. Grimes 		return (NULL);
255df8bae1dSRodney W. Grimes 
256df8bae1dSRodney W. Grimes 	vp = (struct vnode *)handle;
2576470c8d3SKonstantin Belousov 	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
2586ded8427SKonstantin Belousov 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
2596470c8d3SKonstantin Belousov retry:
2606470c8d3SKonstantin Belousov 	object = vp->v_object;
2612be70f79SJohn Dyson 
26224a1cce3SDavid Greenman 	if (object == NULL) {
263df8bae1dSRodney W. Grimes 		/*
2642ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
265df8bae1dSRodney W. Grimes 		 */
2666470c8d3SKonstantin Belousov 		object = vm_object_allocate(OBJT_VNODE,
2676470c8d3SKonstantin Belousov 		    OFF_TO_IDX(round_page(size)));
268bbc0ec52SDavid Greenman 
2696cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
27084110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
271e5818a53SJeff Roberson 		object->domain.dr_policy = vnode_domainset;
27224a1cce3SDavid Greenman 		object->handle = handle;
273208b81bbSKonstantin Belousov 		if ((vp->v_vflag & VV_VMSIZEVNLOCK) != 0) {
274208b81bbSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
275208b81bbSKonstantin Belousov 			vm_object_set_flag(object, OBJ_SIZEVNLOCK);
276208b81bbSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
277208b81bbSKonstantin Belousov 		}
27811be8415SStephan Uphoff 		VI_LOCK(vp);
2792ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2802ac78f0eSStephan Uphoff 			/*
2816470c8d3SKonstantin Belousov 			 * Object has been created while we were allocating.
2822ac78f0eSStephan Uphoff 			 */
28311be8415SStephan Uphoff 			VI_UNLOCK(vp);
2849cddade7SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
2859cddade7SKonstantin Belousov 			KASSERT(object->ref_count == 1,
2869cddade7SKonstantin Belousov 			    ("leaked ref %p %d", object, object->ref_count));
2879cddade7SKonstantin Belousov 			object->type = OBJT_DEAD;
2889cddade7SKonstantin Belousov 			object->ref_count = 0;
2899cddade7SKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
2902ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2912ac78f0eSStephan Uphoff 			goto retry;
292df8bae1dSRodney W. Grimes 		}
2932ac78f0eSStephan Uphoff 		vp->v_object = object;
29411be8415SStephan Uphoff 		VI_UNLOCK(vp);
29511be8415SStephan Uphoff 	} else {
2966470c8d3SKonstantin Belousov 		VM_OBJECT_WLOCK(object);
2972ac78f0eSStephan Uphoff 		object->ref_count++;
2983d653db0SAlan Cox #if VM_NRESERVLEVEL > 0
2993d653db0SAlan Cox 		vm_object_color(object, 0);
3003d653db0SAlan Cox #endif
30189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
30211be8415SStephan Uphoff 	}
3036ff51a36SMateusz Guzik 	vrefact(vp);
30424a1cce3SDavid Greenman 	return (object);
305df8bae1dSRodney W. Grimes }
306df8bae1dSRodney W. Grimes 
307658ad5ffSAlan Cox /*
308658ad5ffSAlan Cox  *	The object must be locked.
309658ad5ffSAlan Cox  */
310f708ef1bSPoul-Henning Kamp static void
3117ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
31224a1cce3SDavid Greenman {
313b9f180d1SKonstantin Belousov 	struct vnode *vp;
314b9f180d1SKonstantin Belousov 	int refs;
315df8bae1dSRodney W. Grimes 
316b9f180d1SKonstantin Belousov 	vp = object->handle;
31724a1cce3SDavid Greenman 	if (vp == NULL)
31824a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
31924a1cce3SDavid Greenman 
32089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
32166095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
322b9f180d1SKonstantin Belousov 	refs = object->ref_count;
32324a1cce3SDavid Greenman 
32424a1cce3SDavid Greenman 	object->handle = NULL;
32595461b45SJohn Dyson 	object->type = OBJT_DEAD;
32657fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
32784110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
32884110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
32978022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
330b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
331b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
33284110e7eSKonstantin Belousov 	}
333aa2cabb9SDavid Greenman 	vp->v_object = NULL;
33478022527SKonstantin Belousov 	VI_LOCK(vp);
33578022527SKonstantin Belousov 
33678022527SKonstantin Belousov 	/*
33778022527SKonstantin Belousov 	 * vm_map_entry_set_vnode_text() cannot reach this vnode by
33878022527SKonstantin Belousov 	 * following object->handle.  Clear all text references now.
33978022527SKonstantin Belousov 	 * This also clears the transient references from
34078022527SKonstantin Belousov 	 * kern_execve(), which is fine because dead_vnodeops uses nop
34178022527SKonstantin Belousov 	 * for VOP_UNSET_TEXT().
34278022527SKonstantin Belousov 	 */
34378022527SKonstantin Belousov 	if (vp->v_writecount < 0)
34478022527SKonstantin Belousov 		vp->v_writecount = 0;
34578022527SKonstantin Belousov 	VI_UNLOCK(vp);
34689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
347b9f180d1SKonstantin Belousov 	while (refs-- > 0)
348b9f180d1SKonstantin Belousov 		vunref(vp);
34989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
350df8bae1dSRodney W. Grimes }
35126f9a767SRodney W. Grimes 
352f708ef1bSPoul-Henning Kamp static boolean_t
3537ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
3547ebba1f8SGleb Smirnoff     int *after)
355df8bae1dSRodney W. Grimes {
35624a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
35798b0c789SPoul-Henning Kamp 	daddr_t bn;
3584153054aSJeff Roberson 	uintptr_t lockstate;
3593af76890SPoul-Henning Kamp 	int err;
360170db9c6SJohn Dyson 	daddr_t reqblock;
3612c4488fcSJohn Dyson 	int poff;
3622c4488fcSJohn Dyson 	int bsize;
363d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
364df8bae1dSRodney W. Grimes 
3654153054aSJeff Roberson 	VM_OBJECT_ASSERT_LOCKED(object);
36624579ca1SMatthew Dillon 	/*
36724579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
36824579ca1SMatthew Dillon 	 * have the page.
36924579ca1SMatthew Dillon 	 */
370b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
37147221757SJohn Dyson 		return FALSE;
372df8bae1dSRodney W. Grimes 	/*
373b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3740d94caffSDavid Greenman 	 * not have the page.
375df8bae1dSRodney W. Grimes 	 */
376b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3774abc71c0SDavid Greenman 		return FALSE;
378df8bae1dSRodney W. Grimes 
379eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
380170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
381d63596ceSJohn Dyson 	blocksperpage = 0;
382d63596ceSJohn Dyson 	if (pagesperblock > 0) {
383a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
384d63596ceSJohn Dyson 	} else {
385d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
386d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
387d63596ceSJohn Dyson 	}
3884153054aSJeff Roberson 	lockstate = VM_OBJECT_DROP(object);
389ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
3904153054aSJeff Roberson 	VM_OBJECT_PICKUP(object, lockstate);
3910d94caffSDavid Greenman 	if (err)
39224a1cce3SDavid Greenman 		return TRUE;
3936eab77f2SJohn Dyson 	if (bn == -1)
394ced399eeSJohn Dyson 		return FALSE;
395d63596ceSJohn Dyson 	if (pagesperblock > 0) {
396a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
397170db9c6SJohn Dyson 		if (before) {
398170db9c6SJohn Dyson 			*before *= pagesperblock;
399170db9c6SJohn Dyson 			*before += poff;
400170db9c6SJohn Dyson 		}
401170db9c6SJohn Dyson 		if (after) {
40284d31376SGleb Smirnoff 			/*
40384d31376SGleb Smirnoff 			 * The BMAP vop can report a partial block in the
404d2596d17SGleb Smirnoff 			 * 'after', but must not report blocks after EOF.
40584d31376SGleb Smirnoff 			 * Assert the latter, and truncate 'after' in case
40684d31376SGleb Smirnoff 			 * of the former.
40784d31376SGleb Smirnoff 			 */
408d2596d17SGleb Smirnoff 			KASSERT((reqblock + *after) * pagesperblock <
409d2596d17SGleb Smirnoff 			    roundup2(object->size, pagesperblock),
41084d31376SGleb Smirnoff 			    ("%s: reqblock %jd after %d size %ju", __func__,
41184d31376SGleb Smirnoff 			    (intmax_t )reqblock, *after,
41284d31376SGleb Smirnoff 			    (uintmax_t )object->size));
413170db9c6SJohn Dyson 			*after *= pagesperblock;
41484d31376SGleb Smirnoff 			*after += pagesperblock - (poff + 1);
41584d31376SGleb Smirnoff 			if (pindex + *after >= object->size)
41684d31376SGleb Smirnoff 				*after = object->size - 1 - pindex;
417170db9c6SJohn Dyson 		}
418d63596ceSJohn Dyson 	} else {
419d63596ceSJohn Dyson 		if (before) {
420d63596ceSJohn Dyson 			*before /= blocksperpage;
421d63596ceSJohn Dyson 		}
422d63596ceSJohn Dyson 
423d63596ceSJohn Dyson 		if (after) {
424d63596ceSJohn Dyson 			*after /= blocksperpage;
425d63596ceSJohn Dyson 		}
426d63596ceSJohn Dyson 	}
427ced399eeSJohn Dyson 	return TRUE;
428df8bae1dSRodney W. Grimes }
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes /*
431df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
43224a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
433df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
434df8bae1dSRodney W. Grimes  *
435df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
436df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
437df8bae1dSRodney W. Grimes  */
438df8bae1dSRodney W. Grimes void
4397ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
440df8bae1dSRodney W. Grimes {
4412a8f9ab5SAlan Cox 	vm_object_t object;
4422a8f9ab5SAlan Cox 	vm_page_t m;
443c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
444df8bae1dSRodney W. Grimes 
4452a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
446df8bae1dSRodney W. Grimes 		return;
4475b87ecc6SKonstantin Belousov #ifdef DEBUG_VFS_LOCKS
4485b87ecc6SKonstantin Belousov 	{
4495b87ecc6SKonstantin Belousov 		struct mount *mp;
4505b87ecc6SKonstantin Belousov 
4515b87ecc6SKonstantin Belousov 		mp = vp->v_mount;
4525b87ecc6SKonstantin Belousov 		if (mp != NULL && (mp->mnt_kern_flag & MNTK_VMSETSIZE_BUG) == 0)
4535b87ecc6SKonstantin Belousov 			assert_vop_elocked(vp,
4545b87ecc6SKonstantin Belousov 			    "vnode_pager_setsize and not locked vnode");
4555b87ecc6SKonstantin Belousov 	}
4565b87ecc6SKonstantin Belousov #endif
45789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
4589b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
4599b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
4609b8851faSKonstantin Belousov 		return;
4619b8851faSKonstantin Belousov 	}
4629b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
4639b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
4642a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
465df8bae1dSRodney W. Grimes 		/*
466df8bae1dSRodney W. Grimes 		 * Hasn't changed size
467df8bae1dSRodney W. Grimes 		 */
46889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
469df8bae1dSRodney W. Grimes 		return;
4702a8f9ab5SAlan Cox 	}
471c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
4722a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
473df8bae1dSRodney W. Grimes 		/*
474bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
475df8bae1dSRodney W. Grimes 		 */
4762a8f9ab5SAlan Cox 		if (nobjsize < object->size)
477c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
4786bbee8e2SAlan Cox 			    0);
479bbc0ec52SDavid Greenman 		/*
480bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4813ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4823ebeaf59SMatthew Dillon 		 *
4833ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4843ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4853ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
486bbc0ec52SDavid Greenman 		 */
4870012f373SJeff Roberson 		if (!(nsize & PAGE_MASK))
4880012f373SJeff Roberson 			goto out;
4890012f373SJeff Roberson 		m = vm_page_grab(object, OFF_TO_IDX(nsize), VM_ALLOC_NOCREAT);
4900012f373SJeff Roberson 		if (m == NULL)
4910012f373SJeff Roberson 			goto out;
4920012f373SJeff Roberson 		if (!vm_page_none_valid(m)) {
4932b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4942b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4952b6b0df7SMatthew Dillon 
4962b6b0df7SMatthew Dillon 			/*
4972b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4982b6b0df7SMatthew Dillon 			 * the page has been mapped.
4992b6b0df7SMatthew Dillon 			 */
500fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
5012b6b0df7SMatthew Dillon 
5022b6b0df7SMatthew Dillon 			/*
5033c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
5043c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
5053c33df62SAlan Cox 			 * have already been set.
5063c33df62SAlan Cox 			 */
507dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
5083c33df62SAlan Cox 
5093c33df62SAlan Cox 			/*
5103c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
5113c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
5123c33df62SAlan Cox 			 * cleared.
5133c33df62SAlan Cox 			 */
5143c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
5153c33df62SAlan Cox 
5163c33df62SAlan Cox 			/*
5173c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
5183ebeaf59SMatthew Dillon 			 *
5193ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
5203ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
5213ebeaf59SMatthew Dillon 			 * replacement from working properly.
5222b6b0df7SMatthew Dillon 			 */
5233c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
524bbc0ec52SDavid Greenman 		}
5250012f373SJeff Roberson 		vm_page_xunbusy(m);
526bbc0ec52SDavid Greenman 	}
5270012f373SJeff Roberson out:
528a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
529c576d121SLuoqi Chen 	object->size = nobjsize;
53089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
531df8bae1dSRodney W. Grimes }
532df8bae1dSRodney W. Grimes 
53326f9a767SRodney W. Grimes /*
53426f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
53526f9a767SRodney W. Grimes  * file address
53626f9a767SRodney W. Grimes  */
537bff76343SAlan Cox static int
538bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
539bff76343SAlan Cox     int *run)
54026f9a767SRodney W. Grimes {
54126f9a767SRodney W. Grimes 	int bsize;
54226f9a767SRodney W. Grimes 	int err;
543a316d390SJohn Dyson 	daddr_t vblock;
544f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
54526f9a767SRodney W. Grimes 
5462ad036b6SAlan Cox 	if (address < 0)
5470d94caffSDavid Greenman 		return -1;
5480d94caffSDavid Greenman 
549b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5502c4488fcSJohn Dyson 		return -1;
5512c4488fcSJohn Dyson 
55226f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
55326f9a767SRodney W. Grimes 	vblock = address / bsize;
55426f9a767SRodney W. Grimes 	voffset = address % bsize;
55526f9a767SRodney W. Grimes 
556bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
557bff76343SAlan Cox 	if (err == 0) {
558bff76343SAlan Cox 		if (*rtaddress != -1)
559bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
560efc68ce1SDavid Greenman 		if (run) {
561efc68ce1SDavid Greenman 			*run += 1;
562efc68ce1SDavid Greenman 			*run *= bsize / PAGE_SIZE;
563efc68ce1SDavid Greenman 			*run -= voffset / PAGE_SIZE;
564efc68ce1SDavid Greenman 		}
565efc68ce1SDavid Greenman 	}
56626f9a767SRodney W. Grimes 
567bff76343SAlan Cox 	return (err);
56826f9a767SRodney W. Grimes }
56926f9a767SRodney W. Grimes 
57026f9a767SRodney W. Grimes /*
57126f9a767SRodney W. Grimes  * small block filesystem vnode pager input
57226f9a767SRodney W. Grimes  */
573f708ef1bSPoul-Henning Kamp static int
5747ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
57526f9a767SRodney W. Grimes {
5769c83534dSPoul-Henning Kamp 	struct vnode *vp;
5779c83534dSPoul-Henning Kamp 	struct bufobj *bo;
57826f9a767SRodney W. Grimes 	struct buf *bp;
5799e0ddbd0SAlan Cox 	struct sf_buf *sf;
580f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
58126f9a767SRodney W. Grimes 	vm_offset_t bsize;
582561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
583561cc9fcSKonstantin Belousov 	int error, i;
58426f9a767SRodney W. Grimes 
585561cc9fcSKonstantin Belousov 	error = 0;
58624a1cce3SDavid Greenman 	vp = object->handle;
587b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5882c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5892c4488fcSJohn Dyson 
59026f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5910bdb7528SDavid Greenman 
5929c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
59326f9a767SRodney W. Grimes 
5949e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
59526f9a767SRodney W. Grimes 
59626f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
59733c67741SMatthew Dillon 		vm_ooffset_t address;
598bbc0ec52SDavid Greenman 
5990d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
6000d53a17bSAlan Cox 		if (m->valid & bits)
60126f9a767SRodney W. Grimes 			continue;
60226f9a767SRodney W. Grimes 
60333c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
60433c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
60533c67741SMatthew Dillon 			fileaddr = -1;
60633c67741SMatthew Dillon 		} else {
607bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
608bff76343SAlan Cox 			if (error)
609bff76343SAlan Cox 				break;
61033c67741SMatthew Dillon 		}
61126f9a767SRodney W. Grimes 		if (fileaddr != -1) {
612756a5412SGleb Smirnoff 			bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
61326f9a767SRodney W. Grimes 
61426f9a767SRodney W. Grimes 			/* build a minimal buffer header */
61521144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
6166a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
617bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
618bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
619a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
620a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
6219e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
622187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
6239c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
6241faacf5dSKirk McKusick 			bp->b_vp = vp;
62526f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
62626f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
6272b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
6285bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
62926f9a767SRodney W. Grimes 
63026f9a767SRodney W. Grimes 			/* do the input */
6312c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
632b792bebeSPoul-Henning Kamp 			bstrategy(bp);
63326f9a767SRodney W. Grimes 
6346a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
6356a4b5823SPoul-Henning Kamp 
636c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
63726f9a767SRodney W. Grimes 				error = EIO;
63826f9a767SRodney W. Grimes 
63926f9a767SRodney W. Grimes 			/*
64026f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
64126f9a767SRodney W. Grimes 			 */
6421faacf5dSKirk McKusick 			bp->b_vp = NULL;
6439c83534dSPoul-Henning Kamp 			pbrelbo(bp);
644756a5412SGleb Smirnoff 			uma_zfree(vnode_pbuf_zone, bp);
64526f9a767SRodney W. Grimes 			if (error)
64626f9a767SRodney W. Grimes 				break;
6470d53a17bSAlan Cox 		} else
6489e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
6490d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
6500d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
65189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
6520d53a17bSAlan Cox 		m->valid |= bits;
65389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
65426f9a767SRodney W. Grimes 	}
6559e0ddbd0SAlan Cox 	sf_buf_free(sf);
65626f9a767SRodney W. Grimes 	if (error) {
657a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
65826f9a767SRodney W. Grimes 	}
65926f9a767SRodney W. Grimes 	return VM_PAGER_OK;
66026f9a767SRodney W. Grimes }
66126f9a767SRodney W. Grimes 
66226f9a767SRodney W. Grimes /*
663475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
66426f9a767SRodney W. Grimes  */
665f708ef1bSPoul-Henning Kamp static int
6667ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
66726f9a767SRodney W. Grimes {
668df8bae1dSRodney W. Grimes 	struct uio auio;
669df8bae1dSRodney W. Grimes 	struct iovec aiov;
67026f9a767SRodney W. Grimes 	int error;
67126f9a767SRodney W. Grimes 	int size;
6729e0ddbd0SAlan Cox 	struct sf_buf *sf;
673342a1480SJohn Baldwin 	struct vnode *vp;
674df8bae1dSRodney W. Grimes 
67589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
67626f9a767SRodney W. Grimes 	error = 0;
677bbc0ec52SDavid Greenman 
678df8bae1dSRodney W. Grimes 	/*
67926f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
68026f9a767SRodney W. Grimes 	 */
681a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
68226f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
68326f9a767SRodney W. Grimes 	} else {
68426f9a767SRodney W. Grimes 		size = PAGE_SIZE;
685a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
686a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
68752051abcSAlan Cox 		vp = object->handle;
68889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
6890bdb7528SDavid Greenman 
69026f9a767SRodney W. Grimes 		/*
691df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
692df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
693df8bae1dSRodney W. Grimes 		 */
6949e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6950bdb7528SDavid Greenman 
6969e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
697df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
698df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
699df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
700a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
701df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
70226f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
703df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
704b40ce416SJulian Elischer 		auio.uio_td = curthread;
70526f9a767SRodney W. Grimes 
706a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
707df8bae1dSRodney W. Grimes 		if (!error) {
70854d92145SMatthew Dillon 			int count = size - auio.uio_resid;
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes 			if (count == 0)
711df8bae1dSRodney W. Grimes 				error = EINVAL;
71226f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
7139e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
7149e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
715df8bae1dSRodney W. Grimes 		}
7169e0ddbd0SAlan Cox 		sf_buf_free(sf);
7171b26eb10SAlan Cox 
71889f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
719df8bae1dSRodney W. Grimes 	}
7200d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
7216e3a3f38SRobert V. Baron 	if (!error)
7220012f373SJeff Roberson 		vm_page_valid(m);
723a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
72426f9a767SRodney W. Grimes }
72526f9a767SRodney W. Grimes 
72626f9a767SRodney W. Grimes /*
72726f9a767SRodney W. Grimes  * generic vnode pager input routine
72826f9a767SRodney W. Grimes  */
729170db9c6SJohn Dyson 
730ce75f2c3SMike Smith /*
73123955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
73247e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
73347e151ddSRobert Drehmel  * to implement the previous behaviour.
734ce75f2c3SMike Smith  *
735ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
736ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
737ce75f2c3SMike Smith  */
738f708ef1bSPoul-Henning Kamp static int
739b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
740b0cd2017SGleb Smirnoff     int *rahead)
74124a1cce3SDavid Greenman {
742170db9c6SJohn Dyson 	struct vnode *vp;
743b0cd2017SGleb Smirnoff 	int rtval;
74495e5e988SJohn Dyson 
745170db9c6SJohn Dyson 	vp = object->handle;
74689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
747b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
74823955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
74923955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
75089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
751170db9c6SJohn Dyson 	return rtval;
752170db9c6SJohn Dyson }
753170db9c6SJohn Dyson 
75490effb23SGleb Smirnoff static int
75590effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
756b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
75790effb23SGleb Smirnoff {
75890effb23SGleb Smirnoff 	struct vnode *vp;
75990effb23SGleb Smirnoff 	int rtval;
76090effb23SGleb Smirnoff 
76190effb23SGleb Smirnoff 	vp = object->handle;
76290effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
763b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
76490effb23SGleb Smirnoff 	KASSERT(rtval != EOPNOTSUPP,
76590effb23SGleb Smirnoff 	    ("vnode_pager: FS getpages_async not implemented\n"));
76690effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
76790effb23SGleb Smirnoff 	return (rtval);
76890effb23SGleb Smirnoff }
76990effb23SGleb Smirnoff 
770ce75f2c3SMike Smith /*
77190effb23SGleb Smirnoff  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
77290effb23SGleb Smirnoff  * local filesystems, where partially valid pages can only occur at
77390effb23SGleb Smirnoff  * the end of file.
774d15b55c5SKonstantin Belousov  */
775d15b55c5SKonstantin Belousov int
776d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
777d15b55c5SKonstantin Belousov {
77890effb23SGleb Smirnoff 
779b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
780b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
78190effb23SGleb Smirnoff }
78290effb23SGleb Smirnoff 
78390effb23SGleb Smirnoff int
78490effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
78590effb23SGleb Smirnoff {
78690effb23SGleb Smirnoff 
787b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
788b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
789d15b55c5SKonstantin Belousov }
790d15b55c5SKonstantin Belousov 
791d15b55c5SKonstantin Belousov /*
792ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
793ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
794ce75f2c3SMike Smith  */
795ce75f2c3SMike Smith int
796b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
797b0cd2017SGleb Smirnoff     int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
798170db9c6SJohn Dyson {
799ce75f2c3SMike Smith 	vm_object_t object;
8009c83534dSPoul-Henning Kamp 	struct bufobj *bo;
8010bdb7528SDavid Greenman 	struct buf *bp;
802b0cd2017SGleb Smirnoff 	off_t foff;
803e48b82bdSGleb Smirnoff #ifdef INVARIANTS
804e48b82bdSGleb Smirnoff 	off_t blkno0;
805e48b82bdSGleb Smirnoff #endif
806756a5412SGleb Smirnoff 	int bsize, pagesperblock;
807b0cd2017SGleb Smirnoff 	int error, before, after, rbehind, rahead, poff, i;
808b0cd2017SGleb Smirnoff 	int bytecount, secmask;
809ce75f2c3SMike Smith 
8109c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
811b0cd2017SGleb Smirnoff 	    ("%s does not support devices", __func__));
812b0cd2017SGleb Smirnoff 
813b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
814eac91e32SKonstantin Belousov 		return (VM_PAGER_BAD);
8152c4488fcSJohn Dyson 
816eac91e32SKonstantin Belousov 	object = vp->v_object;
817b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(m[0]->pindex);
81826f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
819b0cd2017SGleb Smirnoff 	pagesperblock = bsize / PAGE_SIZE;
820b0cd2017SGleb Smirnoff 
821b0cd2017SGleb Smirnoff 	KASSERT(foff < object->un_pager.vnp.vnp_size,
822b0cd2017SGleb Smirnoff 	    ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
82340a51684SJason A. Harmening 	KASSERT(count <= nitems(bp->b_pages),
824b0cd2017SGleb Smirnoff 	    ("%s: requested %d pages", __func__, count));
825b0cd2017SGleb Smirnoff 
826b0cd2017SGleb Smirnoff 	/*
827b0cd2017SGleb Smirnoff 	 * The last page has valid blocks.  Invalid part can only
828b0cd2017SGleb Smirnoff 	 * exist at the end of file, and the page is made fully valid
829b0cd2017SGleb Smirnoff 	 * by zeroing in vm_pager_get_pages().
830b0cd2017SGleb Smirnoff 	 */
8310012f373SJeff Roberson 	if (!vm_page_none_valid(m[count - 1]) && --count == 0) {
832b0cd2017SGleb Smirnoff 		if (iodone != NULL)
833b0cd2017SGleb Smirnoff 			iodone(arg, m, 1, 0);
834b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
835b0cd2017SGleb Smirnoff 	}
836bbc0ec52SDavid Greenman 
837756a5412SGleb Smirnoff 	bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
83873e9030eSGleb Smirnoff 
83926f9a767SRodney W. Grimes 	/*
840e122dfc1SGleb Smirnoff 	 * Get the underlying device blocks for the file with VOP_BMAP().
841e122dfc1SGleb Smirnoff 	 * If the file system doesn't support VOP_BMAP, use old way of
842e122dfc1SGleb Smirnoff 	 * getting pages via VOP_READ.
84326f9a767SRodney W. Grimes 	 */
844b0cd2017SGleb Smirnoff 	error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
8451de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
846756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
84789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
848b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
84983c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodein);
85083c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodepgsin);
851b0cd2017SGleb Smirnoff 			error = vnode_pager_input_old(object, m[i]);
852b0cd2017SGleb Smirnoff 			if (error)
853b0cd2017SGleb Smirnoff 				break;
854b0cd2017SGleb Smirnoff 		}
85589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
85652051abcSAlan Cox 		return (error);
8571de11f1aSAlan Cox 	} else if (error != 0) {
858756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
8591de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
860b0cd2017SGleb Smirnoff 	}
861bbc0ec52SDavid Greenman 
86226f9a767SRodney W. Grimes 	/*
863b0cd2017SGleb Smirnoff 	 * If the file system supports BMAP, but blocksize is smaller
864b0cd2017SGleb Smirnoff 	 * than a page size, then use special small filesystem code.
86526f9a767SRodney W. Grimes 	 */
866b0cd2017SGleb Smirnoff 	if (pagesperblock == 0) {
867756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
868b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
86983c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodein);
87083c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodepgsin);
871b0cd2017SGleb Smirnoff 			error = vnode_pager_input_smlfs(object, m[i]);
872b0cd2017SGleb Smirnoff 			if (error)
873b0cd2017SGleb Smirnoff 				break;
874b0cd2017SGleb Smirnoff 		}
875b0cd2017SGleb Smirnoff 		return (error);
87626f9a767SRodney W. Grimes 	}
8778d17e694SJulian Elischer 
87826f9a767SRodney W. Grimes 	/*
879b0cd2017SGleb Smirnoff 	 * A sparse file can be encountered only for a single page request,
880763df3ecSPedro F. Giffuni 	 * which may not be preceded by call to vm_pager_haspage().
881a7fecb4dSAlan Cox 	 */
882b0cd2017SGleb Smirnoff 	if (bp->b_blkno == -1) {
883b0cd2017SGleb Smirnoff 		KASSERT(count == 1,
884b0cd2017SGleb Smirnoff 		    ("%s: array[%d] request to a sparse file %p", __func__,
885b0cd2017SGleb Smirnoff 		    count, vp));
886756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
887b0cd2017SGleb Smirnoff 		pmap_zero_page(m[0]);
888b0cd2017SGleb Smirnoff 		KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
889b0cd2017SGleb Smirnoff 		    __func__, m[0]));
890a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
8910012f373SJeff Roberson 		vm_page_valid(m[0]);
89289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
893f4f83da0SAlan Cox 		return (VM_PAGER_OK);
894b0cd2017SGleb Smirnoff 	}
895b0cd2017SGleb Smirnoff 
896e48b82bdSGleb Smirnoff #ifdef INVARIANTS
897e48b82bdSGleb Smirnoff 	blkno0 = bp->b_blkno;
898e48b82bdSGleb Smirnoff #endif
899b0cd2017SGleb Smirnoff 	bp->b_blkno += (foff % bsize) / DEV_BSIZE;
900b0cd2017SGleb Smirnoff 
901b0cd2017SGleb Smirnoff 	/* Recalculate blocks available after/before to pages. */
902b0cd2017SGleb Smirnoff 	poff = (foff % bsize) / PAGE_SIZE;
903b0cd2017SGleb Smirnoff 	before *= pagesperblock;
904b0cd2017SGleb Smirnoff 	before += poff;
905b0cd2017SGleb Smirnoff 	after *= pagesperblock;
906b0cd2017SGleb Smirnoff 	after += pagesperblock - (poff + 1);
907b0cd2017SGleb Smirnoff 	if (m[0]->pindex + after >= object->size)
908b0cd2017SGleb Smirnoff 		after = object->size - 1 - m[0]->pindex;
909b0cd2017SGleb Smirnoff 	KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
910b0cd2017SGleb Smirnoff 	    __func__, count, after + 1));
911b0cd2017SGleb Smirnoff 	after -= count - 1;
912b0cd2017SGleb Smirnoff 
913b0cd2017SGleb Smirnoff 	/* Trim requested rbehind/rahead to possible values. */
914b0cd2017SGleb Smirnoff 	rbehind = a_rbehind ? *a_rbehind : 0;
915b0cd2017SGleb Smirnoff 	rahead = a_rahead ? *a_rahead : 0;
916b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, before);
917b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, m[0]->pindex);
918b0cd2017SGleb Smirnoff 	rahead = min(rahead, after);
919b0cd2017SGleb Smirnoff 	rahead = min(rahead, object->size - m[count - 1]->pindex);
920e48b82bdSGleb Smirnoff 	/*
921e48b82bdSGleb Smirnoff 	 * Check that total amount of pages fit into buf.  Trim rbehind and
922e48b82bdSGleb Smirnoff 	 * rahead evenly if not.
923e48b82bdSGleb Smirnoff 	 */
924e48b82bdSGleb Smirnoff 	if (rbehind + rahead + count > nitems(bp->b_pages)) {
925e48b82bdSGleb Smirnoff 		int trim, sum;
926e48b82bdSGleb Smirnoff 
927e48b82bdSGleb Smirnoff 		trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
928e48b82bdSGleb Smirnoff 		sum = rbehind + rahead;
929e48b82bdSGleb Smirnoff 		if (rbehind == before) {
930e48b82bdSGleb Smirnoff 			/* Roundup rbehind trim to block size. */
931e48b82bdSGleb Smirnoff 			rbehind -= roundup(trim * rbehind / sum, pagesperblock);
932e48b82bdSGleb Smirnoff 			if (rbehind < 0)
933e48b82bdSGleb Smirnoff 				rbehind = 0;
934e48b82bdSGleb Smirnoff 		} else
935e48b82bdSGleb Smirnoff 			rbehind -= trim * rbehind / sum;
936e48b82bdSGleb Smirnoff 		rahead -= trim * rahead / sum;
937e48b82bdSGleb Smirnoff 	}
938e48b82bdSGleb Smirnoff 	KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
939b0cd2017SGleb Smirnoff 	    ("%s: behind %d ahead %d count %d", __func__,
940b0cd2017SGleb Smirnoff 	    rbehind, rahead, count));
941b0cd2017SGleb Smirnoff 
942b0cd2017SGleb Smirnoff 	/*
943b0cd2017SGleb Smirnoff 	 * Fill in the bp->b_pages[] array with requested and optional
944b0cd2017SGleb Smirnoff 	 * read behind or read ahead pages.  Read behind pages are looked
945b0cd2017SGleb Smirnoff 	 * up in a backward direction, down to a first cached page.  Same
946b0cd2017SGleb Smirnoff 	 * for read ahead pages, but there is no need to shift the array
947b0cd2017SGleb Smirnoff 	 * in case of encountering a cached page.
948b0cd2017SGleb Smirnoff 	 */
949b0cd2017SGleb Smirnoff 	i = bp->b_npages = 0;
950b0cd2017SGleb Smirnoff 	if (rbehind) {
951b0cd2017SGleb Smirnoff 		vm_pindex_t startpindex, tpindex;
952b0cd2017SGleb Smirnoff 		vm_page_t p;
953b0cd2017SGleb Smirnoff 
954a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
955b0cd2017SGleb Smirnoff 		startpindex = m[0]->pindex - rbehind;
956b0cd2017SGleb Smirnoff 		if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
957b0cd2017SGleb Smirnoff 		    p->pindex >= startpindex)
958b0cd2017SGleb Smirnoff 			startpindex = p->pindex + 1;
959b0cd2017SGleb Smirnoff 
960b0cd2017SGleb Smirnoff 		/* tpindex is unsigned; beware of numeric underflow. */
961b0cd2017SGleb Smirnoff 		for (tpindex = m[0]->pindex - 1;
962b0cd2017SGleb Smirnoff 		    tpindex >= startpindex && tpindex < m[0]->pindex;
963b0cd2017SGleb Smirnoff 		    tpindex--, i++) {
9647667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
965b0cd2017SGleb Smirnoff 			if (p == NULL) {
966b0cd2017SGleb Smirnoff 				/* Shift the array. */
967b0cd2017SGleb Smirnoff 				for (int j = 0; j < i; j++)
968b0cd2017SGleb Smirnoff 					bp->b_pages[j] = bp->b_pages[j +
969b0cd2017SGleb Smirnoff 					    tpindex + 1 - startpindex];
970b0cd2017SGleb Smirnoff 				break;
971b0cd2017SGleb Smirnoff 			}
972b0cd2017SGleb Smirnoff 			bp->b_pages[tpindex - startpindex] = p;
973a7fecb4dSAlan Cox 		}
9740bdb7528SDavid Greenman 
975b0cd2017SGleb Smirnoff 		bp->b_pgbefore = i;
976b0cd2017SGleb Smirnoff 		bp->b_npages += i;
977b0cd2017SGleb Smirnoff 		bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
978b0cd2017SGleb Smirnoff 	} else
979b0cd2017SGleb Smirnoff 		bp->b_pgbefore = 0;
980b0cd2017SGleb Smirnoff 
981b0cd2017SGleb Smirnoff 	/* Requested pages. */
982b0cd2017SGleb Smirnoff 	for (int j = 0; j < count; j++, i++)
983b0cd2017SGleb Smirnoff 		bp->b_pages[i] = m[j];
984b0cd2017SGleb Smirnoff 	bp->b_npages += count;
985b0cd2017SGleb Smirnoff 
986b0cd2017SGleb Smirnoff 	if (rahead) {
987b0cd2017SGleb Smirnoff 		vm_pindex_t endpindex, tpindex;
988b0cd2017SGleb Smirnoff 		vm_page_t p;
989b0cd2017SGleb Smirnoff 
990b0cd2017SGleb Smirnoff 		if (!VM_OBJECT_WOWNED(object))
991eac91e32SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
992b0cd2017SGleb Smirnoff 		endpindex = m[count - 1]->pindex + rahead + 1;
993b0cd2017SGleb Smirnoff 		if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
994b0cd2017SGleb Smirnoff 		    p->pindex < endpindex)
995b0cd2017SGleb Smirnoff 			endpindex = p->pindex;
996b0cd2017SGleb Smirnoff 		if (endpindex > object->size)
997b0cd2017SGleb Smirnoff 			endpindex = object->size;
998b0cd2017SGleb Smirnoff 
999b0cd2017SGleb Smirnoff 		for (tpindex = m[count - 1]->pindex + 1;
1000b0cd2017SGleb Smirnoff 		    tpindex < endpindex; i++, tpindex++) {
10017667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1002b0cd2017SGleb Smirnoff 			if (p == NULL)
1003b0cd2017SGleb Smirnoff 				break;
1004b0cd2017SGleb Smirnoff 			bp->b_pages[i] = p;
1005eac91e32SKonstantin Belousov 		}
1006b0cd2017SGleb Smirnoff 
1007b0cd2017SGleb Smirnoff 		bp->b_pgafter = i - bp->b_npages;
1008b0cd2017SGleb Smirnoff 		bp->b_npages = i;
1009b0cd2017SGleb Smirnoff 	} else
1010b0cd2017SGleb Smirnoff 		bp->b_pgafter = 0;
1011b0cd2017SGleb Smirnoff 
1012b0cd2017SGleb Smirnoff 	if (VM_OBJECT_WOWNED(object))
1013eac91e32SKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
1014b0cd2017SGleb Smirnoff 
1015b0cd2017SGleb Smirnoff 	/* Report back actual behind/ahead read. */
1016b0cd2017SGleb Smirnoff 	if (a_rbehind)
1017b0cd2017SGleb Smirnoff 		*a_rbehind = bp->b_pgbefore;
1018b0cd2017SGleb Smirnoff 	if (a_rahead)
1019b0cd2017SGleb Smirnoff 		*a_rahead = bp->b_pgafter;
1020b0cd2017SGleb Smirnoff 
1021e48b82bdSGleb Smirnoff #ifdef INVARIANTS
1022dcc0ff5aSGleb Smirnoff 	KASSERT(bp->b_npages <= nitems(bp->b_pages),
1023b0cd2017SGleb Smirnoff 	    ("%s: buf %p overflowed", __func__, bp));
10244f56243aSGleb Smirnoff 	for (int j = 1, prev = 0; j < bp->b_npages; j++) {
10251e0c121fSGleb Smirnoff 		if (bp->b_pages[j] == bogus_page)
10261e0c121fSGleb Smirnoff 			continue;
10271e0c121fSGleb Smirnoff 		KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
10281e0c121fSGleb Smirnoff 		    j - prev, ("%s: pages array not consecutive, bp %p",
10291e0c121fSGleb Smirnoff 		     __func__, bp));
10301e0c121fSGleb Smirnoff 		prev = j;
10311e0c121fSGleb Smirnoff 	}
1032e48b82bdSGleb Smirnoff #endif
1033eac91e32SKonstantin Belousov 
10340d94caffSDavid Greenman 	/*
1035b0cd2017SGleb Smirnoff 	 * Recalculate first offset and bytecount with regards to read behind.
1036b0cd2017SGleb Smirnoff 	 * Truncate bytecount to vnode real size and round up physical size
1037b0cd2017SGleb Smirnoff 	 * for real devices.
103826f9a767SRodney W. Grimes 	 */
1039b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1040b0cd2017SGleb Smirnoff 	bytecount = bp->b_npages << PAGE_SHIFT;
1041b0cd2017SGleb Smirnoff 	if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
1042b0cd2017SGleb Smirnoff 		bytecount = object->un_pager.vnp.vnp_size - foff;
1043eac91e32SKonstantin Belousov 	secmask = bo->bo_bsize - 1;
10446229cc50SPoul-Henning Kamp 	KASSERT(secmask < PAGE_SIZE && secmask > 0,
1045b0cd2017SGleb Smirnoff 	    ("%s: sector size %d too large", __func__, secmask + 1));
1046b0cd2017SGleb Smirnoff 	bytecount = (bytecount + secmask) & ~secmask;
104726f9a767SRodney W. Grimes 
104826f9a767SRodney W. Grimes 	/*
1049b0cd2017SGleb Smirnoff 	 * And map the pages to be read into the kva, if the filesystem
10506ce697dcSKonstantin Belousov 	 * requires mapped buffers.
105126f9a767SRodney W. Grimes 	 */
10522a5eef69SGleb Smirnoff 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
10536ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
10546ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
10556ce697dcSKonstantin Belousov 		bp->b_offset = 0;
1056fade8dd7SJeff Roberson 	} else {
1057fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
1058b0cd2017SGleb Smirnoff 		pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1059fade8dd7SJeff Roberson 	}
106026f9a767SRodney W. Grimes 
1061b0cd2017SGleb Smirnoff 	/* Build a minimal buffer header. */
106221144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
1063bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1064bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1065a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
1066a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
10679c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
10681faacf5dSKirk McKusick 	bp->b_vp = vp;
1069b0cd2017SGleb Smirnoff 	bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
10702c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
1071e48b82bdSGleb Smirnoff 	KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1072e48b82bdSGleb Smirnoff 	    (blkno0 - bp->b_blkno) * DEV_BSIZE +
1073e48b82bdSGleb Smirnoff 	    IDX_TO_OFF(m[0]->pindex) % bsize,
1074e48b82bdSGleb Smirnoff 	    ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1075e48b82bdSGleb Smirnoff 	    "blkno0 %ju b_blkno %ju", bsize,
1076e48b82bdSGleb Smirnoff 	    (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1077e48b82bdSGleb Smirnoff 	    (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
107890effb23SGleb Smirnoff 
1079b0cd2017SGleb Smirnoff 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
108083c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodein);
108183c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1082b0cd2017SGleb Smirnoff 
108390effb23SGleb Smirnoff 	if (iodone != NULL) { /* async */
1084b0cd2017SGleb Smirnoff 		bp->b_pgiodone = iodone;
108590effb23SGleb Smirnoff 		bp->b_caller1 = arg;
108690effb23SGleb Smirnoff 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
108790effb23SGleb Smirnoff 		bp->b_flags |= B_ASYNC;
108890effb23SGleb Smirnoff 		BUF_KERNPROC(bp);
1089b792bebeSPoul-Henning Kamp 		bstrategy(bp);
1090b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
109190effb23SGleb Smirnoff 	} else {
109290effb23SGleb Smirnoff 		bp->b_iodone = bdone;
109390effb23SGleb Smirnoff 		bstrategy(bp);
10946a4b5823SPoul-Henning Kamp 		bwait(bp, PVM, "vnread");
109590effb23SGleb Smirnoff 		error = vnode_pager_generic_getpages_done(bp);
10961bb5ad63SGleb Smirnoff 		for (i = 0; i < bp->b_npages; i++)
10976ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
10981faacf5dSKirk McKusick 		bp->b_vp = NULL;
10999c83534dSPoul-Henning Kamp 		pbrelbo(bp);
1100756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
110190effb23SGleb Smirnoff 		return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
110290effb23SGleb Smirnoff 	}
1103b0cd2017SGleb Smirnoff }
110490effb23SGleb Smirnoff 
110590effb23SGleb Smirnoff static void
110690effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp)
110790effb23SGleb Smirnoff {
110890effb23SGleb Smirnoff 	int error;
110990effb23SGleb Smirnoff 
111090effb23SGleb Smirnoff 	error = vnode_pager_generic_getpages_done(bp);
1111b0cd2017SGleb Smirnoff 	/* Run the iodone upon the requested range. */
1112b0cd2017SGleb Smirnoff 	bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1113b0cd2017SGleb Smirnoff 	    bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
111490effb23SGleb Smirnoff 	for (int i = 0; i < bp->b_npages; i++)
111590effb23SGleb Smirnoff 		bp->b_pages[i] = NULL;
111690effb23SGleb Smirnoff 	bp->b_vp = NULL;
111790effb23SGleb Smirnoff 	pbrelbo(bp);
1118756a5412SGleb Smirnoff 	uma_zfree(vnode_pbuf_zone, bp);
111990effb23SGleb Smirnoff }
112090effb23SGleb Smirnoff 
112190effb23SGleb Smirnoff static int
112290effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp)
112390effb23SGleb Smirnoff {
112490effb23SGleb Smirnoff 	vm_object_t object;
112590effb23SGleb Smirnoff 	off_t tfoff, nextoff;
112690effb23SGleb Smirnoff 	int i, error;
112790effb23SGleb Smirnoff 
112890effb23SGleb Smirnoff 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
112990effb23SGleb Smirnoff 	object = bp->b_vp->v_object;
113090effb23SGleb Smirnoff 
113190effb23SGleb Smirnoff 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1132fade8dd7SJeff Roberson 		if (!buf_mapped(bp)) {
1133fade8dd7SJeff Roberson 			bp->b_data = bp->b_kvabase;
1134fade8dd7SJeff Roberson 			pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
113590effb23SGleb Smirnoff 			    bp->b_npages);
113690effb23SGleb Smirnoff 		}
1137fade8dd7SJeff Roberson 		bzero(bp->b_data + bp->b_bcount,
113890effb23SGleb Smirnoff 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
113990effb23SGleb Smirnoff 	}
1140fade8dd7SJeff Roberson 	if (buf_mapped(bp)) {
1141fade8dd7SJeff Roberson 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1142fade8dd7SJeff Roberson 		bp->b_data = unmapped_buf;
114390effb23SGleb Smirnoff 	}
114426f9a767SRodney W. Grimes 
114589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
114690effb23SGleb Smirnoff 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
114790effb23SGleb Smirnoff 	    i < bp->b_npages; i++, tfoff = nextoff) {
11488f9110f6SJohn Dyson 		vm_page_t mt;
11498f9110f6SJohn Dyson 
11508f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
115190effb23SGleb Smirnoff 		mt = bp->b_pages[i];
1152*2f81c92eSMark Johnston 		if (mt == bogus_page)
1153*2f81c92eSMark Johnston 			continue;
11548f9110f6SJohn Dyson 
115554746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
11568d17e694SJulian Elischer 			/*
11578d17e694SJulian Elischer 			 * Read filled up entire page.
11588d17e694SJulian Elischer 			 */
11590012f373SJeff Roberson 			vm_page_valid(mt);
1160016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
116179f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
1162016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
116379f0deb9SGleb Smirnoff 			    ("%s: page %p is mapped", __func__, mt));
11648f9110f6SJohn Dyson 		} else {
11658d17e694SJulian Elischer 			/*
116642eb4108SAlan Cox 			 * Read did not fill up entire page.
11678d17e694SJulian Elischer 			 *
11688d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
11698d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
11708d17e694SJulian Elischer 			 * read.
11718d17e694SJulian Elischer 			 */
1172dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
117354746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
117442eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
117542eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
117679f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
11778f9110f6SJohn Dyson 		}
11788f9110f6SJohn Dyson 
1179b0cd2017SGleb Smirnoff 		if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1180b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
118103679e23SAlan Cox 	}
118289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
118390effb23SGleb Smirnoff 	if (error != 0)
118490effb23SGleb Smirnoff 		printf("%s: I/O read error %d\n", __func__, error);
118590effb23SGleb Smirnoff 
118690effb23SGleb Smirnoff 	return (error);
118726f9a767SRodney W. Grimes }
118826f9a767SRodney W. Grimes 
1189ce75f2c3SMike Smith /*
1190ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1191ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1192ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1193ce75f2c3SMike Smith  *
1194ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1195ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1196ce75f2c3SMike Smith  */
1197e4542174SMatthew Dillon static void
11987ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
119933cad9e9SKonstantin Belousov     int flags, int *rtvals)
1200170db9c6SJohn Dyson {
1201170db9c6SJohn Dyson 	int rtval;
1202170db9c6SJohn Dyson 	struct vnode *vp;
120386ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1204ad980522SJohn Dyson 
12050e3cdf2cSAlan Cox 	/*
12060e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
12070e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
12080e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
12090e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
12100e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
12110e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
12120e3cdf2cSAlan Cox 	 *
12130e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
12140e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
12150e3cdf2cSAlan Cox 	 */
12160e3cdf2cSAlan Cox 
1217e2068d0bSJeff Roberson 	if (vm_page_count_min())
121833cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
12190e3cdf2cSAlan Cox 
12200e3cdf2cSAlan Cox 	/*
12210e3cdf2cSAlan Cox 	 * Call device-specific putpages function
12220e3cdf2cSAlan Cox 	 */
1223170db9c6SJohn Dyson 	vp = object->handle;
122489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
122533cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
122623955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
122723955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
122889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1229170db9c6SJohn Dyson }
1230170db9c6SJohn Dyson 
123105877a85SKonstantin Belousov static int
123205877a85SKonstantin Belousov vn_off2bidx(vm_ooffset_t offset)
123305877a85SKonstantin Belousov {
123405877a85SKonstantin Belousov 
123505877a85SKonstantin Belousov 	return ((offset & PAGE_MASK) / DEV_BSIZE);
123605877a85SKonstantin Belousov }
123705877a85SKonstantin Belousov 
123805877a85SKonstantin Belousov static bool
123905877a85SKonstantin Belousov vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
124005877a85SKonstantin Belousov {
124105877a85SKonstantin Belousov 
124205877a85SKonstantin Belousov 	KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
124305877a85SKonstantin Belousov 	    offset < IDX_TO_OFF(m->pindex + 1),
124405877a85SKonstantin Belousov 	    ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
124505877a85SKonstantin Belousov 	    (uintmax_t)offset));
124605877a85SKonstantin Belousov 	return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
124705877a85SKonstantin Belousov }
1248ce75f2c3SMike Smith 
124926f9a767SRodney W. Grimes /*
1250ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
12514491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
12522b6b0df7SMatthew Dillon  *
12532b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
1254763df3ecSPedro F. Giffuni  * clustering has already typically occurred, so in general we ask the
12552b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
12562b6b0df7SMatthew Dillon  * then delayed.
125726f9a767SRodney W. Grimes  */
1258ce75f2c3SMike Smith int
1259c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1260c46b90e9SAlan Cox     int flags, int *rtvals)
126126f9a767SRodney W. Grimes {
1262ce75f2c3SMike Smith 	vm_object_t object;
1263c46b90e9SAlan Cox 	vm_page_t m;
126405877a85SKonstantin Belousov 	vm_ooffset_t maxblksz, next_offset, poffset, prev_offset;
1265f6b04d2bSDavid Greenman 	struct uio auio;
1266f6b04d2bSDavid Greenman 	struct iovec aiov;
126705877a85SKonstantin Belousov 	off_t prev_resid, wrsz;
1268e6c44f65SKonstantin Belousov 	int count, error, i, maxsize, ncount, pgoff, ppscheck;
126905877a85SKonstantin Belousov 	bool in_hole;
1270dd498befSPaul Saab 	static struct timeval lastfail;
1271dd498befSPaul Saab 	static int curfail;
127226f9a767SRodney W. Grimes 
1273ce75f2c3SMike Smith 	object = vp->v_object;
1274ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1275ce75f2c3SMike Smith 
127626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1277031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
127826f9a767SRodney W. Grimes 
1279c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
1280e6c44f65SKonstantin Belousov 		printf("vnode_pager_generic_putpages: "
1281e6c44f65SKonstantin Belousov 		    "attempt to write meta-data 0x%jx(%lx)\n",
1282e6c44f65SKonstantin Belousov 		    (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1283f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1284e6c44f65SKonstantin Belousov 		return (VM_PAGER_BAD);
12850d94caffSDavid Greenman 	}
12860bdb7528SDavid Greenman 
1287f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1288f6b04d2bSDavid Greenman 	ncount = count;
128926f9a767SRodney W. Grimes 
1290c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
129100a6f47fSMatthew Dillon 
129200a6f47fSMatthew Dillon 	/*
129300a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
1294763df3ecSPedro F. Giffuni 	 * have to invalidate pages occurring beyond the file EOF.  However,
129500a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
129600a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
129700a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
129800a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
129900a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
13003ebeaf59SMatthew Dillon 	 *
13013ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
13023ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
130300a6f47fSMatthew Dillon 	 */
1304b3d4ab66SKonstantin Belousov 	VM_OBJECT_RLOCK(object);
1305a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1306b3d4ab66SKonstantin Belousov 		if (!VM_OBJECT_TRYUPGRADE(object)) {
1307b3d4ab66SKonstantin Belousov 			VM_OBJECT_RUNLOCK(object);
1308b3d4ab66SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
1309b3d4ab66SKonstantin Belousov 			if (maxsize + poffset <= object->un_pager.vnp.vnp_size)
1310b3d4ab66SKonstantin Belousov 				goto downgrade;
1311b3d4ab66SKonstantin Belousov 		}
131200a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
1313a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1314aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
131500a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1316938cdc42SKonstantin Belousov 				pgoff = roundup2(pgoff, DEV_BSIZE);
1317938cdc42SKonstantin Belousov 
1318c46b90e9SAlan Cox 				/*
1319c46b90e9SAlan Cox 				 * If the object is locked and the following
1320c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1321c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1322c46b90e9SAlan Cox 				 * pmap operation.
1323c46b90e9SAlan Cox 				 */
1324c46b90e9SAlan Cox 				m = ma[ncount - 1];
1325c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
13266031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1327c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1328e6c44f65SKonstantin Belousov 				MPASS(m->dirty != 0);
1329c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1330c46b90e9SAlan Cox 				    pgoff);
133100a6f47fSMatthew Dillon 			}
133200a6f47fSMatthew Dillon 		} else {
133300a6f47fSMatthew Dillon 			maxsize = 0;
133400a6f47fSMatthew Dillon 			ncount = 0;
133500a6f47fSMatthew Dillon 		}
1336e6c44f65SKonstantin Belousov 		for (i = ncount; i < count; i++)
1337f6b04d2bSDavid Greenman 			rtvals[i] = VM_PAGER_BAD;
1338b3d4ab66SKonstantin Belousov downgrade:
1339b3d4ab66SKonstantin Belousov 		VM_OBJECT_LOCK_DOWNGRADE(object);
1340f6b04d2bSDavid Greenman 	}
134126f9a767SRodney W. Grimes 
1342f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1343f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1344f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1345e6c44f65SKonstantin Belousov 	auio.uio_td = NULL;
134605877a85SKonstantin Belousov 	maxblksz = roundup2(poffset + maxsize, DEV_BSIZE);
134705877a85SKonstantin Belousov 
134805877a85SKonstantin Belousov 	for (prev_offset = poffset; prev_offset < maxblksz;) {
134905877a85SKonstantin Belousov 		/* Skip clean blocks. */
135005877a85SKonstantin Belousov 		for (in_hole = true; in_hole && prev_offset < maxblksz;) {
135105877a85SKonstantin Belousov 			m = ma[OFF_TO_IDX(prev_offset - poffset)];
135205877a85SKonstantin Belousov 			for (i = vn_off2bidx(prev_offset);
135305877a85SKonstantin Belousov 			    i < sizeof(vm_page_bits_t) * NBBY &&
135405877a85SKonstantin Belousov 			    prev_offset < maxblksz; i++) {
135505877a85SKonstantin Belousov 				if (vn_dirty_blk(m, prev_offset)) {
135605877a85SKonstantin Belousov 					in_hole = false;
135705877a85SKonstantin Belousov 					break;
135805877a85SKonstantin Belousov 				}
135905877a85SKonstantin Belousov 				prev_offset += DEV_BSIZE;
136005877a85SKonstantin Belousov 			}
136105877a85SKonstantin Belousov 		}
136205877a85SKonstantin Belousov 		if (in_hole)
136305877a85SKonstantin Belousov 			goto write_done;
136405877a85SKonstantin Belousov 
136505877a85SKonstantin Belousov 		/* Find longest run of dirty blocks. */
136605877a85SKonstantin Belousov 		for (next_offset = prev_offset; next_offset < maxblksz;) {
136705877a85SKonstantin Belousov 			m = ma[OFF_TO_IDX(next_offset - poffset)];
136805877a85SKonstantin Belousov 			for (i = vn_off2bidx(next_offset);
136905877a85SKonstantin Belousov 			    i < sizeof(vm_page_bits_t) * NBBY &&
137005877a85SKonstantin Belousov 			    next_offset < maxblksz; i++) {
137105877a85SKonstantin Belousov 				if (!vn_dirty_blk(m, next_offset))
137205877a85SKonstantin Belousov 					goto start_write;
137305877a85SKonstantin Belousov 				next_offset += DEV_BSIZE;
137405877a85SKonstantin Belousov 			}
137505877a85SKonstantin Belousov 		}
137605877a85SKonstantin Belousov start_write:
137705877a85SKonstantin Belousov 		if (next_offset > poffset + maxsize)
137805877a85SKonstantin Belousov 			next_offset = poffset + maxsize;
137905877a85SKonstantin Belousov 
138005877a85SKonstantin Belousov 		/*
138105877a85SKonstantin Belousov 		 * Getting here requires finding a dirty block in the
138205877a85SKonstantin Belousov 		 * 'skip clean blocks' loop.
138305877a85SKonstantin Belousov 		 */
138405877a85SKonstantin Belousov 		MPASS(prev_offset < next_offset);
138505877a85SKonstantin Belousov 
1386b3d4ab66SKonstantin Belousov 		VM_OBJECT_RUNLOCK(object);
138705877a85SKonstantin Belousov 		aiov.iov_base = NULL;
138805877a85SKonstantin Belousov 		auio.uio_iovcnt = 1;
138905877a85SKonstantin Belousov 		auio.uio_offset = prev_offset;
139005877a85SKonstantin Belousov 		prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
139105877a85SKonstantin Belousov 		    prev_offset;
139205877a85SKonstantin Belousov 		error = VOP_WRITE(vp, &auio,
139305877a85SKonstantin Belousov 		    vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
139405877a85SKonstantin Belousov 
139505877a85SKonstantin Belousov 		wrsz = prev_resid - auio.uio_resid;
139605877a85SKonstantin Belousov 		if (wrsz == 0) {
139705877a85SKonstantin Belousov 			if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
139805877a85SKonstantin Belousov 				vn_printf(vp, "vnode_pager_putpages: "
139905877a85SKonstantin Belousov 				    "zero-length write at %ju resid %zd\n",
140005877a85SKonstantin Belousov 				    auio.uio_offset, auio.uio_resid);
140105877a85SKonstantin Belousov 			}
1402b3d4ab66SKonstantin Belousov 			VM_OBJECT_RLOCK(object);
140305877a85SKonstantin Belousov 			break;
140405877a85SKonstantin Belousov 		}
140505877a85SKonstantin Belousov 
140605877a85SKonstantin Belousov 		/* Adjust the starting offset for next iteration. */
140705877a85SKonstantin Belousov 		prev_offset += wrsz;
140805877a85SKonstantin Belousov 		MPASS(auio.uio_offset == prev_offset);
1409f6b04d2bSDavid Greenman 
14103dbb0ca6SKonstantin Belousov 		ppscheck = 0;
141105877a85SKonstantin Belousov 		if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
141205877a85SKonstantin Belousov 		    &curfail, 1)) != 0)
141305877a85SKonstantin Belousov 			vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
141405877a85SKonstantin Belousov 			    error);
1415e6c44f65SKonstantin Belousov 		if (auio.uio_resid != 0 && (ppscheck != 0 ||
1416e6c44f65SKonstantin Belousov 		    ppsratecheck(&lastfail, &curfail, 1) != 0))
141705877a85SKonstantin Belousov 			vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
141805877a85SKonstantin Belousov 			    "at %ju\n", auio.uio_resid,
141905877a85SKonstantin Belousov 			    (uintmax_t)ma[0]->pindex);
1420b3d4ab66SKonstantin Belousov 		VM_OBJECT_RLOCK(object);
142105877a85SKonstantin Belousov 		if (error != 0 || auio.uio_resid != 0)
142205877a85SKonstantin Belousov 			break;
142305877a85SKonstantin Belousov 	}
142405877a85SKonstantin Belousov write_done:
142505877a85SKonstantin Belousov 	/* Mark completely processed pages. */
142605877a85SKonstantin Belousov 	for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
142726f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
142805877a85SKonstantin Belousov 	/* Mark partial EOF page. */
142905877a85SKonstantin Belousov 	if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
143005877a85SKonstantin Belousov 		rtvals[i++] = VM_PAGER_OK;
143105877a85SKonstantin Belousov 	/* Unwritten pages in range, free bonus if the page is clean. */
143205877a85SKonstantin Belousov 	for (; i < ncount; i++)
143305877a85SKonstantin Belousov 		rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
1434b3d4ab66SKonstantin Belousov 	VM_OBJECT_RUNLOCK(object);
143505877a85SKonstantin Belousov 	VM_CNT_ADD(v_vnodepgsout, i);
143605877a85SKonstantin Belousov 	VM_CNT_INC(v_vnodeout);
1437e6c44f65SKonstantin Belousov 	return (rtvals[0]);
143826f9a767SRodney W. Grimes }
1439031ec8c1SKonstantin Belousov 
144065b9599aSKonstantin Belousov int
144165b9599aSKonstantin Belousov vnode_pager_putpages_ioflags(int pager_flags)
144265b9599aSKonstantin Belousov {
144365b9599aSKonstantin Belousov 	int ioflags;
144465b9599aSKonstantin Belousov 
144565b9599aSKonstantin Belousov 	/*
144665b9599aSKonstantin Belousov 	 * Pageouts are already clustered, use IO_ASYNC to force a
144765b9599aSKonstantin Belousov 	 * bawrite() rather then a bdwrite() to prevent paging I/O
144865b9599aSKonstantin Belousov 	 * from saturating the buffer cache.  Dummy-up the sequential
144965b9599aSKonstantin Belousov 	 * heuristic to cause large ranges to cluster.  If neither
145065b9599aSKonstantin Belousov 	 * IO_SYNC or IO_ASYNC is set, the system decides how to
145165b9599aSKonstantin Belousov 	 * cluster.
145265b9599aSKonstantin Belousov 	 */
145365b9599aSKonstantin Belousov 	ioflags = IO_VMIO;
145465b9599aSKonstantin Belousov 	if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0)
145565b9599aSKonstantin Belousov 		ioflags |= IO_SYNC;
145665b9599aSKonstantin Belousov 	else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0)
145765b9599aSKonstantin Belousov 		ioflags |= IO_ASYNC;
145865b9599aSKonstantin Belousov 	ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0;
145965b9599aSKonstantin Belousov 	ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0;
146065b9599aSKonstantin Belousov 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
146165b9599aSKonstantin Belousov 	return (ioflags);
146265b9599aSKonstantin Belousov }
146365b9599aSKonstantin Belousov 
1464555b7bb4SKonstantin Belousov /*
1465555b7bb4SKonstantin Belousov  * vnode_pager_undirty_pages().
1466555b7bb4SKonstantin Belousov  *
1467555b7bb4SKonstantin Belousov  * A helper to mark pages as clean after pageout that was possibly
1468555b7bb4SKonstantin Belousov  * done with a short write.  The lpos argument specifies the page run
1469555b7bb4SKonstantin Belousov  * length in bytes, and the written argument specifies how many bytes
1470555b7bb4SKonstantin Belousov  * were actually written.  eof is the offset past the last valid byte
1471555b7bb4SKonstantin Belousov  * in the vnode using the absolute file position of the first byte in
1472555b7bb4SKonstantin Belousov  * the run as the base from which it is computed.
1473555b7bb4SKonstantin Belousov  */
1474031ec8c1SKonstantin Belousov void
1475555b7bb4SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof,
1476555b7bb4SKonstantin Belousov     int lpos)
1477031ec8c1SKonstantin Belousov {
14789d17da3bSKonstantin Belousov 	vm_object_t obj;
1479555b7bb4SKonstantin Belousov 	int i, pos, pos_devb;
1480031ec8c1SKonstantin Belousov 
1481555b7bb4SKonstantin Belousov 	if (written == 0 && eof >= lpos)
14829d17da3bSKonstantin Belousov 		return;
14839d17da3bSKonstantin Belousov 	obj = ma[0]->object;
148489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1485031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1486031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1487031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1488031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1489031ec8c1SKonstantin Belousov 		} else {
1490031ec8c1SKonstantin Belousov 			/* Partially written page. */
1491031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1492031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1493031ec8c1SKonstantin Belousov 		}
1494031ec8c1SKonstantin Belousov 	}
1495555b7bb4SKonstantin Belousov 	if (eof >= lpos) /* avoid truncation */
1496555b7bb4SKonstantin Belousov 		goto done;
1497555b7bb4SKonstantin Belousov 	for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1498555b7bb4SKonstantin Belousov 		if (pos != trunc_page(pos)) {
1499555b7bb4SKonstantin Belousov 			/*
1500555b7bb4SKonstantin Belousov 			 * The page contains the last valid byte in
1501555b7bb4SKonstantin Belousov 			 * the vnode, mark the rest of the page as
1502555b7bb4SKonstantin Belousov 			 * clean, potentially making the whole page
1503555b7bb4SKonstantin Belousov 			 * clean.
1504555b7bb4SKonstantin Belousov 			 */
1505555b7bb4SKonstantin Belousov 			pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1506555b7bb4SKonstantin Belousov 			vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1507555b7bb4SKonstantin Belousov 			    pos_devb);
1508555b7bb4SKonstantin Belousov 
1509555b7bb4SKonstantin Belousov 			/*
1510555b7bb4SKonstantin Belousov 			 * If the page was cleaned, report the pageout
1511555b7bb4SKonstantin Belousov 			 * on it as successful.  msync() no longer
1512555b7bb4SKonstantin Belousov 			 * needs to write out the page, endlessly
1513555b7bb4SKonstantin Belousov 			 * creating write requests and dirty buffers.
1514555b7bb4SKonstantin Belousov 			 */
1515555b7bb4SKonstantin Belousov 			if (ma[i]->dirty == 0)
1516555b7bb4SKonstantin Belousov 				rtvals[i] = VM_PAGER_OK;
1517555b7bb4SKonstantin Belousov 
1518555b7bb4SKonstantin Belousov 			pos = round_page(pos);
1519555b7bb4SKonstantin Belousov 		} else {
1520555b7bb4SKonstantin Belousov 			/* vm_pageout_flush() clears dirty */
1521555b7bb4SKonstantin Belousov 			rtvals[i] = VM_PAGER_BAD;
1522555b7bb4SKonstantin Belousov 			pos += PAGE_SIZE;
1523555b7bb4SKonstantin Belousov 		}
1524555b7bb4SKonstantin Belousov 	}
1525555b7bb4SKonstantin Belousov done:
152689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1527031ec8c1SKonstantin Belousov }
152884110e7eSKonstantin Belousov 
1529fe7bcbafSKyle Evans static void
153084110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
153184110e7eSKonstantin Belousov     vm_offset_t end)
153284110e7eSKonstantin Belousov {
153384110e7eSKonstantin Belousov 	struct vnode *vp;
153484110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
153584110e7eSKonstantin Belousov 
153689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
153784110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
153889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
153984110e7eSKonstantin Belousov 		return;
154084110e7eSKonstantin Belousov 	}
154184110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
154284110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
154384110e7eSKonstantin Belousov 	vp = object->handle;
154484110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
154578022527SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "v_writecount inc");
154678022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, 1);
1547b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1548b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
154984110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
155078022527SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "v_writecount dec");
155178022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
1552b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1553b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
155484110e7eSKonstantin Belousov 	}
155589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
155684110e7eSKonstantin Belousov }
155784110e7eSKonstantin Belousov 
1558fe7bcbafSKyle Evans static void
155984110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
156084110e7eSKonstantin Belousov     vm_offset_t end)
156184110e7eSKonstantin Belousov {
156284110e7eSKonstantin Belousov 	struct vnode *vp;
156384110e7eSKonstantin Belousov 	struct mount *mp;
156484110e7eSKonstantin Belousov 	vm_offset_t inc;
156584110e7eSKonstantin Belousov 
156689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
156784110e7eSKonstantin Belousov 
156884110e7eSKonstantin Belousov 	/*
156984110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
157084110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
157184110e7eSKonstantin Belousov 	 */
157284110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
157389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
157484110e7eSKonstantin Belousov 		return;
157584110e7eSKonstantin Belousov 	}
157684110e7eSKonstantin Belousov 
157784110e7eSKonstantin Belousov 	/*
157884110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
157984110e7eSKonstantin Belousov 	 * zero.
158084110e7eSKonstantin Belousov 	 */
158184110e7eSKonstantin Belousov 	inc = end - start;
158284110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
158384110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
158489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
158584110e7eSKonstantin Belousov 		return;
158684110e7eSKonstantin Belousov 	}
158784110e7eSKonstantin Belousov 
158884110e7eSKonstantin Belousov 	vp = object->handle;
158984110e7eSKonstantin Belousov 	vhold(vp);
159089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
159184110e7eSKonstantin Belousov 	mp = NULL;
159284110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
159378022527SKonstantin Belousov 	vn_lock(vp, LK_SHARED | LK_RETRY);
159484110e7eSKonstantin Belousov 
159584110e7eSKonstantin Belousov 	/*
159684110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
159784110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
159884110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
159984110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
160084110e7eSKonstantin Belousov 	 */
160184110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
160284110e7eSKonstantin Belousov 	VOP_UNLOCK(vp, 0);
160384110e7eSKonstantin Belousov 	vdrop(vp);
160484110e7eSKonstantin Belousov 	if (mp != NULL)
160584110e7eSKonstantin Belousov 		vn_finished_write(mp);
160684110e7eSKonstantin Belousov }
1607