xref: /freebsd/sys/vm/vnode_pager.c (revision 95e5e988e0185323f57093b8cb1202e130b57314)
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
4195e5e988SJohn Dyson  *	$Id: vnode_pager.c,v 1.78 1997/12/29 00:25:11 dyson 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 
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>
5924a1cce3SDavid Greenman #include <sys/buf.h>
60efeaf95aSDavid Greenman #include <sys/vmmeter.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <vm/vm.h>
63efeaf95aSDavid Greenman #include <vm/vm_prot.h>
64efeaf95aSDavid Greenman #include <vm/vm_object.h>
65df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
6624a1cce3SDavid Greenman #include <vm/vm_pager.h>
671efb74fbSJohn Dyson #include <vm/vm_map.h>
68df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
69efeaf95aSDavid Greenman #include <vm/vm_extern.h>
70df8bae1dSRodney W. Grimes 
71f708ef1bSPoul-Henning Kamp static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
720b8253a7SBruce Evans 					 int *run));
73f708ef1bSPoul-Henning Kamp static void vnode_pager_iodone __P((struct buf *bp));
74f708ef1bSPoul-Henning Kamp static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
75f708ef1bSPoul-Henning Kamp static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
76f708ef1bSPoul-Henning Kamp static void vnode_pager_dealloc __P((vm_object_t));
77f708ef1bSPoul-Henning Kamp static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
78f708ef1bSPoul-Henning Kamp static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
79f708ef1bSPoul-Henning Kamp static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
800b8253a7SBruce Evans 
81df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
8224a1cce3SDavid Greenman 	NULL,
83df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
84df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
8524a1cce3SDavid Greenman 	vnode_pager_getpages,
8624a1cce3SDavid Greenman 	vnode_pager_putpages,
8724a1cce3SDavid Greenman 	vnode_pager_haspage,
8824a1cce3SDavid Greenman 	NULL
89df8bae1dSRodney W. Grimes };
90df8bae1dSRodney W. Grimes 
910b8253a7SBruce Evans static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m,
920b8253a7SBruce Evans 					  int count, int reqpage));
930b8253a7SBruce Evans static int vnode_pager_leaf_putpages __P((vm_object_t object, vm_page_t *m,
940b8253a7SBruce Evans 					  int count, boolean_t sync,
950b8253a7SBruce Evans 					  int *rtvals));
96170db9c6SJohn Dyson 
97df8bae1dSRodney W. Grimes /*
98df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
99df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
100df8bae1dSRodney W. Grimes  */
10124a1cce3SDavid Greenman vm_object_t
102b9dcd593SBruce Evans vnode_pager_alloc(void *handle, vm_size_t size, vm_prot_t prot,
103b9dcd593SBruce Evans 		  vm_ooffset_t offset)
104df8bae1dSRodney W. Grimes {
10506cb7259SDavid Greenman 	vm_object_t object;
106df8bae1dSRodney W. Grimes 	struct vnode *vp;
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes 	/*
109df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
110df8bae1dSRodney W. Grimes 	 */
111df8bae1dSRodney W. Grimes 	if (handle == NULL)
112df8bae1dSRodney W. Grimes 		return (NULL);
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
11539d38f93SDavid Greenman 
11639d38f93SDavid Greenman 	/*
11739d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
11839d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
11939d38f93SDavid Greenman 	 */
12039d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
12139d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
12239d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
12339d38f93SDavid Greenman 	}
12439d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
12539d38f93SDavid Greenman 
12639d38f93SDavid Greenman 	/*
12739d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
12839d38f93SDavid Greenman 	 * go away.
12939d38f93SDavid Greenman 	 */
130bd7e5f99SJohn Dyson 	while (((object = vp->v_object) != NULL) &&
131bd7e5f99SJohn Dyson 		(object->flags & OBJ_DEAD)) {
132aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
13324a1cce3SDavid Greenman 	}
1340d94caffSDavid Greenman 
1352be70f79SJohn Dyson 	if (vp->v_usecount == 0)
1362be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
1372be70f79SJohn Dyson 
13824a1cce3SDavid Greenman 	if (object == NULL) {
139df8bae1dSRodney W. Grimes 		/*
140df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
141df8bae1dSRodney W. Grimes 		 */
142a316d390SJohn Dyson 		object = vm_object_allocate(OBJT_VNODE, size);
143ad5dd234SJohn Dyson 		object->flags = 0;
144bbc0ec52SDavid Greenman 
145a316d390SJohn Dyson 		object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE;
14626f9a767SRodney W. Grimes 
14724a1cce3SDavid Greenman 		object->handle = handle;
14824a1cce3SDavid Greenman 		vp->v_object = object;
14995e5e988SJohn Dyson 		vp->v_usecount++;
150df8bae1dSRodney W. Grimes 	} else {
15195e5e988SJohn Dyson 		object->ref_count++;
15295e5e988SJohn Dyson 		vp->v_usecount++;
153df8bae1dSRodney W. Grimes 	}
15439d38f93SDavid Greenman 
15539d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
15639d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
15739d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
15839d38f93SDavid Greenman 		wakeup(vp);
15939d38f93SDavid Greenman 	}
16024a1cce3SDavid Greenman 	return (object);
161df8bae1dSRodney W. Grimes }
162df8bae1dSRodney W. Grimes 
163f708ef1bSPoul-Henning Kamp static void
16424a1cce3SDavid Greenman vnode_pager_dealloc(object)
1650d94caffSDavid Greenman 	vm_object_t object;
16624a1cce3SDavid Greenman {
16724a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
168df8bae1dSRodney W. Grimes 
16924a1cce3SDavid Greenman 	if (vp == NULL)
17024a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
17124a1cce3SDavid Greenman 
17224a1cce3SDavid Greenman 	if (object->paging_in_progress) {
1730d94caffSDavid Greenman 		int s = splbio();
1740d94caffSDavid Greenman 		while (object->paging_in_progress) {
175c0503609SDavid Greenman 			object->flags |= OBJ_PIPWNT;
1760d94caffSDavid Greenman 			tsleep(object, PVM, "vnpdea", 0);
1770d94caffSDavid Greenman 		}
1780d94caffSDavid Greenman 		splx(s);
17924a1cce3SDavid Greenman 	}
18024a1cce3SDavid Greenman 
18195e5e988SJohn Dyson 	object->flags |= OBJ_DEAD;
18224a1cce3SDavid Greenman 	object->handle = NULL;
18395e5e988SJohn Dyson 	object->type = OBJT_DEFAULT;
184aa2cabb9SDavid Greenman 	vp->v_object = NULL;
18595e5e988SJohn Dyson 	vp->v_flag &= ~(VTEXT|VOBJBUF);
186df8bae1dSRodney W. Grimes }
18726f9a767SRodney W. Grimes 
188f708ef1bSPoul-Henning Kamp static boolean_t
189a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
19024a1cce3SDavid Greenman 	vm_object_t object;
191a316d390SJohn Dyson 	vm_pindex_t pindex;
19224a1cce3SDavid Greenman 	int *before;
19324a1cce3SDavid Greenman 	int *after;
194df8bae1dSRodney W. Grimes {
19524a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
196df8bae1dSRodney W. Grimes 	daddr_t bn;
1973af76890SPoul-Henning Kamp 	int err;
198170db9c6SJohn Dyson 	daddr_t reqblock;
1992c4488fcSJohn Dyson 	int poff;
2002c4488fcSJohn Dyson 	int bsize;
201d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
202df8bae1dSRodney W. Grimes 
203df8bae1dSRodney W. Grimes 	/*
2040d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
2050d94caffSDavid Greenman 	 * not have the page.
206df8bae1dSRodney W. Grimes 	 */
207a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
208a316d390SJohn Dyson 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2094abc71c0SDavid Greenman 		return FALSE;
210df8bae1dSRodney W. Grimes 
211eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
212170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
213d63596ceSJohn Dyson 	blocksperpage = 0;
214d63596ceSJohn Dyson 	if (pagesperblock > 0) {
215a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
216d63596ceSJohn Dyson 	} else {
217d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
218d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
219d63596ceSJohn Dyson 	}
220170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
221170db9c6SJohn Dyson 		after, before);
2220d94caffSDavid Greenman 	if (err)
22324a1cce3SDavid Greenman 		return TRUE;
2246eab77f2SJohn Dyson 	if ( bn == -1)
225ced399eeSJohn Dyson 		return FALSE;
226d63596ceSJohn Dyson 	if (pagesperblock > 0) {
227a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
228170db9c6SJohn Dyson 		if (before) {
229170db9c6SJohn Dyson 			*before *= pagesperblock;
230170db9c6SJohn Dyson 			*before += poff;
231170db9c6SJohn Dyson 		}
232170db9c6SJohn Dyson 		if (after) {
233b1fc01b7SJohn Dyson 			int numafter;
234170db9c6SJohn Dyson 			*after *= pagesperblock;
235b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
236a316d390SJohn Dyson 			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
237a316d390SJohn Dyson 				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
238b1fc01b7SJohn Dyson 			}
239b1fc01b7SJohn Dyson 			*after += numafter;
240170db9c6SJohn Dyson 		}
241d63596ceSJohn Dyson 	} else {
242d63596ceSJohn Dyson 		if (before) {
243d63596ceSJohn Dyson 			*before /= blocksperpage;
244d63596ceSJohn Dyson 		}
245d63596ceSJohn Dyson 
246d63596ceSJohn Dyson 		if (after) {
247d63596ceSJohn Dyson 			*after /= blocksperpage;
248d63596ceSJohn Dyson 		}
249d63596ceSJohn Dyson 	}
250ced399eeSJohn Dyson 	return TRUE;
251df8bae1dSRodney W. Grimes }
252df8bae1dSRodney W. Grimes 
253df8bae1dSRodney W. Grimes /*
254df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
25524a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
256df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
257df8bae1dSRodney W. Grimes  *
258df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
259df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
260df8bae1dSRodney W. Grimes  */
261df8bae1dSRodney W. Grimes void
262df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
263df8bae1dSRodney W. Grimes 	struct vnode *vp;
264a316d390SJohn Dyson 	vm_ooffset_t nsize;
265df8bae1dSRodney W. Grimes {
26624a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
267df8bae1dSRodney W. Grimes 
26824a1cce3SDavid Greenman 	if (object == NULL)
269df8bae1dSRodney W. Grimes 		return;
270bbc0ec52SDavid Greenman 
271df8bae1dSRodney W. Grimes 	/*
272df8bae1dSRodney W. Grimes 	 * Hasn't changed size
273df8bae1dSRodney W. Grimes 	 */
27424a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
275df8bae1dSRodney W. Grimes 		return;
276bbc0ec52SDavid Greenman 
277df8bae1dSRodney W. Grimes 	/*
278bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
279df8bae1dSRodney W. Grimes 	 */
28024a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
281a316d390SJohn Dyson 		vm_ooffset_t nsizerounded;
282aa8de40aSPoul-Henning Kamp 		nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_MASK));
283a316d390SJohn Dyson 		if (nsizerounded < object->un_pager.vnp.vnp_size) {
2841efb74fbSJohn Dyson 			vm_pindex_t st, end;
2851efb74fbSJohn Dyson 			st = OFF_TO_IDX(nsize + PAGE_MASK);
2861efb74fbSJohn Dyson 			end = OFF_TO_IDX(object->un_pager.vnp.vnp_size);
2871efb74fbSJohn Dyson 
2881efb74fbSJohn Dyson 			vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
2891efb74fbSJohn Dyson 			vm_object_page_remove(object, st, end, FALSE);
2900d94caffSDavid Greenman 		}
291bbc0ec52SDavid Greenman 		/*
292bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
293bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
294bbc0ec52SDavid Greenman 		 */
295bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
296bbc0ec52SDavid Greenman 			vm_offset_t kva;
297bbc0ec52SDavid Greenman 			vm_page_t m;
298bbc0ec52SDavid Greenman 
299a316d390SJohn Dyson 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
300bbc0ec52SDavid Greenman 			if (m) {
301bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
302bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
303a316d390SJohn Dyson 				    (int) (round_page(nsize) - nsize));
304bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
305bbc0ec52SDavid Greenman 			}
306bbc0ec52SDavid Greenman 		}
307bbc0ec52SDavid Greenman 	}
308a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
309aa8de40aSPoul-Henning Kamp 	object->size = OFF_TO_IDX(nsize + PAGE_MASK);
310df8bae1dSRodney W. Grimes }
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes void
31326f9a767SRodney W. Grimes vnode_pager_freepage(m)
31426f9a767SRodney W. Grimes 	vm_page_t m;
315df8bae1dSRodney W. Grimes {
31626f9a767SRodney W. Grimes 	PAGE_WAKEUP(m);
31726f9a767SRodney W. Grimes 	vm_page_free(m);
31826f9a767SRodney W. Grimes }
31926f9a767SRodney W. Grimes 
32026f9a767SRodney W. Grimes /*
32126f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
32226f9a767SRodney W. Grimes  * file address
32326f9a767SRodney W. Grimes  */
324f708ef1bSPoul-Henning Kamp static vm_offset_t
325efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
32626f9a767SRodney W. Grimes 	struct vnode *vp;
327a316d390SJohn Dyson 	vm_ooffset_t address;
328efc68ce1SDavid Greenman 	int *run;
32926f9a767SRodney W. Grimes {
33026f9a767SRodney W. Grimes 	int rtaddress;
33126f9a767SRodney W. Grimes 	int bsize;
332a316d390SJohn Dyson 	daddr_t block;
33326f9a767SRodney W. Grimes 	struct vnode *rtvp;
33426f9a767SRodney W. Grimes 	int err;
335a316d390SJohn Dyson 	daddr_t vblock;
336a316d390SJohn Dyson 	int voffset;
33726f9a767SRodney W. Grimes 
3380d94caffSDavid Greenman 	if ((int) address < 0)
3390d94caffSDavid Greenman 		return -1;
3400d94caffSDavid Greenman 
3412c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3422c4488fcSJohn Dyson 		return -1;
3432c4488fcSJohn Dyson 
34426f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
34526f9a767SRodney W. Grimes 	vblock = address / bsize;
34626f9a767SRodney W. Grimes 	voffset = address % bsize;
34726f9a767SRodney W. Grimes 
348c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
34926f9a767SRodney W. Grimes 
350efc68ce1SDavid Greenman 	if (err || (block == -1))
35126f9a767SRodney W. Grimes 		rtaddress = -1;
352efc68ce1SDavid Greenman 	else {
353187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
354efc68ce1SDavid Greenman 		if( run) {
355efc68ce1SDavid Greenman 			*run += 1;
356efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
357efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
358efc68ce1SDavid Greenman 		}
359efc68ce1SDavid Greenman 	}
36026f9a767SRodney W. Grimes 
36126f9a767SRodney W. Grimes 	return rtaddress;
36226f9a767SRodney W. Grimes }
36326f9a767SRodney W. Grimes 
36426f9a767SRodney W. Grimes /*
36526f9a767SRodney W. Grimes  * interrupt routine for I/O completion
36626f9a767SRodney W. Grimes  */
367f708ef1bSPoul-Henning Kamp static void
36826f9a767SRodney W. Grimes vnode_pager_iodone(bp)
36926f9a767SRodney W. Grimes 	struct buf *bp;
37026f9a767SRodney W. Grimes {
37126f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
37224a1cce3SDavid Greenman 	wakeup(bp);
37326f9a767SRodney W. Grimes }
37426f9a767SRodney W. Grimes 
37526f9a767SRodney W. Grimes /*
37626f9a767SRodney W. Grimes  * small block file system vnode pager input
37726f9a767SRodney W. Grimes  */
378f708ef1bSPoul-Henning Kamp static int
37924a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
38024a1cce3SDavid Greenman 	vm_object_t object;
38126f9a767SRodney W. Grimes 	vm_page_t m;
38226f9a767SRodney W. Grimes {
38326f9a767SRodney W. Grimes 	int i;
38426f9a767SRodney W. Grimes 	int s;
38526f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
38626f9a767SRodney W. Grimes 	struct buf *bp;
38726f9a767SRodney W. Grimes 	vm_offset_t kva;
38826f9a767SRodney W. Grimes 	int fileaddr;
38926f9a767SRodney W. Grimes 	vm_offset_t bsize;
39026f9a767SRodney W. Grimes 	int error = 0;
39126f9a767SRodney W. Grimes 
39224a1cce3SDavid Greenman 	vp = object->handle;
3932c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3942c4488fcSJohn Dyson 		return VM_PAGER_BAD;
3952c4488fcSJohn Dyson 
39626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
39726f9a767SRodney W. Grimes 
3980bdb7528SDavid Greenman 
399c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
40026f9a767SRodney W. Grimes 
40126f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
40226f9a767SRodney W. Grimes 
40326f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
404bbc0ec52SDavid Greenman 
405a316d390SJohn Dyson 		if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
40626f9a767SRodney W. Grimes 			continue;
40726f9a767SRodney W. Grimes 
408a316d390SJohn Dyson 		fileaddr = vnode_pager_addr(vp,
409a316d390SJohn Dyson 			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
41026f9a767SRodney W. Grimes 		if (fileaddr != -1) {
41126f9a767SRodney W. Grimes 			bp = getpbuf();
41226f9a767SRodney W. Grimes 
41326f9a767SRodney W. Grimes 			/* build a minimal buffer header */
41426f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
41526f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
41626f9a767SRodney W. Grimes 			bp->b_proc = curproc;
41726f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
41826f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
41926f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
42026f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
42126f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
422ab3f7469SPoul-Henning Kamp 			bp->b_data = (caddr_t) kva + i * bsize;
423187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4240d94caffSDavid Greenman 			pbgetvp(dp, bp);
42526f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
42626f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
42726f9a767SRodney W. Grimes 
42826f9a767SRodney W. Grimes 			/* do the input */
42926f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
43026f9a767SRodney W. Grimes 
43126f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
43226f9a767SRodney W. Grimes 
43326f9a767SRodney W. Grimes 			s = splbio();
43426f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
435aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
43626f9a767SRodney W. Grimes 			}
43726f9a767SRodney W. Grimes 			splx(s);
43826f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
43926f9a767SRodney W. Grimes 				error = EIO;
44026f9a767SRodney W. Grimes 
44126f9a767SRodney W. Grimes 			/*
44226f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
44326f9a767SRodney W. Grimes 			 */
44426f9a767SRodney W. Grimes 			relpbuf(bp);
44526f9a767SRodney W. Grimes 			if (error)
44626f9a767SRodney W. Grimes 				break;
4470d94caffSDavid Greenman 
448aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
44926f9a767SRodney W. Grimes 		} else {
450aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
45126f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
45226f9a767SRodney W. Grimes 		}
45326f9a767SRodney W. Grimes 	}
45426f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
45567bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
456b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
45726f9a767SRodney W. Grimes 	if (error) {
458a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
45926f9a767SRodney W. Grimes 	}
46026f9a767SRodney W. Grimes 	return VM_PAGER_OK;
46126f9a767SRodney W. Grimes 
46226f9a767SRodney W. Grimes }
46326f9a767SRodney W. Grimes 
46426f9a767SRodney W. Grimes 
46526f9a767SRodney W. Grimes /*
46626f9a767SRodney W. Grimes  * old style vnode pager output routine
46726f9a767SRodney W. Grimes  */
468f708ef1bSPoul-Henning Kamp static int
46924a1cce3SDavid Greenman vnode_pager_input_old(object, m)
47024a1cce3SDavid Greenman 	vm_object_t object;
47126f9a767SRodney W. Grimes 	vm_page_t m;
47226f9a767SRodney W. Grimes {
473df8bae1dSRodney W. Grimes 	struct uio auio;
474df8bae1dSRodney W. Grimes 	struct iovec aiov;
47526f9a767SRodney W. Grimes 	int error;
47626f9a767SRodney W. Grimes 	int size;
47726f9a767SRodney W. Grimes 	vm_offset_t kva;
478df8bae1dSRodney W. Grimes 
47926f9a767SRodney W. Grimes 	error = 0;
480bbc0ec52SDavid Greenman 
481df8bae1dSRodney W. Grimes 	/*
48226f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
48326f9a767SRodney W. Grimes 	 */
484a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
48526f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
48626f9a767SRodney W. Grimes 	} else {
48726f9a767SRodney W. Grimes 		size = PAGE_SIZE;
488a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
489a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
4900bdb7528SDavid Greenman 
49126f9a767SRodney W. Grimes 		/*
492df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
493df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
494df8bae1dSRodney W. Grimes 		 */
49526f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
4960bdb7528SDavid Greenman 
497df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
498df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
499df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
500df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
501a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
502df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
50326f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
504df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
505df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
50626f9a767SRodney W. Grimes 
50724a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
508df8bae1dSRodney W. Grimes 		if (!error) {
509df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 			if (count == 0)
512df8bae1dSRodney W. Grimes 				error = EINVAL;
51326f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
51426f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
515df8bae1dSRodney W. Grimes 		}
51626f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
517df8bae1dSRodney W. Grimes 	}
51867bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5190d94caffSDavid Greenman 	m->dirty = 0;
520b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
521a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
52226f9a767SRodney W. Grimes }
52326f9a767SRodney W. Grimes 
52426f9a767SRodney W. Grimes /*
52526f9a767SRodney W. Grimes  * generic vnode pager input routine
52626f9a767SRodney W. Grimes  */
527170db9c6SJohn Dyson 
528f708ef1bSPoul-Henning Kamp static int
52924a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
53026f9a767SRodney W. Grimes 	vm_object_t object;
53124a1cce3SDavid Greenman 	vm_page_t *m;
53224a1cce3SDavid Greenman 	int count;
53324a1cce3SDavid Greenman 	int reqpage;
53424a1cce3SDavid Greenman {
535170db9c6SJohn Dyson 	int rtval;
536170db9c6SJohn Dyson 	struct vnode *vp;
53795e5e988SJohn Dyson 
538170db9c6SJohn Dyson 	vp = object->handle;
5392c4488fcSJohn Dyson 	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
540170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
5410b8253a7SBruce Evans 		return vnode_pager_leaf_getpages(object, m, count, reqpage);
542170db9c6SJohn Dyson 	else
543170db9c6SJohn Dyson 		return rtval;
544170db9c6SJohn Dyson }
545170db9c6SJohn Dyson 
546170db9c6SJohn Dyson static int
547170db9c6SJohn Dyson vnode_pager_leaf_getpages(object, m, count, reqpage)
548170db9c6SJohn Dyson 	vm_object_t object;
549170db9c6SJohn Dyson 	vm_page_t *m;
550170db9c6SJohn Dyson 	int count;
551170db9c6SJohn Dyson 	int reqpage;
552170db9c6SJohn Dyson {
553a316d390SJohn Dyson 	vm_offset_t kva;
554a316d390SJohn Dyson 	off_t foff;
55524a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
55626f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
557efc68ce1SDavid Greenman 	int runpg;
558efc68ce1SDavid Greenman 	int runend;
5590bdb7528SDavid Greenman 	struct buf *bp;
56026f9a767SRodney W. Grimes 	int s;
56126f9a767SRodney W. Grimes 	int error = 0;
56226f9a767SRodney W. Grimes 
56324a1cce3SDavid Greenman 	vp = object->handle;
5642c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
5652c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5662c4488fcSJohn Dyson 
56726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
56826f9a767SRodney W. Grimes 
56926f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
570bbc0ec52SDavid Greenman 
57126f9a767SRodney W. Grimes 	/*
572bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
573bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
57426f9a767SRodney W. Grimes 	 */
575a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
576bbc0ec52SDavid Greenman 
57726f9a767SRodney W. Grimes 	/*
57816f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
57926f9a767SRodney W. Grimes 	 */
580c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
58126f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
58226f9a767SRodney W. Grimes 			if (i != reqpage) {
58326f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
58426f9a767SRodney W. Grimes 			}
58526f9a767SRodney W. Grimes 		}
586976e77fcSDavid Greenman 		cnt.v_vnodein++;
587976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
58824a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
589bbc0ec52SDavid Greenman 
59026f9a767SRodney W. Grimes 		/*
59126f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
59226f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
59326f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
59426f9a767SRodney W. Grimes 		 */
59526f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
59626f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
59726f9a767SRodney W. Grimes 
59826f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
59926f9a767SRodney W. Grimes 			if (i != reqpage) {
60026f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
60126f9a767SRodney W. Grimes 			}
60226f9a767SRodney W. Grimes 		}
603976e77fcSDavid Greenman 		cnt.v_vnodein++;
604976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
60524a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
60626f9a767SRodney W. Grimes 	}
60726f9a767SRodney W. Grimes 	/*
6080d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
6090d94caffSDavid Greenman 	 * then, the entire page is valid --
61032ad9cb5SDoug Rabson 	 * XXX no it isn't
6110d94caffSDavid Greenman 	 */
61232ad9cb5SDoug Rabson 
61332ad9cb5SDoug Rabson 	if (m[reqpage]->valid != VM_PAGE_BITS_ALL)
61432ad9cb5SDoug Rabson 	    m[reqpage]->valid = 0;
61532ad9cb5SDoug Rabson 
6160d94caffSDavid Greenman 	if (m[reqpage]->valid) {
6170d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
6180d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6190d94caffSDavid Greenman 			if (i != reqpage)
6200d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6210d94caffSDavid Greenman 		}
6220d94caffSDavid Greenman 		return VM_PAGER_OK;
6230d94caffSDavid Greenman 	}
6240bdb7528SDavid Greenman 
6250d94caffSDavid Greenman 	/*
62626f9a767SRodney W. Grimes 	 * here on direct device I/O
62726f9a767SRodney W. Grimes 	 */
62826f9a767SRodney W. Grimes 
629efc68ce1SDavid Greenman 	firstaddr = -1;
63026f9a767SRodney W. Grimes 	/*
631efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
63226f9a767SRodney W. Grimes 	 */
633efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
634a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
635a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
636efc68ce1SDavid Greenman 		if (firstaddr == -1) {
63724a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
63895e5e988SJohn Dyson 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
63924a1cce3SDavid Greenman 			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
640efc68ce1SDavid Greenman 			}
64126f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
642efc68ce1SDavid Greenman 			runend = i + 1;
643efc68ce1SDavid Greenman 			first = runend;
644efc68ce1SDavid Greenman 			continue;
645efc68ce1SDavid Greenman 		}
646efc68ce1SDavid Greenman 		runend = i + runpg;
647efc68ce1SDavid Greenman 		if (runend <= reqpage) {
648efc68ce1SDavid Greenman 			int j;
649efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
650efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
651efc68ce1SDavid Greenman 			}
65226f9a767SRodney W. Grimes 		} else {
653efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
654efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
65526f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
656efc68ce1SDavid Greenman 				count = first + runpg;
65726f9a767SRodney W. Grimes 			}
658efc68ce1SDavid Greenman 			break;
65926f9a767SRodney W. Grimes 		}
660efc68ce1SDavid Greenman 		first = runend;
661efc68ce1SDavid Greenman 	}
66226f9a767SRodney W. Grimes 
66326f9a767SRodney W. Grimes 	/*
664bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
665bbc0ec52SDavid Greenman 	 * to be zero based...
66626f9a767SRodney W. Grimes 	 */
66726f9a767SRodney W. Grimes 	if (first != 0) {
66826f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
66926f9a767SRodney W. Grimes 			m[i - first] = m[i];
67026f9a767SRodney W. Grimes 		}
67126f9a767SRodney W. Grimes 		count -= first;
67226f9a767SRodney W. Grimes 		reqpage -= first;
67326f9a767SRodney W. Grimes 	}
674efc68ce1SDavid Greenman 
67526f9a767SRodney W. Grimes 	/*
67626f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
67726f9a767SRodney W. Grimes 	 */
678a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
67926f9a767SRodney W. Grimes 
68026f9a767SRodney W. Grimes 	/*
68126f9a767SRodney W. Grimes 	 * calculate the size of the transfer
68226f9a767SRodney W. Grimes 	 */
68326f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
68424a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
68524a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
68626f9a767SRodney W. Grimes 
68726f9a767SRodney W. Grimes 	/*
68826f9a767SRodney W. Grimes 	 * round up physical size for real devices
68926f9a767SRodney W. Grimes 	 */
69026f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
69126f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
69226f9a767SRodney W. Grimes 
6936d40c3d3SDavid Greenman 	bp = getpbuf();
69416f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
69516f62314SDavid Greenman 
69626f9a767SRodney W. Grimes 	/*
69726f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
69826f9a767SRodney W. Grimes 	 */
69916f62314SDavid Greenman 	pmap_qenter(kva, m, count);
70026f9a767SRodney W. Grimes 
70126f9a767SRodney W. Grimes 	/* build a minimal buffer header */
70226f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
70326f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
70426f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
70526f9a767SRodney W. Grimes 	bp->b_proc = curproc;
70626f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
70726f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
70826f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
70926f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
71026f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
711187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7120d94caffSDavid Greenman 	pbgetvp(dp, bp);
71326f9a767SRodney W. Grimes 	bp->b_bcount = size;
71426f9a767SRodney W. Grimes 	bp->b_bufsize = size;
71526f9a767SRodney W. Grimes 
716976e77fcSDavid Greenman 	cnt.v_vnodein++;
717976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
718976e77fcSDavid Greenman 
71926f9a767SRodney W. Grimes 	/* do the input */
72026f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
721976e77fcSDavid Greenman 
72226f9a767SRodney W. Grimes 	s = splbio();
72326f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
72426f9a767SRodney W. Grimes 
72526f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
726aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
72726f9a767SRodney W. Grimes 	}
72826f9a767SRodney W. Grimes 	splx(s);
72926f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
73026f9a767SRodney W. Grimes 		error = EIO;
73126f9a767SRodney W. Grimes 
73226f9a767SRodney W. Grimes 	if (!error) {
73326f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
73426f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
73526f9a767SRodney W. Grimes 	}
73616f62314SDavid Greenman 	pmap_qremove(kva, count);
73726f9a767SRodney W. Grimes 
73826f9a767SRodney W. Grimes 	/*
73926f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
74026f9a767SRodney W. Grimes 	 */
74126f9a767SRodney W. Grimes 	relpbuf(bp);
74226f9a767SRodney W. Grimes 
74326f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
74467bf6868SJohn Dyson 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
7450d94caffSDavid Greenman 		m[i]->dirty = 0;
7460d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
747b1fc01b7SJohn Dyson 		m[i]->flags &= ~PG_ZERO;
74826f9a767SRodney W. Grimes 		if (i != reqpage) {
749bbc0ec52SDavid Greenman 
75026f9a767SRodney W. Grimes 			/*
751bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
752bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
753bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
754bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
755bbc0ec52SDavid Greenman 			 * deactivating pages is best.
75626f9a767SRodney W. Grimes 			 */
757bbc0ec52SDavid Greenman 
75826f9a767SRodney W. Grimes 			/*
759bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
760bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
76126f9a767SRodney W. Grimes 			 */
76226f9a767SRodney W. Grimes 			if (!error) {
76326f9a767SRodney W. Grimes 				vm_page_deactivate(m[i]);
76426f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
76526f9a767SRodney W. Grimes 			} else {
76626f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
76726f9a767SRodney W. Grimes 			}
76826f9a767SRodney W. Grimes 		}
76926f9a767SRodney W. Grimes 	}
77026f9a767SRodney W. Grimes 	if (error) {
77124a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
77226f9a767SRodney W. Grimes 	}
773a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
77426f9a767SRodney W. Grimes }
77526f9a767SRodney W. Grimes 
776f708ef1bSPoul-Henning Kamp static int
777170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
778170db9c6SJohn Dyson 	vm_object_t object;
779170db9c6SJohn Dyson 	vm_page_t *m;
780170db9c6SJohn Dyson 	int count;
781170db9c6SJohn Dyson 	boolean_t sync;
782170db9c6SJohn Dyson 	int *rtvals;
783170db9c6SJohn Dyson {
784170db9c6SJohn Dyson 	int rtval;
785170db9c6SJohn Dyson 	struct vnode *vp;
786ad980522SJohn Dyson 
787170db9c6SJohn Dyson 	vp = object->handle;
7882c4488fcSJohn Dyson 	rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
789170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
7900b8253a7SBruce Evans 		return vnode_pager_leaf_putpages(object, m, count, sync, rtvals);
791170db9c6SJohn Dyson 	else
792170db9c6SJohn Dyson 		return rtval;
793170db9c6SJohn Dyson }
794170db9c6SJohn Dyson 
79526f9a767SRodney W. Grimes /*
79626f9a767SRodney W. Grimes  * generic vnode pager output routine
79726f9a767SRodney W. Grimes  */
798170db9c6SJohn Dyson static int
799170db9c6SJohn Dyson vnode_pager_leaf_putpages(object, m, count, sync, rtvals)
80024a1cce3SDavid Greenman 	vm_object_t object;
80126f9a767SRodney W. Grimes 	vm_page_t *m;
80226f9a767SRodney W. Grimes 	int count;
80324a1cce3SDavid Greenman 	boolean_t sync;
80426f9a767SRodney W. Grimes 	int *rtvals;
80526f9a767SRodney W. Grimes {
806f6b04d2bSDavid Greenman 	int i;
80726f9a767SRodney W. Grimes 
808f6b04d2bSDavid Greenman 	struct vnode *vp;
809f6b04d2bSDavid Greenman 	int maxsize, ncount;
810a316d390SJohn Dyson 	vm_ooffset_t poffset;
811f6b04d2bSDavid Greenman 	struct uio auio;
812f6b04d2bSDavid Greenman 	struct iovec aiov;
813f6b04d2bSDavid Greenman 	int error;
81426f9a767SRodney W. Grimes 
81524a1cce3SDavid Greenman 	vp = object->handle;;
81626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
81726f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
81826f9a767SRodney W. Grimes 
819a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
820a316d390SJohn Dyson 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty);
821f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
822f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
8230d94caffSDavid Greenman 	}
8240bdb7528SDavid Greenman 
825f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
826f6b04d2bSDavid Greenman 	ncount = count;
82726f9a767SRodney W. Grimes 
828a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
829a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
830a316d390SJohn Dyson 		if (object->un_pager.vnp.vnp_size > poffset)
831a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
8325f55e841SDavid Greenman 		else
8335f55e841SDavid Greenman 			maxsize = 0;
834aa8de40aSPoul-Henning Kamp 		ncount = btoc(maxsize);
835f6b04d2bSDavid Greenman 		if (ncount < count) {
836f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
837f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
838f6b04d2bSDavid Greenman 			}
839a316d390SJohn Dyson #ifdef BOGUS
840f6b04d2bSDavid Greenman 			if (ncount == 0) {
841a316d390SJohn Dyson 				printf("vnode_pager_putpages: write past end of file: %d, %lu\n",
842a316d390SJohn Dyson 					poffset,
843a316d390SJohn Dyson 					(unsigned long) object->un_pager.vnp.vnp_size);
84426f9a767SRodney W. Grimes 				return rtvals[0];
84526f9a767SRodney W. Grimes 			}
846a316d390SJohn Dyson #endif
847f6b04d2bSDavid Greenman 		}
848f6b04d2bSDavid Greenman 	}
84926f9a767SRodney W. Grimes 
85026f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8515f55e841SDavid Greenman 		m[i]->busy++;
852f6b04d2bSDavid Greenman 		m[i]->flags &= ~PG_BUSY;
85326f9a767SRodney W. Grimes 	}
854f6b04d2bSDavid Greenman 
855f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
856f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
857f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
858f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
859a316d390SJohn Dyson 	auio.uio_offset = poffset;
860f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
861f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
862f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
863f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
864a316d390SJohn Dyson 	error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred);
865976e77fcSDavid Greenman 	cnt.v_vnodeout++;
866f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
867f6b04d2bSDavid Greenman 
868f6b04d2bSDavid Greenman 	if (error) {
86924a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
870f6b04d2bSDavid Greenman 	}
871f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
872f708ef1bSPoul-Henning Kamp 		printf("vnode_pager_putpages: residual I/O %d at %ld\n",
873a316d390SJohn Dyson 			auio.uio_resid, m[0]->pindex);
87426f9a767SRodney W. Grimes 	}
87526f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8765f55e841SDavid Greenman 		m[i]->busy--;
877f6b04d2bSDavid Greenman 		if (i < ncount) {
87826f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
87926f9a767SRodney W. Grimes 		}
880f6b04d2bSDavid Greenman 		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED))
88124a1cce3SDavid Greenman 			wakeup(m[i]);
88226f9a767SRodney W. Grimes 	}
883f6b04d2bSDavid Greenman 	return rtvals[0];
88426f9a767SRodney W. Grimes }
885f6b04d2bSDavid Greenman 
886f6b04d2bSDavid Greenman struct vnode *
88724a1cce3SDavid Greenman vnode_pager_lock(object)
88824a1cce3SDavid Greenman 	vm_object_t object;
88924a1cce3SDavid Greenman {
890996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
891996c772fSJohn Dyson 
89224a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
89324a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
894f6b04d2bSDavid Greenman 			continue;
895f6b04d2bSDavid Greenman 
896e7b0208fSJohn Dyson 		vn_lock(object->handle,
897e7b0208fSJohn Dyson 			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p);
89824a1cce3SDavid Greenman 		return object->handle;
89926f9a767SRodney W. Grimes 	}
90024a1cce3SDavid Greenman 	return NULL;
901f6b04d2bSDavid Greenman }
902