xref: /freebsd/sys/vm/vnode_pager.c (revision 2a8f9ab57fd16fcef1bae3436cfaef9f977a5b52)
1df8bae1dSRodney W. Grimes /*
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
41c3aac50fSPeter Wemm  * $FreeBSD$
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes /*
45df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
46df8bae1dSRodney W. Grimes  */
47df8bae1dSRodney W. Grimes 
4826f9a767SRodney W. Grimes /*
4926f9a767SRodney W. Grimes  * TODO:
5024a1cce3SDavid Greenman  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
51f6b04d2bSDavid Greenman  *	greatly re-simplify the vnode_pager.
5226f9a767SRodney W. Grimes  */
5326f9a767SRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <sys/param.h>
55df8bae1dSRodney W. Grimes #include <sys/systm.h>
56df8bae1dSRodney W. Grimes #include <sys/proc.h>
57df8bae1dSRodney W. Grimes #include <sys/vnode.h>
58df8bae1dSRodney W. Grimes #include <sys/mount.h>
599626b608SPoul-Henning Kamp #include <sys/bio.h>
6024a1cce3SDavid Greenman #include <sys/buf.h>
61efeaf95aSDavid Greenman #include <sys/vmmeter.h>
6224579ca1SMatthew Dillon #include <sys/conf.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <vm/vm.h>
65efeaf95aSDavid Greenman #include <vm/vm_object.h>
66df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
6724a1cce3SDavid Greenman #include <vm/vm_pager.h>
681efb74fbSJohn Dyson #include <vm/vm_map.h>
69df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
70efeaf95aSDavid Greenman #include <vm/vm_extern.h>
71df8bae1dSRodney W. Grimes 
7211caded3SAlfred Perlstein static void vnode_pager_init(void);
7311caded3SAlfred Perlstein static vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
7411caded3SAlfred Perlstein 					 int *run);
7511caded3SAlfred Perlstein static void vnode_pager_iodone(struct buf *bp);
7611caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
7711caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
7811caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
7911caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
8011caded3SAlfred Perlstein static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
8111caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
820b8253a7SBruce Evans 
83df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
84b62b9b64SJohn Baldwin 	vnode_pager_init,
85df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
86df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
8724a1cce3SDavid Greenman 	vnode_pager_getpages,
8824a1cce3SDavid Greenman 	vnode_pager_putpages,
8924a1cce3SDavid Greenman 	vnode_pager_haspage,
9024a1cce3SDavid Greenman 	NULL
91df8bae1dSRodney W. Grimes };
92df8bae1dSRodney W. Grimes 
93b62b9b64SJohn Baldwin int vnode_pbuf_freecnt;
941c7c3c6aSMatthew Dillon 
9537c84183SPoul-Henning Kamp static void
96b62b9b64SJohn Baldwin vnode_pager_init(void)
97b62b9b64SJohn Baldwin {
98b62b9b64SJohn Baldwin 
99b62b9b64SJohn Baldwin 	vnode_pbuf_freecnt = nswbuf / 2 + 1;
100b62b9b64SJohn Baldwin }
101170db9c6SJohn Dyson 
102df8bae1dSRodney W. Grimes /*
103df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
104df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
105990ab7adSAlan Cox  *
106990ab7adSAlan Cox  * MPSAFE
107df8bae1dSRodney W. Grimes  */
10824a1cce3SDavid Greenman vm_object_t
1096cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
110b9dcd593SBruce Evans 		  vm_ooffset_t offset)
111df8bae1dSRodney W. Grimes {
11206cb7259SDavid Greenman 	vm_object_t object;
113df8bae1dSRodney W. Grimes 	struct vnode *vp;
114df8bae1dSRodney W. Grimes 
115df8bae1dSRodney W. Grimes 	/*
116df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
117df8bae1dSRodney W. Grimes 	 */
118df8bae1dSRodney W. Grimes 	if (handle == NULL)
119df8bae1dSRodney W. Grimes 		return (NULL);
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
12239d38f93SDavid Greenman 
12363e7e60dSJeff Roberson 	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
12463e7e60dSJeff Roberson 
125990ab7adSAlan Cox 	mtx_lock(&Giant);
12639d38f93SDavid Greenman 	/*
12739d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
12839d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
12939d38f93SDavid Greenman 	 */
130e6e370a7SJeff Roberson 	VI_LOCK(vp);
131e6e370a7SJeff Roberson 	while (vp->v_iflag & VI_OLOCK) {
132e6e370a7SJeff Roberson 		vp->v_iflag |= VI_OWANT;
133e6e370a7SJeff Roberson 		msleep(vp, VI_MTX(vp), PVM, "vnpobj", 0);
13439d38f93SDavid Greenman 	}
135e6e370a7SJeff Roberson 	vp->v_iflag |= VI_OLOCK;
136e6e370a7SJeff Roberson 	VI_UNLOCK(vp);
13739d38f93SDavid Greenman 
13839d38f93SDavid Greenman 	/*
13939d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
14039d38f93SDavid Greenman 	 * go away.
14139d38f93SDavid Greenman 	 */
1421ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
1431ca58953SAlan Cox 		VM_OBJECT_LOCK(object);
1441ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
1451ca58953SAlan Cox 			break;
1461ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
14724a1cce3SDavid Greenman 	}
1480d94caffSDavid Greenman 
1492be70f79SJohn Dyson 	if (vp->v_usecount == 0)
1502be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
1512be70f79SJohn Dyson 
15224a1cce3SDavid Greenman 	if (object == NULL) {
153df8bae1dSRodney W. Grimes 		/*
154df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
155df8bae1dSRodney W. Grimes 		 */
1566cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
157bbc0ec52SDavid Greenman 
1586cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
15926f9a767SRodney W. Grimes 
16024a1cce3SDavid Greenman 		object->handle = handle;
16124a1cce3SDavid Greenman 		vp->v_object = object;
162df8bae1dSRodney W. Grimes 	} else {
16395e5e988SJohn Dyson 		object->ref_count++;
1641ca58953SAlan Cox 		VM_OBJECT_UNLOCK(object);
165df8bae1dSRodney W. Grimes 	}
166e6e370a7SJeff Roberson 	VI_LOCK(vp);
167990ab7adSAlan Cox 	vp->v_usecount++;
168e6e370a7SJeff Roberson 	vp->v_iflag &= ~VI_OLOCK;
169e6e370a7SJeff Roberson 	if (vp->v_iflag & VI_OWANT) {
170e6e370a7SJeff Roberson 		vp->v_iflag &= ~VI_OWANT;
17139d38f93SDavid Greenman 		wakeup(vp);
17239d38f93SDavid Greenman 	}
173e6e370a7SJeff Roberson 	VI_UNLOCK(vp);
174990ab7adSAlan Cox 	mtx_unlock(&Giant);
17524a1cce3SDavid Greenman 	return (object);
176df8bae1dSRodney W. Grimes }
177df8bae1dSRodney W. Grimes 
178658ad5ffSAlan Cox /*
179658ad5ffSAlan Cox  *	The object must be locked.
180658ad5ffSAlan Cox  */
181f708ef1bSPoul-Henning Kamp static void
18224a1cce3SDavid Greenman vnode_pager_dealloc(object)
1830d94caffSDavid Greenman 	vm_object_t object;
18424a1cce3SDavid Greenman {
18554d92145SMatthew Dillon 	struct vnode *vp = object->handle;
186df8bae1dSRodney W. Grimes 
18724a1cce3SDavid Greenman 	if (vp == NULL)
18824a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
18924a1cce3SDavid Greenman 
190658ad5ffSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
19166095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
19224a1cce3SDavid Greenman 
19324a1cce3SDavid Greenman 	object->handle = NULL;
19495461b45SJohn Dyson 	object->type = OBJT_DEAD;
195e6e370a7SJeff Roberson 	ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc");
196aa2cabb9SDavid Greenman 	vp->v_object = NULL;
197e6e370a7SJeff Roberson 	vp->v_vflag &= ~(VV_TEXT | VV_OBJBUF);
198df8bae1dSRodney W. Grimes }
19926f9a767SRodney W. Grimes 
200f708ef1bSPoul-Henning Kamp static boolean_t
201a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
20224a1cce3SDavid Greenman 	vm_object_t object;
203a316d390SJohn Dyson 	vm_pindex_t pindex;
20424a1cce3SDavid Greenman 	int *before;
20524a1cce3SDavid Greenman 	int *after;
206df8bae1dSRodney W. Grimes {
20724a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
20898b0c789SPoul-Henning Kamp 	daddr_t bn;
2093af76890SPoul-Henning Kamp 	int err;
210170db9c6SJohn Dyson 	daddr_t reqblock;
2112c4488fcSJohn Dyson 	int poff;
2122c4488fcSJohn Dyson 	int bsize;
213d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
214df8bae1dSRodney W. Grimes 
2150cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
21624579ca1SMatthew Dillon 	/*
21724579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
21824579ca1SMatthew Dillon 	 * have the page.
21924579ca1SMatthew Dillon 	 */
220e6e370a7SJeff Roberson 	if (vp == NULL)
22147221757SJohn Dyson 		return FALSE;
22247221757SJohn Dyson 
22363e7e60dSJeff Roberson 	VI_LOCK(vp);
22463e7e60dSJeff Roberson 	if (vp->v_iflag & VI_DOOMED) {
22563e7e60dSJeff Roberson 		VI_UNLOCK(vp);
226e6e370a7SJeff Roberson 		return FALSE;
22763e7e60dSJeff Roberson 	}
22863e7e60dSJeff Roberson 	VI_UNLOCK(vp);
229df8bae1dSRodney W. Grimes 	/*
2300d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
2310d94caffSDavid Greenman 	 * not have the page.
232df8bae1dSRodney W. Grimes 	 */
233a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
234a316d390SJohn Dyson 	    (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2354abc71c0SDavid Greenman 		return FALSE;
236df8bae1dSRodney W. Grimes 
237eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
238170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
239d63596ceSJohn Dyson 	blocksperpage = 0;
240d63596ceSJohn Dyson 	if (pagesperblock > 0) {
241a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
242d63596ceSJohn Dyson 	} else {
243d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
244d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
245d63596ceSJohn Dyson 	}
246170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
247170db9c6SJohn Dyson 		after, before);
2480d94caffSDavid Greenman 	if (err)
24924a1cce3SDavid Greenman 		return TRUE;
2506eab77f2SJohn Dyson 	if (bn == -1)
251ced399eeSJohn Dyson 		return FALSE;
252d63596ceSJohn Dyson 	if (pagesperblock > 0) {
253a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
254170db9c6SJohn Dyson 		if (before) {
255170db9c6SJohn Dyson 			*before *= pagesperblock;
256170db9c6SJohn Dyson 			*before += poff;
257170db9c6SJohn Dyson 		}
258170db9c6SJohn Dyson 		if (after) {
259b1fc01b7SJohn Dyson 			int numafter;
260170db9c6SJohn Dyson 			*after *= pagesperblock;
261b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
26247e151ddSRobert Drehmel 			if (IDX_TO_OFF(pindex + numafter) >
26347e151ddSRobert Drehmel 			    object->un_pager.vnp.vnp_size) {
26447e151ddSRobert Drehmel 				numafter =
26547e151ddSRobert Drehmel 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
26647e151ddSRobert Drehmel 				    pindex;
267b1fc01b7SJohn Dyson 			}
268b1fc01b7SJohn Dyson 			*after += numafter;
269170db9c6SJohn Dyson 		}
270d63596ceSJohn Dyson 	} else {
271d63596ceSJohn Dyson 		if (before) {
272d63596ceSJohn Dyson 			*before /= blocksperpage;
273d63596ceSJohn Dyson 		}
274d63596ceSJohn Dyson 
275d63596ceSJohn Dyson 		if (after) {
276d63596ceSJohn Dyson 			*after /= blocksperpage;
277d63596ceSJohn Dyson 		}
278d63596ceSJohn Dyson 	}
279ced399eeSJohn Dyson 	return TRUE;
280df8bae1dSRodney W. Grimes }
281df8bae1dSRodney W. Grimes 
282df8bae1dSRodney W. Grimes /*
283df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
28424a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
285df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
286df8bae1dSRodney W. Grimes  *
287df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
288df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
289df8bae1dSRodney W. Grimes  */
290df8bae1dSRodney W. Grimes void
291df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
292df8bae1dSRodney W. Grimes 	struct vnode *vp;
293a316d390SJohn Dyson 	vm_ooffset_t nsize;
294df8bae1dSRodney W. Grimes {
2952a8f9ab5SAlan Cox 	vm_object_t object;
2962a8f9ab5SAlan Cox 	vm_page_t m;
297c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
298df8bae1dSRodney W. Grimes 
2992a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
300df8bae1dSRodney W. Grimes 		return;
3012a8f9ab5SAlan Cox 	VM_OBJECT_LOCK(object);
3022a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
303df8bae1dSRodney W. Grimes 		/*
304df8bae1dSRodney W. Grimes 		 * Hasn't changed size
305df8bae1dSRodney W. Grimes 		 */
3062a8f9ab5SAlan Cox 		VM_OBJECT_UNLOCK(object);
307df8bae1dSRodney W. Grimes 		return;
3082a8f9ab5SAlan Cox 	}
309c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
3102a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
311df8bae1dSRodney W. Grimes 		/*
312bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
313df8bae1dSRodney W. Grimes 		 */
3142a8f9ab5SAlan Cox 		if (nobjsize < object->size)
315c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
316c576d121SLuoqi Chen 			    FALSE);
317bbc0ec52SDavid Greenman 		/*
318bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
3193ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
3203ebeaf59SMatthew Dillon 		 *
3213ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
3223ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
3233ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
324bbc0ec52SDavid Greenman 		 */
3252a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
3262a8f9ab5SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL) {
3272a8f9ab5SAlan Cox 			vm_page_lock_queues();
3282a8f9ab5SAlan Cox 			if (m->valid) {
3292b6b0df7SMatthew Dillon 				int base = (int)nsize & PAGE_MASK;
3302b6b0df7SMatthew Dillon 				int size = PAGE_SIZE - base;
3312b6b0df7SMatthew Dillon 
3322b6b0df7SMatthew Dillon 				/*
3332b6b0df7SMatthew Dillon 				 * Clear out partial-page garbage in case
3342b6b0df7SMatthew Dillon 				 * the page has been mapped.
3352b6b0df7SMatthew Dillon 				 */
336fff6062aSAlan Cox 				pmap_zero_page_area(m, base, size);
3372b6b0df7SMatthew Dillon 
3382b6b0df7SMatthew Dillon 				/*
3393ebeaf59SMatthew Dillon 				 * XXX work around SMP data integrity race
3403ebeaf59SMatthew Dillon 				 * by unmapping the page from user processes.
3413ebeaf59SMatthew Dillon 				 * The garbage we just cleared may be mapped
3423ebeaf59SMatthew Dillon 				 * to a user process running on another cpu
3433ebeaf59SMatthew Dillon 				 * and this code is not running through normal
3443ebeaf59SMatthew Dillon 				 * I/O channels which handle SMP issues for
3453ebeaf59SMatthew Dillon 				 * us, so unmap page to synchronize all cpus.
3463ebeaf59SMatthew Dillon 				 *
3473ebeaf59SMatthew Dillon 				 * XXX should vm_pager_unmap_page() have
3483ebeaf59SMatthew Dillon 				 * dealt with this?
3493ebeaf59SMatthew Dillon 				 */
3504fec79beSAlan Cox 				pmap_remove_all(m);
3513ebeaf59SMatthew Dillon 
3523ebeaf59SMatthew Dillon 				/*
3532b6b0df7SMatthew Dillon 				 * Clear out partial-page dirty bits.  This
3542b6b0df7SMatthew Dillon 				 * has the side effect of setting the valid
3552b6b0df7SMatthew Dillon 				 * bits, but that is ok.  There are a bunch
3562b6b0df7SMatthew Dillon 				 * of places in the VM system where we expected
3572b6b0df7SMatthew Dillon 				 * m->dirty == VM_PAGE_BITS_ALL.  The file EOF
3582b6b0df7SMatthew Dillon 				 * case is one of them.  If the page is still
3592b6b0df7SMatthew Dillon 				 * partially dirty, make it fully dirty.
3603ebeaf59SMatthew Dillon 				 *
3613ebeaf59SMatthew Dillon 				 * note that we do not clear out the valid
3623ebeaf59SMatthew Dillon 				 * bits.  This would prevent bogus_page
3633ebeaf59SMatthew Dillon 				 * replacement from working properly.
3642b6b0df7SMatthew Dillon 				 */
3652b6b0df7SMatthew Dillon 				vm_page_set_validclean(m, base, size);
3662b6b0df7SMatthew Dillon 				if (m->dirty != 0)
3672b6b0df7SMatthew Dillon 					m->dirty = VM_PAGE_BITS_ALL;
368bbc0ec52SDavid Greenman 			}
3692a8f9ab5SAlan Cox 			vm_page_unlock_queues();
370bbc0ec52SDavid Greenman 		}
371bbc0ec52SDavid Greenman 	}
372a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
373c576d121SLuoqi Chen 	object->size = nobjsize;
3742a8f9ab5SAlan Cox 	VM_OBJECT_UNLOCK(object);
375df8bae1dSRodney W. Grimes }
376df8bae1dSRodney W. Grimes 
37726f9a767SRodney W. Grimes /*
37826f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
37926f9a767SRodney W. Grimes  * file address
38026f9a767SRodney W. Grimes  */
381f708ef1bSPoul-Henning Kamp static vm_offset_t
382efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
38326f9a767SRodney W. Grimes 	struct vnode *vp;
384a316d390SJohn Dyson 	vm_ooffset_t address;
385efc68ce1SDavid Greenman 	int *run;
38626f9a767SRodney W. Grimes {
38726f9a767SRodney W. Grimes 	int rtaddress;
38826f9a767SRodney W. Grimes 	int bsize;
38998b0c789SPoul-Henning Kamp 	daddr_t block;
39026f9a767SRodney W. Grimes 	struct vnode *rtvp;
39126f9a767SRodney W. Grimes 	int err;
392a316d390SJohn Dyson 	daddr_t vblock;
393a316d390SJohn Dyson 	int voffset;
39426f9a767SRodney W. Grimes 
3950cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
3960d94caffSDavid Greenman 	if ((int) address < 0)
3970d94caffSDavid Greenman 		return -1;
3980d94caffSDavid Greenman 
3992c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4002c4488fcSJohn Dyson 		return -1;
4012c4488fcSJohn Dyson 
40226f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
40326f9a767SRodney W. Grimes 	vblock = address / bsize;
40426f9a767SRodney W. Grimes 	voffset = address % bsize;
40526f9a767SRodney W. Grimes 
406c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
40726f9a767SRodney W. Grimes 
408efc68ce1SDavid Greenman 	if (err || (block == -1))
40926f9a767SRodney W. Grimes 		rtaddress = -1;
410efc68ce1SDavid Greenman 	else {
411187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
412efc68ce1SDavid Greenman 		if (run) {
413efc68ce1SDavid Greenman 			*run += 1;
414efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
415efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
416efc68ce1SDavid Greenman 		}
417efc68ce1SDavid Greenman 	}
41826f9a767SRodney W. Grimes 
41926f9a767SRodney W. Grimes 	return rtaddress;
42026f9a767SRodney W. Grimes }
42126f9a767SRodney W. Grimes 
42226f9a767SRodney W. Grimes /*
42326f9a767SRodney W. Grimes  * interrupt routine for I/O completion
42426f9a767SRodney W. Grimes  */
425f708ef1bSPoul-Henning Kamp static void
42626f9a767SRodney W. Grimes vnode_pager_iodone(bp)
42726f9a767SRodney W. Grimes 	struct buf *bp;
42826f9a767SRodney W. Grimes {
42926f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
43024a1cce3SDavid Greenman 	wakeup(bp);
43126f9a767SRodney W. Grimes }
43226f9a767SRodney W. Grimes 
43326f9a767SRodney W. Grimes /*
43426f9a767SRodney W. Grimes  * small block filesystem vnode pager input
43526f9a767SRodney W. Grimes  */
436f708ef1bSPoul-Henning Kamp static int
43724a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
43824a1cce3SDavid Greenman 	vm_object_t object;
43926f9a767SRodney W. Grimes 	vm_page_t m;
44026f9a767SRodney W. Grimes {
44126f9a767SRodney W. Grimes 	int i;
44226f9a767SRodney W. Grimes 	int s;
44326f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
44426f9a767SRodney W. Grimes 	struct buf *bp;
44526f9a767SRodney W. Grimes 	vm_offset_t kva;
44626f9a767SRodney W. Grimes 	int fileaddr;
44726f9a767SRodney W. Grimes 	vm_offset_t bsize;
44826f9a767SRodney W. Grimes 	int error = 0;
44926f9a767SRodney W. Grimes 
4500cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
4510cddd8f0SMatthew Dillon 
45224a1cce3SDavid Greenman 	vp = object->handle;
4532c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4542c4488fcSJohn Dyson 		return VM_PAGER_BAD;
4552c4488fcSJohn Dyson 
45626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
4570bdb7528SDavid Greenman 
458c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
45926f9a767SRodney W. Grimes 
46026f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
46126f9a767SRodney W. Grimes 
46226f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
46333c67741SMatthew Dillon 		vm_ooffset_t address;
464bbc0ec52SDavid Greenman 
465897a45efSDmitrij Tejblum 		if (vm_page_bits(i * bsize, bsize) & m->valid)
46626f9a767SRodney W. Grimes 			continue;
46726f9a767SRodney W. Grimes 
46833c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
46933c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
47033c67741SMatthew Dillon 			fileaddr = -1;
47133c67741SMatthew Dillon 		} else {
47233c67741SMatthew Dillon 			fileaddr = vnode_pager_addr(vp, address, NULL);
47333c67741SMatthew Dillon 		}
47426f9a767SRodney W. Grimes 		if (fileaddr != -1) {
4751c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
47626f9a767SRodney W. Grimes 
47726f9a767SRodney W. Grimes 			/* build a minimal buffer header */
47821144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
47926f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
480bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
481bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
482a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
483a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
484ab3f7469SPoul-Henning Kamp 			bp->b_data = (caddr_t) kva + i * bsize;
485187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4860d94caffSDavid Greenman 			pbgetvp(dp, bp);
48726f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
48826f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
4892b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
4902b6b0df7SMatthew Dillon 			runningbufspace += bp->b_runningbufspace;
49126f9a767SRodney W. Grimes 
49226f9a767SRodney W. Grimes 			/* do the input */
4935266a767SPoul-Henning Kamp 			VOP_SPECSTRATEGY(bp->b_vp, bp);
49426f9a767SRodney W. Grimes 
495e47ed70bSJohn Dyson 			/* we definitely need to be at splvm here */
49626f9a767SRodney W. Grimes 
497e47ed70bSJohn Dyson 			s = splvm();
49826f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
499aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
50026f9a767SRodney W. Grimes 			}
50126f9a767SRodney W. Grimes 			splx(s);
502c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
50326f9a767SRodney W. Grimes 				error = EIO;
50426f9a767SRodney W. Grimes 
50526f9a767SRodney W. Grimes 			/*
50626f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
50726f9a767SRodney W. Grimes 			 */
5081c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
50926f9a767SRodney W. Grimes 			if (error)
51026f9a767SRodney W. Grimes 				break;
5110d94caffSDavid Greenman 
512178949e0SAlan Cox 			vm_page_lock_queues();
513aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
514178949e0SAlan Cox 			vm_page_unlock_queues();
51526f9a767SRodney W. Grimes 		} else {
516178949e0SAlan Cox 			vm_page_lock_queues();
517aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
518178949e0SAlan Cox 			vm_page_unlock_queues();
51926f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
52026f9a767SRodney W. Grimes 		}
52126f9a767SRodney W. Grimes 	}
52226f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
52385e01243SAlan Cox 	vm_page_lock_queues();
5240385347cSPeter Wemm 	pmap_clear_modify(m);
525e69763a3SDoug Rabson 	vm_page_flag_clear(m, PG_ZERO);
52685e01243SAlan Cox 	vm_page_unlock_queues();
52726f9a767SRodney W. Grimes 	if (error) {
528a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
52926f9a767SRodney W. Grimes 	}
53026f9a767SRodney W. Grimes 	return VM_PAGER_OK;
53126f9a767SRodney W. Grimes 
53226f9a767SRodney W. Grimes }
53326f9a767SRodney W. Grimes 
53426f9a767SRodney W. Grimes 
53526f9a767SRodney W. Grimes /*
53626f9a767SRodney W. Grimes  * old style vnode pager output routine
53726f9a767SRodney W. Grimes  */
538f708ef1bSPoul-Henning Kamp static int
53924a1cce3SDavid Greenman vnode_pager_input_old(object, m)
54024a1cce3SDavid Greenman 	vm_object_t object;
54126f9a767SRodney W. Grimes 	vm_page_t m;
54226f9a767SRodney W. Grimes {
543df8bae1dSRodney W. Grimes 	struct uio auio;
544df8bae1dSRodney W. Grimes 	struct iovec aiov;
54526f9a767SRodney W. Grimes 	int error;
54626f9a767SRodney W. Grimes 	int size;
54726f9a767SRodney W. Grimes 	vm_offset_t kva;
548342a1480SJohn Baldwin 	struct vnode *vp;
549df8bae1dSRodney W. Grimes 
5500cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
55126f9a767SRodney W. Grimes 	error = 0;
552bbc0ec52SDavid Greenman 
553df8bae1dSRodney W. Grimes 	/*
55426f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
55526f9a767SRodney W. Grimes 	 */
556a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
55726f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
55826f9a767SRodney W. Grimes 	} else {
55926f9a767SRodney W. Grimes 		size = PAGE_SIZE;
560a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
561a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
5620bdb7528SDavid Greenman 
56326f9a767SRodney W. Grimes 		/*
564df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
565df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
566df8bae1dSRodney W. Grimes 		 */
56726f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
5680bdb7528SDavid Greenman 
569342a1480SJohn Baldwin 		vp = object->handle;
570df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
571df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
572df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
573df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
574a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
575df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
57626f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
577df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
578b40ce416SJulian Elischer 		auio.uio_td = curthread;
57926f9a767SRodney W. Grimes 
580a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
581df8bae1dSRodney W. Grimes 		if (!error) {
58254d92145SMatthew Dillon 			int count = size - auio.uio_resid;
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 			if (count == 0)
585df8bae1dSRodney W. Grimes 				error = EINVAL;
58626f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
58726f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
588df8bae1dSRodney W. Grimes 		}
58926f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
590df8bae1dSRodney W. Grimes 	}
59185e01243SAlan Cox 	vm_page_lock_queues();
5920385347cSPeter Wemm 	pmap_clear_modify(m);
5932c28a105SAlan Cox 	vm_page_undirty(m);
594e69763a3SDoug Rabson 	vm_page_flag_clear(m, PG_ZERO);
5956e3a3f38SRobert V. Baron 	if (!error)
5966e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
59785e01243SAlan Cox 	vm_page_unlock_queues();
598a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
59926f9a767SRodney W. Grimes }
60026f9a767SRodney W. Grimes 
60126f9a767SRodney W. Grimes /*
60226f9a767SRodney W. Grimes  * generic vnode pager input routine
60326f9a767SRodney W. Grimes  */
604170db9c6SJohn Dyson 
605ce75f2c3SMike Smith /*
60623955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
60747e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
60847e151ddSRobert Drehmel  * to implement the previous behaviour.
609ce75f2c3SMike Smith  *
610ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
611ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
612ce75f2c3SMike Smith  */
613f708ef1bSPoul-Henning Kamp static int
61424a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
61526f9a767SRodney W. Grimes 	vm_object_t object;
61624a1cce3SDavid Greenman 	vm_page_t *m;
61724a1cce3SDavid Greenman 	int count;
61824a1cce3SDavid Greenman 	int reqpage;
61924a1cce3SDavid Greenman {
620170db9c6SJohn Dyson 	int rtval;
621170db9c6SJohn Dyson 	struct vnode *vp;
62286ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
62395e5e988SJohn Dyson 
6240cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
625170db9c6SJohn Dyson 	vp = object->handle;
62686ffbd76SMike Smith 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
62723955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
62823955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
629170db9c6SJohn Dyson 	return rtval;
630170db9c6SJohn Dyson }
631170db9c6SJohn Dyson 
632ce75f2c3SMike Smith /*
633ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
634ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
635ce75f2c3SMike Smith  */
636ce75f2c3SMike Smith int
637ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
638ce75f2c3SMike Smith 	struct vnode *vp;
639170db9c6SJohn Dyson 	vm_page_t *m;
640ce75f2c3SMike Smith 	int bytecount;
641170db9c6SJohn Dyson 	int reqpage;
642170db9c6SJohn Dyson {
643ce75f2c3SMike Smith 	vm_object_t object;
644a316d390SJohn Dyson 	vm_offset_t kva;
6458f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
646e43c2eabSAlan Cox 	int i, j, size, bsize, first, firstaddr;
647ce75f2c3SMike Smith 	struct vnode *dp;
648efc68ce1SDavid Greenman 	int runpg;
649efc68ce1SDavid Greenman 	int runend;
6500bdb7528SDavid Greenman 	struct buf *bp;
65126f9a767SRodney W. Grimes 	int s;
652ce75f2c3SMike Smith 	int count;
65326f9a767SRodney W. Grimes 	int error = 0;
65426f9a767SRodney W. Grimes 
6550cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
656ce75f2c3SMike Smith 	object = vp->v_object;
657ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
658ce75f2c3SMike Smith 
6592c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
6602c4488fcSJohn Dyson 		return VM_PAGER_BAD;
6612c4488fcSJohn Dyson 
66226f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
66326f9a767SRodney W. Grimes 
66426f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
665bbc0ec52SDavid Greenman 
66626f9a767SRodney W. Grimes 	/*
667bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
668bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
66926f9a767SRodney W. Grimes 	 */
670a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
671bbc0ec52SDavid Greenman 
67226f9a767SRodney W. Grimes 	/*
67316f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
67426f9a767SRodney W. Grimes 	 */
675c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
676e43c2eabSAlan Cox 		vm_page_lock_queues();
677e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
678e43c2eabSAlan Cox 			if (i != reqpage)
679d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
680e43c2eabSAlan Cox 		vm_page_unlock_queues();
681976e77fcSDavid Greenman 		cnt.v_vnodein++;
682976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
68324a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
684bbc0ec52SDavid Greenman 
68526f9a767SRodney W. Grimes 		/*
68626f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
68726f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
68826f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
68926f9a767SRodney W. Grimes 		 */
69026f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
691500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
692e43c2eabSAlan Cox 		vm_page_lock_queues();
693e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
694e43c2eabSAlan Cox 			if (i != reqpage)
695d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
696e43c2eabSAlan Cox 		vm_page_unlock_queues();
697976e77fcSDavid Greenman 		cnt.v_vnodein++;
698976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
69924a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
70026f9a767SRodney W. Grimes 	}
7018d17e694SJulian Elischer 
70226f9a767SRodney W. Grimes 	/*
7038d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
7048d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
7058d17e694SJulian Elischer 	 * media.
7060d94caffSDavid Greenman 	 */
7078d17e694SJulian Elischer 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
708e43c2eabSAlan Cox 		vm_page_lock_queues();
709e43c2eabSAlan Cox 		for (i = 0; i < count; i++)
7100d94caffSDavid Greenman 			if (i != reqpage)
711d8d5fa88SAlfred Perlstein 				vm_page_free(m[i]);
712e43c2eabSAlan Cox 		vm_page_unlock_queues();
7130d94caffSDavid Greenman 		return VM_PAGER_OK;
7140d94caffSDavid Greenman 	}
7158d17e694SJulian Elischer 	m[reqpage]->valid = 0;
7160bdb7528SDavid Greenman 
7170d94caffSDavid Greenman 	/*
71826f9a767SRodney W. Grimes 	 * here on direct device I/O
71926f9a767SRodney W. Grimes 	 */
720efc68ce1SDavid Greenman 	firstaddr = -1;
721a1287949SEivind Eklund 
72226f9a767SRodney W. Grimes 	/*
723efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
72426f9a767SRodney W. Grimes 	 */
725efc68ce1SDavid Greenman 	for (first = 0, i = 0; i < count; i = runend) {
726a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
727a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
728efc68ce1SDavid Greenman 		if (firstaddr == -1) {
72924a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
730bf1001faSMaxime Henrion 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
731bf1001faSMaxime Henrion 				    firstaddr, (uintmax_t)(foff >> 32),
732bf1001faSMaxime Henrion 				    (uintmax_t)foff,
733bf1001faSMaxime Henrion 				    (uintmax_t)
734fc62ef1fSBruce Evans 				    (object->un_pager.vnp.vnp_size >> 32),
735bf1001faSMaxime Henrion 				    (uintmax_t)object->un_pager.vnp.vnp_size);
736efc68ce1SDavid Greenman 			}
737e43c2eabSAlan Cox 			vm_page_lock_queues();
738d8d5fa88SAlfred Perlstein 			vm_page_free(m[i]);
739e43c2eabSAlan Cox 			vm_page_unlock_queues();
740efc68ce1SDavid Greenman 			runend = i + 1;
741efc68ce1SDavid Greenman 			first = runend;
742efc68ce1SDavid Greenman 			continue;
743efc68ce1SDavid Greenman 		}
744efc68ce1SDavid Greenman 		runend = i + runpg;
745efc68ce1SDavid Greenman 		if (runend <= reqpage) {
746e43c2eabSAlan Cox 			vm_page_lock_queues();
747e43c2eabSAlan Cox 			for (j = i; j < runend; j++)
748d8d5fa88SAlfred Perlstein 				vm_page_free(m[j]);
749e43c2eabSAlan Cox 			vm_page_unlock_queues();
75026f9a767SRodney W. Grimes 		} else {
751efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
752e43c2eabSAlan Cox 				vm_page_lock_queues();
753efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
754d8d5fa88SAlfred Perlstein 					vm_page_free(m[i]);
755e43c2eabSAlan Cox 				vm_page_unlock_queues();
756efc68ce1SDavid Greenman 				count = first + runpg;
75726f9a767SRodney W. Grimes 			}
758efc68ce1SDavid Greenman 			break;
75926f9a767SRodney W. Grimes 		}
760efc68ce1SDavid Greenman 		first = runend;
761efc68ce1SDavid Greenman 	}
76226f9a767SRodney W. Grimes 
76326f9a767SRodney W. Grimes 	/*
764bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
765bbc0ec52SDavid Greenman 	 * to be zero based...
76626f9a767SRodney W. Grimes 	 */
76726f9a767SRodney W. Grimes 	if (first != 0) {
76826f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
76926f9a767SRodney W. Grimes 			m[i - first] = m[i];
77026f9a767SRodney W. Grimes 		}
77126f9a767SRodney W. Grimes 		count -= first;
77226f9a767SRodney W. Grimes 		reqpage -= first;
77326f9a767SRodney W. Grimes 	}
774efc68ce1SDavid Greenman 
77526f9a767SRodney W. Grimes 	/*
77626f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
77726f9a767SRodney W. Grimes 	 */
778a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
77926f9a767SRodney W. Grimes 
78026f9a767SRodney W. Grimes 	/*
78126f9a767SRodney W. Grimes 	 * calculate the size of the transfer
78226f9a767SRodney W. Grimes 	 */
78326f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
78424a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
78524a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
78626f9a767SRodney W. Grimes 
78726f9a767SRodney W. Grimes 	/*
78824579ca1SMatthew Dillon 	 * round up physical size for real devices.
78926f9a767SRodney W. Grimes 	 */
79024579ca1SMatthew Dillon 	if (dp->v_type == VBLK || dp->v_type == VCHR) {
79124579ca1SMatthew Dillon 		int secmask = dp->v_rdev->si_bsize_phys - 1;
79224579ca1SMatthew Dillon 		KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
79324579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
79424579ca1SMatthew Dillon 	}
79526f9a767SRodney W. Grimes 
7961c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
79716f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
79816f62314SDavid Greenman 
79926f9a767SRodney W. Grimes 	/*
80026f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
80126f9a767SRodney W. Grimes 	 */
80216f62314SDavid Greenman 	pmap_qenter(kva, m, count);
80326f9a767SRodney W. Grimes 
80426f9a767SRodney W. Grimes 	/* build a minimal buffer header */
80521144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
80626f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
80726f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
808bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
809bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
810a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
811a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
812187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
8130d94caffSDavid Greenman 	pbgetvp(dp, bp);
81426f9a767SRodney W. Grimes 	bp->b_bcount = size;
81526f9a767SRodney W. Grimes 	bp->b_bufsize = size;
8162b6b0df7SMatthew Dillon 	bp->b_runningbufspace = bp->b_bufsize;
8172b6b0df7SMatthew Dillon 	runningbufspace += bp->b_runningbufspace;
81826f9a767SRodney W. Grimes 
819976e77fcSDavid Greenman 	cnt.v_vnodein++;
820976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
821976e77fcSDavid Greenman 
82226f9a767SRodney W. Grimes 	/* do the input */
823ca94e7c4SPoul-Henning Kamp 	if (dp->v_type == VCHR)
8245266a767SPoul-Henning Kamp 		VOP_SPECSTRATEGY(bp->b_vp, bp);
825ca94e7c4SPoul-Henning Kamp 	else
826ca94e7c4SPoul-Henning Kamp 		VOP_STRATEGY(bp->b_vp, bp);
827976e77fcSDavid Greenman 
828e47ed70bSJohn Dyson 	s = splvm();
829e47ed70bSJohn Dyson 	/* we definitely need to be at splvm here */
83026f9a767SRodney W. Grimes 
83126f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
832aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
83326f9a767SRodney W. Grimes 	}
83426f9a767SRodney W. Grimes 	splx(s);
835c244d2deSPoul-Henning Kamp 	if ((bp->b_ioflags & BIO_ERROR) != 0)
83626f9a767SRodney W. Grimes 		error = EIO;
83726f9a767SRodney W. Grimes 
83826f9a767SRodney W. Grimes 	if (!error) {
83926f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
84026f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
84126f9a767SRodney W. Grimes 	}
84216f62314SDavid Greenman 	pmap_qremove(kva, count);
84326f9a767SRodney W. Grimes 
84426f9a767SRodney W. Grimes 	/*
84526f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
84626f9a767SRodney W. Grimes 	 */
8471c7c3c6aSMatthew Dillon 	relpbuf(bp, &vnode_pbuf_freecnt);
84826f9a767SRodney W. Grimes 
8499d522888SAlan Cox 	vm_page_lock_queues();
8508f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
8518f9110f6SJohn Dyson 		vm_page_t mt;
8528f9110f6SJohn Dyson 
8538f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
8548f9110f6SJohn Dyson 		mt = m[i];
8558f9110f6SJohn Dyson 
85654746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
8578d17e694SJulian Elischer 			/*
8588d17e694SJulian Elischer 			 * Read filled up entire page.
8598d17e694SJulian Elischer 			 */
8608f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
8612c28a105SAlan Cox 			vm_page_undirty(mt);	/* should be an assert? XXX */
8620385347cSPeter Wemm 			pmap_clear_modify(mt);
8638f9110f6SJohn Dyson 		} else {
8648d17e694SJulian Elischer 			/*
8658d17e694SJulian Elischer 			 * Read did not fill up entire page.  Since this
8668d17e694SJulian Elischer 			 * is getpages, the page may be mapped, so we have
8678d17e694SJulian Elischer 			 * to zero the invalid portions of the page even
8688d17e694SJulian Elischer 			 * though we aren't setting them valid.
8698d17e694SJulian Elischer 			 *
8708d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
8718d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
8728d17e694SJulian Elischer 			 * read.
8738d17e694SJulian Elischer 			 */
87454746b67SDmitrij Tejblum 			vm_page_set_validclean(mt, 0,
87554746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
8764221e284SAlan Cox 			/* handled by vm_fault now */
8774221e284SAlan Cox 			/* vm_page_zero_invalid(mt, FALSE); */
8788f9110f6SJohn Dyson 		}
8798f9110f6SJohn Dyson 
880e69763a3SDoug Rabson 		vm_page_flag_clear(mt, PG_ZERO);
88126f9a767SRodney W. Grimes 		if (i != reqpage) {
882bbc0ec52SDavid Greenman 
88326f9a767SRodney W. Grimes 			/*
884bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
885bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
886bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
887956f3135SPhilippe Charnier 			 * It appears that empirical results show that
888bbc0ec52SDavid Greenman 			 * deactivating pages is best.
88926f9a767SRodney W. Grimes 			 */
890bbc0ec52SDavid Greenman 
89126f9a767SRodney W. Grimes 			/*
892bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
893bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
89426f9a767SRodney W. Grimes 			 */
89526f9a767SRodney W. Grimes 			if (!error) {
8968f9110f6SJohn Dyson 				if (mt->flags & PG_WANTED)
8978f9110f6SJohn Dyson 					vm_page_activate(mt);
89895461b45SJohn Dyson 				else
8998f9110f6SJohn Dyson 					vm_page_deactivate(mt);
900e69763a3SDoug Rabson 				vm_page_wakeup(mt);
90126f9a767SRodney W. Grimes 			} else {
902d8d5fa88SAlfred Perlstein 				vm_page_free(mt);
90326f9a767SRodney W. Grimes 			}
90426f9a767SRodney W. Grimes 		}
90526f9a767SRodney W. Grimes 	}
9069d522888SAlan Cox 	vm_page_unlock_queues();
90726f9a767SRodney W. Grimes 	if (error) {
90824a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
90926f9a767SRodney W. Grimes 	}
910a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
91126f9a767SRodney W. Grimes }
91226f9a767SRodney W. Grimes 
913ce75f2c3SMike Smith /*
914ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
915ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
916ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
917ce75f2c3SMike Smith  *
918ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
919ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
920ce75f2c3SMike Smith  */
921e4542174SMatthew Dillon static void
922170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
923170db9c6SJohn Dyson 	vm_object_t object;
924170db9c6SJohn Dyson 	vm_page_t *m;
925170db9c6SJohn Dyson 	int count;
926170db9c6SJohn Dyson 	boolean_t sync;
927170db9c6SJohn Dyson 	int *rtvals;
928170db9c6SJohn Dyson {
929170db9c6SJohn Dyson 	int rtval;
930170db9c6SJohn Dyson 	struct vnode *vp;
931f2a2857bSKirk McKusick 	struct mount *mp;
93286ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
933ad980522SJohn Dyson 
9340cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
9350e3cdf2cSAlan Cox 	/*
9360e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
9370e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
9380e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
9390e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
9400e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
9410e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
9420e3cdf2cSAlan Cox 	 *
9430e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
9440e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
9450e3cdf2cSAlan Cox 	 */
9460e3cdf2cSAlan Cox 
9470e3cdf2cSAlan Cox 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
9480e3cdf2cSAlan Cox 		sync |= OBJPC_SYNC;
9490e3cdf2cSAlan Cox 
9500e3cdf2cSAlan Cox 	/*
9510e3cdf2cSAlan Cox 	 * Call device-specific putpages function
9520e3cdf2cSAlan Cox 	 */
953170db9c6SJohn Dyson 	vp = object->handle;
954f2a2857bSKirk McKusick 	if (vp->v_type != VREG)
955f2a2857bSKirk McKusick 		mp = NULL;
956f2a2857bSKirk McKusick 	(void)vn_start_write(vp, &mp, V_WAIT);
95786ffbd76SMike Smith 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
95823955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
95923955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
960f2a2857bSKirk McKusick 	vn_finished_write(mp);
961170db9c6SJohn Dyson }
962170db9c6SJohn Dyson 
963ce75f2c3SMike Smith 
96426f9a767SRodney W. Grimes /*
965ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
9664491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
9672b6b0df7SMatthew Dillon  *
9682b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
9692b6b0df7SMatthew Dillon  * clustering has already typically occured, so in general we ask the
9702b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
9712b6b0df7SMatthew Dillon  * then delayed.
97226f9a767SRodney W. Grimes  */
973ce75f2c3SMike Smith int
9748f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
975ce75f2c3SMike Smith 	struct vnode *vp;
97626f9a767SRodney W. Grimes 	vm_page_t *m;
977ce75f2c3SMike Smith 	int bytecount;
9788f9110f6SJohn Dyson 	int flags;
97926f9a767SRodney W. Grimes 	int *rtvals;
98026f9a767SRodney W. Grimes {
981f6b04d2bSDavid Greenman 	int i;
982ce75f2c3SMike Smith 	vm_object_t object;
983ce75f2c3SMike Smith 	int count;
98426f9a767SRodney W. Grimes 
985f6b04d2bSDavid Greenman 	int maxsize, ncount;
986a316d390SJohn Dyson 	vm_ooffset_t poffset;
987f6b04d2bSDavid Greenman 	struct uio auio;
988f6b04d2bSDavid Greenman 	struct iovec aiov;
989f6b04d2bSDavid Greenman 	int error;
9908f9110f6SJohn Dyson 	int ioflags;
99126f9a767SRodney W. Grimes 
9920cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
993ce75f2c3SMike Smith 	object = vp->v_object;
994ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
995ce75f2c3SMike Smith 
99626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
99726f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
99826f9a767SRodney W. Grimes 
999a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
10003efc015bSPeter Wemm 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n",
10013efc015bSPeter Wemm 			(long)m[0]->pindex, m[0]->dirty);
1002f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1003f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
10040d94caffSDavid Greenman 	}
10050bdb7528SDavid Greenman 
1006f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1007f6b04d2bSDavid Greenman 	ncount = count;
100826f9a767SRodney W. Grimes 
1009a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
101000a6f47fSMatthew Dillon 
101100a6f47fSMatthew Dillon 	/*
101200a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
101300a6f47fSMatthew Dillon 	 * have to invalidate pages occuring beyond the file EOF.  However,
101400a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
101500a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
101600a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
101700a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
101800a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
10193ebeaf59SMatthew Dillon 	 *
10203ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
10213ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
102200a6f47fSMatthew Dillon 	 */
1023a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
102400a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
102500a6f47fSMatthew Dillon 			int pgoff;
102600a6f47fSMatthew Dillon 
1027a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1028aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
102900a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
103000a6f47fSMatthew Dillon 				vm_page_clear_dirty(m[ncount - 1], pgoff,
103100a6f47fSMatthew Dillon 					PAGE_SIZE - pgoff);
103200a6f47fSMatthew Dillon 			}
103300a6f47fSMatthew Dillon 		} else {
103400a6f47fSMatthew Dillon 			maxsize = 0;
103500a6f47fSMatthew Dillon 			ncount = 0;
103600a6f47fSMatthew Dillon 		}
1037f6b04d2bSDavid Greenman 		if (ncount < count) {
1038f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1039f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1040f6b04d2bSDavid Greenman 			}
1041f6b04d2bSDavid Greenman 		}
1042f6b04d2bSDavid Greenman 	}
104326f9a767SRodney W. Grimes 
10442b6b0df7SMatthew Dillon 	/*
10452b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
10462b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
104743b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
104843b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
104943b7990eSMatthew Dillon 	 * the system decides how to cluster.
10502b6b0df7SMatthew Dillon 	 */
10518f9110f6SJohn Dyson 	ioflags = IO_VMIO;
105243b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
105343b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
105443b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
105543b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
10568f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
105743b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1058f6b04d2bSDavid Greenman 
1059f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1060f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1061f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1062f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1063a316d390SJohn Dyson 	auio.uio_offset = poffset;
1064f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1065f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1066f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1067b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1068a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1069976e77fcSDavid Greenman 	cnt.v_vnodeout++;
1070f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
1071f6b04d2bSDavid Greenman 
1072f6b04d2bSDavid Greenman 	if (error) {
107324a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
1074f6b04d2bSDavid Greenman 	}
1075f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1076ac1e407bSBruce Evans 		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1077ac1e407bSBruce Evans 		    auio.uio_resid, (u_long)m[0]->pindex);
107826f9a767SRodney W. Grimes 	}
1079ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
108026f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
108126f9a767SRodney W. Grimes 	}
1082f6b04d2bSDavid Greenman 	return rtvals[0];
108326f9a767SRodney W. Grimes }
1084f6b04d2bSDavid Greenman 
1085f6b04d2bSDavid Greenman struct vnode *
108624a1cce3SDavid Greenman vnode_pager_lock(object)
108724a1cce3SDavid Greenman 	vm_object_t object;
108824a1cce3SDavid Greenman {
1089b40ce416SJulian Elischer 	struct thread *td = curthread;	/* XXX */
1090996c772fSJohn Dyson 
10910cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
10920cddd8f0SMatthew Dillon 
109324a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
109424a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
1095f6b04d2bSDavid Greenman 			continue;
1096e6b961ffSJohn Baldwin 		if (object->flags & OBJ_DEAD) {
109747221757SJohn Dyson 			return NULL;
1098e6b961ffSJohn Baldwin 		}
1099f6b04d2bSDavid Greenman 
1100e6b961ffSJohn Baldwin 		/* XXX; If object->handle can change, we need to cache it. */
110147221757SJohn Dyson 		while (vget(object->handle,
1102b40ce416SJulian Elischer 			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, td)){
1103bef608bdSJohn Dyson 			if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE))
1104bef608bdSJohn Dyson 				return NULL;
110547221757SJohn Dyson 			printf("vnode_pager_lock: retrying\n");
110647221757SJohn Dyson 		}
110724a1cce3SDavid Greenman 		return object->handle;
110826f9a767SRodney W. Grimes 	}
110924a1cce3SDavid Greenman 	return NULL;
1110f6b04d2bSDavid Greenman }
1111