xref: /freebsd/sys/vm/vnode_pager.c (revision d63596ce9e18d68c419602064972fb8e625d8410)
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
41d63596ceSJohn Dyson  *	$Id: vnode_pager.c,v 1.56 1995/12/14 09:55:14 phk Exp $
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes /*
45df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
46df8bae1dSRodney W. Grimes  */
47df8bae1dSRodney W. Grimes 
4826f9a767SRodney W. Grimes /*
4926f9a767SRodney W. Grimes  * TODO:
5024a1cce3SDavid Greenman  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
51f6b04d2bSDavid Greenman  *	greatly re-simplify the vnode_pager.
5226f9a767SRodney W. Grimes  */
5326f9a767SRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <sys/param.h>
55df8bae1dSRodney W. Grimes #include <sys/systm.h>
560d94caffSDavid Greenman #include <sys/kernel.h>
57df8bae1dSRodney W. Grimes #include <sys/proc.h>
58df8bae1dSRodney W. Grimes #include <sys/malloc.h>
59df8bae1dSRodney W. Grimes #include <sys/vnode.h>
60df8bae1dSRodney W. Grimes #include <sys/uio.h>
61df8bae1dSRodney W. Grimes #include <sys/mount.h>
6224a1cce3SDavid Greenman #include <sys/buf.h>
63efeaf95aSDavid Greenman #include <sys/vmmeter.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <vm/vm.h>
66efeaf95aSDavid Greenman #include <vm/vm_param.h>
67efeaf95aSDavid Greenman #include <vm/vm_prot.h>
68efeaf95aSDavid Greenman #include <vm/vm_object.h>
69df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7024a1cce3SDavid Greenman #include <vm/vm_pager.h>
71df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
72efeaf95aSDavid Greenman #include <vm/vm_extern.h>
73df8bae1dSRodney W. Grimes 
74f708ef1bSPoul-Henning Kamp static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
750b8253a7SBruce Evans 					 int *run));
76f708ef1bSPoul-Henning Kamp static void vnode_pager_iodone __P((struct buf *bp));
77f708ef1bSPoul-Henning Kamp static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
78f708ef1bSPoul-Henning Kamp static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
79f708ef1bSPoul-Henning Kamp static void vnode_pager_dealloc __P((vm_object_t));
80f708ef1bSPoul-Henning Kamp static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
81f708ef1bSPoul-Henning Kamp static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
82f708ef1bSPoul-Henning Kamp static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
830b8253a7SBruce Evans 
84df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
8524a1cce3SDavid Greenman 	NULL,
86df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
87df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
8824a1cce3SDavid Greenman 	vnode_pager_getpages,
8924a1cce3SDavid Greenman 	vnode_pager_putpages,
9024a1cce3SDavid Greenman 	vnode_pager_haspage,
9124a1cce3SDavid Greenman 	NULL
92df8bae1dSRodney W. Grimes };
93df8bae1dSRodney W. Grimes 
940b8253a7SBruce Evans static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m,
950b8253a7SBruce Evans 					  int count, int reqpage));
960b8253a7SBruce Evans static int vnode_pager_leaf_putpages __P((vm_object_t object, vm_page_t *m,
970b8253a7SBruce Evans 					  int count, boolean_t sync,
980b8253a7SBruce Evans 					  int *rtvals));
99170db9c6SJohn Dyson 
100df8bae1dSRodney W. Grimes /*
101df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
102df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
103df8bae1dSRodney W. Grimes  */
10424a1cce3SDavid Greenman vm_object_t
10526f9a767SRodney W. Grimes vnode_pager_alloc(handle, size, prot, offset)
106ee3a64c9SDavid Greenman 	void *handle;
107df8bae1dSRodney W. Grimes 	vm_size_t size;
108df8bae1dSRodney W. Grimes 	vm_prot_t prot;
109a316d390SJohn Dyson 	vm_ooffset_t offset;
110df8bae1dSRodney W. Grimes {
11106cb7259SDavid Greenman 	vm_object_t object;
112df8bae1dSRodney W. Grimes 	struct vnode *vp;
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes 	/*
115df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
116df8bae1dSRodney W. Grimes 	 */
117df8bae1dSRodney W. Grimes 	if (handle == NULL)
118df8bae1dSRodney W. Grimes 		return (NULL);
119df8bae1dSRodney W. Grimes 
120df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
12139d38f93SDavid Greenman 
12239d38f93SDavid Greenman 	/*
12339d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
12439d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
12539d38f93SDavid Greenman 	 */
12639d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
12739d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
12839d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
12939d38f93SDavid Greenman 	}
13039d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
13139d38f93SDavid Greenman 
13239d38f93SDavid Greenman 	/*
13339d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
13439d38f93SDavid Greenman 	 * go away.
13539d38f93SDavid Greenman 	 */
13624a1cce3SDavid Greenman 	while (((object = vp->v_object) != NULL) && (object->flags & OBJ_DEAD)) {
137aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
13824a1cce3SDavid Greenman 	}
1390d94caffSDavid Greenman 
14024a1cce3SDavid Greenman 	if (object == NULL) {
141df8bae1dSRodney W. Grimes 		/*
142df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
143df8bae1dSRodney W. Grimes 		 */
144a316d390SJohn Dyson 		object = vm_object_allocate(OBJT_VNODE, size);
1454bb62461SDavid Greenman 		object->flags = OBJ_CANPERSIST;
146bbc0ec52SDavid Greenman 
147df8bae1dSRodney W. Grimes 		/*
14824a1cce3SDavid Greenman 		 * Hold a reference to the vnode and initialize object data.
149df8bae1dSRodney W. Grimes 		 */
150df8bae1dSRodney W. Grimes 		VREF(vp);
151a316d390SJohn Dyson 		object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE;
15226f9a767SRodney W. Grimes 
15324a1cce3SDavid Greenman 		object->handle = handle;
15424a1cce3SDavid Greenman 		vp->v_object = object;
155df8bae1dSRodney W. Grimes 	} else {
156df8bae1dSRodney W. Grimes 		/*
15724a1cce3SDavid Greenman 		 * vm_object_reference() will remove the object from the cache if
15824a1cce3SDavid Greenman 		 * found and gain a reference to the object.
159df8bae1dSRodney W. Grimes 		 */
16024a1cce3SDavid Greenman 		vm_object_reference(object);
161df8bae1dSRodney W. Grimes 	}
16239d38f93SDavid Greenman 
163f6b04d2bSDavid Greenman 	if (vp->v_type == VREG)
164f6b04d2bSDavid Greenman 		vp->v_flag |= VVMIO;
16539d38f93SDavid Greenman 
16639d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
16739d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
16839d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
16939d38f93SDavid Greenman 		wakeup(vp);
17039d38f93SDavid Greenman 	}
17124a1cce3SDavid Greenman 	return (object);
172df8bae1dSRodney W. Grimes }
173df8bae1dSRodney W. Grimes 
174f708ef1bSPoul-Henning Kamp static void
17524a1cce3SDavid Greenman vnode_pager_dealloc(object)
1760d94caffSDavid Greenman 	vm_object_t object;
17724a1cce3SDavid Greenman {
17824a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
179df8bae1dSRodney W. Grimes 
18024a1cce3SDavid Greenman 	if (vp == NULL)
18124a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
18224a1cce3SDavid Greenman 
18324a1cce3SDavid Greenman 	if (object->paging_in_progress) {
1840d94caffSDavid Greenman 		int s = splbio();
1850d94caffSDavid Greenman 		while (object->paging_in_progress) {
186c0503609SDavid Greenman 			object->flags |= OBJ_PIPWNT;
1870d94caffSDavid Greenman 			tsleep(object, PVM, "vnpdea", 0);
1880d94caffSDavid Greenman 		}
1890d94caffSDavid Greenman 		splx(s);
19024a1cce3SDavid Greenman 	}
19124a1cce3SDavid Greenman 
19224a1cce3SDavid Greenman 	object->handle = NULL;
1930d94caffSDavid Greenman 
194aa2cabb9SDavid Greenman 	vp->v_object = NULL;
1958e58bf68SDavid Greenman 	vp->v_flag &= ~(VTEXT | VVMIO);
19600072442SDavid Greenman 	vp->v_flag |= VAGE;
197df8bae1dSRodney W. Grimes 	vrele(vp);
198df8bae1dSRodney W. Grimes }
19926f9a767SRodney W. Grimes 
200f708ef1bSPoul-Henning Kamp static boolean_t
201a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
20224a1cce3SDavid Greenman 	vm_object_t object;
203a316d390SJohn Dyson 	vm_pindex_t pindex;
20424a1cce3SDavid Greenman 	int *before;
20524a1cce3SDavid Greenman 	int *after;
206df8bae1dSRodney W. Grimes {
20724a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
208df8bae1dSRodney W. Grimes 	daddr_t bn;
2093af76890SPoul-Henning Kamp 	int err;
210170db9c6SJohn Dyson 	daddr_t reqblock;
2112c4488fcSJohn Dyson 	int poff;
2122c4488fcSJohn Dyson 	int bsize;
213d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
214df8bae1dSRodney W. Grimes 
215df8bae1dSRodney W. Grimes 	/*
2160d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
2170d94caffSDavid Greenman 	 * not have the page.
218df8bae1dSRodney W. Grimes 	 */
219a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
220a316d390SJohn Dyson 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2214abc71c0SDavid Greenman 		return FALSE;
222df8bae1dSRodney W. Grimes 
223eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
224170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
225d63596ceSJohn Dyson 	blocksperpage = 0;
226d63596ceSJohn Dyson 	if (pagesperblock > 0) {
227a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
228d63596ceSJohn Dyson 	} else {
229d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
230d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
231d63596ceSJohn Dyson 	}
232170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
233170db9c6SJohn Dyson 		after, before);
2340d94caffSDavid Greenman 	if (err)
23524a1cce3SDavid Greenman 		return TRUE;
2366eab77f2SJohn Dyson 	if ( bn == -1)
237ced399eeSJohn Dyson 		return FALSE;
238d63596ceSJohn Dyson 	if (pagesperblock > 0) {
239a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
240170db9c6SJohn Dyson 		if (before) {
241170db9c6SJohn Dyson 			*before *= pagesperblock;
242170db9c6SJohn Dyson 			*before += poff;
243170db9c6SJohn Dyson 		}
244170db9c6SJohn Dyson 		if (after) {
245b1fc01b7SJohn Dyson 			int numafter;
246170db9c6SJohn Dyson 			*after *= pagesperblock;
247b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
248a316d390SJohn Dyson 			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
249a316d390SJohn Dyson 				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
250b1fc01b7SJohn Dyson 			}
251b1fc01b7SJohn Dyson 			*after += numafter;
252170db9c6SJohn Dyson 		}
253d63596ceSJohn Dyson 	} else {
254d63596ceSJohn Dyson 		if (before) {
255d63596ceSJohn Dyson 			*before /= blocksperpage;
256d63596ceSJohn Dyson 		}
257d63596ceSJohn Dyson 
258d63596ceSJohn Dyson 		if (after) {
259d63596ceSJohn Dyson 			*after /= blocksperpage;
260d63596ceSJohn Dyson 		}
261d63596ceSJohn Dyson 	}
262ced399eeSJohn Dyson 	return TRUE;
263df8bae1dSRodney W. Grimes }
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes /*
266df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
26724a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
268df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
269df8bae1dSRodney W. Grimes  *
270df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
271df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
272df8bae1dSRodney W. Grimes  */
273df8bae1dSRodney W. Grimes void
274df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
275df8bae1dSRodney W. Grimes 	struct vnode *vp;
276a316d390SJohn Dyson 	vm_ooffset_t nsize;
277df8bae1dSRodney W. Grimes {
27824a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
279df8bae1dSRodney W. Grimes 
28024a1cce3SDavid Greenman 	if (object == NULL)
281df8bae1dSRodney W. Grimes 		return;
282bbc0ec52SDavid Greenman 
283df8bae1dSRodney W. Grimes 	/*
284df8bae1dSRodney W. Grimes 	 * Hasn't changed size
285df8bae1dSRodney W. Grimes 	 */
28624a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
287df8bae1dSRodney W. Grimes 		return;
288bbc0ec52SDavid Greenman 
289df8bae1dSRodney W. Grimes 	/*
290bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
291df8bae1dSRodney W. Grimes 	 */
29224a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
293a316d390SJohn Dyson 		vm_ooffset_t nsizerounded;
294a316d390SJohn Dyson 		nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_SIZE - 1));
295a316d390SJohn Dyson 		if (nsizerounded < object->un_pager.vnp.vnp_size) {
296df8bae1dSRodney W. Grimes 			vm_object_page_remove(object,
297a316d390SJohn Dyson 				OFF_TO_IDX(nsize + PAGE_SIZE - 1),
298a316d390SJohn Dyson 				OFF_TO_IDX(object->un_pager.vnp.vnp_size),
299a316d390SJohn Dyson 				FALSE);
3000d94caffSDavid Greenman 		}
301bbc0ec52SDavid Greenman 		/*
302bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
303bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
304bbc0ec52SDavid Greenman 		 */
305bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
306bbc0ec52SDavid Greenman 			vm_offset_t kva;
307bbc0ec52SDavid Greenman 			vm_page_t m;
308bbc0ec52SDavid Greenman 
309a316d390SJohn Dyson 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
310bbc0ec52SDavid Greenman 			if (m) {
311bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
312bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
313a316d390SJohn Dyson 				    (int) (round_page(nsize) - nsize));
314bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
315bbc0ec52SDavid Greenman 			}
316bbc0ec52SDavid Greenman 		}
317bbc0ec52SDavid Greenman 	}
318a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
319a316d390SJohn Dyson 	object->size = OFF_TO_IDX(nsize + PAGE_SIZE - 1);
320df8bae1dSRodney W. Grimes }
321df8bae1dSRodney W. Grimes 
322df8bae1dSRodney W. Grimes void
323df8bae1dSRodney W. Grimes vnode_pager_umount(mp)
324df8bae1dSRodney W. Grimes 	register struct mount *mp;
325df8bae1dSRodney W. Grimes {
32624a1cce3SDavid Greenman 	struct vnode *vp, *nvp;
327df8bae1dSRodney W. Grimes 
32824a1cce3SDavid Greenman loop:
32924a1cce3SDavid Greenman 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
33024a1cce3SDavid Greenman 		/*
33124a1cce3SDavid Greenman 		 * Vnode can be reclaimed by getnewvnode() while we
33224a1cce3SDavid Greenman 		 * traverse the list.
33324a1cce3SDavid Greenman 		 */
33424a1cce3SDavid Greenman 		if (vp->v_mount != mp)
33524a1cce3SDavid Greenman 			goto loop;
33624a1cce3SDavid Greenman 
337df8bae1dSRodney W. Grimes 		/*
338bbc0ec52SDavid Greenman 		 * Save the next pointer now since uncaching may terminate the
33924a1cce3SDavid Greenman 		 * object and render vnode invalid
340df8bae1dSRodney W. Grimes 		 */
34124a1cce3SDavid Greenman 		nvp = vp->v_mntvnodes.le_next;
34224a1cce3SDavid Greenman 
34324a1cce3SDavid Greenman 		if (vp->v_object != NULL) {
344c01a9b8cSDavid Greenman 			VOP_LOCK(vp);
34524a1cce3SDavid Greenman 			vnode_pager_uncache(vp);
346c01a9b8cSDavid Greenman 			VOP_UNLOCK(vp);
347c01a9b8cSDavid Greenman 		}
348df8bae1dSRodney W. Grimes 	}
349df8bae1dSRodney W. Grimes }
350df8bae1dSRodney W. Grimes 
351df8bae1dSRodney W. Grimes /*
352df8bae1dSRodney W. Grimes  * Remove vnode associated object from the object cache.
353c01a9b8cSDavid Greenman  * This routine must be called with the vnode locked.
354df8bae1dSRodney W. Grimes  *
355c01a9b8cSDavid Greenman  * XXX unlock the vnode.
356c01a9b8cSDavid Greenman  * We must do this since uncaching the object may result in its
357c01a9b8cSDavid Greenman  * destruction which may initiate paging activity which may necessitate
358c01a9b8cSDavid Greenman  * re-locking the vnode.
35926f9a767SRodney W. Grimes  */
36024a1cce3SDavid Greenman void
36126f9a767SRodney W. Grimes vnode_pager_uncache(vp)
36224a1cce3SDavid Greenman 	struct vnode *vp;
36326f9a767SRodney W. Grimes {
36424a1cce3SDavid Greenman 	vm_object_t object;
36526f9a767SRodney W. Grimes 
36626f9a767SRodney W. Grimes 	/*
36726f9a767SRodney W. Grimes 	 * Not a mapped vnode
36826f9a767SRodney W. Grimes 	 */
369aa2cabb9SDavid Greenman 	object = vp->v_object;
3708e58bf68SDavid Greenman 	if (object == NULL)
37124a1cce3SDavid Greenman 		return;
3720d94caffSDavid Greenman 
37324a1cce3SDavid Greenman 	vm_object_reference(object);
374c01a9b8cSDavid Greenman 	VOP_UNLOCK(vp);
37526f9a767SRodney W. Grimes 	pager_cache(object, FALSE);
376c01a9b8cSDavid Greenman 	VOP_LOCK(vp);
37724a1cce3SDavid Greenman 	return;
37826f9a767SRodney W. Grimes }
379df8bae1dSRodney W. Grimes 
38026f9a767SRodney W. Grimes 
38126f9a767SRodney W. Grimes void
38226f9a767SRodney W. Grimes vnode_pager_freepage(m)
38326f9a767SRodney W. Grimes 	vm_page_t m;
384df8bae1dSRodney W. Grimes {
38526f9a767SRodney W. Grimes 	PAGE_WAKEUP(m);
38626f9a767SRodney W. Grimes 	vm_page_free(m);
38726f9a767SRodney W. Grimes }
38826f9a767SRodney W. Grimes 
38926f9a767SRodney W. Grimes /*
39026f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
39126f9a767SRodney W. Grimes  * file address
39226f9a767SRodney W. Grimes  */
393f708ef1bSPoul-Henning Kamp static vm_offset_t
394efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
39526f9a767SRodney W. Grimes 	struct vnode *vp;
396a316d390SJohn Dyson 	vm_ooffset_t address;
397efc68ce1SDavid Greenman 	int *run;
39826f9a767SRodney W. Grimes {
39926f9a767SRodney W. Grimes 	int rtaddress;
40026f9a767SRodney W. Grimes 	int bsize;
401a316d390SJohn Dyson 	daddr_t block;
40226f9a767SRodney W. Grimes 	struct vnode *rtvp;
40326f9a767SRodney W. Grimes 	int err;
404a316d390SJohn Dyson 	daddr_t vblock;
405a316d390SJohn Dyson 	int voffset;
40626f9a767SRodney W. Grimes 
4070d94caffSDavid Greenman 	if ((int) address < 0)
4080d94caffSDavid Greenman 		return -1;
4090d94caffSDavid Greenman 
4102c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4112c4488fcSJohn Dyson 		return -1;
4122c4488fcSJohn Dyson 
41326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
41426f9a767SRodney W. Grimes 	vblock = address / bsize;
41526f9a767SRodney W. Grimes 	voffset = address % bsize;
41626f9a767SRodney W. Grimes 
417c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
41826f9a767SRodney W. Grimes 
419efc68ce1SDavid Greenman 	if (err || (block == -1))
42026f9a767SRodney W. Grimes 		rtaddress = -1;
421efc68ce1SDavid Greenman 	else {
422187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
423efc68ce1SDavid Greenman 		if( run) {
424efc68ce1SDavid Greenman 			*run += 1;
425efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
426efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
427efc68ce1SDavid Greenman 		}
428efc68ce1SDavid Greenman 	}
42926f9a767SRodney W. Grimes 
43026f9a767SRodney W. Grimes 	return rtaddress;
43126f9a767SRodney W. Grimes }
43226f9a767SRodney W. Grimes 
43326f9a767SRodney W. Grimes /*
43426f9a767SRodney W. Grimes  * interrupt routine for I/O completion
43526f9a767SRodney W. Grimes  */
436f708ef1bSPoul-Henning Kamp static void
43726f9a767SRodney W. Grimes vnode_pager_iodone(bp)
43826f9a767SRodney W. Grimes 	struct buf *bp;
43926f9a767SRodney W. Grimes {
44026f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
44124a1cce3SDavid Greenman 	wakeup(bp);
44226f9a767SRodney W. Grimes }
44326f9a767SRodney W. Grimes 
44426f9a767SRodney W. Grimes /*
44526f9a767SRodney W. Grimes  * small block file system vnode pager input
44626f9a767SRodney W. Grimes  */
447f708ef1bSPoul-Henning Kamp static int
44824a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
44924a1cce3SDavid Greenman 	vm_object_t object;
45026f9a767SRodney W. Grimes 	vm_page_t m;
45126f9a767SRodney W. Grimes {
45226f9a767SRodney W. Grimes 	int i;
45326f9a767SRodney W. Grimes 	int s;
45426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
45526f9a767SRodney W. Grimes 	struct buf *bp;
45626f9a767SRodney W. Grimes 	vm_offset_t kva;
45726f9a767SRodney W. Grimes 	int fileaddr;
45826f9a767SRodney W. Grimes 	vm_offset_t bsize;
45926f9a767SRodney W. Grimes 	int error = 0;
46026f9a767SRodney W. Grimes 
46124a1cce3SDavid Greenman 	vp = object->handle;
4622c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4632c4488fcSJohn Dyson 		return VM_PAGER_BAD;
4642c4488fcSJohn Dyson 
46526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
46626f9a767SRodney W. Grimes 
4670bdb7528SDavid Greenman 
468c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
46926f9a767SRodney W. Grimes 
47026f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
47126f9a767SRodney W. Grimes 
47226f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
473bbc0ec52SDavid Greenman 
474a316d390SJohn Dyson 		if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
47526f9a767SRodney W. Grimes 			continue;
47626f9a767SRodney W. Grimes 
477a316d390SJohn Dyson 		fileaddr = vnode_pager_addr(vp,
478a316d390SJohn Dyson 			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
47926f9a767SRodney W. Grimes 		if (fileaddr != -1) {
48026f9a767SRodney W. Grimes 			bp = getpbuf();
48126f9a767SRodney W. Grimes 
48226f9a767SRodney W. Grimes 			/* build a minimal buffer header */
48326f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
48426f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
48526f9a767SRodney W. Grimes 			bp->b_proc = curproc;
48626f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
48726f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
48826f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
48926f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
49026f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
49126f9a767SRodney W. Grimes 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
492187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4930d94caffSDavid Greenman 			pbgetvp(dp, bp);
49426f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
49526f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
49626f9a767SRodney W. Grimes 
49726f9a767SRodney W. Grimes 			/* do the input */
49826f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
49926f9a767SRodney W. Grimes 
50026f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
50126f9a767SRodney W. Grimes 
50226f9a767SRodney W. Grimes 			s = splbio();
50326f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
504aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
50526f9a767SRodney W. Grimes 			}
50626f9a767SRodney W. Grimes 			splx(s);
50726f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
50826f9a767SRodney W. Grimes 				error = EIO;
50926f9a767SRodney W. Grimes 
51026f9a767SRodney W. Grimes 			/*
51126f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
51226f9a767SRodney W. Grimes 			 */
51326f9a767SRodney W. Grimes 			relpbuf(bp);
51426f9a767SRodney W. Grimes 			if (error)
51526f9a767SRodney W. Grimes 				break;
5160d94caffSDavid Greenman 
517170db9c6SJohn Dyson 			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
51826f9a767SRodney W. Grimes 		} else {
519b1fc01b7SJohn Dyson 			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
52026f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
52126f9a767SRodney W. Grimes 		}
52226f9a767SRodney W. Grimes 	}
52326f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
5240d94caffSDavid Greenman 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
525b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
52626f9a767SRodney W. Grimes 	if (error) {
527a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
52826f9a767SRodney W. Grimes 	}
52926f9a767SRodney W. Grimes 	return VM_PAGER_OK;
53026f9a767SRodney W. Grimes 
53126f9a767SRodney W. Grimes }
53226f9a767SRodney W. Grimes 
53326f9a767SRodney W. Grimes 
53426f9a767SRodney W. Grimes /*
53526f9a767SRodney W. Grimes  * old style vnode pager output routine
53626f9a767SRodney W. Grimes  */
537f708ef1bSPoul-Henning Kamp static int
53824a1cce3SDavid Greenman vnode_pager_input_old(object, m)
53924a1cce3SDavid Greenman 	vm_object_t object;
54026f9a767SRodney W. Grimes 	vm_page_t m;
54126f9a767SRodney W. Grimes {
542df8bae1dSRodney W. Grimes 	struct uio auio;
543df8bae1dSRodney W. Grimes 	struct iovec aiov;
54426f9a767SRodney W. Grimes 	int error;
54526f9a767SRodney W. Grimes 	int size;
54626f9a767SRodney W. Grimes 	vm_offset_t kva;
547df8bae1dSRodney W. Grimes 
54826f9a767SRodney W. Grimes 	error = 0;
549bbc0ec52SDavid Greenman 
550df8bae1dSRodney W. Grimes 	/*
55126f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
55226f9a767SRodney W. Grimes 	 */
553a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
55426f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
55526f9a767SRodney W. Grimes 	} else {
55626f9a767SRodney W. Grimes 		size = PAGE_SIZE;
557a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
558a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
5590bdb7528SDavid Greenman 
56026f9a767SRodney W. Grimes 		/*
561df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
562df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
563df8bae1dSRodney W. Grimes 		 */
56426f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
5650bdb7528SDavid Greenman 
566df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
567df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
568df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
569df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
570a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
571df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
57226f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
573df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
574df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
57526f9a767SRodney W. Grimes 
57624a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
577df8bae1dSRodney W. Grimes 		if (!error) {
578df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes 			if (count == 0)
581df8bae1dSRodney W. Grimes 				error = EINVAL;
58226f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
58326f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
584df8bae1dSRodney W. Grimes 		}
58526f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
586df8bae1dSRodney W. Grimes 	}
58726f9a767SRodney W. Grimes 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5880d94caffSDavid Greenman 	m->dirty = 0;
589b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
590a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
59126f9a767SRodney W. Grimes }
59226f9a767SRodney W. Grimes 
59326f9a767SRodney W. Grimes /*
59426f9a767SRodney W. Grimes  * generic vnode pager input routine
59526f9a767SRodney W. Grimes  */
596170db9c6SJohn Dyson 
597f708ef1bSPoul-Henning Kamp static int
59824a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
59926f9a767SRodney W. Grimes 	vm_object_t object;
60024a1cce3SDavid Greenman 	vm_page_t *m;
60124a1cce3SDavid Greenman 	int count;
60224a1cce3SDavid Greenman 	int reqpage;
60324a1cce3SDavid Greenman {
604170db9c6SJohn Dyson 	int rtval;
605170db9c6SJohn Dyson 	struct vnode *vp;
606170db9c6SJohn Dyson 	vp = object->handle;
6072c4488fcSJohn Dyson 	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
608170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
6090b8253a7SBruce Evans 		return vnode_pager_leaf_getpages(object, m, count, reqpage);
610170db9c6SJohn Dyson 	else
611170db9c6SJohn Dyson 		return rtval;
612170db9c6SJohn Dyson }
613170db9c6SJohn Dyson 
614170db9c6SJohn Dyson static int
615170db9c6SJohn Dyson vnode_pager_leaf_getpages(object, m, count, reqpage)
616170db9c6SJohn Dyson 	vm_object_t object;
617170db9c6SJohn Dyson 	vm_page_t *m;
618170db9c6SJohn Dyson 	int count;
619170db9c6SJohn Dyson 	int reqpage;
620170db9c6SJohn Dyson {
621a316d390SJohn Dyson 	vm_offset_t kva;
622a316d390SJohn Dyson 	off_t foff;
62324a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
62426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
625efc68ce1SDavid Greenman 	int runpg;
626efc68ce1SDavid Greenman 	int runend;
6270bdb7528SDavid Greenman 	struct buf *bp;
62826f9a767SRodney W. Grimes 	int s;
62926f9a767SRodney W. Grimes 	int error = 0;
63026f9a767SRodney W. Grimes 
63124a1cce3SDavid Greenman 	vp = object->handle;
6322c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
6332c4488fcSJohn Dyson 		return VM_PAGER_BAD;
6342c4488fcSJohn Dyson 
63526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
63626f9a767SRodney W. Grimes 
63726f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
638bbc0ec52SDavid Greenman 
63926f9a767SRodney W. Grimes 	/*
640bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
641bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
64226f9a767SRodney W. Grimes 	 */
643a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
644bbc0ec52SDavid Greenman 
64526f9a767SRodney W. Grimes 	/*
64616f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
64726f9a767SRodney W. Grimes 	 */
648c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
64926f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
65026f9a767SRodney W. Grimes 			if (i != reqpage) {
65126f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
65226f9a767SRodney W. Grimes 			}
65326f9a767SRodney W. Grimes 		}
654976e77fcSDavid Greenman 		cnt.v_vnodein++;
655976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
65624a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
657bbc0ec52SDavid Greenman 
65826f9a767SRodney W. Grimes 		/*
65926f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
66026f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
66126f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
66226f9a767SRodney W. Grimes 		 */
66326f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
66426f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
66526f9a767SRodney W. Grimes 
66626f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
66726f9a767SRodney W. Grimes 			if (i != reqpage) {
66826f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
66926f9a767SRodney W. Grimes 			}
67026f9a767SRodney W. Grimes 		}
671976e77fcSDavid Greenman 		cnt.v_vnodein++;
672976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
67324a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
67426f9a767SRodney W. Grimes 	}
67526f9a767SRodney W. Grimes 	/*
6760d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
6770d94caffSDavid Greenman 	 * then, the entire page is valid --
6780d94caffSDavid Greenman 	 */
6790d94caffSDavid Greenman 	if (m[reqpage]->valid) {
6800d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
6810d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6820d94caffSDavid Greenman 			if (i != reqpage)
6830d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6840d94caffSDavid Greenman 		}
6850d94caffSDavid Greenman 		return VM_PAGER_OK;
6860d94caffSDavid Greenman 	}
6870bdb7528SDavid Greenman 
6880d94caffSDavid Greenman 	/*
68926f9a767SRodney W. Grimes 	 * here on direct device I/O
69026f9a767SRodney W. Grimes 	 */
69126f9a767SRodney W. Grimes 
692efc68ce1SDavid Greenman 	firstaddr = -1;
69326f9a767SRodney W. Grimes 	/*
694efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
69526f9a767SRodney W. Grimes 	 */
696efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
697a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
698a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
699efc68ce1SDavid Greenman 		if (firstaddr == -1) {
70024a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
70124a1cce3SDavid Greenman 				panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
70224a1cce3SDavid Greenman 			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
703efc68ce1SDavid Greenman 			}
70426f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
705efc68ce1SDavid Greenman 			runend = i + 1;
706efc68ce1SDavid Greenman 			first = runend;
707efc68ce1SDavid Greenman 			continue;
708efc68ce1SDavid Greenman 		}
709efc68ce1SDavid Greenman 		runend = i + runpg;
710efc68ce1SDavid Greenman 		if (runend <= reqpage) {
711efc68ce1SDavid Greenman 			int j;
712efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
713efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
714efc68ce1SDavid Greenman 			}
71526f9a767SRodney W. Grimes 		} else {
716efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
717efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
71826f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
719efc68ce1SDavid Greenman 				count = first + runpg;
72026f9a767SRodney W. Grimes 			}
721efc68ce1SDavid Greenman 			break;
72226f9a767SRodney W. Grimes 		}
723efc68ce1SDavid Greenman 		first = runend;
724efc68ce1SDavid Greenman 	}
72526f9a767SRodney W. Grimes 
72626f9a767SRodney W. Grimes 	/*
727bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
728bbc0ec52SDavid Greenman 	 * to be zero based...
72926f9a767SRodney W. Grimes 	 */
73026f9a767SRodney W. Grimes 	if (first != 0) {
73126f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
73226f9a767SRodney W. Grimes 			m[i - first] = m[i];
73326f9a767SRodney W. Grimes 		}
73426f9a767SRodney W. Grimes 		count -= first;
73526f9a767SRodney W. Grimes 		reqpage -= first;
73626f9a767SRodney W. Grimes 	}
737efc68ce1SDavid Greenman 
73826f9a767SRodney W. Grimes 	/*
73926f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
74026f9a767SRodney W. Grimes 	 */
741a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
74226f9a767SRodney W. Grimes 
74326f9a767SRodney W. Grimes 	/*
74426f9a767SRodney W. Grimes 	 * calculate the size of the transfer
74526f9a767SRodney W. Grimes 	 */
74626f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
74724a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
74824a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
74926f9a767SRodney W. Grimes 
75026f9a767SRodney W. Grimes 	/*
75126f9a767SRodney W. Grimes 	 * round up physical size for real devices
75226f9a767SRodney W. Grimes 	 */
75326f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
75426f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
75526f9a767SRodney W. Grimes 
7566d40c3d3SDavid Greenman 	bp = getpbuf();
75716f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
75816f62314SDavid Greenman 
75926f9a767SRodney W. Grimes 	/*
76026f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
76126f9a767SRodney W. Grimes 	 */
76216f62314SDavid Greenman 	pmap_qenter(kva, m, count);
76326f9a767SRodney W. Grimes 
76426f9a767SRodney W. Grimes 	/* build a minimal buffer header */
76526f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
76626f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
76726f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
76826f9a767SRodney W. Grimes 	bp->b_proc = curproc;
76926f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
77026f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
77126f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
77226f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
77326f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
774187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7750d94caffSDavid Greenman 	pbgetvp(dp, bp);
77626f9a767SRodney W. Grimes 	bp->b_bcount = size;
77726f9a767SRodney W. Grimes 	bp->b_bufsize = size;
77826f9a767SRodney W. Grimes 
779976e77fcSDavid Greenman 	cnt.v_vnodein++;
780976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
781976e77fcSDavid Greenman 
78226f9a767SRodney W. Grimes 	/* do the input */
78326f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
784976e77fcSDavid Greenman 
78526f9a767SRodney W. Grimes 	s = splbio();
78626f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
78726f9a767SRodney W. Grimes 
78826f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
789aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
79026f9a767SRodney W. Grimes 	}
79126f9a767SRodney W. Grimes 	splx(s);
79226f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
79326f9a767SRodney W. Grimes 		error = EIO;
79426f9a767SRodney W. Grimes 
79526f9a767SRodney W. Grimes 	if (!error) {
79626f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
79726f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
79826f9a767SRodney W. Grimes 	}
79916f62314SDavid Greenman 	pmap_qremove(kva, count);
80026f9a767SRodney W. Grimes 
80126f9a767SRodney W. Grimes 	/*
80226f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
80326f9a767SRodney W. Grimes 	 */
80426f9a767SRodney W. Grimes 	relpbuf(bp);
80526f9a767SRodney W. Grimes 
80626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
807fff93ab6SDavid Greenman 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
8080d94caffSDavid Greenman 		m[i]->dirty = 0;
8090d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
810b1fc01b7SJohn Dyson 		m[i]->flags &= ~PG_ZERO;
81126f9a767SRodney W. Grimes 		if (i != reqpage) {
812bbc0ec52SDavid Greenman 
81326f9a767SRodney W. Grimes 			/*
814bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
815bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
816bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
817bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
818bbc0ec52SDavid Greenman 			 * deactivating pages is best.
81926f9a767SRodney W. Grimes 			 */
820bbc0ec52SDavid Greenman 
82126f9a767SRodney W. Grimes 			/*
822bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
823bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
82426f9a767SRodney W. Grimes 			 */
82526f9a767SRodney W. Grimes 			if (!error) {
82626f9a767SRodney W. Grimes 				vm_page_deactivate(m[i]);
82726f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
82826f9a767SRodney W. Grimes 			} else {
82926f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
83026f9a767SRodney W. Grimes 			}
83126f9a767SRodney W. Grimes 		}
83226f9a767SRodney W. Grimes 	}
83326f9a767SRodney W. Grimes 	if (error) {
83424a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
83526f9a767SRodney W. Grimes 	}
836a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
83726f9a767SRodney W. Grimes }
83826f9a767SRodney W. Grimes 
839f708ef1bSPoul-Henning Kamp static int
840170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
841170db9c6SJohn Dyson 	vm_object_t object;
842170db9c6SJohn Dyson 	vm_page_t *m;
843170db9c6SJohn Dyson 	int count;
844170db9c6SJohn Dyson 	boolean_t sync;
845170db9c6SJohn Dyson 	int *rtvals;
846170db9c6SJohn Dyson {
847170db9c6SJohn Dyson 	int rtval;
848170db9c6SJohn Dyson 	struct vnode *vp;
849170db9c6SJohn Dyson 	vp = object->handle;
8502c4488fcSJohn Dyson 	rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
851170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
8520b8253a7SBruce Evans 		return vnode_pager_leaf_putpages(object, m, count, sync, rtvals);
853170db9c6SJohn Dyson 	else
854170db9c6SJohn Dyson 		return rtval;
855170db9c6SJohn Dyson }
856170db9c6SJohn Dyson 
85726f9a767SRodney W. Grimes /*
85826f9a767SRodney W. Grimes  * generic vnode pager output routine
85926f9a767SRodney W. Grimes  */
860170db9c6SJohn Dyson static int
861170db9c6SJohn Dyson vnode_pager_leaf_putpages(object, m, count, sync, rtvals)
86224a1cce3SDavid Greenman 	vm_object_t object;
86326f9a767SRodney W. Grimes 	vm_page_t *m;
86426f9a767SRodney W. Grimes 	int count;
86524a1cce3SDavid Greenman 	boolean_t sync;
86626f9a767SRodney W. Grimes 	int *rtvals;
86726f9a767SRodney W. Grimes {
868f6b04d2bSDavid Greenman 	int i;
86926f9a767SRodney W. Grimes 
870f6b04d2bSDavid Greenman 	struct vnode *vp;
871f6b04d2bSDavid Greenman 	int maxsize, ncount;
872a316d390SJohn Dyson 	vm_ooffset_t poffset;
873f6b04d2bSDavid Greenman 	struct uio auio;
874f6b04d2bSDavid Greenman 	struct iovec aiov;
875f6b04d2bSDavid Greenman 	int error;
87626f9a767SRodney W. Grimes 
87724a1cce3SDavid Greenman 	vp = object->handle;;
87826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
87926f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
88026f9a767SRodney W. Grimes 
881a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
882a316d390SJohn Dyson 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty);
883f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
884f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
8850d94caffSDavid Greenman 	}
8860bdb7528SDavid Greenman 
887f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
888f6b04d2bSDavid Greenman 	ncount = count;
88926f9a767SRodney W. Grimes 
890a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
891a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
892a316d390SJohn Dyson 		if (object->un_pager.vnp.vnp_size > poffset)
893a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
8945f55e841SDavid Greenman 		else
8955f55e841SDavid Greenman 			maxsize = 0;
896f6b04d2bSDavid Greenman 		ncount = (maxsize + PAGE_SIZE - 1) / PAGE_SIZE;
897f6b04d2bSDavid Greenman 		if (ncount < count) {
898f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
899f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
900f6b04d2bSDavid Greenman 			}
901a316d390SJohn Dyson #ifdef BOGUS
902f6b04d2bSDavid Greenman 			if (ncount == 0) {
903a316d390SJohn Dyson 				printf("vnode_pager_putpages: write past end of file: %d, %lu\n",
904a316d390SJohn Dyson 					poffset,
905a316d390SJohn Dyson 					(unsigned long) object->un_pager.vnp.vnp_size);
90626f9a767SRodney W. Grimes 				return rtvals[0];
90726f9a767SRodney W. Grimes 			}
908a316d390SJohn Dyson #endif
909f6b04d2bSDavid Greenman 		}
910f6b04d2bSDavid Greenman 	}
91126f9a767SRodney W. Grimes 
91226f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
9135f55e841SDavid Greenman 		m[i]->busy++;
914f6b04d2bSDavid Greenman 		m[i]->flags &= ~PG_BUSY;
91526f9a767SRodney W. Grimes 	}
916f6b04d2bSDavid Greenman 
917f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
918f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
919f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
920f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
921a316d390SJohn Dyson 	auio.uio_offset = poffset;
922f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
923f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
924f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
925f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
926a316d390SJohn Dyson 	error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred);
927976e77fcSDavid Greenman 	cnt.v_vnodeout++;
928f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
929f6b04d2bSDavid Greenman 
930f6b04d2bSDavid Greenman 	if (error) {
93124a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
932f6b04d2bSDavid Greenman 	}
933f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
934f708ef1bSPoul-Henning Kamp 		printf("vnode_pager_putpages: residual I/O %d at %ld\n",
935a316d390SJohn Dyson 			auio.uio_resid, m[0]->pindex);
93626f9a767SRodney W. Grimes 	}
93726f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
9385f55e841SDavid Greenman 		m[i]->busy--;
939f6b04d2bSDavid Greenman 		if (i < ncount) {
94026f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
94126f9a767SRodney W. Grimes 		}
942f6b04d2bSDavid Greenman 		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED))
94324a1cce3SDavid Greenman 			wakeup(m[i]);
94426f9a767SRodney W. Grimes 	}
945f6b04d2bSDavid Greenman 	return rtvals[0];
94626f9a767SRodney W. Grimes }
947f6b04d2bSDavid Greenman 
948f6b04d2bSDavid Greenman struct vnode *
94924a1cce3SDavid Greenman vnode_pager_lock(object)
95024a1cce3SDavid Greenman 	vm_object_t object;
95124a1cce3SDavid Greenman {
95224a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
95324a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
954f6b04d2bSDavid Greenman 			continue;
955f6b04d2bSDavid Greenman 
95624a1cce3SDavid Greenman 		VOP_LOCK(object->handle);
95724a1cce3SDavid Greenman 		return object->handle;
95826f9a767SRodney W. Grimes 	}
95924a1cce3SDavid Greenman 	return NULL;
960f6b04d2bSDavid Greenman }
961