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