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