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