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