xref: /freebsd/sys/vm/vnode_pager.c (revision 031ec8c10ae3ddd0ab79ec7802ecfb0a6918efd5)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
326f9a767SRodney W. Grimes  * Copyright (c) 1991 The Regents of the University of California.
426f9a767SRodney W. Grimes  * All rights reserved.
526f9a767SRodney W. Grimes  * Copyright (c) 1993, 1994 John S. Dyson
624a1cce3SDavid Greenman  * Copyright (c) 1995, David Greenman
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
215929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
4026f9a767SRodney W. Grimes  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes /*
44df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
45df8bae1dSRodney W. Grimes  */
46df8bae1dSRodney W. Grimes 
4726f9a767SRodney W. Grimes /*
4826f9a767SRodney W. Grimes  * TODO:
4924a1cce3SDavid Greenman  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
50f6b04d2bSDavid Greenman  *	greatly re-simplify the vnode_pager.
5126f9a767SRodney W. Grimes  */
5226f9a767SRodney W. Grimes 
53874651b1SDavid E. O'Brien #include <sys/cdefs.h>
54874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
55874651b1SDavid E. O'Brien 
56df8bae1dSRodney W. Grimes #include <sys/param.h>
57df8bae1dSRodney W. Grimes #include <sys/systm.h>
58df8bae1dSRodney W. Grimes #include <sys/proc.h>
59df8bae1dSRodney W. Grimes #include <sys/vnode.h>
60df8bae1dSRodney W. Grimes #include <sys/mount.h>
619626b608SPoul-Henning Kamp #include <sys/bio.h>
6224a1cce3SDavid Greenman #include <sys/buf.h>
63efeaf95aSDavid Greenman #include <sys/vmmeter.h>
64d07a6d3fSPoul-Henning Kamp #include <sys/limits.h>
6524579ca1SMatthew Dillon #include <sys/conf.h>
669e0ddbd0SAlan Cox #include <sys/sf_buf.h>
67df8bae1dSRodney W. Grimes 
684f12e0acSSuleiman Souhlal #include <machine/atomic.h>
694f12e0acSSuleiman Souhlal 
70df8bae1dSRodney W. Grimes #include <vm/vm.h>
71efeaf95aSDavid Greenman #include <vm/vm_object.h>
72df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7324a1cce3SDavid Greenman #include <vm/vm_pager.h>
741efb74fbSJohn Dyson #include <vm/vm_map.h>
75df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
76efeaf95aSDavid Greenman #include <vm/vm_extern.h>
77df8bae1dSRodney W. Grimes 
78bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
79bff76343SAlan Cox     daddr_t *rtaddress, int *run);
8011caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
8111caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
8211caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
8311caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
8411caded3SAlfred Perlstein static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
8511caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
863364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
873364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *cred);
880b8253a7SBruce Evans 
89df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
904e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
914e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
924e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
934e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
944e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
95df8bae1dSRodney W. Grimes };
96df8bae1dSRodney W. Grimes 
97b62b9b64SJohn Baldwin int vnode_pbuf_freecnt;
981c7c3c6aSMatthew Dillon 
99d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
100d07a6d3fSPoul-Henning Kamp int
101731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
102d07a6d3fSPoul-Henning Kamp {
103d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
104d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
105d07a6d3fSPoul-Henning Kamp 	struct vattr va;
106d07a6d3fSPoul-Henning Kamp 
107d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
108d07a6d3fSPoul-Henning Kamp 		return (0);
109d07a6d3fSPoul-Henning Kamp 
110d07a6d3fSPoul-Henning Kamp 	while ((object = vp->v_object) != NULL) {
111d07a6d3fSPoul-Henning Kamp 		VM_OBJECT_LOCK(object);
112d07a6d3fSPoul-Henning Kamp 		if (!(object->flags & OBJ_DEAD)) {
113d07a6d3fSPoul-Henning Kamp 			VM_OBJECT_UNLOCK(object);
114d07a6d3fSPoul-Henning Kamp 			return (0);
115d07a6d3fSPoul-Henning Kamp 		}
11622db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
117d07a6d3fSPoul-Henning Kamp 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
118d07a6d3fSPoul-Henning Kamp 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0);
119cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
120d07a6d3fSPoul-Henning Kamp 	}
121d07a6d3fSPoul-Henning Kamp 
122d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
123d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
124d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
125d07a6d3fSPoul-Henning Kamp 		} else {
1260359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
127d07a6d3fSPoul-Henning Kamp 				return (0);
128d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
129d07a6d3fSPoul-Henning Kamp 		}
130d07a6d3fSPoul-Henning Kamp 	}
131d07a6d3fSPoul-Henning Kamp 
1323364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
133d07a6d3fSPoul-Henning Kamp 	/*
134d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
135d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
136d07a6d3fSPoul-Henning Kamp 	 */
137d07a6d3fSPoul-Henning Kamp 	VM_OBJECT_LOCK(object);
138d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
139d07a6d3fSPoul-Henning Kamp 	VM_OBJECT_UNLOCK(object);
140d07a6d3fSPoul-Henning Kamp 	vrele(vp);
141d07a6d3fSPoul-Henning Kamp 
142d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
143d07a6d3fSPoul-Henning Kamp 
144d07a6d3fSPoul-Henning Kamp 	return (0);
145d07a6d3fSPoul-Henning Kamp }
146d07a6d3fSPoul-Henning Kamp 
1477146d6cbSPoul-Henning Kamp void
1487146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1497146d6cbSPoul-Henning Kamp {
1507146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1517146d6cbSPoul-Henning Kamp 
1527146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1537146d6cbSPoul-Henning Kamp 	if (obj == NULL)
1547146d6cbSPoul-Henning Kamp 		return;
15557fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
1567146d6cbSPoul-Henning Kamp 	VM_OBJECT_LOCK(obj);
1577146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1587146d6cbSPoul-Henning Kamp 		/*
1597146d6cbSPoul-Henning Kamp 		 * vclean() may be called twice. The first time
1607146d6cbSPoul-Henning Kamp 		 * removes the primary reference to the object,
1617146d6cbSPoul-Henning Kamp 		 * the second time goes one further and is a
1627146d6cbSPoul-Henning Kamp 		 * special-case to terminate the object.
1637146d6cbSPoul-Henning Kamp 		 *
1647146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1657146d6cbSPoul-Henning Kamp 		 */
1667146d6cbSPoul-Henning Kamp 		if ((obj->flags & OBJ_DEAD) == 0)
1677146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
1687146d6cbSPoul-Henning Kamp 		else
1697146d6cbSPoul-Henning Kamp 			VM_OBJECT_UNLOCK(obj);
1707146d6cbSPoul-Henning Kamp 	} else {
1717146d6cbSPoul-Henning Kamp 		/*
1727146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
1737146d6cbSPoul-Henning Kamp 		 */
1747146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
1757146d6cbSPoul-Henning Kamp 		VM_OBJECT_UNLOCK(obj);
1767146d6cbSPoul-Henning Kamp 	}
1776e4b2820SJeff Roberson 	vp->v_object = NULL;
1787146d6cbSPoul-Henning Kamp }
1797146d6cbSPoul-Henning Kamp 
1807146d6cbSPoul-Henning Kamp 
181df8bae1dSRodney W. Grimes /*
182df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
183df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
184990ab7adSAlan Cox  *
185990ab7adSAlan Cox  * MPSAFE
186df8bae1dSRodney W. Grimes  */
18724a1cce3SDavid Greenman vm_object_t
1886cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
1893364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
190df8bae1dSRodney W. Grimes {
19106cb7259SDavid Greenman 	vm_object_t object;
192df8bae1dSRodney W. Grimes 	struct vnode *vp;
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 	/*
195df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
196df8bae1dSRodney W. Grimes 	 */
197df8bae1dSRodney W. Grimes 	if (handle == NULL)
198df8bae1dSRodney W. Grimes 		return (NULL);
199df8bae1dSRodney W. Grimes 
200df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
20139d38f93SDavid Greenman 
20239d38f93SDavid Greenman 	/*
20339d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
20439d38f93SDavid Greenman 	 * go away.
20539d38f93SDavid Greenman 	 */
2062ac78f0eSStephan Uphoff retry:
2071ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
2081ca58953SAlan Cox 		VM_OBJECT_LOCK(object);
2091ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
2101ca58953SAlan Cox 			break;
21119187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
2121ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
21324a1cce3SDavid Greenman 	}
2140d94caffSDavid Greenman 
2152be70f79SJohn Dyson 	if (vp->v_usecount == 0)
2162be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
2172be70f79SJohn Dyson 
21824a1cce3SDavid Greenman 	if (object == NULL) {
219df8bae1dSRodney W. Grimes 		/*
2202ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
221df8bae1dSRodney W. Grimes 		 */
2226cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
223bbc0ec52SDavid Greenman 
2246cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
22526f9a767SRodney W. Grimes 
22624a1cce3SDavid Greenman 		object->handle = handle;
22711be8415SStephan Uphoff 		VI_LOCK(vp);
2282ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2292ac78f0eSStephan Uphoff 			/*
2302ac78f0eSStephan Uphoff 			 * Object has been created while we were sleeping
2312ac78f0eSStephan Uphoff 			 */
23211be8415SStephan Uphoff 			VI_UNLOCK(vp);
2332ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2342ac78f0eSStephan Uphoff 			goto retry;
235df8bae1dSRodney W. Grimes 		}
2362ac78f0eSStephan Uphoff 		vp->v_object = object;
23711be8415SStephan Uphoff 		VI_UNLOCK(vp);
23811be8415SStephan Uphoff 	} else {
2392ac78f0eSStephan Uphoff 		object->ref_count++;
2402ac78f0eSStephan Uphoff 		VM_OBJECT_UNLOCK(object);
24111be8415SStephan Uphoff 	}
2427747c038SJeff Roberson 	vref(vp);
24324a1cce3SDavid Greenman 	return (object);
244df8bae1dSRodney W. Grimes }
245df8bae1dSRodney W. Grimes 
246658ad5ffSAlan Cox /*
247658ad5ffSAlan Cox  *	The object must be locked.
248658ad5ffSAlan Cox  */
249f708ef1bSPoul-Henning Kamp static void
25024a1cce3SDavid Greenman vnode_pager_dealloc(object)
2510d94caffSDavid Greenman 	vm_object_t object;
25224a1cce3SDavid Greenman {
253b9f180d1SKonstantin Belousov 	struct vnode *vp;
254b9f180d1SKonstantin Belousov 	int refs;
255df8bae1dSRodney W. Grimes 
256b9f180d1SKonstantin Belousov 	vp = object->handle;
25724a1cce3SDavid Greenman 	if (vp == NULL)
25824a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
25924a1cce3SDavid Greenman 
260658ad5ffSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
26166095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
262b9f180d1SKonstantin Belousov 	refs = object->ref_count;
26324a1cce3SDavid Greenman 
26424a1cce3SDavid Greenman 	object->handle = NULL;
26595461b45SJohn Dyson 	object->type = OBJT_DEAD;
26619187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
26719187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
26819187819SAlan Cox 		wakeup(object);
26919187819SAlan Cox 	}
27057fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
271aa2cabb9SDavid Greenman 	vp->v_object = NULL;
27235764be3SPoul-Henning Kamp 	vp->v_vflag &= ~VV_TEXT;
273b9f180d1SKonstantin Belousov 	while (refs-- > 0)
274b9f180d1SKonstantin Belousov 		vunref(vp);
275df8bae1dSRodney W. Grimes }
27626f9a767SRodney W. Grimes 
277f708ef1bSPoul-Henning Kamp static boolean_t
278a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
27924a1cce3SDavid Greenman 	vm_object_t object;
280a316d390SJohn Dyson 	vm_pindex_t pindex;
28124a1cce3SDavid Greenman 	int *before;
28224a1cce3SDavid Greenman 	int *after;
283df8bae1dSRodney W. Grimes {
28424a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
28598b0c789SPoul-Henning Kamp 	daddr_t bn;
2863af76890SPoul-Henning Kamp 	int err;
287170db9c6SJohn Dyson 	daddr_t reqblock;
2882c4488fcSJohn Dyson 	int poff;
2892c4488fcSJohn Dyson 	int bsize;
290d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
291ae51ff11SJeff Roberson 	int vfslocked;
292df8bae1dSRodney W. Grimes 
293f29ba63eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
29424579ca1SMatthew Dillon 	/*
29524579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
29624579ca1SMatthew Dillon 	 * have the page.
29724579ca1SMatthew Dillon 	 */
298b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
29947221757SJohn Dyson 		return FALSE;
300df8bae1dSRodney W. Grimes 	/*
301b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3020d94caffSDavid Greenman 	 * not have the page.
303df8bae1dSRodney W. Grimes 	 */
304b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3054abc71c0SDavid Greenman 		return FALSE;
306df8bae1dSRodney W. Grimes 
307eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
308170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
309d63596ceSJohn Dyson 	blocksperpage = 0;
310d63596ceSJohn Dyson 	if (pagesperblock > 0) {
311a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
312d63596ceSJohn Dyson 	} else {
313d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
314d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
315d63596ceSJohn Dyson 	}
316f29ba63eSAlan Cox 	VM_OBJECT_UNLOCK(object);
317ae51ff11SJeff Roberson 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
318ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
319ae51ff11SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
320f29ba63eSAlan Cox 	VM_OBJECT_LOCK(object);
3210d94caffSDavid Greenman 	if (err)
32224a1cce3SDavid Greenman 		return TRUE;
3236eab77f2SJohn Dyson 	if (bn == -1)
324ced399eeSJohn Dyson 		return FALSE;
325d63596ceSJohn Dyson 	if (pagesperblock > 0) {
326a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
327170db9c6SJohn Dyson 		if (before) {
328170db9c6SJohn Dyson 			*before *= pagesperblock;
329170db9c6SJohn Dyson 			*before += poff;
330170db9c6SJohn Dyson 		}
331170db9c6SJohn Dyson 		if (after) {
332b1fc01b7SJohn Dyson 			int numafter;
333170db9c6SJohn Dyson 			*after *= pagesperblock;
334b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
33547e151ddSRobert Drehmel 			if (IDX_TO_OFF(pindex + numafter) >
33647e151ddSRobert Drehmel 			    object->un_pager.vnp.vnp_size) {
33747e151ddSRobert Drehmel 				numafter =
33847e151ddSRobert Drehmel 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
33947e151ddSRobert Drehmel 				    pindex;
340b1fc01b7SJohn Dyson 			}
341b1fc01b7SJohn Dyson 			*after += numafter;
342170db9c6SJohn Dyson 		}
343d63596ceSJohn Dyson 	} else {
344d63596ceSJohn Dyson 		if (before) {
345d63596ceSJohn Dyson 			*before /= blocksperpage;
346d63596ceSJohn Dyson 		}
347d63596ceSJohn Dyson 
348d63596ceSJohn Dyson 		if (after) {
349d63596ceSJohn Dyson 			*after /= blocksperpage;
350d63596ceSJohn Dyson 		}
351d63596ceSJohn Dyson 	}
352ced399eeSJohn Dyson 	return TRUE;
353df8bae1dSRodney W. Grimes }
354df8bae1dSRodney W. Grimes 
355df8bae1dSRodney W. Grimes /*
356df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
35724a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
358df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
359df8bae1dSRodney W. Grimes  *
360df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
361df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
362df8bae1dSRodney W. Grimes  */
363df8bae1dSRodney W. Grimes void
364df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
365df8bae1dSRodney W. Grimes 	struct vnode *vp;
366a316d390SJohn Dyson 	vm_ooffset_t nsize;
367df8bae1dSRodney W. Grimes {
3682a8f9ab5SAlan Cox 	vm_object_t object;
3692a8f9ab5SAlan Cox 	vm_page_t m;
370c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
371df8bae1dSRodney W. Grimes 
3722a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
373df8bae1dSRodney W. Grimes 		return;
374cb61d698SKonstantin Belousov /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
3752a8f9ab5SAlan Cox 	VM_OBJECT_LOCK(object);
3762a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
377df8bae1dSRodney W. Grimes 		/*
378df8bae1dSRodney W. Grimes 		 * Hasn't changed size
379df8bae1dSRodney W. Grimes 		 */
3802a8f9ab5SAlan Cox 		VM_OBJECT_UNLOCK(object);
381df8bae1dSRodney W. Grimes 		return;
3822a8f9ab5SAlan Cox 	}
383c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
3842a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
385df8bae1dSRodney W. Grimes 		/*
386bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
387df8bae1dSRodney W. Grimes 		 */
3882a8f9ab5SAlan Cox 		if (nobjsize < object->size)
389c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
390c576d121SLuoqi Chen 			    FALSE);
391bbc0ec52SDavid Greenman 		/*
392bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
3933ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
3943ebeaf59SMatthew Dillon 		 *
3953ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
3963ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
3973ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
398bbc0ec52SDavid Greenman 		 */
3992a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4001b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4011b26eb10SAlan Cox 		    m->valid != 0) {
4022b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4032b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4042b6b0df7SMatthew Dillon 
4052b6b0df7SMatthew Dillon 			/*
4062b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4072b6b0df7SMatthew Dillon 			 * the page has been mapped.
4082b6b0df7SMatthew Dillon 			 */
409fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4102b6b0df7SMatthew Dillon 
4112b6b0df7SMatthew Dillon 			/*
4123c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4133c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
4143c33df62SAlan Cox 			 * have already been set.
4153c33df62SAlan Cox 			 */
4163c33df62SAlan Cox 			vm_page_set_valid(m, base, size);
4173c33df62SAlan Cox 
4183c33df62SAlan Cox 			/*
4193c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
4203c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
4213c33df62SAlan Cox 			 * cleared.
4223c33df62SAlan Cox 			 */
4233c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
4243c33df62SAlan Cox 
4253c33df62SAlan Cox 			/*
4263c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
4273ebeaf59SMatthew Dillon 			 *
4283ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4293ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4303ebeaf59SMatthew Dillon 			 * replacement from working properly.
4312b6b0df7SMatthew Dillon 			 */
4323c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
4330ab3c7a5SAlan Cox 		} else if ((nsize & PAGE_MASK) &&
4340ab3c7a5SAlan Cox 		    __predict_false(object->cache != NULL)) {
4350ab3c7a5SAlan Cox 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
4360ab3c7a5SAlan Cox 			    nobjsize);
437bbc0ec52SDavid Greenman 		}
438bbc0ec52SDavid Greenman 	}
439a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
440c576d121SLuoqi Chen 	object->size = nobjsize;
4412a8f9ab5SAlan Cox 	VM_OBJECT_UNLOCK(object);
442df8bae1dSRodney W. Grimes }
443df8bae1dSRodney W. Grimes 
44426f9a767SRodney W. Grimes /*
44526f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
44626f9a767SRodney W. Grimes  * file address
44726f9a767SRodney W. Grimes  */
448bff76343SAlan Cox static int
449bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
450bff76343SAlan Cox     int *run)
45126f9a767SRodney W. Grimes {
45226f9a767SRodney W. Grimes 	int bsize;
45326f9a767SRodney W. Grimes 	int err;
454a316d390SJohn Dyson 	daddr_t vblock;
455f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
45626f9a767SRodney W. Grimes 
4572ad036b6SAlan Cox 	if (address < 0)
4580d94caffSDavid Greenman 		return -1;
4590d94caffSDavid Greenman 
460b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
4612c4488fcSJohn Dyson 		return -1;
4622c4488fcSJohn Dyson 
46326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
46426f9a767SRodney W. Grimes 	vblock = address / bsize;
46526f9a767SRodney W. Grimes 	voffset = address % bsize;
46626f9a767SRodney W. Grimes 
467bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
468bff76343SAlan Cox 	if (err == 0) {
469bff76343SAlan Cox 		if (*rtaddress != -1)
470bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
471efc68ce1SDavid Greenman 		if (run) {
472efc68ce1SDavid Greenman 			*run += 1;
473efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
474efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
475efc68ce1SDavid Greenman 		}
476efc68ce1SDavid Greenman 	}
47726f9a767SRodney W. Grimes 
478bff76343SAlan Cox 	return (err);
47926f9a767SRodney W. Grimes }
48026f9a767SRodney W. Grimes 
48126f9a767SRodney W. Grimes /*
48226f9a767SRodney W. Grimes  * small block filesystem vnode pager input
48326f9a767SRodney W. Grimes  */
484f708ef1bSPoul-Henning Kamp static int
48524a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
48624a1cce3SDavid Greenman 	vm_object_t object;
48726f9a767SRodney W. Grimes 	vm_page_t m;
48826f9a767SRodney W. Grimes {
4890d53a17bSAlan Cox 	int bits, i;
4909c83534dSPoul-Henning Kamp 	struct vnode *vp;
4919c83534dSPoul-Henning Kamp 	struct bufobj *bo;
49226f9a767SRodney W. Grimes 	struct buf *bp;
4939e0ddbd0SAlan Cox 	struct sf_buf *sf;
494f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
49526f9a767SRodney W. Grimes 	vm_offset_t bsize;
49626f9a767SRodney W. Grimes 	int error = 0;
49726f9a767SRodney W. Grimes 
49824a1cce3SDavid Greenman 	vp = object->handle;
499b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5002c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5012c4488fcSJohn Dyson 
50226f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5030bdb7528SDavid Greenman 
5049c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
50526f9a767SRodney W. Grimes 
5069e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
50726f9a767SRodney W. Grimes 
50826f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
50933c67741SMatthew Dillon 		vm_ooffset_t address;
510bbc0ec52SDavid Greenman 
5110d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5120d53a17bSAlan Cox 		if (m->valid & bits)
51326f9a767SRodney W. Grimes 			continue;
51426f9a767SRodney W. Grimes 
51533c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
51633c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
51733c67741SMatthew Dillon 			fileaddr = -1;
51833c67741SMatthew Dillon 		} else {
519bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
520bff76343SAlan Cox 			if (error)
521bff76343SAlan Cox 				break;
52233c67741SMatthew Dillon 		}
52326f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5241c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
52526f9a767SRodney W. Grimes 
52626f9a767SRodney W. Grimes 			/* build a minimal buffer header */
52721144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5286a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
529bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
530bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
531a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
532a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5339e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
534187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5359c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
53626f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
53726f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5382b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5395bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
54026f9a767SRodney W. Grimes 
54126f9a767SRodney W. Grimes 			/* do the input */
5422c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
543b792bebeSPoul-Henning Kamp 			bstrategy(bp);
54426f9a767SRodney W. Grimes 
5456a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5466a4b5823SPoul-Henning Kamp 
547c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
54826f9a767SRodney W. Grimes 				error = EIO;
54926f9a767SRodney W. Grimes 
55026f9a767SRodney W. Grimes 			/*
55126f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
55226f9a767SRodney W. Grimes 			 */
5539c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5541c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
55526f9a767SRodney W. Grimes 			if (error)
55626f9a767SRodney W. Grimes 				break;
5570d53a17bSAlan Cox 		} else
5589e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
5590d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
5600d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
5610d53a17bSAlan Cox 		VM_OBJECT_LOCK(object);
5620d53a17bSAlan Cox 		m->valid |= bits;
5630d53a17bSAlan Cox 		VM_OBJECT_UNLOCK(object);
56426f9a767SRodney W. Grimes 	}
5659e0ddbd0SAlan Cox 	sf_buf_free(sf);
56626f9a767SRodney W. Grimes 	if (error) {
567a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
56826f9a767SRodney W. Grimes 	}
56926f9a767SRodney W. Grimes 	return VM_PAGER_OK;
57026f9a767SRodney W. Grimes }
57126f9a767SRodney W. Grimes 
57226f9a767SRodney W. Grimes /*
573475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
57426f9a767SRodney W. Grimes  */
575f708ef1bSPoul-Henning Kamp static int
57624a1cce3SDavid Greenman vnode_pager_input_old(object, m)
57724a1cce3SDavid Greenman 	vm_object_t object;
57826f9a767SRodney W. Grimes 	vm_page_t m;
57926f9a767SRodney W. Grimes {
580df8bae1dSRodney W. Grimes 	struct uio auio;
581df8bae1dSRodney W. Grimes 	struct iovec aiov;
58226f9a767SRodney W. Grimes 	int error;
58326f9a767SRodney W. Grimes 	int size;
5849e0ddbd0SAlan Cox 	struct sf_buf *sf;
585342a1480SJohn Baldwin 	struct vnode *vp;
586df8bae1dSRodney W. Grimes 
58752051abcSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
58826f9a767SRodney W. Grimes 	error = 0;
589bbc0ec52SDavid Greenman 
590df8bae1dSRodney W. Grimes 	/*
59126f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
59226f9a767SRodney W. Grimes 	 */
593a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
59426f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
59526f9a767SRodney W. Grimes 	} else {
59626f9a767SRodney W. Grimes 		size = PAGE_SIZE;
597a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
598a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
59952051abcSAlan Cox 		vp = object->handle;
60052051abcSAlan Cox 		VM_OBJECT_UNLOCK(object);
6010bdb7528SDavid Greenman 
60226f9a767SRodney W. Grimes 		/*
603df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
604df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
605df8bae1dSRodney W. Grimes 		 */
6069e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6070bdb7528SDavid Greenman 
6089e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
609df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
610df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
611df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
612a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
613df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
61426f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
615df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
616b40ce416SJulian Elischer 		auio.uio_td = curthread;
61726f9a767SRodney W. Grimes 
618a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
619df8bae1dSRodney W. Grimes 		if (!error) {
62054d92145SMatthew Dillon 			int count = size - auio.uio_resid;
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes 			if (count == 0)
623df8bae1dSRodney W. Grimes 				error = EINVAL;
62426f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6259e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6269e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
627df8bae1dSRodney W. Grimes 		}
6289e0ddbd0SAlan Cox 		sf_buf_free(sf);
6291b26eb10SAlan Cox 
6301b26eb10SAlan Cox 		VM_OBJECT_LOCK(object);
631df8bae1dSRodney W. Grimes 	}
6320d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
6336e3a3f38SRobert V. Baron 	if (!error)
6346e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
635a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
63626f9a767SRodney W. Grimes }
63726f9a767SRodney W. Grimes 
63826f9a767SRodney W. Grimes /*
63926f9a767SRodney W. Grimes  * generic vnode pager input routine
64026f9a767SRodney W. Grimes  */
641170db9c6SJohn Dyson 
642ce75f2c3SMike Smith /*
64323955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
64447e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
64547e151ddSRobert Drehmel  * to implement the previous behaviour.
646ce75f2c3SMike Smith  *
647ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
648ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
649ce75f2c3SMike Smith  */
650f708ef1bSPoul-Henning Kamp static int
65124a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
65226f9a767SRodney W. Grimes 	vm_object_t object;
65324a1cce3SDavid Greenman 	vm_page_t *m;
65424a1cce3SDavid Greenman 	int count;
65524a1cce3SDavid Greenman 	int reqpage;
65624a1cce3SDavid Greenman {
657170db9c6SJohn Dyson 	int rtval;
658170db9c6SJohn Dyson 	struct vnode *vp;
65986ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
660ae51ff11SJeff Roberson 	int vfslocked;
66195e5e988SJohn Dyson 
662170db9c6SJohn Dyson 	vp = object->handle;
6638630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
664ae51ff11SJeff Roberson 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
66586ffbd76SMike Smith 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
66623955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
66723955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
668ae51ff11SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
6698630c117SAlan Cox 	VM_OBJECT_LOCK(object);
670170db9c6SJohn Dyson 	return rtval;
671170db9c6SJohn Dyson }
672170db9c6SJohn Dyson 
673ce75f2c3SMike Smith /*
674ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
675ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
676ce75f2c3SMike Smith  */
677ce75f2c3SMike Smith int
678ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
679ce75f2c3SMike Smith 	struct vnode *vp;
680170db9c6SJohn Dyson 	vm_page_t *m;
681ce75f2c3SMike Smith 	int bytecount;
682170db9c6SJohn Dyson 	int reqpage;
683170db9c6SJohn Dyson {
684ce75f2c3SMike Smith 	vm_object_t object;
685a316d390SJohn Dyson 	vm_offset_t kva;
6868f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
687f3aad9a6SBjoern A. Zeeb 	int i, j, size, bsize, first;
688f4f83da0SAlan Cox 	daddr_t firstaddr, reqblock;
6899c83534dSPoul-Henning Kamp 	struct bufobj *bo;
690efc68ce1SDavid Greenman 	int runpg;
691efc68ce1SDavid Greenman 	int runend;
6920bdb7528SDavid Greenman 	struct buf *bp;
693ce75f2c3SMike Smith 	int count;
6941de11f1aSAlan Cox 	int error;
69526f9a767SRodney W. Grimes 
696ce75f2c3SMike Smith 	object = vp->v_object;
697ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
698ce75f2c3SMike Smith 
6999c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
7009c83534dSPoul-Henning Kamp 	    ("vnode_pager_generic_getpages does not support devices"));
701b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
7022c4488fcSJohn Dyson 		return VM_PAGER_BAD;
7032c4488fcSJohn Dyson 
70426f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
70526f9a767SRodney W. Grimes 
70626f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
707bbc0ec52SDavid Greenman 
70826f9a767SRodney W. Grimes 	/*
709bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
710bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
71126f9a767SRodney W. Grimes 	 */
712a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
713bbc0ec52SDavid Greenman 
71426f9a767SRodney W. Grimes 	/*
71516f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
71626f9a767SRodney W. Grimes 	 */
7171de11f1aSAlan Cox 	error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
7181de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
71931953be9SAlan Cox 		VM_OBJECT_LOCK(object);
7202965a453SKip Macy 
721e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7222965a453SKip Macy 			if (i != reqpage) {
7232965a453SKip Macy 				vm_page_lock(m[i]);
724d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7252965a453SKip Macy 				vm_page_unlock(m[i]);
7262965a453SKip Macy 			}
727b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodein);
728b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodepgsin);
72952051abcSAlan Cox 		error = vnode_pager_input_old(object, m[reqpage]);
73052051abcSAlan Cox 		VM_OBJECT_UNLOCK(object);
73152051abcSAlan Cox 		return (error);
7321de11f1aSAlan Cox 	} else if (error != 0) {
7331de11f1aSAlan Cox 		VM_OBJECT_LOCK(object);
7341de11f1aSAlan Cox 		for (i = 0; i < count; i++)
7352965a453SKip Macy 			if (i != reqpage) {
7362965a453SKip Macy 				vm_page_lock(m[i]);
7371de11f1aSAlan Cox 				vm_page_free(m[i]);
7382965a453SKip Macy 				vm_page_unlock(m[i]);
7392965a453SKip Macy 			}
7401de11f1aSAlan Cox 		VM_OBJECT_UNLOCK(object);
7411de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
742bbc0ec52SDavid Greenman 
74326f9a767SRodney W. Grimes 		/*
74426f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
74526f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
74626f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
74726f9a767SRodney W. Grimes 		 */
74826f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
749500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
75031953be9SAlan Cox 		VM_OBJECT_LOCK(object);
751e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7522965a453SKip Macy 			if (i != reqpage) {
7532965a453SKip Macy 				vm_page_lock(m[i]);
754d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7552965a453SKip Macy 				vm_page_unlock(m[i]);
7562965a453SKip Macy 			}
75731953be9SAlan Cox 		VM_OBJECT_UNLOCK(object);
758b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodein);
759b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodepgsin);
76024a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
76126f9a767SRodney W. Grimes 	}
7628d17e694SJulian Elischer 
76326f9a767SRodney W. Grimes 	/*
7648d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
7658d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
7668d17e694SJulian Elischer 	 * media.
7670d94caffSDavid Greenman 	 */
76831953be9SAlan Cox 	VM_OBJECT_LOCK(object);
7698b575f6cSAlan Cox 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
770e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7712965a453SKip Macy 			if (i != reqpage) {
7722965a453SKip Macy 				vm_page_lock(m[i]);
773d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7742965a453SKip Macy 				vm_page_unlock(m[i]);
7752965a453SKip Macy 			}
77631953be9SAlan Cox 		VM_OBJECT_UNLOCK(object);
7770d94caffSDavid Greenman 		return VM_PAGER_OK;
778f4f83da0SAlan Cox 	} else if (reqblock == -1) {
779f4f83da0SAlan Cox 		pmap_zero_page(m[reqpage]);
78012aa4fdcSAlan Cox 		KASSERT(m[reqpage]->dirty == 0,
78112aa4fdcSAlan Cox 		    ("vnode_pager_generic_getpages: page %p is dirty", m));
782f4f83da0SAlan Cox 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
783f4f83da0SAlan Cox 		for (i = 0; i < count; i++)
7842965a453SKip Macy 			if (i != reqpage) {
7852965a453SKip Macy 				vm_page_lock(m[i]);
786f4f83da0SAlan Cox 				vm_page_free(m[i]);
7872965a453SKip Macy 				vm_page_unlock(m[i]);
7882965a453SKip Macy 			}
789f4f83da0SAlan Cox 		VM_OBJECT_UNLOCK(object);
790f4f83da0SAlan Cox 		return (VM_PAGER_OK);
7910d94caffSDavid Greenman 	}
7928d17e694SJulian Elischer 	m[reqpage]->valid = 0;
7938b575f6cSAlan Cox 	VM_OBJECT_UNLOCK(object);
7940bdb7528SDavid Greenman 
7950d94caffSDavid Greenman 	/*
79626f9a767SRodney W. Grimes 	 * here on direct device I/O
79726f9a767SRodney W. Grimes 	 */
798efc68ce1SDavid Greenman 	firstaddr = -1;
799a1287949SEivind Eklund 
80026f9a767SRodney W. Grimes 	/*
801efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
80226f9a767SRodney W. Grimes 	 */
803efc68ce1SDavid Greenman 	for (first = 0, i = 0; i < count; i = runend) {
804bff76343SAlan Cox 		if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
805bff76343SAlan Cox 		    &runpg) != 0) {
806bff76343SAlan Cox 			VM_OBJECT_LOCK(object);
807bff76343SAlan Cox 			for (; i < count; i++)
8082965a453SKip Macy 				if (i != reqpage) {
8092965a453SKip Macy 					vm_page_lock(m[i]);
810bff76343SAlan Cox 					vm_page_free(m[i]);
8112965a453SKip Macy 					vm_page_unlock(m[i]);
8122965a453SKip Macy 				}
813bff76343SAlan Cox 			VM_OBJECT_UNLOCK(object);
814bff76343SAlan Cox 			return (VM_PAGER_ERROR);
815bff76343SAlan Cox 		}
816efc68ce1SDavid Greenman 		if (firstaddr == -1) {
81731953be9SAlan Cox 			VM_OBJECT_LOCK(object);
81824a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
819f3aad9a6SBjoern A. Zeeb 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
820f3aad9a6SBjoern A. Zeeb 				    (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
821bf1001faSMaxime Henrion 				    (uintmax_t)foff,
822bf1001faSMaxime Henrion 				    (uintmax_t)
823fc62ef1fSBruce Evans 				    (object->un_pager.vnp.vnp_size >> 32),
824bf1001faSMaxime Henrion 				    (uintmax_t)object->un_pager.vnp.vnp_size);
825efc68ce1SDavid Greenman 			}
8262965a453SKip Macy 			vm_page_lock(m[i]);
827d8d5fa88SAlfred Perlstein 			vm_page_free(m[i]);
8282965a453SKip Macy 			vm_page_unlock(m[i]);
82931953be9SAlan Cox 			VM_OBJECT_UNLOCK(object);
830efc68ce1SDavid Greenman 			runend = i + 1;
831efc68ce1SDavid Greenman 			first = runend;
832efc68ce1SDavid Greenman 			continue;
833efc68ce1SDavid Greenman 		}
834efc68ce1SDavid Greenman 		runend = i + runpg;
835efc68ce1SDavid Greenman 		if (runend <= reqpage) {
83631953be9SAlan Cox 			VM_OBJECT_LOCK(object);
8372965a453SKip Macy 			for (j = i; j < runend; j++) {
8382965a453SKip Macy 				vm_page_lock(m[j]);
839d8d5fa88SAlfred Perlstein 				vm_page_free(m[j]);
8402965a453SKip Macy 				vm_page_unlock(m[j]);
8412965a453SKip Macy 			}
84231953be9SAlan Cox 			VM_OBJECT_UNLOCK(object);
84326f9a767SRodney W. Grimes 		} else {
844efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
84531953be9SAlan Cox 				VM_OBJECT_LOCK(object);
8462965a453SKip Macy 				for (i = first + runpg; i < count; i++) {
8472965a453SKip Macy 					vm_page_lock(m[i]);
848d8d5fa88SAlfred Perlstein 					vm_page_free(m[i]);
8492965a453SKip Macy 					vm_page_unlock(m[i]);
8502965a453SKip Macy 				}
85131953be9SAlan Cox 				VM_OBJECT_UNLOCK(object);
852efc68ce1SDavid Greenman 				count = first + runpg;
85326f9a767SRodney W. Grimes 			}
854efc68ce1SDavid Greenman 			break;
85526f9a767SRodney W. Grimes 		}
856efc68ce1SDavid Greenman 		first = runend;
857efc68ce1SDavid Greenman 	}
85826f9a767SRodney W. Grimes 
85926f9a767SRodney W. Grimes 	/*
860bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
861bbc0ec52SDavid Greenman 	 * to be zero based...
86226f9a767SRodney W. Grimes 	 */
86326f9a767SRodney W. Grimes 	if (first != 0) {
8647e2393ffSAlan Cox 		m += first;
86526f9a767SRodney W. Grimes 		count -= first;
86626f9a767SRodney W. Grimes 		reqpage -= first;
86726f9a767SRodney W. Grimes 	}
868efc68ce1SDavid Greenman 
86926f9a767SRodney W. Grimes 	/*
87026f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
87126f9a767SRodney W. Grimes 	 */
872a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
87326f9a767SRodney W. Grimes 
87426f9a767SRodney W. Grimes 	/*
87526f9a767SRodney W. Grimes 	 * calculate the size of the transfer
87626f9a767SRodney W. Grimes 	 */
87726f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
8781a31a6c3SPoul-Henning Kamp 	KASSERT(count > 0, ("zero count"));
87924a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
88024a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
8811a31a6c3SPoul-Henning Kamp 	KASSERT(size > 0, ("zero size"));
88226f9a767SRodney W. Grimes 
88326f9a767SRodney W. Grimes 	/*
88424579ca1SMatthew Dillon 	 * round up physical size for real devices.
88526f9a767SRodney W. Grimes 	 */
8869c83534dSPoul-Henning Kamp 	if (1) {
8879c83534dSPoul-Henning Kamp 		int secmask = bo->bo_bsize - 1;
8886229cc50SPoul-Henning Kamp 		KASSERT(secmask < PAGE_SIZE && secmask > 0,
8896229cc50SPoul-Henning Kamp 		    ("vnode_pager_generic_getpages: sector size %d too large",
8906229cc50SPoul-Henning Kamp 		    secmask + 1));
89124579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
89224579ca1SMatthew Dillon 	}
89326f9a767SRodney W. Grimes 
8941c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
89516f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
89616f62314SDavid Greenman 
89726f9a767SRodney W. Grimes 	/*
89826f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
89926f9a767SRodney W. Grimes 	 */
90016f62314SDavid Greenman 	pmap_qenter(kva, m, count);
90126f9a767SRodney W. Grimes 
90226f9a767SRodney W. Grimes 	/* build a minimal buffer header */
90321144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
9046a4b5823SPoul-Henning Kamp 	bp->b_iodone = bdone;
905bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
906bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
907a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
908a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
909187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
9109c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
91126f9a767SRodney W. Grimes 	bp->b_bcount = size;
91226f9a767SRodney W. Grimes 	bp->b_bufsize = size;
9132b6b0df7SMatthew Dillon 	bp->b_runningbufspace = bp->b_bufsize;
9145bd65606SJohn Baldwin 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
91526f9a767SRodney W. Grimes 
916b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodein);
917b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsin, count);
918976e77fcSDavid Greenman 
91926f9a767SRodney W. Grimes 	/* do the input */
9202c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
921b792bebeSPoul-Henning Kamp 	bstrategy(bp);
922976e77fcSDavid Greenman 
9236a4b5823SPoul-Henning Kamp 	bwait(bp, PVM, "vnread");
92426f9a767SRodney W. Grimes 
925c244d2deSPoul-Henning Kamp 	if ((bp->b_ioflags & BIO_ERROR) != 0)
92626f9a767SRodney W. Grimes 		error = EIO;
92726f9a767SRodney W. Grimes 
92826f9a767SRodney W. Grimes 	if (!error) {
92926f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
93026f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
93126f9a767SRodney W. Grimes 	}
93216f62314SDavid Greenman 	pmap_qremove(kva, count);
93326f9a767SRodney W. Grimes 
93426f9a767SRodney W. Grimes 	/*
93526f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
93626f9a767SRodney W. Grimes 	 */
9379c83534dSPoul-Henning Kamp 	pbrelbo(bp);
9381c7c3c6aSMatthew Dillon 	relpbuf(bp, &vnode_pbuf_freecnt);
93926f9a767SRodney W. Grimes 
94031953be9SAlan Cox 	VM_OBJECT_LOCK(object);
9418f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
9428f9110f6SJohn Dyson 		vm_page_t mt;
9438f9110f6SJohn Dyson 
9448f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
9458f9110f6SJohn Dyson 		mt = m[i];
9468f9110f6SJohn Dyson 
94754746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
9488d17e694SJulian Elischer 			/*
9498d17e694SJulian Elischer 			 * Read filled up entire page.
9508d17e694SJulian Elischer 			 */
9518f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
952016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
953016a3c93SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is dirty",
954016a3c93SAlan Cox 			    mt));
955016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
956016a3c93SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is mapped",
957016a3c93SAlan Cox 			    mt));
9588f9110f6SJohn Dyson 		} else {
9598d17e694SJulian Elischer 			/*
96042eb4108SAlan Cox 			 * Read did not fill up entire page.
9618d17e694SJulian Elischer 			 *
9628d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
9638d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
9648d17e694SJulian Elischer 			 * read.
9658d17e694SJulian Elischer 			 */
96642eb4108SAlan Cox 			vm_page_set_valid(mt, 0,
96754746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
96842eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
96942eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
97042eb4108SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is dirty",
97142eb4108SAlan Cox 			    mt));
9728f9110f6SJohn Dyson 		}
9738f9110f6SJohn Dyson 
97426f9a767SRodney W. Grimes 		if (i != reqpage) {
975bbc0ec52SDavid Greenman 
97626f9a767SRodney W. Grimes 			/*
977bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
978bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
979bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
980956f3135SPhilippe Charnier 			 * It appears that empirical results show that
981bbc0ec52SDavid Greenman 			 * deactivating pages is best.
98226f9a767SRodney W. Grimes 			 */
983bbc0ec52SDavid Greenman 
98426f9a767SRodney W. Grimes 			/*
985bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
986bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
98726f9a767SRodney W. Grimes 			 */
98826f9a767SRodney W. Grimes 			if (!error) {
98903679e23SAlan Cox 				if (mt->oflags & VPO_WANTED) {
99003679e23SAlan Cox 					vm_page_lock(mt);
9918f9110f6SJohn Dyson 					vm_page_activate(mt);
99203679e23SAlan Cox 					vm_page_unlock(mt);
99303679e23SAlan Cox 				} else {
99403679e23SAlan Cox 					vm_page_lock(mt);
9958f9110f6SJohn Dyson 					vm_page_deactivate(mt);
99603679e23SAlan Cox 					vm_page_unlock(mt);
99703679e23SAlan Cox 				}
998e69763a3SDoug Rabson 				vm_page_wakeup(mt);
99926f9a767SRodney W. Grimes 			} else {
100003679e23SAlan Cox 				vm_page_lock(mt);
1001d8d5fa88SAlfred Perlstein 				vm_page_free(mt);
10022965a453SKip Macy 				vm_page_unlock(mt);
10032965a453SKip Macy 			}
100403679e23SAlan Cox 		}
100503679e23SAlan Cox 	}
100631953be9SAlan Cox 	VM_OBJECT_UNLOCK(object);
100726f9a767SRodney W. Grimes 	if (error) {
100824a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
100926f9a767SRodney W. Grimes 	}
1010a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
101126f9a767SRodney W. Grimes }
101226f9a767SRodney W. Grimes 
1013ce75f2c3SMike Smith /*
1014ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1015ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1016ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1017ce75f2c3SMike Smith  *
1018ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1019ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1020ce75f2c3SMike Smith  */
1021e4542174SMatthew Dillon static void
1022170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
1023170db9c6SJohn Dyson 	vm_object_t object;
1024170db9c6SJohn Dyson 	vm_page_t *m;
1025170db9c6SJohn Dyson 	int count;
1026170db9c6SJohn Dyson 	boolean_t sync;
1027170db9c6SJohn Dyson 	int *rtvals;
1028170db9c6SJohn Dyson {
1029170db9c6SJohn Dyson 	int rtval;
1030170db9c6SJohn Dyson 	struct vnode *vp;
103186ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1032ad980522SJohn Dyson 
10330e3cdf2cSAlan Cox 	/*
10340e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
10350e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
10360e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
10370e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
10380e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
10390e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
10400e3cdf2cSAlan Cox 	 *
10410e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
10420e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
10430e3cdf2cSAlan Cox 	 */
10440e3cdf2cSAlan Cox 
10452feb50bfSAttilio Rao 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
10460e3cdf2cSAlan Cox 		sync |= OBJPC_SYNC;
10470e3cdf2cSAlan Cox 
10480e3cdf2cSAlan Cox 	/*
10490e3cdf2cSAlan Cox 	 * Call device-specific putpages function
10500e3cdf2cSAlan Cox 	 */
1051170db9c6SJohn Dyson 	vp = object->handle;
10522e3b314dSAlan Cox 	VM_OBJECT_UNLOCK(object);
105386ffbd76SMike Smith 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
105423955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
105523955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
10562e3b314dSAlan Cox 	VM_OBJECT_LOCK(object);
1057170db9c6SJohn Dyson }
1058170db9c6SJohn Dyson 
1059ce75f2c3SMike Smith 
106026f9a767SRodney W. Grimes /*
1061ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
10624491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
10632b6b0df7SMatthew Dillon  *
10642b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
10652b6b0df7SMatthew Dillon  * clustering has already typically occured, so in general we ask the
10662b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
10672b6b0df7SMatthew Dillon  * then delayed.
106826f9a767SRodney W. Grimes  */
1069ce75f2c3SMike Smith int
1070c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1071c46b90e9SAlan Cox     int flags, int *rtvals)
107226f9a767SRodney W. Grimes {
1073f6b04d2bSDavid Greenman 	int i;
1074ce75f2c3SMike Smith 	vm_object_t object;
1075c46b90e9SAlan Cox 	vm_page_t m;
1076ce75f2c3SMike Smith 	int count;
107726f9a767SRodney W. Grimes 
1078f6b04d2bSDavid Greenman 	int maxsize, ncount;
1079a316d390SJohn Dyson 	vm_ooffset_t poffset;
1080f6b04d2bSDavid Greenman 	struct uio auio;
1081f6b04d2bSDavid Greenman 	struct iovec aiov;
1082f6b04d2bSDavid Greenman 	int error;
10838f9110f6SJohn Dyson 	int ioflags;
1084dd498befSPaul Saab 	int ppscheck = 0;
1085dd498befSPaul Saab 	static struct timeval lastfail;
1086dd498befSPaul Saab 	static int curfail;
108726f9a767SRodney W. Grimes 
1088ce75f2c3SMike Smith 	object = vp->v_object;
1089ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1090ce75f2c3SMike Smith 
109126f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1092*031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
109326f9a767SRodney W. Grimes 
1094c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
109523562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1096c46b90e9SAlan Cox 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1097f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1098f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
10990d94caffSDavid Greenman 	}
11000bdb7528SDavid Greenman 
1101f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1102f6b04d2bSDavid Greenman 	ncount = count;
110326f9a767SRodney W. Grimes 
1104c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
110500a6f47fSMatthew Dillon 
110600a6f47fSMatthew Dillon 	/*
110700a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
110800a6f47fSMatthew Dillon 	 * have to invalidate pages occuring beyond the file EOF.  However,
110900a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
111000a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
111100a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
111200a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
111300a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
11143ebeaf59SMatthew Dillon 	 *
11153ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
11163ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
111700a6f47fSMatthew Dillon 	 */
1118c46b90e9SAlan Cox 	VM_OBJECT_LOCK(object);
1119a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
112000a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
112100a6f47fSMatthew Dillon 			int pgoff;
112200a6f47fSMatthew Dillon 
1123a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1124aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
112500a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1126c46b90e9SAlan Cox 				/*
1127c46b90e9SAlan Cox 				 * If the object is locked and the following
1128c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1129c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1130c46b90e9SAlan Cox 				 * pmap operation.
1131c46b90e9SAlan Cox 				 */
1132c46b90e9SAlan Cox 				m = ma[ncount - 1];
1133c46b90e9SAlan Cox 				KASSERT(m->busy > 0,
1134c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not busy", m));
1135c46b90e9SAlan Cox 				KASSERT((m->flags & PG_WRITEABLE) == 0,
1136c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1137c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1138c46b90e9SAlan Cox 				    pgoff);
113900a6f47fSMatthew Dillon 			}
114000a6f47fSMatthew Dillon 		} else {
114100a6f47fSMatthew Dillon 			maxsize = 0;
114200a6f47fSMatthew Dillon 			ncount = 0;
114300a6f47fSMatthew Dillon 		}
1144f6b04d2bSDavid Greenman 		if (ncount < count) {
1145f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1146f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1147f6b04d2bSDavid Greenman 			}
1148f6b04d2bSDavid Greenman 		}
1149f6b04d2bSDavid Greenman 	}
1150c46b90e9SAlan Cox 	VM_OBJECT_UNLOCK(object);
115126f9a767SRodney W. Grimes 
11522b6b0df7SMatthew Dillon 	/*
11532b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
11542b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
115543b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
115643b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
115743b7990eSMatthew Dillon 	 * the system decides how to cluster.
11582b6b0df7SMatthew Dillon 	 */
11598f9110f6SJohn Dyson 	ioflags = IO_VMIO;
116043b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
116143b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
116243b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
116343b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
11648f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
116543b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1166f6b04d2bSDavid Greenman 
1167f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1168f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1169f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1170f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1171a316d390SJohn Dyson 	auio.uio_offset = poffset;
1172f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1173f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1174f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1175b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1176a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1177b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodeout);
1178b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1179f6b04d2bSDavid Greenman 
1180f6b04d2bSDavid Greenman 	if (error) {
1181dd498befSPaul Saab 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
118224a1cce3SDavid Greenman 			printf("vnode_pager_putpages: I/O error %d\n", error);
1183f6b04d2bSDavid Greenman 	}
1184f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1185dd498befSPaul Saab 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
11869f80ce04SKonstantin Belousov 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1187c46b90e9SAlan Cox 			    auio.uio_resid, (u_long)ma[0]->pindex);
118826f9a767SRodney W. Grimes 	}
1189ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
119026f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
119126f9a767SRodney W. Grimes 	}
1192f6b04d2bSDavid Greenman 	return rtvals[0];
119326f9a767SRodney W. Grimes }
1194*031ec8c1SKonstantin Belousov 
1195*031ec8c1SKonstantin Belousov void
1196*031ec8c1SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1197*031ec8c1SKonstantin Belousov {
1198*031ec8c1SKonstantin Belousov 	int i, pos;
1199*031ec8c1SKonstantin Belousov 
1200*031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1201*031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1202*031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1203*031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1204*031ec8c1SKonstantin Belousov 		} else {
1205*031ec8c1SKonstantin Belousov 			/* Partially written page. */
1206*031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1207*031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1208*031ec8c1SKonstantin Belousov 		}
1209*031ec8c1SKonstantin Belousov 	}
1210*031ec8c1SKonstantin Belousov }
1211