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