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