xref: /freebsd/sys/vm/vnode_pager.c (revision 7146d6cb3e9b88a881f8133cb98106feb39a1f44)
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 
68df8bae1dSRodney W. Grimes #include <vm/vm.h>
69efeaf95aSDavid Greenman #include <vm/vm_object.h>
70df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7124a1cce3SDavid Greenman #include <vm/vm_pager.h>
721efb74fbSJohn Dyson #include <vm/vm_map.h>
73df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
74efeaf95aSDavid Greenman #include <vm/vm_extern.h>
75df8bae1dSRodney W. Grimes 
7611caded3SAlfred Perlstein static void vnode_pager_init(void);
7711caded3SAlfred Perlstein static vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
7811caded3SAlfred Perlstein 					 int *run);
7911caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
8011caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
8111caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
8211caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
8311caded3SAlfred Perlstein static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
8411caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
85d07a6d3fSPoul-Henning Kamp static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t);
860b8253a7SBruce Evans 
87df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
884e658600SPoul-Henning Kamp 	.pgo_init =	vnode_pager_init,
894e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
904e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
914e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
924e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
934e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
94df8bae1dSRodney W. Grimes };
95df8bae1dSRodney W. Grimes 
96b62b9b64SJohn Baldwin int vnode_pbuf_freecnt;
971c7c3c6aSMatthew Dillon 
9837c84183SPoul-Henning Kamp static void
99b62b9b64SJohn Baldwin vnode_pager_init(void)
100b62b9b64SJohn Baldwin {
101b62b9b64SJohn Baldwin 
102b62b9b64SJohn Baldwin 	vnode_pbuf_freecnt = nswbuf / 2 + 1;
103b62b9b64SJohn Baldwin }
104170db9c6SJohn Dyson 
105d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
106d07a6d3fSPoul-Henning Kamp int
107d07a6d3fSPoul-Henning Kamp vnode_create_vobject(struct vnode *vp, size_t isize, struct thread *td)
108d07a6d3fSPoul-Henning Kamp {
109d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
110d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
111d07a6d3fSPoul-Henning Kamp 	struct vattr va;
112d07a6d3fSPoul-Henning Kamp 
113d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
114d07a6d3fSPoul-Henning Kamp 		return (0);
115d07a6d3fSPoul-Henning Kamp 
116d07a6d3fSPoul-Henning Kamp 	while ((object = vp->v_object) != NULL) {
117d07a6d3fSPoul-Henning Kamp 		VM_OBJECT_LOCK(object);
118d07a6d3fSPoul-Henning Kamp 		if (!(object->flags & OBJ_DEAD)) {
119d07a6d3fSPoul-Henning Kamp 			VM_OBJECT_UNLOCK(object);
120d07a6d3fSPoul-Henning Kamp 			return (0);
121d07a6d3fSPoul-Henning Kamp 		}
122d07a6d3fSPoul-Henning Kamp 		VOP_UNLOCK(vp, 0, td);
123d07a6d3fSPoul-Henning Kamp 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
124d07a6d3fSPoul-Henning Kamp 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0);
125d07a6d3fSPoul-Henning Kamp 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
126d07a6d3fSPoul-Henning Kamp 	}
127d07a6d3fSPoul-Henning Kamp 
128d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
129d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
130d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
131d07a6d3fSPoul-Henning Kamp 		} else {
132d07a6d3fSPoul-Henning Kamp 			if (VOP_GETATTR(vp, &va, td->td_ucred, td) != 0)
133d07a6d3fSPoul-Henning Kamp 				return (0);
134d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
135d07a6d3fSPoul-Henning Kamp 		}
136d07a6d3fSPoul-Henning Kamp 	}
137d07a6d3fSPoul-Henning Kamp 
138d07a6d3fSPoul-Henning Kamp 	object = vnode_pager_alloc(vp, size, 0, 0);
139d07a6d3fSPoul-Henning Kamp 	/*
140d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
141d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
142d07a6d3fSPoul-Henning Kamp 	 */
143d07a6d3fSPoul-Henning Kamp 	VM_OBJECT_LOCK(object);
144d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
145d07a6d3fSPoul-Henning Kamp 	VM_OBJECT_UNLOCK(object);
146d07a6d3fSPoul-Henning Kamp 	vrele(vp);
147d07a6d3fSPoul-Henning Kamp 
148d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
149d07a6d3fSPoul-Henning Kamp 
150d07a6d3fSPoul-Henning Kamp 	return (0);
151d07a6d3fSPoul-Henning Kamp }
152d07a6d3fSPoul-Henning Kamp 
1537146d6cbSPoul-Henning Kamp void
1547146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1557146d6cbSPoul-Henning Kamp {
1567146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1577146d6cbSPoul-Henning Kamp 
1587146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1597146d6cbSPoul-Henning Kamp 	if (obj == NULL)
1607146d6cbSPoul-Henning Kamp 		return;
1617146d6cbSPoul-Henning Kamp 	vp->v_object = NULL;
1627146d6cbSPoul-Henning Kamp 	VM_OBJECT_LOCK(obj);
1637146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1647146d6cbSPoul-Henning Kamp 		/*
1657146d6cbSPoul-Henning Kamp 		 * vclean() may be called twice. The first time
1667146d6cbSPoul-Henning Kamp 		 * removes the primary reference to the object,
1677146d6cbSPoul-Henning Kamp 		 * the second time goes one further and is a
1687146d6cbSPoul-Henning Kamp 		 * special-case to terminate the object.
1697146d6cbSPoul-Henning Kamp 		 *
1707146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1717146d6cbSPoul-Henning Kamp 		 */
1727146d6cbSPoul-Henning Kamp 		if ((obj->flags & OBJ_DEAD) == 0)
1737146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
1747146d6cbSPoul-Henning Kamp 		else
1757146d6cbSPoul-Henning Kamp 			VM_OBJECT_UNLOCK(obj);
1767146d6cbSPoul-Henning Kamp 	} else {
1777146d6cbSPoul-Henning Kamp 		/*
1787146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
1797146d6cbSPoul-Henning Kamp 		 */
1807146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
1817146d6cbSPoul-Henning Kamp 		VM_OBJECT_UNLOCK(obj);
1827146d6cbSPoul-Henning Kamp 	}
1837146d6cbSPoul-Henning Kamp }
1847146d6cbSPoul-Henning Kamp 
1857146d6cbSPoul-Henning Kamp 
186df8bae1dSRodney W. Grimes /*
187df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
188df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
189990ab7adSAlan Cox  *
190990ab7adSAlan Cox  * MPSAFE
191df8bae1dSRodney W. Grimes  */
19224a1cce3SDavid Greenman vm_object_t
1936cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
194b9dcd593SBruce Evans 		  vm_ooffset_t offset)
195df8bae1dSRodney W. Grimes {
19606cb7259SDavid Greenman 	vm_object_t object;
197df8bae1dSRodney W. Grimes 	struct vnode *vp;
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes 	/*
200df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
201df8bae1dSRodney W. Grimes 	 */
202df8bae1dSRodney W. Grimes 	if (handle == NULL)
203df8bae1dSRodney W. Grimes 		return (NULL);
204df8bae1dSRodney W. Grimes 
205df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
20639d38f93SDavid Greenman 
20763e7e60dSJeff Roberson 	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
20863e7e60dSJeff Roberson 
20939d38f93SDavid Greenman 	/*
21039d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
21139d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
21239d38f93SDavid Greenman 	 */
213e6e370a7SJeff Roberson 	VI_LOCK(vp);
214e6e370a7SJeff Roberson 	while (vp->v_iflag & VI_OLOCK) {
215e6e370a7SJeff Roberson 		vp->v_iflag |= VI_OWANT;
216e6e370a7SJeff Roberson 		msleep(vp, VI_MTX(vp), PVM, "vnpobj", 0);
21739d38f93SDavid Greenman 	}
218e6e370a7SJeff Roberson 	vp->v_iflag |= VI_OLOCK;
219e6e370a7SJeff Roberson 	VI_UNLOCK(vp);
22039d38f93SDavid Greenman 
22139d38f93SDavid Greenman 	/*
22239d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
22339d38f93SDavid Greenman 	 * go away.
22439d38f93SDavid Greenman 	 */
2251ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
2261ca58953SAlan Cox 		VM_OBJECT_LOCK(object);
2271ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
2281ca58953SAlan Cox 			break;
22919187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
2301ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
23124a1cce3SDavid Greenman 	}
2320d94caffSDavid Greenman 
2332be70f79SJohn Dyson 	if (vp->v_usecount == 0)
2342be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
2352be70f79SJohn Dyson 
23624a1cce3SDavid Greenman 	if (object == NULL) {
237df8bae1dSRodney W. Grimes 		/*
238df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
239df8bae1dSRodney W. Grimes 		 */
2406cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
241bbc0ec52SDavid Greenman 
2426cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
24326f9a767SRodney W. Grimes 
24424a1cce3SDavid Greenman 		object->handle = handle;
24524a1cce3SDavid Greenman 		vp->v_object = object;
246df8bae1dSRodney W. Grimes 	} else {
24795e5e988SJohn Dyson 		object->ref_count++;
2481ca58953SAlan Cox 		VM_OBJECT_UNLOCK(object);
249df8bae1dSRodney W. Grimes 	}
250e6e370a7SJeff Roberson 	VI_LOCK(vp);
251990ab7adSAlan Cox 	vp->v_usecount++;
252e6e370a7SJeff Roberson 	vp->v_iflag &= ~VI_OLOCK;
253e6e370a7SJeff Roberson 	if (vp->v_iflag & VI_OWANT) {
254e6e370a7SJeff Roberson 		vp->v_iflag &= ~VI_OWANT;
25539d38f93SDavid Greenman 		wakeup(vp);
25639d38f93SDavid Greenman 	}
257e6e370a7SJeff Roberson 	VI_UNLOCK(vp);
25824a1cce3SDavid Greenman 	return (object);
259df8bae1dSRodney W. Grimes }
260df8bae1dSRodney W. Grimes 
261658ad5ffSAlan Cox /*
262658ad5ffSAlan Cox  *	The object must be locked.
263658ad5ffSAlan Cox  */
264f708ef1bSPoul-Henning Kamp static void
26524a1cce3SDavid Greenman vnode_pager_dealloc(object)
2660d94caffSDavid Greenman 	vm_object_t object;
26724a1cce3SDavid Greenman {
26854d92145SMatthew Dillon 	struct vnode *vp = object->handle;
269df8bae1dSRodney W. Grimes 
27024a1cce3SDavid Greenman 	if (vp == NULL)
27124a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
27224a1cce3SDavid Greenman 
273658ad5ffSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
27466095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
27524a1cce3SDavid Greenman 
27624a1cce3SDavid Greenman 	object->handle = NULL;
27795461b45SJohn Dyson 	object->type = OBJT_DEAD;
27819187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
27919187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
28019187819SAlan Cox 		wakeup(object);
28119187819SAlan Cox 	}
282e6e370a7SJeff Roberson 	ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc");
283aa2cabb9SDavid Greenman 	vp->v_object = NULL;
28435764be3SPoul-Henning Kamp 	vp->v_vflag &= ~VV_TEXT;
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 	 */
308e6e370a7SJeff Roberson 	if (vp == NULL)
30947221757SJohn Dyson 		return FALSE;
31047221757SJohn Dyson 
31163e7e60dSJeff Roberson 	VI_LOCK(vp);
31263e7e60dSJeff Roberson 	if (vp->v_iflag & VI_DOOMED) {
31363e7e60dSJeff Roberson 		VI_UNLOCK(vp);
314e6e370a7SJeff Roberson 		return FALSE;
31563e7e60dSJeff Roberson 	}
31663e7e60dSJeff Roberson 	VI_UNLOCK(vp);
317df8bae1dSRodney W. Grimes 	/*
3180d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
3190d94caffSDavid Greenman 	 * not have the page.
320df8bae1dSRodney W. Grimes 	 */
321a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
322a316d390SJohn Dyson 	    (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
3234abc71c0SDavid Greenman 		return FALSE;
324df8bae1dSRodney W. Grimes 
325eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
326170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
327d63596ceSJohn Dyson 	blocksperpage = 0;
328d63596ceSJohn Dyson 	if (pagesperblock > 0) {
329a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
330d63596ceSJohn Dyson 	} else {
331d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
332d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
333d63596ceSJohn Dyson 	}
334f29ba63eSAlan Cox 	VM_OBJECT_UNLOCK(object);
335ae51ff11SJeff Roberson 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
336ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
337ae51ff11SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
338f29ba63eSAlan Cox 	VM_OBJECT_LOCK(object);
3390d94caffSDavid Greenman 	if (err)
34024a1cce3SDavid Greenman 		return TRUE;
3416eab77f2SJohn Dyson 	if (bn == -1)
342ced399eeSJohn Dyson 		return FALSE;
343d63596ceSJohn Dyson 	if (pagesperblock > 0) {
344a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
345170db9c6SJohn Dyson 		if (before) {
346170db9c6SJohn Dyson 			*before *= pagesperblock;
347170db9c6SJohn Dyson 			*before += poff;
348170db9c6SJohn Dyson 		}
349170db9c6SJohn Dyson 		if (after) {
350b1fc01b7SJohn Dyson 			int numafter;
351170db9c6SJohn Dyson 			*after *= pagesperblock;
352b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
35347e151ddSRobert Drehmel 			if (IDX_TO_OFF(pindex + numafter) >
35447e151ddSRobert Drehmel 			    object->un_pager.vnp.vnp_size) {
35547e151ddSRobert Drehmel 				numafter =
35647e151ddSRobert Drehmel 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
35747e151ddSRobert Drehmel 				    pindex;
358b1fc01b7SJohn Dyson 			}
359b1fc01b7SJohn Dyson 			*after += numafter;
360170db9c6SJohn Dyson 		}
361d63596ceSJohn Dyson 	} else {
362d63596ceSJohn Dyson 		if (before) {
363d63596ceSJohn Dyson 			*before /= blocksperpage;
364d63596ceSJohn Dyson 		}
365d63596ceSJohn Dyson 
366d63596ceSJohn Dyson 		if (after) {
367d63596ceSJohn Dyson 			*after /= blocksperpage;
368d63596ceSJohn Dyson 		}
369d63596ceSJohn Dyson 	}
370ced399eeSJohn Dyson 	return TRUE;
371df8bae1dSRodney W. Grimes }
372df8bae1dSRodney W. Grimes 
373df8bae1dSRodney W. Grimes /*
374df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
37524a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
376df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
377df8bae1dSRodney W. Grimes  *
378df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
379df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
380df8bae1dSRodney W. Grimes  */
381df8bae1dSRodney W. Grimes void
382df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
383df8bae1dSRodney W. Grimes 	struct vnode *vp;
384a316d390SJohn Dyson 	vm_ooffset_t nsize;
385df8bae1dSRodney W. Grimes {
3862a8f9ab5SAlan Cox 	vm_object_t object;
3872a8f9ab5SAlan Cox 	vm_page_t m;
388c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
389df8bae1dSRodney W. Grimes 
3902a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
391df8bae1dSRodney W. Grimes 		return;
3922a8f9ab5SAlan Cox 	VM_OBJECT_LOCK(object);
3932a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
394df8bae1dSRodney W. Grimes 		/*
395df8bae1dSRodney W. Grimes 		 * Hasn't changed size
396df8bae1dSRodney W. Grimes 		 */
3972a8f9ab5SAlan Cox 		VM_OBJECT_UNLOCK(object);
398df8bae1dSRodney W. Grimes 		return;
3992a8f9ab5SAlan Cox 	}
400c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
4012a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
402df8bae1dSRodney W. Grimes 		/*
403bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
404df8bae1dSRodney W. Grimes 		 */
4052a8f9ab5SAlan Cox 		if (nobjsize < object->size)
406c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
407c576d121SLuoqi Chen 			    FALSE);
408bbc0ec52SDavid Greenman 		/*
409bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4103ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4113ebeaf59SMatthew Dillon 		 *
4123ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4133ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4143ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
415bbc0ec52SDavid Greenman 		 */
4162a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4171b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4181b26eb10SAlan Cox 		    m->valid != 0) {
4192b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4202b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4212b6b0df7SMatthew Dillon 
4222b6b0df7SMatthew Dillon 			/*
4232b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4242b6b0df7SMatthew Dillon 			 * the page has been mapped.
4252b6b0df7SMatthew Dillon 			 */
426fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4272b6b0df7SMatthew Dillon 
4282b6b0df7SMatthew Dillon 			/*
4293ebeaf59SMatthew Dillon 			 * XXX work around SMP data integrity race
4303ebeaf59SMatthew Dillon 			 * by unmapping the page from user processes.
4313ebeaf59SMatthew Dillon 			 * The garbage we just cleared may be mapped
4323ebeaf59SMatthew Dillon 			 * to a user process running on another cpu
4333ebeaf59SMatthew Dillon 			 * and this code is not running through normal
4343ebeaf59SMatthew Dillon 			 * I/O channels which handle SMP issues for
4353ebeaf59SMatthew Dillon 			 * us, so unmap page to synchronize all cpus.
4363ebeaf59SMatthew Dillon 			 *
4373ebeaf59SMatthew Dillon 			 * XXX should vm_pager_unmap_page() have
4383ebeaf59SMatthew Dillon 			 * dealt with this?
4393ebeaf59SMatthew Dillon 			 */
4401b26eb10SAlan Cox 			vm_page_lock_queues();
4414fec79beSAlan Cox 			pmap_remove_all(m);
4423ebeaf59SMatthew Dillon 
4433ebeaf59SMatthew Dillon 			/*
4442b6b0df7SMatthew Dillon 			 * Clear out partial-page dirty bits.  This
4452b6b0df7SMatthew Dillon 			 * has the side effect of setting the valid
4462b6b0df7SMatthew Dillon 			 * bits, but that is ok.  There are a bunch
4472b6b0df7SMatthew Dillon 			 * of places in the VM system where we expected
4482b6b0df7SMatthew Dillon 			 * m->dirty == VM_PAGE_BITS_ALL.  The file EOF
4492b6b0df7SMatthew Dillon 			 * case is one of them.  If the page is still
4502b6b0df7SMatthew Dillon 			 * partially dirty, make it fully dirty.
4513ebeaf59SMatthew Dillon 			 *
4523ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4533ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4543ebeaf59SMatthew Dillon 			 * replacement from working properly.
4552b6b0df7SMatthew Dillon 			 */
4562b6b0df7SMatthew Dillon 			vm_page_set_validclean(m, base, size);
4572b6b0df7SMatthew Dillon 			if (m->dirty != 0)
4582b6b0df7SMatthew Dillon 				m->dirty = VM_PAGE_BITS_ALL;
4592a8f9ab5SAlan Cox 			vm_page_unlock_queues();
460bbc0ec52SDavid Greenman 		}
461bbc0ec52SDavid Greenman 	}
462a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
463c576d121SLuoqi Chen 	object->size = nobjsize;
4642a8f9ab5SAlan Cox 	VM_OBJECT_UNLOCK(object);
465df8bae1dSRodney W. Grimes }
466df8bae1dSRodney W. Grimes 
46726f9a767SRodney W. Grimes /*
46826f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
46926f9a767SRodney W. Grimes  * file address
47026f9a767SRodney W. Grimes  */
471f708ef1bSPoul-Henning Kamp static vm_offset_t
472efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
47326f9a767SRodney W. Grimes 	struct vnode *vp;
474a316d390SJohn Dyson 	vm_ooffset_t address;
475efc68ce1SDavid Greenman 	int *run;
47626f9a767SRodney W. Grimes {
47726f9a767SRodney W. Grimes 	int rtaddress;
47826f9a767SRodney W. Grimes 	int bsize;
47998b0c789SPoul-Henning Kamp 	daddr_t block;
48026f9a767SRodney W. Grimes 	int err;
481a316d390SJohn Dyson 	daddr_t vblock;
482a316d390SJohn Dyson 	int voffset;
48326f9a767SRodney W. Grimes 
4842ad036b6SAlan Cox 	if (address < 0)
4850d94caffSDavid Greenman 		return -1;
4860d94caffSDavid Greenman 
4872c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4882c4488fcSJohn Dyson 		return -1;
4892c4488fcSJohn Dyson 
49026f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
49126f9a767SRodney W. Grimes 	vblock = address / bsize;
49226f9a767SRodney W. Grimes 	voffset = address % bsize;
49326f9a767SRodney W. Grimes 
494ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, vblock, NULL, &block, run, NULL);
49526f9a767SRodney W. Grimes 
496efc68ce1SDavid Greenman 	if (err || (block == -1))
49726f9a767SRodney W. Grimes 		rtaddress = -1;
498efc68ce1SDavid Greenman 	else {
499187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
500efc68ce1SDavid Greenman 		if (run) {
501efc68ce1SDavid Greenman 			*run += 1;
502efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
503efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
504efc68ce1SDavid Greenman 		}
505efc68ce1SDavid Greenman 	}
50626f9a767SRodney W. Grimes 
50726f9a767SRodney W. Grimes 	return rtaddress;
50826f9a767SRodney W. Grimes }
50926f9a767SRodney W. Grimes 
51026f9a767SRodney W. Grimes /*
51126f9a767SRodney W. Grimes  * small block filesystem vnode pager input
51226f9a767SRodney W. Grimes  */
513f708ef1bSPoul-Henning Kamp static int
51424a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
51524a1cce3SDavid Greenman 	vm_object_t object;
51626f9a767SRodney W. Grimes 	vm_page_t m;
51726f9a767SRodney W. Grimes {
51826f9a767SRodney W. Grimes 	int i;
5199c83534dSPoul-Henning Kamp 	struct vnode *vp;
5209c83534dSPoul-Henning Kamp 	struct bufobj *bo;
52126f9a767SRodney W. Grimes 	struct buf *bp;
5229e0ddbd0SAlan Cox 	struct sf_buf *sf;
52326f9a767SRodney W. Grimes 	int fileaddr;
52426f9a767SRodney W. Grimes 	vm_offset_t bsize;
52526f9a767SRodney W. Grimes 	int error = 0;
52626f9a767SRodney W. Grimes 
52724a1cce3SDavid Greenman 	vp = object->handle;
5282c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
5292c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5302c4488fcSJohn Dyson 
53126f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5320bdb7528SDavid Greenman 
5339c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
53426f9a767SRodney W. Grimes 
5359e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
53626f9a767SRodney W. Grimes 
53726f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
53833c67741SMatthew Dillon 		vm_ooffset_t address;
539bbc0ec52SDavid Greenman 
540897a45efSDmitrij Tejblum 		if (vm_page_bits(i * bsize, bsize) & m->valid)
54126f9a767SRodney W. Grimes 			continue;
54226f9a767SRodney W. Grimes 
54333c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
54433c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
54533c67741SMatthew Dillon 			fileaddr = -1;
54633c67741SMatthew Dillon 		} else {
54733c67741SMatthew Dillon 			fileaddr = vnode_pager_addr(vp, address, NULL);
54833c67741SMatthew Dillon 		}
54926f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5501c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
55126f9a767SRodney W. Grimes 
55226f9a767SRodney W. Grimes 			/* build a minimal buffer header */
55321144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5546a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
555bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
556bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
557a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
558a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5599e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
560187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5619c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
56226f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
56326f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5642b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5652b6b0df7SMatthew Dillon 			runningbufspace += bp->b_runningbufspace;
56626f9a767SRodney W. Grimes 
56726f9a767SRodney W. Grimes 			/* do the input */
5682c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
569b792bebeSPoul-Henning Kamp 			bstrategy(bp);
57026f9a767SRodney W. Grimes 
571e47ed70bSJohn Dyson 			/* we definitely need to be at splvm here */
57226f9a767SRodney W. Grimes 
5736a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5746a4b5823SPoul-Henning Kamp 
575c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
57626f9a767SRodney W. Grimes 				error = EIO;
57726f9a767SRodney W. Grimes 
57826f9a767SRodney W. Grimes 			/*
57926f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
58026f9a767SRodney W. Grimes 			 */
5819c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5821c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
58326f9a767SRodney W. Grimes 			if (error)
58426f9a767SRodney W. Grimes 				break;
5850d94caffSDavid Greenman 
5862bf43e43SAlan Cox 			VM_OBJECT_LOCK(object);
587178949e0SAlan Cox 			vm_page_lock_queues();
588aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
589178949e0SAlan Cox 			vm_page_unlock_queues();
5902bf43e43SAlan Cox 			VM_OBJECT_UNLOCK(object);
59126f9a767SRodney W. Grimes 		} else {
5922bf43e43SAlan Cox 			VM_OBJECT_LOCK(object);
593178949e0SAlan Cox 			vm_page_lock_queues();
594aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
595178949e0SAlan Cox 			vm_page_unlock_queues();
5962bf43e43SAlan Cox 			VM_OBJECT_UNLOCK(object);
5979e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
59826f9a767SRodney W. Grimes 		}
59926f9a767SRodney W. Grimes 	}
6009e0ddbd0SAlan Cox 	sf_buf_free(sf);
60185e01243SAlan Cox 	vm_page_lock_queues();
6020385347cSPeter Wemm 	pmap_clear_modify(m);
60385e01243SAlan Cox 	vm_page_unlock_queues();
60426f9a767SRodney W. Grimes 	if (error) {
605a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
60626f9a767SRodney W. Grimes 	}
60726f9a767SRodney W. Grimes 	return VM_PAGER_OK;
60826f9a767SRodney W. Grimes 
60926f9a767SRodney W. Grimes }
61026f9a767SRodney W. Grimes 
61126f9a767SRodney W. Grimes 
61226f9a767SRodney W. Grimes /*
613475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
61426f9a767SRodney W. Grimes  */
615f708ef1bSPoul-Henning Kamp static int
61624a1cce3SDavid Greenman vnode_pager_input_old(object, m)
61724a1cce3SDavid Greenman 	vm_object_t object;
61826f9a767SRodney W. Grimes 	vm_page_t m;
61926f9a767SRodney W. Grimes {
620df8bae1dSRodney W. Grimes 	struct uio auio;
621df8bae1dSRodney W. Grimes 	struct iovec aiov;
62226f9a767SRodney W. Grimes 	int error;
62326f9a767SRodney W. Grimes 	int size;
6249e0ddbd0SAlan Cox 	struct sf_buf *sf;
625342a1480SJohn Baldwin 	struct vnode *vp;
626df8bae1dSRodney W. Grimes 
62752051abcSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
62826f9a767SRodney W. Grimes 	error = 0;
629bbc0ec52SDavid Greenman 
630df8bae1dSRodney W. Grimes 	/*
63126f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
63226f9a767SRodney W. Grimes 	 */
633a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
63426f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
63526f9a767SRodney W. Grimes 	} else {
63626f9a767SRodney W. Grimes 		size = PAGE_SIZE;
637a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
638a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
63952051abcSAlan Cox 		vp = object->handle;
64052051abcSAlan Cox 		VM_OBJECT_UNLOCK(object);
6410bdb7528SDavid Greenman 
64226f9a767SRodney W. Grimes 		/*
643df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
644df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
645df8bae1dSRodney W. Grimes 		 */
6469e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6470bdb7528SDavid Greenman 
6489e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
649df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
650df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
651df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
652a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
653df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
65426f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
655df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
656b40ce416SJulian Elischer 		auio.uio_td = curthread;
65726f9a767SRodney W. Grimes 
658a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
659df8bae1dSRodney W. Grimes 		if (!error) {
66054d92145SMatthew Dillon 			int count = size - auio.uio_resid;
661df8bae1dSRodney W. Grimes 
662df8bae1dSRodney W. Grimes 			if (count == 0)
663df8bae1dSRodney W. Grimes 				error = EINVAL;
66426f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6659e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6669e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
667df8bae1dSRodney W. Grimes 		}
6689e0ddbd0SAlan Cox 		sf_buf_free(sf);
6691b26eb10SAlan Cox 
6701b26eb10SAlan Cox 		VM_OBJECT_LOCK(object);
671df8bae1dSRodney W. Grimes 	}
67285e01243SAlan Cox 	vm_page_lock_queues();
6730385347cSPeter Wemm 	pmap_clear_modify(m);
6742c28a105SAlan Cox 	vm_page_undirty(m);
6751b26eb10SAlan Cox 	vm_page_unlock_queues();
6766e3a3f38SRobert V. Baron 	if (!error)
6776e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
678a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
67926f9a767SRodney W. Grimes }
68026f9a767SRodney W. Grimes 
68126f9a767SRodney W. Grimes /*
68226f9a767SRodney W. Grimes  * generic vnode pager input routine
68326f9a767SRodney W. Grimes  */
684170db9c6SJohn Dyson 
685ce75f2c3SMike Smith /*
68623955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
68747e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
68847e151ddSRobert Drehmel  * to implement the previous behaviour.
689ce75f2c3SMike Smith  *
690ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
691ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
692ce75f2c3SMike Smith  */
693f708ef1bSPoul-Henning Kamp static int
69424a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
69526f9a767SRodney W. Grimes 	vm_object_t object;
69624a1cce3SDavid Greenman 	vm_page_t *m;
69724a1cce3SDavid Greenman 	int count;
69824a1cce3SDavid Greenman 	int reqpage;
69924a1cce3SDavid Greenman {
700170db9c6SJohn Dyson 	int rtval;
701170db9c6SJohn Dyson 	struct vnode *vp;
70286ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
703ae51ff11SJeff Roberson 	int vfslocked;
70495e5e988SJohn Dyson 
705170db9c6SJohn Dyson 	vp = object->handle;
7068630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
707ae51ff11SJeff Roberson 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
70886ffbd76SMike Smith 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
70923955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
71023955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
711ae51ff11SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
7128630c117SAlan Cox 	VM_OBJECT_LOCK(object);
713170db9c6SJohn Dyson 	return rtval;
714170db9c6SJohn Dyson }
715170db9c6SJohn Dyson 
716ce75f2c3SMike Smith /*
717ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
718ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
719ce75f2c3SMike Smith  */
720ce75f2c3SMike Smith int
721ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
722ce75f2c3SMike Smith 	struct vnode *vp;
723170db9c6SJohn Dyson 	vm_page_t *m;
724ce75f2c3SMike Smith 	int bytecount;
725170db9c6SJohn Dyson 	int reqpage;
726170db9c6SJohn Dyson {
727ce75f2c3SMike Smith 	vm_object_t object;
728a316d390SJohn Dyson 	vm_offset_t kva;
7298f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
730e43c2eabSAlan Cox 	int i, j, size, bsize, first, firstaddr;
7319c83534dSPoul-Henning Kamp 	struct bufobj *bo;
732efc68ce1SDavid Greenman 	int runpg;
733efc68ce1SDavid Greenman 	int runend;
7340bdb7528SDavid Greenman 	struct buf *bp;
735ce75f2c3SMike Smith 	int count;
73626f9a767SRodney W. Grimes 	int error = 0;
73726f9a767SRodney W. Grimes 
738ce75f2c3SMike Smith 	object = vp->v_object;
739ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
740ce75f2c3SMike Smith 
7419c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
7429c83534dSPoul-Henning Kamp 	    ("vnode_pager_generic_getpages does not support devices"));
7432c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
7442c4488fcSJohn Dyson 		return VM_PAGER_BAD;
7452c4488fcSJohn Dyson 
74626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
74726f9a767SRodney W. Grimes 
74826f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
749bbc0ec52SDavid Greenman 
75026f9a767SRodney W. Grimes 	/*
751bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
752bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
75326f9a767SRodney W. Grimes 	 */
754a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
755bbc0ec52SDavid Greenman 
75626f9a767SRodney W. Grimes 	/*
75716f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
75826f9a767SRodney W. Grimes 	 */
7599c83534dSPoul-Henning Kamp 	if (VOP_BMAP(vp, 0, &bo, 0, NULL, NULL)) {
76031953be9SAlan Cox 		VM_OBJECT_LOCK(object);
761e43c2eabSAlan Cox 		vm_page_lock_queues();
762e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
763e43c2eabSAlan Cox 			if (i != reqpage)
764d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
765e43c2eabSAlan Cox 		vm_page_unlock_queues();
766976e77fcSDavid Greenman 		cnt.v_vnodein++;
767976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
76852051abcSAlan Cox 		error = vnode_pager_input_old(object, m[reqpage]);
76952051abcSAlan Cox 		VM_OBJECT_UNLOCK(object);
77052051abcSAlan Cox 		return (error);
771bbc0ec52SDavid Greenman 
77226f9a767SRodney W. Grimes 		/*
77326f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
77426f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
77526f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
77626f9a767SRodney W. Grimes 		 */
77726f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
778500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
77931953be9SAlan Cox 		VM_OBJECT_LOCK(object);
780e43c2eabSAlan Cox 		vm_page_lock_queues();
781e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
782e43c2eabSAlan Cox 			if (i != reqpage)
783d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
784e43c2eabSAlan Cox 		vm_page_unlock_queues();
78531953be9SAlan Cox 		VM_OBJECT_UNLOCK(object);
786976e77fcSDavid Greenman 		cnt.v_vnodein++;
787976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
78824a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
78926f9a767SRodney W. Grimes 	}
7908d17e694SJulian Elischer 
79126f9a767SRodney W. Grimes 	/*
7928d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
7938d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
7948d17e694SJulian Elischer 	 * media.
7950d94caffSDavid Greenman 	 */
79631953be9SAlan Cox 	VM_OBJECT_LOCK(object);
7978b575f6cSAlan Cox 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
798e43c2eabSAlan Cox 		vm_page_lock_queues();
799e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
8000d94caffSDavid Greenman 			if (i != reqpage)
801d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
802e43c2eabSAlan Cox 		vm_page_unlock_queues();
80331953be9SAlan Cox 		VM_OBJECT_UNLOCK(object);
8040d94caffSDavid Greenman 		return VM_PAGER_OK;
8050d94caffSDavid Greenman 	}
8068d17e694SJulian Elischer 	m[reqpage]->valid = 0;
8078b575f6cSAlan Cox 	VM_OBJECT_UNLOCK(object);
8080bdb7528SDavid Greenman 
8090d94caffSDavid Greenman 	/*
81026f9a767SRodney W. Grimes 	 * here on direct device I/O
81126f9a767SRodney W. Grimes 	 */
812efc68ce1SDavid Greenman 	firstaddr = -1;
813a1287949SEivind Eklund 
81426f9a767SRodney W. Grimes 	/*
815efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
81626f9a767SRodney W. Grimes 	 */
817efc68ce1SDavid Greenman 	for (first = 0, i = 0; i < count; i = runend) {
818a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
819a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
820efc68ce1SDavid Greenman 		if (firstaddr == -1) {
82131953be9SAlan Cox 			VM_OBJECT_LOCK(object);
82224a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
823bf1001faSMaxime Henrion 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
824bf1001faSMaxime Henrion 				    firstaddr, (uintmax_t)(foff >> 32),
825bf1001faSMaxime Henrion 				    (uintmax_t)foff,
826bf1001faSMaxime Henrion 				    (uintmax_t)
827fc62ef1fSBruce Evans 				    (object->un_pager.vnp.vnp_size >> 32),
828bf1001faSMaxime Henrion 				    (uintmax_t)object->un_pager.vnp.vnp_size);
829efc68ce1SDavid Greenman 			}
830e43c2eabSAlan Cox 			vm_page_lock_queues();
831d8d5fa88SAlfred Perlstein 			vm_page_free(m[i]);
832e43c2eabSAlan Cox 			vm_page_unlock_queues();
83331953be9SAlan Cox 			VM_OBJECT_UNLOCK(object);
834efc68ce1SDavid Greenman 			runend = i + 1;
835efc68ce1SDavid Greenman 			first = runend;
836efc68ce1SDavid Greenman 			continue;
837efc68ce1SDavid Greenman 		}
838efc68ce1SDavid Greenman 		runend = i + runpg;
839efc68ce1SDavid Greenman 		if (runend <= reqpage) {
84031953be9SAlan Cox 			VM_OBJECT_LOCK(object);
841e43c2eabSAlan Cox 			vm_page_lock_queues();
842e43c2eabSAlan Cox 			for (j = i; j < runend; j++)
843d8d5fa88SAlfred Perlstein 				vm_page_free(m[j]);
844e43c2eabSAlan Cox 			vm_page_unlock_queues();
84531953be9SAlan Cox 			VM_OBJECT_UNLOCK(object);
84626f9a767SRodney W. Grimes 		} else {
847efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
84831953be9SAlan Cox 				VM_OBJECT_LOCK(object);
849e43c2eabSAlan Cox 				vm_page_lock_queues();
850efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
851d8d5fa88SAlfred Perlstein 					vm_page_free(m[i]);
852e43c2eabSAlan Cox 				vm_page_unlock_queues();
85331953be9SAlan Cox 				VM_OBJECT_UNLOCK(object);
854efc68ce1SDavid Greenman 				count = first + runpg;
85526f9a767SRodney W. Grimes 			}
856efc68ce1SDavid Greenman 			break;
85726f9a767SRodney W. Grimes 		}
858efc68ce1SDavid Greenman 		first = runend;
859efc68ce1SDavid Greenman 	}
86026f9a767SRodney W. Grimes 
86126f9a767SRodney W. Grimes 	/*
862bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
863bbc0ec52SDavid Greenman 	 * to be zero based...
86426f9a767SRodney W. Grimes 	 */
86526f9a767SRodney W. Grimes 	if (first != 0) {
86626f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
86726f9a767SRodney W. Grimes 			m[i - first] = m[i];
86826f9a767SRodney W. Grimes 		}
86926f9a767SRodney W. Grimes 		count -= first;
87026f9a767SRodney W. Grimes 		reqpage -= first;
87126f9a767SRodney W. Grimes 	}
872efc68ce1SDavid Greenman 
87326f9a767SRodney W. Grimes 	/*
87426f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
87526f9a767SRodney W. Grimes 	 */
876a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
87726f9a767SRodney W. Grimes 
87826f9a767SRodney W. Grimes 	/*
87926f9a767SRodney W. Grimes 	 * calculate the size of the transfer
88026f9a767SRodney W. Grimes 	 */
88126f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
8821a31a6c3SPoul-Henning Kamp 	KASSERT(count > 0, ("zero count"));
88324a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
88424a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
8851a31a6c3SPoul-Henning Kamp 	KASSERT(size > 0, ("zero size"));
88626f9a767SRodney W. Grimes 
88726f9a767SRodney W. Grimes 	/*
88824579ca1SMatthew Dillon 	 * round up physical size for real devices.
88926f9a767SRodney W. Grimes 	 */
8909c83534dSPoul-Henning Kamp 	if (1) {
8919c83534dSPoul-Henning Kamp 		int secmask = bo->bo_bsize - 1;
8926229cc50SPoul-Henning Kamp 		KASSERT(secmask < PAGE_SIZE && secmask > 0,
8936229cc50SPoul-Henning Kamp 		    ("vnode_pager_generic_getpages: sector size %d too large",
8946229cc50SPoul-Henning Kamp 		    secmask + 1));
89524579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
89624579ca1SMatthew Dillon 	}
89726f9a767SRodney W. Grimes 
8981c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
89916f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
90016f62314SDavid Greenman 
90126f9a767SRodney W. Grimes 	/*
90226f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
90326f9a767SRodney W. Grimes 	 */
90416f62314SDavid Greenman 	pmap_qenter(kva, m, count);
90526f9a767SRodney W. Grimes 
90626f9a767SRodney W. Grimes 	/* build a minimal buffer header */
90721144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
9086a4b5823SPoul-Henning Kamp 	bp->b_iodone = bdone;
909bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
910bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
911a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
912a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
913187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
9149c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
91526f9a767SRodney W. Grimes 	bp->b_bcount = size;
91626f9a767SRodney W. Grimes 	bp->b_bufsize = size;
9172b6b0df7SMatthew Dillon 	bp->b_runningbufspace = bp->b_bufsize;
9182b6b0df7SMatthew Dillon 	runningbufspace += bp->b_runningbufspace;
91926f9a767SRodney W. Grimes 
920976e77fcSDavid Greenman 	cnt.v_vnodein++;
921976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
922976e77fcSDavid Greenman 
92326f9a767SRodney W. Grimes 	/* do the input */
9242c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
925b792bebeSPoul-Henning Kamp 	bstrategy(bp);
926976e77fcSDavid Greenman 
9276a4b5823SPoul-Henning Kamp 	bwait(bp, PVM, "vnread");
92826f9a767SRodney W. Grimes 
929c244d2deSPoul-Henning Kamp 	if ((bp->b_ioflags & BIO_ERROR) != 0)
93026f9a767SRodney W. Grimes 		error = EIO;
93126f9a767SRodney W. Grimes 
93226f9a767SRodney W. Grimes 	if (!error) {
93326f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
93426f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
93526f9a767SRodney W. Grimes 	}
93616f62314SDavid Greenman 	pmap_qremove(kva, count);
93726f9a767SRodney W. Grimes 
93826f9a767SRodney W. Grimes 	/*
93926f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
94026f9a767SRodney W. Grimes 	 */
9419c83534dSPoul-Henning Kamp 	pbrelbo(bp);
9421c7c3c6aSMatthew Dillon 	relpbuf(bp, &vnode_pbuf_freecnt);
94326f9a767SRodney W. Grimes 
94431953be9SAlan Cox 	VM_OBJECT_LOCK(object);
9459d522888SAlan Cox 	vm_page_lock_queues();
9468f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
9478f9110f6SJohn Dyson 		vm_page_t mt;
9488f9110f6SJohn Dyson 
9498f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
9508f9110f6SJohn Dyson 		mt = m[i];
9518f9110f6SJohn Dyson 
95254746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
9538d17e694SJulian Elischer 			/*
9548d17e694SJulian Elischer 			 * Read filled up entire page.
9558d17e694SJulian Elischer 			 */
9568f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
9572c28a105SAlan Cox 			vm_page_undirty(mt);	/* should be an assert? XXX */
9580385347cSPeter Wemm 			pmap_clear_modify(mt);
9598f9110f6SJohn Dyson 		} else {
9608d17e694SJulian Elischer 			/*
9618d17e694SJulian Elischer 			 * Read did not fill up entire page.  Since this
9628d17e694SJulian Elischer 			 * is getpages, the page may be mapped, so we have
9638d17e694SJulian Elischer 			 * to zero the invalid portions of the page even
9648d17e694SJulian Elischer 			 * though we aren't setting them valid.
9658d17e694SJulian Elischer 			 *
9668d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
9678d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
9688d17e694SJulian Elischer 			 * read.
9698d17e694SJulian Elischer 			 */
97054746b67SDmitrij Tejblum 			vm_page_set_validclean(mt, 0,
97154746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
9724221e284SAlan Cox 			/* handled by vm_fault now */
9734221e284SAlan Cox 			/* vm_page_zero_invalid(mt, FALSE); */
9748f9110f6SJohn Dyson 		}
9758f9110f6SJohn Dyson 
97626f9a767SRodney W. Grimes 		if (i != reqpage) {
977bbc0ec52SDavid Greenman 
97826f9a767SRodney W. Grimes 			/*
979bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
980bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
981bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
982956f3135SPhilippe Charnier 			 * It appears that empirical results show that
983bbc0ec52SDavid Greenman 			 * deactivating pages is best.
98426f9a767SRodney W. Grimes 			 */
985bbc0ec52SDavid Greenman 
98626f9a767SRodney W. Grimes 			/*
987bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
988bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
98926f9a767SRodney W. Grimes 			 */
99026f9a767SRodney W. Grimes 			if (!error) {
9918f9110f6SJohn Dyson 				if (mt->flags & PG_WANTED)
9928f9110f6SJohn Dyson 					vm_page_activate(mt);
99395461b45SJohn Dyson 				else
9948f9110f6SJohn Dyson 					vm_page_deactivate(mt);
995e69763a3SDoug Rabson 				vm_page_wakeup(mt);
99626f9a767SRodney W. Grimes 			} else {
997d8d5fa88SAlfred Perlstein 				vm_page_free(mt);
99826f9a767SRodney W. Grimes 			}
99926f9a767SRodney W. Grimes 		}
100026f9a767SRodney W. Grimes 	}
10019d522888SAlan Cox 	vm_page_unlock_queues();
100231953be9SAlan Cox 	VM_OBJECT_UNLOCK(object);
100326f9a767SRodney W. Grimes 	if (error) {
100424a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
100526f9a767SRodney W. Grimes 	}
1006a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
100726f9a767SRodney W. Grimes }
100826f9a767SRodney W. Grimes 
1009ce75f2c3SMike Smith /*
1010ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1011ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1012ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1013ce75f2c3SMike Smith  *
1014ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1015ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1016ce75f2c3SMike Smith  */
1017e4542174SMatthew Dillon static void
1018170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
1019170db9c6SJohn Dyson 	vm_object_t object;
1020170db9c6SJohn Dyson 	vm_page_t *m;
1021170db9c6SJohn Dyson 	int count;
1022170db9c6SJohn Dyson 	boolean_t sync;
1023170db9c6SJohn Dyson 	int *rtvals;
1024170db9c6SJohn Dyson {
1025170db9c6SJohn Dyson 	int rtval;
1026170db9c6SJohn Dyson 	struct vnode *vp;
1027f2a2857bSKirk McKusick 	struct mount *mp;
102886ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1029ad980522SJohn Dyson 
10300e3cdf2cSAlan Cox 	/*
10310e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
10320e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
10330e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
10340e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
10350e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
10360e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
10370e3cdf2cSAlan Cox 	 *
10380e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
10390e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
10400e3cdf2cSAlan Cox 	 */
10410e3cdf2cSAlan Cox 
10420e3cdf2cSAlan Cox 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
10430e3cdf2cSAlan Cox 		sync |= OBJPC_SYNC;
10440e3cdf2cSAlan Cox 
10450e3cdf2cSAlan Cox 	/*
10460e3cdf2cSAlan Cox 	 * Call device-specific putpages function
10470e3cdf2cSAlan Cox 	 */
1048170db9c6SJohn Dyson 	vp = object->handle;
10492e3b314dSAlan Cox 	VM_OBJECT_UNLOCK(object);
1050f2a2857bSKirk McKusick 	if (vp->v_type != VREG)
1051f2a2857bSKirk McKusick 		mp = NULL;
1052f2a2857bSKirk McKusick 	(void)vn_start_write(vp, &mp, V_WAIT);
105386ffbd76SMike Smith 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
105423955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
105523955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
1056f2a2857bSKirk McKusick 	vn_finished_write(mp);
10572e3b314dSAlan Cox 	VM_OBJECT_LOCK(object);
1058170db9c6SJohn Dyson }
1059170db9c6SJohn Dyson 
1060ce75f2c3SMike Smith 
106126f9a767SRodney W. Grimes /*
1062ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
10634491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
10642b6b0df7SMatthew Dillon  *
10652b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
10662b6b0df7SMatthew Dillon  * clustering has already typically occured, so in general we ask the
10672b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
10682b6b0df7SMatthew Dillon  * then delayed.
106926f9a767SRodney W. Grimes  */
1070ce75f2c3SMike Smith int
10718f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
1072ce75f2c3SMike Smith 	struct vnode *vp;
107326f9a767SRodney W. Grimes 	vm_page_t *m;
1074ce75f2c3SMike Smith 	int bytecount;
10758f9110f6SJohn Dyson 	int flags;
107626f9a767SRodney W. Grimes 	int *rtvals;
107726f9a767SRodney W. Grimes {
1078f6b04d2bSDavid Greenman 	int i;
1079ce75f2c3SMike Smith 	vm_object_t object;
1080ce75f2c3SMike Smith 	int count;
108126f9a767SRodney W. Grimes 
1082f6b04d2bSDavid Greenman 	int maxsize, ncount;
1083a316d390SJohn Dyson 	vm_ooffset_t poffset;
1084f6b04d2bSDavid Greenman 	struct uio auio;
1085f6b04d2bSDavid Greenman 	struct iovec aiov;
1086f6b04d2bSDavid Greenman 	int error;
10878f9110f6SJohn Dyson 	int ioflags;
108826f9a767SRodney W. Grimes 
1089ce75f2c3SMike Smith 	object = vp->v_object;
1090ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1091ce75f2c3SMike Smith 
109226f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
109326f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
109426f9a767SRodney W. Grimes 
1095d8fed1d0SAlan Cox 	if ((int64_t)m[0]->pindex < 0) {
109623562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
109723562e4bSMarcel Moolenaar 			(long)m[0]->pindex, (u_long)m[0]->dirty);
1098f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1099f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
11000d94caffSDavid Greenman 	}
11010bdb7528SDavid Greenman 
1102f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1103f6b04d2bSDavid Greenman 	ncount = count;
110426f9a767SRodney W. Grimes 
1105a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
110600a6f47fSMatthew Dillon 
110700a6f47fSMatthew Dillon 	/*
110800a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
110900a6f47fSMatthew Dillon 	 * have to invalidate pages occuring beyond the file EOF.  However,
111000a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
111100a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
111200a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
111300a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
111400a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
11153ebeaf59SMatthew Dillon 	 *
11163ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
11173ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
111800a6f47fSMatthew Dillon 	 */
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) {
1126b7ad744dSAlan Cox 				vm_page_lock_queues();
112700a6f47fSMatthew Dillon 				vm_page_clear_dirty(m[ncount - 1], pgoff,
112800a6f47fSMatthew Dillon 					PAGE_SIZE - pgoff);
1129b7ad744dSAlan Cox 				vm_page_unlock_queues();
113000a6f47fSMatthew Dillon 			}
113100a6f47fSMatthew Dillon 		} else {
113200a6f47fSMatthew Dillon 			maxsize = 0;
113300a6f47fSMatthew Dillon 			ncount = 0;
113400a6f47fSMatthew Dillon 		}
1135f6b04d2bSDavid Greenman 		if (ncount < count) {
1136f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1137f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1138f6b04d2bSDavid Greenman 			}
1139f6b04d2bSDavid Greenman 		}
1140f6b04d2bSDavid Greenman 	}
114126f9a767SRodney W. Grimes 
11422b6b0df7SMatthew Dillon 	/*
11432b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
11442b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
114543b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
114643b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
114743b7990eSMatthew Dillon 	 * the system decides how to cluster.
11482b6b0df7SMatthew Dillon 	 */
11498f9110f6SJohn Dyson 	ioflags = IO_VMIO;
115043b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
115143b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
115243b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
115343b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
11548f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
115543b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1156f6b04d2bSDavid Greenman 
1157f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1158f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1159f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1160f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1161a316d390SJohn Dyson 	auio.uio_offset = poffset;
1162f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1163f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1164f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1165b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1166a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1167976e77fcSDavid Greenman 	cnt.v_vnodeout++;
1168f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
1169f6b04d2bSDavid Greenman 
1170f6b04d2bSDavid Greenman 	if (error) {
117124a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
1172f6b04d2bSDavid Greenman 	}
1173f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1174ac1e407bSBruce Evans 		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1175ac1e407bSBruce Evans 		    auio.uio_resid, (u_long)m[0]->pindex);
117626f9a767SRodney W. Grimes 	}
1177ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
117826f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
117926f9a767SRodney W. Grimes 	}
1180f6b04d2bSDavid Greenman 	return rtvals[0];
118126f9a767SRodney W. Grimes }
1182f6b04d2bSDavid Greenman 
1183f6b04d2bSDavid Greenman struct vnode *
1184417a26a1SAlan Cox vnode_pager_lock(vm_object_t first_object)
118524a1cce3SDavid Greenman {
1186417a26a1SAlan Cox 	struct vnode *vp;
1187417a26a1SAlan Cox 	vm_object_t backing_object, object;
1188996c772fSJohn Dyson 
1189417a26a1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
1190417a26a1SAlan Cox 	for (object = first_object; object != NULL; object = backing_object) {
1191417a26a1SAlan Cox 		if (object->type != OBJT_VNODE) {
1192417a26a1SAlan Cox 			if ((backing_object = object->backing_object) != NULL)
1193417a26a1SAlan Cox 				VM_OBJECT_LOCK(backing_object);
1194417a26a1SAlan Cox 			if (object != first_object)
1195417a26a1SAlan Cox 				VM_OBJECT_UNLOCK(object);
1196f6b04d2bSDavid Greenman 			continue;
1197417a26a1SAlan Cox 		}
1198417a26a1SAlan Cox 	retry:
1199e6b961ffSJohn Baldwin 		if (object->flags & OBJ_DEAD) {
1200417a26a1SAlan Cox 			if (object != first_object)
1201417a26a1SAlan Cox 				VM_OBJECT_UNLOCK(object);
120247221757SJohn Dyson 			return NULL;
1203e6b961ffSJohn Baldwin 		}
1204417a26a1SAlan Cox 		vp = object->handle;
1205417a26a1SAlan Cox 		VI_LOCK(vp);
1206417a26a1SAlan Cox 		VM_OBJECT_UNLOCK(object);
1207417a26a1SAlan Cox 		if (first_object != object)
1208417a26a1SAlan Cox 			VM_OBJECT_UNLOCK(first_object);
1209417a26a1SAlan Cox 		if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | LK_NOPAUSE |
1210417a26a1SAlan Cox 		    LK_RETRY | LK_SHARED, curthread)) {
1211417a26a1SAlan Cox 			VM_OBJECT_LOCK(first_object);
1212417a26a1SAlan Cox 			if (object != first_object)
1213417a26a1SAlan Cox 				VM_OBJECT_LOCK(object);
1214417a26a1SAlan Cox 			if (object->type != OBJT_VNODE) {
1215417a26a1SAlan Cox 				if (object != first_object)
1216417a26a1SAlan Cox 					VM_OBJECT_UNLOCK(object);
1217bef608bdSJohn Dyson 				return NULL;
1218417a26a1SAlan Cox 			}
121947221757SJohn Dyson 			printf("vnode_pager_lock: retrying\n");
1220417a26a1SAlan Cox 			goto retry;
122147221757SJohn Dyson 		}
1222417a26a1SAlan Cox 		VM_OBJECT_LOCK(first_object);
1223417a26a1SAlan Cox 		return (vp);
122426f9a767SRodney W. Grimes 	}
122524a1cce3SDavid Greenman 	return NULL;
1226f6b04d2bSDavid Greenman }
1227