160727d8bSWarner Losh /*- 2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 3df57947fSPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 526f9a767SRodney W. Grimes * Copyright (c) 1991 The Regents of the University of California. 626f9a767SRodney W. Grimes * All rights reserved. 726f9a767SRodney W. Grimes * Copyright (c) 1993, 1994 John S. Dyson 824a1cce3SDavid Greenman * Copyright (c) 1995, David Greenman 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 11df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 12df8bae1dSRodney W. Grimes * Science Department. 13df8bae1dSRodney W. Grimes * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 235929bcfaSPhilippe Charnier * must display the following acknowledgement: 24df8bae1dSRodney W. Grimes * This product includes software developed by the University of 25df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 26df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 27df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 28df8bae1dSRodney W. Grimes * without specific prior written permission. 29df8bae1dSRodney W. Grimes * 30df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40df8bae1dSRodney W. Grimes * SUCH DAMAGE. 41df8bae1dSRodney W. Grimes * 4226f9a767SRodney W. Grimes * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91 43df8bae1dSRodney W. Grimes */ 44df8bae1dSRodney W. Grimes 45df8bae1dSRodney W. Grimes /* 46df8bae1dSRodney W. Grimes * Page to/from files (vnodes). 47df8bae1dSRodney W. Grimes */ 48df8bae1dSRodney W. Grimes 4926f9a767SRodney W. Grimes /* 5026f9a767SRodney W. Grimes * TODO: 5124a1cce3SDavid Greenman * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 52f6b04d2bSDavid Greenman * greatly re-simplify the vnode_pager. 5326f9a767SRodney W. Grimes */ 5426f9a767SRodney W. Grimes 55874651b1SDavid E. O'Brien #include <sys/cdefs.h> 56874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 57874651b1SDavid E. O'Brien 583d653db0SAlan Cox #include "opt_vm.h" 593d653db0SAlan Cox 60df8bae1dSRodney W. Grimes #include <sys/param.h> 61df8bae1dSRodney W. Grimes #include <sys/systm.h> 62*e5818a53SJeff Roberson #include <sys/sysctl.h> 63df8bae1dSRodney W. Grimes #include <sys/proc.h> 64df8bae1dSRodney W. Grimes #include <sys/vnode.h> 65df8bae1dSRodney W. Grimes #include <sys/mount.h> 669626b608SPoul-Henning Kamp #include <sys/bio.h> 6724a1cce3SDavid Greenman #include <sys/buf.h> 68efeaf95aSDavid Greenman #include <sys/vmmeter.h> 69d07a6d3fSPoul-Henning Kamp #include <sys/limits.h> 7024579ca1SMatthew Dillon #include <sys/conf.h> 7189f6b863SAttilio Rao #include <sys/rwlock.h> 729e0ddbd0SAlan Cox #include <sys/sf_buf.h> 73*e5818a53SJeff Roberson #include <sys/domainset.h> 74df8bae1dSRodney W. Grimes 754f12e0acSSuleiman Souhlal #include <machine/atomic.h> 764f12e0acSSuleiman Souhlal 77df8bae1dSRodney W. Grimes #include <vm/vm.h> 781c771f92SKonstantin Belousov #include <vm/vm_param.h> 79efeaf95aSDavid Greenman #include <vm/vm_object.h> 80df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8124a1cce3SDavid Greenman #include <vm/vm_pager.h> 821efb74fbSJohn Dyson #include <vm/vm_map.h> 83df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h> 84efeaf95aSDavid Greenman #include <vm/vm_extern.h> 85df8bae1dSRodney W. Grimes 86bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 87bff76343SAlan Cox daddr_t *rtaddress, int *run); 8811caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 8911caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 9011caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t); 91b0cd2017SGleb Smirnoff static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *); 92b0cd2017SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 93b0cd2017SGleb Smirnoff int *, vop_getpages_iodone_t, void *); 9433cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 9511caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 963364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 973364c323SKonstantin Belousov vm_ooffset_t, struct ucred *cred); 9890effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *); 9990effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *); 1000b8253a7SBruce Evans 101df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 1024e658600SPoul-Henning Kamp .pgo_alloc = vnode_pager_alloc, 1034e658600SPoul-Henning Kamp .pgo_dealloc = vnode_pager_dealloc, 1044e658600SPoul-Henning Kamp .pgo_getpages = vnode_pager_getpages, 10590effb23SGleb Smirnoff .pgo_getpages_async = vnode_pager_getpages_async, 1064e658600SPoul-Henning Kamp .pgo_putpages = vnode_pager_putpages, 1074e658600SPoul-Henning Kamp .pgo_haspage = vnode_pager_haspage, 108df8bae1dSRodney W. Grimes }; 109df8bae1dSRodney W. Grimes 110b62b9b64SJohn Baldwin int vnode_pbuf_freecnt; 11173e9030eSGleb Smirnoff int vnode_async_pbuf_freecnt; 1121c7c3c6aSMatthew Dillon 113*e5818a53SJeff Roberson static struct domainset *vnode_domainset = NULL; 114*e5818a53SJeff Roberson 115*e5818a53SJeff Roberson SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset, CTLTYPE_STRING | CTLFLAG_RW, 116*e5818a53SJeff Roberson &vnode_domainset, 0, sysctl_handle_domainset, "A", 117*e5818a53SJeff Roberson "Default vnode NUMA policy"); 118*e5818a53SJeff Roberson 119d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */ 120d07a6d3fSPoul-Henning Kamp int 121731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td) 122d07a6d3fSPoul-Henning Kamp { 123d07a6d3fSPoul-Henning Kamp vm_object_t object; 124d07a6d3fSPoul-Henning Kamp vm_ooffset_t size = isize; 125d07a6d3fSPoul-Henning Kamp struct vattr va; 126d07a6d3fSPoul-Henning Kamp 127d07a6d3fSPoul-Henning Kamp if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE) 128d07a6d3fSPoul-Henning Kamp return (0); 129d07a6d3fSPoul-Henning Kamp 130d07a6d3fSPoul-Henning Kamp while ((object = vp->v_object) != NULL) { 13189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 132d07a6d3fSPoul-Henning Kamp if (!(object->flags & OBJ_DEAD)) { 13389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 134d07a6d3fSPoul-Henning Kamp return (0); 135d07a6d3fSPoul-Henning Kamp } 13622db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 137d07a6d3fSPoul-Henning Kamp vm_object_set_flag(object, OBJ_DISCONNECTWNT); 1380dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0); 139cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 140d07a6d3fSPoul-Henning Kamp } 141d07a6d3fSPoul-Henning Kamp 142d07a6d3fSPoul-Henning Kamp if (size == 0) { 143d07a6d3fSPoul-Henning Kamp if (vn_isdisk(vp, NULL)) { 144d07a6d3fSPoul-Henning Kamp size = IDX_TO_OFF(INT_MAX); 145d07a6d3fSPoul-Henning Kamp } else { 1460359a12eSAttilio Rao if (VOP_GETATTR(vp, &va, td->td_ucred)) 147d07a6d3fSPoul-Henning Kamp return (0); 148d07a6d3fSPoul-Henning Kamp size = va.va_size; 149d07a6d3fSPoul-Henning Kamp } 150d07a6d3fSPoul-Henning Kamp } 151d07a6d3fSPoul-Henning Kamp 1523364c323SKonstantin Belousov object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred); 153d07a6d3fSPoul-Henning Kamp /* 154d07a6d3fSPoul-Henning Kamp * Dereference the reference we just created. This assumes 155d07a6d3fSPoul-Henning Kamp * that the object is associated with the vp. 156d07a6d3fSPoul-Henning Kamp */ 15789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 158d07a6d3fSPoul-Henning Kamp object->ref_count--; 15989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 160d07a6d3fSPoul-Henning Kamp vrele(vp); 161d07a6d3fSPoul-Henning Kamp 162d07a6d3fSPoul-Henning Kamp KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object")); 163d07a6d3fSPoul-Henning Kamp 164d07a6d3fSPoul-Henning Kamp return (0); 165d07a6d3fSPoul-Henning Kamp } 166d07a6d3fSPoul-Henning Kamp 1677146d6cbSPoul-Henning Kamp void 1687146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp) 1697146d6cbSPoul-Henning Kamp { 1707146d6cbSPoul-Henning Kamp struct vm_object *obj; 1717146d6cbSPoul-Henning Kamp 1727146d6cbSPoul-Henning Kamp obj = vp->v_object; 1737146d6cbSPoul-Henning Kamp if (obj == NULL) 1747146d6cbSPoul-Henning Kamp return; 17557fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject"); 17689f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 1772a339d9eSKonstantin Belousov umtx_shm_object_terminated(obj); 1787146d6cbSPoul-Henning Kamp if (obj->ref_count == 0) { 1797146d6cbSPoul-Henning Kamp /* 1807146d6cbSPoul-Henning Kamp * don't double-terminate the object 1817146d6cbSPoul-Henning Kamp */ 18290880a1bSKonstantin Belousov if ((obj->flags & OBJ_DEAD) == 0) { 1837146d6cbSPoul-Henning Kamp vm_object_terminate(obj); 18490880a1bSKonstantin Belousov } else { 18590880a1bSKonstantin Belousov /* 18690880a1bSKonstantin Belousov * Waiters were already handled during object 18790880a1bSKonstantin Belousov * termination. The exclusive vnode lock hopefully 18890880a1bSKonstantin Belousov * prevented new waiters from referencing the dying 18990880a1bSKonstantin Belousov * object. 19090880a1bSKonstantin Belousov */ 19190880a1bSKonstantin Belousov KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0, 19290880a1bSKonstantin Belousov ("OBJ_DISCONNECTWNT set obj %p flags %x", 19390880a1bSKonstantin Belousov obj, obj->flags)); 19490880a1bSKonstantin Belousov vp->v_object = NULL; 19589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 19690880a1bSKonstantin Belousov } 1977146d6cbSPoul-Henning Kamp } else { 1987146d6cbSPoul-Henning Kamp /* 1997146d6cbSPoul-Henning Kamp * Woe to the process that tries to page now :-). 2007146d6cbSPoul-Henning Kamp */ 2017146d6cbSPoul-Henning Kamp vm_pager_deallocate(obj); 20289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 2037146d6cbSPoul-Henning Kamp } 20490880a1bSKonstantin Belousov KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object)); 2057146d6cbSPoul-Henning Kamp } 2067146d6cbSPoul-Henning Kamp 2077146d6cbSPoul-Henning Kamp 208df8bae1dSRodney W. Grimes /* 209df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 210df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 211990ab7adSAlan Cox * 212990ab7adSAlan Cox * MPSAFE 213df8bae1dSRodney W. Grimes */ 21424a1cce3SDavid Greenman vm_object_t 2156cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 2163364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 217df8bae1dSRodney W. Grimes { 21806cb7259SDavid Greenman vm_object_t object; 219df8bae1dSRodney W. Grimes struct vnode *vp; 220df8bae1dSRodney W. Grimes 221df8bae1dSRodney W. Grimes /* 222df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 223df8bae1dSRodney W. Grimes */ 224df8bae1dSRodney W. Grimes if (handle == NULL) 225df8bae1dSRodney W. Grimes return (NULL); 226df8bae1dSRodney W. Grimes 227df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 22839d38f93SDavid Greenman 22939d38f93SDavid Greenman /* 23039d38f93SDavid Greenman * If the object is being terminated, wait for it to 23139d38f93SDavid Greenman * go away. 23239d38f93SDavid Greenman */ 2332ac78f0eSStephan Uphoff retry: 2341ca58953SAlan Cox while ((object = vp->v_object) != NULL) { 23589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2361ca58953SAlan Cox if ((object->flags & OBJ_DEAD) == 0) 2371ca58953SAlan Cox break; 23819187819SAlan Cox vm_object_set_flag(object, OBJ_DISCONNECTWNT); 2390dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0); 24024a1cce3SDavid Greenman } 2410d94caffSDavid Greenman 2426ded8427SKonstantin Belousov KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference")); 2432be70f79SJohn Dyson 24424a1cce3SDavid Greenman if (object == NULL) { 245df8bae1dSRodney W. Grimes /* 2462ac78f0eSStephan Uphoff * Add an object of the appropriate size 247df8bae1dSRodney W. Grimes */ 2486cde7a16SDavid Greenman object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 249bbc0ec52SDavid Greenman 2506cde7a16SDavid Greenman object->un_pager.vnp.vnp_size = size; 25184110e7eSKonstantin Belousov object->un_pager.vnp.writemappings = 0; 252*e5818a53SJeff Roberson object->domain.dr_policy = vnode_domainset; 25326f9a767SRodney W. Grimes 25424a1cce3SDavid Greenman object->handle = handle; 25511be8415SStephan Uphoff VI_LOCK(vp); 2562ac78f0eSStephan Uphoff if (vp->v_object != NULL) { 2572ac78f0eSStephan Uphoff /* 2582ac78f0eSStephan Uphoff * Object has been created while we were sleeping 2592ac78f0eSStephan Uphoff */ 26011be8415SStephan Uphoff VI_UNLOCK(vp); 2619cddade7SKonstantin Belousov VM_OBJECT_WLOCK(object); 2629cddade7SKonstantin Belousov KASSERT(object->ref_count == 1, 2639cddade7SKonstantin Belousov ("leaked ref %p %d", object, object->ref_count)); 2649cddade7SKonstantin Belousov object->type = OBJT_DEAD; 2659cddade7SKonstantin Belousov object->ref_count = 0; 2669cddade7SKonstantin Belousov VM_OBJECT_WUNLOCK(object); 2672ac78f0eSStephan Uphoff vm_object_destroy(object); 2682ac78f0eSStephan Uphoff goto retry; 269df8bae1dSRodney W. Grimes } 2702ac78f0eSStephan Uphoff vp->v_object = object; 27111be8415SStephan Uphoff VI_UNLOCK(vp); 27211be8415SStephan Uphoff } else { 2732ac78f0eSStephan Uphoff object->ref_count++; 2743d653db0SAlan Cox #if VM_NRESERVLEVEL > 0 2753d653db0SAlan Cox vm_object_color(object, 0); 2763d653db0SAlan Cox #endif 27789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 27811be8415SStephan Uphoff } 2796ff51a36SMateusz Guzik vrefact(vp); 28024a1cce3SDavid Greenman return (object); 281df8bae1dSRodney W. Grimes } 282df8bae1dSRodney W. Grimes 283658ad5ffSAlan Cox /* 284658ad5ffSAlan Cox * The object must be locked. 285658ad5ffSAlan Cox */ 286f708ef1bSPoul-Henning Kamp static void 2877ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object) 28824a1cce3SDavid Greenman { 289b9f180d1SKonstantin Belousov struct vnode *vp; 290b9f180d1SKonstantin Belousov int refs; 291df8bae1dSRodney W. Grimes 292b9f180d1SKonstantin Belousov vp = object->handle; 29324a1cce3SDavid Greenman if (vp == NULL) 29424a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 29524a1cce3SDavid Greenman 29689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 29766095752SJohn Dyson vm_object_pip_wait(object, "vnpdea"); 298b9f180d1SKonstantin Belousov refs = object->ref_count; 29924a1cce3SDavid Greenman 30024a1cce3SDavid Greenman object->handle = NULL; 30195461b45SJohn Dyson object->type = OBJT_DEAD; 30219187819SAlan Cox if (object->flags & OBJ_DISCONNECTWNT) { 30319187819SAlan Cox vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 30419187819SAlan Cox wakeup(object); 30519187819SAlan Cox } 30657fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc"); 30784110e7eSKonstantin Belousov if (object->un_pager.vnp.writemappings > 0) { 30884110e7eSKonstantin Belousov object->un_pager.vnp.writemappings = 0; 309140dedb8SKonstantin Belousov VOP_ADD_WRITECOUNT(vp, -1); 310b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 311b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 31284110e7eSKonstantin Belousov } 313aa2cabb9SDavid Greenman vp->v_object = NULL; 314877d24acSKonstantin Belousov VOP_UNSET_TEXT(vp); 31589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 316b9f180d1SKonstantin Belousov while (refs-- > 0) 317b9f180d1SKonstantin Belousov vunref(vp); 31889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 319df8bae1dSRodney W. Grimes } 32026f9a767SRodney W. Grimes 321f708ef1bSPoul-Henning Kamp static boolean_t 3227ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 3237ebba1f8SGleb Smirnoff int *after) 324df8bae1dSRodney W. Grimes { 32524a1cce3SDavid Greenman struct vnode *vp = object->handle; 32698b0c789SPoul-Henning Kamp daddr_t bn; 3273af76890SPoul-Henning Kamp int err; 328170db9c6SJohn Dyson daddr_t reqblock; 3292c4488fcSJohn Dyson int poff; 3302c4488fcSJohn Dyson int bsize; 331d63596ceSJohn Dyson int pagesperblock, blocksperpage; 332df8bae1dSRodney W. Grimes 33389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 33424579ca1SMatthew Dillon /* 33524579ca1SMatthew Dillon * If no vp or vp is doomed or marked transparent to VM, we do not 33624579ca1SMatthew Dillon * have the page. 33724579ca1SMatthew Dillon */ 338b73f64c4SJeff Roberson if (vp == NULL || vp->v_iflag & VI_DOOMED) 33947221757SJohn Dyson return FALSE; 340df8bae1dSRodney W. Grimes /* 341b73f64c4SJeff Roberson * If the offset is beyond end of file we do 3420d94caffSDavid Greenman * not have the page. 343df8bae1dSRodney W. Grimes */ 344b73f64c4SJeff Roberson if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 3454abc71c0SDavid Greenman return FALSE; 346df8bae1dSRodney W. Grimes 347eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 348170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 349d63596ceSJohn Dyson blocksperpage = 0; 350d63596ceSJohn Dyson if (pagesperblock > 0) { 351a316d390SJohn Dyson reqblock = pindex / pagesperblock; 352d63596ceSJohn Dyson } else { 353d63596ceSJohn Dyson blocksperpage = (PAGE_SIZE / bsize); 354d63596ceSJohn Dyson reqblock = pindex * blocksperpage; 355d63596ceSJohn Dyson } 35689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 357ec794849SPoul-Henning Kamp err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 35889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 3590d94caffSDavid Greenman if (err) 36024a1cce3SDavid Greenman return TRUE; 3616eab77f2SJohn Dyson if (bn == -1) 362ced399eeSJohn Dyson return FALSE; 363d63596ceSJohn Dyson if (pagesperblock > 0) { 364a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 365170db9c6SJohn Dyson if (before) { 366170db9c6SJohn Dyson *before *= pagesperblock; 367170db9c6SJohn Dyson *before += poff; 368170db9c6SJohn Dyson } 369170db9c6SJohn Dyson if (after) { 37084d31376SGleb Smirnoff /* 37184d31376SGleb Smirnoff * The BMAP vop can report a partial block in the 372d2596d17SGleb Smirnoff * 'after', but must not report blocks after EOF. 37384d31376SGleb Smirnoff * Assert the latter, and truncate 'after' in case 37484d31376SGleb Smirnoff * of the former. 37584d31376SGleb Smirnoff */ 376d2596d17SGleb Smirnoff KASSERT((reqblock + *after) * pagesperblock < 377d2596d17SGleb Smirnoff roundup2(object->size, pagesperblock), 37884d31376SGleb Smirnoff ("%s: reqblock %jd after %d size %ju", __func__, 37984d31376SGleb Smirnoff (intmax_t )reqblock, *after, 38084d31376SGleb Smirnoff (uintmax_t )object->size)); 381170db9c6SJohn Dyson *after *= pagesperblock; 38284d31376SGleb Smirnoff *after += pagesperblock - (poff + 1); 38384d31376SGleb Smirnoff if (pindex + *after >= object->size) 38484d31376SGleb Smirnoff *after = object->size - 1 - pindex; 385170db9c6SJohn Dyson } 386d63596ceSJohn Dyson } else { 387d63596ceSJohn Dyson if (before) { 388d63596ceSJohn Dyson *before /= blocksperpage; 389d63596ceSJohn Dyson } 390d63596ceSJohn Dyson 391d63596ceSJohn Dyson if (after) { 392d63596ceSJohn Dyson *after /= blocksperpage; 393d63596ceSJohn Dyson } 394d63596ceSJohn Dyson } 395ced399eeSJohn Dyson return TRUE; 396df8bae1dSRodney W. Grimes } 397df8bae1dSRodney W. Grimes 398df8bae1dSRodney W. Grimes /* 399df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 40024a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 401df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 402df8bae1dSRodney W. Grimes * 403df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 404df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 405df8bae1dSRodney W. Grimes */ 406df8bae1dSRodney W. Grimes void 4077ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize) 408df8bae1dSRodney W. Grimes { 4092a8f9ab5SAlan Cox vm_object_t object; 4102a8f9ab5SAlan Cox vm_page_t m; 411c576d121SLuoqi Chen vm_pindex_t nobjsize; 412df8bae1dSRodney W. Grimes 4132a8f9ab5SAlan Cox if ((object = vp->v_object) == NULL) 414df8bae1dSRodney W. Grimes return; 415cb61d698SKonstantin Belousov /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */ 41689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 4179b8851faSKonstantin Belousov if (object->type == OBJT_DEAD) { 4189b8851faSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 4199b8851faSKonstantin Belousov return; 4209b8851faSKonstantin Belousov } 4219b8851faSKonstantin Belousov KASSERT(object->type == OBJT_VNODE, 4229b8851faSKonstantin Belousov ("not vnode-backed object %p", object)); 4232a8f9ab5SAlan Cox if (nsize == object->un_pager.vnp.vnp_size) { 424df8bae1dSRodney W. Grimes /* 425df8bae1dSRodney W. Grimes * Hasn't changed size 426df8bae1dSRodney W. Grimes */ 42789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 428df8bae1dSRodney W. Grimes return; 4292a8f9ab5SAlan Cox } 430c576d121SLuoqi Chen nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 4312a8f9ab5SAlan Cox if (nsize < object->un_pager.vnp.vnp_size) { 432df8bae1dSRodney W. Grimes /* 433bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 434df8bae1dSRodney W. Grimes */ 4352a8f9ab5SAlan Cox if (nobjsize < object->size) 436c576d121SLuoqi Chen vm_object_page_remove(object, nobjsize, object->size, 4376bbee8e2SAlan Cox 0); 438bbc0ec52SDavid Greenman /* 439bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 4403ebeaf59SMatthew Dillon * only partially backed by the vnode. 4413ebeaf59SMatthew Dillon * 4423ebeaf59SMatthew Dillon * XXX for some reason (I don't know yet), if we take a 4433ebeaf59SMatthew Dillon * completely invalid page and mark it partially valid 4443ebeaf59SMatthew Dillon * it can screw up NFS reads, so we don't allow the case. 445bbc0ec52SDavid Greenman */ 4462a8f9ab5SAlan Cox if ((nsize & PAGE_MASK) && 4471b26eb10SAlan Cox (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 4481b26eb10SAlan Cox m->valid != 0) { 4492b6b0df7SMatthew Dillon int base = (int)nsize & PAGE_MASK; 4502b6b0df7SMatthew Dillon int size = PAGE_SIZE - base; 4512b6b0df7SMatthew Dillon 4522b6b0df7SMatthew Dillon /* 4532b6b0df7SMatthew Dillon * Clear out partial-page garbage in case 4542b6b0df7SMatthew Dillon * the page has been mapped. 4552b6b0df7SMatthew Dillon */ 456fff6062aSAlan Cox pmap_zero_page_area(m, base, size); 4572b6b0df7SMatthew Dillon 4582b6b0df7SMatthew Dillon /* 4593c33df62SAlan Cox * Update the valid bits to reflect the blocks that 4603c33df62SAlan Cox * have been zeroed. Some of these valid bits may 4613c33df62SAlan Cox * have already been set. 4623c33df62SAlan Cox */ 463dc874f98SKonstantin Belousov vm_page_set_valid_range(m, base, size); 4643c33df62SAlan Cox 4653c33df62SAlan Cox /* 4663c33df62SAlan Cox * Round "base" to the next block boundary so that the 4673c33df62SAlan Cox * dirty bit for a partially zeroed block is not 4683c33df62SAlan Cox * cleared. 4693c33df62SAlan Cox */ 4703c33df62SAlan Cox base = roundup2(base, DEV_BSIZE); 4713c33df62SAlan Cox 4723c33df62SAlan Cox /* 4733c33df62SAlan Cox * Clear out partial-page dirty bits. 4743ebeaf59SMatthew Dillon * 4753ebeaf59SMatthew Dillon * note that we do not clear out the valid 4763ebeaf59SMatthew Dillon * bits. This would prevent bogus_page 4773ebeaf59SMatthew Dillon * replacement from working properly. 4782b6b0df7SMatthew Dillon */ 4793c33df62SAlan Cox vm_page_clear_dirty(m, base, PAGE_SIZE - base); 480bbc0ec52SDavid Greenman } 481bbc0ec52SDavid Greenman } 482a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 483c576d121SLuoqi Chen object->size = nobjsize; 48489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes 48726f9a767SRodney W. Grimes /* 48826f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 48926f9a767SRodney W. Grimes * file address 49026f9a767SRodney W. Grimes */ 491bff76343SAlan Cox static int 492bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 493bff76343SAlan Cox int *run) 49426f9a767SRodney W. Grimes { 49526f9a767SRodney W. Grimes int bsize; 49626f9a767SRodney W. Grimes int err; 497a316d390SJohn Dyson daddr_t vblock; 498f3aad9a6SBjoern A. Zeeb daddr_t voffset; 49926f9a767SRodney W. Grimes 5002ad036b6SAlan Cox if (address < 0) 5010d94caffSDavid Greenman return -1; 5020d94caffSDavid Greenman 503b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 5042c4488fcSJohn Dyson return -1; 5052c4488fcSJohn Dyson 50626f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 50726f9a767SRodney W. Grimes vblock = address / bsize; 50826f9a767SRodney W. Grimes voffset = address % bsize; 50926f9a767SRodney W. Grimes 510bff76343SAlan Cox err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 511bff76343SAlan Cox if (err == 0) { 512bff76343SAlan Cox if (*rtaddress != -1) 513bff76343SAlan Cox *rtaddress += voffset / DEV_BSIZE; 514efc68ce1SDavid Greenman if (run) { 515efc68ce1SDavid Greenman *run += 1; 516efc68ce1SDavid Greenman *run *= bsize/PAGE_SIZE; 517efc68ce1SDavid Greenman *run -= voffset/PAGE_SIZE; 518efc68ce1SDavid Greenman } 519efc68ce1SDavid Greenman } 52026f9a767SRodney W. Grimes 521bff76343SAlan Cox return (err); 52226f9a767SRodney W. Grimes } 52326f9a767SRodney W. Grimes 52426f9a767SRodney W. Grimes /* 52526f9a767SRodney W. Grimes * small block filesystem vnode pager input 52626f9a767SRodney W. Grimes */ 527f708ef1bSPoul-Henning Kamp static int 5287ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m) 52926f9a767SRodney W. Grimes { 5309c83534dSPoul-Henning Kamp struct vnode *vp; 5319c83534dSPoul-Henning Kamp struct bufobj *bo; 53226f9a767SRodney W. Grimes struct buf *bp; 5339e0ddbd0SAlan Cox struct sf_buf *sf; 534f3aad9a6SBjoern A. Zeeb daddr_t fileaddr; 53526f9a767SRodney W. Grimes vm_offset_t bsize; 536561cc9fcSKonstantin Belousov vm_page_bits_t bits; 537561cc9fcSKonstantin Belousov int error, i; 53826f9a767SRodney W. Grimes 539561cc9fcSKonstantin Belousov error = 0; 54024a1cce3SDavid Greenman vp = object->handle; 541b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 5422c4488fcSJohn Dyson return VM_PAGER_BAD; 5432c4488fcSJohn Dyson 54426f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 5450bdb7528SDavid Greenman 5469c83534dSPoul-Henning Kamp VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 54726f9a767SRodney W. Grimes 5489e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 54926f9a767SRodney W. Grimes 55026f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 55133c67741SMatthew Dillon vm_ooffset_t address; 552bbc0ec52SDavid Greenman 5530d53a17bSAlan Cox bits = vm_page_bits(i * bsize, bsize); 5540d53a17bSAlan Cox if (m->valid & bits) 55526f9a767SRodney W. Grimes continue; 55626f9a767SRodney W. Grimes 55733c67741SMatthew Dillon address = IDX_TO_OFF(m->pindex) + i * bsize; 55833c67741SMatthew Dillon if (address >= object->un_pager.vnp.vnp_size) { 55933c67741SMatthew Dillon fileaddr = -1; 56033c67741SMatthew Dillon } else { 561bff76343SAlan Cox error = vnode_pager_addr(vp, address, &fileaddr, NULL); 562bff76343SAlan Cox if (error) 563bff76343SAlan Cox break; 56433c67741SMatthew Dillon } 56526f9a767SRodney W. Grimes if (fileaddr != -1) { 5661c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 56726f9a767SRodney W. Grimes 56826f9a767SRodney W. Grimes /* build a minimal buffer header */ 56921144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 5706a4b5823SPoul-Henning Kamp bp->b_iodone = bdone; 571bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 572bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 573a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 574a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 5759e0ddbd0SAlan Cox bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 576187f0071SDavid Greenman bp->b_blkno = fileaddr; 5779c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 5781faacf5dSKirk McKusick bp->b_vp = vp; 57926f9a767SRodney W. Grimes bp->b_bcount = bsize; 58026f9a767SRodney W. Grimes bp->b_bufsize = bsize; 5812b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 5825bd65606SJohn Baldwin atomic_add_long(&runningbufspace, bp->b_runningbufspace); 58326f9a767SRodney W. Grimes 58426f9a767SRodney W. Grimes /* do the input */ 5852c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 586b792bebeSPoul-Henning Kamp bstrategy(bp); 58726f9a767SRodney W. Grimes 5886a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnsrd"); 5896a4b5823SPoul-Henning Kamp 590c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 59126f9a767SRodney W. Grimes error = EIO; 59226f9a767SRodney W. Grimes 59326f9a767SRodney W. Grimes /* 59426f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 59526f9a767SRodney W. Grimes */ 5961faacf5dSKirk McKusick bp->b_vp = NULL; 5979c83534dSPoul-Henning Kamp pbrelbo(bp); 5981c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 59926f9a767SRodney W. Grimes if (error) 60026f9a767SRodney W. Grimes break; 6010d53a17bSAlan Cox } else 6029e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 6030d53a17bSAlan Cox KASSERT((m->dirty & bits) == 0, 6040d53a17bSAlan Cox ("vnode_pager_input_smlfs: page %p is dirty", m)); 60589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 6060d53a17bSAlan Cox m->valid |= bits; 60789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60826f9a767SRodney W. Grimes } 6099e0ddbd0SAlan Cox sf_buf_free(sf); 61026f9a767SRodney W. Grimes if (error) { 611a83c285cSDavid Greenman return VM_PAGER_ERROR; 61226f9a767SRodney W. Grimes } 61326f9a767SRodney W. Grimes return VM_PAGER_OK; 61426f9a767SRodney W. Grimes } 61526f9a767SRodney W. Grimes 61626f9a767SRodney W. Grimes /* 617475e8cc3SPoul-Henning Kamp * old style vnode pager input routine 61826f9a767SRodney W. Grimes */ 619f708ef1bSPoul-Henning Kamp static int 6207ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m) 62126f9a767SRodney W. Grimes { 622df8bae1dSRodney W. Grimes struct uio auio; 623df8bae1dSRodney W. Grimes struct iovec aiov; 62426f9a767SRodney W. Grimes int error; 62526f9a767SRodney W. Grimes int size; 6269e0ddbd0SAlan Cox struct sf_buf *sf; 627342a1480SJohn Baldwin struct vnode *vp; 628df8bae1dSRodney W. Grimes 62989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 63026f9a767SRodney W. Grimes error = 0; 631bbc0ec52SDavid Greenman 632df8bae1dSRodney W. Grimes /* 63326f9a767SRodney W. Grimes * Return failure if beyond current EOF 63426f9a767SRodney W. Grimes */ 635a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 63626f9a767SRodney W. Grimes return VM_PAGER_BAD; 63726f9a767SRodney W. Grimes } else { 63826f9a767SRodney W. Grimes size = PAGE_SIZE; 639a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 640a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 64152051abcSAlan Cox vp = object->handle; 64289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 6430bdb7528SDavid Greenman 64426f9a767SRodney W. Grimes /* 645df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 646df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 647df8bae1dSRodney W. Grimes */ 6489e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 6490bdb7528SDavid Greenman 6509e0ddbd0SAlan Cox aiov.iov_base = (caddr_t)sf_buf_kva(sf); 651df8bae1dSRodney W. Grimes aiov.iov_len = size; 652df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 653df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 654a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 655df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 65626f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 657df8bae1dSRodney W. Grimes auio.uio_resid = size; 658b40ce416SJulian Elischer auio.uio_td = curthread; 65926f9a767SRodney W. Grimes 660a854ed98SJohn Baldwin error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 661df8bae1dSRodney W. Grimes if (!error) { 66254d92145SMatthew Dillon int count = size - auio.uio_resid; 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes if (count == 0) 665df8bae1dSRodney W. Grimes error = EINVAL; 66626f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 6679e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + count, 6689e0ddbd0SAlan Cox PAGE_SIZE - count); 669df8bae1dSRodney W. Grimes } 6709e0ddbd0SAlan Cox sf_buf_free(sf); 6711b26eb10SAlan Cox 67289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 673df8bae1dSRodney W. Grimes } 6740d53a17bSAlan Cox KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m)); 6756e3a3f38SRobert V. Baron if (!error) 6766e3a3f38SRobert V. Baron m->valid = VM_PAGE_BITS_ALL; 677a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 67826f9a767SRodney W. Grimes } 67926f9a767SRodney W. Grimes 68026f9a767SRodney W. Grimes /* 68126f9a767SRodney W. Grimes * generic vnode pager input routine 68226f9a767SRodney W. Grimes */ 683170db9c6SJohn Dyson 684ce75f2c3SMike Smith /* 68523955314SAlfred Perlstein * Local media VFS's that do not implement their own VOP_GETPAGES 68647e151ddSRobert Drehmel * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 68747e151ddSRobert Drehmel * to implement the previous behaviour. 688ce75f2c3SMike Smith * 689ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 690ce75f2c3SMike Smith * backing vp's VOP_GETPAGES. 691ce75f2c3SMike Smith */ 692f708ef1bSPoul-Henning Kamp static int 693b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind, 694b0cd2017SGleb Smirnoff int *rahead) 69524a1cce3SDavid Greenman { 696170db9c6SJohn Dyson struct vnode *vp; 697b0cd2017SGleb Smirnoff int rtval; 69895e5e988SJohn Dyson 699170db9c6SJohn Dyson vp = object->handle; 70089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 701b0cd2017SGleb Smirnoff rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead); 70223955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 70323955314SAlfred Perlstein ("vnode_pager: FS getpages not implemented\n")); 70489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 705170db9c6SJohn Dyson return rtval; 706170db9c6SJohn Dyson } 707170db9c6SJohn Dyson 70890effb23SGleb Smirnoff static int 70990effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count, 710b0cd2017SGleb Smirnoff int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg) 71190effb23SGleb Smirnoff { 71290effb23SGleb Smirnoff struct vnode *vp; 71390effb23SGleb Smirnoff int rtval; 71490effb23SGleb Smirnoff 71590effb23SGleb Smirnoff vp = object->handle; 71690effb23SGleb Smirnoff VM_OBJECT_WUNLOCK(object); 717b0cd2017SGleb Smirnoff rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg); 71890effb23SGleb Smirnoff KASSERT(rtval != EOPNOTSUPP, 71990effb23SGleb Smirnoff ("vnode_pager: FS getpages_async not implemented\n")); 72090effb23SGleb Smirnoff VM_OBJECT_WLOCK(object); 72190effb23SGleb Smirnoff return (rtval); 72290effb23SGleb Smirnoff } 72390effb23SGleb Smirnoff 724ce75f2c3SMike Smith /* 72590effb23SGleb Smirnoff * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for 72690effb23SGleb Smirnoff * local filesystems, where partially valid pages can only occur at 72790effb23SGleb Smirnoff * the end of file. 728d15b55c5SKonstantin Belousov */ 729d15b55c5SKonstantin Belousov int 730d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap) 731d15b55c5SKonstantin Belousov { 73290effb23SGleb Smirnoff 733b0cd2017SGleb Smirnoff return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 734b0cd2017SGleb Smirnoff ap->a_rbehind, ap->a_rahead, NULL, NULL)); 73590effb23SGleb Smirnoff } 73690effb23SGleb Smirnoff 73790effb23SGleb Smirnoff int 73890effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap) 73990effb23SGleb Smirnoff { 74090effb23SGleb Smirnoff 741b0cd2017SGleb Smirnoff return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 742b0cd2017SGleb Smirnoff ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg)); 743d15b55c5SKonstantin Belousov } 744d15b55c5SKonstantin Belousov 745d15b55c5SKonstantin Belousov /* 746ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 747ce75f2c3SMike Smith * own vnodes if they fail to implement VOP_GETPAGES. 748ce75f2c3SMike Smith */ 749ce75f2c3SMike Smith int 750b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count, 751b0cd2017SGleb Smirnoff int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg) 752170db9c6SJohn Dyson { 753ce75f2c3SMike Smith vm_object_t object; 7549c83534dSPoul-Henning Kamp struct bufobj *bo; 7550bdb7528SDavid Greenman struct buf *bp; 756b0cd2017SGleb Smirnoff off_t foff; 757e48b82bdSGleb Smirnoff #ifdef INVARIANTS 758e48b82bdSGleb Smirnoff off_t blkno0; 759e48b82bdSGleb Smirnoff #endif 760b0cd2017SGleb Smirnoff int bsize, pagesperblock, *freecnt; 761b0cd2017SGleb Smirnoff int error, before, after, rbehind, rahead, poff, i; 762b0cd2017SGleb Smirnoff int bytecount, secmask; 763ce75f2c3SMike Smith 7649c83534dSPoul-Henning Kamp KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 765b0cd2017SGleb Smirnoff ("%s does not support devices", __func__)); 766b0cd2017SGleb Smirnoff 767b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 768eac91e32SKonstantin Belousov return (VM_PAGER_BAD); 7692c4488fcSJohn Dyson 770eac91e32SKonstantin Belousov object = vp->v_object; 771b0cd2017SGleb Smirnoff foff = IDX_TO_OFF(m[0]->pindex); 77226f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 773b0cd2017SGleb Smirnoff pagesperblock = bsize / PAGE_SIZE; 774b0cd2017SGleb Smirnoff 775b0cd2017SGleb Smirnoff KASSERT(foff < object->un_pager.vnp.vnp_size, 776b0cd2017SGleb Smirnoff ("%s: page %p offset beyond vp %p size", __func__, m[0], vp)); 777b0cd2017SGleb Smirnoff KASSERT(count <= sizeof(bp->b_pages), 778b0cd2017SGleb Smirnoff ("%s: requested %d pages", __func__, count)); 779b0cd2017SGleb Smirnoff 780b0cd2017SGleb Smirnoff /* 781b0cd2017SGleb Smirnoff * The last page has valid blocks. Invalid part can only 782b0cd2017SGleb Smirnoff * exist at the end of file, and the page is made fully valid 783b0cd2017SGleb Smirnoff * by zeroing in vm_pager_get_pages(). 784b0cd2017SGleb Smirnoff */ 785b0cd2017SGleb Smirnoff if (m[count - 1]->valid != 0 && --count == 0) { 786b0cd2017SGleb Smirnoff if (iodone != NULL) 787b0cd2017SGleb Smirnoff iodone(arg, m, 1, 0); 788b0cd2017SGleb Smirnoff return (VM_PAGER_OK); 789b0cd2017SGleb Smirnoff } 790bbc0ec52SDavid Greenman 79141c895a8SGleb Smirnoff /* 79241c895a8SGleb Smirnoff * Synchronous and asynchronous paging operations use different 79341c895a8SGleb Smirnoff * free pbuf counters. This is done to avoid asynchronous requests 79441c895a8SGleb Smirnoff * to consume all pbufs. 79541c895a8SGleb Smirnoff * Allocate the pbuf at the very beginning of the function, so that 79641c895a8SGleb Smirnoff * if we are low on certain kind of pbufs don't even proceed to BMAP, 79741c895a8SGleb Smirnoff * but sleep. 79841c895a8SGleb Smirnoff */ 79973e9030eSGleb Smirnoff freecnt = iodone != NULL ? 80073e9030eSGleb Smirnoff &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt; 80173e9030eSGleb Smirnoff bp = getpbuf(freecnt); 80273e9030eSGleb Smirnoff 80326f9a767SRodney W. Grimes /* 804e122dfc1SGleb Smirnoff * Get the underlying device blocks for the file with VOP_BMAP(). 805e122dfc1SGleb Smirnoff * If the file system doesn't support VOP_BMAP, use old way of 806e122dfc1SGleb Smirnoff * getting pages via VOP_READ. 80726f9a767SRodney W. Grimes */ 808b0cd2017SGleb Smirnoff error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before); 8091de11f1aSAlan Cox if (error == EOPNOTSUPP) { 81073e9030eSGleb Smirnoff relpbuf(bp, freecnt); 81189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 812b0cd2017SGleb Smirnoff for (i = 0; i < count; i++) { 81383c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 81483c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodepgsin); 815b0cd2017SGleb Smirnoff error = vnode_pager_input_old(object, m[i]); 816b0cd2017SGleb Smirnoff if (error) 817b0cd2017SGleb Smirnoff break; 818b0cd2017SGleb Smirnoff } 81989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 82052051abcSAlan Cox return (error); 8211de11f1aSAlan Cox } else if (error != 0) { 82273e9030eSGleb Smirnoff relpbuf(bp, freecnt); 8231de11f1aSAlan Cox return (VM_PAGER_ERROR); 824b0cd2017SGleb Smirnoff } 825bbc0ec52SDavid Greenman 82626f9a767SRodney W. Grimes /* 827b0cd2017SGleb Smirnoff * If the file system supports BMAP, but blocksize is smaller 828b0cd2017SGleb Smirnoff * than a page size, then use special small filesystem code. 82926f9a767SRodney W. Grimes */ 830b0cd2017SGleb Smirnoff if (pagesperblock == 0) { 831ff64a90eSKonstantin Belousov relpbuf(bp, freecnt); 832b0cd2017SGleb Smirnoff for (i = 0; i < count; i++) { 83383c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 83483c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodepgsin); 835b0cd2017SGleb Smirnoff error = vnode_pager_input_smlfs(object, m[i]); 836b0cd2017SGleb Smirnoff if (error) 837b0cd2017SGleb Smirnoff break; 838b0cd2017SGleb Smirnoff } 839b0cd2017SGleb Smirnoff return (error); 84026f9a767SRodney W. Grimes } 8418d17e694SJulian Elischer 84226f9a767SRodney W. Grimes /* 843b0cd2017SGleb Smirnoff * A sparse file can be encountered only for a single page request, 844763df3ecSPedro F. Giffuni * which may not be preceded by call to vm_pager_haspage(). 845a7fecb4dSAlan Cox */ 846b0cd2017SGleb Smirnoff if (bp->b_blkno == -1) { 847b0cd2017SGleb Smirnoff KASSERT(count == 1, 848b0cd2017SGleb Smirnoff ("%s: array[%d] request to a sparse file %p", __func__, 849b0cd2017SGleb Smirnoff count, vp)); 85073e9030eSGleb Smirnoff relpbuf(bp, freecnt); 851b0cd2017SGleb Smirnoff pmap_zero_page(m[0]); 852b0cd2017SGleb Smirnoff KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty", 853b0cd2017SGleb Smirnoff __func__, m[0])); 854a7fecb4dSAlan Cox VM_OBJECT_WLOCK(object); 855b0cd2017SGleb Smirnoff m[0]->valid = VM_PAGE_BITS_ALL; 85689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 857f4f83da0SAlan Cox return (VM_PAGER_OK); 858b0cd2017SGleb Smirnoff } 859b0cd2017SGleb Smirnoff 860e48b82bdSGleb Smirnoff #ifdef INVARIANTS 861e48b82bdSGleb Smirnoff blkno0 = bp->b_blkno; 862e48b82bdSGleb Smirnoff #endif 863b0cd2017SGleb Smirnoff bp->b_blkno += (foff % bsize) / DEV_BSIZE; 864b0cd2017SGleb Smirnoff 865b0cd2017SGleb Smirnoff /* Recalculate blocks available after/before to pages. */ 866b0cd2017SGleb Smirnoff poff = (foff % bsize) / PAGE_SIZE; 867b0cd2017SGleb Smirnoff before *= pagesperblock; 868b0cd2017SGleb Smirnoff before += poff; 869b0cd2017SGleb Smirnoff after *= pagesperblock; 870b0cd2017SGleb Smirnoff after += pagesperblock - (poff + 1); 871b0cd2017SGleb Smirnoff if (m[0]->pindex + after >= object->size) 872b0cd2017SGleb Smirnoff after = object->size - 1 - m[0]->pindex; 873b0cd2017SGleb Smirnoff KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d", 874b0cd2017SGleb Smirnoff __func__, count, after + 1)); 875b0cd2017SGleb Smirnoff after -= count - 1; 876b0cd2017SGleb Smirnoff 877b0cd2017SGleb Smirnoff /* Trim requested rbehind/rahead to possible values. */ 878b0cd2017SGleb Smirnoff rbehind = a_rbehind ? *a_rbehind : 0; 879b0cd2017SGleb Smirnoff rahead = a_rahead ? *a_rahead : 0; 880b0cd2017SGleb Smirnoff rbehind = min(rbehind, before); 881b0cd2017SGleb Smirnoff rbehind = min(rbehind, m[0]->pindex); 882b0cd2017SGleb Smirnoff rahead = min(rahead, after); 883b0cd2017SGleb Smirnoff rahead = min(rahead, object->size - m[count - 1]->pindex); 884e48b82bdSGleb Smirnoff /* 885e48b82bdSGleb Smirnoff * Check that total amount of pages fit into buf. Trim rbehind and 886e48b82bdSGleb Smirnoff * rahead evenly if not. 887e48b82bdSGleb Smirnoff */ 888e48b82bdSGleb Smirnoff if (rbehind + rahead + count > nitems(bp->b_pages)) { 889e48b82bdSGleb Smirnoff int trim, sum; 890e48b82bdSGleb Smirnoff 891e48b82bdSGleb Smirnoff trim = rbehind + rahead + count - nitems(bp->b_pages) + 1; 892e48b82bdSGleb Smirnoff sum = rbehind + rahead; 893e48b82bdSGleb Smirnoff if (rbehind == before) { 894e48b82bdSGleb Smirnoff /* Roundup rbehind trim to block size. */ 895e48b82bdSGleb Smirnoff rbehind -= roundup(trim * rbehind / sum, pagesperblock); 896e48b82bdSGleb Smirnoff if (rbehind < 0) 897e48b82bdSGleb Smirnoff rbehind = 0; 898e48b82bdSGleb Smirnoff } else 899e48b82bdSGleb Smirnoff rbehind -= trim * rbehind / sum; 900e48b82bdSGleb Smirnoff rahead -= trim * rahead / sum; 901e48b82bdSGleb Smirnoff } 902e48b82bdSGleb Smirnoff KASSERT(rbehind + rahead + count <= nitems(bp->b_pages), 903b0cd2017SGleb Smirnoff ("%s: behind %d ahead %d count %d", __func__, 904b0cd2017SGleb Smirnoff rbehind, rahead, count)); 905b0cd2017SGleb Smirnoff 906b0cd2017SGleb Smirnoff /* 907b0cd2017SGleb Smirnoff * Fill in the bp->b_pages[] array with requested and optional 908b0cd2017SGleb Smirnoff * read behind or read ahead pages. Read behind pages are looked 909b0cd2017SGleb Smirnoff * up in a backward direction, down to a first cached page. Same 910b0cd2017SGleb Smirnoff * for read ahead pages, but there is no need to shift the array 911b0cd2017SGleb Smirnoff * in case of encountering a cached page. 912b0cd2017SGleb Smirnoff */ 913b0cd2017SGleb Smirnoff i = bp->b_npages = 0; 914b0cd2017SGleb Smirnoff if (rbehind) { 915b0cd2017SGleb Smirnoff vm_pindex_t startpindex, tpindex; 916b0cd2017SGleb Smirnoff vm_page_t p; 917b0cd2017SGleb Smirnoff 918a7fecb4dSAlan Cox VM_OBJECT_WLOCK(object); 919b0cd2017SGleb Smirnoff startpindex = m[0]->pindex - rbehind; 920b0cd2017SGleb Smirnoff if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL && 921b0cd2017SGleb Smirnoff p->pindex >= startpindex) 922b0cd2017SGleb Smirnoff startpindex = p->pindex + 1; 923b0cd2017SGleb Smirnoff 924b0cd2017SGleb Smirnoff /* tpindex is unsigned; beware of numeric underflow. */ 925b0cd2017SGleb Smirnoff for (tpindex = m[0]->pindex - 1; 926b0cd2017SGleb Smirnoff tpindex >= startpindex && tpindex < m[0]->pindex; 927b0cd2017SGleb Smirnoff tpindex--, i++) { 9287667839aSAlan Cox p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 929b0cd2017SGleb Smirnoff if (p == NULL) { 930b0cd2017SGleb Smirnoff /* Shift the array. */ 931b0cd2017SGleb Smirnoff for (int j = 0; j < i; j++) 932b0cd2017SGleb Smirnoff bp->b_pages[j] = bp->b_pages[j + 933b0cd2017SGleb Smirnoff tpindex + 1 - startpindex]; 934b0cd2017SGleb Smirnoff break; 935b0cd2017SGleb Smirnoff } 936b0cd2017SGleb Smirnoff bp->b_pages[tpindex - startpindex] = p; 937a7fecb4dSAlan Cox } 9380bdb7528SDavid Greenman 939b0cd2017SGleb Smirnoff bp->b_pgbefore = i; 940b0cd2017SGleb Smirnoff bp->b_npages += i; 941b0cd2017SGleb Smirnoff bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE; 942b0cd2017SGleb Smirnoff } else 943b0cd2017SGleb Smirnoff bp->b_pgbefore = 0; 944b0cd2017SGleb Smirnoff 945b0cd2017SGleb Smirnoff /* Requested pages. */ 946b0cd2017SGleb Smirnoff for (int j = 0; j < count; j++, i++) 947b0cd2017SGleb Smirnoff bp->b_pages[i] = m[j]; 948b0cd2017SGleb Smirnoff bp->b_npages += count; 949b0cd2017SGleb Smirnoff 950b0cd2017SGleb Smirnoff if (rahead) { 951b0cd2017SGleb Smirnoff vm_pindex_t endpindex, tpindex; 952b0cd2017SGleb Smirnoff vm_page_t p; 953b0cd2017SGleb Smirnoff 954b0cd2017SGleb Smirnoff if (!VM_OBJECT_WOWNED(object)) 955eac91e32SKonstantin Belousov VM_OBJECT_WLOCK(object); 956b0cd2017SGleb Smirnoff endpindex = m[count - 1]->pindex + rahead + 1; 957b0cd2017SGleb Smirnoff if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL && 958b0cd2017SGleb Smirnoff p->pindex < endpindex) 959b0cd2017SGleb Smirnoff endpindex = p->pindex; 960b0cd2017SGleb Smirnoff if (endpindex > object->size) 961b0cd2017SGleb Smirnoff endpindex = object->size; 962b0cd2017SGleb Smirnoff 963b0cd2017SGleb Smirnoff for (tpindex = m[count - 1]->pindex + 1; 964b0cd2017SGleb Smirnoff tpindex < endpindex; i++, tpindex++) { 9657667839aSAlan Cox p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 966b0cd2017SGleb Smirnoff if (p == NULL) 967b0cd2017SGleb Smirnoff break; 968b0cd2017SGleb Smirnoff bp->b_pages[i] = p; 969eac91e32SKonstantin Belousov } 970b0cd2017SGleb Smirnoff 971b0cd2017SGleb Smirnoff bp->b_pgafter = i - bp->b_npages; 972b0cd2017SGleb Smirnoff bp->b_npages = i; 973b0cd2017SGleb Smirnoff } else 974b0cd2017SGleb Smirnoff bp->b_pgafter = 0; 975b0cd2017SGleb Smirnoff 976b0cd2017SGleb Smirnoff if (VM_OBJECT_WOWNED(object)) 977eac91e32SKonstantin Belousov VM_OBJECT_WUNLOCK(object); 978b0cd2017SGleb Smirnoff 979b0cd2017SGleb Smirnoff /* Report back actual behind/ahead read. */ 980b0cd2017SGleb Smirnoff if (a_rbehind) 981b0cd2017SGleb Smirnoff *a_rbehind = bp->b_pgbefore; 982b0cd2017SGleb Smirnoff if (a_rahead) 983b0cd2017SGleb Smirnoff *a_rahead = bp->b_pgafter; 984b0cd2017SGleb Smirnoff 985e48b82bdSGleb Smirnoff #ifdef INVARIANTS 986dcc0ff5aSGleb Smirnoff KASSERT(bp->b_npages <= nitems(bp->b_pages), 987b0cd2017SGleb Smirnoff ("%s: buf %p overflowed", __func__, bp)); 9884f56243aSGleb Smirnoff for (int j = 1, prev = 0; j < bp->b_npages; j++) { 9891e0c121fSGleb Smirnoff if (bp->b_pages[j] == bogus_page) 9901e0c121fSGleb Smirnoff continue; 9911e0c121fSGleb Smirnoff KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex == 9921e0c121fSGleb Smirnoff j - prev, ("%s: pages array not consecutive, bp %p", 9931e0c121fSGleb Smirnoff __func__, bp)); 9941e0c121fSGleb Smirnoff prev = j; 9951e0c121fSGleb Smirnoff } 996e48b82bdSGleb Smirnoff #endif 997eac91e32SKonstantin Belousov 9980d94caffSDavid Greenman /* 999b0cd2017SGleb Smirnoff * Recalculate first offset and bytecount with regards to read behind. 1000b0cd2017SGleb Smirnoff * Truncate bytecount to vnode real size and round up physical size 1001b0cd2017SGleb Smirnoff * for real devices. 100226f9a767SRodney W. Grimes */ 1003b0cd2017SGleb Smirnoff foff = IDX_TO_OFF(bp->b_pages[0]->pindex); 1004b0cd2017SGleb Smirnoff bytecount = bp->b_npages << PAGE_SHIFT; 1005b0cd2017SGleb Smirnoff if ((foff + bytecount) > object->un_pager.vnp.vnp_size) 1006b0cd2017SGleb Smirnoff bytecount = object->un_pager.vnp.vnp_size - foff; 1007eac91e32SKonstantin Belousov secmask = bo->bo_bsize - 1; 10086229cc50SPoul-Henning Kamp KASSERT(secmask < PAGE_SIZE && secmask > 0, 1009b0cd2017SGleb Smirnoff ("%s: sector size %d too large", __func__, secmask + 1)); 1010b0cd2017SGleb Smirnoff bytecount = (bytecount + secmask) & ~secmask; 101126f9a767SRodney W. Grimes 101226f9a767SRodney W. Grimes /* 1013b0cd2017SGleb Smirnoff * And map the pages to be read into the kva, if the filesystem 10146ce697dcSKonstantin Belousov * requires mapped buffers. 101526f9a767SRodney W. Grimes */ 10162a5eef69SGleb Smirnoff if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 && 10176ce697dcSKonstantin Belousov unmapped_buf_allowed) { 10186ce697dcSKonstantin Belousov bp->b_data = unmapped_buf; 10196ce697dcSKonstantin Belousov bp->b_offset = 0; 1020fade8dd7SJeff Roberson } else { 1021fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 1022b0cd2017SGleb Smirnoff pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages); 1023fade8dd7SJeff Roberson } 102426f9a767SRodney W. Grimes 1025b0cd2017SGleb Smirnoff /* Build a minimal buffer header. */ 102621144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 1027bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 1028bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 1029a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 1030a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 10319c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 10321faacf5dSKirk McKusick bp->b_vp = vp; 1033b0cd2017SGleb Smirnoff bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount; 10342c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 1035e48b82bdSGleb Smirnoff KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) == 1036e48b82bdSGleb Smirnoff (blkno0 - bp->b_blkno) * DEV_BSIZE + 1037e48b82bdSGleb Smirnoff IDX_TO_OFF(m[0]->pindex) % bsize, 1038e48b82bdSGleb Smirnoff ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju " 1039e48b82bdSGleb Smirnoff "blkno0 %ju b_blkno %ju", bsize, 1040e48b82bdSGleb Smirnoff (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex, 1041e48b82bdSGleb Smirnoff (uintmax_t)blkno0, (uintmax_t)bp->b_blkno)); 104290effb23SGleb Smirnoff 1043b0cd2017SGleb Smirnoff atomic_add_long(&runningbufspace, bp->b_runningbufspace); 104483c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 104583c9dea1SGleb Smirnoff VM_CNT_ADD(v_vnodepgsin, bp->b_npages); 1046b0cd2017SGleb Smirnoff 104790effb23SGleb Smirnoff if (iodone != NULL) { /* async */ 1048b0cd2017SGleb Smirnoff bp->b_pgiodone = iodone; 104990effb23SGleb Smirnoff bp->b_caller1 = arg; 105090effb23SGleb Smirnoff bp->b_iodone = vnode_pager_generic_getpages_done_async; 105190effb23SGleb Smirnoff bp->b_flags |= B_ASYNC; 105290effb23SGleb Smirnoff BUF_KERNPROC(bp); 1053b792bebeSPoul-Henning Kamp bstrategy(bp); 1054b0cd2017SGleb Smirnoff return (VM_PAGER_OK); 105590effb23SGleb Smirnoff } else { 105690effb23SGleb Smirnoff bp->b_iodone = bdone; 105790effb23SGleb Smirnoff bstrategy(bp); 10586a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnread"); 105990effb23SGleb Smirnoff error = vnode_pager_generic_getpages_done(bp); 10601bb5ad63SGleb Smirnoff for (i = 0; i < bp->b_npages; i++) 10616ce697dcSKonstantin Belousov bp->b_pages[i] = NULL; 10621faacf5dSKirk McKusick bp->b_vp = NULL; 10639c83534dSPoul-Henning Kamp pbrelbo(bp); 10641c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 106590effb23SGleb Smirnoff return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK); 106690effb23SGleb Smirnoff } 1067b0cd2017SGleb Smirnoff } 106890effb23SGleb Smirnoff 106990effb23SGleb Smirnoff static void 107090effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp) 107190effb23SGleb Smirnoff { 107290effb23SGleb Smirnoff int error; 107390effb23SGleb Smirnoff 107490effb23SGleb Smirnoff error = vnode_pager_generic_getpages_done(bp); 1075b0cd2017SGleb Smirnoff /* Run the iodone upon the requested range. */ 1076b0cd2017SGleb Smirnoff bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore, 1077b0cd2017SGleb Smirnoff bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error); 107890effb23SGleb Smirnoff for (int i = 0; i < bp->b_npages; i++) 107990effb23SGleb Smirnoff bp->b_pages[i] = NULL; 108090effb23SGleb Smirnoff bp->b_vp = NULL; 108190effb23SGleb Smirnoff pbrelbo(bp); 108273e9030eSGleb Smirnoff relpbuf(bp, &vnode_async_pbuf_freecnt); 108390effb23SGleb Smirnoff } 108490effb23SGleb Smirnoff 108590effb23SGleb Smirnoff static int 108690effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp) 108790effb23SGleb Smirnoff { 108890effb23SGleb Smirnoff vm_object_t object; 108990effb23SGleb Smirnoff off_t tfoff, nextoff; 109090effb23SGleb Smirnoff int i, error; 109190effb23SGleb Smirnoff 109290effb23SGleb Smirnoff error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0; 109390effb23SGleb Smirnoff object = bp->b_vp->v_object; 109490effb23SGleb Smirnoff 109590effb23SGleb Smirnoff if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) { 1096fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 1097fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 1098fade8dd7SJeff Roberson pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, 109990effb23SGleb Smirnoff bp->b_npages); 110090effb23SGleb Smirnoff } 1101fade8dd7SJeff Roberson bzero(bp->b_data + bp->b_bcount, 110290effb23SGleb Smirnoff PAGE_SIZE * bp->b_npages - bp->b_bcount); 110390effb23SGleb Smirnoff } 1104fade8dd7SJeff Roberson if (buf_mapped(bp)) { 1105fade8dd7SJeff Roberson pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1106fade8dd7SJeff Roberson bp->b_data = unmapped_buf; 110790effb23SGleb Smirnoff } 110826f9a767SRodney W. Grimes 110989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 111090effb23SGleb Smirnoff for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex); 111190effb23SGleb Smirnoff i < bp->b_npages; i++, tfoff = nextoff) { 11128f9110f6SJohn Dyson vm_page_t mt; 11138f9110f6SJohn Dyson 11148f9110f6SJohn Dyson nextoff = tfoff + PAGE_SIZE; 111590effb23SGleb Smirnoff mt = bp->b_pages[i]; 11168f9110f6SJohn Dyson 111754746b67SDmitrij Tejblum if (nextoff <= object->un_pager.vnp.vnp_size) { 11188d17e694SJulian Elischer /* 11198d17e694SJulian Elischer * Read filled up entire page. 11208d17e694SJulian Elischer */ 11218f9110f6SJohn Dyson mt->valid = VM_PAGE_BITS_ALL; 1122016a3c93SAlan Cox KASSERT(mt->dirty == 0, 112379f0deb9SGleb Smirnoff ("%s: page %p is dirty", __func__, mt)); 1124016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(mt), 112579f0deb9SGleb Smirnoff ("%s: page %p is mapped", __func__, mt)); 11268f9110f6SJohn Dyson } else { 11278d17e694SJulian Elischer /* 112842eb4108SAlan Cox * Read did not fill up entire page. 11298d17e694SJulian Elischer * 11308d17e694SJulian Elischer * Currently we do not set the entire page valid, 11318d17e694SJulian Elischer * we just try to clear the piece that we couldn't 11328d17e694SJulian Elischer * read. 11338d17e694SJulian Elischer */ 1134dc874f98SKonstantin Belousov vm_page_set_valid_range(mt, 0, 113554746b67SDmitrij Tejblum object->un_pager.vnp.vnp_size - tfoff); 113642eb4108SAlan Cox KASSERT((mt->dirty & vm_page_bits(0, 113742eb4108SAlan Cox object->un_pager.vnp.vnp_size - tfoff)) == 0, 113879f0deb9SGleb Smirnoff ("%s: page %p is dirty", __func__, mt)); 11398f9110f6SJohn Dyson } 11408f9110f6SJohn Dyson 1141b0cd2017SGleb Smirnoff if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter) 1142b6c00483SKonstantin Belousov vm_page_readahead_finish(mt); 114303679e23SAlan Cox } 114489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 114590effb23SGleb Smirnoff if (error != 0) 114690effb23SGleb Smirnoff printf("%s: I/O read error %d\n", __func__, error); 114790effb23SGleb Smirnoff 114890effb23SGleb Smirnoff return (error); 114926f9a767SRodney W. Grimes } 115026f9a767SRodney W. Grimes 1151ce75f2c3SMike Smith /* 1152ce75f2c3SMike Smith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 1153ce75f2c3SMike Smith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 1154ce75f2c3SMike Smith * vnode_pager_generic_putpages() to implement the previous behaviour. 1155ce75f2c3SMike Smith * 1156ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 1157ce75f2c3SMike Smith * backing vp's VOP_PUTPAGES. 1158ce75f2c3SMike Smith */ 1159e4542174SMatthew Dillon static void 11607ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count, 116133cad9e9SKonstantin Belousov int flags, int *rtvals) 1162170db9c6SJohn Dyson { 1163170db9c6SJohn Dyson int rtval; 1164170db9c6SJohn Dyson struct vnode *vp; 116586ffbd76SMike Smith int bytes = count * PAGE_SIZE; 1166ad980522SJohn Dyson 11670e3cdf2cSAlan Cox /* 11680e3cdf2cSAlan Cox * Force synchronous operation if we are extremely low on memory 11690e3cdf2cSAlan Cox * to prevent a low-memory deadlock. VOP operations often need to 11700e3cdf2cSAlan Cox * allocate more memory to initiate the I/O ( i.e. do a BMAP 11710e3cdf2cSAlan Cox * operation ). The swapper handles the case by limiting the amount 11720e3cdf2cSAlan Cox * of asynchronous I/O, but that sort of solution doesn't scale well 11730e3cdf2cSAlan Cox * for the vnode pager without a lot of work. 11740e3cdf2cSAlan Cox * 11750e3cdf2cSAlan Cox * Also, the backing vnode's iodone routine may not wake the pageout 11760e3cdf2cSAlan Cox * daemon up. This should be probably be addressed XXX. 11770e3cdf2cSAlan Cox */ 11780e3cdf2cSAlan Cox 1179e2068d0bSJeff Roberson if (vm_page_count_min()) 118033cad9e9SKonstantin Belousov flags |= VM_PAGER_PUT_SYNC; 11810e3cdf2cSAlan Cox 11820e3cdf2cSAlan Cox /* 11830e3cdf2cSAlan Cox * Call device-specific putpages function 11840e3cdf2cSAlan Cox */ 1185170db9c6SJohn Dyson vp = object->handle; 118689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 118733cad9e9SKonstantin Belousov rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals); 118823955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 118923955314SAlfred Perlstein ("vnode_pager: stale FS putpages\n")); 119089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1191170db9c6SJohn Dyson } 1192170db9c6SJohn Dyson 119305877a85SKonstantin Belousov static int 119405877a85SKonstantin Belousov vn_off2bidx(vm_ooffset_t offset) 119505877a85SKonstantin Belousov { 119605877a85SKonstantin Belousov 119705877a85SKonstantin Belousov return ((offset & PAGE_MASK) / DEV_BSIZE); 119805877a85SKonstantin Belousov } 119905877a85SKonstantin Belousov 120005877a85SKonstantin Belousov static bool 120105877a85SKonstantin Belousov vn_dirty_blk(vm_page_t m, vm_ooffset_t offset) 120205877a85SKonstantin Belousov { 120305877a85SKonstantin Belousov 120405877a85SKonstantin Belousov KASSERT(IDX_TO_OFF(m->pindex) <= offset && 120505877a85SKonstantin Belousov offset < IDX_TO_OFF(m->pindex + 1), 120605877a85SKonstantin Belousov ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex, 120705877a85SKonstantin Belousov (uintmax_t)offset)); 120805877a85SKonstantin Belousov return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0); 120905877a85SKonstantin Belousov } 1210ce75f2c3SMike Smith 121126f9a767SRodney W. Grimes /* 1212ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 12134491ea91SEivind Eklund * own vnodes if they fail to implement VOP_PUTPAGES. 12142b6b0df7SMatthew Dillon * 12152b6b0df7SMatthew Dillon * This is typically called indirectly via the pageout daemon and 1216763df3ecSPedro F. Giffuni * clustering has already typically occurred, so in general we ask the 12172b6b0df7SMatthew Dillon * underlying filesystem to write the data out asynchronously rather 12182b6b0df7SMatthew Dillon * then delayed. 121926f9a767SRodney W. Grimes */ 1220ce75f2c3SMike Smith int 1221c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount, 1222c46b90e9SAlan Cox int flags, int *rtvals) 122326f9a767SRodney W. Grimes { 1224ce75f2c3SMike Smith vm_object_t object; 1225c46b90e9SAlan Cox vm_page_t m; 122605877a85SKonstantin Belousov vm_ooffset_t maxblksz, next_offset, poffset, prev_offset; 1227f6b04d2bSDavid Greenman struct uio auio; 1228f6b04d2bSDavid Greenman struct iovec aiov; 122905877a85SKonstantin Belousov off_t prev_resid, wrsz; 1230e6c44f65SKonstantin Belousov int count, error, i, maxsize, ncount, pgoff, ppscheck; 123105877a85SKonstantin Belousov bool in_hole; 1232dd498befSPaul Saab static struct timeval lastfail; 1233dd498befSPaul Saab static int curfail; 123426f9a767SRodney W. Grimes 1235ce75f2c3SMike Smith object = vp->v_object; 1236ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 1237ce75f2c3SMike Smith 123826f9a767SRodney W. Grimes for (i = 0; i < count; i++) 1239031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_ERROR; 124026f9a767SRodney W. Grimes 1241c46b90e9SAlan Cox if ((int64_t)ma[0]->pindex < 0) { 1242e6c44f65SKonstantin Belousov printf("vnode_pager_generic_putpages: " 1243e6c44f65SKonstantin Belousov "attempt to write meta-data 0x%jx(%lx)\n", 1244e6c44f65SKonstantin Belousov (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty); 1245f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 1246e6c44f65SKonstantin Belousov return (VM_PAGER_BAD); 12470d94caffSDavid Greenman } 12480bdb7528SDavid Greenman 1249f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 1250f6b04d2bSDavid Greenman ncount = count; 125126f9a767SRodney W. Grimes 1252c46b90e9SAlan Cox poffset = IDX_TO_OFF(ma[0]->pindex); 125300a6f47fSMatthew Dillon 125400a6f47fSMatthew Dillon /* 125500a6f47fSMatthew Dillon * If the page-aligned write is larger then the actual file we 1256763df3ecSPedro F. Giffuni * have to invalidate pages occurring beyond the file EOF. However, 125700a6f47fSMatthew Dillon * there is an edge case where a file may not be page-aligned where 125800a6f47fSMatthew Dillon * the last page is partially invalid. In this case the filesystem 125900a6f47fSMatthew Dillon * may not properly clear the dirty bits for the entire page (which 126000a6f47fSMatthew Dillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 126100a6f47fSMatthew Dillon * With the page locked we are free to fix-up the dirty bits here. 12623ebeaf59SMatthew Dillon * 12633ebeaf59SMatthew Dillon * We do not under any circumstances truncate the valid bits, as 12643ebeaf59SMatthew Dillon * this will screw up bogus page replacement. 126500a6f47fSMatthew Dillon */ 1266b3d4ab66SKonstantin Belousov VM_OBJECT_RLOCK(object); 1267a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 1268b3d4ab66SKonstantin Belousov if (!VM_OBJECT_TRYUPGRADE(object)) { 1269b3d4ab66SKonstantin Belousov VM_OBJECT_RUNLOCK(object); 1270b3d4ab66SKonstantin Belousov VM_OBJECT_WLOCK(object); 1271b3d4ab66SKonstantin Belousov if (maxsize + poffset <= object->un_pager.vnp.vnp_size) 1272b3d4ab66SKonstantin Belousov goto downgrade; 1273b3d4ab66SKonstantin Belousov } 127400a6f47fSMatthew Dillon if (object->un_pager.vnp.vnp_size > poffset) { 1275a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 1276aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 127700a6f47fSMatthew Dillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1278938cdc42SKonstantin Belousov pgoff = roundup2(pgoff, DEV_BSIZE); 1279938cdc42SKonstantin Belousov 1280c46b90e9SAlan Cox /* 1281c46b90e9SAlan Cox * If the object is locked and the following 1282c46b90e9SAlan Cox * conditions hold, then the page's dirty 1283c46b90e9SAlan Cox * field cannot be concurrently changed by a 1284c46b90e9SAlan Cox * pmap operation. 1285c46b90e9SAlan Cox */ 1286c46b90e9SAlan Cox m = ma[ncount - 1]; 1287c7aebda8SAttilio Rao vm_page_assert_sbusied(m); 12886031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1289c46b90e9SAlan Cox ("vnode_pager_generic_putpages: page %p is not read-only", m)); 1290e6c44f65SKonstantin Belousov MPASS(m->dirty != 0); 1291c46b90e9SAlan Cox vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 1292c46b90e9SAlan Cox pgoff); 129300a6f47fSMatthew Dillon } 129400a6f47fSMatthew Dillon } else { 129500a6f47fSMatthew Dillon maxsize = 0; 129600a6f47fSMatthew Dillon ncount = 0; 129700a6f47fSMatthew Dillon } 1298e6c44f65SKonstantin Belousov for (i = ncount; i < count; i++) 1299f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 1300b3d4ab66SKonstantin Belousov downgrade: 1301b3d4ab66SKonstantin Belousov VM_OBJECT_LOCK_DOWNGRADE(object); 1302f6b04d2bSDavid Greenman } 130326f9a767SRodney W. Grimes 1304f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 1305f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 1306f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 1307e6c44f65SKonstantin Belousov auio.uio_td = NULL; 130805877a85SKonstantin Belousov maxblksz = roundup2(poffset + maxsize, DEV_BSIZE); 130905877a85SKonstantin Belousov 131005877a85SKonstantin Belousov for (prev_offset = poffset; prev_offset < maxblksz;) { 131105877a85SKonstantin Belousov /* Skip clean blocks. */ 131205877a85SKonstantin Belousov for (in_hole = true; in_hole && prev_offset < maxblksz;) { 131305877a85SKonstantin Belousov m = ma[OFF_TO_IDX(prev_offset - poffset)]; 131405877a85SKonstantin Belousov for (i = vn_off2bidx(prev_offset); 131505877a85SKonstantin Belousov i < sizeof(vm_page_bits_t) * NBBY && 131605877a85SKonstantin Belousov prev_offset < maxblksz; i++) { 131705877a85SKonstantin Belousov if (vn_dirty_blk(m, prev_offset)) { 131805877a85SKonstantin Belousov in_hole = false; 131905877a85SKonstantin Belousov break; 132005877a85SKonstantin Belousov } 132105877a85SKonstantin Belousov prev_offset += DEV_BSIZE; 132205877a85SKonstantin Belousov } 132305877a85SKonstantin Belousov } 132405877a85SKonstantin Belousov if (in_hole) 132505877a85SKonstantin Belousov goto write_done; 132605877a85SKonstantin Belousov 132705877a85SKonstantin Belousov /* Find longest run of dirty blocks. */ 132805877a85SKonstantin Belousov for (next_offset = prev_offset; next_offset < maxblksz;) { 132905877a85SKonstantin Belousov m = ma[OFF_TO_IDX(next_offset - poffset)]; 133005877a85SKonstantin Belousov for (i = vn_off2bidx(next_offset); 133105877a85SKonstantin Belousov i < sizeof(vm_page_bits_t) * NBBY && 133205877a85SKonstantin Belousov next_offset < maxblksz; i++) { 133305877a85SKonstantin Belousov if (!vn_dirty_blk(m, next_offset)) 133405877a85SKonstantin Belousov goto start_write; 133505877a85SKonstantin Belousov next_offset += DEV_BSIZE; 133605877a85SKonstantin Belousov } 133705877a85SKonstantin Belousov } 133805877a85SKonstantin Belousov start_write: 133905877a85SKonstantin Belousov if (next_offset > poffset + maxsize) 134005877a85SKonstantin Belousov next_offset = poffset + maxsize; 134105877a85SKonstantin Belousov 134205877a85SKonstantin Belousov /* 134305877a85SKonstantin Belousov * Getting here requires finding a dirty block in the 134405877a85SKonstantin Belousov * 'skip clean blocks' loop. 134505877a85SKonstantin Belousov */ 134605877a85SKonstantin Belousov MPASS(prev_offset < next_offset); 134705877a85SKonstantin Belousov 1348b3d4ab66SKonstantin Belousov VM_OBJECT_RUNLOCK(object); 134905877a85SKonstantin Belousov aiov.iov_base = NULL; 135005877a85SKonstantin Belousov auio.uio_iovcnt = 1; 135105877a85SKonstantin Belousov auio.uio_offset = prev_offset; 135205877a85SKonstantin Belousov prev_resid = auio.uio_resid = aiov.iov_len = next_offset - 135305877a85SKonstantin Belousov prev_offset; 135405877a85SKonstantin Belousov error = VOP_WRITE(vp, &auio, 135505877a85SKonstantin Belousov vnode_pager_putpages_ioflags(flags), curthread->td_ucred); 135605877a85SKonstantin Belousov 135705877a85SKonstantin Belousov wrsz = prev_resid - auio.uio_resid; 135805877a85SKonstantin Belousov if (wrsz == 0) { 135905877a85SKonstantin Belousov if (ppsratecheck(&lastfail, &curfail, 1) != 0) { 136005877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: " 136105877a85SKonstantin Belousov "zero-length write at %ju resid %zd\n", 136205877a85SKonstantin Belousov auio.uio_offset, auio.uio_resid); 136305877a85SKonstantin Belousov } 1364b3d4ab66SKonstantin Belousov VM_OBJECT_RLOCK(object); 136505877a85SKonstantin Belousov break; 136605877a85SKonstantin Belousov } 136705877a85SKonstantin Belousov 136805877a85SKonstantin Belousov /* Adjust the starting offset for next iteration. */ 136905877a85SKonstantin Belousov prev_offset += wrsz; 137005877a85SKonstantin Belousov MPASS(auio.uio_offset == prev_offset); 1371f6b04d2bSDavid Greenman 13723dbb0ca6SKonstantin Belousov ppscheck = 0; 137305877a85SKonstantin Belousov if (error != 0 && (ppscheck = ppsratecheck(&lastfail, 137405877a85SKonstantin Belousov &curfail, 1)) != 0) 137505877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: I/O error %d\n", 137605877a85SKonstantin Belousov error); 1377e6c44f65SKonstantin Belousov if (auio.uio_resid != 0 && (ppscheck != 0 || 1378e6c44f65SKonstantin Belousov ppsratecheck(&lastfail, &curfail, 1) != 0)) 137905877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: residual I/O %zd " 138005877a85SKonstantin Belousov "at %ju\n", auio.uio_resid, 138105877a85SKonstantin Belousov (uintmax_t)ma[0]->pindex); 1382b3d4ab66SKonstantin Belousov VM_OBJECT_RLOCK(object); 138305877a85SKonstantin Belousov if (error != 0 || auio.uio_resid != 0) 138405877a85SKonstantin Belousov break; 138505877a85SKonstantin Belousov } 138605877a85SKonstantin Belousov write_done: 138705877a85SKonstantin Belousov /* Mark completely processed pages. */ 138805877a85SKonstantin Belousov for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++) 138926f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 139005877a85SKonstantin Belousov /* Mark partial EOF page. */ 139105877a85SKonstantin Belousov if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0) 139205877a85SKonstantin Belousov rtvals[i++] = VM_PAGER_OK; 139305877a85SKonstantin Belousov /* Unwritten pages in range, free bonus if the page is clean. */ 139405877a85SKonstantin Belousov for (; i < ncount; i++) 139505877a85SKonstantin Belousov rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR; 1396b3d4ab66SKonstantin Belousov VM_OBJECT_RUNLOCK(object); 139705877a85SKonstantin Belousov VM_CNT_ADD(v_vnodepgsout, i); 139805877a85SKonstantin Belousov VM_CNT_INC(v_vnodeout); 1399e6c44f65SKonstantin Belousov return (rtvals[0]); 140026f9a767SRodney W. Grimes } 1401031ec8c1SKonstantin Belousov 140265b9599aSKonstantin Belousov int 140365b9599aSKonstantin Belousov vnode_pager_putpages_ioflags(int pager_flags) 140465b9599aSKonstantin Belousov { 140565b9599aSKonstantin Belousov int ioflags; 140665b9599aSKonstantin Belousov 140765b9599aSKonstantin Belousov /* 140865b9599aSKonstantin Belousov * Pageouts are already clustered, use IO_ASYNC to force a 140965b9599aSKonstantin Belousov * bawrite() rather then a bdwrite() to prevent paging I/O 141065b9599aSKonstantin Belousov * from saturating the buffer cache. Dummy-up the sequential 141165b9599aSKonstantin Belousov * heuristic to cause large ranges to cluster. If neither 141265b9599aSKonstantin Belousov * IO_SYNC or IO_ASYNC is set, the system decides how to 141365b9599aSKonstantin Belousov * cluster. 141465b9599aSKonstantin Belousov */ 141565b9599aSKonstantin Belousov ioflags = IO_VMIO; 141665b9599aSKonstantin Belousov if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0) 141765b9599aSKonstantin Belousov ioflags |= IO_SYNC; 141865b9599aSKonstantin Belousov else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0) 141965b9599aSKonstantin Belousov ioflags |= IO_ASYNC; 142065b9599aSKonstantin Belousov ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0; 142165b9599aSKonstantin Belousov ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0; 142265b9599aSKonstantin Belousov ioflags |= IO_SEQMAX << IO_SEQSHIFT; 142365b9599aSKonstantin Belousov return (ioflags); 142465b9599aSKonstantin Belousov } 142565b9599aSKonstantin Belousov 1426555b7bb4SKonstantin Belousov /* 1427555b7bb4SKonstantin Belousov * vnode_pager_undirty_pages(). 1428555b7bb4SKonstantin Belousov * 1429555b7bb4SKonstantin Belousov * A helper to mark pages as clean after pageout that was possibly 1430555b7bb4SKonstantin Belousov * done with a short write. The lpos argument specifies the page run 1431555b7bb4SKonstantin Belousov * length in bytes, and the written argument specifies how many bytes 1432555b7bb4SKonstantin Belousov * were actually written. eof is the offset past the last valid byte 1433555b7bb4SKonstantin Belousov * in the vnode using the absolute file position of the first byte in 1434555b7bb4SKonstantin Belousov * the run as the base from which it is computed. 1435555b7bb4SKonstantin Belousov */ 1436031ec8c1SKonstantin Belousov void 1437555b7bb4SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof, 1438555b7bb4SKonstantin Belousov int lpos) 1439031ec8c1SKonstantin Belousov { 14409d17da3bSKonstantin Belousov vm_object_t obj; 1441555b7bb4SKonstantin Belousov int i, pos, pos_devb; 1442031ec8c1SKonstantin Belousov 1443555b7bb4SKonstantin Belousov if (written == 0 && eof >= lpos) 14449d17da3bSKonstantin Belousov return; 14459d17da3bSKonstantin Belousov obj = ma[0]->object; 144689f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 1447031ec8c1SKonstantin Belousov for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) { 1448031ec8c1SKonstantin Belousov if (pos < trunc_page(written)) { 1449031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_OK; 1450031ec8c1SKonstantin Belousov vm_page_undirty(ma[i]); 1451031ec8c1SKonstantin Belousov } else { 1452031ec8c1SKonstantin Belousov /* Partially written page. */ 1453031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_AGAIN; 1454031ec8c1SKonstantin Belousov vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK); 1455031ec8c1SKonstantin Belousov } 1456031ec8c1SKonstantin Belousov } 1457555b7bb4SKonstantin Belousov if (eof >= lpos) /* avoid truncation */ 1458555b7bb4SKonstantin Belousov goto done; 1459555b7bb4SKonstantin Belousov for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) { 1460555b7bb4SKonstantin Belousov if (pos != trunc_page(pos)) { 1461555b7bb4SKonstantin Belousov /* 1462555b7bb4SKonstantin Belousov * The page contains the last valid byte in 1463555b7bb4SKonstantin Belousov * the vnode, mark the rest of the page as 1464555b7bb4SKonstantin Belousov * clean, potentially making the whole page 1465555b7bb4SKonstantin Belousov * clean. 1466555b7bb4SKonstantin Belousov */ 1467555b7bb4SKonstantin Belousov pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE); 1468555b7bb4SKonstantin Belousov vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE - 1469555b7bb4SKonstantin Belousov pos_devb); 1470555b7bb4SKonstantin Belousov 1471555b7bb4SKonstantin Belousov /* 1472555b7bb4SKonstantin Belousov * If the page was cleaned, report the pageout 1473555b7bb4SKonstantin Belousov * on it as successful. msync() no longer 1474555b7bb4SKonstantin Belousov * needs to write out the page, endlessly 1475555b7bb4SKonstantin Belousov * creating write requests and dirty buffers. 1476555b7bb4SKonstantin Belousov */ 1477555b7bb4SKonstantin Belousov if (ma[i]->dirty == 0) 1478555b7bb4SKonstantin Belousov rtvals[i] = VM_PAGER_OK; 1479555b7bb4SKonstantin Belousov 1480555b7bb4SKonstantin Belousov pos = round_page(pos); 1481555b7bb4SKonstantin Belousov } else { 1482555b7bb4SKonstantin Belousov /* vm_pageout_flush() clears dirty */ 1483555b7bb4SKonstantin Belousov rtvals[i] = VM_PAGER_BAD; 1484555b7bb4SKonstantin Belousov pos += PAGE_SIZE; 1485555b7bb4SKonstantin Belousov } 1486555b7bb4SKonstantin Belousov } 1487555b7bb4SKonstantin Belousov done: 148889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 1489031ec8c1SKonstantin Belousov } 149084110e7eSKonstantin Belousov 149184110e7eSKonstantin Belousov void 149284110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start, 149384110e7eSKonstantin Belousov vm_offset_t end) 149484110e7eSKonstantin Belousov { 149584110e7eSKonstantin Belousov struct vnode *vp; 149684110e7eSKonstantin Belousov vm_ooffset_t old_wm; 149784110e7eSKonstantin Belousov 149889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 149984110e7eSKonstantin Belousov if (object->type != OBJT_VNODE) { 150089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 150184110e7eSKonstantin Belousov return; 150284110e7eSKonstantin Belousov } 150384110e7eSKonstantin Belousov old_wm = object->un_pager.vnp.writemappings; 150484110e7eSKonstantin Belousov object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start; 150584110e7eSKonstantin Belousov vp = object->handle; 150684110e7eSKonstantin Belousov if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) { 150784110e7eSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "v_writecount inc"); 1508140dedb8SKonstantin Belousov VOP_ADD_WRITECOUNT(vp, 1); 1509b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 1510b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 151184110e7eSKonstantin Belousov } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) { 151284110e7eSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "v_writecount dec"); 1513140dedb8SKonstantin Belousov VOP_ADD_WRITECOUNT(vp, -1); 1514b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 1515b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 151684110e7eSKonstantin Belousov } 151789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 151884110e7eSKonstantin Belousov } 151984110e7eSKonstantin Belousov 152084110e7eSKonstantin Belousov void 152184110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start, 152284110e7eSKonstantin Belousov vm_offset_t end) 152384110e7eSKonstantin Belousov { 152484110e7eSKonstantin Belousov struct vnode *vp; 152584110e7eSKonstantin Belousov struct mount *mp; 152684110e7eSKonstantin Belousov vm_offset_t inc; 152784110e7eSKonstantin Belousov 152889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 152984110e7eSKonstantin Belousov 153084110e7eSKonstantin Belousov /* 153184110e7eSKonstantin Belousov * First, recheck the object type to account for the race when 153284110e7eSKonstantin Belousov * the vnode is reclaimed. 153384110e7eSKonstantin Belousov */ 153484110e7eSKonstantin Belousov if (object->type != OBJT_VNODE) { 153589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 153684110e7eSKonstantin Belousov return; 153784110e7eSKonstantin Belousov } 153884110e7eSKonstantin Belousov 153984110e7eSKonstantin Belousov /* 154084110e7eSKonstantin Belousov * Optimize for the case when writemappings is not going to 154184110e7eSKonstantin Belousov * zero. 154284110e7eSKonstantin Belousov */ 154384110e7eSKonstantin Belousov inc = end - start; 154484110e7eSKonstantin Belousov if (object->un_pager.vnp.writemappings != inc) { 154584110e7eSKonstantin Belousov object->un_pager.vnp.writemappings -= inc; 154689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 154784110e7eSKonstantin Belousov return; 154884110e7eSKonstantin Belousov } 154984110e7eSKonstantin Belousov 155084110e7eSKonstantin Belousov vp = object->handle; 155184110e7eSKonstantin Belousov vhold(vp); 155289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 155384110e7eSKonstantin Belousov mp = NULL; 155484110e7eSKonstantin Belousov vn_start_write(vp, &mp, V_WAIT); 155584110e7eSKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 155684110e7eSKonstantin Belousov 155784110e7eSKonstantin Belousov /* 155884110e7eSKonstantin Belousov * Decrement the object's writemappings, by swapping the start 155984110e7eSKonstantin Belousov * and end arguments for vnode_pager_update_writecount(). If 156084110e7eSKonstantin Belousov * there was not a race with vnode reclaimation, then the 156184110e7eSKonstantin Belousov * vnode's v_writecount is decremented. 156284110e7eSKonstantin Belousov */ 156384110e7eSKonstantin Belousov vnode_pager_update_writecount(object, end, start); 156484110e7eSKonstantin Belousov VOP_UNLOCK(vp, 0); 156584110e7eSKonstantin Belousov vdrop(vp); 156684110e7eSKonstantin Belousov if (mp != NULL) 156784110e7eSKonstantin Belousov vn_finished_write(mp); 156884110e7eSKonstantin Belousov } 1569