xref: /freebsd/sys/vm/vnode_pager.c (revision 6cde7a165f154ed47b58fc42d6d2041e18384680)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
326f9a767SRodney W. Grimes  * Copyright (c) 1991 The Regents of the University of California.
426f9a767SRodney W. Grimes  * All rights reserved.
526f9a767SRodney W. Grimes  * Copyright (c) 1993, 1994 John S. Dyson
624a1cce3SDavid Greenman  * Copyright (c) 1995, David Greenman
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
21df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
4026f9a767SRodney W. Grimes  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
416cde7a16SDavid Greenman  *	$Id: vnode_pager.c,v 1.99 1998/09/28 23:58:10 rvb Exp $
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes /*
45df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
46df8bae1dSRodney W. Grimes  */
47df8bae1dSRodney W. Grimes 
4826f9a767SRodney W. Grimes /*
4926f9a767SRodney W. Grimes  * TODO:
5024a1cce3SDavid Greenman  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
51f6b04d2bSDavid Greenman  *	greatly re-simplify the vnode_pager.
5226f9a767SRodney W. Grimes  */
5326f9a767SRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <sys/param.h>
55df8bae1dSRodney W. Grimes #include <sys/systm.h>
56df8bae1dSRodney W. Grimes #include <sys/proc.h>
57df8bae1dSRodney W. Grimes #include <sys/vnode.h>
58df8bae1dSRodney W. Grimes #include <sys/mount.h>
5924a1cce3SDavid Greenman #include <sys/buf.h>
60efeaf95aSDavid Greenman #include <sys/vmmeter.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <vm/vm.h>
63efeaf95aSDavid Greenman #include <vm/vm_prot.h>
64efeaf95aSDavid Greenman #include <vm/vm_object.h>
65df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
6624a1cce3SDavid Greenman #include <vm/vm_pager.h>
671efb74fbSJohn Dyson #include <vm/vm_map.h>
68df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
69efeaf95aSDavid Greenman #include <vm/vm_extern.h>
70df8bae1dSRodney W. Grimes 
71f708ef1bSPoul-Henning Kamp static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
720b8253a7SBruce Evans 					 int *run));
73f708ef1bSPoul-Henning Kamp static void vnode_pager_iodone __P((struct buf *bp));
74f708ef1bSPoul-Henning Kamp static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
75f708ef1bSPoul-Henning Kamp static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
76f708ef1bSPoul-Henning Kamp static void vnode_pager_dealloc __P((vm_object_t));
77f708ef1bSPoul-Henning Kamp static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
78f708ef1bSPoul-Henning Kamp static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
79f708ef1bSPoul-Henning Kamp static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
800b8253a7SBruce Evans 
81df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
8224a1cce3SDavid Greenman 	NULL,
83df8bae1dSRodney W. Grimes 	vnode_pager_alloc,
84df8bae1dSRodney W. Grimes 	vnode_pager_dealloc,
8524a1cce3SDavid Greenman 	vnode_pager_getpages,
8624a1cce3SDavid Greenman 	vnode_pager_putpages,
8724a1cce3SDavid Greenman 	vnode_pager_haspage,
8824a1cce3SDavid Greenman 	NULL
89df8bae1dSRodney W. Grimes };
90df8bae1dSRodney W. Grimes 
91170db9c6SJohn Dyson 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
94df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
95df8bae1dSRodney W. Grimes  */
9624a1cce3SDavid Greenman vm_object_t
976cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
98b9dcd593SBruce Evans 		  vm_ooffset_t offset)
99df8bae1dSRodney W. Grimes {
10006cb7259SDavid Greenman 	vm_object_t object;
101df8bae1dSRodney W. Grimes 	struct vnode *vp;
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes 	/*
104df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
105df8bae1dSRodney W. Grimes 	 */
106df8bae1dSRodney W. Grimes 	if (handle == NULL)
107df8bae1dSRodney W. Grimes 		return (NULL);
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
11039d38f93SDavid Greenman 
11139d38f93SDavid Greenman 	/*
11239d38f93SDavid Greenman 	 * Prevent race condition when allocating the object. This
11339d38f93SDavid Greenman 	 * can happen with NFS vnodes since the nfsnode isn't locked.
11439d38f93SDavid Greenman 	 */
11539d38f93SDavid Greenman 	while (vp->v_flag & VOLOCK) {
11639d38f93SDavid Greenman 		vp->v_flag |= VOWANT;
11739d38f93SDavid Greenman 		tsleep(vp, PVM, "vnpobj", 0);
11839d38f93SDavid Greenman 	}
11939d38f93SDavid Greenman 	vp->v_flag |= VOLOCK;
12039d38f93SDavid Greenman 
12139d38f93SDavid Greenman 	/*
12239d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
12339d38f93SDavid Greenman 	 * go away.
12439d38f93SDavid Greenman 	 */
125bd7e5f99SJohn Dyson 	while (((object = vp->v_object) != NULL) &&
126bd7e5f99SJohn Dyson 		(object->flags & OBJ_DEAD)) {
127aa2cabb9SDavid Greenman 		tsleep(object, PVM, "vadead", 0);
12824a1cce3SDavid Greenman 	}
1290d94caffSDavid Greenman 
1302be70f79SJohn Dyson 	if (vp->v_usecount == 0)
1312be70f79SJohn Dyson 		panic("vnode_pager_alloc: no vnode reference");
1322be70f79SJohn Dyson 
13324a1cce3SDavid Greenman 	if (object == NULL) {
134df8bae1dSRodney W. Grimes 		/*
135df8bae1dSRodney W. Grimes 		 * And an object of the appropriate size
136df8bae1dSRodney W. Grimes 		 */
1376cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
138ad5dd234SJohn Dyson 		object->flags = 0;
139bbc0ec52SDavid Greenman 
1406cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
14126f9a767SRodney W. Grimes 
14224a1cce3SDavid Greenman 		object->handle = handle;
14324a1cce3SDavid Greenman 		vp->v_object = object;
14495e5e988SJohn Dyson 		vp->v_usecount++;
145df8bae1dSRodney W. Grimes 	} else {
14695e5e988SJohn Dyson 		object->ref_count++;
14795e5e988SJohn Dyson 		vp->v_usecount++;
148df8bae1dSRodney W. Grimes 	}
14939d38f93SDavid Greenman 
15039d38f93SDavid Greenman 	vp->v_flag &= ~VOLOCK;
15139d38f93SDavid Greenman 	if (vp->v_flag & VOWANT) {
15239d38f93SDavid Greenman 		vp->v_flag &= ~VOWANT;
15339d38f93SDavid Greenman 		wakeup(vp);
15439d38f93SDavid Greenman 	}
15524a1cce3SDavid Greenman 	return (object);
156df8bae1dSRodney W. Grimes }
157df8bae1dSRodney W. Grimes 
158f708ef1bSPoul-Henning Kamp static void
15924a1cce3SDavid Greenman vnode_pager_dealloc(object)
1600d94caffSDavid Greenman 	vm_object_t object;
16124a1cce3SDavid Greenman {
16224a1cce3SDavid Greenman 	register struct vnode *vp = object->handle;
163df8bae1dSRodney W. Grimes 
16424a1cce3SDavid Greenman 	if (vp == NULL)
16524a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
16624a1cce3SDavid Greenman 
16766095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
16824a1cce3SDavid Greenman 
16924a1cce3SDavid Greenman 	object->handle = NULL;
17095461b45SJohn Dyson 	object->type = OBJT_DEAD;
171aa2cabb9SDavid Greenman 	vp->v_object = NULL;
17295e5e988SJohn Dyson 	vp->v_flag &= ~(VTEXT | VOBJBUF);
173df8bae1dSRodney W. Grimes }
17426f9a767SRodney W. Grimes 
175f708ef1bSPoul-Henning Kamp static boolean_t
176a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after)
17724a1cce3SDavid Greenman 	vm_object_t object;
178a316d390SJohn Dyson 	vm_pindex_t pindex;
17924a1cce3SDavid Greenman 	int *before;
18024a1cce3SDavid Greenman 	int *after;
181df8bae1dSRodney W. Grimes {
18224a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
183df8bae1dSRodney W. Grimes 	daddr_t bn;
1843af76890SPoul-Henning Kamp 	int err;
185170db9c6SJohn Dyson 	daddr_t reqblock;
1862c4488fcSJohn Dyson 	int poff;
1872c4488fcSJohn Dyson 	int bsize;
188d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
189df8bae1dSRodney W. Grimes 
19047221757SJohn Dyson 	if ((vp == NULL) || (vp->v_flag & VDOOMED))
19147221757SJohn Dyson 		return FALSE;
19247221757SJohn Dyson 
193df8bae1dSRodney W. Grimes 	/*
1940d94caffSDavid Greenman 	 * If filesystem no longer mounted or offset beyond end of file we do
1950d94caffSDavid Greenman 	 * not have the page.
196df8bae1dSRodney W. Grimes 	 */
197a316d390SJohn Dyson 	if ((vp->v_mount == NULL) ||
198a316d390SJohn Dyson 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
1994abc71c0SDavid Greenman 		return FALSE;
200df8bae1dSRodney W. Grimes 
201eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
202170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
203d63596ceSJohn Dyson 	blocksperpage = 0;
204d63596ceSJohn Dyson 	if (pagesperblock > 0) {
205a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
206d63596ceSJohn Dyson 	} else {
207d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
208d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
209d63596ceSJohn Dyson 	}
210170db9c6SJohn Dyson 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
211170db9c6SJohn Dyson 		after, before);
2120d94caffSDavid Greenman 	if (err)
21324a1cce3SDavid Greenman 		return TRUE;
2146eab77f2SJohn Dyson 	if ( bn == -1)
215ced399eeSJohn Dyson 		return FALSE;
216d63596ceSJohn Dyson 	if (pagesperblock > 0) {
217a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
218170db9c6SJohn Dyson 		if (before) {
219170db9c6SJohn Dyson 			*before *= pagesperblock;
220170db9c6SJohn Dyson 			*before += poff;
221170db9c6SJohn Dyson 		}
222170db9c6SJohn Dyson 		if (after) {
223b1fc01b7SJohn Dyson 			int numafter;
224170db9c6SJohn Dyson 			*after *= pagesperblock;
225b1fc01b7SJohn Dyson 			numafter = pagesperblock - (poff + 1);
226a316d390SJohn Dyson 			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
227a316d390SJohn Dyson 				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
228b1fc01b7SJohn Dyson 			}
229b1fc01b7SJohn Dyson 			*after += numafter;
230170db9c6SJohn Dyson 		}
231d63596ceSJohn Dyson 	} else {
232d63596ceSJohn Dyson 		if (before) {
233d63596ceSJohn Dyson 			*before /= blocksperpage;
234d63596ceSJohn Dyson 		}
235d63596ceSJohn Dyson 
236d63596ceSJohn Dyson 		if (after) {
237d63596ceSJohn Dyson 			*after /= blocksperpage;
238d63596ceSJohn Dyson 		}
239d63596ceSJohn Dyson 	}
240ced399eeSJohn Dyson 	return TRUE;
241df8bae1dSRodney W. Grimes }
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes /*
244df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
24524a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
246df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
247df8bae1dSRodney W. Grimes  *
248df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
249df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
250df8bae1dSRodney W. Grimes  */
251df8bae1dSRodney W. Grimes void
252df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize)
253df8bae1dSRodney W. Grimes 	struct vnode *vp;
254a316d390SJohn Dyson 	vm_ooffset_t nsize;
255df8bae1dSRodney W. Grimes {
256c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
25724a1cce3SDavid Greenman 	vm_object_t object = vp->v_object;
258df8bae1dSRodney W. Grimes 
25924a1cce3SDavid Greenman 	if (object == NULL)
260df8bae1dSRodney W. Grimes 		return;
261bbc0ec52SDavid Greenman 
262df8bae1dSRodney W. Grimes 	/*
263df8bae1dSRodney W. Grimes 	 * Hasn't changed size
264df8bae1dSRodney W. Grimes 	 */
26524a1cce3SDavid Greenman 	if (nsize == object->un_pager.vnp.vnp_size)
266df8bae1dSRodney W. Grimes 		return;
267bbc0ec52SDavid Greenman 
268c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
269c576d121SLuoqi Chen 
270df8bae1dSRodney W. Grimes 	/*
271bbc0ec52SDavid Greenman 	 * File has shrunk. Toss any cached pages beyond the new EOF.
272df8bae1dSRodney W. Grimes 	 */
27324a1cce3SDavid Greenman 	if (nsize < object->un_pager.vnp.vnp_size) {
2741efb74fbSJohn Dyson 		vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
275c576d121SLuoqi Chen 		if (nobjsize < object->size) {
276c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
277c576d121SLuoqi Chen 				FALSE);
2780d94caffSDavid Greenman 		}
279bbc0ec52SDavid Greenman 		/*
280bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
281bbc0ec52SDavid Greenman 		 * only partially backed by the vnode...
282bbc0ec52SDavid Greenman 		 */
283bbc0ec52SDavid Greenman 		if (nsize & PAGE_MASK) {
284bbc0ec52SDavid Greenman 			vm_offset_t kva;
285bbc0ec52SDavid Greenman 			vm_page_t m;
286bbc0ec52SDavid Greenman 
287a316d390SJohn Dyson 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
288bbc0ec52SDavid Greenman 			if (m) {
289bbc0ec52SDavid Greenman 				kva = vm_pager_map_page(m);
290bbc0ec52SDavid Greenman 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
291a316d390SJohn Dyson 				    (int) (round_page(nsize) - nsize));
292bbc0ec52SDavid Greenman 				vm_pager_unmap_page(kva);
293bbc0ec52SDavid Greenman 			}
294bbc0ec52SDavid Greenman 		}
295bbc0ec52SDavid Greenman 	}
296a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
297c576d121SLuoqi Chen 	object->size = nobjsize;
298df8bae1dSRodney W. Grimes }
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes void
30126f9a767SRodney W. Grimes vnode_pager_freepage(m)
30226f9a767SRodney W. Grimes 	vm_page_t m;
303df8bae1dSRodney W. Grimes {
30426f9a767SRodney W. Grimes 	vm_page_free(m);
30526f9a767SRodney W. Grimes }
30626f9a767SRodney W. Grimes 
30726f9a767SRodney W. Grimes /*
30826f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
30926f9a767SRodney W. Grimes  * file address
31026f9a767SRodney W. Grimes  */
311f708ef1bSPoul-Henning Kamp static vm_offset_t
312efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run)
31326f9a767SRodney W. Grimes 	struct vnode *vp;
314a316d390SJohn Dyson 	vm_ooffset_t address;
315efc68ce1SDavid Greenman 	int *run;
31626f9a767SRodney W. Grimes {
31726f9a767SRodney W. Grimes 	int rtaddress;
31826f9a767SRodney W. Grimes 	int bsize;
319a316d390SJohn Dyson 	daddr_t block;
32026f9a767SRodney W. Grimes 	struct vnode *rtvp;
32126f9a767SRodney W. Grimes 	int err;
322a316d390SJohn Dyson 	daddr_t vblock;
323a316d390SJohn Dyson 	int voffset;
32426f9a767SRodney W. Grimes 
3250d94caffSDavid Greenman 	if ((int) address < 0)
3260d94caffSDavid Greenman 		return -1;
3270d94caffSDavid Greenman 
3282c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3292c4488fcSJohn Dyson 		return -1;
3302c4488fcSJohn Dyson 
33126f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
33226f9a767SRodney W. Grimes 	vblock = address / bsize;
33326f9a767SRodney W. Grimes 	voffset = address % bsize;
33426f9a767SRodney W. Grimes 
335c83ebe77SJohn Dyson 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
33626f9a767SRodney W. Grimes 
337efc68ce1SDavid Greenman 	if (err || (block == -1))
33826f9a767SRodney W. Grimes 		rtaddress = -1;
339efc68ce1SDavid Greenman 	else {
340187f0071SDavid Greenman 		rtaddress = block + voffset / DEV_BSIZE;
341efc68ce1SDavid Greenman 		if( run) {
342efc68ce1SDavid Greenman 			*run += 1;
343efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
344efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
345efc68ce1SDavid Greenman 		}
346efc68ce1SDavid Greenman 	}
34726f9a767SRodney W. Grimes 
34826f9a767SRodney W. Grimes 	return rtaddress;
34926f9a767SRodney W. Grimes }
35026f9a767SRodney W. Grimes 
35126f9a767SRodney W. Grimes /*
35226f9a767SRodney W. Grimes  * interrupt routine for I/O completion
35326f9a767SRodney W. Grimes  */
354f708ef1bSPoul-Henning Kamp static void
35526f9a767SRodney W. Grimes vnode_pager_iodone(bp)
35626f9a767SRodney W. Grimes 	struct buf *bp;
35726f9a767SRodney W. Grimes {
35826f9a767SRodney W. Grimes 	bp->b_flags |= B_DONE;
35924a1cce3SDavid Greenman 	wakeup(bp);
36026f9a767SRodney W. Grimes }
36126f9a767SRodney W. Grimes 
36226f9a767SRodney W. Grimes /*
36326f9a767SRodney W. Grimes  * small block file system vnode pager input
36426f9a767SRodney W. Grimes  */
365f708ef1bSPoul-Henning Kamp static int
36624a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m)
36724a1cce3SDavid Greenman 	vm_object_t object;
36826f9a767SRodney W. Grimes 	vm_page_t m;
36926f9a767SRodney W. Grimes {
37026f9a767SRodney W. Grimes 	int i;
37126f9a767SRodney W. Grimes 	int s;
37226f9a767SRodney W. Grimes 	struct vnode *dp, *vp;
37326f9a767SRodney W. Grimes 	struct buf *bp;
37426f9a767SRodney W. Grimes 	vm_offset_t kva;
37526f9a767SRodney W. Grimes 	int fileaddr;
37626f9a767SRodney W. Grimes 	vm_offset_t bsize;
37726f9a767SRodney W. Grimes 	int error = 0;
37826f9a767SRodney W. Grimes 
37924a1cce3SDavid Greenman 	vp = object->handle;
3802c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
3812c4488fcSJohn Dyson 		return VM_PAGER_BAD;
3822c4488fcSJohn Dyson 
38326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
38426f9a767SRodney W. Grimes 
3850bdb7528SDavid Greenman 
386c83ebe77SJohn Dyson 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
38726f9a767SRodney W. Grimes 
38826f9a767SRodney W. Grimes 	kva = vm_pager_map_page(m);
38926f9a767SRodney W. Grimes 
39026f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
391bbc0ec52SDavid Greenman 
392a316d390SJohn Dyson 		if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
39326f9a767SRodney W. Grimes 			continue;
39426f9a767SRodney W. Grimes 
395a316d390SJohn Dyson 		fileaddr = vnode_pager_addr(vp,
396a316d390SJohn Dyson 			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
39726f9a767SRodney W. Grimes 		if (fileaddr != -1) {
39826f9a767SRodney W. Grimes 			bp = getpbuf();
39926f9a767SRodney W. Grimes 
40026f9a767SRodney W. Grimes 			/* build a minimal buffer header */
40126f9a767SRodney W. Grimes 			bp->b_flags = B_BUSY | B_READ | B_CALL;
40226f9a767SRodney W. Grimes 			bp->b_iodone = vnode_pager_iodone;
40326f9a767SRodney W. Grimes 			bp->b_proc = curproc;
40426f9a767SRodney W. Grimes 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
40526f9a767SRodney W. Grimes 			if (bp->b_rcred != NOCRED)
40626f9a767SRodney W. Grimes 				crhold(bp->b_rcred);
40726f9a767SRodney W. Grimes 			if (bp->b_wcred != NOCRED)
40826f9a767SRodney W. Grimes 				crhold(bp->b_wcred);
409ab3f7469SPoul-Henning Kamp 			bp->b_data = (caddr_t) kva + i * bsize;
410187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
4110d94caffSDavid Greenman 			pbgetvp(dp, bp);
41226f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
41326f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
41426f9a767SRodney W. Grimes 
41526f9a767SRodney W. Grimes 			/* do the input */
416fd5d1124SJulian Elischer 			VOP_STRATEGY(bp->b_vp, bp);
41726f9a767SRodney W. Grimes 
418e47ed70bSJohn Dyson 			/* we definitely need to be at splvm here */
41926f9a767SRodney W. Grimes 
420e47ed70bSJohn Dyson 			s = splvm();
42126f9a767SRodney W. Grimes 			while ((bp->b_flags & B_DONE) == 0) {
422aa2cabb9SDavid Greenman 				tsleep(bp, PVM, "vnsrd", 0);
42326f9a767SRodney W. Grimes 			}
42426f9a767SRodney W. Grimes 			splx(s);
42526f9a767SRodney W. Grimes 			if ((bp->b_flags & B_ERROR) != 0)
42626f9a767SRodney W. Grimes 				error = EIO;
42726f9a767SRodney W. Grimes 
42826f9a767SRodney W. Grimes 			/*
42926f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
43026f9a767SRodney W. Grimes 			 */
43126f9a767SRodney W. Grimes 			relpbuf(bp);
43226f9a767SRodney W. Grimes 			if (error)
43326f9a767SRodney W. Grimes 				break;
4340d94caffSDavid Greenman 
435aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
43626f9a767SRodney W. Grimes 		} else {
437aa8de40aSPoul-Henning Kamp 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
43826f9a767SRodney W. Grimes 			bzero((caddr_t) kva + i * bsize, bsize);
43926f9a767SRodney W. Grimes 		}
44026f9a767SRodney W. Grimes 	}
44126f9a767SRodney W. Grimes 	vm_pager_unmap_page(kva);
44267bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
443e69763a3SDoug Rabson 	vm_page_flag_clear(m, PG_ZERO);
44426f9a767SRodney W. Grimes 	if (error) {
445a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
44626f9a767SRodney W. Grimes 	}
44726f9a767SRodney W. Grimes 	return VM_PAGER_OK;
44826f9a767SRodney W. Grimes 
44926f9a767SRodney W. Grimes }
45026f9a767SRodney W. Grimes 
45126f9a767SRodney W. Grimes 
45226f9a767SRodney W. Grimes /*
45326f9a767SRodney W. Grimes  * old style vnode pager output routine
45426f9a767SRodney W. Grimes  */
455f708ef1bSPoul-Henning Kamp static int
45624a1cce3SDavid Greenman vnode_pager_input_old(object, m)
45724a1cce3SDavid Greenman 	vm_object_t object;
45826f9a767SRodney W. Grimes 	vm_page_t m;
45926f9a767SRodney W. Grimes {
460df8bae1dSRodney W. Grimes 	struct uio auio;
461df8bae1dSRodney W. Grimes 	struct iovec aiov;
46226f9a767SRodney W. Grimes 	int error;
46326f9a767SRodney W. Grimes 	int size;
46426f9a767SRodney W. Grimes 	vm_offset_t kva;
465df8bae1dSRodney W. Grimes 
46626f9a767SRodney W. Grimes 	error = 0;
467bbc0ec52SDavid Greenman 
468df8bae1dSRodney W. Grimes 	/*
46926f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
47026f9a767SRodney W. Grimes 	 */
471a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
47226f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
47326f9a767SRodney W. Grimes 	} else {
47426f9a767SRodney W. Grimes 		size = PAGE_SIZE;
475a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
476a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
4770bdb7528SDavid Greenman 
47826f9a767SRodney W. Grimes 		/*
479df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
480df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
481df8bae1dSRodney W. Grimes 		 */
48226f9a767SRodney W. Grimes 		kva = vm_pager_map_page(m);
4830bdb7528SDavid Greenman 
484df8bae1dSRodney W. Grimes 		aiov.iov_base = (caddr_t) kva;
485df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
486df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
487df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
488a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
489df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
49026f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
491df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
492df8bae1dSRodney W. Grimes 		auio.uio_procp = (struct proc *) 0;
49326f9a767SRodney W. Grimes 
49424a1cce3SDavid Greenman 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
495df8bae1dSRodney W. Grimes 		if (!error) {
496df8bae1dSRodney W. Grimes 			register int count = size - auio.uio_resid;
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 			if (count == 0)
499df8bae1dSRodney W. Grimes 				error = EINVAL;
50026f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
50126f9a767SRodney W. Grimes 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
502df8bae1dSRodney W. Grimes 		}
50326f9a767SRodney W. Grimes 		vm_pager_unmap_page(kva);
504df8bae1dSRodney W. Grimes 	}
50567bf6868SJohn Dyson 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
5060d94caffSDavid Greenman 	m->dirty = 0;
507e69763a3SDoug Rabson 	vm_page_flag_clear(m, PG_ZERO);
5086e3a3f38SRobert V. Baron 	if (!error)
5096e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
510a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
51126f9a767SRodney W. Grimes }
51226f9a767SRodney W. Grimes 
51326f9a767SRodney W. Grimes /*
51426f9a767SRodney W. Grimes  * generic vnode pager input routine
51526f9a767SRodney W. Grimes  */
516170db9c6SJohn Dyson 
517ce75f2c3SMike Smith /*
518ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
519ce75f2c3SMike Smith  * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to
520ce75f2c3SMike Smith  * vnode_pager_generic_getpages() to implement the previous behaviour.
521ce75f2c3SMike Smith  *
522ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
523ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
524ce75f2c3SMike Smith  */
525f708ef1bSPoul-Henning Kamp static int
52624a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage)
52726f9a767SRodney W. Grimes 	vm_object_t object;
52824a1cce3SDavid Greenman 	vm_page_t *m;
52924a1cce3SDavid Greenman 	int count;
53024a1cce3SDavid Greenman 	int reqpage;
53124a1cce3SDavid Greenman {
532170db9c6SJohn Dyson 	int rtval;
533170db9c6SJohn Dyson 	struct vnode *vp;
53486ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
53595e5e988SJohn Dyson 
536170db9c6SJohn Dyson 	vp = object->handle;
537ce75f2c3SMike Smith 	/*
538ce75f2c3SMike Smith 	 * XXX temporary diagnostic message to help track stale FS code,
539ce75f2c3SMike Smith 	 * Returning EOPNOTSUPP from here may make things unhappy.
540ce75f2c3SMike Smith 	 */
54186ffbd76SMike Smith 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
54286ffbd76SMike Smith 	if (rtval == EOPNOTSUPP) {
54386ffbd76SMike Smith 	    printf("vnode_pager: *** WARNING *** stale FS getpages\n");
54486ffbd76SMike Smith 	    rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage);
54586ffbd76SMike Smith 	}
546170db9c6SJohn Dyson 	return rtval;
547170db9c6SJohn Dyson }
548170db9c6SJohn Dyson 
549ce75f2c3SMike Smith 
550ce75f2c3SMike Smith /*
551ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
552ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
553ce75f2c3SMike Smith  */
554ce75f2c3SMike Smith int
555ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
556ce75f2c3SMike Smith 	struct vnode *vp;
557170db9c6SJohn Dyson 	vm_page_t *m;
558ce75f2c3SMike Smith 	int bytecount;
559170db9c6SJohn Dyson 	int reqpage;
560170db9c6SJohn Dyson {
561ce75f2c3SMike Smith 	vm_object_t object;
562a316d390SJohn Dyson 	vm_offset_t kva;
5638f9110f6SJohn Dyson 	off_t foff, tfoff, nextoff;
56424a1cce3SDavid Greenman 	int i, size, bsize, first, firstaddr;
565ce75f2c3SMike Smith 	struct vnode *dp;
566efc68ce1SDavid Greenman 	int runpg;
567efc68ce1SDavid Greenman 	int runend;
5680bdb7528SDavid Greenman 	struct buf *bp;
56926f9a767SRodney W. Grimes 	int s;
570ce75f2c3SMike Smith 	int count;
57126f9a767SRodney W. Grimes 	int error = 0;
57226f9a767SRodney W. Grimes 
573ce75f2c3SMike Smith 	object = vp->v_object;
574ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
575ce75f2c3SMike Smith 
5762c4488fcSJohn Dyson 	if (vp->v_mount == NULL)
5772c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5782c4488fcSJohn Dyson 
57926f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
58026f9a767SRodney W. Grimes 
58126f9a767SRodney W. Grimes 	/* get the UNDERLYING device for the file with VOP_BMAP() */
582bbc0ec52SDavid Greenman 
58326f9a767SRodney W. Grimes 	/*
584bbc0ec52SDavid Greenman 	 * originally, we did not check for an error return value -- assuming
585bbc0ec52SDavid Greenman 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
58626f9a767SRodney W. Grimes 	 */
587a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[reqpage]->pindex);
588bbc0ec52SDavid Greenman 
58926f9a767SRodney W. Grimes 	/*
59016f62314SDavid Greenman 	 * if we can't bmap, use old VOP code
59126f9a767SRodney W. Grimes 	 */
592c83ebe77SJohn Dyson 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
59326f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
59426f9a767SRodney W. Grimes 			if (i != reqpage) {
59526f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
59626f9a767SRodney W. Grimes 			}
59726f9a767SRodney W. Grimes 		}
598976e77fcSDavid Greenman 		cnt.v_vnodein++;
599976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
60024a1cce3SDavid Greenman 		return vnode_pager_input_old(object, m[reqpage]);
601bbc0ec52SDavid Greenman 
60226f9a767SRodney W. Grimes 		/*
60326f9a767SRodney W. Grimes 		 * if the blocksize is smaller than a page size, then use
60426f9a767SRodney W. Grimes 		 * special small filesystem code.  NFS sometimes has a small
60526f9a767SRodney W. Grimes 		 * blocksize, but it can handle large reads itself.
60626f9a767SRodney W. Grimes 		 */
60726f9a767SRodney W. Grimes 	} else if ((PAGE_SIZE / bsize) > 1 &&
608500b04a2SBruce Evans 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
60926f9a767SRodney W. Grimes 		for (i = 0; i < count; i++) {
61026f9a767SRodney W. Grimes 			if (i != reqpage) {
61126f9a767SRodney W. Grimes 				vnode_pager_freepage(m[i]);
61226f9a767SRodney W. Grimes 			}
61326f9a767SRodney W. Grimes 		}
614976e77fcSDavid Greenman 		cnt.v_vnodein++;
615976e77fcSDavid Greenman 		cnt.v_vnodepgsin++;
61624a1cce3SDavid Greenman 		return vnode_pager_input_smlfs(object, m[reqpage]);
61726f9a767SRodney W. Grimes 	}
61826f9a767SRodney W. Grimes 	/*
6190d94caffSDavid Greenman 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
6200d94caffSDavid Greenman 	 * then, the entire page is valid --
62132ad9cb5SDoug Rabson 	 * XXX no it isn't
6220d94caffSDavid Greenman 	 */
62332ad9cb5SDoug Rabson 
62432ad9cb5SDoug Rabson 	if (m[reqpage]->valid != VM_PAGE_BITS_ALL)
62532ad9cb5SDoug Rabson 	    m[reqpage]->valid = 0;
62632ad9cb5SDoug Rabson 
6270d94caffSDavid Greenman 	if (m[reqpage]->valid) {
6280d94caffSDavid Greenman 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
6290d94caffSDavid Greenman 		for (i = 0; i < count; i++) {
6300d94caffSDavid Greenman 			if (i != reqpage)
6310d94caffSDavid Greenman 				vnode_pager_freepage(m[i]);
6320d94caffSDavid Greenman 		}
6330d94caffSDavid Greenman 		return VM_PAGER_OK;
6340d94caffSDavid Greenman 	}
6350bdb7528SDavid Greenman 
6360d94caffSDavid Greenman 	/*
63726f9a767SRodney W. Grimes 	 * here on direct device I/O
63826f9a767SRodney W. Grimes 	 */
63926f9a767SRodney W. Grimes 
640efc68ce1SDavid Greenman 	firstaddr = -1;
64126f9a767SRodney W. Grimes 	/*
642efc68ce1SDavid Greenman 	 * calculate the run that includes the required page
64326f9a767SRodney W. Grimes 	 */
644efc68ce1SDavid Greenman 	for(first = 0, i = 0; i < count; i = runend) {
645a316d390SJohn Dyson 		firstaddr = vnode_pager_addr(vp,
646a316d390SJohn Dyson 			IDX_TO_OFF(m[i]->pindex), &runpg);
647efc68ce1SDavid Greenman 		if (firstaddr == -1) {
64824a1cce3SDavid Greenman 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
649fc62ef1fSBruce Evans 				/* XXX no %qd in kernel. */
650fc62ef1fSBruce Evans 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx",
651fc62ef1fSBruce Evans 			   	 firstaddr, (u_long)(foff >> 32),
652fc62ef1fSBruce Evans 			   	 (u_long)(u_int32_t)foff,
653fc62ef1fSBruce Evans 				 (u_long)(u_int32_t)
654fc62ef1fSBruce Evans 				 (object->un_pager.vnp.vnp_size >> 32),
655fc62ef1fSBruce Evans 				 (u_long)(u_int32_t)
656fc62ef1fSBruce Evans 				 object->un_pager.vnp.vnp_size);
657efc68ce1SDavid Greenman 			}
65826f9a767SRodney W. Grimes 			vnode_pager_freepage(m[i]);
659efc68ce1SDavid Greenman 			runend = i + 1;
660efc68ce1SDavid Greenman 			first = runend;
661efc68ce1SDavid Greenman 			continue;
662efc68ce1SDavid Greenman 		}
663efc68ce1SDavid Greenman 		runend = i + runpg;
664efc68ce1SDavid Greenman 		if (runend <= reqpage) {
665efc68ce1SDavid Greenman 			int j;
666efc68ce1SDavid Greenman 			for (j = i; j < runend; j++) {
667efc68ce1SDavid Greenman 				vnode_pager_freepage(m[j]);
668efc68ce1SDavid Greenman 			}
66926f9a767SRodney W. Grimes 		} else {
670efc68ce1SDavid Greenman 			if (runpg < (count - first)) {
671efc68ce1SDavid Greenman 				for (i = first + runpg; i < count; i++)
67226f9a767SRodney W. Grimes 					vnode_pager_freepage(m[i]);
673efc68ce1SDavid Greenman 				count = first + runpg;
67426f9a767SRodney W. Grimes 			}
675efc68ce1SDavid Greenman 			break;
67626f9a767SRodney W. Grimes 		}
677efc68ce1SDavid Greenman 		first = runend;
678efc68ce1SDavid Greenman 	}
67926f9a767SRodney W. Grimes 
68026f9a767SRodney W. Grimes 	/*
681bbc0ec52SDavid Greenman 	 * the first and last page have been calculated now, move input pages
682bbc0ec52SDavid Greenman 	 * to be zero based...
68326f9a767SRodney W. Grimes 	 */
68426f9a767SRodney W. Grimes 	if (first != 0) {
68526f9a767SRodney W. Grimes 		for (i = first; i < count; i++) {
68626f9a767SRodney W. Grimes 			m[i - first] = m[i];
68726f9a767SRodney W. Grimes 		}
68826f9a767SRodney W. Grimes 		count -= first;
68926f9a767SRodney W. Grimes 		reqpage -= first;
69026f9a767SRodney W. Grimes 	}
691efc68ce1SDavid Greenman 
69226f9a767SRodney W. Grimes 	/*
69326f9a767SRodney W. Grimes 	 * calculate the file virtual address for the transfer
69426f9a767SRodney W. Grimes 	 */
695a316d390SJohn Dyson 	foff = IDX_TO_OFF(m[0]->pindex);
69626f9a767SRodney W. Grimes 
69726f9a767SRodney W. Grimes 	/*
69826f9a767SRodney W. Grimes 	 * calculate the size of the transfer
69926f9a767SRodney W. Grimes 	 */
70026f9a767SRodney W. Grimes 	size = count * PAGE_SIZE;
70124a1cce3SDavid Greenman 	if ((foff + size) > object->un_pager.vnp.vnp_size)
70224a1cce3SDavid Greenman 		size = object->un_pager.vnp.vnp_size - foff;
70326f9a767SRodney W. Grimes 
70426f9a767SRodney W. Grimes 	/*
70526f9a767SRodney W. Grimes 	 * round up physical size for real devices
70626f9a767SRodney W. Grimes 	 */
70726f9a767SRodney W. Grimes 	if (dp->v_type == VBLK || dp->v_type == VCHR)
70826f9a767SRodney W. Grimes 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
70926f9a767SRodney W. Grimes 
7106d40c3d3SDavid Greenman 	bp = getpbuf();
71116f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
71216f62314SDavid Greenman 
71326f9a767SRodney W. Grimes 	/*
71426f9a767SRodney W. Grimes 	 * and map the pages to be read into the kva
71526f9a767SRodney W. Grimes 	 */
71616f62314SDavid Greenman 	pmap_qenter(kva, m, count);
71726f9a767SRodney W. Grimes 
71826f9a767SRodney W. Grimes 	/* build a minimal buffer header */
71926f9a767SRodney W. Grimes 	bp->b_flags = B_BUSY | B_READ | B_CALL;
72026f9a767SRodney W. Grimes 	bp->b_iodone = vnode_pager_iodone;
72126f9a767SRodney W. Grimes 	/* B_PHYS is not set, but it is nice to fill this in */
72226f9a767SRodney W. Grimes 	bp->b_proc = curproc;
72326f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
72426f9a767SRodney W. Grimes 	if (bp->b_rcred != NOCRED)
72526f9a767SRodney W. Grimes 		crhold(bp->b_rcred);
72626f9a767SRodney W. Grimes 	if (bp->b_wcred != NOCRED)
72726f9a767SRodney W. Grimes 		crhold(bp->b_wcred);
728187f0071SDavid Greenman 	bp->b_blkno = firstaddr;
7290d94caffSDavid Greenman 	pbgetvp(dp, bp);
73026f9a767SRodney W. Grimes 	bp->b_bcount = size;
73126f9a767SRodney W. Grimes 	bp->b_bufsize = size;
73226f9a767SRodney W. Grimes 
733976e77fcSDavid Greenman 	cnt.v_vnodein++;
734976e77fcSDavid Greenman 	cnt.v_vnodepgsin += count;
735976e77fcSDavid Greenman 
73626f9a767SRodney W. Grimes 	/* do the input */
737fd5d1124SJulian Elischer 	VOP_STRATEGY(bp->b_vp, bp);
738976e77fcSDavid Greenman 
739e47ed70bSJohn Dyson 	s = splvm();
740e47ed70bSJohn Dyson 	/* we definitely need to be at splvm here */
74126f9a767SRodney W. Grimes 
74226f9a767SRodney W. Grimes 	while ((bp->b_flags & B_DONE) == 0) {
743aa2cabb9SDavid Greenman 		tsleep(bp, PVM, "vnread", 0);
74426f9a767SRodney W. Grimes 	}
74526f9a767SRodney W. Grimes 	splx(s);
74626f9a767SRodney W. Grimes 	if ((bp->b_flags & B_ERROR) != 0)
74726f9a767SRodney W. Grimes 		error = EIO;
74826f9a767SRodney W. Grimes 
74926f9a767SRodney W. Grimes 	if (!error) {
75026f9a767SRodney W. Grimes 		if (size != count * PAGE_SIZE)
75126f9a767SRodney W. Grimes 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
75226f9a767SRodney W. Grimes 	}
75316f62314SDavid Greenman 	pmap_qremove(kva, count);
75426f9a767SRodney W. Grimes 
75526f9a767SRodney W. Grimes 	/*
75626f9a767SRodney W. Grimes 	 * free the buffer header back to the swap buffer pool
75726f9a767SRodney W. Grimes 	 */
75826f9a767SRodney W. Grimes 	relpbuf(bp);
75926f9a767SRodney W. Grimes 
7608f9110f6SJohn Dyson 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
7618f9110f6SJohn Dyson 		vm_page_t mt;
7628f9110f6SJohn Dyson 
7638f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
7648f9110f6SJohn Dyson 		mt = m[i];
7658f9110f6SJohn Dyson 
7668f9110f6SJohn Dyson 		if (nextoff <= size) {
7678f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
7688f9110f6SJohn Dyson 			mt->dirty = 0;
7698f9110f6SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
7708f9110f6SJohn Dyson 		} else {
7718f9110f6SJohn Dyson 			int nvalid = ((size + DEV_BSIZE - 1) - tfoff) & ~(DEV_BSIZE - 1);
7728f9110f6SJohn Dyson 			vm_page_set_validclean(mt, 0, nvalid);
7738f9110f6SJohn Dyson 		}
7748f9110f6SJohn Dyson 
775e69763a3SDoug Rabson 		vm_page_flag_clear(mt, PG_ZERO);
77626f9a767SRodney W. Grimes 		if (i != reqpage) {
777bbc0ec52SDavid Greenman 
77826f9a767SRodney W. Grimes 			/*
779bbc0ec52SDavid Greenman 			 * whether or not to leave the page activated is up in
780bbc0ec52SDavid Greenman 			 * the air, but we should put the page on a page queue
781bbc0ec52SDavid Greenman 			 * somewhere. (it already is in the object). Result:
782bbc0ec52SDavid Greenman 			 * It appears that emperical results show that
783bbc0ec52SDavid Greenman 			 * deactivating pages is best.
78426f9a767SRodney W. Grimes 			 */
785bbc0ec52SDavid Greenman 
78626f9a767SRodney W. Grimes 			/*
787bbc0ec52SDavid Greenman 			 * just in case someone was asking for this page we
788bbc0ec52SDavid Greenman 			 * now tell them that it is ok to use
78926f9a767SRodney W. Grimes 			 */
79026f9a767SRodney W. Grimes 			if (!error) {
7918f9110f6SJohn Dyson 				if (mt->flags & PG_WANTED)
7928f9110f6SJohn Dyson 					vm_page_activate(mt);
79395461b45SJohn Dyson 				else
7948f9110f6SJohn Dyson 					vm_page_deactivate(mt);
795e69763a3SDoug Rabson 				vm_page_wakeup(mt);
79626f9a767SRodney W. Grimes 			} else {
7978f9110f6SJohn Dyson 				vnode_pager_freepage(mt);
79826f9a767SRodney W. Grimes 			}
79926f9a767SRodney W. Grimes 		}
80026f9a767SRodney W. Grimes 	}
80126f9a767SRodney W. Grimes 	if (error) {
80224a1cce3SDavid Greenman 		printf("vnode_pager_getpages: I/O read error\n");
80326f9a767SRodney W. Grimes 	}
804a83c285cSDavid Greenman 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
80526f9a767SRodney W. Grimes }
80626f9a767SRodney W. Grimes 
807ce75f2c3SMike Smith /*
808ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
809ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
810ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
811ce75f2c3SMike Smith  *
812ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
813ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
814ce75f2c3SMike Smith  */
815f708ef1bSPoul-Henning Kamp static int
816170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals)
817170db9c6SJohn Dyson 	vm_object_t object;
818170db9c6SJohn Dyson 	vm_page_t *m;
819170db9c6SJohn Dyson 	int count;
820170db9c6SJohn Dyson 	boolean_t sync;
821170db9c6SJohn Dyson 	int *rtvals;
822170db9c6SJohn Dyson {
823170db9c6SJohn Dyson 	int rtval;
824170db9c6SJohn Dyson 	struct vnode *vp;
82586ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
826ad980522SJohn Dyson 
827170db9c6SJohn Dyson 	vp = object->handle;
82886ffbd76SMike Smith 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
82986ffbd76SMike Smith 	if (rtval == EOPNOTSUPP) {
83086ffbd76SMike Smith 	    printf("vnode_pager: *** WARNING *** stale FS putpages\n");
83186ffbd76SMike Smith 	    rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals);
83286ffbd76SMike Smith 	}
83386ffbd76SMike Smith 	return rtval;
834170db9c6SJohn Dyson }
835170db9c6SJohn Dyson 
836ce75f2c3SMike Smith 
83726f9a767SRodney W. Grimes /*
838ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
839ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
84026f9a767SRodney W. Grimes  */
841ce75f2c3SMike Smith int
8428f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
843ce75f2c3SMike Smith 	struct vnode *vp;
84426f9a767SRodney W. Grimes 	vm_page_t *m;
845ce75f2c3SMike Smith 	int bytecount;
8468f9110f6SJohn Dyson 	int flags;
84726f9a767SRodney W. Grimes 	int *rtvals;
84826f9a767SRodney W. Grimes {
849f6b04d2bSDavid Greenman 	int i;
850ce75f2c3SMike Smith 	vm_object_t object;
851ce75f2c3SMike Smith 	int count;
85226f9a767SRodney W. Grimes 
853f6b04d2bSDavid Greenman 	int maxsize, ncount;
854a316d390SJohn Dyson 	vm_ooffset_t poffset;
855f6b04d2bSDavid Greenman 	struct uio auio;
856f6b04d2bSDavid Greenman 	struct iovec aiov;
857f6b04d2bSDavid Greenman 	int error;
8588f9110f6SJohn Dyson 	int ioflags;
85926f9a767SRodney W. Grimes 
860ce75f2c3SMike Smith 	object = vp->v_object;
861ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
862ce75f2c3SMike Smith 
86326f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
86426f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_AGAIN;
86526f9a767SRodney W. Grimes 
866a316d390SJohn Dyson 	if ((int) m[0]->pindex < 0) {
8678f9110f6SJohn Dyson 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n",
8688f9110f6SJohn Dyson 			m[0]->pindex, m[0]->dirty);
869f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
870f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
8710d94caffSDavid Greenman 	}
8720bdb7528SDavid Greenman 
873f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
874f6b04d2bSDavid Greenman 	ncount = count;
87526f9a767SRodney W. Grimes 
876a316d390SJohn Dyson 	poffset = IDX_TO_OFF(m[0]->pindex);
877a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
878a316d390SJohn Dyson 		if (object->un_pager.vnp.vnp_size > poffset)
879a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
8805f55e841SDavid Greenman 		else
8815f55e841SDavid Greenman 			maxsize = 0;
882aa8de40aSPoul-Henning Kamp 		ncount = btoc(maxsize);
883f6b04d2bSDavid Greenman 		if (ncount < count) {
884f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
885f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
886f6b04d2bSDavid Greenman 			}
887f6b04d2bSDavid Greenman 		}
888f6b04d2bSDavid Greenman 	}
88926f9a767SRodney W. Grimes 
8908f9110f6SJohn Dyson 	ioflags = IO_VMIO;
8918f9110f6SJohn Dyson 	ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: 0;
8928f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
893f6b04d2bSDavid Greenman 
894f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
895f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
896f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
897f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
898a316d390SJohn Dyson 	auio.uio_offset = poffset;
899f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
900f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
901f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
902f6b04d2bSDavid Greenman 	auio.uio_procp = (struct proc *) 0;
9038f9110f6SJohn Dyson 	error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred);
904976e77fcSDavid Greenman 	cnt.v_vnodeout++;
905f6b04d2bSDavid Greenman 	cnt.v_vnodepgsout += ncount;
906f6b04d2bSDavid Greenman 
907f6b04d2bSDavid Greenman 	if (error) {
90824a1cce3SDavid Greenman 		printf("vnode_pager_putpages: I/O error %d\n", error);
909f6b04d2bSDavid Greenman 	}
910f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
911ac1e407bSBruce Evans 		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
912ac1e407bSBruce Evans 		    auio.uio_resid, (u_long)m[0]->pindex);
91326f9a767SRodney W. Grimes 	}
914ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
91526f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
91626f9a767SRodney W. Grimes 	}
917f6b04d2bSDavid Greenman 	return rtvals[0];
91826f9a767SRodney W. Grimes }
919f6b04d2bSDavid Greenman 
920f6b04d2bSDavid Greenman struct vnode *
92124a1cce3SDavid Greenman vnode_pager_lock(object)
92224a1cce3SDavid Greenman 	vm_object_t object;
92324a1cce3SDavid Greenman {
924996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
925996c772fSJohn Dyson 
92624a1cce3SDavid Greenman 	for (; object != NULL; object = object->backing_object) {
92724a1cce3SDavid Greenman 		if (object->type != OBJT_VNODE)
928f6b04d2bSDavid Greenman 			continue;
92947221757SJohn Dyson 		if (object->flags & OBJ_DEAD)
93047221757SJohn Dyson 			return NULL;
931f6b04d2bSDavid Greenman 
93247221757SJohn Dyson 		while (vget(object->handle,
93347221757SJohn Dyson 			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) {
934bef608bdSJohn Dyson 			if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE))
935bef608bdSJohn Dyson 				return NULL;
93647221757SJohn Dyson 			printf("vnode_pager_lock: retrying\n");
93747221757SJohn Dyson 		}
93824a1cce3SDavid Greenman 		return object->handle;
93926f9a767SRodney W. Grimes 	}
94024a1cce3SDavid Greenman 	return NULL;
941f6b04d2bSDavid Greenman }
942