xref: /freebsd/sys/vm/vnode_pager.c (revision 2c4488fce37090fbfb35b31f1a0390f4fa6da3fb)
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
412c4488fcSJohn Dyson  *	$Id: vnode_pager.c,v 1.50 1995/10/19 21:35:03 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>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <vm/vm.h>
65df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
6624a1cce3SDavid Greenman #include <vm/vm_pager.h>
67df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
7024a1cce3SDavid Greenman 	NULL,
71df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
72df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
7324a1cce3SDavid Greenman 	vnode_pager_getpages,
7424a1cce3SDavid Greenman 	vnode_pager_putpages,
7524a1cce3SDavid Greenman 	vnode_pager_haspage,
7624a1cce3SDavid Greenman 	NULL
77df8bae1dSRodney W. Grimes };
78df8bae1dSRodney W. Grimes 
79170db9c6SJohn Dyson static int vnode_pager_leaf_getpages();
80170db9c6SJohn Dyson 
81170db9c6SJohn Dyson static int vnode_pager_leaf_putpages();
82df8bae1dSRodney W. Grimes /*
83df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
84df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
85df8bae1dSRodney W. Grimes  */
8624a1cce3SDavid Greenman vm_object_t
8726f9a767SRodney W. Grimes vnode_pager_alloc(handle, size, prot, offset)
88ee3a64c9SDavid Greenman 	void *handle;
89df8bae1dSRodney W. Grimes 	vm_size_t size;
90df8bae1dSRodney W. Grimes 	vm_prot_t prot;
9126f9a767SRodney W. Grimes 	vm_offset_t offset;
92df8bae1dSRodney W. Grimes {
9306cb7259SDavid Greenman 	vm_object_t object;
94df8bae1dSRodney W. Grimes 	struct vnode *vp;
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes 	/*
97df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
98df8bae1dSRodney W. Grimes 	 */
99df8bae1dSRodney W. Grimes 	if (handle == NULL)
100df8bae1dSRodney W. Grimes 		return (NULL);
101df8bae1dSRodney W. Grimes 
102df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
10339d38f93SDavid Greenman 
10439d38f93SDavid Greenman 	/*
10539d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
10639d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
10739d38f93SDavid Greenman 	 */
10839d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
10939d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
11039d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
11139d38f93SDavid Greenman 	}
11239d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
11339d38f93SDavid Greenman 
11439d38f93SDavid Greenman 	/*
11539d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
11639d38f93SDavid Greenman 	 * go away.
11739d38f93SDavid Greenman 	 */
11824a1cce3SDavid Greenman 	while (((object = vp->v_object) != NULL) && (object->flags & OBJ_DEAD)) {
119aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
12024a1cce3SDavid Greenman 	}
1210d94caffSDavid Greenman 
12224a1cce3SDavid Greenman 	if (object == NULL) {
123df8bae1dSRodney W. Grimes 		/*
124df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
125df8bae1dSRodney W. Grimes 		 */
12624a1cce3SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, round_page(size));
1274bb62461SDavid Greenman 		object->flags = OBJ_CANPERSIST;
128bbc0ec52SDavid Greenman 
129df8bae1dSRodney W. Grimes 		/*
13024a1cce3SDavid Greenman 		 * Hold a reference to the vnode and initialize object data.
131df8bae1dSRodney W. Grimes 		 */
132df8bae1dSRodney W. Grimes 		VREF(vp);
13324a1cce3SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
13426f9a767SRodney W. Grimes 
13524a1cce3SDavid Greenman 		object->handle = handle;
13624a1cce3SDavid Greenman 		vp->v_object = object;
137df8bae1dSRodney W. Grimes 	} else {
138df8bae1dSRodney W. Grimes 		/*
13924a1cce3SDavid Greenman 		 * vm_object_reference() will remove the object from the cache if
14024a1cce3SDavid Greenman 		 * found and gain a reference to the object.
141df8bae1dSRodney W. Grimes 		 */
14224a1cce3SDavid Greenman 		vm_object_reference(object);
143df8bae1dSRodney W. Grimes 	}
14439d38f93SDavid Greenman 
145f6b04d2bSDavid Greenman 	if (vp->v_type == VREG)
146f6b04d2bSDavid Greenman 		vp->v_flag |= VVMIO;
14739d38f93SDavid Greenman 
14839d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
14939d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
15039d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
15139d38f93SDavid Greenman 		wakeup(vp);
15239d38f93SDavid Greenman 	}
15324a1cce3SDavid Greenman 	return (object);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
15626f9a767SRodney W. Grimes void
15724a1cce3SDavid Greenman vnode_pager_dealloc(object)
1580d94caffSDavid Greenman 	vm_object_t object;
15924a1cce3SDavid Greenman {
16024a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
161df8bae1dSRodney W. Grimes 
16224a1cce3SDavid Greenman 	if (vp == NULL)
16324a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
16424a1cce3SDavid Greenman 
16524a1cce3SDavid Greenman 	if (object->paging_in_progress) {
1660d94caffSDavid Greenman 		int s = splbio();
1670d94caffSDavid Greenman 		while (object->paging_in_progress) {
168c0503609SDavid Greenman 			object->flags |= OBJ_PIPWNT;
1690d94caffSDavid Greenman 			tsleep(object, PVM, "vnpdea", 0);
1700d94caffSDavid Greenman 		}
1710d94caffSDavid Greenman 		splx(s);
17224a1cce3SDavid Greenman 	}
17324a1cce3SDavid Greenman 
17424a1cce3SDavid Greenman 	object->handle = NULL;
1750d94caffSDavid Greenman 
176aa2cabb9SDavid Greenman 	vp->v_object = NULL;
1778e58bf68SDavid Greenman 	vp->v_flag &= ~(VTEXT | VVMIO);
17800072442SDavid Greenman 	vp->v_flag |= VAGE;
179df8bae1dSRodney W. Grimes 	vrele(vp);
180df8bae1dSRodney W. Grimes }
18126f9a767SRodney W. Grimes 
18226f9a767SRodney W. Grimes boolean_t
18324a1cce3SDavid Greenman vnode_pager_haspage(object, offset, before, after)
18424a1cce3SDavid Greenman 	vm_object_t object;
185df8bae1dSRodney W. Grimes 	vm_offset_t offset;
18624a1cce3SDavid Greenman 	int *before;
18724a1cce3SDavid Greenman 	int *after;
188df8bae1dSRodney W. Grimes {
18924a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
190df8bae1dSRodney W. Grimes 	daddr_t bn;
1912c4488fcSJohn Dyson 	int err, run;
192170db9c6SJohn Dyson 	daddr_t reqblock;
1932c4488fcSJohn Dyson 	int poff;
1942c4488fcSJohn Dyson 	int bsize;
1952c4488fcSJohn Dyson 	int pagesperblock;
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes 	/*
1980d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
1990d94caffSDavid Greenman 	 * not have the page.
200df8bae1dSRodney W. Grimes 	 */
20124a1cce3SDavid Greenman 	if ((vp->v_mount == NULL) || (offset >= object->un_pager.vnp.vnp_size))
2024abc71c0SDavid Greenman 		return FALSE;
203df8bae1dSRodney W. Grimes 
204eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
205170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
206170db9c6SJohn Dyson 	reqblock = offset / bsize;
207170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
208170db9c6SJohn Dyson 		after, before);
2090d94caffSDavid Greenman 	if (err)
21024a1cce3SDavid Greenman 		return TRUE;
2116eab77f2SJohn Dyson 	if ( bn == -1)
212ced399eeSJohn Dyson 		return FALSE;
213170db9c6SJohn Dyson 	poff = (offset - (reqblock * bsize)) / PAGE_SIZE;
214170db9c6SJohn Dyson 	if (before) {
215170db9c6SJohn Dyson 		*before *= pagesperblock;
216170db9c6SJohn Dyson 		*before += poff;
217170db9c6SJohn Dyson 	}
218170db9c6SJohn Dyson 	if (after) {
219b1fc01b7SJohn Dyson 		int numafter;
220170db9c6SJohn Dyson 		*after *= pagesperblock;
221b1fc01b7SJohn Dyson 		numafter = pagesperblock - (poff + 1);
222b1fc01b7SJohn Dyson 		if (offset + numafter * PAGE_SIZE > object->un_pager.vnp.vnp_size) {
223b1fc01b7SJohn Dyson 			numafter = (object->un_pager.vnp.vnp_size - offset)/PAGE_SIZE;
224b1fc01b7SJohn Dyson 		}
225b1fc01b7SJohn Dyson 		*after += numafter;
226170db9c6SJohn Dyson 	}
227ced399eeSJohn Dyson 	return TRUE;
228df8bae1dSRodney W. Grimes }
229df8bae1dSRodney W. Grimes 
230df8bae1dSRodney W. Grimes /*
231df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
23224a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
233df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
234df8bae1dSRodney W. Grimes  *
235df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
236df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
237df8bae1dSRodney W. Grimes  */
238df8bae1dSRodney W. Grimes void
239df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
240df8bae1dSRodney W. Grimes 	struct vnode *vp;
241df8bae1dSRodney W. Grimes 	u_long nsize;
242df8bae1dSRodney W. Grimes {
24324a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
244df8bae1dSRodney W. Grimes 
24524a1cce3SDavid Greenman 	if (object == NULL)
246df8bae1dSRodney W. Grimes 		return;
247bbc0ec52SDavid Greenman 
248df8bae1dSRodney W. Grimes 	/*
249df8bae1dSRodney W. Grimes 	 * Hasn't changed size
250df8bae1dSRodney W. Grimes 	 */
25124a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
252df8bae1dSRodney W. Grimes 		return;
253bbc0ec52SDavid Greenman 
254df8bae1dSRodney W. Grimes 	/*
255bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
256df8bae1dSRodney W. Grimes 	 */
25724a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
25824a1cce3SDavid Greenman 		if (round_page((vm_offset_t) nsize) < object->un_pager.vnp.vnp_size) {
259df8bae1dSRodney W. Grimes 			vm_object_page_remove(object,
26024a1cce3SDavid Greenman 			    round_page((vm_offset_t) nsize), object->un_pager.vnp.vnp_size, FALSE);
2610d94caffSDavid Greenman 		}
262bbc0ec52SDavid Greenman 		/*
263bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
264bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
265bbc0ec52SDavid Greenman 		 */
266bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
267bbc0ec52SDavid Greenman 			vm_offset_t kva;
268bbc0ec52SDavid Greenman 			vm_page_t m;
269bbc0ec52SDavid Greenman 
270bbc0ec52SDavid Greenman 			m = vm_page_lookup(object, trunc_page((vm_offset_t) nsize));
271bbc0ec52SDavid Greenman 			if (m) {
272bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
273bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
274bbc0ec52SDavid Greenman 				    round_page(nsize) - nsize);
275bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
276bbc0ec52SDavid Greenman 			}
277bbc0ec52SDavid Greenman 		}
278bbc0ec52SDavid Greenman 	}
27924a1cce3SDavid Greenman 	object->un_pager.vnp.vnp_size = (vm_offset_t) nsize;
280bbc0ec52SDavid Greenman 	object->size = round_page(nsize);
281df8bae1dSRodney W. Grimes }
282df8bae1dSRodney W. Grimes 
283df8bae1dSRodney W. Grimes void
284df8bae1dSRodney W. Grimes vnode_pager_umount(mp)
285df8bae1dSRodney W. Grimes 	register struct mount *mp;
286df8bae1dSRodney W. Grimes {
28724a1cce3SDavid Greenman 	struct vnode *vp, *nvp;
288df8bae1dSRodney W. Grimes 
28924a1cce3SDavid Greenman loop:
29024a1cce3SDavid Greenman 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
29124a1cce3SDavid Greenman 		/*
29224a1cce3SDavid Greenman 		 * Vnode can be reclaimed by getnewvnode() while we
29324a1cce3SDavid Greenman 		 * traverse the list.
29424a1cce3SDavid Greenman 		 */
29524a1cce3SDavid Greenman 		if (vp->v_mount != mp)
29624a1cce3SDavid Greenman 			goto loop;
29724a1cce3SDavid Greenman 
298df8bae1dSRodney W. Grimes 		/*
299bbc0ec52SDavid Greenman 		 * Save the next pointer now since uncaching may terminate the
30024a1cce3SDavid Greenman 		 * object and render vnode invalid
301df8bae1dSRodney W. Grimes 		 */
30224a1cce3SDavid Greenman 		nvp = vp->v_mntvnodes.le_next;
30324a1cce3SDavid Greenman 
30424a1cce3SDavid Greenman 		if (vp->v_object != NULL) {
305c01a9b8cSDavid Greenman 			VOP_LOCK(vp);
30624a1cce3SDavid Greenman 			vnode_pager_uncache(vp);
307c01a9b8cSDavid Greenman 			VOP_UNLOCK(vp);
308c01a9b8cSDavid Greenman 		}
309df8bae1dSRodney W. Grimes 	}
310df8bae1dSRodney W. Grimes }
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes /*
313df8bae1dSRodney W. Grimes  * Remove vnode associated object from the object cache.
314c01a9b8cSDavid Greenman  * This routine must be called with the vnode locked.
315df8bae1dSRodney W. Grimes  *
316c01a9b8cSDavid Greenman  * XXX unlock the vnode.
317c01a9b8cSDavid Greenman  * We must do this since uncaching the object may result in its
318c01a9b8cSDavid Greenman  * destruction which may initiate paging activity which may necessitate
319c01a9b8cSDavid Greenman  * re-locking the vnode.
32026f9a767SRodney W. Grimes  */
32124a1cce3SDavid Greenman void
32226f9a767SRodney W. Grimes vnode_pager_uncache(vp)
32324a1cce3SDavid Greenman 	struct vnode *vp;
32426f9a767SRodney W. Grimes {
32524a1cce3SDavid Greenman 	vm_object_t object;
32626f9a767SRodney W. Grimes 
32726f9a767SRodney W. Grimes 	/*
32826f9a767SRodney W. Grimes 	 * Not a mapped vnode
32926f9a767SRodney W. Grimes 	 */
330aa2cabb9SDavid Greenman 	object = vp->v_object;
3318e58bf68SDavid Greenman 	if (object == NULL)
33224a1cce3SDavid Greenman 		return;
3330d94caffSDavid Greenman 
33424a1cce3SDavid Greenman 	vm_object_reference(object);
335c01a9b8cSDavid Greenman 	VOP_UNLOCK(vp);
33626f9a767SRodney W. Grimes 	pager_cache(object, FALSE);
337c01a9b8cSDavid Greenman 	VOP_LOCK(vp);
33824a1cce3SDavid Greenman 	return;
33926f9a767SRodney W. Grimes }
340df8bae1dSRodney W. Grimes 
34126f9a767SRodney W. Grimes 
34226f9a767SRodney W. Grimes void
34326f9a767SRodney W. Grimes vnode_pager_freepage(m)
34426f9a767SRodney W. Grimes 	vm_page_t m;
345df8bae1dSRodney W. Grimes {
34626f9a767SRodney W. Grimes 	PAGE_WAKEUP(m);
34726f9a767SRodney W. Grimes 	vm_page_free(m);
34826f9a767SRodney W. Grimes }
34926f9a767SRodney W. Grimes 
35026f9a767SRodney W. Grimes /*
35126f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
35226f9a767SRodney W. Grimes  * file address
35326f9a767SRodney W. Grimes  */
35426f9a767SRodney W. Grimes vm_offset_t
355efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
35626f9a767SRodney W. Grimes 	struct vnode *vp;
35726f9a767SRodney W. Grimes 	vm_offset_t address;
358efc68ce1SDavid Greenman 	int *run;
35926f9a767SRodney W. Grimes {
36026f9a767SRodney W. Grimes 	int rtaddress;
36126f9a767SRodney W. Grimes 	int bsize;
36226f9a767SRodney W. Grimes 	vm_offset_t block;
36326f9a767SRodney W. Grimes 	struct vnode *rtvp;
36426f9a767SRodney W. Grimes 	int err;
36526f9a767SRodney W. Grimes 	int vblock, voffset;
36626f9a767SRodney W. Grimes 
3670d94caffSDavid Greenman 	if ((int) address < 0)
3680d94caffSDavid Greenman 		return -1;
3690d94caffSDavid Greenman 
3702c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3712c4488fcSJohn Dyson 		return -1;
3722c4488fcSJohn Dyson 
37326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
37426f9a767SRodney W. Grimes 	vblock = address / bsize;
37526f9a767SRodney W. Grimes 	voffset = address % bsize;
37626f9a767SRodney W. Grimes 
377c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
37826f9a767SRodney W. Grimes 
379efc68ce1SDavid Greenman 	if (err || (block == -1))
38026f9a767SRodney W. Grimes 		rtaddress = -1;
381efc68ce1SDavid Greenman 	else {
382187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
383efc68ce1SDavid Greenman 		if( run) {
384efc68ce1SDavid Greenman 			*run += 1;
385efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
386efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
387efc68ce1SDavid Greenman 		}
388efc68ce1SDavid Greenman 	}
38926f9a767SRodney W. Grimes 
39026f9a767SRodney W. Grimes 	return rtaddress;
39126f9a767SRodney W. Grimes }
39226f9a767SRodney W. Grimes 
39326f9a767SRodney W. Grimes /*
39426f9a767SRodney W. Grimes  * interrupt routine for I/O completion
39526f9a767SRodney W. Grimes  */
39626f9a767SRodney W. Grimes void
39726f9a767SRodney W. Grimes vnode_pager_iodone(bp)
39826f9a767SRodney W. Grimes 	struct buf *bp;
39926f9a767SRodney W. Grimes {
40026f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
40124a1cce3SDavid Greenman 	wakeup(bp);
40226f9a767SRodney W. Grimes }
40326f9a767SRodney W. Grimes 
40426f9a767SRodney W. Grimes /*
40526f9a767SRodney W. Grimes  * small block file system vnode pager input
40626f9a767SRodney W. Grimes  */
40726f9a767SRodney W. Grimes int
40824a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
40924a1cce3SDavid Greenman 	vm_object_t object;
41026f9a767SRodney W. Grimes 	vm_page_t m;
41126f9a767SRodney W. Grimes {
41226f9a767SRodney W. Grimes 	int i;
41326f9a767SRodney W. Grimes 	int s;
41426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
41526f9a767SRodney W. Grimes 	struct buf *bp;
41626f9a767SRodney W. Grimes 	vm_offset_t kva;
41726f9a767SRodney W. Grimes 	int fileaddr;
41826f9a767SRodney W. Grimes 	vm_offset_t bsize;
41926f9a767SRodney W. Grimes 	int error = 0;
42026f9a767SRodney W. Grimes 
42124a1cce3SDavid Greenman 	vp = object->handle;
4222c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
4232c4488fcSJohn Dyson 		return VM_PAGER_BAD;
4242c4488fcSJohn Dyson 
42526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
42626f9a767SRodney W. Grimes 
4270bdb7528SDavid Greenman 
428c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
42926f9a767SRodney W. Grimes 
43026f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
43126f9a767SRodney W. Grimes 
43226f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
433bbc0ec52SDavid Greenman 
4340d94caffSDavid Greenman 		if ((vm_page_bits(m->offset + i * bsize, bsize) & m->valid))
43526f9a767SRodney W. Grimes 			continue;
43626f9a767SRodney W. Grimes 
437efc68ce1SDavid Greenman 		fileaddr = vnode_pager_addr(vp, m->offset + i * bsize, (int *)0);
43826f9a767SRodney W. Grimes 		if (fileaddr != -1) {
43926f9a767SRodney W. Grimes 			bp = getpbuf();
44026f9a767SRodney W. Grimes 
44126f9a767SRodney W. Grimes 			/* build a minimal buffer header */
44226f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
44326f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
44426f9a767SRodney W. Grimes 			bp->b_proc = curproc;
44526f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
44626f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
44726f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
44826f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
44926f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
45026f9a767SRodney W. Grimes 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
451187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4520d94caffSDavid Greenman 			pbgetvp(dp, bp);
45326f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
45426f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
45526f9a767SRodney W. Grimes 
45626f9a767SRodney W. Grimes 			/* do the input */
45726f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
45826f9a767SRodney W. Grimes 
45926f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
46026f9a767SRodney W. Grimes 
46126f9a767SRodney W. Grimes 			s = splbio();
46226f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
463aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
46426f9a767SRodney W. Grimes 			}
46526f9a767SRodney W. Grimes 			splx(s);
46626f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
46726f9a767SRodney W. Grimes 				error = EIO;
46826f9a767SRodney W. Grimes 
46926f9a767SRodney W. Grimes 			/*
47026f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
47126f9a767SRodney W. Grimes 			 */
47226f9a767SRodney W. Grimes 			relpbuf(bp);
47326f9a767SRodney W. Grimes 			if (error)
47426f9a767SRodney W. Grimes 				break;
4750d94caffSDavid Greenman 
476170db9c6SJohn Dyson 			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
47726f9a767SRodney W. Grimes 		} else {
478b1fc01b7SJohn Dyson 			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
47926f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
48026f9a767SRodney W. Grimes 		}
48126f9a767SRodney W. Grimes 	}
48226f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
4830d94caffSDavid Greenman 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
484b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
48526f9a767SRodney W. Grimes 	if (error) {
486a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
48726f9a767SRodney W. Grimes 	}
48826f9a767SRodney W. Grimes 	return VM_PAGER_OK;
48926f9a767SRodney W. Grimes 
49026f9a767SRodney W. Grimes }
49126f9a767SRodney W. Grimes 
49226f9a767SRodney W. Grimes 
49326f9a767SRodney W. Grimes /*
49426f9a767SRodney W. Grimes  * old style vnode pager output routine
49526f9a767SRodney W. Grimes  */
49626f9a767SRodney W. Grimes int
49724a1cce3SDavid Greenman vnode_pager_input_old(object, m)
49824a1cce3SDavid Greenman 	vm_object_t object;
49926f9a767SRodney W. Grimes 	vm_page_t m;
50026f9a767SRodney W. Grimes {
501df8bae1dSRodney W. Grimes 	struct uio auio;
502df8bae1dSRodney W. Grimes 	struct iovec aiov;
50326f9a767SRodney W. Grimes 	int error;
50426f9a767SRodney W. Grimes 	int size;
50526f9a767SRodney W. Grimes 	vm_offset_t kva;
506df8bae1dSRodney W. Grimes 
50726f9a767SRodney W. Grimes 	error = 0;
508bbc0ec52SDavid Greenman 
509df8bae1dSRodney W. Grimes 	/*
51026f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
51126f9a767SRodney W. Grimes 	 */
51224a1cce3SDavid Greenman 	if (m->offset >= object->un_pager.vnp.vnp_size) {
51326f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
51426f9a767SRodney W. Grimes 	} else {
51526f9a767SRodney W. Grimes 		size = PAGE_SIZE;
51624a1cce3SDavid Greenman 		if (m->offset + size > object->un_pager.vnp.vnp_size)
51724a1cce3SDavid Greenman 			size = object->un_pager.vnp.vnp_size - m->offset;
5180bdb7528SDavid Greenman 
51926f9a767SRodney W. Grimes 		/*
520df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
521df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
522df8bae1dSRodney W. Grimes 		 */
52326f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
5240bdb7528SDavid Greenman 
525df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
526df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
527df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
528df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
5290d94caffSDavid Greenman 		auio.uio_offset = m->offset;
530df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
53126f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
532df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
533df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
53426f9a767SRodney W. Grimes 
53524a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
536df8bae1dSRodney W. Grimes 		if (!error) {
537df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
538df8bae1dSRodney W. Grimes 
539df8bae1dSRodney W. Grimes 			if (count == 0)
540df8bae1dSRodney W. Grimes 				error = EINVAL;
54126f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
54226f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
543df8bae1dSRodney W. Grimes 		}
54426f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
545df8bae1dSRodney W. Grimes 	}
54626f9a767SRodney W. Grimes 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5470d94caffSDavid Greenman 	m->dirty = 0;
548b1fc01b7SJohn Dyson 	m->flags &= ~PG_ZERO;
549a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
55026f9a767SRodney W. Grimes }
55126f9a767SRodney W. Grimes 
55226f9a767SRodney W. Grimes /*
55326f9a767SRodney W. Grimes  * generic vnode pager input routine
55426f9a767SRodney W. Grimes  */
555170db9c6SJohn Dyson 
55626f9a767SRodney W. Grimes int
55724a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
55826f9a767SRodney W. Grimes 	vm_object_t object;
55924a1cce3SDavid Greenman 	vm_page_t *m;
56024a1cce3SDavid Greenman 	int count;
56124a1cce3SDavid Greenman 	int reqpage;
56224a1cce3SDavid Greenman {
563170db9c6SJohn Dyson 	int rtval;
564170db9c6SJohn Dyson 	struct vnode *vp;
565170db9c6SJohn Dyson 	vp = object->handle;
5662c4488fcSJohn Dyson 	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
567170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
5682c4488fcSJohn Dyson 		return vnode_pager_leaf_getpages(object, m, count, reqpage, 0);
569170db9c6SJohn Dyson 	else
570170db9c6SJohn Dyson 		return rtval;
571170db9c6SJohn Dyson }
572170db9c6SJohn Dyson 
573170db9c6SJohn Dyson static int
574170db9c6SJohn Dyson vnode_pager_leaf_getpages(object, m, count, reqpage)
575170db9c6SJohn Dyson 	vm_object_t object;
576170db9c6SJohn Dyson 	vm_page_t *m;
577170db9c6SJohn Dyson 	int count;
578170db9c6SJohn Dyson 	int reqpage;
579170db9c6SJohn Dyson {
58024a1cce3SDavid Greenman 	vm_offset_t kva, foff;
58124a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
58226f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
583efc68ce1SDavid Greenman 	int runpg;
584efc68ce1SDavid Greenman 	int runend;
5850bdb7528SDavid Greenman 	struct buf *bp;
58626f9a767SRodney W. Grimes 	int s;
58726f9a767SRodney W. Grimes 	int error = 0;
58826f9a767SRodney W. Grimes 
58924a1cce3SDavid Greenman 	vp = object->handle;
5902c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
5912c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5922c4488fcSJohn Dyson 
59326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
59426f9a767SRodney W. Grimes 
59526f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
596bbc0ec52SDavid Greenman 
59726f9a767SRodney W. Grimes 	/*
598bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
599bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
60026f9a767SRodney W. Grimes 	 */
6010d94caffSDavid Greenman 	foff = m[reqpage]->offset;
602bbc0ec52SDavid Greenman 
60326f9a767SRodney W. Grimes 	/*
60416f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
60526f9a767SRodney W. Grimes 	 */
606c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
60726f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
60826f9a767SRodney W. Grimes 			if (i != reqpage) {
60926f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
61026f9a767SRodney W. Grimes 			}
61126f9a767SRodney W. Grimes 		}
612976e77fcSDavid Greenman 		cnt.v_vnodein++;
613976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
61424a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
615bbc0ec52SDavid Greenman 
61626f9a767SRodney W. Grimes 		/*
61726f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
61826f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
61926f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
62026f9a767SRodney W. Grimes 		 */
62126f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
62226f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
62326f9a767SRodney W. Grimes 
62426f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
62526f9a767SRodney W. Grimes 			if (i != reqpage) {
62626f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
62726f9a767SRodney W. Grimes 			}
62826f9a767SRodney W. Grimes 		}
629976e77fcSDavid Greenman 		cnt.v_vnodein++;
630976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
63124a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
63226f9a767SRodney W. Grimes 	}
63326f9a767SRodney W. Grimes 	/*
6340d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
6350d94caffSDavid Greenman 	 * then, the entire page is valid --
6360d94caffSDavid Greenman 	 */
6370d94caffSDavid Greenman 	if (m[reqpage]->valid) {
6380d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
6390d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6400d94caffSDavid Greenman 			if (i != reqpage)
6410d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6420d94caffSDavid Greenman 		}
6430d94caffSDavid Greenman 		return VM_PAGER_OK;
6440d94caffSDavid Greenman 	}
6450bdb7528SDavid Greenman 
6460d94caffSDavid Greenman 	/*
64726f9a767SRodney W. Grimes 	 * here on direct device I/O
64826f9a767SRodney W. Grimes 	 */
64926f9a767SRodney W. Grimes 
650efc68ce1SDavid Greenman 	firstaddr = -1;
65126f9a767SRodney W. Grimes 	/*
652efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
65326f9a767SRodney W. Grimes 	 */
654efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
655efc68ce1SDavid Greenman 		firstaddr = vnode_pager_addr(vp, m[i]->offset, &runpg);
656efc68ce1SDavid Greenman 		if (firstaddr == -1) {
65724a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
65824a1cce3SDavid Greenman 				panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
65924a1cce3SDavid Greenman 			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
660efc68ce1SDavid Greenman 			}
66126f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
662efc68ce1SDavid Greenman 			runend = i + 1;
663efc68ce1SDavid Greenman 			first = runend;
664efc68ce1SDavid Greenman 			continue;
665efc68ce1SDavid Greenman 		}
666efc68ce1SDavid Greenman 		runend = i + runpg;
667efc68ce1SDavid Greenman 		if (runend <= reqpage) {
668efc68ce1SDavid Greenman 			int j;
669efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
670efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
671efc68ce1SDavid Greenman 			}
67226f9a767SRodney W. Grimes 		} else {
673efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
674efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
67526f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
676efc68ce1SDavid Greenman 				count = first + runpg;
67726f9a767SRodney W. Grimes 			}
678efc68ce1SDavid Greenman 			break;
67926f9a767SRodney W. Grimes 		}
680efc68ce1SDavid Greenman 		first = runend;
681efc68ce1SDavid Greenman 	}
68226f9a767SRodney W. Grimes 
68326f9a767SRodney W. Grimes 	/*
684bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
685bbc0ec52SDavid Greenman 	 * to be zero based...
68626f9a767SRodney W. Grimes 	 */
68726f9a767SRodney W. Grimes 	if (first != 0) {
68826f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
68926f9a767SRodney W. Grimes 			m[i - first] = m[i];
69026f9a767SRodney W. Grimes 		}
69126f9a767SRodney W. Grimes 		count -= first;
69226f9a767SRodney W. Grimes 		reqpage -= first;
69326f9a767SRodney W. Grimes 	}
694efc68ce1SDavid Greenman 
69526f9a767SRodney W. Grimes 	/*
69626f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
69726f9a767SRodney W. Grimes 	 */
6980d94caffSDavid Greenman 	foff = m[0]->offset;
69926f9a767SRodney W. Grimes 
70026f9a767SRodney W. Grimes 	/*
70126f9a767SRodney W. Grimes 	 * calculate the size of the transfer
70226f9a767SRodney W. Grimes 	 */
70326f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
70424a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
70524a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
70626f9a767SRodney W. Grimes 
70726f9a767SRodney W. Grimes 	/*
70826f9a767SRodney W. Grimes 	 * round up physical size for real devices
70926f9a767SRodney W. Grimes 	 */
71026f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
71126f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
71226f9a767SRodney W. Grimes 
7136d40c3d3SDavid Greenman 	bp = getpbuf();
71416f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
71516f62314SDavid Greenman 
71626f9a767SRodney W. Grimes 	/*
71726f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
71826f9a767SRodney W. Grimes 	 */
71916f62314SDavid Greenman 	pmap_qenter(kva, m, count);
72026f9a767SRodney W. Grimes 
72126f9a767SRodney W. Grimes 	/* build a minimal buffer header */
72226f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
72326f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
72426f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
72526f9a767SRodney W. Grimes 	bp->b_proc = curproc;
72626f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
72726f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
72826f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
72926f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
73026f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
731187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7320d94caffSDavid Greenman 	pbgetvp(dp, bp);
73326f9a767SRodney W. Grimes 	bp->b_bcount = size;
73426f9a767SRodney W. Grimes 	bp->b_bufsize = size;
73526f9a767SRodney W. Grimes 
736976e77fcSDavid Greenman 	cnt.v_vnodein++;
737976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
738976e77fcSDavid Greenman 
73926f9a767SRodney W. Grimes 	/* do the input */
74026f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
741976e77fcSDavid Greenman 
74226f9a767SRodney W. Grimes 	s = splbio();
74326f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
74426f9a767SRodney W. Grimes 
74526f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
746aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
74726f9a767SRodney W. Grimes 	}
74826f9a767SRodney W. Grimes 	splx(s);
74926f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
75026f9a767SRodney W. Grimes 		error = EIO;
75126f9a767SRodney W. Grimes 
75226f9a767SRodney W. Grimes 	if (!error) {
75326f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
75426f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
75526f9a767SRodney W. Grimes 	}
75616f62314SDavid Greenman 	pmap_qremove(kva, count);
75726f9a767SRodney W. Grimes 
75826f9a767SRodney W. Grimes 	/*
75926f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
76026f9a767SRodney W. Grimes 	 */
76126f9a767SRodney W. Grimes 	relpbuf(bp);
76226f9a767SRodney W. Grimes 
76326f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
764fff93ab6SDavid Greenman 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
7650d94caffSDavid Greenman 		m[i]->dirty = 0;
7660d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
767b1fc01b7SJohn Dyson 		m[i]->flags &= ~PG_ZERO;
76826f9a767SRodney W. Grimes 		if (i != reqpage) {
769bbc0ec52SDavid Greenman 
77026f9a767SRodney W. Grimes 			/*
771bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
772bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
773bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
774bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
775bbc0ec52SDavid Greenman 			 * deactivating pages is best.
77626f9a767SRodney W. Grimes 			 */
777bbc0ec52SDavid Greenman 
77826f9a767SRodney W. Grimes 			/*
779bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
780bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
78126f9a767SRodney W. Grimes 			 */
78226f9a767SRodney W. Grimes 			if (!error) {
78326f9a767SRodney W. Grimes 				vm_page_deactivate(m[i]);
78426f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
78526f9a767SRodney W. Grimes 			} else {
78626f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
78726f9a767SRodney W. Grimes 			}
78826f9a767SRodney W. Grimes 		}
78926f9a767SRodney W. Grimes 	}
79026f9a767SRodney W. Grimes 	if (error) {
79124a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
79226f9a767SRodney W. Grimes 	}
793a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
79426f9a767SRodney W. Grimes }
79526f9a767SRodney W. Grimes 
796170db9c6SJohn Dyson int
797170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
798170db9c6SJohn Dyson 	vm_object_t object;
799170db9c6SJohn Dyson 	vm_page_t *m;
800170db9c6SJohn Dyson 	int count;
801170db9c6SJohn Dyson 	boolean_t sync;
802170db9c6SJohn Dyson 	int *rtvals;
803170db9c6SJohn Dyson {
804170db9c6SJohn Dyson 	int rtval;
805170db9c6SJohn Dyson 	struct vnode *vp;
806170db9c6SJohn Dyson 	vp = object->handle;
8072c4488fcSJohn Dyson 	rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
808170db9c6SJohn Dyson 	if (rtval == EOPNOTSUPP)
8092c4488fcSJohn Dyson 		return vnode_pager_leaf_putpages(object, m, count, sync, rtvals, 0);
810170db9c6SJohn Dyson 	else
811170db9c6SJohn Dyson 		return rtval;
812170db9c6SJohn Dyson }
813170db9c6SJohn Dyson 
81426f9a767SRodney W. Grimes /*
81526f9a767SRodney W. Grimes  * generic vnode pager output routine
81626f9a767SRodney W. Grimes  */
817170db9c6SJohn Dyson static int
818170db9c6SJohn Dyson vnode_pager_leaf_putpages(object, m, count, sync, rtvals)
81924a1cce3SDavid Greenman 	vm_object_t object;
82026f9a767SRodney W. Grimes 	vm_page_t *m;
82126f9a767SRodney W. Grimes 	int count;
82224a1cce3SDavid Greenman 	boolean_t sync;
82326f9a767SRodney W. Grimes 	int *rtvals;
82426f9a767SRodney W. Grimes {
825f6b04d2bSDavid Greenman 	int i;
82626f9a767SRodney W. Grimes 
827f6b04d2bSDavid Greenman 	struct vnode *vp;
828f6b04d2bSDavid Greenman 	int maxsize, ncount;
829f6b04d2bSDavid Greenman 	struct uio auio;
830f6b04d2bSDavid Greenman 	struct iovec aiov;
831f6b04d2bSDavid Greenman 	int error;
83226f9a767SRodney W. Grimes 
83324a1cce3SDavid Greenman 	vp = object->handle;;
83426f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
83526f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
83626f9a767SRodney W. Grimes 
8370d94caffSDavid Greenman 	if ((int) m[0]->offset < 0) {
83824a1cce3SDavid Greenman 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->offset, m[0]->dirty);
839f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
840f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
8410d94caffSDavid Greenman 	}
8420bdb7528SDavid Greenman 
843f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
844f6b04d2bSDavid Greenman 	ncount = count;
84526f9a767SRodney W. Grimes 
84624a1cce3SDavid Greenman 	if (maxsize + m[0]->offset > object->un_pager.vnp.vnp_size) {
84724a1cce3SDavid Greenman 		if (object->un_pager.vnp.vnp_size > m[0]->offset)
84824a1cce3SDavid Greenman 			maxsize = object->un_pager.vnp.vnp_size - m[0]->offset;
8495f55e841SDavid Greenman 		else
8505f55e841SDavid Greenman 			maxsize = 0;
851f6b04d2bSDavid Greenman 		ncount = (maxsize + PAGE_SIZE - 1) / PAGE_SIZE;
852f6b04d2bSDavid Greenman 		if (ncount < count) {
853f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
854f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
855f6b04d2bSDavid Greenman 			}
856f6b04d2bSDavid Greenman 			if (ncount == 0) {
85724a1cce3SDavid Greenman 				printf("vnode_pager_putpages: write past end of file: %d, %d\n",
85824a1cce3SDavid Greenman 					m[0]->offset, object->un_pager.vnp.vnp_size);
85926f9a767SRodney W. Grimes 				return rtvals[0];
86026f9a767SRodney W. Grimes 			}
861f6b04d2bSDavid Greenman 		}
862f6b04d2bSDavid Greenman 	}
86326f9a767SRodney W. Grimes 
86426f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8655f55e841SDavid Greenman 		m[i]->busy++;
866f6b04d2bSDavid Greenman 		m[i]->flags &= ~PG_BUSY;
86726f9a767SRodney W. Grimes 	}
868f6b04d2bSDavid Greenman 
869f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
870f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
871f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
872f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
873f6b04d2bSDavid Greenman 	auio.uio_offset = m[0]->offset;
874f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
875f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
876f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
877f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
878f6b04d2bSDavid Greenman 	error = VOP_WRITE(vp, &auio, IO_VMIO, curproc->p_ucred);
879976e77fcSDavid Greenman 	cnt.v_vnodeout++;
880f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
881f6b04d2bSDavid Greenman 
882f6b04d2bSDavid Greenman 	if (error) {
88324a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
884f6b04d2bSDavid Greenman 	}
885f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
88624a1cce3SDavid Greenman 		printf("vnode_pager_putpages: residual I/O %d at %d\n", auio.uio_resid, m[0]->offset);
88726f9a767SRodney W. Grimes 	}
88826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
8895f55e841SDavid Greenman 		m[i]->busy--;
890f6b04d2bSDavid Greenman 		if (i < ncount) {
89126f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
89226f9a767SRodney W. Grimes 		}
893f6b04d2bSDavid Greenman 		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED))
89424a1cce3SDavid Greenman 			wakeup(m[i]);
89526f9a767SRodney W. Grimes 	}
896f6b04d2bSDavid Greenman 	return rtvals[0];
89726f9a767SRodney W. Grimes }
898f6b04d2bSDavid Greenman 
899f6b04d2bSDavid Greenman struct vnode *
90024a1cce3SDavid Greenman vnode_pager_lock(object)
90124a1cce3SDavid Greenman 	vm_object_t object;
90224a1cce3SDavid Greenman {
90324a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
90424a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
905f6b04d2bSDavid Greenman 			continue;
906f6b04d2bSDavid Greenman 
90724a1cce3SDavid Greenman 		VOP_LOCK(object->handle);
90824a1cce3SDavid Greenman 		return object->handle;
90926f9a767SRodney W. Grimes 	}
91024a1cce3SDavid Greenman 	return NULL;
911f6b04d2bSDavid Greenman }
912