xref: /freebsd/sys/vm/vnode_pager.c (revision efec381dd1d9e0e449bcc4cb1f189434f9c7d55c)
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>
7351df5321SJeff Roberson #include <sys/refcount.h>
7489f6b863SAttilio Rao #include <sys/rwlock.h>
759e0ddbd0SAlan Cox #include <sys/sf_buf.h>
76e5818a53SJeff Roberson #include <sys/domainset.h>
77df8bae1dSRodney W. Grimes 
784f12e0acSSuleiman Souhlal #include <machine/atomic.h>
794f12e0acSSuleiman Souhlal 
80df8bae1dSRodney W. Grimes #include <vm/vm.h>
811c771f92SKonstantin Belousov #include <vm/vm_param.h>
82efeaf95aSDavid Greenman #include <vm/vm_object.h>
83df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8424a1cce3SDavid Greenman #include <vm/vm_pager.h>
851efb74fbSJohn Dyson #include <vm/vm_map.h>
86df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
87efeaf95aSDavid Greenman #include <vm/vm_extern.h>
88756a5412SGleb Smirnoff #include <vm/uma.h>
89df8bae1dSRodney W. Grimes 
90bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
91bff76343SAlan Cox     daddr_t *rtaddress, int *run);
9211caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
9311caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
9411caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
95b0cd2017SGleb Smirnoff static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
96b0cd2017SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
97b0cd2017SGleb Smirnoff     int *, vop_getpages_iodone_t, void *);
9833cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
9911caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
1003364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
1013364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *cred);
10290effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *);
10390effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *);
104fe7bcbafSKyle Evans static void vnode_pager_update_writecount(vm_object_t, vm_offset_t,
105fe7bcbafSKyle Evans     vm_offset_t);
106fe7bcbafSKyle Evans static void vnode_pager_release_writecount(vm_object_t, vm_offset_t,
107fe7bcbafSKyle Evans     vm_offset_t);
1080b8253a7SBruce Evans 
109df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
1104e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
1114e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
1124e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
11390effb23SGleb Smirnoff 	.pgo_getpages_async = vnode_pager_getpages_async,
1144e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
1154e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
116fe7bcbafSKyle Evans 	.pgo_update_writecount = vnode_pager_update_writecount,
117fe7bcbafSKyle Evans 	.pgo_release_writecount = vnode_pager_release_writecount,
118df8bae1dSRodney W. Grimes };
119df8bae1dSRodney W. Grimes 
120e5818a53SJeff Roberson static struct domainset *vnode_domainset = NULL;
121e5818a53SJeff Roberson 
122a314aba8SMateusz Guzik SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset,
123a314aba8SMateusz Guzik     CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_RW, &vnode_domainset, 0,
124a314aba8SMateusz Guzik     sysctl_handle_domainset, "A", "Default vnode NUMA policy");
125e5818a53SJeff Roberson 
12666fb0b1aSGleb Smirnoff static int nvnpbufs;
12766fb0b1aSGleb Smirnoff SYSCTL_INT(_vm, OID_AUTO, vnode_pbufs, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
12866fb0b1aSGleb Smirnoff     &nvnpbufs, 0, "number of physical buffers allocated for vnode pager");
12966fb0b1aSGleb Smirnoff 
130756a5412SGleb Smirnoff static uma_zone_t vnode_pbuf_zone;
131756a5412SGleb Smirnoff 
132756a5412SGleb Smirnoff static void
133756a5412SGleb Smirnoff vnode_pager_init(void *dummy)
134756a5412SGleb Smirnoff {
135756a5412SGleb Smirnoff 
13666fb0b1aSGleb Smirnoff #ifdef __LP64__
13766fb0b1aSGleb Smirnoff 	nvnpbufs = nswbuf * 2;
13866fb0b1aSGleb Smirnoff #else
13966fb0b1aSGleb Smirnoff 	nvnpbufs = nswbuf / 2;
14066fb0b1aSGleb Smirnoff #endif
14166fb0b1aSGleb Smirnoff 	TUNABLE_INT_FETCH("vm.vnode_pbufs", &nvnpbufs);
14266fb0b1aSGleb Smirnoff 	vnode_pbuf_zone = pbuf_zsecond_create("vnpbuf", nvnpbufs);
143756a5412SGleb Smirnoff }
144756a5412SGleb Smirnoff SYSINIT(vnode_pager, SI_SUB_CPU, SI_ORDER_ANY, vnode_pager_init, NULL);
145756a5412SGleb Smirnoff 
146d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
147d07a6d3fSPoul-Henning Kamp int
148731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
149d07a6d3fSPoul-Henning Kamp {
150d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
151d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
152d07a6d3fSPoul-Henning Kamp 	struct vattr va;
153a67d5408SJeff Roberson 	bool last;
154d07a6d3fSPoul-Henning Kamp 
155d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
156d07a6d3fSPoul-Henning Kamp 		return (0);
157d07a6d3fSPoul-Henning Kamp 
1586470c8d3SKonstantin Belousov 	object = vp->v_object;
1596470c8d3SKonstantin Belousov 	if (object != NULL)
160d07a6d3fSPoul-Henning Kamp 		return (0);
161d07a6d3fSPoul-Henning Kamp 
162d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
163d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
164d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
165d07a6d3fSPoul-Henning Kamp 		} else {
1660359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
167d07a6d3fSPoul-Henning Kamp 				return (0);
168d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
169d07a6d3fSPoul-Henning Kamp 		}
170d07a6d3fSPoul-Henning Kamp 	}
171d07a6d3fSPoul-Henning Kamp 
1723364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
173d07a6d3fSPoul-Henning Kamp 	/*
174d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
175a67d5408SJeff Roberson 	 * that the object is associated with the vp.  We still have
176a67d5408SJeff Roberson 	 * to serialize with vnode_pager_dealloc() for the last
177a67d5408SJeff Roberson 	 * potential reference.
178d07a6d3fSPoul-Henning Kamp 	 */
17951df5321SJeff Roberson 	VM_OBJECT_RLOCK(object);
180a67d5408SJeff Roberson 	last = refcount_release(&object->ref_count);
18151df5321SJeff Roberson 	VM_OBJECT_RUNLOCK(object);
182a67d5408SJeff Roberson 	if (last)
183d07a6d3fSPoul-Henning Kamp 		vrele(vp);
184d07a6d3fSPoul-Henning Kamp 
185d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
186d07a6d3fSPoul-Henning Kamp 
187d07a6d3fSPoul-Henning Kamp 	return (0);
188d07a6d3fSPoul-Henning Kamp }
189d07a6d3fSPoul-Henning Kamp 
1907146d6cbSPoul-Henning Kamp void
1917146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1927146d6cbSPoul-Henning Kamp {
1937146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1947146d6cbSPoul-Henning Kamp 
1957146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1966470c8d3SKonstantin Belousov 	if (obj == NULL || obj->handle != vp)
1977146d6cbSPoul-Henning Kamp 		return;
19857fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
19989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
2006470c8d3SKonstantin Belousov 	MPASS(obj->type == OBJT_VNODE);
2012a339d9eSKonstantin Belousov 	umtx_shm_object_terminated(obj);
2027146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
2039c83ff2dSJeff Roberson 		KASSERT((obj->flags & OBJ_DEAD) == 0,
2049c83ff2dSJeff Roberson 		   ("vnode_destroy_vobject: Terminating dead object"));
205783a68aaSKonstantin Belousov 		vm_object_set_flag(obj, OBJ_DEAD);
206783a68aaSKonstantin Belousov 
207783a68aaSKonstantin Belousov 		/*
208783a68aaSKonstantin Belousov 		 * Clean pages and flush buffers.
209783a68aaSKonstantin Belousov 		 */
210783a68aaSKonstantin Belousov 		vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
211783a68aaSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
212783a68aaSKonstantin Belousov 
213783a68aaSKonstantin Belousov 		vinvalbuf(vp, V_SAVE, 0, 0);
214783a68aaSKonstantin Belousov 
215783a68aaSKonstantin Belousov 		BO_LOCK(&vp->v_bufobj);
216783a68aaSKonstantin Belousov 		vp->v_bufobj.bo_flag |= BO_DEAD;
217783a68aaSKonstantin Belousov 		BO_UNLOCK(&vp->v_bufobj);
218783a68aaSKonstantin Belousov 
219783a68aaSKonstantin Belousov 		VM_OBJECT_WLOCK(obj);
2207146d6cbSPoul-Henning Kamp 		vm_object_terminate(obj);
22190880a1bSKonstantin Belousov 	} else {
22290880a1bSKonstantin Belousov 		/*
2237146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
2247146d6cbSPoul-Henning Kamp 		 */
2257146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
22689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
2277146d6cbSPoul-Henning Kamp 	}
22890880a1bSKonstantin Belousov 	KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
2297146d6cbSPoul-Henning Kamp }
2307146d6cbSPoul-Henning Kamp 
2317146d6cbSPoul-Henning Kamp 
232df8bae1dSRodney W. Grimes /*
233df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
234df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
235df8bae1dSRodney W. Grimes  */
23624a1cce3SDavid Greenman vm_object_t
2376cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
2383364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
239df8bae1dSRodney W. Grimes {
24006cb7259SDavid Greenman 	vm_object_t object;
241df8bae1dSRodney W. Grimes 	struct vnode *vp;
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes 	/*
244df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
245df8bae1dSRodney W. Grimes 	 */
246df8bae1dSRodney W. Grimes 	if (handle == NULL)
247df8bae1dSRodney W. Grimes 		return (NULL);
248df8bae1dSRodney W. Grimes 
249df8bae1dSRodney W. Grimes 	vp = (struct vnode *)handle;
2506470c8d3SKonstantin Belousov 	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
251f1fa1ba3SMateusz Guzik 	VNPASS(vp->v_usecount > 0, vp);
2526470c8d3SKonstantin Belousov retry:
2536470c8d3SKonstantin Belousov 	object = vp->v_object;
2542be70f79SJohn Dyson 
25524a1cce3SDavid Greenman 	if (object == NULL) {
256df8bae1dSRodney W. Grimes 		/*
2572ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
258df8bae1dSRodney W. Grimes 		 */
2596470c8d3SKonstantin Belousov 		object = vm_object_allocate(OBJT_VNODE,
2606470c8d3SKonstantin Belousov 		    OFF_TO_IDX(round_page(size)));
261bbc0ec52SDavid Greenman 
2626cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
26384110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
264e5818a53SJeff Roberson 		object->domain.dr_policy = vnode_domainset;
26524a1cce3SDavid Greenman 		object->handle = handle;
266208b81bbSKonstantin Belousov 		if ((vp->v_vflag & VV_VMSIZEVNLOCK) != 0) {
267208b81bbSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
268208b81bbSKonstantin Belousov 			vm_object_set_flag(object, OBJ_SIZEVNLOCK);
269208b81bbSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
270208b81bbSKonstantin Belousov 		}
27111be8415SStephan Uphoff 		VI_LOCK(vp);
2722ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2732ac78f0eSStephan Uphoff 			/*
2746470c8d3SKonstantin Belousov 			 * Object has been created while we were allocating.
2752ac78f0eSStephan Uphoff 			 */
27611be8415SStephan Uphoff 			VI_UNLOCK(vp);
2779cddade7SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
2789cddade7SKonstantin Belousov 			KASSERT(object->ref_count == 1,
2799cddade7SKonstantin Belousov 			    ("leaked ref %p %d", object, object->ref_count));
2809cddade7SKonstantin Belousov 			object->type = OBJT_DEAD;
28151df5321SJeff Roberson 			refcount_init(&object->ref_count, 0);
2829cddade7SKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
2832ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2842ac78f0eSStephan Uphoff 			goto retry;
285df8bae1dSRodney W. Grimes 		}
2862ac78f0eSStephan Uphoff 		vp->v_object = object;
28711be8415SStephan Uphoff 		VI_UNLOCK(vp);
288a67d5408SJeff Roberson 		vrefact(vp);
28911be8415SStephan Uphoff 	} else {
290a67d5408SJeff Roberson 		vm_object_reference(object);
2913d653db0SAlan Cox #if VM_NRESERVLEVEL > 0
292a67d5408SJeff Roberson 		if ((object->flags & OBJ_COLORED) == 0) {
293a67d5408SJeff Roberson 			VM_OBJECT_WLOCK(object);
2943d653db0SAlan Cox 			vm_object_color(object, 0);
29589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
29611be8415SStephan Uphoff 		}
297a67d5408SJeff Roberson #endif
298a67d5408SJeff Roberson 	}
29924a1cce3SDavid Greenman 	return (object);
300df8bae1dSRodney W. Grimes }
301df8bae1dSRodney W. Grimes 
302658ad5ffSAlan Cox /*
303658ad5ffSAlan Cox  *	The object must be locked.
304658ad5ffSAlan Cox  */
305f708ef1bSPoul-Henning Kamp static void
3067ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
30724a1cce3SDavid Greenman {
308b9f180d1SKonstantin Belousov 	struct vnode *vp;
309b9f180d1SKonstantin Belousov 	int refs;
310df8bae1dSRodney W. Grimes 
311b9f180d1SKonstantin Belousov 	vp = object->handle;
31224a1cce3SDavid Greenman 	if (vp == NULL)
31324a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
31424a1cce3SDavid Greenman 
31589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
31666095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
317b9f180d1SKonstantin Belousov 	refs = object->ref_count;
31824a1cce3SDavid Greenman 
31924a1cce3SDavid Greenman 	object->handle = NULL;
32095461b45SJohn Dyson 	object->type = OBJT_DEAD;
32157fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
32284110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
32384110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
32478022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
325b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
326b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
32784110e7eSKonstantin Belousov 	}
328aa2cabb9SDavid Greenman 	vp->v_object = NULL;
32978022527SKonstantin Belousov 	VI_LOCK(vp);
33078022527SKonstantin Belousov 
33178022527SKonstantin Belousov 	/*
33278022527SKonstantin Belousov 	 * vm_map_entry_set_vnode_text() cannot reach this vnode by
33378022527SKonstantin Belousov 	 * following object->handle.  Clear all text references now.
33478022527SKonstantin Belousov 	 * This also clears the transient references from
33578022527SKonstantin Belousov 	 * kern_execve(), which is fine because dead_vnodeops uses nop
33678022527SKonstantin Belousov 	 * for VOP_UNSET_TEXT().
33778022527SKonstantin Belousov 	 */
33878022527SKonstantin Belousov 	if (vp->v_writecount < 0)
33978022527SKonstantin Belousov 		vp->v_writecount = 0;
34078022527SKonstantin Belousov 	VI_UNLOCK(vp);
34189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
342a67d5408SJeff Roberson 	if (refs > 0)
343b9f180d1SKonstantin Belousov 		vunref(vp);
34489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
345df8bae1dSRodney W. Grimes }
34626f9a767SRodney W. Grimes 
347f708ef1bSPoul-Henning Kamp static boolean_t
3487ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
3497ebba1f8SGleb Smirnoff     int *after)
350df8bae1dSRodney W. Grimes {
35124a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
35298b0c789SPoul-Henning Kamp 	daddr_t bn;
3534153054aSJeff Roberson 	uintptr_t lockstate;
3543af76890SPoul-Henning Kamp 	int err;
355170db9c6SJohn Dyson 	daddr_t reqblock;
3562c4488fcSJohn Dyson 	int poff;
3572c4488fcSJohn Dyson 	int bsize;
358d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
359df8bae1dSRodney W. Grimes 
3604153054aSJeff Roberson 	VM_OBJECT_ASSERT_LOCKED(object);
36124579ca1SMatthew Dillon 	/*
36224579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
36324579ca1SMatthew Dillon 	 * have the page.
36424579ca1SMatthew Dillon 	 */
365abd80ddbSMateusz Guzik 	if (vp == NULL || VN_IS_DOOMED(vp))
36647221757SJohn Dyson 		return FALSE;
367df8bae1dSRodney W. Grimes 	/*
368b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3690d94caffSDavid Greenman 	 * not have the page.
370df8bae1dSRodney W. Grimes 	 */
371b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3724abc71c0SDavid Greenman 		return FALSE;
373df8bae1dSRodney W. Grimes 
374eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
375170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
376d63596ceSJohn Dyson 	blocksperpage = 0;
377d63596ceSJohn Dyson 	if (pagesperblock > 0) {
378a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
379d63596ceSJohn Dyson 	} else {
380d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
381d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
382d63596ceSJohn Dyson 	}
3834153054aSJeff Roberson 	lockstate = VM_OBJECT_DROP(object);
384ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
3854153054aSJeff Roberson 	VM_OBJECT_PICKUP(object, lockstate);
3860d94caffSDavid Greenman 	if (err)
38724a1cce3SDavid Greenman 		return TRUE;
3886eab77f2SJohn Dyson 	if (bn == -1)
389ced399eeSJohn Dyson 		return FALSE;
390d63596ceSJohn Dyson 	if (pagesperblock > 0) {
391a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
392170db9c6SJohn Dyson 		if (before) {
393170db9c6SJohn Dyson 			*before *= pagesperblock;
394170db9c6SJohn Dyson 			*before += poff;
395170db9c6SJohn Dyson 		}
396170db9c6SJohn Dyson 		if (after) {
39784d31376SGleb Smirnoff 			/*
39884d31376SGleb Smirnoff 			 * The BMAP vop can report a partial block in the
399d2596d17SGleb Smirnoff 			 * 'after', but must not report blocks after EOF.
40084d31376SGleb Smirnoff 			 * Assert the latter, and truncate 'after' in case
40184d31376SGleb Smirnoff 			 * of the former.
40284d31376SGleb Smirnoff 			 */
403d2596d17SGleb Smirnoff 			KASSERT((reqblock + *after) * pagesperblock <
404d2596d17SGleb Smirnoff 			    roundup2(object->size, pagesperblock),
40584d31376SGleb Smirnoff 			    ("%s: reqblock %jd after %d size %ju", __func__,
40684d31376SGleb Smirnoff 			    (intmax_t )reqblock, *after,
40784d31376SGleb Smirnoff 			    (uintmax_t )object->size));
408170db9c6SJohn Dyson 			*after *= pagesperblock;
40984d31376SGleb Smirnoff 			*after += pagesperblock - (poff + 1);
41084d31376SGleb Smirnoff 			if (pindex + *after >= object->size)
41184d31376SGleb Smirnoff 				*after = object->size - 1 - pindex;
412170db9c6SJohn Dyson 		}
413d63596ceSJohn Dyson 	} else {
414d63596ceSJohn Dyson 		if (before) {
415d63596ceSJohn Dyson 			*before /= blocksperpage;
416d63596ceSJohn Dyson 		}
417d63596ceSJohn Dyson 
418d63596ceSJohn Dyson 		if (after) {
419d63596ceSJohn Dyson 			*after /= blocksperpage;
420d63596ceSJohn Dyson 		}
421d63596ceSJohn Dyson 	}
422ced399eeSJohn Dyson 	return TRUE;
423df8bae1dSRodney W. Grimes }
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes /*
426df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
42724a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
428df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
429df8bae1dSRodney W. Grimes  *
430df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
431df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
432df8bae1dSRodney W. Grimes  */
433df8bae1dSRodney W. Grimes void
4347ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
435df8bae1dSRodney W. Grimes {
4362a8f9ab5SAlan Cox 	vm_object_t object;
4372a8f9ab5SAlan Cox 	vm_page_t m;
438c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
439df8bae1dSRodney W. Grimes 
4402a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
441df8bae1dSRodney W. Grimes 		return;
4425b87ecc6SKonstantin Belousov #ifdef DEBUG_VFS_LOCKS
4435b87ecc6SKonstantin Belousov 	{
4445b87ecc6SKonstantin Belousov 		struct mount *mp;
4455b87ecc6SKonstantin Belousov 
4465b87ecc6SKonstantin Belousov 		mp = vp->v_mount;
4475b87ecc6SKonstantin Belousov 		if (mp != NULL && (mp->mnt_kern_flag & MNTK_VMSETSIZE_BUG) == 0)
4485b87ecc6SKonstantin Belousov 			assert_vop_elocked(vp,
4495b87ecc6SKonstantin Belousov 			    "vnode_pager_setsize and not locked vnode");
4505b87ecc6SKonstantin Belousov 	}
4515b87ecc6SKonstantin Belousov #endif
45289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
4539b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
4549b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
4559b8851faSKonstantin Belousov 		return;
4569b8851faSKonstantin Belousov 	}
4579b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
4589b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
4592a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
460df8bae1dSRodney W. Grimes 		/*
461df8bae1dSRodney W. Grimes 		 * Hasn't changed size
462df8bae1dSRodney W. Grimes 		 */
46389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
464df8bae1dSRodney W. Grimes 		return;
4652a8f9ab5SAlan Cox 	}
466c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
4672a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
468df8bae1dSRodney W. Grimes 		/*
469bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
470df8bae1dSRodney W. Grimes 		 */
4712a8f9ab5SAlan Cox 		if (nobjsize < object->size)
472c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
4736bbee8e2SAlan Cox 			    0);
474bbc0ec52SDavid Greenman 		/*
475bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4763ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4773ebeaf59SMatthew Dillon 		 *
4783ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4793ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4803ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
481bbc0ec52SDavid Greenman 		 */
4820012f373SJeff Roberson 		if (!(nsize & PAGE_MASK))
4830012f373SJeff Roberson 			goto out;
4840012f373SJeff Roberson 		m = vm_page_grab(object, OFF_TO_IDX(nsize), VM_ALLOC_NOCREAT);
4850012f373SJeff Roberson 		if (m == NULL)
4860012f373SJeff Roberson 			goto out;
4870012f373SJeff Roberson 		if (!vm_page_none_valid(m)) {
4882b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4892b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4902b6b0df7SMatthew Dillon 
4912b6b0df7SMatthew Dillon 			/*
4922b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4932b6b0df7SMatthew Dillon 			 * the page has been mapped.
4942b6b0df7SMatthew Dillon 			 */
495fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4962b6b0df7SMatthew Dillon 
4972b6b0df7SMatthew Dillon 			/*
4983c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4993c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
5003c33df62SAlan Cox 			 * have already been set.
5013c33df62SAlan Cox 			 */
502dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
5033c33df62SAlan Cox 
5043c33df62SAlan Cox 			/*
5053c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
5063c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
5073c33df62SAlan Cox 			 * cleared.
5083c33df62SAlan Cox 			 */
5093c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
5103c33df62SAlan Cox 
5113c33df62SAlan Cox 			/*
5123c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
5133ebeaf59SMatthew Dillon 			 *
5143ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
5153ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
5163ebeaf59SMatthew Dillon 			 * replacement from working properly.
5172b6b0df7SMatthew Dillon 			 */
5183c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
519bbc0ec52SDavid Greenman 		}
5200012f373SJeff Roberson 		vm_page_xunbusy(m);
521bbc0ec52SDavid Greenman 	}
5220012f373SJeff Roberson out:
523a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
524c576d121SLuoqi Chen 	object->size = nobjsize;
52589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
526df8bae1dSRodney W. Grimes }
527df8bae1dSRodney W. Grimes 
52826f9a767SRodney W. Grimes /*
52926f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
53026f9a767SRodney W. Grimes  * file address
53126f9a767SRodney W. Grimes  */
532bff76343SAlan Cox static int
533bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
534bff76343SAlan Cox     int *run)
53526f9a767SRodney W. Grimes {
53626f9a767SRodney W. Grimes 	int bsize;
53726f9a767SRodney W. Grimes 	int err;
538a316d390SJohn Dyson 	daddr_t vblock;
539f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
54026f9a767SRodney W. Grimes 
5412ad036b6SAlan Cox 	if (address < 0)
5420d94caffSDavid Greenman 		return -1;
5430d94caffSDavid Greenman 
544abd80ddbSMateusz Guzik 	if (VN_IS_DOOMED(vp))
5452c4488fcSJohn Dyson 		return -1;
5462c4488fcSJohn Dyson 
54726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
54826f9a767SRodney W. Grimes 	vblock = address / bsize;
54926f9a767SRodney W. Grimes 	voffset = address % bsize;
55026f9a767SRodney W. Grimes 
551bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
552bff76343SAlan Cox 	if (err == 0) {
553bff76343SAlan Cox 		if (*rtaddress != -1)
554bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
555efc68ce1SDavid Greenman 		if (run) {
556efc68ce1SDavid Greenman 			*run += 1;
557efc68ce1SDavid Greenman 			*run *= bsize / PAGE_SIZE;
558efc68ce1SDavid Greenman 			*run -= voffset / PAGE_SIZE;
559efc68ce1SDavid Greenman 		}
560efc68ce1SDavid Greenman 	}
56126f9a767SRodney W. Grimes 
562bff76343SAlan Cox 	return (err);
56326f9a767SRodney W. Grimes }
56426f9a767SRodney W. Grimes 
56526f9a767SRodney W. Grimes /*
56626f9a767SRodney W. Grimes  * small block filesystem vnode pager input
56726f9a767SRodney W. Grimes  */
568f708ef1bSPoul-Henning Kamp static int
5697ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
57026f9a767SRodney W. Grimes {
5719c83534dSPoul-Henning Kamp 	struct vnode *vp;
5729c83534dSPoul-Henning Kamp 	struct bufobj *bo;
57326f9a767SRodney W. Grimes 	struct buf *bp;
5749e0ddbd0SAlan Cox 	struct sf_buf *sf;
575f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
57626f9a767SRodney W. Grimes 	vm_offset_t bsize;
577561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
578561cc9fcSKonstantin Belousov 	int error, i;
57926f9a767SRodney W. Grimes 
580561cc9fcSKonstantin Belousov 	error = 0;
58124a1cce3SDavid Greenman 	vp = object->handle;
582abd80ddbSMateusz Guzik 	if (VN_IS_DOOMED(vp))
5832c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5842c4488fcSJohn Dyson 
58526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5860bdb7528SDavid Greenman 
5879c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
58826f9a767SRodney W. Grimes 
5899e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
59026f9a767SRodney W. Grimes 
59126f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
59233c67741SMatthew Dillon 		vm_ooffset_t address;
593bbc0ec52SDavid Greenman 
5940d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5950d53a17bSAlan Cox 		if (m->valid & bits)
59626f9a767SRodney W. Grimes 			continue;
59726f9a767SRodney W. Grimes 
59833c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
59933c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
60033c67741SMatthew Dillon 			fileaddr = -1;
60133c67741SMatthew Dillon 		} else {
602bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
603bff76343SAlan Cox 			if (error)
604bff76343SAlan Cox 				break;
60533c67741SMatthew Dillon 		}
60626f9a767SRodney W. Grimes 		if (fileaddr != -1) {
607756a5412SGleb Smirnoff 			bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
60826f9a767SRodney W. Grimes 
60926f9a767SRodney W. Grimes 			/* build a minimal buffer header */
61021144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
6116a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
612bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
613bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
614a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
615a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
6169e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
617187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
6189c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
6191faacf5dSKirk McKusick 			bp->b_vp = vp;
62026f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
62126f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
6222b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
6235bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
62426f9a767SRodney W. Grimes 
62526f9a767SRodney W. Grimes 			/* do the input */
6262c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
627b792bebeSPoul-Henning Kamp 			bstrategy(bp);
62826f9a767SRodney W. Grimes 
6296a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
6306a4b5823SPoul-Henning Kamp 
631cafbf0c6SWarner Losh 			if ((bp->b_ioflags & BIO_ERROR) != 0) {
632cafbf0c6SWarner Losh 				KASSERT(bp->b_error != 0,
633cafbf0c6SWarner Losh 				    ("%s: buf error but b_error == 0\n", __func__));
634cafbf0c6SWarner Losh 				error = bp->b_error;
635cafbf0c6SWarner Losh 			}
63626f9a767SRodney W. Grimes 
63726f9a767SRodney W. Grimes 			/*
63826f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
63926f9a767SRodney W. Grimes 			 */
6401faacf5dSKirk McKusick 			bp->b_vp = NULL;
6419c83534dSPoul-Henning Kamp 			pbrelbo(bp);
642756a5412SGleb Smirnoff 			uma_zfree(vnode_pbuf_zone, bp);
64326f9a767SRodney W. Grimes 			if (error)
64426f9a767SRodney W. Grimes 				break;
6450d53a17bSAlan Cox 		} else
6469e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
6470d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
6480d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
6497f935055SJeff Roberson 		vm_page_bits_set(m, &m->valid, bits);
65026f9a767SRodney W. Grimes 	}
6519e0ddbd0SAlan Cox 	sf_buf_free(sf);
65226f9a767SRodney W. Grimes 	if (error) {
653a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
65426f9a767SRodney W. Grimes 	}
65526f9a767SRodney W. Grimes 	return VM_PAGER_OK;
65626f9a767SRodney W. Grimes }
65726f9a767SRodney W. Grimes 
65826f9a767SRodney W. Grimes /*
659475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
66026f9a767SRodney W. Grimes  */
661f708ef1bSPoul-Henning Kamp static int
6627ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
66326f9a767SRodney W. Grimes {
664df8bae1dSRodney W. Grimes 	struct uio auio;
665df8bae1dSRodney W. Grimes 	struct iovec aiov;
66626f9a767SRodney W. Grimes 	int error;
66726f9a767SRodney W. Grimes 	int size;
6689e0ddbd0SAlan Cox 	struct sf_buf *sf;
669342a1480SJohn Baldwin 	struct vnode *vp;
670df8bae1dSRodney W. Grimes 
67189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
67226f9a767SRodney W. Grimes 	error = 0;
673bbc0ec52SDavid Greenman 
674df8bae1dSRodney W. Grimes 	/*
67526f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
67626f9a767SRodney W. Grimes 	 */
677a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
67826f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
67926f9a767SRodney W. Grimes 	} else {
68026f9a767SRodney W. Grimes 		size = PAGE_SIZE;
681a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
682a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
68352051abcSAlan Cox 		vp = object->handle;
68489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
6850bdb7528SDavid Greenman 
68626f9a767SRodney W. Grimes 		/*
687df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
688df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
689df8bae1dSRodney W. Grimes 		 */
6909e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6910bdb7528SDavid Greenman 
6929e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
693df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
694df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
695df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
696a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
697df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
69826f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
699df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
700b40ce416SJulian Elischer 		auio.uio_td = curthread;
70126f9a767SRodney W. Grimes 
702a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
703df8bae1dSRodney W. Grimes 		if (!error) {
70454d92145SMatthew Dillon 			int count = size - auio.uio_resid;
705df8bae1dSRodney W. Grimes 
706df8bae1dSRodney W. Grimes 			if (count == 0)
707df8bae1dSRodney W. Grimes 				error = EINVAL;
70826f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
7099e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
7109e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
711df8bae1dSRodney W. Grimes 		}
7129e0ddbd0SAlan Cox 		sf_buf_free(sf);
7131b26eb10SAlan Cox 
71489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
715df8bae1dSRodney W. Grimes 	}
7160d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
7176e3a3f38SRobert V. Baron 	if (!error)
7180012f373SJeff Roberson 		vm_page_valid(m);
719a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
72026f9a767SRodney W. Grimes }
72126f9a767SRodney W. Grimes 
72226f9a767SRodney W. Grimes /*
72326f9a767SRodney W. Grimes  * generic vnode pager input routine
72426f9a767SRodney W. Grimes  */
725170db9c6SJohn Dyson 
726ce75f2c3SMike Smith /*
72723955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
72847e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
72947e151ddSRobert Drehmel  * to implement the previous behaviour.
730ce75f2c3SMike Smith  *
731ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
732ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
733ce75f2c3SMike Smith  */
734f708ef1bSPoul-Henning Kamp static int
735b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
736b0cd2017SGleb Smirnoff     int *rahead)
73724a1cce3SDavid Greenman {
738170db9c6SJohn Dyson 	struct vnode *vp;
739b0cd2017SGleb Smirnoff 	int rtval;
74095e5e988SJohn Dyson 
741d6e13f3bSJeff Roberson 	/* Handle is stable with paging in progress. */
742170db9c6SJohn Dyson 	vp = object->handle;
743b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
74423955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
74523955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
746170db9c6SJohn Dyson 	return rtval;
747170db9c6SJohn Dyson }
748170db9c6SJohn Dyson 
74990effb23SGleb Smirnoff static int
75090effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
751b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
75290effb23SGleb Smirnoff {
75390effb23SGleb Smirnoff 	struct vnode *vp;
75490effb23SGleb Smirnoff 	int rtval;
75590effb23SGleb Smirnoff 
75690effb23SGleb Smirnoff 	vp = object->handle;
757b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
75890effb23SGleb Smirnoff 	KASSERT(rtval != EOPNOTSUPP,
75990effb23SGleb Smirnoff 	    ("vnode_pager: FS getpages_async not implemented\n"));
76090effb23SGleb Smirnoff 	return (rtval);
76190effb23SGleb Smirnoff }
76290effb23SGleb Smirnoff 
763ce75f2c3SMike Smith /*
76490effb23SGleb Smirnoff  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
76590effb23SGleb Smirnoff  * local filesystems, where partially valid pages can only occur at
76690effb23SGleb Smirnoff  * the end of file.
767d15b55c5SKonstantin Belousov  */
768d15b55c5SKonstantin Belousov int
769d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
770d15b55c5SKonstantin Belousov {
77190effb23SGleb Smirnoff 
772b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
773b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
77490effb23SGleb Smirnoff }
77590effb23SGleb Smirnoff 
77690effb23SGleb Smirnoff int
77790effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
77890effb23SGleb Smirnoff {
779abfdf767SKonstantin Belousov 	int error;
78090effb23SGleb Smirnoff 
781abfdf767SKonstantin Belousov 	error = vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
782abfdf767SKonstantin Belousov 	    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
783abfdf767SKonstantin Belousov 	if (error != 0 && ap->a_iodone != NULL)
784abfdf767SKonstantin Belousov 		ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
785abfdf767SKonstantin Belousov 	return (error);
786d15b55c5SKonstantin Belousov }
787d15b55c5SKonstantin Belousov 
788d15b55c5SKonstantin Belousov /*
789ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
790ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
791ce75f2c3SMike Smith  */
792ce75f2c3SMike Smith int
793b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
794b0cd2017SGleb Smirnoff     int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
795170db9c6SJohn Dyson {
796ce75f2c3SMike Smith 	vm_object_t object;
7979c83534dSPoul-Henning Kamp 	struct bufobj *bo;
7980bdb7528SDavid Greenman 	struct buf *bp;
799b0cd2017SGleb Smirnoff 	off_t foff;
800e48b82bdSGleb Smirnoff #ifdef INVARIANTS
801e48b82bdSGleb Smirnoff 	off_t blkno0;
802e48b82bdSGleb Smirnoff #endif
803756a5412SGleb Smirnoff 	int bsize, pagesperblock;
804b0cd2017SGleb Smirnoff 	int error, before, after, rbehind, rahead, poff, i;
805b0cd2017SGleb Smirnoff 	int bytecount, secmask;
806ce75f2c3SMike Smith 
8079c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
808b0cd2017SGleb Smirnoff 	    ("%s does not support devices", __func__));
809b0cd2017SGleb Smirnoff 
810abd80ddbSMateusz Guzik 	if (VN_IS_DOOMED(vp))
811eac91e32SKonstantin Belousov 		return (VM_PAGER_BAD);
8122c4488fcSJohn Dyson 
813eac91e32SKonstantin Belousov 	object = vp->v_object;
814b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(m[0]->pindex);
81526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
816b0cd2017SGleb Smirnoff 	pagesperblock = bsize / PAGE_SIZE;
817b0cd2017SGleb Smirnoff 
818b0cd2017SGleb Smirnoff 	KASSERT(foff < object->un_pager.vnp.vnp_size,
819b0cd2017SGleb Smirnoff 	    ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
82040a51684SJason A. Harmening 	KASSERT(count <= nitems(bp->b_pages),
821b0cd2017SGleb Smirnoff 	    ("%s: requested %d pages", __func__, count));
822b0cd2017SGleb Smirnoff 
823b0cd2017SGleb Smirnoff 	/*
824b0cd2017SGleb Smirnoff 	 * The last page has valid blocks.  Invalid part can only
825b0cd2017SGleb Smirnoff 	 * exist at the end of file, and the page is made fully valid
826b0cd2017SGleb Smirnoff 	 * by zeroing in vm_pager_get_pages().
827b0cd2017SGleb Smirnoff 	 */
8280012f373SJeff Roberson 	if (!vm_page_none_valid(m[count - 1]) && --count == 0) {
829b0cd2017SGleb Smirnoff 		if (iodone != NULL)
830b0cd2017SGleb Smirnoff 			iodone(arg, m, 1, 0);
831b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
832b0cd2017SGleb Smirnoff 	}
833bbc0ec52SDavid Greenman 
834756a5412SGleb Smirnoff 	bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
83573e9030eSGleb Smirnoff 
83626f9a767SRodney W. Grimes 	/*
837e122dfc1SGleb Smirnoff 	 * Get the underlying device blocks for the file with VOP_BMAP().
838e122dfc1SGleb Smirnoff 	 * If the file system doesn't support VOP_BMAP, use old way of
839e122dfc1SGleb Smirnoff 	 * getting pages via VOP_READ.
84026f9a767SRodney W. Grimes 	 */
841b0cd2017SGleb Smirnoff 	error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
8421de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
843756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
84489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
845b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
84683c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodein);
84783c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodepgsin);
848b0cd2017SGleb Smirnoff 			error = vnode_pager_input_old(object, m[i]);
849b0cd2017SGleb Smirnoff 			if (error)
850b0cd2017SGleb Smirnoff 				break;
851b0cd2017SGleb Smirnoff 		}
85289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
85352051abcSAlan Cox 		return (error);
8541de11f1aSAlan Cox 	} else if (error != 0) {
855756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
8561de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
857b0cd2017SGleb Smirnoff 	}
858bbc0ec52SDavid Greenman 
85926f9a767SRodney W. Grimes 	/*
860b0cd2017SGleb Smirnoff 	 * If the file system supports BMAP, but blocksize is smaller
861b0cd2017SGleb Smirnoff 	 * than a page size, then use special small filesystem code.
86226f9a767SRodney W. Grimes 	 */
863b0cd2017SGleb Smirnoff 	if (pagesperblock == 0) {
864756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
865b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
86683c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodein);
86783c9dea1SGleb Smirnoff 			VM_CNT_INC(v_vnodepgsin);
868b0cd2017SGleb Smirnoff 			error = vnode_pager_input_smlfs(object, m[i]);
869b0cd2017SGleb Smirnoff 			if (error)
870b0cd2017SGleb Smirnoff 				break;
871b0cd2017SGleb Smirnoff 		}
872b0cd2017SGleb Smirnoff 		return (error);
87326f9a767SRodney W. Grimes 	}
8748d17e694SJulian Elischer 
87526f9a767SRodney W. Grimes 	/*
876b0cd2017SGleb Smirnoff 	 * A sparse file can be encountered only for a single page request,
877763df3ecSPedro F. Giffuni 	 * which may not be preceded by call to vm_pager_haspage().
878a7fecb4dSAlan Cox 	 */
879b0cd2017SGleb Smirnoff 	if (bp->b_blkno == -1) {
880b0cd2017SGleb Smirnoff 		KASSERT(count == 1,
881b0cd2017SGleb Smirnoff 		    ("%s: array[%d] request to a sparse file %p", __func__,
882b0cd2017SGleb Smirnoff 		    count, vp));
883756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
884b0cd2017SGleb Smirnoff 		pmap_zero_page(m[0]);
885b0cd2017SGleb Smirnoff 		KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
886b0cd2017SGleb Smirnoff 		    __func__, m[0]));
8870012f373SJeff Roberson 		vm_page_valid(m[0]);
888f4f83da0SAlan Cox 		return (VM_PAGER_OK);
889b0cd2017SGleb Smirnoff 	}
890b0cd2017SGleb Smirnoff 
891e48b82bdSGleb Smirnoff #ifdef INVARIANTS
892e48b82bdSGleb Smirnoff 	blkno0 = bp->b_blkno;
893e48b82bdSGleb Smirnoff #endif
894b0cd2017SGleb Smirnoff 	bp->b_blkno += (foff % bsize) / DEV_BSIZE;
895b0cd2017SGleb Smirnoff 
896b0cd2017SGleb Smirnoff 	/* Recalculate blocks available after/before to pages. */
897b0cd2017SGleb Smirnoff 	poff = (foff % bsize) / PAGE_SIZE;
898b0cd2017SGleb Smirnoff 	before *= pagesperblock;
899b0cd2017SGleb Smirnoff 	before += poff;
900b0cd2017SGleb Smirnoff 	after *= pagesperblock;
901b0cd2017SGleb Smirnoff 	after += pagesperblock - (poff + 1);
902b0cd2017SGleb Smirnoff 	if (m[0]->pindex + after >= object->size)
903b0cd2017SGleb Smirnoff 		after = object->size - 1 - m[0]->pindex;
904b0cd2017SGleb Smirnoff 	KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
905b0cd2017SGleb Smirnoff 	    __func__, count, after + 1));
906b0cd2017SGleb Smirnoff 	after -= count - 1;
907b0cd2017SGleb Smirnoff 
908b0cd2017SGleb Smirnoff 	/* Trim requested rbehind/rahead to possible values. */
909b0cd2017SGleb Smirnoff 	rbehind = a_rbehind ? *a_rbehind : 0;
910b0cd2017SGleb Smirnoff 	rahead = a_rahead ? *a_rahead : 0;
911b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, before);
912b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, m[0]->pindex);
913b0cd2017SGleb Smirnoff 	rahead = min(rahead, after);
914b0cd2017SGleb Smirnoff 	rahead = min(rahead, object->size - m[count - 1]->pindex);
915e48b82bdSGleb Smirnoff 	/*
916e48b82bdSGleb Smirnoff 	 * Check that total amount of pages fit into buf.  Trim rbehind and
917e48b82bdSGleb Smirnoff 	 * rahead evenly if not.
918e48b82bdSGleb Smirnoff 	 */
919e48b82bdSGleb Smirnoff 	if (rbehind + rahead + count > nitems(bp->b_pages)) {
920e48b82bdSGleb Smirnoff 		int trim, sum;
921e48b82bdSGleb Smirnoff 
922e48b82bdSGleb Smirnoff 		trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
923e48b82bdSGleb Smirnoff 		sum = rbehind + rahead;
924e48b82bdSGleb Smirnoff 		if (rbehind == before) {
925e48b82bdSGleb Smirnoff 			/* Roundup rbehind trim to block size. */
926e48b82bdSGleb Smirnoff 			rbehind -= roundup(trim * rbehind / sum, pagesperblock);
927e48b82bdSGleb Smirnoff 			if (rbehind < 0)
928e48b82bdSGleb Smirnoff 				rbehind = 0;
929e48b82bdSGleb Smirnoff 		} else
930e48b82bdSGleb Smirnoff 			rbehind -= trim * rbehind / sum;
931e48b82bdSGleb Smirnoff 		rahead -= trim * rahead / sum;
932e48b82bdSGleb Smirnoff 	}
933e48b82bdSGleb Smirnoff 	KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
934b0cd2017SGleb Smirnoff 	    ("%s: behind %d ahead %d count %d", __func__,
935b0cd2017SGleb Smirnoff 	    rbehind, rahead, count));
936b0cd2017SGleb Smirnoff 
937b0cd2017SGleb Smirnoff 	/*
938b0cd2017SGleb Smirnoff 	 * Fill in the bp->b_pages[] array with requested and optional
939b0cd2017SGleb Smirnoff 	 * read behind or read ahead pages.  Read behind pages are looked
940b0cd2017SGleb Smirnoff 	 * up in a backward direction, down to a first cached page.  Same
941b0cd2017SGleb Smirnoff 	 * for read ahead pages, but there is no need to shift the array
942b0cd2017SGleb Smirnoff 	 * in case of encountering a cached page.
943b0cd2017SGleb Smirnoff 	 */
944b0cd2017SGleb Smirnoff 	i = bp->b_npages = 0;
945b0cd2017SGleb Smirnoff 	if (rbehind) {
946b0cd2017SGleb Smirnoff 		vm_pindex_t startpindex, tpindex;
947b0cd2017SGleb Smirnoff 		vm_page_t p;
948b0cd2017SGleb Smirnoff 
949a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
950b0cd2017SGleb Smirnoff 		startpindex = m[0]->pindex - rbehind;
951b0cd2017SGleb Smirnoff 		if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
952b0cd2017SGleb Smirnoff 		    p->pindex >= startpindex)
953b0cd2017SGleb Smirnoff 			startpindex = p->pindex + 1;
954b0cd2017SGleb Smirnoff 
955b0cd2017SGleb Smirnoff 		/* tpindex is unsigned; beware of numeric underflow. */
956b0cd2017SGleb Smirnoff 		for (tpindex = m[0]->pindex - 1;
957b0cd2017SGleb Smirnoff 		    tpindex >= startpindex && tpindex < m[0]->pindex;
958b0cd2017SGleb Smirnoff 		    tpindex--, i++) {
9597667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
960b0cd2017SGleb Smirnoff 			if (p == NULL) {
961b0cd2017SGleb Smirnoff 				/* Shift the array. */
962b0cd2017SGleb Smirnoff 				for (int j = 0; j < i; j++)
963b0cd2017SGleb Smirnoff 					bp->b_pages[j] = bp->b_pages[j +
964b0cd2017SGleb Smirnoff 					    tpindex + 1 - startpindex];
965b0cd2017SGleb Smirnoff 				break;
966b0cd2017SGleb Smirnoff 			}
967b0cd2017SGleb Smirnoff 			bp->b_pages[tpindex - startpindex] = p;
968a7fecb4dSAlan Cox 		}
9690bdb7528SDavid Greenman 
970b0cd2017SGleb Smirnoff 		bp->b_pgbefore = i;
971b0cd2017SGleb Smirnoff 		bp->b_npages += i;
972b0cd2017SGleb Smirnoff 		bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
973b0cd2017SGleb Smirnoff 	} else
974b0cd2017SGleb Smirnoff 		bp->b_pgbefore = 0;
975b0cd2017SGleb Smirnoff 
976b0cd2017SGleb Smirnoff 	/* Requested pages. */
977b0cd2017SGleb Smirnoff 	for (int j = 0; j < count; j++, i++)
978b0cd2017SGleb Smirnoff 		bp->b_pages[i] = m[j];
979b0cd2017SGleb Smirnoff 	bp->b_npages += count;
980b0cd2017SGleb Smirnoff 
981b0cd2017SGleb Smirnoff 	if (rahead) {
982b0cd2017SGleb Smirnoff 		vm_pindex_t endpindex, tpindex;
983b0cd2017SGleb Smirnoff 		vm_page_t p;
984b0cd2017SGleb Smirnoff 
985b0cd2017SGleb Smirnoff 		if (!VM_OBJECT_WOWNED(object))
986eac91e32SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
987b0cd2017SGleb Smirnoff 		endpindex = m[count - 1]->pindex + rahead + 1;
988b0cd2017SGleb Smirnoff 		if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
989b0cd2017SGleb Smirnoff 		    p->pindex < endpindex)
990b0cd2017SGleb Smirnoff 			endpindex = p->pindex;
991b0cd2017SGleb Smirnoff 		if (endpindex > object->size)
992b0cd2017SGleb Smirnoff 			endpindex = object->size;
993b0cd2017SGleb Smirnoff 
994b0cd2017SGleb Smirnoff 		for (tpindex = m[count - 1]->pindex + 1;
995b0cd2017SGleb Smirnoff 		    tpindex < endpindex; i++, tpindex++) {
9967667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
997b0cd2017SGleb Smirnoff 			if (p == NULL)
998b0cd2017SGleb Smirnoff 				break;
999b0cd2017SGleb Smirnoff 			bp->b_pages[i] = p;
1000eac91e32SKonstantin Belousov 		}
1001b0cd2017SGleb Smirnoff 
1002b0cd2017SGleb Smirnoff 		bp->b_pgafter = i - bp->b_npages;
1003b0cd2017SGleb Smirnoff 		bp->b_npages = i;
1004b0cd2017SGleb Smirnoff 	} else
1005b0cd2017SGleb Smirnoff 		bp->b_pgafter = 0;
1006b0cd2017SGleb Smirnoff 
1007b0cd2017SGleb Smirnoff 	if (VM_OBJECT_WOWNED(object))
1008eac91e32SKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
1009b0cd2017SGleb Smirnoff 
1010b0cd2017SGleb Smirnoff 	/* Report back actual behind/ahead read. */
1011b0cd2017SGleb Smirnoff 	if (a_rbehind)
1012b0cd2017SGleb Smirnoff 		*a_rbehind = bp->b_pgbefore;
1013b0cd2017SGleb Smirnoff 	if (a_rahead)
1014b0cd2017SGleb Smirnoff 		*a_rahead = bp->b_pgafter;
1015b0cd2017SGleb Smirnoff 
1016e48b82bdSGleb Smirnoff #ifdef INVARIANTS
1017dcc0ff5aSGleb Smirnoff 	KASSERT(bp->b_npages <= nitems(bp->b_pages),
1018b0cd2017SGleb Smirnoff 	    ("%s: buf %p overflowed", __func__, bp));
10194f56243aSGleb Smirnoff 	for (int j = 1, prev = 0; j < bp->b_npages; j++) {
10201e0c121fSGleb Smirnoff 		if (bp->b_pages[j] == bogus_page)
10211e0c121fSGleb Smirnoff 			continue;
10221e0c121fSGleb Smirnoff 		KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
10231e0c121fSGleb Smirnoff 		    j - prev, ("%s: pages array not consecutive, bp %p",
10241e0c121fSGleb Smirnoff 		     __func__, bp));
10251e0c121fSGleb Smirnoff 		prev = j;
10261e0c121fSGleb Smirnoff 	}
1027e48b82bdSGleb Smirnoff #endif
1028eac91e32SKonstantin Belousov 
10290d94caffSDavid Greenman 	/*
1030b0cd2017SGleb Smirnoff 	 * Recalculate first offset and bytecount with regards to read behind.
1031b0cd2017SGleb Smirnoff 	 * Truncate bytecount to vnode real size and round up physical size
1032b0cd2017SGleb Smirnoff 	 * for real devices.
103326f9a767SRodney W. Grimes 	 */
1034b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1035b0cd2017SGleb Smirnoff 	bytecount = bp->b_npages << PAGE_SHIFT;
1036b0cd2017SGleb Smirnoff 	if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
1037b0cd2017SGleb Smirnoff 		bytecount = object->un_pager.vnp.vnp_size - foff;
1038eac91e32SKonstantin Belousov 	secmask = bo->bo_bsize - 1;
10396229cc50SPoul-Henning Kamp 	KASSERT(secmask < PAGE_SIZE && secmask > 0,
1040b0cd2017SGleb Smirnoff 	    ("%s: sector size %d too large", __func__, secmask + 1));
1041b0cd2017SGleb Smirnoff 	bytecount = (bytecount + secmask) & ~secmask;
104226f9a767SRodney W. Grimes 
104326f9a767SRodney W. Grimes 	/*
1044b0cd2017SGleb Smirnoff 	 * And map the pages to be read into the kva, if the filesystem
10456ce697dcSKonstantin Belousov 	 * requires mapped buffers.
104626f9a767SRodney W. Grimes 	 */
10472a5eef69SGleb Smirnoff 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
10486ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
10496ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
10506ce697dcSKonstantin Belousov 		bp->b_offset = 0;
1051fade8dd7SJeff Roberson 	} else {
1052fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
1053b0cd2017SGleb Smirnoff 		pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1054fade8dd7SJeff Roberson 	}
105526f9a767SRodney W. Grimes 
1056b0cd2017SGleb Smirnoff 	/* Build a minimal buffer header. */
105721144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
1058bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1059bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1060a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
1061a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
10629c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
10631faacf5dSKirk McKusick 	bp->b_vp = vp;
1064b0cd2017SGleb Smirnoff 	bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
10652c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
1066e48b82bdSGleb Smirnoff 	KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1067e48b82bdSGleb Smirnoff 	    (blkno0 - bp->b_blkno) * DEV_BSIZE +
1068e48b82bdSGleb Smirnoff 	    IDX_TO_OFF(m[0]->pindex) % bsize,
1069e48b82bdSGleb Smirnoff 	    ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1070e48b82bdSGleb Smirnoff 	    "blkno0 %ju b_blkno %ju", bsize,
1071e48b82bdSGleb Smirnoff 	    (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1072e48b82bdSGleb Smirnoff 	    (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
107390effb23SGleb Smirnoff 
1074b0cd2017SGleb Smirnoff 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
107583c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vnodein);
107683c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1077b0cd2017SGleb Smirnoff 
107890effb23SGleb Smirnoff 	if (iodone != NULL) { /* async */
1079b0cd2017SGleb Smirnoff 		bp->b_pgiodone = iodone;
108090effb23SGleb Smirnoff 		bp->b_caller1 = arg;
108190effb23SGleb Smirnoff 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
108290effb23SGleb Smirnoff 		bp->b_flags |= B_ASYNC;
108390effb23SGleb Smirnoff 		BUF_KERNPROC(bp);
1084b792bebeSPoul-Henning Kamp 		bstrategy(bp);
1085b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
108690effb23SGleb Smirnoff 	} else {
108790effb23SGleb Smirnoff 		bp->b_iodone = bdone;
108890effb23SGleb Smirnoff 		bstrategy(bp);
10896a4b5823SPoul-Henning Kamp 		bwait(bp, PVM, "vnread");
109090effb23SGleb Smirnoff 		error = vnode_pager_generic_getpages_done(bp);
10911bb5ad63SGleb Smirnoff 		for (i = 0; i < bp->b_npages; i++)
10926ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
10931faacf5dSKirk McKusick 		bp->b_vp = NULL;
10949c83534dSPoul-Henning Kamp 		pbrelbo(bp);
1095756a5412SGleb Smirnoff 		uma_zfree(vnode_pbuf_zone, bp);
109690effb23SGleb Smirnoff 		return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
109790effb23SGleb Smirnoff 	}
1098b0cd2017SGleb Smirnoff }
109990effb23SGleb Smirnoff 
110090effb23SGleb Smirnoff static void
110190effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp)
110290effb23SGleb Smirnoff {
110390effb23SGleb Smirnoff 	int error;
110490effb23SGleb Smirnoff 
110590effb23SGleb Smirnoff 	error = vnode_pager_generic_getpages_done(bp);
1106b0cd2017SGleb Smirnoff 	/* Run the iodone upon the requested range. */
1107b0cd2017SGleb Smirnoff 	bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1108b0cd2017SGleb Smirnoff 	    bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
110990effb23SGleb Smirnoff 	for (int i = 0; i < bp->b_npages; i++)
111090effb23SGleb Smirnoff 		bp->b_pages[i] = NULL;
111190effb23SGleb Smirnoff 	bp->b_vp = NULL;
111290effb23SGleb Smirnoff 	pbrelbo(bp);
1113756a5412SGleb Smirnoff 	uma_zfree(vnode_pbuf_zone, bp);
111490effb23SGleb Smirnoff }
111590effb23SGleb Smirnoff 
111690effb23SGleb Smirnoff static int
111790effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp)
111890effb23SGleb Smirnoff {
111990effb23SGleb Smirnoff 	vm_object_t object;
112090effb23SGleb Smirnoff 	off_t tfoff, nextoff;
112190effb23SGleb Smirnoff 	int i, error;
112290effb23SGleb Smirnoff 
1123cafbf0c6SWarner Losh 	KASSERT((bp->b_ioflags & BIO_ERROR) == 0 || bp->b_error != 0,
1124cafbf0c6SWarner Losh 	    ("%s: buf error but b_error == 0\n", __func__));
1125cafbf0c6SWarner Losh 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? bp->b_error : 0;
112690effb23SGleb Smirnoff 	object = bp->b_vp->v_object;
112790effb23SGleb Smirnoff 
112890effb23SGleb Smirnoff 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1129fade8dd7SJeff Roberson 		if (!buf_mapped(bp)) {
1130fade8dd7SJeff Roberson 			bp->b_data = bp->b_kvabase;
1131fade8dd7SJeff Roberson 			pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
113290effb23SGleb Smirnoff 			    bp->b_npages);
113390effb23SGleb Smirnoff 		}
1134fade8dd7SJeff Roberson 		bzero(bp->b_data + bp->b_bcount,
113590effb23SGleb Smirnoff 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
113690effb23SGleb Smirnoff 	}
1137fade8dd7SJeff Roberson 	if (buf_mapped(bp)) {
1138fade8dd7SJeff Roberson 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1139fade8dd7SJeff Roberson 		bp->b_data = unmapped_buf;
114090effb23SGleb Smirnoff 	}
114126f9a767SRodney W. Grimes 
11421bd12a3bSChuck Silvers 	/*
11431bd12a3bSChuck Silvers 	 * If the read failed, we must free any read ahead/behind pages here.
11441bd12a3bSChuck Silvers 	 * The requested pages are freed by the caller (for sync requests)
11451bd12a3bSChuck Silvers 	 * or by the bp->b_pgiodone callback (for async requests).
11461bd12a3bSChuck Silvers 	 */
11471bd12a3bSChuck Silvers 	if (error != 0) {
11481bd12a3bSChuck Silvers 		VM_OBJECT_WLOCK(object);
11491bd12a3bSChuck Silvers 		for (i = 0; i < bp->b_pgbefore; i++)
11501bd12a3bSChuck Silvers 			vm_page_free_invalid(bp->b_pages[i]);
11511bd12a3bSChuck Silvers 		for (i = bp->b_npages - bp->b_pgafter; i < bp->b_npages; i++)
11521bd12a3bSChuck Silvers 			vm_page_free_invalid(bp->b_pages[i]);
11531bd12a3bSChuck Silvers 		VM_OBJECT_WUNLOCK(object);
11541bd12a3bSChuck Silvers 		return (error);
11551bd12a3bSChuck Silvers 	}
11561bd12a3bSChuck Silvers 
11577f935055SJeff Roberson 	/* Read lock to protect size. */
11587f935055SJeff Roberson 	VM_OBJECT_RLOCK(object);
115990effb23SGleb Smirnoff 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
116090effb23SGleb Smirnoff 	    i < bp->b_npages; i++, tfoff = nextoff) {
11618f9110f6SJohn Dyson 		vm_page_t mt;
11628f9110f6SJohn Dyson 
11638f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
116490effb23SGleb Smirnoff 		mt = bp->b_pages[i];
11652f81c92eSMark Johnston 		if (mt == bogus_page)
11662f81c92eSMark Johnston 			continue;
11678f9110f6SJohn Dyson 
116854746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
11698d17e694SJulian Elischer 			/*
11708d17e694SJulian Elischer 			 * Read filled up entire page.
11718d17e694SJulian Elischer 			 */
11720012f373SJeff Roberson 			vm_page_valid(mt);
1173016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
117479f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
1175016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
117679f0deb9SGleb Smirnoff 			    ("%s: page %p is mapped", __func__, mt));
11778f9110f6SJohn Dyson 		} else {
11788d17e694SJulian Elischer 			/*
117942eb4108SAlan Cox 			 * Read did not fill up entire page.
11808d17e694SJulian Elischer 			 *
1181c3dbadc1SChuck Silvers 			 * Currently we do not set the entire page valid,
1182c3dbadc1SChuck Silvers 			 * we just try to clear the piece that we couldn't
1183c3dbadc1SChuck Silvers 			 * read.
11848d17e694SJulian Elischer 			 */
1185dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
118654746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
118742eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
1188c3dbadc1SChuck Silvers 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
1189c3dbadc1SChuck Silvers 			    ("%s: page %p is dirty", __func__, mt));
11908f9110f6SJohn Dyson 		}
11918f9110f6SJohn Dyson 
1192b0cd2017SGleb Smirnoff 		if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1193b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
119403679e23SAlan Cox 	}
11957f935055SJeff Roberson 	VM_OBJECT_RUNLOCK(object);
119690effb23SGleb Smirnoff 
119790effb23SGleb Smirnoff 	return (error);
119826f9a767SRodney W. Grimes }
119926f9a767SRodney W. Grimes 
1200ce75f2c3SMike Smith /*
1201ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1202ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1203ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1204ce75f2c3SMike Smith  *
1205ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1206ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1207ce75f2c3SMike Smith  */
1208e4542174SMatthew Dillon static void
12097ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
121033cad9e9SKonstantin Belousov     int flags, int *rtvals)
1211170db9c6SJohn Dyson {
1212170db9c6SJohn Dyson 	int rtval;
1213170db9c6SJohn Dyson 	struct vnode *vp;
121486ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1215ad980522SJohn Dyson 
12160e3cdf2cSAlan Cox 	/*
12170e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
12180e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
12190e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
12200e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
12210e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
12220e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
12230e3cdf2cSAlan Cox 	 *
12240e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
12250e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
12260e3cdf2cSAlan Cox 	 */
12270e3cdf2cSAlan Cox 
1228e2068d0bSJeff Roberson 	if (vm_page_count_min())
122933cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
12300e3cdf2cSAlan Cox 
12310e3cdf2cSAlan Cox 	/*
12320e3cdf2cSAlan Cox 	 * Call device-specific putpages function
12330e3cdf2cSAlan Cox 	 */
1234170db9c6SJohn Dyson 	vp = object->handle;
123589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
123633cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
123723955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
123823955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
123989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1240170db9c6SJohn Dyson }
1241170db9c6SJohn Dyson 
124205877a85SKonstantin Belousov static int
124305877a85SKonstantin Belousov vn_off2bidx(vm_ooffset_t offset)
124405877a85SKonstantin Belousov {
124505877a85SKonstantin Belousov 
124605877a85SKonstantin Belousov 	return ((offset & PAGE_MASK) / DEV_BSIZE);
124705877a85SKonstantin Belousov }
124805877a85SKonstantin Belousov 
124905877a85SKonstantin Belousov static bool
125005877a85SKonstantin Belousov vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
125105877a85SKonstantin Belousov {
125205877a85SKonstantin Belousov 
125305877a85SKonstantin Belousov 	KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
125405877a85SKonstantin Belousov 	    offset < IDX_TO_OFF(m->pindex + 1),
125505877a85SKonstantin Belousov 	    ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
125605877a85SKonstantin Belousov 	    (uintmax_t)offset));
125705877a85SKonstantin Belousov 	return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
125805877a85SKonstantin Belousov }
1259ce75f2c3SMike Smith 
126026f9a767SRodney W. Grimes /*
1261ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
12624491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
12632b6b0df7SMatthew Dillon  *
12642b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
1265763df3ecSPedro F. Giffuni  * clustering has already typically occurred, so in general we ask the
12662b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
12672b6b0df7SMatthew Dillon  * then delayed.
126826f9a767SRodney W. Grimes  */
1269ce75f2c3SMike Smith int
1270c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1271c46b90e9SAlan Cox     int flags, int *rtvals)
127226f9a767SRodney W. Grimes {
1273ce75f2c3SMike Smith 	vm_object_t object;
1274c46b90e9SAlan Cox 	vm_page_t m;
127505877a85SKonstantin Belousov 	vm_ooffset_t maxblksz, next_offset, poffset, prev_offset;
1276f6b04d2bSDavid Greenman 	struct uio auio;
1277f6b04d2bSDavid Greenman 	struct iovec aiov;
127805877a85SKonstantin Belousov 	off_t prev_resid, wrsz;
1279e6c44f65SKonstantin Belousov 	int count, error, i, maxsize, ncount, pgoff, ppscheck;
128005877a85SKonstantin Belousov 	bool in_hole;
1281dd498befSPaul Saab 	static struct timeval lastfail;
1282dd498befSPaul Saab 	static int curfail;
128326f9a767SRodney W. Grimes 
1284ce75f2c3SMike Smith 	object = vp->v_object;
1285ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1286ce75f2c3SMike Smith 
128726f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1288031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
128926f9a767SRodney W. Grimes 
1290c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
1291e6c44f65SKonstantin Belousov 		printf("vnode_pager_generic_putpages: "
1292e6c44f65SKonstantin Belousov 		    "attempt to write meta-data 0x%jx(%lx)\n",
1293e6c44f65SKonstantin Belousov 		    (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1294f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1295e6c44f65SKonstantin Belousov 		return (VM_PAGER_BAD);
12960d94caffSDavid Greenman 	}
12970bdb7528SDavid Greenman 
1298f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1299f6b04d2bSDavid Greenman 	ncount = count;
130026f9a767SRodney W. Grimes 
1301c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
130200a6f47fSMatthew Dillon 
130300a6f47fSMatthew Dillon 	/*
130400a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
1305763df3ecSPedro F. Giffuni 	 * have to invalidate pages occurring beyond the file EOF.  However,
130600a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
130700a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
130800a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
130900a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1310*efec381dSMark Johnston 	 * With the page busied we are free to fix up the dirty bits here.
13113ebeaf59SMatthew Dillon 	 *
13123ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
13133ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
131400a6f47fSMatthew Dillon 	 */
1315b3d4ab66SKonstantin Belousov 	VM_OBJECT_RLOCK(object);
1316a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
131700a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
1318a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1319aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
132000a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1321938cdc42SKonstantin Belousov 				pgoff = roundup2(pgoff, DEV_BSIZE);
1322938cdc42SKonstantin Belousov 
1323c46b90e9SAlan Cox 				/*
13247f935055SJeff Roberson 				 * If the page is busy and the following
1325c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1326c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1327c46b90e9SAlan Cox 				 * pmap operation.
1328c46b90e9SAlan Cox 				 */
1329c46b90e9SAlan Cox 				m = ma[ncount - 1];
1330c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
13316031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1332c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1333e6c44f65SKonstantin Belousov 				MPASS(m->dirty != 0);
1334c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1335c46b90e9SAlan Cox 				    pgoff);
133600a6f47fSMatthew Dillon 			}
133700a6f47fSMatthew Dillon 		} else {
133800a6f47fSMatthew Dillon 			maxsize = 0;
133900a6f47fSMatthew Dillon 			ncount = 0;
134000a6f47fSMatthew Dillon 		}
1341e6c44f65SKonstantin Belousov 		for (i = ncount; i < count; i++)
1342f6b04d2bSDavid Greenman 			rtvals[i] = VM_PAGER_BAD;
1343f6b04d2bSDavid Greenman 	}
13447f935055SJeff Roberson 	VM_OBJECT_RUNLOCK(object);
134526f9a767SRodney W. Grimes 
1346f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1347f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1348f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1349e6c44f65SKonstantin Belousov 	auio.uio_td = NULL;
135005877a85SKonstantin Belousov 	maxblksz = roundup2(poffset + maxsize, DEV_BSIZE);
135105877a85SKonstantin Belousov 
135205877a85SKonstantin Belousov 	for (prev_offset = poffset; prev_offset < maxblksz;) {
135305877a85SKonstantin Belousov 		/* Skip clean blocks. */
135405877a85SKonstantin Belousov 		for (in_hole = true; in_hole && prev_offset < maxblksz;) {
135505877a85SKonstantin Belousov 			m = ma[OFF_TO_IDX(prev_offset - poffset)];
135605877a85SKonstantin Belousov 			for (i = vn_off2bidx(prev_offset);
135705877a85SKonstantin Belousov 			    i < sizeof(vm_page_bits_t) * NBBY &&
135805877a85SKonstantin Belousov 			    prev_offset < maxblksz; i++) {
135905877a85SKonstantin Belousov 				if (vn_dirty_blk(m, prev_offset)) {
136005877a85SKonstantin Belousov 					in_hole = false;
136105877a85SKonstantin Belousov 					break;
136205877a85SKonstantin Belousov 				}
136305877a85SKonstantin Belousov 				prev_offset += DEV_BSIZE;
136405877a85SKonstantin Belousov 			}
136505877a85SKonstantin Belousov 		}
136605877a85SKonstantin Belousov 		if (in_hole)
136705877a85SKonstantin Belousov 			goto write_done;
136805877a85SKonstantin Belousov 
136905877a85SKonstantin Belousov 		/* Find longest run of dirty blocks. */
137005877a85SKonstantin Belousov 		for (next_offset = prev_offset; next_offset < maxblksz;) {
137105877a85SKonstantin Belousov 			m = ma[OFF_TO_IDX(next_offset - poffset)];
137205877a85SKonstantin Belousov 			for (i = vn_off2bidx(next_offset);
137305877a85SKonstantin Belousov 			    i < sizeof(vm_page_bits_t) * NBBY &&
137405877a85SKonstantin Belousov 			    next_offset < maxblksz; i++) {
137505877a85SKonstantin Belousov 				if (!vn_dirty_blk(m, next_offset))
137605877a85SKonstantin Belousov 					goto start_write;
137705877a85SKonstantin Belousov 				next_offset += DEV_BSIZE;
137805877a85SKonstantin Belousov 			}
137905877a85SKonstantin Belousov 		}
138005877a85SKonstantin Belousov start_write:
138105877a85SKonstantin Belousov 		if (next_offset > poffset + maxsize)
138205877a85SKonstantin Belousov 			next_offset = poffset + maxsize;
138305877a85SKonstantin Belousov 
138405877a85SKonstantin Belousov 		/*
138505877a85SKonstantin Belousov 		 * Getting here requires finding a dirty block in the
138605877a85SKonstantin Belousov 		 * 'skip clean blocks' loop.
138705877a85SKonstantin Belousov 		 */
138805877a85SKonstantin Belousov 		MPASS(prev_offset < next_offset);
138905877a85SKonstantin Belousov 
139005877a85SKonstantin Belousov 		aiov.iov_base = NULL;
139105877a85SKonstantin Belousov 		auio.uio_iovcnt = 1;
139205877a85SKonstantin Belousov 		auio.uio_offset = prev_offset;
139305877a85SKonstantin Belousov 		prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
139405877a85SKonstantin Belousov 		    prev_offset;
139505877a85SKonstantin Belousov 		error = VOP_WRITE(vp, &auio,
139605877a85SKonstantin Belousov 		    vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
139705877a85SKonstantin Belousov 
139805877a85SKonstantin Belousov 		wrsz = prev_resid - auio.uio_resid;
139905877a85SKonstantin Belousov 		if (wrsz == 0) {
140005877a85SKonstantin Belousov 			if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
140105877a85SKonstantin Belousov 				vn_printf(vp, "vnode_pager_putpages: "
140205877a85SKonstantin Belousov 				    "zero-length write at %ju resid %zd\n",
140305877a85SKonstantin Belousov 				    auio.uio_offset, auio.uio_resid);
140405877a85SKonstantin Belousov 			}
140505877a85SKonstantin Belousov 			break;
140605877a85SKonstantin Belousov 		}
140705877a85SKonstantin Belousov 
140805877a85SKonstantin Belousov 		/* Adjust the starting offset for next iteration. */
140905877a85SKonstantin Belousov 		prev_offset += wrsz;
141005877a85SKonstantin Belousov 		MPASS(auio.uio_offset == prev_offset);
1411f6b04d2bSDavid Greenman 
14123dbb0ca6SKonstantin Belousov 		ppscheck = 0;
141305877a85SKonstantin Belousov 		if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
141405877a85SKonstantin Belousov 		    &curfail, 1)) != 0)
141505877a85SKonstantin Belousov 			vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
141605877a85SKonstantin Belousov 			    error);
1417e6c44f65SKonstantin Belousov 		if (auio.uio_resid != 0 && (ppscheck != 0 ||
1418e6c44f65SKonstantin Belousov 		    ppsratecheck(&lastfail, &curfail, 1) != 0))
141905877a85SKonstantin Belousov 			vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
142005877a85SKonstantin Belousov 			    "at %ju\n", auio.uio_resid,
142105877a85SKonstantin Belousov 			    (uintmax_t)ma[0]->pindex);
142205877a85SKonstantin Belousov 		if (error != 0 || auio.uio_resid != 0)
142305877a85SKonstantin Belousov 			break;
142405877a85SKonstantin Belousov 	}
142505877a85SKonstantin Belousov write_done:
142605877a85SKonstantin Belousov 	/* Mark completely processed pages. */
142705877a85SKonstantin Belousov 	for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
142826f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
142905877a85SKonstantin Belousov 	/* Mark partial EOF page. */
143005877a85SKonstantin Belousov 	if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
143105877a85SKonstantin Belousov 		rtvals[i++] = VM_PAGER_OK;
143205877a85SKonstantin Belousov 	/* Unwritten pages in range, free bonus if the page is clean. */
143305877a85SKonstantin Belousov 	for (; i < ncount; i++)
143405877a85SKonstantin Belousov 		rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
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;
1484031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1485031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1486031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1487031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1488031ec8c1SKonstantin Belousov 		} else {
1489031ec8c1SKonstantin Belousov 			/* Partially written page. */
1490031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1491031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1492031ec8c1SKonstantin Belousov 		}
1493031ec8c1SKonstantin Belousov 	}
1494555b7bb4SKonstantin Belousov 	if (eof >= lpos) /* avoid truncation */
14957f935055SJeff Roberson 		return;
1496555b7bb4SKonstantin Belousov 	for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1497555b7bb4SKonstantin Belousov 		if (pos != trunc_page(pos)) {
1498555b7bb4SKonstantin Belousov 			/*
1499555b7bb4SKonstantin Belousov 			 * The page contains the last valid byte in
1500555b7bb4SKonstantin Belousov 			 * the vnode, mark the rest of the page as
1501555b7bb4SKonstantin Belousov 			 * clean, potentially making the whole page
1502555b7bb4SKonstantin Belousov 			 * clean.
1503555b7bb4SKonstantin Belousov 			 */
1504555b7bb4SKonstantin Belousov 			pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1505555b7bb4SKonstantin Belousov 			vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1506555b7bb4SKonstantin Belousov 			    pos_devb);
1507555b7bb4SKonstantin Belousov 
1508555b7bb4SKonstantin Belousov 			/*
1509555b7bb4SKonstantin Belousov 			 * If the page was cleaned, report the pageout
1510555b7bb4SKonstantin Belousov 			 * on it as successful.  msync() no longer
1511555b7bb4SKonstantin Belousov 			 * needs to write out the page, endlessly
1512555b7bb4SKonstantin Belousov 			 * creating write requests and dirty buffers.
1513555b7bb4SKonstantin Belousov 			 */
1514555b7bb4SKonstantin Belousov 			if (ma[i]->dirty == 0)
1515555b7bb4SKonstantin Belousov 				rtvals[i] = VM_PAGER_OK;
1516555b7bb4SKonstantin Belousov 
1517555b7bb4SKonstantin Belousov 			pos = round_page(pos);
1518555b7bb4SKonstantin Belousov 		} else {
1519555b7bb4SKonstantin Belousov 			/* vm_pageout_flush() clears dirty */
1520555b7bb4SKonstantin Belousov 			rtvals[i] = VM_PAGER_BAD;
1521555b7bb4SKonstantin Belousov 			pos += PAGE_SIZE;
1522555b7bb4SKonstantin Belousov 		}
1523555b7bb4SKonstantin Belousov 	}
1524031ec8c1SKonstantin Belousov }
152584110e7eSKonstantin Belousov 
1526fe7bcbafSKyle Evans static void
152784110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
152884110e7eSKonstantin Belousov     vm_offset_t end)
152984110e7eSKonstantin Belousov {
153084110e7eSKonstantin Belousov 	struct vnode *vp;
153184110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
153284110e7eSKonstantin Belousov 
153389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
153484110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
153589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
153684110e7eSKonstantin Belousov 		return;
153784110e7eSKonstantin Belousov 	}
153884110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
153984110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
154084110e7eSKonstantin Belousov 	vp = object->handle;
154184110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
154278022527SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "v_writecount inc");
154378022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, 1);
1544b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1545b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
154684110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
154778022527SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "v_writecount dec");
154878022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
1549b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1550b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
155184110e7eSKonstantin Belousov 	}
155289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
155384110e7eSKonstantin Belousov }
155484110e7eSKonstantin Belousov 
1555fe7bcbafSKyle Evans static void
155684110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
155784110e7eSKonstantin Belousov     vm_offset_t end)
155884110e7eSKonstantin Belousov {
155984110e7eSKonstantin Belousov 	struct vnode *vp;
156084110e7eSKonstantin Belousov 	struct mount *mp;
156184110e7eSKonstantin Belousov 	vm_offset_t inc;
156284110e7eSKonstantin Belousov 
156389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
156484110e7eSKonstantin Belousov 
156584110e7eSKonstantin Belousov 	/*
156684110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
156784110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
156884110e7eSKonstantin Belousov 	 */
156984110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
157089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
157184110e7eSKonstantin Belousov 		return;
157284110e7eSKonstantin Belousov 	}
157384110e7eSKonstantin Belousov 
157484110e7eSKonstantin Belousov 	/*
157584110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
157684110e7eSKonstantin Belousov 	 * zero.
157784110e7eSKonstantin Belousov 	 */
157884110e7eSKonstantin Belousov 	inc = end - start;
157984110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
158084110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
158189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
158284110e7eSKonstantin Belousov 		return;
158384110e7eSKonstantin Belousov 	}
158484110e7eSKonstantin Belousov 
158584110e7eSKonstantin Belousov 	vp = object->handle;
158684110e7eSKonstantin Belousov 	vhold(vp);
158789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
158884110e7eSKonstantin Belousov 	mp = NULL;
158984110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
159078022527SKonstantin Belousov 	vn_lock(vp, LK_SHARED | LK_RETRY);
159184110e7eSKonstantin Belousov 
159284110e7eSKonstantin Belousov 	/*
159384110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
159484110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
159584110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
159684110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
159784110e7eSKonstantin Belousov 	 */
159884110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
1599b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
160084110e7eSKonstantin Belousov 	vdrop(vp);
160184110e7eSKonstantin Belousov 	if (mp != NULL)
160284110e7eSKonstantin Belousov 		vn_finished_write(mp);
160384110e7eSKonstantin Belousov }
1604