xref: /freebsd/sys/vm/vnode_pager.c (revision d07a6d3f6177239bbc47a24131f87738c1dfb1c3)
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 
153df8bae1dSRodney W. Grimes /*
154df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
155df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
156990ab7adSAlan Cox  *
157990ab7adSAlan Cox  * MPSAFE
158df8bae1dSRodney W. Grimes  */
15924a1cce3SDavid Greenman vm_object_t
1606cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
161b9dcd593SBruce Evans 		  vm_ooffset_t offset)
162df8bae1dSRodney W. Grimes {
16306cb7259SDavid Greenman 	vm_object_t object;
164df8bae1dSRodney W. Grimes 	struct vnode *vp;
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes 	/*
167df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
168df8bae1dSRodney W. Grimes 	 */
169df8bae1dSRodney W. Grimes 	if (handle == NULL)
170df8bae1dSRodney W. Grimes 		return (NULL);
171df8bae1dSRodney W. Grimes 
172df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
17339d38f93SDavid Greenman 
17463e7e60dSJeff Roberson 	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
17563e7e60dSJeff Roberson 
17639d38f93SDavid Greenman 	/*
17739d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
17839d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
17939d38f93SDavid Greenman 	 */
180e6e370a7SJeff Roberson 	VI_LOCK(vp);
181e6e370a7SJeff Roberson 	while (vp->v_iflag & VI_OLOCK) {
182e6e370a7SJeff Roberson 		vp->v_iflag |= VI_OWANT;
183e6e370a7SJeff Roberson 		msleep(vp, VI_MTX(vp), PVM, "vnpobj", 0);
18439d38f93SDavid Greenman 	}
185e6e370a7SJeff Roberson 	vp->v_iflag |= VI_OLOCK;
186e6e370a7SJeff Roberson 	VI_UNLOCK(vp);
18739d38f93SDavid Greenman 
18839d38f93SDavid Greenman 	/*
18939d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
19039d38f93SDavid Greenman 	 * go away.
19139d38f93SDavid Greenman 	 */
1921ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
1931ca58953SAlan Cox 		VM_OBJECT_LOCK(object);
1941ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
1951ca58953SAlan Cox 			break;
19619187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
1971ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
19824a1cce3SDavid Greenman 	}
1990d94caffSDavid Greenman 
2002be70f79SJohn Dyson 	if (vp->v_usecount == 0)
2012be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
2022be70f79SJohn Dyson 
20324a1cce3SDavid Greenman 	if (object == NULL) {
204df8bae1dSRodney W. Grimes 		/*
205df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
206df8bae1dSRodney W. Grimes 		 */
2076cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
208bbc0ec52SDavid Greenman 
2096cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
21026f9a767SRodney W. Grimes 
21124a1cce3SDavid Greenman 		object->handle = handle;
21224a1cce3SDavid Greenman 		vp->v_object = object;
213df8bae1dSRodney W. Grimes 	} else {
21495e5e988SJohn Dyson 		object->ref_count++;
2151ca58953SAlan Cox 		VM_OBJECT_UNLOCK(object);
216df8bae1dSRodney W. Grimes 	}
217e6e370a7SJeff Roberson 	VI_LOCK(vp);
218990ab7adSAlan Cox 	vp->v_usecount++;
219e6e370a7SJeff Roberson 	vp->v_iflag &= ~VI_OLOCK;
220e6e370a7SJeff Roberson 	if (vp->v_iflag & VI_OWANT) {
221e6e370a7SJeff Roberson 		vp->v_iflag &= ~VI_OWANT;
22239d38f93SDavid Greenman 		wakeup(vp);
22339d38f93SDavid Greenman 	}
224e6e370a7SJeff Roberson 	VI_UNLOCK(vp);
22524a1cce3SDavid Greenman 	return (object);
226df8bae1dSRodney W. Grimes }
227df8bae1dSRodney W. Grimes 
228658ad5ffSAlan Cox /*
229658ad5ffSAlan Cox  *	The object must be locked.
230658ad5ffSAlan Cox  */
231f708ef1bSPoul-Henning Kamp static void
23224a1cce3SDavid Greenman vnode_pager_dealloc(object)
2330d94caffSDavid Greenman 	vm_object_t object;
23424a1cce3SDavid Greenman {
23554d92145SMatthew Dillon 	struct vnode *vp = object->handle;
236df8bae1dSRodney W. Grimes 
23724a1cce3SDavid Greenman 	if (vp == NULL)
23824a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
23924a1cce3SDavid Greenman 
240658ad5ffSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
24166095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
24224a1cce3SDavid Greenman 
24324a1cce3SDavid Greenman 	object->handle = NULL;
24495461b45SJohn Dyson 	object->type = OBJT_DEAD;
24519187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
24619187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
24719187819SAlan Cox 		wakeup(object);
24819187819SAlan Cox 	}
249e6e370a7SJeff Roberson 	ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc");
250aa2cabb9SDavid Greenman 	vp->v_object = NULL;
25135764be3SPoul-Henning Kamp 	vp->v_vflag &= ~VV_TEXT;
252df8bae1dSRodney W. Grimes }
25326f9a767SRodney W. Grimes 
254f708ef1bSPoul-Henning Kamp static boolean_t
255a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
25624a1cce3SDavid Greenman 	vm_object_t object;
257a316d390SJohn Dyson 	vm_pindex_t pindex;
25824a1cce3SDavid Greenman 	int *before;
25924a1cce3SDavid Greenman 	int *after;
260df8bae1dSRodney W. Grimes {
26124a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
26298b0c789SPoul-Henning Kamp 	daddr_t bn;
2633af76890SPoul-Henning Kamp 	int err;
264170db9c6SJohn Dyson 	daddr_t reqblock;
2652c4488fcSJohn Dyson 	int poff;
2662c4488fcSJohn Dyson 	int bsize;
267d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
268ae51ff11SJeff Roberson 	int vfslocked;
269df8bae1dSRodney W. Grimes 
270f29ba63eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
27124579ca1SMatthew Dillon 	/*
27224579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
27324579ca1SMatthew Dillon 	 * have the page.
27424579ca1SMatthew Dillon 	 */
275e6e370a7SJeff Roberson 	if (vp == NULL)
27647221757SJohn Dyson 		return FALSE;
27747221757SJohn Dyson 
27863e7e60dSJeff Roberson 	VI_LOCK(vp);
27963e7e60dSJeff Roberson 	if (vp->v_iflag & VI_DOOMED) {
28063e7e60dSJeff Roberson 		VI_UNLOCK(vp);
281e6e370a7SJeff Roberson 		return FALSE;
28263e7e60dSJeff Roberson 	}
28363e7e60dSJeff Roberson 	VI_UNLOCK(vp);
284df8bae1dSRodney W. Grimes 	/*
2850d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
2860d94caffSDavid Greenman 	 * not have the page.
287df8bae1dSRodney W. Grimes 	 */
288a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
289a316d390SJohn Dyson 	    (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2904abc71c0SDavid Greenman 		return FALSE;
291df8bae1dSRodney W. Grimes 
292eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
293170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
294d63596ceSJohn Dyson 	blocksperpage = 0;
295d63596ceSJohn Dyson 	if (pagesperblock > 0) {
296a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
297d63596ceSJohn Dyson 	} else {
298d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
299d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
300d63596ceSJohn Dyson 	}
301f29ba63eSAlan Cox 	VM_OBJECT_UNLOCK(object);
302ae51ff11SJeff Roberson 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
303ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
304ae51ff11SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
305f29ba63eSAlan Cox 	VM_OBJECT_LOCK(object);
3060d94caffSDavid Greenman 	if (err)
30724a1cce3SDavid Greenman 		return TRUE;
3086eab77f2SJohn Dyson 	if (bn == -1)
309ced399eeSJohn Dyson 		return FALSE;
310d63596ceSJohn Dyson 	if (pagesperblock > 0) {
311a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
312170db9c6SJohn Dyson 		if (before) {
313170db9c6SJohn Dyson 			*before *= pagesperblock;
314170db9c6SJohn Dyson 			*before += poff;
315170db9c6SJohn Dyson 		}
316170db9c6SJohn Dyson 		if (after) {
317b1fc01b7SJohn Dyson 			int numafter;
318170db9c6SJohn Dyson 			*after *= pagesperblock;
319b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
32047e151ddSRobert Drehmel 			if (IDX_TO_OFF(pindex + numafter) >
32147e151ddSRobert Drehmel 			    object->un_pager.vnp.vnp_size) {
32247e151ddSRobert Drehmel 				numafter =
32347e151ddSRobert Drehmel 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
32447e151ddSRobert Drehmel 				    pindex;
325b1fc01b7SJohn Dyson 			}
326b1fc01b7SJohn Dyson 			*after += numafter;
327170db9c6SJohn Dyson 		}
328d63596ceSJohn Dyson 	} else {
329d63596ceSJohn Dyson 		if (before) {
330d63596ceSJohn Dyson 			*before /= blocksperpage;
331d63596ceSJohn Dyson 		}
332d63596ceSJohn Dyson 
333d63596ceSJohn Dyson 		if (after) {
334d63596ceSJohn Dyson 			*after /= blocksperpage;
335d63596ceSJohn Dyson 		}
336d63596ceSJohn Dyson 	}
337ced399eeSJohn Dyson 	return TRUE;
338df8bae1dSRodney W. Grimes }
339df8bae1dSRodney W. Grimes 
340df8bae1dSRodney W. Grimes /*
341df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
34224a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
343df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
344df8bae1dSRodney W. Grimes  *
345df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
346df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
347df8bae1dSRodney W. Grimes  */
348df8bae1dSRodney W. Grimes void
349df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
350df8bae1dSRodney W. Grimes 	struct vnode *vp;
351a316d390SJohn Dyson 	vm_ooffset_t nsize;
352df8bae1dSRodney W. Grimes {
3532a8f9ab5SAlan Cox 	vm_object_t object;
3542a8f9ab5SAlan Cox 	vm_page_t m;
355c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
356df8bae1dSRodney W. Grimes 
3572a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
358df8bae1dSRodney W. Grimes 		return;
3592a8f9ab5SAlan Cox 	VM_OBJECT_LOCK(object);
3602a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
361df8bae1dSRodney W. Grimes 		/*
362df8bae1dSRodney W. Grimes 		 * Hasn't changed size
363df8bae1dSRodney W. Grimes 		 */
3642a8f9ab5SAlan Cox 		VM_OBJECT_UNLOCK(object);
365df8bae1dSRodney W. Grimes 		return;
3662a8f9ab5SAlan Cox 	}
367c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
3682a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
369df8bae1dSRodney W. Grimes 		/*
370bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
371df8bae1dSRodney W. Grimes 		 */
3722a8f9ab5SAlan Cox 		if (nobjsize < object->size)
373c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
374c576d121SLuoqi Chen 			    FALSE);
375bbc0ec52SDavid Greenman 		/*
376bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
3773ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
3783ebeaf59SMatthew Dillon 		 *
3793ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
3803ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
3813ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
382bbc0ec52SDavid Greenman 		 */
3832a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
3841b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
3851b26eb10SAlan Cox 		    m->valid != 0) {
3862b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
3872b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
3882b6b0df7SMatthew Dillon 
3892b6b0df7SMatthew Dillon 			/*
3902b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
3912b6b0df7SMatthew Dillon 			 * the page has been mapped.
3922b6b0df7SMatthew Dillon 			 */
393fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
3942b6b0df7SMatthew Dillon 
3952b6b0df7SMatthew Dillon 			/*
3963ebeaf59SMatthew Dillon 			 * XXX work around SMP data integrity race
3973ebeaf59SMatthew Dillon 			 * by unmapping the page from user processes.
3983ebeaf59SMatthew Dillon 			 * The garbage we just cleared may be mapped
3993ebeaf59SMatthew Dillon 			 * to a user process running on another cpu
4003ebeaf59SMatthew Dillon 			 * and this code is not running through normal
4013ebeaf59SMatthew Dillon 			 * I/O channels which handle SMP issues for
4023ebeaf59SMatthew Dillon 			 * us, so unmap page to synchronize all cpus.
4033ebeaf59SMatthew Dillon 			 *
4043ebeaf59SMatthew Dillon 			 * XXX should vm_pager_unmap_page() have
4053ebeaf59SMatthew Dillon 			 * dealt with this?
4063ebeaf59SMatthew Dillon 			 */
4071b26eb10SAlan Cox 			vm_page_lock_queues();
4084fec79beSAlan Cox 			pmap_remove_all(m);
4093ebeaf59SMatthew Dillon 
4103ebeaf59SMatthew Dillon 			/*
4112b6b0df7SMatthew Dillon 			 * Clear out partial-page dirty bits.  This
4122b6b0df7SMatthew Dillon 			 * has the side effect of setting the valid
4132b6b0df7SMatthew Dillon 			 * bits, but that is ok.  There are a bunch
4142b6b0df7SMatthew Dillon 			 * of places in the VM system where we expected
4152b6b0df7SMatthew Dillon 			 * m->dirty == VM_PAGE_BITS_ALL.  The file EOF
4162b6b0df7SMatthew Dillon 			 * case is one of them.  If the page is still
4172b6b0df7SMatthew Dillon 			 * partially dirty, make it fully dirty.
4183ebeaf59SMatthew Dillon 			 *
4193ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4203ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4213ebeaf59SMatthew Dillon 			 * replacement from working properly.
4222b6b0df7SMatthew Dillon 			 */
4232b6b0df7SMatthew Dillon 			vm_page_set_validclean(m, base, size);
4242b6b0df7SMatthew Dillon 			if (m->dirty != 0)
4252b6b0df7SMatthew Dillon 				m->dirty = VM_PAGE_BITS_ALL;
4262a8f9ab5SAlan Cox 			vm_page_unlock_queues();
427bbc0ec52SDavid Greenman 		}
428bbc0ec52SDavid Greenman 	}
429a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
430c576d121SLuoqi Chen 	object->size = nobjsize;
4312a8f9ab5SAlan Cox 	VM_OBJECT_UNLOCK(object);
432df8bae1dSRodney W. Grimes }
433df8bae1dSRodney W. Grimes 
43426f9a767SRodney W. Grimes /*
43526f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
43626f9a767SRodney W. Grimes  * file address
43726f9a767SRodney W. Grimes  */
438f708ef1bSPoul-Henning Kamp static vm_offset_t
439efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
44026f9a767SRodney W. Grimes 	struct vnode *vp;
441a316d390SJohn Dyson 	vm_ooffset_t address;
442efc68ce1SDavid Greenman 	int *run;
44326f9a767SRodney W. Grimes {
44426f9a767SRodney W. Grimes 	int rtaddress;
44526f9a767SRodney W. Grimes 	int bsize;
44698b0c789SPoul-Henning Kamp 	daddr_t block;
44726f9a767SRodney W. Grimes 	int err;
448a316d390SJohn Dyson 	daddr_t vblock;
449a316d390SJohn Dyson 	int voffset;
45026f9a767SRodney W. Grimes 
4512ad036b6SAlan Cox 	if (address < 0)
4520d94caffSDavid Greenman 		return -1;
4530d94caffSDavid Greenman 
4542c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4552c4488fcSJohn Dyson 		return -1;
4562c4488fcSJohn Dyson 
45726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
45826f9a767SRodney W. Grimes 	vblock = address / bsize;
45926f9a767SRodney W. Grimes 	voffset = address % bsize;
46026f9a767SRodney W. Grimes 
461ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, vblock, NULL, &block, run, NULL);
46226f9a767SRodney W. Grimes 
463efc68ce1SDavid Greenman 	if (err || (block == -1))
46426f9a767SRodney W. Grimes 		rtaddress = -1;
465efc68ce1SDavid Greenman 	else {
466187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
467efc68ce1SDavid Greenman 		if (run) {
468efc68ce1SDavid Greenman 			*run += 1;
469efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
470efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
471efc68ce1SDavid Greenman 		}
472efc68ce1SDavid Greenman 	}
47326f9a767SRodney W. Grimes 
47426f9a767SRodney W. Grimes 	return rtaddress;
47526f9a767SRodney W. Grimes }
47626f9a767SRodney W. Grimes 
47726f9a767SRodney W. Grimes /*
47826f9a767SRodney W. Grimes  * small block filesystem vnode pager input
47926f9a767SRodney W. Grimes  */
480f708ef1bSPoul-Henning Kamp static int
48124a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
48224a1cce3SDavid Greenman 	vm_object_t object;
48326f9a767SRodney W. Grimes 	vm_page_t m;
48426f9a767SRodney W. Grimes {
48526f9a767SRodney W. Grimes 	int i;
4869c83534dSPoul-Henning Kamp 	struct vnode *vp;
4879c83534dSPoul-Henning Kamp 	struct bufobj *bo;
48826f9a767SRodney W. Grimes 	struct buf *bp;
4899e0ddbd0SAlan Cox 	struct sf_buf *sf;
49026f9a767SRodney W. Grimes 	int fileaddr;
49126f9a767SRodney W. Grimes 	vm_offset_t bsize;
49226f9a767SRodney W. Grimes 	int error = 0;
49326f9a767SRodney W. Grimes 
49424a1cce3SDavid Greenman 	vp = object->handle;
4952c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4962c4488fcSJohn Dyson 		return VM_PAGER_BAD;
4972c4488fcSJohn Dyson 
49826f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
4990bdb7528SDavid Greenman 
5009c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
50126f9a767SRodney W. Grimes 
5029e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
50326f9a767SRodney W. Grimes 
50426f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
50533c67741SMatthew Dillon 		vm_ooffset_t address;
506bbc0ec52SDavid Greenman 
507897a45efSDmitrij Tejblum 		if (vm_page_bits(i * bsize, bsize) & m->valid)
50826f9a767SRodney W. Grimes 			continue;
50926f9a767SRodney W. Grimes 
51033c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
51133c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
51233c67741SMatthew Dillon 			fileaddr = -1;
51333c67741SMatthew Dillon 		} else {
51433c67741SMatthew Dillon 			fileaddr = vnode_pager_addr(vp, address, NULL);
51533c67741SMatthew Dillon 		}
51626f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5171c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
51826f9a767SRodney W. Grimes 
51926f9a767SRodney W. Grimes 			/* build a minimal buffer header */
52021144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5216a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
522bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
523bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
524a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
525a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5269e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
527187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5289c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
52926f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
53026f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5312b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5322b6b0df7SMatthew Dillon 			runningbufspace += bp->b_runningbufspace;
53326f9a767SRodney W. Grimes 
53426f9a767SRodney W. Grimes 			/* do the input */
5352c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
536b792bebeSPoul-Henning Kamp 			bstrategy(bp);
53726f9a767SRodney W. Grimes 
538e47ed70bSJohn Dyson 			/* we definitely need to be at splvm here */
53926f9a767SRodney W. Grimes 
5406a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5416a4b5823SPoul-Henning Kamp 
542c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
54326f9a767SRodney W. Grimes 				error = EIO;
54426f9a767SRodney W. Grimes 
54526f9a767SRodney W. Grimes 			/*
54626f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
54726f9a767SRodney W. Grimes 			 */
5489c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5491c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
55026f9a767SRodney W. Grimes 			if (error)
55126f9a767SRodney W. Grimes 				break;
5520d94caffSDavid Greenman 
5532bf43e43SAlan Cox 			VM_OBJECT_LOCK(object);
554178949e0SAlan Cox 			vm_page_lock_queues();
555aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
556178949e0SAlan Cox 			vm_page_unlock_queues();
5572bf43e43SAlan Cox 			VM_OBJECT_UNLOCK(object);
55826f9a767SRodney W. Grimes 		} else {
5592bf43e43SAlan Cox 			VM_OBJECT_LOCK(object);
560178949e0SAlan Cox 			vm_page_lock_queues();
561aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
562178949e0SAlan Cox 			vm_page_unlock_queues();
5632bf43e43SAlan Cox 			VM_OBJECT_UNLOCK(object);
5649e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
56526f9a767SRodney W. Grimes 		}
56626f9a767SRodney W. Grimes 	}
5679e0ddbd0SAlan Cox 	sf_buf_free(sf);
56885e01243SAlan Cox 	vm_page_lock_queues();
5690385347cSPeter Wemm 	pmap_clear_modify(m);
57085e01243SAlan Cox 	vm_page_unlock_queues();
57126f9a767SRodney W. Grimes 	if (error) {
572a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
57326f9a767SRodney W. Grimes 	}
57426f9a767SRodney W. Grimes 	return VM_PAGER_OK;
57526f9a767SRodney W. Grimes 
57626f9a767SRodney W. Grimes }
57726f9a767SRodney W. Grimes 
57826f9a767SRodney W. Grimes 
57926f9a767SRodney W. Grimes /*
580475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
58126f9a767SRodney W. Grimes  */
582f708ef1bSPoul-Henning Kamp static int
58324a1cce3SDavid Greenman vnode_pager_input_old(object, m)
58424a1cce3SDavid Greenman 	vm_object_t object;
58526f9a767SRodney W. Grimes 	vm_page_t m;
58626f9a767SRodney W. Grimes {
587df8bae1dSRodney W. Grimes 	struct uio auio;
588df8bae1dSRodney W. Grimes 	struct iovec aiov;
58926f9a767SRodney W. Grimes 	int error;
59026f9a767SRodney W. Grimes 	int size;
5919e0ddbd0SAlan Cox 	struct sf_buf *sf;
592342a1480SJohn Baldwin 	struct vnode *vp;
593df8bae1dSRodney W. Grimes 
59452051abcSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
59526f9a767SRodney W. Grimes 	error = 0;
596bbc0ec52SDavid Greenman 
597df8bae1dSRodney W. Grimes 	/*
59826f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
59926f9a767SRodney W. Grimes 	 */
600a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
60126f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
60226f9a767SRodney W. Grimes 	} else {
60326f9a767SRodney W. Grimes 		size = PAGE_SIZE;
604a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
605a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
60652051abcSAlan Cox 		vp = object->handle;
60752051abcSAlan Cox 		VM_OBJECT_UNLOCK(object);
6080bdb7528SDavid Greenman 
60926f9a767SRodney W. Grimes 		/*
610df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
611df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
612df8bae1dSRodney W. Grimes 		 */
6139e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6140bdb7528SDavid Greenman 
6159e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
616df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
617df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
618df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
619a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
620df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
62126f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
622df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
623b40ce416SJulian Elischer 		auio.uio_td = curthread;
62426f9a767SRodney W. Grimes 
625a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
626df8bae1dSRodney W. Grimes 		if (!error) {
62754d92145SMatthew Dillon 			int count = size - auio.uio_resid;
628df8bae1dSRodney W. Grimes 
629df8bae1dSRodney W. Grimes 			if (count == 0)
630df8bae1dSRodney W. Grimes 				error = EINVAL;
63126f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6329e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6339e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
634df8bae1dSRodney W. Grimes 		}
6359e0ddbd0SAlan Cox 		sf_buf_free(sf);
6361b26eb10SAlan Cox 
6371b26eb10SAlan Cox 		VM_OBJECT_LOCK(object);
638df8bae1dSRodney W. Grimes 	}
63985e01243SAlan Cox 	vm_page_lock_queues();
6400385347cSPeter Wemm 	pmap_clear_modify(m);
6412c28a105SAlan Cox 	vm_page_undirty(m);
6421b26eb10SAlan Cox 	vm_page_unlock_queues();
6436e3a3f38SRobert V. Baron 	if (!error)
6446e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
645a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
64626f9a767SRodney W. Grimes }
64726f9a767SRodney W. Grimes 
64826f9a767SRodney W. Grimes /*
64926f9a767SRodney W. Grimes  * generic vnode pager input routine
65026f9a767SRodney W. Grimes  */
651170db9c6SJohn Dyson 
652ce75f2c3SMike Smith /*
65323955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
65447e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
65547e151ddSRobert Drehmel  * to implement the previous behaviour.
656ce75f2c3SMike Smith  *
657ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
658ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
659ce75f2c3SMike Smith  */
660f708ef1bSPoul-Henning Kamp static int
66124a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
66226f9a767SRodney W. Grimes 	vm_object_t object;
66324a1cce3SDavid Greenman 	vm_page_t *m;
66424a1cce3SDavid Greenman 	int count;
66524a1cce3SDavid Greenman 	int reqpage;
66624a1cce3SDavid Greenman {
667170db9c6SJohn Dyson 	int rtval;
668170db9c6SJohn Dyson 	struct vnode *vp;
66986ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
670ae51ff11SJeff Roberson 	int vfslocked;
67195e5e988SJohn Dyson 
672170db9c6SJohn Dyson 	vp = object->handle;
6738630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
674ae51ff11SJeff Roberson 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
67586ffbd76SMike Smith 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
67623955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
67723955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
678ae51ff11SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
6798630c117SAlan Cox 	VM_OBJECT_LOCK(object);
680170db9c6SJohn Dyson 	return rtval;
681170db9c6SJohn Dyson }
682170db9c6SJohn Dyson 
683ce75f2c3SMike Smith /*
684ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
685ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
686ce75f2c3SMike Smith  */
687ce75f2c3SMike Smith int
688ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
689ce75f2c3SMike Smith 	struct vnode *vp;
690170db9c6SJohn Dyson 	vm_page_t *m;
691ce75f2c3SMike Smith 	int bytecount;
692170db9c6SJohn Dyson 	int reqpage;
693170db9c6SJohn Dyson {
694ce75f2c3SMike Smith 	vm_object_t object;
695a316d390SJohn Dyson 	vm_offset_t kva;
6968f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
697e43c2eabSAlan Cox 	int i, j, size, bsize, first, firstaddr;
6989c83534dSPoul-Henning Kamp 	struct bufobj *bo;
699efc68ce1SDavid Greenman 	int runpg;
700efc68ce1SDavid Greenman 	int runend;
7010bdb7528SDavid Greenman 	struct buf *bp;
702ce75f2c3SMike Smith 	int count;
70326f9a767SRodney W. Grimes 	int error = 0;
70426f9a767SRodney W. Grimes 
705ce75f2c3SMike Smith 	object = vp->v_object;
706ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
707ce75f2c3SMike Smith 
7089c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
7099c83534dSPoul-Henning Kamp 	    ("vnode_pager_generic_getpages does not support devices"));
7102c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
7112c4488fcSJohn Dyson 		return VM_PAGER_BAD;
7122c4488fcSJohn Dyson 
71326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
71426f9a767SRodney W. Grimes 
71526f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
716bbc0ec52SDavid Greenman 
71726f9a767SRodney W. Grimes 	/*
718bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
719bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
72026f9a767SRodney W. Grimes 	 */
721a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
722bbc0ec52SDavid Greenman 
72326f9a767SRodney W. Grimes 	/*
72416f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
72526f9a767SRodney W. Grimes 	 */
7269c83534dSPoul-Henning Kamp 	if (VOP_BMAP(vp, 0, &bo, 0, NULL, NULL)) {
72731953be9SAlan Cox 		VM_OBJECT_LOCK(object);
728e43c2eabSAlan Cox 		vm_page_lock_queues();
729e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
730e43c2eabSAlan Cox 			if (i != reqpage)
731d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
732e43c2eabSAlan Cox 		vm_page_unlock_queues();
733976e77fcSDavid Greenman 		cnt.v_vnodein++;
734976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
73552051abcSAlan Cox 		error = vnode_pager_input_old(object, m[reqpage]);
73652051abcSAlan Cox 		VM_OBJECT_UNLOCK(object);
73752051abcSAlan Cox 		return (error);
738bbc0ec52SDavid Greenman 
73926f9a767SRodney W. Grimes 		/*
74026f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
74126f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
74226f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
74326f9a767SRodney W. Grimes 		 */
74426f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
745500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
74631953be9SAlan Cox 		VM_OBJECT_LOCK(object);
747e43c2eabSAlan Cox 		vm_page_lock_queues();
748e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
749e43c2eabSAlan Cox 			if (i != reqpage)
750d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
751e43c2eabSAlan Cox 		vm_page_unlock_queues();
75231953be9SAlan Cox 		VM_OBJECT_UNLOCK(object);
753976e77fcSDavid Greenman 		cnt.v_vnodein++;
754976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
75524a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
75626f9a767SRodney W. Grimes 	}
7578d17e694SJulian Elischer 
75826f9a767SRodney W. Grimes 	/*
7598d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
7608d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
7618d17e694SJulian Elischer 	 * media.
7620d94caffSDavid Greenman 	 */
76331953be9SAlan Cox 	VM_OBJECT_LOCK(object);
7648b575f6cSAlan Cox 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
765e43c2eabSAlan Cox 		vm_page_lock_queues();
766e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7670d94caffSDavid Greenman 			if (i != reqpage)
768d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
769e43c2eabSAlan Cox 		vm_page_unlock_queues();
77031953be9SAlan Cox 		VM_OBJECT_UNLOCK(object);
7710d94caffSDavid Greenman 		return VM_PAGER_OK;
7720d94caffSDavid Greenman 	}
7738d17e694SJulian Elischer 	m[reqpage]->valid = 0;
7748b575f6cSAlan Cox 	VM_OBJECT_UNLOCK(object);
7750bdb7528SDavid Greenman 
7760d94caffSDavid Greenman 	/*
77726f9a767SRodney W. Grimes 	 * here on direct device I/O
77826f9a767SRodney W. Grimes 	 */
779efc68ce1SDavid Greenman 	firstaddr = -1;
780a1287949SEivind Eklund 
78126f9a767SRodney W. Grimes 	/*
782efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
78326f9a767SRodney W. Grimes 	 */
784efc68ce1SDavid Greenman 	for (first = 0, i = 0; i < count; i = runend) {
785a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
786a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
787efc68ce1SDavid Greenman 		if (firstaddr == -1) {
78831953be9SAlan Cox 			VM_OBJECT_LOCK(object);
78924a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
790bf1001faSMaxime Henrion 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
791bf1001faSMaxime Henrion 				    firstaddr, (uintmax_t)(foff >> 32),
792bf1001faSMaxime Henrion 				    (uintmax_t)foff,
793bf1001faSMaxime Henrion 				    (uintmax_t)
794fc62ef1fSBruce Evans 				    (object->un_pager.vnp.vnp_size >> 32),
795bf1001faSMaxime Henrion 				    (uintmax_t)object->un_pager.vnp.vnp_size);
796efc68ce1SDavid Greenman 			}
797e43c2eabSAlan Cox 			vm_page_lock_queues();
798d8d5fa88SAlfred Perlstein 			vm_page_free(m[i]);
799e43c2eabSAlan Cox 			vm_page_unlock_queues();
80031953be9SAlan Cox 			VM_OBJECT_UNLOCK(object);
801efc68ce1SDavid Greenman 			runend = i + 1;
802efc68ce1SDavid Greenman 			first = runend;
803efc68ce1SDavid Greenman 			continue;
804efc68ce1SDavid Greenman 		}
805efc68ce1SDavid Greenman 		runend = i + runpg;
806efc68ce1SDavid Greenman 		if (runend <= reqpage) {
80731953be9SAlan Cox 			VM_OBJECT_LOCK(object);
808e43c2eabSAlan Cox 			vm_page_lock_queues();
809e43c2eabSAlan Cox 			for (j = i; j < runend; j++)
810d8d5fa88SAlfred Perlstein 				vm_page_free(m[j]);
811e43c2eabSAlan Cox 			vm_page_unlock_queues();
81231953be9SAlan Cox 			VM_OBJECT_UNLOCK(object);
81326f9a767SRodney W. Grimes 		} else {
814efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
81531953be9SAlan Cox 				VM_OBJECT_LOCK(object);
816e43c2eabSAlan Cox 				vm_page_lock_queues();
817efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
818d8d5fa88SAlfred Perlstein 					vm_page_free(m[i]);
819e43c2eabSAlan Cox 				vm_page_unlock_queues();
82031953be9SAlan Cox 				VM_OBJECT_UNLOCK(object);
821efc68ce1SDavid Greenman 				count = first + runpg;
82226f9a767SRodney W. Grimes 			}
823efc68ce1SDavid Greenman 			break;
82426f9a767SRodney W. Grimes 		}
825efc68ce1SDavid Greenman 		first = runend;
826efc68ce1SDavid Greenman 	}
82726f9a767SRodney W. Grimes 
82826f9a767SRodney W. Grimes 	/*
829bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
830bbc0ec52SDavid Greenman 	 * to be zero based...
83126f9a767SRodney W. Grimes 	 */
83226f9a767SRodney W. Grimes 	if (first != 0) {
83326f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
83426f9a767SRodney W. Grimes 			m[i - first] = m[i];
83526f9a767SRodney W. Grimes 		}
83626f9a767SRodney W. Grimes 		count -= first;
83726f9a767SRodney W. Grimes 		reqpage -= first;
83826f9a767SRodney W. Grimes 	}
839efc68ce1SDavid Greenman 
84026f9a767SRodney W. Grimes 	/*
84126f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
84226f9a767SRodney W. Grimes 	 */
843a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
84426f9a767SRodney W. Grimes 
84526f9a767SRodney W. Grimes 	/*
84626f9a767SRodney W. Grimes 	 * calculate the size of the transfer
84726f9a767SRodney W. Grimes 	 */
84826f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
8491a31a6c3SPoul-Henning Kamp 	KASSERT(count > 0, ("zero count"));
85024a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
85124a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
8521a31a6c3SPoul-Henning Kamp 	KASSERT(size > 0, ("zero size"));
85326f9a767SRodney W. Grimes 
85426f9a767SRodney W. Grimes 	/*
85524579ca1SMatthew Dillon 	 * round up physical size for real devices.
85626f9a767SRodney W. Grimes 	 */
8579c83534dSPoul-Henning Kamp 	if (1) {
8589c83534dSPoul-Henning Kamp 		int secmask = bo->bo_bsize - 1;
8596229cc50SPoul-Henning Kamp 		KASSERT(secmask < PAGE_SIZE && secmask > 0,
8606229cc50SPoul-Henning Kamp 		    ("vnode_pager_generic_getpages: sector size %d too large",
8616229cc50SPoul-Henning Kamp 		    secmask + 1));
86224579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
86324579ca1SMatthew Dillon 	}
86426f9a767SRodney W. Grimes 
8651c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
86616f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
86716f62314SDavid Greenman 
86826f9a767SRodney W. Grimes 	/*
86926f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
87026f9a767SRodney W. Grimes 	 */
87116f62314SDavid Greenman 	pmap_qenter(kva, m, count);
87226f9a767SRodney W. Grimes 
87326f9a767SRodney W. Grimes 	/* build a minimal buffer header */
87421144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
8756a4b5823SPoul-Henning Kamp 	bp->b_iodone = bdone;
876bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
877bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
878a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
879a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
880187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
8819c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
88226f9a767SRodney W. Grimes 	bp->b_bcount = size;
88326f9a767SRodney W. Grimes 	bp->b_bufsize = size;
8842b6b0df7SMatthew Dillon 	bp->b_runningbufspace = bp->b_bufsize;
8852b6b0df7SMatthew Dillon 	runningbufspace += bp->b_runningbufspace;
88626f9a767SRodney W. Grimes 
887976e77fcSDavid Greenman 	cnt.v_vnodein++;
888976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
889976e77fcSDavid Greenman 
89026f9a767SRodney W. Grimes 	/* do the input */
8912c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
892b792bebeSPoul-Henning Kamp 	bstrategy(bp);
893976e77fcSDavid Greenman 
8946a4b5823SPoul-Henning Kamp 	bwait(bp, PVM, "vnread");
89526f9a767SRodney W. Grimes 
896c244d2deSPoul-Henning Kamp 	if ((bp->b_ioflags & BIO_ERROR) != 0)
89726f9a767SRodney W. Grimes 		error = EIO;
89826f9a767SRodney W. Grimes 
89926f9a767SRodney W. Grimes 	if (!error) {
90026f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
90126f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
90226f9a767SRodney W. Grimes 	}
90316f62314SDavid Greenman 	pmap_qremove(kva, count);
90426f9a767SRodney W. Grimes 
90526f9a767SRodney W. Grimes 	/*
90626f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
90726f9a767SRodney W. Grimes 	 */
9089c83534dSPoul-Henning Kamp 	pbrelbo(bp);
9091c7c3c6aSMatthew Dillon 	relpbuf(bp, &vnode_pbuf_freecnt);
91026f9a767SRodney W. Grimes 
91131953be9SAlan Cox 	VM_OBJECT_LOCK(object);
9129d522888SAlan Cox 	vm_page_lock_queues();
9138f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
9148f9110f6SJohn Dyson 		vm_page_t mt;
9158f9110f6SJohn Dyson 
9168f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
9178f9110f6SJohn Dyson 		mt = m[i];
9188f9110f6SJohn Dyson 
91954746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
9208d17e694SJulian Elischer 			/*
9218d17e694SJulian Elischer 			 * Read filled up entire page.
9228d17e694SJulian Elischer 			 */
9238f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
9242c28a105SAlan Cox 			vm_page_undirty(mt);	/* should be an assert? XXX */
9250385347cSPeter Wemm 			pmap_clear_modify(mt);
9268f9110f6SJohn Dyson 		} else {
9278d17e694SJulian Elischer 			/*
9288d17e694SJulian Elischer 			 * Read did not fill up entire page.  Since this
9298d17e694SJulian Elischer 			 * is getpages, the page may be mapped, so we have
9308d17e694SJulian Elischer 			 * to zero the invalid portions of the page even
9318d17e694SJulian Elischer 			 * though we aren't setting them valid.
9328d17e694SJulian Elischer 			 *
9338d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
9348d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
9358d17e694SJulian Elischer 			 * read.
9368d17e694SJulian Elischer 			 */
93754746b67SDmitrij Tejblum 			vm_page_set_validclean(mt, 0,
93854746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
9394221e284SAlan Cox 			/* handled by vm_fault now */
9404221e284SAlan Cox 			/* vm_page_zero_invalid(mt, FALSE); */
9418f9110f6SJohn Dyson 		}
9428f9110f6SJohn Dyson 
94326f9a767SRodney W. Grimes 		if (i != reqpage) {
944bbc0ec52SDavid Greenman 
94526f9a767SRodney W. Grimes 			/*
946bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
947bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
948bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
949956f3135SPhilippe Charnier 			 * It appears that empirical results show that
950bbc0ec52SDavid Greenman 			 * deactivating pages is best.
95126f9a767SRodney W. Grimes 			 */
952bbc0ec52SDavid Greenman 
95326f9a767SRodney W. Grimes 			/*
954bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
955bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
95626f9a767SRodney W. Grimes 			 */
95726f9a767SRodney W. Grimes 			if (!error) {
9588f9110f6SJohn Dyson 				if (mt->flags & PG_WANTED)
9598f9110f6SJohn Dyson 					vm_page_activate(mt);
96095461b45SJohn Dyson 				else
9618f9110f6SJohn Dyson 					vm_page_deactivate(mt);
962e69763a3SDoug Rabson 				vm_page_wakeup(mt);
96326f9a767SRodney W. Grimes 			} else {
964d8d5fa88SAlfred Perlstein 				vm_page_free(mt);
96526f9a767SRodney W. Grimes 			}
96626f9a767SRodney W. Grimes 		}
96726f9a767SRodney W. Grimes 	}
9689d522888SAlan Cox 	vm_page_unlock_queues();
96931953be9SAlan Cox 	VM_OBJECT_UNLOCK(object);
97026f9a767SRodney W. Grimes 	if (error) {
97124a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
97226f9a767SRodney W. Grimes 	}
973a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
97426f9a767SRodney W. Grimes }
97526f9a767SRodney W. Grimes 
976ce75f2c3SMike Smith /*
977ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
978ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
979ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
980ce75f2c3SMike Smith  *
981ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
982ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
983ce75f2c3SMike Smith  */
984e4542174SMatthew Dillon static void
985170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
986170db9c6SJohn Dyson 	vm_object_t object;
987170db9c6SJohn Dyson 	vm_page_t *m;
988170db9c6SJohn Dyson 	int count;
989170db9c6SJohn Dyson 	boolean_t sync;
990170db9c6SJohn Dyson 	int *rtvals;
991170db9c6SJohn Dyson {
992170db9c6SJohn Dyson 	int rtval;
993170db9c6SJohn Dyson 	struct vnode *vp;
994f2a2857bSKirk McKusick 	struct mount *mp;
99586ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
996ad980522SJohn Dyson 
9970e3cdf2cSAlan Cox 	/*
9980e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
9990e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
10000e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
10010e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
10020e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
10030e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
10040e3cdf2cSAlan Cox 	 *
10050e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
10060e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
10070e3cdf2cSAlan Cox 	 */
10080e3cdf2cSAlan Cox 
10090e3cdf2cSAlan Cox 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
10100e3cdf2cSAlan Cox 		sync |= OBJPC_SYNC;
10110e3cdf2cSAlan Cox 
10120e3cdf2cSAlan Cox 	/*
10130e3cdf2cSAlan Cox 	 * Call device-specific putpages function
10140e3cdf2cSAlan Cox 	 */
1015170db9c6SJohn Dyson 	vp = object->handle;
10162e3b314dSAlan Cox 	VM_OBJECT_UNLOCK(object);
1017f2a2857bSKirk McKusick 	if (vp->v_type != VREG)
1018f2a2857bSKirk McKusick 		mp = NULL;
1019f2a2857bSKirk McKusick 	(void)vn_start_write(vp, &mp, V_WAIT);
102086ffbd76SMike Smith 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
102123955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
102223955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
1023f2a2857bSKirk McKusick 	vn_finished_write(mp);
10242e3b314dSAlan Cox 	VM_OBJECT_LOCK(object);
1025170db9c6SJohn Dyson }
1026170db9c6SJohn Dyson 
1027ce75f2c3SMike Smith 
102826f9a767SRodney W. Grimes /*
1029ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
10304491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
10312b6b0df7SMatthew Dillon  *
10322b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
10332b6b0df7SMatthew Dillon  * clustering has already typically occured, so in general we ask the
10342b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
10352b6b0df7SMatthew Dillon  * then delayed.
103626f9a767SRodney W. Grimes  */
1037ce75f2c3SMike Smith int
10388f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
1039ce75f2c3SMike Smith 	struct vnode *vp;
104026f9a767SRodney W. Grimes 	vm_page_t *m;
1041ce75f2c3SMike Smith 	int bytecount;
10428f9110f6SJohn Dyson 	int flags;
104326f9a767SRodney W. Grimes 	int *rtvals;
104426f9a767SRodney W. Grimes {
1045f6b04d2bSDavid Greenman 	int i;
1046ce75f2c3SMike Smith 	vm_object_t object;
1047ce75f2c3SMike Smith 	int count;
104826f9a767SRodney W. Grimes 
1049f6b04d2bSDavid Greenman 	int maxsize, ncount;
1050a316d390SJohn Dyson 	vm_ooffset_t poffset;
1051f6b04d2bSDavid Greenman 	struct uio auio;
1052f6b04d2bSDavid Greenman 	struct iovec aiov;
1053f6b04d2bSDavid Greenman 	int error;
10548f9110f6SJohn Dyson 	int ioflags;
105526f9a767SRodney W. Grimes 
1056ce75f2c3SMike Smith 	object = vp->v_object;
1057ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1058ce75f2c3SMike Smith 
105926f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
106026f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
106126f9a767SRodney W. Grimes 
1062d8fed1d0SAlan Cox 	if ((int64_t)m[0]->pindex < 0) {
106323562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
106423562e4bSMarcel Moolenaar 			(long)m[0]->pindex, (u_long)m[0]->dirty);
1065f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1066f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
10670d94caffSDavid Greenman 	}
10680bdb7528SDavid Greenman 
1069f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1070f6b04d2bSDavid Greenman 	ncount = count;
107126f9a767SRodney W. Grimes 
1072a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
107300a6f47fSMatthew Dillon 
107400a6f47fSMatthew Dillon 	/*
107500a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
107600a6f47fSMatthew Dillon 	 * have to invalidate pages occuring beyond the file EOF.  However,
107700a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
107800a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
107900a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
108000a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
108100a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
10823ebeaf59SMatthew Dillon 	 *
10833ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
10843ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
108500a6f47fSMatthew Dillon 	 */
1086a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
108700a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
108800a6f47fSMatthew Dillon 			int pgoff;
108900a6f47fSMatthew Dillon 
1090a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1091aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
109200a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1093b7ad744dSAlan Cox 				vm_page_lock_queues();
109400a6f47fSMatthew Dillon 				vm_page_clear_dirty(m[ncount - 1], pgoff,
109500a6f47fSMatthew Dillon 					PAGE_SIZE - pgoff);
1096b7ad744dSAlan Cox 				vm_page_unlock_queues();
109700a6f47fSMatthew Dillon 			}
109800a6f47fSMatthew Dillon 		} else {
109900a6f47fSMatthew Dillon 			maxsize = 0;
110000a6f47fSMatthew Dillon 			ncount = 0;
110100a6f47fSMatthew Dillon 		}
1102f6b04d2bSDavid Greenman 		if (ncount < count) {
1103f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1104f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1105f6b04d2bSDavid Greenman 			}
1106f6b04d2bSDavid Greenman 		}
1107f6b04d2bSDavid Greenman 	}
110826f9a767SRodney W. Grimes 
11092b6b0df7SMatthew Dillon 	/*
11102b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
11112b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
111243b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
111343b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
111443b7990eSMatthew Dillon 	 * the system decides how to cluster.
11152b6b0df7SMatthew Dillon 	 */
11168f9110f6SJohn Dyson 	ioflags = IO_VMIO;
111743b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
111843b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
111943b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
112043b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
11218f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
112243b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1123f6b04d2bSDavid Greenman 
1124f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1125f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1126f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1127f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1128a316d390SJohn Dyson 	auio.uio_offset = poffset;
1129f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1130f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1131f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1132b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1133a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1134976e77fcSDavid Greenman 	cnt.v_vnodeout++;
1135f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
1136f6b04d2bSDavid Greenman 
1137f6b04d2bSDavid Greenman 	if (error) {
113824a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
1139f6b04d2bSDavid Greenman 	}
1140f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1141ac1e407bSBruce Evans 		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1142ac1e407bSBruce Evans 		    auio.uio_resid, (u_long)m[0]->pindex);
114326f9a767SRodney W. Grimes 	}
1144ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
114526f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
114626f9a767SRodney W. Grimes 	}
1147f6b04d2bSDavid Greenman 	return rtvals[0];
114826f9a767SRodney W. Grimes }
1149f6b04d2bSDavid Greenman 
1150f6b04d2bSDavid Greenman struct vnode *
1151417a26a1SAlan Cox vnode_pager_lock(vm_object_t first_object)
115224a1cce3SDavid Greenman {
1153417a26a1SAlan Cox 	struct vnode *vp;
1154417a26a1SAlan Cox 	vm_object_t backing_object, object;
1155996c772fSJohn Dyson 
1156417a26a1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
1157417a26a1SAlan Cox 	for (object = first_object; object != NULL; object = backing_object) {
1158417a26a1SAlan Cox 		if (object->type != OBJT_VNODE) {
1159417a26a1SAlan Cox 			if ((backing_object = object->backing_object) != NULL)
1160417a26a1SAlan Cox 				VM_OBJECT_LOCK(backing_object);
1161417a26a1SAlan Cox 			if (object != first_object)
1162417a26a1SAlan Cox 				VM_OBJECT_UNLOCK(object);
1163f6b04d2bSDavid Greenman 			continue;
1164417a26a1SAlan Cox 		}
1165417a26a1SAlan Cox 	retry:
1166e6b961ffSJohn Baldwin 		if (object->flags & OBJ_DEAD) {
1167417a26a1SAlan Cox 			if (object != first_object)
1168417a26a1SAlan Cox 				VM_OBJECT_UNLOCK(object);
116947221757SJohn Dyson 			return NULL;
1170e6b961ffSJohn Baldwin 		}
1171417a26a1SAlan Cox 		vp = object->handle;
1172417a26a1SAlan Cox 		VI_LOCK(vp);
1173417a26a1SAlan Cox 		VM_OBJECT_UNLOCK(object);
1174417a26a1SAlan Cox 		if (first_object != object)
1175417a26a1SAlan Cox 			VM_OBJECT_UNLOCK(first_object);
1176417a26a1SAlan Cox 		if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | LK_NOPAUSE |
1177417a26a1SAlan Cox 		    LK_RETRY | LK_SHARED, curthread)) {
1178417a26a1SAlan Cox 			VM_OBJECT_LOCK(first_object);
1179417a26a1SAlan Cox 			if (object != first_object)
1180417a26a1SAlan Cox 				VM_OBJECT_LOCK(object);
1181417a26a1SAlan Cox 			if (object->type != OBJT_VNODE) {
1182417a26a1SAlan Cox 				if (object != first_object)
1183417a26a1SAlan Cox 					VM_OBJECT_UNLOCK(object);
1184bef608bdSJohn Dyson 				return NULL;
1185417a26a1SAlan Cox 			}
118647221757SJohn Dyson 			printf("vnode_pager_lock: retrying\n");
1187417a26a1SAlan Cox 			goto retry;
118847221757SJohn Dyson 		}
1189417a26a1SAlan Cox 		VM_OBJECT_LOCK(first_object);
1190417a26a1SAlan Cox 		return (vp);
119126f9a767SRodney W. Grimes 	}
119224a1cce3SDavid Greenman 	return NULL;
1193f6b04d2bSDavid Greenman }
1194