xref: /freebsd/sys/vm/vnode_pager.c (revision 3dbb0ca64602973e29db1be7b94f37e9d9ba5a75)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
326f9a767SRodney W. Grimes  * Copyright (c) 1991 The Regents of the University of California.
426f9a767SRodney W. Grimes  * All rights reserved.
526f9a767SRodney W. Grimes  * Copyright (c) 1993, 1994 John S. Dyson
624a1cce3SDavid Greenman  * Copyright (c) 1995, David Greenman
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
215929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
4026f9a767SRodney W. Grimes  *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes /*
44df8bae1dSRodney W. Grimes  * Page to/from files (vnodes).
45df8bae1dSRodney W. Grimes  */
46df8bae1dSRodney W. Grimes 
4726f9a767SRodney W. Grimes /*
4826f9a767SRodney W. Grimes  * TODO:
4924a1cce3SDavid Greenman  *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
50f6b04d2bSDavid Greenman  *	greatly re-simplify the vnode_pager.
5126f9a767SRodney W. Grimes  */
5226f9a767SRodney W. Grimes 
53874651b1SDavid E. O'Brien #include <sys/cdefs.h>
54874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
55874651b1SDavid E. O'Brien 
563d653db0SAlan Cox #include "opt_vm.h"
573d653db0SAlan Cox 
58df8bae1dSRodney W. Grimes #include <sys/param.h>
59df8bae1dSRodney W. Grimes #include <sys/systm.h>
60df8bae1dSRodney W. Grimes #include <sys/proc.h>
61df8bae1dSRodney W. Grimes #include <sys/vnode.h>
62df8bae1dSRodney W. Grimes #include <sys/mount.h>
639626b608SPoul-Henning Kamp #include <sys/bio.h>
6424a1cce3SDavid Greenman #include <sys/buf.h>
65efeaf95aSDavid Greenman #include <sys/vmmeter.h>
66d07a6d3fSPoul-Henning Kamp #include <sys/limits.h>
6724579ca1SMatthew Dillon #include <sys/conf.h>
6889f6b863SAttilio Rao #include <sys/rwlock.h>
699e0ddbd0SAlan Cox #include <sys/sf_buf.h>
70df8bae1dSRodney W. Grimes 
714f12e0acSSuleiman Souhlal #include <machine/atomic.h>
724f12e0acSSuleiman Souhlal 
73df8bae1dSRodney W. Grimes #include <vm/vm.h>
741c771f92SKonstantin Belousov #include <vm/vm_param.h>
75efeaf95aSDavid Greenman #include <vm/vm_object.h>
76df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7724a1cce3SDavid Greenman #include <vm/vm_pager.h>
781efb74fbSJohn Dyson #include <vm/vm_map.h>
79df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h>
80efeaf95aSDavid Greenman #include <vm/vm_extern.h>
81df8bae1dSRodney W. Grimes 
82bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
83bff76343SAlan Cox     daddr_t *rtaddress, int *run);
8411caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
8511caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
8611caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t);
87b0cd2017SGleb Smirnoff static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
88b0cd2017SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
89b0cd2017SGleb Smirnoff     int *, vop_getpages_iodone_t, void *);
9033cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
9111caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
923364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
933364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *cred);
9490effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *);
9590effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *);
960b8253a7SBruce Evans 
97df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = {
984e658600SPoul-Henning Kamp 	.pgo_alloc =	vnode_pager_alloc,
994e658600SPoul-Henning Kamp 	.pgo_dealloc =	vnode_pager_dealloc,
1004e658600SPoul-Henning Kamp 	.pgo_getpages =	vnode_pager_getpages,
10190effb23SGleb Smirnoff 	.pgo_getpages_async = vnode_pager_getpages_async,
1024e658600SPoul-Henning Kamp 	.pgo_putpages =	vnode_pager_putpages,
1034e658600SPoul-Henning Kamp 	.pgo_haspage =	vnode_pager_haspage,
104df8bae1dSRodney W. Grimes };
105df8bae1dSRodney W. Grimes 
106b62b9b64SJohn Baldwin int vnode_pbuf_freecnt;
10773e9030eSGleb Smirnoff int vnode_async_pbuf_freecnt;
1081c7c3c6aSMatthew Dillon 
109d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */
110d07a6d3fSPoul-Henning Kamp int
111731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
112d07a6d3fSPoul-Henning Kamp {
113d07a6d3fSPoul-Henning Kamp 	vm_object_t object;
114d07a6d3fSPoul-Henning Kamp 	vm_ooffset_t size = isize;
115d07a6d3fSPoul-Henning Kamp 	struct vattr va;
116d07a6d3fSPoul-Henning Kamp 
117d07a6d3fSPoul-Henning Kamp 	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
118d07a6d3fSPoul-Henning Kamp 		return (0);
119d07a6d3fSPoul-Henning Kamp 
120d07a6d3fSPoul-Henning Kamp 	while ((object = vp->v_object) != NULL) {
12189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
122d07a6d3fSPoul-Henning Kamp 		if (!(object->flags & OBJ_DEAD)) {
12389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
124d07a6d3fSPoul-Henning Kamp 			return (0);
125d07a6d3fSPoul-Henning Kamp 		}
12622db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
127d07a6d3fSPoul-Henning Kamp 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
1280dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
129cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
130d07a6d3fSPoul-Henning Kamp 	}
131d07a6d3fSPoul-Henning Kamp 
132d07a6d3fSPoul-Henning Kamp 	if (size == 0) {
133d07a6d3fSPoul-Henning Kamp 		if (vn_isdisk(vp, NULL)) {
134d07a6d3fSPoul-Henning Kamp 			size = IDX_TO_OFF(INT_MAX);
135d07a6d3fSPoul-Henning Kamp 		} else {
1360359a12eSAttilio Rao 			if (VOP_GETATTR(vp, &va, td->td_ucred))
137d07a6d3fSPoul-Henning Kamp 				return (0);
138d07a6d3fSPoul-Henning Kamp 			size = va.va_size;
139d07a6d3fSPoul-Henning Kamp 		}
140d07a6d3fSPoul-Henning Kamp 	}
141d07a6d3fSPoul-Henning Kamp 
1423364c323SKonstantin Belousov 	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
143d07a6d3fSPoul-Henning Kamp 	/*
144d07a6d3fSPoul-Henning Kamp 	 * Dereference the reference we just created.  This assumes
145d07a6d3fSPoul-Henning Kamp 	 * that the object is associated with the vp.
146d07a6d3fSPoul-Henning Kamp 	 */
14789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
148d07a6d3fSPoul-Henning Kamp 	object->ref_count--;
14989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
150d07a6d3fSPoul-Henning Kamp 	vrele(vp);
151d07a6d3fSPoul-Henning Kamp 
152d07a6d3fSPoul-Henning Kamp 	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
153d07a6d3fSPoul-Henning Kamp 
154d07a6d3fSPoul-Henning Kamp 	return (0);
155d07a6d3fSPoul-Henning Kamp }
156d07a6d3fSPoul-Henning Kamp 
1577146d6cbSPoul-Henning Kamp void
1587146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp)
1597146d6cbSPoul-Henning Kamp {
1607146d6cbSPoul-Henning Kamp 	struct vm_object *obj;
1617146d6cbSPoul-Henning Kamp 
1627146d6cbSPoul-Henning Kamp 	obj = vp->v_object;
1637146d6cbSPoul-Henning Kamp 	if (obj == NULL)
1647146d6cbSPoul-Henning Kamp 		return;
16557fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
16689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1672a339d9eSKonstantin Belousov 	umtx_shm_object_terminated(obj);
1687146d6cbSPoul-Henning Kamp 	if (obj->ref_count == 0) {
1697146d6cbSPoul-Henning Kamp 		/*
1707146d6cbSPoul-Henning Kamp 		 * don't double-terminate the object
1717146d6cbSPoul-Henning Kamp 		 */
17290880a1bSKonstantin Belousov 		if ((obj->flags & OBJ_DEAD) == 0) {
1737146d6cbSPoul-Henning Kamp 			vm_object_terminate(obj);
17490880a1bSKonstantin Belousov 		} else {
17590880a1bSKonstantin Belousov 			/*
17690880a1bSKonstantin Belousov 			 * Waiters were already handled during object
17790880a1bSKonstantin Belousov 			 * termination.  The exclusive vnode lock hopefully
17890880a1bSKonstantin Belousov 			 * prevented new waiters from referencing the dying
17990880a1bSKonstantin Belousov 			 * object.
18090880a1bSKonstantin Belousov 			 */
18190880a1bSKonstantin Belousov 			KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0,
18290880a1bSKonstantin Belousov 			    ("OBJ_DISCONNECTWNT set obj %p flags %x",
18390880a1bSKonstantin Belousov 			    obj, obj->flags));
18490880a1bSKonstantin Belousov 			vp->v_object = NULL;
18589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
18690880a1bSKonstantin Belousov 		}
1877146d6cbSPoul-Henning Kamp 	} else {
1887146d6cbSPoul-Henning Kamp 		/*
1897146d6cbSPoul-Henning Kamp 		 * Woe to the process that tries to page now :-).
1907146d6cbSPoul-Henning Kamp 		 */
1917146d6cbSPoul-Henning Kamp 		vm_pager_deallocate(obj);
19289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
1937146d6cbSPoul-Henning Kamp 	}
19490880a1bSKonstantin Belousov 	KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
1957146d6cbSPoul-Henning Kamp }
1967146d6cbSPoul-Henning Kamp 
1977146d6cbSPoul-Henning Kamp 
198df8bae1dSRodney W. Grimes /*
199df8bae1dSRodney W. Grimes  * Allocate (or lookup) pager for a vnode.
200df8bae1dSRodney W. Grimes  * Handle is a vnode pointer.
201990ab7adSAlan Cox  *
202990ab7adSAlan Cox  * MPSAFE
203df8bae1dSRodney W. Grimes  */
20424a1cce3SDavid Greenman vm_object_t
2056cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
2063364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
207df8bae1dSRodney W. Grimes {
20806cb7259SDavid Greenman 	vm_object_t object;
209df8bae1dSRodney W. Grimes 	struct vnode *vp;
210df8bae1dSRodney W. Grimes 
211df8bae1dSRodney W. Grimes 	/*
212df8bae1dSRodney W. Grimes 	 * Pageout to vnode, no can do yet.
213df8bae1dSRodney W. Grimes 	 */
214df8bae1dSRodney W. Grimes 	if (handle == NULL)
215df8bae1dSRodney W. Grimes 		return (NULL);
216df8bae1dSRodney W. Grimes 
217df8bae1dSRodney W. Grimes 	vp = (struct vnode *) handle;
21839d38f93SDavid Greenman 
21939d38f93SDavid Greenman 	/*
22039d38f93SDavid Greenman 	 * If the object is being terminated, wait for it to
22139d38f93SDavid Greenman 	 * go away.
22239d38f93SDavid Greenman 	 */
2232ac78f0eSStephan Uphoff retry:
2241ca58953SAlan Cox 	while ((object = vp->v_object) != NULL) {
22589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
2261ca58953SAlan Cox 		if ((object->flags & OBJ_DEAD) == 0)
2271ca58953SAlan Cox 			break;
22819187819SAlan Cox 		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
2290dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
23024a1cce3SDavid Greenman 	}
2310d94caffSDavid Greenman 
2326ded8427SKonstantin Belousov 	KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
2332be70f79SJohn Dyson 
23424a1cce3SDavid Greenman 	if (object == NULL) {
235df8bae1dSRodney W. Grimes 		/*
2362ac78f0eSStephan Uphoff 		 * Add an object of the appropriate size
237df8bae1dSRodney W. Grimes 		 */
2386cde7a16SDavid Greenman 		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
239bbc0ec52SDavid Greenman 
2406cde7a16SDavid Greenman 		object->un_pager.vnp.vnp_size = size;
24184110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
24226f9a767SRodney W. Grimes 
24324a1cce3SDavid Greenman 		object->handle = handle;
24411be8415SStephan Uphoff 		VI_LOCK(vp);
2452ac78f0eSStephan Uphoff 		if (vp->v_object != NULL) {
2462ac78f0eSStephan Uphoff 			/*
2472ac78f0eSStephan Uphoff 			 * Object has been created while we were sleeping
2482ac78f0eSStephan Uphoff 			 */
24911be8415SStephan Uphoff 			VI_UNLOCK(vp);
2509cddade7SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
2519cddade7SKonstantin Belousov 			KASSERT(object->ref_count == 1,
2529cddade7SKonstantin Belousov 			    ("leaked ref %p %d", object, object->ref_count));
2539cddade7SKonstantin Belousov 			object->type = OBJT_DEAD;
2549cddade7SKonstantin Belousov 			object->ref_count = 0;
2559cddade7SKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
2562ac78f0eSStephan Uphoff 			vm_object_destroy(object);
2572ac78f0eSStephan Uphoff 			goto retry;
258df8bae1dSRodney W. Grimes 		}
2592ac78f0eSStephan Uphoff 		vp->v_object = object;
26011be8415SStephan Uphoff 		VI_UNLOCK(vp);
26111be8415SStephan Uphoff 	} else {
2622ac78f0eSStephan Uphoff 		object->ref_count++;
2633d653db0SAlan Cox #if VM_NRESERVLEVEL > 0
2643d653db0SAlan Cox 		vm_object_color(object, 0);
2653d653db0SAlan Cox #endif
26689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
26711be8415SStephan Uphoff 	}
2686ff51a36SMateusz Guzik 	vrefact(vp);
26924a1cce3SDavid Greenman 	return (object);
270df8bae1dSRodney W. Grimes }
271df8bae1dSRodney W. Grimes 
272658ad5ffSAlan Cox /*
273658ad5ffSAlan Cox  *	The object must be locked.
274658ad5ffSAlan Cox  */
275f708ef1bSPoul-Henning Kamp static void
2767ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object)
27724a1cce3SDavid Greenman {
278b9f180d1SKonstantin Belousov 	struct vnode *vp;
279b9f180d1SKonstantin Belousov 	int refs;
280df8bae1dSRodney W. Grimes 
281b9f180d1SKonstantin Belousov 	vp = object->handle;
28224a1cce3SDavid Greenman 	if (vp == NULL)
28324a1cce3SDavid Greenman 		panic("vnode_pager_dealloc: pager already dealloced");
28424a1cce3SDavid Greenman 
28589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
28666095752SJohn Dyson 	vm_object_pip_wait(object, "vnpdea");
287b9f180d1SKonstantin Belousov 	refs = object->ref_count;
28824a1cce3SDavid Greenman 
28924a1cce3SDavid Greenman 	object->handle = NULL;
29095461b45SJohn Dyson 	object->type = OBJT_DEAD;
29119187819SAlan Cox 	if (object->flags & OBJ_DISCONNECTWNT) {
29219187819SAlan Cox 		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
29319187819SAlan Cox 		wakeup(object);
29419187819SAlan Cox 	}
29557fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
29684110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings > 0) {
29784110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings = 0;
298140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
299b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
300b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
30184110e7eSKonstantin Belousov 	}
302aa2cabb9SDavid Greenman 	vp->v_object = NULL;
303877d24acSKonstantin Belousov 	VOP_UNSET_TEXT(vp);
30489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
305b9f180d1SKonstantin Belousov 	while (refs-- > 0)
306b9f180d1SKonstantin Belousov 		vunref(vp);
30789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
308df8bae1dSRodney W. Grimes }
30926f9a767SRodney W. Grimes 
310f708ef1bSPoul-Henning Kamp static boolean_t
3117ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
3127ebba1f8SGleb Smirnoff     int *after)
313df8bae1dSRodney W. Grimes {
31424a1cce3SDavid Greenman 	struct vnode *vp = object->handle;
31598b0c789SPoul-Henning Kamp 	daddr_t bn;
3163af76890SPoul-Henning Kamp 	int err;
317170db9c6SJohn Dyson 	daddr_t reqblock;
3182c4488fcSJohn Dyson 	int poff;
3192c4488fcSJohn Dyson 	int bsize;
320d63596ceSJohn Dyson 	int pagesperblock, blocksperpage;
321df8bae1dSRodney W. Grimes 
32289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
32324579ca1SMatthew Dillon 	/*
32424579ca1SMatthew Dillon 	 * If no vp or vp is doomed or marked transparent to VM, we do not
32524579ca1SMatthew Dillon 	 * have the page.
32624579ca1SMatthew Dillon 	 */
327b73f64c4SJeff Roberson 	if (vp == NULL || vp->v_iflag & VI_DOOMED)
32847221757SJohn Dyson 		return FALSE;
329df8bae1dSRodney W. Grimes 	/*
330b73f64c4SJeff Roberson 	 * If the offset is beyond end of file we do
3310d94caffSDavid Greenman 	 * not have the page.
332df8bae1dSRodney W. Grimes 	 */
333b73f64c4SJeff Roberson 	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
3344abc71c0SDavid Greenman 		return FALSE;
335df8bae1dSRodney W. Grimes 
336eed2d59bSDavid Greenman 	bsize = vp->v_mount->mnt_stat.f_iosize;
337170db9c6SJohn Dyson 	pagesperblock = bsize / PAGE_SIZE;
338d63596ceSJohn Dyson 	blocksperpage = 0;
339d63596ceSJohn Dyson 	if (pagesperblock > 0) {
340a316d390SJohn Dyson 		reqblock = pindex / pagesperblock;
341d63596ceSJohn Dyson 	} else {
342d63596ceSJohn Dyson 		blocksperpage = (PAGE_SIZE / bsize);
343d63596ceSJohn Dyson 		reqblock = pindex * blocksperpage;
344d63596ceSJohn Dyson 	}
34589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
346ec794849SPoul-Henning Kamp 	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
34789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
3480d94caffSDavid Greenman 	if (err)
34924a1cce3SDavid Greenman 		return TRUE;
3506eab77f2SJohn Dyson 	if (bn == -1)
351ced399eeSJohn Dyson 		return FALSE;
352d63596ceSJohn Dyson 	if (pagesperblock > 0) {
353a316d390SJohn Dyson 		poff = pindex - (reqblock * pagesperblock);
354170db9c6SJohn Dyson 		if (before) {
355170db9c6SJohn Dyson 			*before *= pagesperblock;
356170db9c6SJohn Dyson 			*before += poff;
357170db9c6SJohn Dyson 		}
358170db9c6SJohn Dyson 		if (after) {
35984d31376SGleb Smirnoff 			/*
36084d31376SGleb Smirnoff 			 * The BMAP vop can report a partial block in the
361d2596d17SGleb Smirnoff 			 * 'after', but must not report blocks after EOF.
36284d31376SGleb Smirnoff 			 * Assert the latter, and truncate 'after' in case
36384d31376SGleb Smirnoff 			 * of the former.
36484d31376SGleb Smirnoff 			 */
365d2596d17SGleb Smirnoff 			KASSERT((reqblock + *after) * pagesperblock <
366d2596d17SGleb Smirnoff 			    roundup2(object->size, pagesperblock),
36784d31376SGleb Smirnoff 			    ("%s: reqblock %jd after %d size %ju", __func__,
36884d31376SGleb Smirnoff 			    (intmax_t )reqblock, *after,
36984d31376SGleb Smirnoff 			    (uintmax_t )object->size));
370170db9c6SJohn Dyson 			*after *= pagesperblock;
37184d31376SGleb Smirnoff 			*after += pagesperblock - (poff + 1);
37284d31376SGleb Smirnoff 			if (pindex + *after >= object->size)
37384d31376SGleb Smirnoff 				*after = object->size - 1 - pindex;
374170db9c6SJohn Dyson 		}
375d63596ceSJohn Dyson 	} else {
376d63596ceSJohn Dyson 		if (before) {
377d63596ceSJohn Dyson 			*before /= blocksperpage;
378d63596ceSJohn Dyson 		}
379d63596ceSJohn Dyson 
380d63596ceSJohn Dyson 		if (after) {
381d63596ceSJohn Dyson 			*after /= blocksperpage;
382d63596ceSJohn Dyson 		}
383d63596ceSJohn Dyson 	}
384ced399eeSJohn Dyson 	return TRUE;
385df8bae1dSRodney W. Grimes }
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes /*
388df8bae1dSRodney W. Grimes  * Lets the VM system know about a change in size for a file.
38924a1cce3SDavid Greenman  * We adjust our own internal size and flush any cached pages in
390df8bae1dSRodney W. Grimes  * the associated object that are affected by the size change.
391df8bae1dSRodney W. Grimes  *
392df8bae1dSRodney W. Grimes  * Note: this routine may be invoked as a result of a pager put
393df8bae1dSRodney W. Grimes  * operation (possibly at object termination time), so we must be careful.
394df8bae1dSRodney W. Grimes  */
395df8bae1dSRodney W. Grimes void
3967ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
397df8bae1dSRodney W. Grimes {
3982a8f9ab5SAlan Cox 	vm_object_t object;
3992a8f9ab5SAlan Cox 	vm_page_t m;
400c576d121SLuoqi Chen 	vm_pindex_t nobjsize;
401df8bae1dSRodney W. Grimes 
4022a8f9ab5SAlan Cox 	if ((object = vp->v_object) == NULL)
403df8bae1dSRodney W. Grimes 		return;
404cb61d698SKonstantin Belousov /* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
40589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
4069b8851faSKonstantin Belousov 	if (object->type == OBJT_DEAD) {
4079b8851faSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
4089b8851faSKonstantin Belousov 		return;
4099b8851faSKonstantin Belousov 	}
4109b8851faSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE,
4119b8851faSKonstantin Belousov 	    ("not vnode-backed object %p", object));
4122a8f9ab5SAlan Cox 	if (nsize == object->un_pager.vnp.vnp_size) {
413df8bae1dSRodney W. Grimes 		/*
414df8bae1dSRodney W. Grimes 		 * Hasn't changed size
415df8bae1dSRodney W. Grimes 		 */
41689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
417df8bae1dSRodney W. Grimes 		return;
4182a8f9ab5SAlan Cox 	}
419c576d121SLuoqi Chen 	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
4202a8f9ab5SAlan Cox 	if (nsize < object->un_pager.vnp.vnp_size) {
421df8bae1dSRodney W. Grimes 		/*
422bbc0ec52SDavid Greenman 		 * File has shrunk. Toss any cached pages beyond the new EOF.
423df8bae1dSRodney W. Grimes 		 */
4242a8f9ab5SAlan Cox 		if (nobjsize < object->size)
425c576d121SLuoqi Chen 			vm_object_page_remove(object, nobjsize, object->size,
4266bbee8e2SAlan Cox 			    0);
427bbc0ec52SDavid Greenman 		/*
428bbc0ec52SDavid Greenman 		 * this gets rid of garbage at the end of a page that is now
4293ebeaf59SMatthew Dillon 		 * only partially backed by the vnode.
4303ebeaf59SMatthew Dillon 		 *
4313ebeaf59SMatthew Dillon 		 * XXX for some reason (I don't know yet), if we take a
4323ebeaf59SMatthew Dillon 		 * completely invalid page and mark it partially valid
4333ebeaf59SMatthew Dillon 		 * it can screw up NFS reads, so we don't allow the case.
434bbc0ec52SDavid Greenman 		 */
4352a8f9ab5SAlan Cox 		if ((nsize & PAGE_MASK) &&
4361b26eb10SAlan Cox 		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
4371b26eb10SAlan Cox 		    m->valid != 0) {
4382b6b0df7SMatthew Dillon 			int base = (int)nsize & PAGE_MASK;
4392b6b0df7SMatthew Dillon 			int size = PAGE_SIZE - base;
4402b6b0df7SMatthew Dillon 
4412b6b0df7SMatthew Dillon 			/*
4422b6b0df7SMatthew Dillon 			 * Clear out partial-page garbage in case
4432b6b0df7SMatthew Dillon 			 * the page has been mapped.
4442b6b0df7SMatthew Dillon 			 */
445fff6062aSAlan Cox 			pmap_zero_page_area(m, base, size);
4462b6b0df7SMatthew Dillon 
4472b6b0df7SMatthew Dillon 			/*
4483c33df62SAlan Cox 			 * Update the valid bits to reflect the blocks that
4493c33df62SAlan Cox 			 * have been zeroed.  Some of these valid bits may
4503c33df62SAlan Cox 			 * have already been set.
4513c33df62SAlan Cox 			 */
452dc874f98SKonstantin Belousov 			vm_page_set_valid_range(m, base, size);
4533c33df62SAlan Cox 
4543c33df62SAlan Cox 			/*
4553c33df62SAlan Cox 			 * Round "base" to the next block boundary so that the
4563c33df62SAlan Cox 			 * dirty bit for a partially zeroed block is not
4573c33df62SAlan Cox 			 * cleared.
4583c33df62SAlan Cox 			 */
4593c33df62SAlan Cox 			base = roundup2(base, DEV_BSIZE);
4603c33df62SAlan Cox 
4613c33df62SAlan Cox 			/*
4623c33df62SAlan Cox 			 * Clear out partial-page dirty bits.
4633ebeaf59SMatthew Dillon 			 *
4643ebeaf59SMatthew Dillon 			 * note that we do not clear out the valid
4653ebeaf59SMatthew Dillon 			 * bits.  This would prevent bogus_page
4663ebeaf59SMatthew Dillon 			 * replacement from working properly.
4672b6b0df7SMatthew Dillon 			 */
4683c33df62SAlan Cox 			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
469bbc0ec52SDavid Greenman 		}
470bbc0ec52SDavid Greenman 	}
471a316d390SJohn Dyson 	object->un_pager.vnp.vnp_size = nsize;
472c576d121SLuoqi Chen 	object->size = nobjsize;
47389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
474df8bae1dSRodney W. Grimes }
475df8bae1dSRodney W. Grimes 
47626f9a767SRodney W. Grimes /*
47726f9a767SRodney W. Grimes  * calculate the linear (byte) disk address of specified virtual
47826f9a767SRodney W. Grimes  * file address
47926f9a767SRodney W. Grimes  */
480bff76343SAlan Cox static int
481bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
482bff76343SAlan Cox     int *run)
48326f9a767SRodney W. Grimes {
48426f9a767SRodney W. Grimes 	int bsize;
48526f9a767SRodney W. Grimes 	int err;
486a316d390SJohn Dyson 	daddr_t vblock;
487f3aad9a6SBjoern A. Zeeb 	daddr_t voffset;
48826f9a767SRodney W. Grimes 
4892ad036b6SAlan Cox 	if (address < 0)
4900d94caffSDavid Greenman 		return -1;
4910d94caffSDavid Greenman 
492b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
4932c4488fcSJohn Dyson 		return -1;
4942c4488fcSJohn Dyson 
49526f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
49626f9a767SRodney W. Grimes 	vblock = address / bsize;
49726f9a767SRodney W. Grimes 	voffset = address % bsize;
49826f9a767SRodney W. Grimes 
499bff76343SAlan Cox 	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
500bff76343SAlan Cox 	if (err == 0) {
501bff76343SAlan Cox 		if (*rtaddress != -1)
502bff76343SAlan Cox 			*rtaddress += voffset / DEV_BSIZE;
503efc68ce1SDavid Greenman 		if (run) {
504efc68ce1SDavid Greenman 			*run += 1;
505efc68ce1SDavid Greenman 			*run *= bsize/PAGE_SIZE;
506efc68ce1SDavid Greenman 			*run -= voffset/PAGE_SIZE;
507efc68ce1SDavid Greenman 		}
508efc68ce1SDavid Greenman 	}
50926f9a767SRodney W. Grimes 
510bff76343SAlan Cox 	return (err);
51126f9a767SRodney W. Grimes }
51226f9a767SRodney W. Grimes 
51326f9a767SRodney W. Grimes /*
51426f9a767SRodney W. Grimes  * small block filesystem vnode pager input
51526f9a767SRodney W. Grimes  */
516f708ef1bSPoul-Henning Kamp static int
5177ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
51826f9a767SRodney W. Grimes {
5199c83534dSPoul-Henning Kamp 	struct vnode *vp;
5209c83534dSPoul-Henning Kamp 	struct bufobj *bo;
52126f9a767SRodney W. Grimes 	struct buf *bp;
5229e0ddbd0SAlan Cox 	struct sf_buf *sf;
523f3aad9a6SBjoern A. Zeeb 	daddr_t fileaddr;
52426f9a767SRodney W. Grimes 	vm_offset_t bsize;
525561cc9fcSKonstantin Belousov 	vm_page_bits_t bits;
526561cc9fcSKonstantin Belousov 	int error, i;
52726f9a767SRodney W. Grimes 
528561cc9fcSKonstantin Belousov 	error = 0;
52924a1cce3SDavid Greenman 	vp = object->handle;
530b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
5312c4488fcSJohn Dyson 		return VM_PAGER_BAD;
5322c4488fcSJohn Dyson 
53326f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
5340bdb7528SDavid Greenman 
5359c83534dSPoul-Henning Kamp 	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
53626f9a767SRodney W. Grimes 
5379e0ddbd0SAlan Cox 	sf = sf_buf_alloc(m, 0);
53826f9a767SRodney W. Grimes 
53926f9a767SRodney W. Grimes 	for (i = 0; i < PAGE_SIZE / bsize; i++) {
54033c67741SMatthew Dillon 		vm_ooffset_t address;
541bbc0ec52SDavid Greenman 
5420d53a17bSAlan Cox 		bits = vm_page_bits(i * bsize, bsize);
5430d53a17bSAlan Cox 		if (m->valid & bits)
54426f9a767SRodney W. Grimes 			continue;
54526f9a767SRodney W. Grimes 
54633c67741SMatthew Dillon 		address = IDX_TO_OFF(m->pindex) + i * bsize;
54733c67741SMatthew Dillon 		if (address >= object->un_pager.vnp.vnp_size) {
54833c67741SMatthew Dillon 			fileaddr = -1;
54933c67741SMatthew Dillon 		} else {
550bff76343SAlan Cox 			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
551bff76343SAlan Cox 			if (error)
552bff76343SAlan Cox 				break;
55333c67741SMatthew Dillon 		}
55426f9a767SRodney W. Grimes 		if (fileaddr != -1) {
5551c7c3c6aSMatthew Dillon 			bp = getpbuf(&vnode_pbuf_freecnt);
55626f9a767SRodney W. Grimes 
55726f9a767SRodney W. Grimes 			/* build a minimal buffer header */
55821144e3bSPoul-Henning Kamp 			bp->b_iocmd = BIO_READ;
5596a4b5823SPoul-Henning Kamp 			bp->b_iodone = bdone;
560bd78ceceSJohn Baldwin 			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
561bd78ceceSJohn Baldwin 			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
562a854ed98SJohn Baldwin 			bp->b_rcred = crhold(curthread->td_ucred);
563a854ed98SJohn Baldwin 			bp->b_wcred = crhold(curthread->td_ucred);
5649e0ddbd0SAlan Cox 			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
565187f0071SDavid Greenman 			bp->b_blkno = fileaddr;
5669c83534dSPoul-Henning Kamp 			pbgetbo(bo, bp);
5671faacf5dSKirk McKusick 			bp->b_vp = vp;
56826f9a767SRodney W. Grimes 			bp->b_bcount = bsize;
56926f9a767SRodney W. Grimes 			bp->b_bufsize = bsize;
5702b6b0df7SMatthew Dillon 			bp->b_runningbufspace = bp->b_bufsize;
5715bd65606SJohn Baldwin 			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
57226f9a767SRodney W. Grimes 
57326f9a767SRodney W. Grimes 			/* do the input */
5742c18019fSPoul-Henning Kamp 			bp->b_iooffset = dbtob(bp->b_blkno);
575b792bebeSPoul-Henning Kamp 			bstrategy(bp);
57626f9a767SRodney W. Grimes 
5776a4b5823SPoul-Henning Kamp 			bwait(bp, PVM, "vnsrd");
5786a4b5823SPoul-Henning Kamp 
579c244d2deSPoul-Henning Kamp 			if ((bp->b_ioflags & BIO_ERROR) != 0)
58026f9a767SRodney W. Grimes 				error = EIO;
58126f9a767SRodney W. Grimes 
58226f9a767SRodney W. Grimes 			/*
58326f9a767SRodney W. Grimes 			 * free the buffer header back to the swap buffer pool
58426f9a767SRodney W. Grimes 			 */
5851faacf5dSKirk McKusick 			bp->b_vp = NULL;
5869c83534dSPoul-Henning Kamp 			pbrelbo(bp);
5871c7c3c6aSMatthew Dillon 			relpbuf(bp, &vnode_pbuf_freecnt);
58826f9a767SRodney W. Grimes 			if (error)
58926f9a767SRodney W. Grimes 				break;
5900d53a17bSAlan Cox 		} else
5919e0ddbd0SAlan Cox 			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
5920d53a17bSAlan Cox 		KASSERT((m->dirty & bits) == 0,
5930d53a17bSAlan Cox 		    ("vnode_pager_input_smlfs: page %p is dirty", m));
59489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5950d53a17bSAlan Cox 		m->valid |= bits;
59689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
59726f9a767SRodney W. Grimes 	}
5989e0ddbd0SAlan Cox 	sf_buf_free(sf);
59926f9a767SRodney W. Grimes 	if (error) {
600a83c285cSDavid Greenman 		return VM_PAGER_ERROR;
60126f9a767SRodney W. Grimes 	}
60226f9a767SRodney W. Grimes 	return VM_PAGER_OK;
60326f9a767SRodney W. Grimes }
60426f9a767SRodney W. Grimes 
60526f9a767SRodney W. Grimes /*
606475e8cc3SPoul-Henning Kamp  * old style vnode pager input routine
60726f9a767SRodney W. Grimes  */
608f708ef1bSPoul-Henning Kamp static int
6097ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m)
61026f9a767SRodney W. Grimes {
611df8bae1dSRodney W. Grimes 	struct uio auio;
612df8bae1dSRodney W. Grimes 	struct iovec aiov;
61326f9a767SRodney W. Grimes 	int error;
61426f9a767SRodney W. Grimes 	int size;
6159e0ddbd0SAlan Cox 	struct sf_buf *sf;
616342a1480SJohn Baldwin 	struct vnode *vp;
617df8bae1dSRodney W. Grimes 
61889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
61926f9a767SRodney W. Grimes 	error = 0;
620bbc0ec52SDavid Greenman 
621df8bae1dSRodney W. Grimes 	/*
62226f9a767SRodney W. Grimes 	 * Return failure if beyond current EOF
62326f9a767SRodney W. Grimes 	 */
624a316d390SJohn Dyson 	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
62526f9a767SRodney W. Grimes 		return VM_PAGER_BAD;
62626f9a767SRodney W. Grimes 	} else {
62726f9a767SRodney W. Grimes 		size = PAGE_SIZE;
628a316d390SJohn Dyson 		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
629a316d390SJohn Dyson 			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
63052051abcSAlan Cox 		vp = object->handle;
63189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
6320bdb7528SDavid Greenman 
63326f9a767SRodney W. Grimes 		/*
634df8bae1dSRodney W. Grimes 		 * Allocate a kernel virtual address and initialize so that
635df8bae1dSRodney W. Grimes 		 * we can use VOP_READ/WRITE routines.
636df8bae1dSRodney W. Grimes 		 */
6379e0ddbd0SAlan Cox 		sf = sf_buf_alloc(m, 0);
6380bdb7528SDavid Greenman 
6399e0ddbd0SAlan Cox 		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
640df8bae1dSRodney W. Grimes 		aiov.iov_len = size;
641df8bae1dSRodney W. Grimes 		auio.uio_iov = &aiov;
642df8bae1dSRodney W. Grimes 		auio.uio_iovcnt = 1;
643a316d390SJohn Dyson 		auio.uio_offset = IDX_TO_OFF(m->pindex);
644df8bae1dSRodney W. Grimes 		auio.uio_segflg = UIO_SYSSPACE;
64526f9a767SRodney W. Grimes 		auio.uio_rw = UIO_READ;
646df8bae1dSRodney W. Grimes 		auio.uio_resid = size;
647b40ce416SJulian Elischer 		auio.uio_td = curthread;
64826f9a767SRodney W. Grimes 
649a854ed98SJohn Baldwin 		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
650df8bae1dSRodney W. Grimes 		if (!error) {
65154d92145SMatthew Dillon 			int count = size - auio.uio_resid;
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes 			if (count == 0)
654df8bae1dSRodney W. Grimes 				error = EINVAL;
65526f9a767SRodney W. Grimes 			else if (count != PAGE_SIZE)
6569e0ddbd0SAlan Cox 				bzero((caddr_t)sf_buf_kva(sf) + count,
6579e0ddbd0SAlan Cox 				    PAGE_SIZE - count);
658df8bae1dSRodney W. Grimes 		}
6599e0ddbd0SAlan Cox 		sf_buf_free(sf);
6601b26eb10SAlan Cox 
66189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
662df8bae1dSRodney W. Grimes 	}
6630d53a17bSAlan Cox 	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
6646e3a3f38SRobert V. Baron 	if (!error)
6656e3a3f38SRobert V. Baron 		m->valid = VM_PAGE_BITS_ALL;
666a83c285cSDavid Greenman 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
66726f9a767SRodney W. Grimes }
66826f9a767SRodney W. Grimes 
66926f9a767SRodney W. Grimes /*
67026f9a767SRodney W. Grimes  * generic vnode pager input routine
67126f9a767SRodney W. Grimes  */
672170db9c6SJohn Dyson 
673ce75f2c3SMike Smith /*
67423955314SAlfred Perlstein  * Local media VFS's that do not implement their own VOP_GETPAGES
67547e151ddSRobert Drehmel  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
67647e151ddSRobert Drehmel  * to implement the previous behaviour.
677ce75f2c3SMike Smith  *
678ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
679ce75f2c3SMike Smith  * backing vp's VOP_GETPAGES.
680ce75f2c3SMike Smith  */
681f708ef1bSPoul-Henning Kamp static int
682b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
683b0cd2017SGleb Smirnoff     int *rahead)
68424a1cce3SDavid Greenman {
685170db9c6SJohn Dyson 	struct vnode *vp;
686b0cd2017SGleb Smirnoff 	int rtval;
68795e5e988SJohn Dyson 
688170db9c6SJohn Dyson 	vp = object->handle;
68989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
690b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
69123955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
69223955314SAlfred Perlstein 	    ("vnode_pager: FS getpages not implemented\n"));
69389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
694170db9c6SJohn Dyson 	return rtval;
695170db9c6SJohn Dyson }
696170db9c6SJohn Dyson 
69790effb23SGleb Smirnoff static int
69890effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
699b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
70090effb23SGleb Smirnoff {
70190effb23SGleb Smirnoff 	struct vnode *vp;
70290effb23SGleb Smirnoff 	int rtval;
70390effb23SGleb Smirnoff 
70490effb23SGleb Smirnoff 	vp = object->handle;
70590effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
706b0cd2017SGleb Smirnoff 	rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
70790effb23SGleb Smirnoff 	KASSERT(rtval != EOPNOTSUPP,
70890effb23SGleb Smirnoff 	    ("vnode_pager: FS getpages_async not implemented\n"));
70990effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
71090effb23SGleb Smirnoff 	return (rtval);
71190effb23SGleb Smirnoff }
71290effb23SGleb Smirnoff 
713ce75f2c3SMike Smith /*
71490effb23SGleb Smirnoff  * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
71590effb23SGleb Smirnoff  * local filesystems, where partially valid pages can only occur at
71690effb23SGleb Smirnoff  * the end of file.
717d15b55c5SKonstantin Belousov  */
718d15b55c5SKonstantin Belousov int
719d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap)
720d15b55c5SKonstantin Belousov {
72190effb23SGleb Smirnoff 
722b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
723b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
72490effb23SGleb Smirnoff }
72590effb23SGleb Smirnoff 
72690effb23SGleb Smirnoff int
72790effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
72890effb23SGleb Smirnoff {
72990effb23SGleb Smirnoff 
730b0cd2017SGleb Smirnoff 	return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
731b0cd2017SGleb Smirnoff 	    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
732d15b55c5SKonstantin Belousov }
733d15b55c5SKonstantin Belousov 
734d15b55c5SKonstantin Belousov /*
735ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
736ce75f2c3SMike Smith  * own vnodes if they fail to implement VOP_GETPAGES.
737ce75f2c3SMike Smith  */
738ce75f2c3SMike Smith int
739b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
740b0cd2017SGleb Smirnoff     int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
741170db9c6SJohn Dyson {
742ce75f2c3SMike Smith 	vm_object_t object;
7439c83534dSPoul-Henning Kamp 	struct bufobj *bo;
7440bdb7528SDavid Greenman 	struct buf *bp;
745b0cd2017SGleb Smirnoff 	off_t foff;
746e48b82bdSGleb Smirnoff #ifdef INVARIANTS
747e48b82bdSGleb Smirnoff 	off_t blkno0;
748e48b82bdSGleb Smirnoff #endif
749b0cd2017SGleb Smirnoff 	int bsize, pagesperblock, *freecnt;
750b0cd2017SGleb Smirnoff 	int error, before, after, rbehind, rahead, poff, i;
751b0cd2017SGleb Smirnoff 	int bytecount, secmask;
752ce75f2c3SMike Smith 
7539c83534dSPoul-Henning Kamp 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
754b0cd2017SGleb Smirnoff 	    ("%s does not support devices", __func__));
755b0cd2017SGleb Smirnoff 
756b73f64c4SJeff Roberson 	if (vp->v_iflag & VI_DOOMED)
757eac91e32SKonstantin Belousov 		return (VM_PAGER_BAD);
7582c4488fcSJohn Dyson 
759eac91e32SKonstantin Belousov 	object = vp->v_object;
760b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(m[0]->pindex);
76126f9a767SRodney W. Grimes 	bsize = vp->v_mount->mnt_stat.f_iosize;
762b0cd2017SGleb Smirnoff 	pagesperblock = bsize / PAGE_SIZE;
763b0cd2017SGleb Smirnoff 
764b0cd2017SGleb Smirnoff 	KASSERT(foff < object->un_pager.vnp.vnp_size,
765b0cd2017SGleb Smirnoff 	    ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
766b0cd2017SGleb Smirnoff 	KASSERT(count <= sizeof(bp->b_pages),
767b0cd2017SGleb Smirnoff 	    ("%s: requested %d pages", __func__, count));
768b0cd2017SGleb Smirnoff 
769b0cd2017SGleb Smirnoff 	/*
770b0cd2017SGleb Smirnoff 	 * The last page has valid blocks.  Invalid part can only
771b0cd2017SGleb Smirnoff 	 * exist at the end of file, and the page is made fully valid
772b0cd2017SGleb Smirnoff 	 * by zeroing in vm_pager_get_pages().
773b0cd2017SGleb Smirnoff 	 */
774b0cd2017SGleb Smirnoff 	if (m[count - 1]->valid != 0 && --count == 0) {
775b0cd2017SGleb Smirnoff 		if (iodone != NULL)
776b0cd2017SGleb Smirnoff 			iodone(arg, m, 1, 0);
777b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
778b0cd2017SGleb Smirnoff 	}
779bbc0ec52SDavid Greenman 
78041c895a8SGleb Smirnoff 	/*
78141c895a8SGleb Smirnoff 	 * Synchronous and asynchronous paging operations use different
78241c895a8SGleb Smirnoff 	 * free pbuf counters.  This is done to avoid asynchronous requests
78341c895a8SGleb Smirnoff 	 * to consume all pbufs.
78441c895a8SGleb Smirnoff 	 * Allocate the pbuf at the very beginning of the function, so that
78541c895a8SGleb Smirnoff 	 * if we are low on certain kind of pbufs don't even proceed to BMAP,
78641c895a8SGleb Smirnoff 	 * but sleep.
78741c895a8SGleb Smirnoff 	 */
78873e9030eSGleb Smirnoff 	freecnt = iodone != NULL ?
78973e9030eSGleb Smirnoff 	    &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
79073e9030eSGleb Smirnoff 	bp = getpbuf(freecnt);
79173e9030eSGleb Smirnoff 
79226f9a767SRodney W. Grimes 	/*
793e122dfc1SGleb Smirnoff 	 * Get the underlying device blocks for the file with VOP_BMAP().
794e122dfc1SGleb Smirnoff 	 * If the file system doesn't support VOP_BMAP, use old way of
795e122dfc1SGleb Smirnoff 	 * getting pages via VOP_READ.
79626f9a767SRodney W. Grimes 	 */
797b0cd2017SGleb Smirnoff 	error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
7981de11f1aSAlan Cox 	if (error == EOPNOTSUPP) {
79973e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
80089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
801b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
802b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodein);
803b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodepgsin);
804b0cd2017SGleb Smirnoff 			error = vnode_pager_input_old(object, m[i]);
805b0cd2017SGleb Smirnoff 			if (error)
806b0cd2017SGleb Smirnoff 				break;
807b0cd2017SGleb Smirnoff 		}
80889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
80952051abcSAlan Cox 		return (error);
8101de11f1aSAlan Cox 	} else if (error != 0) {
81173e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
8121de11f1aSAlan Cox 		return (VM_PAGER_ERROR);
813b0cd2017SGleb Smirnoff 	}
814bbc0ec52SDavid Greenman 
81526f9a767SRodney W. Grimes 	/*
816b0cd2017SGleb Smirnoff 	 * If the file system supports BMAP, but blocksize is smaller
817b0cd2017SGleb Smirnoff 	 * than a page size, then use special small filesystem code.
81826f9a767SRodney W. Grimes 	 */
819b0cd2017SGleb Smirnoff 	if (pagesperblock == 0) {
820ff64a90eSKonstantin Belousov 		relpbuf(bp, freecnt);
821b0cd2017SGleb Smirnoff 		for (i = 0; i < count; i++) {
822b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodein);
823b4b70819SAttilio Rao 			PCPU_INC(cnt.v_vnodepgsin);
824b0cd2017SGleb Smirnoff 			error = vnode_pager_input_smlfs(object, m[i]);
825b0cd2017SGleb Smirnoff 			if (error)
826b0cd2017SGleb Smirnoff 				break;
827b0cd2017SGleb Smirnoff 		}
828b0cd2017SGleb Smirnoff 		return (error);
82926f9a767SRodney W. Grimes 	}
8308d17e694SJulian Elischer 
83126f9a767SRodney W. Grimes 	/*
832b0cd2017SGleb Smirnoff 	 * A sparse file can be encountered only for a single page request,
833763df3ecSPedro F. Giffuni 	 * which may not be preceded by call to vm_pager_haspage().
834a7fecb4dSAlan Cox 	 */
835b0cd2017SGleb Smirnoff 	if (bp->b_blkno == -1) {
836b0cd2017SGleb Smirnoff 		KASSERT(count == 1,
837b0cd2017SGleb Smirnoff 		    ("%s: array[%d] request to a sparse file %p", __func__,
838b0cd2017SGleb Smirnoff 		    count, vp));
83973e9030eSGleb Smirnoff 		relpbuf(bp, freecnt);
840b0cd2017SGleb Smirnoff 		pmap_zero_page(m[0]);
841b0cd2017SGleb Smirnoff 		KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
842b0cd2017SGleb Smirnoff 		    __func__, m[0]));
843a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
844b0cd2017SGleb Smirnoff 		m[0]->valid = VM_PAGE_BITS_ALL;
84589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
846f4f83da0SAlan Cox 		return (VM_PAGER_OK);
847b0cd2017SGleb Smirnoff 	}
848b0cd2017SGleb Smirnoff 
849e48b82bdSGleb Smirnoff #ifdef INVARIANTS
850e48b82bdSGleb Smirnoff 	blkno0 = bp->b_blkno;
851e48b82bdSGleb Smirnoff #endif
852b0cd2017SGleb Smirnoff 	bp->b_blkno += (foff % bsize) / DEV_BSIZE;
853b0cd2017SGleb Smirnoff 
854b0cd2017SGleb Smirnoff 	/* Recalculate blocks available after/before to pages. */
855b0cd2017SGleb Smirnoff 	poff = (foff % bsize) / PAGE_SIZE;
856b0cd2017SGleb Smirnoff 	before *= pagesperblock;
857b0cd2017SGleb Smirnoff 	before += poff;
858b0cd2017SGleb Smirnoff 	after *= pagesperblock;
859b0cd2017SGleb Smirnoff 	after += pagesperblock - (poff + 1);
860b0cd2017SGleb Smirnoff 	if (m[0]->pindex + after >= object->size)
861b0cd2017SGleb Smirnoff 		after = object->size - 1 - m[0]->pindex;
862b0cd2017SGleb Smirnoff 	KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
863b0cd2017SGleb Smirnoff 	    __func__, count, after + 1));
864b0cd2017SGleb Smirnoff 	after -= count - 1;
865b0cd2017SGleb Smirnoff 
866b0cd2017SGleb Smirnoff 	/* Trim requested rbehind/rahead to possible values. */
867b0cd2017SGleb Smirnoff 	rbehind = a_rbehind ? *a_rbehind : 0;
868b0cd2017SGleb Smirnoff 	rahead = a_rahead ? *a_rahead : 0;
869b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, before);
870b0cd2017SGleb Smirnoff 	rbehind = min(rbehind, m[0]->pindex);
871b0cd2017SGleb Smirnoff 	rahead = min(rahead, after);
872b0cd2017SGleb Smirnoff 	rahead = min(rahead, object->size - m[count - 1]->pindex);
873e48b82bdSGleb Smirnoff 	/*
874e48b82bdSGleb Smirnoff 	 * Check that total amount of pages fit into buf.  Trim rbehind and
875e48b82bdSGleb Smirnoff 	 * rahead evenly if not.
876e48b82bdSGleb Smirnoff 	 */
877e48b82bdSGleb Smirnoff 	if (rbehind + rahead + count > nitems(bp->b_pages)) {
878e48b82bdSGleb Smirnoff 		int trim, sum;
879e48b82bdSGleb Smirnoff 
880e48b82bdSGleb Smirnoff 		trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
881e48b82bdSGleb Smirnoff 		sum = rbehind + rahead;
882e48b82bdSGleb Smirnoff 		if (rbehind == before) {
883e48b82bdSGleb Smirnoff 			/* Roundup rbehind trim to block size. */
884e48b82bdSGleb Smirnoff 			rbehind -= roundup(trim * rbehind / sum, pagesperblock);
885e48b82bdSGleb Smirnoff 			if (rbehind < 0)
886e48b82bdSGleb Smirnoff 				rbehind = 0;
887e48b82bdSGleb Smirnoff 		} else
888e48b82bdSGleb Smirnoff 			rbehind -= trim * rbehind / sum;
889e48b82bdSGleb Smirnoff 		rahead -= trim * rahead / sum;
890e48b82bdSGleb Smirnoff 	}
891e48b82bdSGleb Smirnoff 	KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
892b0cd2017SGleb Smirnoff 	    ("%s: behind %d ahead %d count %d", __func__,
893b0cd2017SGleb Smirnoff 	    rbehind, rahead, count));
894b0cd2017SGleb Smirnoff 
895b0cd2017SGleb Smirnoff 	/*
896b0cd2017SGleb Smirnoff 	 * Fill in the bp->b_pages[] array with requested and optional
897b0cd2017SGleb Smirnoff 	 * read behind or read ahead pages.  Read behind pages are looked
898b0cd2017SGleb Smirnoff 	 * up in a backward direction, down to a first cached page.  Same
899b0cd2017SGleb Smirnoff 	 * for read ahead pages, but there is no need to shift the array
900b0cd2017SGleb Smirnoff 	 * in case of encountering a cached page.
901b0cd2017SGleb Smirnoff 	 */
902b0cd2017SGleb Smirnoff 	i = bp->b_npages = 0;
903b0cd2017SGleb Smirnoff 	if (rbehind) {
904b0cd2017SGleb Smirnoff 		vm_pindex_t startpindex, tpindex;
905b0cd2017SGleb Smirnoff 		vm_page_t p;
906b0cd2017SGleb Smirnoff 
907a7fecb4dSAlan Cox 		VM_OBJECT_WLOCK(object);
908b0cd2017SGleb Smirnoff 		startpindex = m[0]->pindex - rbehind;
909b0cd2017SGleb Smirnoff 		if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
910b0cd2017SGleb Smirnoff 		    p->pindex >= startpindex)
911b0cd2017SGleb Smirnoff 			startpindex = p->pindex + 1;
912b0cd2017SGleb Smirnoff 
913b0cd2017SGleb Smirnoff 		/* tpindex is unsigned; beware of numeric underflow. */
914b0cd2017SGleb Smirnoff 		for (tpindex = m[0]->pindex - 1;
915b0cd2017SGleb Smirnoff 		    tpindex >= startpindex && tpindex < m[0]->pindex;
916b0cd2017SGleb Smirnoff 		    tpindex--, i++) {
9177667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
918b0cd2017SGleb Smirnoff 			if (p == NULL) {
919b0cd2017SGleb Smirnoff 				/* Shift the array. */
920b0cd2017SGleb Smirnoff 				for (int j = 0; j < i; j++)
921b0cd2017SGleb Smirnoff 					bp->b_pages[j] = bp->b_pages[j +
922b0cd2017SGleb Smirnoff 					    tpindex + 1 - startpindex];
923b0cd2017SGleb Smirnoff 				break;
924b0cd2017SGleb Smirnoff 			}
925b0cd2017SGleb Smirnoff 			bp->b_pages[tpindex - startpindex] = p;
926a7fecb4dSAlan Cox 		}
9270bdb7528SDavid Greenman 
928b0cd2017SGleb Smirnoff 		bp->b_pgbefore = i;
929b0cd2017SGleb Smirnoff 		bp->b_npages += i;
930b0cd2017SGleb Smirnoff 		bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
931b0cd2017SGleb Smirnoff 	} else
932b0cd2017SGleb Smirnoff 		bp->b_pgbefore = 0;
933b0cd2017SGleb Smirnoff 
934b0cd2017SGleb Smirnoff 	/* Requested pages. */
935b0cd2017SGleb Smirnoff 	for (int j = 0; j < count; j++, i++)
936b0cd2017SGleb Smirnoff 		bp->b_pages[i] = m[j];
937b0cd2017SGleb Smirnoff 	bp->b_npages += count;
938b0cd2017SGleb Smirnoff 
939b0cd2017SGleb Smirnoff 	if (rahead) {
940b0cd2017SGleb Smirnoff 		vm_pindex_t endpindex, tpindex;
941b0cd2017SGleb Smirnoff 		vm_page_t p;
942b0cd2017SGleb Smirnoff 
943b0cd2017SGleb Smirnoff 		if (!VM_OBJECT_WOWNED(object))
944eac91e32SKonstantin Belousov 			VM_OBJECT_WLOCK(object);
945b0cd2017SGleb Smirnoff 		endpindex = m[count - 1]->pindex + rahead + 1;
946b0cd2017SGleb Smirnoff 		if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
947b0cd2017SGleb Smirnoff 		    p->pindex < endpindex)
948b0cd2017SGleb Smirnoff 			endpindex = p->pindex;
949b0cd2017SGleb Smirnoff 		if (endpindex > object->size)
950b0cd2017SGleb Smirnoff 			endpindex = object->size;
951b0cd2017SGleb Smirnoff 
952b0cd2017SGleb Smirnoff 		for (tpindex = m[count - 1]->pindex + 1;
953b0cd2017SGleb Smirnoff 		    tpindex < endpindex; i++, tpindex++) {
9547667839aSAlan Cox 			p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
955b0cd2017SGleb Smirnoff 			if (p == NULL)
956b0cd2017SGleb Smirnoff 				break;
957b0cd2017SGleb Smirnoff 			bp->b_pages[i] = p;
958eac91e32SKonstantin Belousov 		}
959b0cd2017SGleb Smirnoff 
960b0cd2017SGleb Smirnoff 		bp->b_pgafter = i - bp->b_npages;
961b0cd2017SGleb Smirnoff 		bp->b_npages = i;
962b0cd2017SGleb Smirnoff 	} else
963b0cd2017SGleb Smirnoff 		bp->b_pgafter = 0;
964b0cd2017SGleb Smirnoff 
965b0cd2017SGleb Smirnoff 	if (VM_OBJECT_WOWNED(object))
966eac91e32SKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
967b0cd2017SGleb Smirnoff 
968b0cd2017SGleb Smirnoff 	/* Report back actual behind/ahead read. */
969b0cd2017SGleb Smirnoff 	if (a_rbehind)
970b0cd2017SGleb Smirnoff 		*a_rbehind = bp->b_pgbefore;
971b0cd2017SGleb Smirnoff 	if (a_rahead)
972b0cd2017SGleb Smirnoff 		*a_rahead = bp->b_pgafter;
973b0cd2017SGleb Smirnoff 
974e48b82bdSGleb Smirnoff #ifdef INVARIANTS
975dcc0ff5aSGleb Smirnoff 	KASSERT(bp->b_npages <= nitems(bp->b_pages),
976b0cd2017SGleb Smirnoff 	    ("%s: buf %p overflowed", __func__, bp));
9774f56243aSGleb Smirnoff 	for (int j = 1, prev = 0; j < bp->b_npages; j++) {
9781e0c121fSGleb Smirnoff 		if (bp->b_pages[j] == bogus_page)
9791e0c121fSGleb Smirnoff 			continue;
9801e0c121fSGleb Smirnoff 		KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
9811e0c121fSGleb Smirnoff 		    j - prev, ("%s: pages array not consecutive, bp %p",
9821e0c121fSGleb Smirnoff 		     __func__, bp));
9831e0c121fSGleb Smirnoff 		prev = j;
9841e0c121fSGleb Smirnoff 	}
985e48b82bdSGleb Smirnoff #endif
986eac91e32SKonstantin Belousov 
9870d94caffSDavid Greenman 	/*
988b0cd2017SGleb Smirnoff 	 * Recalculate first offset and bytecount with regards to read behind.
989b0cd2017SGleb Smirnoff 	 * Truncate bytecount to vnode real size and round up physical size
990b0cd2017SGleb Smirnoff 	 * for real devices.
99126f9a767SRodney W. Grimes 	 */
992b0cd2017SGleb Smirnoff 	foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
993b0cd2017SGleb Smirnoff 	bytecount = bp->b_npages << PAGE_SHIFT;
994b0cd2017SGleb Smirnoff 	if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
995b0cd2017SGleb Smirnoff 		bytecount = object->un_pager.vnp.vnp_size - foff;
996eac91e32SKonstantin Belousov 	secmask = bo->bo_bsize - 1;
9976229cc50SPoul-Henning Kamp 	KASSERT(secmask < PAGE_SIZE && secmask > 0,
998b0cd2017SGleb Smirnoff 	    ("%s: sector size %d too large", __func__, secmask + 1));
999b0cd2017SGleb Smirnoff 	bytecount = (bytecount + secmask) & ~secmask;
100026f9a767SRodney W. Grimes 
100126f9a767SRodney W. Grimes 	/*
1002b0cd2017SGleb Smirnoff 	 * And map the pages to be read into the kva, if the filesystem
10036ce697dcSKonstantin Belousov 	 * requires mapped buffers.
100426f9a767SRodney W. Grimes 	 */
10052a5eef69SGleb Smirnoff 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
10066ce697dcSKonstantin Belousov 	    unmapped_buf_allowed) {
10076ce697dcSKonstantin Belousov 		bp->b_data = unmapped_buf;
10086ce697dcSKonstantin Belousov 		bp->b_offset = 0;
1009fade8dd7SJeff Roberson 	} else {
1010fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
1011b0cd2017SGleb Smirnoff 		pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1012fade8dd7SJeff Roberson 	}
101326f9a767SRodney W. Grimes 
1014b0cd2017SGleb Smirnoff 	/* Build a minimal buffer header. */
101521144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
1016bd78ceceSJohn Baldwin 	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1017bd78ceceSJohn Baldwin 	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1018a854ed98SJohn Baldwin 	bp->b_rcred = crhold(curthread->td_ucred);
1019a854ed98SJohn Baldwin 	bp->b_wcred = crhold(curthread->td_ucred);
10209c83534dSPoul-Henning Kamp 	pbgetbo(bo, bp);
10211faacf5dSKirk McKusick 	bp->b_vp = vp;
1022b0cd2017SGleb Smirnoff 	bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
10232c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
1024e48b82bdSGleb Smirnoff 	KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1025e48b82bdSGleb Smirnoff 	    (blkno0 - bp->b_blkno) * DEV_BSIZE +
1026e48b82bdSGleb Smirnoff 	    IDX_TO_OFF(m[0]->pindex) % bsize,
1027e48b82bdSGleb Smirnoff 	    ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1028e48b82bdSGleb Smirnoff 	    "blkno0 %ju b_blkno %ju", bsize,
1029e48b82bdSGleb Smirnoff 	    (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1030e48b82bdSGleb Smirnoff 	    (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
103190effb23SGleb Smirnoff 
1032b0cd2017SGleb Smirnoff 	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
1033b0cd2017SGleb Smirnoff 	PCPU_INC(cnt.v_vnodein);
1034b0cd2017SGleb Smirnoff 	PCPU_ADD(cnt.v_vnodepgsin, bp->b_npages);
1035b0cd2017SGleb Smirnoff 
103690effb23SGleb Smirnoff 	if (iodone != NULL) { /* async */
1037b0cd2017SGleb Smirnoff 		bp->b_pgiodone = iodone;
103890effb23SGleb Smirnoff 		bp->b_caller1 = arg;
103990effb23SGleb Smirnoff 		bp->b_iodone = vnode_pager_generic_getpages_done_async;
104090effb23SGleb Smirnoff 		bp->b_flags |= B_ASYNC;
104190effb23SGleb Smirnoff 		BUF_KERNPROC(bp);
1042b792bebeSPoul-Henning Kamp 		bstrategy(bp);
1043b0cd2017SGleb Smirnoff 		return (VM_PAGER_OK);
104490effb23SGleb Smirnoff 	} else {
104590effb23SGleb Smirnoff 		bp->b_iodone = bdone;
104690effb23SGleb Smirnoff 		bstrategy(bp);
10476a4b5823SPoul-Henning Kamp 		bwait(bp, PVM, "vnread");
104890effb23SGleb Smirnoff 		error = vnode_pager_generic_getpages_done(bp);
10491bb5ad63SGleb Smirnoff 		for (i = 0; i < bp->b_npages; i++)
10506ce697dcSKonstantin Belousov 			bp->b_pages[i] = NULL;
10511faacf5dSKirk McKusick 		bp->b_vp = NULL;
10529c83534dSPoul-Henning Kamp 		pbrelbo(bp);
10531c7c3c6aSMatthew Dillon 		relpbuf(bp, &vnode_pbuf_freecnt);
105490effb23SGleb Smirnoff 		return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
105590effb23SGleb Smirnoff 	}
1056b0cd2017SGleb Smirnoff }
105790effb23SGleb Smirnoff 
105890effb23SGleb Smirnoff static void
105990effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp)
106090effb23SGleb Smirnoff {
106190effb23SGleb Smirnoff 	int error;
106290effb23SGleb Smirnoff 
106390effb23SGleb Smirnoff 	error = vnode_pager_generic_getpages_done(bp);
1064b0cd2017SGleb Smirnoff 	/* Run the iodone upon the requested range. */
1065b0cd2017SGleb Smirnoff 	bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1066b0cd2017SGleb Smirnoff 	    bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
106790effb23SGleb Smirnoff 	for (int i = 0; i < bp->b_npages; i++)
106890effb23SGleb Smirnoff 		bp->b_pages[i] = NULL;
106990effb23SGleb Smirnoff 	bp->b_vp = NULL;
107090effb23SGleb Smirnoff 	pbrelbo(bp);
107173e9030eSGleb Smirnoff 	relpbuf(bp, &vnode_async_pbuf_freecnt);
107290effb23SGleb Smirnoff }
107390effb23SGleb Smirnoff 
107490effb23SGleb Smirnoff static int
107590effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp)
107690effb23SGleb Smirnoff {
107790effb23SGleb Smirnoff 	vm_object_t object;
107890effb23SGleb Smirnoff 	off_t tfoff, nextoff;
107990effb23SGleb Smirnoff 	int i, error;
108090effb23SGleb Smirnoff 
108190effb23SGleb Smirnoff 	error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
108290effb23SGleb Smirnoff 	object = bp->b_vp->v_object;
108390effb23SGleb Smirnoff 
108490effb23SGleb Smirnoff 	if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1085fade8dd7SJeff Roberson 		if (!buf_mapped(bp)) {
1086fade8dd7SJeff Roberson 			bp->b_data = bp->b_kvabase;
1087fade8dd7SJeff Roberson 			pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
108890effb23SGleb Smirnoff 			    bp->b_npages);
108990effb23SGleb Smirnoff 		}
1090fade8dd7SJeff Roberson 		bzero(bp->b_data + bp->b_bcount,
109190effb23SGleb Smirnoff 		    PAGE_SIZE * bp->b_npages - bp->b_bcount);
109290effb23SGleb Smirnoff 	}
1093fade8dd7SJeff Roberson 	if (buf_mapped(bp)) {
1094fade8dd7SJeff Roberson 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1095fade8dd7SJeff Roberson 		bp->b_data = unmapped_buf;
109690effb23SGleb Smirnoff 	}
109726f9a767SRodney W. Grimes 
109889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
109990effb23SGleb Smirnoff 	for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
110090effb23SGleb Smirnoff 	    i < bp->b_npages; i++, tfoff = nextoff) {
11018f9110f6SJohn Dyson 		vm_page_t mt;
11028f9110f6SJohn Dyson 
11038f9110f6SJohn Dyson 		nextoff = tfoff + PAGE_SIZE;
110490effb23SGleb Smirnoff 		mt = bp->b_pages[i];
11058f9110f6SJohn Dyson 
110654746b67SDmitrij Tejblum 		if (nextoff <= object->un_pager.vnp.vnp_size) {
11078d17e694SJulian Elischer 			/*
11088d17e694SJulian Elischer 			 * Read filled up entire page.
11098d17e694SJulian Elischer 			 */
11108f9110f6SJohn Dyson 			mt->valid = VM_PAGE_BITS_ALL;
1111016a3c93SAlan Cox 			KASSERT(mt->dirty == 0,
111279f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
1113016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(mt),
111479f0deb9SGleb Smirnoff 			    ("%s: page %p is mapped", __func__, mt));
11158f9110f6SJohn Dyson 		} else {
11168d17e694SJulian Elischer 			/*
111742eb4108SAlan Cox 			 * Read did not fill up entire page.
11188d17e694SJulian Elischer 			 *
11198d17e694SJulian Elischer 			 * Currently we do not set the entire page valid,
11208d17e694SJulian Elischer 			 * we just try to clear the piece that we couldn't
11218d17e694SJulian Elischer 			 * read.
11228d17e694SJulian Elischer 			 */
1123dc874f98SKonstantin Belousov 			vm_page_set_valid_range(mt, 0,
112454746b67SDmitrij Tejblum 			    object->un_pager.vnp.vnp_size - tfoff);
112542eb4108SAlan Cox 			KASSERT((mt->dirty & vm_page_bits(0,
112642eb4108SAlan Cox 			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
112779f0deb9SGleb Smirnoff 			    ("%s: page %p is dirty", __func__, mt));
11288f9110f6SJohn Dyson 		}
11298f9110f6SJohn Dyson 
1130b0cd2017SGleb Smirnoff 		if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1131b6c00483SKonstantin Belousov 			vm_page_readahead_finish(mt);
113203679e23SAlan Cox 	}
113389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
113490effb23SGleb Smirnoff 	if (error != 0)
113590effb23SGleb Smirnoff 		printf("%s: I/O read error %d\n", __func__, error);
113690effb23SGleb Smirnoff 
113790effb23SGleb Smirnoff 	return (error);
113826f9a767SRodney W. Grimes }
113926f9a767SRodney W. Grimes 
1140ce75f2c3SMike Smith /*
1141ce75f2c3SMike Smith  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
1142ce75f2c3SMike Smith  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1143ce75f2c3SMike Smith  * vnode_pager_generic_putpages() to implement the previous behaviour.
1144ce75f2c3SMike Smith  *
1145ce75f2c3SMike Smith  * All other FS's should use the bypass to get to the local media
1146ce75f2c3SMike Smith  * backing vp's VOP_PUTPAGES.
1147ce75f2c3SMike Smith  */
1148e4542174SMatthew Dillon static void
11497ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
115033cad9e9SKonstantin Belousov     int flags, int *rtvals)
1151170db9c6SJohn Dyson {
1152170db9c6SJohn Dyson 	int rtval;
1153170db9c6SJohn Dyson 	struct vnode *vp;
115486ffbd76SMike Smith 	int bytes = count * PAGE_SIZE;
1155ad980522SJohn Dyson 
11560e3cdf2cSAlan Cox 	/*
11570e3cdf2cSAlan Cox 	 * Force synchronous operation if we are extremely low on memory
11580e3cdf2cSAlan Cox 	 * to prevent a low-memory deadlock.  VOP operations often need to
11590e3cdf2cSAlan Cox 	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
11600e3cdf2cSAlan Cox 	 * operation ).  The swapper handles the case by limiting the amount
11610e3cdf2cSAlan Cox 	 * of asynchronous I/O, but that sort of solution doesn't scale well
11620e3cdf2cSAlan Cox 	 * for the vnode pager without a lot of work.
11630e3cdf2cSAlan Cox 	 *
11640e3cdf2cSAlan Cox 	 * Also, the backing vnode's iodone routine may not wake the pageout
11650e3cdf2cSAlan Cox 	 * daemon up.  This should be probably be addressed XXX.
11660e3cdf2cSAlan Cox 	 */
11670e3cdf2cSAlan Cox 
1168bba39b9aSAlan Cox 	if (vm_cnt.v_free_count < vm_cnt.v_pageout_free_min)
116933cad9e9SKonstantin Belousov 		flags |= VM_PAGER_PUT_SYNC;
11700e3cdf2cSAlan Cox 
11710e3cdf2cSAlan Cox 	/*
11720e3cdf2cSAlan Cox 	 * Call device-specific putpages function
11730e3cdf2cSAlan Cox 	 */
1174170db9c6SJohn Dyson 	vp = object->handle;
117589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
117633cad9e9SKonstantin Belousov 	rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
117723955314SAlfred Perlstein 	KASSERT(rtval != EOPNOTSUPP,
117823955314SAlfred Perlstein 	    ("vnode_pager: stale FS putpages\n"));
117989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1180170db9c6SJohn Dyson }
1181170db9c6SJohn Dyson 
1182ce75f2c3SMike Smith 
118326f9a767SRodney W. Grimes /*
1184ce75f2c3SMike Smith  * This is now called from local media FS's to operate against their
11854491ea91SEivind Eklund  * own vnodes if they fail to implement VOP_PUTPAGES.
11862b6b0df7SMatthew Dillon  *
11872b6b0df7SMatthew Dillon  * This is typically called indirectly via the pageout daemon and
1188763df3ecSPedro F. Giffuni  * clustering has already typically occurred, so in general we ask the
11892b6b0df7SMatthew Dillon  * underlying filesystem to write the data out asynchronously rather
11902b6b0df7SMatthew Dillon  * then delayed.
119126f9a767SRodney W. Grimes  */
1192ce75f2c3SMike Smith int
1193c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1194c46b90e9SAlan Cox     int flags, int *rtvals)
119526f9a767SRodney W. Grimes {
1196ce75f2c3SMike Smith 	vm_object_t object;
1197c46b90e9SAlan Cox 	vm_page_t m;
1198a316d390SJohn Dyson 	vm_ooffset_t poffset;
1199f6b04d2bSDavid Greenman 	struct uio auio;
1200f6b04d2bSDavid Greenman 	struct iovec aiov;
1201*3dbb0ca6SKonstantin Belousov 	int count, error, i, ioflags, maxsize, ncount, ppscheck;
1202dd498befSPaul Saab 	static struct timeval lastfail;
1203dd498befSPaul Saab 	static int curfail;
120426f9a767SRodney W. Grimes 
1205ce75f2c3SMike Smith 	object = vp->v_object;
1206ce75f2c3SMike Smith 	count = bytecount / PAGE_SIZE;
1207ce75f2c3SMike Smith 
120826f9a767SRodney W. Grimes 	for (i = 0; i < count; i++)
1209031ec8c1SKonstantin Belousov 		rtvals[i] = VM_PAGER_ERROR;
121026f9a767SRodney W. Grimes 
1211c46b90e9SAlan Cox 	if ((int64_t)ma[0]->pindex < 0) {
121223562e4bSMarcel Moolenaar 		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1213c46b90e9SAlan Cox 		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1214f6b04d2bSDavid Greenman 		rtvals[0] = VM_PAGER_BAD;
1215f6b04d2bSDavid Greenman 		return VM_PAGER_BAD;
12160d94caffSDavid Greenman 	}
12170bdb7528SDavid Greenman 
1218f6b04d2bSDavid Greenman 	maxsize = count * PAGE_SIZE;
1219f6b04d2bSDavid Greenman 	ncount = count;
122026f9a767SRodney W. Grimes 
1221c46b90e9SAlan Cox 	poffset = IDX_TO_OFF(ma[0]->pindex);
122200a6f47fSMatthew Dillon 
122300a6f47fSMatthew Dillon 	/*
122400a6f47fSMatthew Dillon 	 * If the page-aligned write is larger then the actual file we
1225763df3ecSPedro F. Giffuni 	 * have to invalidate pages occurring beyond the file EOF.  However,
122600a6f47fSMatthew Dillon 	 * there is an edge case where a file may not be page-aligned where
122700a6f47fSMatthew Dillon 	 * the last page is partially invalid.  In this case the filesystem
122800a6f47fSMatthew Dillon 	 * may not properly clear the dirty bits for the entire page (which
122900a6f47fSMatthew Dillon 	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
123000a6f47fSMatthew Dillon 	 * With the page locked we are free to fix-up the dirty bits here.
12313ebeaf59SMatthew Dillon 	 *
12323ebeaf59SMatthew Dillon 	 * We do not under any circumstances truncate the valid bits, as
12333ebeaf59SMatthew Dillon 	 * this will screw up bogus page replacement.
123400a6f47fSMatthew Dillon 	 */
123589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1236a316d390SJohn Dyson 	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
123700a6f47fSMatthew Dillon 		if (object->un_pager.vnp.vnp_size > poffset) {
123800a6f47fSMatthew Dillon 			int pgoff;
123900a6f47fSMatthew Dillon 
1240a316d390SJohn Dyson 			maxsize = object->un_pager.vnp.vnp_size - poffset;
1241aa8de40aSPoul-Henning Kamp 			ncount = btoc(maxsize);
124200a6f47fSMatthew Dillon 			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1243c46b90e9SAlan Cox 				/*
1244c46b90e9SAlan Cox 				 * If the object is locked and the following
1245c46b90e9SAlan Cox 				 * conditions hold, then the page's dirty
1246c46b90e9SAlan Cox 				 * field cannot be concurrently changed by a
1247c46b90e9SAlan Cox 				 * pmap operation.
1248c46b90e9SAlan Cox 				 */
1249c46b90e9SAlan Cox 				m = ma[ncount - 1];
1250c7aebda8SAttilio Rao 				vm_page_assert_sbusied(m);
12516031c68dSAlan Cox 				KASSERT(!pmap_page_is_write_mapped(m),
1252c46b90e9SAlan Cox 		("vnode_pager_generic_putpages: page %p is not read-only", m));
1253c46b90e9SAlan Cox 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1254c46b90e9SAlan Cox 				    pgoff);
125500a6f47fSMatthew Dillon 			}
125600a6f47fSMatthew Dillon 		} else {
125700a6f47fSMatthew Dillon 			maxsize = 0;
125800a6f47fSMatthew Dillon 			ncount = 0;
125900a6f47fSMatthew Dillon 		}
1260f6b04d2bSDavid Greenman 		if (ncount < count) {
1261f6b04d2bSDavid Greenman 			for (i = ncount; i < count; i++) {
1262f6b04d2bSDavid Greenman 				rtvals[i] = VM_PAGER_BAD;
1263f6b04d2bSDavid Greenman 			}
1264f6b04d2bSDavid Greenman 		}
1265f6b04d2bSDavid Greenman 	}
126689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
126726f9a767SRodney W. Grimes 
12682b6b0df7SMatthew Dillon 	/*
12692b6b0df7SMatthew Dillon 	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
12702b6b0df7SMatthew Dillon 	 * rather then a bdwrite() to prevent paging I/O from saturating
127143b7990eSMatthew Dillon 	 * the buffer cache.  Dummy-up the sequential heuristic to cause
127243b7990eSMatthew Dillon 	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
127343b7990eSMatthew Dillon 	 * the system decides how to cluster.
12742b6b0df7SMatthew Dillon 	 */
12758f9110f6SJohn Dyson 	ioflags = IO_VMIO;
127643b7990eSMatthew Dillon 	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
127743b7990eSMatthew Dillon 		ioflags |= IO_SYNC;
127843b7990eSMatthew Dillon 	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
127943b7990eSMatthew Dillon 		ioflags |= IO_ASYNC;
12808f9110f6SJohn Dyson 	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
128199e6e193SMark Johnston 	ioflags |= (flags & VM_PAGER_PUT_NOREUSE) ? IO_NOREUSE : 0;
128243b7990eSMatthew Dillon 	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1283f6b04d2bSDavid Greenman 
1284f6b04d2bSDavid Greenman 	aiov.iov_base = (caddr_t) 0;
1285f6b04d2bSDavid Greenman 	aiov.iov_len = maxsize;
1286f6b04d2bSDavid Greenman 	auio.uio_iov = &aiov;
1287f6b04d2bSDavid Greenman 	auio.uio_iovcnt = 1;
1288a316d390SJohn Dyson 	auio.uio_offset = poffset;
1289f6b04d2bSDavid Greenman 	auio.uio_segflg = UIO_NOCOPY;
1290f6b04d2bSDavid Greenman 	auio.uio_rw = UIO_WRITE;
1291f6b04d2bSDavid Greenman 	auio.uio_resid = maxsize;
1292b40ce416SJulian Elischer 	auio.uio_td = (struct thread *) 0;
1293a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1294b4b70819SAttilio Rao 	PCPU_INC(cnt.v_vnodeout);
1295b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1296f6b04d2bSDavid Greenman 
1297*3dbb0ca6SKonstantin Belousov 	ppscheck = 0;
1298f6b04d2bSDavid Greenman 	if (error) {
1299dd498befSPaul Saab 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
130024a1cce3SDavid Greenman 			printf("vnode_pager_putpages: I/O error %d\n", error);
1301f6b04d2bSDavid Greenman 	}
1302f6b04d2bSDavid Greenman 	if (auio.uio_resid) {
1303dd498befSPaul Saab 		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
13049f80ce04SKonstantin Belousov 			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1305c46b90e9SAlan Cox 			    auio.uio_resid, (u_long)ma[0]->pindex);
130626f9a767SRodney W. Grimes 	}
1307ffc82b0aSJohn Dyson 	for (i = 0; i < ncount; i++) {
130826f9a767SRodney W. Grimes 		rtvals[i] = VM_PAGER_OK;
130926f9a767SRodney W. Grimes 	}
1310f6b04d2bSDavid Greenman 	return rtvals[0];
131126f9a767SRodney W. Grimes }
1312031ec8c1SKonstantin Belousov 
1313031ec8c1SKonstantin Belousov void
1314031ec8c1SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1315031ec8c1SKonstantin Belousov {
13169d17da3bSKonstantin Belousov 	vm_object_t obj;
1317031ec8c1SKonstantin Belousov 	int i, pos;
1318031ec8c1SKonstantin Belousov 
13199d17da3bSKonstantin Belousov 	if (written == 0)
13209d17da3bSKonstantin Belousov 		return;
13219d17da3bSKonstantin Belousov 	obj = ma[0]->object;
132289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1323031ec8c1SKonstantin Belousov 	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1324031ec8c1SKonstantin Belousov 		if (pos < trunc_page(written)) {
1325031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_OK;
1326031ec8c1SKonstantin Belousov 			vm_page_undirty(ma[i]);
1327031ec8c1SKonstantin Belousov 		} else {
1328031ec8c1SKonstantin Belousov 			/* Partially written page. */
1329031ec8c1SKonstantin Belousov 			rtvals[i] = VM_PAGER_AGAIN;
1330031ec8c1SKonstantin Belousov 			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1331031ec8c1SKonstantin Belousov 		}
1332031ec8c1SKonstantin Belousov 	}
133389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1334031ec8c1SKonstantin Belousov }
133584110e7eSKonstantin Belousov 
133684110e7eSKonstantin Belousov void
133784110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
133884110e7eSKonstantin Belousov     vm_offset_t end)
133984110e7eSKonstantin Belousov {
134084110e7eSKonstantin Belousov 	struct vnode *vp;
134184110e7eSKonstantin Belousov 	vm_ooffset_t old_wm;
134284110e7eSKonstantin Belousov 
134389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
134484110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
134589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
134684110e7eSKonstantin Belousov 		return;
134784110e7eSKonstantin Belousov 	}
134884110e7eSKonstantin Belousov 	old_wm = object->un_pager.vnp.writemappings;
134984110e7eSKonstantin Belousov 	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
135084110e7eSKonstantin Belousov 	vp = object->handle;
135184110e7eSKonstantin Belousov 	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
135284110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1353140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, 1);
1354b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1355b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
135684110e7eSKonstantin Belousov 	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
135784110e7eSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1358140dedb8SKonstantin Belousov 		VOP_ADD_WRITECOUNT(vp, -1);
1359b47f6241SJohn Baldwin 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1360b47f6241SJohn Baldwin 		    __func__, vp, vp->v_writecount);
136184110e7eSKonstantin Belousov 	}
136289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
136384110e7eSKonstantin Belousov }
136484110e7eSKonstantin Belousov 
136584110e7eSKonstantin Belousov void
136684110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
136784110e7eSKonstantin Belousov     vm_offset_t end)
136884110e7eSKonstantin Belousov {
136984110e7eSKonstantin Belousov 	struct vnode *vp;
137084110e7eSKonstantin Belousov 	struct mount *mp;
137184110e7eSKonstantin Belousov 	vm_offset_t inc;
137284110e7eSKonstantin Belousov 
137389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
137484110e7eSKonstantin Belousov 
137584110e7eSKonstantin Belousov 	/*
137684110e7eSKonstantin Belousov 	 * First, recheck the object type to account for the race when
137784110e7eSKonstantin Belousov 	 * the vnode is reclaimed.
137884110e7eSKonstantin Belousov 	 */
137984110e7eSKonstantin Belousov 	if (object->type != OBJT_VNODE) {
138089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
138184110e7eSKonstantin Belousov 		return;
138284110e7eSKonstantin Belousov 	}
138384110e7eSKonstantin Belousov 
138484110e7eSKonstantin Belousov 	/*
138584110e7eSKonstantin Belousov 	 * Optimize for the case when writemappings is not going to
138684110e7eSKonstantin Belousov 	 * zero.
138784110e7eSKonstantin Belousov 	 */
138884110e7eSKonstantin Belousov 	inc = end - start;
138984110e7eSKonstantin Belousov 	if (object->un_pager.vnp.writemappings != inc) {
139084110e7eSKonstantin Belousov 		object->un_pager.vnp.writemappings -= inc;
139189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
139284110e7eSKonstantin Belousov 		return;
139384110e7eSKonstantin Belousov 	}
139484110e7eSKonstantin Belousov 
139584110e7eSKonstantin Belousov 	vp = object->handle;
139684110e7eSKonstantin Belousov 	vhold(vp);
139789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
139884110e7eSKonstantin Belousov 	mp = NULL;
139984110e7eSKonstantin Belousov 	vn_start_write(vp, &mp, V_WAIT);
140084110e7eSKonstantin Belousov 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
140184110e7eSKonstantin Belousov 
140284110e7eSKonstantin Belousov 	/*
140384110e7eSKonstantin Belousov 	 * Decrement the object's writemappings, by swapping the start
140484110e7eSKonstantin Belousov 	 * and end arguments for vnode_pager_update_writecount().  If
140584110e7eSKonstantin Belousov 	 * there was not a race with vnode reclaimation, then the
140684110e7eSKonstantin Belousov 	 * vnode's v_writecount is decremented.
140784110e7eSKonstantin Belousov 	 */
140884110e7eSKonstantin Belousov 	vnode_pager_update_writecount(object, end, start);
140984110e7eSKonstantin Belousov 	VOP_UNLOCK(vp, 0);
141084110e7eSKonstantin Belousov 	vdrop(vp);
141184110e7eSKonstantin Belousov 	if (mp != NULL)
141284110e7eSKonstantin Belousov 		vn_finished_write(mp);
141384110e7eSKonstantin Belousov }
1414