xref: /freebsd/sys/vm/vnode_pager.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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  */
42 
43 /*
44  * Page to/from files (vnodes).
45  */
46 
47 /*
48  * TODO:
49  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
50  *	greatly re-simplify the vnode_pager.
51  */
52 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/vnode.h>
60 #include <sys/mount.h>
61 #include <sys/bio.h>
62 #include <sys/buf.h>
63 #include <sys/vmmeter.h>
64 #include <sys/conf.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_pager.h>
70 #include <vm/vm_map.h>
71 #include <vm/vnode_pager.h>
72 #include <vm/vm_extern.h>
73 
74 static void vnode_pager_init(void);
75 static vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
76 					 int *run);
77 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
78 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
79 static void vnode_pager_dealloc(vm_object_t);
80 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
81 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
82 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
83 
84 struct pagerops vnodepagerops = {
85 	.pgo_init =	vnode_pager_init,
86 	.pgo_alloc =	vnode_pager_alloc,
87 	.pgo_dealloc =	vnode_pager_dealloc,
88 	.pgo_getpages =	vnode_pager_getpages,
89 	.pgo_putpages =	vnode_pager_putpages,
90 	.pgo_haspage =	vnode_pager_haspage,
91 };
92 
93 int vnode_pbuf_freecnt;
94 
95 static void
96 vnode_pager_init(void)
97 {
98 
99 	vnode_pbuf_freecnt = nswbuf / 2 + 1;
100 }
101 
102 /*
103  * Allocate (or lookup) pager for a vnode.
104  * Handle is a vnode pointer.
105  *
106  * MPSAFE
107  */
108 vm_object_t
109 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
110 		  vm_ooffset_t offset)
111 {
112 	vm_object_t object;
113 	struct vnode *vp;
114 
115 	/*
116 	 * Pageout to vnode, no can do yet.
117 	 */
118 	if (handle == NULL)
119 		return (NULL);
120 
121 	vp = (struct vnode *) handle;
122 
123 	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
124 
125 	mtx_lock(&Giant);
126 	/*
127 	 * Prevent race condition when allocating the object. This
128 	 * can happen with NFS vnodes since the nfsnode isn't locked.
129 	 */
130 	VI_LOCK(vp);
131 	while (vp->v_iflag & VI_OLOCK) {
132 		vp->v_iflag |= VI_OWANT;
133 		msleep(vp, VI_MTX(vp), PVM, "vnpobj", 0);
134 	}
135 	vp->v_iflag |= VI_OLOCK;
136 	VI_UNLOCK(vp);
137 
138 	/*
139 	 * If the object is being terminated, wait for it to
140 	 * go away.
141 	 */
142 	while ((object = vp->v_object) != NULL) {
143 		VM_OBJECT_LOCK(object);
144 		if ((object->flags & OBJ_DEAD) == 0)
145 			break;
146 		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
147 	}
148 
149 	if (vp->v_usecount == 0)
150 		panic("vnode_pager_alloc: no vnode reference");
151 
152 	if (object == NULL) {
153 		/*
154 		 * And an object of the appropriate size
155 		 */
156 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
157 
158 		object->un_pager.vnp.vnp_size = size;
159 
160 		object->handle = handle;
161 		vp->v_object = object;
162 	} else {
163 		object->ref_count++;
164 		VM_OBJECT_UNLOCK(object);
165 	}
166 	VI_LOCK(vp);
167 	vp->v_usecount++;
168 	vp->v_iflag &= ~VI_OLOCK;
169 	if (vp->v_iflag & VI_OWANT) {
170 		vp->v_iflag &= ~VI_OWANT;
171 		wakeup(vp);
172 	}
173 	VI_UNLOCK(vp);
174 	mtx_unlock(&Giant);
175 	return (object);
176 }
177 
178 /*
179  *	The object must be locked.
180  */
181 static void
182 vnode_pager_dealloc(object)
183 	vm_object_t object;
184 {
185 	struct vnode *vp = object->handle;
186 
187 	if (vp == NULL)
188 		panic("vnode_pager_dealloc: pager already dealloced");
189 
190 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
191 	vm_object_pip_wait(object, "vnpdea");
192 
193 	object->handle = NULL;
194 	object->type = OBJT_DEAD;
195 	ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc");
196 	vp->v_object = NULL;
197 	vp->v_vflag &= ~(VV_TEXT | VV_OBJBUF);
198 }
199 
200 static boolean_t
201 vnode_pager_haspage(object, pindex, before, after)
202 	vm_object_t object;
203 	vm_pindex_t pindex;
204 	int *before;
205 	int *after;
206 {
207 	struct vnode *vp = object->handle;
208 	daddr_t bn;
209 	int err;
210 	daddr_t reqblock;
211 	int poff;
212 	int bsize;
213 	int pagesperblock, blocksperpage;
214 
215 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
216 	/*
217 	 * If no vp or vp is doomed or marked transparent to VM, we do not
218 	 * have the page.
219 	 */
220 	if (vp == NULL)
221 		return FALSE;
222 
223 	VI_LOCK(vp);
224 	if (vp->v_iflag & VI_DOOMED) {
225 		VI_UNLOCK(vp);
226 		return FALSE;
227 	}
228 	VI_UNLOCK(vp);
229 	/*
230 	 * If filesystem no longer mounted or offset beyond end of file we do
231 	 * not have the page.
232 	 */
233 	if ((vp->v_mount == NULL) ||
234 	    (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
235 		return FALSE;
236 
237 	bsize = vp->v_mount->mnt_stat.f_iosize;
238 	pagesperblock = bsize / PAGE_SIZE;
239 	blocksperpage = 0;
240 	if (pagesperblock > 0) {
241 		reqblock = pindex / pagesperblock;
242 	} else {
243 		blocksperpage = (PAGE_SIZE / bsize);
244 		reqblock = pindex * blocksperpage;
245 	}
246 	VM_OBJECT_UNLOCK(object);
247 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
248 	VM_OBJECT_LOCK(object);
249 	if (err)
250 		return TRUE;
251 	if (bn == -1)
252 		return FALSE;
253 	if (pagesperblock > 0) {
254 		poff = pindex - (reqblock * pagesperblock);
255 		if (before) {
256 			*before *= pagesperblock;
257 			*before += poff;
258 		}
259 		if (after) {
260 			int numafter;
261 			*after *= pagesperblock;
262 			numafter = pagesperblock - (poff + 1);
263 			if (IDX_TO_OFF(pindex + numafter) >
264 			    object->un_pager.vnp.vnp_size) {
265 				numafter =
266 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
267 				    pindex;
268 			}
269 			*after += numafter;
270 		}
271 	} else {
272 		if (before) {
273 			*before /= blocksperpage;
274 		}
275 
276 		if (after) {
277 			*after /= blocksperpage;
278 		}
279 	}
280 	return TRUE;
281 }
282 
283 /*
284  * Lets the VM system know about a change in size for a file.
285  * We adjust our own internal size and flush any cached pages in
286  * the associated object that are affected by the size change.
287  *
288  * Note: this routine may be invoked as a result of a pager put
289  * operation (possibly at object termination time), so we must be careful.
290  */
291 void
292 vnode_pager_setsize(vp, nsize)
293 	struct vnode *vp;
294 	vm_ooffset_t nsize;
295 {
296 	vm_object_t object;
297 	vm_page_t m;
298 	vm_pindex_t nobjsize;
299 
300 	if ((object = vp->v_object) == NULL)
301 		return;
302 	VM_OBJECT_LOCK(object);
303 	if (nsize == object->un_pager.vnp.vnp_size) {
304 		/*
305 		 * Hasn't changed size
306 		 */
307 		VM_OBJECT_UNLOCK(object);
308 		return;
309 	}
310 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
311 	if (nsize < object->un_pager.vnp.vnp_size) {
312 		/*
313 		 * File has shrunk. Toss any cached pages beyond the new EOF.
314 		 */
315 		if (nobjsize < object->size)
316 			vm_object_page_remove(object, nobjsize, object->size,
317 			    FALSE);
318 		/*
319 		 * this gets rid of garbage at the end of a page that is now
320 		 * only partially backed by the vnode.
321 		 *
322 		 * XXX for some reason (I don't know yet), if we take a
323 		 * completely invalid page and mark it partially valid
324 		 * it can screw up NFS reads, so we don't allow the case.
325 		 */
326 		if ((nsize & PAGE_MASK) &&
327 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL) {
328 			vm_page_lock_queues();
329 			if (m->valid) {
330 				int base = (int)nsize & PAGE_MASK;
331 				int size = PAGE_SIZE - base;
332 
333 				/*
334 				 * Clear out partial-page garbage in case
335 				 * the page has been mapped.
336 				 */
337 				pmap_zero_page_area(m, base, size);
338 
339 				/*
340 				 * XXX work around SMP data integrity race
341 				 * by unmapping the page from user processes.
342 				 * The garbage we just cleared may be mapped
343 				 * to a user process running on another cpu
344 				 * and this code is not running through normal
345 				 * I/O channels which handle SMP issues for
346 				 * us, so unmap page to synchronize all cpus.
347 				 *
348 				 * XXX should vm_pager_unmap_page() have
349 				 * dealt with this?
350 				 */
351 				pmap_remove_all(m);
352 
353 				/*
354 				 * Clear out partial-page dirty bits.  This
355 				 * has the side effect of setting the valid
356 				 * bits, but that is ok.  There are a bunch
357 				 * of places in the VM system where we expected
358 				 * m->dirty == VM_PAGE_BITS_ALL.  The file EOF
359 				 * case is one of them.  If the page is still
360 				 * partially dirty, make it fully dirty.
361 				 *
362 				 * note that we do not clear out the valid
363 				 * bits.  This would prevent bogus_page
364 				 * replacement from working properly.
365 				 */
366 				vm_page_set_validclean(m, base, size);
367 				if (m->dirty != 0)
368 					m->dirty = VM_PAGE_BITS_ALL;
369 			}
370 			vm_page_unlock_queues();
371 		}
372 	}
373 	object->un_pager.vnp.vnp_size = nsize;
374 	object->size = nobjsize;
375 	VM_OBJECT_UNLOCK(object);
376 }
377 
378 /*
379  * calculate the linear (byte) disk address of specified virtual
380  * file address
381  */
382 static vm_offset_t
383 vnode_pager_addr(vp, address, run)
384 	struct vnode *vp;
385 	vm_ooffset_t address;
386 	int *run;
387 {
388 	int rtaddress;
389 	int bsize;
390 	daddr_t block;
391 	int err;
392 	daddr_t vblock;
393 	int voffset;
394 
395 	GIANT_REQUIRED;
396 	if ((int) address < 0)
397 		return -1;
398 
399 	if (vp->v_mount == NULL)
400 		return -1;
401 
402 	bsize = vp->v_mount->mnt_stat.f_iosize;
403 	vblock = address / bsize;
404 	voffset = address % bsize;
405 
406 	err = VOP_BMAP(vp, vblock, NULL, &block, run, NULL);
407 
408 	if (err || (block == -1))
409 		rtaddress = -1;
410 	else {
411 		rtaddress = block + voffset / DEV_BSIZE;
412 		if (run) {
413 			*run += 1;
414 			*run *= bsize/PAGE_SIZE;
415 			*run -= voffset/PAGE_SIZE;
416 		}
417 	}
418 
419 	return rtaddress;
420 }
421 
422 /*
423  * small block filesystem vnode pager input
424  */
425 static int
426 vnode_pager_input_smlfs(object, m)
427 	vm_object_t object;
428 	vm_page_t m;
429 {
430 	int i;
431 	struct vnode *dp, *vp;
432 	struct buf *bp;
433 	vm_offset_t kva;
434 	int fileaddr;
435 	vm_offset_t bsize;
436 	int error = 0;
437 
438 	GIANT_REQUIRED;
439 
440 	vp = object->handle;
441 	if (vp->v_mount == NULL)
442 		return VM_PAGER_BAD;
443 
444 	bsize = vp->v_mount->mnt_stat.f_iosize;
445 
446 	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
447 
448 	kva = vm_pager_map_page(m);
449 
450 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
451 		vm_ooffset_t address;
452 
453 		if (vm_page_bits(i * bsize, bsize) & m->valid)
454 			continue;
455 
456 		address = IDX_TO_OFF(m->pindex) + i * bsize;
457 		if (address >= object->un_pager.vnp.vnp_size) {
458 			fileaddr = -1;
459 		} else {
460 			fileaddr = vnode_pager_addr(vp, address, NULL);
461 		}
462 		if (fileaddr != -1) {
463 			bp = getpbuf(&vnode_pbuf_freecnt);
464 
465 			/* build a minimal buffer header */
466 			bp->b_iocmd = BIO_READ;
467 			bp->b_iodone = bdone;
468 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
469 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
470 			bp->b_rcred = crhold(curthread->td_ucred);
471 			bp->b_wcred = crhold(curthread->td_ucred);
472 			bp->b_data = (caddr_t) kva + i * bsize;
473 			bp->b_blkno = fileaddr;
474 			pbgetvp(dp, bp);
475 			bp->b_bcount = bsize;
476 			bp->b_bufsize = bsize;
477 			bp->b_runningbufspace = bp->b_bufsize;
478 			runningbufspace += bp->b_runningbufspace;
479 
480 			/* do the input */
481 			VOP_SPECSTRATEGY(bp->b_vp, bp);
482 
483 			/* we definitely need to be at splvm here */
484 
485 			bwait(bp, PVM, "vnsrd");
486 
487 			if ((bp->b_ioflags & BIO_ERROR) != 0)
488 				error = EIO;
489 
490 			/*
491 			 * free the buffer header back to the swap buffer pool
492 			 */
493 			relpbuf(bp, &vnode_pbuf_freecnt);
494 			if (error)
495 				break;
496 
497 			vm_page_lock_queues();
498 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
499 			vm_page_unlock_queues();
500 		} else {
501 			vm_page_lock_queues();
502 			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
503 			vm_page_unlock_queues();
504 			bzero((caddr_t) kva + i * bsize, bsize);
505 		}
506 	}
507 	vm_pager_unmap_page(kva);
508 	vm_page_lock_queues();
509 	pmap_clear_modify(m);
510 	vm_page_flag_clear(m, PG_ZERO);
511 	vm_page_unlock_queues();
512 	if (error) {
513 		return VM_PAGER_ERROR;
514 	}
515 	return VM_PAGER_OK;
516 
517 }
518 
519 
520 /*
521  * old style vnode pager output routine
522  */
523 static int
524 vnode_pager_input_old(object, m)
525 	vm_object_t object;
526 	vm_page_t m;
527 {
528 	struct uio auio;
529 	struct iovec aiov;
530 	int error;
531 	int size;
532 	vm_offset_t kva;
533 	struct vnode *vp;
534 
535 	GIANT_REQUIRED;
536 	error = 0;
537 
538 	/*
539 	 * Return failure if beyond current EOF
540 	 */
541 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
542 		return VM_PAGER_BAD;
543 	} else {
544 		size = PAGE_SIZE;
545 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
546 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
547 
548 		/*
549 		 * Allocate a kernel virtual address and initialize so that
550 		 * we can use VOP_READ/WRITE routines.
551 		 */
552 		kva = vm_pager_map_page(m);
553 
554 		vp = object->handle;
555 		aiov.iov_base = (caddr_t) kva;
556 		aiov.iov_len = size;
557 		auio.uio_iov = &aiov;
558 		auio.uio_iovcnt = 1;
559 		auio.uio_offset = IDX_TO_OFF(m->pindex);
560 		auio.uio_segflg = UIO_SYSSPACE;
561 		auio.uio_rw = UIO_READ;
562 		auio.uio_resid = size;
563 		auio.uio_td = curthread;
564 
565 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
566 		if (!error) {
567 			int count = size - auio.uio_resid;
568 
569 			if (count == 0)
570 				error = EINVAL;
571 			else if (count != PAGE_SIZE)
572 				bzero((caddr_t) kva + count, PAGE_SIZE - count);
573 		}
574 		vm_pager_unmap_page(kva);
575 	}
576 	vm_page_lock_queues();
577 	pmap_clear_modify(m);
578 	vm_page_undirty(m);
579 	vm_page_flag_clear(m, PG_ZERO);
580 	if (!error)
581 		m->valid = VM_PAGE_BITS_ALL;
582 	vm_page_unlock_queues();
583 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
584 }
585 
586 /*
587  * generic vnode pager input routine
588  */
589 
590 /*
591  * Local media VFS's that do not implement their own VOP_GETPAGES
592  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
593  * to implement the previous behaviour.
594  *
595  * All other FS's should use the bypass to get to the local media
596  * backing vp's VOP_GETPAGES.
597  */
598 static int
599 vnode_pager_getpages(object, m, count, reqpage)
600 	vm_object_t object;
601 	vm_page_t *m;
602 	int count;
603 	int reqpage;
604 {
605 	int rtval;
606 	struct vnode *vp;
607 	int bytes = count * PAGE_SIZE;
608 
609 	vp = object->handle;
610 	VM_OBJECT_UNLOCK(object);
611 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
612 	KASSERT(rtval != EOPNOTSUPP,
613 	    ("vnode_pager: FS getpages not implemented\n"));
614 	VM_OBJECT_LOCK(object);
615 	return rtval;
616 }
617 
618 /*
619  * This is now called from local media FS's to operate against their
620  * own vnodes if they fail to implement VOP_GETPAGES.
621  */
622 int
623 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
624 	struct vnode *vp;
625 	vm_page_t *m;
626 	int bytecount;
627 	int reqpage;
628 {
629 	vm_object_t object;
630 	vm_offset_t kva;
631 	off_t foff, tfoff, nextoff;
632 	int i, j, size, bsize, first, firstaddr;
633 	struct vnode *dp;
634 	int runpg;
635 	int runend;
636 	struct buf *bp;
637 	int count;
638 	int error = 0;
639 
640 	GIANT_REQUIRED;
641 	object = vp->v_object;
642 	count = bytecount / PAGE_SIZE;
643 
644 	if (vp->v_mount == NULL)
645 		return VM_PAGER_BAD;
646 
647 	bsize = vp->v_mount->mnt_stat.f_iosize;
648 
649 	/* get the UNDERLYING device for the file with VOP_BMAP() */
650 
651 	/*
652 	 * originally, we did not check for an error return value -- assuming
653 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
654 	 */
655 	foff = IDX_TO_OFF(m[reqpage]->pindex);
656 
657 	/*
658 	 * if we can't bmap, use old VOP code
659 	 */
660 	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
661 		VM_OBJECT_LOCK(object);
662 		vm_page_lock_queues();
663 		for (i = 0; i < count; i++)
664 			if (i != reqpage)
665 				vm_page_free(m[i]);
666 		vm_page_unlock_queues();
667 		VM_OBJECT_UNLOCK(object);
668 		cnt.v_vnodein++;
669 		cnt.v_vnodepgsin++;
670 		return vnode_pager_input_old(object, m[reqpage]);
671 
672 		/*
673 		 * if the blocksize is smaller than a page size, then use
674 		 * special small filesystem code.  NFS sometimes has a small
675 		 * blocksize, but it can handle large reads itself.
676 		 */
677 	} else if ((PAGE_SIZE / bsize) > 1 &&
678 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
679 		VM_OBJECT_LOCK(object);
680 		vm_page_lock_queues();
681 		for (i = 0; i < count; i++)
682 			if (i != reqpage)
683 				vm_page_free(m[i]);
684 		vm_page_unlock_queues();
685 		VM_OBJECT_UNLOCK(object);
686 		cnt.v_vnodein++;
687 		cnt.v_vnodepgsin++;
688 		return vnode_pager_input_smlfs(object, m[reqpage]);
689 	}
690 
691 	/*
692 	 * If we have a completely valid page available to us, we can
693 	 * clean up and return.  Otherwise we have to re-read the
694 	 * media.
695 	 */
696 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
697 		VM_OBJECT_LOCK(object);
698 		vm_page_lock_queues();
699 		for (i = 0; i < count; i++)
700 			if (i != reqpage)
701 				vm_page_free(m[i]);
702 		vm_page_unlock_queues();
703 		VM_OBJECT_UNLOCK(object);
704 		return VM_PAGER_OK;
705 	}
706 	m[reqpage]->valid = 0;
707 
708 	/*
709 	 * here on direct device I/O
710 	 */
711 	firstaddr = -1;
712 
713 	/*
714 	 * calculate the run that includes the required page
715 	 */
716 	for (first = 0, i = 0; i < count; i = runend) {
717 		firstaddr = vnode_pager_addr(vp,
718 			IDX_TO_OFF(m[i]->pindex), &runpg);
719 		if (firstaddr == -1) {
720 			VM_OBJECT_LOCK(object);
721 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
722 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
723 				    firstaddr, (uintmax_t)(foff >> 32),
724 				    (uintmax_t)foff,
725 				    (uintmax_t)
726 				    (object->un_pager.vnp.vnp_size >> 32),
727 				    (uintmax_t)object->un_pager.vnp.vnp_size);
728 			}
729 			vm_page_lock_queues();
730 			vm_page_free(m[i]);
731 			vm_page_unlock_queues();
732 			VM_OBJECT_UNLOCK(object);
733 			runend = i + 1;
734 			first = runend;
735 			continue;
736 		}
737 		runend = i + runpg;
738 		if (runend <= reqpage) {
739 			VM_OBJECT_LOCK(object);
740 			vm_page_lock_queues();
741 			for (j = i; j < runend; j++)
742 				vm_page_free(m[j]);
743 			vm_page_unlock_queues();
744 			VM_OBJECT_UNLOCK(object);
745 		} else {
746 			if (runpg < (count - first)) {
747 				VM_OBJECT_LOCK(object);
748 				vm_page_lock_queues();
749 				for (i = first + runpg; i < count; i++)
750 					vm_page_free(m[i]);
751 				vm_page_unlock_queues();
752 				VM_OBJECT_UNLOCK(object);
753 				count = first + runpg;
754 			}
755 			break;
756 		}
757 		first = runend;
758 	}
759 
760 	/*
761 	 * the first and last page have been calculated now, move input pages
762 	 * to be zero based...
763 	 */
764 	if (first != 0) {
765 		for (i = first; i < count; i++) {
766 			m[i - first] = m[i];
767 		}
768 		count -= first;
769 		reqpage -= first;
770 	}
771 
772 	/*
773 	 * calculate the file virtual address for the transfer
774 	 */
775 	foff = IDX_TO_OFF(m[0]->pindex);
776 
777 	/*
778 	 * calculate the size of the transfer
779 	 */
780 	size = count * PAGE_SIZE;
781 	if ((foff + size) > object->un_pager.vnp.vnp_size)
782 		size = object->un_pager.vnp.vnp_size - foff;
783 
784 	/*
785 	 * round up physical size for real devices.
786 	 */
787 	if (dp->v_type == VBLK || dp->v_type == VCHR) {
788 		int secmask = dp->v_rdev->si_bsize_phys - 1;
789 		KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
790 		size = (size + secmask) & ~secmask;
791 	}
792 
793 	bp = getpbuf(&vnode_pbuf_freecnt);
794 	kva = (vm_offset_t) bp->b_data;
795 
796 	/*
797 	 * and map the pages to be read into the kva
798 	 */
799 	pmap_qenter(kva, m, count);
800 
801 	/* build a minimal buffer header */
802 	bp->b_iocmd = BIO_READ;
803 	bp->b_iodone = bdone;
804 	/* B_PHYS is not set, but it is nice to fill this in */
805 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
806 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
807 	bp->b_rcred = crhold(curthread->td_ucred);
808 	bp->b_wcred = crhold(curthread->td_ucred);
809 	bp->b_blkno = firstaddr;
810 	pbgetvp(dp, bp);
811 	bp->b_bcount = size;
812 	bp->b_bufsize = size;
813 	bp->b_runningbufspace = bp->b_bufsize;
814 	runningbufspace += bp->b_runningbufspace;
815 
816 	cnt.v_vnodein++;
817 	cnt.v_vnodepgsin += count;
818 
819 	/* do the input */
820 	if (dp->v_type == VCHR)
821 		VOP_SPECSTRATEGY(bp->b_vp, bp);
822 	else
823 		VOP_STRATEGY(bp->b_vp, bp);
824 
825 	bwait(bp, PVM, "vnread");
826 
827 	if ((bp->b_ioflags & BIO_ERROR) != 0)
828 		error = EIO;
829 
830 	if (!error) {
831 		if (size != count * PAGE_SIZE)
832 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
833 	}
834 	pmap_qremove(kva, count);
835 
836 	/*
837 	 * free the buffer header back to the swap buffer pool
838 	 */
839 	relpbuf(bp, &vnode_pbuf_freecnt);
840 
841 	VM_OBJECT_LOCK(object);
842 	vm_page_lock_queues();
843 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
844 		vm_page_t mt;
845 
846 		nextoff = tfoff + PAGE_SIZE;
847 		mt = m[i];
848 
849 		if (nextoff <= object->un_pager.vnp.vnp_size) {
850 			/*
851 			 * Read filled up entire page.
852 			 */
853 			mt->valid = VM_PAGE_BITS_ALL;
854 			vm_page_undirty(mt);	/* should be an assert? XXX */
855 			pmap_clear_modify(mt);
856 		} else {
857 			/*
858 			 * Read did not fill up entire page.  Since this
859 			 * is getpages, the page may be mapped, so we have
860 			 * to zero the invalid portions of the page even
861 			 * though we aren't setting them valid.
862 			 *
863 			 * Currently we do not set the entire page valid,
864 			 * we just try to clear the piece that we couldn't
865 			 * read.
866 			 */
867 			vm_page_set_validclean(mt, 0,
868 			    object->un_pager.vnp.vnp_size - tfoff);
869 			/* handled by vm_fault now */
870 			/* vm_page_zero_invalid(mt, FALSE); */
871 		}
872 
873 		vm_page_flag_clear(mt, PG_ZERO);
874 		if (i != reqpage) {
875 
876 			/*
877 			 * whether or not to leave the page activated is up in
878 			 * the air, but we should put the page on a page queue
879 			 * somewhere. (it already is in the object). Result:
880 			 * It appears that empirical results show that
881 			 * deactivating pages is best.
882 			 */
883 
884 			/*
885 			 * just in case someone was asking for this page we
886 			 * now tell them that it is ok to use
887 			 */
888 			if (!error) {
889 				if (mt->flags & PG_WANTED)
890 					vm_page_activate(mt);
891 				else
892 					vm_page_deactivate(mt);
893 				vm_page_wakeup(mt);
894 			} else {
895 				vm_page_free(mt);
896 			}
897 		}
898 	}
899 	vm_page_unlock_queues();
900 	VM_OBJECT_UNLOCK(object);
901 	if (error) {
902 		printf("vnode_pager_getpages: I/O read error\n");
903 	}
904 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
905 }
906 
907 /*
908  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
909  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
910  * vnode_pager_generic_putpages() to implement the previous behaviour.
911  *
912  * All other FS's should use the bypass to get to the local media
913  * backing vp's VOP_PUTPAGES.
914  */
915 static void
916 vnode_pager_putpages(object, m, count, sync, rtvals)
917 	vm_object_t object;
918 	vm_page_t *m;
919 	int count;
920 	boolean_t sync;
921 	int *rtvals;
922 {
923 	int rtval;
924 	struct vnode *vp;
925 	struct mount *mp;
926 	int bytes = count * PAGE_SIZE;
927 
928 	GIANT_REQUIRED;
929 	/*
930 	 * Force synchronous operation if we are extremely low on memory
931 	 * to prevent a low-memory deadlock.  VOP operations often need to
932 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
933 	 * operation ).  The swapper handles the case by limiting the amount
934 	 * of asynchronous I/O, but that sort of solution doesn't scale well
935 	 * for the vnode pager without a lot of work.
936 	 *
937 	 * Also, the backing vnode's iodone routine may not wake the pageout
938 	 * daemon up.  This should be probably be addressed XXX.
939 	 */
940 
941 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
942 		sync |= OBJPC_SYNC;
943 
944 	/*
945 	 * Call device-specific putpages function
946 	 */
947 	vp = object->handle;
948 	if (vp->v_type != VREG)
949 		mp = NULL;
950 	(void)vn_start_write(vp, &mp, V_WAIT);
951 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
952 	KASSERT(rtval != EOPNOTSUPP,
953 	    ("vnode_pager: stale FS putpages\n"));
954 	vn_finished_write(mp);
955 }
956 
957 
958 /*
959  * This is now called from local media FS's to operate against their
960  * own vnodes if they fail to implement VOP_PUTPAGES.
961  *
962  * This is typically called indirectly via the pageout daemon and
963  * clustering has already typically occured, so in general we ask the
964  * underlying filesystem to write the data out asynchronously rather
965  * then delayed.
966  */
967 int
968 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
969 	struct vnode *vp;
970 	vm_page_t *m;
971 	int bytecount;
972 	int flags;
973 	int *rtvals;
974 {
975 	int i;
976 	vm_object_t object;
977 	int count;
978 
979 	int maxsize, ncount;
980 	vm_ooffset_t poffset;
981 	struct uio auio;
982 	struct iovec aiov;
983 	int error;
984 	int ioflags;
985 
986 	GIANT_REQUIRED;
987 	object = vp->v_object;
988 	count = bytecount / PAGE_SIZE;
989 
990 	for (i = 0; i < count; i++)
991 		rtvals[i] = VM_PAGER_AGAIN;
992 
993 	if ((int) m[0]->pindex < 0) {
994 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
995 			(long)m[0]->pindex, (u_long)m[0]->dirty);
996 		rtvals[0] = VM_PAGER_BAD;
997 		return VM_PAGER_BAD;
998 	}
999 
1000 	maxsize = count * PAGE_SIZE;
1001 	ncount = count;
1002 
1003 	poffset = IDX_TO_OFF(m[0]->pindex);
1004 
1005 	/*
1006 	 * If the page-aligned write is larger then the actual file we
1007 	 * have to invalidate pages occuring beyond the file EOF.  However,
1008 	 * there is an edge case where a file may not be page-aligned where
1009 	 * the last page is partially invalid.  In this case the filesystem
1010 	 * may not properly clear the dirty bits for the entire page (which
1011 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1012 	 * With the page locked we are free to fix-up the dirty bits here.
1013 	 *
1014 	 * We do not under any circumstances truncate the valid bits, as
1015 	 * this will screw up bogus page replacement.
1016 	 */
1017 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1018 		if (object->un_pager.vnp.vnp_size > poffset) {
1019 			int pgoff;
1020 
1021 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1022 			ncount = btoc(maxsize);
1023 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1024 				vm_page_lock_queues();
1025 				vm_page_clear_dirty(m[ncount - 1], pgoff,
1026 					PAGE_SIZE - pgoff);
1027 				vm_page_unlock_queues();
1028 			}
1029 		} else {
1030 			maxsize = 0;
1031 			ncount = 0;
1032 		}
1033 		if (ncount < count) {
1034 			for (i = ncount; i < count; i++) {
1035 				rtvals[i] = VM_PAGER_BAD;
1036 			}
1037 		}
1038 	}
1039 
1040 	/*
1041 	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
1042 	 * rather then a bdwrite() to prevent paging I/O from saturating
1043 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
1044 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
1045 	 * the system decides how to cluster.
1046 	 */
1047 	ioflags = IO_VMIO;
1048 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1049 		ioflags |= IO_SYNC;
1050 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1051 		ioflags |= IO_ASYNC;
1052 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1053 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1054 
1055 	aiov.iov_base = (caddr_t) 0;
1056 	aiov.iov_len = maxsize;
1057 	auio.uio_iov = &aiov;
1058 	auio.uio_iovcnt = 1;
1059 	auio.uio_offset = poffset;
1060 	auio.uio_segflg = UIO_NOCOPY;
1061 	auio.uio_rw = UIO_WRITE;
1062 	auio.uio_resid = maxsize;
1063 	auio.uio_td = (struct thread *) 0;
1064 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1065 	cnt.v_vnodeout++;
1066 	cnt.v_vnodepgsout += ncount;
1067 
1068 	if (error) {
1069 		printf("vnode_pager_putpages: I/O error %d\n", error);
1070 	}
1071 	if (auio.uio_resid) {
1072 		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1073 		    auio.uio_resid, (u_long)m[0]->pindex);
1074 	}
1075 	for (i = 0; i < ncount; i++) {
1076 		rtvals[i] = VM_PAGER_OK;
1077 	}
1078 	return rtvals[0];
1079 }
1080 
1081 struct vnode *
1082 vnode_pager_lock(vm_object_t first_object)
1083 {
1084 	struct vnode *vp;
1085 	vm_object_t backing_object, object;
1086 
1087 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
1088 	for (object = first_object; object != NULL; object = backing_object) {
1089 		if (object->type != OBJT_VNODE) {
1090 			if ((backing_object = object->backing_object) != NULL)
1091 				VM_OBJECT_LOCK(backing_object);
1092 			if (object != first_object)
1093 				VM_OBJECT_UNLOCK(object);
1094 			continue;
1095 		}
1096 	retry:
1097 		if (object->flags & OBJ_DEAD) {
1098 			if (object != first_object)
1099 				VM_OBJECT_UNLOCK(object);
1100 			return NULL;
1101 		}
1102 		vp = object->handle;
1103 		VI_LOCK(vp);
1104 		VM_OBJECT_UNLOCK(object);
1105 		if (first_object != object)
1106 			VM_OBJECT_UNLOCK(first_object);
1107 		if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | LK_NOPAUSE |
1108 		    LK_RETRY | LK_SHARED, curthread)) {
1109 			VM_OBJECT_LOCK(first_object);
1110 			if (object != first_object)
1111 				VM_OBJECT_LOCK(object);
1112 			if (object->type != OBJT_VNODE) {
1113 				if (object != first_object)
1114 					VM_OBJECT_UNLOCK(object);
1115 				return NULL;
1116 			}
1117 			printf("vnode_pager_lock: retrying\n");
1118 			goto retry;
1119 		}
1120 		VM_OBJECT_LOCK(first_object);
1121 		return (vp);
1122 	}
1123 	return NULL;
1124 }
1125