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