xref: /freebsd/sys/vm/vnode_pager.c (revision 1130b656e5fe4c2d1ba299e024d1b40eaeebd380)
1 /*
2  * Copyright (c) 1990 University of Utah.
3  * Copyright (c) 1991 The Regents of the University of California.
4  * All rights reserved.
5  * Copyright (c) 1993, 1994 John S. Dyson
6  * Copyright (c) 1995, David Greenman
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
41  *	$FreeBSD$
42  */
43 
44 /*
45  * Page to/from files (vnodes).
46  */
47 
48 /*
49  * TODO:
50  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
51  *	greatly re-simplify the vnode_pager.
52  */
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/proc.h>
58 #include <sys/malloc.h>
59 #include <sys/vnode.h>
60 #include <sys/uio.h>
61 #include <sys/mount.h>
62 #include <sys/buf.h>
63 #include <sys/vmmeter.h>
64 
65 #include <vm/vm.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_prot.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pager.h>
71 #include <vm/vnode_pager.h>
72 #include <vm/vm_extern.h>
73 
74 static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
75 					 int *run));
76 static void vnode_pager_iodone __P((struct buf *bp));
77 static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
78 static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
79 static void vnode_pager_dealloc __P((vm_object_t));
80 static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
81 static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
82 static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
83 
84 struct pagerops vnodepagerops = {
85 	NULL,
86 	vnode_pager_alloc,
87 	vnode_pager_dealloc,
88 	vnode_pager_getpages,
89 	vnode_pager_putpages,
90 	vnode_pager_haspage,
91 	NULL
92 };
93 
94 static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m,
95 					  int count, int reqpage));
96 static int vnode_pager_leaf_putpages __P((vm_object_t object, vm_page_t *m,
97 					  int count, boolean_t sync,
98 					  int *rtvals));
99 
100 /*
101  * Allocate (or lookup) pager for a vnode.
102  * Handle is a vnode pointer.
103  */
104 vm_object_t
105 vnode_pager_alloc(handle, size, prot, offset)
106 	void *handle;
107 	vm_size_t size;
108 	vm_prot_t prot;
109 	vm_ooffset_t offset;
110 {
111 	vm_object_t object;
112 	struct vnode *vp;
113 
114 	/*
115 	 * Pageout to vnode, no can do yet.
116 	 */
117 	if (handle == NULL)
118 		return (NULL);
119 
120 	vp = (struct vnode *) handle;
121 
122 	/*
123 	 * Prevent race condition when allocating the object. This
124 	 * can happen with NFS vnodes since the nfsnode isn't locked.
125 	 */
126 	while (vp->v_flag & VOLOCK) {
127 		vp->v_flag |= VOWANT;
128 		tsleep(vp, PVM, "vnpobj", 0);
129 	}
130 	vp->v_flag |= VOLOCK;
131 
132 	/*
133 	 * If the object is being terminated, wait for it to
134 	 * go away.
135 	 */
136 	while (((object = vp->v_object) != NULL) &&
137 		(object->flags & OBJ_DEAD)) {
138 		tsleep(object, PVM, "vadead", 0);
139 	}
140 
141 	if (object == NULL) {
142 		/*
143 		 * And an object of the appropriate size
144 		 */
145 		object = vm_object_allocate(OBJT_VNODE, size);
146 		if (vp->v_type == VREG)
147 			object->flags = OBJ_CANPERSIST;
148 		else
149 			object->flags = 0;
150 
151 		/*
152 		 * Hold a reference to the vnode and initialize object data.
153 		 */
154 		vp->v_usecount++;
155 		object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE;
156 
157 		object->handle = handle;
158 		vp->v_object = object;
159 	} else {
160 		/*
161 		 * vm_object_reference() will remove the object from the cache if
162 		 * found and gain a reference to the object.
163 		 */
164 		vm_object_reference(object);
165 	}
166 
167 	if (vp->v_type == VREG)
168 		vp->v_flag |= VVMIO;
169 
170 	vp->v_flag &= ~VOLOCK;
171 	if (vp->v_flag & VOWANT) {
172 		vp->v_flag &= ~VOWANT;
173 		wakeup(vp);
174 	}
175 	return (object);
176 }
177 
178 static void
179 vnode_pager_dealloc(object)
180 	vm_object_t object;
181 {
182 	register struct vnode *vp = object->handle;
183 
184 	if (vp == NULL)
185 		panic("vnode_pager_dealloc: pager already dealloced");
186 
187 	if (object->paging_in_progress) {
188 		int s = splbio();
189 		while (object->paging_in_progress) {
190 			object->flags |= OBJ_PIPWNT;
191 			tsleep(object, PVM, "vnpdea", 0);
192 		}
193 		splx(s);
194 	}
195 
196 	object->handle = NULL;
197 
198 	vp->v_object = NULL;
199 	vp->v_flag &= ~(VTEXT | VVMIO);
200 	vp->v_flag |= VAGE;
201 	vrele(vp);
202 }
203 
204 static boolean_t
205 vnode_pager_haspage(object, pindex, before, after)
206 	vm_object_t object;
207 	vm_pindex_t pindex;
208 	int *before;
209 	int *after;
210 {
211 	struct vnode *vp = object->handle;
212 	daddr_t bn;
213 	int err;
214 	daddr_t reqblock;
215 	int poff;
216 	int bsize;
217 	int pagesperblock, blocksperpage;
218 
219 	/*
220 	 * If filesystem no longer mounted or offset beyond end of file we do
221 	 * not have the page.
222 	 */
223 	if ((vp->v_mount == NULL) ||
224 		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
225 		return FALSE;
226 
227 	bsize = vp->v_mount->mnt_stat.f_iosize;
228 	pagesperblock = bsize / PAGE_SIZE;
229 	blocksperpage = 0;
230 	if (pagesperblock > 0) {
231 		reqblock = pindex / pagesperblock;
232 	} else {
233 		blocksperpage = (PAGE_SIZE / bsize);
234 		reqblock = pindex * blocksperpage;
235 	}
236 	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
237 		after, before);
238 	if (err)
239 		return TRUE;
240 	if ( bn == -1)
241 		return FALSE;
242 	if (pagesperblock > 0) {
243 		poff = pindex - (reqblock * pagesperblock);
244 		if (before) {
245 			*before *= pagesperblock;
246 			*before += poff;
247 		}
248 		if (after) {
249 			int numafter;
250 			*after *= pagesperblock;
251 			numafter = pagesperblock - (poff + 1);
252 			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
253 				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
254 			}
255 			*after += numafter;
256 		}
257 	} else {
258 		if (before) {
259 			*before /= blocksperpage;
260 		}
261 
262 		if (after) {
263 			*after /= blocksperpage;
264 		}
265 	}
266 	return TRUE;
267 }
268 
269 /*
270  * Lets the VM system know about a change in size for a file.
271  * We adjust our own internal size and flush any cached pages in
272  * the associated object that are affected by the size change.
273  *
274  * Note: this routine may be invoked as a result of a pager put
275  * operation (possibly at object termination time), so we must be careful.
276  */
277 void
278 vnode_pager_setsize(vp, nsize)
279 	struct vnode *vp;
280 	vm_ooffset_t nsize;
281 {
282 	vm_object_t object = vp->v_object;
283 
284 	if (object == NULL)
285 		return;
286 
287 	/*
288 	 * Hasn't changed size
289 	 */
290 	if (nsize == object->un_pager.vnp.vnp_size)
291 		return;
292 
293 	/*
294 	 * File has shrunk. Toss any cached pages beyond the new EOF.
295 	 */
296 	if (nsize < object->un_pager.vnp.vnp_size) {
297 		vm_ooffset_t nsizerounded;
298 		nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_MASK));
299 		if (nsizerounded < object->un_pager.vnp.vnp_size) {
300 			vm_object_page_remove(object,
301 				OFF_TO_IDX(nsize + PAGE_MASK),
302 				OFF_TO_IDX(object->un_pager.vnp.vnp_size),
303 				FALSE);
304 		}
305 		/*
306 		 * this gets rid of garbage at the end of a page that is now
307 		 * only partially backed by the vnode...
308 		 */
309 		if (nsize & PAGE_MASK) {
310 			vm_offset_t kva;
311 			vm_page_t m;
312 
313 			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
314 			if (m) {
315 				kva = vm_pager_map_page(m);
316 				bzero((caddr_t) kva + (nsize & PAGE_MASK),
317 				    (int) (round_page(nsize) - nsize));
318 				vm_pager_unmap_page(kva);
319 			}
320 		}
321 	}
322 	object->un_pager.vnp.vnp_size = nsize;
323 	object->size = OFF_TO_IDX(nsize + PAGE_MASK);
324 }
325 
326 void
327 vnode_pager_umount(mp)
328 	register struct mount *mp;
329 {
330 	struct vnode *vp, *nvp;
331 
332 loop:
333 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
334 		/*
335 		 * Vnode can be reclaimed by getnewvnode() while we
336 		 * traverse the list.
337 		 */
338 		if (vp->v_mount != mp)
339 			goto loop;
340 
341 		/*
342 		 * Save the next pointer now since uncaching may terminate the
343 		 * object and render vnode invalid
344 		 */
345 		nvp = vp->v_mntvnodes.le_next;
346 
347 		if (vp->v_object != NULL) {
348 			VOP_LOCK(vp);
349 			vnode_pager_uncache(vp);
350 			VOP_UNLOCK(vp);
351 		}
352 	}
353 }
354 
355 /*
356  * Remove vnode associated object from the object cache.
357  * This routine must be called with the vnode locked.
358  *
359  * XXX unlock the vnode.
360  * We must do this since uncaching the object may result in its
361  * destruction which may initiate paging activity which may necessitate
362  * re-locking the vnode.
363  */
364 void
365 vnode_pager_uncache(vp)
366 	struct vnode *vp;
367 {
368 	vm_object_t object;
369 
370 	/*
371 	 * Not a mapped vnode
372 	 */
373 	object = vp->v_object;
374 	if (object == NULL)
375 		return;
376 
377 	vm_object_reference(object);
378 
379 	/*
380 	 * XXX We really should handle locking on
381 	 * VBLK devices...
382 	 */
383 	if (vp->v_type != VBLK)
384 		VOP_UNLOCK(vp);
385 	pager_cache(object, FALSE);
386 	if (vp->v_type != VBLK)
387 		VOP_LOCK(vp);
388 	return;
389 }
390 
391 
392 void
393 vnode_pager_freepage(m)
394 	vm_page_t m;
395 {
396 	PAGE_WAKEUP(m);
397 	vm_page_free(m);
398 }
399 
400 /*
401  * calculate the linear (byte) disk address of specified virtual
402  * file address
403  */
404 static vm_offset_t
405 vnode_pager_addr(vp, address, run)
406 	struct vnode *vp;
407 	vm_ooffset_t address;
408 	int *run;
409 {
410 	int rtaddress;
411 	int bsize;
412 	daddr_t block;
413 	struct vnode *rtvp;
414 	int err;
415 	daddr_t vblock;
416 	int voffset;
417 
418 	if ((int) address < 0)
419 		return -1;
420 
421 	if (vp->v_mount == NULL)
422 		return -1;
423 
424 	bsize = vp->v_mount->mnt_stat.f_iosize;
425 	vblock = address / bsize;
426 	voffset = address % bsize;
427 
428 	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
429 
430 	if (err || (block == -1))
431 		rtaddress = -1;
432 	else {
433 		rtaddress = block + voffset / DEV_BSIZE;
434 		if( run) {
435 			*run += 1;
436 			*run *= bsize/PAGE_SIZE;
437 			*run -= voffset/PAGE_SIZE;
438 		}
439 	}
440 
441 	return rtaddress;
442 }
443 
444 /*
445  * interrupt routine for I/O completion
446  */
447 static void
448 vnode_pager_iodone(bp)
449 	struct buf *bp;
450 {
451 	bp->b_flags |= B_DONE;
452 	wakeup(bp);
453 }
454 
455 /*
456  * small block file system vnode pager input
457  */
458 static int
459 vnode_pager_input_smlfs(object, m)
460 	vm_object_t object;
461 	vm_page_t m;
462 {
463 	int i;
464 	int s;
465 	struct vnode *dp, *vp;
466 	struct buf *bp;
467 	vm_offset_t kva;
468 	int fileaddr;
469 	vm_offset_t bsize;
470 	int error = 0;
471 
472 	vp = object->handle;
473 	if (vp->v_mount == NULL)
474 		return VM_PAGER_BAD;
475 
476 	bsize = vp->v_mount->mnt_stat.f_iosize;
477 
478 
479 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
480 
481 	kva = vm_pager_map_page(m);
482 
483 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
484 
485 		if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
486 			continue;
487 
488 		fileaddr = vnode_pager_addr(vp,
489 			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
490 		if (fileaddr != -1) {
491 			bp = getpbuf();
492 
493 			/* build a minimal buffer header */
494 			bp->b_flags = B_BUSY | B_READ | B_CALL;
495 			bp->b_iodone = vnode_pager_iodone;
496 			bp->b_proc = curproc;
497 			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
498 			if (bp->b_rcred != NOCRED)
499 				crhold(bp->b_rcred);
500 			if (bp->b_wcred != NOCRED)
501 				crhold(bp->b_wcred);
502 			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
503 			bp->b_blkno = fileaddr;
504 			pbgetvp(dp, bp);
505 			bp->b_bcount = bsize;
506 			bp->b_bufsize = bsize;
507 
508 			/* do the input */
509 			VOP_STRATEGY(bp);
510 
511 			/* we definitely need to be at splbio here */
512 
513 			s = splbio();
514 			while ((bp->b_flags & B_DONE) == 0) {
515 				tsleep(bp, PVM, "vnsrd", 0);
516 			}
517 			splx(s);
518 			if ((bp->b_flags & B_ERROR) != 0)
519 				error = EIO;
520 
521 			/*
522 			 * free the buffer header back to the swap buffer pool
523 			 */
524 			relpbuf(bp);
525 			if (error)
526 				break;
527 
528 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
529 		} else {
530 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
531 			bzero((caddr_t) kva + i * bsize, bsize);
532 		}
533 	}
534 	vm_pager_unmap_page(kva);
535 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
536 	m->flags &= ~PG_ZERO;
537 	if (error) {
538 		return VM_PAGER_ERROR;
539 	}
540 	return VM_PAGER_OK;
541 
542 }
543 
544 
545 /*
546  * old style vnode pager output routine
547  */
548 static int
549 vnode_pager_input_old(object, m)
550 	vm_object_t object;
551 	vm_page_t m;
552 {
553 	struct uio auio;
554 	struct iovec aiov;
555 	int error;
556 	int size;
557 	vm_offset_t kva;
558 
559 	error = 0;
560 
561 	/*
562 	 * Return failure if beyond current EOF
563 	 */
564 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
565 		return VM_PAGER_BAD;
566 	} else {
567 		size = PAGE_SIZE;
568 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
569 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
570 
571 		/*
572 		 * Allocate a kernel virtual address and initialize so that
573 		 * we can use VOP_READ/WRITE routines.
574 		 */
575 		kva = vm_pager_map_page(m);
576 
577 		aiov.iov_base = (caddr_t) kva;
578 		aiov.iov_len = size;
579 		auio.uio_iov = &aiov;
580 		auio.uio_iovcnt = 1;
581 		auio.uio_offset = IDX_TO_OFF(m->pindex);
582 		auio.uio_segflg = UIO_SYSSPACE;
583 		auio.uio_rw = UIO_READ;
584 		auio.uio_resid = size;
585 		auio.uio_procp = (struct proc *) 0;
586 
587 		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
588 		if (!error) {
589 			register int count = size - auio.uio_resid;
590 
591 			if (count == 0)
592 				error = EINVAL;
593 			else if (count != PAGE_SIZE)
594 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
595 		}
596 		vm_pager_unmap_page(kva);
597 	}
598 	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
599 	m->dirty = 0;
600 	m->flags &= ~PG_ZERO;
601 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
602 }
603 
604 /*
605  * generic vnode pager input routine
606  */
607 
608 static int
609 vnode_pager_getpages(object, m, count, reqpage)
610 	vm_object_t object;
611 	vm_page_t *m;
612 	int count;
613 	int reqpage;
614 {
615 	int rtval;
616 	struct vnode *vp;
617 	if (object->flags & OBJ_VNODE_GONE)
618 		return VM_PAGER_ERROR;
619 	vp = object->handle;
620 	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
621 	if (rtval == EOPNOTSUPP)
622 		return vnode_pager_leaf_getpages(object, m, count, reqpage);
623 	else
624 		return rtval;
625 }
626 
627 static int
628 vnode_pager_leaf_getpages(object, m, count, reqpage)
629 	vm_object_t object;
630 	vm_page_t *m;
631 	int count;
632 	int reqpage;
633 {
634 	vm_offset_t kva;
635 	off_t foff;
636 	int i, size, bsize, first, firstaddr;
637 	struct vnode *dp, *vp;
638 	int runpg;
639 	int runend;
640 	struct buf *bp;
641 	int s;
642 	int error = 0;
643 
644 	vp = object->handle;
645 	if (vp->v_mount == NULL)
646 		return VM_PAGER_BAD;
647 
648 	bsize = vp->v_mount->mnt_stat.f_iosize;
649 
650 	/* get the UNDERLYING device for the file with VOP_BMAP() */
651 
652 	/*
653 	 * originally, we did not check for an error return value -- assuming
654 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
655 	 */
656 	foff = IDX_TO_OFF(m[reqpage]->pindex);
657 
658 	/*
659 	 * if we can't bmap, use old VOP code
660 	 */
661 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
662 		for (i = 0; i < count; i++) {
663 			if (i != reqpage) {
664 				vnode_pager_freepage(m[i]);
665 			}
666 		}
667 		cnt.v_vnodein++;
668 		cnt.v_vnodepgsin++;
669 		return vnode_pager_input_old(object, m[reqpage]);
670 
671 		/*
672 		 * if the blocksize is smaller than a page size, then use
673 		 * special small filesystem code.  NFS sometimes has a small
674 		 * blocksize, but it can handle large reads itself.
675 		 */
676 	} else if ((PAGE_SIZE / bsize) > 1 &&
677 	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
678 
679 		for (i = 0; i < count; i++) {
680 			if (i != reqpage) {
681 				vnode_pager_freepage(m[i]);
682 			}
683 		}
684 		cnt.v_vnodein++;
685 		cnt.v_vnodepgsin++;
686 		return vnode_pager_input_smlfs(object, m[reqpage]);
687 	}
688 	/*
689 	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
690 	 * then, the entire page is valid --
691 	 */
692 	if (m[reqpage]->valid) {
693 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
694 		for (i = 0; i < count; i++) {
695 			if (i != reqpage)
696 				vnode_pager_freepage(m[i]);
697 		}
698 		return VM_PAGER_OK;
699 	}
700 
701 	/*
702 	 * here on direct device I/O
703 	 */
704 
705 	firstaddr = -1;
706 	/*
707 	 * calculate the run that includes the required page
708 	 */
709 	for(first = 0, i = 0; i < count; i = runend) {
710 		firstaddr = vnode_pager_addr(vp,
711 			IDX_TO_OFF(m[i]->pindex), &runpg);
712 		if (firstaddr == -1) {
713 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
714 				panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
715 			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
716 			}
717 			vnode_pager_freepage(m[i]);
718 			runend = i + 1;
719 			first = runend;
720 			continue;
721 		}
722 		runend = i + runpg;
723 		if (runend <= reqpage) {
724 			int j;
725 			for (j = i; j < runend; j++) {
726 				vnode_pager_freepage(m[j]);
727 			}
728 		} else {
729 			if (runpg < (count - first)) {
730 				for (i = first + runpg; i < count; i++)
731 					vnode_pager_freepage(m[i]);
732 				count = first + runpg;
733 			}
734 			break;
735 		}
736 		first = runend;
737 	}
738 
739 	/*
740 	 * the first and last page have been calculated now, move input pages
741 	 * to be zero based...
742 	 */
743 	if (first != 0) {
744 		for (i = first; i < count; i++) {
745 			m[i - first] = m[i];
746 		}
747 		count -= first;
748 		reqpage -= first;
749 	}
750 
751 	/*
752 	 * calculate the file virtual address for the transfer
753 	 */
754 	foff = IDX_TO_OFF(m[0]->pindex);
755 
756 	/*
757 	 * calculate the size of the transfer
758 	 */
759 	size = count * PAGE_SIZE;
760 	if ((foff + size) > object->un_pager.vnp.vnp_size)
761 		size = object->un_pager.vnp.vnp_size - foff;
762 
763 	/*
764 	 * round up physical size for real devices
765 	 */
766 	if (dp->v_type == VBLK || dp->v_type == VCHR)
767 		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
768 
769 	bp = getpbuf();
770 	kva = (vm_offset_t) bp->b_data;
771 
772 	/*
773 	 * and map the pages to be read into the kva
774 	 */
775 	pmap_qenter(kva, m, count);
776 
777 	/* build a minimal buffer header */
778 	bp->b_flags = B_BUSY | B_READ | B_CALL;
779 	bp->b_iodone = vnode_pager_iodone;
780 	/* B_PHYS is not set, but it is nice to fill this in */
781 	bp->b_proc = curproc;
782 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
783 	if (bp->b_rcred != NOCRED)
784 		crhold(bp->b_rcred);
785 	if (bp->b_wcred != NOCRED)
786 		crhold(bp->b_wcred);
787 	bp->b_blkno = firstaddr;
788 	pbgetvp(dp, bp);
789 	bp->b_bcount = size;
790 	bp->b_bufsize = size;
791 
792 	cnt.v_vnodein++;
793 	cnt.v_vnodepgsin += count;
794 
795 	/* do the input */
796 	VOP_STRATEGY(bp);
797 
798 	s = splbio();
799 	/* we definitely need to be at splbio here */
800 
801 	while ((bp->b_flags & B_DONE) == 0) {
802 		tsleep(bp, PVM, "vnread", 0);
803 	}
804 	splx(s);
805 	if ((bp->b_flags & B_ERROR) != 0)
806 		error = EIO;
807 
808 	if (!error) {
809 		if (size != count * PAGE_SIZE)
810 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
811 	}
812 	pmap_qremove(kva, count);
813 
814 	/*
815 	 * free the buffer header back to the swap buffer pool
816 	 */
817 	relpbuf(bp);
818 
819 	for (i = 0; i < count; i++) {
820 		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
821 		m[i]->dirty = 0;
822 		m[i]->valid = VM_PAGE_BITS_ALL;
823 		m[i]->flags &= ~PG_ZERO;
824 		if (i != reqpage) {
825 
826 			/*
827 			 * whether or not to leave the page activated is up in
828 			 * the air, but we should put the page on a page queue
829 			 * somewhere. (it already is in the object). Result:
830 			 * It appears that emperical results show that
831 			 * deactivating pages is best.
832 			 */
833 
834 			/*
835 			 * just in case someone was asking for this page we
836 			 * now tell them that it is ok to use
837 			 */
838 			if (!error) {
839 				vm_page_deactivate(m[i]);
840 				PAGE_WAKEUP(m[i]);
841 			} else {
842 				vnode_pager_freepage(m[i]);
843 			}
844 		}
845 	}
846 	if (error) {
847 		printf("vnode_pager_getpages: I/O read error\n");
848 	}
849 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
850 }
851 
852 static int
853 vnode_pager_putpages(object, m, count, sync, rtvals)
854 	vm_object_t object;
855 	vm_page_t *m;
856 	int count;
857 	boolean_t sync;
858 	int *rtvals;
859 {
860 	int rtval;
861 	struct vnode *vp;
862 
863 	if (object->flags & OBJ_VNODE_GONE)
864 		return VM_PAGER_ERROR;
865 
866 	vp = object->handle;
867 	rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
868 	if (rtval == EOPNOTSUPP)
869 		return vnode_pager_leaf_putpages(object, m, count, sync, rtvals);
870 	else
871 		return rtval;
872 }
873 
874 /*
875  * generic vnode pager output routine
876  */
877 static int
878 vnode_pager_leaf_putpages(object, m, count, sync, rtvals)
879 	vm_object_t object;
880 	vm_page_t *m;
881 	int count;
882 	boolean_t sync;
883 	int *rtvals;
884 {
885 	int i;
886 
887 	struct vnode *vp;
888 	int maxsize, ncount;
889 	vm_ooffset_t poffset;
890 	struct uio auio;
891 	struct iovec aiov;
892 	int error;
893 
894 	vp = object->handle;;
895 	for (i = 0; i < count; i++)
896 		rtvals[i] = VM_PAGER_AGAIN;
897 
898 	if ((int) m[0]->pindex < 0) {
899 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty);
900 		rtvals[0] = VM_PAGER_BAD;
901 		return VM_PAGER_BAD;
902 	}
903 
904 	maxsize = count * PAGE_SIZE;
905 	ncount = count;
906 
907 	poffset = IDX_TO_OFF(m[0]->pindex);
908 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
909 		if (object->un_pager.vnp.vnp_size > poffset)
910 			maxsize = object->un_pager.vnp.vnp_size - poffset;
911 		else
912 			maxsize = 0;
913 		ncount = btoc(maxsize);
914 		if (ncount < count) {
915 			for (i = ncount; i < count; i++) {
916 				rtvals[i] = VM_PAGER_BAD;
917 			}
918 #ifdef BOGUS
919 			if (ncount == 0) {
920 				printf("vnode_pager_putpages: write past end of file: %d, %lu\n",
921 					poffset,
922 					(unsigned long) object->un_pager.vnp.vnp_size);
923 				return rtvals[0];
924 			}
925 #endif
926 		}
927 	}
928 
929 	for (i = 0; i < count; i++) {
930 		m[i]->busy++;
931 		m[i]->flags &= ~PG_BUSY;
932 	}
933 
934 	aiov.iov_base = (caddr_t) 0;
935 	aiov.iov_len = maxsize;
936 	auio.uio_iov = &aiov;
937 	auio.uio_iovcnt = 1;
938 	auio.uio_offset = poffset;
939 	auio.uio_segflg = UIO_NOCOPY;
940 	auio.uio_rw = UIO_WRITE;
941 	auio.uio_resid = maxsize;
942 	auio.uio_procp = (struct proc *) 0;
943 	error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred);
944 	cnt.v_vnodeout++;
945 	cnt.v_vnodepgsout += ncount;
946 
947 	if (error) {
948 		printf("vnode_pager_putpages: I/O error %d\n", error);
949 	}
950 	if (auio.uio_resid) {
951 		printf("vnode_pager_putpages: residual I/O %d at %ld\n",
952 			auio.uio_resid, m[0]->pindex);
953 	}
954 	for (i = 0; i < count; i++) {
955 		m[i]->busy--;
956 		if (i < ncount) {
957 			rtvals[i] = VM_PAGER_OK;
958 		}
959 		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED))
960 			wakeup(m[i]);
961 	}
962 	return rtvals[0];
963 }
964 
965 struct vnode *
966 vnode_pager_lock(object)
967 	vm_object_t object;
968 {
969 	for (; object != NULL; object = object->backing_object) {
970 		if (object->type != OBJT_VNODE)
971 			continue;
972 
973 		VOP_LOCK(object->handle);
974 		return object->handle;
975 	}
976 	return NULL;
977 }
978