xref: /freebsd/sys/vm/vnode_pager.c (revision 0bdb7528db8029f7497f969ac102333716e548fb)
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
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
9df8bae1dSRodney W. Grimes  * Science Department.
10df8bae1dSRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
20df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
21df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
22df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
23df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
24df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
25df8bae1dSRodney W. Grimes  *    without specific prior written permission.
26df8bae1dSRodney W. Grimes  *
27df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
38df8bae1dSRodney W. Grimes  *
3926f9a767SRodney W. Grimes  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
400bdb7528SDavid Greenman  *	$Id: vnode_pager.c,v 1.31 1995/03/19 12:36:10 davidg Exp $
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes /*
44df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * TODO:
47df8bae1dSRodney W. Grimes  *	pageouts
48df8bae1dSRodney W. Grimes  *	fix credential use (uses current process credentials now)
49df8bae1dSRodney W. Grimes  */
50df8bae1dSRodney W. Grimes 
5126f9a767SRodney W. Grimes /*
5226f9a767SRodney W. Grimes  * MODIFICATIONS:
5326f9a767SRodney W. Grimes  * John S. Dyson  08 Dec 93
5426f9a767SRodney W. Grimes  *
5526f9a767SRodney W. Grimes  * This file in conjunction with some vm_fault mods, eliminate the performance
5626f9a767SRodney W. Grimes  * advantage for using the buffer cache and minimize memory copies.
5726f9a767SRodney W. Grimes  *
5826f9a767SRodney W. Grimes  * 1) Supports multiple - block reads
5926f9a767SRodney W. Grimes  * 2) Bypasses buffer cache for reads
6026f9a767SRodney W. Grimes  *
6126f9a767SRodney W. Grimes  * TODO:
6226f9a767SRodney W. Grimes  *
6326f9a767SRodney W. Grimes  * 1) Totally bypass buffer cache for reads
6426f9a767SRodney W. Grimes  *    (Currently will still sometimes use buffer cache for reads)
6526f9a767SRodney W. Grimes  * 2) Bypass buffer cache for writes
6626f9a767SRodney W. Grimes  *    (Code does not support it, but mods are simple)
6726f9a767SRodney W. Grimes  */
6826f9a767SRodney W. Grimes 
69df8bae1dSRodney W. Grimes #include <sys/param.h>
70df8bae1dSRodney W. Grimes #include <sys/systm.h>
710d94caffSDavid Greenman #include <sys/kernel.h>
72df8bae1dSRodney W. Grimes #include <sys/proc.h>
73df8bae1dSRodney W. Grimes #include <sys/malloc.h>
74df8bae1dSRodney W. Grimes #include <sys/vnode.h>
75df8bae1dSRodney W. Grimes #include <sys/uio.h>
76df8bae1dSRodney W. Grimes #include <sys/mount.h>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <vm/vm.h>
79df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
80df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
81df8bae1dSRodney W. Grimes 
8226f9a767SRodney W. Grimes #include <sys/buf.h>
8326f9a767SRodney W. Grimes #include <miscfs/specfs/specdev.h>
84df8bae1dSRodney W. Grimes 
8526f9a767SRodney W. Grimes int vnode_pager_putmulti();
86df8bae1dSRodney W. Grimes 
8726f9a767SRodney W. Grimes void vnode_pager_init();
8826f9a767SRodney W. Grimes void vnode_pager_dealloc();
8926f9a767SRodney W. Grimes int vnode_pager_getpage();
9026f9a767SRodney W. Grimes int vnode_pager_getmulti();
9126f9a767SRodney W. Grimes int vnode_pager_putpage();
9226f9a767SRodney W. Grimes boolean_t vnode_pager_haspage();
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
95df8bae1dSRodney W. Grimes 	vnode_pager_init,
96df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
97df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
98df8bae1dSRodney W. Grimes 	vnode_pager_getpage,
9926f9a767SRodney W. Grimes 	vnode_pager_getmulti,
100df8bae1dSRodney W. Grimes 	vnode_pager_putpage,
10126f9a767SRodney W. Grimes 	vnode_pager_putmulti,
10226f9a767SRodney W. Grimes 	vnode_pager_haspage
103df8bae1dSRodney W. Grimes };
104df8bae1dSRodney W. Grimes 
10516f62314SDavid Greenman 
10616f62314SDavid Greenman 
10726f9a767SRodney W. Grimes static int vnode_pager_input(vn_pager_t vnp, vm_page_t * m, int count, int reqpage);
10826f9a767SRodney W. Grimes static int vnode_pager_output(vn_pager_t vnp, vm_page_t * m, int count, int *rtvals);
10926f9a767SRodney W. Grimes 
11026f9a767SRodney W. Grimes extern vm_map_t pager_map;
11126f9a767SRodney W. Grimes 
11226f9a767SRodney W. Grimes struct pagerlst vnode_pager_list;	/* list of managed vnodes */
11326f9a767SRodney W. Grimes 
11426f9a767SRodney W. Grimes #define MAXBP (PAGE_SIZE/DEV_BSIZE);
11526f9a767SRodney W. Grimes 
11626f9a767SRodney W. Grimes void
117df8bae1dSRodney W. Grimes vnode_pager_init()
118df8bae1dSRodney W. Grimes {
119df8bae1dSRodney W. Grimes 	TAILQ_INIT(&vnode_pager_list);
120df8bae1dSRodney W. Grimes }
121df8bae1dSRodney W. Grimes 
122df8bae1dSRodney W. Grimes /*
123df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
124df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
125df8bae1dSRodney W. Grimes  */
12626f9a767SRodney W. Grimes vm_pager_t
12726f9a767SRodney W. Grimes vnode_pager_alloc(handle, size, prot, offset)
128df8bae1dSRodney W. Grimes 	caddr_t handle;
129df8bae1dSRodney W. Grimes 	vm_size_t size;
130df8bae1dSRodney W. Grimes 	vm_prot_t prot;
13126f9a767SRodney W. Grimes 	vm_offset_t offset;
132df8bae1dSRodney W. Grimes {
133df8bae1dSRodney W. Grimes 	register vm_pager_t pager;
134df8bae1dSRodney W. Grimes 	register vn_pager_t vnp;
1350d94caffSDavid Greenman 	vm_object_t object, tobject;
136df8bae1dSRodney W. Grimes 	struct vattr vattr;
137df8bae1dSRodney W. Grimes 	struct vnode *vp;
138df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
1390d94caffSDavid Greenman 	int rtval;
140df8bae1dSRodney W. Grimes 
141df8bae1dSRodney W. Grimes 	/*
142df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
143df8bae1dSRodney W. Grimes 	 */
144df8bae1dSRodney W. Grimes 	if (handle == NULL)
145df8bae1dSRodney W. Grimes 		return (NULL);
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes 	/*
148bbc0ec52SDavid Greenman 	 * Vnodes keep a pointer to any associated pager so no need to lookup
149bbc0ec52SDavid Greenman 	 * with vm_pager_lookup.
150df8bae1dSRodney W. Grimes 	 */
151df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
1520d94caffSDavid Greenman 	while ((object = (vm_object_t) vp->v_vmdata) && (object->flags & OBJ_DEAD))
1530d94caffSDavid Greenman 		tsleep((caddr_t) object, PVM, "vadead", 0);
1540d94caffSDavid Greenman 
1558e58bf68SDavid Greenman 	pager = NULL;
1568e58bf68SDavid Greenman 	if (object != NULL)
1578e58bf68SDavid Greenman 		pager = object->pager;
158df8bae1dSRodney W. Grimes 	if (pager == NULL) {
159bbc0ec52SDavid Greenman 
160df8bae1dSRodney W. Grimes 		/*
161df8bae1dSRodney W. Grimes 		 * Allocate pager structures
162df8bae1dSRodney W. Grimes 		 */
163df8bae1dSRodney W. Grimes 		pager = (vm_pager_t) malloc(sizeof *pager, M_VMPAGER, M_WAITOK);
164df8bae1dSRodney W. Grimes 		if (pager == NULL)
165df8bae1dSRodney W. Grimes 			return (NULL);
166df8bae1dSRodney W. Grimes 		vnp = (vn_pager_t) malloc(sizeof *vnp, M_VMPGDATA, M_WAITOK);
167df8bae1dSRodney W. Grimes 		if (vnp == NULL) {
168df8bae1dSRodney W. Grimes 			free((caddr_t) pager, M_VMPAGER);
169df8bae1dSRodney W. Grimes 			return (NULL);
170df8bae1dSRodney W. Grimes 		}
171df8bae1dSRodney W. Grimes 		/*
172df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
173df8bae1dSRodney W. Grimes 		 */
1740d94caffSDavid Greenman 		if ((rtval = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) == 0) {
175df8bae1dSRodney W. Grimes 			object = vm_object_allocate(round_page(vattr.va_size));
1764bb62461SDavid Greenman 			object->flags = OBJ_CANPERSIST;
177df8bae1dSRodney W. Grimes 			vm_object_enter(object, pager);
1787fb0c17eSDavid Greenman 			object->pager = pager;
179df8bae1dSRodney W. Grimes 		} else {
1800d94caffSDavid Greenman 			printf("Error in getattr: %d\n", rtval);
181df8bae1dSRodney W. Grimes 			free((caddr_t) vnp, M_VMPGDATA);
182df8bae1dSRodney W. Grimes 			free((caddr_t) pager, M_VMPAGER);
183df8bae1dSRodney W. Grimes 			return (NULL);
184df8bae1dSRodney W. Grimes 		}
185bbc0ec52SDavid Greenman 
186df8bae1dSRodney W. Grimes 		/*
187df8bae1dSRodney W. Grimes 		 * Hold a reference to the vnode and initialize pager data.
188df8bae1dSRodney W. Grimes 		 */
189df8bae1dSRodney W. Grimes 		VREF(vp);
190df8bae1dSRodney W. Grimes 		vnp->vnp_flags = 0;
191df8bae1dSRodney W. Grimes 		vnp->vnp_vp = vp;
192df8bae1dSRodney W. Grimes 		vnp->vnp_size = vattr.va_size;
19326f9a767SRodney W. Grimes 
194df8bae1dSRodney W. Grimes 		TAILQ_INSERT_TAIL(&vnode_pager_list, pager, pg_list);
195df8bae1dSRodney W. Grimes 		pager->pg_handle = handle;
196df8bae1dSRodney W. Grimes 		pager->pg_type = PG_VNODE;
197df8bae1dSRodney W. Grimes 		pager->pg_ops = &vnodepagerops;
19826f9a767SRodney W. Grimes 		pager->pg_data = (caddr_t) vnp;
1998e58bf68SDavid Greenman 		vp->v_vmdata = (caddr_t) object;
200df8bae1dSRodney W. Grimes 	} else {
201bbc0ec52SDavid Greenman 
202df8bae1dSRodney W. Grimes 		/*
203bbc0ec52SDavid Greenman 		 * vm_object_lookup() will remove the object from the cache if
204bbc0ec52SDavid Greenman 		 * found and also gain a reference to the object.
205df8bae1dSRodney W. Grimes 		 */
2068e58bf68SDavid Greenman 		(void) vm_object_lookup(pager);
207df8bae1dSRodney W. Grimes 	}
208df8bae1dSRodney W. Grimes 	return (pager);
209df8bae1dSRodney W. Grimes }
210df8bae1dSRodney W. Grimes 
21126f9a767SRodney W. Grimes void
212df8bae1dSRodney W. Grimes vnode_pager_dealloc(pager)
213df8bae1dSRodney W. Grimes 	vm_pager_t pager;
214df8bae1dSRodney W. Grimes {
215df8bae1dSRodney W. Grimes 	register vn_pager_t vnp = (vn_pager_t) pager->pg_data;
216df8bae1dSRodney W. Grimes 	register struct vnode *vp;
2170d94caffSDavid Greenman 	vm_object_t object;
218df8bae1dSRodney W. Grimes 
21905f0fdd2SPoul-Henning Kamp 	vp = vnp->vnp_vp;
22005f0fdd2SPoul-Henning Kamp 	if (vp) {
2210d94caffSDavid Greenman 		int s = splbio();
2220d94caffSDavid Greenman 
2230d94caffSDavid Greenman 		object = (vm_object_t) vp->v_vmdata;
2240d94caffSDavid Greenman 		if (object) {
2250d94caffSDavid Greenman 			while (object->paging_in_progress) {
226c0503609SDavid Greenman 				object->flags |= OBJ_PIPWNT;
2270d94caffSDavid Greenman 				tsleep(object, PVM, "vnpdea", 0);
2280d94caffSDavid Greenman 			}
2290d94caffSDavid Greenman 		}
2300d94caffSDavid Greenman 		splx(s);
2310d94caffSDavid Greenman 
232df8bae1dSRodney W. Grimes 		vp->v_vmdata = NULL;
2338e58bf68SDavid Greenman 		vp->v_flag &= ~(VTEXT | VVMIO);
23400072442SDavid Greenman 		vp->v_flag |= VAGE;
235df8bae1dSRodney W. Grimes 		vrele(vp);
236df8bae1dSRodney W. Grimes 	}
237df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vnode_pager_list, pager, pg_list);
238df8bae1dSRodney W. Grimes 	free((caddr_t) vnp, M_VMPGDATA);
239df8bae1dSRodney W. Grimes 	free((caddr_t) pager, M_VMPAGER);
240df8bae1dSRodney W. Grimes }
241df8bae1dSRodney W. Grimes 
24226f9a767SRodney W. Grimes int
24326f9a767SRodney W. Grimes vnode_pager_getmulti(pager, m, count, reqpage, sync)
244df8bae1dSRodney W. Grimes 	vm_pager_t pager;
24526f9a767SRodney W. Grimes 	vm_page_t *m;
24626f9a767SRodney W. Grimes 	int count;
24726f9a767SRodney W. Grimes 	int reqpage;
248df8bae1dSRodney W. Grimes 	boolean_t sync;
249df8bae1dSRodney W. Grimes {
250df8bae1dSRodney W. Grimes 
25126f9a767SRodney W. Grimes 	return vnode_pager_input((vn_pager_t) pager->pg_data, m, count, reqpage);
252df8bae1dSRodney W. Grimes }
253df8bae1dSRodney W. Grimes 
25426f9a767SRodney W. Grimes int
25526f9a767SRodney W. Grimes vnode_pager_getpage(pager, m, sync)
256df8bae1dSRodney W. Grimes 	vm_pager_t pager;
25726f9a767SRodney W. Grimes 	vm_page_t m;
25826f9a767SRodney W. Grimes 	boolean_t sync;
25926f9a767SRodney W. Grimes {
26026f9a767SRodney W. Grimes 
26126f9a767SRodney W. Grimes 	vm_page_t marray[1];
262bbc0ec52SDavid Greenman 
26326f9a767SRodney W. Grimes 	if (pager == NULL)
26426f9a767SRodney W. Grimes 		return FALSE;
26526f9a767SRodney W. Grimes 	marray[0] = m;
26626f9a767SRodney W. Grimes 
26726f9a767SRodney W. Grimes 	return vnode_pager_input((vn_pager_t) pager->pg_data, marray, 1, 0);
26826f9a767SRodney W. Grimes }
26926f9a767SRodney W. Grimes 
27026f9a767SRodney W. Grimes boolean_t
27126f9a767SRodney W. Grimes vnode_pager_putpage(pager, m, sync)
27226f9a767SRodney W. Grimes 	vm_pager_t pager;
27326f9a767SRodney W. Grimes 	vm_page_t m;
274df8bae1dSRodney W. Grimes 	boolean_t sync;
275df8bae1dSRodney W. Grimes {
27626f9a767SRodney W. Grimes 	vm_page_t marray[1];
27726f9a767SRodney W. Grimes 	int rtvals[1];
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes 	if (pager == NULL)
28026f9a767SRodney W. Grimes 		return FALSE;
28126f9a767SRodney W. Grimes 	marray[0] = m;
28226f9a767SRodney W. Grimes 	vnode_pager_output((vn_pager_t) pager->pg_data, marray, 1, rtvals);
28326f9a767SRodney W. Grimes 	return rtvals[0];
284df8bae1dSRodney W. Grimes }
285df8bae1dSRodney W. Grimes 
28626f9a767SRodney W. Grimes int
28726f9a767SRodney W. Grimes vnode_pager_putmulti(pager, m, c, sync, rtvals)
28826f9a767SRodney W. Grimes 	vm_pager_t pager;
28926f9a767SRodney W. Grimes 	vm_page_t *m;
29026f9a767SRodney W. Grimes 	int c;
29126f9a767SRodney W. Grimes 	boolean_t sync;
29226f9a767SRodney W. Grimes 	int *rtvals;
29326f9a767SRodney W. Grimes {
29426f9a767SRodney W. Grimes 	return vnode_pager_output((vn_pager_t) pager->pg_data, m, c, rtvals);
29526f9a767SRodney W. Grimes }
29626f9a767SRodney W. Grimes 
29726f9a767SRodney W. Grimes 
29826f9a767SRodney W. Grimes boolean_t
299df8bae1dSRodney W. Grimes vnode_pager_haspage(pager, offset)
300df8bae1dSRodney W. Grimes 	vm_pager_t pager;
301df8bae1dSRodney W. Grimes 	vm_offset_t offset;
302df8bae1dSRodney W. Grimes {
303df8bae1dSRodney W. Grimes 	register vn_pager_t vnp = (vn_pager_t) pager->pg_data;
3044abc71c0SDavid Greenman 	register struct vnode *vp = vnp->vnp_vp;
305df8bae1dSRodney W. Grimes 	daddr_t bn;
306df8bae1dSRodney W. Grimes 	int err;
307317205caSDavid Greenman 	daddr_t block;
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes 	/*
3100d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
3110d94caffSDavid Greenman 	 * not have the page.
312df8bae1dSRodney W. Grimes 	 */
3134abc71c0SDavid Greenman 	if ((vp->v_mount == NULL) || (offset >= vnp->vnp_size))
3144abc71c0SDavid Greenman 		return FALSE;
315df8bae1dSRodney W. Grimes 
3164abc71c0SDavid Greenman 	block = offset / vp->v_mount->mnt_stat.f_iosize;
3174abc71c0SDavid Greenman 	if (incore(vp, block))
318317205caSDavid Greenman 		return TRUE;
3190bdb7528SDavid Greenman 
3200bdb7528SDavid Greenman 	VOP_LOCK(vp);
321df8bae1dSRodney W. Grimes 	/*
322bbc0ec52SDavid Greenman 	 * Read the index to find the disk block to read from.  If there is no
323bbc0ec52SDavid Greenman 	 * block, report that we don't have this data.
324df8bae1dSRodney W. Grimes 	 *
325df8bae1dSRodney W. Grimes 	 * Assumes that the vnode has whole page or nothing.
326df8bae1dSRodney W. Grimes 	 */
3274abc71c0SDavid Greenman 	err = VOP_BMAP(vp, block, (struct vnode **) 0, &bn, 0);
3280bdb7528SDavid Greenman 	VOP_UNLOCK(vp);
3290d94caffSDavid Greenman 	if (err)
330df8bae1dSRodney W. Grimes 		return (TRUE);
331df8bae1dSRodney W. Grimes 	return ((long) bn < 0 ? FALSE : TRUE);
332df8bae1dSRodney W. Grimes }
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes /*
335df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
336df8bae1dSRodney W. Grimes  * If this vnode is mapped into some address space (i.e. we have a pager
337df8bae1dSRodney W. Grimes  * for it) we adjust our own internal size and flush any cached pages in
338df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
339df8bae1dSRodney W. Grimes  *
340df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
341df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
342df8bae1dSRodney W. Grimes  */
343df8bae1dSRodney W. Grimes void
344df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
345df8bae1dSRodney W. Grimes 	struct vnode *vp;
346df8bae1dSRodney W. Grimes 	u_long nsize;
347df8bae1dSRodney W. Grimes {
348df8bae1dSRodney W. Grimes 	register vn_pager_t vnp;
349df8bae1dSRodney W. Grimes 	register vm_object_t object;
350df8bae1dSRodney W. Grimes 	vm_pager_t pager;
351df8bae1dSRodney W. Grimes 
352df8bae1dSRodney W. Grimes 	/*
353df8bae1dSRodney W. Grimes 	 * Not a mapped vnode
354df8bae1dSRodney W. Grimes 	 */
355df8bae1dSRodney W. Grimes 	if (vp == NULL || vp->v_type != VREG || vp->v_vmdata == NULL)
356df8bae1dSRodney W. Grimes 		return;
357bbc0ec52SDavid Greenman 
358df8bae1dSRodney W. Grimes 	/*
359df8bae1dSRodney W. Grimes 	 * Hasn't changed size
360df8bae1dSRodney W. Grimes 	 */
3618e58bf68SDavid Greenman 	object = (vm_object_t) vp->v_vmdata;
3628e58bf68SDavid Greenman 	if (object == NULL)
3638e58bf68SDavid Greenman 		return;
3648e58bf68SDavid Greenman 	if ((pager = object->pager) == NULL)
3658e58bf68SDavid Greenman 		return;
366df8bae1dSRodney W. Grimes 	vnp = (vn_pager_t) pager->pg_data;
367df8bae1dSRodney W. Grimes 	if (nsize == vnp->vnp_size)
368df8bae1dSRodney W. Grimes 		return;
369bbc0ec52SDavid Greenman 
370df8bae1dSRodney W. Grimes 	/*
371bbc0ec52SDavid Greenman 	 * No object. This can happen during object termination since
372bbc0ec52SDavid Greenman 	 * vm_object_page_clean is called after the object has been removed
373bbc0ec52SDavid Greenman 	 * from the hash table, and clean may cause vnode write operations
374bbc0ec52SDavid Greenman 	 * which can wind up back here.
375df8bae1dSRodney W. Grimes 	 */
376df8bae1dSRodney W. Grimes 	object = vm_object_lookup(pager);
377df8bae1dSRodney W. Grimes 	if (object == NULL)
378df8bae1dSRodney W. Grimes 		return;
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	/*
381bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
382df8bae1dSRodney W. Grimes 	 */
383bbc0ec52SDavid Greenman 	if (nsize < vnp->vnp_size) {
3840d94caffSDavid Greenman 		if (round_page((vm_offset_t) nsize) < vnp->vnp_size) {
385df8bae1dSRodney W. Grimes 			vm_object_lock(object);
386df8bae1dSRodney W. Grimes 			vm_object_page_remove(object,
387bbc0ec52SDavid Greenman 			    round_page((vm_offset_t) nsize), vnp->vnp_size);
388bbc0ec52SDavid Greenman 			vm_object_unlock(object);
3890d94caffSDavid Greenman 		}
390bbc0ec52SDavid Greenman 		/*
391bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
392bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
393bbc0ec52SDavid Greenman 		 */
394bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
395bbc0ec52SDavid Greenman 			vm_offset_t kva;
396bbc0ec52SDavid Greenman 			vm_page_t m;
397bbc0ec52SDavid Greenman 
398bbc0ec52SDavid Greenman 			m = vm_page_lookup(object, trunc_page((vm_offset_t) nsize));
399bbc0ec52SDavid Greenman 			if (m) {
400bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
401bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
402bbc0ec52SDavid Greenman 				    round_page(nsize) - nsize);
403bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
404bbc0ec52SDavid Greenman 			}
405bbc0ec52SDavid Greenman 		}
406bbc0ec52SDavid Greenman 	}
407df8bae1dSRodney W. Grimes 	vnp->vnp_size = (vm_offset_t) nsize;
408bbc0ec52SDavid Greenman 	object->size = round_page(nsize);
409bbc0ec52SDavid Greenman 
410df8bae1dSRodney W. Grimes 	vm_object_deallocate(object);
411df8bae1dSRodney W. Grimes }
412df8bae1dSRodney W. Grimes 
413df8bae1dSRodney W. Grimes void
414df8bae1dSRodney W. Grimes vnode_pager_umount(mp)
415df8bae1dSRodney W. Grimes 	register struct mount *mp;
416df8bae1dSRodney W. Grimes {
417df8bae1dSRodney W. Grimes 	register vm_pager_t pager, npager;
418df8bae1dSRodney W. Grimes 	struct vnode *vp;
419df8bae1dSRodney W. Grimes 
420c01a9b8cSDavid Greenman 	for (pager = vnode_pager_list.tqh_first; pager != NULL; pager = npager) {
421df8bae1dSRodney W. Grimes 		/*
422bbc0ec52SDavid Greenman 		 * Save the next pointer now since uncaching may terminate the
423bbc0ec52SDavid Greenman 		 * object and render pager invalid
424df8bae1dSRodney W. Grimes 		 */
42526f9a767SRodney W. Grimes 		npager = pager->pg_list.tqe_next;
426c01a9b8cSDavid Greenman 		vp = ((vn_pager_t) pager->pg_data)->vnp_vp;
427c01a9b8cSDavid Greenman 		if (mp == (struct mount *) 0 || vp->v_mount == mp) {
428c01a9b8cSDavid Greenman 			VOP_LOCK(vp);
429df8bae1dSRodney W. Grimes 			(void) vnode_pager_uncache(vp);
430c01a9b8cSDavid Greenman 			VOP_UNLOCK(vp);
431c01a9b8cSDavid Greenman 		}
432df8bae1dSRodney W. Grimes 	}
433df8bae1dSRodney W. Grimes }
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes /*
436df8bae1dSRodney W. Grimes  * Remove vnode associated object from the object cache.
437c01a9b8cSDavid Greenman  * This routine must be called with the vnode locked.
438df8bae1dSRodney W. Grimes  *
439c01a9b8cSDavid Greenman  * XXX unlock the vnode.
440c01a9b8cSDavid Greenman  * We must do this since uncaching the object may result in its
441c01a9b8cSDavid Greenman  * destruction which may initiate paging activity which may necessitate
442c01a9b8cSDavid Greenman  * re-locking the vnode.
44326f9a767SRodney W. Grimes  */
44426f9a767SRodney W. Grimes boolean_t
44526f9a767SRodney W. Grimes vnode_pager_uncache(vp)
44626f9a767SRodney W. Grimes 	register struct vnode *vp;
44726f9a767SRodney W. Grimes {
44826f9a767SRodney W. Grimes 	register vm_object_t object;
449c01a9b8cSDavid Greenman 	boolean_t uncached;
45026f9a767SRodney W. Grimes 	vm_pager_t pager;
45126f9a767SRodney W. Grimes 
45226f9a767SRodney W. Grimes 	/*
45326f9a767SRodney W. Grimes 	 * Not a mapped vnode
45426f9a767SRodney W. Grimes 	 */
4558e58bf68SDavid Greenman 	object = (vm_object_t) vp->v_vmdata;
4568e58bf68SDavid Greenman 	if (object == NULL)
4578e58bf68SDavid Greenman 		return (TRUE);
4580d94caffSDavid Greenman 
4598e58bf68SDavid Greenman 	pager = object->pager;
46026f9a767SRodney W. Grimes 	if (pager == NULL)
46126f9a767SRodney W. Grimes 		return (TRUE);
462bbc0ec52SDavid Greenman 
463c01a9b8cSDavid Greenman #ifdef DEBUG
464c01a9b8cSDavid Greenman 	if (!VOP_ISLOCKED(vp)) {
465c01a9b8cSDavid Greenman 		extern int (**nfsv2_vnodeop_p)();
466bbc0ec52SDavid Greenman 
467c01a9b8cSDavid Greenman 		if (vp->v_op != nfsv2_vnodeop_p)
468c01a9b8cSDavid Greenman 			panic("vnode_pager_uncache: vnode not locked!");
469c01a9b8cSDavid Greenman 	}
470c01a9b8cSDavid Greenman #endif
47126f9a767SRodney W. Grimes 	/*
472bbc0ec52SDavid Greenman 	 * Must use vm_object_lookup() as it actually removes the object from
473bbc0ec52SDavid Greenman 	 * the cache list.
47426f9a767SRodney W. Grimes 	 */
47526f9a767SRodney W. Grimes 	object = vm_object_lookup(pager);
47626f9a767SRodney W. Grimes 	if (object) {
47726f9a767SRodney W. Grimes 		uncached = (object->ref_count <= 1);
478c01a9b8cSDavid Greenman 		VOP_UNLOCK(vp);
47926f9a767SRodney W. Grimes 		pager_cache(object, FALSE);
480c01a9b8cSDavid Greenman 		VOP_LOCK(vp);
48126f9a767SRodney W. Grimes 	} else
48226f9a767SRodney W. Grimes 		uncached = TRUE;
48326f9a767SRodney W. Grimes 	return (uncached);
48426f9a767SRodney W. Grimes }
485df8bae1dSRodney W. Grimes 
48626f9a767SRodney W. Grimes 
48726f9a767SRodney W. Grimes void
48826f9a767SRodney W. Grimes vnode_pager_freepage(m)
48926f9a767SRodney W. Grimes 	vm_page_t m;
490df8bae1dSRodney W. Grimes {
49126f9a767SRodney W. Grimes 	PAGE_WAKEUP(m);
49226f9a767SRodney W. Grimes 	vm_page_free(m);
49326f9a767SRodney W. Grimes }
49426f9a767SRodney W. Grimes 
49526f9a767SRodney W. Grimes /*
49626f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
49726f9a767SRodney W. Grimes  * file address
49826f9a767SRodney W. Grimes  */
49926f9a767SRodney W. Grimes vm_offset_t
500efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
50126f9a767SRodney W. Grimes 	struct vnode *vp;
50226f9a767SRodney W. Grimes 	vm_offset_t address;
503efc68ce1SDavid Greenman 	int *run;
50426f9a767SRodney W. Grimes {
50526f9a767SRodney W. Grimes 	int rtaddress;
50626f9a767SRodney W. Grimes 	int bsize;
50726f9a767SRodney W. Grimes 	vm_offset_t block;
50826f9a767SRodney W. Grimes 	struct vnode *rtvp;
50926f9a767SRodney W. Grimes 	int err;
51026f9a767SRodney W. Grimes 	int vblock, voffset;
51126f9a767SRodney W. Grimes 
5120d94caffSDavid Greenman 	if ((int) address < 0)
5130d94caffSDavid Greenman 		return -1;
5140d94caffSDavid Greenman 
51526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
51626f9a767SRodney W. Grimes 	vblock = address / bsize;
51726f9a767SRodney W. Grimes 	voffset = address % bsize;
51826f9a767SRodney W. Grimes 
519efc68ce1SDavid Greenman 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run);
52026f9a767SRodney W. Grimes 
521efc68ce1SDavid Greenman 	if (err || (block == -1))
52226f9a767SRodney W. Grimes 		rtaddress = -1;
523efc68ce1SDavid Greenman 	else {
524187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
525efc68ce1SDavid Greenman 		if( run) {
526efc68ce1SDavid Greenman 			*run += 1;
527efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
528efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
529efc68ce1SDavid Greenman 		}
530efc68ce1SDavid Greenman 	}
53126f9a767SRodney W. Grimes 
53226f9a767SRodney W. Grimes 	return rtaddress;
53326f9a767SRodney W. Grimes }
53426f9a767SRodney W. Grimes 
53526f9a767SRodney W. Grimes /*
53626f9a767SRodney W. Grimes  * interrupt routine for I/O completion
53726f9a767SRodney W. Grimes  */
53826f9a767SRodney W. Grimes void
53926f9a767SRodney W. Grimes vnode_pager_iodone(bp)
54026f9a767SRodney W. Grimes 	struct buf *bp;
54126f9a767SRodney W. Grimes {
54226f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
54326f9a767SRodney W. Grimes 	wakeup((caddr_t) bp);
54416f62314SDavid Greenman 	if (bp->b_flags & B_ASYNC) {
54516f62314SDavid Greenman 		vm_offset_t paddr;
54616f62314SDavid Greenman 		vm_page_t m;
54716f62314SDavid Greenman 		vm_object_t obj = 0;
54816f62314SDavid Greenman 		int i;
54916f62314SDavid Greenman 		int npages;
55016f62314SDavid Greenman 
55116f62314SDavid Greenman 		paddr = (vm_offset_t) bp->b_data;
55216f62314SDavid Greenman 		if (bp->b_bufsize != bp->b_bcount)
55316f62314SDavid Greenman 			bzero(bp->b_data + bp->b_bcount,
55416f62314SDavid Greenman 			    bp->b_bufsize - bp->b_bcount);
55516f62314SDavid Greenman 
55616f62314SDavid Greenman 		npages = (bp->b_bufsize + PAGE_SIZE - 1) / PAGE_SIZE;
55716f62314SDavid Greenman 		for (i = 0; i < npages; i++) {
55816f62314SDavid Greenman 			m = PHYS_TO_VM_PAGE(pmap_kextract(paddr + i * PAGE_SIZE));
55916f62314SDavid Greenman 			obj = m->object;
56016f62314SDavid Greenman 			if (m) {
5610d94caffSDavid Greenman 				m->dirty = 0;
5620d94caffSDavid Greenman 				m->valid = VM_PAGE_BITS_ALL;
5630d94caffSDavid Greenman 				if (m->flags & PG_WANTED)
5640d94caffSDavid Greenman 					m->flags |= PG_REFERENCED;
56516f62314SDavid Greenman 				PAGE_WAKEUP(m);
56616f62314SDavid Greenman 			} else {
56716f62314SDavid Greenman 				panic("vnode_pager_iodone: page is gone!!!");
56816f62314SDavid Greenman 			}
56916f62314SDavid Greenman 		}
570a481f200SDavid Greenman 		pmap_qremove(paddr, npages);
57116f62314SDavid Greenman 		if (obj) {
572f919ebdeSDavid Greenman 			vm_object_pip_wakeup(obj);
57316f62314SDavid Greenman 		} else {
57416f62314SDavid Greenman 			panic("vnode_pager_iodone: object is gone???");
57516f62314SDavid Greenman 		}
57616f62314SDavid Greenman 		relpbuf(bp);
57716f62314SDavid Greenman 	}
57826f9a767SRodney W. Grimes }
57926f9a767SRodney W. Grimes 
58026f9a767SRodney W. Grimes /*
58126f9a767SRodney W. Grimes  * small block file system vnode pager input
58226f9a767SRodney W. Grimes  */
58326f9a767SRodney W. Grimes int
58426f9a767SRodney W. Grimes vnode_pager_input_smlfs(vnp, m)
58526f9a767SRodney W. Grimes 	vn_pager_t vnp;
58626f9a767SRodney W. Grimes 	vm_page_t m;
58726f9a767SRodney W. Grimes {
58826f9a767SRodney W. Grimes 	int i;
58926f9a767SRodney W. Grimes 	int s;
59026f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
59126f9a767SRodney W. Grimes 	struct buf *bp;
59226f9a767SRodney W. Grimes 	vm_offset_t kva;
59326f9a767SRodney W. Grimes 	int fileaddr;
59426f9a767SRodney W. Grimes 	int block;
59526f9a767SRodney W. Grimes 	vm_offset_t bsize;
59626f9a767SRodney W. Grimes 	int error = 0;
59726f9a767SRodney W. Grimes 
59826f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
59926f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
60026f9a767SRodney W. Grimes 
6010bdb7528SDavid Greenman 
6020bdb7528SDavid Greenman 	VOP_LOCK(vp);
6030bdb7528SDavid Greenman 
6040d94caffSDavid Greenman 	VOP_BMAP(vp, 0, &dp, 0, 0);
60526f9a767SRodney W. Grimes 
60626f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
60726f9a767SRodney W. Grimes 
60826f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
609bbc0ec52SDavid Greenman 
6100d94caffSDavid Greenman 		if ((vm_page_bits(m->offset + i * bsize, bsize) & m->valid))
61126f9a767SRodney W. Grimes 			continue;
61226f9a767SRodney W. Grimes 
613efc68ce1SDavid Greenman 		fileaddr = vnode_pager_addr(vp, m->offset + i * bsize, (int *)0);
61426f9a767SRodney W. Grimes 		if (fileaddr != -1) {
61526f9a767SRodney W. Grimes 			bp = getpbuf();
61626f9a767SRodney W. Grimes 
61726f9a767SRodney W. Grimes 			/* build a minimal buffer header */
61826f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
61926f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
62026f9a767SRodney W. Grimes 			bp->b_proc = curproc;
62126f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
62226f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
62326f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
62426f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
62526f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
62626f9a767SRodney W. Grimes 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
627187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
6280d94caffSDavid Greenman 			pbgetvp(dp, bp);
62926f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
63026f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
63126f9a767SRodney W. Grimes 
63226f9a767SRodney W. Grimes 			/* do the input */
63326f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
63426f9a767SRodney W. Grimes 
63526f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
63626f9a767SRodney W. Grimes 
63726f9a767SRodney W. Grimes 			s = splbio();
63826f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
63926f9a767SRodney W. Grimes 				tsleep((caddr_t) bp, PVM, "vnsrd", 0);
64026f9a767SRodney W. Grimes 			}
64126f9a767SRodney W. Grimes 			splx(s);
64226f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
64326f9a767SRodney W. Grimes 				error = EIO;
64426f9a767SRodney W. Grimes 
64526f9a767SRodney W. Grimes 			/*
64626f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
64726f9a767SRodney W. Grimes 			 */
64826f9a767SRodney W. Grimes 			relpbuf(bp);
64926f9a767SRodney W. Grimes 			if (error)
65026f9a767SRodney W. Grimes 				break;
6510d94caffSDavid Greenman 
6520d94caffSDavid Greenman 			vm_page_set_clean(m, i * bsize, bsize);
6530d94caffSDavid Greenman 			vm_page_set_valid(m, i * bsize, bsize);
65426f9a767SRodney W. Grimes 		} else {
6550d94caffSDavid Greenman 			vm_page_set_clean(m, i * bsize, bsize);
65626f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
65726f9a767SRodney W. Grimes 		}
65826f9a767SRodney W. Grimes nextblock:
65926f9a767SRodney W. Grimes 	}
66026f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
6610bdb7528SDavid Greenman 	VOP_UNLOCK(vp);
6620d94caffSDavid Greenman 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
66326f9a767SRodney W. Grimes 	if (error) {
664a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
66526f9a767SRodney W. Grimes 	}
66626f9a767SRodney W. Grimes 	return VM_PAGER_OK;
66726f9a767SRodney W. Grimes 
66826f9a767SRodney W. Grimes }
66926f9a767SRodney W. Grimes 
67026f9a767SRodney W. Grimes 
67126f9a767SRodney W. Grimes /*
67226f9a767SRodney W. Grimes  * old style vnode pager output routine
67326f9a767SRodney W. Grimes  */
67426f9a767SRodney W. Grimes int
67526f9a767SRodney W. Grimes vnode_pager_input_old(vnp, m)
67626f9a767SRodney W. Grimes 	vn_pager_t vnp;
67726f9a767SRodney W. Grimes 	vm_page_t m;
67826f9a767SRodney W. Grimes {
679df8bae1dSRodney W. Grimes 	struct uio auio;
680df8bae1dSRodney W. Grimes 	struct iovec aiov;
68126f9a767SRodney W. Grimes 	int error;
68226f9a767SRodney W. Grimes 	int size;
68326f9a767SRodney W. Grimes 	vm_offset_t kva;
684df8bae1dSRodney W. Grimes 
68526f9a767SRodney W. Grimes 	error = 0;
686bbc0ec52SDavid Greenman 
687df8bae1dSRodney W. Grimes 	/*
68826f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
68926f9a767SRodney W. Grimes 	 */
6900d94caffSDavid Greenman 	if (m->offset >= vnp->vnp_size) {
69126f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
69226f9a767SRodney W. Grimes 	} else {
69326f9a767SRodney W. Grimes 		size = PAGE_SIZE;
6940d94caffSDavid Greenman 		if (m->offset + size > vnp->vnp_size)
6950d94caffSDavid Greenman 			size = vnp->vnp_size - m->offset;
6960bdb7528SDavid Greenman 
6970bdb7528SDavid Greenman 		VOP_LOCK(vnp->vnp_vp);
69826f9a767SRodney W. Grimes 		/*
699df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
700df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
701df8bae1dSRodney W. Grimes 		 */
70226f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
7030bdb7528SDavid Greenman 
704df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
705df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
706df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
707df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
7080d94caffSDavid Greenman 		auio.uio_offset = m->offset;
709df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
71026f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
711df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
712df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
71326f9a767SRodney W. Grimes 
71426f9a767SRodney W. Grimes 		error = VOP_READ(vnp->vnp_vp, &auio, 0, curproc->p_ucred);
715df8bae1dSRodney W. Grimes 		if (!error) {
716df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes 			if (count == 0)
719df8bae1dSRodney W. Grimes 				error = EINVAL;
72026f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
72126f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
722df8bae1dSRodney W. Grimes 		}
72326f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
7240bdb7528SDavid Greenman 		VOP_UNLOCK(vnp->vnp_vp);
725df8bae1dSRodney W. Grimes 	}
72626f9a767SRodney W. Grimes 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
7270d94caffSDavid Greenman 	m->dirty = 0;
728a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
72926f9a767SRodney W. Grimes }
73026f9a767SRodney W. Grimes 
73126f9a767SRodney W. Grimes /*
73226f9a767SRodney W. Grimes  * generic vnode pager input routine
73326f9a767SRodney W. Grimes  */
73426f9a767SRodney W. Grimes int
73526f9a767SRodney W. Grimes vnode_pager_input(vnp, m, count, reqpage)
73626f9a767SRodney W. Grimes 	register vn_pager_t vnp;
73726f9a767SRodney W. Grimes 	vm_page_t *m;
73826f9a767SRodney W. Grimes 	int count, reqpage;
73926f9a767SRodney W. Grimes {
74005f0fdd2SPoul-Henning Kamp 	int i;
74126f9a767SRodney W. Grimes 	vm_offset_t kva, foff;
7420bdb7528SDavid Greenman 	int size;
74326f9a767SRodney W. Grimes 	vm_object_t object;
74426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
74526f9a767SRodney W. Grimes 	int bsize;
74626f9a767SRodney W. Grimes 
74726f9a767SRodney W. Grimes 	int first, last;
748efc68ce1SDavid Greenman 	int firstaddr;
74926f9a767SRodney W. Grimes 	int block, offset;
750efc68ce1SDavid Greenman 	int runpg;
751efc68ce1SDavid Greenman 	int runend;
75226f9a767SRodney W. Grimes 
7530bdb7528SDavid Greenman 	struct buf *bp;
75426f9a767SRodney W. Grimes 	int s;
75526f9a767SRodney W. Grimes 	int failflag;
75626f9a767SRodney W. Grimes 
75726f9a767SRodney W. Grimes 	int error = 0;
75826f9a767SRodney W. Grimes 
759bbc0ec52SDavid Greenman 	object = m[reqpage]->object;	/* all vm_page_t items are in same
760bbc0ec52SDavid Greenman 					 * object */
76126f9a767SRodney W. Grimes 
76226f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
76326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
76426f9a767SRodney W. Grimes 
76526f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
766bbc0ec52SDavid Greenman 
76726f9a767SRodney W. Grimes 	/*
768bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
769bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
77026f9a767SRodney W. Grimes 	 */
7710d94caffSDavid Greenman 	foff = m[reqpage]->offset;
772bbc0ec52SDavid Greenman 
77326f9a767SRodney W. Grimes 	/*
77416f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
77526f9a767SRodney W. Grimes 	 */
7760d94caffSDavid Greenman 	if (VOP_BMAP(vp, 0, &dp, 0, 0)) {
77726f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
77826f9a767SRodney W. Grimes 			if (i != reqpage) {
77926f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
78026f9a767SRodney W. Grimes 			}
78126f9a767SRodney W. Grimes 		}
782976e77fcSDavid Greenman 		cnt.v_vnodein++;
783976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
78426f9a767SRodney W. Grimes 		return vnode_pager_input_old(vnp, m[reqpage]);
785bbc0ec52SDavid Greenman 
78626f9a767SRodney W. Grimes 		/*
78726f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
78826f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
78926f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
79026f9a767SRodney W. Grimes 		 */
79126f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
79226f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
79326f9a767SRodney W. Grimes 
79426f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
79526f9a767SRodney W. Grimes 			if (i != reqpage) {
79626f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
79726f9a767SRodney W. Grimes 			}
79826f9a767SRodney W. Grimes 		}
799976e77fcSDavid Greenman 		cnt.v_vnodein++;
800976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
80126f9a767SRodney W. Grimes 		return vnode_pager_input_smlfs(vnp, m[reqpage]);
80226f9a767SRodney W. Grimes 	}
80326f9a767SRodney W. Grimes 	/*
8040d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
8050d94caffSDavid Greenman 	 * then, the entire page is valid --
8060d94caffSDavid Greenman 	 */
8070d94caffSDavid Greenman 	if (m[reqpage]->valid) {
8080d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
8090d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
8100d94caffSDavid Greenman 			if (i != reqpage)
8110d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
8120d94caffSDavid Greenman 		}
8130d94caffSDavid Greenman 		return VM_PAGER_OK;
8140d94caffSDavid Greenman 	}
8150bdb7528SDavid Greenman 
8160d94caffSDavid Greenman 	/*
81726f9a767SRodney W. Grimes 	 * here on direct device I/O
81826f9a767SRodney W. Grimes 	 */
81926f9a767SRodney W. Grimes 
8200bdb7528SDavid Greenman 	VOP_LOCK(vp);
821efc68ce1SDavid Greenman 	firstaddr = -1;
82226f9a767SRodney W. Grimes 	/*
823efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
82426f9a767SRodney W. Grimes 	 */
825efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
826efc68ce1SDavid Greenman 		firstaddr = vnode_pager_addr(vp, m[i]->offset, &runpg);
827efc68ce1SDavid Greenman 		if (firstaddr == -1) {
828efc68ce1SDavid Greenman 			if( i == reqpage && foff < vnp->vnp_size) {
829efc68ce1SDavid Greenman 				printf("vnode_pager_input: unexpected missing page: firstaddr: %d, foff: %d, vnp_size: %d\n",
830efc68ce1SDavid Greenman 			   	 firstaddr, foff, vnp->vnp_size);
831efc68ce1SDavid Greenman 				panic("vnode_pager_input:...");
832efc68ce1SDavid Greenman 			}
83326f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
834efc68ce1SDavid Greenman 			runend = i + 1;
835efc68ce1SDavid Greenman 			first = runend;
836efc68ce1SDavid Greenman 			continue;
837efc68ce1SDavid Greenman 		}
838efc68ce1SDavid Greenman 		runend = i + runpg;
839efc68ce1SDavid Greenman 		if( runend <= reqpage) {
840efc68ce1SDavid Greenman 			int j;
841efc68ce1SDavid Greenman 			for(j = i; j < runend; j++) {
842efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
843efc68ce1SDavid Greenman 			}
84426f9a767SRodney W. Grimes 		} else {
845efc68ce1SDavid Greenman 			if( runpg < (count - first)) {
846efc68ce1SDavid Greenman 				for(i=first + runpg; i < count; i++)
84726f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
848efc68ce1SDavid Greenman 				count = first + runpg;
84926f9a767SRodney W. Grimes 			}
850efc68ce1SDavid Greenman 			break;
85126f9a767SRodney W. Grimes 		}
852efc68ce1SDavid Greenman 		first = runend;
853efc68ce1SDavid Greenman 	}
85426f9a767SRodney W. Grimes 
85526f9a767SRodney W. Grimes 	/*
856bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
857bbc0ec52SDavid Greenman 	 * to be zero based...
85826f9a767SRodney W. Grimes 	 */
85926f9a767SRodney W. Grimes 	if (first != 0) {
86026f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
86126f9a767SRodney W. Grimes 			m[i - first] = m[i];
86226f9a767SRodney W. Grimes 		}
86326f9a767SRodney W. Grimes 		count -= first;
86426f9a767SRodney W. Grimes 		reqpage -= first;
86526f9a767SRodney W. Grimes 	}
866efc68ce1SDavid Greenman 
86726f9a767SRodney W. Grimes 	/*
86826f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
86926f9a767SRodney W. Grimes 	 */
8700d94caffSDavid Greenman 	foff = m[0]->offset;
87126f9a767SRodney W. Grimes 
87226f9a767SRodney W. Grimes 	/*
87326f9a767SRodney W. Grimes 	 * calculate the size of the transfer
87426f9a767SRodney W. Grimes 	 */
87526f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
87626f9a767SRodney W. Grimes 	if ((foff + size) > vnp->vnp_size)
87726f9a767SRodney W. Grimes 		size = vnp->vnp_size - foff;
87826f9a767SRodney W. Grimes 
87926f9a767SRodney W. Grimes 	/*
88026f9a767SRodney W. Grimes 	 * round up physical size for real devices
88126f9a767SRodney W. Grimes 	 */
88226f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
88326f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
88426f9a767SRodney W. Grimes 
8856d40c3d3SDavid Greenman 	bp = getpbuf();
88616f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
88716f62314SDavid Greenman 
88826f9a767SRodney W. Grimes 	/*
88926f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
89026f9a767SRodney W. Grimes 	 */
89116f62314SDavid Greenman 	pmap_qenter(kva, m, count);
89226f9a767SRodney W. Grimes 
89326f9a767SRodney W. Grimes 	/* build a minimal buffer header */
89426f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
89526f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
89626f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
89726f9a767SRodney W. Grimes 	bp->b_proc = curproc;
89826f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
89926f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
90026f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
90126f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
90226f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
903187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
9040d94caffSDavid Greenman 	pbgetvp(dp, bp);
90526f9a767SRodney W. Grimes 	bp->b_bcount = size;
90626f9a767SRodney W. Grimes 	bp->b_bufsize = size;
90726f9a767SRodney W. Grimes 
908976e77fcSDavid Greenman 	cnt.v_vnodein++;
909976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
910976e77fcSDavid Greenman 
91126f9a767SRodney W. Grimes 	/* do the input */
91226f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
913976e77fcSDavid Greenman 
91426f9a767SRodney W. Grimes 	s = splbio();
91526f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
91626f9a767SRodney W. Grimes 
91726f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
91826f9a767SRodney W. Grimes 		tsleep((caddr_t) bp, PVM, "vnread", 0);
91926f9a767SRodney W. Grimes 	}
92026f9a767SRodney W. Grimes 	splx(s);
92126f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
92226f9a767SRodney W. Grimes 		error = EIO;
92326f9a767SRodney W. Grimes 
92426f9a767SRodney W. Grimes 	if (!error) {
92526f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
92626f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
92726f9a767SRodney W. Grimes 	}
92816f62314SDavid Greenman 	pmap_qremove(kva, count);
92926f9a767SRodney W. Grimes 
93026f9a767SRodney W. Grimes 	/*
93126f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
93226f9a767SRodney W. Grimes 	 */
93326f9a767SRodney W. Grimes 	relpbuf(bp);
9340bdb7528SDavid Greenman 	VOP_UNLOCK(vp);
93526f9a767SRodney W. Grimes 
93626f9a767SRodney W. Grimes finishup:
93726f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
938fff93ab6SDavid Greenman 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
9390d94caffSDavid Greenman 		m[i]->dirty = 0;
9400d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
94126f9a767SRodney W. Grimes 		if (i != reqpage) {
942bbc0ec52SDavid Greenman 
94326f9a767SRodney W. Grimes 			/*
944bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
945bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
946bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
947bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
948bbc0ec52SDavid Greenman 			 * deactivating pages is best.
94926f9a767SRodney W. Grimes 			 */
950bbc0ec52SDavid Greenman 
95126f9a767SRodney W. Grimes 			/*
952bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
953bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
95426f9a767SRodney W. Grimes 			 */
95526f9a767SRodney W. Grimes 			if (!error) {
95626f9a767SRodney W. Grimes 				vm_page_deactivate(m[i]);
95726f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
95826f9a767SRodney W. Grimes 			} else {
95926f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
96026f9a767SRodney W. Grimes 			}
96126f9a767SRodney W. Grimes 		}
96226f9a767SRodney W. Grimes 	}
96326f9a767SRodney W. Grimes 	if (error) {
964a83c285cSDavid Greenman 		printf("vnode_pager_input: I/O read error\n");
96526f9a767SRodney W. Grimes 	}
966a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
96726f9a767SRodney W. Grimes }
96826f9a767SRodney W. Grimes 
96926f9a767SRodney W. Grimes /*
97026f9a767SRodney W. Grimes  * old-style vnode pager output routine
97126f9a767SRodney W. Grimes  */
97226f9a767SRodney W. Grimes int
97326f9a767SRodney W. Grimes vnode_pager_output_old(vnp, m)
97426f9a767SRodney W. Grimes 	register vn_pager_t vnp;
97526f9a767SRodney W. Grimes 	vm_page_t m;
97626f9a767SRodney W. Grimes {
9770d94caffSDavid Greenman 	vm_offset_t kva, kva2;
97826f9a767SRodney W. Grimes 	vm_offset_t size;
97926f9a767SRodney W. Grimes 	struct iovec aiov;
98026f9a767SRodney W. Grimes 	struct uio auio;
98126f9a767SRodney W. Grimes 	struct vnode *vp;
98226f9a767SRodney W. Grimes 	int error;
98326f9a767SRodney W. Grimes 
98426f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
985bbc0ec52SDavid Greenman 
98626f9a767SRodney W. Grimes 	/*
9870d94caffSDavid Greenman 	 * Dont return failure if beyond current EOF placate the VM system.
98826f9a767SRodney W. Grimes 	 */
9890d94caffSDavid Greenman 	if (m->offset >= vnp->vnp_size) {
9900d94caffSDavid Greenman 		return VM_PAGER_OK;
99126f9a767SRodney W. Grimes 	} else {
99226f9a767SRodney W. Grimes 		size = PAGE_SIZE;
9930d94caffSDavid Greenman 		if (m->offset + size > vnp->vnp_size)
9940d94caffSDavid Greenman 			size = vnp->vnp_size - m->offset;
9950d94caffSDavid Greenman 
9960d94caffSDavid Greenman 		kva2 = kmem_alloc(pager_map, PAGE_SIZE);
99726f9a767SRodney W. Grimes 		/*
99826f9a767SRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
99926f9a767SRodney W. Grimes 		 * we can use VOP_WRITE routines.
100026f9a767SRodney W. Grimes 		 */
100126f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
10020d94caffSDavid Greenman 		bcopy((caddr_t) kva, (caddr_t) kva2, size);
10030d94caffSDavid Greenman 		vm_pager_unmap_page(kva);
10040d94caffSDavid Greenman 		pmap_clear_modify(VM_PAGE_TO_PHYS(m));
10050d94caffSDavid Greenman 		PAGE_WAKEUP(m);
10060d94caffSDavid Greenman 
10070d94caffSDavid Greenman 		aiov.iov_base = (caddr_t) kva2;
100826f9a767SRodney W. Grimes 		aiov.iov_len = size;
100926f9a767SRodney W. Grimes 		auio.uio_iov = &aiov;
101026f9a767SRodney W. Grimes 		auio.uio_iovcnt = 1;
10110d94caffSDavid Greenman 		auio.uio_offset = m->offset;
101226f9a767SRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
101326f9a767SRodney W. Grimes 		auio.uio_rw = UIO_WRITE;
101426f9a767SRodney W. Grimes 		auio.uio_resid = size;
101526f9a767SRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
101626f9a767SRodney W. Grimes 
101726f9a767SRodney W. Grimes 		error = VOP_WRITE(vp, &auio, 0, curproc->p_ucred);
101826f9a767SRodney W. Grimes 
10190d94caffSDavid Greenman 		kmem_free_wakeup(pager_map, kva2, PAGE_SIZE);
102026f9a767SRodney W. Grimes 		if (!error) {
102126f9a767SRodney W. Grimes 			if ((size - auio.uio_resid) == 0) {
102226f9a767SRodney W. Grimes 				error = EINVAL;
102326f9a767SRodney W. Grimes 			}
102426f9a767SRodney W. Grimes 		}
10250bdb7528SDavid Greenman 
1026a83c285cSDavid Greenman 		return error ? VM_PAGER_ERROR : VM_PAGER_OK;
102726f9a767SRodney W. Grimes 	}
102826f9a767SRodney W. Grimes }
102926f9a767SRodney W. Grimes 
103026f9a767SRodney W. Grimes /*
103126f9a767SRodney W. Grimes  * vnode pager output on a small-block file system
103226f9a767SRodney W. Grimes  */
103326f9a767SRodney W. Grimes int
103426f9a767SRodney W. Grimes vnode_pager_output_smlfs(vnp, m)
103526f9a767SRodney W. Grimes 	vn_pager_t vnp;
103626f9a767SRodney W. Grimes 	vm_page_t m;
103726f9a767SRodney W. Grimes {
103826f9a767SRodney W. Grimes 	int i;
103926f9a767SRodney W. Grimes 	int s;
104026f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
104126f9a767SRodney W. Grimes 	struct buf *bp;
104226f9a767SRodney W. Grimes 	vm_offset_t kva;
104326f9a767SRodney W. Grimes 	int fileaddr;
104426f9a767SRodney W. Grimes 	vm_offset_t bsize;
104526f9a767SRodney W. Grimes 	int error = 0;
104626f9a767SRodney W. Grimes 
104726f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
104826f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
104926f9a767SRodney W. Grimes 
10500d94caffSDavid Greenman 	VOP_BMAP(vp, 0, &dp, 0, 0);
105126f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
105226f9a767SRodney W. Grimes 	for (i = 0; !error && i < (PAGE_SIZE / bsize); i++) {
1053bbc0ec52SDavid Greenman 
10540d94caffSDavid Greenman 		if ((vm_page_bits(m->offset + i * bsize, bsize) & m->valid & m->dirty) == 0)
10550d94caffSDavid Greenman 			continue;
105626f9a767SRodney W. Grimes 		/*
105726f9a767SRodney W. Grimes 		 * calculate logical block and offset
105826f9a767SRodney W. Grimes 		 */
1059efc68ce1SDavid Greenman 		fileaddr = vnode_pager_addr(vp, m->offset + i * bsize, (int *)0);
106026f9a767SRodney W. Grimes 		if (fileaddr != -1) {
106126f9a767SRodney W. Grimes 
106226f9a767SRodney W. Grimes 			bp = getpbuf();
106326f9a767SRodney W. Grimes 
106426f9a767SRodney W. Grimes 			/* build a minimal buffer header */
106526f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_CALL | B_WRITE;
106626f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
106726f9a767SRodney W. Grimes 			bp->b_proc = curproc;
106826f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
106926f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
107026f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
107126f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
107226f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
107326f9a767SRodney W. Grimes 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
1074187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
10750d94caffSDavid Greenman 			pbgetvp(dp, bp);
107626f9a767SRodney W. Grimes 			++dp->v_numoutput;
107726f9a767SRodney W. Grimes 			/* for NFS */
107826f9a767SRodney W. Grimes 			bp->b_dirtyoff = 0;
107926f9a767SRodney W. Grimes 			bp->b_dirtyend = bsize;
108026f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
108126f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
108226f9a767SRodney W. Grimes 
108326f9a767SRodney W. Grimes 			/* do the input */
108426f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
108526f9a767SRodney W. Grimes 
108626f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
108726f9a767SRodney W. Grimes 
108826f9a767SRodney W. Grimes 			s = splbio();
108926f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
109026f9a767SRodney W. Grimes 				tsleep((caddr_t) bp, PVM, "vnswrt", 0);
109126f9a767SRodney W. Grimes 			}
109226f9a767SRodney W. Grimes 			splx(s);
109326f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
109426f9a767SRodney W. Grimes 				error = EIO;
109526f9a767SRodney W. Grimes 
10960d94caffSDavid Greenman 			vm_page_set_clean(m, i * bsize, bsize);
109726f9a767SRodney W. Grimes 			/*
109826f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
109926f9a767SRodney W. Grimes 			 */
110026f9a767SRodney W. Grimes 			relpbuf(bp);
110126f9a767SRodney W. Grimes 		}
110226f9a767SRodney W. Grimes 	}
110326f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
110426f9a767SRodney W. Grimes 	if (error)
1105a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
110626f9a767SRodney W. Grimes 	else
110726f9a767SRodney W. Grimes 		return VM_PAGER_OK;
110826f9a767SRodney W. Grimes }
110926f9a767SRodney W. Grimes 
111026f9a767SRodney W. Grimes /*
111126f9a767SRodney W. Grimes  * generic vnode pager output routine
111226f9a767SRodney W. Grimes  */
111326f9a767SRodney W. Grimes int
111426f9a767SRodney W. Grimes vnode_pager_output(vnp, m, count, rtvals)
111526f9a767SRodney W. Grimes 	vn_pager_t vnp;
111626f9a767SRodney W. Grimes 	vm_page_t *m;
111726f9a767SRodney W. Grimes 	int count;
111826f9a767SRodney W. Grimes 	int *rtvals;
111926f9a767SRodney W. Grimes {
112026f9a767SRodney W. Grimes 	int i, j;
112126f9a767SRodney W. Grimes 	vm_offset_t kva, foff;
112226f9a767SRodney W. Grimes 	int size;
112326f9a767SRodney W. Grimes 	vm_object_t object;
112426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
112526f9a767SRodney W. Grimes 	struct buf *bp;
112626f9a767SRodney W. Grimes 	vm_offset_t reqaddr;
112726f9a767SRodney W. Grimes 	int bsize;
112826f9a767SRodney W. Grimes 	int s;
11290d94caffSDavid Greenman 	daddr_t block;
11300d94caffSDavid Greenman 	struct timeval tv;
1131efc68ce1SDavid Greenman 	int runpg;
113226f9a767SRodney W. Grimes 
113326f9a767SRodney W. Grimes 	int error = 0;
113426f9a767SRodney W. Grimes 
113526f9a767SRodney W. Grimes retryoutput:
113626f9a767SRodney W. Grimes 	object = m[0]->object;	/* all vm_page_t items are in same object */
113726f9a767SRodney W. Grimes 
113826f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
11394abc71c0SDavid Greenman 
11404abc71c0SDavid Greenman 	/*
11414abc71c0SDavid Greenman 	 * Make sure underlying filesystem is still mounted.
11424abc71c0SDavid Greenman 	 */
11434abc71c0SDavid Greenman 	if (vp->v_mount == NULL)
11444abc71c0SDavid Greenman 		return VM_PAGER_FAIL;
11454abc71c0SDavid Greenman 
114626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
114726f9a767SRodney W. Grimes 
114826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
114926f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
115026f9a767SRodney W. Grimes 
11510d94caffSDavid Greenman 	if ((int) m[0]->offset < 0) {
11520d94caffSDavid Greenman 		printf("vnode_pager_output: attempt to write meta-data!!! -- 0x%x\n", m[0]->offset);
11530d94caffSDavid Greenman 		m[0]->dirty = 0;
11540d94caffSDavid Greenman 		rtvals[0] = VM_PAGER_OK;
11550d94caffSDavid Greenman 		return VM_PAGER_OK;
11560d94caffSDavid Greenman 	}
11570bdb7528SDavid Greenman 
11580bdb7528SDavid Greenman 	VOP_LOCK(vp);
115926f9a767SRodney W. Grimes 	/*
1160bbc0ec52SDavid Greenman 	 * if the filesystem does not have a bmap, then use the old code
116126f9a767SRodney W. Grimes 	 */
11620d94caffSDavid Greenman 	if (VOP_BMAP(vp, (m[0]->offset / bsize), &dp, &block, 0) ||
11630d94caffSDavid Greenman 	    (block == -1)) {
116426f9a767SRodney W. Grimes 
116526f9a767SRodney W. Grimes 		rtvals[0] = vnode_pager_output_old(vnp, m[0]);
116626f9a767SRodney W. Grimes 
11670d94caffSDavid Greenman 		m[0]->dirty = 0;
1168976e77fcSDavid Greenman 		cnt.v_vnodeout++;
1169976e77fcSDavid Greenman 		cnt.v_vnodepgsout++;
11700bdb7528SDavid Greenman 		VOP_UNLOCK(vp);
117126f9a767SRodney W. Grimes 		return rtvals[0];
117226f9a767SRodney W. Grimes 	}
11730d94caffSDavid Greenman 	tv = time;
11740d94caffSDavid Greenman 	VOP_UPDATE(vp, &tv, &tv, 0);
117526f9a767SRodney W. Grimes 
117626f9a767SRodney W. Grimes 	/*
1177bbc0ec52SDavid Greenman 	 * if the filesystem has a small blocksize, then use the small block
1178bbc0ec52SDavid Greenman 	 * filesystem output code
117926f9a767SRodney W. Grimes 	 */
118026f9a767SRodney W. Grimes 	if ((bsize < PAGE_SIZE) &&
118126f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
118226f9a767SRodney W. Grimes 
118326f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
118426f9a767SRodney W. Grimes 			rtvals[i] = vnode_pager_output_smlfs(vnp, m[i]);
118526f9a767SRodney W. Grimes 			if (rtvals[i] == VM_PAGER_OK) {
118626f9a767SRodney W. Grimes 				pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
118726f9a767SRodney W. Grimes 			}
118826f9a767SRodney W. Grimes 		}
1189976e77fcSDavid Greenman 		cnt.v_vnodeout++;
1190976e77fcSDavid Greenman 		cnt.v_vnodepgsout += count;
11910bdb7528SDavid Greenman 		VOP_UNLOCK(vp);
119226f9a767SRodney W. Grimes 		return rtvals[0];
119326f9a767SRodney W. Grimes 	}
119426f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
11950d94caffSDavid Greenman 		foff = m[i]->offset;
119626f9a767SRodney W. Grimes 		if (foff >= vnp->vnp_size) {
119726f9a767SRodney W. Grimes 			for (j = i; j < count; j++)
119826f9a767SRodney W. Grimes 				rtvals[j] = VM_PAGER_BAD;
119926f9a767SRodney W. Grimes 			count = i;
120026f9a767SRodney W. Grimes 			break;
120126f9a767SRodney W. Grimes 		}
120226f9a767SRodney W. Grimes 	}
120326f9a767SRodney W. Grimes 	if (count == 0) {
12040bdb7528SDavid Greenman 		VOP_UNLOCK(vp);
120526f9a767SRodney W. Grimes 		return rtvals[0];
120626f9a767SRodney W. Grimes 	}
12070d94caffSDavid Greenman 	foff = m[0]->offset;
1208efc68ce1SDavid Greenman 	reqaddr = vnode_pager_addr(vp, foff, &runpg);
1209efc68ce1SDavid Greenman 	if( runpg < count)
1210efc68ce1SDavid Greenman 		count = runpg;
121126f9a767SRodney W. Grimes 
121226f9a767SRodney W. Grimes 	/*
121326f9a767SRodney W. Grimes 	 * calculate the size of the transfer
121426f9a767SRodney W. Grimes 	 */
121526f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
121626f9a767SRodney W. Grimes 	if ((foff + size) > vnp->vnp_size)
121726f9a767SRodney W. Grimes 		size = vnp->vnp_size - foff;
121826f9a767SRodney W. Grimes 
121926f9a767SRodney W. Grimes 	/*
122026f9a767SRodney W. Grimes 	 * round up physical size for real devices
122126f9a767SRodney W. Grimes 	 */
122226f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
122326f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
122426f9a767SRodney W. Grimes 
122516f62314SDavid Greenman 	bp = getpbuf();
122616f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
122726f9a767SRodney W. Grimes 	/*
122826f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
122926f9a767SRodney W. Grimes 	 */
123016f62314SDavid Greenman 	pmap_qenter(kva, m, count);
1231bbc0ec52SDavid Greenman 
123226f9a767SRodney W. Grimes 	/* build a minimal buffer header */
123326f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_WRITE | B_CALL;
123426f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
123526f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
123626f9a767SRodney W. Grimes 	bp->b_proc = curproc;
123726f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
123826f9a767SRodney W. Grimes 
123926f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
124026f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
124126f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
124226f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
1243187f0071SDavid Greenman 	bp->b_blkno = reqaddr;
12440d94caffSDavid Greenman 	pbgetvp(dp, bp);
124526f9a767SRodney W. Grimes 	++dp->v_numoutput;
124626f9a767SRodney W. Grimes 
124726f9a767SRodney W. Grimes 	/* for NFS */
124826f9a767SRodney W. Grimes 	bp->b_dirtyoff = 0;
124926f9a767SRodney W. Grimes 	bp->b_dirtyend = size;
125026f9a767SRodney W. Grimes 
125126f9a767SRodney W. Grimes 	bp->b_bcount = size;
125226f9a767SRodney W. Grimes 	bp->b_bufsize = size;
125326f9a767SRodney W. Grimes 
1254976e77fcSDavid Greenman 	cnt.v_vnodeout++;
1255976e77fcSDavid Greenman 	cnt.v_vnodepgsout += count;
1256976e77fcSDavid Greenman 
125726f9a767SRodney W. Grimes 	/* do the output */
125826f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
125926f9a767SRodney W. Grimes 
126026f9a767SRodney W. Grimes 	s = splbio();
126126f9a767SRodney W. Grimes 
126226f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
126326f9a767SRodney W. Grimes 
126426f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
126526f9a767SRodney W. Grimes 		tsleep((caddr_t) bp, PVM, "vnwrite", 0);
126626f9a767SRodney W. Grimes 	}
126726f9a767SRodney W. Grimes 	splx(s);
126826f9a767SRodney W. Grimes 
126926f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
127026f9a767SRodney W. Grimes 		error = EIO;
127126f9a767SRodney W. Grimes 
127216f62314SDavid Greenman 	pmap_qremove(kva, count);
127326f9a767SRodney W. Grimes 
127426f9a767SRodney W. Grimes 	/*
127526f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
127626f9a767SRodney W. Grimes 	 */
127726f9a767SRodney W. Grimes 	relpbuf(bp);
127826f9a767SRodney W. Grimes 
12790bdb7528SDavid Greenman 	VOP_UNLOCK(vp);
12800bdb7528SDavid Greenman 
128126f9a767SRodney W. Grimes 	if (!error) {
128226f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
128326f9a767SRodney W. Grimes 			pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
12840d94caffSDavid Greenman 			m[i]->dirty = 0;
128526f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
128626f9a767SRodney W. Grimes 		}
128726f9a767SRodney W. Grimes 	} else if (count != 1) {
128826f9a767SRodney W. Grimes 		error = 0;
128926f9a767SRodney W. Grimes 		count = 1;
129026f9a767SRodney W. Grimes 		goto retryoutput;
129126f9a767SRodney W. Grimes 	}
129226f9a767SRodney W. Grimes 	if (error) {
1293a83c285cSDavid Greenman 		printf("vnode_pager_output: I/O write error\n");
129426f9a767SRodney W. Grimes 	}
1295a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
129626f9a767SRodney W. Grimes }
1297