xref: /freebsd/sys/vm/vnode_pager.c (revision 24579ca1d7a4ffaa220af5bd7593aa759e1d9c38)
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
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_prot.h>
65efeaf95aSDavid Greenman #include <vm/vm_object.h>
66df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
6724a1cce3SDavid Greenman #include <vm/vm_pager.h>
681efb74fbSJohn Dyson #include <vm/vm_map.h>
69df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
70efeaf95aSDavid Greenman #include <vm/vm_extern.h>
71df8bae1dSRodney W. Grimes 
72f708ef1bSPoul-Henning Kamp static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
730b8253a7SBruce Evans 					 int *run));
74f708ef1bSPoul-Henning Kamp static void vnode_pager_iodone __P((struct buf *bp));
75f708ef1bSPoul-Henning Kamp static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
76f708ef1bSPoul-Henning Kamp static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
77f708ef1bSPoul-Henning Kamp static void vnode_pager_dealloc __P((vm_object_t));
78f708ef1bSPoul-Henning Kamp static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
79e4542174SMatthew Dillon static void vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
80f708ef1bSPoul-Henning Kamp static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
810b8253a7SBruce Evans 
82df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
8324a1cce3SDavid Greenman 	NULL,
84df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
85df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
8624a1cce3SDavid Greenman 	vnode_pager_getpages,
8724a1cce3SDavid Greenman 	vnode_pager_putpages,
8824a1cce3SDavid Greenman 	vnode_pager_haspage,
8924a1cce3SDavid Greenman 	NULL
90df8bae1dSRodney W. Grimes };
91df8bae1dSRodney W. Grimes 
921c7c3c6aSMatthew Dillon int vnode_pbuf_freecnt = -1;	/* start out unlimited */
931c7c3c6aSMatthew Dillon 
94170db9c6SJohn Dyson 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
97df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
98df8bae1dSRodney W. Grimes  */
9924a1cce3SDavid Greenman vm_object_t
1006cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
101b9dcd593SBruce Evans 		  vm_ooffset_t offset)
102df8bae1dSRodney W. Grimes {
10306cb7259SDavid Greenman 	vm_object_t object;
104df8bae1dSRodney W. Grimes 	struct vnode *vp;
105df8bae1dSRodney W. Grimes 
106df8bae1dSRodney W. Grimes 	/*
107df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
108df8bae1dSRodney W. Grimes 	 */
109df8bae1dSRodney W. Grimes 	if (handle == NULL)
110df8bae1dSRodney W. Grimes 		return (NULL);
111df8bae1dSRodney W. Grimes 
1121c7c3c6aSMatthew Dillon 	/*
1131c7c3c6aSMatthew Dillon 	 * XXX hack - This initialization should be put somewhere else.
1141c7c3c6aSMatthew Dillon 	 */
1151c7c3c6aSMatthew Dillon 	if (vnode_pbuf_freecnt < 0) {
1161c7c3c6aSMatthew Dillon 	    vnode_pbuf_freecnt = nswbuf / 2 + 1;
1171c7c3c6aSMatthew Dillon 	}
1181c7c3c6aSMatthew Dillon 
119df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
12039d38f93SDavid Greenman 
12139d38f93SDavid Greenman 	/*
12239d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
12339d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
12439d38f93SDavid Greenman 	 */
12539d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
12639d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
12739d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
12839d38f93SDavid Greenman 	}
12939d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
13039d38f93SDavid Greenman 
13139d38f93SDavid Greenman 	/*
13239d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
13339d38f93SDavid Greenman 	 * go away.
13439d38f93SDavid Greenman 	 */
135bd7e5f99SJohn Dyson 	while (((object = vp->v_object) != NULL) &&
136bd7e5f99SJohn Dyson 		(object->flags & OBJ_DEAD)) {
137aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
13824a1cce3SDavid Greenman 	}
1390d94caffSDavid Greenman 
1402be70f79SJohn Dyson 	if (vp->v_usecount == 0)
1412be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
1422be70f79SJohn Dyson 
14324a1cce3SDavid Greenman 	if (object == NULL) {
144df8bae1dSRodney W. Grimes 		/*
145df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
146df8bae1dSRodney W. Grimes 		 */
1476cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
148ad5dd234SJohn Dyson 		object->flags = 0;
149bbc0ec52SDavid Greenman 
1506cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
15126f9a767SRodney W. Grimes 
15224a1cce3SDavid Greenman 		object->handle = handle;
15324a1cce3SDavid Greenman 		vp->v_object = object;
15495e5e988SJohn Dyson 		vp->v_usecount++;
155df8bae1dSRodney W. Grimes 	} else {
15695e5e988SJohn Dyson 		object->ref_count++;
15795e5e988SJohn Dyson 		vp->v_usecount++;
158df8bae1dSRodney W. Grimes 	}
15939d38f93SDavid Greenman 
16039d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
16139d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
16239d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
16339d38f93SDavid Greenman 		wakeup(vp);
16439d38f93SDavid Greenman 	}
16524a1cce3SDavid Greenman 	return (object);
166df8bae1dSRodney W. Grimes }
167df8bae1dSRodney W. Grimes 
168f708ef1bSPoul-Henning Kamp static void
16924a1cce3SDavid Greenman vnode_pager_dealloc(object)
1700d94caffSDavid Greenman 	vm_object_t object;
17124a1cce3SDavid Greenman {
17224a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
173df8bae1dSRodney W. Grimes 
17424a1cce3SDavid Greenman 	if (vp == NULL)
17524a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
17624a1cce3SDavid Greenman 
17766095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
17824a1cce3SDavid Greenman 
17924a1cce3SDavid Greenman 	object->handle = NULL;
18095461b45SJohn Dyson 	object->type = OBJT_DEAD;
181aa2cabb9SDavid Greenman 	vp->v_object = NULL;
18295e5e988SJohn Dyson 	vp->v_flag &= ~(VTEXT | VOBJBUF);
183df8bae1dSRodney W. Grimes }
18426f9a767SRodney W. Grimes 
185f708ef1bSPoul-Henning Kamp static boolean_t
186a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
18724a1cce3SDavid Greenman 	vm_object_t object;
188a316d390SJohn Dyson 	vm_pindex_t pindex;
18924a1cce3SDavid Greenman 	int *before;
19024a1cce3SDavid Greenman 	int *after;
191df8bae1dSRodney W. Grimes {
19224a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
193df8bae1dSRodney W. Grimes 	daddr_t bn;
1943af76890SPoul-Henning Kamp 	int err;
195170db9c6SJohn Dyson 	daddr_t reqblock;
1962c4488fcSJohn Dyson 	int poff;
1972c4488fcSJohn Dyson 	int bsize;
198d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
199df8bae1dSRodney W. Grimes 
20024579ca1SMatthew Dillon 	/*
20124579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
20224579ca1SMatthew Dillon 	 * have the page.
20324579ca1SMatthew Dillon 	 */
20447221757SJohn Dyson 	if ((vp == NULL) || (vp->v_flag & VDOOMED))
20547221757SJohn Dyson 		return FALSE;
20647221757SJohn Dyson 
207df8bae1dSRodney W. Grimes 	/*
2080d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
2090d94caffSDavid Greenman 	 * not have the page.
210df8bae1dSRodney W. Grimes 	 */
211a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
212a316d390SJohn Dyson 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2134abc71c0SDavid Greenman 		return FALSE;
214df8bae1dSRodney W. Grimes 
215eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
216170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
217d63596ceSJohn Dyson 	blocksperpage = 0;
218d63596ceSJohn Dyson 	if (pagesperblock > 0) {
219a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
220d63596ceSJohn Dyson 	} else {
221d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
222d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
223d63596ceSJohn Dyson 	}
224170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
225170db9c6SJohn Dyson 		after, before);
2260d94caffSDavid Greenman 	if (err)
22724a1cce3SDavid Greenman 		return TRUE;
2286eab77f2SJohn Dyson 	if ( bn == -1)
229ced399eeSJohn Dyson 		return FALSE;
230d63596ceSJohn Dyson 	if (pagesperblock > 0) {
231a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
232170db9c6SJohn Dyson 		if (before) {
233170db9c6SJohn Dyson 			*before *= pagesperblock;
234170db9c6SJohn Dyson 			*before += poff;
235170db9c6SJohn Dyson 		}
236170db9c6SJohn Dyson 		if (after) {
237b1fc01b7SJohn Dyson 			int numafter;
238170db9c6SJohn Dyson 			*after *= pagesperblock;
239b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
240a316d390SJohn Dyson 			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
241a316d390SJohn Dyson 				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
242b1fc01b7SJohn Dyson 			}
243b1fc01b7SJohn Dyson 			*after += numafter;
244170db9c6SJohn Dyson 		}
245d63596ceSJohn Dyson 	} else {
246d63596ceSJohn Dyson 		if (before) {
247d63596ceSJohn Dyson 			*before /= blocksperpage;
248d63596ceSJohn Dyson 		}
249d63596ceSJohn Dyson 
250d63596ceSJohn Dyson 		if (after) {
251d63596ceSJohn Dyson 			*after /= blocksperpage;
252d63596ceSJohn Dyson 		}
253d63596ceSJohn Dyson 	}
254ced399eeSJohn Dyson 	return TRUE;
255df8bae1dSRodney W. Grimes }
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes /*
258df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
25924a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
260df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
261df8bae1dSRodney W. Grimes  *
262df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
263df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
264df8bae1dSRodney W. Grimes  */
265df8bae1dSRodney W. Grimes void
266df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
267df8bae1dSRodney W. Grimes 	struct vnode *vp;
268a316d390SJohn Dyson 	vm_ooffset_t nsize;
269df8bae1dSRodney W. Grimes {
270c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
27124a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
272df8bae1dSRodney W. Grimes 
27324a1cce3SDavid Greenman 	if (object == NULL)
274df8bae1dSRodney W. Grimes 		return;
275bbc0ec52SDavid Greenman 
276df8bae1dSRodney W. Grimes 	/*
277df8bae1dSRodney W. Grimes 	 * Hasn't changed size
278df8bae1dSRodney W. Grimes 	 */
27924a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
280df8bae1dSRodney W. Grimes 		return;
281bbc0ec52SDavid Greenman 
282c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
283c576d121SLuoqi Chen 
284df8bae1dSRodney W. Grimes 	/*
285bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
286df8bae1dSRodney W. Grimes 	 */
28724a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
2881efb74fbSJohn Dyson 		vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
289c576d121SLuoqi Chen 		if (nobjsize < object->size) {
290c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
291c576d121SLuoqi Chen 				FALSE);
2920d94caffSDavid Greenman 		}
293bbc0ec52SDavid Greenman 		/*
294bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
295bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
296bbc0ec52SDavid Greenman 		 */
297bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
298bbc0ec52SDavid Greenman 			vm_offset_t kva;
299bbc0ec52SDavid Greenman 			vm_page_t m;
300bbc0ec52SDavid Greenman 
301a316d390SJohn Dyson 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
302bbc0ec52SDavid Greenman 			if (m) {
303bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
304bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
305a316d390SJohn Dyson 				    (int) (round_page(nsize) - nsize));
306bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
307bbc0ec52SDavid Greenman 			}
308bbc0ec52SDavid Greenman 		}
309bbc0ec52SDavid Greenman 	}
310a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
311c576d121SLuoqi Chen 	object->size = nobjsize;
312df8bae1dSRodney W. Grimes }
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes void
31526f9a767SRodney W. Grimes vnode_pager_freepage(m)
31626f9a767SRodney W. Grimes 	vm_page_t m;
317df8bae1dSRodney W. Grimes {
31826f9a767SRodney W. Grimes 	vm_page_free(m);
31926f9a767SRodney W. Grimes }
32026f9a767SRodney W. Grimes 
32126f9a767SRodney W. Grimes /*
32226f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
32326f9a767SRodney W. Grimes  * file address
32426f9a767SRodney W. Grimes  */
325f708ef1bSPoul-Henning Kamp static vm_offset_t
326efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
32726f9a767SRodney W. Grimes 	struct vnode *vp;
328a316d390SJohn Dyson 	vm_ooffset_t address;
329efc68ce1SDavid Greenman 	int *run;
33026f9a767SRodney W. Grimes {
33126f9a767SRodney W. Grimes 	int rtaddress;
33226f9a767SRodney W. Grimes 	int bsize;
333a316d390SJohn Dyson 	daddr_t block;
33426f9a767SRodney W. Grimes 	struct vnode *rtvp;
33526f9a767SRodney W. Grimes 	int err;
336a316d390SJohn Dyson 	daddr_t vblock;
337a316d390SJohn Dyson 	int voffset;
33826f9a767SRodney W. Grimes 
3390d94caffSDavid Greenman 	if ((int) address < 0)
3400d94caffSDavid Greenman 		return -1;
3410d94caffSDavid Greenman 
3422c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3432c4488fcSJohn Dyson 		return -1;
3442c4488fcSJohn Dyson 
34526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
34626f9a767SRodney W. Grimes 	vblock = address / bsize;
34726f9a767SRodney W. Grimes 	voffset = address % bsize;
34826f9a767SRodney W. Grimes 
349c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
35026f9a767SRodney W. Grimes 
351efc68ce1SDavid Greenman 	if (err || (block == -1))
35226f9a767SRodney W. Grimes 		rtaddress = -1;
353efc68ce1SDavid Greenman 	else {
354187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
355efc68ce1SDavid Greenman 		if( run) {
356efc68ce1SDavid Greenman 			*run += 1;
357efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
358efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
359efc68ce1SDavid Greenman 		}
360efc68ce1SDavid Greenman 	}
36126f9a767SRodney W. Grimes 
36226f9a767SRodney W. Grimes 	return rtaddress;
36326f9a767SRodney W. Grimes }
36426f9a767SRodney W. Grimes 
36526f9a767SRodney W. Grimes /*
36626f9a767SRodney W. Grimes  * interrupt routine for I/O completion
36726f9a767SRodney W. Grimes  */
368f708ef1bSPoul-Henning Kamp static void
36926f9a767SRodney W. Grimes vnode_pager_iodone(bp)
37026f9a767SRodney W. Grimes 	struct buf *bp;
37126f9a767SRodney W. Grimes {
37226f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
37324a1cce3SDavid Greenman 	wakeup(bp);
37426f9a767SRodney W. Grimes }
37526f9a767SRodney W. Grimes 
37626f9a767SRodney W. Grimes /*
37726f9a767SRodney W. Grimes  * small block file system vnode pager input
37826f9a767SRodney W. Grimes  */
379f708ef1bSPoul-Henning Kamp static int
38024a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
38124a1cce3SDavid Greenman 	vm_object_t object;
38226f9a767SRodney W. Grimes 	vm_page_t m;
38326f9a767SRodney W. Grimes {
38426f9a767SRodney W. Grimes 	int i;
38526f9a767SRodney W. Grimes 	int s;
38626f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
38726f9a767SRodney W. Grimes 	struct buf *bp;
38826f9a767SRodney W. Grimes 	vm_offset_t kva;
38926f9a767SRodney W. Grimes 	int fileaddr;
39026f9a767SRodney W. Grimes 	vm_offset_t bsize;
39126f9a767SRodney W. Grimes 	int error = 0;
39226f9a767SRodney W. Grimes 
39324a1cce3SDavid Greenman 	vp = object->handle;
3942c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3952c4488fcSJohn Dyson 		return VM_PAGER_BAD;
3962c4488fcSJohn Dyson 
39726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
39826f9a767SRodney W. Grimes 
3990bdb7528SDavid Greenman 
400c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
40126f9a767SRodney W. Grimes 
40226f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
40326f9a767SRodney W. Grimes 
40426f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
405bbc0ec52SDavid Greenman 
406897a45efSDmitrij Tejblum 		if (vm_page_bits(i * bsize, bsize) & m->valid)
40726f9a767SRodney W. Grimes 			continue;
40826f9a767SRodney W. Grimes 
409a316d390SJohn Dyson 		fileaddr = vnode_pager_addr(vp,
410a316d390SJohn Dyson 			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
41126f9a767SRodney W. Grimes 		if (fileaddr != -1) {
4121c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
41326f9a767SRodney W. Grimes 
41426f9a767SRodney W. Grimes 			/* build a minimal buffer header */
41567812eacSKirk McKusick 			bp->b_flags = B_READ | B_CALL;
41626f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
417b0eeea20SPoul-Henning Kamp 			bp->b_rcred = bp->b_wcred = curproc->p_ucred;
41826f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
41926f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
42026f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
42126f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
422ab3f7469SPoul-Henning Kamp 			bp->b_data = (caddr_t) kva + i * bsize;
423187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4240d94caffSDavid Greenman 			pbgetvp(dp, bp);
42526f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
42626f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
42726f9a767SRodney W. Grimes 
42826f9a767SRodney W. Grimes 			/* do the input */
429fd5d1124SJulian Elischer 			VOP_STRATEGY(bp->b_vp, bp);
43026f9a767SRodney W. Grimes 
431e47ed70bSJohn Dyson 			/* we definitely need to be at splvm here */
43226f9a767SRodney W. Grimes 
433e47ed70bSJohn Dyson 			s = splvm();
43426f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
435aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
43626f9a767SRodney W. Grimes 			}
43726f9a767SRodney W. Grimes 			splx(s);
43826f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
43926f9a767SRodney W. Grimes 				error = EIO;
44026f9a767SRodney W. Grimes 
44126f9a767SRodney W. Grimes 			/*
44226f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
44326f9a767SRodney W. Grimes 			 */
4441c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
44526f9a767SRodney W. Grimes 			if (error)
44626f9a767SRodney W. Grimes 				break;
4470d94caffSDavid Greenman 
448aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
44926f9a767SRodney W. Grimes 		} else {
450aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
45126f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
45226f9a767SRodney W. Grimes 		}
45326f9a767SRodney W. Grimes 	}
45426f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
45567bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
456e69763a3SDoug Rabson 	vm_page_flag_clear(m, PG_ZERO);
45726f9a767SRodney W. Grimes 	if (error) {
458a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
45926f9a767SRodney W. Grimes 	}
46026f9a767SRodney W. Grimes 	return VM_PAGER_OK;
46126f9a767SRodney W. Grimes 
46226f9a767SRodney W. Grimes }
46326f9a767SRodney W. Grimes 
46426f9a767SRodney W. Grimes 
46526f9a767SRodney W. Grimes /*
46626f9a767SRodney W. Grimes  * old style vnode pager output routine
46726f9a767SRodney W. Grimes  */
468f708ef1bSPoul-Henning Kamp static int
46924a1cce3SDavid Greenman vnode_pager_input_old(object, m)
47024a1cce3SDavid Greenman 	vm_object_t object;
47126f9a767SRodney W. Grimes 	vm_page_t m;
47226f9a767SRodney W. Grimes {
473df8bae1dSRodney W. Grimes 	struct uio auio;
474df8bae1dSRodney W. Grimes 	struct iovec aiov;
47526f9a767SRodney W. Grimes 	int error;
47626f9a767SRodney W. Grimes 	int size;
47726f9a767SRodney W. Grimes 	vm_offset_t kva;
478df8bae1dSRodney W. Grimes 
47926f9a767SRodney W. Grimes 	error = 0;
480bbc0ec52SDavid Greenman 
481df8bae1dSRodney W. Grimes 	/*
48226f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
48326f9a767SRodney W. Grimes 	 */
484a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
48526f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
48626f9a767SRodney W. Grimes 	} else {
48726f9a767SRodney W. Grimes 		size = PAGE_SIZE;
488a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
489a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
4900bdb7528SDavid Greenman 
49126f9a767SRodney W. Grimes 		/*
492df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
493df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
494df8bae1dSRodney W. Grimes 		 */
49526f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
4960bdb7528SDavid Greenman 
497df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
498df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
499df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
500df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
501a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
502df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
50326f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
504df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
505af1f63c7SRobert V. Baron 		auio.uio_procp = curproc;
50626f9a767SRodney W. Grimes 
50724a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
508df8bae1dSRodney W. Grimes 		if (!error) {
509df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 			if (count == 0)
512df8bae1dSRodney W. Grimes 				error = EINVAL;
51326f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
51426f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
515df8bae1dSRodney W. Grimes 		}
51626f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
517df8bae1dSRodney W. Grimes 	}
51867bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5192c28a105SAlan Cox 	vm_page_undirty(m);
520e69763a3SDoug Rabson 	vm_page_flag_clear(m, PG_ZERO);
5216e3a3f38SRobert V. Baron 	if (!error)
5226e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
523a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
52426f9a767SRodney W. Grimes }
52526f9a767SRodney W. Grimes 
52626f9a767SRodney W. Grimes /*
52726f9a767SRodney W. Grimes  * generic vnode pager input routine
52826f9a767SRodney W. Grimes  */
529170db9c6SJohn Dyson 
530ce75f2c3SMike Smith /*
531ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
532ce75f2c3SMike Smith  * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to
533ce75f2c3SMike Smith  * vnode_pager_generic_getpages() to implement the previous behaviour.
534ce75f2c3SMike Smith  *
535ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
536ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
537ce75f2c3SMike Smith  */
538f708ef1bSPoul-Henning Kamp static int
53924a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
54026f9a767SRodney W. Grimes 	vm_object_t object;
54124a1cce3SDavid Greenman 	vm_page_t *m;
54224a1cce3SDavid Greenman 	int count;
54324a1cce3SDavid Greenman 	int reqpage;
54424a1cce3SDavid Greenman {
545170db9c6SJohn Dyson 	int rtval;
546170db9c6SJohn Dyson 	struct vnode *vp;
54786ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
54895e5e988SJohn Dyson 
549170db9c6SJohn Dyson 	vp = object->handle;
550ce75f2c3SMike Smith 	/*
551ce75f2c3SMike Smith 	 * XXX temporary diagnostic message to help track stale FS code,
552ce75f2c3SMike Smith 	 * Returning EOPNOTSUPP from here may make things unhappy.
553ce75f2c3SMike Smith 	 */
55486ffbd76SMike Smith 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
55586ffbd76SMike Smith 	if (rtval == EOPNOTSUPP) {
55686ffbd76SMike Smith 	    printf("vnode_pager: *** WARNING *** stale FS getpages\n");
55786ffbd76SMike Smith 	    rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage);
55886ffbd76SMike Smith 	}
559170db9c6SJohn Dyson 	return rtval;
560170db9c6SJohn Dyson }
561170db9c6SJohn Dyson 
562ce75f2c3SMike Smith 
563ce75f2c3SMike Smith /*
564ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
565ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
566ce75f2c3SMike Smith  */
567ce75f2c3SMike Smith int
568ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
569ce75f2c3SMike Smith 	struct vnode *vp;
570170db9c6SJohn Dyson 	vm_page_t *m;
571ce75f2c3SMike Smith 	int bytecount;
572170db9c6SJohn Dyson 	int reqpage;
573170db9c6SJohn Dyson {
574ce75f2c3SMike Smith 	vm_object_t object;
575a316d390SJohn Dyson 	vm_offset_t kva;
5768f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
57724a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
578ce75f2c3SMike Smith 	struct vnode *dp;
579efc68ce1SDavid Greenman 	int runpg;
580efc68ce1SDavid Greenman 	int runend;
5810bdb7528SDavid Greenman 	struct buf *bp;
58226f9a767SRodney W. Grimes 	int s;
583ce75f2c3SMike Smith 	int count;
58426f9a767SRodney W. Grimes 	int error = 0;
58526f9a767SRodney W. Grimes 
586ce75f2c3SMike Smith 	object = vp->v_object;
587ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
588ce75f2c3SMike Smith 
5892c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
5902c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5912c4488fcSJohn Dyson 
59226f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
59326f9a767SRodney W. Grimes 
59426f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
595bbc0ec52SDavid Greenman 
59626f9a767SRodney W. Grimes 	/*
597bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
598bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
59926f9a767SRodney W. Grimes 	 */
600a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
601bbc0ec52SDavid Greenman 
60226f9a767SRodney W. Grimes 	/*
60316f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
60426f9a767SRodney W. Grimes 	 */
605c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
60626f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
60726f9a767SRodney W. Grimes 			if (i != reqpage) {
60826f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
60926f9a767SRodney W. Grimes 			}
61026f9a767SRodney W. Grimes 		}
611976e77fcSDavid Greenman 		cnt.v_vnodein++;
612976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
61324a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
614bbc0ec52SDavid Greenman 
61526f9a767SRodney W. Grimes 		/*
61626f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
61726f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
61826f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
61926f9a767SRodney W. Grimes 		 */
62026f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
621500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
62226f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
62326f9a767SRodney W. Grimes 			if (i != reqpage) {
62426f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
62526f9a767SRodney W. Grimes 			}
62626f9a767SRodney W. Grimes 		}
627976e77fcSDavid Greenman 		cnt.v_vnodein++;
628976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
62924a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
63026f9a767SRodney W. Grimes 	}
6318d17e694SJulian Elischer 
63226f9a767SRodney W. Grimes 	/*
6338d17e694SJulian Elischer 	 * If we have a completely valid page available to us, we can
6348d17e694SJulian Elischer 	 * clean up and return.  Otherwise we have to re-read the
6358d17e694SJulian Elischer 	 * media.
6360d94caffSDavid Greenman 	 */
63732ad9cb5SDoug Rabson 
6388d17e694SJulian Elischer 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
6390d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6400d94caffSDavid Greenman 			if (i != reqpage)
6410d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6420d94caffSDavid Greenman 		}
6430d94caffSDavid Greenman 		return VM_PAGER_OK;
6440d94caffSDavid Greenman 	}
6458d17e694SJulian Elischer 	m[reqpage]->valid = 0;
6460bdb7528SDavid Greenman 
6470d94caffSDavid Greenman 	/*
64826f9a767SRodney W. Grimes 	 * here on direct device I/O
64926f9a767SRodney W. Grimes 	 */
65026f9a767SRodney W. Grimes 
651efc68ce1SDavid Greenman 	firstaddr = -1;
65226f9a767SRodney W. Grimes 	/*
653efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
65426f9a767SRodney W. Grimes 	 */
655efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
656a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
657a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
658efc68ce1SDavid Greenman 		if (firstaddr == -1) {
65924a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
660fc62ef1fSBruce Evans 				/* XXX no %qd in kernel. */
661fc62ef1fSBruce Evans 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx",
662fc62ef1fSBruce Evans 			   	 firstaddr, (u_long)(foff >> 32),
663fc62ef1fSBruce Evans 			   	 (u_long)(u_int32_t)foff,
664fc62ef1fSBruce Evans 				 (u_long)(u_int32_t)
665fc62ef1fSBruce Evans 				 (object->un_pager.vnp.vnp_size >> 32),
666fc62ef1fSBruce Evans 				 (u_long)(u_int32_t)
667fc62ef1fSBruce Evans 				 object->un_pager.vnp.vnp_size);
668efc68ce1SDavid Greenman 			}
66926f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
670efc68ce1SDavid Greenman 			runend = i + 1;
671efc68ce1SDavid Greenman 			first = runend;
672efc68ce1SDavid Greenman 			continue;
673efc68ce1SDavid Greenman 		}
674efc68ce1SDavid Greenman 		runend = i + runpg;
675efc68ce1SDavid Greenman 		if (runend <= reqpage) {
676efc68ce1SDavid Greenman 			int j;
677efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
678efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
679efc68ce1SDavid Greenman 			}
68026f9a767SRodney W. Grimes 		} else {
681efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
682efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
68326f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
684efc68ce1SDavid Greenman 				count = first + runpg;
68526f9a767SRodney W. Grimes 			}
686efc68ce1SDavid Greenman 			break;
68726f9a767SRodney W. Grimes 		}
688efc68ce1SDavid Greenman 		first = runend;
689efc68ce1SDavid Greenman 	}
69026f9a767SRodney W. Grimes 
69126f9a767SRodney W. Grimes 	/*
692bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
693bbc0ec52SDavid Greenman 	 * to be zero based...
69426f9a767SRodney W. Grimes 	 */
69526f9a767SRodney W. Grimes 	if (first != 0) {
69626f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
69726f9a767SRodney W. Grimes 			m[i - first] = m[i];
69826f9a767SRodney W. Grimes 		}
69926f9a767SRodney W. Grimes 		count -= first;
70026f9a767SRodney W. Grimes 		reqpage -= first;
70126f9a767SRodney W. Grimes 	}
702efc68ce1SDavid Greenman 
70326f9a767SRodney W. Grimes 	/*
70426f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
70526f9a767SRodney W. Grimes 	 */
706a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
70726f9a767SRodney W. Grimes 
70826f9a767SRodney W. Grimes 	/*
70926f9a767SRodney W. Grimes 	 * calculate the size of the transfer
71026f9a767SRodney W. Grimes 	 */
71126f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
71224a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
71324a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
71426f9a767SRodney W. Grimes 
71526f9a767SRodney W. Grimes 	/*
71624579ca1SMatthew Dillon 	 * round up physical size for real devices.
71726f9a767SRodney W. Grimes 	 */
71824579ca1SMatthew Dillon 	if (dp->v_type == VBLK || dp->v_type == VCHR) {
71924579ca1SMatthew Dillon 		int secmask = dp->v_rdev->si_bsize_phys - 1;
72024579ca1SMatthew Dillon 		KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
72124579ca1SMatthew Dillon 		size = (size + secmask) & ~secmask;
72224579ca1SMatthew Dillon 	}
72326f9a767SRodney W. Grimes 
7241c7c3c6aSMatthew Dillon 	bp = getpbuf(&vnode_pbuf_freecnt);
72516f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
72616f62314SDavid Greenman 
72726f9a767SRodney W. Grimes 	/*
72826f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
72926f9a767SRodney W. Grimes 	 */
73016f62314SDavid Greenman 	pmap_qenter(kva, m, count);
73126f9a767SRodney W. Grimes 
73226f9a767SRodney W. Grimes 	/* build a minimal buffer header */
73367812eacSKirk McKusick 	bp->b_flags = B_READ | B_CALL;
73426f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
73526f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
736b0eeea20SPoul-Henning Kamp 	bp->b_rcred = bp->b_wcred = curproc->p_ucred;
73726f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
73826f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
73926f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
74026f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
741187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7420d94caffSDavid Greenman 	pbgetvp(dp, bp);
74326f9a767SRodney W. Grimes 	bp->b_bcount = size;
74426f9a767SRodney W. Grimes 	bp->b_bufsize = size;
74526f9a767SRodney W. Grimes 
746976e77fcSDavid Greenman 	cnt.v_vnodein++;
747976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
748976e77fcSDavid Greenman 
74926f9a767SRodney W. Grimes 	/* do the input */
750fd5d1124SJulian Elischer 	VOP_STRATEGY(bp->b_vp, bp);
751976e77fcSDavid Greenman 
752e47ed70bSJohn Dyson 	s = splvm();
753e47ed70bSJohn Dyson 	/* we definitely need to be at splvm here */
75426f9a767SRodney W. Grimes 
75526f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
756aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
75726f9a767SRodney W. Grimes 	}
75826f9a767SRodney W. Grimes 	splx(s);
75926f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
76026f9a767SRodney W. Grimes 		error = EIO;
76126f9a767SRodney W. Grimes 
76226f9a767SRodney W. Grimes 	if (!error) {
76326f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
76426f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
76526f9a767SRodney W. Grimes 	}
76616f62314SDavid Greenman 	pmap_qremove(kva, count);
76726f9a767SRodney W. Grimes 
76826f9a767SRodney W. Grimes 	/*
76926f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
77026f9a767SRodney W. Grimes 	 */
7711c7c3c6aSMatthew Dillon 	relpbuf(bp, &vnode_pbuf_freecnt);
77226f9a767SRodney W. Grimes 
7738f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
7748f9110f6SJohn Dyson 		vm_page_t mt;
7758f9110f6SJohn Dyson 
7768f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
7778f9110f6SJohn Dyson 		mt = m[i];
7788f9110f6SJohn Dyson 
77954746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
7808d17e694SJulian Elischer 			/*
7818d17e694SJulian Elischer 			 * Read filled up entire page.
7828d17e694SJulian Elischer 			 */
7838f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
7842c28a105SAlan Cox 			vm_page_undirty(mt);	/* should be an assert? XXX */
7858f9110f6SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
7868f9110f6SJohn Dyson 		} else {
7878d17e694SJulian Elischer 			/*
7888d17e694SJulian Elischer 			 * Read did not fill up entire page.  Since this
7898d17e694SJulian Elischer 			 * is getpages, the page may be mapped, so we have
7908d17e694SJulian Elischer 			 * to zero the invalid portions of the page even
7918d17e694SJulian Elischer 			 * though we aren't setting them valid.
7928d17e694SJulian Elischer 			 *
7938d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
7948d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
7958d17e694SJulian Elischer 			 * read.
7968d17e694SJulian Elischer 			 */
79754746b67SDmitrij Tejblum 			vm_page_set_validclean(mt, 0,
79854746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
7994221e284SAlan Cox 			/* handled by vm_fault now */
8004221e284SAlan Cox 			/* vm_page_zero_invalid(mt, FALSE); */
8018f9110f6SJohn Dyson 		}
8028f9110f6SJohn Dyson 
803e69763a3SDoug Rabson 		vm_page_flag_clear(mt, PG_ZERO);
80426f9a767SRodney W. Grimes 		if (i != reqpage) {
805bbc0ec52SDavid Greenman 
80626f9a767SRodney W. Grimes 			/*
807bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
808bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
809bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
810bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
811bbc0ec52SDavid Greenman 			 * deactivating pages is best.
81226f9a767SRodney W. Grimes 			 */
813bbc0ec52SDavid Greenman 
81426f9a767SRodney W. Grimes 			/*
815bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
816bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
81726f9a767SRodney W. Grimes 			 */
81826f9a767SRodney W. Grimes 			if (!error) {
8198f9110f6SJohn Dyson 				if (mt->flags & PG_WANTED)
8208f9110f6SJohn Dyson 					vm_page_activate(mt);
82195461b45SJohn Dyson 				else
8228f9110f6SJohn Dyson 					vm_page_deactivate(mt);
823e69763a3SDoug Rabson 				vm_page_wakeup(mt);
82426f9a767SRodney W. Grimes 			} else {
8258f9110f6SJohn Dyson 				vnode_pager_freepage(mt);
82626f9a767SRodney W. Grimes 			}
82726f9a767SRodney W. Grimes 		}
82826f9a767SRodney W. Grimes 	}
82926f9a767SRodney W. Grimes 	if (error) {
83024a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
83126f9a767SRodney W. Grimes 	}
832a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
83326f9a767SRodney W. Grimes }
83426f9a767SRodney W. Grimes 
835ce75f2c3SMike Smith /*
836ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
837ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
838ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
839ce75f2c3SMike Smith  *
840ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
841ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
842ce75f2c3SMike Smith  */
843e4542174SMatthew Dillon static void
844170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
845170db9c6SJohn Dyson 	vm_object_t object;
846170db9c6SJohn Dyson 	vm_page_t *m;
847170db9c6SJohn Dyson 	int count;
848170db9c6SJohn Dyson 	boolean_t sync;
849170db9c6SJohn Dyson 	int *rtvals;
850170db9c6SJohn Dyson {
851170db9c6SJohn Dyson 	int rtval;
852170db9c6SJohn Dyson 	struct vnode *vp;
85386ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
854ad980522SJohn Dyson 
8550e3cdf2cSAlan Cox 	/*
8560e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
8570e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
8580e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
8590e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
8600e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
8610e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
8620e3cdf2cSAlan Cox 	 *
8630e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
8640e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
8650e3cdf2cSAlan Cox 	 */
8660e3cdf2cSAlan Cox 
8670e3cdf2cSAlan Cox 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
8680e3cdf2cSAlan Cox 		sync |= OBJPC_SYNC;
8690e3cdf2cSAlan Cox 
8700e3cdf2cSAlan Cox 	/*
8710e3cdf2cSAlan Cox 	 * Call device-specific putpages function
8720e3cdf2cSAlan Cox 	 */
8730e3cdf2cSAlan Cox 
874170db9c6SJohn Dyson 	vp = object->handle;
87586ffbd76SMike Smith 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
87686ffbd76SMike Smith 	if (rtval == EOPNOTSUPP) {
87786ffbd76SMike Smith 	    printf("vnode_pager: *** WARNING *** stale FS putpages\n");
87886ffbd76SMike Smith 	    rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals);
87986ffbd76SMike Smith 	}
880170db9c6SJohn Dyson }
881170db9c6SJohn Dyson 
882ce75f2c3SMike Smith 
88326f9a767SRodney W. Grimes /*
884ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
8854491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
88626f9a767SRodney W. Grimes  */
887ce75f2c3SMike Smith int
8888f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
889ce75f2c3SMike Smith 	struct vnode *vp;
89026f9a767SRodney W. Grimes 	vm_page_t *m;
891ce75f2c3SMike Smith 	int bytecount;
8928f9110f6SJohn Dyson 	int flags;
89326f9a767SRodney W. Grimes 	int *rtvals;
89426f9a767SRodney W. Grimes {
895f6b04d2bSDavid Greenman 	int i;
896ce75f2c3SMike Smith 	vm_object_t object;
897ce75f2c3SMike Smith 	int count;
89826f9a767SRodney W. Grimes 
899f6b04d2bSDavid Greenman 	int maxsize, ncount;
900a316d390SJohn Dyson 	vm_ooffset_t poffset;
901f6b04d2bSDavid Greenman 	struct uio auio;
902f6b04d2bSDavid Greenman 	struct iovec aiov;
903f6b04d2bSDavid Greenman 	int error;
9048f9110f6SJohn Dyson 	int ioflags;
90526f9a767SRodney W. Grimes 
906ce75f2c3SMike Smith 	object = vp->v_object;
907ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
908ce75f2c3SMike Smith 
90926f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
91026f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
91126f9a767SRodney W. Grimes 
912a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
9133efc015bSPeter Wemm 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n",
9143efc015bSPeter Wemm 			(long)m[0]->pindex, m[0]->dirty);
915f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
916f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
9170d94caffSDavid Greenman 	}
9180bdb7528SDavid Greenman 
919f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
920f6b04d2bSDavid Greenman 	ncount = count;
92126f9a767SRodney W. Grimes 
922a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
923a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
924a316d390SJohn Dyson 		if (object->un_pager.vnp.vnp_size > poffset)
925a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
9265f55e841SDavid Greenman 		else
9275f55e841SDavid Greenman 			maxsize = 0;
928aa8de40aSPoul-Henning Kamp 		ncount = btoc(maxsize);
929f6b04d2bSDavid Greenman 		if (ncount < count) {
930f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
931f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
932f6b04d2bSDavid Greenman 			}
933f6b04d2bSDavid Greenman 		}
934f6b04d2bSDavid Greenman 	}
93526f9a767SRodney W. Grimes 
9368f9110f6SJohn Dyson 	ioflags = IO_VMIO;
9378f9110f6SJohn Dyson 	ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: 0;
9388f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
939f6b04d2bSDavid Greenman 
940f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
941f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
942f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
943f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
944a316d390SJohn Dyson 	auio.uio_offset = poffset;
945f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
946f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
947f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
948f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
9498f9110f6SJohn Dyson 	error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred);
950976e77fcSDavid Greenman 	cnt.v_vnodeout++;
951f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
952f6b04d2bSDavid Greenman 
953f6b04d2bSDavid Greenman 	if (error) {
95424a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
955f6b04d2bSDavid Greenman 	}
956f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
957ac1e407bSBruce Evans 		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
958ac1e407bSBruce Evans 		    auio.uio_resid, (u_long)m[0]->pindex);
95926f9a767SRodney W. Grimes 	}
960ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
96126f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
96226f9a767SRodney W. Grimes 	}
963f6b04d2bSDavid Greenman 	return rtvals[0];
96426f9a767SRodney W. Grimes }
965f6b04d2bSDavid Greenman 
966f6b04d2bSDavid Greenman struct vnode *
96724a1cce3SDavid Greenman vnode_pager_lock(object)
96824a1cce3SDavid Greenman 	vm_object_t object;
96924a1cce3SDavid Greenman {
970996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
971996c772fSJohn Dyson 
97224a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
97324a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
974f6b04d2bSDavid Greenman 			continue;
97547221757SJohn Dyson 		if (object->flags & OBJ_DEAD)
97647221757SJohn Dyson 			return NULL;
977f6b04d2bSDavid Greenman 
97847221757SJohn Dyson 		while (vget(object->handle,
97947221757SJohn Dyson 			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) {
980bef608bdSJohn Dyson 			if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE))
981bef608bdSJohn Dyson 				return NULL;
98247221757SJohn Dyson 			printf("vnode_pager_lock: retrying\n");
98347221757SJohn Dyson 		}
98424a1cce3SDavid Greenman 		return object->handle;
98526f9a767SRodney W. Grimes 	}
98624a1cce3SDavid Greenman 	return NULL;
987f6b04d2bSDavid Greenman }
988