xref: /freebsd/sys/vm/vnode_pager.c (revision 95461b450d8d76f79be75d7dd0aee4adec4223fb)
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
21df8bae1dSRodney W. Grimes  *    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
4195461b45SJohn Dyson  *	$Id: vnode_pager.c,v 1.82 1998/02/04 22:34:03 eivind Exp $
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 
5447cfdb16SEivind Eklund #include "opt_diagnostic.h"
5547cfdb16SEivind Eklund 
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>
6124a1cce3SDavid Greenman #include <sys/buf.h>
62efeaf95aSDavid Greenman #include <sys/vmmeter.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <vm/vm.h>
65efeaf95aSDavid Greenman #include <vm/vm_prot.h>
66efeaf95aSDavid Greenman #include <vm/vm_object.h>
67df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
6824a1cce3SDavid Greenman #include <vm/vm_pager.h>
691efb74fbSJohn Dyson #include <vm/vm_map.h>
70df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
71efeaf95aSDavid Greenman #include <vm/vm_extern.h>
72df8bae1dSRodney W. Grimes 
73f708ef1bSPoul-Henning Kamp static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
740b8253a7SBruce Evans 					 int *run));
75f708ef1bSPoul-Henning Kamp static void vnode_pager_iodone __P((struct buf *bp));
76f708ef1bSPoul-Henning Kamp static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
77f708ef1bSPoul-Henning Kamp static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
78f708ef1bSPoul-Henning Kamp static void vnode_pager_dealloc __P((vm_object_t));
79f708ef1bSPoul-Henning Kamp static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
80f708ef1bSPoul-Henning Kamp static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
81f708ef1bSPoul-Henning Kamp static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
820b8253a7SBruce Evans 
83df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
8424a1cce3SDavid Greenman 	NULL,
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 
930b8253a7SBruce Evans static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m,
940b8253a7SBruce Evans 					  int count, int reqpage));
950b8253a7SBruce Evans static int vnode_pager_leaf_putpages __P((vm_object_t object, vm_page_t *m,
960b8253a7SBruce Evans 					  int count, boolean_t sync,
970b8253a7SBruce Evans 					  int *rtvals));
98170db9c6SJohn Dyson 
99df8bae1dSRodney W. Grimes /*
100df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
101df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
102df8bae1dSRodney W. Grimes  */
10324a1cce3SDavid Greenman vm_object_t
104b9dcd593SBruce Evans vnode_pager_alloc(void *handle, vm_size_t size, vm_prot_t prot,
105b9dcd593SBruce Evans 		  vm_ooffset_t offset)
106df8bae1dSRodney W. Grimes {
10706cb7259SDavid Greenman 	vm_object_t object;
108df8bae1dSRodney W. Grimes 	struct vnode *vp;
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes 	/*
111df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
112df8bae1dSRodney W. Grimes 	 */
113df8bae1dSRodney W. Grimes 	if (handle == NULL)
114df8bae1dSRodney W. Grimes 		return (NULL);
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
11739d38f93SDavid Greenman 
11839d38f93SDavid Greenman 	/*
11939d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
12039d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
12139d38f93SDavid Greenman 	 */
12239d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
12339d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
12439d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
12539d38f93SDavid Greenman 	}
12639d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
12739d38f93SDavid Greenman 
12839d38f93SDavid Greenman 	/*
12939d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
13039d38f93SDavid Greenman 	 * go away.
13139d38f93SDavid Greenman 	 */
132bd7e5f99SJohn Dyson 	while (((object = vp->v_object) != NULL) &&
133bd7e5f99SJohn Dyson 		(object->flags & OBJ_DEAD)) {
134aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
13524a1cce3SDavid Greenman 	}
1360d94caffSDavid Greenman 
1372be70f79SJohn Dyson 	if (vp->v_usecount == 0)
1382be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
1392be70f79SJohn Dyson 
14024a1cce3SDavid Greenman 	if (object == NULL) {
141df8bae1dSRodney W. Grimes 		/*
142df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
143df8bae1dSRodney W. Grimes 		 */
144a316d390SJohn Dyson 		object = vm_object_allocate(OBJT_VNODE, size);
145ad5dd234SJohn Dyson 		object->flags = 0;
146bbc0ec52SDavid Greenman 
147a316d390SJohn Dyson 		object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE;
14826f9a767SRodney W. Grimes 
14924a1cce3SDavid Greenman 		object->handle = handle;
15024a1cce3SDavid Greenman 		vp->v_object = object;
15195e5e988SJohn Dyson 		vp->v_usecount++;
152df8bae1dSRodney W. Grimes 	} else {
15395e5e988SJohn Dyson 		object->ref_count++;
15495e5e988SJohn Dyson 		vp->v_usecount++;
155df8bae1dSRodney W. Grimes 	}
15639d38f93SDavid Greenman 
15739d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
15839d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
15939d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
16039d38f93SDavid Greenman 		wakeup(vp);
16139d38f93SDavid Greenman 	}
16224a1cce3SDavid Greenman 	return (object);
163df8bae1dSRodney W. Grimes }
164df8bae1dSRodney W. Grimes 
165f708ef1bSPoul-Henning Kamp static void
16624a1cce3SDavid Greenman vnode_pager_dealloc(object)
1670d94caffSDavid Greenman 	vm_object_t object;
16824a1cce3SDavid Greenman {
16924a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
170df8bae1dSRodney W. Grimes 
17124a1cce3SDavid Greenman 	if (vp == NULL)
17224a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
17324a1cce3SDavid Greenman 
17424a1cce3SDavid Greenman 	if (object->paging_in_progress) {
1750d94caffSDavid Greenman 		int s = splbio();
1760d94caffSDavid Greenman 		while (object->paging_in_progress) {
177c0503609SDavid Greenman 			object->flags |= OBJ_PIPWNT;
1780d94caffSDavid Greenman 			tsleep(object, PVM, "vnpdea", 0);
1790d94caffSDavid Greenman 		}
1800d94caffSDavid Greenman 		splx(s);
18124a1cce3SDavid Greenman 	}
18224a1cce3SDavid Greenman 
18324a1cce3SDavid Greenman 	object->handle = NULL;
18495461b45SJohn Dyson 	object->type = OBJT_DEAD;
185aa2cabb9SDavid Greenman 	vp->v_object = NULL;
18695e5e988SJohn Dyson 	vp->v_flag &= ~(VTEXT | VOBJBUF);
187df8bae1dSRodney W. Grimes }
18826f9a767SRodney W. Grimes 
189f708ef1bSPoul-Henning Kamp static boolean_t
190a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
19124a1cce3SDavid Greenman 	vm_object_t object;
192a316d390SJohn Dyson 	vm_pindex_t pindex;
19324a1cce3SDavid Greenman 	int *before;
19424a1cce3SDavid Greenman 	int *after;
195df8bae1dSRodney W. Grimes {
19624a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
197df8bae1dSRodney W. Grimes 	daddr_t bn;
1983af76890SPoul-Henning Kamp 	int err;
199170db9c6SJohn Dyson 	daddr_t reqblock;
2002c4488fcSJohn Dyson 	int poff;
2012c4488fcSJohn Dyson 	int bsize;
202d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
203df8bae1dSRodney W. Grimes 
20447221757SJohn Dyson 	if ((vp == NULL) || (vp->v_flag & VDOOMED))
20547221757SJohn Dyson 		return FALSE;
20647221757SJohn Dyson 
207df8bae1dSRodney W. Grimes 	/*
2080d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
2090d94caffSDavid Greenman 	 * not have the page.
210df8bae1dSRodney W. Grimes 	 */
211a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
212a316d390SJohn Dyson 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2134abc71c0SDavid Greenman 		return FALSE;
214df8bae1dSRodney W. Grimes 
215eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
216170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
217d63596ceSJohn Dyson 	blocksperpage = 0;
218d63596ceSJohn Dyson 	if (pagesperblock > 0) {
219a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
220d63596ceSJohn Dyson 	} else {
221d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
222d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
223d63596ceSJohn Dyson 	}
224170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
225170db9c6SJohn Dyson 		after, before);
2260d94caffSDavid Greenman 	if (err)
22724a1cce3SDavid Greenman 		return TRUE;
2286eab77f2SJohn Dyson 	if ( bn == -1)
229ced399eeSJohn Dyson 		return FALSE;
230d63596ceSJohn Dyson 	if (pagesperblock > 0) {
231a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
232170db9c6SJohn Dyson 		if (before) {
233170db9c6SJohn Dyson 			*before *= pagesperblock;
234170db9c6SJohn Dyson 			*before += poff;
235170db9c6SJohn Dyson 		}
236170db9c6SJohn Dyson 		if (after) {
237b1fc01b7SJohn Dyson 			int numafter;
238170db9c6SJohn Dyson 			*after *= pagesperblock;
239b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
240a316d390SJohn Dyson 			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
241a316d390SJohn Dyson 				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
242b1fc01b7SJohn Dyson 			}
243b1fc01b7SJohn Dyson 			*after += numafter;
244170db9c6SJohn Dyson 		}
245d63596ceSJohn Dyson 	} else {
246d63596ceSJohn Dyson 		if (before) {
247d63596ceSJohn Dyson 			*before /= blocksperpage;
248d63596ceSJohn Dyson 		}
249d63596ceSJohn Dyson 
250d63596ceSJohn Dyson 		if (after) {
251d63596ceSJohn Dyson 			*after /= blocksperpage;
252d63596ceSJohn Dyson 		}
253d63596ceSJohn Dyson 	}
254ced399eeSJohn Dyson 	return TRUE;
255df8bae1dSRodney W. Grimes }
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes /*
258df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
25924a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
260df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
261df8bae1dSRodney W. Grimes  *
262df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
263df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
264df8bae1dSRodney W. Grimes  */
265df8bae1dSRodney W. Grimes void
266df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
267df8bae1dSRodney W. Grimes 	struct vnode *vp;
268a316d390SJohn Dyson 	vm_ooffset_t nsize;
269df8bae1dSRodney W. Grimes {
27024a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
271df8bae1dSRodney W. Grimes 
27224a1cce3SDavid Greenman 	if (object == NULL)
273df8bae1dSRodney W. Grimes 		return;
274bbc0ec52SDavid Greenman 
275df8bae1dSRodney W. Grimes 	/*
276df8bae1dSRodney W. Grimes 	 * Hasn't changed size
277df8bae1dSRodney W. Grimes 	 */
27824a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
279df8bae1dSRodney W. Grimes 		return;
280bbc0ec52SDavid Greenman 
281df8bae1dSRodney W. Grimes 	/*
282bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
283df8bae1dSRodney W. Grimes 	 */
28424a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
285a316d390SJohn Dyson 		vm_ooffset_t nsizerounded;
286aa8de40aSPoul-Henning Kamp 		nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_MASK));
287a316d390SJohn Dyson 		if (nsizerounded < object->un_pager.vnp.vnp_size) {
2881efb74fbSJohn Dyson 			vm_pindex_t st, end;
2891efb74fbSJohn Dyson 			st = OFF_TO_IDX(nsize + PAGE_MASK);
2901efb74fbSJohn Dyson 			end = OFF_TO_IDX(object->un_pager.vnp.vnp_size);
2911efb74fbSJohn Dyson 
2921efb74fbSJohn Dyson 			vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
2931efb74fbSJohn Dyson 			vm_object_page_remove(object, st, end, FALSE);
2940d94caffSDavid Greenman 		}
295bbc0ec52SDavid Greenman 		/*
296bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
297bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
298bbc0ec52SDavid Greenman 		 */
299bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
300bbc0ec52SDavid Greenman 			vm_offset_t kva;
301bbc0ec52SDavid Greenman 			vm_page_t m;
302bbc0ec52SDavid Greenman 
303a316d390SJohn Dyson 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
304bbc0ec52SDavid Greenman 			if (m) {
305bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
306bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
307a316d390SJohn Dyson 				    (int) (round_page(nsize) - nsize));
308bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
309bbc0ec52SDavid Greenman 			}
310bbc0ec52SDavid Greenman 		}
311bbc0ec52SDavid Greenman 	}
312a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
313aa8de40aSPoul-Henning Kamp 	object->size = OFF_TO_IDX(nsize + PAGE_MASK);
314df8bae1dSRodney W. Grimes }
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes void
31726f9a767SRodney W. Grimes vnode_pager_freepage(m)
31826f9a767SRodney W. Grimes 	vm_page_t m;
319df8bae1dSRodney W. Grimes {
32026f9a767SRodney W. Grimes 	vm_page_free(m);
32126f9a767SRodney W. Grimes }
32226f9a767SRodney W. Grimes 
32326f9a767SRodney W. Grimes /*
32426f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
32526f9a767SRodney W. Grimes  * file address
32626f9a767SRodney W. Grimes  */
327f708ef1bSPoul-Henning Kamp static vm_offset_t
328efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
32926f9a767SRodney W. Grimes 	struct vnode *vp;
330a316d390SJohn Dyson 	vm_ooffset_t address;
331efc68ce1SDavid Greenman 	int *run;
33226f9a767SRodney W. Grimes {
33326f9a767SRodney W. Grimes 	int rtaddress;
33426f9a767SRodney W. Grimes 	int bsize;
335a316d390SJohn Dyson 	daddr_t block;
33626f9a767SRodney W. Grimes 	struct vnode *rtvp;
33726f9a767SRodney W. Grimes 	int err;
338a316d390SJohn Dyson 	daddr_t vblock;
339a316d390SJohn Dyson 	int voffset;
34026f9a767SRodney W. Grimes 
3410d94caffSDavid Greenman 	if ((int) address < 0)
3420d94caffSDavid Greenman 		return -1;
3430d94caffSDavid Greenman 
3442c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3452c4488fcSJohn Dyson 		return -1;
3462c4488fcSJohn Dyson 
34726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
34826f9a767SRodney W. Grimes 	vblock = address / bsize;
34926f9a767SRodney W. Grimes 	voffset = address % bsize;
35026f9a767SRodney W. Grimes 
351c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
35226f9a767SRodney W. Grimes 
353efc68ce1SDavid Greenman 	if (err || (block == -1))
35426f9a767SRodney W. Grimes 		rtaddress = -1;
355efc68ce1SDavid Greenman 	else {
356187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
357efc68ce1SDavid Greenman 		if( run) {
358efc68ce1SDavid Greenman 			*run += 1;
359efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
360efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
361efc68ce1SDavid Greenman 		}
362efc68ce1SDavid Greenman 	}
36326f9a767SRodney W. Grimes 
36426f9a767SRodney W. Grimes 	return rtaddress;
36526f9a767SRodney W. Grimes }
36626f9a767SRodney W. Grimes 
36726f9a767SRodney W. Grimes /*
36826f9a767SRodney W. Grimes  * interrupt routine for I/O completion
36926f9a767SRodney W. Grimes  */
370f708ef1bSPoul-Henning Kamp static void
37126f9a767SRodney W. Grimes vnode_pager_iodone(bp)
37226f9a767SRodney W. Grimes 	struct buf *bp;
37326f9a767SRodney W. Grimes {
37426f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
37524a1cce3SDavid Greenman 	wakeup(bp);
37626f9a767SRodney W. Grimes }
37726f9a767SRodney W. Grimes 
37826f9a767SRodney W. Grimes /*
37926f9a767SRodney W. Grimes  * small block file system vnode pager input
38026f9a767SRodney W. Grimes  */
381f708ef1bSPoul-Henning Kamp static int
38224a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
38324a1cce3SDavid Greenman 	vm_object_t object;
38426f9a767SRodney W. Grimes 	vm_page_t m;
38526f9a767SRodney W. Grimes {
38626f9a767SRodney W. Grimes 	int i;
38726f9a767SRodney W. Grimes 	int s;
38826f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
38926f9a767SRodney W. Grimes 	struct buf *bp;
39026f9a767SRodney W. Grimes 	vm_offset_t kva;
39126f9a767SRodney W. Grimes 	int fileaddr;
39226f9a767SRodney W. Grimes 	vm_offset_t bsize;
39326f9a767SRodney W. Grimes 	int error = 0;
39426f9a767SRodney W. Grimes 
39524a1cce3SDavid Greenman 	vp = object->handle;
3962c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3972c4488fcSJohn Dyson 		return VM_PAGER_BAD;
3982c4488fcSJohn Dyson 
39926f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
40026f9a767SRodney W. Grimes 
4010bdb7528SDavid Greenman 
402c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
40326f9a767SRodney W. Grimes 
40426f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
40526f9a767SRodney W. Grimes 
40626f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
407bbc0ec52SDavid Greenman 
408a316d390SJohn Dyson 		if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
40926f9a767SRodney W. Grimes 			continue;
41026f9a767SRodney W. Grimes 
411a316d390SJohn Dyson 		fileaddr = vnode_pager_addr(vp,
412a316d390SJohn Dyson 			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
41326f9a767SRodney W. Grimes 		if (fileaddr != -1) {
41426f9a767SRodney W. Grimes 			bp = getpbuf();
41526f9a767SRodney W. Grimes 
41626f9a767SRodney W. Grimes 			/* build a minimal buffer header */
41726f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
41826f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
41926f9a767SRodney W. Grimes 			bp->b_proc = curproc;
42026f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
42126f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
42226f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
42326f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
42426f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
425ab3f7469SPoul-Henning Kamp 			bp->b_data = (caddr_t) kva + i * bsize;
426187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4270d94caffSDavid Greenman 			pbgetvp(dp, bp);
42826f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
42926f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
43026f9a767SRodney W. Grimes 
43126f9a767SRodney W. Grimes 			/* do the input */
43226f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
43326f9a767SRodney W. Grimes 
43426f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
43526f9a767SRodney W. Grimes 
43626f9a767SRodney W. Grimes 			s = splbio();
43726f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
438aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
43926f9a767SRodney W. Grimes 			}
44026f9a767SRodney W. Grimes 			splx(s);
44126f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
44226f9a767SRodney W. Grimes 				error = EIO;
44326f9a767SRodney W. Grimes 
44426f9a767SRodney W. Grimes 			/*
44526f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
44626f9a767SRodney W. Grimes 			 */
44726f9a767SRodney W. Grimes 			relpbuf(bp);
44826f9a767SRodney W. Grimes 			if (error)
44926f9a767SRodney W. Grimes 				break;
4500d94caffSDavid Greenman 
451aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
45226f9a767SRodney W. Grimes 		} else {
453aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
45426f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
45526f9a767SRodney W. Grimes 		}
45626f9a767SRodney W. Grimes 	}
45726f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
45867bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
459b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
46026f9a767SRodney W. Grimes 	if (error) {
461a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
46226f9a767SRodney W. Grimes 	}
46326f9a767SRodney W. Grimes 	return VM_PAGER_OK;
46426f9a767SRodney W. Grimes 
46526f9a767SRodney W. Grimes }
46626f9a767SRodney W. Grimes 
46726f9a767SRodney W. Grimes 
46826f9a767SRodney W. Grimes /*
46926f9a767SRodney W. Grimes  * old style vnode pager output routine
47026f9a767SRodney W. Grimes  */
471f708ef1bSPoul-Henning Kamp static int
47224a1cce3SDavid Greenman vnode_pager_input_old(object, m)
47324a1cce3SDavid Greenman 	vm_object_t object;
47426f9a767SRodney W. Grimes 	vm_page_t m;
47526f9a767SRodney W. Grimes {
476df8bae1dSRodney W. Grimes 	struct uio auio;
477df8bae1dSRodney W. Grimes 	struct iovec aiov;
47826f9a767SRodney W. Grimes 	int error;
47926f9a767SRodney W. Grimes 	int size;
48026f9a767SRodney W. Grimes 	vm_offset_t kva;
481df8bae1dSRodney W. Grimes 
48226f9a767SRodney W. Grimes 	error = 0;
483bbc0ec52SDavid Greenman 
484df8bae1dSRodney W. Grimes 	/*
48526f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
48626f9a767SRodney W. Grimes 	 */
487a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
48826f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
48926f9a767SRodney W. Grimes 	} else {
49026f9a767SRodney W. Grimes 		size = PAGE_SIZE;
491a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
492a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
4930bdb7528SDavid Greenman 
49426f9a767SRodney W. Grimes 		/*
495df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
496df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
497df8bae1dSRodney W. Grimes 		 */
49826f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
4990bdb7528SDavid Greenman 
500df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
501df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
502df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
503df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
504a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
505df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
50626f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
507df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
508df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
50926f9a767SRodney W. Grimes 
51024a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
511df8bae1dSRodney W. Grimes 		if (!error) {
512df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes 			if (count == 0)
515df8bae1dSRodney W. Grimes 				error = EINVAL;
51626f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
51726f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
518df8bae1dSRodney W. Grimes 		}
51926f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
520df8bae1dSRodney W. Grimes 	}
52167bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5220d94caffSDavid Greenman 	m->dirty = 0;
523b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
524a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
52526f9a767SRodney W. Grimes }
52626f9a767SRodney W. Grimes 
52726f9a767SRodney W. Grimes /*
52826f9a767SRodney W. Grimes  * generic vnode pager input routine
52926f9a767SRodney W. Grimes  */
530170db9c6SJohn Dyson 
531f708ef1bSPoul-Henning Kamp static int
53224a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
53326f9a767SRodney W. Grimes 	vm_object_t object;
53424a1cce3SDavid Greenman 	vm_page_t *m;
53524a1cce3SDavid Greenman 	int count;
53624a1cce3SDavid Greenman 	int reqpage;
53724a1cce3SDavid Greenman {
538170db9c6SJohn Dyson 	int rtval;
539170db9c6SJohn Dyson 	struct vnode *vp;
54095e5e988SJohn Dyson 
541170db9c6SJohn Dyson 	vp = object->handle;
5422c4488fcSJohn Dyson 	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
543170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
5440b8253a7SBruce Evans 		return vnode_pager_leaf_getpages(object, m, count, reqpage);
545170db9c6SJohn Dyson 	else
546170db9c6SJohn Dyson 		return rtval;
547170db9c6SJohn Dyson }
548170db9c6SJohn Dyson 
549170db9c6SJohn Dyson static int
550170db9c6SJohn Dyson vnode_pager_leaf_getpages(object, m, count, reqpage)
551170db9c6SJohn Dyson 	vm_object_t object;
552170db9c6SJohn Dyson 	vm_page_t *m;
553170db9c6SJohn Dyson 	int count;
554170db9c6SJohn Dyson 	int reqpage;
555170db9c6SJohn Dyson {
556a316d390SJohn Dyson 	vm_offset_t kva;
557a316d390SJohn Dyson 	off_t foff;
55824a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
55926f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
560efc68ce1SDavid Greenman 	int runpg;
561efc68ce1SDavid Greenman 	int runend;
5620bdb7528SDavid Greenman 	struct buf *bp;
56326f9a767SRodney W. Grimes 	int s;
56426f9a767SRodney W. Grimes 	int error = 0;
56526f9a767SRodney W. Grimes 
56624a1cce3SDavid Greenman 	vp = object->handle;
5672c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
5682c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5692c4488fcSJohn Dyson 
57026f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
57126f9a767SRodney W. Grimes 
57226f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
573bbc0ec52SDavid Greenman 
57426f9a767SRodney W. Grimes 	/*
575bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
576bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
57726f9a767SRodney W. Grimes 	 */
578a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
579bbc0ec52SDavid Greenman 
58026f9a767SRodney W. Grimes 	/*
58116f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
58226f9a767SRodney W. Grimes 	 */
583c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
58426f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
58526f9a767SRodney W. Grimes 			if (i != reqpage) {
58626f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
58726f9a767SRodney W. Grimes 			}
58826f9a767SRodney W. Grimes 		}
589976e77fcSDavid Greenman 		cnt.v_vnodein++;
590976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
59124a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
592bbc0ec52SDavid Greenman 
59326f9a767SRodney W. Grimes 		/*
59426f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
59526f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
59626f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
59726f9a767SRodney W. Grimes 		 */
59826f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
59926f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
60026f9a767SRodney W. Grimes 
60126f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
60226f9a767SRodney W. Grimes 			if (i != reqpage) {
60326f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
60426f9a767SRodney W. Grimes 			}
60526f9a767SRodney W. Grimes 		}
606976e77fcSDavid Greenman 		cnt.v_vnodein++;
607976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
60824a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
60926f9a767SRodney W. Grimes 	}
61026f9a767SRodney W. Grimes 	/*
6110d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
6120d94caffSDavid Greenman 	 * then, the entire page is valid --
61332ad9cb5SDoug Rabson 	 * XXX no it isn't
6140d94caffSDavid Greenman 	 */
61532ad9cb5SDoug Rabson 
61632ad9cb5SDoug Rabson 	if (m[reqpage]->valid != VM_PAGE_BITS_ALL)
61732ad9cb5SDoug Rabson 	    m[reqpage]->valid = 0;
61832ad9cb5SDoug Rabson 
6190d94caffSDavid Greenman 	if (m[reqpage]->valid) {
6200d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
6210d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6220d94caffSDavid Greenman 			if (i != reqpage)
6230d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6240d94caffSDavid Greenman 		}
6250d94caffSDavid Greenman 		return VM_PAGER_OK;
6260d94caffSDavid Greenman 	}
6270bdb7528SDavid Greenman 
6280d94caffSDavid Greenman 	/*
62926f9a767SRodney W. Grimes 	 * here on direct device I/O
63026f9a767SRodney W. Grimes 	 */
63126f9a767SRodney W. Grimes 
632efc68ce1SDavid Greenman 	firstaddr = -1;
63326f9a767SRodney W. Grimes 	/*
634efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
63526f9a767SRodney W. Grimes 	 */
636efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
637a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
638a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
639efc68ce1SDavid Greenman 		if (firstaddr == -1) {
64024a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
64195e5e988SJohn Dyson 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
64224a1cce3SDavid Greenman 			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
643efc68ce1SDavid Greenman 			}
64426f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
645efc68ce1SDavid Greenman 			runend = i + 1;
646efc68ce1SDavid Greenman 			first = runend;
647efc68ce1SDavid Greenman 			continue;
648efc68ce1SDavid Greenman 		}
649efc68ce1SDavid Greenman 		runend = i + runpg;
650efc68ce1SDavid Greenman 		if (runend <= reqpage) {
651efc68ce1SDavid Greenman 			int j;
652efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
653efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
654efc68ce1SDavid Greenman 			}
65526f9a767SRodney W. Grimes 		} else {
656efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
657efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
65826f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
659efc68ce1SDavid Greenman 				count = first + runpg;
66026f9a767SRodney W. Grimes 			}
661efc68ce1SDavid Greenman 			break;
66226f9a767SRodney W. Grimes 		}
663efc68ce1SDavid Greenman 		first = runend;
664efc68ce1SDavid Greenman 	}
66526f9a767SRodney W. Grimes 
66626f9a767SRodney W. Grimes 	/*
667bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
668bbc0ec52SDavid Greenman 	 * to be zero based...
66926f9a767SRodney W. Grimes 	 */
67026f9a767SRodney W. Grimes 	if (first != 0) {
67126f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
67226f9a767SRodney W. Grimes 			m[i - first] = m[i];
67326f9a767SRodney W. Grimes 		}
67426f9a767SRodney W. Grimes 		count -= first;
67526f9a767SRodney W. Grimes 		reqpage -= first;
67626f9a767SRodney W. Grimes 	}
677efc68ce1SDavid Greenman 
67826f9a767SRodney W. Grimes 	/*
67926f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
68026f9a767SRodney W. Grimes 	 */
681a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
68226f9a767SRodney W. Grimes 
68326f9a767SRodney W. Grimes 	/*
68426f9a767SRodney W. Grimes 	 * calculate the size of the transfer
68526f9a767SRodney W. Grimes 	 */
68626f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
68724a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
68824a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
68926f9a767SRodney W. Grimes 
69026f9a767SRodney W. Grimes 	/*
69126f9a767SRodney W. Grimes 	 * round up physical size for real devices
69226f9a767SRodney W. Grimes 	 */
69326f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
69426f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
69526f9a767SRodney W. Grimes 
6966d40c3d3SDavid Greenman 	bp = getpbuf();
69716f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
69816f62314SDavid Greenman 
69926f9a767SRodney W. Grimes 	/*
70026f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
70126f9a767SRodney W. Grimes 	 */
70216f62314SDavid Greenman 	pmap_qenter(kva, m, count);
70326f9a767SRodney W. Grimes 
70426f9a767SRodney W. Grimes 	/* build a minimal buffer header */
70526f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
70626f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
70726f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
70826f9a767SRodney W. Grimes 	bp->b_proc = curproc;
70926f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
71026f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
71126f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
71226f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
71326f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
714187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7150d94caffSDavid Greenman 	pbgetvp(dp, bp);
71626f9a767SRodney W. Grimes 	bp->b_bcount = size;
71726f9a767SRodney W. Grimes 	bp->b_bufsize = size;
71826f9a767SRodney W. Grimes 
719976e77fcSDavid Greenman 	cnt.v_vnodein++;
720976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
721976e77fcSDavid Greenman 
72226f9a767SRodney W. Grimes 	/* do the input */
72326f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
724976e77fcSDavid Greenman 
72526f9a767SRodney W. Grimes 	s = splbio();
72626f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
72726f9a767SRodney W. Grimes 
72826f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
729aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
73026f9a767SRodney W. Grimes 	}
73126f9a767SRodney W. Grimes 	splx(s);
73226f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
73326f9a767SRodney W. Grimes 		error = EIO;
73426f9a767SRodney W. Grimes 
73526f9a767SRodney W. Grimes 	if (!error) {
73626f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
73726f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
73826f9a767SRodney W. Grimes 	}
73916f62314SDavid Greenman 	pmap_qremove(kva, count);
74026f9a767SRodney W. Grimes 
74126f9a767SRodney W. Grimes 	/*
74226f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
74326f9a767SRodney W. Grimes 	 */
74426f9a767SRodney W. Grimes 	relpbuf(bp);
74526f9a767SRodney W. Grimes 
74626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
74767bf6868SJohn Dyson 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
7480d94caffSDavid Greenman 		m[i]->dirty = 0;
7490d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
750b1fc01b7SJohn Dyson 		m[i]->flags &= ~PG_ZERO;
75126f9a767SRodney W. Grimes 		if (i != reqpage) {
752bbc0ec52SDavid Greenman 
75326f9a767SRodney W. Grimes 			/*
754bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
755bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
756bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
757bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
758bbc0ec52SDavid Greenman 			 * deactivating pages is best.
75926f9a767SRodney W. Grimes 			 */
760bbc0ec52SDavid Greenman 
76126f9a767SRodney W. Grimes 			/*
762bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
763bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
76426f9a767SRodney W. Grimes 			 */
76526f9a767SRodney W. Grimes 			if (!error) {
76695461b45SJohn Dyson 				if (m[i]->flags & PG_WANTED)
76795461b45SJohn Dyson 					vm_page_activate(m[i]);
76895461b45SJohn Dyson 				else
76926f9a767SRodney W. Grimes 					vm_page_deactivate(m[i]);
77026f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
77126f9a767SRodney W. Grimes 			} else {
77226f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
77326f9a767SRodney W. Grimes 			}
77426f9a767SRodney W. Grimes 		}
77526f9a767SRodney W. Grimes 	}
77626f9a767SRodney W. Grimes 	if (error) {
77724a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
77826f9a767SRodney W. Grimes 	}
779a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
78026f9a767SRodney W. Grimes }
78126f9a767SRodney W. Grimes 
782f708ef1bSPoul-Henning Kamp static int
783170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
784170db9c6SJohn Dyson 	vm_object_t object;
785170db9c6SJohn Dyson 	vm_page_t *m;
786170db9c6SJohn Dyson 	int count;
787170db9c6SJohn Dyson 	boolean_t sync;
788170db9c6SJohn Dyson 	int *rtvals;
789170db9c6SJohn Dyson {
790170db9c6SJohn Dyson 	int rtval;
791170db9c6SJohn Dyson 	struct vnode *vp;
792ad980522SJohn Dyson 
793170db9c6SJohn Dyson 	vp = object->handle;
7942c4488fcSJohn Dyson 	rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
795170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
7960b8253a7SBruce Evans 		return vnode_pager_leaf_putpages(object, m, count, sync, rtvals);
797170db9c6SJohn Dyson 	else
798170db9c6SJohn Dyson 		return rtval;
799170db9c6SJohn Dyson }
800170db9c6SJohn Dyson 
80126f9a767SRodney W. Grimes /*
80226f9a767SRodney W. Grimes  * generic vnode pager output routine
80326f9a767SRodney W. Grimes  */
804170db9c6SJohn Dyson static int
805170db9c6SJohn Dyson vnode_pager_leaf_putpages(object, m, count, sync, rtvals)
80624a1cce3SDavid Greenman 	vm_object_t object;
80726f9a767SRodney W. Grimes 	vm_page_t *m;
80826f9a767SRodney W. Grimes 	int count;
80924a1cce3SDavid Greenman 	boolean_t sync;
81026f9a767SRodney W. Grimes 	int *rtvals;
81126f9a767SRodney W. Grimes {
812f6b04d2bSDavid Greenman 	int i;
81326f9a767SRodney W. Grimes 
814f6b04d2bSDavid Greenman 	struct vnode *vp;
815f6b04d2bSDavid Greenman 	int maxsize, ncount;
816a316d390SJohn Dyson 	vm_ooffset_t poffset;
817f6b04d2bSDavid Greenman 	struct uio auio;
818f6b04d2bSDavid Greenman 	struct iovec aiov;
819f6b04d2bSDavid Greenman 	int error;
82026f9a767SRodney W. Grimes 
82124a1cce3SDavid Greenman 	vp = object->handle;;
82226f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
82326f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
82426f9a767SRodney W. Grimes 
825a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
826a316d390SJohn Dyson 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty);
827f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
828f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
8290d94caffSDavid Greenman 	}
8300bdb7528SDavid Greenman 
831f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
832f6b04d2bSDavid Greenman 	ncount = count;
83326f9a767SRodney W. Grimes 
834a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
835a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
836a316d390SJohn Dyson 		if (object->un_pager.vnp.vnp_size > poffset)
837a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
8385f55e841SDavid Greenman 		else
8395f55e841SDavid Greenman 			maxsize = 0;
840aa8de40aSPoul-Henning Kamp 		ncount = btoc(maxsize);
841f6b04d2bSDavid Greenman 		if (ncount < count) {
842f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
843f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
844f6b04d2bSDavid Greenman 			}
845a316d390SJohn Dyson #ifdef BOGUS
846f6b04d2bSDavid Greenman 			if (ncount == 0) {
847a316d390SJohn Dyson 				printf("vnode_pager_putpages: write past end of file: %d, %lu\n",
848a316d390SJohn Dyson 					poffset,
849a316d390SJohn Dyson 					(unsigned long) object->un_pager.vnp.vnp_size);
85026f9a767SRodney W. Grimes 				return rtvals[0];
85126f9a767SRodney W. Grimes 			}
852a316d390SJohn Dyson #endif
853f6b04d2bSDavid Greenman 		}
854f6b04d2bSDavid Greenman 	}
85526f9a767SRodney W. Grimes 
85626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8575f55e841SDavid Greenman 		m[i]->busy++;
858f6b04d2bSDavid Greenman 		m[i]->flags &= ~PG_BUSY;
85926f9a767SRodney W. Grimes 	}
860f6b04d2bSDavid Greenman 
861f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
862f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
863f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
864f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
865a316d390SJohn Dyson 	auio.uio_offset = poffset;
866f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
867f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
868f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
869f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
870a316d390SJohn Dyson 	error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred);
871976e77fcSDavid Greenman 	cnt.v_vnodeout++;
872f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
873f6b04d2bSDavid Greenman 
874f6b04d2bSDavid Greenman 	if (error) {
87524a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
876f6b04d2bSDavid Greenman 	}
877f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
878f708ef1bSPoul-Henning Kamp 		printf("vnode_pager_putpages: residual I/O %d at %ld\n",
879a316d390SJohn Dyson 			auio.uio_resid, m[0]->pindex);
88026f9a767SRodney W. Grimes 	}
88126f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8825f55e841SDavid Greenman 		m[i]->busy--;
883f6b04d2bSDavid Greenman 		if (i < ncount) {
88426f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
88526f9a767SRodney W. Grimes 		}
88695461b45SJohn Dyson 		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED)) {
88795461b45SJohn Dyson 			vm_page_activate(m[i]);
88824a1cce3SDavid Greenman 			wakeup(m[i]);
88926f9a767SRodney W. Grimes 		}
89095461b45SJohn Dyson 	}
891f6b04d2bSDavid Greenman 	return rtvals[0];
89226f9a767SRodney W. Grimes }
893f6b04d2bSDavid Greenman 
894f6b04d2bSDavid Greenman struct vnode *
89524a1cce3SDavid Greenman vnode_pager_lock(object)
89624a1cce3SDavid Greenman 	vm_object_t object;
89724a1cce3SDavid Greenman {
898996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
899996c772fSJohn Dyson 
90024a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
90124a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
902f6b04d2bSDavid Greenman 			continue;
90347221757SJohn Dyson 		if (object->flags & OBJ_DEAD)
90447221757SJohn Dyson 			return NULL;
905f6b04d2bSDavid Greenman 
90647221757SJohn Dyson 		while (vget(object->handle,
90747221757SJohn Dyson 			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) {
90847221757SJohn Dyson 			printf("vnode_pager_lock: retrying\n");
90947221757SJohn Dyson 		}
91024a1cce3SDavid Greenman 		return object->handle;
91126f9a767SRodney W. Grimes 	}
91224a1cce3SDavid Greenman 	return NULL;
913f6b04d2bSDavid Greenman }
914