xref: /freebsd/sys/vm/vnode_pager.c (revision efeaf95a41820e4eb661a90cfb59a26e36575784)
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
41efeaf95aSDavid Greenman  *	$Id: vnode_pager.c,v 1.53 1995/11/20 12:19:11 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 
740b8253a7SBruce Evans extern vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_offset_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;
10526f9a767SRodney W. Grimes 	vm_offset_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 		 */
14024a1cce3SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, round_page(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);
14724a1cce3SDavid Greenman 		object->un_pager.vnp.vnp_size = 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
19724a1cce3SDavid Greenman vnode_pager_haspage(object, offset, before, after)
19824a1cce3SDavid Greenman 	vm_object_t object;
199df8bae1dSRodney W. Grimes 	vm_offset_t offset;
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 	 */
21524a1cce3SDavid Greenman 	if ((vp->v_mount == NULL) || (offset >= object->un_pager.vnp.vnp_size))
2164abc71c0SDavid Greenman 		return FALSE;
217df8bae1dSRodney W. Grimes 
218eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
219170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
220170db9c6SJohn Dyson 	reqblock = offset / bsize;
221170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
222170db9c6SJohn Dyson 		after, before);
2230d94caffSDavid Greenman 	if (err)
22424a1cce3SDavid Greenman 		return TRUE;
2256eab77f2SJohn Dyson 	if ( bn == -1)
226ced399eeSJohn Dyson 		return FALSE;
227170db9c6SJohn Dyson 	poff = (offset - (reqblock * bsize)) / PAGE_SIZE;
228170db9c6SJohn Dyson 	if (before) {
229170db9c6SJohn Dyson 		*before *= pagesperblock;
230170db9c6SJohn Dyson 		*before += poff;
231170db9c6SJohn Dyson 	}
232170db9c6SJohn Dyson 	if (after) {
233b1fc01b7SJohn Dyson 		int numafter;
234170db9c6SJohn Dyson 		*after *= pagesperblock;
235b1fc01b7SJohn Dyson 		numafter = pagesperblock - (poff + 1);
236b1fc01b7SJohn Dyson 		if (offset + numafter * PAGE_SIZE > object->un_pager.vnp.vnp_size) {
237b1fc01b7SJohn Dyson 			numafter = (object->un_pager.vnp.vnp_size - offset)/PAGE_SIZE;
238b1fc01b7SJohn Dyson 		}
239b1fc01b7SJohn Dyson 		*after += numafter;
240170db9c6SJohn Dyson 	}
241ced399eeSJohn Dyson 	return TRUE;
242df8bae1dSRodney W. Grimes }
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes /*
245df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
24624a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
247df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
248df8bae1dSRodney W. Grimes  *
249df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
250df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
251df8bae1dSRodney W. Grimes  */
252df8bae1dSRodney W. Grimes void
253df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
254df8bae1dSRodney W. Grimes 	struct vnode *vp;
255df8bae1dSRodney W. Grimes 	u_long nsize;
256df8bae1dSRodney W. Grimes {
25724a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
258df8bae1dSRodney W. Grimes 
25924a1cce3SDavid Greenman 	if (object == NULL)
260df8bae1dSRodney W. Grimes 		return;
261bbc0ec52SDavid Greenman 
262df8bae1dSRodney W. Grimes 	/*
263df8bae1dSRodney W. Grimes 	 * Hasn't changed size
264df8bae1dSRodney W. Grimes 	 */
26524a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
266df8bae1dSRodney W. Grimes 		return;
267bbc0ec52SDavid Greenman 
268df8bae1dSRodney W. Grimes 	/*
269bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
270df8bae1dSRodney W. Grimes 	 */
27124a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
27224a1cce3SDavid Greenman 		if (round_page((vm_offset_t) nsize) < object->un_pager.vnp.vnp_size) {
273df8bae1dSRodney W. Grimes 			vm_object_page_remove(object,
27424a1cce3SDavid Greenman 			    round_page((vm_offset_t) nsize), object->un_pager.vnp.vnp_size, FALSE);
2750d94caffSDavid Greenman 		}
276bbc0ec52SDavid Greenman 		/*
277bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
278bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
279bbc0ec52SDavid Greenman 		 */
280bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
281bbc0ec52SDavid Greenman 			vm_offset_t kva;
282bbc0ec52SDavid Greenman 			vm_page_t m;
283bbc0ec52SDavid Greenman 
284bbc0ec52SDavid Greenman 			m = vm_page_lookup(object, trunc_page((vm_offset_t) nsize));
285bbc0ec52SDavid Greenman 			if (m) {
286bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
287bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
288bbc0ec52SDavid Greenman 				    round_page(nsize) - nsize);
289bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
290bbc0ec52SDavid Greenman 			}
291bbc0ec52SDavid Greenman 		}
292bbc0ec52SDavid Greenman 	}
29324a1cce3SDavid Greenman 	object->un_pager.vnp.vnp_size = (vm_offset_t) nsize;
294bbc0ec52SDavid Greenman 	object->size = round_page(nsize);
295df8bae1dSRodney W. Grimes }
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes void
298df8bae1dSRodney W. Grimes vnode_pager_umount(mp)
299df8bae1dSRodney W. Grimes 	register struct mount *mp;
300df8bae1dSRodney W. Grimes {
30124a1cce3SDavid Greenman 	struct vnode *vp, *nvp;
302df8bae1dSRodney W. Grimes 
30324a1cce3SDavid Greenman loop:
30424a1cce3SDavid Greenman 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
30524a1cce3SDavid Greenman 		/*
30624a1cce3SDavid Greenman 		 * Vnode can be reclaimed by getnewvnode() while we
30724a1cce3SDavid Greenman 		 * traverse the list.
30824a1cce3SDavid Greenman 		 */
30924a1cce3SDavid Greenman 		if (vp->v_mount != mp)
31024a1cce3SDavid Greenman 			goto loop;
31124a1cce3SDavid Greenman 
312df8bae1dSRodney W. Grimes 		/*
313bbc0ec52SDavid Greenman 		 * Save the next pointer now since uncaching may terminate the
31424a1cce3SDavid Greenman 		 * object and render vnode invalid
315df8bae1dSRodney W. Grimes 		 */
31624a1cce3SDavid Greenman 		nvp = vp->v_mntvnodes.le_next;
31724a1cce3SDavid Greenman 
31824a1cce3SDavid Greenman 		if (vp->v_object != NULL) {
319c01a9b8cSDavid Greenman 			VOP_LOCK(vp);
32024a1cce3SDavid Greenman 			vnode_pager_uncache(vp);
321c01a9b8cSDavid Greenman 			VOP_UNLOCK(vp);
322c01a9b8cSDavid Greenman 		}
323df8bae1dSRodney W. Grimes 	}
324df8bae1dSRodney W. Grimes }
325df8bae1dSRodney W. Grimes 
326df8bae1dSRodney W. Grimes /*
327df8bae1dSRodney W. Grimes  * Remove vnode associated object from the object cache.
328c01a9b8cSDavid Greenman  * This routine must be called with the vnode locked.
329df8bae1dSRodney W. Grimes  *
330c01a9b8cSDavid Greenman  * XXX unlock the vnode.
331c01a9b8cSDavid Greenman  * We must do this since uncaching the object may result in its
332c01a9b8cSDavid Greenman  * destruction which may initiate paging activity which may necessitate
333c01a9b8cSDavid Greenman  * re-locking the vnode.
33426f9a767SRodney W. Grimes  */
33524a1cce3SDavid Greenman void
33626f9a767SRodney W. Grimes vnode_pager_uncache(vp)
33724a1cce3SDavid Greenman 	struct vnode *vp;
33826f9a767SRodney W. Grimes {
33924a1cce3SDavid Greenman 	vm_object_t object;
34026f9a767SRodney W. Grimes 
34126f9a767SRodney W. Grimes 	/*
34226f9a767SRodney W. Grimes 	 * Not a mapped vnode
34326f9a767SRodney W. Grimes 	 */
344aa2cabb9SDavid Greenman 	object = vp->v_object;
3458e58bf68SDavid Greenman 	if (object == NULL)
34624a1cce3SDavid Greenman 		return;
3470d94caffSDavid Greenman 
34824a1cce3SDavid Greenman 	vm_object_reference(object);
349c01a9b8cSDavid Greenman 	VOP_UNLOCK(vp);
35026f9a767SRodney W. Grimes 	pager_cache(object, FALSE);
351c01a9b8cSDavid Greenman 	VOP_LOCK(vp);
35224a1cce3SDavid Greenman 	return;
35326f9a767SRodney W. Grimes }
354df8bae1dSRodney W. Grimes 
35526f9a767SRodney W. Grimes 
35626f9a767SRodney W. Grimes void
35726f9a767SRodney W. Grimes vnode_pager_freepage(m)
35826f9a767SRodney W. Grimes 	vm_page_t m;
359df8bae1dSRodney W. Grimes {
36026f9a767SRodney W. Grimes 	PAGE_WAKEUP(m);
36126f9a767SRodney W. Grimes 	vm_page_free(m);
36226f9a767SRodney W. Grimes }
36326f9a767SRodney W. Grimes 
36426f9a767SRodney W. Grimes /*
36526f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
36626f9a767SRodney W. Grimes  * file address
36726f9a767SRodney W. Grimes  */
36826f9a767SRodney W. Grimes vm_offset_t
369efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
37026f9a767SRodney W. Grimes 	struct vnode *vp;
37126f9a767SRodney W. Grimes 	vm_offset_t address;
372efc68ce1SDavid Greenman 	int *run;
37326f9a767SRodney W. Grimes {
37426f9a767SRodney W. Grimes 	int rtaddress;
37526f9a767SRodney W. Grimes 	int bsize;
37626f9a767SRodney W. Grimes 	vm_offset_t block;
37726f9a767SRodney W. Grimes 	struct vnode *rtvp;
37826f9a767SRodney W. Grimes 	int err;
37926f9a767SRodney W. Grimes 	int vblock, voffset;
38026f9a767SRodney W. Grimes 
3810d94caffSDavid Greenman 	if ((int) address < 0)
3820d94caffSDavid Greenman 		return -1;
3830d94caffSDavid Greenman 
3842c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3852c4488fcSJohn Dyson 		return -1;
3862c4488fcSJohn Dyson 
38726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
38826f9a767SRodney W. Grimes 	vblock = address / bsize;
38926f9a767SRodney W. Grimes 	voffset = address % bsize;
39026f9a767SRodney W. Grimes 
391c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
39226f9a767SRodney W. Grimes 
393efc68ce1SDavid Greenman 	if (err || (block == -1))
39426f9a767SRodney W. Grimes 		rtaddress = -1;
395efc68ce1SDavid Greenman 	else {
396187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
397efc68ce1SDavid Greenman 		if( run) {
398efc68ce1SDavid Greenman 			*run += 1;
399efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
400efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
401efc68ce1SDavid Greenman 		}
402efc68ce1SDavid Greenman 	}
40326f9a767SRodney W. Grimes 
40426f9a767SRodney W. Grimes 	return rtaddress;
40526f9a767SRodney W. Grimes }
40626f9a767SRodney W. Grimes 
40726f9a767SRodney W. Grimes /*
40826f9a767SRodney W. Grimes  * interrupt routine for I/O completion
40926f9a767SRodney W. Grimes  */
41026f9a767SRodney W. Grimes void
41126f9a767SRodney W. Grimes vnode_pager_iodone(bp)
41226f9a767SRodney W. Grimes 	struct buf *bp;
41326f9a767SRodney W. Grimes {
41426f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
41524a1cce3SDavid Greenman 	wakeup(bp);
41626f9a767SRodney W. Grimes }
41726f9a767SRodney W. Grimes 
41826f9a767SRodney W. Grimes /*
41926f9a767SRodney W. Grimes  * small block file system vnode pager input
42026f9a767SRodney W. Grimes  */
42126f9a767SRodney W. Grimes int
42224a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
42324a1cce3SDavid Greenman 	vm_object_t object;
42426f9a767SRodney W. Grimes 	vm_page_t m;
42526f9a767SRodney W. Grimes {
42626f9a767SRodney W. Grimes 	int i;
42726f9a767SRodney W. Grimes 	int s;
42826f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
42926f9a767SRodney W. Grimes 	struct buf *bp;
43026f9a767SRodney W. Grimes 	vm_offset_t kva;
43126f9a767SRodney W. Grimes 	int fileaddr;
43226f9a767SRodney W. Grimes 	vm_offset_t bsize;
43326f9a767SRodney W. Grimes 	int error = 0;
43426f9a767SRodney W. Grimes 
43524a1cce3SDavid Greenman 	vp = object->handle;
4362c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4372c4488fcSJohn Dyson 		return VM_PAGER_BAD;
4382c4488fcSJohn Dyson 
43926f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
44026f9a767SRodney W. Grimes 
4410bdb7528SDavid Greenman 
442c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
44326f9a767SRodney W. Grimes 
44426f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
44526f9a767SRodney W. Grimes 
44626f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
447bbc0ec52SDavid Greenman 
4480d94caffSDavid Greenman 		if ((vm_page_bits(m->offset + i * bsize, bsize) & m->valid))
44926f9a767SRodney W. Grimes 			continue;
45026f9a767SRodney W. Grimes 
451efc68ce1SDavid Greenman 		fileaddr = vnode_pager_addr(vp, m->offset + i * bsize, (int *)0);
45226f9a767SRodney W. Grimes 		if (fileaddr != -1) {
45326f9a767SRodney W. Grimes 			bp = getpbuf();
45426f9a767SRodney W. Grimes 
45526f9a767SRodney W. Grimes 			/* build a minimal buffer header */
45626f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
45726f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
45826f9a767SRodney W. Grimes 			bp->b_proc = curproc;
45926f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
46026f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
46126f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
46226f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
46326f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
46426f9a767SRodney W. Grimes 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
465187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4660d94caffSDavid Greenman 			pbgetvp(dp, bp);
46726f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
46826f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
46926f9a767SRodney W. Grimes 
47026f9a767SRodney W. Grimes 			/* do the input */
47126f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
47226f9a767SRodney W. Grimes 
47326f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
47426f9a767SRodney W. Grimes 
47526f9a767SRodney W. Grimes 			s = splbio();
47626f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
477aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
47826f9a767SRodney W. Grimes 			}
47926f9a767SRodney W. Grimes 			splx(s);
48026f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
48126f9a767SRodney W. Grimes 				error = EIO;
48226f9a767SRodney W. Grimes 
48326f9a767SRodney W. Grimes 			/*
48426f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
48526f9a767SRodney W. Grimes 			 */
48626f9a767SRodney W. Grimes 			relpbuf(bp);
48726f9a767SRodney W. Grimes 			if (error)
48826f9a767SRodney W. Grimes 				break;
4890d94caffSDavid Greenman 
490170db9c6SJohn Dyson 			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
49126f9a767SRodney W. Grimes 		} else {
492b1fc01b7SJohn Dyson 			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
49326f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
49426f9a767SRodney W. Grimes 		}
49526f9a767SRodney W. Grimes 	}
49626f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
4970d94caffSDavid Greenman 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
498b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
49926f9a767SRodney W. Grimes 	if (error) {
500a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
50126f9a767SRodney W. Grimes 	}
50226f9a767SRodney W. Grimes 	return VM_PAGER_OK;
50326f9a767SRodney W. Grimes 
50426f9a767SRodney W. Grimes }
50526f9a767SRodney W. Grimes 
50626f9a767SRodney W. Grimes 
50726f9a767SRodney W. Grimes /*
50826f9a767SRodney W. Grimes  * old style vnode pager output routine
50926f9a767SRodney W. Grimes  */
51026f9a767SRodney W. Grimes int
51124a1cce3SDavid Greenman vnode_pager_input_old(object, m)
51224a1cce3SDavid Greenman 	vm_object_t object;
51326f9a767SRodney W. Grimes 	vm_page_t m;
51426f9a767SRodney W. Grimes {
515df8bae1dSRodney W. Grimes 	struct uio auio;
516df8bae1dSRodney W. Grimes 	struct iovec aiov;
51726f9a767SRodney W. Grimes 	int error;
51826f9a767SRodney W. Grimes 	int size;
51926f9a767SRodney W. Grimes 	vm_offset_t kva;
520df8bae1dSRodney W. Grimes 
52126f9a767SRodney W. Grimes 	error = 0;
522bbc0ec52SDavid Greenman 
523df8bae1dSRodney W. Grimes 	/*
52426f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
52526f9a767SRodney W. Grimes 	 */
52624a1cce3SDavid Greenman 	if (m->offset >= object->un_pager.vnp.vnp_size) {
52726f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
52826f9a767SRodney W. Grimes 	} else {
52926f9a767SRodney W. Grimes 		size = PAGE_SIZE;
53024a1cce3SDavid Greenman 		if (m->offset + size > object->un_pager.vnp.vnp_size)
53124a1cce3SDavid Greenman 			size = object->un_pager.vnp.vnp_size - m->offset;
5320bdb7528SDavid Greenman 
53326f9a767SRodney W. Grimes 		/*
534df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
535df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
536df8bae1dSRodney W. Grimes 		 */
53726f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
5380bdb7528SDavid Greenman 
539df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
540df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
541df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
542df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
5430d94caffSDavid Greenman 		auio.uio_offset = m->offset;
544df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
54526f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
546df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
547df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
54826f9a767SRodney W. Grimes 
54924a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
550df8bae1dSRodney W. Grimes 		if (!error) {
551df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 			if (count == 0)
554df8bae1dSRodney W. Grimes 				error = EINVAL;
55526f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
55626f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
557df8bae1dSRodney W. Grimes 		}
55826f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
559df8bae1dSRodney W. Grimes 	}
56026f9a767SRodney W. Grimes 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5610d94caffSDavid Greenman 	m->dirty = 0;
562b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
563a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
56426f9a767SRodney W. Grimes }
56526f9a767SRodney W. Grimes 
56626f9a767SRodney W. Grimes /*
56726f9a767SRodney W. Grimes  * generic vnode pager input routine
56826f9a767SRodney W. Grimes  */
569170db9c6SJohn Dyson 
57026f9a767SRodney W. Grimes int
57124a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
57226f9a767SRodney W. Grimes 	vm_object_t object;
57324a1cce3SDavid Greenman 	vm_page_t *m;
57424a1cce3SDavid Greenman 	int count;
57524a1cce3SDavid Greenman 	int reqpage;
57624a1cce3SDavid Greenman {
577170db9c6SJohn Dyson 	int rtval;
578170db9c6SJohn Dyson 	struct vnode *vp;
579170db9c6SJohn Dyson 	vp = object->handle;
5802c4488fcSJohn Dyson 	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
581170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
5820b8253a7SBruce Evans 		return vnode_pager_leaf_getpages(object, m, count, reqpage);
583170db9c6SJohn Dyson 	else
584170db9c6SJohn Dyson 		return rtval;
585170db9c6SJohn Dyson }
586170db9c6SJohn Dyson 
587170db9c6SJohn Dyson static int
588170db9c6SJohn Dyson vnode_pager_leaf_getpages(object, m, count, reqpage)
589170db9c6SJohn Dyson 	vm_object_t object;
590170db9c6SJohn Dyson 	vm_page_t *m;
591170db9c6SJohn Dyson 	int count;
592170db9c6SJohn Dyson 	int reqpage;
593170db9c6SJohn Dyson {
59424a1cce3SDavid Greenman 	vm_offset_t kva, foff;
59524a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
59626f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
597efc68ce1SDavid Greenman 	int runpg;
598efc68ce1SDavid Greenman 	int runend;
5990bdb7528SDavid Greenman 	struct buf *bp;
60026f9a767SRodney W. Grimes 	int s;
60126f9a767SRodney W. Grimes 	int error = 0;
60226f9a767SRodney W. Grimes 
60324a1cce3SDavid Greenman 	vp = object->handle;
6042c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
6052c4488fcSJohn Dyson 		return VM_PAGER_BAD;
6062c4488fcSJohn Dyson 
60726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
60826f9a767SRodney W. Grimes 
60926f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
610bbc0ec52SDavid Greenman 
61126f9a767SRodney W. Grimes 	/*
612bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
613bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
61426f9a767SRodney W. Grimes 	 */
6150d94caffSDavid Greenman 	foff = m[reqpage]->offset;
616bbc0ec52SDavid Greenman 
61726f9a767SRodney W. Grimes 	/*
61816f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
61926f9a767SRodney W. Grimes 	 */
620c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
62126f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
62226f9a767SRodney W. Grimes 			if (i != reqpage) {
62326f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
62426f9a767SRodney W. Grimes 			}
62526f9a767SRodney W. Grimes 		}
626976e77fcSDavid Greenman 		cnt.v_vnodein++;
627976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
62824a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
629bbc0ec52SDavid Greenman 
63026f9a767SRodney W. Grimes 		/*
63126f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
63226f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
63326f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
63426f9a767SRodney W. Grimes 		 */
63526f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
63626f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
63726f9a767SRodney W. Grimes 
63826f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
63926f9a767SRodney W. Grimes 			if (i != reqpage) {
64026f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
64126f9a767SRodney W. Grimes 			}
64226f9a767SRodney W. Grimes 		}
643976e77fcSDavid Greenman 		cnt.v_vnodein++;
644976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
64524a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
64626f9a767SRodney W. Grimes 	}
64726f9a767SRodney W. Grimes 	/*
6480d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
6490d94caffSDavid Greenman 	 * then, the entire page is valid --
6500d94caffSDavid Greenman 	 */
6510d94caffSDavid Greenman 	if (m[reqpage]->valid) {
6520d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
6530d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6540d94caffSDavid Greenman 			if (i != reqpage)
6550d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6560d94caffSDavid Greenman 		}
6570d94caffSDavid Greenman 		return VM_PAGER_OK;
6580d94caffSDavid Greenman 	}
6590bdb7528SDavid Greenman 
6600d94caffSDavid Greenman 	/*
66126f9a767SRodney W. Grimes 	 * here on direct device I/O
66226f9a767SRodney W. Grimes 	 */
66326f9a767SRodney W. Grimes 
664efc68ce1SDavid Greenman 	firstaddr = -1;
66526f9a767SRodney W. Grimes 	/*
666efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
66726f9a767SRodney W. Grimes 	 */
668efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
669efc68ce1SDavid Greenman 		firstaddr = vnode_pager_addr(vp, m[i]->offset, &runpg);
670efc68ce1SDavid Greenman 		if (firstaddr == -1) {
67124a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
67224a1cce3SDavid Greenman 				panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
67324a1cce3SDavid Greenman 			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
674efc68ce1SDavid Greenman 			}
67526f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
676efc68ce1SDavid Greenman 			runend = i + 1;
677efc68ce1SDavid Greenman 			first = runend;
678efc68ce1SDavid Greenman 			continue;
679efc68ce1SDavid Greenman 		}
680efc68ce1SDavid Greenman 		runend = i + runpg;
681efc68ce1SDavid Greenman 		if (runend <= reqpage) {
682efc68ce1SDavid Greenman 			int j;
683efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
684efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
685efc68ce1SDavid Greenman 			}
68626f9a767SRodney W. Grimes 		} else {
687efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
688efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
68926f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
690efc68ce1SDavid Greenman 				count = first + runpg;
69126f9a767SRodney W. Grimes 			}
692efc68ce1SDavid Greenman 			break;
69326f9a767SRodney W. Grimes 		}
694efc68ce1SDavid Greenman 		first = runend;
695efc68ce1SDavid Greenman 	}
69626f9a767SRodney W. Grimes 
69726f9a767SRodney W. Grimes 	/*
698bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
699bbc0ec52SDavid Greenman 	 * to be zero based...
70026f9a767SRodney W. Grimes 	 */
70126f9a767SRodney W. Grimes 	if (first != 0) {
70226f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
70326f9a767SRodney W. Grimes 			m[i - first] = m[i];
70426f9a767SRodney W. Grimes 		}
70526f9a767SRodney W. Grimes 		count -= first;
70626f9a767SRodney W. Grimes 		reqpage -= first;
70726f9a767SRodney W. Grimes 	}
708efc68ce1SDavid Greenman 
70926f9a767SRodney W. Grimes 	/*
71026f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
71126f9a767SRodney W. Grimes 	 */
7120d94caffSDavid Greenman 	foff = m[0]->offset;
71326f9a767SRodney W. Grimes 
71426f9a767SRodney W. Grimes 	/*
71526f9a767SRodney W. Grimes 	 * calculate the size of the transfer
71626f9a767SRodney W. Grimes 	 */
71726f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
71824a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
71924a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
72026f9a767SRodney W. Grimes 
72126f9a767SRodney W. Grimes 	/*
72226f9a767SRodney W. Grimes 	 * round up physical size for real devices
72326f9a767SRodney W. Grimes 	 */
72426f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
72526f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
72626f9a767SRodney W. Grimes 
7276d40c3d3SDavid Greenman 	bp = getpbuf();
72816f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
72916f62314SDavid Greenman 
73026f9a767SRodney W. Grimes 	/*
73126f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
73226f9a767SRodney W. Grimes 	 */
73316f62314SDavid Greenman 	pmap_qenter(kva, m, count);
73426f9a767SRodney W. Grimes 
73526f9a767SRodney W. Grimes 	/* build a minimal buffer header */
73626f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
73726f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
73826f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
73926f9a767SRodney W. Grimes 	bp->b_proc = curproc;
74026f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
74126f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
74226f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
74326f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
74426f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
745187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7460d94caffSDavid Greenman 	pbgetvp(dp, bp);
74726f9a767SRodney W. Grimes 	bp->b_bcount = size;
74826f9a767SRodney W. Grimes 	bp->b_bufsize = size;
74926f9a767SRodney W. Grimes 
750976e77fcSDavid Greenman 	cnt.v_vnodein++;
751976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
752976e77fcSDavid Greenman 
75326f9a767SRodney W. Grimes 	/* do the input */
75426f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
755976e77fcSDavid Greenman 
75626f9a767SRodney W. Grimes 	s = splbio();
75726f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
75826f9a767SRodney W. Grimes 
75926f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
760aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
76126f9a767SRodney W. Grimes 	}
76226f9a767SRodney W. Grimes 	splx(s);
76326f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
76426f9a767SRodney W. Grimes 		error = EIO;
76526f9a767SRodney W. Grimes 
76626f9a767SRodney W. Grimes 	if (!error) {
76726f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
76826f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
76926f9a767SRodney W. Grimes 	}
77016f62314SDavid Greenman 	pmap_qremove(kva, count);
77126f9a767SRodney W. Grimes 
77226f9a767SRodney W. Grimes 	/*
77326f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
77426f9a767SRodney W. Grimes 	 */
77526f9a767SRodney W. Grimes 	relpbuf(bp);
77626f9a767SRodney W. Grimes 
77726f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
778fff93ab6SDavid Greenman 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
7790d94caffSDavid Greenman 		m[i]->dirty = 0;
7800d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
781b1fc01b7SJohn Dyson 		m[i]->flags &= ~PG_ZERO;
78226f9a767SRodney W. Grimes 		if (i != reqpage) {
783bbc0ec52SDavid Greenman 
78426f9a767SRodney W. Grimes 			/*
785bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
786bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
787bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
788bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
789bbc0ec52SDavid Greenman 			 * deactivating pages is best.
79026f9a767SRodney W. Grimes 			 */
791bbc0ec52SDavid Greenman 
79226f9a767SRodney W. Grimes 			/*
793bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
794bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
79526f9a767SRodney W. Grimes 			 */
79626f9a767SRodney W. Grimes 			if (!error) {
79726f9a767SRodney W. Grimes 				vm_page_deactivate(m[i]);
79826f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
79926f9a767SRodney W. Grimes 			} else {
80026f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
80126f9a767SRodney W. Grimes 			}
80226f9a767SRodney W. Grimes 		}
80326f9a767SRodney W. Grimes 	}
80426f9a767SRodney W. Grimes 	if (error) {
80524a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
80626f9a767SRodney W. Grimes 	}
807a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
80826f9a767SRodney W. Grimes }
80926f9a767SRodney W. Grimes 
810170db9c6SJohn Dyson int
811170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
812170db9c6SJohn Dyson 	vm_object_t object;
813170db9c6SJohn Dyson 	vm_page_t *m;
814170db9c6SJohn Dyson 	int count;
815170db9c6SJohn Dyson 	boolean_t sync;
816170db9c6SJohn Dyson 	int *rtvals;
817170db9c6SJohn Dyson {
818170db9c6SJohn Dyson 	int rtval;
819170db9c6SJohn Dyson 	struct vnode *vp;
820170db9c6SJohn Dyson 	vp = object->handle;
8212c4488fcSJohn Dyson 	rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
822170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
8230b8253a7SBruce Evans 		return vnode_pager_leaf_putpages(object, m, count, sync, rtvals);
824170db9c6SJohn Dyson 	else
825170db9c6SJohn Dyson 		return rtval;
826170db9c6SJohn Dyson }
827170db9c6SJohn Dyson 
82826f9a767SRodney W. Grimes /*
82926f9a767SRodney W. Grimes  * generic vnode pager output routine
83026f9a767SRodney W. Grimes  */
831170db9c6SJohn Dyson static int
832170db9c6SJohn Dyson vnode_pager_leaf_putpages(object, m, count, sync, rtvals)
83324a1cce3SDavid Greenman 	vm_object_t object;
83426f9a767SRodney W. Grimes 	vm_page_t *m;
83526f9a767SRodney W. Grimes 	int count;
83624a1cce3SDavid Greenman 	boolean_t sync;
83726f9a767SRodney W. Grimes 	int *rtvals;
83826f9a767SRodney W. Grimes {
839f6b04d2bSDavid Greenman 	int i;
84026f9a767SRodney W. Grimes 
841f6b04d2bSDavid Greenman 	struct vnode *vp;
842f6b04d2bSDavid Greenman 	int maxsize, ncount;
843f6b04d2bSDavid Greenman 	struct uio auio;
844f6b04d2bSDavid Greenman 	struct iovec aiov;
845f6b04d2bSDavid Greenman 	int error;
84626f9a767SRodney W. Grimes 
84724a1cce3SDavid Greenman 	vp = object->handle;;
84826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
84926f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
85026f9a767SRodney W. Grimes 
8510d94caffSDavid Greenman 	if ((int) m[0]->offset < 0) {
85224a1cce3SDavid Greenman 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->offset, m[0]->dirty);
853f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
854f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
8550d94caffSDavid Greenman 	}
8560bdb7528SDavid Greenman 
857f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
858f6b04d2bSDavid Greenman 	ncount = count;
85926f9a767SRodney W. Grimes 
86024a1cce3SDavid Greenman 	if (maxsize + m[0]->offset > object->un_pager.vnp.vnp_size) {
86124a1cce3SDavid Greenman 		if (object->un_pager.vnp.vnp_size > m[0]->offset)
86224a1cce3SDavid Greenman 			maxsize = object->un_pager.vnp.vnp_size - m[0]->offset;
8635f55e841SDavid Greenman 		else
8645f55e841SDavid Greenman 			maxsize = 0;
865f6b04d2bSDavid Greenman 		ncount = (maxsize + PAGE_SIZE - 1) / PAGE_SIZE;
866f6b04d2bSDavid Greenman 		if (ncount < count) {
867f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
868f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
869f6b04d2bSDavid Greenman 			}
870f6b04d2bSDavid Greenman 			if (ncount == 0) {
8713af76890SPoul-Henning Kamp 				printf("vnode_pager_putpages: write past end of file: %ld, %ld\n",
8723af76890SPoul-Henning Kamp 					m[0]->offset,
8733af76890SPoul-Henning Kamp 					object->un_pager.vnp.vnp_size);
87426f9a767SRodney W. Grimes 				return rtvals[0];
87526f9a767SRodney W. Grimes 			}
876f6b04d2bSDavid Greenman 		}
877f6b04d2bSDavid Greenman 	}
87826f9a767SRodney W. Grimes 
87926f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8805f55e841SDavid Greenman 		m[i]->busy++;
881f6b04d2bSDavid Greenman 		m[i]->flags &= ~PG_BUSY;
88226f9a767SRodney W. Grimes 	}
883f6b04d2bSDavid Greenman 
884f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
885f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
886f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
887f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
888f6b04d2bSDavid Greenman 	auio.uio_offset = m[0]->offset;
889f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
890f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
891f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
892f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
893f6b04d2bSDavid Greenman 	error = VOP_WRITE(vp, &auio, IO_VMIO, curproc->p_ucred);
894976e77fcSDavid Greenman 	cnt.v_vnodeout++;
895f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
896f6b04d2bSDavid Greenman 
897f6b04d2bSDavid Greenman 	if (error) {
89824a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
899f6b04d2bSDavid Greenman 	}
900f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
9013af76890SPoul-Henning Kamp 		printf("vnode_pager_putpages: residual I/O %d at %ld\n",
9023af76890SPoul-Henning Kamp 			auio.uio_resid, m[0]->offset);
90326f9a767SRodney W. Grimes 	}
90426f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
9055f55e841SDavid Greenman 		m[i]->busy--;
906f6b04d2bSDavid Greenman 		if (i < ncount) {
90726f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
90826f9a767SRodney W. Grimes 		}
909f6b04d2bSDavid Greenman 		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED))
91024a1cce3SDavid Greenman 			wakeup(m[i]);
91126f9a767SRodney W. Grimes 	}
912f6b04d2bSDavid Greenman 	return rtvals[0];
91326f9a767SRodney W. Grimes }
914f6b04d2bSDavid Greenman 
915f6b04d2bSDavid Greenman struct vnode *
91624a1cce3SDavid Greenman vnode_pager_lock(object)
91724a1cce3SDavid Greenman 	vm_object_t object;
91824a1cce3SDavid Greenman {
91924a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
92024a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
921f6b04d2bSDavid Greenman 			continue;
922f6b04d2bSDavid Greenman 
92324a1cce3SDavid Greenman 		VOP_LOCK(object->handle);
92424a1cce3SDavid Greenman 		return object->handle;
92526f9a767SRodney W. Grimes 	}
92624a1cce3SDavid Greenman 	return NULL;
927f6b04d2bSDavid Greenman }
928