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> 61756a5412SGleb Smirnoff #include <sys/kernel.h> 62df8bae1dSRodney W. Grimes #include <sys/systm.h> 63e5818a53SJeff Roberson #include <sys/sysctl.h> 64df8bae1dSRodney W. Grimes #include <sys/proc.h> 65df8bae1dSRodney W. Grimes #include <sys/vnode.h> 66df8bae1dSRodney W. Grimes #include <sys/mount.h> 679626b608SPoul-Henning Kamp #include <sys/bio.h> 6824a1cce3SDavid Greenman #include <sys/buf.h> 69efeaf95aSDavid Greenman #include <sys/vmmeter.h> 70daec9284SConrad Meyer #include <sys/ktr.h> 71d07a6d3fSPoul-Henning Kamp #include <sys/limits.h> 7224579ca1SMatthew Dillon #include <sys/conf.h> 7351df5321SJeff Roberson #include <sys/refcount.h> 7489f6b863SAttilio Rao #include <sys/rwlock.h> 759e0ddbd0SAlan Cox #include <sys/sf_buf.h> 76e5818a53SJeff Roberson #include <sys/domainset.h> 7700a3fe96SKonstantin Belousov #include <sys/user.h> 78df8bae1dSRodney W. Grimes 794f12e0acSSuleiman Souhlal #include <machine/atomic.h> 804f12e0acSSuleiman Souhlal 81df8bae1dSRodney W. Grimes #include <vm/vm.h> 821c771f92SKonstantin Belousov #include <vm/vm_param.h> 83efeaf95aSDavid Greenman #include <vm/vm_object.h> 84df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8524a1cce3SDavid Greenman #include <vm/vm_pager.h> 861efb74fbSJohn Dyson #include <vm/vm_map.h> 87df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h> 88efeaf95aSDavid Greenman #include <vm/vm_extern.h> 89756a5412SGleb Smirnoff #include <vm/uma.h> 90df8bae1dSRodney W. Grimes 91bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 92bff76343SAlan Cox daddr_t *rtaddress, int *run); 9311caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 9411caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 9511caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t); 96b0cd2017SGleb Smirnoff static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *); 97b0cd2017SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 98b0cd2017SGleb Smirnoff int *, vop_getpages_iodone_t, void *); 9933cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 10011caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 1013364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 1023364c323SKonstantin Belousov vm_ooffset_t, struct ucred *cred); 10390effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *); 10490effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *); 105fe7bcbafSKyle Evans static void vnode_pager_update_writecount(vm_object_t, vm_offset_t, 106fe7bcbafSKyle Evans vm_offset_t); 107fe7bcbafSKyle Evans static void vnode_pager_release_writecount(vm_object_t, vm_offset_t, 108fe7bcbafSKyle Evans vm_offset_t); 109192112b7SKonstantin Belousov static void vnode_pager_getvp(vm_object_t, struct vnode **, bool *); 1100b8253a7SBruce Evans 111d474440aSKonstantin Belousov const struct pagerops vnodepagerops = { 11200a3fe96SKonstantin Belousov .pgo_kvme_type = KVME_TYPE_VNODE, 1134e658600SPoul-Henning Kamp .pgo_alloc = vnode_pager_alloc, 1144e658600SPoul-Henning Kamp .pgo_dealloc = vnode_pager_dealloc, 1154e658600SPoul-Henning Kamp .pgo_getpages = vnode_pager_getpages, 11690effb23SGleb Smirnoff .pgo_getpages_async = vnode_pager_getpages_async, 1174e658600SPoul-Henning Kamp .pgo_putpages = vnode_pager_putpages, 1184e658600SPoul-Henning Kamp .pgo_haspage = vnode_pager_haspage, 119fe7bcbafSKyle Evans .pgo_update_writecount = vnode_pager_update_writecount, 120fe7bcbafSKyle Evans .pgo_release_writecount = vnode_pager_release_writecount, 121180bcaa4SKonstantin Belousov .pgo_set_writeable_dirty = vm_object_set_writeable_dirty_, 122c23c555bSKonstantin Belousov .pgo_mightbedirty = vm_object_mightbedirty_, 123192112b7SKonstantin Belousov .pgo_getvp = vnode_pager_getvp, 124df8bae1dSRodney W. Grimes }; 125df8bae1dSRodney W. Grimes 126e5818a53SJeff Roberson static struct domainset *vnode_domainset = NULL; 127e5818a53SJeff Roberson 128a314aba8SMateusz Guzik SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset, 129a314aba8SMateusz Guzik CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_RW, &vnode_domainset, 0, 130a314aba8SMateusz Guzik sysctl_handle_domainset, "A", "Default vnode NUMA policy"); 131e5818a53SJeff Roberson 13266fb0b1aSGleb Smirnoff static int nvnpbufs; 13366fb0b1aSGleb Smirnoff SYSCTL_INT(_vm, OID_AUTO, vnode_pbufs, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 13466fb0b1aSGleb Smirnoff &nvnpbufs, 0, "number of physical buffers allocated for vnode pager"); 13566fb0b1aSGleb Smirnoff 136756a5412SGleb Smirnoff static uma_zone_t vnode_pbuf_zone; 137756a5412SGleb Smirnoff 138756a5412SGleb Smirnoff static void 139756a5412SGleb Smirnoff vnode_pager_init(void *dummy) 140756a5412SGleb Smirnoff { 141756a5412SGleb Smirnoff 14266fb0b1aSGleb Smirnoff #ifdef __LP64__ 14366fb0b1aSGleb Smirnoff nvnpbufs = nswbuf * 2; 14466fb0b1aSGleb Smirnoff #else 14566fb0b1aSGleb Smirnoff nvnpbufs = nswbuf / 2; 14666fb0b1aSGleb Smirnoff #endif 14766fb0b1aSGleb Smirnoff TUNABLE_INT_FETCH("vm.vnode_pbufs", &nvnpbufs); 14866fb0b1aSGleb Smirnoff vnode_pbuf_zone = pbuf_zsecond_create("vnpbuf", nvnpbufs); 149756a5412SGleb Smirnoff } 150756a5412SGleb Smirnoff SYSINIT(vnode_pager, SI_SUB_CPU, SI_ORDER_ANY, vnode_pager_init, NULL); 151756a5412SGleb Smirnoff 152d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */ 153d07a6d3fSPoul-Henning Kamp int 154731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td) 155d07a6d3fSPoul-Henning Kamp { 156d07a6d3fSPoul-Henning Kamp vm_object_t object; 157d07a6d3fSPoul-Henning Kamp vm_ooffset_t size = isize; 158a67d5408SJeff Roberson bool last; 159d07a6d3fSPoul-Henning Kamp 1607ad2a82dSMateusz Guzik if (!vn_isdisk(vp) && vn_canvmio(vp) == FALSE) 161d07a6d3fSPoul-Henning Kamp return (0); 162d07a6d3fSPoul-Henning Kamp 1636470c8d3SKonstantin Belousov object = vp->v_object; 1646470c8d3SKonstantin Belousov if (object != NULL) 165d07a6d3fSPoul-Henning Kamp return (0); 166d07a6d3fSPoul-Henning Kamp 167d07a6d3fSPoul-Henning Kamp if (size == 0) { 1687ad2a82dSMateusz Guzik if (vn_isdisk(vp)) { 169d07a6d3fSPoul-Henning Kamp size = IDX_TO_OFF(INT_MAX); 170d07a6d3fSPoul-Henning Kamp } else { 171*f45feecfSMateusz Guzik if (vn_getsize_locked(vp, &size, td->td_ucred) != 0) 172d07a6d3fSPoul-Henning Kamp return (0); 173d07a6d3fSPoul-Henning Kamp } 174d07a6d3fSPoul-Henning Kamp } 175d07a6d3fSPoul-Henning Kamp 1763364c323SKonstantin Belousov object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred); 177d07a6d3fSPoul-Henning Kamp /* 178d07a6d3fSPoul-Henning Kamp * Dereference the reference we just created. This assumes 179a67d5408SJeff Roberson * that the object is associated with the vp. We still have 180a67d5408SJeff Roberson * to serialize with vnode_pager_dealloc() for the last 181a67d5408SJeff Roberson * potential reference. 182d07a6d3fSPoul-Henning Kamp */ 18351df5321SJeff Roberson VM_OBJECT_RLOCK(object); 184a67d5408SJeff Roberson last = refcount_release(&object->ref_count); 18551df5321SJeff Roberson VM_OBJECT_RUNLOCK(object); 186a67d5408SJeff Roberson if (last) 187d07a6d3fSPoul-Henning Kamp vrele(vp); 188d07a6d3fSPoul-Henning Kamp 189d07a6d3fSPoul-Henning Kamp KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object")); 190d07a6d3fSPoul-Henning Kamp 191d07a6d3fSPoul-Henning Kamp return (0); 192d07a6d3fSPoul-Henning Kamp } 193d07a6d3fSPoul-Henning Kamp 1947146d6cbSPoul-Henning Kamp void 1957146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp) 1967146d6cbSPoul-Henning Kamp { 1977146d6cbSPoul-Henning Kamp struct vm_object *obj; 1987146d6cbSPoul-Henning Kamp 1997146d6cbSPoul-Henning Kamp obj = vp->v_object; 2006470c8d3SKonstantin Belousov if (obj == NULL || obj->handle != vp) 2017146d6cbSPoul-Henning Kamp return; 20257fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject"); 20389f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 2046470c8d3SKonstantin Belousov MPASS(obj->type == OBJT_VNODE); 2052a339d9eSKonstantin Belousov umtx_shm_object_terminated(obj); 2067146d6cbSPoul-Henning Kamp if (obj->ref_count == 0) { 2079c83ff2dSJeff Roberson KASSERT((obj->flags & OBJ_DEAD) == 0, 2089c83ff2dSJeff Roberson ("vnode_destroy_vobject: Terminating dead object")); 209783a68aaSKonstantin Belousov vm_object_set_flag(obj, OBJ_DEAD); 210783a68aaSKonstantin Belousov 211783a68aaSKonstantin Belousov /* 212783a68aaSKonstantin Belousov * Clean pages and flush buffers. 213783a68aaSKonstantin Belousov */ 214783a68aaSKonstantin Belousov vm_object_page_clean(obj, 0, 0, OBJPC_SYNC); 215783a68aaSKonstantin Belousov VM_OBJECT_WUNLOCK(obj); 216783a68aaSKonstantin Belousov 217783a68aaSKonstantin Belousov vinvalbuf(vp, V_SAVE, 0, 0); 218783a68aaSKonstantin Belousov 219783a68aaSKonstantin Belousov BO_LOCK(&vp->v_bufobj); 220783a68aaSKonstantin Belousov vp->v_bufobj.bo_flag |= BO_DEAD; 221783a68aaSKonstantin Belousov BO_UNLOCK(&vp->v_bufobj); 222783a68aaSKonstantin Belousov 223783a68aaSKonstantin Belousov VM_OBJECT_WLOCK(obj); 2247146d6cbSPoul-Henning Kamp vm_object_terminate(obj); 22590880a1bSKonstantin Belousov } else { 22690880a1bSKonstantin Belousov /* 2277146d6cbSPoul-Henning Kamp * Woe to the process that tries to page now :-). 2287146d6cbSPoul-Henning Kamp */ 2297146d6cbSPoul-Henning Kamp vm_pager_deallocate(obj); 23089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 2317146d6cbSPoul-Henning Kamp } 23290880a1bSKonstantin Belousov KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object)); 2337146d6cbSPoul-Henning Kamp } 2347146d6cbSPoul-Henning Kamp 235df8bae1dSRodney W. Grimes /* 236df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 237df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 238df8bae1dSRodney W. Grimes */ 23924a1cce3SDavid Greenman vm_object_t 2406cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 2413364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 242df8bae1dSRodney W. Grimes { 24306cb7259SDavid Greenman vm_object_t object; 244df8bae1dSRodney W. Grimes struct vnode *vp; 245df8bae1dSRodney W. Grimes 246df8bae1dSRodney W. Grimes /* 247df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 248df8bae1dSRodney W. Grimes */ 249df8bae1dSRodney W. Grimes if (handle == NULL) 250df8bae1dSRodney W. Grimes return (NULL); 251df8bae1dSRodney W. Grimes 252df8bae1dSRodney W. Grimes vp = (struct vnode *)handle; 2536470c8d3SKonstantin Belousov ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc"); 254f1fa1ba3SMateusz Guzik VNPASS(vp->v_usecount > 0, vp); 2556470c8d3SKonstantin Belousov retry: 2566470c8d3SKonstantin Belousov object = vp->v_object; 2572be70f79SJohn Dyson 25824a1cce3SDavid Greenman if (object == NULL) { 259df8bae1dSRodney W. Grimes /* 2602ac78f0eSStephan Uphoff * Add an object of the appropriate size 261df8bae1dSRodney W. Grimes */ 2626470c8d3SKonstantin Belousov object = vm_object_allocate(OBJT_VNODE, 2636470c8d3SKonstantin Belousov OFF_TO_IDX(round_page(size))); 264bbc0ec52SDavid Greenman 2656cde7a16SDavid Greenman object->un_pager.vnp.vnp_size = size; 26684110e7eSKonstantin Belousov object->un_pager.vnp.writemappings = 0; 267e5818a53SJeff Roberson object->domain.dr_policy = vnode_domainset; 26824a1cce3SDavid Greenman object->handle = handle; 269208b81bbSKonstantin Belousov if ((vp->v_vflag & VV_VMSIZEVNLOCK) != 0) { 270208b81bbSKonstantin Belousov VM_OBJECT_WLOCK(object); 271208b81bbSKonstantin Belousov vm_object_set_flag(object, OBJ_SIZEVNLOCK); 272208b81bbSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 273208b81bbSKonstantin Belousov } 27411be8415SStephan Uphoff VI_LOCK(vp); 2752ac78f0eSStephan Uphoff if (vp->v_object != NULL) { 2762ac78f0eSStephan Uphoff /* 2776470c8d3SKonstantin Belousov * Object has been created while we were allocating. 2782ac78f0eSStephan Uphoff */ 27911be8415SStephan Uphoff VI_UNLOCK(vp); 2809cddade7SKonstantin Belousov VM_OBJECT_WLOCK(object); 2819cddade7SKonstantin Belousov KASSERT(object->ref_count == 1, 2829cddade7SKonstantin Belousov ("leaked ref %p %d", object, object->ref_count)); 2839cddade7SKonstantin Belousov object->type = OBJT_DEAD; 28451df5321SJeff Roberson refcount_init(&object->ref_count, 0); 2859cddade7SKonstantin Belousov VM_OBJECT_WUNLOCK(object); 2862ac78f0eSStephan Uphoff vm_object_destroy(object); 2872ac78f0eSStephan Uphoff goto retry; 288df8bae1dSRodney W. Grimes } 2892ac78f0eSStephan Uphoff vp->v_object = object; 29011be8415SStephan Uphoff VI_UNLOCK(vp); 291a67d5408SJeff Roberson vrefact(vp); 29211be8415SStephan Uphoff } else { 293a67d5408SJeff Roberson vm_object_reference(object); 2943d653db0SAlan Cox #if VM_NRESERVLEVEL > 0 295a67d5408SJeff Roberson if ((object->flags & OBJ_COLORED) == 0) { 296a67d5408SJeff Roberson VM_OBJECT_WLOCK(object); 2973d653db0SAlan Cox vm_object_color(object, 0); 29889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 29911be8415SStephan Uphoff } 300a67d5408SJeff Roberson #endif 301a67d5408SJeff Roberson } 30224a1cce3SDavid Greenman return (object); 303df8bae1dSRodney W. Grimes } 304df8bae1dSRodney W. Grimes 305658ad5ffSAlan Cox /* 306658ad5ffSAlan Cox * The object must be locked. 307658ad5ffSAlan Cox */ 308f708ef1bSPoul-Henning Kamp static void 3097ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object) 31024a1cce3SDavid Greenman { 311b9f180d1SKonstantin Belousov struct vnode *vp; 312b9f180d1SKonstantin Belousov int refs; 313df8bae1dSRodney W. Grimes 314b9f180d1SKonstantin Belousov vp = object->handle; 31524a1cce3SDavid Greenman if (vp == NULL) 31624a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 31724a1cce3SDavid Greenman 31889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 31966095752SJohn Dyson vm_object_pip_wait(object, "vnpdea"); 320b9f180d1SKonstantin Belousov refs = object->ref_count; 32124a1cce3SDavid Greenman 32224a1cce3SDavid Greenman object->handle = NULL; 32395461b45SJohn Dyson object->type = OBJT_DEAD; 32457fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc"); 32584110e7eSKonstantin Belousov if (object->un_pager.vnp.writemappings > 0) { 32684110e7eSKonstantin Belousov object->un_pager.vnp.writemappings = 0; 32778022527SKonstantin Belousov VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 328b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 329b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 33084110e7eSKonstantin Belousov } 331aa2cabb9SDavid Greenman vp->v_object = NULL; 33278022527SKonstantin Belousov VI_LOCK(vp); 33378022527SKonstantin Belousov 33478022527SKonstantin Belousov /* 33578022527SKonstantin Belousov * vm_map_entry_set_vnode_text() cannot reach this vnode by 33678022527SKonstantin Belousov * following object->handle. Clear all text references now. 33778022527SKonstantin Belousov * This also clears the transient references from 33878022527SKonstantin Belousov * kern_execve(), which is fine because dead_vnodeops uses nop 33978022527SKonstantin Belousov * for VOP_UNSET_TEXT(). 34078022527SKonstantin Belousov */ 34178022527SKonstantin Belousov if (vp->v_writecount < 0) 34278022527SKonstantin Belousov vp->v_writecount = 0; 34378022527SKonstantin Belousov VI_UNLOCK(vp); 34489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 345a67d5408SJeff Roberson if (refs > 0) 346b9f180d1SKonstantin Belousov vunref(vp); 34789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 348df8bae1dSRodney W. Grimes } 34926f9a767SRodney W. Grimes 350f708ef1bSPoul-Henning Kamp static boolean_t 3517ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 3527ebba1f8SGleb Smirnoff int *after) 353df8bae1dSRodney W. Grimes { 35424a1cce3SDavid Greenman struct vnode *vp = object->handle; 35598b0c789SPoul-Henning Kamp daddr_t bn; 3564153054aSJeff Roberson uintptr_t lockstate; 3573af76890SPoul-Henning Kamp int err; 358170db9c6SJohn Dyson daddr_t reqblock; 3592c4488fcSJohn Dyson int poff; 3602c4488fcSJohn Dyson int bsize; 361d63596ceSJohn Dyson int pagesperblock, blocksperpage; 362df8bae1dSRodney W. Grimes 3634153054aSJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 36424579ca1SMatthew Dillon /* 36524579ca1SMatthew Dillon * If no vp or vp is doomed or marked transparent to VM, we do not 36624579ca1SMatthew Dillon * have the page. 36724579ca1SMatthew Dillon */ 368abd80ddbSMateusz Guzik if (vp == NULL || VN_IS_DOOMED(vp)) 36947221757SJohn Dyson return FALSE; 370df8bae1dSRodney W. Grimes /* 371b73f64c4SJeff Roberson * If the offset is beyond end of file we do 3720d94caffSDavid Greenman * not have the page. 373df8bae1dSRodney W. Grimes */ 374b73f64c4SJeff Roberson if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 3754abc71c0SDavid Greenman return FALSE; 376df8bae1dSRodney W. Grimes 377eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 378170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 379d63596ceSJohn Dyson blocksperpage = 0; 380d63596ceSJohn Dyson if (pagesperblock > 0) { 381a316d390SJohn Dyson reqblock = pindex / pagesperblock; 382d63596ceSJohn Dyson } else { 383d63596ceSJohn Dyson blocksperpage = (PAGE_SIZE / bsize); 384d63596ceSJohn Dyson reqblock = pindex * blocksperpage; 385d63596ceSJohn Dyson } 3864153054aSJeff Roberson lockstate = VM_OBJECT_DROP(object); 387ec794849SPoul-Henning Kamp err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 3884153054aSJeff Roberson VM_OBJECT_PICKUP(object, lockstate); 3890d94caffSDavid Greenman if (err) 39024a1cce3SDavid Greenman return TRUE; 3916eab77f2SJohn Dyson if (bn == -1) 392ced399eeSJohn Dyson return FALSE; 393d63596ceSJohn Dyson if (pagesperblock > 0) { 394a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 395170db9c6SJohn Dyson if (before) { 396170db9c6SJohn Dyson *before *= pagesperblock; 397170db9c6SJohn Dyson *before += poff; 398170db9c6SJohn Dyson } 399170db9c6SJohn Dyson if (after) { 40084d31376SGleb Smirnoff /* 40184d31376SGleb Smirnoff * The BMAP vop can report a partial block in the 402d2596d17SGleb Smirnoff * 'after', but must not report blocks after EOF. 40384d31376SGleb Smirnoff * Assert the latter, and truncate 'after' in case 40484d31376SGleb Smirnoff * of the former. 40584d31376SGleb Smirnoff */ 406d2596d17SGleb Smirnoff KASSERT((reqblock + *after) * pagesperblock < 407d2596d17SGleb Smirnoff roundup2(object->size, pagesperblock), 40884d31376SGleb Smirnoff ("%s: reqblock %jd after %d size %ju", __func__, 40984d31376SGleb Smirnoff (intmax_t )reqblock, *after, 41084d31376SGleb Smirnoff (uintmax_t )object->size)); 411170db9c6SJohn Dyson *after *= pagesperblock; 41284d31376SGleb Smirnoff *after += pagesperblock - (poff + 1); 41384d31376SGleb Smirnoff if (pindex + *after >= object->size) 41484d31376SGleb Smirnoff *after = object->size - 1 - pindex; 415170db9c6SJohn Dyson } 416d63596ceSJohn Dyson } else { 417d63596ceSJohn Dyson if (before) { 418d63596ceSJohn Dyson *before /= blocksperpage; 419d63596ceSJohn Dyson } 420d63596ceSJohn Dyson 421d63596ceSJohn Dyson if (after) { 422d63596ceSJohn Dyson *after /= blocksperpage; 423d63596ceSJohn Dyson } 424d63596ceSJohn Dyson } 425ced399eeSJohn Dyson return TRUE; 426df8bae1dSRodney W. Grimes } 427df8bae1dSRodney W. Grimes 428df8bae1dSRodney W. Grimes /* 429de2e1529SKa Ho Ng * Internal routine clearing partial-page content 430de2e1529SKa Ho Ng */ 431de2e1529SKa Ho Ng static void 432de2e1529SKa Ho Ng vnode_pager_subpage_purge(struct vm_page *m, int base, int end) 433de2e1529SKa Ho Ng { 434de2e1529SKa Ho Ng int size; 435de2e1529SKa Ho Ng 436de2e1529SKa Ho Ng KASSERT(end > base && end <= PAGE_SIZE, 437de2e1529SKa Ho Ng ("%s: start %d end %d", __func__, base, end)); 438de2e1529SKa Ho Ng size = end - base; 439de2e1529SKa Ho Ng 440de2e1529SKa Ho Ng /* 441de2e1529SKa Ho Ng * Clear out partial-page garbage in case 442de2e1529SKa Ho Ng * the page has been mapped. 443de2e1529SKa Ho Ng */ 444de2e1529SKa Ho Ng pmap_zero_page_area(m, base, size); 445de2e1529SKa Ho Ng 446de2e1529SKa Ho Ng /* 447de2e1529SKa Ho Ng * Update the valid bits to reflect the blocks 448de2e1529SKa Ho Ng * that have been zeroed. Some of these valid 449de2e1529SKa Ho Ng * bits may have already been set. 450de2e1529SKa Ho Ng */ 451de2e1529SKa Ho Ng vm_page_set_valid_range(m, base, size); 452de2e1529SKa Ho Ng 453de2e1529SKa Ho Ng /* 454de2e1529SKa Ho Ng * Round up "base" to the next block boundary so 455de2e1529SKa Ho Ng * that the dirty bit for a partially zeroed 456de2e1529SKa Ho Ng * block is not cleared. 457de2e1529SKa Ho Ng */ 458de2e1529SKa Ho Ng base = roundup2(base, DEV_BSIZE); 459de2e1529SKa Ho Ng end = rounddown2(end, DEV_BSIZE); 460de2e1529SKa Ho Ng 461de2e1529SKa Ho Ng if (end > base) { 462de2e1529SKa Ho Ng /* 463de2e1529SKa Ho Ng * Clear out partial-page dirty bits. 464de2e1529SKa Ho Ng * 465de2e1529SKa Ho Ng * note that we do not clear out the 466de2e1529SKa Ho Ng * valid bits. This would prevent 467de2e1529SKa Ho Ng * bogus_page replacement from working 468de2e1529SKa Ho Ng * properly. 469de2e1529SKa Ho Ng */ 470de2e1529SKa Ho Ng vm_page_clear_dirty(m, base, end - base); 471de2e1529SKa Ho Ng } 472de2e1529SKa Ho Ng 473de2e1529SKa Ho Ng } 474de2e1529SKa Ho Ng 475de2e1529SKa Ho Ng /* 476df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 47724a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 478df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 479df8bae1dSRodney W. Grimes * 480df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 481df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 482df8bae1dSRodney W. Grimes */ 483df8bae1dSRodney W. Grimes void 4847ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize) 485df8bae1dSRodney W. Grimes { 4862a8f9ab5SAlan Cox vm_object_t object; 4872a8f9ab5SAlan Cox vm_page_t m; 488c576d121SLuoqi Chen vm_pindex_t nobjsize; 489df8bae1dSRodney W. Grimes 4902a8f9ab5SAlan Cox if ((object = vp->v_object) == NULL) 491df8bae1dSRodney W. Grimes return; 4925b87ecc6SKonstantin Belousov #ifdef DEBUG_VFS_LOCKS 4935b87ecc6SKonstantin Belousov { 4945b87ecc6SKonstantin Belousov struct mount *mp; 4955b87ecc6SKonstantin Belousov 4965b87ecc6SKonstantin Belousov mp = vp->v_mount; 4975b87ecc6SKonstantin Belousov if (mp != NULL && (mp->mnt_kern_flag & MNTK_VMSETSIZE_BUG) == 0) 4985b87ecc6SKonstantin Belousov assert_vop_elocked(vp, 4995b87ecc6SKonstantin Belousov "vnode_pager_setsize and not locked vnode"); 5005b87ecc6SKonstantin Belousov } 5015b87ecc6SKonstantin Belousov #endif 50289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 5039b8851faSKonstantin Belousov if (object->type == OBJT_DEAD) { 5049b8851faSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 5059b8851faSKonstantin Belousov return; 5069b8851faSKonstantin Belousov } 5079b8851faSKonstantin Belousov KASSERT(object->type == OBJT_VNODE, 5089b8851faSKonstantin Belousov ("not vnode-backed object %p", object)); 5092a8f9ab5SAlan Cox if (nsize == object->un_pager.vnp.vnp_size) { 510df8bae1dSRodney W. Grimes /* 511df8bae1dSRodney W. Grimes * Hasn't changed size 512df8bae1dSRodney W. Grimes */ 51389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 514df8bae1dSRodney W. Grimes return; 5152a8f9ab5SAlan Cox } 516c576d121SLuoqi Chen nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 5172a8f9ab5SAlan Cox if (nsize < object->un_pager.vnp.vnp_size) { 518df8bae1dSRodney W. Grimes /* 519bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 520df8bae1dSRodney W. Grimes */ 5212a8f9ab5SAlan Cox if (nobjsize < object->size) 522c576d121SLuoqi Chen vm_object_page_remove(object, nobjsize, object->size, 5236bbee8e2SAlan Cox 0); 524bbc0ec52SDavid Greenman /* 525bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 5263ebeaf59SMatthew Dillon * only partially backed by the vnode. 5273ebeaf59SMatthew Dillon * 5283ebeaf59SMatthew Dillon * XXX for some reason (I don't know yet), if we take a 5293ebeaf59SMatthew Dillon * completely invalid page and mark it partially valid 5303ebeaf59SMatthew Dillon * it can screw up NFS reads, so we don't allow the case. 531bbc0ec52SDavid Greenman */ 5320012f373SJeff Roberson if (!(nsize & PAGE_MASK)) 5330012f373SJeff Roberson goto out; 5340012f373SJeff Roberson m = vm_page_grab(object, OFF_TO_IDX(nsize), VM_ALLOC_NOCREAT); 5350012f373SJeff Roberson if (m == NULL) 5360012f373SJeff Roberson goto out; 537de2e1529SKa Ho Ng if (!vm_page_none_valid(m)) 538de2e1529SKa Ho Ng vnode_pager_subpage_purge(m, (int)nsize & PAGE_MASK, 539de2e1529SKa Ho Ng PAGE_SIZE); 5400012f373SJeff Roberson vm_page_xunbusy(m); 541bbc0ec52SDavid Greenman } 5420012f373SJeff Roberson out: 543419e5698SKonstantin Belousov #if defined(__powerpc__) && !defined(__powerpc64__) 544a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 545419e5698SKonstantin Belousov #else 546419e5698SKonstantin Belousov atomic_store_64(&object->un_pager.vnp.vnp_size, nsize); 547419e5698SKonstantin Belousov #endif 548c576d121SLuoqi Chen object->size = nobjsize; 54989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes 55226f9a767SRodney W. Grimes /* 553de2e1529SKa Ho Ng * Lets the VM system know about the purged range for a file. We toss away any 554de2e1529SKa Ho Ng * cached pages in the associated object that are affected by the purge 555de2e1529SKa Ho Ng * operation. Partial-page area not aligned to page boundaries will be zeroed 556de2e1529SKa Ho Ng * and the dirty blocks in DEV_BSIZE unit within a page will not be flushed. 557de2e1529SKa Ho Ng */ 558de2e1529SKa Ho Ng void 559de2e1529SKa Ho Ng vnode_pager_purge_range(struct vnode *vp, vm_ooffset_t start, vm_ooffset_t end) 560de2e1529SKa Ho Ng { 561de2e1529SKa Ho Ng struct vm_page *m; 562de2e1529SKa Ho Ng struct vm_object *object; 563de2e1529SKa Ho Ng vm_pindex_t pi, pistart, piend; 564de2e1529SKa Ho Ng bool same_page; 565de2e1529SKa Ho Ng int base, pend; 566de2e1529SKa Ho Ng 567de2e1529SKa Ho Ng ASSERT_VOP_LOCKED(vp, "vnode_pager_purge_range"); 568de2e1529SKa Ho Ng 569de2e1529SKa Ho Ng object = vp->v_object; 570de2e1529SKa Ho Ng pi = start + PAGE_MASK < start ? OBJ_MAX_SIZE : 571de2e1529SKa Ho Ng OFF_TO_IDX(start + PAGE_MASK); 572de2e1529SKa Ho Ng pistart = OFF_TO_IDX(start); 573de2e1529SKa Ho Ng piend = end == 0 ? OBJ_MAX_SIZE : OFF_TO_IDX(end); 574de2e1529SKa Ho Ng same_page = pistart == piend; 575de2e1529SKa Ho Ng if ((end != 0 && end <= start) || object == NULL) 576de2e1529SKa Ho Ng return; 577de2e1529SKa Ho Ng 578de2e1529SKa Ho Ng VM_OBJECT_WLOCK(object); 579de2e1529SKa Ho Ng 580de2e1529SKa Ho Ng if (pi < piend) 581de2e1529SKa Ho Ng vm_object_page_remove(object, pi, piend, 0); 582de2e1529SKa Ho Ng 583de2e1529SKa Ho Ng if ((start & PAGE_MASK) != 0) { 584de2e1529SKa Ho Ng base = (int)start & PAGE_MASK; 585de2e1529SKa Ho Ng pend = same_page ? (int)end & PAGE_MASK : PAGE_SIZE; 586de2e1529SKa Ho Ng m = vm_page_grab(object, pistart, VM_ALLOC_NOCREAT); 587de2e1529SKa Ho Ng if (m != NULL) { 588de2e1529SKa Ho Ng if (!vm_page_none_valid(m)) 589de2e1529SKa Ho Ng vnode_pager_subpage_purge(m, base, pend); 590de2e1529SKa Ho Ng vm_page_xunbusy(m); 591de2e1529SKa Ho Ng } 592de2e1529SKa Ho Ng if (same_page) 593de2e1529SKa Ho Ng goto out; 594de2e1529SKa Ho Ng } 595de2e1529SKa Ho Ng if ((end & PAGE_MASK) != 0) { 596de2e1529SKa Ho Ng base = same_page ? (int)start & PAGE_MASK : 0 ; 597de2e1529SKa Ho Ng pend = (int)end & PAGE_MASK; 598de2e1529SKa Ho Ng m = vm_page_grab(object, piend, VM_ALLOC_NOCREAT); 599de2e1529SKa Ho Ng if (m != NULL) { 600de2e1529SKa Ho Ng if (!vm_page_none_valid(m)) 601de2e1529SKa Ho Ng vnode_pager_subpage_purge(m, base, pend); 602de2e1529SKa Ho Ng vm_page_xunbusy(m); 603de2e1529SKa Ho Ng } 604de2e1529SKa Ho Ng } 605de2e1529SKa Ho Ng out: 606de2e1529SKa Ho Ng VM_OBJECT_WUNLOCK(object); 607de2e1529SKa Ho Ng } 608de2e1529SKa Ho Ng 609de2e1529SKa Ho Ng /* 61026f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 61126f9a767SRodney W. Grimes * file address 61226f9a767SRodney W. Grimes */ 613bff76343SAlan Cox static int 614bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 615bff76343SAlan Cox int *run) 61626f9a767SRodney W. Grimes { 61726f9a767SRodney W. Grimes int bsize; 61826f9a767SRodney W. Grimes int err; 619a316d390SJohn Dyson daddr_t vblock; 620f3aad9a6SBjoern A. Zeeb daddr_t voffset; 62126f9a767SRodney W. Grimes 622abd80ddbSMateusz Guzik if (VN_IS_DOOMED(vp)) 6232c4488fcSJohn Dyson return -1; 6242c4488fcSJohn Dyson 62526f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 62626f9a767SRodney W. Grimes vblock = address / bsize; 62726f9a767SRodney W. Grimes voffset = address % bsize; 62826f9a767SRodney W. Grimes 629bff76343SAlan Cox err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 630bff76343SAlan Cox if (err == 0) { 631bff76343SAlan Cox if (*rtaddress != -1) 632bff76343SAlan Cox *rtaddress += voffset / DEV_BSIZE; 633efc68ce1SDavid Greenman if (run) { 634efc68ce1SDavid Greenman *run += 1; 635efc68ce1SDavid Greenman *run *= bsize / PAGE_SIZE; 636efc68ce1SDavid Greenman *run -= voffset / PAGE_SIZE; 637efc68ce1SDavid Greenman } 638efc68ce1SDavid Greenman } 63926f9a767SRodney W. Grimes 640bff76343SAlan Cox return (err); 64126f9a767SRodney W. Grimes } 64226f9a767SRodney W. Grimes 64326f9a767SRodney W. Grimes /* 64426f9a767SRodney W. Grimes * small block filesystem vnode pager input 64526f9a767SRodney W. Grimes */ 646f708ef1bSPoul-Henning Kamp static int 6477ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m) 64826f9a767SRodney W. Grimes { 6499c83534dSPoul-Henning Kamp struct vnode *vp; 6509c83534dSPoul-Henning Kamp struct bufobj *bo; 65126f9a767SRodney W. Grimes struct buf *bp; 6529e0ddbd0SAlan Cox struct sf_buf *sf; 653f3aad9a6SBjoern A. Zeeb daddr_t fileaddr; 65426f9a767SRodney W. Grimes vm_offset_t bsize; 655561cc9fcSKonstantin Belousov vm_page_bits_t bits; 656561cc9fcSKonstantin Belousov int error, i; 65726f9a767SRodney W. Grimes 658561cc9fcSKonstantin Belousov error = 0; 65924a1cce3SDavid Greenman vp = object->handle; 660abd80ddbSMateusz Guzik if (VN_IS_DOOMED(vp)) 6612c4488fcSJohn Dyson return VM_PAGER_BAD; 6622c4488fcSJohn Dyson 66326f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 6640bdb7528SDavid Greenman 6659c83534dSPoul-Henning Kamp VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 66626f9a767SRodney W. Grimes 6679e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 66826f9a767SRodney W. Grimes 66926f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 67033c67741SMatthew Dillon vm_ooffset_t address; 671bbc0ec52SDavid Greenman 6720d53a17bSAlan Cox bits = vm_page_bits(i * bsize, bsize); 6730d53a17bSAlan Cox if (m->valid & bits) 67426f9a767SRodney W. Grimes continue; 67526f9a767SRodney W. Grimes 67633c67741SMatthew Dillon address = IDX_TO_OFF(m->pindex) + i * bsize; 67733c67741SMatthew Dillon if (address >= object->un_pager.vnp.vnp_size) { 67833c67741SMatthew Dillon fileaddr = -1; 67933c67741SMatthew Dillon } else { 680bff76343SAlan Cox error = vnode_pager_addr(vp, address, &fileaddr, NULL); 681bff76343SAlan Cox if (error) 682bff76343SAlan Cox break; 68333c67741SMatthew Dillon } 68426f9a767SRodney W. Grimes if (fileaddr != -1) { 685756a5412SGleb Smirnoff bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK); 68626f9a767SRodney W. Grimes 68726f9a767SRodney W. Grimes /* build a minimal buffer header */ 68821144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 6896a4b5823SPoul-Henning Kamp bp->b_iodone = bdone; 690bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 691bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 692a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 693a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 6949e0ddbd0SAlan Cox bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 695187f0071SDavid Greenman bp->b_blkno = fileaddr; 6969c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 6971faacf5dSKirk McKusick bp->b_vp = vp; 69826f9a767SRodney W. Grimes bp->b_bcount = bsize; 69926f9a767SRodney W. Grimes bp->b_bufsize = bsize; 7002b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 7015bd65606SJohn Baldwin atomic_add_long(&runningbufspace, bp->b_runningbufspace); 70226f9a767SRodney W. Grimes 70326f9a767SRodney W. Grimes /* do the input */ 7042c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 705b792bebeSPoul-Henning Kamp bstrategy(bp); 70626f9a767SRodney W. Grimes 7076a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnsrd"); 7086a4b5823SPoul-Henning Kamp 709cafbf0c6SWarner Losh if ((bp->b_ioflags & BIO_ERROR) != 0) { 710cafbf0c6SWarner Losh KASSERT(bp->b_error != 0, 711cafbf0c6SWarner Losh ("%s: buf error but b_error == 0\n", __func__)); 712cafbf0c6SWarner Losh error = bp->b_error; 713cafbf0c6SWarner Losh } 71426f9a767SRodney W. Grimes 71526f9a767SRodney W. Grimes /* 71626f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 71726f9a767SRodney W. Grimes */ 7181faacf5dSKirk McKusick bp->b_vp = NULL; 7199c83534dSPoul-Henning Kamp pbrelbo(bp); 720756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 72126f9a767SRodney W. Grimes if (error) 72226f9a767SRodney W. Grimes break; 7230d53a17bSAlan Cox } else 7249e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 7250d53a17bSAlan Cox KASSERT((m->dirty & bits) == 0, 7260d53a17bSAlan Cox ("vnode_pager_input_smlfs: page %p is dirty", m)); 7277f935055SJeff Roberson vm_page_bits_set(m, &m->valid, bits); 72826f9a767SRodney W. Grimes } 7299e0ddbd0SAlan Cox sf_buf_free(sf); 73026f9a767SRodney W. Grimes if (error) { 731a83c285cSDavid Greenman return VM_PAGER_ERROR; 73226f9a767SRodney W. Grimes } 73326f9a767SRodney W. Grimes return VM_PAGER_OK; 73426f9a767SRodney W. Grimes } 73526f9a767SRodney W. Grimes 73626f9a767SRodney W. Grimes /* 737475e8cc3SPoul-Henning Kamp * old style vnode pager input routine 73826f9a767SRodney W. Grimes */ 739f708ef1bSPoul-Henning Kamp static int 7407ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m) 74126f9a767SRodney W. Grimes { 742df8bae1dSRodney W. Grimes struct uio auio; 743df8bae1dSRodney W. Grimes struct iovec aiov; 74426f9a767SRodney W. Grimes int error; 74526f9a767SRodney W. Grimes int size; 7469e0ddbd0SAlan Cox struct sf_buf *sf; 747342a1480SJohn Baldwin struct vnode *vp; 748df8bae1dSRodney W. Grimes 74989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 75026f9a767SRodney W. Grimes error = 0; 751bbc0ec52SDavid Greenman 752df8bae1dSRodney W. Grimes /* 75326f9a767SRodney W. Grimes * Return failure if beyond current EOF 75426f9a767SRodney W. Grimes */ 755a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 75626f9a767SRodney W. Grimes return VM_PAGER_BAD; 75726f9a767SRodney W. Grimes } else { 75826f9a767SRodney W. Grimes size = PAGE_SIZE; 759a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 760a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 76152051abcSAlan Cox vp = object->handle; 76289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 7630bdb7528SDavid Greenman 76426f9a767SRodney W. Grimes /* 765df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 766df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 767df8bae1dSRodney W. Grimes */ 7689e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 7690bdb7528SDavid Greenman 7709e0ddbd0SAlan Cox aiov.iov_base = (caddr_t)sf_buf_kva(sf); 771df8bae1dSRodney W. Grimes aiov.iov_len = size; 772df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 773df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 774a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 775df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 77626f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 777df8bae1dSRodney W. Grimes auio.uio_resid = size; 778b40ce416SJulian Elischer auio.uio_td = curthread; 77926f9a767SRodney W. Grimes 780a854ed98SJohn Baldwin error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 781df8bae1dSRodney W. Grimes if (!error) { 78254d92145SMatthew Dillon int count = size - auio.uio_resid; 783df8bae1dSRodney W. Grimes 784df8bae1dSRodney W. Grimes if (count == 0) 785df8bae1dSRodney W. Grimes error = EINVAL; 78626f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 7879e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + count, 7889e0ddbd0SAlan Cox PAGE_SIZE - count); 789df8bae1dSRodney W. Grimes } 7909e0ddbd0SAlan Cox sf_buf_free(sf); 7911b26eb10SAlan Cox 79289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 793df8bae1dSRodney W. Grimes } 7940d53a17bSAlan Cox KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m)); 7956e3a3f38SRobert V. Baron if (!error) 7960012f373SJeff Roberson vm_page_valid(m); 797a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 79826f9a767SRodney W. Grimes } 79926f9a767SRodney W. Grimes 80026f9a767SRodney W. Grimes /* 80126f9a767SRodney W. Grimes * generic vnode pager input routine 80226f9a767SRodney W. Grimes */ 803170db9c6SJohn Dyson 804ce75f2c3SMike Smith /* 80523955314SAlfred Perlstein * Local media VFS's that do not implement their own VOP_GETPAGES 80647e151ddSRobert Drehmel * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 80747e151ddSRobert Drehmel * to implement the previous behaviour. 808ce75f2c3SMike Smith * 809ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 810ce75f2c3SMike Smith * backing vp's VOP_GETPAGES. 811ce75f2c3SMike Smith */ 812f708ef1bSPoul-Henning Kamp static int 813b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind, 814b0cd2017SGleb Smirnoff int *rahead) 81524a1cce3SDavid Greenman { 816170db9c6SJohn Dyson struct vnode *vp; 817b0cd2017SGleb Smirnoff int rtval; 81895e5e988SJohn Dyson 819d6e13f3bSJeff Roberson /* Handle is stable with paging in progress. */ 820170db9c6SJohn Dyson vp = object->handle; 821b0cd2017SGleb Smirnoff rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead); 82223955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 82323955314SAlfred Perlstein ("vnode_pager: FS getpages not implemented\n")); 824170db9c6SJohn Dyson return rtval; 825170db9c6SJohn Dyson } 826170db9c6SJohn Dyson 82790effb23SGleb Smirnoff static int 82890effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count, 829b0cd2017SGleb Smirnoff int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg) 83090effb23SGleb Smirnoff { 83190effb23SGleb Smirnoff struct vnode *vp; 83290effb23SGleb Smirnoff int rtval; 83390effb23SGleb Smirnoff 83490effb23SGleb Smirnoff vp = object->handle; 835b0cd2017SGleb Smirnoff rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg); 83690effb23SGleb Smirnoff KASSERT(rtval != EOPNOTSUPP, 83790effb23SGleb Smirnoff ("vnode_pager: FS getpages_async not implemented\n")); 83890effb23SGleb Smirnoff return (rtval); 83990effb23SGleb Smirnoff } 84090effb23SGleb Smirnoff 841ce75f2c3SMike Smith /* 84290effb23SGleb Smirnoff * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for 84390effb23SGleb Smirnoff * local filesystems, where partially valid pages can only occur at 84490effb23SGleb Smirnoff * the end of file. 845d15b55c5SKonstantin Belousov */ 846d15b55c5SKonstantin Belousov int 847d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap) 848d15b55c5SKonstantin Belousov { 84990effb23SGleb Smirnoff 850b0cd2017SGleb Smirnoff return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 851b0cd2017SGleb Smirnoff ap->a_rbehind, ap->a_rahead, NULL, NULL)); 85290effb23SGleb Smirnoff } 85390effb23SGleb Smirnoff 85490effb23SGleb Smirnoff int 85590effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap) 85690effb23SGleb Smirnoff { 857abfdf767SKonstantin Belousov int error; 85890effb23SGleb Smirnoff 859abfdf767SKonstantin Belousov error = vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 860abfdf767SKonstantin Belousov ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg); 861abfdf767SKonstantin Belousov if (error != 0 && ap->a_iodone != NULL) 862abfdf767SKonstantin Belousov ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error); 863abfdf767SKonstantin Belousov return (error); 864d15b55c5SKonstantin Belousov } 865d15b55c5SKonstantin Belousov 866d15b55c5SKonstantin Belousov /* 867ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 868ce75f2c3SMike Smith * own vnodes if they fail to implement VOP_GETPAGES. 869ce75f2c3SMike Smith */ 870ce75f2c3SMike Smith int 871b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count, 872b0cd2017SGleb Smirnoff int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg) 873170db9c6SJohn Dyson { 874ce75f2c3SMike Smith vm_object_t object; 8759c83534dSPoul-Henning Kamp struct bufobj *bo; 8760bdb7528SDavid Greenman struct buf *bp; 877b0cd2017SGleb Smirnoff off_t foff; 878e48b82bdSGleb Smirnoff #ifdef INVARIANTS 879e48b82bdSGleb Smirnoff off_t blkno0; 880e48b82bdSGleb Smirnoff #endif 881756a5412SGleb Smirnoff int bsize, pagesperblock; 882b0cd2017SGleb Smirnoff int error, before, after, rbehind, rahead, poff, i; 883b0cd2017SGleb Smirnoff int bytecount, secmask; 884ce75f2c3SMike Smith 8859c83534dSPoul-Henning Kamp KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 886b0cd2017SGleb Smirnoff ("%s does not support devices", __func__)); 887b0cd2017SGleb Smirnoff 888abd80ddbSMateusz Guzik if (VN_IS_DOOMED(vp)) 889eac91e32SKonstantin Belousov return (VM_PAGER_BAD); 8902c4488fcSJohn Dyson 891eac91e32SKonstantin Belousov object = vp->v_object; 892b0cd2017SGleb Smirnoff foff = IDX_TO_OFF(m[0]->pindex); 89326f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 894b0cd2017SGleb Smirnoff pagesperblock = bsize / PAGE_SIZE; 895b0cd2017SGleb Smirnoff 896b0cd2017SGleb Smirnoff KASSERT(foff < object->un_pager.vnp.vnp_size, 897b0cd2017SGleb Smirnoff ("%s: page %p offset beyond vp %p size", __func__, m[0], vp)); 898cd853791SKonstantin Belousov KASSERT(count <= atop(maxphys), 899b0cd2017SGleb Smirnoff ("%s: requested %d pages", __func__, count)); 900b0cd2017SGleb Smirnoff 901b0cd2017SGleb Smirnoff /* 902b0cd2017SGleb Smirnoff * The last page has valid blocks. Invalid part can only 903b0cd2017SGleb Smirnoff * exist at the end of file, and the page is made fully valid 904b0cd2017SGleb Smirnoff * by zeroing in vm_pager_get_pages(). 905b0cd2017SGleb Smirnoff */ 9060012f373SJeff Roberson if (!vm_page_none_valid(m[count - 1]) && --count == 0) { 907b0cd2017SGleb Smirnoff if (iodone != NULL) 908b0cd2017SGleb Smirnoff iodone(arg, m, 1, 0); 909b0cd2017SGleb Smirnoff return (VM_PAGER_OK); 910b0cd2017SGleb Smirnoff } 911bbc0ec52SDavid Greenman 912756a5412SGleb Smirnoff bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK); 913cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 91473e9030eSGleb Smirnoff 91526f9a767SRodney W. Grimes /* 916e122dfc1SGleb Smirnoff * Get the underlying device blocks for the file with VOP_BMAP(). 917e122dfc1SGleb Smirnoff * If the file system doesn't support VOP_BMAP, use old way of 918e122dfc1SGleb Smirnoff * getting pages via VOP_READ. 91926f9a767SRodney W. Grimes */ 920b0cd2017SGleb Smirnoff error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before); 9211de11f1aSAlan Cox if (error == EOPNOTSUPP) { 922756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 92389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 924b0cd2017SGleb Smirnoff for (i = 0; i < count; i++) { 92583c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 92683c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodepgsin); 927b0cd2017SGleb Smirnoff error = vnode_pager_input_old(object, m[i]); 928b0cd2017SGleb Smirnoff if (error) 929b0cd2017SGleb Smirnoff break; 930b0cd2017SGleb Smirnoff } 93189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 93252051abcSAlan Cox return (error); 9331de11f1aSAlan Cox } else if (error != 0) { 934756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 9351de11f1aSAlan Cox return (VM_PAGER_ERROR); 936b0cd2017SGleb Smirnoff } 937bbc0ec52SDavid Greenman 93826f9a767SRodney W. Grimes /* 939b0cd2017SGleb Smirnoff * If the file system supports BMAP, but blocksize is smaller 940b0cd2017SGleb Smirnoff * than a page size, then use special small filesystem code. 94126f9a767SRodney W. Grimes */ 942b0cd2017SGleb Smirnoff if (pagesperblock == 0) { 943756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 944b0cd2017SGleb Smirnoff for (i = 0; i < count; i++) { 94583c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 94683c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodepgsin); 947b0cd2017SGleb Smirnoff error = vnode_pager_input_smlfs(object, m[i]); 948b0cd2017SGleb Smirnoff if (error) 949b0cd2017SGleb Smirnoff break; 950b0cd2017SGleb Smirnoff } 951b0cd2017SGleb Smirnoff return (error); 95226f9a767SRodney W. Grimes } 9538d17e694SJulian Elischer 95426f9a767SRodney W. Grimes /* 955b0cd2017SGleb Smirnoff * A sparse file can be encountered only for a single page request, 956763df3ecSPedro F. Giffuni * which may not be preceded by call to vm_pager_haspage(). 957a7fecb4dSAlan Cox */ 958b0cd2017SGleb Smirnoff if (bp->b_blkno == -1) { 959b0cd2017SGleb Smirnoff KASSERT(count == 1, 960b0cd2017SGleb Smirnoff ("%s: array[%d] request to a sparse file %p", __func__, 961b0cd2017SGleb Smirnoff count, vp)); 962756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 963b0cd2017SGleb Smirnoff pmap_zero_page(m[0]); 964b0cd2017SGleb Smirnoff KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty", 965b0cd2017SGleb Smirnoff __func__, m[0])); 9660012f373SJeff Roberson vm_page_valid(m[0]); 967f4f83da0SAlan Cox return (VM_PAGER_OK); 968b0cd2017SGleb Smirnoff } 969b0cd2017SGleb Smirnoff 970e48b82bdSGleb Smirnoff #ifdef INVARIANTS 971e48b82bdSGleb Smirnoff blkno0 = bp->b_blkno; 972e48b82bdSGleb Smirnoff #endif 973b0cd2017SGleb Smirnoff bp->b_blkno += (foff % bsize) / DEV_BSIZE; 974b0cd2017SGleb Smirnoff 975b0cd2017SGleb Smirnoff /* Recalculate blocks available after/before to pages. */ 976b0cd2017SGleb Smirnoff poff = (foff % bsize) / PAGE_SIZE; 977b0cd2017SGleb Smirnoff before *= pagesperblock; 978b0cd2017SGleb Smirnoff before += poff; 979b0cd2017SGleb Smirnoff after *= pagesperblock; 980b0cd2017SGleb Smirnoff after += pagesperblock - (poff + 1); 981b0cd2017SGleb Smirnoff if (m[0]->pindex + after >= object->size) 982b0cd2017SGleb Smirnoff after = object->size - 1 - m[0]->pindex; 983b0cd2017SGleb Smirnoff KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d", 984b0cd2017SGleb Smirnoff __func__, count, after + 1)); 985b0cd2017SGleb Smirnoff after -= count - 1; 986b0cd2017SGleb Smirnoff 987b0cd2017SGleb Smirnoff /* Trim requested rbehind/rahead to possible values. */ 988b0cd2017SGleb Smirnoff rbehind = a_rbehind ? *a_rbehind : 0; 989b0cd2017SGleb Smirnoff rahead = a_rahead ? *a_rahead : 0; 990b0cd2017SGleb Smirnoff rbehind = min(rbehind, before); 991b0cd2017SGleb Smirnoff rbehind = min(rbehind, m[0]->pindex); 992b0cd2017SGleb Smirnoff rahead = min(rahead, after); 993b0cd2017SGleb Smirnoff rahead = min(rahead, object->size - m[count - 1]->pindex); 994e48b82bdSGleb Smirnoff /* 995e48b82bdSGleb Smirnoff * Check that total amount of pages fit into buf. Trim rbehind and 996e48b82bdSGleb Smirnoff * rahead evenly if not. 997e48b82bdSGleb Smirnoff */ 998cd853791SKonstantin Belousov if (rbehind + rahead + count > atop(maxphys)) { 999e48b82bdSGleb Smirnoff int trim, sum; 1000e48b82bdSGleb Smirnoff 1001cd853791SKonstantin Belousov trim = rbehind + rahead + count - atop(maxphys) + 1; 1002e48b82bdSGleb Smirnoff sum = rbehind + rahead; 1003e48b82bdSGleb Smirnoff if (rbehind == before) { 1004e48b82bdSGleb Smirnoff /* Roundup rbehind trim to block size. */ 1005e48b82bdSGleb Smirnoff rbehind -= roundup(trim * rbehind / sum, pagesperblock); 1006e48b82bdSGleb Smirnoff if (rbehind < 0) 1007e48b82bdSGleb Smirnoff rbehind = 0; 1008e48b82bdSGleb Smirnoff } else 1009e48b82bdSGleb Smirnoff rbehind -= trim * rbehind / sum; 1010e48b82bdSGleb Smirnoff rahead -= trim * rahead / sum; 1011e48b82bdSGleb Smirnoff } 1012cd853791SKonstantin Belousov KASSERT(rbehind + rahead + count <= atop(maxphys), 1013cd853791SKonstantin Belousov ("%s: behind %d ahead %d count %d maxphys %lu", __func__, 1014cd853791SKonstantin Belousov rbehind, rahead, count, maxphys)); 1015b0cd2017SGleb Smirnoff 1016b0cd2017SGleb Smirnoff /* 1017b0cd2017SGleb Smirnoff * Fill in the bp->b_pages[] array with requested and optional 1018b0cd2017SGleb Smirnoff * read behind or read ahead pages. Read behind pages are looked 1019b0cd2017SGleb Smirnoff * up in a backward direction, down to a first cached page. Same 1020b0cd2017SGleb Smirnoff * for read ahead pages, but there is no need to shift the array 1021b0cd2017SGleb Smirnoff * in case of encountering a cached page. 1022b0cd2017SGleb Smirnoff */ 1023b0cd2017SGleb Smirnoff i = bp->b_npages = 0; 1024b0cd2017SGleb Smirnoff if (rbehind) { 1025b0cd2017SGleb Smirnoff vm_pindex_t startpindex, tpindex; 1026b0cd2017SGleb Smirnoff vm_page_t p; 1027b0cd2017SGleb Smirnoff 1028a7fecb4dSAlan Cox VM_OBJECT_WLOCK(object); 1029b0cd2017SGleb Smirnoff startpindex = m[0]->pindex - rbehind; 1030b0cd2017SGleb Smirnoff if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL && 1031b0cd2017SGleb Smirnoff p->pindex >= startpindex) 1032b0cd2017SGleb Smirnoff startpindex = p->pindex + 1; 1033b0cd2017SGleb Smirnoff 1034b0cd2017SGleb Smirnoff /* tpindex is unsigned; beware of numeric underflow. */ 1035b0cd2017SGleb Smirnoff for (tpindex = m[0]->pindex - 1; 1036b0cd2017SGleb Smirnoff tpindex >= startpindex && tpindex < m[0]->pindex; 1037b0cd2017SGleb Smirnoff tpindex--, i++) { 10387667839aSAlan Cox p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 1039b0cd2017SGleb Smirnoff if (p == NULL) { 1040b0cd2017SGleb Smirnoff /* Shift the array. */ 1041b0cd2017SGleb Smirnoff for (int j = 0; j < i; j++) 1042b0cd2017SGleb Smirnoff bp->b_pages[j] = bp->b_pages[j + 1043b0cd2017SGleb Smirnoff tpindex + 1 - startpindex]; 1044b0cd2017SGleb Smirnoff break; 1045b0cd2017SGleb Smirnoff } 1046b0cd2017SGleb Smirnoff bp->b_pages[tpindex - startpindex] = p; 1047a7fecb4dSAlan Cox } 10480bdb7528SDavid Greenman 1049b0cd2017SGleb Smirnoff bp->b_pgbefore = i; 1050b0cd2017SGleb Smirnoff bp->b_npages += i; 1051b0cd2017SGleb Smirnoff bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE; 1052b0cd2017SGleb Smirnoff } else 1053b0cd2017SGleb Smirnoff bp->b_pgbefore = 0; 1054b0cd2017SGleb Smirnoff 1055b0cd2017SGleb Smirnoff /* Requested pages. */ 1056b0cd2017SGleb Smirnoff for (int j = 0; j < count; j++, i++) 1057b0cd2017SGleb Smirnoff bp->b_pages[i] = m[j]; 1058b0cd2017SGleb Smirnoff bp->b_npages += count; 1059b0cd2017SGleb Smirnoff 1060b0cd2017SGleb Smirnoff if (rahead) { 1061b0cd2017SGleb Smirnoff vm_pindex_t endpindex, tpindex; 1062b0cd2017SGleb Smirnoff vm_page_t p; 1063b0cd2017SGleb Smirnoff 1064b0cd2017SGleb Smirnoff if (!VM_OBJECT_WOWNED(object)) 1065eac91e32SKonstantin Belousov VM_OBJECT_WLOCK(object); 1066b0cd2017SGleb Smirnoff endpindex = m[count - 1]->pindex + rahead + 1; 1067b0cd2017SGleb Smirnoff if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL && 1068b0cd2017SGleb Smirnoff p->pindex < endpindex) 1069b0cd2017SGleb Smirnoff endpindex = p->pindex; 1070b0cd2017SGleb Smirnoff if (endpindex > object->size) 1071b0cd2017SGleb Smirnoff endpindex = object->size; 1072b0cd2017SGleb Smirnoff 1073b0cd2017SGleb Smirnoff for (tpindex = m[count - 1]->pindex + 1; 1074b0cd2017SGleb Smirnoff tpindex < endpindex; i++, tpindex++) { 10757667839aSAlan Cox p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 1076b0cd2017SGleb Smirnoff if (p == NULL) 1077b0cd2017SGleb Smirnoff break; 1078b0cd2017SGleb Smirnoff bp->b_pages[i] = p; 1079eac91e32SKonstantin Belousov } 1080b0cd2017SGleb Smirnoff 1081b0cd2017SGleb Smirnoff bp->b_pgafter = i - bp->b_npages; 1082b0cd2017SGleb Smirnoff bp->b_npages = i; 1083b0cd2017SGleb Smirnoff } else 1084b0cd2017SGleb Smirnoff bp->b_pgafter = 0; 1085b0cd2017SGleb Smirnoff 1086b0cd2017SGleb Smirnoff if (VM_OBJECT_WOWNED(object)) 1087eac91e32SKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1088b0cd2017SGleb Smirnoff 1089b0cd2017SGleb Smirnoff /* Report back actual behind/ahead read. */ 1090b0cd2017SGleb Smirnoff if (a_rbehind) 1091b0cd2017SGleb Smirnoff *a_rbehind = bp->b_pgbefore; 1092b0cd2017SGleb Smirnoff if (a_rahead) 1093b0cd2017SGleb Smirnoff *a_rahead = bp->b_pgafter; 1094b0cd2017SGleb Smirnoff 1095e48b82bdSGleb Smirnoff #ifdef INVARIANTS 1096cd853791SKonstantin Belousov KASSERT(bp->b_npages <= atop(maxphys), 1097b0cd2017SGleb Smirnoff ("%s: buf %p overflowed", __func__, bp)); 10984f56243aSGleb Smirnoff for (int j = 1, prev = 0; j < bp->b_npages; j++) { 10991e0c121fSGleb Smirnoff if (bp->b_pages[j] == bogus_page) 11001e0c121fSGleb Smirnoff continue; 11011e0c121fSGleb Smirnoff KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex == 11021e0c121fSGleb Smirnoff j - prev, ("%s: pages array not consecutive, bp %p", 11031e0c121fSGleb Smirnoff __func__, bp)); 11041e0c121fSGleb Smirnoff prev = j; 11051e0c121fSGleb Smirnoff } 1106e48b82bdSGleb Smirnoff #endif 1107eac91e32SKonstantin Belousov 11080d94caffSDavid Greenman /* 1109b0cd2017SGleb Smirnoff * Recalculate first offset and bytecount with regards to read behind. 1110b0cd2017SGleb Smirnoff * Truncate bytecount to vnode real size and round up physical size 1111b0cd2017SGleb Smirnoff * for real devices. 111226f9a767SRodney W. Grimes */ 1113b0cd2017SGleb Smirnoff foff = IDX_TO_OFF(bp->b_pages[0]->pindex); 1114b0cd2017SGleb Smirnoff bytecount = bp->b_npages << PAGE_SHIFT; 1115b0cd2017SGleb Smirnoff if ((foff + bytecount) > object->un_pager.vnp.vnp_size) 1116b0cd2017SGleb Smirnoff bytecount = object->un_pager.vnp.vnp_size - foff; 1117eac91e32SKonstantin Belousov secmask = bo->bo_bsize - 1; 11186229cc50SPoul-Henning Kamp KASSERT(secmask < PAGE_SIZE && secmask > 0, 1119b0cd2017SGleb Smirnoff ("%s: sector size %d too large", __func__, secmask + 1)); 1120b0cd2017SGleb Smirnoff bytecount = (bytecount + secmask) & ~secmask; 112126f9a767SRodney W. Grimes 112226f9a767SRodney W. Grimes /* 1123b0cd2017SGleb Smirnoff * And map the pages to be read into the kva, if the filesystem 11246ce697dcSKonstantin Belousov * requires mapped buffers. 112526f9a767SRodney W. Grimes */ 11262a5eef69SGleb Smirnoff if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 && 11276ce697dcSKonstantin Belousov unmapped_buf_allowed) { 11286ce697dcSKonstantin Belousov bp->b_data = unmapped_buf; 11296ce697dcSKonstantin Belousov bp->b_offset = 0; 1130fade8dd7SJeff Roberson } else { 1131fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 1132b0cd2017SGleb Smirnoff pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages); 1133fade8dd7SJeff Roberson } 113426f9a767SRodney W. Grimes 1135b0cd2017SGleb Smirnoff /* Build a minimal buffer header. */ 113621144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 1137bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 1138bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 1139a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 1140a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 11419c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 11421faacf5dSKirk McKusick bp->b_vp = vp; 1143b0cd2017SGleb Smirnoff bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount; 11442c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 1145e48b82bdSGleb Smirnoff KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) == 1146e48b82bdSGleb Smirnoff (blkno0 - bp->b_blkno) * DEV_BSIZE + 1147e48b82bdSGleb Smirnoff IDX_TO_OFF(m[0]->pindex) % bsize, 1148e48b82bdSGleb Smirnoff ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju " 1149e48b82bdSGleb Smirnoff "blkno0 %ju b_blkno %ju", bsize, 1150e48b82bdSGleb Smirnoff (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex, 1151e48b82bdSGleb Smirnoff (uintmax_t)blkno0, (uintmax_t)bp->b_blkno)); 115290effb23SGleb Smirnoff 1153b0cd2017SGleb Smirnoff atomic_add_long(&runningbufspace, bp->b_runningbufspace); 115483c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 115583c9dea1SGleb Smirnoff VM_CNT_ADD(v_vnodepgsin, bp->b_npages); 1156b0cd2017SGleb Smirnoff 115790effb23SGleb Smirnoff if (iodone != NULL) { /* async */ 1158b0cd2017SGleb Smirnoff bp->b_pgiodone = iodone; 115990effb23SGleb Smirnoff bp->b_caller1 = arg; 116090effb23SGleb Smirnoff bp->b_iodone = vnode_pager_generic_getpages_done_async; 116190effb23SGleb Smirnoff bp->b_flags |= B_ASYNC; 116290effb23SGleb Smirnoff BUF_KERNPROC(bp); 1163b792bebeSPoul-Henning Kamp bstrategy(bp); 1164b0cd2017SGleb Smirnoff return (VM_PAGER_OK); 116590effb23SGleb Smirnoff } else { 116690effb23SGleb Smirnoff bp->b_iodone = bdone; 116790effb23SGleb Smirnoff bstrategy(bp); 11686a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnread"); 116990effb23SGleb Smirnoff error = vnode_pager_generic_getpages_done(bp); 11701bb5ad63SGleb Smirnoff for (i = 0; i < bp->b_npages; i++) 11716ce697dcSKonstantin Belousov bp->b_pages[i] = NULL; 11721faacf5dSKirk McKusick bp->b_vp = NULL; 11739c83534dSPoul-Henning Kamp pbrelbo(bp); 1174756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 117590effb23SGleb Smirnoff return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK); 117690effb23SGleb Smirnoff } 1177b0cd2017SGleb Smirnoff } 117890effb23SGleb Smirnoff 117990effb23SGleb Smirnoff static void 118090effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp) 118190effb23SGleb Smirnoff { 118290effb23SGleb Smirnoff int error; 118390effb23SGleb Smirnoff 118490effb23SGleb Smirnoff error = vnode_pager_generic_getpages_done(bp); 1185b0cd2017SGleb Smirnoff /* Run the iodone upon the requested range. */ 1186b0cd2017SGleb Smirnoff bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore, 1187b0cd2017SGleb Smirnoff bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error); 118890effb23SGleb Smirnoff for (int i = 0; i < bp->b_npages; i++) 118990effb23SGleb Smirnoff bp->b_pages[i] = NULL; 119090effb23SGleb Smirnoff bp->b_vp = NULL; 119190effb23SGleb Smirnoff pbrelbo(bp); 1192756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 119390effb23SGleb Smirnoff } 119490effb23SGleb Smirnoff 119590effb23SGleb Smirnoff static int 119690effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp) 119790effb23SGleb Smirnoff { 119890effb23SGleb Smirnoff vm_object_t object; 119990effb23SGleb Smirnoff off_t tfoff, nextoff; 120090effb23SGleb Smirnoff int i, error; 120190effb23SGleb Smirnoff 1202cafbf0c6SWarner Losh KASSERT((bp->b_ioflags & BIO_ERROR) == 0 || bp->b_error != 0, 1203cafbf0c6SWarner Losh ("%s: buf error but b_error == 0\n", __func__)); 1204cafbf0c6SWarner Losh error = (bp->b_ioflags & BIO_ERROR) != 0 ? bp->b_error : 0; 120590effb23SGleb Smirnoff object = bp->b_vp->v_object; 120690effb23SGleb Smirnoff 120790effb23SGleb Smirnoff if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) { 1208fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 1209fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 1210fade8dd7SJeff Roberson pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, 121190effb23SGleb Smirnoff bp->b_npages); 121290effb23SGleb Smirnoff } 1213fade8dd7SJeff Roberson bzero(bp->b_data + bp->b_bcount, 121490effb23SGleb Smirnoff PAGE_SIZE * bp->b_npages - bp->b_bcount); 121590effb23SGleb Smirnoff } 1216fade8dd7SJeff Roberson if (buf_mapped(bp)) { 1217fade8dd7SJeff Roberson pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1218fade8dd7SJeff Roberson bp->b_data = unmapped_buf; 121990effb23SGleb Smirnoff } 122026f9a767SRodney W. Grimes 12211bd12a3bSChuck Silvers /* 12221bd12a3bSChuck Silvers * If the read failed, we must free any read ahead/behind pages here. 12231bd12a3bSChuck Silvers * The requested pages are freed by the caller (for sync requests) 12241bd12a3bSChuck Silvers * or by the bp->b_pgiodone callback (for async requests). 12251bd12a3bSChuck Silvers */ 12261bd12a3bSChuck Silvers if (error != 0) { 12271bd12a3bSChuck Silvers VM_OBJECT_WLOCK(object); 12281bd12a3bSChuck Silvers for (i = 0; i < bp->b_pgbefore; i++) 12291bd12a3bSChuck Silvers vm_page_free_invalid(bp->b_pages[i]); 12301bd12a3bSChuck Silvers for (i = bp->b_npages - bp->b_pgafter; i < bp->b_npages; i++) 12311bd12a3bSChuck Silvers vm_page_free_invalid(bp->b_pages[i]); 12321bd12a3bSChuck Silvers VM_OBJECT_WUNLOCK(object); 12331bd12a3bSChuck Silvers return (error); 12341bd12a3bSChuck Silvers } 12351bd12a3bSChuck Silvers 12367f935055SJeff Roberson /* Read lock to protect size. */ 12377f935055SJeff Roberson VM_OBJECT_RLOCK(object); 123890effb23SGleb Smirnoff for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex); 123990effb23SGleb Smirnoff i < bp->b_npages; i++, tfoff = nextoff) { 12408f9110f6SJohn Dyson vm_page_t mt; 12418f9110f6SJohn Dyson 12428f9110f6SJohn Dyson nextoff = tfoff + PAGE_SIZE; 124390effb23SGleb Smirnoff mt = bp->b_pages[i]; 12442f81c92eSMark Johnston if (mt == bogus_page) 12452f81c92eSMark Johnston continue; 12468f9110f6SJohn Dyson 124754746b67SDmitrij Tejblum if (nextoff <= object->un_pager.vnp.vnp_size) { 12488d17e694SJulian Elischer /* 12498d17e694SJulian Elischer * Read filled up entire page. 12508d17e694SJulian Elischer */ 12510012f373SJeff Roberson vm_page_valid(mt); 1252016a3c93SAlan Cox KASSERT(mt->dirty == 0, 125379f0deb9SGleb Smirnoff ("%s: page %p is dirty", __func__, mt)); 1254016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(mt), 125579f0deb9SGleb Smirnoff ("%s: page %p is mapped", __func__, mt)); 12568f9110f6SJohn Dyson } else { 12578d17e694SJulian Elischer /* 125842eb4108SAlan Cox * Read did not fill up entire page. 12598d17e694SJulian Elischer * 1260c3dbadc1SChuck Silvers * Currently we do not set the entire page valid, 1261c3dbadc1SChuck Silvers * we just try to clear the piece that we couldn't 1262c3dbadc1SChuck Silvers * read. 12638d17e694SJulian Elischer */ 1264dc874f98SKonstantin Belousov vm_page_set_valid_range(mt, 0, 126554746b67SDmitrij Tejblum object->un_pager.vnp.vnp_size - tfoff); 126642eb4108SAlan Cox KASSERT((mt->dirty & vm_page_bits(0, 1267c3dbadc1SChuck Silvers object->un_pager.vnp.vnp_size - tfoff)) == 0, 1268c3dbadc1SChuck Silvers ("%s: page %p is dirty", __func__, mt)); 12698f9110f6SJohn Dyson } 12708f9110f6SJohn Dyson 1271b0cd2017SGleb Smirnoff if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter) 1272b6c00483SKonstantin Belousov vm_page_readahead_finish(mt); 127303679e23SAlan Cox } 12747f935055SJeff Roberson VM_OBJECT_RUNLOCK(object); 127590effb23SGleb Smirnoff 127690effb23SGleb Smirnoff return (error); 127726f9a767SRodney W. Grimes } 127826f9a767SRodney W. Grimes 1279ce75f2c3SMike Smith /* 1280ce75f2c3SMike Smith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 1281ce75f2c3SMike Smith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 1282ce75f2c3SMike Smith * vnode_pager_generic_putpages() to implement the previous behaviour. 1283ce75f2c3SMike Smith * 1284ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 1285ce75f2c3SMike Smith * backing vp's VOP_PUTPAGES. 1286ce75f2c3SMike Smith */ 1287e4542174SMatthew Dillon static void 12887ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count, 128933cad9e9SKonstantin Belousov int flags, int *rtvals) 1290170db9c6SJohn Dyson { 1291b8ebd99aSJohn Baldwin int rtval __diagused; 1292170db9c6SJohn Dyson struct vnode *vp; 129386ffbd76SMike Smith int bytes = count * PAGE_SIZE; 1294ad980522SJohn Dyson 12950e3cdf2cSAlan Cox /* 12960e3cdf2cSAlan Cox * Force synchronous operation if we are extremely low on memory 12970e3cdf2cSAlan Cox * to prevent a low-memory deadlock. VOP operations often need to 12980e3cdf2cSAlan Cox * allocate more memory to initiate the I/O ( i.e. do a BMAP 12990e3cdf2cSAlan Cox * operation ). The swapper handles the case by limiting the amount 13000e3cdf2cSAlan Cox * of asynchronous I/O, but that sort of solution doesn't scale well 13010e3cdf2cSAlan Cox * for the vnode pager without a lot of work. 13020e3cdf2cSAlan Cox * 13030e3cdf2cSAlan Cox * Also, the backing vnode's iodone routine may not wake the pageout 13040e3cdf2cSAlan Cox * daemon up. This should be probably be addressed XXX. 13050e3cdf2cSAlan Cox */ 13060e3cdf2cSAlan Cox 1307e2068d0bSJeff Roberson if (vm_page_count_min()) 130833cad9e9SKonstantin Belousov flags |= VM_PAGER_PUT_SYNC; 13090e3cdf2cSAlan Cox 13100e3cdf2cSAlan Cox /* 13110e3cdf2cSAlan Cox * Call device-specific putpages function 13120e3cdf2cSAlan Cox */ 1313170db9c6SJohn Dyson vp = object->handle; 131489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 131533cad9e9SKonstantin Belousov rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals); 131623955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 131723955314SAlfred Perlstein ("vnode_pager: stale FS putpages\n")); 131889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1319170db9c6SJohn Dyson } 1320170db9c6SJohn Dyson 132105877a85SKonstantin Belousov static int 132205877a85SKonstantin Belousov vn_off2bidx(vm_ooffset_t offset) 132305877a85SKonstantin Belousov { 132405877a85SKonstantin Belousov 132505877a85SKonstantin Belousov return ((offset & PAGE_MASK) / DEV_BSIZE); 132605877a85SKonstantin Belousov } 132705877a85SKonstantin Belousov 132805877a85SKonstantin Belousov static bool 132905877a85SKonstantin Belousov vn_dirty_blk(vm_page_t m, vm_ooffset_t offset) 133005877a85SKonstantin Belousov { 133105877a85SKonstantin Belousov 133205877a85SKonstantin Belousov KASSERT(IDX_TO_OFF(m->pindex) <= offset && 133305877a85SKonstantin Belousov offset < IDX_TO_OFF(m->pindex + 1), 133405877a85SKonstantin Belousov ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex, 133505877a85SKonstantin Belousov (uintmax_t)offset)); 133605877a85SKonstantin Belousov return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0); 133705877a85SKonstantin Belousov } 1338ce75f2c3SMike Smith 133926f9a767SRodney W. Grimes /* 1340ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 13414491ea91SEivind Eklund * own vnodes if they fail to implement VOP_PUTPAGES. 13422b6b0df7SMatthew Dillon * 13432b6b0df7SMatthew Dillon * This is typically called indirectly via the pageout daemon and 1344763df3ecSPedro F. Giffuni * clustering has already typically occurred, so in general we ask the 13452b6b0df7SMatthew Dillon * underlying filesystem to write the data out asynchronously rather 13462b6b0df7SMatthew Dillon * then delayed. 134726f9a767SRodney W. Grimes */ 1348ce75f2c3SMike Smith int 1349c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount, 1350c46b90e9SAlan Cox int flags, int *rtvals) 135126f9a767SRodney W. Grimes { 1352ce75f2c3SMike Smith vm_object_t object; 1353c46b90e9SAlan Cox vm_page_t m; 135405877a85SKonstantin Belousov vm_ooffset_t maxblksz, next_offset, poffset, prev_offset; 1355f6b04d2bSDavid Greenman struct uio auio; 1356f6b04d2bSDavid Greenman struct iovec aiov; 135705877a85SKonstantin Belousov off_t prev_resid, wrsz; 1358e6c44f65SKonstantin Belousov int count, error, i, maxsize, ncount, pgoff, ppscheck; 135905877a85SKonstantin Belousov bool in_hole; 1360dd498befSPaul Saab static struct timeval lastfail; 1361dd498befSPaul Saab static int curfail; 136226f9a767SRodney W. Grimes 1363ce75f2c3SMike Smith object = vp->v_object; 1364ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 1365ce75f2c3SMike Smith 136626f9a767SRodney W. Grimes for (i = 0; i < count; i++) 1367031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_ERROR; 136826f9a767SRodney W. Grimes 1369c46b90e9SAlan Cox if ((int64_t)ma[0]->pindex < 0) { 1370e6c44f65SKonstantin Belousov printf("vnode_pager_generic_putpages: " 1371e6c44f65SKonstantin Belousov "attempt to write meta-data 0x%jx(%lx)\n", 1372e6c44f65SKonstantin Belousov (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty); 1373f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 1374e6c44f65SKonstantin Belousov return (VM_PAGER_BAD); 13750d94caffSDavid Greenman } 13760bdb7528SDavid Greenman 1377f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 1378f6b04d2bSDavid Greenman ncount = count; 137926f9a767SRodney W. Grimes 1380c46b90e9SAlan Cox poffset = IDX_TO_OFF(ma[0]->pindex); 138100a6f47fSMatthew Dillon 138200a6f47fSMatthew Dillon /* 138300a6f47fSMatthew Dillon * If the page-aligned write is larger then the actual file we 1384763df3ecSPedro F. Giffuni * have to invalidate pages occurring beyond the file EOF. However, 138500a6f47fSMatthew Dillon * there is an edge case where a file may not be page-aligned where 138600a6f47fSMatthew Dillon * the last page is partially invalid. In this case the filesystem 138700a6f47fSMatthew Dillon * may not properly clear the dirty bits for the entire page (which 138800a6f47fSMatthew Dillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 1389efec381dSMark Johnston * With the page busied we are free to fix up the dirty bits here. 13903ebeaf59SMatthew Dillon * 13913ebeaf59SMatthew Dillon * We do not under any circumstances truncate the valid bits, as 13923ebeaf59SMatthew Dillon * this will screw up bogus page replacement. 139300a6f47fSMatthew Dillon */ 1394b3d4ab66SKonstantin Belousov VM_OBJECT_RLOCK(object); 1395a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 139600a6f47fSMatthew Dillon if (object->un_pager.vnp.vnp_size > poffset) { 1397a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 1398aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 139900a6f47fSMatthew Dillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1400938cdc42SKonstantin Belousov pgoff = roundup2(pgoff, DEV_BSIZE); 1401938cdc42SKonstantin Belousov 1402c46b90e9SAlan Cox /* 14037f935055SJeff Roberson * If the page is busy and the following 1404c46b90e9SAlan Cox * conditions hold, then the page's dirty 1405c46b90e9SAlan Cox * field cannot be concurrently changed by a 1406c46b90e9SAlan Cox * pmap operation. 1407c46b90e9SAlan Cox */ 1408c46b90e9SAlan Cox m = ma[ncount - 1]; 1409c7aebda8SAttilio Rao vm_page_assert_sbusied(m); 14106031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1411c46b90e9SAlan Cox ("vnode_pager_generic_putpages: page %p is not read-only", m)); 1412e6c44f65SKonstantin Belousov MPASS(m->dirty != 0); 1413c46b90e9SAlan Cox vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 1414c46b90e9SAlan Cox pgoff); 141500a6f47fSMatthew Dillon } 141600a6f47fSMatthew Dillon } else { 141700a6f47fSMatthew Dillon maxsize = 0; 141800a6f47fSMatthew Dillon ncount = 0; 141900a6f47fSMatthew Dillon } 1420e6c44f65SKonstantin Belousov for (i = ncount; i < count; i++) 1421f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 1422f6b04d2bSDavid Greenman } 14237f935055SJeff Roberson VM_OBJECT_RUNLOCK(object); 142426f9a767SRodney W. Grimes 1425f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 1426f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 1427f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 1428e6c44f65SKonstantin Belousov auio.uio_td = NULL; 142905877a85SKonstantin Belousov maxblksz = roundup2(poffset + maxsize, DEV_BSIZE); 143005877a85SKonstantin Belousov 143105877a85SKonstantin Belousov for (prev_offset = poffset; prev_offset < maxblksz;) { 143205877a85SKonstantin Belousov /* Skip clean blocks. */ 143305877a85SKonstantin Belousov for (in_hole = true; in_hole && prev_offset < maxblksz;) { 143405877a85SKonstantin Belousov m = ma[OFF_TO_IDX(prev_offset - poffset)]; 143505877a85SKonstantin Belousov for (i = vn_off2bidx(prev_offset); 143605877a85SKonstantin Belousov i < sizeof(vm_page_bits_t) * NBBY && 143705877a85SKonstantin Belousov prev_offset < maxblksz; i++) { 143805877a85SKonstantin Belousov if (vn_dirty_blk(m, prev_offset)) { 143905877a85SKonstantin Belousov in_hole = false; 144005877a85SKonstantin Belousov break; 144105877a85SKonstantin Belousov } 144205877a85SKonstantin Belousov prev_offset += DEV_BSIZE; 144305877a85SKonstantin Belousov } 144405877a85SKonstantin Belousov } 144505877a85SKonstantin Belousov if (in_hole) 144605877a85SKonstantin Belousov goto write_done; 144705877a85SKonstantin Belousov 144805877a85SKonstantin Belousov /* Find longest run of dirty blocks. */ 144905877a85SKonstantin Belousov for (next_offset = prev_offset; next_offset < maxblksz;) { 145005877a85SKonstantin Belousov m = ma[OFF_TO_IDX(next_offset - poffset)]; 145105877a85SKonstantin Belousov for (i = vn_off2bidx(next_offset); 145205877a85SKonstantin Belousov i < sizeof(vm_page_bits_t) * NBBY && 145305877a85SKonstantin Belousov next_offset < maxblksz; i++) { 145405877a85SKonstantin Belousov if (!vn_dirty_blk(m, next_offset)) 145505877a85SKonstantin Belousov goto start_write; 145605877a85SKonstantin Belousov next_offset += DEV_BSIZE; 145705877a85SKonstantin Belousov } 145805877a85SKonstantin Belousov } 145905877a85SKonstantin Belousov start_write: 146005877a85SKonstantin Belousov if (next_offset > poffset + maxsize) 146105877a85SKonstantin Belousov next_offset = poffset + maxsize; 146205877a85SKonstantin Belousov 146305877a85SKonstantin Belousov /* 146405877a85SKonstantin Belousov * Getting here requires finding a dirty block in the 146505877a85SKonstantin Belousov * 'skip clean blocks' loop. 146605877a85SKonstantin Belousov */ 146705877a85SKonstantin Belousov MPASS(prev_offset < next_offset); 146805877a85SKonstantin Belousov 146905877a85SKonstantin Belousov aiov.iov_base = NULL; 147005877a85SKonstantin Belousov auio.uio_iovcnt = 1; 147105877a85SKonstantin Belousov auio.uio_offset = prev_offset; 147205877a85SKonstantin Belousov prev_resid = auio.uio_resid = aiov.iov_len = next_offset - 147305877a85SKonstantin Belousov prev_offset; 147405877a85SKonstantin Belousov error = VOP_WRITE(vp, &auio, 147505877a85SKonstantin Belousov vnode_pager_putpages_ioflags(flags), curthread->td_ucred); 147605877a85SKonstantin Belousov 147705877a85SKonstantin Belousov wrsz = prev_resid - auio.uio_resid; 147805877a85SKonstantin Belousov if (wrsz == 0) { 147905877a85SKonstantin Belousov if (ppsratecheck(&lastfail, &curfail, 1) != 0) { 148005877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: " 148105877a85SKonstantin Belousov "zero-length write at %ju resid %zd\n", 148205877a85SKonstantin Belousov auio.uio_offset, auio.uio_resid); 148305877a85SKonstantin Belousov } 148405877a85SKonstantin Belousov break; 148505877a85SKonstantin Belousov } 148605877a85SKonstantin Belousov 148705877a85SKonstantin Belousov /* Adjust the starting offset for next iteration. */ 148805877a85SKonstantin Belousov prev_offset += wrsz; 148905877a85SKonstantin Belousov MPASS(auio.uio_offset == prev_offset); 1490f6b04d2bSDavid Greenman 14913dbb0ca6SKonstantin Belousov ppscheck = 0; 149205877a85SKonstantin Belousov if (error != 0 && (ppscheck = ppsratecheck(&lastfail, 149305877a85SKonstantin Belousov &curfail, 1)) != 0) 149405877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: I/O error %d\n", 149505877a85SKonstantin Belousov error); 1496e6c44f65SKonstantin Belousov if (auio.uio_resid != 0 && (ppscheck != 0 || 1497e6c44f65SKonstantin Belousov ppsratecheck(&lastfail, &curfail, 1) != 0)) 149805877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: residual I/O %zd " 149905877a85SKonstantin Belousov "at %ju\n", auio.uio_resid, 150005877a85SKonstantin Belousov (uintmax_t)ma[0]->pindex); 150105877a85SKonstantin Belousov if (error != 0 || auio.uio_resid != 0) 150205877a85SKonstantin Belousov break; 150305877a85SKonstantin Belousov } 150405877a85SKonstantin Belousov write_done: 150505877a85SKonstantin Belousov /* Mark completely processed pages. */ 150605877a85SKonstantin Belousov for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++) 150726f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 150805877a85SKonstantin Belousov /* Mark partial EOF page. */ 150905877a85SKonstantin Belousov if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0) 151005877a85SKonstantin Belousov rtvals[i++] = VM_PAGER_OK; 151105877a85SKonstantin Belousov /* Unwritten pages in range, free bonus if the page is clean. */ 151205877a85SKonstantin Belousov for (; i < ncount; i++) 151305877a85SKonstantin Belousov rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR; 151405877a85SKonstantin Belousov VM_CNT_ADD(v_vnodepgsout, i); 151505877a85SKonstantin Belousov VM_CNT_INC(v_vnodeout); 1516e6c44f65SKonstantin Belousov return (rtvals[0]); 151726f9a767SRodney W. Grimes } 1518031ec8c1SKonstantin Belousov 151965b9599aSKonstantin Belousov int 152065b9599aSKonstantin Belousov vnode_pager_putpages_ioflags(int pager_flags) 152165b9599aSKonstantin Belousov { 152265b9599aSKonstantin Belousov int ioflags; 152365b9599aSKonstantin Belousov 152465b9599aSKonstantin Belousov /* 152565b9599aSKonstantin Belousov * Pageouts are already clustered, use IO_ASYNC to force a 152665b9599aSKonstantin Belousov * bawrite() rather then a bdwrite() to prevent paging I/O 152765b9599aSKonstantin Belousov * from saturating the buffer cache. Dummy-up the sequential 152865b9599aSKonstantin Belousov * heuristic to cause large ranges to cluster. If neither 152965b9599aSKonstantin Belousov * IO_SYNC or IO_ASYNC is set, the system decides how to 153065b9599aSKonstantin Belousov * cluster. 153165b9599aSKonstantin Belousov */ 153265b9599aSKonstantin Belousov ioflags = IO_VMIO; 153365b9599aSKonstantin Belousov if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0) 153465b9599aSKonstantin Belousov ioflags |= IO_SYNC; 153565b9599aSKonstantin Belousov else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0) 153665b9599aSKonstantin Belousov ioflags |= IO_ASYNC; 153765b9599aSKonstantin Belousov ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0; 153865b9599aSKonstantin Belousov ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0; 153965b9599aSKonstantin Belousov ioflags |= IO_SEQMAX << IO_SEQSHIFT; 154065b9599aSKonstantin Belousov return (ioflags); 154165b9599aSKonstantin Belousov } 154265b9599aSKonstantin Belousov 1543555b7bb4SKonstantin Belousov /* 1544555b7bb4SKonstantin Belousov * vnode_pager_undirty_pages(). 1545555b7bb4SKonstantin Belousov * 1546555b7bb4SKonstantin Belousov * A helper to mark pages as clean after pageout that was possibly 1547555b7bb4SKonstantin Belousov * done with a short write. The lpos argument specifies the page run 1548555b7bb4SKonstantin Belousov * length in bytes, and the written argument specifies how many bytes 1549555b7bb4SKonstantin Belousov * were actually written. eof is the offset past the last valid byte 1550555b7bb4SKonstantin Belousov * in the vnode using the absolute file position of the first byte in 1551555b7bb4SKonstantin Belousov * the run as the base from which it is computed. 1552555b7bb4SKonstantin Belousov */ 1553031ec8c1SKonstantin Belousov void 1554555b7bb4SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof, 1555555b7bb4SKonstantin Belousov int lpos) 1556031ec8c1SKonstantin Belousov { 1557555b7bb4SKonstantin Belousov int i, pos, pos_devb; 1558031ec8c1SKonstantin Belousov 1559555b7bb4SKonstantin Belousov if (written == 0 && eof >= lpos) 15609d17da3bSKonstantin Belousov return; 1561031ec8c1SKonstantin Belousov for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) { 1562031ec8c1SKonstantin Belousov if (pos < trunc_page(written)) { 1563031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_OK; 1564031ec8c1SKonstantin Belousov vm_page_undirty(ma[i]); 1565031ec8c1SKonstantin Belousov } else { 1566031ec8c1SKonstantin Belousov /* Partially written page. */ 1567031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_AGAIN; 1568031ec8c1SKonstantin Belousov vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK); 1569031ec8c1SKonstantin Belousov } 1570031ec8c1SKonstantin Belousov } 1571555b7bb4SKonstantin Belousov if (eof >= lpos) /* avoid truncation */ 15727f935055SJeff Roberson return; 1573555b7bb4SKonstantin Belousov for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) { 1574555b7bb4SKonstantin Belousov if (pos != trunc_page(pos)) { 1575555b7bb4SKonstantin Belousov /* 1576555b7bb4SKonstantin Belousov * The page contains the last valid byte in 1577555b7bb4SKonstantin Belousov * the vnode, mark the rest of the page as 1578555b7bb4SKonstantin Belousov * clean, potentially making the whole page 1579555b7bb4SKonstantin Belousov * clean. 1580555b7bb4SKonstantin Belousov */ 1581555b7bb4SKonstantin Belousov pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE); 1582555b7bb4SKonstantin Belousov vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE - 1583555b7bb4SKonstantin Belousov pos_devb); 1584555b7bb4SKonstantin Belousov 1585555b7bb4SKonstantin Belousov /* 1586555b7bb4SKonstantin Belousov * If the page was cleaned, report the pageout 1587555b7bb4SKonstantin Belousov * on it as successful. msync() no longer 1588555b7bb4SKonstantin Belousov * needs to write out the page, endlessly 1589555b7bb4SKonstantin Belousov * creating write requests and dirty buffers. 1590555b7bb4SKonstantin Belousov */ 1591555b7bb4SKonstantin Belousov if (ma[i]->dirty == 0) 1592555b7bb4SKonstantin Belousov rtvals[i] = VM_PAGER_OK; 1593555b7bb4SKonstantin Belousov 1594555b7bb4SKonstantin Belousov pos = round_page(pos); 1595555b7bb4SKonstantin Belousov } else { 1596555b7bb4SKonstantin Belousov /* vm_pageout_flush() clears dirty */ 1597555b7bb4SKonstantin Belousov rtvals[i] = VM_PAGER_BAD; 1598555b7bb4SKonstantin Belousov pos += PAGE_SIZE; 1599555b7bb4SKonstantin Belousov } 1600555b7bb4SKonstantin Belousov } 1601031ec8c1SKonstantin Belousov } 160284110e7eSKonstantin Belousov 1603fe7bcbafSKyle Evans static void 160484110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start, 160584110e7eSKonstantin Belousov vm_offset_t end) 160684110e7eSKonstantin Belousov { 160784110e7eSKonstantin Belousov struct vnode *vp; 160884110e7eSKonstantin Belousov vm_ooffset_t old_wm; 160984110e7eSKonstantin Belousov 161089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 161184110e7eSKonstantin Belousov if (object->type != OBJT_VNODE) { 161289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 161384110e7eSKonstantin Belousov return; 161484110e7eSKonstantin Belousov } 161584110e7eSKonstantin Belousov old_wm = object->un_pager.vnp.writemappings; 161684110e7eSKonstantin Belousov object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start; 161784110e7eSKonstantin Belousov vp = object->handle; 161884110e7eSKonstantin Belousov if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) { 161978022527SKonstantin Belousov ASSERT_VOP_LOCKED(vp, "v_writecount inc"); 162078022527SKonstantin Belousov VOP_ADD_WRITECOUNT_CHECKED(vp, 1); 1621b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 1622b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 162384110e7eSKonstantin Belousov } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) { 162478022527SKonstantin Belousov ASSERT_VOP_LOCKED(vp, "v_writecount dec"); 162578022527SKonstantin Belousov VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 1626b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 1627b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 162884110e7eSKonstantin Belousov } 162989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 163084110e7eSKonstantin Belousov } 163184110e7eSKonstantin Belousov 1632fe7bcbafSKyle Evans static void 163384110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start, 163484110e7eSKonstantin Belousov vm_offset_t end) 163584110e7eSKonstantin Belousov { 163684110e7eSKonstantin Belousov struct vnode *vp; 163784110e7eSKonstantin Belousov struct mount *mp; 163884110e7eSKonstantin Belousov vm_offset_t inc; 163984110e7eSKonstantin Belousov 164089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 164184110e7eSKonstantin Belousov 164284110e7eSKonstantin Belousov /* 164384110e7eSKonstantin Belousov * First, recheck the object type to account for the race when 164484110e7eSKonstantin Belousov * the vnode is reclaimed. 164584110e7eSKonstantin Belousov */ 164684110e7eSKonstantin Belousov if (object->type != OBJT_VNODE) { 164789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 164884110e7eSKonstantin Belousov return; 164984110e7eSKonstantin Belousov } 165084110e7eSKonstantin Belousov 165184110e7eSKonstantin Belousov /* 165284110e7eSKonstantin Belousov * Optimize for the case when writemappings is not going to 165384110e7eSKonstantin Belousov * zero. 165484110e7eSKonstantin Belousov */ 165584110e7eSKonstantin Belousov inc = end - start; 165684110e7eSKonstantin Belousov if (object->un_pager.vnp.writemappings != inc) { 165784110e7eSKonstantin Belousov object->un_pager.vnp.writemappings -= inc; 165889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 165984110e7eSKonstantin Belousov return; 166084110e7eSKonstantin Belousov } 166184110e7eSKonstantin Belousov 166284110e7eSKonstantin Belousov vp = object->handle; 166384110e7eSKonstantin Belousov vhold(vp); 166489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 166584110e7eSKonstantin Belousov mp = NULL; 166684110e7eSKonstantin Belousov vn_start_write(vp, &mp, V_WAIT); 166778022527SKonstantin Belousov vn_lock(vp, LK_SHARED | LK_RETRY); 166884110e7eSKonstantin Belousov 166984110e7eSKonstantin Belousov /* 167084110e7eSKonstantin Belousov * Decrement the object's writemappings, by swapping the start 167184110e7eSKonstantin Belousov * and end arguments for vnode_pager_update_writecount(). If 167284110e7eSKonstantin Belousov * there was not a race with vnode reclaimation, then the 167384110e7eSKonstantin Belousov * vnode's v_writecount is decremented. 167484110e7eSKonstantin Belousov */ 167584110e7eSKonstantin Belousov vnode_pager_update_writecount(object, end, start); 1676b249ce48SMateusz Guzik VOP_UNLOCK(vp); 167784110e7eSKonstantin Belousov vdrop(vp); 167884110e7eSKonstantin Belousov if (mp != NULL) 167984110e7eSKonstantin Belousov vn_finished_write(mp); 168084110e7eSKonstantin Belousov } 1681192112b7SKonstantin Belousov 1682192112b7SKonstantin Belousov static void 1683192112b7SKonstantin Belousov vnode_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp) 1684192112b7SKonstantin Belousov { 1685192112b7SKonstantin Belousov *vpp = object->handle; 1686192112b7SKonstantin Belousov } 1687