xref: /freebsd/sys/vm/vnode_pager.c (revision 1c771f9222a08a2a4a376209430038d9561f2470)
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>
71*1c771f92SKonstantin Belousov #include <vm/vm_param.h>
72efeaf95aSDavid Greenman #include <vm/vm_object.h>
73df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7424a1cce3SDavid Greenman #include <vm/vm_pager.h>
751efb74fbSJohn Dyson #include <vm/vm_map.h>
76df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
77efeaf95aSDavid Greenman #include <vm/vm_extern.h>
78df8bae1dSRodney W. Grimes 
79bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
80bff76343SAlan Cox     daddr_t *rtaddress, int *run);
8111caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
8211caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
8311caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
8411caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
8511caded3SAlfred Perlstein static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
8611caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
873364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
883364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *cred);
890b8253a7SBruce Evans 
90df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
914e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
924e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
934e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
944e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
954e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
96df8bae1dSRodney W. Grimes };
97df8bae1dSRodney W. Grimes 
98b62b9b64SJohn Baldwin int vnode_pbuf_freecnt;
991c7c3c6aSMatthew Dillon 
100d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
101d07a6d3fSPoul-Henning Kamp int
102731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
103d07a6d3fSPoul-Henning Kamp {
104d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
105d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
106d07a6d3fSPoul-Henning Kamp 	struct vattr va;
107d07a6d3fSPoul-Henning Kamp 
108d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
109d07a6d3fSPoul-Henning Kamp 		return (0);
110d07a6d3fSPoul-Henning Kamp 
111d07a6d3fSPoul-Henning Kamp 	while ((object = vp->v_object) != NULL) {
112d07a6d3fSPoul-Henning Kamp 		VM_OBJECT_LOCK(object);
113d07a6d3fSPoul-Henning Kamp 		if (!(object->flags & OBJ_DEAD)) {
114d07a6d3fSPoul-Henning Kamp 			VM_OBJECT_UNLOCK(object);
115d07a6d3fSPoul-Henning Kamp 			return (0);
116d07a6d3fSPoul-Henning Kamp 		}
11722db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
118d07a6d3fSPoul-Henning Kamp 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
119d07a6d3fSPoul-Henning Kamp 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0);
120cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
121d07a6d3fSPoul-Henning Kamp 	}
122d07a6d3fSPoul-Henning Kamp 
123d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
124d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
125d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
126d07a6d3fSPoul-Henning Kamp 		} else {
1270359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
128d07a6d3fSPoul-Henning Kamp 				return (0);
129d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
130d07a6d3fSPoul-Henning Kamp 		}
131d07a6d3fSPoul-Henning Kamp 	}
132d07a6d3fSPoul-Henning Kamp 
1333364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
134d07a6d3fSPoul-Henning Kamp 	/*
135d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
136d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
137d07a6d3fSPoul-Henning Kamp 	 */
138d07a6d3fSPoul-Henning Kamp 	VM_OBJECT_LOCK(object);
139d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
140d07a6d3fSPoul-Henning Kamp 	VM_OBJECT_UNLOCK(object);
141d07a6d3fSPoul-Henning Kamp 	vrele(vp);
142d07a6d3fSPoul-Henning Kamp 
143d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
144d07a6d3fSPoul-Henning Kamp 
145d07a6d3fSPoul-Henning Kamp 	return (0);
146d07a6d3fSPoul-Henning Kamp }
147d07a6d3fSPoul-Henning Kamp 
1487146d6cbSPoul-Henning Kamp void
1497146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1507146d6cbSPoul-Henning Kamp {
1517146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1527146d6cbSPoul-Henning Kamp 
1537146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1547146d6cbSPoul-Henning Kamp 	if (obj == NULL)
1557146d6cbSPoul-Henning Kamp 		return;
15657fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
1577146d6cbSPoul-Henning Kamp 	VM_OBJECT_LOCK(obj);
1587146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1597146d6cbSPoul-Henning Kamp 		/*
1607146d6cbSPoul-Henning Kamp 		 * vclean() may be called twice. The first time
1617146d6cbSPoul-Henning Kamp 		 * removes the primary reference to the object,
1627146d6cbSPoul-Henning Kamp 		 * the second time goes one further and is a
1637146d6cbSPoul-Henning Kamp 		 * special-case to terminate the object.
1647146d6cbSPoul-Henning Kamp 		 *
1657146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1667146d6cbSPoul-Henning Kamp 		 */
1677146d6cbSPoul-Henning Kamp 		if ((obj->flags & OBJ_DEAD) == 0)
1687146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
1697146d6cbSPoul-Henning Kamp 		else
1707146d6cbSPoul-Henning Kamp 			VM_OBJECT_UNLOCK(obj);
1717146d6cbSPoul-Henning Kamp 	} else {
1727146d6cbSPoul-Henning Kamp 		/*
1737146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
1747146d6cbSPoul-Henning Kamp 		 */
1757146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
1767146d6cbSPoul-Henning Kamp 		VM_OBJECT_UNLOCK(obj);
1777146d6cbSPoul-Henning Kamp 	}
1786e4b2820SJeff Roberson 	vp->v_object = NULL;
1797146d6cbSPoul-Henning Kamp }
1807146d6cbSPoul-Henning Kamp 
1817146d6cbSPoul-Henning Kamp 
182df8bae1dSRodney W. Grimes /*
183df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
184df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
185990ab7adSAlan Cox  *
186990ab7adSAlan Cox  * MPSAFE
187df8bae1dSRodney W. Grimes  */
18824a1cce3SDavid Greenman vm_object_t
1896cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
1903364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
191df8bae1dSRodney W. Grimes {
19206cb7259SDavid Greenman 	vm_object_t object;
193df8bae1dSRodney W. Grimes 	struct vnode *vp;
194df8bae1dSRodney W. Grimes 
195df8bae1dSRodney W. Grimes 	/*
196df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
197df8bae1dSRodney W. Grimes 	 */
198df8bae1dSRodney W. Grimes 	if (handle == NULL)
199df8bae1dSRodney W. Grimes 		return (NULL);
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
20239d38f93SDavid Greenman 
20339d38f93SDavid Greenman 	/*
20439d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
20539d38f93SDavid Greenman 	 * go away.
20639d38f93SDavid Greenman 	 */
2072ac78f0eSStephan Uphoff retry:
2081ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
2091ca58953SAlan Cox 		VM_OBJECT_LOCK(object);
2101ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
2111ca58953SAlan Cox 			break;
21219187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
2131ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
21424a1cce3SDavid Greenman 	}
2150d94caffSDavid Greenman 
2162be70f79SJohn Dyson 	if (vp->v_usecount == 0)
2172be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
2182be70f79SJohn Dyson 
21924a1cce3SDavid Greenman 	if (object == NULL) {
220df8bae1dSRodney W. Grimes 		/*
2212ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
222df8bae1dSRodney W. Grimes 		 */
2236cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
224bbc0ec52SDavid Greenman 
2256cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
22684110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
22726f9a767SRodney W. Grimes 
22824a1cce3SDavid Greenman 		object->handle = handle;
22911be8415SStephan Uphoff 		VI_LOCK(vp);
2302ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2312ac78f0eSStephan Uphoff 			/*
2322ac78f0eSStephan Uphoff 			 * Object has been created while we were sleeping
2332ac78f0eSStephan Uphoff 			 */
23411be8415SStephan Uphoff 			VI_UNLOCK(vp);
2352ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2362ac78f0eSStephan Uphoff 			goto retry;
237df8bae1dSRodney W. Grimes 		}
2382ac78f0eSStephan Uphoff 		vp->v_object = object;
23911be8415SStephan Uphoff 		VI_UNLOCK(vp);
24011be8415SStephan Uphoff 	} else {
2412ac78f0eSStephan Uphoff 		object->ref_count++;
2422ac78f0eSStephan Uphoff 		VM_OBJECT_UNLOCK(object);
24311be8415SStephan Uphoff 	}
2447747c038SJeff Roberson 	vref(vp);
24524a1cce3SDavid Greenman 	return (object);
246df8bae1dSRodney W. Grimes }
247df8bae1dSRodney W. Grimes 
248658ad5ffSAlan Cox /*
249658ad5ffSAlan Cox  *	The object must be locked.
250658ad5ffSAlan Cox  */
251f708ef1bSPoul-Henning Kamp static void
25224a1cce3SDavid Greenman vnode_pager_dealloc(object)
2530d94caffSDavid Greenman 	vm_object_t object;
25424a1cce3SDavid Greenman {
255b9f180d1SKonstantin Belousov 	struct vnode *vp;
256b9f180d1SKonstantin Belousov 	int refs;
257df8bae1dSRodney W. Grimes 
258b9f180d1SKonstantin Belousov 	vp = object->handle;
25924a1cce3SDavid Greenman 	if (vp == NULL)
26024a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
26124a1cce3SDavid Greenman 
262658ad5ffSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
26366095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
264b9f180d1SKonstantin Belousov 	refs = object->ref_count;
26524a1cce3SDavid Greenman 
26624a1cce3SDavid Greenman 	object->handle = NULL;
26795461b45SJohn Dyson 	object->type = OBJT_DEAD;
26819187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
26919187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
27019187819SAlan Cox 		wakeup(object);
27119187819SAlan Cox 	}
27257fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
27384110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
27484110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
27584110e7eSKonstantin Belousov 		vp->v_writecount--;
276b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
277b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
27884110e7eSKonstantin Belousov 	}
279aa2cabb9SDavid Greenman 	vp->v_object = NULL;
28035764be3SPoul-Henning Kamp 	vp->v_vflag &= ~VV_TEXT;
28184110e7eSKonstantin Belousov 	VM_OBJECT_UNLOCK(object);
282b9f180d1SKonstantin Belousov 	while (refs-- > 0)
283b9f180d1SKonstantin Belousov 		vunref(vp);
28484110e7eSKonstantin Belousov 	VM_OBJECT_LOCK(object);
285df8bae1dSRodney W. Grimes }
28626f9a767SRodney W. Grimes 
287f708ef1bSPoul-Henning Kamp static boolean_t
288a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
28924a1cce3SDavid Greenman 	vm_object_t object;
290a316d390SJohn Dyson 	vm_pindex_t pindex;
29124a1cce3SDavid Greenman 	int *before;
29224a1cce3SDavid Greenman 	int *after;
293df8bae1dSRodney W. Grimes {
29424a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
29598b0c789SPoul-Henning Kamp 	daddr_t bn;
2963af76890SPoul-Henning Kamp 	int err;
297170db9c6SJohn Dyson 	daddr_t reqblock;
2982c4488fcSJohn Dyson 	int poff;
2992c4488fcSJohn Dyson 	int bsize;
300d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
301ae51ff11SJeff Roberson 	int vfslocked;
302df8bae1dSRodney W. Grimes 
303f29ba63eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
30424579ca1SMatthew Dillon 	/*
30524579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
30624579ca1SMatthew Dillon 	 * have the page.
30724579ca1SMatthew Dillon 	 */
308b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
30947221757SJohn Dyson 		return FALSE;
310df8bae1dSRodney W. Grimes 	/*
311b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3120d94caffSDavid Greenman 	 * not have the page.
313df8bae1dSRodney W. Grimes 	 */
314b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3154abc71c0SDavid Greenman 		return FALSE;
316df8bae1dSRodney W. Grimes 
317eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
318170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
319d63596ceSJohn Dyson 	blocksperpage = 0;
320d63596ceSJohn Dyson 	if (pagesperblock > 0) {
321a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
322d63596ceSJohn Dyson 	} else {
323d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
324d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
325d63596ceSJohn Dyson 	}
326f29ba63eSAlan Cox 	VM_OBJECT_UNLOCK(object);
327ae51ff11SJeff Roberson 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
328ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
329ae51ff11SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
330f29ba63eSAlan Cox 	VM_OBJECT_LOCK(object);
3310d94caffSDavid Greenman 	if (err)
33224a1cce3SDavid Greenman 		return TRUE;
3336eab77f2SJohn Dyson 	if (bn == -1)
334ced399eeSJohn Dyson 		return FALSE;
335d63596ceSJohn Dyson 	if (pagesperblock > 0) {
336a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
337170db9c6SJohn Dyson 		if (before) {
338170db9c6SJohn Dyson 			*before *= pagesperblock;
339170db9c6SJohn Dyson 			*before += poff;
340170db9c6SJohn Dyson 		}
341170db9c6SJohn Dyson 		if (after) {
342b1fc01b7SJohn Dyson 			int numafter;
343170db9c6SJohn Dyson 			*after *= pagesperblock;
344b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
34547e151ddSRobert Drehmel 			if (IDX_TO_OFF(pindex + numafter) >
34647e151ddSRobert Drehmel 			    object->un_pager.vnp.vnp_size) {
34747e151ddSRobert Drehmel 				numafter =
34847e151ddSRobert Drehmel 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
34947e151ddSRobert Drehmel 				    pindex;
350b1fc01b7SJohn Dyson 			}
351b1fc01b7SJohn Dyson 			*after += numafter;
352170db9c6SJohn Dyson 		}
353d63596ceSJohn Dyson 	} else {
354d63596ceSJohn Dyson 		if (before) {
355d63596ceSJohn Dyson 			*before /= blocksperpage;
356d63596ceSJohn Dyson 		}
357d63596ceSJohn Dyson 
358d63596ceSJohn Dyson 		if (after) {
359d63596ceSJohn Dyson 			*after /= blocksperpage;
360d63596ceSJohn Dyson 		}
361d63596ceSJohn Dyson 	}
362ced399eeSJohn Dyson 	return TRUE;
363df8bae1dSRodney W. Grimes }
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes /*
366df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
36724a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
368df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
369df8bae1dSRodney W. Grimes  *
370df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
371df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
372df8bae1dSRodney W. Grimes  */
373df8bae1dSRodney W. Grimes void
374df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
375df8bae1dSRodney W. Grimes 	struct vnode *vp;
376a316d390SJohn Dyson 	vm_ooffset_t nsize;
377df8bae1dSRodney W. Grimes {
3782a8f9ab5SAlan Cox 	vm_object_t object;
3792a8f9ab5SAlan Cox 	vm_page_t m;
380c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
381df8bae1dSRodney W. Grimes 
3822a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
383df8bae1dSRodney W. Grimes 		return;
384cb61d698SKonstantin Belousov /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
3852a8f9ab5SAlan Cox 	VM_OBJECT_LOCK(object);
3862a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
387df8bae1dSRodney W. Grimes 		/*
388df8bae1dSRodney W. Grimes 		 * Hasn't changed size
389df8bae1dSRodney W. Grimes 		 */
3902a8f9ab5SAlan Cox 		VM_OBJECT_UNLOCK(object);
391df8bae1dSRodney W. Grimes 		return;
3922a8f9ab5SAlan Cox 	}
393c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
3942a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
395df8bae1dSRodney W. Grimes 		/*
396bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
397df8bae1dSRodney W. Grimes 		 */
3982a8f9ab5SAlan Cox 		if (nobjsize < object->size)
399c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
4006bbee8e2SAlan Cox 			    0);
401bbc0ec52SDavid Greenman 		/*
402bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4033ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4043ebeaf59SMatthew Dillon 		 *
4053ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4063ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4073ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
408bbc0ec52SDavid Greenman 		 */
4092a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4101b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4111b26eb10SAlan Cox 		    m->valid != 0) {
4122b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4132b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4142b6b0df7SMatthew Dillon 
4152b6b0df7SMatthew Dillon 			/*
4162b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4172b6b0df7SMatthew Dillon 			 * the page has been mapped.
4182b6b0df7SMatthew Dillon 			 */
419fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4202b6b0df7SMatthew Dillon 
4212b6b0df7SMatthew Dillon 			/*
4223c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4233c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
4243c33df62SAlan Cox 			 * have already been set.
4253c33df62SAlan Cox 			 */
426dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
4273c33df62SAlan Cox 
4283c33df62SAlan Cox 			/*
4293c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
4303c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
4313c33df62SAlan Cox 			 * cleared.
4323c33df62SAlan Cox 			 */
4333c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
4343c33df62SAlan Cox 
4353c33df62SAlan Cox 			/*
4363c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
4373ebeaf59SMatthew Dillon 			 *
4383ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4393ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4403ebeaf59SMatthew Dillon 			 * replacement from working properly.
4412b6b0df7SMatthew Dillon 			 */
4423c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
4430ab3c7a5SAlan Cox 		} else if ((nsize & PAGE_MASK) &&
444db9ba578SAttilio Rao 		    vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
4450ab3c7a5SAlan Cox 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
4460ab3c7a5SAlan Cox 			    nobjsize);
447bbc0ec52SDavid Greenman 		}
448bbc0ec52SDavid Greenman 	}
449a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
450c576d121SLuoqi Chen 	object->size = nobjsize;
4512a8f9ab5SAlan Cox 	VM_OBJECT_UNLOCK(object);
452df8bae1dSRodney W. Grimes }
453df8bae1dSRodney W. Grimes 
45426f9a767SRodney W. Grimes /*
45526f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
45626f9a767SRodney W. Grimes  * file address
45726f9a767SRodney W. Grimes  */
458bff76343SAlan Cox static int
459bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
460bff76343SAlan Cox     int *run)
46126f9a767SRodney W. Grimes {
46226f9a767SRodney W. Grimes 	int bsize;
46326f9a767SRodney W. Grimes 	int err;
464a316d390SJohn Dyson 	daddr_t vblock;
465f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
46626f9a767SRodney W. Grimes 
4672ad036b6SAlan Cox 	if (address < 0)
4680d94caffSDavid Greenman 		return -1;
4690d94caffSDavid Greenman 
470b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
4712c4488fcSJohn Dyson 		return -1;
4722c4488fcSJohn Dyson 
47326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
47426f9a767SRodney W. Grimes 	vblock = address / bsize;
47526f9a767SRodney W. Grimes 	voffset = address % bsize;
47626f9a767SRodney W. Grimes 
477bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
478bff76343SAlan Cox 	if (err == 0) {
479bff76343SAlan Cox 		if (*rtaddress != -1)
480bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
481efc68ce1SDavid Greenman 		if (run) {
482efc68ce1SDavid Greenman 			*run += 1;
483efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
484efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
485efc68ce1SDavid Greenman 		}
486efc68ce1SDavid Greenman 	}
48726f9a767SRodney W. Grimes 
488bff76343SAlan Cox 	return (err);
48926f9a767SRodney W. Grimes }
49026f9a767SRodney W. Grimes 
49126f9a767SRodney W. Grimes /*
49226f9a767SRodney W. Grimes  * small block filesystem vnode pager input
49326f9a767SRodney W. Grimes  */
494f708ef1bSPoul-Henning Kamp static int
49524a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
49624a1cce3SDavid Greenman 	vm_object_t object;
49726f9a767SRodney W. Grimes 	vm_page_t m;
49826f9a767SRodney W. Grimes {
4999c83534dSPoul-Henning Kamp 	struct vnode *vp;
5009c83534dSPoul-Henning Kamp 	struct bufobj *bo;
50126f9a767SRodney W. Grimes 	struct buf *bp;
5029e0ddbd0SAlan Cox 	struct sf_buf *sf;
503f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
50426f9a767SRodney W. Grimes 	vm_offset_t bsize;
505561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
506561cc9fcSKonstantin Belousov 	int error, i;
50726f9a767SRodney W. Grimes 
508561cc9fcSKonstantin Belousov 	error = 0;
50924a1cce3SDavid Greenman 	vp = object->handle;
510b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5112c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5122c4488fcSJohn Dyson 
51326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5140bdb7528SDavid Greenman 
5159c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
51626f9a767SRodney W. Grimes 
5179e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
51826f9a767SRodney W. Grimes 
51926f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
52033c67741SMatthew Dillon 		vm_ooffset_t address;
521bbc0ec52SDavid Greenman 
5220d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5230d53a17bSAlan Cox 		if (m->valid & bits)
52426f9a767SRodney W. Grimes 			continue;
52526f9a767SRodney W. Grimes 
52633c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
52733c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
52833c67741SMatthew Dillon 			fileaddr = -1;
52933c67741SMatthew Dillon 		} else {
530bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
531bff76343SAlan Cox 			if (error)
532bff76343SAlan Cox 				break;
53333c67741SMatthew Dillon 		}
53426f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5351c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
53626f9a767SRodney W. Grimes 
53726f9a767SRodney W. Grimes 			/* build a minimal buffer header */
53821144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5396a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
540bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
541bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
542a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
543a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5449e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
545187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5469c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
5471faacf5dSKirk McKusick 			bp->b_vp = vp;
54826f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
54926f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5502b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5515bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
55226f9a767SRodney W. Grimes 
55326f9a767SRodney W. Grimes 			/* do the input */
5542c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
555b792bebeSPoul-Henning Kamp 			bstrategy(bp);
55626f9a767SRodney W. Grimes 
5576a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5586a4b5823SPoul-Henning Kamp 
559c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
56026f9a767SRodney W. Grimes 				error = EIO;
56126f9a767SRodney W. Grimes 
56226f9a767SRodney W. Grimes 			/*
56326f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
56426f9a767SRodney W. Grimes 			 */
5651faacf5dSKirk McKusick 			bp->b_vp = NULL;
5669c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5671c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
56826f9a767SRodney W. Grimes 			if (error)
56926f9a767SRodney W. Grimes 				break;
5700d53a17bSAlan Cox 		} else
5719e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
5720d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
5730d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
5740d53a17bSAlan Cox 		VM_OBJECT_LOCK(object);
5750d53a17bSAlan Cox 		m->valid |= bits;
5760d53a17bSAlan Cox 		VM_OBJECT_UNLOCK(object);
57726f9a767SRodney W. Grimes 	}
5789e0ddbd0SAlan Cox 	sf_buf_free(sf);
57926f9a767SRodney W. Grimes 	if (error) {
580a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
58126f9a767SRodney W. Grimes 	}
58226f9a767SRodney W. Grimes 	return VM_PAGER_OK;
58326f9a767SRodney W. Grimes }
58426f9a767SRodney W. Grimes 
58526f9a767SRodney W. Grimes /*
586475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
58726f9a767SRodney W. Grimes  */
588f708ef1bSPoul-Henning Kamp static int
58924a1cce3SDavid Greenman vnode_pager_input_old(object, m)
59024a1cce3SDavid Greenman 	vm_object_t object;
59126f9a767SRodney W. Grimes 	vm_page_t m;
59226f9a767SRodney W. Grimes {
593df8bae1dSRodney W. Grimes 	struct uio auio;
594df8bae1dSRodney W. Grimes 	struct iovec aiov;
59526f9a767SRodney W. Grimes 	int error;
59626f9a767SRodney W. Grimes 	int size;
5979e0ddbd0SAlan Cox 	struct sf_buf *sf;
598342a1480SJohn Baldwin 	struct vnode *vp;
599df8bae1dSRodney W. Grimes 
60052051abcSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
60126f9a767SRodney W. Grimes 	error = 0;
602bbc0ec52SDavid Greenman 
603df8bae1dSRodney W. Grimes 	/*
60426f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
60526f9a767SRodney W. Grimes 	 */
606a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
60726f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
60826f9a767SRodney W. Grimes 	} else {
60926f9a767SRodney W. Grimes 		size = PAGE_SIZE;
610a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
611a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
61252051abcSAlan Cox 		vp = object->handle;
61352051abcSAlan Cox 		VM_OBJECT_UNLOCK(object);
6140bdb7528SDavid Greenman 
61526f9a767SRodney W. Grimes 		/*
616df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
617df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
618df8bae1dSRodney W. Grimes 		 */
6199e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6200bdb7528SDavid Greenman 
6219e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
622df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
623df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
624df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
625a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
626df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
62726f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
628df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
629b40ce416SJulian Elischer 		auio.uio_td = curthread;
63026f9a767SRodney W. Grimes 
631a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
632df8bae1dSRodney W. Grimes 		if (!error) {
63354d92145SMatthew Dillon 			int count = size - auio.uio_resid;
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes 			if (count == 0)
636df8bae1dSRodney W. Grimes 				error = EINVAL;
63726f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6389e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6399e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
640df8bae1dSRodney W. Grimes 		}
6419e0ddbd0SAlan Cox 		sf_buf_free(sf);
6421b26eb10SAlan Cox 
6431b26eb10SAlan Cox 		VM_OBJECT_LOCK(object);
644df8bae1dSRodney W. Grimes 	}
6450d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
6466e3a3f38SRobert V. Baron 	if (!error)
6476e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
648a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
64926f9a767SRodney W. Grimes }
65026f9a767SRodney W. Grimes 
65126f9a767SRodney W. Grimes /*
65226f9a767SRodney W. Grimes  * generic vnode pager input routine
65326f9a767SRodney W. Grimes  */
654170db9c6SJohn Dyson 
655ce75f2c3SMike Smith /*
65623955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
65747e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
65847e151ddSRobert Drehmel  * to implement the previous behaviour.
659ce75f2c3SMike Smith  *
660ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
661ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
662ce75f2c3SMike Smith  */
663f708ef1bSPoul-Henning Kamp static int
66424a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
66526f9a767SRodney W. Grimes 	vm_object_t object;
66624a1cce3SDavid Greenman 	vm_page_t *m;
66724a1cce3SDavid Greenman 	int count;
66824a1cce3SDavid Greenman 	int reqpage;
66924a1cce3SDavid Greenman {
670170db9c6SJohn Dyson 	int rtval;
671170db9c6SJohn Dyson 	struct vnode *vp;
67286ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
673ae51ff11SJeff Roberson 	int vfslocked;
67495e5e988SJohn Dyson 
675170db9c6SJohn Dyson 	vp = object->handle;
6768630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
677ae51ff11SJeff Roberson 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
67886ffbd76SMike Smith 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
67923955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
68023955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
681ae51ff11SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
6828630c117SAlan Cox 	VM_OBJECT_LOCK(object);
683170db9c6SJohn Dyson 	return rtval;
684170db9c6SJohn Dyson }
685170db9c6SJohn Dyson 
686ce75f2c3SMike Smith /*
687ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
688ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
689ce75f2c3SMike Smith  */
690ce75f2c3SMike Smith int
691ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
692ce75f2c3SMike Smith 	struct vnode *vp;
693170db9c6SJohn Dyson 	vm_page_t *m;
694ce75f2c3SMike Smith 	int bytecount;
695170db9c6SJohn Dyson 	int reqpage;
696170db9c6SJohn Dyson {
697ce75f2c3SMike Smith 	vm_object_t object;
698a316d390SJohn Dyson 	vm_offset_t kva;
6998f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
700f3aad9a6SBjoern A. Zeeb 	int i, j, size, bsize, first;
701f4f83da0SAlan Cox 	daddr_t firstaddr, reqblock;
7029c83534dSPoul-Henning Kamp 	struct bufobj *bo;
703efc68ce1SDavid Greenman 	int runpg;
704efc68ce1SDavid Greenman 	int runend;
7050bdb7528SDavid Greenman 	struct buf *bp;
706ce75f2c3SMike Smith 	int count;
7071de11f1aSAlan Cox 	int error;
70826f9a767SRodney W. Grimes 
709ce75f2c3SMike Smith 	object = vp->v_object;
710ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
711ce75f2c3SMike Smith 
7129c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
7139c83534dSPoul-Henning Kamp 	    ("vnode_pager_generic_getpages does not support devices"));
714b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
7152c4488fcSJohn Dyson 		return VM_PAGER_BAD;
7162c4488fcSJohn Dyson 
71726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
71826f9a767SRodney W. Grimes 
71926f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
720bbc0ec52SDavid Greenman 
72126f9a767SRodney W. Grimes 	/*
722bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
723bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
72426f9a767SRodney W. Grimes 	 */
725a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
726bbc0ec52SDavid Greenman 
72726f9a767SRodney W. Grimes 	/*
72816f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
72926f9a767SRodney W. Grimes 	 */
7301de11f1aSAlan Cox 	error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
7311de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
73231953be9SAlan Cox 		VM_OBJECT_LOCK(object);
7332965a453SKip Macy 
734e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7352965a453SKip Macy 			if (i != reqpage) {
7362965a453SKip Macy 				vm_page_lock(m[i]);
737d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7382965a453SKip Macy 				vm_page_unlock(m[i]);
7392965a453SKip Macy 			}
740b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodein);
741b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodepgsin);
74252051abcSAlan Cox 		error = vnode_pager_input_old(object, m[reqpage]);
74352051abcSAlan Cox 		VM_OBJECT_UNLOCK(object);
74452051abcSAlan Cox 		return (error);
7451de11f1aSAlan Cox 	} else if (error != 0) {
7461de11f1aSAlan Cox 		VM_OBJECT_LOCK(object);
7471de11f1aSAlan Cox 		for (i = 0; i < count; i++)
7482965a453SKip Macy 			if (i != reqpage) {
7492965a453SKip Macy 				vm_page_lock(m[i]);
7501de11f1aSAlan Cox 				vm_page_free(m[i]);
7512965a453SKip Macy 				vm_page_unlock(m[i]);
7522965a453SKip Macy 			}
7531de11f1aSAlan Cox 		VM_OBJECT_UNLOCK(object);
7541de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
755bbc0ec52SDavid Greenman 
75626f9a767SRodney W. Grimes 		/*
75726f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
75826f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
75926f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
76026f9a767SRodney W. Grimes 		 */
76126f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
762500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
76331953be9SAlan Cox 		VM_OBJECT_LOCK(object);
764e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7652965a453SKip Macy 			if (i != reqpage) {
7662965a453SKip Macy 				vm_page_lock(m[i]);
767d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7682965a453SKip Macy 				vm_page_unlock(m[i]);
7692965a453SKip Macy 			}
77031953be9SAlan Cox 		VM_OBJECT_UNLOCK(object);
771b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodein);
772b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodepgsin);
77324a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
77426f9a767SRodney W. Grimes 	}
7758d17e694SJulian Elischer 
77626f9a767SRodney W. Grimes 	/*
7778d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
7788d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
7798d17e694SJulian Elischer 	 * media.
7800d94caffSDavid Greenman 	 */
78131953be9SAlan Cox 	VM_OBJECT_LOCK(object);
7828b575f6cSAlan Cox 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
783e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7842965a453SKip Macy 			if (i != reqpage) {
7852965a453SKip Macy 				vm_page_lock(m[i]);
786d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7872965a453SKip Macy 				vm_page_unlock(m[i]);
7882965a453SKip Macy 			}
78931953be9SAlan Cox 		VM_OBJECT_UNLOCK(object);
7900d94caffSDavid Greenman 		return VM_PAGER_OK;
791f4f83da0SAlan Cox 	} else if (reqblock == -1) {
792f4f83da0SAlan Cox 		pmap_zero_page(m[reqpage]);
79312aa4fdcSAlan Cox 		KASSERT(m[reqpage]->dirty == 0,
79412aa4fdcSAlan Cox 		    ("vnode_pager_generic_getpages: page %p is dirty", m));
795f4f83da0SAlan Cox 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
796f4f83da0SAlan Cox 		for (i = 0; i < count; i++)
7972965a453SKip Macy 			if (i != reqpage) {
7982965a453SKip Macy 				vm_page_lock(m[i]);
799f4f83da0SAlan Cox 				vm_page_free(m[i]);
8002965a453SKip Macy 				vm_page_unlock(m[i]);
8012965a453SKip Macy 			}
802f4f83da0SAlan Cox 		VM_OBJECT_UNLOCK(object);
803f4f83da0SAlan Cox 		return (VM_PAGER_OK);
8040d94caffSDavid Greenman 	}
8058d17e694SJulian Elischer 	m[reqpage]->valid = 0;
8068b575f6cSAlan Cox 	VM_OBJECT_UNLOCK(object);
8070bdb7528SDavid Greenman 
8080d94caffSDavid Greenman 	/*
80926f9a767SRodney W. Grimes 	 * here on direct device I/O
81026f9a767SRodney W. Grimes 	 */
811efc68ce1SDavid Greenman 	firstaddr = -1;
812a1287949SEivind Eklund 
81326f9a767SRodney W. Grimes 	/*
814efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
81526f9a767SRodney W. Grimes 	 */
816efc68ce1SDavid Greenman 	for (first = 0, i = 0; i < count; i = runend) {
817bff76343SAlan Cox 		if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
818bff76343SAlan Cox 		    &runpg) != 0) {
819bff76343SAlan Cox 			VM_OBJECT_LOCK(object);
820bff76343SAlan Cox 			for (; i < count; i++)
8212965a453SKip Macy 				if (i != reqpage) {
8222965a453SKip Macy 					vm_page_lock(m[i]);
823bff76343SAlan Cox 					vm_page_free(m[i]);
8242965a453SKip Macy 					vm_page_unlock(m[i]);
8252965a453SKip Macy 				}
826bff76343SAlan Cox 			VM_OBJECT_UNLOCK(object);
827bff76343SAlan Cox 			return (VM_PAGER_ERROR);
828bff76343SAlan Cox 		}
829efc68ce1SDavid Greenman 		if (firstaddr == -1) {
83031953be9SAlan Cox 			VM_OBJECT_LOCK(object);
83124a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
832f3aad9a6SBjoern A. Zeeb 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
833f3aad9a6SBjoern A. Zeeb 				    (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
834bf1001faSMaxime Henrion 				    (uintmax_t)foff,
835bf1001faSMaxime Henrion 				    (uintmax_t)
836fc62ef1fSBruce Evans 				    (object->un_pager.vnp.vnp_size >> 32),
837bf1001faSMaxime Henrion 				    (uintmax_t)object->un_pager.vnp.vnp_size);
838efc68ce1SDavid Greenman 			}
8392965a453SKip Macy 			vm_page_lock(m[i]);
840d8d5fa88SAlfred Perlstein 			vm_page_free(m[i]);
8412965a453SKip Macy 			vm_page_unlock(m[i]);
84231953be9SAlan Cox 			VM_OBJECT_UNLOCK(object);
843efc68ce1SDavid Greenman 			runend = i + 1;
844efc68ce1SDavid Greenman 			first = runend;
845efc68ce1SDavid Greenman 			continue;
846efc68ce1SDavid Greenman 		}
847efc68ce1SDavid Greenman 		runend = i + runpg;
848efc68ce1SDavid Greenman 		if (runend <= reqpage) {
84931953be9SAlan Cox 			VM_OBJECT_LOCK(object);
8502965a453SKip Macy 			for (j = i; j < runend; j++) {
8512965a453SKip Macy 				vm_page_lock(m[j]);
852d8d5fa88SAlfred Perlstein 				vm_page_free(m[j]);
8532965a453SKip Macy 				vm_page_unlock(m[j]);
8542965a453SKip Macy 			}
85531953be9SAlan Cox 			VM_OBJECT_UNLOCK(object);
85626f9a767SRodney W. Grimes 		} else {
857efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
85831953be9SAlan Cox 				VM_OBJECT_LOCK(object);
8592965a453SKip Macy 				for (i = first + runpg; i < count; i++) {
8602965a453SKip Macy 					vm_page_lock(m[i]);
861d8d5fa88SAlfred Perlstein 					vm_page_free(m[i]);
8622965a453SKip Macy 					vm_page_unlock(m[i]);
8632965a453SKip Macy 				}
86431953be9SAlan Cox 				VM_OBJECT_UNLOCK(object);
865efc68ce1SDavid Greenman 				count = first + runpg;
86626f9a767SRodney W. Grimes 			}
867efc68ce1SDavid Greenman 			break;
86826f9a767SRodney W. Grimes 		}
869efc68ce1SDavid Greenman 		first = runend;
870efc68ce1SDavid Greenman 	}
87126f9a767SRodney W. Grimes 
87226f9a767SRodney W. Grimes 	/*
873bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
874bbc0ec52SDavid Greenman 	 * to be zero based...
87526f9a767SRodney W. Grimes 	 */
87626f9a767SRodney W. Grimes 	if (first != 0) {
8777e2393ffSAlan Cox 		m += first;
87826f9a767SRodney W. Grimes 		count -= first;
87926f9a767SRodney W. Grimes 		reqpage -= first;
88026f9a767SRodney W. Grimes 	}
881efc68ce1SDavid Greenman 
88226f9a767SRodney W. Grimes 	/*
88326f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
88426f9a767SRodney W. Grimes 	 */
885a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
88626f9a767SRodney W. Grimes 
88726f9a767SRodney W. Grimes 	/*
88826f9a767SRodney W. Grimes 	 * calculate the size of the transfer
88926f9a767SRodney W. Grimes 	 */
89026f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
8911a31a6c3SPoul-Henning Kamp 	KASSERT(count > 0, ("zero count"));
89224a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
89324a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
8941a31a6c3SPoul-Henning Kamp 	KASSERT(size > 0, ("zero size"));
89526f9a767SRodney W. Grimes 
89626f9a767SRodney W. Grimes 	/*
89724579ca1SMatthew Dillon 	 * round up physical size for real devices.
89826f9a767SRodney W. Grimes 	 */
8999c83534dSPoul-Henning Kamp 	if (1) {
9009c83534dSPoul-Henning Kamp 		int secmask = bo->bo_bsize - 1;
9016229cc50SPoul-Henning Kamp 		KASSERT(secmask < PAGE_SIZE && secmask > 0,
9026229cc50SPoul-Henning Kamp 		    ("vnode_pager_generic_getpages: sector size %d too large",
9036229cc50SPoul-Henning Kamp 		    secmask + 1));
90424579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
90524579ca1SMatthew Dillon 	}
90626f9a767SRodney W. Grimes 
9071c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
90816f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
90916f62314SDavid Greenman 
91026f9a767SRodney W. Grimes 	/*
91126f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
91226f9a767SRodney W. Grimes 	 */
91316f62314SDavid Greenman 	pmap_qenter(kva, m, count);
91426f9a767SRodney W. Grimes 
91526f9a767SRodney W. Grimes 	/* build a minimal buffer header */
91621144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
9176a4b5823SPoul-Henning Kamp 	bp->b_iodone = bdone;
918bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
919bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
920a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
921a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
922187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
9239c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
9241faacf5dSKirk McKusick 	bp->b_vp = vp;
92526f9a767SRodney W. Grimes 	bp->b_bcount = size;
92626f9a767SRodney W. Grimes 	bp->b_bufsize = size;
9272b6b0df7SMatthew Dillon 	bp->b_runningbufspace = bp->b_bufsize;
9285bd65606SJohn Baldwin 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
92926f9a767SRodney W. Grimes 
930b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodein);
931b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsin, count);
932976e77fcSDavid Greenman 
93326f9a767SRodney W. Grimes 	/* do the input */
9342c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
935b792bebeSPoul-Henning Kamp 	bstrategy(bp);
936976e77fcSDavid Greenman 
9376a4b5823SPoul-Henning Kamp 	bwait(bp, PVM, "vnread");
93826f9a767SRodney W. Grimes 
939c244d2deSPoul-Henning Kamp 	if ((bp->b_ioflags & BIO_ERROR) != 0)
94026f9a767SRodney W. Grimes 		error = EIO;
94126f9a767SRodney W. Grimes 
94226f9a767SRodney W. Grimes 	if (!error) {
94326f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
94426f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
94526f9a767SRodney W. Grimes 	}
94616f62314SDavid Greenman 	pmap_qremove(kva, count);
94726f9a767SRodney W. Grimes 
94826f9a767SRodney W. Grimes 	/*
94926f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
95026f9a767SRodney W. Grimes 	 */
9511faacf5dSKirk McKusick 	bp->b_vp = NULL;
9529c83534dSPoul-Henning Kamp 	pbrelbo(bp);
9531c7c3c6aSMatthew Dillon 	relpbuf(bp, &vnode_pbuf_freecnt);
95426f9a767SRodney W. Grimes 
95531953be9SAlan Cox 	VM_OBJECT_LOCK(object);
9568f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
9578f9110f6SJohn Dyson 		vm_page_t mt;
9588f9110f6SJohn Dyson 
9598f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
9608f9110f6SJohn Dyson 		mt = m[i];
9618f9110f6SJohn Dyson 
96254746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
9638d17e694SJulian Elischer 			/*
9648d17e694SJulian Elischer 			 * Read filled up entire page.
9658d17e694SJulian Elischer 			 */
9668f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
967016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
968016a3c93SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is dirty",
969016a3c93SAlan Cox 			    mt));
970016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
971016a3c93SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is mapped",
972016a3c93SAlan Cox 			    mt));
9738f9110f6SJohn Dyson 		} else {
9748d17e694SJulian Elischer 			/*
97542eb4108SAlan Cox 			 * Read did not fill up entire page.
9768d17e694SJulian Elischer 			 *
9778d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
9788d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
9798d17e694SJulian Elischer 			 * read.
9808d17e694SJulian Elischer 			 */
981dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
98254746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
98342eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
98442eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
98542eb4108SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is dirty",
98642eb4108SAlan Cox 			    mt));
9878f9110f6SJohn Dyson 		}
9888f9110f6SJohn Dyson 
9890055cbd3SKonstantin Belousov 		if (i != reqpage)
9900055cbd3SKonstantin Belousov 			vm_page_readahead_finish(mt, error);
99103679e23SAlan Cox 	}
99231953be9SAlan Cox 	VM_OBJECT_UNLOCK(object);
99326f9a767SRodney W. Grimes 	if (error) {
99424a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
99526f9a767SRodney W. Grimes 	}
996a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
99726f9a767SRodney W. Grimes }
99826f9a767SRodney W. Grimes 
999ce75f2c3SMike Smith /*
1000ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1001ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1002ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1003ce75f2c3SMike Smith  *
1004ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1005ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1006ce75f2c3SMike Smith  */
1007e4542174SMatthew Dillon static void
1008170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
1009170db9c6SJohn Dyson 	vm_object_t object;
1010170db9c6SJohn Dyson 	vm_page_t *m;
1011170db9c6SJohn Dyson 	int count;
1012170db9c6SJohn Dyson 	boolean_t sync;
1013170db9c6SJohn Dyson 	int *rtvals;
1014170db9c6SJohn Dyson {
1015170db9c6SJohn Dyson 	int rtval;
1016170db9c6SJohn Dyson 	struct vnode *vp;
101786ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1018ad980522SJohn Dyson 
10190e3cdf2cSAlan Cox 	/*
10200e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
10210e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
10220e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
10230e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
10240e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
10250e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
10260e3cdf2cSAlan Cox 	 *
10270e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
10280e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
10290e3cdf2cSAlan Cox 	 */
10300e3cdf2cSAlan Cox 
10312feb50bfSAttilio Rao 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
10320e3cdf2cSAlan Cox 		sync |= OBJPC_SYNC;
10330e3cdf2cSAlan Cox 
10340e3cdf2cSAlan Cox 	/*
10350e3cdf2cSAlan Cox 	 * Call device-specific putpages function
10360e3cdf2cSAlan Cox 	 */
1037170db9c6SJohn Dyson 	vp = object->handle;
10382e3b314dSAlan Cox 	VM_OBJECT_UNLOCK(object);
103986ffbd76SMike Smith 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
104023955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
104123955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
10422e3b314dSAlan Cox 	VM_OBJECT_LOCK(object);
1043170db9c6SJohn Dyson }
1044170db9c6SJohn Dyson 
1045ce75f2c3SMike Smith 
104626f9a767SRodney W. Grimes /*
1047ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
10484491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
10492b6b0df7SMatthew Dillon  *
10502b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
10512b6b0df7SMatthew Dillon  * clustering has already typically occured, so in general we ask the
10522b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
10532b6b0df7SMatthew Dillon  * then delayed.
105426f9a767SRodney W. Grimes  */
1055ce75f2c3SMike Smith int
1056c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1057c46b90e9SAlan Cox     int flags, int *rtvals)
105826f9a767SRodney W. Grimes {
1059f6b04d2bSDavid Greenman 	int i;
1060ce75f2c3SMike Smith 	vm_object_t object;
1061c46b90e9SAlan Cox 	vm_page_t m;
1062ce75f2c3SMike Smith 	int count;
106326f9a767SRodney W. Grimes 
1064f6b04d2bSDavid Greenman 	int maxsize, ncount;
1065a316d390SJohn Dyson 	vm_ooffset_t poffset;
1066f6b04d2bSDavid Greenman 	struct uio auio;
1067f6b04d2bSDavid Greenman 	struct iovec aiov;
1068f6b04d2bSDavid Greenman 	int error;
10698f9110f6SJohn Dyson 	int ioflags;
1070dd498befSPaul Saab 	int ppscheck = 0;
1071dd498befSPaul Saab 	static struct timeval lastfail;
1072dd498befSPaul Saab 	static int curfail;
107326f9a767SRodney W. Grimes 
1074ce75f2c3SMike Smith 	object = vp->v_object;
1075ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1076ce75f2c3SMike Smith 
107726f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1078031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
107926f9a767SRodney W. Grimes 
1080c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
108123562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1082c46b90e9SAlan Cox 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1083f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1084f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
10850d94caffSDavid Greenman 	}
10860bdb7528SDavid Greenman 
1087f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1088f6b04d2bSDavid Greenman 	ncount = count;
108926f9a767SRodney W. Grimes 
1090c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
109100a6f47fSMatthew Dillon 
109200a6f47fSMatthew Dillon 	/*
109300a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
109400a6f47fSMatthew Dillon 	 * have to invalidate pages occuring beyond the file EOF.  However,
109500a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
109600a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
109700a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
109800a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
109900a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
11003ebeaf59SMatthew Dillon 	 *
11013ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
11023ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
110300a6f47fSMatthew Dillon 	 */
1104c46b90e9SAlan Cox 	VM_OBJECT_LOCK(object);
1105a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
110600a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
110700a6f47fSMatthew Dillon 			int pgoff;
110800a6f47fSMatthew Dillon 
1109a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1110aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
111100a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1112c46b90e9SAlan Cox 				/*
1113c46b90e9SAlan Cox 				 * If the object is locked and the following
1114c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1115c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1116c46b90e9SAlan Cox 				 * pmap operation.
1117c46b90e9SAlan Cox 				 */
1118c46b90e9SAlan Cox 				m = ma[ncount - 1];
1119c46b90e9SAlan Cox 				KASSERT(m->busy > 0,
1120c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not busy", m));
11216031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1122c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1123c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1124c46b90e9SAlan Cox 				    pgoff);
112500a6f47fSMatthew Dillon 			}
112600a6f47fSMatthew Dillon 		} else {
112700a6f47fSMatthew Dillon 			maxsize = 0;
112800a6f47fSMatthew Dillon 			ncount = 0;
112900a6f47fSMatthew Dillon 		}
1130f6b04d2bSDavid Greenman 		if (ncount < count) {
1131f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1132f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1133f6b04d2bSDavid Greenman 			}
1134f6b04d2bSDavid Greenman 		}
1135f6b04d2bSDavid Greenman 	}
1136c46b90e9SAlan Cox 	VM_OBJECT_UNLOCK(object);
113726f9a767SRodney W. Grimes 
11382b6b0df7SMatthew Dillon 	/*
11392b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
11402b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
114143b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
114243b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
114343b7990eSMatthew Dillon 	 * the system decides how to cluster.
11442b6b0df7SMatthew Dillon 	 */
11458f9110f6SJohn Dyson 	ioflags = IO_VMIO;
114643b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
114743b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
114843b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
114943b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
11508f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
115143b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1152f6b04d2bSDavid Greenman 
1153f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1154f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1155f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1156f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1157a316d390SJohn Dyson 	auio.uio_offset = poffset;
1158f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1159f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1160f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1161b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1162a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1163b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodeout);
1164b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1165f6b04d2bSDavid Greenman 
1166f6b04d2bSDavid Greenman 	if (error) {
1167dd498befSPaul Saab 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
116824a1cce3SDavid Greenman 			printf("vnode_pager_putpages: I/O error %d\n", error);
1169f6b04d2bSDavid Greenman 	}
1170f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1171dd498befSPaul Saab 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
11729f80ce04SKonstantin Belousov 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1173c46b90e9SAlan Cox 			    auio.uio_resid, (u_long)ma[0]->pindex);
117426f9a767SRodney W. Grimes 	}
1175ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
117626f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
117726f9a767SRodney W. Grimes 	}
1178f6b04d2bSDavid Greenman 	return rtvals[0];
117926f9a767SRodney W. Grimes }
1180031ec8c1SKonstantin Belousov 
1181031ec8c1SKonstantin Belousov void
1182031ec8c1SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1183031ec8c1SKonstantin Belousov {
11849d17da3bSKonstantin Belousov 	vm_object_t obj;
1185031ec8c1SKonstantin Belousov 	int i, pos;
1186031ec8c1SKonstantin Belousov 
11879d17da3bSKonstantin Belousov 	if (written == 0)
11889d17da3bSKonstantin Belousov 		return;
11899d17da3bSKonstantin Belousov 	obj = ma[0]->object;
11909d17da3bSKonstantin Belousov 	VM_OBJECT_LOCK(obj);
1191031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1192031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1193031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1194031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1195031ec8c1SKonstantin Belousov 		} else {
1196031ec8c1SKonstantin Belousov 			/* Partially written page. */
1197031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1198031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1199031ec8c1SKonstantin Belousov 		}
1200031ec8c1SKonstantin Belousov 	}
12019d17da3bSKonstantin Belousov 	VM_OBJECT_UNLOCK(obj);
1202031ec8c1SKonstantin Belousov }
120384110e7eSKonstantin Belousov 
120484110e7eSKonstantin Belousov void
120584110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
120684110e7eSKonstantin Belousov     vm_offset_t end)
120784110e7eSKonstantin Belousov {
120884110e7eSKonstantin Belousov 	struct vnode *vp;
120984110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
121084110e7eSKonstantin Belousov 
121184110e7eSKonstantin Belousov 	VM_OBJECT_LOCK(object);
121284110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
121384110e7eSKonstantin Belousov 		VM_OBJECT_UNLOCK(object);
121484110e7eSKonstantin Belousov 		return;
121584110e7eSKonstantin Belousov 	}
121684110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
121784110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
121884110e7eSKonstantin Belousov 	vp = object->handle;
121984110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
122084110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
122184110e7eSKonstantin Belousov 		vp->v_writecount++;
1222b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1223b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
122484110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
122584110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
122684110e7eSKonstantin Belousov 		vp->v_writecount--;
1227b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1228b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
122984110e7eSKonstantin Belousov 	}
123084110e7eSKonstantin Belousov 	VM_OBJECT_UNLOCK(object);
123184110e7eSKonstantin Belousov }
123284110e7eSKonstantin Belousov 
123384110e7eSKonstantin Belousov void
123484110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
123584110e7eSKonstantin Belousov     vm_offset_t end)
123684110e7eSKonstantin Belousov {
123784110e7eSKonstantin Belousov 	struct vnode *vp;
123884110e7eSKonstantin Belousov 	struct mount *mp;
123984110e7eSKonstantin Belousov 	vm_offset_t inc;
124084110e7eSKonstantin Belousov 	int vfslocked;
124184110e7eSKonstantin Belousov 
124284110e7eSKonstantin Belousov 	VM_OBJECT_LOCK(object);
124384110e7eSKonstantin Belousov 
124484110e7eSKonstantin Belousov 	/*
124584110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
124684110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
124784110e7eSKonstantin Belousov 	 */
124884110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
124984110e7eSKonstantin Belousov 		VM_OBJECT_UNLOCK(object);
125084110e7eSKonstantin Belousov 		return;
125184110e7eSKonstantin Belousov 	}
125284110e7eSKonstantin Belousov 
125384110e7eSKonstantin Belousov 	/*
125484110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
125584110e7eSKonstantin Belousov 	 * zero.
125684110e7eSKonstantin Belousov 	 */
125784110e7eSKonstantin Belousov 	inc = end - start;
125884110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
125984110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
126084110e7eSKonstantin Belousov 		VM_OBJECT_UNLOCK(object);
126184110e7eSKonstantin Belousov 		return;
126284110e7eSKonstantin Belousov 	}
126384110e7eSKonstantin Belousov 
126484110e7eSKonstantin Belousov 	vp = object->handle;
126584110e7eSKonstantin Belousov 	vhold(vp);
126684110e7eSKonstantin Belousov 	VM_OBJECT_UNLOCK(object);
126784110e7eSKonstantin Belousov 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
126884110e7eSKonstantin Belousov 	mp = NULL;
126984110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
127084110e7eSKonstantin Belousov 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
127184110e7eSKonstantin Belousov 
127284110e7eSKonstantin Belousov 	/*
127384110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
127484110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
127584110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
127684110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
127784110e7eSKonstantin Belousov 	 */
127884110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
127984110e7eSKonstantin Belousov 	VOP_UNLOCK(vp, 0);
128084110e7eSKonstantin Belousov 	vdrop(vp);
128184110e7eSKonstantin Belousov 	if (mp != NULL)
128284110e7eSKonstantin Belousov 		vn_finished_write(mp);
128384110e7eSKonstantin Belousov 	VFS_UNLOCK_GIANT(vfslocked);
128484110e7eSKonstantin Belousov }
1285