xref: /freebsd/sys/vm/vnode_pager.c (revision ec0e626bafb335b30c499d06066997f54b10c092)
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/rwlock.h>
67 #include <sys/sf_buf.h>
68 
69 #include <machine/atomic.h>
70 
71 #include <vm/vm.h>
72 #include <vm/vm_param.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pager.h>
76 #include <vm/vm_map.h>
77 #include <vm/vnode_pager.h>
78 #include <vm/vm_extern.h>
79 
80 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
81     daddr_t *rtaddress, int *run);
82 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
83 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
84 static void vnode_pager_dealloc(vm_object_t);
85 static int vnode_pager_local_getpages0(struct vnode *, vm_page_t *, int, int,
86     vop_getpages_iodone_t, void *);
87 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
88 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int,
89     vop_getpages_iodone_t, void *);
90 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
91 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
92 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
93     vm_ooffset_t, struct ucred *cred);
94 static int vnode_pager_generic_getpages_done(struct buf *);
95 static void vnode_pager_generic_getpages_done_async(struct buf *);
96 
97 struct pagerops vnodepagerops = {
98 	.pgo_alloc =	vnode_pager_alloc,
99 	.pgo_dealloc =	vnode_pager_dealloc,
100 	.pgo_getpages =	vnode_pager_getpages,
101 	.pgo_getpages_async = vnode_pager_getpages_async,
102 	.pgo_putpages =	vnode_pager_putpages,
103 	.pgo_haspage =	vnode_pager_haspage,
104 };
105 
106 int vnode_pbuf_freecnt;
107 int vnode_async_pbuf_freecnt;
108 
109 /* Create the VM system backing object for this vnode */
110 int
111 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
112 {
113 	vm_object_t object;
114 	vm_ooffset_t size = isize;
115 	struct vattr va;
116 
117 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
118 		return (0);
119 
120 	while ((object = vp->v_object) != NULL) {
121 		VM_OBJECT_WLOCK(object);
122 		if (!(object->flags & OBJ_DEAD)) {
123 			VM_OBJECT_WUNLOCK(object);
124 			return (0);
125 		}
126 		VOP_UNLOCK(vp, 0);
127 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
128 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
129 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
130 	}
131 
132 	if (size == 0) {
133 		if (vn_isdisk(vp, NULL)) {
134 			size = IDX_TO_OFF(INT_MAX);
135 		} else {
136 			if (VOP_GETATTR(vp, &va, td->td_ucred))
137 				return (0);
138 			size = va.va_size;
139 		}
140 	}
141 
142 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
143 	/*
144 	 * Dereference the reference we just created.  This assumes
145 	 * that the object is associated with the vp.
146 	 */
147 	VM_OBJECT_WLOCK(object);
148 	object->ref_count--;
149 	VM_OBJECT_WUNLOCK(object);
150 	vrele(vp);
151 
152 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
153 
154 	return (0);
155 }
156 
157 void
158 vnode_destroy_vobject(struct vnode *vp)
159 {
160 	struct vm_object *obj;
161 
162 	obj = vp->v_object;
163 	if (obj == NULL)
164 		return;
165 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
166 	VM_OBJECT_WLOCK(obj);
167 	if (obj->ref_count == 0) {
168 		/*
169 		 * don't double-terminate the object
170 		 */
171 		if ((obj->flags & OBJ_DEAD) == 0)
172 			vm_object_terminate(obj);
173 		else
174 			VM_OBJECT_WUNLOCK(obj);
175 	} else {
176 		/*
177 		 * Woe to the process that tries to page now :-).
178 		 */
179 		vm_pager_deallocate(obj);
180 		VM_OBJECT_WUNLOCK(obj);
181 	}
182 	vp->v_object = NULL;
183 }
184 
185 
186 /*
187  * Allocate (or lookup) pager for a vnode.
188  * Handle is a vnode pointer.
189  *
190  * MPSAFE
191  */
192 vm_object_t
193 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
194     vm_ooffset_t offset, struct ucred *cred)
195 {
196 	vm_object_t object;
197 	struct vnode *vp;
198 
199 	/*
200 	 * Pageout to vnode, no can do yet.
201 	 */
202 	if (handle == NULL)
203 		return (NULL);
204 
205 	vp = (struct vnode *) handle;
206 
207 	/*
208 	 * If the object is being terminated, wait for it to
209 	 * go away.
210 	 */
211 retry:
212 	while ((object = vp->v_object) != NULL) {
213 		VM_OBJECT_WLOCK(object);
214 		if ((object->flags & OBJ_DEAD) == 0)
215 			break;
216 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
217 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
218 	}
219 
220 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
221 
222 	if (object == NULL) {
223 		/*
224 		 * Add an object of the appropriate size
225 		 */
226 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
227 
228 		object->un_pager.vnp.vnp_size = size;
229 		object->un_pager.vnp.writemappings = 0;
230 
231 		object->handle = handle;
232 		VI_LOCK(vp);
233 		if (vp->v_object != NULL) {
234 			/*
235 			 * Object has been created while we were sleeping
236 			 */
237 			VI_UNLOCK(vp);
238 			vm_object_destroy(object);
239 			goto retry;
240 		}
241 		vp->v_object = object;
242 		VI_UNLOCK(vp);
243 	} else {
244 		object->ref_count++;
245 		VM_OBJECT_WUNLOCK(object);
246 	}
247 	vref(vp);
248 	return (object);
249 }
250 
251 /*
252  *	The object must be locked.
253  */
254 static void
255 vnode_pager_dealloc(vm_object_t object)
256 {
257 	struct vnode *vp;
258 	int refs;
259 
260 	vp = object->handle;
261 	if (vp == NULL)
262 		panic("vnode_pager_dealloc: pager already dealloced");
263 
264 	VM_OBJECT_ASSERT_WLOCKED(object);
265 	vm_object_pip_wait(object, "vnpdea");
266 	refs = object->ref_count;
267 
268 	object->handle = NULL;
269 	object->type = OBJT_DEAD;
270 	if (object->flags & OBJ_DISCONNECTWNT) {
271 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
272 		wakeup(object);
273 	}
274 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
275 	if (object->un_pager.vnp.writemappings > 0) {
276 		object->un_pager.vnp.writemappings = 0;
277 		VOP_ADD_WRITECOUNT(vp, -1);
278 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
279 		    __func__, vp, vp->v_writecount);
280 	}
281 	vp->v_object = NULL;
282 	VOP_UNSET_TEXT(vp);
283 	VM_OBJECT_WUNLOCK(object);
284 	while (refs-- > 0)
285 		vunref(vp);
286 	VM_OBJECT_WLOCK(object);
287 }
288 
289 static boolean_t
290 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
291     int *after)
292 {
293 	struct vnode *vp = object->handle;
294 	daddr_t bn;
295 	int err;
296 	daddr_t reqblock;
297 	int poff;
298 	int bsize;
299 	int pagesperblock, blocksperpage;
300 
301 	VM_OBJECT_ASSERT_WLOCKED(object);
302 	/*
303 	 * If no vp or vp is doomed or marked transparent to VM, we do not
304 	 * have the page.
305 	 */
306 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
307 		return FALSE;
308 	/*
309 	 * If the offset is beyond end of file we do
310 	 * not have the page.
311 	 */
312 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
313 		return FALSE;
314 
315 	bsize = vp->v_mount->mnt_stat.f_iosize;
316 	pagesperblock = bsize / PAGE_SIZE;
317 	blocksperpage = 0;
318 	if (pagesperblock > 0) {
319 		reqblock = pindex / pagesperblock;
320 	} else {
321 		blocksperpage = (PAGE_SIZE / bsize);
322 		reqblock = pindex * blocksperpage;
323 	}
324 	VM_OBJECT_WUNLOCK(object);
325 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
326 	VM_OBJECT_WLOCK(object);
327 	if (err)
328 		return TRUE;
329 	if (bn == -1)
330 		return FALSE;
331 	if (pagesperblock > 0) {
332 		poff = pindex - (reqblock * pagesperblock);
333 		if (before) {
334 			*before *= pagesperblock;
335 			*before += poff;
336 		}
337 		if (after) {
338 			int numafter;
339 			*after *= pagesperblock;
340 			numafter = pagesperblock - (poff + 1);
341 			if (IDX_TO_OFF(pindex + numafter) >
342 			    object->un_pager.vnp.vnp_size) {
343 				numafter =
344 		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
345 				    pindex;
346 			}
347 			*after += numafter;
348 		}
349 	} else {
350 		if (before) {
351 			*before /= blocksperpage;
352 		}
353 
354 		if (after) {
355 			*after /= blocksperpage;
356 		}
357 	}
358 	return TRUE;
359 }
360 
361 /*
362  * Lets the VM system know about a change in size for a file.
363  * We adjust our own internal size and flush any cached pages in
364  * the associated object that are affected by the size change.
365  *
366  * Note: this routine may be invoked as a result of a pager put
367  * operation (possibly at object termination time), so we must be careful.
368  */
369 void
370 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
371 {
372 	vm_object_t object;
373 	vm_page_t m;
374 	vm_pindex_t nobjsize;
375 
376 	if ((object = vp->v_object) == NULL)
377 		return;
378 /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
379 	VM_OBJECT_WLOCK(object);
380 	if (object->type == OBJT_DEAD) {
381 		VM_OBJECT_WUNLOCK(object);
382 		return;
383 	}
384 	KASSERT(object->type == OBJT_VNODE,
385 	    ("not vnode-backed object %p", object));
386 	if (nsize == object->un_pager.vnp.vnp_size) {
387 		/*
388 		 * Hasn't changed size
389 		 */
390 		VM_OBJECT_WUNLOCK(object);
391 		return;
392 	}
393 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
394 	if (nsize < object->un_pager.vnp.vnp_size) {
395 		/*
396 		 * File has shrunk. Toss any cached pages beyond the new EOF.
397 		 */
398 		if (nobjsize < object->size)
399 			vm_object_page_remove(object, nobjsize, object->size,
400 			    0);
401 		/*
402 		 * this gets rid of garbage at the end of a page that is now
403 		 * only partially backed by the vnode.
404 		 *
405 		 * XXX for some reason (I don't know yet), if we take a
406 		 * completely invalid page and mark it partially valid
407 		 * it can screw up NFS reads, so we don't allow the case.
408 		 */
409 		if ((nsize & PAGE_MASK) &&
410 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
411 		    m->valid != 0) {
412 			int base = (int)nsize & PAGE_MASK;
413 			int size = PAGE_SIZE - base;
414 
415 			/*
416 			 * Clear out partial-page garbage in case
417 			 * the page has been mapped.
418 			 */
419 			pmap_zero_page_area(m, base, size);
420 
421 			/*
422 			 * Update the valid bits to reflect the blocks that
423 			 * have been zeroed.  Some of these valid bits may
424 			 * have already been set.
425 			 */
426 			vm_page_set_valid_range(m, base, size);
427 
428 			/*
429 			 * Round "base" to the next block boundary so that the
430 			 * dirty bit for a partially zeroed block is not
431 			 * cleared.
432 			 */
433 			base = roundup2(base, DEV_BSIZE);
434 
435 			/*
436 			 * Clear out partial-page dirty bits.
437 			 *
438 			 * note that we do not clear out the valid
439 			 * bits.  This would prevent bogus_page
440 			 * replacement from working properly.
441 			 */
442 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
443 		} else if ((nsize & PAGE_MASK) &&
444 		    vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
445 			vm_page_cache_free(object, OFF_TO_IDX(nsize),
446 			    nobjsize);
447 		}
448 	}
449 	object->un_pager.vnp.vnp_size = nsize;
450 	object->size = nobjsize;
451 	VM_OBJECT_WUNLOCK(object);
452 }
453 
454 /*
455  * calculate the linear (byte) disk address of specified virtual
456  * file address
457  */
458 static int
459 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
460     int *run)
461 {
462 	int bsize;
463 	int err;
464 	daddr_t vblock;
465 	daddr_t voffset;
466 
467 	if (address < 0)
468 		return -1;
469 
470 	if (vp->v_iflag & VI_DOOMED)
471 		return -1;
472 
473 	bsize = vp->v_mount->mnt_stat.f_iosize;
474 	vblock = address / bsize;
475 	voffset = address % bsize;
476 
477 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
478 	if (err == 0) {
479 		if (*rtaddress != -1)
480 			*rtaddress += voffset / DEV_BSIZE;
481 		if (run) {
482 			*run += 1;
483 			*run *= bsize/PAGE_SIZE;
484 			*run -= voffset/PAGE_SIZE;
485 		}
486 	}
487 
488 	return (err);
489 }
490 
491 /*
492  * small block filesystem vnode pager input
493  */
494 static int
495 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
496 {
497 	struct vnode *vp;
498 	struct bufobj *bo;
499 	struct buf *bp;
500 	struct sf_buf *sf;
501 	daddr_t fileaddr;
502 	vm_offset_t bsize;
503 	vm_page_bits_t bits;
504 	int error, i;
505 
506 	error = 0;
507 	vp = object->handle;
508 	if (vp->v_iflag & VI_DOOMED)
509 		return VM_PAGER_BAD;
510 
511 	bsize = vp->v_mount->mnt_stat.f_iosize;
512 
513 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
514 
515 	sf = sf_buf_alloc(m, 0);
516 
517 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
518 		vm_ooffset_t address;
519 
520 		bits = vm_page_bits(i * bsize, bsize);
521 		if (m->valid & bits)
522 			continue;
523 
524 		address = IDX_TO_OFF(m->pindex) + i * bsize;
525 		if (address >= object->un_pager.vnp.vnp_size) {
526 			fileaddr = -1;
527 		} else {
528 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
529 			if (error)
530 				break;
531 		}
532 		if (fileaddr != -1) {
533 			bp = getpbuf(&vnode_pbuf_freecnt);
534 
535 			/* build a minimal buffer header */
536 			bp->b_iocmd = BIO_READ;
537 			bp->b_iodone = bdone;
538 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
539 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
540 			bp->b_rcred = crhold(curthread->td_ucred);
541 			bp->b_wcred = crhold(curthread->td_ucred);
542 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
543 			bp->b_blkno = fileaddr;
544 			pbgetbo(bo, bp);
545 			bp->b_vp = vp;
546 			bp->b_bcount = bsize;
547 			bp->b_bufsize = bsize;
548 			bp->b_runningbufspace = bp->b_bufsize;
549 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
550 
551 			/* do the input */
552 			bp->b_iooffset = dbtob(bp->b_blkno);
553 			bstrategy(bp);
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 			bp->b_vp = NULL;
564 			pbrelbo(bp);
565 			relpbuf(bp, &vnode_pbuf_freecnt);
566 			if (error)
567 				break;
568 		} else
569 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
570 		KASSERT((m->dirty & bits) == 0,
571 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
572 		VM_OBJECT_WLOCK(object);
573 		m->valid |= bits;
574 		VM_OBJECT_WUNLOCK(object);
575 	}
576 	sf_buf_free(sf);
577 	if (error) {
578 		return VM_PAGER_ERROR;
579 	}
580 	return VM_PAGER_OK;
581 }
582 
583 /*
584  * old style vnode pager input routine
585  */
586 static int
587 vnode_pager_input_old(vm_object_t object, vm_page_t m)
588 {
589 	struct uio auio;
590 	struct iovec aiov;
591 	int error;
592 	int size;
593 	struct sf_buf *sf;
594 	struct vnode *vp;
595 
596 	VM_OBJECT_ASSERT_WLOCKED(object);
597 	error = 0;
598 
599 	/*
600 	 * Return failure if beyond current EOF
601 	 */
602 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
603 		return VM_PAGER_BAD;
604 	} else {
605 		size = PAGE_SIZE;
606 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
607 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
608 		vp = object->handle;
609 		VM_OBJECT_WUNLOCK(object);
610 
611 		/*
612 		 * Allocate a kernel virtual address and initialize so that
613 		 * we can use VOP_READ/WRITE routines.
614 		 */
615 		sf = sf_buf_alloc(m, 0);
616 
617 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
618 		aiov.iov_len = size;
619 		auio.uio_iov = &aiov;
620 		auio.uio_iovcnt = 1;
621 		auio.uio_offset = IDX_TO_OFF(m->pindex);
622 		auio.uio_segflg = UIO_SYSSPACE;
623 		auio.uio_rw = UIO_READ;
624 		auio.uio_resid = size;
625 		auio.uio_td = curthread;
626 
627 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
628 		if (!error) {
629 			int count = size - auio.uio_resid;
630 
631 			if (count == 0)
632 				error = EINVAL;
633 			else if (count != PAGE_SIZE)
634 				bzero((caddr_t)sf_buf_kva(sf) + count,
635 				    PAGE_SIZE - count);
636 		}
637 		sf_buf_free(sf);
638 
639 		VM_OBJECT_WLOCK(object);
640 	}
641 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
642 	if (!error)
643 		m->valid = VM_PAGE_BITS_ALL;
644 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
645 }
646 
647 /*
648  * generic vnode pager input routine
649  */
650 
651 /*
652  * Local media VFS's that do not implement their own VOP_GETPAGES
653  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
654  * to implement the previous behaviour.
655  *
656  * All other FS's should use the bypass to get to the local media
657  * backing vp's VOP_GETPAGES.
658  */
659 static int
660 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
661 {
662 	int rtval;
663 	struct vnode *vp;
664 	int bytes = count * PAGE_SIZE;
665 
666 	vp = object->handle;
667 	VM_OBJECT_WUNLOCK(object);
668 	rtval = VOP_GETPAGES(vp, m, bytes, reqpage);
669 	KASSERT(rtval != EOPNOTSUPP,
670 	    ("vnode_pager: FS getpages not implemented\n"));
671 	VM_OBJECT_WLOCK(object);
672 	return rtval;
673 }
674 
675 static int
676 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
677     int reqpage, vop_getpages_iodone_t iodone, void *arg)
678 {
679 	struct vnode *vp;
680 	int rtval;
681 
682 	vp = object->handle;
683 	VM_OBJECT_WUNLOCK(object);
684 	rtval = VOP_GETPAGES_ASYNC(vp, m, count * PAGE_SIZE, reqpage, 0,
685 	    iodone, arg);
686 	KASSERT(rtval != EOPNOTSUPP,
687 	    ("vnode_pager: FS getpages_async not implemented\n"));
688 	VM_OBJECT_WLOCK(object);
689 	return (rtval);
690 }
691 
692 /*
693  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
694  * local filesystems, where partially valid pages can only occur at
695  * the end of file.
696  */
697 int
698 vnode_pager_local_getpages(struct vop_getpages_args *ap)
699 {
700 
701 	return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count,
702 	    ap->a_reqpage, NULL, NULL));
703 }
704 
705 int
706 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
707 {
708 
709 	return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count,
710 	    ap->a_reqpage, ap->a_iodone, ap->a_arg));
711 }
712 
713 static int
714 vnode_pager_local_getpages0(struct vnode *vp, vm_page_t *m, int bytecount,
715     int reqpage, vop_getpages_iodone_t iodone, void *arg)
716 {
717 	vm_page_t mreq;
718 
719 	mreq = m[reqpage];
720 
721 	/*
722 	 * Since the caller has busied the requested page, that page's valid
723 	 * field will not be changed by other threads.
724 	 */
725 	vm_page_assert_xbusied(mreq);
726 
727 	/*
728 	 * The requested page has valid blocks.  Invalid part can only
729 	 * exist at the end of file, and the page is made fully valid
730 	 * by zeroing in vm_pager_get_pages().  Free non-requested
731 	 * pages, since no i/o is done to read its content.
732 	 */
733 	if (mreq->valid != 0) {
734 		vm_pager_free_nonreq(mreq->object, m, reqpage,
735 		    round_page(bytecount) / PAGE_SIZE, FALSE);
736 		if (iodone != NULL)
737 			iodone(arg, m, reqpage, 0);
738 		return (VM_PAGER_OK);
739 	}
740 
741 	return (vnode_pager_generic_getpages(vp, m, bytecount, reqpage,
742 	    iodone, arg));
743 }
744 
745 /*
746  * This is now called from local media FS's to operate against their
747  * own vnodes if they fail to implement VOP_GETPAGES.
748  */
749 int
750 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount,
751     int reqpage, vop_getpages_iodone_t iodone, void *arg)
752 {
753 	vm_object_t object;
754 	off_t foff;
755 	int i, j, size, bsize, first, *freecnt;
756 	daddr_t firstaddr, reqblock;
757 	struct bufobj *bo;
758 	int runpg;
759 	int runend;
760 	struct buf *bp;
761 	int count;
762 	int error;
763 
764 	object = vp->v_object;
765 	count = bytecount / PAGE_SIZE;
766 
767 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
768 	    ("vnode_pager_generic_getpages does not support devices"));
769 	if (vp->v_iflag & VI_DOOMED)
770 		return VM_PAGER_BAD;
771 
772 	bsize = vp->v_mount->mnt_stat.f_iosize;
773 	foff = IDX_TO_OFF(m[reqpage]->pindex);
774 
775 	/*
776 	 * Synchronous and asynchronous paging operations use different
777 	 * free pbuf counters.  This is done to avoid asynchronous requests
778 	 * to consume all pbufs.
779 	 * Allocate the pbuf at the very beginning of the function, so that
780 	 * if we are low on certain kind of pbufs don't even proceed to BMAP,
781 	 * but sleep.
782 	 */
783 	freecnt = iodone != NULL ?
784 	    &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
785 	bp = getpbuf(freecnt);
786 
787 	/*
788 	 * Get the underlying device blocks for the file with VOP_BMAP().
789 	 * If the file system doesn't support VOP_BMAP, use old way of
790 	 * getting pages via VOP_READ.
791 	 */
792 	error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
793 	if (error == EOPNOTSUPP) {
794 		relpbuf(bp, freecnt);
795 		VM_OBJECT_WLOCK(object);
796 		for (i = 0; i < count; i++)
797 			if (i != reqpage) {
798 				vm_page_lock(m[i]);
799 				vm_page_free(m[i]);
800 				vm_page_unlock(m[i]);
801 			}
802 		PCPU_INC(cnt.v_vnodein);
803 		PCPU_INC(cnt.v_vnodepgsin);
804 		error = vnode_pager_input_old(object, m[reqpage]);
805 		VM_OBJECT_WUNLOCK(object);
806 		return (error);
807 	} else if (error != 0) {
808 		relpbuf(bp, freecnt);
809 		vm_pager_free_nonreq(object, m, reqpage, count, FALSE);
810 		return (VM_PAGER_ERROR);
811 
812 		/*
813 		 * if the blocksize is smaller than a page size, then use
814 		 * special small filesystem code.  NFS sometimes has a small
815 		 * blocksize, but it can handle large reads itself.
816 		 */
817 	} else if ((PAGE_SIZE / bsize) > 1 &&
818 	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
819 		relpbuf(bp, freecnt);
820 		vm_pager_free_nonreq(object, m, reqpage, count, FALSE);
821 		PCPU_INC(cnt.v_vnodein);
822 		PCPU_INC(cnt.v_vnodepgsin);
823 		return vnode_pager_input_smlfs(object, m[reqpage]);
824 	}
825 
826 	/*
827 	 * Since the caller has busied the requested page, that page's valid
828 	 * field will not be changed by other threads.
829 	 */
830 	vm_page_assert_xbusied(m[reqpage]);
831 
832 	/*
833 	 * If we have a completely valid page available to us, we can
834 	 * clean up and return.  Otherwise we have to re-read the
835 	 * media.
836 	 */
837 	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
838 		relpbuf(bp, freecnt);
839 		vm_pager_free_nonreq(object, m, reqpage, count, FALSE);
840 		return (VM_PAGER_OK);
841 	} else if (reqblock == -1) {
842 		relpbuf(bp, freecnt);
843 		pmap_zero_page(m[reqpage]);
844 		KASSERT(m[reqpage]->dirty == 0,
845 		    ("vnode_pager_generic_getpages: page %p is dirty", m));
846 		VM_OBJECT_WLOCK(object);
847 		m[reqpage]->valid = VM_PAGE_BITS_ALL;
848 		vm_pager_free_nonreq(object, m, reqpage, count, TRUE);
849 		VM_OBJECT_WUNLOCK(object);
850 		return (VM_PAGER_OK);
851 	} else if (m[reqpage]->valid != 0) {
852 		VM_OBJECT_WLOCK(object);
853 		m[reqpage]->valid = 0;
854 		VM_OBJECT_WUNLOCK(object);
855 	}
856 
857 	/*
858 	 * here on direct device I/O
859 	 */
860 	firstaddr = -1;
861 
862 	/*
863 	 * calculate the run that includes the required page
864 	 */
865 	for (first = 0, i = 0; i < count; i = runend) {
866 		if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
867 		    &runpg) != 0) {
868 			relpbuf(bp, freecnt);
869 			/* The requested page may be out of range. */
870 			vm_pager_free_nonreq(object, m + i, reqpage - i,
871 			    count - i, FALSE);
872 			return (VM_PAGER_ERROR);
873 		}
874 		if (firstaddr == -1) {
875 			VM_OBJECT_WLOCK(object);
876 			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
877 				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
878 				    (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
879 				    (uintmax_t)foff,
880 				    (uintmax_t)
881 				    (object->un_pager.vnp.vnp_size >> 32),
882 				    (uintmax_t)object->un_pager.vnp.vnp_size);
883 			}
884 			vm_page_lock(m[i]);
885 			vm_page_free(m[i]);
886 			vm_page_unlock(m[i]);
887 			VM_OBJECT_WUNLOCK(object);
888 			runend = i + 1;
889 			first = runend;
890 			continue;
891 		}
892 		runend = i + runpg;
893 		if (runend <= reqpage) {
894 			VM_OBJECT_WLOCK(object);
895 			for (j = i; j < runend; j++) {
896 				vm_page_lock(m[j]);
897 				vm_page_free(m[j]);
898 				vm_page_unlock(m[j]);
899 			}
900 			VM_OBJECT_WUNLOCK(object);
901 		} else {
902 			if (runpg < (count - first)) {
903 				VM_OBJECT_WLOCK(object);
904 				for (i = first + runpg; i < count; i++) {
905 					vm_page_lock(m[i]);
906 					vm_page_free(m[i]);
907 					vm_page_unlock(m[i]);
908 				}
909 				VM_OBJECT_WUNLOCK(object);
910 				count = first + runpg;
911 			}
912 			break;
913 		}
914 		first = runend;
915 	}
916 
917 	/*
918 	 * the first and last page have been calculated now, move input pages
919 	 * to be zero based...
920 	 */
921 	if (first != 0) {
922 		m += first;
923 		count -= first;
924 		reqpage -= first;
925 	}
926 
927 	/*
928 	 * calculate the file virtual address for the transfer
929 	 */
930 	foff = IDX_TO_OFF(m[0]->pindex);
931 
932 	/*
933 	 * calculate the size of the transfer
934 	 */
935 	size = count * PAGE_SIZE;
936 	KASSERT(count > 0, ("zero count"));
937 	if ((foff + size) > object->un_pager.vnp.vnp_size)
938 		size = object->un_pager.vnp.vnp_size - foff;
939 	KASSERT(size > 0, ("zero size"));
940 
941 	/*
942 	 * round up physical size for real devices.
943 	 */
944 	if (1) {
945 		int secmask = bo->bo_bsize - 1;
946 		KASSERT(secmask < PAGE_SIZE && secmask > 0,
947 		    ("vnode_pager_generic_getpages: sector size %d too large",
948 		    secmask + 1));
949 		size = (size + secmask) & ~secmask;
950 	}
951 
952 	bp->b_kvaalloc = bp->b_data;
953 
954 	/*
955 	 * and map the pages to be read into the kva, if the filesystem
956 	 * requires mapped buffers.
957 	 */
958 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
959 	    unmapped_buf_allowed) {
960 		bp->b_data = unmapped_buf;
961 		bp->b_kvabase = unmapped_buf;
962 		bp->b_offset = 0;
963 		bp->b_flags |= B_UNMAPPED;
964 	} else
965 		pmap_qenter((vm_offset_t)bp->b_kvaalloc, m, count);
966 
967 	/* build a minimal buffer header */
968 	bp->b_iocmd = BIO_READ;
969 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
970 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
971 	bp->b_rcred = crhold(curthread->td_ucred);
972 	bp->b_wcred = crhold(curthread->td_ucred);
973 	bp->b_blkno = firstaddr;
974 	pbgetbo(bo, bp);
975 	bp->b_vp = vp;
976 	bp->b_bcount = size;
977 	bp->b_bufsize = size;
978 	bp->b_runningbufspace = bp->b_bufsize;
979 	for (i = 0; i < count; i++)
980 		bp->b_pages[i] = m[i];
981 	bp->b_npages = count;
982 	bp->b_pager.pg_reqpage = reqpage;
983 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
984 
985 	PCPU_INC(cnt.v_vnodein);
986 	PCPU_ADD(cnt.v_vnodepgsin, count);
987 
988 	/* do the input */
989 	bp->b_iooffset = dbtob(bp->b_blkno);
990 
991 	if (iodone != NULL) { /* async */
992 		bp->b_pager.pg_iodone = iodone;
993 		bp->b_caller1 = arg;
994 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
995 		bp->b_flags |= B_ASYNC;
996 		BUF_KERNPROC(bp);
997 		bstrategy(bp);
998 		/* Good bye! */
999 	} else {
1000 		bp->b_iodone = bdone;
1001 		bstrategy(bp);
1002 		bwait(bp, PVM, "vnread");
1003 		error = vnode_pager_generic_getpages_done(bp);
1004 		for (i = 0; i < bp->b_npages; i++)
1005 			bp->b_pages[i] = NULL;
1006 		bp->b_vp = NULL;
1007 		pbrelbo(bp);
1008 		relpbuf(bp, &vnode_pbuf_freecnt);
1009 	}
1010 
1011 	return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1012 }
1013 
1014 static void
1015 vnode_pager_generic_getpages_done_async(struct buf *bp)
1016 {
1017 	int error;
1018 
1019 	error = vnode_pager_generic_getpages_done(bp);
1020 	bp->b_pager.pg_iodone(bp->b_caller1, bp->b_pages,
1021 	  bp->b_pager.pg_reqpage, error);
1022 	for (int i = 0; i < bp->b_npages; i++)
1023 		bp->b_pages[i] = NULL;
1024 	bp->b_vp = NULL;
1025 	pbrelbo(bp);
1026 	relpbuf(bp, &vnode_async_pbuf_freecnt);
1027 }
1028 
1029 static int
1030 vnode_pager_generic_getpages_done(struct buf *bp)
1031 {
1032 	vm_object_t object;
1033 	off_t tfoff, nextoff;
1034 	int i, error;
1035 
1036 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1037 	object = bp->b_vp->v_object;
1038 
1039 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1040 		if ((bp->b_flags & B_UNMAPPED) != 0) {
1041 			bp->b_flags &= ~B_UNMAPPED;
1042 			pmap_qenter((vm_offset_t)bp->b_kvaalloc, bp->b_pages,
1043 			    bp->b_npages);
1044 		}
1045 		bzero(bp->b_kvaalloc + bp->b_bcount,
1046 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
1047 	}
1048 	if ((bp->b_flags & B_UNMAPPED) == 0)
1049 		pmap_qremove((vm_offset_t)bp->b_kvaalloc, bp->b_npages);
1050 	if ((bp->b_vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0) {
1051 		bp->b_data = bp->b_kvaalloc;
1052 		bp->b_kvabase = bp->b_kvaalloc;
1053 		bp->b_flags &= ~B_UNMAPPED;
1054 	}
1055 
1056 	VM_OBJECT_WLOCK(object);
1057 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1058 	    i < bp->b_npages; i++, tfoff = nextoff) {
1059 		vm_page_t mt;
1060 
1061 		nextoff = tfoff + PAGE_SIZE;
1062 		mt = bp->b_pages[i];
1063 
1064 		if (nextoff <= object->un_pager.vnp.vnp_size) {
1065 			/*
1066 			 * Read filled up entire page.
1067 			 */
1068 			mt->valid = VM_PAGE_BITS_ALL;
1069 			KASSERT(mt->dirty == 0,
1070 			    ("%s: page %p is dirty", __func__, mt));
1071 			KASSERT(!pmap_page_is_mapped(mt),
1072 			    ("%s: page %p is mapped", __func__, mt));
1073 		} else {
1074 			/*
1075 			 * Read did not fill up entire page.
1076 			 *
1077 			 * Currently we do not set the entire page valid,
1078 			 * we just try to clear the piece that we couldn't
1079 			 * read.
1080 			 */
1081 			vm_page_set_valid_range(mt, 0,
1082 			    object->un_pager.vnp.vnp_size - tfoff);
1083 			KASSERT((mt->dirty & vm_page_bits(0,
1084 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
1085 			    ("%s: page %p is dirty", __func__, mt));
1086 		}
1087 
1088 		if (i != bp->b_pager.pg_reqpage)
1089 			vm_page_readahead_finish(mt);
1090 	}
1091 	VM_OBJECT_WUNLOCK(object);
1092 	if (error != 0)
1093 		printf("%s: I/O read error %d\n", __func__, error);
1094 
1095 	return (error);
1096 }
1097 
1098 /*
1099  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1100  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1101  * vnode_pager_generic_putpages() to implement the previous behaviour.
1102  *
1103  * All other FS's should use the bypass to get to the local media
1104  * backing vp's VOP_PUTPAGES.
1105  */
1106 static void
1107 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1108     int flags, int *rtvals)
1109 {
1110 	int rtval;
1111 	struct vnode *vp;
1112 	int bytes = count * PAGE_SIZE;
1113 
1114 	/*
1115 	 * Force synchronous operation if we are extremely low on memory
1116 	 * to prevent a low-memory deadlock.  VOP operations often need to
1117 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1118 	 * operation ).  The swapper handles the case by limiting the amount
1119 	 * of asynchronous I/O, but that sort of solution doesn't scale well
1120 	 * for the vnode pager without a lot of work.
1121 	 *
1122 	 * Also, the backing vnode's iodone routine may not wake the pageout
1123 	 * daemon up.  This should be probably be addressed XXX.
1124 	 */
1125 
1126 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count <
1127 	    vm_cnt.v_pageout_free_min)
1128 		flags |= VM_PAGER_PUT_SYNC;
1129 
1130 	/*
1131 	 * Call device-specific putpages function
1132 	 */
1133 	vp = object->handle;
1134 	VM_OBJECT_WUNLOCK(object);
1135 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1136 	KASSERT(rtval != EOPNOTSUPP,
1137 	    ("vnode_pager: stale FS putpages\n"));
1138 	VM_OBJECT_WLOCK(object);
1139 }
1140 
1141 
1142 /*
1143  * This is now called from local media FS's to operate against their
1144  * own vnodes if they fail to implement VOP_PUTPAGES.
1145  *
1146  * This is typically called indirectly via the pageout daemon and
1147  * clustering has already typically occured, so in general we ask the
1148  * underlying filesystem to write the data out asynchronously rather
1149  * then delayed.
1150  */
1151 int
1152 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1153     int flags, int *rtvals)
1154 {
1155 	int i;
1156 	vm_object_t object;
1157 	vm_page_t m;
1158 	int count;
1159 
1160 	int maxsize, ncount;
1161 	vm_ooffset_t poffset;
1162 	struct uio auio;
1163 	struct iovec aiov;
1164 	int error;
1165 	int ioflags;
1166 	int ppscheck = 0;
1167 	static struct timeval lastfail;
1168 	static int curfail;
1169 
1170 	object = vp->v_object;
1171 	count = bytecount / PAGE_SIZE;
1172 
1173 	for (i = 0; i < count; i++)
1174 		rtvals[i] = VM_PAGER_ERROR;
1175 
1176 	if ((int64_t)ma[0]->pindex < 0) {
1177 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1178 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1179 		rtvals[0] = VM_PAGER_BAD;
1180 		return VM_PAGER_BAD;
1181 	}
1182 
1183 	maxsize = count * PAGE_SIZE;
1184 	ncount = count;
1185 
1186 	poffset = IDX_TO_OFF(ma[0]->pindex);
1187 
1188 	/*
1189 	 * If the page-aligned write is larger then the actual file we
1190 	 * have to invalidate pages occuring beyond the file EOF.  However,
1191 	 * there is an edge case where a file may not be page-aligned where
1192 	 * the last page is partially invalid.  In this case the filesystem
1193 	 * may not properly clear the dirty bits for the entire page (which
1194 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1195 	 * With the page locked we are free to fix-up the dirty bits here.
1196 	 *
1197 	 * We do not under any circumstances truncate the valid bits, as
1198 	 * this will screw up bogus page replacement.
1199 	 */
1200 	VM_OBJECT_WLOCK(object);
1201 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1202 		if (object->un_pager.vnp.vnp_size > poffset) {
1203 			int pgoff;
1204 
1205 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1206 			ncount = btoc(maxsize);
1207 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1208 				/*
1209 				 * If the object is locked and the following
1210 				 * conditions hold, then the page's dirty
1211 				 * field cannot be concurrently changed by a
1212 				 * pmap operation.
1213 				 */
1214 				m = ma[ncount - 1];
1215 				vm_page_assert_sbusied(m);
1216 				KASSERT(!pmap_page_is_write_mapped(m),
1217 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1218 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1219 				    pgoff);
1220 			}
1221 		} else {
1222 			maxsize = 0;
1223 			ncount = 0;
1224 		}
1225 		if (ncount < count) {
1226 			for (i = ncount; i < count; i++) {
1227 				rtvals[i] = VM_PAGER_BAD;
1228 			}
1229 		}
1230 	}
1231 	VM_OBJECT_WUNLOCK(object);
1232 
1233 	/*
1234 	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
1235 	 * rather then a bdwrite() to prevent paging I/O from saturating
1236 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
1237 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
1238 	 * the system decides how to cluster.
1239 	 */
1240 	ioflags = IO_VMIO;
1241 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1242 		ioflags |= IO_SYNC;
1243 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1244 		ioflags |= IO_ASYNC;
1245 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1246 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1247 
1248 	aiov.iov_base = (caddr_t) 0;
1249 	aiov.iov_len = maxsize;
1250 	auio.uio_iov = &aiov;
1251 	auio.uio_iovcnt = 1;
1252 	auio.uio_offset = poffset;
1253 	auio.uio_segflg = UIO_NOCOPY;
1254 	auio.uio_rw = UIO_WRITE;
1255 	auio.uio_resid = maxsize;
1256 	auio.uio_td = (struct thread *) 0;
1257 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1258 	PCPU_INC(cnt.v_vnodeout);
1259 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1260 
1261 	if (error) {
1262 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1263 			printf("vnode_pager_putpages: I/O error %d\n", error);
1264 	}
1265 	if (auio.uio_resid) {
1266 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1267 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1268 			    auio.uio_resid, (u_long)ma[0]->pindex);
1269 	}
1270 	for (i = 0; i < ncount; i++) {
1271 		rtvals[i] = VM_PAGER_OK;
1272 	}
1273 	return rtvals[0];
1274 }
1275 
1276 void
1277 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1278 {
1279 	vm_object_t obj;
1280 	int i, pos;
1281 
1282 	if (written == 0)
1283 		return;
1284 	obj = ma[0]->object;
1285 	VM_OBJECT_WLOCK(obj);
1286 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1287 		if (pos < trunc_page(written)) {
1288 			rtvals[i] = VM_PAGER_OK;
1289 			vm_page_undirty(ma[i]);
1290 		} else {
1291 			/* Partially written page. */
1292 			rtvals[i] = VM_PAGER_AGAIN;
1293 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1294 		}
1295 	}
1296 	VM_OBJECT_WUNLOCK(obj);
1297 }
1298 
1299 void
1300 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1301     vm_offset_t end)
1302 {
1303 	struct vnode *vp;
1304 	vm_ooffset_t old_wm;
1305 
1306 	VM_OBJECT_WLOCK(object);
1307 	if (object->type != OBJT_VNODE) {
1308 		VM_OBJECT_WUNLOCK(object);
1309 		return;
1310 	}
1311 	old_wm = object->un_pager.vnp.writemappings;
1312 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1313 	vp = object->handle;
1314 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1315 		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1316 		VOP_ADD_WRITECOUNT(vp, 1);
1317 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1318 		    __func__, vp, vp->v_writecount);
1319 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1320 		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1321 		VOP_ADD_WRITECOUNT(vp, -1);
1322 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1323 		    __func__, vp, vp->v_writecount);
1324 	}
1325 	VM_OBJECT_WUNLOCK(object);
1326 }
1327 
1328 void
1329 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1330     vm_offset_t end)
1331 {
1332 	struct vnode *vp;
1333 	struct mount *mp;
1334 	vm_offset_t inc;
1335 
1336 	VM_OBJECT_WLOCK(object);
1337 
1338 	/*
1339 	 * First, recheck the object type to account for the race when
1340 	 * the vnode is reclaimed.
1341 	 */
1342 	if (object->type != OBJT_VNODE) {
1343 		VM_OBJECT_WUNLOCK(object);
1344 		return;
1345 	}
1346 
1347 	/*
1348 	 * Optimize for the case when writemappings is not going to
1349 	 * zero.
1350 	 */
1351 	inc = end - start;
1352 	if (object->un_pager.vnp.writemappings != inc) {
1353 		object->un_pager.vnp.writemappings -= inc;
1354 		VM_OBJECT_WUNLOCK(object);
1355 		return;
1356 	}
1357 
1358 	vp = object->handle;
1359 	vhold(vp);
1360 	VM_OBJECT_WUNLOCK(object);
1361 	mp = NULL;
1362 	vn_start_write(vp, &mp, V_WAIT);
1363 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1364 
1365 	/*
1366 	 * Decrement the object's writemappings, by swapping the start
1367 	 * and end arguments for vnode_pager_update_writecount().  If
1368 	 * there was not a race with vnode reclaimation, then the
1369 	 * vnode's v_writecount is decremented.
1370 	 */
1371 	vnode_pager_update_writecount(object, end, start);
1372 	VOP_UNLOCK(vp, 0);
1373 	vdrop(vp);
1374 	if (mp != NULL)
1375 		vn_finished_write(mp);
1376 }
1377