xref: /freebsd/sys/vm/vnode_pager.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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 			 * Update the valid bits to reflect the blocks that
407 			 * have been zeroed.  Some of these valid bits may
408 			 * have already been set.
409 			 */
410 			vm_page_set_valid(m, base, size);
411 
412 			/*
413 			 * Round "base" to the next block boundary so that the
414 			 * dirty bit for a partially zeroed block is not
415 			 * cleared.
416 			 */
417 			base = roundup2(base, DEV_BSIZE);
418 
419 			/*
420 			 * Clear out partial-page dirty bits.
421 			 *
422 			 * note that we do not clear out the valid
423 			 * bits.  This would prevent bogus_page
424 			 * replacement from working properly.
425 			 */
426 			vm_page_lock_queues();
427 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
428 			vm_page_unlock_queues();
429 		} else if ((nsize & PAGE_MASK) &&
430 		    __predict_false(object->cache != NULL)) {
431 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
432 			    nobjsize);
433 		}
434 	}
435 	object->un_pager.vnp.vnp_size = nsize;
436 	object->size = nobjsize;
437 	VM_OBJECT_UNLOCK(object);
438 }
439 
440 /*
441  * calculate the linear (byte) disk address of specified virtual
442  * file address
443  */
444 static int
445 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
446     int *run)
447 {
448 	int bsize;
449 	int err;
450 	daddr_t vblock;
451 	daddr_t voffset;
452 
453 	if (address < 0)
454 		return -1;
455 
456 	if (vp->v_iflag & VI_DOOMED)
457 		return -1;
458 
459 	bsize = vp->v_mount->mnt_stat.f_iosize;
460 	vblock = address / bsize;
461 	voffset = address % bsize;
462 
463 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
464 	if (err == 0) {
465 		if (*rtaddress != -1)
466 			*rtaddress += voffset / DEV_BSIZE;
467 		if (run) {
468 			*run += 1;
469 			*run *= bsize/PAGE_SIZE;
470 			*run -= voffset/PAGE_SIZE;
471 		}
472 	}
473 
474 	return (err);
475 }
476 
477 /*
478  * small block filesystem vnode pager input
479  */
480 static int
481 vnode_pager_input_smlfs(object, m)
482 	vm_object_t object;
483 	vm_page_t m;
484 {
485 	int bits, i;
486 	struct vnode *vp;
487 	struct bufobj *bo;
488 	struct buf *bp;
489 	struct sf_buf *sf;
490 	daddr_t fileaddr;
491 	vm_offset_t bsize;
492 	int error = 0;
493 
494 	vp = object->handle;
495 	if (vp->v_iflag & VI_DOOMED)
496 		return VM_PAGER_BAD;
497 
498 	bsize = vp->v_mount->mnt_stat.f_iosize;
499 
500 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
501 
502 	sf = sf_buf_alloc(m, 0);
503 
504 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
505 		vm_ooffset_t address;
506 
507 		bits = vm_page_bits(i * bsize, bsize);
508 		if (m->valid & bits)
509 			continue;
510 
511 		address = IDX_TO_OFF(m->pindex) + i * bsize;
512 		if (address >= object->un_pager.vnp.vnp_size) {
513 			fileaddr = -1;
514 		} else {
515 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
516 			if (error)
517 				break;
518 		}
519 		if (fileaddr != -1) {
520 			bp = getpbuf(&vnode_pbuf_freecnt);
521 
522 			/* build a minimal buffer header */
523 			bp->b_iocmd = BIO_READ;
524 			bp->b_iodone = bdone;
525 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
526 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
527 			bp->b_rcred = crhold(curthread->td_ucred);
528 			bp->b_wcred = crhold(curthread->td_ucred);
529 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
530 			bp->b_blkno = fileaddr;
531 			pbgetbo(bo, bp);
532 			bp->b_bcount = bsize;
533 			bp->b_bufsize = bsize;
534 			bp->b_runningbufspace = bp->b_bufsize;
535 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
536 
537 			/* do the input */
538 			bp->b_iooffset = dbtob(bp->b_blkno);
539 			bstrategy(bp);
540 
541 			bwait(bp, PVM, "vnsrd");
542 
543 			if ((bp->b_ioflags & BIO_ERROR) != 0)
544 				error = EIO;
545 
546 			/*
547 			 * free the buffer header back to the swap buffer pool
548 			 */
549 			pbrelbo(bp);
550 			relpbuf(bp, &vnode_pbuf_freecnt);
551 			if (error)
552 				break;
553 		} else
554 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
555 		KASSERT((m->dirty & bits) == 0,
556 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
557 		VM_OBJECT_LOCK(object);
558 		m->valid |= bits;
559 		VM_OBJECT_UNLOCK(object);
560 	}
561 	sf_buf_free(sf);
562 	if (error) {
563 		return VM_PAGER_ERROR;
564 	}
565 	return VM_PAGER_OK;
566 }
567 
568 /*
569  * old style vnode pager input routine
570  */
571 static int
572 vnode_pager_input_old(object, m)
573 	vm_object_t object;
574 	vm_page_t m;
575 {
576 	struct uio auio;
577 	struct iovec aiov;
578 	int error;
579 	int size;
580 	struct sf_buf *sf;
581 	struct vnode *vp;
582 
583 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
584 	error = 0;
585 
586 	/*
587 	 * Return failure if beyond current EOF
588 	 */
589 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
590 		return VM_PAGER_BAD;
591 	} else {
592 		size = PAGE_SIZE;
593 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
594 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
595 		vp = object->handle;
596 		VM_OBJECT_UNLOCK(object);
597 
598 		/*
599 		 * Allocate a kernel virtual address and initialize so that
600 		 * we can use VOP_READ/WRITE routines.
601 		 */
602 		sf = sf_buf_alloc(m, 0);
603 
604 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
605 		aiov.iov_len = size;
606 		auio.uio_iov = &aiov;
607 		auio.uio_iovcnt = 1;
608 		auio.uio_offset = IDX_TO_OFF(m->pindex);
609 		auio.uio_segflg = UIO_SYSSPACE;
610 		auio.uio_rw = UIO_READ;
611 		auio.uio_resid = size;
612 		auio.uio_td = curthread;
613 
614 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
615 		if (!error) {
616 			int count = size - auio.uio_resid;
617 
618 			if (count == 0)
619 				error = EINVAL;
620 			else if (count != PAGE_SIZE)
621 				bzero((caddr_t)sf_buf_kva(sf) + count,
622 				    PAGE_SIZE - count);
623 		}
624 		sf_buf_free(sf);
625 
626 		VM_OBJECT_LOCK(object);
627 	}
628 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
629 	if (!error)
630 		m->valid = VM_PAGE_BITS_ALL;
631 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
632 }
633 
634 /*
635  * generic vnode pager input routine
636  */
637 
638 /*
639  * Local media VFS's that do not implement their own VOP_GETPAGES
640  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
641  * to implement the previous behaviour.
642  *
643  * All other FS's should use the bypass to get to the local media
644  * backing vp's VOP_GETPAGES.
645  */
646 static int
647 vnode_pager_getpages(object, m, count, reqpage)
648 	vm_object_t object;
649 	vm_page_t *m;
650 	int count;
651 	int reqpage;
652 {
653 	int rtval;
654 	struct vnode *vp;
655 	int bytes = count * PAGE_SIZE;
656 	int vfslocked;
657 
658 	vp = object->handle;
659 	VM_OBJECT_UNLOCK(object);
660 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
661 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
662 	KASSERT(rtval != EOPNOTSUPP,
663 	    ("vnode_pager: FS getpages not implemented\n"));
664 	VFS_UNLOCK_GIANT(vfslocked);
665 	VM_OBJECT_LOCK(object);
666 	return rtval;
667 }
668 
669 /*
670  * This is now called from local media FS's to operate against their
671  * own vnodes if they fail to implement VOP_GETPAGES.
672  */
673 int
674 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
675 	struct vnode *vp;
676 	vm_page_t *m;
677 	int bytecount;
678 	int reqpage;
679 {
680 	vm_object_t object;
681 	vm_offset_t kva;
682 	off_t foff, tfoff, nextoff;
683 	int i, j, size, bsize, first;
684 	daddr_t firstaddr, reqblock;
685 	struct bufobj *bo;
686 	int runpg;
687 	int runend;
688 	struct buf *bp;
689 	int count;
690 	int error;
691 
692 	object = vp->v_object;
693 	count = bytecount / PAGE_SIZE;
694 
695 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
696 	    ("vnode_pager_generic_getpages does not support devices"));
697 	if (vp->v_iflag & VI_DOOMED)
698 		return VM_PAGER_BAD;
699 
700 	bsize = vp->v_mount->mnt_stat.f_iosize;
701 
702 	/* get the UNDERLYING device for the file with VOP_BMAP() */
703 
704 	/*
705 	 * originally, we did not check for an error return value -- assuming
706 	 * an fs always has a bmap entry point -- that assumption is wrong!!!
707 	 */
708 	foff = IDX_TO_OFF(m[reqpage]->pindex);
709 
710 	/*
711 	 * if we can't bmap, use old VOP code
712 	 */
713 	error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
714 	if (error == EOPNOTSUPP) {
715 		VM_OBJECT_LOCK(object);
716 		vm_page_lock_queues();
717 		for (i = 0; i < count; i++)
718 			if (i != reqpage)
719 				vm_page_free(m[i]);
720 		vm_page_unlock_queues();
721 		PCPU_INC(cnt.v_vnodein);
722 		PCPU_INC(cnt.v_vnodepgsin);
723 		error = vnode_pager_input_old(object, m[reqpage]);
724 		VM_OBJECT_UNLOCK(object);
725 		return (error);
726 	} else if (error != 0) {
727 		VM_OBJECT_LOCK(object);
728 		vm_page_lock_queues();
729 		for (i = 0; i < count; i++)
730 			if (i != reqpage)
731 				vm_page_free(m[i]);
732 		vm_page_unlock_queues();
733 		VM_OBJECT_UNLOCK(object);
734 		return (VM_PAGER_ERROR);
735 
736 		/*
737 		 * if the blocksize is smaller than a page size, then use
738 		 * special small filesystem code.  NFS sometimes has a small
739 		 * blocksize, but it can handle large reads itself.
740 		 */
741 	} else if ((PAGE_SIZE / bsize) > 1 &&
742 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
743 		VM_OBJECT_LOCK(object);
744 		vm_page_lock_queues();
745 		for (i = 0; i < count; i++)
746 			if (i != reqpage)
747 				vm_page_free(m[i]);
748 		vm_page_unlock_queues();
749 		VM_OBJECT_UNLOCK(object);
750 		PCPU_INC(cnt.v_vnodein);
751 		PCPU_INC(cnt.v_vnodepgsin);
752 		return vnode_pager_input_smlfs(object, m[reqpage]);
753 	}
754 
755 	/*
756 	 * If we have a completely valid page available to us, we can
757 	 * clean up and return.  Otherwise we have to re-read the
758 	 * media.
759 	 */
760 	VM_OBJECT_LOCK(object);
761 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
762 		vm_page_lock_queues();
763 		for (i = 0; i < count; i++)
764 			if (i != reqpage)
765 				vm_page_free(m[i]);
766 		vm_page_unlock_queues();
767 		VM_OBJECT_UNLOCK(object);
768 		return VM_PAGER_OK;
769 	} else if (reqblock == -1) {
770 		pmap_zero_page(m[reqpage]);
771 		KASSERT(m[reqpage]->dirty == 0,
772 		    ("vnode_pager_generic_getpages: page %p is dirty", m));
773 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
774 		vm_page_lock_queues();
775 		for (i = 0; i < count; i++)
776 			if (i != reqpage)
777 				vm_page_free(m[i]);
778 		vm_page_unlock_queues();
779 		VM_OBJECT_UNLOCK(object);
780 		return (VM_PAGER_OK);
781 	}
782 	m[reqpage]->valid = 0;
783 	VM_OBJECT_UNLOCK(object);
784 
785 	/*
786 	 * here on direct device I/O
787 	 */
788 	firstaddr = -1;
789 
790 	/*
791 	 * calculate the run that includes the required page
792 	 */
793 	for (first = 0, i = 0; i < count; i = runend) {
794 		if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
795 		    &runpg) != 0) {
796 			VM_OBJECT_LOCK(object);
797 			vm_page_lock_queues();
798 			for (; i < count; i++)
799 				if (i != reqpage)
800 					vm_page_free(m[i]);
801 			vm_page_unlock_queues();
802 			VM_OBJECT_UNLOCK(object);
803 			return (VM_PAGER_ERROR);
804 		}
805 		if (firstaddr == -1) {
806 			VM_OBJECT_LOCK(object);
807 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
808 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
809 				    (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
810 				    (uintmax_t)foff,
811 				    (uintmax_t)
812 				    (object->un_pager.vnp.vnp_size >> 32),
813 				    (uintmax_t)object->un_pager.vnp.vnp_size);
814 			}
815 			vm_page_lock_queues();
816 			vm_page_free(m[i]);
817 			vm_page_unlock_queues();
818 			VM_OBJECT_UNLOCK(object);
819 			runend = i + 1;
820 			first = runend;
821 			continue;
822 		}
823 		runend = i + runpg;
824 		if (runend <= reqpage) {
825 			VM_OBJECT_LOCK(object);
826 			vm_page_lock_queues();
827 			for (j = i; j < runend; j++)
828 				vm_page_free(m[j]);
829 			vm_page_unlock_queues();
830 			VM_OBJECT_UNLOCK(object);
831 		} else {
832 			if (runpg < (count - first)) {
833 				VM_OBJECT_LOCK(object);
834 				vm_page_lock_queues();
835 				for (i = first + runpg; i < count; i++)
836 					vm_page_free(m[i]);
837 				vm_page_unlock_queues();
838 				VM_OBJECT_UNLOCK(object);
839 				count = first + runpg;
840 			}
841 			break;
842 		}
843 		first = runend;
844 	}
845 
846 	/*
847 	 * the first and last page have been calculated now, move input pages
848 	 * to be zero based...
849 	 */
850 	if (first != 0) {
851 		m += first;
852 		count -= first;
853 		reqpage -= first;
854 	}
855 
856 	/*
857 	 * calculate the file virtual address for the transfer
858 	 */
859 	foff = IDX_TO_OFF(m[0]->pindex);
860 
861 	/*
862 	 * calculate the size of the transfer
863 	 */
864 	size = count * PAGE_SIZE;
865 	KASSERT(count > 0, ("zero count"));
866 	if ((foff + size) > object->un_pager.vnp.vnp_size)
867 		size = object->un_pager.vnp.vnp_size - foff;
868 	KASSERT(size > 0, ("zero size"));
869 
870 	/*
871 	 * round up physical size for real devices.
872 	 */
873 	if (1) {
874 		int secmask = bo->bo_bsize - 1;
875 		KASSERT(secmask < PAGE_SIZE && secmask > 0,
876 		    ("vnode_pager_generic_getpages: sector size %d too large",
877 		    secmask + 1));
878 		size = (size + secmask) & ~secmask;
879 	}
880 
881 	bp = getpbuf(&vnode_pbuf_freecnt);
882 	kva = (vm_offset_t) bp->b_data;
883 
884 	/*
885 	 * and map the pages to be read into the kva
886 	 */
887 	pmap_qenter(kva, m, count);
888 
889 	/* build a minimal buffer header */
890 	bp->b_iocmd = BIO_READ;
891 	bp->b_iodone = bdone;
892 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
893 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
894 	bp->b_rcred = crhold(curthread->td_ucred);
895 	bp->b_wcred = crhold(curthread->td_ucred);
896 	bp->b_blkno = firstaddr;
897 	pbgetbo(bo, bp);
898 	bp->b_bcount = size;
899 	bp->b_bufsize = size;
900 	bp->b_runningbufspace = bp->b_bufsize;
901 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
902 
903 	PCPU_INC(cnt.v_vnodein);
904 	PCPU_ADD(cnt.v_vnodepgsin, count);
905 
906 	/* do the input */
907 	bp->b_iooffset = dbtob(bp->b_blkno);
908 	bstrategy(bp);
909 
910 	bwait(bp, PVM, "vnread");
911 
912 	if ((bp->b_ioflags & BIO_ERROR) != 0)
913 		error = EIO;
914 
915 	if (!error) {
916 		if (size != count * PAGE_SIZE)
917 			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
918 	}
919 	pmap_qremove(kva, count);
920 
921 	/*
922 	 * free the buffer header back to the swap buffer pool
923 	 */
924 	pbrelbo(bp);
925 	relpbuf(bp, &vnode_pbuf_freecnt);
926 
927 	VM_OBJECT_LOCK(object);
928 	vm_page_lock_queues();
929 	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
930 		vm_page_t mt;
931 
932 		nextoff = tfoff + PAGE_SIZE;
933 		mt = m[i];
934 
935 		if (nextoff <= object->un_pager.vnp.vnp_size) {
936 			/*
937 			 * Read filled up entire page.
938 			 */
939 			mt->valid = VM_PAGE_BITS_ALL;
940 			KASSERT(mt->dirty == 0,
941 			    ("vnode_pager_generic_getpages: page %p is dirty",
942 			    mt));
943 			KASSERT(!pmap_page_is_mapped(mt),
944 			    ("vnode_pager_generic_getpages: page %p is mapped",
945 			    mt));
946 		} else {
947 			/*
948 			 * Read did not fill up entire page.
949 			 *
950 			 * Currently we do not set the entire page valid,
951 			 * we just try to clear the piece that we couldn't
952 			 * read.
953 			 */
954 			vm_page_set_valid(mt, 0,
955 			    object->un_pager.vnp.vnp_size - tfoff);
956 			KASSERT((mt->dirty & vm_page_bits(0,
957 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
958 			    ("vnode_pager_generic_getpages: page %p is dirty",
959 			    mt));
960 		}
961 
962 		if (i != reqpage) {
963 
964 			/*
965 			 * whether or not to leave the page activated is up in
966 			 * the air, but we should put the page on a page queue
967 			 * somewhere. (it already is in the object). Result:
968 			 * It appears that empirical results show that
969 			 * deactivating pages is best.
970 			 */
971 
972 			/*
973 			 * just in case someone was asking for this page we
974 			 * now tell them that it is ok to use
975 			 */
976 			if (!error) {
977 				if (mt->oflags & VPO_WANTED)
978 					vm_page_activate(mt);
979 				else
980 					vm_page_deactivate(mt);
981 				vm_page_wakeup(mt);
982 			} else {
983 				vm_page_free(mt);
984 			}
985 		}
986 	}
987 	vm_page_unlock_queues();
988 	VM_OBJECT_UNLOCK(object);
989 	if (error) {
990 		printf("vnode_pager_getpages: I/O read error\n");
991 	}
992 	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
993 }
994 
995 /*
996  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
997  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
998  * vnode_pager_generic_putpages() to implement the previous behaviour.
999  *
1000  * All other FS's should use the bypass to get to the local media
1001  * backing vp's VOP_PUTPAGES.
1002  */
1003 static void
1004 vnode_pager_putpages(object, m, count, sync, rtvals)
1005 	vm_object_t object;
1006 	vm_page_t *m;
1007 	int count;
1008 	boolean_t sync;
1009 	int *rtvals;
1010 {
1011 	int rtval;
1012 	struct vnode *vp;
1013 	struct mount *mp;
1014 	int bytes = count * PAGE_SIZE;
1015 
1016 	/*
1017 	 * Force synchronous operation if we are extremely low on memory
1018 	 * to prevent a low-memory deadlock.  VOP operations often need to
1019 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1020 	 * operation ).  The swapper handles the case by limiting the amount
1021 	 * of asynchronous I/O, but that sort of solution doesn't scale well
1022 	 * for the vnode pager without a lot of work.
1023 	 *
1024 	 * Also, the backing vnode's iodone routine may not wake the pageout
1025 	 * daemon up.  This should be probably be addressed XXX.
1026 	 */
1027 
1028 	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
1029 		sync |= OBJPC_SYNC;
1030 
1031 	/*
1032 	 * Call device-specific putpages function
1033 	 */
1034 	vp = object->handle;
1035 	VM_OBJECT_UNLOCK(object);
1036 	if (vp->v_type != VREG)
1037 		mp = NULL;
1038 	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
1039 	KASSERT(rtval != EOPNOTSUPP,
1040 	    ("vnode_pager: stale FS putpages\n"));
1041 	VM_OBJECT_LOCK(object);
1042 }
1043 
1044 
1045 /*
1046  * This is now called from local media FS's to operate against their
1047  * own vnodes if they fail to implement VOP_PUTPAGES.
1048  *
1049  * This is typically called indirectly via the pageout daemon and
1050  * clustering has already typically occured, so in general we ask the
1051  * underlying filesystem to write the data out asynchronously rather
1052  * then delayed.
1053  */
1054 int
1055 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
1056 	struct vnode *vp;
1057 	vm_page_t *m;
1058 	int bytecount;
1059 	int flags;
1060 	int *rtvals;
1061 {
1062 	int i;
1063 	vm_object_t object;
1064 	int count;
1065 
1066 	int maxsize, ncount;
1067 	vm_ooffset_t poffset;
1068 	struct uio auio;
1069 	struct iovec aiov;
1070 	int error;
1071 	int ioflags;
1072 	int ppscheck = 0;
1073 	static struct timeval lastfail;
1074 	static int curfail;
1075 
1076 	object = vp->v_object;
1077 	count = bytecount / PAGE_SIZE;
1078 
1079 	for (i = 0; i < count; i++)
1080 		rtvals[i] = VM_PAGER_AGAIN;
1081 
1082 	if ((int64_t)m[0]->pindex < 0) {
1083 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1084 			(long)m[0]->pindex, (u_long)m[0]->dirty);
1085 		rtvals[0] = VM_PAGER_BAD;
1086 		return VM_PAGER_BAD;
1087 	}
1088 
1089 	maxsize = count * PAGE_SIZE;
1090 	ncount = count;
1091 
1092 	poffset = IDX_TO_OFF(m[0]->pindex);
1093 
1094 	/*
1095 	 * If the page-aligned write is larger then the actual file we
1096 	 * have to invalidate pages occuring beyond the file EOF.  However,
1097 	 * there is an edge case where a file may not be page-aligned where
1098 	 * the last page is partially invalid.  In this case the filesystem
1099 	 * may not properly clear the dirty bits for the entire page (which
1100 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1101 	 * With the page locked we are free to fix-up the dirty bits here.
1102 	 *
1103 	 * We do not under any circumstances truncate the valid bits, as
1104 	 * this will screw up bogus page replacement.
1105 	 */
1106 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1107 		if (object->un_pager.vnp.vnp_size > poffset) {
1108 			int pgoff;
1109 
1110 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1111 			ncount = btoc(maxsize);
1112 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1113 				vm_page_lock_queues();
1114 				vm_page_clear_dirty(m[ncount - 1], pgoff,
1115 					PAGE_SIZE - pgoff);
1116 				vm_page_unlock_queues();
1117 			}
1118 		} else {
1119 			maxsize = 0;
1120 			ncount = 0;
1121 		}
1122 		if (ncount < count) {
1123 			for (i = ncount; i < count; i++) {
1124 				rtvals[i] = VM_PAGER_BAD;
1125 			}
1126 		}
1127 	}
1128 
1129 	/*
1130 	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
1131 	 * rather then a bdwrite() to prevent paging I/O from saturating
1132 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
1133 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
1134 	 * the system decides how to cluster.
1135 	 */
1136 	ioflags = IO_VMIO;
1137 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1138 		ioflags |= IO_SYNC;
1139 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1140 		ioflags |= IO_ASYNC;
1141 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1142 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1143 
1144 	aiov.iov_base = (caddr_t) 0;
1145 	aiov.iov_len = maxsize;
1146 	auio.uio_iov = &aiov;
1147 	auio.uio_iovcnt = 1;
1148 	auio.uio_offset = poffset;
1149 	auio.uio_segflg = UIO_NOCOPY;
1150 	auio.uio_rw = UIO_WRITE;
1151 	auio.uio_resid = maxsize;
1152 	auio.uio_td = (struct thread *) 0;
1153 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1154 	PCPU_INC(cnt.v_vnodeout);
1155 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1156 
1157 	if (error) {
1158 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1159 			printf("vnode_pager_putpages: I/O error %d\n", error);
1160 	}
1161 	if (auio.uio_resid) {
1162 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1163 			printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1164 			    auio.uio_resid, (u_long)m[0]->pindex);
1165 	}
1166 	for (i = 0; i < ncount; i++) {
1167 		rtvals[i] = VM_PAGER_OK;
1168 	}
1169 	return rtvals[0];
1170 }
1171