xref: /freebsd/sys/vm/vnode_pager.c (revision a316d390bda3e185e04632e807a012a345492935)
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
41a316d390SJohn Dyson  *	$Id: vnode_pager.c,v 1.54 1995/12/07 12:48:31 davidg 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 
74a316d390SJohn Dyson extern vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
750b8253a7SBruce Evans 					 int *run));
760b8253a7SBruce Evans extern void vnode_pager_iodone __P((struct buf *bp));
770b8253a7SBruce Evans extern int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
780b8253a7SBruce Evans extern int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
790b8253a7SBruce Evans 
80df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
8124a1cce3SDavid Greenman 	NULL,
82df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
83df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
8424a1cce3SDavid Greenman 	vnode_pager_getpages,
8524a1cce3SDavid Greenman 	vnode_pager_putpages,
8624a1cce3SDavid Greenman 	vnode_pager_haspage,
8724a1cce3SDavid Greenman 	NULL
88df8bae1dSRodney W. Grimes };
89df8bae1dSRodney W. Grimes 
900b8253a7SBruce Evans static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m,
910b8253a7SBruce Evans 					  int count, int reqpage));
920b8253a7SBruce Evans static int vnode_pager_leaf_putpages __P((vm_object_t object, vm_page_t *m,
930b8253a7SBruce Evans 					  int count, boolean_t sync,
940b8253a7SBruce Evans 					  int *rtvals));
95170db9c6SJohn Dyson 
96df8bae1dSRodney W. Grimes /*
97df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
98df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
99df8bae1dSRodney W. Grimes  */
10024a1cce3SDavid Greenman vm_object_t
10126f9a767SRodney W. Grimes vnode_pager_alloc(handle, size, prot, offset)
102ee3a64c9SDavid Greenman 	void *handle;
103df8bae1dSRodney W. Grimes 	vm_size_t size;
104df8bae1dSRodney W. Grimes 	vm_prot_t prot;
105a316d390SJohn Dyson 	vm_ooffset_t offset;
106df8bae1dSRodney W. Grimes {
10706cb7259SDavid Greenman 	vm_object_t object;
108df8bae1dSRodney W. Grimes 	struct vnode *vp;
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes 	/*
111df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
112df8bae1dSRodney W. Grimes 	 */
113df8bae1dSRodney W. Grimes 	if (handle == NULL)
114df8bae1dSRodney W. Grimes 		return (NULL);
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
11739d38f93SDavid Greenman 
11839d38f93SDavid Greenman 	/*
11939d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
12039d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
12139d38f93SDavid Greenman 	 */
12239d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
12339d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
12439d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
12539d38f93SDavid Greenman 	}
12639d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
12739d38f93SDavid Greenman 
12839d38f93SDavid Greenman 	/*
12939d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
13039d38f93SDavid Greenman 	 * go away.
13139d38f93SDavid Greenman 	 */
13224a1cce3SDavid Greenman 	while (((object = vp->v_object) != NULL) && (object->flags & OBJ_DEAD)) {
133aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
13424a1cce3SDavid Greenman 	}
1350d94caffSDavid Greenman 
13624a1cce3SDavid Greenman 	if (object == NULL) {
137df8bae1dSRodney W. Grimes 		/*
138df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
139df8bae1dSRodney W. Grimes 		 */
140a316d390SJohn Dyson 		object = vm_object_allocate(OBJT_VNODE, size);
1414bb62461SDavid Greenman 		object->flags = OBJ_CANPERSIST;
142bbc0ec52SDavid Greenman 
143df8bae1dSRodney W. Grimes 		/*
14424a1cce3SDavid Greenman 		 * Hold a reference to the vnode and initialize object data.
145df8bae1dSRodney W. Grimes 		 */
146df8bae1dSRodney W. Grimes 		VREF(vp);
147a316d390SJohn Dyson 		object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE;
14826f9a767SRodney W. Grimes 
14924a1cce3SDavid Greenman 		object->handle = handle;
15024a1cce3SDavid Greenman 		vp->v_object = object;
151df8bae1dSRodney W. Grimes 	} else {
152df8bae1dSRodney W. Grimes 		/*
15324a1cce3SDavid Greenman 		 * vm_object_reference() will remove the object from the cache if
15424a1cce3SDavid Greenman 		 * found and gain a reference to the object.
155df8bae1dSRodney W. Grimes 		 */
15624a1cce3SDavid Greenman 		vm_object_reference(object);
157df8bae1dSRodney W. Grimes 	}
15839d38f93SDavid Greenman 
159f6b04d2bSDavid Greenman 	if (vp->v_type == VREG)
160f6b04d2bSDavid Greenman 		vp->v_flag |= VVMIO;
16139d38f93SDavid Greenman 
16239d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
16339d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
16439d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
16539d38f93SDavid Greenman 		wakeup(vp);
16639d38f93SDavid Greenman 	}
16724a1cce3SDavid Greenman 	return (object);
168df8bae1dSRodney W. Grimes }
169df8bae1dSRodney W. Grimes 
17026f9a767SRodney W. Grimes void
17124a1cce3SDavid Greenman vnode_pager_dealloc(object)
1720d94caffSDavid Greenman 	vm_object_t object;
17324a1cce3SDavid Greenman {
17424a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
175df8bae1dSRodney W. Grimes 
17624a1cce3SDavid Greenman 	if (vp == NULL)
17724a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
17824a1cce3SDavid Greenman 
17924a1cce3SDavid Greenman 	if (object->paging_in_progress) {
1800d94caffSDavid Greenman 		int s = splbio();
1810d94caffSDavid Greenman 		while (object->paging_in_progress) {
182c0503609SDavid Greenman 			object->flags |= OBJ_PIPWNT;
1830d94caffSDavid Greenman 			tsleep(object, PVM, "vnpdea", 0);
1840d94caffSDavid Greenman 		}
1850d94caffSDavid Greenman 		splx(s);
18624a1cce3SDavid Greenman 	}
18724a1cce3SDavid Greenman 
18824a1cce3SDavid Greenman 	object->handle = NULL;
1890d94caffSDavid Greenman 
190aa2cabb9SDavid Greenman 	vp->v_object = NULL;
1918e58bf68SDavid Greenman 	vp->v_flag &= ~(VTEXT | VVMIO);
19200072442SDavid Greenman 	vp->v_flag |= VAGE;
193df8bae1dSRodney W. Grimes 	vrele(vp);
194df8bae1dSRodney W. Grimes }
19526f9a767SRodney W. Grimes 
19626f9a767SRodney W. Grimes boolean_t
197a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
19824a1cce3SDavid Greenman 	vm_object_t object;
199a316d390SJohn Dyson 	vm_pindex_t pindex;
20024a1cce3SDavid Greenman 	int *before;
20124a1cce3SDavid Greenman 	int *after;
202df8bae1dSRodney W. Grimes {
20324a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
204df8bae1dSRodney W. Grimes 	daddr_t bn;
2053af76890SPoul-Henning Kamp 	int err;
206170db9c6SJohn Dyson 	daddr_t reqblock;
2072c4488fcSJohn Dyson 	int poff;
2082c4488fcSJohn Dyson 	int bsize;
2092c4488fcSJohn Dyson 	int pagesperblock;
210df8bae1dSRodney W. Grimes 
211df8bae1dSRodney W. Grimes 	/*
2120d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
2130d94caffSDavid Greenman 	 * not have the page.
214df8bae1dSRodney W. Grimes 	 */
215a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
216a316d390SJohn Dyson 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2174abc71c0SDavid Greenman 		return FALSE;
218df8bae1dSRodney W. Grimes 
219eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
220170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
221a316d390SJohn Dyson 	reqblock = pindex / pagesperblock;
222170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
223170db9c6SJohn Dyson 		after, before);
2240d94caffSDavid Greenman 	if (err)
22524a1cce3SDavid Greenman 		return TRUE;
2266eab77f2SJohn Dyson 	if ( bn == -1)
227ced399eeSJohn Dyson 		return FALSE;
228a316d390SJohn Dyson 	poff = pindex - (reqblock * pagesperblock);
229170db9c6SJohn Dyson 	if (before) {
230170db9c6SJohn Dyson 		*before *= pagesperblock;
231170db9c6SJohn Dyson 		*before += poff;
232170db9c6SJohn Dyson 	}
233170db9c6SJohn Dyson 	if (after) {
234b1fc01b7SJohn Dyson 		int numafter;
235170db9c6SJohn Dyson 		*after *= pagesperblock;
236b1fc01b7SJohn Dyson 		numafter = pagesperblock - (poff + 1);
237a316d390SJohn Dyson 		if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
238a316d390SJohn Dyson 			numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
239b1fc01b7SJohn Dyson 		}
240b1fc01b7SJohn Dyson 		*after += numafter;
241170db9c6SJohn Dyson 	}
242ced399eeSJohn Dyson 	return TRUE;
243df8bae1dSRodney W. Grimes }
244df8bae1dSRodney W. Grimes 
245df8bae1dSRodney W. Grimes /*
246df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
24724a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
248df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
249df8bae1dSRodney W. Grimes  *
250df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
251df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
252df8bae1dSRodney W. Grimes  */
253df8bae1dSRodney W. Grimes void
254df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
255df8bae1dSRodney W. Grimes 	struct vnode *vp;
256a316d390SJohn Dyson 	vm_ooffset_t nsize;
257df8bae1dSRodney W. Grimes {
25824a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
259df8bae1dSRodney W. Grimes 
26024a1cce3SDavid Greenman 	if (object == NULL)
261df8bae1dSRodney W. Grimes 		return;
262bbc0ec52SDavid Greenman 
263df8bae1dSRodney W. Grimes 	/*
264df8bae1dSRodney W. Grimes 	 * Hasn't changed size
265df8bae1dSRodney W. Grimes 	 */
26624a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
267df8bae1dSRodney W. Grimes 		return;
268bbc0ec52SDavid Greenman 
269df8bae1dSRodney W. Grimes 	/*
270bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
271df8bae1dSRodney W. Grimes 	 */
27224a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
273a316d390SJohn Dyson 		vm_ooffset_t nsizerounded;
274a316d390SJohn Dyson 		nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_SIZE - 1));
275a316d390SJohn Dyson 		if (nsizerounded < object->un_pager.vnp.vnp_size) {
276df8bae1dSRodney W. Grimes 			vm_object_page_remove(object,
277a316d390SJohn Dyson 				OFF_TO_IDX(nsize + PAGE_SIZE - 1),
278a316d390SJohn Dyson 				OFF_TO_IDX(object->un_pager.vnp.vnp_size),
279a316d390SJohn Dyson 				FALSE);
2800d94caffSDavid Greenman 		}
281bbc0ec52SDavid Greenman 		/*
282bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
283bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
284bbc0ec52SDavid Greenman 		 */
285bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
286bbc0ec52SDavid Greenman 			vm_offset_t kva;
287bbc0ec52SDavid Greenman 			vm_page_t m;
288bbc0ec52SDavid Greenman 
289a316d390SJohn Dyson 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
290bbc0ec52SDavid Greenman 			if (m) {
291bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
292bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
293a316d390SJohn Dyson 				    (int) (round_page(nsize) - nsize));
294bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
295bbc0ec52SDavid Greenman 			}
296bbc0ec52SDavid Greenman 		}
297bbc0ec52SDavid Greenman 	}
298a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
299a316d390SJohn Dyson 	object->size = OFF_TO_IDX(nsize + PAGE_SIZE - 1);
300df8bae1dSRodney W. Grimes }
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes void
303df8bae1dSRodney W. Grimes vnode_pager_umount(mp)
304df8bae1dSRodney W. Grimes 	register struct mount *mp;
305df8bae1dSRodney W. Grimes {
30624a1cce3SDavid Greenman 	struct vnode *vp, *nvp;
307df8bae1dSRodney W. Grimes 
30824a1cce3SDavid Greenman loop:
30924a1cce3SDavid Greenman 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
31024a1cce3SDavid Greenman 		/*
31124a1cce3SDavid Greenman 		 * Vnode can be reclaimed by getnewvnode() while we
31224a1cce3SDavid Greenman 		 * traverse the list.
31324a1cce3SDavid Greenman 		 */
31424a1cce3SDavid Greenman 		if (vp->v_mount != mp)
31524a1cce3SDavid Greenman 			goto loop;
31624a1cce3SDavid Greenman 
317df8bae1dSRodney W. Grimes 		/*
318bbc0ec52SDavid Greenman 		 * Save the next pointer now since uncaching may terminate the
31924a1cce3SDavid Greenman 		 * object and render vnode invalid
320df8bae1dSRodney W. Grimes 		 */
32124a1cce3SDavid Greenman 		nvp = vp->v_mntvnodes.le_next;
32224a1cce3SDavid Greenman 
32324a1cce3SDavid Greenman 		if (vp->v_object != NULL) {
324c01a9b8cSDavid Greenman 			VOP_LOCK(vp);
32524a1cce3SDavid Greenman 			vnode_pager_uncache(vp);
326c01a9b8cSDavid Greenman 			VOP_UNLOCK(vp);
327c01a9b8cSDavid Greenman 		}
328df8bae1dSRodney W. Grimes 	}
329df8bae1dSRodney W. Grimes }
330df8bae1dSRodney W. Grimes 
331df8bae1dSRodney W. Grimes /*
332df8bae1dSRodney W. Grimes  * Remove vnode associated object from the object cache.
333c01a9b8cSDavid Greenman  * This routine must be called with the vnode locked.
334df8bae1dSRodney W. Grimes  *
335c01a9b8cSDavid Greenman  * XXX unlock the vnode.
336c01a9b8cSDavid Greenman  * We must do this since uncaching the object may result in its
337c01a9b8cSDavid Greenman  * destruction which may initiate paging activity which may necessitate
338c01a9b8cSDavid Greenman  * re-locking the vnode.
33926f9a767SRodney W. Grimes  */
34024a1cce3SDavid Greenman void
34126f9a767SRodney W. Grimes vnode_pager_uncache(vp)
34224a1cce3SDavid Greenman 	struct vnode *vp;
34326f9a767SRodney W. Grimes {
34424a1cce3SDavid Greenman 	vm_object_t object;
34526f9a767SRodney W. Grimes 
34626f9a767SRodney W. Grimes 	/*
34726f9a767SRodney W. Grimes 	 * Not a mapped vnode
34826f9a767SRodney W. Grimes 	 */
349aa2cabb9SDavid Greenman 	object = vp->v_object;
3508e58bf68SDavid Greenman 	if (object == NULL)
35124a1cce3SDavid Greenman 		return;
3520d94caffSDavid Greenman 
35324a1cce3SDavid Greenman 	vm_object_reference(object);
354c01a9b8cSDavid Greenman 	VOP_UNLOCK(vp);
35526f9a767SRodney W. Grimes 	pager_cache(object, FALSE);
356c01a9b8cSDavid Greenman 	VOP_LOCK(vp);
35724a1cce3SDavid Greenman 	return;
35826f9a767SRodney W. Grimes }
359df8bae1dSRodney W. Grimes 
36026f9a767SRodney W. Grimes 
36126f9a767SRodney W. Grimes void
36226f9a767SRodney W. Grimes vnode_pager_freepage(m)
36326f9a767SRodney W. Grimes 	vm_page_t m;
364df8bae1dSRodney W. Grimes {
36526f9a767SRodney W. Grimes 	PAGE_WAKEUP(m);
36626f9a767SRodney W. Grimes 	vm_page_free(m);
36726f9a767SRodney W. Grimes }
36826f9a767SRodney W. Grimes 
36926f9a767SRodney W. Grimes /*
37026f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
37126f9a767SRodney W. Grimes  * file address
37226f9a767SRodney W. Grimes  */
37326f9a767SRodney W. Grimes vm_offset_t
374efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
37526f9a767SRodney W. Grimes 	struct vnode *vp;
376a316d390SJohn Dyson 	vm_ooffset_t address;
377efc68ce1SDavid Greenman 	int *run;
37826f9a767SRodney W. Grimes {
37926f9a767SRodney W. Grimes 	int rtaddress;
38026f9a767SRodney W. Grimes 	int bsize;
381a316d390SJohn Dyson 	daddr_t block;
38226f9a767SRodney W. Grimes 	struct vnode *rtvp;
38326f9a767SRodney W. Grimes 	int err;
384a316d390SJohn Dyson 	daddr_t vblock;
385a316d390SJohn Dyson 	int voffset;
38626f9a767SRodney W. Grimes 
3870d94caffSDavid Greenman 	if ((int) address < 0)
3880d94caffSDavid Greenman 		return -1;
3890d94caffSDavid Greenman 
3902c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3912c4488fcSJohn Dyson 		return -1;
3922c4488fcSJohn Dyson 
39326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
39426f9a767SRodney W. Grimes 	vblock = address / bsize;
39526f9a767SRodney W. Grimes 	voffset = address % bsize;
39626f9a767SRodney W. Grimes 
397c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
39826f9a767SRodney W. Grimes 
399efc68ce1SDavid Greenman 	if (err || (block == -1))
40026f9a767SRodney W. Grimes 		rtaddress = -1;
401efc68ce1SDavid Greenman 	else {
402187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
403efc68ce1SDavid Greenman 		if( run) {
404efc68ce1SDavid Greenman 			*run += 1;
405efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
406efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
407efc68ce1SDavid Greenman 		}
408efc68ce1SDavid Greenman 	}
40926f9a767SRodney W. Grimes 
41026f9a767SRodney W. Grimes 	return rtaddress;
41126f9a767SRodney W. Grimes }
41226f9a767SRodney W. Grimes 
41326f9a767SRodney W. Grimes /*
41426f9a767SRodney W. Grimes  * interrupt routine for I/O completion
41526f9a767SRodney W. Grimes  */
41626f9a767SRodney W. Grimes void
41726f9a767SRodney W. Grimes vnode_pager_iodone(bp)
41826f9a767SRodney W. Grimes 	struct buf *bp;
41926f9a767SRodney W. Grimes {
42026f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
42124a1cce3SDavid Greenman 	wakeup(bp);
42226f9a767SRodney W. Grimes }
42326f9a767SRodney W. Grimes 
42426f9a767SRodney W. Grimes /*
42526f9a767SRodney W. Grimes  * small block file system vnode pager input
42626f9a767SRodney W. Grimes  */
42726f9a767SRodney W. Grimes int
42824a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
42924a1cce3SDavid Greenman 	vm_object_t object;
43026f9a767SRodney W. Grimes 	vm_page_t m;
43126f9a767SRodney W. Grimes {
43226f9a767SRodney W. Grimes 	int i;
43326f9a767SRodney W. Grimes 	int s;
43426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
43526f9a767SRodney W. Grimes 	struct buf *bp;
43626f9a767SRodney W. Grimes 	vm_offset_t kva;
43726f9a767SRodney W. Grimes 	int fileaddr;
43826f9a767SRodney W. Grimes 	vm_offset_t bsize;
43926f9a767SRodney W. Grimes 	int error = 0;
44026f9a767SRodney W. Grimes 
44124a1cce3SDavid Greenman 	vp = object->handle;
4422c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4432c4488fcSJohn Dyson 		return VM_PAGER_BAD;
4442c4488fcSJohn Dyson 
44526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
44626f9a767SRodney W. Grimes 
4470bdb7528SDavid Greenman 
448c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
44926f9a767SRodney W. Grimes 
45026f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
45126f9a767SRodney W. Grimes 
45226f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
453bbc0ec52SDavid Greenman 
454a316d390SJohn Dyson 		if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
45526f9a767SRodney W. Grimes 			continue;
45626f9a767SRodney W. Grimes 
457a316d390SJohn Dyson 		fileaddr = vnode_pager_addr(vp,
458a316d390SJohn Dyson 			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
45926f9a767SRodney W. Grimes 		if (fileaddr != -1) {
46026f9a767SRodney W. Grimes 			bp = getpbuf();
46126f9a767SRodney W. Grimes 
46226f9a767SRodney W. Grimes 			/* build a minimal buffer header */
46326f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
46426f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
46526f9a767SRodney W. Grimes 			bp->b_proc = curproc;
46626f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
46726f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
46826f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
46926f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
47026f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
47126f9a767SRodney W. Grimes 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
472187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4730d94caffSDavid Greenman 			pbgetvp(dp, bp);
47426f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
47526f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
47626f9a767SRodney W. Grimes 
47726f9a767SRodney W. Grimes 			/* do the input */
47826f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
47926f9a767SRodney W. Grimes 
48026f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
48126f9a767SRodney W. Grimes 
48226f9a767SRodney W. Grimes 			s = splbio();
48326f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
484aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
48526f9a767SRodney W. Grimes 			}
48626f9a767SRodney W. Grimes 			splx(s);
48726f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
48826f9a767SRodney W. Grimes 				error = EIO;
48926f9a767SRodney W. Grimes 
49026f9a767SRodney W. Grimes 			/*
49126f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
49226f9a767SRodney W. Grimes 			 */
49326f9a767SRodney W. Grimes 			relpbuf(bp);
49426f9a767SRodney W. Grimes 			if (error)
49526f9a767SRodney W. Grimes 				break;
4960d94caffSDavid Greenman 
497170db9c6SJohn Dyson 			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
49826f9a767SRodney W. Grimes 		} else {
499b1fc01b7SJohn Dyson 			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
50026f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
50126f9a767SRodney W. Grimes 		}
50226f9a767SRodney W. Grimes 	}
50326f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
5040d94caffSDavid Greenman 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
505b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
50626f9a767SRodney W. Grimes 	if (error) {
507a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
50826f9a767SRodney W. Grimes 	}
50926f9a767SRodney W. Grimes 	return VM_PAGER_OK;
51026f9a767SRodney W. Grimes 
51126f9a767SRodney W. Grimes }
51226f9a767SRodney W. Grimes 
51326f9a767SRodney W. Grimes 
51426f9a767SRodney W. Grimes /*
51526f9a767SRodney W. Grimes  * old style vnode pager output routine
51626f9a767SRodney W. Grimes  */
51726f9a767SRodney W. Grimes int
51824a1cce3SDavid Greenman vnode_pager_input_old(object, m)
51924a1cce3SDavid Greenman 	vm_object_t object;
52026f9a767SRodney W. Grimes 	vm_page_t m;
52126f9a767SRodney W. Grimes {
522df8bae1dSRodney W. Grimes 	struct uio auio;
523df8bae1dSRodney W. Grimes 	struct iovec aiov;
52426f9a767SRodney W. Grimes 	int error;
52526f9a767SRodney W. Grimes 	int size;
52626f9a767SRodney W. Grimes 	vm_offset_t kva;
527df8bae1dSRodney W. Grimes 
52826f9a767SRodney W. Grimes 	error = 0;
529bbc0ec52SDavid Greenman 
530df8bae1dSRodney W. Grimes 	/*
53126f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
53226f9a767SRodney W. Grimes 	 */
533a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
53426f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
53526f9a767SRodney W. Grimes 	} else {
53626f9a767SRodney W. Grimes 		size = PAGE_SIZE;
537a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
538a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
5390bdb7528SDavid Greenman 
54026f9a767SRodney W. Grimes 		/*
541df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
542df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
543df8bae1dSRodney W. Grimes 		 */
54426f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
5450bdb7528SDavid Greenman 
546df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
547df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
548df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
549df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
550a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
551df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
55226f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
553df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
554df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
55526f9a767SRodney W. Grimes 
55624a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
557df8bae1dSRodney W. Grimes 		if (!error) {
558df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes 			if (count == 0)
561df8bae1dSRodney W. Grimes 				error = EINVAL;
56226f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
56326f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
564df8bae1dSRodney W. Grimes 		}
56526f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
566df8bae1dSRodney W. Grimes 	}
56726f9a767SRodney W. Grimes 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5680d94caffSDavid Greenman 	m->dirty = 0;
569b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
570a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
57126f9a767SRodney W. Grimes }
57226f9a767SRodney W. Grimes 
57326f9a767SRodney W. Grimes /*
57426f9a767SRodney W. Grimes  * generic vnode pager input routine
57526f9a767SRodney W. Grimes  */
576170db9c6SJohn Dyson 
57726f9a767SRodney W. Grimes int
57824a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
57926f9a767SRodney W. Grimes 	vm_object_t object;
58024a1cce3SDavid Greenman 	vm_page_t *m;
58124a1cce3SDavid Greenman 	int count;
58224a1cce3SDavid Greenman 	int reqpage;
58324a1cce3SDavid Greenman {
584170db9c6SJohn Dyson 	int rtval;
585170db9c6SJohn Dyson 	struct vnode *vp;
586170db9c6SJohn Dyson 	vp = object->handle;
5872c4488fcSJohn Dyson 	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
588170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
5890b8253a7SBruce Evans 		return vnode_pager_leaf_getpages(object, m, count, reqpage);
590170db9c6SJohn Dyson 	else
591170db9c6SJohn Dyson 		return rtval;
592170db9c6SJohn Dyson }
593170db9c6SJohn Dyson 
594170db9c6SJohn Dyson static int
595170db9c6SJohn Dyson vnode_pager_leaf_getpages(object, m, count, reqpage)
596170db9c6SJohn Dyson 	vm_object_t object;
597170db9c6SJohn Dyson 	vm_page_t *m;
598170db9c6SJohn Dyson 	int count;
599170db9c6SJohn Dyson 	int reqpage;
600170db9c6SJohn Dyson {
601a316d390SJohn Dyson 	vm_offset_t kva;
602a316d390SJohn Dyson 	off_t foff;
60324a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
60426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
605efc68ce1SDavid Greenman 	int runpg;
606efc68ce1SDavid Greenman 	int runend;
6070bdb7528SDavid Greenman 	struct buf *bp;
60826f9a767SRodney W. Grimes 	int s;
60926f9a767SRodney W. Grimes 	int error = 0;
61026f9a767SRodney W. Grimes 
61124a1cce3SDavid Greenman 	vp = object->handle;
6122c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
6132c4488fcSJohn Dyson 		return VM_PAGER_BAD;
6142c4488fcSJohn Dyson 
61526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
61626f9a767SRodney W. Grimes 
61726f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
618bbc0ec52SDavid Greenman 
61926f9a767SRodney W. Grimes 	/*
620bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
621bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
62226f9a767SRodney W. Grimes 	 */
623a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
624bbc0ec52SDavid Greenman 
62526f9a767SRodney W. Grimes 	/*
62616f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
62726f9a767SRodney W. Grimes 	 */
628c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
62926f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
63026f9a767SRodney W. Grimes 			if (i != reqpage) {
63126f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
63226f9a767SRodney W. Grimes 			}
63326f9a767SRodney W. Grimes 		}
634976e77fcSDavid Greenman 		cnt.v_vnodein++;
635976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
63624a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
637bbc0ec52SDavid Greenman 
63826f9a767SRodney W. Grimes 		/*
63926f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
64026f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
64126f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
64226f9a767SRodney W. Grimes 		 */
64326f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
64426f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
64526f9a767SRodney W. Grimes 
64626f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
64726f9a767SRodney W. Grimes 			if (i != reqpage) {
64826f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
64926f9a767SRodney W. Grimes 			}
65026f9a767SRodney W. Grimes 		}
651976e77fcSDavid Greenman 		cnt.v_vnodein++;
652976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
65324a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
65426f9a767SRodney W. Grimes 	}
65526f9a767SRodney W. Grimes 	/*
6560d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
6570d94caffSDavid Greenman 	 * then, the entire page is valid --
6580d94caffSDavid Greenman 	 */
6590d94caffSDavid Greenman 	if (m[reqpage]->valid) {
6600d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
6610d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6620d94caffSDavid Greenman 			if (i != reqpage)
6630d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6640d94caffSDavid Greenman 		}
6650d94caffSDavid Greenman 		return VM_PAGER_OK;
6660d94caffSDavid Greenman 	}
6670bdb7528SDavid Greenman 
6680d94caffSDavid Greenman 	/*
66926f9a767SRodney W. Grimes 	 * here on direct device I/O
67026f9a767SRodney W. Grimes 	 */
67126f9a767SRodney W. Grimes 
672efc68ce1SDavid Greenman 	firstaddr = -1;
67326f9a767SRodney W. Grimes 	/*
674efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
67526f9a767SRodney W. Grimes 	 */
676efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
677a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
678a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
679efc68ce1SDavid Greenman 		if (firstaddr == -1) {
68024a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
68124a1cce3SDavid Greenman 				panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
68224a1cce3SDavid Greenman 			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
683efc68ce1SDavid Greenman 			}
68426f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
685efc68ce1SDavid Greenman 			runend = i + 1;
686efc68ce1SDavid Greenman 			first = runend;
687efc68ce1SDavid Greenman 			continue;
688efc68ce1SDavid Greenman 		}
689efc68ce1SDavid Greenman 		runend = i + runpg;
690efc68ce1SDavid Greenman 		if (runend <= reqpage) {
691efc68ce1SDavid Greenman 			int j;
692efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
693efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
694efc68ce1SDavid Greenman 			}
69526f9a767SRodney W. Grimes 		} else {
696efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
697efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
69826f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
699efc68ce1SDavid Greenman 				count = first + runpg;
70026f9a767SRodney W. Grimes 			}
701efc68ce1SDavid Greenman 			break;
70226f9a767SRodney W. Grimes 		}
703efc68ce1SDavid Greenman 		first = runend;
704efc68ce1SDavid Greenman 	}
70526f9a767SRodney W. Grimes 
70626f9a767SRodney W. Grimes 	/*
707bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
708bbc0ec52SDavid Greenman 	 * to be zero based...
70926f9a767SRodney W. Grimes 	 */
71026f9a767SRodney W. Grimes 	if (first != 0) {
71126f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
71226f9a767SRodney W. Grimes 			m[i - first] = m[i];
71326f9a767SRodney W. Grimes 		}
71426f9a767SRodney W. Grimes 		count -= first;
71526f9a767SRodney W. Grimes 		reqpage -= first;
71626f9a767SRodney W. Grimes 	}
717efc68ce1SDavid Greenman 
71826f9a767SRodney W. Grimes 	/*
71926f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
72026f9a767SRodney W. Grimes 	 */
721a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
72226f9a767SRodney W. Grimes 
72326f9a767SRodney W. Grimes 	/*
72426f9a767SRodney W. Grimes 	 * calculate the size of the transfer
72526f9a767SRodney W. Grimes 	 */
72626f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
72724a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
72824a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
72926f9a767SRodney W. Grimes 
73026f9a767SRodney W. Grimes 	/*
73126f9a767SRodney W. Grimes 	 * round up physical size for real devices
73226f9a767SRodney W. Grimes 	 */
73326f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
73426f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
73526f9a767SRodney W. Grimes 
7366d40c3d3SDavid Greenman 	bp = getpbuf();
73716f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
73816f62314SDavid Greenman 
73926f9a767SRodney W. Grimes 	/*
74026f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
74126f9a767SRodney W. Grimes 	 */
74216f62314SDavid Greenman 	pmap_qenter(kva, m, count);
74326f9a767SRodney W. Grimes 
74426f9a767SRodney W. Grimes 	/* build a minimal buffer header */
74526f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
74626f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
74726f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
74826f9a767SRodney W. Grimes 	bp->b_proc = curproc;
74926f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
75026f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
75126f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
75226f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
75326f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
754187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7550d94caffSDavid Greenman 	pbgetvp(dp, bp);
75626f9a767SRodney W. Grimes 	bp->b_bcount = size;
75726f9a767SRodney W. Grimes 	bp->b_bufsize = size;
75826f9a767SRodney W. Grimes 
759976e77fcSDavid Greenman 	cnt.v_vnodein++;
760976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
761976e77fcSDavid Greenman 
76226f9a767SRodney W. Grimes 	/* do the input */
76326f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
764976e77fcSDavid Greenman 
76526f9a767SRodney W. Grimes 	s = splbio();
76626f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
76726f9a767SRodney W. Grimes 
76826f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
769aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
77026f9a767SRodney W. Grimes 	}
77126f9a767SRodney W. Grimes 	splx(s);
77226f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
77326f9a767SRodney W. Grimes 		error = EIO;
77426f9a767SRodney W. Grimes 
77526f9a767SRodney W. Grimes 	if (!error) {
77626f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
77726f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
77826f9a767SRodney W. Grimes 	}
77916f62314SDavid Greenman 	pmap_qremove(kva, count);
78026f9a767SRodney W. Grimes 
78126f9a767SRodney W. Grimes 	/*
78226f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
78326f9a767SRodney W. Grimes 	 */
78426f9a767SRodney W. Grimes 	relpbuf(bp);
78526f9a767SRodney W. Grimes 
78626f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
787fff93ab6SDavid Greenman 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
7880d94caffSDavid Greenman 		m[i]->dirty = 0;
7890d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
790b1fc01b7SJohn Dyson 		m[i]->flags &= ~PG_ZERO;
79126f9a767SRodney W. Grimes 		if (i != reqpage) {
792bbc0ec52SDavid Greenman 
79326f9a767SRodney W. Grimes 			/*
794bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
795bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
796bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
797bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
798bbc0ec52SDavid Greenman 			 * deactivating pages is best.
79926f9a767SRodney W. Grimes 			 */
800bbc0ec52SDavid Greenman 
80126f9a767SRodney W. Grimes 			/*
802bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
803bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
80426f9a767SRodney W. Grimes 			 */
80526f9a767SRodney W. Grimes 			if (!error) {
80626f9a767SRodney W. Grimes 				vm_page_deactivate(m[i]);
80726f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
80826f9a767SRodney W. Grimes 			} else {
80926f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
81026f9a767SRodney W. Grimes 			}
81126f9a767SRodney W. Grimes 		}
81226f9a767SRodney W. Grimes 	}
81326f9a767SRodney W. Grimes 	if (error) {
81424a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
81526f9a767SRodney W. Grimes 	}
816a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
81726f9a767SRodney W. Grimes }
81826f9a767SRodney W. Grimes 
819170db9c6SJohn Dyson int
820170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
821170db9c6SJohn Dyson 	vm_object_t object;
822170db9c6SJohn Dyson 	vm_page_t *m;
823170db9c6SJohn Dyson 	int count;
824170db9c6SJohn Dyson 	boolean_t sync;
825170db9c6SJohn Dyson 	int *rtvals;
826170db9c6SJohn Dyson {
827170db9c6SJohn Dyson 	int rtval;
828170db9c6SJohn Dyson 	struct vnode *vp;
829170db9c6SJohn Dyson 	vp = object->handle;
8302c4488fcSJohn Dyson 	rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
831170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
8320b8253a7SBruce Evans 		return vnode_pager_leaf_putpages(object, m, count, sync, rtvals);
833170db9c6SJohn Dyson 	else
834170db9c6SJohn Dyson 		return rtval;
835170db9c6SJohn Dyson }
836170db9c6SJohn Dyson 
83726f9a767SRodney W. Grimes /*
83826f9a767SRodney W. Grimes  * generic vnode pager output routine
83926f9a767SRodney W. Grimes  */
840170db9c6SJohn Dyson static int
841170db9c6SJohn Dyson vnode_pager_leaf_putpages(object, m, count, sync, rtvals)
84224a1cce3SDavid Greenman 	vm_object_t object;
84326f9a767SRodney W. Grimes 	vm_page_t *m;
84426f9a767SRodney W. Grimes 	int count;
84524a1cce3SDavid Greenman 	boolean_t sync;
84626f9a767SRodney W. Grimes 	int *rtvals;
84726f9a767SRodney W. Grimes {
848f6b04d2bSDavid Greenman 	int i;
84926f9a767SRodney W. Grimes 
850f6b04d2bSDavid Greenman 	struct vnode *vp;
851f6b04d2bSDavid Greenman 	int maxsize, ncount;
852a316d390SJohn Dyson 	vm_ooffset_t poffset;
853f6b04d2bSDavid Greenman 	struct uio auio;
854f6b04d2bSDavid Greenman 	struct iovec aiov;
855f6b04d2bSDavid Greenman 	int error;
85626f9a767SRodney W. Grimes 
85724a1cce3SDavid Greenman 	vp = object->handle;;
85826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
85926f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
86026f9a767SRodney W. Grimes 
861a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
862a316d390SJohn Dyson 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty);
863f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
864f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
8650d94caffSDavid Greenman 	}
8660bdb7528SDavid Greenman 
867f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
868f6b04d2bSDavid Greenman 	ncount = count;
86926f9a767SRodney W. Grimes 
870a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
871a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
872a316d390SJohn Dyson 		if (object->un_pager.vnp.vnp_size > poffset)
873a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
8745f55e841SDavid Greenman 		else
8755f55e841SDavid Greenman 			maxsize = 0;
876f6b04d2bSDavid Greenman 		ncount = (maxsize + PAGE_SIZE - 1) / PAGE_SIZE;
877f6b04d2bSDavid Greenman 		if (ncount < count) {
878f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
879f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
880f6b04d2bSDavid Greenman 			}
881a316d390SJohn Dyson #ifdef BOGUS
882f6b04d2bSDavid Greenman 			if (ncount == 0) {
883a316d390SJohn Dyson 				printf("vnode_pager_putpages: write past end of file: %d, %lu\n",
884a316d390SJohn Dyson 					poffset,
885a316d390SJohn Dyson 					(unsigned long) object->un_pager.vnp.vnp_size);
88626f9a767SRodney W. Grimes 				return rtvals[0];
88726f9a767SRodney W. Grimes 			}
888a316d390SJohn Dyson #endif
889f6b04d2bSDavid Greenman 		}
890f6b04d2bSDavid Greenman 	}
89126f9a767SRodney W. Grimes 
89226f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8935f55e841SDavid Greenman 		m[i]->busy++;
894f6b04d2bSDavid Greenman 		m[i]->flags &= ~PG_BUSY;
89526f9a767SRodney W. Grimes 	}
896f6b04d2bSDavid Greenman 
897f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
898f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
899f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
900f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
901a316d390SJohn Dyson 	auio.uio_offset = poffset;
902f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
903f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
904f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
905f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
906a316d390SJohn Dyson 	error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred);
907976e77fcSDavid Greenman 	cnt.v_vnodeout++;
908f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
909f6b04d2bSDavid Greenman 
910f6b04d2bSDavid Greenman 	if (error) {
91124a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
912f6b04d2bSDavid Greenman 	}
913f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
914a316d390SJohn Dyson 		printf("vnode_pager_putpages: residual I/O %d at %d\n",
915a316d390SJohn Dyson 			auio.uio_resid, m[0]->pindex);
91626f9a767SRodney W. Grimes 	}
91726f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
9185f55e841SDavid Greenman 		m[i]->busy--;
919f6b04d2bSDavid Greenman 		if (i < ncount) {
92026f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
92126f9a767SRodney W. Grimes 		}
922f6b04d2bSDavid Greenman 		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED))
92324a1cce3SDavid Greenman 			wakeup(m[i]);
92426f9a767SRodney W. Grimes 	}
925f6b04d2bSDavid Greenman 	return rtvals[0];
92626f9a767SRodney W. Grimes }
927f6b04d2bSDavid Greenman 
928f6b04d2bSDavid Greenman struct vnode *
92924a1cce3SDavid Greenman vnode_pager_lock(object)
93024a1cce3SDavid Greenman 	vm_object_t object;
93124a1cce3SDavid Greenman {
93224a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
93324a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
934f6b04d2bSDavid Greenman 			continue;
935f6b04d2bSDavid Greenman 
93624a1cce3SDavid Greenman 		VOP_LOCK(object->handle);
93724a1cce3SDavid Greenman 		return object->handle;
93826f9a767SRodney W. Grimes 	}
93924a1cce3SDavid Greenman 	return NULL;
940f6b04d2bSDavid Greenman }
941