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