xref: /freebsd/sys/vm/vnode_pager.c (revision 5929bcfaba4ee663511173d3241213fc86bb5fb4)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
326f9a767SRodney W. Grimes  * Copyright (c) 1991 The Regents of the University of California.
426f9a767SRodney W. Grimes  * All rights reserved.
526f9a767SRodney W. Grimes  * Copyright (c) 1993, 1994 John S. Dyson
624a1cce3SDavid Greenman  * Copyright (c) 1995, David Greenman
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
215929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
4026f9a767SRodney W. Grimes  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
41c3aac50fSPeter Wemm  * $FreeBSD$
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes /*
45df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
46df8bae1dSRodney W. Grimes  */
47df8bae1dSRodney W. Grimes 
4826f9a767SRodney W. Grimes /*
4926f9a767SRodney W. Grimes  * TODO:
5024a1cce3SDavid Greenman  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
51f6b04d2bSDavid Greenman  *	greatly re-simplify the vnode_pager.
5226f9a767SRodney W. Grimes  */
5326f9a767SRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <sys/param.h>
55df8bae1dSRodney W. Grimes #include <sys/systm.h>
56df8bae1dSRodney W. Grimes #include <sys/proc.h>
57df8bae1dSRodney W. Grimes #include <sys/vnode.h>
58df8bae1dSRodney W. Grimes #include <sys/mount.h>
5924a1cce3SDavid Greenman #include <sys/buf.h>
60efeaf95aSDavid Greenman #include <sys/vmmeter.h>
6124579ca1SMatthew Dillon #include <sys/conf.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <vm/vm.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));
78e4542174SMatthew Dillon static void 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 
911c7c3c6aSMatthew Dillon int vnode_pbuf_freecnt = -1;	/* start out unlimited */
921c7c3c6aSMatthew Dillon 
93170db9c6SJohn Dyson 
94df8bae1dSRodney W. Grimes /*
95df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
96df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
97df8bae1dSRodney W. Grimes  */
9824a1cce3SDavid Greenman vm_object_t
996cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
100b9dcd593SBruce Evans 		  vm_ooffset_t offset)
101df8bae1dSRodney W. Grimes {
10206cb7259SDavid Greenman 	vm_object_t object;
103df8bae1dSRodney W. Grimes 	struct vnode *vp;
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes 	/*
106df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
107df8bae1dSRodney W. Grimes 	 */
108df8bae1dSRodney W. Grimes 	if (handle == NULL)
109df8bae1dSRodney W. Grimes 		return (NULL);
110df8bae1dSRodney W. Grimes 
1111c7c3c6aSMatthew Dillon 	/*
1121c7c3c6aSMatthew Dillon 	 * XXX hack - This initialization should be put somewhere else.
1131c7c3c6aSMatthew Dillon 	 */
1141c7c3c6aSMatthew Dillon 	if (vnode_pbuf_freecnt < 0) {
1151c7c3c6aSMatthew Dillon 	    vnode_pbuf_freecnt = nswbuf / 2 + 1;
1161c7c3c6aSMatthew Dillon 	}
1171c7c3c6aSMatthew Dillon 
118df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
11939d38f93SDavid Greenman 
12039d38f93SDavid Greenman 	/*
12139d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
12239d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
12339d38f93SDavid Greenman 	 */
12439d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
12539d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
12639d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
12739d38f93SDavid Greenman 	}
12839d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
12939d38f93SDavid Greenman 
13039d38f93SDavid Greenman 	/*
13139d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
13239d38f93SDavid Greenman 	 * go away.
13339d38f93SDavid Greenman 	 */
134bd7e5f99SJohn Dyson 	while (((object = vp->v_object) != NULL) &&
135bd7e5f99SJohn Dyson 		(object->flags & OBJ_DEAD)) {
136aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
13724a1cce3SDavid Greenman 	}
1380d94caffSDavid Greenman 
1392be70f79SJohn Dyson 	if (vp->v_usecount == 0)
1402be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
1412be70f79SJohn Dyson 
14224a1cce3SDavid Greenman 	if (object == NULL) {
143df8bae1dSRodney W. Grimes 		/*
144df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
145df8bae1dSRodney W. Grimes 		 */
1466cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
147ad5dd234SJohn Dyson 		object->flags = 0;
148bbc0ec52SDavid Greenman 
1496cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
15026f9a767SRodney W. Grimes 
15124a1cce3SDavid Greenman 		object->handle = handle;
15224a1cce3SDavid Greenman 		vp->v_object = object;
15395e5e988SJohn Dyson 		vp->v_usecount++;
154df8bae1dSRodney W. Grimes 	} else {
15595e5e988SJohn Dyson 		object->ref_count++;
15695e5e988SJohn Dyson 		vp->v_usecount++;
157df8bae1dSRodney W. Grimes 	}
15839d38f93SDavid Greenman 
15939d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
16039d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
16139d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
16239d38f93SDavid Greenman 		wakeup(vp);
16339d38f93SDavid Greenman 	}
16424a1cce3SDavid Greenman 	return (object);
165df8bae1dSRodney W. Grimes }
166df8bae1dSRodney W. Grimes 
167f708ef1bSPoul-Henning Kamp static void
16824a1cce3SDavid Greenman vnode_pager_dealloc(object)
1690d94caffSDavid Greenman 	vm_object_t object;
17024a1cce3SDavid Greenman {
17124a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
172df8bae1dSRodney W. Grimes 
17324a1cce3SDavid Greenman 	if (vp == NULL)
17424a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
17524a1cce3SDavid Greenman 
17666095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
17724a1cce3SDavid Greenman 
17824a1cce3SDavid Greenman 	object->handle = NULL;
17995461b45SJohn Dyson 	object->type = OBJT_DEAD;
180aa2cabb9SDavid Greenman 	vp->v_object = NULL;
18195e5e988SJohn Dyson 	vp->v_flag &= ~(VTEXT | VOBJBUF);
182df8bae1dSRodney W. Grimes }
18326f9a767SRodney W. Grimes 
184f708ef1bSPoul-Henning Kamp static boolean_t
185a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
18624a1cce3SDavid Greenman 	vm_object_t object;
187a316d390SJohn Dyson 	vm_pindex_t pindex;
18824a1cce3SDavid Greenman 	int *before;
18924a1cce3SDavid Greenman 	int *after;
190df8bae1dSRodney W. Grimes {
19124a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
192df8bae1dSRodney W. Grimes 	daddr_t bn;
1933af76890SPoul-Henning Kamp 	int err;
194170db9c6SJohn Dyson 	daddr_t reqblock;
1952c4488fcSJohn Dyson 	int poff;
1962c4488fcSJohn Dyson 	int bsize;
197d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
198df8bae1dSRodney W. Grimes 
19924579ca1SMatthew Dillon 	/*
20024579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
20124579ca1SMatthew Dillon 	 * have the page.
20224579ca1SMatthew Dillon 	 */
20347221757SJohn Dyson 	if ((vp == NULL) || (vp->v_flag & VDOOMED))
20447221757SJohn Dyson 		return FALSE;
20547221757SJohn Dyson 
206df8bae1dSRodney W. Grimes 	/*
2070d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
2080d94caffSDavid Greenman 	 * not have the page.
209df8bae1dSRodney W. Grimes 	 */
210a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
211a316d390SJohn Dyson 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2124abc71c0SDavid Greenman 		return FALSE;
213df8bae1dSRodney W. Grimes 
214eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
215170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
216d63596ceSJohn Dyson 	blocksperpage = 0;
217d63596ceSJohn Dyson 	if (pagesperblock > 0) {
218a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
219d63596ceSJohn Dyson 	} else {
220d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
221d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
222d63596ceSJohn Dyson 	}
223170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
224170db9c6SJohn Dyson 		after, before);
2250d94caffSDavid Greenman 	if (err)
22624a1cce3SDavid Greenman 		return TRUE;
2276eab77f2SJohn Dyson 	if ( bn == -1)
228ced399eeSJohn Dyson 		return FALSE;
229d63596ceSJohn Dyson 	if (pagesperblock > 0) {
230a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
231170db9c6SJohn Dyson 		if (before) {
232170db9c6SJohn Dyson 			*before *= pagesperblock;
233170db9c6SJohn Dyson 			*before += poff;
234170db9c6SJohn Dyson 		}
235170db9c6SJohn Dyson 		if (after) {
236b1fc01b7SJohn Dyson 			int numafter;
237170db9c6SJohn Dyson 			*after *= pagesperblock;
238b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
239a316d390SJohn Dyson 			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
240a316d390SJohn Dyson 				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
241b1fc01b7SJohn Dyson 			}
242b1fc01b7SJohn Dyson 			*after += numafter;
243170db9c6SJohn Dyson 		}
244d63596ceSJohn Dyson 	} else {
245d63596ceSJohn Dyson 		if (before) {
246d63596ceSJohn Dyson 			*before /= blocksperpage;
247d63596ceSJohn Dyson 		}
248d63596ceSJohn Dyson 
249d63596ceSJohn Dyson 		if (after) {
250d63596ceSJohn Dyson 			*after /= blocksperpage;
251d63596ceSJohn Dyson 		}
252d63596ceSJohn Dyson 	}
253ced399eeSJohn Dyson 	return TRUE;
254df8bae1dSRodney W. Grimes }
255df8bae1dSRodney W. Grimes 
256df8bae1dSRodney W. Grimes /*
257df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
25824a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
259df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
260df8bae1dSRodney W. Grimes  *
261df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
262df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
263df8bae1dSRodney W. Grimes  */
264df8bae1dSRodney W. Grimes void
265df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
266df8bae1dSRodney W. Grimes 	struct vnode *vp;
267a316d390SJohn Dyson 	vm_ooffset_t nsize;
268df8bae1dSRodney W. Grimes {
269c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
27024a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
271df8bae1dSRodney W. Grimes 
27224a1cce3SDavid Greenman 	if (object == NULL)
273df8bae1dSRodney W. Grimes 		return;
274bbc0ec52SDavid Greenman 
275df8bae1dSRodney W. Grimes 	/*
276df8bae1dSRodney W. Grimes 	 * Hasn't changed size
277df8bae1dSRodney W. Grimes 	 */
27824a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
279df8bae1dSRodney W. Grimes 		return;
280bbc0ec52SDavid Greenman 
281c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
282c576d121SLuoqi Chen 
283df8bae1dSRodney W. Grimes 	/*
284bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
285df8bae1dSRodney W. Grimes 	 */
28624a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
2871efb74fbSJohn Dyson 		vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
288c576d121SLuoqi Chen 		if (nobjsize < object->size) {
289c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
290c576d121SLuoqi Chen 				FALSE);
2910d94caffSDavid Greenman 		}
292bbc0ec52SDavid Greenman 		/*
293bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
294bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
295bbc0ec52SDavid Greenman 		 */
296bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
297bbc0ec52SDavid Greenman 			vm_offset_t kva;
298bbc0ec52SDavid Greenman 			vm_page_t m;
299bbc0ec52SDavid Greenman 
300a316d390SJohn Dyson 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
301bbc0ec52SDavid Greenman 			if (m) {
302bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
303bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
304a316d390SJohn Dyson 				    (int) (round_page(nsize) - nsize));
305bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
306bbc0ec52SDavid Greenman 			}
307bbc0ec52SDavid Greenman 		}
308bbc0ec52SDavid Greenman 	}
309a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
310c576d121SLuoqi Chen 	object->size = nobjsize;
311df8bae1dSRodney W. Grimes }
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes void
31426f9a767SRodney W. Grimes vnode_pager_freepage(m)
31526f9a767SRodney W. Grimes 	vm_page_t m;
316df8bae1dSRodney W. Grimes {
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 
405897a45efSDmitrij Tejblum 		if (vm_page_bits(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) {
4111c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
41226f9a767SRodney W. Grimes 
41326f9a767SRodney W. Grimes 			/* build a minimal buffer header */
41421144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
41526f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
416b0eeea20SPoul-Henning Kamp 			bp->b_rcred = bp->b_wcred = curproc->p_ucred;
41726f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
41826f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
41926f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
42026f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
421ab3f7469SPoul-Henning Kamp 			bp->b_data = (caddr_t) kva + i * bsize;
422187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4230d94caffSDavid Greenman 			pbgetvp(dp, bp);
42426f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
42526f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
42626f9a767SRodney W. Grimes 
42726f9a767SRodney W. Grimes 			/* do the input */
428b99c307aSPoul-Henning Kamp 			BUF_STRATEGY(bp);
42926f9a767SRodney W. Grimes 
430e47ed70bSJohn Dyson 			/* we definitely need to be at splvm here */
43126f9a767SRodney W. Grimes 
432e47ed70bSJohn Dyson 			s = splvm();
43326f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
434aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
43526f9a767SRodney W. Grimes 			}
43626f9a767SRodney W. Grimes 			splx(s);
43726f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
43826f9a767SRodney W. Grimes 				error = EIO;
43926f9a767SRodney W. Grimes 
44026f9a767SRodney W. Grimes 			/*
44126f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
44226f9a767SRodney W. Grimes 			 */
4431c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
44426f9a767SRodney W. Grimes 			if (error)
44526f9a767SRodney W. Grimes 				break;
4460d94caffSDavid Greenman 
447aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
44826f9a767SRodney W. Grimes 		} else {
449aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
45026f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
45126f9a767SRodney W. Grimes 		}
45226f9a767SRodney W. Grimes 	}
45326f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
45467bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
455e69763a3SDoug Rabson 	vm_page_flag_clear(m, PG_ZERO);
45626f9a767SRodney W. Grimes 	if (error) {
457a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
45826f9a767SRodney W. Grimes 	}
45926f9a767SRodney W. Grimes 	return VM_PAGER_OK;
46026f9a767SRodney W. Grimes 
46126f9a767SRodney W. Grimes }
46226f9a767SRodney W. Grimes 
46326f9a767SRodney W. Grimes 
46426f9a767SRodney W. Grimes /*
46526f9a767SRodney W. Grimes  * old style vnode pager output routine
46626f9a767SRodney W. Grimes  */
467f708ef1bSPoul-Henning Kamp static int
46824a1cce3SDavid Greenman vnode_pager_input_old(object, m)
46924a1cce3SDavid Greenman 	vm_object_t object;
47026f9a767SRodney W. Grimes 	vm_page_t m;
47126f9a767SRodney W. Grimes {
472df8bae1dSRodney W. Grimes 	struct uio auio;
473df8bae1dSRodney W. Grimes 	struct iovec aiov;
47426f9a767SRodney W. Grimes 	int error;
47526f9a767SRodney W. Grimes 	int size;
47626f9a767SRodney W. Grimes 	vm_offset_t kva;
477df8bae1dSRodney W. Grimes 
47826f9a767SRodney W. Grimes 	error = 0;
479bbc0ec52SDavid Greenman 
480df8bae1dSRodney W. Grimes 	/*
48126f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
48226f9a767SRodney W. Grimes 	 */
483a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
48426f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
48526f9a767SRodney W. Grimes 	} else {
48626f9a767SRodney W. Grimes 		size = PAGE_SIZE;
487a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
488a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
4890bdb7528SDavid Greenman 
49026f9a767SRodney W. Grimes 		/*
491df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
492df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
493df8bae1dSRodney W. Grimes 		 */
49426f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
4950bdb7528SDavid Greenman 
496df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
497df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
498df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
499df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
500a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
501df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
50226f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
503df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
504af1f63c7SRobert V. Baron 		auio.uio_procp = curproc;
50526f9a767SRodney W. Grimes 
50624a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
507df8bae1dSRodney W. Grimes 		if (!error) {
508df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes 			if (count == 0)
511df8bae1dSRodney W. Grimes 				error = EINVAL;
51226f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
51326f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
514df8bae1dSRodney W. Grimes 		}
51526f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
516df8bae1dSRodney W. Grimes 	}
51767bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5182c28a105SAlan Cox 	vm_page_undirty(m);
519e69763a3SDoug Rabson 	vm_page_flag_clear(m, PG_ZERO);
5206e3a3f38SRobert V. Baron 	if (!error)
5216e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
522a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
52326f9a767SRodney W. Grimes }
52426f9a767SRodney W. Grimes 
52526f9a767SRodney W. Grimes /*
52626f9a767SRodney W. Grimes  * generic vnode pager input routine
52726f9a767SRodney W. Grimes  */
528170db9c6SJohn Dyson 
529ce75f2c3SMike Smith /*
530ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
531ce75f2c3SMike Smith  * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to
532ce75f2c3SMike Smith  * vnode_pager_generic_getpages() to implement the previous behaviour.
533ce75f2c3SMike Smith  *
534ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
535ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
536ce75f2c3SMike Smith  */
537f708ef1bSPoul-Henning Kamp static int
53824a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
53926f9a767SRodney W. Grimes 	vm_object_t object;
54024a1cce3SDavid Greenman 	vm_page_t *m;
54124a1cce3SDavid Greenman 	int count;
54224a1cce3SDavid Greenman 	int reqpage;
54324a1cce3SDavid Greenman {
544170db9c6SJohn Dyson 	int rtval;
545170db9c6SJohn Dyson 	struct vnode *vp;
54686ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
54795e5e988SJohn Dyson 
548170db9c6SJohn Dyson 	vp = object->handle;
549ce75f2c3SMike Smith 	/*
550ce75f2c3SMike Smith 	 * XXX temporary diagnostic message to help track stale FS code,
551ce75f2c3SMike Smith 	 * Returning EOPNOTSUPP from here may make things unhappy.
552ce75f2c3SMike Smith 	 */
55386ffbd76SMike Smith 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
55486ffbd76SMike Smith 	if (rtval == EOPNOTSUPP) {
55586ffbd76SMike Smith 	    printf("vnode_pager: *** WARNING *** stale FS getpages\n");
55686ffbd76SMike Smith 	    rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage);
55786ffbd76SMike Smith 	}
558170db9c6SJohn Dyson 	return rtval;
559170db9c6SJohn Dyson }
560170db9c6SJohn Dyson 
561ce75f2c3SMike Smith 
562ce75f2c3SMike Smith /*
563ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
564ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
565ce75f2c3SMike Smith  */
566ce75f2c3SMike Smith int
567ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
568ce75f2c3SMike Smith 	struct vnode *vp;
569170db9c6SJohn Dyson 	vm_page_t *m;
570ce75f2c3SMike Smith 	int bytecount;
571170db9c6SJohn Dyson 	int reqpage;
572170db9c6SJohn Dyson {
573ce75f2c3SMike Smith 	vm_object_t object;
574a316d390SJohn Dyson 	vm_offset_t kva;
5758f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
57624a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
577ce75f2c3SMike Smith 	struct vnode *dp;
578efc68ce1SDavid Greenman 	int runpg;
579efc68ce1SDavid Greenman 	int runend;
5800bdb7528SDavid Greenman 	struct buf *bp;
58126f9a767SRodney W. Grimes 	int s;
582ce75f2c3SMike Smith 	int count;
58326f9a767SRodney W. Grimes 	int error = 0;
58426f9a767SRodney W. Grimes 
585ce75f2c3SMike Smith 	object = vp->v_object;
586ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
587ce75f2c3SMike Smith 
5882c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
5892c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5902c4488fcSJohn Dyson 
59126f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
59226f9a767SRodney W. Grimes 
59326f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
594bbc0ec52SDavid Greenman 
59526f9a767SRodney W. Grimes 	/*
596bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
597bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
59826f9a767SRodney W. Grimes 	 */
599a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
600bbc0ec52SDavid Greenman 
60126f9a767SRodney W. Grimes 	/*
60216f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
60326f9a767SRodney W. Grimes 	 */
604c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
60526f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
60626f9a767SRodney W. Grimes 			if (i != reqpage) {
60726f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
60826f9a767SRodney W. Grimes 			}
60926f9a767SRodney W. Grimes 		}
610976e77fcSDavid Greenman 		cnt.v_vnodein++;
611976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
61224a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
613bbc0ec52SDavid Greenman 
61426f9a767SRodney W. Grimes 		/*
61526f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
61626f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
61726f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
61826f9a767SRodney W. Grimes 		 */
61926f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
620500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
62126f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
62226f9a767SRodney W. Grimes 			if (i != reqpage) {
62326f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
62426f9a767SRodney W. Grimes 			}
62526f9a767SRodney W. Grimes 		}
626976e77fcSDavid Greenman 		cnt.v_vnodein++;
627976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
62824a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
62926f9a767SRodney W. Grimes 	}
6308d17e694SJulian Elischer 
63126f9a767SRodney W. Grimes 	/*
6328d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
6338d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
6348d17e694SJulian Elischer 	 * media.
6350d94caffSDavid Greenman 	 */
63632ad9cb5SDoug Rabson 
6378d17e694SJulian Elischer 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
6380d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6390d94caffSDavid Greenman 			if (i != reqpage)
6400d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6410d94caffSDavid Greenman 		}
6420d94caffSDavid Greenman 		return VM_PAGER_OK;
6430d94caffSDavid Greenman 	}
6448d17e694SJulian Elischer 	m[reqpage]->valid = 0;
6450bdb7528SDavid Greenman 
6460d94caffSDavid Greenman 	/*
64726f9a767SRodney W. Grimes 	 * here on direct device I/O
64826f9a767SRodney W. Grimes 	 */
64926f9a767SRodney W. Grimes 
650efc68ce1SDavid Greenman 	firstaddr = -1;
65126f9a767SRodney W. Grimes 	/*
652efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
65326f9a767SRodney W. Grimes 	 */
654efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
655a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
656a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
657efc68ce1SDavid Greenman 		if (firstaddr == -1) {
65824a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
659fc62ef1fSBruce Evans 				/* XXX no %qd in kernel. */
660fc62ef1fSBruce Evans 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx",
661fc62ef1fSBruce Evans 			   	 firstaddr, (u_long)(foff >> 32),
662fc62ef1fSBruce Evans 			   	 (u_long)(u_int32_t)foff,
663fc62ef1fSBruce Evans 				 (u_long)(u_int32_t)
664fc62ef1fSBruce Evans 				 (object->un_pager.vnp.vnp_size >> 32),
665fc62ef1fSBruce Evans 				 (u_long)(u_int32_t)
666fc62ef1fSBruce Evans 				 object->un_pager.vnp.vnp_size);
667efc68ce1SDavid Greenman 			}
66826f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
669efc68ce1SDavid Greenman 			runend = i + 1;
670efc68ce1SDavid Greenman 			first = runend;
671efc68ce1SDavid Greenman 			continue;
672efc68ce1SDavid Greenman 		}
673efc68ce1SDavid Greenman 		runend = i + runpg;
674efc68ce1SDavid Greenman 		if (runend <= reqpage) {
675efc68ce1SDavid Greenman 			int j;
676efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
677efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
678efc68ce1SDavid Greenman 			}
67926f9a767SRodney W. Grimes 		} else {
680efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
681efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
68226f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
683efc68ce1SDavid Greenman 				count = first + runpg;
68426f9a767SRodney W. Grimes 			}
685efc68ce1SDavid Greenman 			break;
68626f9a767SRodney W. Grimes 		}
687efc68ce1SDavid Greenman 		first = runend;
688efc68ce1SDavid Greenman 	}
68926f9a767SRodney W. Grimes 
69026f9a767SRodney W. Grimes 	/*
691bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
692bbc0ec52SDavid Greenman 	 * to be zero based...
69326f9a767SRodney W. Grimes 	 */
69426f9a767SRodney W. Grimes 	if (first != 0) {
69526f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
69626f9a767SRodney W. Grimes 			m[i - first] = m[i];
69726f9a767SRodney W. Grimes 		}
69826f9a767SRodney W. Grimes 		count -= first;
69926f9a767SRodney W. Grimes 		reqpage -= first;
70026f9a767SRodney W. Grimes 	}
701efc68ce1SDavid Greenman 
70226f9a767SRodney W. Grimes 	/*
70326f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
70426f9a767SRodney W. Grimes 	 */
705a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
70626f9a767SRodney W. Grimes 
70726f9a767SRodney W. Grimes 	/*
70826f9a767SRodney W. Grimes 	 * calculate the size of the transfer
70926f9a767SRodney W. Grimes 	 */
71026f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
71124a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
71224a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
71326f9a767SRodney W. Grimes 
71426f9a767SRodney W. Grimes 	/*
71524579ca1SMatthew Dillon 	 * round up physical size for real devices.
71626f9a767SRodney W. Grimes 	 */
71724579ca1SMatthew Dillon 	if (dp->v_type == VBLK || dp->v_type == VCHR) {
71824579ca1SMatthew Dillon 		int secmask = dp->v_rdev->si_bsize_phys - 1;
71924579ca1SMatthew Dillon 		KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
72024579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
72124579ca1SMatthew Dillon 	}
72226f9a767SRodney W. Grimes 
7231c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
72416f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
72516f62314SDavid Greenman 
72626f9a767SRodney W. Grimes 	/*
72726f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
72826f9a767SRodney W. Grimes 	 */
72916f62314SDavid Greenman 	pmap_qenter(kva, m, count);
73026f9a767SRodney W. Grimes 
73126f9a767SRodney W. Grimes 	/* build a minimal buffer header */
73221144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
73326f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
73426f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
735b0eeea20SPoul-Henning Kamp 	bp->b_rcred = bp->b_wcred = curproc->p_ucred;
73626f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
73726f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
73826f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
73926f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
740187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7410d94caffSDavid Greenman 	pbgetvp(dp, bp);
74226f9a767SRodney W. Grimes 	bp->b_bcount = size;
74326f9a767SRodney W. Grimes 	bp->b_bufsize = size;
74426f9a767SRodney W. Grimes 
745976e77fcSDavid Greenman 	cnt.v_vnodein++;
746976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
747976e77fcSDavid Greenman 
74826f9a767SRodney W. Grimes 	/* do the input */
749b99c307aSPoul-Henning Kamp 	BUF_STRATEGY(bp);
750976e77fcSDavid Greenman 
751e47ed70bSJohn Dyson 	s = splvm();
752e47ed70bSJohn Dyson 	/* we definitely need to be at splvm here */
75326f9a767SRodney W. Grimes 
75426f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
755aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
75626f9a767SRodney W. Grimes 	}
75726f9a767SRodney W. Grimes 	splx(s);
75826f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
75926f9a767SRodney W. Grimes 		error = EIO;
76026f9a767SRodney W. Grimes 
76126f9a767SRodney W. Grimes 	if (!error) {
76226f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
76326f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
76426f9a767SRodney W. Grimes 	}
76516f62314SDavid Greenman 	pmap_qremove(kva, count);
76626f9a767SRodney W. Grimes 
76726f9a767SRodney W. Grimes 	/*
76826f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
76926f9a767SRodney W. Grimes 	 */
7701c7c3c6aSMatthew Dillon 	relpbuf(bp, &vnode_pbuf_freecnt);
77126f9a767SRodney W. Grimes 
7728f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
7738f9110f6SJohn Dyson 		vm_page_t mt;
7748f9110f6SJohn Dyson 
7758f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
7768f9110f6SJohn Dyson 		mt = m[i];
7778f9110f6SJohn Dyson 
77854746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
7798d17e694SJulian Elischer 			/*
7808d17e694SJulian Elischer 			 * Read filled up entire page.
7818d17e694SJulian Elischer 			 */
7828f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
7832c28a105SAlan Cox 			vm_page_undirty(mt);	/* should be an assert? XXX */
7848f9110f6SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
7858f9110f6SJohn Dyson 		} else {
7868d17e694SJulian Elischer 			/*
7878d17e694SJulian Elischer 			 * Read did not fill up entire page.  Since this
7888d17e694SJulian Elischer 			 * is getpages, the page may be mapped, so we have
7898d17e694SJulian Elischer 			 * to zero the invalid portions of the page even
7908d17e694SJulian Elischer 			 * though we aren't setting them valid.
7918d17e694SJulian Elischer 			 *
7928d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
7938d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
7948d17e694SJulian Elischer 			 * read.
7958d17e694SJulian Elischer 			 */
79654746b67SDmitrij Tejblum 			vm_page_set_validclean(mt, 0,
79754746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
7984221e284SAlan Cox 			/* handled by vm_fault now */
7994221e284SAlan Cox 			/* vm_page_zero_invalid(mt, FALSE); */
8008f9110f6SJohn Dyson 		}
8018f9110f6SJohn Dyson 
802e69763a3SDoug Rabson 		vm_page_flag_clear(mt, PG_ZERO);
80326f9a767SRodney W. Grimes 		if (i != reqpage) {
804bbc0ec52SDavid Greenman 
80526f9a767SRodney W. Grimes 			/*
806bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
807bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
808bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
809956f3135SPhilippe Charnier 			 * It appears that empirical results show that
810bbc0ec52SDavid Greenman 			 * deactivating pages is best.
81126f9a767SRodney W. Grimes 			 */
812bbc0ec52SDavid Greenman 
81326f9a767SRodney W. Grimes 			/*
814bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
815bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
81626f9a767SRodney W. Grimes 			 */
81726f9a767SRodney W. Grimes 			if (!error) {
8188f9110f6SJohn Dyson 				if (mt->flags & PG_WANTED)
8198f9110f6SJohn Dyson 					vm_page_activate(mt);
82095461b45SJohn Dyson 				else
8218f9110f6SJohn Dyson 					vm_page_deactivate(mt);
822e69763a3SDoug Rabson 				vm_page_wakeup(mt);
82326f9a767SRodney W. Grimes 			} else {
8248f9110f6SJohn Dyson 				vnode_pager_freepage(mt);
82526f9a767SRodney W. Grimes 			}
82626f9a767SRodney W. Grimes 		}
82726f9a767SRodney W. Grimes 	}
82826f9a767SRodney W. Grimes 	if (error) {
82924a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
83026f9a767SRodney W. Grimes 	}
831a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
83226f9a767SRodney W. Grimes }
83326f9a767SRodney W. Grimes 
834ce75f2c3SMike Smith /*
835ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
836ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
837ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
838ce75f2c3SMike Smith  *
839ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
840ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
841ce75f2c3SMike Smith  */
842e4542174SMatthew Dillon static void
843170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
844170db9c6SJohn Dyson 	vm_object_t object;
845170db9c6SJohn Dyson 	vm_page_t *m;
846170db9c6SJohn Dyson 	int count;
847170db9c6SJohn Dyson 	boolean_t sync;
848170db9c6SJohn Dyson 	int *rtvals;
849170db9c6SJohn Dyson {
850170db9c6SJohn Dyson 	int rtval;
851170db9c6SJohn Dyson 	struct vnode *vp;
85286ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
853ad980522SJohn Dyson 
8540e3cdf2cSAlan Cox 	/*
8550e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
8560e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
8570e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
8580e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
8590e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
8600e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
8610e3cdf2cSAlan Cox 	 *
8620e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
8630e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
8640e3cdf2cSAlan Cox 	 */
8650e3cdf2cSAlan Cox 
8660e3cdf2cSAlan Cox 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
8670e3cdf2cSAlan Cox 		sync |= OBJPC_SYNC;
8680e3cdf2cSAlan Cox 
8690e3cdf2cSAlan Cox 	/*
8700e3cdf2cSAlan Cox 	 * Call device-specific putpages function
8710e3cdf2cSAlan Cox 	 */
8720e3cdf2cSAlan Cox 
873170db9c6SJohn Dyson 	vp = object->handle;
87486ffbd76SMike Smith 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
87586ffbd76SMike Smith 	if (rtval == EOPNOTSUPP) {
87686ffbd76SMike Smith 	    printf("vnode_pager: *** WARNING *** stale FS putpages\n");
87786ffbd76SMike Smith 	    rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals);
87886ffbd76SMike Smith 	}
879170db9c6SJohn Dyson }
880170db9c6SJohn Dyson 
881ce75f2c3SMike Smith 
88226f9a767SRodney W. Grimes /*
883ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
8844491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
88526f9a767SRodney W. Grimes  */
886ce75f2c3SMike Smith int
8878f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
888ce75f2c3SMike Smith 	struct vnode *vp;
88926f9a767SRodney W. Grimes 	vm_page_t *m;
890ce75f2c3SMike Smith 	int bytecount;
8918f9110f6SJohn Dyson 	int flags;
89226f9a767SRodney W. Grimes 	int *rtvals;
89326f9a767SRodney W. Grimes {
894f6b04d2bSDavid Greenman 	int i;
895ce75f2c3SMike Smith 	vm_object_t object;
896ce75f2c3SMike Smith 	int count;
89726f9a767SRodney W. Grimes 
898f6b04d2bSDavid Greenman 	int maxsize, ncount;
899a316d390SJohn Dyson 	vm_ooffset_t poffset;
900f6b04d2bSDavid Greenman 	struct uio auio;
901f6b04d2bSDavid Greenman 	struct iovec aiov;
902f6b04d2bSDavid Greenman 	int error;
9038f9110f6SJohn Dyson 	int ioflags;
90426f9a767SRodney W. Grimes 
905ce75f2c3SMike Smith 	object = vp->v_object;
906ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
907ce75f2c3SMike Smith 
90826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
90926f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
91026f9a767SRodney W. Grimes 
911a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
9123efc015bSPeter Wemm 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n",
9133efc015bSPeter Wemm 			(long)m[0]->pindex, m[0]->dirty);
914f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
915f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
9160d94caffSDavid Greenman 	}
9170bdb7528SDavid Greenman 
918f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
919f6b04d2bSDavid Greenman 	ncount = count;
92026f9a767SRodney W. Grimes 
921a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
922a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
923a316d390SJohn Dyson 		if (object->un_pager.vnp.vnp_size > poffset)
924a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
9255f55e841SDavid Greenman 		else
9265f55e841SDavid Greenman 			maxsize = 0;
927aa8de40aSPoul-Henning Kamp 		ncount = btoc(maxsize);
928f6b04d2bSDavid Greenman 		if (ncount < count) {
929f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
930f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
931f6b04d2bSDavid Greenman 			}
932f6b04d2bSDavid Greenman 		}
933f6b04d2bSDavid Greenman 	}
93426f9a767SRodney W. Grimes 
9358f9110f6SJohn Dyson 	ioflags = IO_VMIO;
9368f9110f6SJohn Dyson 	ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: 0;
9378f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
938f6b04d2bSDavid Greenman 
939f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
940f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
941f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
942f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
943a316d390SJohn Dyson 	auio.uio_offset = poffset;
944f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
945f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
946f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
947f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
9488f9110f6SJohn Dyson 	error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred);
949976e77fcSDavid Greenman 	cnt.v_vnodeout++;
950f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
951f6b04d2bSDavid Greenman 
952f6b04d2bSDavid Greenman 	if (error) {
95324a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
954f6b04d2bSDavid Greenman 	}
955f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
956ac1e407bSBruce Evans 		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
957ac1e407bSBruce Evans 		    auio.uio_resid, (u_long)m[0]->pindex);
95826f9a767SRodney W. Grimes 	}
959ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
96026f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
96126f9a767SRodney W. Grimes 	}
962f6b04d2bSDavid Greenman 	return rtvals[0];
96326f9a767SRodney W. Grimes }
964f6b04d2bSDavid Greenman 
965f6b04d2bSDavid Greenman struct vnode *
96624a1cce3SDavid Greenman vnode_pager_lock(object)
96724a1cce3SDavid Greenman 	vm_object_t object;
96824a1cce3SDavid Greenman {
969996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
970996c772fSJohn Dyson 
97124a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
97224a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
973f6b04d2bSDavid Greenman 			continue;
97447221757SJohn Dyson 		if (object->flags & OBJ_DEAD)
97547221757SJohn Dyson 			return NULL;
976f6b04d2bSDavid Greenman 
97747221757SJohn Dyson 		while (vget(object->handle,
97847221757SJohn Dyson 			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) {
979bef608bdSJohn Dyson 			if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE))
980bef608bdSJohn Dyson 				return NULL;
98147221757SJohn Dyson 			printf("vnode_pager_lock: retrying\n");
98247221757SJohn Dyson 		}
98324a1cce3SDavid Greenman 		return object->handle;
98426f9a767SRodney W. Grimes 	}
98524a1cce3SDavid Greenman 	return NULL;
986f6b04d2bSDavid Greenman }
987