xref: /freebsd/sys/vm/vnode_pager.c (revision ce75f2c365a32572982d8e77ee0405f5b2bd7d8d)
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
41ce75f2c3SMike Smith  *	$Id: vnode_pager.c,v 1.86 1998/02/25 03:55:53 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 
91170db9c6SJohn Dyson 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
94df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
95df8bae1dSRodney W. Grimes  */
9624a1cce3SDavid Greenman vm_object_t
97b9dcd593SBruce Evans vnode_pager_alloc(void *handle, vm_size_t size, vm_prot_t prot,
98b9dcd593SBruce Evans 		  vm_ooffset_t offset)
99df8bae1dSRodney W. Grimes {
10006cb7259SDavid Greenman 	vm_object_t object;
101df8bae1dSRodney W. Grimes 	struct vnode *vp;
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes 	/*
104df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
105df8bae1dSRodney W. Grimes 	 */
106df8bae1dSRodney W. Grimes 	if (handle == NULL)
107df8bae1dSRodney W. Grimes 		return (NULL);
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
11039d38f93SDavid Greenman 
11139d38f93SDavid Greenman 	/*
11239d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
11339d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
11439d38f93SDavid Greenman 	 */
11539d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
11639d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
11739d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
11839d38f93SDavid Greenman 	}
11939d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
12039d38f93SDavid Greenman 
12139d38f93SDavid Greenman 	/*
12239d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
12339d38f93SDavid Greenman 	 * go away.
12439d38f93SDavid Greenman 	 */
125bd7e5f99SJohn Dyson 	while (((object = vp->v_object) != NULL) &&
126bd7e5f99SJohn Dyson 		(object->flags & OBJ_DEAD)) {
127aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
12824a1cce3SDavid Greenman 	}
1290d94caffSDavid Greenman 
1302be70f79SJohn Dyson 	if (vp->v_usecount == 0)
1312be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
1322be70f79SJohn Dyson 
13324a1cce3SDavid Greenman 	if (object == NULL) {
134df8bae1dSRodney W. Grimes 		/*
135df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
136df8bae1dSRodney W. Grimes 		 */
137a316d390SJohn Dyson 		object = vm_object_allocate(OBJT_VNODE, size);
138ad5dd234SJohn Dyson 		object->flags = 0;
139bbc0ec52SDavid Greenman 
140a316d390SJohn Dyson 		object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE;
14126f9a767SRodney W. Grimes 
14224a1cce3SDavid Greenman 		object->handle = handle;
14324a1cce3SDavid Greenman 		vp->v_object = object;
14495e5e988SJohn Dyson 		vp->v_usecount++;
145df8bae1dSRodney W. Grimes 	} else {
14695e5e988SJohn Dyson 		object->ref_count++;
14795e5e988SJohn Dyson 		vp->v_usecount++;
148df8bae1dSRodney W. Grimes 	}
14939d38f93SDavid Greenman 
15039d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
15139d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
15239d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
15339d38f93SDavid Greenman 		wakeup(vp);
15439d38f93SDavid Greenman 	}
15524a1cce3SDavid Greenman 	return (object);
156df8bae1dSRodney W. Grimes }
157df8bae1dSRodney W. Grimes 
158f708ef1bSPoul-Henning Kamp static void
15924a1cce3SDavid Greenman vnode_pager_dealloc(object)
1600d94caffSDavid Greenman 	vm_object_t object;
16124a1cce3SDavid Greenman {
16224a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
163df8bae1dSRodney W. Grimes 
16424a1cce3SDavid Greenman 	if (vp == NULL)
16524a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
16624a1cce3SDavid Greenman 
16766095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
16824a1cce3SDavid Greenman 
16924a1cce3SDavid Greenman 	object->handle = NULL;
17095461b45SJohn Dyson 	object->type = OBJT_DEAD;
171aa2cabb9SDavid Greenman 	vp->v_object = NULL;
17295e5e988SJohn Dyson 	vp->v_flag &= ~(VTEXT | VOBJBUF);
173df8bae1dSRodney W. Grimes }
17426f9a767SRodney W. Grimes 
175f708ef1bSPoul-Henning Kamp static boolean_t
176a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
17724a1cce3SDavid Greenman 	vm_object_t object;
178a316d390SJohn Dyson 	vm_pindex_t pindex;
17924a1cce3SDavid Greenman 	int *before;
18024a1cce3SDavid Greenman 	int *after;
181df8bae1dSRodney W. Grimes {
18224a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
183df8bae1dSRodney W. Grimes 	daddr_t bn;
1843af76890SPoul-Henning Kamp 	int err;
185170db9c6SJohn Dyson 	daddr_t reqblock;
1862c4488fcSJohn Dyson 	int poff;
1872c4488fcSJohn Dyson 	int bsize;
188d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
189df8bae1dSRodney W. Grimes 
19047221757SJohn Dyson 	if ((vp == NULL) || (vp->v_flag & VDOOMED))
19147221757SJohn Dyson 		return FALSE;
19247221757SJohn Dyson 
193df8bae1dSRodney W. Grimes 	/*
1940d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
1950d94caffSDavid Greenman 	 * not have the page.
196df8bae1dSRodney W. Grimes 	 */
197a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
198a316d390SJohn Dyson 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
1994abc71c0SDavid Greenman 		return FALSE;
200df8bae1dSRodney W. Grimes 
201eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
202170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
203d63596ceSJohn Dyson 	blocksperpage = 0;
204d63596ceSJohn Dyson 	if (pagesperblock > 0) {
205a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
206d63596ceSJohn Dyson 	} else {
207d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
208d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
209d63596ceSJohn Dyson 	}
210170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
211170db9c6SJohn Dyson 		after, before);
2120d94caffSDavid Greenman 	if (err)
21324a1cce3SDavid Greenman 		return TRUE;
2146eab77f2SJohn Dyson 	if ( bn == -1)
215ced399eeSJohn Dyson 		return FALSE;
216d63596ceSJohn Dyson 	if (pagesperblock > 0) {
217a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
218170db9c6SJohn Dyson 		if (before) {
219170db9c6SJohn Dyson 			*before *= pagesperblock;
220170db9c6SJohn Dyson 			*before += poff;
221170db9c6SJohn Dyson 		}
222170db9c6SJohn Dyson 		if (after) {
223b1fc01b7SJohn Dyson 			int numafter;
224170db9c6SJohn Dyson 			*after *= pagesperblock;
225b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
226a316d390SJohn Dyson 			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
227a316d390SJohn Dyson 				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
228b1fc01b7SJohn Dyson 			}
229b1fc01b7SJohn Dyson 			*after += numafter;
230170db9c6SJohn Dyson 		}
231d63596ceSJohn Dyson 	} else {
232d63596ceSJohn Dyson 		if (before) {
233d63596ceSJohn Dyson 			*before /= blocksperpage;
234d63596ceSJohn Dyson 		}
235d63596ceSJohn Dyson 
236d63596ceSJohn Dyson 		if (after) {
237d63596ceSJohn Dyson 			*after /= blocksperpage;
238d63596ceSJohn Dyson 		}
239d63596ceSJohn Dyson 	}
240ced399eeSJohn Dyson 	return TRUE;
241df8bae1dSRodney W. Grimes }
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes /*
244df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
24524a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
246df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
247df8bae1dSRodney W. Grimes  *
248df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
249df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
250df8bae1dSRodney W. Grimes  */
251df8bae1dSRodney W. Grimes void
252df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
253df8bae1dSRodney W. Grimes 	struct vnode *vp;
254a316d390SJohn Dyson 	vm_ooffset_t nsize;
255df8bae1dSRodney W. Grimes {
25624a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
257df8bae1dSRodney W. Grimes 
25824a1cce3SDavid Greenman 	if (object == NULL)
259df8bae1dSRodney W. Grimes 		return;
260bbc0ec52SDavid Greenman 
261df8bae1dSRodney W. Grimes 	/*
262df8bae1dSRodney W. Grimes 	 * Hasn't changed size
263df8bae1dSRodney W. Grimes 	 */
26424a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
265df8bae1dSRodney W. Grimes 		return;
266bbc0ec52SDavid Greenman 
267df8bae1dSRodney W. Grimes 	/*
268bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
269df8bae1dSRodney W. Grimes 	 */
27024a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
271a316d390SJohn Dyson 		vm_ooffset_t nsizerounded;
272aa8de40aSPoul-Henning Kamp 		nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_MASK));
273a316d390SJohn Dyson 		if (nsizerounded < object->un_pager.vnp.vnp_size) {
2741efb74fbSJohn Dyson 			vm_pindex_t st, end;
2751efb74fbSJohn Dyson 			st = OFF_TO_IDX(nsize + PAGE_MASK);
2761efb74fbSJohn Dyson 			end = OFF_TO_IDX(object->un_pager.vnp.vnp_size);
2771efb74fbSJohn Dyson 
2781efb74fbSJohn Dyson 			vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
2791efb74fbSJohn Dyson 			vm_object_page_remove(object, st, end, FALSE);
2800d94caffSDavid Greenman 		}
281bbc0ec52SDavid Greenman 		/*
282bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
283bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
284bbc0ec52SDavid Greenman 		 */
285bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
286bbc0ec52SDavid Greenman 			vm_offset_t kva;
287bbc0ec52SDavid Greenman 			vm_page_t m;
288bbc0ec52SDavid Greenman 
289a316d390SJohn Dyson 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
290bbc0ec52SDavid Greenman 			if (m) {
291bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
292bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
293a316d390SJohn Dyson 				    (int) (round_page(nsize) - nsize));
294bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
295bbc0ec52SDavid Greenman 			}
296bbc0ec52SDavid Greenman 		}
297bbc0ec52SDavid Greenman 	}
298a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
299aa8de40aSPoul-Henning Kamp 	object->size = OFF_TO_IDX(nsize + PAGE_MASK);
300df8bae1dSRodney W. Grimes }
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes void
30326f9a767SRodney W. Grimes vnode_pager_freepage(m)
30426f9a767SRodney W. Grimes 	vm_page_t m;
305df8bae1dSRodney W. Grimes {
30626f9a767SRodney W. Grimes 	vm_page_free(m);
30726f9a767SRodney W. Grimes }
30826f9a767SRodney W. Grimes 
30926f9a767SRodney W. Grimes /*
31026f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
31126f9a767SRodney W. Grimes  * file address
31226f9a767SRodney W. Grimes  */
313f708ef1bSPoul-Henning Kamp static vm_offset_t
314efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
31526f9a767SRodney W. Grimes 	struct vnode *vp;
316a316d390SJohn Dyson 	vm_ooffset_t address;
317efc68ce1SDavid Greenman 	int *run;
31826f9a767SRodney W. Grimes {
31926f9a767SRodney W. Grimes 	int rtaddress;
32026f9a767SRodney W. Grimes 	int bsize;
321a316d390SJohn Dyson 	daddr_t block;
32226f9a767SRodney W. Grimes 	struct vnode *rtvp;
32326f9a767SRodney W. Grimes 	int err;
324a316d390SJohn Dyson 	daddr_t vblock;
325a316d390SJohn Dyson 	int voffset;
32626f9a767SRodney W. Grimes 
3270d94caffSDavid Greenman 	if ((int) address < 0)
3280d94caffSDavid Greenman 		return -1;
3290d94caffSDavid Greenman 
3302c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3312c4488fcSJohn Dyson 		return -1;
3322c4488fcSJohn Dyson 
33326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
33426f9a767SRodney W. Grimes 	vblock = address / bsize;
33526f9a767SRodney W. Grimes 	voffset = address % bsize;
33626f9a767SRodney W. Grimes 
337c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
33826f9a767SRodney W. Grimes 
339efc68ce1SDavid Greenman 	if (err || (block == -1))
34026f9a767SRodney W. Grimes 		rtaddress = -1;
341efc68ce1SDavid Greenman 	else {
342187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
343efc68ce1SDavid Greenman 		if( run) {
344efc68ce1SDavid Greenman 			*run += 1;
345efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
346efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
347efc68ce1SDavid Greenman 		}
348efc68ce1SDavid Greenman 	}
34926f9a767SRodney W. Grimes 
35026f9a767SRodney W. Grimes 	return rtaddress;
35126f9a767SRodney W. Grimes }
35226f9a767SRodney W. Grimes 
35326f9a767SRodney W. Grimes /*
35426f9a767SRodney W. Grimes  * interrupt routine for I/O completion
35526f9a767SRodney W. Grimes  */
356f708ef1bSPoul-Henning Kamp static void
35726f9a767SRodney W. Grimes vnode_pager_iodone(bp)
35826f9a767SRodney W. Grimes 	struct buf *bp;
35926f9a767SRodney W. Grimes {
36026f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
36124a1cce3SDavid Greenman 	wakeup(bp);
36226f9a767SRodney W. Grimes }
36326f9a767SRodney W. Grimes 
36426f9a767SRodney W. Grimes /*
36526f9a767SRodney W. Grimes  * small block file system vnode pager input
36626f9a767SRodney W. Grimes  */
367f708ef1bSPoul-Henning Kamp static int
36824a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
36924a1cce3SDavid Greenman 	vm_object_t object;
37026f9a767SRodney W. Grimes 	vm_page_t m;
37126f9a767SRodney W. Grimes {
37226f9a767SRodney W. Grimes 	int i;
37326f9a767SRodney W. Grimes 	int s;
37426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
37526f9a767SRodney W. Grimes 	struct buf *bp;
37626f9a767SRodney W. Grimes 	vm_offset_t kva;
37726f9a767SRodney W. Grimes 	int fileaddr;
37826f9a767SRodney W. Grimes 	vm_offset_t bsize;
37926f9a767SRodney W. Grimes 	int error = 0;
38026f9a767SRodney W. Grimes 
38124a1cce3SDavid Greenman 	vp = object->handle;
3822c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3832c4488fcSJohn Dyson 		return VM_PAGER_BAD;
3842c4488fcSJohn Dyson 
38526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
38626f9a767SRodney W. Grimes 
3870bdb7528SDavid Greenman 
388c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
38926f9a767SRodney W. Grimes 
39026f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
39126f9a767SRodney W. Grimes 
39226f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
393bbc0ec52SDavid Greenman 
394a316d390SJohn Dyson 		if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
39526f9a767SRodney W. Grimes 			continue;
39626f9a767SRodney W. Grimes 
397a316d390SJohn Dyson 		fileaddr = vnode_pager_addr(vp,
398a316d390SJohn Dyson 			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
39926f9a767SRodney W. Grimes 		if (fileaddr != -1) {
40026f9a767SRodney W. Grimes 			bp = getpbuf();
40126f9a767SRodney W. Grimes 
40226f9a767SRodney W. Grimes 			/* build a minimal buffer header */
40326f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
40426f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
40526f9a767SRodney W. Grimes 			bp->b_proc = curproc;
40626f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
40726f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
40826f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
40926f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
41026f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
411ab3f7469SPoul-Henning Kamp 			bp->b_data = (caddr_t) kva + i * bsize;
412187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4130d94caffSDavid Greenman 			pbgetvp(dp, bp);
41426f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
41526f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
41626f9a767SRodney W. Grimes 
41726f9a767SRodney W. Grimes 			/* do the input */
41826f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
41926f9a767SRodney W. Grimes 
420e47ed70bSJohn Dyson 			/* we definitely need to be at splvm here */
42126f9a767SRodney W. Grimes 
422e47ed70bSJohn Dyson 			s = splvm();
42326f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
424aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
42526f9a767SRodney W. Grimes 			}
42626f9a767SRodney W. Grimes 			splx(s);
42726f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
42826f9a767SRodney W. Grimes 				error = EIO;
42926f9a767SRodney W. Grimes 
43026f9a767SRodney W. Grimes 			/*
43126f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
43226f9a767SRodney W. Grimes 			 */
43326f9a767SRodney W. Grimes 			relpbuf(bp);
43426f9a767SRodney W. Grimes 			if (error)
43526f9a767SRodney W. Grimes 				break;
4360d94caffSDavid Greenman 
437aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
43826f9a767SRodney W. Grimes 		} else {
439aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
44026f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
44126f9a767SRodney W. Grimes 		}
44226f9a767SRodney W. Grimes 	}
44326f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
44467bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
445b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
44626f9a767SRodney W. Grimes 	if (error) {
447a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
44826f9a767SRodney W. Grimes 	}
44926f9a767SRodney W. Grimes 	return VM_PAGER_OK;
45026f9a767SRodney W. Grimes 
45126f9a767SRodney W. Grimes }
45226f9a767SRodney W. Grimes 
45326f9a767SRodney W. Grimes 
45426f9a767SRodney W. Grimes /*
45526f9a767SRodney W. Grimes  * old style vnode pager output routine
45626f9a767SRodney W. Grimes  */
457f708ef1bSPoul-Henning Kamp static int
45824a1cce3SDavid Greenman vnode_pager_input_old(object, m)
45924a1cce3SDavid Greenman 	vm_object_t object;
46026f9a767SRodney W. Grimes 	vm_page_t m;
46126f9a767SRodney W. Grimes {
462df8bae1dSRodney W. Grimes 	struct uio auio;
463df8bae1dSRodney W. Grimes 	struct iovec aiov;
46426f9a767SRodney W. Grimes 	int error;
46526f9a767SRodney W. Grimes 	int size;
46626f9a767SRodney W. Grimes 	vm_offset_t kva;
467df8bae1dSRodney W. Grimes 
46826f9a767SRodney W. Grimes 	error = 0;
469bbc0ec52SDavid Greenman 
470df8bae1dSRodney W. Grimes 	/*
47126f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
47226f9a767SRodney W. Grimes 	 */
473a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
47426f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
47526f9a767SRodney W. Grimes 	} else {
47626f9a767SRodney W. Grimes 		size = PAGE_SIZE;
477a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
478a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
4790bdb7528SDavid Greenman 
48026f9a767SRodney W. Grimes 		/*
481df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
482df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
483df8bae1dSRodney W. Grimes 		 */
48426f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
4850bdb7528SDavid Greenman 
486df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
487df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
488df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
489df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
490a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
491df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
49226f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
493df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
494df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
49526f9a767SRodney W. Grimes 
49624a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
497df8bae1dSRodney W. Grimes 		if (!error) {
498df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes 			if (count == 0)
501df8bae1dSRodney W. Grimes 				error = EINVAL;
50226f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
50326f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
504df8bae1dSRodney W. Grimes 		}
50526f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
506df8bae1dSRodney W. Grimes 	}
50767bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5080d94caffSDavid Greenman 	m->dirty = 0;
509b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
510a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
51126f9a767SRodney W. Grimes }
51226f9a767SRodney W. Grimes 
51326f9a767SRodney W. Grimes /*
51426f9a767SRodney W. Grimes  * generic vnode pager input routine
51526f9a767SRodney W. Grimes  */
516170db9c6SJohn Dyson 
517ce75f2c3SMike Smith /*
518ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
519ce75f2c3SMike Smith  * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to
520ce75f2c3SMike Smith  * vnode_pager_generic_getpages() to implement the previous behaviour.
521ce75f2c3SMike Smith  *
522ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
523ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
524ce75f2c3SMike Smith  */
525f708ef1bSPoul-Henning Kamp static int
52624a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
52726f9a767SRodney W. Grimes 	vm_object_t object;
52824a1cce3SDavid Greenman 	vm_page_t *m;
52924a1cce3SDavid Greenman 	int count;
53024a1cce3SDavid Greenman 	int reqpage;
53124a1cce3SDavid Greenman {
532170db9c6SJohn Dyson 	int rtval;
533170db9c6SJohn Dyson 	struct vnode *vp;
53495e5e988SJohn Dyson 
535170db9c6SJohn Dyson 	vp = object->handle;
536ce75f2c3SMike Smith 	/*
537ce75f2c3SMike Smith 	 * XXX temporary diagnostic message to help track stale FS code,
538ce75f2c3SMike Smith 	 * Returning EOPNOTSUPP from here may make things unhappy.
539ce75f2c3SMike Smith 	 */
5402c4488fcSJohn Dyson 	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
541170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
542ce75f2c3SMike Smith 	    printf("vnode_pager: *** WARNING *** stale FS code in system.\n");
543170db9c6SJohn Dyson 	return rtval;
544170db9c6SJohn Dyson }
545170db9c6SJohn Dyson 
546ce75f2c3SMike Smith 
547ce75f2c3SMike Smith /*
548ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
549ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
550ce75f2c3SMike Smith  */
551ce75f2c3SMike Smith int
552ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
553ce75f2c3SMike Smith 	struct vnode *vp;
554170db9c6SJohn Dyson 	vm_page_t *m;
555ce75f2c3SMike Smith 	int bytecount;
556170db9c6SJohn Dyson 	int reqpage;
557170db9c6SJohn Dyson {
558ce75f2c3SMike Smith 	vm_object_t object;
559a316d390SJohn Dyson 	vm_offset_t kva;
560a316d390SJohn Dyson 	off_t foff;
56124a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
562ce75f2c3SMike Smith 	struct vnode *dp;
563efc68ce1SDavid Greenman 	int runpg;
564efc68ce1SDavid Greenman 	int runend;
5650bdb7528SDavid Greenman 	struct buf *bp;
56626f9a767SRodney W. Grimes 	int s;
567ce75f2c3SMike Smith 	int count;
56826f9a767SRodney W. Grimes 	int error = 0;
56926f9a767SRodney W. Grimes 
570ce75f2c3SMike Smith 	object = vp->v_object;
571ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
572ce75f2c3SMike Smith 
5732c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
5742c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5752c4488fcSJohn Dyson 
57626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
57726f9a767SRodney W. Grimes 
57826f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
579bbc0ec52SDavid Greenman 
58026f9a767SRodney W. Grimes 	/*
581bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
582bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
58326f9a767SRodney W. Grimes 	 */
584a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
585bbc0ec52SDavid Greenman 
58626f9a767SRodney W. Grimes 	/*
58716f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
58826f9a767SRodney W. Grimes 	 */
589c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
59026f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
59126f9a767SRodney W. Grimes 			if (i != reqpage) {
59226f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
59326f9a767SRodney W. Grimes 			}
59426f9a767SRodney W. Grimes 		}
595976e77fcSDavid Greenman 		cnt.v_vnodein++;
596976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
59724a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
598bbc0ec52SDavid Greenman 
59926f9a767SRodney W. Grimes 		/*
60026f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
60126f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
60226f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
60326f9a767SRodney W. Grimes 		 */
60426f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
60526f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
60626f9a767SRodney W. Grimes 
60726f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
60826f9a767SRodney W. Grimes 			if (i != reqpage) {
60926f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
61026f9a767SRodney W. Grimes 			}
61126f9a767SRodney W. Grimes 		}
612976e77fcSDavid Greenman 		cnt.v_vnodein++;
613976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
61424a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
61526f9a767SRodney W. Grimes 	}
61626f9a767SRodney W. Grimes 	/*
6170d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
6180d94caffSDavid Greenman 	 * then, the entire page is valid --
61932ad9cb5SDoug Rabson 	 * XXX no it isn't
6200d94caffSDavid Greenman 	 */
62132ad9cb5SDoug Rabson 
62232ad9cb5SDoug Rabson 	if (m[reqpage]->valid != VM_PAGE_BITS_ALL)
62332ad9cb5SDoug Rabson 	    m[reqpage]->valid = 0;
62432ad9cb5SDoug Rabson 
6250d94caffSDavid Greenman 	if (m[reqpage]->valid) {
6260d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
6270d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6280d94caffSDavid Greenman 			if (i != reqpage)
6290d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6300d94caffSDavid Greenman 		}
6310d94caffSDavid Greenman 		return VM_PAGER_OK;
6320d94caffSDavid Greenman 	}
6330bdb7528SDavid Greenman 
6340d94caffSDavid Greenman 	/*
63526f9a767SRodney W. Grimes 	 * here on direct device I/O
63626f9a767SRodney W. Grimes 	 */
63726f9a767SRodney W. Grimes 
638efc68ce1SDavid Greenman 	firstaddr = -1;
63926f9a767SRodney W. Grimes 	/*
640efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
64126f9a767SRodney W. Grimes 	 */
642efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
643a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
644a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
645efc68ce1SDavid Greenman 		if (firstaddr == -1) {
64624a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
64795e5e988SJohn Dyson 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
64824a1cce3SDavid Greenman 			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
649efc68ce1SDavid Greenman 			}
65026f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
651efc68ce1SDavid Greenman 			runend = i + 1;
652efc68ce1SDavid Greenman 			first = runend;
653efc68ce1SDavid Greenman 			continue;
654efc68ce1SDavid Greenman 		}
655efc68ce1SDavid Greenman 		runend = i + runpg;
656efc68ce1SDavid Greenman 		if (runend <= reqpage) {
657efc68ce1SDavid Greenman 			int j;
658efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
659efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
660efc68ce1SDavid Greenman 			}
66126f9a767SRodney W. Grimes 		} else {
662efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
663efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
66426f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
665efc68ce1SDavid Greenman 				count = first + runpg;
66626f9a767SRodney W. Grimes 			}
667efc68ce1SDavid Greenman 			break;
66826f9a767SRodney W. Grimes 		}
669efc68ce1SDavid Greenman 		first = runend;
670efc68ce1SDavid Greenman 	}
67126f9a767SRodney W. Grimes 
67226f9a767SRodney W. Grimes 	/*
673bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
674bbc0ec52SDavid Greenman 	 * to be zero based...
67526f9a767SRodney W. Grimes 	 */
67626f9a767SRodney W. Grimes 	if (first != 0) {
67726f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
67826f9a767SRodney W. Grimes 			m[i - first] = m[i];
67926f9a767SRodney W. Grimes 		}
68026f9a767SRodney W. Grimes 		count -= first;
68126f9a767SRodney W. Grimes 		reqpage -= first;
68226f9a767SRodney W. Grimes 	}
683efc68ce1SDavid Greenman 
68426f9a767SRodney W. Grimes 	/*
68526f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
68626f9a767SRodney W. Grimes 	 */
687a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
68826f9a767SRodney W. Grimes 
68926f9a767SRodney W. Grimes 	/*
69026f9a767SRodney W. Grimes 	 * calculate the size of the transfer
69126f9a767SRodney W. Grimes 	 */
69226f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
69324a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
69424a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
69526f9a767SRodney W. Grimes 
69626f9a767SRodney W. Grimes 	/*
69726f9a767SRodney W. Grimes 	 * round up physical size for real devices
69826f9a767SRodney W. Grimes 	 */
69926f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
70026f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
70126f9a767SRodney W. Grimes 
7026d40c3d3SDavid Greenman 	bp = getpbuf();
70316f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
70416f62314SDavid Greenman 
70526f9a767SRodney W. Grimes 	/*
70626f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
70726f9a767SRodney W. Grimes 	 */
70816f62314SDavid Greenman 	pmap_qenter(kva, m, count);
70926f9a767SRodney W. Grimes 
71026f9a767SRodney W. Grimes 	/* build a minimal buffer header */
71126f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
71226f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
71326f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
71426f9a767SRodney W. Grimes 	bp->b_proc = curproc;
71526f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
71626f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
71726f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
71826f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
71926f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
720187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7210d94caffSDavid Greenman 	pbgetvp(dp, bp);
72226f9a767SRodney W. Grimes 	bp->b_bcount = size;
72326f9a767SRodney W. Grimes 	bp->b_bufsize = size;
72426f9a767SRodney W. Grimes 
725976e77fcSDavid Greenman 	cnt.v_vnodein++;
726976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
727976e77fcSDavid Greenman 
72826f9a767SRodney W. Grimes 	/* do the input */
72926f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
730976e77fcSDavid Greenman 
731e47ed70bSJohn Dyson 	s = splvm();
732e47ed70bSJohn Dyson 	/* we definitely need to be at splvm here */
73326f9a767SRodney W. Grimes 
73426f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
735aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
73626f9a767SRodney W. Grimes 	}
73726f9a767SRodney W. Grimes 	splx(s);
73826f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
73926f9a767SRodney W. Grimes 		error = EIO;
74026f9a767SRodney W. Grimes 
74126f9a767SRodney W. Grimes 	if (!error) {
74226f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
74326f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
74426f9a767SRodney W. Grimes 	}
74516f62314SDavid Greenman 	pmap_qremove(kva, count);
74626f9a767SRodney W. Grimes 
74726f9a767SRodney W. Grimes 	/*
74826f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
74926f9a767SRodney W. Grimes 	 */
75026f9a767SRodney W. Grimes 	relpbuf(bp);
75126f9a767SRodney W. Grimes 
75226f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
75367bf6868SJohn Dyson 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
7540d94caffSDavid Greenman 		m[i]->dirty = 0;
7550d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
756b1fc01b7SJohn Dyson 		m[i]->flags &= ~PG_ZERO;
75726f9a767SRodney W. Grimes 		if (i != reqpage) {
758bbc0ec52SDavid Greenman 
75926f9a767SRodney W. Grimes 			/*
760bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
761bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
762bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
763bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
764bbc0ec52SDavid Greenman 			 * deactivating pages is best.
76526f9a767SRodney W. Grimes 			 */
766bbc0ec52SDavid Greenman 
76726f9a767SRodney W. Grimes 			/*
768bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
769bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
77026f9a767SRodney W. Grimes 			 */
77126f9a767SRodney W. Grimes 			if (!error) {
77295461b45SJohn Dyson 				if (m[i]->flags & PG_WANTED)
77395461b45SJohn Dyson 					vm_page_activate(m[i]);
77495461b45SJohn Dyson 				else
77526f9a767SRodney W. Grimes 					vm_page_deactivate(m[i]);
77626f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
77726f9a767SRodney W. Grimes 			} else {
77826f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
77926f9a767SRodney W. Grimes 			}
78026f9a767SRodney W. Grimes 		}
78126f9a767SRodney W. Grimes 	}
78226f9a767SRodney W. Grimes 	if (error) {
78324a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
78426f9a767SRodney W. Grimes 	}
785a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
78626f9a767SRodney W. Grimes }
78726f9a767SRodney W. Grimes 
788ce75f2c3SMike Smith /*
789ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
790ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
791ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
792ce75f2c3SMike Smith  *
793ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
794ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
795ce75f2c3SMike Smith  */
796f708ef1bSPoul-Henning Kamp static int
797170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
798170db9c6SJohn Dyson 	vm_object_t object;
799170db9c6SJohn Dyson 	vm_page_t *m;
800170db9c6SJohn Dyson 	int count;
801170db9c6SJohn Dyson 	boolean_t sync;
802170db9c6SJohn Dyson 	int *rtvals;
803170db9c6SJohn Dyson {
804170db9c6SJohn Dyson 	int rtval;
805170db9c6SJohn Dyson 	struct vnode *vp;
806ad980522SJohn Dyson 
807170db9c6SJohn Dyson 	vp = object->handle;
808ce75f2c3SMike Smith 	return VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
809170db9c6SJohn Dyson }
810170db9c6SJohn Dyson 
811ce75f2c3SMike Smith 
81226f9a767SRodney W. Grimes /*
813ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
814ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
81526f9a767SRodney W. Grimes  */
816ce75f2c3SMike Smith int
817ce75f2c3SMike Smith vnode_pager_generic_putpages(vp, m, bytecount, sync, rtvals)
818ce75f2c3SMike Smith 	struct vnode *vp;
81926f9a767SRodney W. Grimes 	vm_page_t *m;
820ce75f2c3SMike Smith 	int bytecount;
82124a1cce3SDavid Greenman 	boolean_t sync;
82226f9a767SRodney W. Grimes 	int *rtvals;
82326f9a767SRodney W. Grimes {
824f6b04d2bSDavid Greenman 	int i;
825ce75f2c3SMike Smith 	vm_object_t object;
826ce75f2c3SMike Smith 	int count;
82726f9a767SRodney W. Grimes 
828f6b04d2bSDavid Greenman 	int maxsize, ncount;
829a316d390SJohn Dyson 	vm_ooffset_t poffset;
830f6b04d2bSDavid Greenman 	struct uio auio;
831f6b04d2bSDavid Greenman 	struct iovec aiov;
832f6b04d2bSDavid Greenman 	int error;
83326f9a767SRodney W. Grimes 
834ce75f2c3SMike Smith 	object = vp->v_object;
835ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
836ce75f2c3SMike Smith 
83726f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
83826f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
83926f9a767SRodney W. Grimes 
840a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
841a316d390SJohn Dyson 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty);
842f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
843f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
8440d94caffSDavid Greenman 	}
8450bdb7528SDavid Greenman 
846f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
847f6b04d2bSDavid Greenman 	ncount = count;
84826f9a767SRodney W. Grimes 
849a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
850a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
851a316d390SJohn Dyson 		if (object->un_pager.vnp.vnp_size > poffset)
852a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
8535f55e841SDavid Greenman 		else
8545f55e841SDavid Greenman 			maxsize = 0;
855aa8de40aSPoul-Henning Kamp 		ncount = btoc(maxsize);
856f6b04d2bSDavid Greenman 		if (ncount < count) {
857f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
858f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
859f6b04d2bSDavid Greenman 			}
860a316d390SJohn Dyson #ifdef BOGUS
861f6b04d2bSDavid Greenman 			if (ncount == 0) {
862a316d390SJohn Dyson 				printf("vnode_pager_putpages: write past end of file: %d, %lu\n",
863a316d390SJohn Dyson 					poffset,
864a316d390SJohn Dyson 					(unsigned long) object->un_pager.vnp.vnp_size);
86526f9a767SRodney W. Grimes 				return rtvals[0];
86626f9a767SRodney W. Grimes 			}
867a316d390SJohn Dyson #endif
868f6b04d2bSDavid Greenman 		}
869f6b04d2bSDavid Greenman 	}
87026f9a767SRodney W. Grimes 
87126f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8725f55e841SDavid Greenman 		m[i]->busy++;
873f6b04d2bSDavid Greenman 		m[i]->flags &= ~PG_BUSY;
87426f9a767SRodney W. Grimes 	}
875f6b04d2bSDavid Greenman 
876f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
877f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
878f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
879f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
880a316d390SJohn Dyson 	auio.uio_offset = poffset;
881f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
882f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
883f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
884f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
885a316d390SJohn Dyson 	error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred);
886976e77fcSDavid Greenman 	cnt.v_vnodeout++;
887f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
888f6b04d2bSDavid Greenman 
889f6b04d2bSDavid Greenman 	if (error) {
89024a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
891f6b04d2bSDavid Greenman 	}
892f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
893f708ef1bSPoul-Henning Kamp 		printf("vnode_pager_putpages: residual I/O %d at %ld\n",
894a316d390SJohn Dyson 			auio.uio_resid, m[0]->pindex);
89526f9a767SRodney W. Grimes 	}
89626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8975f55e841SDavid Greenman 		m[i]->busy--;
898f6b04d2bSDavid Greenman 		if (i < ncount) {
89926f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
90026f9a767SRodney W. Grimes 		}
90195461b45SJohn Dyson 		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED)) {
90295461b45SJohn Dyson 			vm_page_activate(m[i]);
90324a1cce3SDavid Greenman 			wakeup(m[i]);
90426f9a767SRodney W. Grimes 		}
90595461b45SJohn Dyson 	}
906f6b04d2bSDavid Greenman 	return rtvals[0];
90726f9a767SRodney W. Grimes }
908f6b04d2bSDavid Greenman 
909f6b04d2bSDavid Greenman struct vnode *
91024a1cce3SDavid Greenman vnode_pager_lock(object)
91124a1cce3SDavid Greenman 	vm_object_t object;
91224a1cce3SDavid Greenman {
913996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
914996c772fSJohn Dyson 
91524a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
91624a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
917f6b04d2bSDavid Greenman 			continue;
91847221757SJohn Dyson 		if (object->flags & OBJ_DEAD)
91947221757SJohn Dyson 			return NULL;
920f6b04d2bSDavid Greenman 
92147221757SJohn Dyson 		while (vget(object->handle,
92247221757SJohn Dyson 			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) {
92347221757SJohn Dyson 			printf("vnode_pager_lock: retrying\n");
92447221757SJohn Dyson 		}
92524a1cce3SDavid Greenman 		return object->handle;
92626f9a767SRodney W. Grimes 	}
92724a1cce3SDavid Greenman 	return NULL;
928f6b04d2bSDavid Greenman }
929