xref: /freebsd/sys/vm/vnode_pager.c (revision efc68ce10f46977b8efc20a117e1474ec0ffd87d)
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
40efc68ce1SDavid Greenman  *	$Id: vnode_pager.c,v 1.21 1995/01/24 10:14:09 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 vm_pager_t vnode_pager_alloc(caddr_t, vm_offset_t, vm_prot_t, vm_offset_t);
8926f9a767SRodney W. Grimes void vnode_pager_dealloc();
9026f9a767SRodney W. Grimes int vnode_pager_getpage();
9126f9a767SRodney W. Grimes int vnode_pager_getmulti();
9226f9a767SRodney W. Grimes int vnode_pager_putpage();
9326f9a767SRodney W. Grimes boolean_t vnode_pager_haspage();
94df8bae1dSRodney W. Grimes 
95df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
96df8bae1dSRodney W. Grimes 	vnode_pager_init,
97df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
98df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
99df8bae1dSRodney W. Grimes 	vnode_pager_getpage,
10026f9a767SRodney W. Grimes 	vnode_pager_getmulti,
101df8bae1dSRodney W. Grimes 	vnode_pager_putpage,
10226f9a767SRodney W. Grimes 	vnode_pager_putmulti,
10326f9a767SRodney W. Grimes 	vnode_pager_haspage
104df8bae1dSRodney W. Grimes };
105df8bae1dSRodney W. Grimes 
10616f62314SDavid Greenman 
10716f62314SDavid Greenman 
10826f9a767SRodney W. Grimes static int vnode_pager_input(vn_pager_t vnp, vm_page_t * m, int count, int reqpage);
10926f9a767SRodney W. Grimes static int vnode_pager_output(vn_pager_t vnp, vm_page_t * m, int count, int *rtvals);
11026f9a767SRodney W. Grimes 
11126f9a767SRodney W. Grimes extern vm_map_t pager_map;
11226f9a767SRodney W. Grimes 
11326f9a767SRodney W. Grimes struct pagerlst vnode_pager_list;	/* list of managed vnodes */
11426f9a767SRodney W. Grimes 
11526f9a767SRodney W. Grimes #define MAXBP (PAGE_SIZE/DEV_BSIZE);
11626f9a767SRodney W. Grimes 
11726f9a767SRodney W. Grimes void
118df8bae1dSRodney W. Grimes vnode_pager_init()
119df8bae1dSRodney W. Grimes {
120df8bae1dSRodney W. Grimes 	TAILQ_INIT(&vnode_pager_list);
121df8bae1dSRodney W. Grimes }
122df8bae1dSRodney W. Grimes 
123df8bae1dSRodney W. Grimes /*
124df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
125df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
126df8bae1dSRodney W. Grimes  */
12726f9a767SRodney W. Grimes vm_pager_t
12826f9a767SRodney W. Grimes vnode_pager_alloc(handle, size, prot, offset)
129df8bae1dSRodney W. Grimes 	caddr_t handle;
130df8bae1dSRodney W. Grimes 	vm_size_t size;
131df8bae1dSRodney W. Grimes 	vm_prot_t prot;
13226f9a767SRodney W. Grimes 	vm_offset_t offset;
133df8bae1dSRodney W. Grimes {
134df8bae1dSRodney W. Grimes 	register vm_pager_t pager;
135df8bae1dSRodney W. Grimes 	register vn_pager_t vnp;
1360d94caffSDavid Greenman 	vm_object_t object, tobject;
137df8bae1dSRodney W. Grimes 	struct vattr vattr;
138df8bae1dSRodney W. Grimes 	struct vnode *vp;
139df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
1400d94caffSDavid Greenman 	int rtval;
141df8bae1dSRodney W. Grimes 
142df8bae1dSRodney W. Grimes 	/*
143df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
144df8bae1dSRodney W. Grimes 	 */
145df8bae1dSRodney W. Grimes 	if (handle == NULL)
146df8bae1dSRodney W. Grimes 		return (NULL);
147df8bae1dSRodney W. Grimes 
148df8bae1dSRodney W. Grimes 	/*
149bbc0ec52SDavid Greenman 	 * Vnodes keep a pointer to any associated pager so no need to lookup
150bbc0ec52SDavid Greenman 	 * with vm_pager_lookup.
151df8bae1dSRodney W. Grimes 	 */
152df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
1530d94caffSDavid Greenman 	while ((object = (vm_object_t) vp->v_vmdata) && (object->flags & OBJ_DEAD))
1540d94caffSDavid Greenman 		tsleep((caddr_t) object, PVM, "vadead", 0);
1550d94caffSDavid Greenman 
1568e58bf68SDavid Greenman 	pager = NULL;
1578e58bf68SDavid Greenman 	if (object != NULL)
1588e58bf68SDavid Greenman 		pager = object->pager;
159df8bae1dSRodney W. Grimes 	if (pager == NULL) {
160bbc0ec52SDavid Greenman 
161df8bae1dSRodney W. Grimes 		/*
162df8bae1dSRodney W. Grimes 		 * Allocate pager structures
163df8bae1dSRodney W. Grimes 		 */
164df8bae1dSRodney W. Grimes 		pager = (vm_pager_t) malloc(sizeof *pager, M_VMPAGER, M_WAITOK);
165df8bae1dSRodney W. Grimes 		if (pager == NULL)
166df8bae1dSRodney W. Grimes 			return (NULL);
167df8bae1dSRodney W. Grimes 		vnp = (vn_pager_t) malloc(sizeof *vnp, M_VMPGDATA, M_WAITOK);
168df8bae1dSRodney W. Grimes 		if (vnp == NULL) {
169df8bae1dSRodney W. Grimes 			free((caddr_t) pager, M_VMPAGER);
170df8bae1dSRodney W. Grimes 			return (NULL);
171df8bae1dSRodney W. Grimes 		}
172df8bae1dSRodney W. Grimes 		/*
173df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
174df8bae1dSRodney W. Grimes 		 */
1750d94caffSDavid Greenman 		if ((rtval = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) == 0) {
176df8bae1dSRodney W. Grimes 			object = vm_object_allocate(round_page(vattr.va_size));
177a7489784SDavid Greenman 			object->flags &= ~OBJ_INTERNAL;
178df8bae1dSRodney W. Grimes 			vm_object_enter(object, pager);
179df8bae1dSRodney W. Grimes 			vm_object_setpager(object, pager, 0, TRUE);
180df8bae1dSRodney W. Grimes 		} else {
1810d94caffSDavid Greenman 			printf("Error in getattr: %d\n", rtval);
182df8bae1dSRodney W. Grimes 			free((caddr_t) vnp, M_VMPGDATA);
183df8bae1dSRodney W. Grimes 			free((caddr_t) pager, M_VMPAGER);
184df8bae1dSRodney W. Grimes 			return (NULL);
185df8bae1dSRodney W. Grimes 		}
186bbc0ec52SDavid Greenman 
187df8bae1dSRodney W. Grimes 		/*
188df8bae1dSRodney W. Grimes 		 * Hold a reference to the vnode and initialize pager data.
189df8bae1dSRodney W. Grimes 		 */
190df8bae1dSRodney W. Grimes 		VREF(vp);
191df8bae1dSRodney W. Grimes 		vnp->vnp_flags = 0;
192df8bae1dSRodney W. Grimes 		vnp->vnp_vp = vp;
193df8bae1dSRodney W. Grimes 		vnp->vnp_size = vattr.va_size;
19426f9a767SRodney W. Grimes 
195df8bae1dSRodney W. Grimes 		TAILQ_INSERT_TAIL(&vnode_pager_list, pager, pg_list);
196df8bae1dSRodney W. Grimes 		pager->pg_handle = handle;
197df8bae1dSRodney W. Grimes 		pager->pg_type = PG_VNODE;
198df8bae1dSRodney W. Grimes 		pager->pg_ops = &vnodepagerops;
19926f9a767SRodney W. Grimes 		pager->pg_data = (caddr_t) vnp;
2008e58bf68SDavid Greenman 		vp->v_vmdata = (caddr_t) object;
201df8bae1dSRodney W. Grimes 	} else {
202bbc0ec52SDavid Greenman 
203df8bae1dSRodney W. Grimes 		/*
204bbc0ec52SDavid Greenman 		 * vm_object_lookup() will remove the object from the cache if
205bbc0ec52SDavid Greenman 		 * found and also gain a reference to the object.
206df8bae1dSRodney W. Grimes 		 */
2078e58bf68SDavid Greenman 		(void) vm_object_lookup(pager);
208df8bae1dSRodney W. Grimes 	}
209df8bae1dSRodney W. Grimes 	return (pager);
210df8bae1dSRodney W. Grimes }
211df8bae1dSRodney W. Grimes 
21226f9a767SRodney W. Grimes void
213df8bae1dSRodney W. Grimes vnode_pager_dealloc(pager)
214df8bae1dSRodney W. Grimes 	vm_pager_t pager;
215df8bae1dSRodney W. Grimes {
216df8bae1dSRodney W. Grimes 	register vn_pager_t vnp = (vn_pager_t) pager->pg_data;
217df8bae1dSRodney W. Grimes 	register struct vnode *vp;
2180d94caffSDavid Greenman 	vm_object_t object;
219df8bae1dSRodney W. Grimes 
22005f0fdd2SPoul-Henning Kamp 	vp = vnp->vnp_vp;
22105f0fdd2SPoul-Henning Kamp 	if (vp) {
2220d94caffSDavid Greenman 		int s = splbio();
2230d94caffSDavid Greenman 
2240d94caffSDavid Greenman 		object = (vm_object_t) vp->v_vmdata;
2250d94caffSDavid Greenman 		if (object) {
2260d94caffSDavid Greenman 			while (object->paging_in_progress) {
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);
234df8bae1dSRodney W. Grimes 		vrele(vp);
235df8bae1dSRodney W. Grimes 	}
236df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vnode_pager_list, pager, pg_list);
237df8bae1dSRodney W. Grimes 	free((caddr_t) vnp, M_VMPGDATA);
238df8bae1dSRodney W. Grimes 	free((caddr_t) pager, M_VMPAGER);
239df8bae1dSRodney W. Grimes }
240df8bae1dSRodney W. Grimes 
24126f9a767SRodney W. Grimes int
24226f9a767SRodney W. Grimes vnode_pager_getmulti(pager, m, count, reqpage, sync)
243df8bae1dSRodney W. Grimes 	vm_pager_t pager;
24426f9a767SRodney W. Grimes 	vm_page_t *m;
24526f9a767SRodney W. Grimes 	int count;
24626f9a767SRodney W. Grimes 	int reqpage;
247df8bae1dSRodney W. Grimes 	boolean_t sync;
248df8bae1dSRodney W. Grimes {
249df8bae1dSRodney W. Grimes 
25026f9a767SRodney W. Grimes 	return vnode_pager_input((vn_pager_t) pager->pg_data, m, count, reqpage);
251df8bae1dSRodney W. Grimes }
252df8bae1dSRodney W. Grimes 
25326f9a767SRodney W. Grimes int
25426f9a767SRodney W. Grimes vnode_pager_getpage(pager, m, sync)
255df8bae1dSRodney W. Grimes 	vm_pager_t pager;
25626f9a767SRodney W. Grimes 	vm_page_t m;
25726f9a767SRodney W. Grimes 	boolean_t sync;
25826f9a767SRodney W. Grimes {
25926f9a767SRodney W. Grimes 
26026f9a767SRodney W. Grimes 	vm_page_t marray[1];
261bbc0ec52SDavid Greenman 
26226f9a767SRodney W. Grimes 	if (pager == NULL)
26326f9a767SRodney W. Grimes 		return FALSE;
26426f9a767SRodney W. Grimes 	marray[0] = m;
26526f9a767SRodney W. Grimes 
26626f9a767SRodney W. Grimes 	return vnode_pager_input((vn_pager_t) pager->pg_data, marray, 1, 0);
26726f9a767SRodney W. Grimes }
26826f9a767SRodney W. Grimes 
26926f9a767SRodney W. Grimes boolean_t
27026f9a767SRodney W. Grimes vnode_pager_putpage(pager, m, sync)
27126f9a767SRodney W. Grimes 	vm_pager_t pager;
27226f9a767SRodney W. Grimes 	vm_page_t m;
273df8bae1dSRodney W. Grimes 	boolean_t sync;
274df8bae1dSRodney W. Grimes {
27526f9a767SRodney W. Grimes 	vm_page_t marray[1];
27626f9a767SRodney W. Grimes 	int rtvals[1];
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes 	if (pager == NULL)
27926f9a767SRodney W. Grimes 		return FALSE;
28026f9a767SRodney W. Grimes 	marray[0] = m;
28126f9a767SRodney W. Grimes 	vnode_pager_output((vn_pager_t) pager->pg_data, marray, 1, rtvals);
28226f9a767SRodney W. Grimes 	return rtvals[0];
283df8bae1dSRodney W. Grimes }
284df8bae1dSRodney W. Grimes 
28526f9a767SRodney W. Grimes int
28626f9a767SRodney W. Grimes vnode_pager_putmulti(pager, m, c, sync, rtvals)
28726f9a767SRodney W. Grimes 	vm_pager_t pager;
28826f9a767SRodney W. Grimes 	vm_page_t *m;
28926f9a767SRodney W. Grimes 	int c;
29026f9a767SRodney W. Grimes 	boolean_t sync;
29126f9a767SRodney W. Grimes 	int *rtvals;
29226f9a767SRodney W. Grimes {
29326f9a767SRodney W. Grimes 	return vnode_pager_output((vn_pager_t) pager->pg_data, m, c, rtvals);
29426f9a767SRodney W. Grimes }
29526f9a767SRodney W. Grimes 
29626f9a767SRodney W. Grimes 
29726f9a767SRodney W. Grimes boolean_t
298df8bae1dSRodney W. Grimes vnode_pager_haspage(pager, offset)
299df8bae1dSRodney W. Grimes 	vm_pager_t pager;
300df8bae1dSRodney W. Grimes 	vm_offset_t offset;
301df8bae1dSRodney W. Grimes {
302df8bae1dSRodney W. Grimes 	register vn_pager_t vnp = (vn_pager_t) pager->pg_data;
3034abc71c0SDavid Greenman 	register struct vnode *vp = vnp->vnp_vp;
304df8bae1dSRodney W. Grimes 	daddr_t bn;
305df8bae1dSRodney W. Grimes 	int err;
306317205caSDavid Greenman 	daddr_t block;
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes 	/*
3090d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
3100d94caffSDavid Greenman 	 * not have the page.
311df8bae1dSRodney W. Grimes 	 */
3124abc71c0SDavid Greenman 	if ((vp->v_mount == NULL) || (offset >= vnp->vnp_size))
3134abc71c0SDavid Greenman 		return FALSE;
314df8bae1dSRodney W. Grimes 
3154abc71c0SDavid Greenman 	block = offset / vp->v_mount->mnt_stat.f_iosize;
3164abc71c0SDavid Greenman 	if (incore(vp, block))
317317205caSDavid Greenman 		return TRUE;
318df8bae1dSRodney W. Grimes 	/*
319bbc0ec52SDavid Greenman 	 * Read the index to find the disk block to read from.  If there is no
320bbc0ec52SDavid Greenman 	 * block, report that we don't have this data.
321df8bae1dSRodney W. Grimes 	 *
322df8bae1dSRodney W. Grimes 	 * Assumes that the vnode has whole page or nothing.
323df8bae1dSRodney W. Grimes 	 */
3244abc71c0SDavid Greenman 	err = VOP_BMAP(vp, block, (struct vnode **) 0, &bn, 0);
3250d94caffSDavid Greenman 	if (err)
326df8bae1dSRodney W. Grimes 		return (TRUE);
327df8bae1dSRodney W. Grimes 	return ((long) bn < 0 ? FALSE : TRUE);
328df8bae1dSRodney W. Grimes }
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes /*
331df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
332df8bae1dSRodney W. Grimes  * If this vnode is mapped into some address space (i.e. we have a pager
333df8bae1dSRodney W. Grimes  * for it) we adjust our own internal size and flush any cached pages in
334df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
335df8bae1dSRodney W. Grimes  *
336df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
337df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
338df8bae1dSRodney W. Grimes  */
339df8bae1dSRodney W. Grimes void
340df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
341df8bae1dSRodney W. Grimes 	struct vnode *vp;
342df8bae1dSRodney W. Grimes 	u_long nsize;
343df8bae1dSRodney W. Grimes {
344df8bae1dSRodney W. Grimes 	register vn_pager_t vnp;
345df8bae1dSRodney W. Grimes 	register vm_object_t object;
346df8bae1dSRodney W. Grimes 	vm_pager_t pager;
347df8bae1dSRodney W. Grimes 
348df8bae1dSRodney W. Grimes 	/*
349df8bae1dSRodney W. Grimes 	 * Not a mapped vnode
350df8bae1dSRodney W. Grimes 	 */
351df8bae1dSRodney W. Grimes 	if (vp == NULL || vp->v_type != VREG || vp->v_vmdata == NULL)
352df8bae1dSRodney W. Grimes 		return;
353bbc0ec52SDavid Greenman 
354df8bae1dSRodney W. Grimes 	/*
355df8bae1dSRodney W. Grimes 	 * Hasn't changed size
356df8bae1dSRodney W. Grimes 	 */
3578e58bf68SDavid Greenman 	object = (vm_object_t) vp->v_vmdata;
3588e58bf68SDavid Greenman 	if (object == NULL)
3598e58bf68SDavid Greenman 		return;
3608e58bf68SDavid Greenman 	if ((pager = object->pager) == NULL)
3618e58bf68SDavid Greenman 		return;
362df8bae1dSRodney W. Grimes 	vnp = (vn_pager_t) pager->pg_data;
363df8bae1dSRodney W. Grimes 	if (nsize == vnp->vnp_size)
364df8bae1dSRodney W. Grimes 		return;
365bbc0ec52SDavid Greenman 
366df8bae1dSRodney W. Grimes 	/*
367bbc0ec52SDavid Greenman 	 * No object. This can happen during object termination since
368bbc0ec52SDavid Greenman 	 * vm_object_page_clean is called after the object has been removed
369bbc0ec52SDavid Greenman 	 * from the hash table, and clean may cause vnode write operations
370bbc0ec52SDavid Greenman 	 * which can wind up back here.
371df8bae1dSRodney W. Grimes 	 */
372df8bae1dSRodney W. Grimes 	object = vm_object_lookup(pager);
373df8bae1dSRodney W. Grimes 	if (object == NULL)
374df8bae1dSRodney W. Grimes 		return;
375df8bae1dSRodney W. Grimes 
376df8bae1dSRodney W. Grimes 	/*
377bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
378df8bae1dSRodney W. Grimes 	 */
379bbc0ec52SDavid Greenman 	if (nsize < vnp->vnp_size) {
3800d94caffSDavid Greenman 		if (round_page((vm_offset_t) nsize) < vnp->vnp_size) {
381df8bae1dSRodney W. Grimes 			vm_object_lock(object);
382df8bae1dSRodney W. Grimes 			vm_object_page_remove(object,
383bbc0ec52SDavid Greenman 			    round_page((vm_offset_t) nsize), vnp->vnp_size);
384bbc0ec52SDavid Greenman 			vm_object_unlock(object);
3850d94caffSDavid Greenman 		}
386bbc0ec52SDavid Greenman 		/*
387bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
388bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
389bbc0ec52SDavid Greenman 		 */
390bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
391bbc0ec52SDavid Greenman 			vm_offset_t kva;
392bbc0ec52SDavid Greenman 			vm_page_t m;
393bbc0ec52SDavid Greenman 
394bbc0ec52SDavid Greenman 			m = vm_page_lookup(object, trunc_page((vm_offset_t) nsize));
395bbc0ec52SDavid Greenman 			if (m) {
396bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
397bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
398bbc0ec52SDavid Greenman 				    round_page(nsize) - nsize);
399bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
400bbc0ec52SDavid Greenman 			}
401bbc0ec52SDavid Greenman 		}
402bbc0ec52SDavid Greenman 	}
403df8bae1dSRodney W. Grimes 	vnp->vnp_size = (vm_offset_t) nsize;
404bbc0ec52SDavid Greenman 	object->size = round_page(nsize);
405bbc0ec52SDavid Greenman 
406df8bae1dSRodney W. Grimes 	vm_object_deallocate(object);
407df8bae1dSRodney W. Grimes }
408df8bae1dSRodney W. Grimes 
409df8bae1dSRodney W. Grimes void
410df8bae1dSRodney W. Grimes vnode_pager_umount(mp)
411df8bae1dSRodney W. Grimes 	register struct mount *mp;
412df8bae1dSRodney W. Grimes {
413df8bae1dSRodney W. Grimes 	register vm_pager_t pager, npager;
414df8bae1dSRodney W. Grimes 	struct vnode *vp;
415df8bae1dSRodney W. Grimes 
41626f9a767SRodney W. Grimes 	pager = vnode_pager_list.tqh_first;
41726f9a767SRodney W. Grimes 	while (pager) {
418bbc0ec52SDavid Greenman 
419df8bae1dSRodney W. Grimes 		/*
420bbc0ec52SDavid Greenman 		 * Save the next pointer now since uncaching may terminate the
421bbc0ec52SDavid Greenman 		 * object and render pager invalid
422df8bae1dSRodney W. Grimes 		 */
423df8bae1dSRodney W. Grimes 		vp = ((vn_pager_t) pager->pg_data)->vnp_vp;
42426f9a767SRodney W. Grimes 		npager = pager->pg_list.tqe_next;
42526f9a767SRodney W. Grimes 		if (mp == (struct mount *) 0 || vp->v_mount == mp)
426df8bae1dSRodney W. Grimes 			(void) vnode_pager_uncache(vp);
42726f9a767SRodney W. Grimes 		pager = npager;
428df8bae1dSRodney W. Grimes 	}
429df8bae1dSRodney W. Grimes }
430df8bae1dSRodney W. Grimes 
431df8bae1dSRodney W. Grimes /*
432df8bae1dSRodney W. Grimes  * Remove vnode associated object from the object cache.
433df8bae1dSRodney W. Grimes  *
43426f9a767SRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
43526f9a767SRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
43626f9a767SRodney W. Grimes  */
43726f9a767SRodney W. Grimes boolean_t
43826f9a767SRodney W. Grimes vnode_pager_uncache(vp)
43926f9a767SRodney W. Grimes 	register struct vnode *vp;
44026f9a767SRodney W. Grimes {
44126f9a767SRodney W. Grimes 	register vm_object_t object;
44226f9a767SRodney W. Grimes 	boolean_t uncached, locked;
44326f9a767SRodney W. Grimes 	vm_pager_t pager;
44426f9a767SRodney W. Grimes 
44526f9a767SRodney W. Grimes 	/*
44626f9a767SRodney W. Grimes 	 * Not a mapped vnode
44726f9a767SRodney W. Grimes 	 */
4488e58bf68SDavid Greenman 	object = (vm_object_t) vp->v_vmdata;
4498e58bf68SDavid Greenman 	if (object == NULL)
4508e58bf68SDavid Greenman 		return (TRUE);
4510d94caffSDavid Greenman 
4528e58bf68SDavid Greenman 	pager = object->pager;
45326f9a767SRodney W. Grimes 	if (pager == NULL)
45426f9a767SRodney W. Grimes 		return (TRUE);
455bbc0ec52SDavid Greenman 
45626f9a767SRodney W. Grimes 	/*
457bbc0ec52SDavid Greenman 	 * Unlock the vnode if it is currently locked. We do this since
458bbc0ec52SDavid Greenman 	 * uncaching the object may result in its destruction which may
459bbc0ec52SDavid Greenman 	 * initiate paging activity which may necessitate locking the vnode.
46026f9a767SRodney W. Grimes 	 */
46126f9a767SRodney W. Grimes 	locked = VOP_ISLOCKED(vp);
46226f9a767SRodney W. Grimes 	if (locked)
46326f9a767SRodney W. Grimes 		VOP_UNLOCK(vp);
464bbc0ec52SDavid Greenman 
46526f9a767SRodney W. Grimes 	/*
466bbc0ec52SDavid Greenman 	 * Must use vm_object_lookup() as it actually removes the object from
467bbc0ec52SDavid Greenman 	 * the cache list.
46826f9a767SRodney W. Grimes 	 */
46926f9a767SRodney W. Grimes 	object = vm_object_lookup(pager);
47026f9a767SRodney W. Grimes 	if (object) {
47126f9a767SRodney W. Grimes 		uncached = (object->ref_count <= 1);
47226f9a767SRodney W. Grimes 		pager_cache(object, FALSE);
47326f9a767SRodney W. Grimes 	} else
47426f9a767SRodney W. Grimes 		uncached = TRUE;
47526f9a767SRodney W. Grimes 	if (locked)
47626f9a767SRodney W. Grimes 		VOP_LOCK(vp);
47726f9a767SRodney W. Grimes 	return (uncached);
47826f9a767SRodney W. Grimes }
479df8bae1dSRodney W. Grimes 
48026f9a767SRodney W. Grimes 
48126f9a767SRodney W. Grimes void
48226f9a767SRodney W. Grimes vnode_pager_freepage(m)
48326f9a767SRodney W. Grimes 	vm_page_t m;
484df8bae1dSRodney W. Grimes {
48526f9a767SRodney W. Grimes 	PAGE_WAKEUP(m);
48626f9a767SRodney W. Grimes 	vm_page_free(m);
48726f9a767SRodney W. Grimes }
48826f9a767SRodney W. Grimes 
48926f9a767SRodney W. Grimes /*
49026f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
49126f9a767SRodney W. Grimes  * file address
49226f9a767SRodney W. Grimes  */
49326f9a767SRodney W. Grimes vm_offset_t
494efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
49526f9a767SRodney W. Grimes 	struct vnode *vp;
49626f9a767SRodney W. Grimes 	vm_offset_t address;
497efc68ce1SDavid Greenman 	int *run;
49826f9a767SRodney W. Grimes {
49926f9a767SRodney W. Grimes 	int rtaddress;
50026f9a767SRodney W. Grimes 	int bsize;
50126f9a767SRodney W. Grimes 	vm_offset_t block;
50226f9a767SRodney W. Grimes 	struct vnode *rtvp;
50326f9a767SRodney W. Grimes 	int err;
50426f9a767SRodney W. Grimes 	int vblock, voffset;
50526f9a767SRodney W. Grimes 
5060d94caffSDavid Greenman 	if ((int) address < 0)
5070d94caffSDavid Greenman 		return -1;
5080d94caffSDavid Greenman 
50926f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
51026f9a767SRodney W. Grimes 	vblock = address / bsize;
51126f9a767SRodney W. Grimes 	voffset = address % bsize;
51226f9a767SRodney W. Grimes 
513efc68ce1SDavid Greenman 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run);
51426f9a767SRodney W. Grimes 
515efc68ce1SDavid Greenman 	if (err || (block == -1))
51626f9a767SRodney W. Grimes 		rtaddress = -1;
517efc68ce1SDavid Greenman 	else {
51826f9a767SRodney W. Grimes 		rtaddress = block * DEV_BSIZE + voffset;
519efc68ce1SDavid Greenman 		if( run) {
520efc68ce1SDavid Greenman 			*run += 1;
521efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
522efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
523efc68ce1SDavid Greenman 		}
524efc68ce1SDavid Greenman 	}
52526f9a767SRodney W. Grimes 
52626f9a767SRodney W. Grimes 	return rtaddress;
52726f9a767SRodney W. Grimes }
52826f9a767SRodney W. Grimes 
52926f9a767SRodney W. Grimes /*
53026f9a767SRodney W. Grimes  * interrupt routine for I/O completion
53126f9a767SRodney W. Grimes  */
53226f9a767SRodney W. Grimes void
53326f9a767SRodney W. Grimes vnode_pager_iodone(bp)
53426f9a767SRodney W. Grimes 	struct buf *bp;
53526f9a767SRodney W. Grimes {
53626f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
53726f9a767SRodney W. Grimes 	wakeup((caddr_t) bp);
53816f62314SDavid Greenman 	if (bp->b_flags & B_ASYNC) {
53916f62314SDavid Greenman 		vm_offset_t paddr;
54016f62314SDavid Greenman 		vm_page_t m;
54116f62314SDavid Greenman 		vm_object_t obj = 0;
54216f62314SDavid Greenman 		int i;
54316f62314SDavid Greenman 		int npages;
54416f62314SDavid Greenman 
54516f62314SDavid Greenman 		paddr = (vm_offset_t) bp->b_data;
54616f62314SDavid Greenman 		if (bp->b_bufsize != bp->b_bcount)
54716f62314SDavid Greenman 			bzero(bp->b_data + bp->b_bcount,
54816f62314SDavid Greenman 			    bp->b_bufsize - bp->b_bcount);
54916f62314SDavid Greenman 
55016f62314SDavid Greenman 		npages = (bp->b_bufsize + PAGE_SIZE - 1) / PAGE_SIZE;
55116f62314SDavid Greenman 		for (i = 0; i < npages; i++) {
55216f62314SDavid Greenman 			m = PHYS_TO_VM_PAGE(pmap_kextract(paddr + i * PAGE_SIZE));
55316f62314SDavid Greenman 			obj = m->object;
55416f62314SDavid Greenman 			if (m) {
5550d94caffSDavid Greenman 				m->dirty = 0;
5560d94caffSDavid Greenman 				m->valid = VM_PAGE_BITS_ALL;
5570d94caffSDavid Greenman 				if (m->flags & PG_WANTED)
5580d94caffSDavid Greenman 					m->flags |= PG_REFERENCED;
55916f62314SDavid Greenman 				PAGE_WAKEUP(m);
56016f62314SDavid Greenman 			} else {
56116f62314SDavid Greenman 				panic("vnode_pager_iodone: page is gone!!!");
56216f62314SDavid Greenman 			}
56316f62314SDavid Greenman 		}
564a481f200SDavid Greenman 		pmap_qremove(paddr, npages);
56516f62314SDavid Greenman 		if (obj) {
56616f62314SDavid Greenman 			--obj->paging_in_progress;
56716f62314SDavid Greenman 			if (obj->paging_in_progress == 0)
56816f62314SDavid Greenman 				wakeup((caddr_t) obj);
56916f62314SDavid Greenman 		} else {
57016f62314SDavid Greenman 			panic("vnode_pager_iodone: object is gone???");
57116f62314SDavid Greenman 		}
57216f62314SDavid Greenman 		relpbuf(bp);
57316f62314SDavid Greenman 	}
57426f9a767SRodney W. Grimes }
57526f9a767SRodney W. Grimes 
57626f9a767SRodney W. Grimes /*
57726f9a767SRodney W. Grimes  * small block file system vnode pager input
57826f9a767SRodney W. Grimes  */
57926f9a767SRodney W. Grimes int
58026f9a767SRodney W. Grimes vnode_pager_input_smlfs(vnp, m)
58126f9a767SRodney W. Grimes 	vn_pager_t vnp;
58226f9a767SRodney W. Grimes 	vm_page_t m;
58326f9a767SRodney W. Grimes {
58426f9a767SRodney W. Grimes 	int i;
58526f9a767SRodney W. Grimes 	int s;
58626f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
58726f9a767SRodney W. Grimes 	struct buf *bp;
58826f9a767SRodney W. Grimes 	vm_offset_t kva;
58926f9a767SRodney W. Grimes 	int fileaddr;
59026f9a767SRodney W. Grimes 	int block;
59126f9a767SRodney W. Grimes 	vm_offset_t bsize;
59226f9a767SRodney W. Grimes 	int error = 0;
59326f9a767SRodney W. Grimes 
59426f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
59526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
59626f9a767SRodney W. Grimes 
5970d94caffSDavid Greenman 	VOP_BMAP(vp, 0, &dp, 0, 0);
59826f9a767SRodney W. Grimes 
59926f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
60026f9a767SRodney W. Grimes 
60126f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
602bbc0ec52SDavid Greenman 
6030d94caffSDavid Greenman 		if ((vm_page_bits(m->offset + i * bsize, bsize) & m->valid))
60426f9a767SRodney W. Grimes 			continue;
60526f9a767SRodney W. Grimes 
606efc68ce1SDavid Greenman 		fileaddr = vnode_pager_addr(vp, m->offset + i * bsize, (int *)0);
60726f9a767SRodney W. Grimes 		if (fileaddr != -1) {
60826f9a767SRodney W. Grimes 			bp = getpbuf();
60926f9a767SRodney W. Grimes 
61026f9a767SRodney W. Grimes 			/* build a minimal buffer header */
61126f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
61226f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
61326f9a767SRodney W. Grimes 			bp->b_proc = curproc;
61426f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
61526f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
61626f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
61726f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
61826f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
61926f9a767SRodney W. Grimes 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
62026f9a767SRodney W. Grimes 			bp->b_blkno = fileaddr / DEV_BSIZE;
6210d94caffSDavid Greenman 			pbgetvp(dp, bp);
62226f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
62326f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
62426f9a767SRodney W. Grimes 
62526f9a767SRodney W. Grimes 			/* do the input */
62626f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
62726f9a767SRodney W. Grimes 
62826f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
62926f9a767SRodney W. Grimes 
63026f9a767SRodney W. Grimes 			s = splbio();
63126f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
63226f9a767SRodney W. Grimes 				tsleep((caddr_t) bp, PVM, "vnsrd", 0);
63326f9a767SRodney W. Grimes 			}
63426f9a767SRodney W. Grimes 			splx(s);
63526f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
63626f9a767SRodney W. Grimes 				error = EIO;
63726f9a767SRodney W. Grimes 
63826f9a767SRodney W. Grimes 			/*
63926f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
64026f9a767SRodney W. Grimes 			 */
64126f9a767SRodney W. Grimes 			relpbuf(bp);
64226f9a767SRodney W. Grimes 			HOLDRELE(vp);
64326f9a767SRodney W. Grimes 			if (error)
64426f9a767SRodney W. Grimes 				break;
6450d94caffSDavid Greenman 
6460d94caffSDavid Greenman 			vm_page_set_clean(m, i * bsize, bsize);
6470d94caffSDavid Greenman 			vm_page_set_valid(m, i * bsize, bsize);
64826f9a767SRodney W. Grimes 		} else {
6490d94caffSDavid Greenman 			vm_page_set_clean(m, i * bsize, bsize);
65026f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
65126f9a767SRodney W. Grimes 		}
65226f9a767SRodney W. Grimes nextblock:
65326f9a767SRodney W. Grimes 	}
65426f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
6550d94caffSDavid Greenman 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
65626f9a767SRodney W. Grimes 	if (error) {
657a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
65826f9a767SRodney W. Grimes 	}
65926f9a767SRodney W. Grimes 	return VM_PAGER_OK;
66026f9a767SRodney W. Grimes 
66126f9a767SRodney W. Grimes }
66226f9a767SRodney W. Grimes 
66326f9a767SRodney W. Grimes 
66426f9a767SRodney W. Grimes /*
66526f9a767SRodney W. Grimes  * old style vnode pager output routine
66626f9a767SRodney W. Grimes  */
66726f9a767SRodney W. Grimes int
66826f9a767SRodney W. Grimes vnode_pager_input_old(vnp, m)
66926f9a767SRodney W. Grimes 	vn_pager_t vnp;
67026f9a767SRodney W. Grimes 	vm_page_t m;
67126f9a767SRodney W. Grimes {
672df8bae1dSRodney W. Grimes 	struct uio auio;
673df8bae1dSRodney W. Grimes 	struct iovec aiov;
67426f9a767SRodney W. Grimes 	int error;
67526f9a767SRodney W. Grimes 	int size;
67626f9a767SRodney W. Grimes 	vm_offset_t kva;
677df8bae1dSRodney W. Grimes 
67826f9a767SRodney W. Grimes 	error = 0;
679bbc0ec52SDavid Greenman 
680df8bae1dSRodney W. Grimes 	/*
68126f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
68226f9a767SRodney W. Grimes 	 */
6830d94caffSDavid Greenman 	if (m->offset >= vnp->vnp_size) {
68426f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
68526f9a767SRodney W. Grimes 	} else {
68626f9a767SRodney W. Grimes 		size = PAGE_SIZE;
6870d94caffSDavid Greenman 		if (m->offset + size > vnp->vnp_size)
6880d94caffSDavid Greenman 			size = vnp->vnp_size - m->offset;
68926f9a767SRodney W. Grimes 		/*
690df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
691df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
692df8bae1dSRodney W. Grimes 		 */
69326f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
694df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
695df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
696df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
697df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
6980d94caffSDavid Greenman 		auio.uio_offset = m->offset;
699df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
70026f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
701df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
702df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
70326f9a767SRodney W. Grimes 
70426f9a767SRodney W. Grimes 		error = VOP_READ(vnp->vnp_vp, &auio, 0, curproc->p_ucred);
705df8bae1dSRodney W. Grimes 		if (!error) {
706df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
707df8bae1dSRodney W. Grimes 
708df8bae1dSRodney W. Grimes 			if (count == 0)
709df8bae1dSRodney W. Grimes 				error = EINVAL;
71026f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
71126f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
712df8bae1dSRodney W. Grimes 		}
71326f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
714df8bae1dSRodney W. Grimes 	}
71526f9a767SRodney W. Grimes 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
7160d94caffSDavid Greenman 	m->dirty = 0;
717a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
71826f9a767SRodney W. Grimes }
71926f9a767SRodney W. Grimes 
72026f9a767SRodney W. Grimes /*
72126f9a767SRodney W. Grimes  * generic vnode pager input routine
72226f9a767SRodney W. Grimes  */
72326f9a767SRodney W. Grimes int
72426f9a767SRodney W. Grimes vnode_pager_input(vnp, m, count, reqpage)
72526f9a767SRodney W. Grimes 	register vn_pager_t vnp;
72626f9a767SRodney W. Grimes 	vm_page_t *m;
72726f9a767SRodney W. Grimes 	int count, reqpage;
72826f9a767SRodney W. Grimes {
72905f0fdd2SPoul-Henning Kamp 	int i;
73026f9a767SRodney W. Grimes 	vm_offset_t kva, foff;
73116f62314SDavid Greenman 	int size, sizea;
73226f9a767SRodney W. Grimes 	vm_object_t object;
73326f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
73426f9a767SRodney W. Grimes 	int bsize;
73526f9a767SRodney W. Grimes 
73626f9a767SRodney W. Grimes 	int first, last;
737efc68ce1SDavid Greenman 	int firstaddr;
73826f9a767SRodney W. Grimes 	int block, offset;
739efc68ce1SDavid Greenman 	int runpg;
740efc68ce1SDavid Greenman 	int runend;
74126f9a767SRodney W. Grimes 
74216f62314SDavid Greenman 	struct buf *bp, *bpa;
74316f62314SDavid Greenman 	int counta;
74426f9a767SRodney W. Grimes 	int s;
74526f9a767SRodney W. Grimes 	int failflag;
74626f9a767SRodney W. Grimes 
74726f9a767SRodney W. Grimes 	int error = 0;
74826f9a767SRodney W. Grimes 
749bbc0ec52SDavid Greenman 	object = m[reqpage]->object;	/* all vm_page_t items are in same
750bbc0ec52SDavid Greenman 					 * object */
75126f9a767SRodney W. Grimes 
75226f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
75326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
75426f9a767SRodney W. Grimes 
75526f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
756bbc0ec52SDavid Greenman 
75726f9a767SRodney W. Grimes 	/*
758bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
759bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
76026f9a767SRodney W. Grimes 	 */
7610d94caffSDavid Greenman 	foff = m[reqpage]->offset;
762bbc0ec52SDavid Greenman 
76326f9a767SRodney W. Grimes 	/*
76416f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
76526f9a767SRodney W. Grimes 	 */
7660d94caffSDavid Greenman 	if (VOP_BMAP(vp, 0, &dp, 0, 0)) {
76726f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
76826f9a767SRodney W. Grimes 			if (i != reqpage) {
76926f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
77026f9a767SRodney W. Grimes 			}
77126f9a767SRodney W. Grimes 		}
772976e77fcSDavid Greenman 		cnt.v_vnodein++;
773976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
77426f9a767SRodney W. Grimes 		return vnode_pager_input_old(vnp, m[reqpage]);
775bbc0ec52SDavid Greenman 
77626f9a767SRodney W. Grimes 		/*
77726f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
77826f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
77926f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
78026f9a767SRodney W. Grimes 		 */
78126f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
78226f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
78326f9a767SRodney W. Grimes 
78426f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
78526f9a767SRodney W. Grimes 			if (i != reqpage) {
78626f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
78726f9a767SRodney W. Grimes 			}
78826f9a767SRodney W. Grimes 		}
789976e77fcSDavid Greenman 		cnt.v_vnodein++;
790976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
79126f9a767SRodney W. Grimes 		return vnode_pager_input_smlfs(vnp, m[reqpage]);
79226f9a767SRodney W. Grimes 	}
79326f9a767SRodney W. Grimes 	/*
7940d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
7950d94caffSDavid Greenman 	 * then, the entire page is valid --
7960d94caffSDavid Greenman 	 */
7970d94caffSDavid Greenman 	if (m[reqpage]->valid) {
7980d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
7990d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
8000d94caffSDavid Greenman 			if (i != reqpage)
8010d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
8020d94caffSDavid Greenman 		}
8030d94caffSDavid Greenman 		return VM_PAGER_OK;
8040d94caffSDavid Greenman 	}
8050d94caffSDavid Greenman 	/*
80626f9a767SRodney W. Grimes 	 * here on direct device I/O
80726f9a767SRodney W. Grimes 	 */
80826f9a767SRodney W. Grimes 
809bbc0ec52SDavid Greenman 
810efc68ce1SDavid Greenman 	firstaddr = -1;
81126f9a767SRodney W. Grimes 	/*
812efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
81326f9a767SRodney W. Grimes 	 */
814efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
815efc68ce1SDavid Greenman 		firstaddr = vnode_pager_addr(vp, m[i]->offset, &runpg);
816efc68ce1SDavid Greenman 		if (firstaddr == -1) {
817efc68ce1SDavid Greenman 			if( i == reqpage && foff < vnp->vnp_size) {
818efc68ce1SDavid Greenman 				printf("vnode_pager_input: unexpected missing page: firstaddr: %d, foff: %d, vnp_size: %d\n",
819efc68ce1SDavid Greenman 			   	 firstaddr, foff, vnp->vnp_size);
820efc68ce1SDavid Greenman 				panic("vnode_pager_input:...");
821efc68ce1SDavid Greenman 			}
82226f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
823efc68ce1SDavid Greenman 			runend = i + 1;
824efc68ce1SDavid Greenman 			first = runend;
825efc68ce1SDavid Greenman 			continue;
826efc68ce1SDavid Greenman 		}
827efc68ce1SDavid Greenman 		runend = i + runpg;
828efc68ce1SDavid Greenman 		if( runend <= reqpage) {
829efc68ce1SDavid Greenman 			int j;
830efc68ce1SDavid Greenman 			for(j = i; j < runend; j++) {
831efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
832efc68ce1SDavid Greenman 			}
83326f9a767SRodney W. Grimes 		} else {
834efc68ce1SDavid Greenman 			if( runpg < (count - first)) {
835efc68ce1SDavid Greenman 				for(i=first + runpg; i < count; i++)
83626f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
837efc68ce1SDavid Greenman 				count = first + runpg;
83826f9a767SRodney W. Grimes 			}
839efc68ce1SDavid Greenman 			break;
84026f9a767SRodney W. Grimes 		}
841efc68ce1SDavid Greenman 		first = runend;
842efc68ce1SDavid Greenman 	}
84326f9a767SRodney W. Grimes 
84426f9a767SRodney W. Grimes 	/*
845bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
846bbc0ec52SDavid Greenman 	 * to be zero based...
84726f9a767SRodney W. Grimes 	 */
84826f9a767SRodney W. Grimes 	if (first != 0) {
84926f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
85026f9a767SRodney W. Grimes 			m[i - first] = m[i];
85126f9a767SRodney W. Grimes 		}
85226f9a767SRodney W. Grimes 		count -= first;
85326f9a767SRodney W. Grimes 		reqpage -= first;
85426f9a767SRodney W. Grimes 	}
855efc68ce1SDavid Greenman 
85626f9a767SRodney W. Grimes 	/*
85726f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
85826f9a767SRodney W. Grimes 	 */
8590d94caffSDavid Greenman 	foff = m[0]->offset;
860efc68ce1SDavid Greenman #if 0
861efc68ce1SDavid Greenman 	printf("foff: 0x%lx, firstaddr: 0x%lx\n",
862efc68ce1SDavid Greenman 		foff, firstaddr);
863efc68ce1SDavid Greenman 	DELAY(6000000);
864efc68ce1SDavid Greenman #endif
86526f9a767SRodney W. Grimes 
86626f9a767SRodney W. Grimes 	/*
86726f9a767SRodney W. Grimes 	 * calculate the size of the transfer
86826f9a767SRodney W. Grimes 	 */
86926f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
87026f9a767SRodney W. Grimes 	if ((foff + size) > vnp->vnp_size)
87126f9a767SRodney W. Grimes 		size = vnp->vnp_size - foff;
87226f9a767SRodney W. Grimes 
87326f9a767SRodney W. Grimes 	/*
87426f9a767SRodney W. Grimes 	 * round up physical size for real devices
87526f9a767SRodney W. Grimes 	 */
87626f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
87726f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
87826f9a767SRodney W. Grimes 
87916f62314SDavid Greenman 	counta = 0;
88016f62314SDavid Greenman 	if (count * PAGE_SIZE > bsize)
88116f62314SDavid Greenman 		counta = (count - reqpage) - 1;
88216f62314SDavid Greenman 	bpa = 0;
88316f62314SDavid Greenman 	sizea = 0;
8846d40c3d3SDavid Greenman 	bp = getpbuf();
88516f62314SDavid Greenman 	if (counta) {
8866d40c3d3SDavid Greenman 		bpa = (struct buf *) trypbuf();
8876d40c3d3SDavid Greenman 		if (bpa) {
88816f62314SDavid Greenman 			count -= counta;
88916f62314SDavid Greenman 			sizea = size - count * PAGE_SIZE;
89016f62314SDavid Greenman 			size = count * PAGE_SIZE;
89116f62314SDavid Greenman 		}
8926d40c3d3SDavid Greenman 	}
89316f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
89416f62314SDavid Greenman 
89526f9a767SRodney W. Grimes 	/*
89626f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
89726f9a767SRodney W. Grimes 	 */
89816f62314SDavid Greenman 	pmap_qenter(kva, m, count);
89926f9a767SRodney W. Grimes 
90026f9a767SRodney W. Grimes 	/* build a minimal buffer header */
90126f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
90226f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
90326f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
90426f9a767SRodney W. Grimes 	bp->b_proc = curproc;
90526f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
90626f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
90726f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
90826f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
90926f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
91026f9a767SRodney W. Grimes 	bp->b_blkno = firstaddr / DEV_BSIZE;
9110d94caffSDavid Greenman 	pbgetvp(dp, bp);
91226f9a767SRodney W. Grimes 	bp->b_bcount = size;
91326f9a767SRodney W. Grimes 	bp->b_bufsize = size;
91426f9a767SRodney W. Grimes 
915976e77fcSDavid Greenman 	cnt.v_vnodein++;
916976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
917976e77fcSDavid Greenman 
91826f9a767SRodney W. Grimes 	/* do the input */
91926f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
920976e77fcSDavid Greenman 
92116f62314SDavid Greenman 	if (counta) {
92216f62314SDavid Greenman 		for (i = 0; i < counta; i++) {
92316f62314SDavid Greenman 			vm_page_deactivate(m[count + i]);
92416f62314SDavid Greenman 		}
925c87801feSDavid Greenman 		pmap_qenter((vm_offset_t) bpa->b_data, &m[count], counta);
92616f62314SDavid Greenman 		++m[count]->object->paging_in_progress;
92716f62314SDavid Greenman 		bpa->b_flags = B_BUSY | B_READ | B_CALL | B_ASYNC;
92816f62314SDavid Greenman 		bpa->b_iodone = vnode_pager_iodone;
92916f62314SDavid Greenman 		/* B_PHYS is not set, but it is nice to fill this in */
93016f62314SDavid Greenman 		bpa->b_proc = curproc;
93116f62314SDavid Greenman 		bpa->b_rcred = bpa->b_wcred = bpa->b_proc->p_ucred;
93216f62314SDavid Greenman 		if (bpa->b_rcred != NOCRED)
93316f62314SDavid Greenman 			crhold(bpa->b_rcred);
93416f62314SDavid Greenman 		if (bpa->b_wcred != NOCRED)
93516f62314SDavid Greenman 			crhold(bpa->b_wcred);
93616f62314SDavid Greenman 		bpa->b_blkno = (firstaddr + count * PAGE_SIZE) / DEV_BSIZE;
9370d94caffSDavid Greenman 		pbgetvp(dp, bpa);
93816f62314SDavid Greenman 		bpa->b_bcount = sizea;
93916f62314SDavid Greenman 		bpa->b_bufsize = counta * PAGE_SIZE;
94016f62314SDavid Greenman 
941976e77fcSDavid Greenman 		cnt.v_vnodepgsin += counta;
94216f62314SDavid Greenman 		VOP_STRATEGY(bpa);
94316f62314SDavid Greenman 	}
94426f9a767SRodney W. Grimes 	s = splbio();
94526f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
94626f9a767SRodney W. Grimes 
94726f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
94826f9a767SRodney W. Grimes 		tsleep((caddr_t) bp, PVM, "vnread", 0);
94926f9a767SRodney W. Grimes 	}
95026f9a767SRodney W. Grimes 	splx(s);
95126f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
95226f9a767SRodney W. Grimes 		error = EIO;
95326f9a767SRodney W. Grimes 
95426f9a767SRodney W. Grimes 	if (!error) {
95526f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
95626f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
95726f9a767SRodney W. Grimes 	}
95816f62314SDavid Greenman 	pmap_qremove(kva, count);
95926f9a767SRodney W. Grimes 
96026f9a767SRodney W. Grimes 	/*
96126f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
96226f9a767SRodney W. Grimes 	 */
96326f9a767SRodney W. Grimes 	relpbuf(bp);
96426f9a767SRodney W. Grimes 	HOLDRELE(vp);
96526f9a767SRodney W. Grimes 
96626f9a767SRodney W. Grimes finishup:
96726f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
968fff93ab6SDavid Greenman 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
9690d94caffSDavid Greenman 		m[i]->dirty = 0;
9700d94caffSDavid Greenman 		m[i]->valid = VM_PAGE_BITS_ALL;
97126f9a767SRodney W. Grimes 		if (i != reqpage) {
972bbc0ec52SDavid Greenman 
97326f9a767SRodney W. Grimes 			/*
974bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
975bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
976bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
977bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
978bbc0ec52SDavid Greenman 			 * deactivating pages is best.
97926f9a767SRodney W. Grimes 			 */
980bbc0ec52SDavid Greenman 
98126f9a767SRodney W. Grimes 			/*
982bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
983bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
98426f9a767SRodney W. Grimes 			 */
98526f9a767SRodney W. Grimes 			if (!error) {
98626f9a767SRodney W. Grimes 				vm_page_deactivate(m[i]);
98726f9a767SRodney W. Grimes 				PAGE_WAKEUP(m[i]);
98826f9a767SRodney W. Grimes 			} else {
98926f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
99026f9a767SRodney W. Grimes 			}
99126f9a767SRodney W. Grimes 		}
99226f9a767SRodney W. Grimes 	}
99326f9a767SRodney W. Grimes 	if (error) {
994a83c285cSDavid Greenman 		printf("vnode_pager_input: I/O read error\n");
99526f9a767SRodney W. Grimes 	}
996a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
99726f9a767SRodney W. Grimes }
99826f9a767SRodney W. Grimes 
99926f9a767SRodney W. Grimes /*
100026f9a767SRodney W. Grimes  * old-style vnode pager output routine
100126f9a767SRodney W. Grimes  */
100226f9a767SRodney W. Grimes int
100326f9a767SRodney W. Grimes vnode_pager_output_old(vnp, m)
100426f9a767SRodney W. Grimes 	register vn_pager_t vnp;
100526f9a767SRodney W. Grimes 	vm_page_t m;
100626f9a767SRodney W. Grimes {
10070d94caffSDavid Greenman 	vm_offset_t kva, kva2;
100826f9a767SRodney W. Grimes 	vm_offset_t size;
100926f9a767SRodney W. Grimes 	struct iovec aiov;
101026f9a767SRodney W. Grimes 	struct uio auio;
101126f9a767SRodney W. Grimes 	struct vnode *vp;
101226f9a767SRodney W. Grimes 	int error;
101326f9a767SRodney W. Grimes 
101426f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
1015bbc0ec52SDavid Greenman 
101626f9a767SRodney W. Grimes 	/*
10170d94caffSDavid Greenman 	 * Dont return failure if beyond current EOF placate the VM system.
101826f9a767SRodney W. Grimes 	 */
10190d94caffSDavid Greenman 	if (m->offset >= vnp->vnp_size) {
10200d94caffSDavid Greenman 		return VM_PAGER_OK;
102126f9a767SRodney W. Grimes 	} else {
102226f9a767SRodney W. Grimes 		size = PAGE_SIZE;
10230d94caffSDavid Greenman 		if (m->offset + size > vnp->vnp_size)
10240d94caffSDavid Greenman 			size = vnp->vnp_size - m->offset;
10250d94caffSDavid Greenman 
10260d94caffSDavid Greenman 		kva2 = kmem_alloc(pager_map, PAGE_SIZE);
102726f9a767SRodney W. Grimes 		/*
102826f9a767SRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
102926f9a767SRodney W. Grimes 		 * we can use VOP_WRITE routines.
103026f9a767SRodney W. Grimes 		 */
103126f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
10320d94caffSDavid Greenman 		bcopy((caddr_t) kva, (caddr_t) kva2, size);
10330d94caffSDavid Greenman 		vm_pager_unmap_page(kva);
10340d94caffSDavid Greenman 		pmap_clear_modify(VM_PAGE_TO_PHYS(m));
10350d94caffSDavid Greenman 		PAGE_WAKEUP(m);
10360d94caffSDavid Greenman 
10370d94caffSDavid Greenman 		aiov.iov_base = (caddr_t) kva2;
103826f9a767SRodney W. Grimes 		aiov.iov_len = size;
103926f9a767SRodney W. Grimes 		auio.uio_iov = &aiov;
104026f9a767SRodney W. Grimes 		auio.uio_iovcnt = 1;
10410d94caffSDavid Greenman 		auio.uio_offset = m->offset;
104226f9a767SRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
104326f9a767SRodney W. Grimes 		auio.uio_rw = UIO_WRITE;
104426f9a767SRodney W. Grimes 		auio.uio_resid = size;
104526f9a767SRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
104626f9a767SRodney W. Grimes 
104726f9a767SRodney W. Grimes 		error = VOP_WRITE(vp, &auio, 0, curproc->p_ucred);
104826f9a767SRodney W. Grimes 
10490d94caffSDavid Greenman 		kmem_free_wakeup(pager_map, kva2, PAGE_SIZE);
105026f9a767SRodney W. Grimes 		if (!error) {
105126f9a767SRodney W. Grimes 			if ((size - auio.uio_resid) == 0) {
105226f9a767SRodney W. Grimes 				error = EINVAL;
105326f9a767SRodney W. Grimes 			}
105426f9a767SRodney W. Grimes 		}
1055a83c285cSDavid Greenman 		return error ? VM_PAGER_ERROR : VM_PAGER_OK;
105626f9a767SRodney W. Grimes 	}
105726f9a767SRodney W. Grimes }
105826f9a767SRodney W. Grimes 
105926f9a767SRodney W. Grimes /*
106026f9a767SRodney W. Grimes  * vnode pager output on a small-block file system
106126f9a767SRodney W. Grimes  */
106226f9a767SRodney W. Grimes int
106326f9a767SRodney W. Grimes vnode_pager_output_smlfs(vnp, m)
106426f9a767SRodney W. Grimes 	vn_pager_t vnp;
106526f9a767SRodney W. Grimes 	vm_page_t m;
106626f9a767SRodney W. Grimes {
106726f9a767SRodney W. Grimes 	int i;
106826f9a767SRodney W. Grimes 	int s;
106926f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
107026f9a767SRodney W. Grimes 	struct buf *bp;
107126f9a767SRodney W. Grimes 	vm_offset_t kva;
107226f9a767SRodney W. Grimes 	int fileaddr;
107326f9a767SRodney W. Grimes 	vm_offset_t bsize;
107426f9a767SRodney W. Grimes 	int error = 0;
107526f9a767SRodney W. Grimes 
107626f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
107726f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
107826f9a767SRodney W. Grimes 
10790d94caffSDavid Greenman 	VOP_BMAP(vp, 0, &dp, 0, 0);
108026f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
108126f9a767SRodney W. Grimes 	for (i = 0; !error && i < (PAGE_SIZE / bsize); i++) {
1082bbc0ec52SDavid Greenman 
10830d94caffSDavid Greenman 		if ((vm_page_bits(m->offset + i * bsize, bsize) & m->valid & m->dirty) == 0)
10840d94caffSDavid Greenman 			continue;
108526f9a767SRodney W. Grimes 		/*
108626f9a767SRodney W. Grimes 		 * calculate logical block and offset
108726f9a767SRodney W. Grimes 		 */
1088efc68ce1SDavid Greenman 		fileaddr = vnode_pager_addr(vp, m->offset + i * bsize, (int *)0);
108926f9a767SRodney W. Grimes 		if (fileaddr != -1) {
109026f9a767SRodney W. Grimes 
109126f9a767SRodney W. Grimes 			bp = getpbuf();
109226f9a767SRodney W. Grimes 
109326f9a767SRodney W. Grimes 			/* build a minimal buffer header */
109426f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_CALL | B_WRITE;
109526f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
109626f9a767SRodney W. Grimes 			bp->b_proc = curproc;
109726f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
109826f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
109926f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
110026f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
110126f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
110226f9a767SRodney W. Grimes 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
110326f9a767SRodney W. Grimes 			bp->b_blkno = fileaddr / DEV_BSIZE;
11040d94caffSDavid Greenman 			pbgetvp(dp, bp);
110526f9a767SRodney W. Grimes 			++dp->v_numoutput;
110626f9a767SRodney W. Grimes 			/* for NFS */
110726f9a767SRodney W. Grimes 			bp->b_dirtyoff = 0;
110826f9a767SRodney W. Grimes 			bp->b_dirtyend = bsize;
110926f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
111026f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
111126f9a767SRodney W. Grimes 
111226f9a767SRodney W. Grimes 			/* do the input */
111326f9a767SRodney W. Grimes 			VOP_STRATEGY(bp);
111426f9a767SRodney W. Grimes 
111526f9a767SRodney W. Grimes 			/* we definitely need to be at splbio here */
111626f9a767SRodney W. Grimes 
111726f9a767SRodney W. Grimes 			s = splbio();
111826f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
111926f9a767SRodney W. Grimes 				tsleep((caddr_t) bp, PVM, "vnswrt", 0);
112026f9a767SRodney W. Grimes 			}
112126f9a767SRodney W. Grimes 			splx(s);
112226f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
112326f9a767SRodney W. Grimes 				error = EIO;
112426f9a767SRodney W. Grimes 
11250d94caffSDavid Greenman 			vm_page_set_clean(m, i * bsize, bsize);
112626f9a767SRodney W. Grimes 			/*
112726f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
112826f9a767SRodney W. Grimes 			 */
112926f9a767SRodney W. Grimes 			relpbuf(bp);
113026f9a767SRodney W. Grimes 			HOLDRELE(vp);
113126f9a767SRodney W. Grimes 		}
113226f9a767SRodney W. Grimes 	}
113326f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
113426f9a767SRodney W. Grimes 	if (error)
1135a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
113626f9a767SRodney W. Grimes 	else
113726f9a767SRodney W. Grimes 		return VM_PAGER_OK;
113826f9a767SRodney W. Grimes }
113926f9a767SRodney W. Grimes 
114026f9a767SRodney W. Grimes /*
114126f9a767SRodney W. Grimes  * generic vnode pager output routine
114226f9a767SRodney W. Grimes  */
114326f9a767SRodney W. Grimes int
114426f9a767SRodney W. Grimes vnode_pager_output(vnp, m, count, rtvals)
114526f9a767SRodney W. Grimes 	vn_pager_t vnp;
114626f9a767SRodney W. Grimes 	vm_page_t *m;
114726f9a767SRodney W. Grimes 	int count;
114826f9a767SRodney W. Grimes 	int *rtvals;
114926f9a767SRodney W. Grimes {
115026f9a767SRodney W. Grimes 	int i, j;
115126f9a767SRodney W. Grimes 	vm_offset_t kva, foff;
115226f9a767SRodney W. Grimes 	int size;
115326f9a767SRodney W. Grimes 	vm_object_t object;
115426f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
115526f9a767SRodney W. Grimes 	struct buf *bp;
115626f9a767SRodney W. Grimes 	vm_offset_t reqaddr;
115726f9a767SRodney W. Grimes 	int bsize;
115826f9a767SRodney W. Grimes 	int s;
11590d94caffSDavid Greenman 	daddr_t block;
11600d94caffSDavid Greenman 	struct timeval tv;
1161efc68ce1SDavid Greenman 	int runpg;
116226f9a767SRodney W. Grimes 
116326f9a767SRodney W. Grimes 	int error = 0;
116426f9a767SRodney W. Grimes 
116526f9a767SRodney W. Grimes retryoutput:
116626f9a767SRodney W. Grimes 	object = m[0]->object;	/* all vm_page_t items are in same object */
116726f9a767SRodney W. Grimes 
116826f9a767SRodney W. Grimes 	vp = vnp->vnp_vp;
11694abc71c0SDavid Greenman 
11704abc71c0SDavid Greenman 	/*
11714abc71c0SDavid Greenman 	 * Make sure underlying filesystem is still mounted.
11724abc71c0SDavid Greenman 	 */
11734abc71c0SDavid Greenman 	if (vp->v_mount == NULL)
11744abc71c0SDavid Greenman 		return VM_PAGER_FAIL;
11754abc71c0SDavid Greenman 
117626f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
117726f9a767SRodney W. Grimes 
117826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
117926f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
118026f9a767SRodney W. Grimes 
11810d94caffSDavid Greenman 	if ((int) m[0]->offset < 0) {
11820d94caffSDavid Greenman 		printf("vnode_pager_output: attempt to write meta-data!!! -- 0x%x\n", m[0]->offset);
11830d94caffSDavid Greenman 		m[0]->dirty = 0;
11840d94caffSDavid Greenman 		rtvals[0] = VM_PAGER_OK;
11850d94caffSDavid Greenman 		return VM_PAGER_OK;
11860d94caffSDavid Greenman 	}
118726f9a767SRodney W. Grimes 	/*
1188bbc0ec52SDavid Greenman 	 * if the filesystem does not have a bmap, then use the old code
118926f9a767SRodney W. Grimes 	 */
11900d94caffSDavid Greenman 	if (VOP_BMAP(vp, (m[0]->offset / bsize), &dp, &block, 0) ||
11910d94caffSDavid Greenman 	    (block == -1)) {
119226f9a767SRodney W. Grimes 
119326f9a767SRodney W. Grimes 		rtvals[0] = vnode_pager_output_old(vnp, m[0]);
119426f9a767SRodney W. Grimes 
11950d94caffSDavid Greenman 		m[0]->dirty = 0;
1196976e77fcSDavid Greenman 		cnt.v_vnodeout++;
1197976e77fcSDavid Greenman 		cnt.v_vnodepgsout++;
119826f9a767SRodney W. Grimes 		return rtvals[0];
119926f9a767SRodney W. Grimes 	}
12000d94caffSDavid Greenman 	tv = time;
12010d94caffSDavid Greenman 	VOP_UPDATE(vp, &tv, &tv, 0);
120226f9a767SRodney W. Grimes 
120326f9a767SRodney W. Grimes 	/*
1204bbc0ec52SDavid Greenman 	 * if the filesystem has a small blocksize, then use the small block
1205bbc0ec52SDavid Greenman 	 * filesystem output code
120626f9a767SRodney W. Grimes 	 */
120726f9a767SRodney W. Grimes 	if ((bsize < PAGE_SIZE) &&
120826f9a767SRodney W. Grimes 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
120926f9a767SRodney W. Grimes 
121026f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
121126f9a767SRodney W. Grimes 			rtvals[i] = vnode_pager_output_smlfs(vnp, m[i]);
121226f9a767SRodney W. Grimes 			if (rtvals[i] == VM_PAGER_OK) {
121326f9a767SRodney W. Grimes 				pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
121426f9a767SRodney W. Grimes 			}
121526f9a767SRodney W. Grimes 		}
1216976e77fcSDavid Greenman 		cnt.v_vnodeout++;
1217976e77fcSDavid Greenman 		cnt.v_vnodepgsout += count;
121826f9a767SRodney W. Grimes 		return rtvals[0];
121926f9a767SRodney W. Grimes 	}
122026f9a767SRodney W. Grimes 	for (i = 0; i < count; i++) {
12210d94caffSDavid Greenman 		foff = m[i]->offset;
122226f9a767SRodney W. Grimes 		if (foff >= vnp->vnp_size) {
122326f9a767SRodney W. Grimes 			for (j = i; j < count; j++)
122426f9a767SRodney W. Grimes 				rtvals[j] = VM_PAGER_BAD;
122526f9a767SRodney W. Grimes 			count = i;
122626f9a767SRodney W. Grimes 			break;
122726f9a767SRodney W. Grimes 		}
122826f9a767SRodney W. Grimes 	}
122926f9a767SRodney W. Grimes 	if (count == 0) {
123026f9a767SRodney W. Grimes 		return rtvals[0];
123126f9a767SRodney W. Grimes 	}
12320d94caffSDavid Greenman 	foff = m[0]->offset;
1233efc68ce1SDavid Greenman 	reqaddr = vnode_pager_addr(vp, foff, &runpg);
1234efc68ce1SDavid Greenman 	if( runpg < count)
1235efc68ce1SDavid Greenman 		count = runpg;
123626f9a767SRodney W. Grimes 
123726f9a767SRodney W. Grimes 	/*
123826f9a767SRodney W. Grimes 	 * calculate the size of the transfer
123926f9a767SRodney W. Grimes 	 */
124026f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
124126f9a767SRodney W. Grimes 	if ((foff + size) > vnp->vnp_size)
124226f9a767SRodney W. Grimes 		size = vnp->vnp_size - foff;
124326f9a767SRodney W. Grimes 
124426f9a767SRodney W. Grimes 	/*
124526f9a767SRodney W. Grimes 	 * round up physical size for real devices
124626f9a767SRodney W. Grimes 	 */
124726f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
124826f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
124926f9a767SRodney W. Grimes 
125016f62314SDavid Greenman 	bp = getpbuf();
125116f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
125226f9a767SRodney W. Grimes 	/*
125326f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
125426f9a767SRodney W. Grimes 	 */
125516f62314SDavid Greenman 	pmap_qenter(kva, m, count);
1256bbc0ec52SDavid Greenman 
125726f9a767SRodney W. Grimes 	/* build a minimal buffer header */
125826f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_WRITE | B_CALL;
125926f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
126026f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
126126f9a767SRodney W. Grimes 	bp->b_proc = curproc;
126226f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
126326f9a767SRodney W. Grimes 
126426f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
126526f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
126626f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
126726f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
126826f9a767SRodney W. Grimes 	bp->b_blkno = reqaddr / DEV_BSIZE;
12690d94caffSDavid Greenman 	pbgetvp(dp, bp);
127026f9a767SRodney W. Grimes 	++dp->v_numoutput;
127126f9a767SRodney W. Grimes 
127226f9a767SRodney W. Grimes 	/* for NFS */
127326f9a767SRodney W. Grimes 	bp->b_dirtyoff = 0;
127426f9a767SRodney W. Grimes 	bp->b_dirtyend = size;
127526f9a767SRodney W. Grimes 
127626f9a767SRodney W. Grimes 	bp->b_bcount = size;
127726f9a767SRodney W. Grimes 	bp->b_bufsize = size;
127826f9a767SRodney W. Grimes 
1279976e77fcSDavid Greenman 	cnt.v_vnodeout++;
1280976e77fcSDavid Greenman 	cnt.v_vnodepgsout += count;
1281976e77fcSDavid Greenman 
128226f9a767SRodney W. Grimes 	/* do the output */
128326f9a767SRodney W. Grimes 	VOP_STRATEGY(bp);
128426f9a767SRodney W. Grimes 
128526f9a767SRodney W. Grimes 	s = splbio();
128626f9a767SRodney W. Grimes 
128726f9a767SRodney W. Grimes 	/* we definitely need to be at splbio here */
128826f9a767SRodney W. Grimes 
128926f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
129026f9a767SRodney W. Grimes 		tsleep((caddr_t) bp, PVM, "vnwrite", 0);
129126f9a767SRodney W. Grimes 	}
129226f9a767SRodney W. Grimes 	splx(s);
129326f9a767SRodney W. Grimes 
129426f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
129526f9a767SRodney W. Grimes 		error = EIO;
129626f9a767SRodney W. Grimes 
129716f62314SDavid Greenman 	pmap_qremove(kva, count);
129826f9a767SRodney W. Grimes 
129926f9a767SRodney W. Grimes 	/*
130026f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
130126f9a767SRodney W. Grimes 	 */
130226f9a767SRodney W. Grimes 	relpbuf(bp);
130326f9a767SRodney W. Grimes 	HOLDRELE(vp);
130426f9a767SRodney W. Grimes 
130526f9a767SRodney W. Grimes 	if (!error) {
130626f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
130726f9a767SRodney W. Grimes 			pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
13080d94caffSDavid Greenman 			m[i]->dirty = 0;
130926f9a767SRodney W. Grimes 			rtvals[i] = VM_PAGER_OK;
131026f9a767SRodney W. Grimes 		}
131126f9a767SRodney W. Grimes 	} else if (count != 1) {
131226f9a767SRodney W. Grimes 		error = 0;
131326f9a767SRodney W. Grimes 		count = 1;
131426f9a767SRodney W. Grimes 		goto retryoutput;
131526f9a767SRodney W. Grimes 	}
131626f9a767SRodney W. Grimes 	if (error) {
1317a83c285cSDavid Greenman 		printf("vnode_pager_output: I/O write error\n");
131826f9a767SRodney W. Grimes 	}
1319a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
132026f9a767SRodney W. Grimes }
1321