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