xref: /freebsd/sys/vm/vnode_pager.c (revision d15b55c554f610d6e7f7dcd37cdc9743c1b204c4)
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>
6689f6b863SAttilio Rao #include <sys/rwlock.h>
679e0ddbd0SAlan Cox #include <sys/sf_buf.h>
68df8bae1dSRodney W. Grimes 
694f12e0acSSuleiman Souhlal #include <machine/atomic.h>
704f12e0acSSuleiman Souhlal 
71df8bae1dSRodney W. Grimes #include <vm/vm.h>
721c771f92SKonstantin Belousov #include <vm/vm_param.h>
73efeaf95aSDavid Greenman #include <vm/vm_object.h>
74df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7524a1cce3SDavid Greenman #include <vm/vm_pager.h>
761efb74fbSJohn Dyson #include <vm/vm_map.h>
77df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
78efeaf95aSDavid Greenman #include <vm/vm_extern.h>
79df8bae1dSRodney W. Grimes 
80bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
81bff76343SAlan Cox     daddr_t *rtaddress, int *run);
8211caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
8311caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
8411caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
8511caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
8633cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
8711caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
883364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
893364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *cred);
900b8253a7SBruce Evans 
91df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
924e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
934e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
944e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
954e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
964e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
97df8bae1dSRodney W. Grimes };
98df8bae1dSRodney W. Grimes 
99b62b9b64SJohn Baldwin int vnode_pbuf_freecnt;
1001c7c3c6aSMatthew Dillon 
101d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
102d07a6d3fSPoul-Henning Kamp int
103731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
104d07a6d3fSPoul-Henning Kamp {
105d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
106d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
107d07a6d3fSPoul-Henning Kamp 	struct vattr va;
108d07a6d3fSPoul-Henning Kamp 
109d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
110d07a6d3fSPoul-Henning Kamp 		return (0);
111d07a6d3fSPoul-Henning Kamp 
112d07a6d3fSPoul-Henning Kamp 	while ((object = vp->v_object) != NULL) {
11389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
114d07a6d3fSPoul-Henning Kamp 		if (!(object->flags & OBJ_DEAD)) {
11589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
116d07a6d3fSPoul-Henning Kamp 			return (0);
117d07a6d3fSPoul-Henning Kamp 		}
11822db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
119d07a6d3fSPoul-Henning Kamp 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
1200dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
121cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
122d07a6d3fSPoul-Henning Kamp 	}
123d07a6d3fSPoul-Henning Kamp 
124d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
125d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
126d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
127d07a6d3fSPoul-Henning Kamp 		} else {
1280359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
129d07a6d3fSPoul-Henning Kamp 				return (0);
130d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
131d07a6d3fSPoul-Henning Kamp 		}
132d07a6d3fSPoul-Henning Kamp 	}
133d07a6d3fSPoul-Henning Kamp 
1343364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
135d07a6d3fSPoul-Henning Kamp 	/*
136d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
137d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
138d07a6d3fSPoul-Henning Kamp 	 */
13989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
140d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
14189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
142d07a6d3fSPoul-Henning Kamp 	vrele(vp);
143d07a6d3fSPoul-Henning Kamp 
144d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
145d07a6d3fSPoul-Henning Kamp 
146d07a6d3fSPoul-Henning Kamp 	return (0);
147d07a6d3fSPoul-Henning Kamp }
148d07a6d3fSPoul-Henning Kamp 
1497146d6cbSPoul-Henning Kamp void
1507146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1517146d6cbSPoul-Henning Kamp {
1527146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1537146d6cbSPoul-Henning Kamp 
1547146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1557146d6cbSPoul-Henning Kamp 	if (obj == NULL)
1567146d6cbSPoul-Henning Kamp 		return;
15757fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
15889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1597146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1607146d6cbSPoul-Henning Kamp 		/*
1617146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1627146d6cbSPoul-Henning Kamp 		 */
1637146d6cbSPoul-Henning Kamp 		if ((obj->flags & OBJ_DEAD) == 0)
1647146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
1657146d6cbSPoul-Henning Kamp 		else
16689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
1677146d6cbSPoul-Henning Kamp 	} else {
1687146d6cbSPoul-Henning Kamp 		/*
1697146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
1707146d6cbSPoul-Henning Kamp 		 */
1717146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
17289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
1737146d6cbSPoul-Henning Kamp 	}
1746e4b2820SJeff Roberson 	vp->v_object = NULL;
1757146d6cbSPoul-Henning Kamp }
1767146d6cbSPoul-Henning Kamp 
1777146d6cbSPoul-Henning Kamp 
178df8bae1dSRodney W. Grimes /*
179df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
180df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
181990ab7adSAlan Cox  *
182990ab7adSAlan Cox  * MPSAFE
183df8bae1dSRodney W. Grimes  */
18424a1cce3SDavid Greenman vm_object_t
1856cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
1863364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
187df8bae1dSRodney W. Grimes {
18806cb7259SDavid Greenman 	vm_object_t object;
189df8bae1dSRodney W. Grimes 	struct vnode *vp;
190df8bae1dSRodney W. Grimes 
191df8bae1dSRodney W. Grimes 	/*
192df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
193df8bae1dSRodney W. Grimes 	 */
194df8bae1dSRodney W. Grimes 	if (handle == NULL)
195df8bae1dSRodney W. Grimes 		return (NULL);
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
19839d38f93SDavid Greenman 
19939d38f93SDavid Greenman 	/*
20039d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
20139d38f93SDavid Greenman 	 * go away.
20239d38f93SDavid Greenman 	 */
2032ac78f0eSStephan Uphoff retry:
2041ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
20589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
2061ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
2071ca58953SAlan Cox 			break;
20819187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
2090dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
21024a1cce3SDavid Greenman 	}
2110d94caffSDavid Greenman 
2126ded8427SKonstantin Belousov 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
2132be70f79SJohn Dyson 
21424a1cce3SDavid Greenman 	if (object == NULL) {
215df8bae1dSRodney W. Grimes 		/*
2162ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
217df8bae1dSRodney W. Grimes 		 */
2186cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
219bbc0ec52SDavid Greenman 
2206cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
22184110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
22226f9a767SRodney W. Grimes 
22324a1cce3SDavid Greenman 		object->handle = handle;
22411be8415SStephan Uphoff 		VI_LOCK(vp);
2252ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2262ac78f0eSStephan Uphoff 			/*
2272ac78f0eSStephan Uphoff 			 * Object has been created while we were sleeping
2282ac78f0eSStephan Uphoff 			 */
22911be8415SStephan Uphoff 			VI_UNLOCK(vp);
2302ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2312ac78f0eSStephan Uphoff 			goto retry;
232df8bae1dSRodney W. Grimes 		}
2332ac78f0eSStephan Uphoff 		vp->v_object = object;
23411be8415SStephan Uphoff 		VI_UNLOCK(vp);
23511be8415SStephan Uphoff 	} else {
2362ac78f0eSStephan Uphoff 		object->ref_count++;
23789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
23811be8415SStephan Uphoff 	}
2397747c038SJeff Roberson 	vref(vp);
24024a1cce3SDavid Greenman 	return (object);
241df8bae1dSRodney W. Grimes }
242df8bae1dSRodney W. Grimes 
243658ad5ffSAlan Cox /*
244658ad5ffSAlan Cox  *	The object must be locked.
245658ad5ffSAlan Cox  */
246f708ef1bSPoul-Henning Kamp static void
2477ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
24824a1cce3SDavid Greenman {
249b9f180d1SKonstantin Belousov 	struct vnode *vp;
250b9f180d1SKonstantin Belousov 	int refs;
251df8bae1dSRodney W. Grimes 
252b9f180d1SKonstantin Belousov 	vp = object->handle;
25324a1cce3SDavid Greenman 	if (vp == NULL)
25424a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
25524a1cce3SDavid Greenman 
25689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
25766095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
258b9f180d1SKonstantin Belousov 	refs = object->ref_count;
25924a1cce3SDavid Greenman 
26024a1cce3SDavid Greenman 	object->handle = NULL;
26195461b45SJohn Dyson 	object->type = OBJT_DEAD;
26219187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
26319187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
26419187819SAlan Cox 		wakeup(object);
26519187819SAlan Cox 	}
26657fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
26784110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
26884110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
269140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
270b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
271b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
27284110e7eSKonstantin Belousov 	}
273aa2cabb9SDavid Greenman 	vp->v_object = NULL;
274877d24acSKonstantin Belousov 	VOP_UNSET_TEXT(vp);
27589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
276b9f180d1SKonstantin Belousov 	while (refs-- > 0)
277b9f180d1SKonstantin Belousov 		vunref(vp);
27889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
279df8bae1dSRodney W. Grimes }
28026f9a767SRodney W. Grimes 
281f708ef1bSPoul-Henning Kamp static boolean_t
2827ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
2837ebba1f8SGleb Smirnoff     int *after)
284df8bae1dSRodney W. Grimes {
28524a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
28698b0c789SPoul-Henning Kamp 	daddr_t bn;
2873af76890SPoul-Henning Kamp 	int err;
288170db9c6SJohn Dyson 	daddr_t reqblock;
2892c4488fcSJohn Dyson 	int poff;
2902c4488fcSJohn Dyson 	int bsize;
291d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
292df8bae1dSRodney W. Grimes 
29389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
29424579ca1SMatthew Dillon 	/*
29524579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
29624579ca1SMatthew Dillon 	 * have the page.
29724579ca1SMatthew Dillon 	 */
298b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
29947221757SJohn Dyson 		return FALSE;
300df8bae1dSRodney W. Grimes 	/*
301b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3020d94caffSDavid Greenman 	 * not have the page.
303df8bae1dSRodney W. Grimes 	 */
304b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3054abc71c0SDavid Greenman 		return FALSE;
306df8bae1dSRodney W. Grimes 
307eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
308170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
309d63596ceSJohn Dyson 	blocksperpage = 0;
310d63596ceSJohn Dyson 	if (pagesperblock > 0) {
311a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
312d63596ceSJohn Dyson 	} else {
313d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
314d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
315d63596ceSJohn Dyson 	}
31689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
317ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
31889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
3190d94caffSDavid Greenman 	if (err)
32024a1cce3SDavid Greenman 		return TRUE;
3216eab77f2SJohn Dyson 	if (bn == -1)
322ced399eeSJohn Dyson 		return FALSE;
323d63596ceSJohn Dyson 	if (pagesperblock > 0) {
324a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
325170db9c6SJohn Dyson 		if (before) {
326170db9c6SJohn Dyson 			*before *= pagesperblock;
327170db9c6SJohn Dyson 			*before += poff;
328170db9c6SJohn Dyson 		}
329170db9c6SJohn Dyson 		if (after) {
330b1fc01b7SJohn Dyson 			int numafter;
331170db9c6SJohn Dyson 			*after *= pagesperblock;
332b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
33347e151ddSRobert Drehmel 			if (IDX_TO_OFF(pindex + numafter) >
33447e151ddSRobert Drehmel 			    object->un_pager.vnp.vnp_size) {
33547e151ddSRobert Drehmel 				numafter =
33647e151ddSRobert Drehmel 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
33747e151ddSRobert Drehmel 				    pindex;
338b1fc01b7SJohn Dyson 			}
339b1fc01b7SJohn Dyson 			*after += numafter;
340170db9c6SJohn Dyson 		}
341d63596ceSJohn Dyson 	} else {
342d63596ceSJohn Dyson 		if (before) {
343d63596ceSJohn Dyson 			*before /= blocksperpage;
344d63596ceSJohn Dyson 		}
345d63596ceSJohn Dyson 
346d63596ceSJohn Dyson 		if (after) {
347d63596ceSJohn Dyson 			*after /= blocksperpage;
348d63596ceSJohn Dyson 		}
349d63596ceSJohn Dyson 	}
350ced399eeSJohn Dyson 	return TRUE;
351df8bae1dSRodney W. Grimes }
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes /*
354df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
35524a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
356df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
357df8bae1dSRodney W. Grimes  *
358df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
359df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
360df8bae1dSRodney W. Grimes  */
361df8bae1dSRodney W. Grimes void
3627ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
363df8bae1dSRodney W. Grimes {
3642a8f9ab5SAlan Cox 	vm_object_t object;
3652a8f9ab5SAlan Cox 	vm_page_t m;
366c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
367df8bae1dSRodney W. Grimes 
3682a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
369df8bae1dSRodney W. Grimes 		return;
370cb61d698SKonstantin Belousov /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
37189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
3729b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
3739b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
3749b8851faSKonstantin Belousov 		return;
3759b8851faSKonstantin Belousov 	}
3769b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
3779b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
3782a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
379df8bae1dSRodney W. Grimes 		/*
380df8bae1dSRodney W. Grimes 		 * Hasn't changed size
381df8bae1dSRodney W. Grimes 		 */
38289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
383df8bae1dSRodney W. Grimes 		return;
3842a8f9ab5SAlan Cox 	}
385c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
3862a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
387df8bae1dSRodney W. Grimes 		/*
388bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
389df8bae1dSRodney W. Grimes 		 */
3902a8f9ab5SAlan Cox 		if (nobjsize < object->size)
391c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
3926bbee8e2SAlan Cox 			    0);
393bbc0ec52SDavid Greenman 		/*
394bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
3953ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
3963ebeaf59SMatthew Dillon 		 *
3973ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
3983ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
3993ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
400bbc0ec52SDavid Greenman 		 */
4012a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4021b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4031b26eb10SAlan Cox 		    m->valid != 0) {
4042b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4052b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4062b6b0df7SMatthew Dillon 
4072b6b0df7SMatthew Dillon 			/*
4082b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4092b6b0df7SMatthew Dillon 			 * the page has been mapped.
4102b6b0df7SMatthew Dillon 			 */
411fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4122b6b0df7SMatthew Dillon 
4132b6b0df7SMatthew Dillon 			/*
4143c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4153c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
4163c33df62SAlan Cox 			 * have already been set.
4173c33df62SAlan Cox 			 */
418dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
4193c33df62SAlan Cox 
4203c33df62SAlan Cox 			/*
4213c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
4223c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
4233c33df62SAlan Cox 			 * cleared.
4243c33df62SAlan Cox 			 */
4253c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
4263c33df62SAlan Cox 
4273c33df62SAlan Cox 			/*
4283c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
4293ebeaf59SMatthew Dillon 			 *
4303ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4313ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4323ebeaf59SMatthew Dillon 			 * replacement from working properly.
4332b6b0df7SMatthew Dillon 			 */
4343c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
4350ab3c7a5SAlan Cox 		} else if ((nsize & PAGE_MASK) &&
436db9ba578SAttilio Rao 		    vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
4370ab3c7a5SAlan Cox 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
4380ab3c7a5SAlan Cox 			    nobjsize);
439bbc0ec52SDavid Greenman 		}
440bbc0ec52SDavid Greenman 	}
441a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
442c576d121SLuoqi Chen 	object->size = nobjsize;
44389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
444df8bae1dSRodney W. Grimes }
445df8bae1dSRodney W. Grimes 
44626f9a767SRodney W. Grimes /*
44726f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
44826f9a767SRodney W. Grimes  * file address
44926f9a767SRodney W. Grimes  */
450bff76343SAlan Cox static int
451bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
452bff76343SAlan Cox     int *run)
45326f9a767SRodney W. Grimes {
45426f9a767SRodney W. Grimes 	int bsize;
45526f9a767SRodney W. Grimes 	int err;
456a316d390SJohn Dyson 	daddr_t vblock;
457f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
45826f9a767SRodney W. Grimes 
4592ad036b6SAlan Cox 	if (address < 0)
4600d94caffSDavid Greenman 		return -1;
4610d94caffSDavid Greenman 
462b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
4632c4488fcSJohn Dyson 		return -1;
4642c4488fcSJohn Dyson 
46526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
46626f9a767SRodney W. Grimes 	vblock = address / bsize;
46726f9a767SRodney W. Grimes 	voffset = address % bsize;
46826f9a767SRodney W. Grimes 
469bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
470bff76343SAlan Cox 	if (err == 0) {
471bff76343SAlan Cox 		if (*rtaddress != -1)
472bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
473efc68ce1SDavid Greenman 		if (run) {
474efc68ce1SDavid Greenman 			*run += 1;
475efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
476efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
477efc68ce1SDavid Greenman 		}
478efc68ce1SDavid Greenman 	}
47926f9a767SRodney W. Grimes 
480bff76343SAlan Cox 	return (err);
48126f9a767SRodney W. Grimes }
48226f9a767SRodney W. Grimes 
48326f9a767SRodney W. Grimes /*
48426f9a767SRodney W. Grimes  * small block filesystem vnode pager input
48526f9a767SRodney W. Grimes  */
486f708ef1bSPoul-Henning Kamp static int
4877ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
48826f9a767SRodney W. Grimes {
4899c83534dSPoul-Henning Kamp 	struct vnode *vp;
4909c83534dSPoul-Henning Kamp 	struct bufobj *bo;
49126f9a767SRodney W. Grimes 	struct buf *bp;
4929e0ddbd0SAlan Cox 	struct sf_buf *sf;
493f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
49426f9a767SRodney W. Grimes 	vm_offset_t bsize;
495561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
496561cc9fcSKonstantin Belousov 	int error, i;
49726f9a767SRodney W. Grimes 
498561cc9fcSKonstantin Belousov 	error = 0;
49924a1cce3SDavid Greenman 	vp = object->handle;
500b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5012c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5022c4488fcSJohn Dyson 
50326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5040bdb7528SDavid Greenman 
5059c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
50626f9a767SRodney W. Grimes 
5079e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
50826f9a767SRodney W. Grimes 
50926f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
51033c67741SMatthew Dillon 		vm_ooffset_t address;
511bbc0ec52SDavid Greenman 
5120d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5130d53a17bSAlan Cox 		if (m->valid & bits)
51426f9a767SRodney W. Grimes 			continue;
51526f9a767SRodney W. Grimes 
51633c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
51733c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
51833c67741SMatthew Dillon 			fileaddr = -1;
51933c67741SMatthew Dillon 		} else {
520bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
521bff76343SAlan Cox 			if (error)
522bff76343SAlan Cox 				break;
52333c67741SMatthew Dillon 		}
52426f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5251c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
52626f9a767SRodney W. Grimes 
52726f9a767SRodney W. Grimes 			/* build a minimal buffer header */
52821144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5296a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
530bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
531bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
532a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
533a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5349e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
535187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5369c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
5371faacf5dSKirk McKusick 			bp->b_vp = vp;
53826f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
53926f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5402b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5415bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
54226f9a767SRodney W. Grimes 
54326f9a767SRodney W. Grimes 			/* do the input */
5442c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
545b792bebeSPoul-Henning Kamp 			bstrategy(bp);
54626f9a767SRodney W. Grimes 
5476a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5486a4b5823SPoul-Henning Kamp 
549c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
55026f9a767SRodney W. Grimes 				error = EIO;
55126f9a767SRodney W. Grimes 
55226f9a767SRodney W. Grimes 			/*
55326f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
55426f9a767SRodney W. Grimes 			 */
5551faacf5dSKirk McKusick 			bp->b_vp = NULL;
5569c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5571c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
55826f9a767SRodney W. Grimes 			if (error)
55926f9a767SRodney W. Grimes 				break;
5600d53a17bSAlan Cox 		} else
5619e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
5620d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
5630d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
56489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5650d53a17bSAlan Cox 		m->valid |= bits;
56689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
56726f9a767SRodney W. Grimes 	}
5689e0ddbd0SAlan Cox 	sf_buf_free(sf);
56926f9a767SRodney W. Grimes 	if (error) {
570a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
57126f9a767SRodney W. Grimes 	}
57226f9a767SRodney W. Grimes 	return VM_PAGER_OK;
57326f9a767SRodney W. Grimes }
57426f9a767SRodney W. Grimes 
57526f9a767SRodney W. Grimes /*
576475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
57726f9a767SRodney W. Grimes  */
578f708ef1bSPoul-Henning Kamp static int
5797ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
58026f9a767SRodney W. Grimes {
581df8bae1dSRodney W. Grimes 	struct uio auio;
582df8bae1dSRodney W. Grimes 	struct iovec aiov;
58326f9a767SRodney W. Grimes 	int error;
58426f9a767SRodney W. Grimes 	int size;
5859e0ddbd0SAlan Cox 	struct sf_buf *sf;
586342a1480SJohn Baldwin 	struct vnode *vp;
587df8bae1dSRodney W. Grimes 
58889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
58926f9a767SRodney W. Grimes 	error = 0;
590bbc0ec52SDavid Greenman 
591df8bae1dSRodney W. Grimes 	/*
59226f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
59326f9a767SRodney W. Grimes 	 */
594a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
59526f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
59626f9a767SRodney W. Grimes 	} else {
59726f9a767SRodney W. Grimes 		size = PAGE_SIZE;
598a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
599a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
60052051abcSAlan Cox 		vp = object->handle;
60189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
6020bdb7528SDavid Greenman 
60326f9a767SRodney W. Grimes 		/*
604df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
605df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
606df8bae1dSRodney W. Grimes 		 */
6079e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6080bdb7528SDavid Greenman 
6099e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
610df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
611df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
612df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
613a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
614df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
61526f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
616df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
617b40ce416SJulian Elischer 		auio.uio_td = curthread;
61826f9a767SRodney W. Grimes 
619a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
620df8bae1dSRodney W. Grimes 		if (!error) {
62154d92145SMatthew Dillon 			int count = size - auio.uio_resid;
622df8bae1dSRodney W. Grimes 
623df8bae1dSRodney W. Grimes 			if (count == 0)
624df8bae1dSRodney W. Grimes 				error = EINVAL;
62526f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6269e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6279e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
628df8bae1dSRodney W. Grimes 		}
6299e0ddbd0SAlan Cox 		sf_buf_free(sf);
6301b26eb10SAlan Cox 
63189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
632df8bae1dSRodney W. Grimes 	}
6330d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
6346e3a3f38SRobert V. Baron 	if (!error)
6356e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
636a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
63726f9a767SRodney W. Grimes }
63826f9a767SRodney W. Grimes 
63926f9a767SRodney W. Grimes /*
64026f9a767SRodney W. Grimes  * generic vnode pager input routine
64126f9a767SRodney W. Grimes  */
642170db9c6SJohn Dyson 
643ce75f2c3SMike Smith /*
64423955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
64547e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
64647e151ddSRobert Drehmel  * to implement the previous behaviour.
647ce75f2c3SMike Smith  *
648ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
649ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
650ce75f2c3SMike Smith  */
651f708ef1bSPoul-Henning Kamp static int
6527ebba1f8SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
65324a1cce3SDavid Greenman {
654170db9c6SJohn Dyson 	int rtval;
655170db9c6SJohn Dyson 	struct vnode *vp;
65686ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
65795e5e988SJohn Dyson 
658170db9c6SJohn Dyson 	vp = object->handle;
65989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
66027ad26d8SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage);
66123955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
66223955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
66389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
664170db9c6SJohn Dyson 	return rtval;
665170db9c6SJohn Dyson }
666170db9c6SJohn Dyson 
667ce75f2c3SMike Smith /*
668*d15b55c5SKonstantin Belousov  * The implementation of VOP_GETPAGES() for local filesystems, where
669*d15b55c5SKonstantin Belousov  * partially valid pages can only occur at the end of file.
670*d15b55c5SKonstantin Belousov  */
671*d15b55c5SKonstantin Belousov int
672*d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
673*d15b55c5SKonstantin Belousov {
674*d15b55c5SKonstantin Belousov 	vm_page_t mreq;
675*d15b55c5SKonstantin Belousov 
676*d15b55c5SKonstantin Belousov 	mreq = ap->a_m[ap->a_reqpage];
677*d15b55c5SKonstantin Belousov 
678*d15b55c5SKonstantin Belousov 	/*
679*d15b55c5SKonstantin Belousov 	 * Since the caller has busied the requested page, that page's valid
680*d15b55c5SKonstantin Belousov 	 * field will not be changed by other threads.
681*d15b55c5SKonstantin Belousov 	 */
682*d15b55c5SKonstantin Belousov 	vm_page_assert_xbusied(mreq);
683*d15b55c5SKonstantin Belousov 
684*d15b55c5SKonstantin Belousov 	/*
685*d15b55c5SKonstantin Belousov 	 * The requested page has valid blocks.  Invalid part can only
686*d15b55c5SKonstantin Belousov 	 * exist at the end of file, and the page is made fully valid
687*d15b55c5SKonstantin Belousov 	 * by zeroing in vm_pager_getpages().  Free non-requested
688*d15b55c5SKonstantin Belousov 	 * pages, since no i/o is done to read its content.
689*d15b55c5SKonstantin Belousov 	 */
690*d15b55c5SKonstantin Belousov 	if (mreq->valid != 0) {
691*d15b55c5SKonstantin Belousov 		vm_pager_free_nonreq(mreq->object, ap->a_m, ap->a_reqpage,
692*d15b55c5SKonstantin Belousov 		    round_page(ap->a_count) / PAGE_SIZE);
693*d15b55c5SKonstantin Belousov 		return (VM_PAGER_OK);
694*d15b55c5SKonstantin Belousov 	}
695*d15b55c5SKonstantin Belousov 
696*d15b55c5SKonstantin Belousov 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
697*d15b55c5SKonstantin Belousov 	    ap->a_count, ap->a_reqpage));
698*d15b55c5SKonstantin Belousov }
699*d15b55c5SKonstantin Belousov 
700*d15b55c5SKonstantin Belousov /*
701ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
702ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
703ce75f2c3SMike Smith  */
704ce75f2c3SMike Smith int
7057ebba1f8SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount,
7067ebba1f8SGleb Smirnoff     int reqpage)
707170db9c6SJohn Dyson {
708ce75f2c3SMike Smith 	vm_object_t object;
709a316d390SJohn Dyson 	vm_offset_t kva;
7108f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
711f3aad9a6SBjoern A. Zeeb 	int i, j, size, bsize, first;
712f4f83da0SAlan Cox 	daddr_t firstaddr, reqblock;
7139c83534dSPoul-Henning Kamp 	struct bufobj *bo;
714efc68ce1SDavid Greenman 	int runpg;
715efc68ce1SDavid Greenman 	int runend;
7160bdb7528SDavid Greenman 	struct buf *bp;
7176ce697dcSKonstantin Belousov 	struct mount *mp;
718ce75f2c3SMike Smith 	int count;
7191de11f1aSAlan Cox 	int error;
72026f9a767SRodney W. Grimes 
721ce75f2c3SMike Smith 	object = vp->v_object;
722ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
723ce75f2c3SMike Smith 
7249c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
7259c83534dSPoul-Henning Kamp 	    ("vnode_pager_generic_getpages does not support devices"));
726b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
7272c4488fcSJohn Dyson 		return VM_PAGER_BAD;
7282c4488fcSJohn Dyson 
72926f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
73026f9a767SRodney W. Grimes 
73126f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
732bbc0ec52SDavid Greenman 
73326f9a767SRodney W. Grimes 	/*
734bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
735bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
73626f9a767SRodney W. Grimes 	 */
737a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
738bbc0ec52SDavid Greenman 
73926f9a767SRodney W. Grimes 	/*
74016f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
74126f9a767SRodney W. Grimes 	 */
7421de11f1aSAlan Cox 	error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
7431de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
74489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
7452965a453SKip Macy 
746e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7472965a453SKip Macy 			if (i != reqpage) {
7482965a453SKip Macy 				vm_page_lock(m[i]);
749d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7502965a453SKip Macy 				vm_page_unlock(m[i]);
7512965a453SKip Macy 			}
752b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodein);
753b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodepgsin);
75452051abcSAlan Cox 		error = vnode_pager_input_old(object, m[reqpage]);
75589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
75652051abcSAlan Cox 		return (error);
7571de11f1aSAlan Cox 	} else if (error != 0) {
758396b3e34SAlan Cox 		vm_pager_free_nonreq(object, m, reqpage, count);
7591de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
760bbc0ec52SDavid Greenman 
76126f9a767SRodney W. Grimes 		/*
76226f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
76326f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
76426f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
76526f9a767SRodney W. Grimes 		 */
76626f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
767500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
768396b3e34SAlan Cox 		vm_pager_free_nonreq(object, m, reqpage, count);
769b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodein);
770b4b70819SAttilio Rao 		PCPU_INC(cnt.v_vnodepgsin);
77124a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
77226f9a767SRodney W. Grimes 	}
7738d17e694SJulian Elischer 
77426f9a767SRodney W. Grimes 	/*
7758d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
7768d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
7778d17e694SJulian Elischer 	 * media.
7780d94caffSDavid Greenman 	 */
77989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
7808b575f6cSAlan Cox 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
781e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7822965a453SKip Macy 			if (i != reqpage) {
7832965a453SKip Macy 				vm_page_lock(m[i]);
784d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
7852965a453SKip Macy 				vm_page_unlock(m[i]);
7862965a453SKip Macy 			}
78789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
7880d94caffSDavid Greenman 		return VM_PAGER_OK;
789f4f83da0SAlan Cox 	} else if (reqblock == -1) {
790f4f83da0SAlan Cox 		pmap_zero_page(m[reqpage]);
79112aa4fdcSAlan Cox 		KASSERT(m[reqpage]->dirty == 0,
79212aa4fdcSAlan Cox 		    ("vnode_pager_generic_getpages: page %p is dirty", m));
793f4f83da0SAlan Cox 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
794f4f83da0SAlan Cox 		for (i = 0; i < count; i++)
7952965a453SKip Macy 			if (i != reqpage) {
7962965a453SKip Macy 				vm_page_lock(m[i]);
797f4f83da0SAlan Cox 				vm_page_free(m[i]);
7982965a453SKip Macy 				vm_page_unlock(m[i]);
7992965a453SKip Macy 			}
80089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
801f4f83da0SAlan Cox 		return (VM_PAGER_OK);
8020d94caffSDavid Greenman 	}
8038d17e694SJulian Elischer 	m[reqpage]->valid = 0;
80489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
8050bdb7528SDavid Greenman 
8060d94caffSDavid Greenman 	/*
80726f9a767SRodney W. Grimes 	 * here on direct device I/O
80826f9a767SRodney W. Grimes 	 */
809efc68ce1SDavid Greenman 	firstaddr = -1;
810a1287949SEivind Eklund 
81126f9a767SRodney W. Grimes 	/*
812efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
81326f9a767SRodney W. Grimes 	 */
814efc68ce1SDavid Greenman 	for (first = 0, i = 0; i < count; i = runend) {
815bff76343SAlan Cox 		if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
816bff76343SAlan Cox 		    &runpg) != 0) {
81789f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
818bff76343SAlan Cox 			for (; i < count; i++)
8192965a453SKip Macy 				if (i != reqpage) {
8202965a453SKip Macy 					vm_page_lock(m[i]);
821bff76343SAlan Cox 					vm_page_free(m[i]);
8222965a453SKip Macy 					vm_page_unlock(m[i]);
8232965a453SKip Macy 				}
82489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
825bff76343SAlan Cox 			return (VM_PAGER_ERROR);
826bff76343SAlan Cox 		}
827efc68ce1SDavid Greenman 		if (firstaddr == -1) {
82889f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
82924a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
830f3aad9a6SBjoern A. Zeeb 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
831f3aad9a6SBjoern A. Zeeb 				    (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
832bf1001faSMaxime Henrion 				    (uintmax_t)foff,
833bf1001faSMaxime Henrion 				    (uintmax_t)
834fc62ef1fSBruce Evans 				    (object->un_pager.vnp.vnp_size >> 32),
835bf1001faSMaxime Henrion 				    (uintmax_t)object->un_pager.vnp.vnp_size);
836efc68ce1SDavid Greenman 			}
8372965a453SKip Macy 			vm_page_lock(m[i]);
838d8d5fa88SAlfred Perlstein 			vm_page_free(m[i]);
8392965a453SKip Macy 			vm_page_unlock(m[i]);
84089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
841efc68ce1SDavid Greenman 			runend = i + 1;
842efc68ce1SDavid Greenman 			first = runend;
843efc68ce1SDavid Greenman 			continue;
844efc68ce1SDavid Greenman 		}
845efc68ce1SDavid Greenman 		runend = i + runpg;
846efc68ce1SDavid Greenman 		if (runend <= reqpage) {
84789f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
8482965a453SKip Macy 			for (j = i; j < runend; j++) {
8492965a453SKip Macy 				vm_page_lock(m[j]);
850d8d5fa88SAlfred Perlstein 				vm_page_free(m[j]);
8512965a453SKip Macy 				vm_page_unlock(m[j]);
8522965a453SKip Macy 			}
85389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
85426f9a767SRodney W. Grimes 		} else {
855efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
85689f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
8572965a453SKip Macy 				for (i = first + runpg; i < count; i++) {
8582965a453SKip Macy 					vm_page_lock(m[i]);
859d8d5fa88SAlfred Perlstein 					vm_page_free(m[i]);
8602965a453SKip Macy 					vm_page_unlock(m[i]);
8612965a453SKip Macy 				}
86289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
863efc68ce1SDavid Greenman 				count = first + runpg;
86426f9a767SRodney W. Grimes 			}
865efc68ce1SDavid Greenman 			break;
86626f9a767SRodney W. Grimes 		}
867efc68ce1SDavid Greenman 		first = runend;
868efc68ce1SDavid Greenman 	}
86926f9a767SRodney W. Grimes 
87026f9a767SRodney W. Grimes 	/*
871bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
872bbc0ec52SDavid Greenman 	 * to be zero based...
87326f9a767SRodney W. Grimes 	 */
87426f9a767SRodney W. Grimes 	if (first != 0) {
8757e2393ffSAlan Cox 		m += first;
87626f9a767SRodney W. Grimes 		count -= first;
87726f9a767SRodney W. Grimes 		reqpage -= first;
87826f9a767SRodney W. Grimes 	}
879efc68ce1SDavid Greenman 
88026f9a767SRodney W. Grimes 	/*
88126f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
88226f9a767SRodney W. Grimes 	 */
883a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
88426f9a767SRodney W. Grimes 
88526f9a767SRodney W. Grimes 	/*
88626f9a767SRodney W. Grimes 	 * calculate the size of the transfer
88726f9a767SRodney W. Grimes 	 */
88826f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
8891a31a6c3SPoul-Henning Kamp 	KASSERT(count > 0, ("zero count"));
89024a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
89124a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
8921a31a6c3SPoul-Henning Kamp 	KASSERT(size > 0, ("zero size"));
89326f9a767SRodney W. Grimes 
89426f9a767SRodney W. Grimes 	/*
89524579ca1SMatthew Dillon 	 * round up physical size for real devices.
89626f9a767SRodney W. Grimes 	 */
8979c83534dSPoul-Henning Kamp 	if (1) {
8989c83534dSPoul-Henning Kamp 		int secmask = bo->bo_bsize - 1;
8996229cc50SPoul-Henning Kamp 		KASSERT(secmask < PAGE_SIZE && secmask > 0,
9006229cc50SPoul-Henning Kamp 		    ("vnode_pager_generic_getpages: sector size %d too large",
9016229cc50SPoul-Henning Kamp 		    secmask + 1));
90224579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
90324579ca1SMatthew Dillon 	}
90426f9a767SRodney W. Grimes 
9051c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
90616f62314SDavid Greenman 	kva = (vm_offset_t)bp->b_data;
90716f62314SDavid Greenman 
90826f9a767SRodney W. Grimes 	/*
9096ce697dcSKonstantin Belousov 	 * and map the pages to be read into the kva, if the filesystem
9106ce697dcSKonstantin Belousov 	 * requires mapped buffers.
91126f9a767SRodney W. Grimes 	 */
9126ce697dcSKonstantin Belousov 	mp = vp->v_mount;
9136ce697dcSKonstantin Belousov 	if (mp != NULL && (mp->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
9146ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
9156ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
9166ce697dcSKonstantin Belousov 		bp->b_kvabase = unmapped_buf;
9176ce697dcSKonstantin Belousov 		bp->b_offset = 0;
9186ce697dcSKonstantin Belousov 		bp->b_flags |= B_UNMAPPED;
9196ce697dcSKonstantin Belousov 		bp->b_npages = count;
9206ce697dcSKonstantin Belousov 		for (i = 0; i < count; i++)
9216ce697dcSKonstantin Belousov 			bp->b_pages[i] = m[i];
9226ce697dcSKonstantin Belousov 	} else
92316f62314SDavid Greenman 		pmap_qenter(kva, m, count);
92426f9a767SRodney W. Grimes 
92526f9a767SRodney W. Grimes 	/* build a minimal buffer header */
92621144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
9276a4b5823SPoul-Henning Kamp 	bp->b_iodone = bdone;
928bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
929bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
930a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
931a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
932187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
9339c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
9341faacf5dSKirk McKusick 	bp->b_vp = vp;
93526f9a767SRodney W. Grimes 	bp->b_bcount = size;
93626f9a767SRodney W. Grimes 	bp->b_bufsize = size;
9372b6b0df7SMatthew Dillon 	bp->b_runningbufspace = bp->b_bufsize;
9385bd65606SJohn Baldwin 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
93926f9a767SRodney W. Grimes 
940b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodein);
941b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsin, count);
942976e77fcSDavid Greenman 
94326f9a767SRodney W. Grimes 	/* do the input */
9442c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
945b792bebeSPoul-Henning Kamp 	bstrategy(bp);
946976e77fcSDavid Greenman 
9476a4b5823SPoul-Henning Kamp 	bwait(bp, PVM, "vnread");
94826f9a767SRodney W. Grimes 
949c244d2deSPoul-Henning Kamp 	if ((bp->b_ioflags & BIO_ERROR) != 0)
95026f9a767SRodney W. Grimes 		error = EIO;
95126f9a767SRodney W. Grimes 
9526991ee13SKonstantin Belousov 	if (error == 0 && size != count * PAGE_SIZE) {
9536ce697dcSKonstantin Belousov 		if ((bp->b_flags & B_UNMAPPED) != 0) {
9546ce697dcSKonstantin Belousov 			bp->b_flags &= ~B_UNMAPPED;
9556ce697dcSKonstantin Belousov 			pmap_qenter(kva, m, count);
9566ce697dcSKonstantin Belousov 		}
95726f9a767SRodney W. Grimes 		bzero((caddr_t)kva + size, PAGE_SIZE * count - size);
95826f9a767SRodney W. Grimes 	}
9596ce697dcSKonstantin Belousov 	if ((bp->b_flags & B_UNMAPPED) == 0)
96016f62314SDavid Greenman 		pmap_qremove(kva, count);
9616ce697dcSKonstantin Belousov 	if (mp != NULL && (mp->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0) {
9626ce697dcSKonstantin Belousov 		bp->b_data = (caddr_t)kva;
9636ce697dcSKonstantin Belousov 		bp->b_kvabase = (caddr_t)kva;
9646ce697dcSKonstantin Belousov 		bp->b_flags &= ~B_UNMAPPED;
9656ce697dcSKonstantin Belousov 		for (i = 0; i < count; i++)
9666ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
9676ce697dcSKonstantin Belousov 	}
96826f9a767SRodney W. Grimes 
96926f9a767SRodney W. Grimes 	/*
97026f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
97126f9a767SRodney W. Grimes 	 */
9721faacf5dSKirk McKusick 	bp->b_vp = NULL;
9739c83534dSPoul-Henning Kamp 	pbrelbo(bp);
9741c7c3c6aSMatthew Dillon 	relpbuf(bp, &vnode_pbuf_freecnt);
97526f9a767SRodney W. Grimes 
97689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
9778f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
9788f9110f6SJohn Dyson 		vm_page_t mt;
9798f9110f6SJohn Dyson 
9808f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
9818f9110f6SJohn Dyson 		mt = m[i];
9828f9110f6SJohn Dyson 
98354746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
9848d17e694SJulian Elischer 			/*
9858d17e694SJulian Elischer 			 * Read filled up entire page.
9868d17e694SJulian Elischer 			 */
9878f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
988016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
989016a3c93SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is dirty",
990016a3c93SAlan Cox 			    mt));
991016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
992016a3c93SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is mapped",
993016a3c93SAlan Cox 			    mt));
9948f9110f6SJohn Dyson 		} else {
9958d17e694SJulian Elischer 			/*
99642eb4108SAlan Cox 			 * Read did not fill up entire page.
9978d17e694SJulian Elischer 			 *
9988d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
9998d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
10008d17e694SJulian Elischer 			 * read.
10018d17e694SJulian Elischer 			 */
1002dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
100354746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
100442eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
100542eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
100642eb4108SAlan Cox 			    ("vnode_pager_generic_getpages: page %p is dirty",
100742eb4108SAlan Cox 			    mt));
10088f9110f6SJohn Dyson 		}
10098f9110f6SJohn Dyson 
10100055cbd3SKonstantin Belousov 		if (i != reqpage)
1011b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
101203679e23SAlan Cox 	}
101389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
101426f9a767SRodney W. Grimes 	if (error) {
101524a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
101626f9a767SRodney W. Grimes 	}
1017a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
101826f9a767SRodney W. Grimes }
101926f9a767SRodney W. Grimes 
1020ce75f2c3SMike Smith /*
1021ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1022ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1023ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1024ce75f2c3SMike Smith  *
1025ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1026ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1027ce75f2c3SMike Smith  */
1028e4542174SMatthew Dillon static void
10297ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
103033cad9e9SKonstantin Belousov     int flags, int *rtvals)
1031170db9c6SJohn Dyson {
1032170db9c6SJohn Dyson 	int rtval;
1033170db9c6SJohn Dyson 	struct vnode *vp;
103486ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1035ad980522SJohn Dyson 
10360e3cdf2cSAlan Cox 	/*
10370e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
10380e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
10390e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
10400e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
10410e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
10420e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
10430e3cdf2cSAlan Cox 	 *
10440e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
10450e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
10460e3cdf2cSAlan Cox 	 */
10470e3cdf2cSAlan Cox 
104833cad9e9SKonstantin Belousov 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count <
104944f1c916SBryan Drewery 	    vm_cnt.v_pageout_free_min)
105033cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
10510e3cdf2cSAlan Cox 
10520e3cdf2cSAlan Cox 	/*
10530e3cdf2cSAlan Cox 	 * Call device-specific putpages function
10540e3cdf2cSAlan Cox 	 */
1055170db9c6SJohn Dyson 	vp = object->handle;
105689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
105733cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
105823955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
105923955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
106089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1061170db9c6SJohn Dyson }
1062170db9c6SJohn Dyson 
1063ce75f2c3SMike Smith 
106426f9a767SRodney W. Grimes /*
1065ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
10664491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
10672b6b0df7SMatthew Dillon  *
10682b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
10692b6b0df7SMatthew Dillon  * clustering has already typically occured, so in general we ask the
10702b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
10712b6b0df7SMatthew Dillon  * then delayed.
107226f9a767SRodney W. Grimes  */
1073ce75f2c3SMike Smith int
1074c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1075c46b90e9SAlan Cox     int flags, int *rtvals)
107626f9a767SRodney W. Grimes {
1077f6b04d2bSDavid Greenman 	int i;
1078ce75f2c3SMike Smith 	vm_object_t object;
1079c46b90e9SAlan Cox 	vm_page_t m;
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;
1088dd498befSPaul Saab 	int ppscheck = 0;
1089dd498befSPaul Saab 	static struct timeval lastfail;
1090dd498befSPaul Saab 	static int curfail;
109126f9a767SRodney W. Grimes 
1092ce75f2c3SMike Smith 	object = vp->v_object;
1093ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1094ce75f2c3SMike Smith 
109526f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1096031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
109726f9a767SRodney W. Grimes 
1098c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
109923562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1100c46b90e9SAlan Cox 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1101f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1102f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
11030d94caffSDavid Greenman 	}
11040bdb7528SDavid Greenman 
1105f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1106f6b04d2bSDavid Greenman 	ncount = count;
110726f9a767SRodney W. Grimes 
1108c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
110900a6f47fSMatthew Dillon 
111000a6f47fSMatthew Dillon 	/*
111100a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
111200a6f47fSMatthew Dillon 	 * have to invalidate pages occuring beyond the file EOF.  However,
111300a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
111400a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
111500a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
111600a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
111700a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
11183ebeaf59SMatthew Dillon 	 *
11193ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
11203ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
112100a6f47fSMatthew Dillon 	 */
112289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1123a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
112400a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
112500a6f47fSMatthew Dillon 			int pgoff;
112600a6f47fSMatthew Dillon 
1127a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1128aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
112900a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1130c46b90e9SAlan Cox 				/*
1131c46b90e9SAlan Cox 				 * If the object is locked and the following
1132c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1133c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1134c46b90e9SAlan Cox 				 * pmap operation.
1135c46b90e9SAlan Cox 				 */
1136c46b90e9SAlan Cox 				m = ma[ncount - 1];
1137c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
11386031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1139c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1140c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1141c46b90e9SAlan Cox 				    pgoff);
114200a6f47fSMatthew Dillon 			}
114300a6f47fSMatthew Dillon 		} else {
114400a6f47fSMatthew Dillon 			maxsize = 0;
114500a6f47fSMatthew Dillon 			ncount = 0;
114600a6f47fSMatthew Dillon 		}
1147f6b04d2bSDavid Greenman 		if (ncount < count) {
1148f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1149f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1150f6b04d2bSDavid Greenman 			}
1151f6b04d2bSDavid Greenman 		}
1152f6b04d2bSDavid Greenman 	}
115389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
115426f9a767SRodney W. Grimes 
11552b6b0df7SMatthew Dillon 	/*
11562b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
11572b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
115843b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
115943b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
116043b7990eSMatthew Dillon 	 * the system decides how to cluster.
11612b6b0df7SMatthew Dillon 	 */
11628f9110f6SJohn Dyson 	ioflags = IO_VMIO;
116343b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
116443b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
116543b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
116643b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
11678f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
116843b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1169f6b04d2bSDavid Greenman 
1170f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1171f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1172f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1173f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1174a316d390SJohn Dyson 	auio.uio_offset = poffset;
1175f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1176f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1177f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1178b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1179a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1180b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodeout);
1181b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1182f6b04d2bSDavid Greenman 
1183f6b04d2bSDavid Greenman 	if (error) {
1184dd498befSPaul Saab 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
118524a1cce3SDavid Greenman 			printf("vnode_pager_putpages: I/O error %d\n", error);
1186f6b04d2bSDavid Greenman 	}
1187f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1188dd498befSPaul Saab 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
11899f80ce04SKonstantin Belousov 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1190c46b90e9SAlan Cox 			    auio.uio_resid, (u_long)ma[0]->pindex);
119126f9a767SRodney W. Grimes 	}
1192ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
119326f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
119426f9a767SRodney W. Grimes 	}
1195f6b04d2bSDavid Greenman 	return rtvals[0];
119626f9a767SRodney W. Grimes }
1197031ec8c1SKonstantin Belousov 
1198031ec8c1SKonstantin Belousov void
1199031ec8c1SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1200031ec8c1SKonstantin Belousov {
12019d17da3bSKonstantin Belousov 	vm_object_t obj;
1202031ec8c1SKonstantin Belousov 	int i, pos;
1203031ec8c1SKonstantin Belousov 
12049d17da3bSKonstantin Belousov 	if (written == 0)
12059d17da3bSKonstantin Belousov 		return;
12069d17da3bSKonstantin Belousov 	obj = ma[0]->object;
120789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1208031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1209031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1210031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1211031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1212031ec8c1SKonstantin Belousov 		} else {
1213031ec8c1SKonstantin Belousov 			/* Partially written page. */
1214031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1215031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1216031ec8c1SKonstantin Belousov 		}
1217031ec8c1SKonstantin Belousov 	}
121889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1219031ec8c1SKonstantin Belousov }
122084110e7eSKonstantin Belousov 
122184110e7eSKonstantin Belousov void
122284110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
122384110e7eSKonstantin Belousov     vm_offset_t end)
122484110e7eSKonstantin Belousov {
122584110e7eSKonstantin Belousov 	struct vnode *vp;
122684110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
122784110e7eSKonstantin Belousov 
122889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
122984110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
123089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
123184110e7eSKonstantin Belousov 		return;
123284110e7eSKonstantin Belousov 	}
123384110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
123484110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
123584110e7eSKonstantin Belousov 	vp = object->handle;
123684110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
123784110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1238140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, 1);
1239b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1240b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
124184110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
124284110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1243140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
1244b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1245b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
124684110e7eSKonstantin Belousov 	}
124789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
124884110e7eSKonstantin Belousov }
124984110e7eSKonstantin Belousov 
125084110e7eSKonstantin Belousov void
125184110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
125284110e7eSKonstantin Belousov     vm_offset_t end)
125384110e7eSKonstantin Belousov {
125484110e7eSKonstantin Belousov 	struct vnode *vp;
125584110e7eSKonstantin Belousov 	struct mount *mp;
125684110e7eSKonstantin Belousov 	vm_offset_t inc;
125784110e7eSKonstantin Belousov 
125889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
125984110e7eSKonstantin Belousov 
126084110e7eSKonstantin Belousov 	/*
126184110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
126284110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
126384110e7eSKonstantin Belousov 	 */
126484110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
126589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
126684110e7eSKonstantin Belousov 		return;
126784110e7eSKonstantin Belousov 	}
126884110e7eSKonstantin Belousov 
126984110e7eSKonstantin Belousov 	/*
127084110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
127184110e7eSKonstantin Belousov 	 * zero.
127284110e7eSKonstantin Belousov 	 */
127384110e7eSKonstantin Belousov 	inc = end - start;
127484110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
127584110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
127689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
127784110e7eSKonstantin Belousov 		return;
127884110e7eSKonstantin Belousov 	}
127984110e7eSKonstantin Belousov 
128084110e7eSKonstantin Belousov 	vp = object->handle;
128184110e7eSKonstantin Belousov 	vhold(vp);
128289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
128384110e7eSKonstantin Belousov 	mp = NULL;
128484110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
128584110e7eSKonstantin Belousov 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
128684110e7eSKonstantin Belousov 
128784110e7eSKonstantin Belousov 	/*
128884110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
128984110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
129084110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
129184110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
129284110e7eSKonstantin Belousov 	 */
129384110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
129484110e7eSKonstantin Belousov 	VOP_UNLOCK(vp, 0);
129584110e7eSKonstantin Belousov 	vdrop(vp);
129684110e7eSKonstantin Belousov 	if (mp != NULL)
129784110e7eSKonstantin Belousov 		vn_finished_write(mp);
129884110e7eSKonstantin Belousov }
1299