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> 7389f6b863SAttilio Rao #include <sys/rwlock.h> 749e0ddbd0SAlan Cox #include <sys/sf_buf.h> 75e5818a53SJeff Roberson #include <sys/domainset.h> 76df8bae1dSRodney W. Grimes 774f12e0acSSuleiman Souhlal #include <machine/atomic.h> 784f12e0acSSuleiman Souhlal 79df8bae1dSRodney W. Grimes #include <vm/vm.h> 801c771f92SKonstantin Belousov #include <vm/vm_param.h> 81efeaf95aSDavid Greenman #include <vm/vm_object.h> 82df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8324a1cce3SDavid Greenman #include <vm/vm_pager.h> 841efb74fbSJohn Dyson #include <vm/vm_map.h> 85df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h> 86efeaf95aSDavid Greenman #include <vm/vm_extern.h> 87756a5412SGleb Smirnoff #include <vm/uma.h> 88df8bae1dSRodney W. Grimes 89bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 90bff76343SAlan Cox daddr_t *rtaddress, int *run); 9111caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 9211caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 9311caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t); 94b0cd2017SGleb Smirnoff static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *); 95b0cd2017SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 96b0cd2017SGleb Smirnoff int *, vop_getpages_iodone_t, void *); 9733cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 9811caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 993364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 1003364c323SKonstantin Belousov vm_ooffset_t, struct ucred *cred); 10190effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *); 10290effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *); 1030b8253a7SBruce Evans 104df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 1054e658600SPoul-Henning Kamp .pgo_alloc = vnode_pager_alloc, 1064e658600SPoul-Henning Kamp .pgo_dealloc = vnode_pager_dealloc, 1074e658600SPoul-Henning Kamp .pgo_getpages = vnode_pager_getpages, 10890effb23SGleb Smirnoff .pgo_getpages_async = vnode_pager_getpages_async, 1094e658600SPoul-Henning Kamp .pgo_putpages = vnode_pager_putpages, 1104e658600SPoul-Henning Kamp .pgo_haspage = vnode_pager_haspage, 111df8bae1dSRodney W. Grimes }; 112df8bae1dSRodney W. Grimes 113e5818a53SJeff Roberson static struct domainset *vnode_domainset = NULL; 114e5818a53SJeff Roberson 115e5818a53SJeff Roberson SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset, CTLTYPE_STRING | CTLFLAG_RW, 116e5818a53SJeff Roberson &vnode_domainset, 0, sysctl_handle_domainset, "A", 117e5818a53SJeff Roberson "Default vnode NUMA policy"); 118e5818a53SJeff Roberson 11966fb0b1aSGleb Smirnoff static int nvnpbufs; 12066fb0b1aSGleb Smirnoff SYSCTL_INT(_vm, OID_AUTO, vnode_pbufs, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 12166fb0b1aSGleb Smirnoff &nvnpbufs, 0, "number of physical buffers allocated for vnode pager"); 12266fb0b1aSGleb Smirnoff 123756a5412SGleb Smirnoff static uma_zone_t vnode_pbuf_zone; 124756a5412SGleb Smirnoff 125756a5412SGleb Smirnoff static void 126756a5412SGleb Smirnoff vnode_pager_init(void *dummy) 127756a5412SGleb Smirnoff { 128756a5412SGleb Smirnoff 12966fb0b1aSGleb Smirnoff #ifdef __LP64__ 13066fb0b1aSGleb Smirnoff nvnpbufs = nswbuf * 2; 13166fb0b1aSGleb Smirnoff #else 13266fb0b1aSGleb Smirnoff nvnpbufs = nswbuf / 2; 13366fb0b1aSGleb Smirnoff #endif 13466fb0b1aSGleb Smirnoff TUNABLE_INT_FETCH("vm.vnode_pbufs", &nvnpbufs); 13566fb0b1aSGleb Smirnoff vnode_pbuf_zone = pbuf_zsecond_create("vnpbuf", nvnpbufs); 136756a5412SGleb Smirnoff } 137756a5412SGleb Smirnoff SYSINIT(vnode_pager, SI_SUB_CPU, SI_ORDER_ANY, vnode_pager_init, NULL); 138756a5412SGleb Smirnoff 139d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */ 140d07a6d3fSPoul-Henning Kamp int 141731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td) 142d07a6d3fSPoul-Henning Kamp { 143d07a6d3fSPoul-Henning Kamp vm_object_t object; 144d07a6d3fSPoul-Henning Kamp vm_ooffset_t size = isize; 145d07a6d3fSPoul-Henning Kamp struct vattr va; 146d07a6d3fSPoul-Henning Kamp 147d07a6d3fSPoul-Henning Kamp if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE) 148d07a6d3fSPoul-Henning Kamp return (0); 149d07a6d3fSPoul-Henning Kamp 150d07a6d3fSPoul-Henning Kamp while ((object = vp->v_object) != NULL) { 15189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 152d07a6d3fSPoul-Henning Kamp if (!(object->flags & OBJ_DEAD)) { 15389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 154d07a6d3fSPoul-Henning Kamp return (0); 155d07a6d3fSPoul-Henning Kamp } 15622db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 157d07a6d3fSPoul-Henning Kamp vm_object_set_flag(object, OBJ_DISCONNECTWNT); 1580dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0); 159cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 160d07a6d3fSPoul-Henning Kamp } 161d07a6d3fSPoul-Henning Kamp 162d07a6d3fSPoul-Henning Kamp if (size == 0) { 163d07a6d3fSPoul-Henning Kamp if (vn_isdisk(vp, NULL)) { 164d07a6d3fSPoul-Henning Kamp size = IDX_TO_OFF(INT_MAX); 165d07a6d3fSPoul-Henning Kamp } else { 1660359a12eSAttilio Rao if (VOP_GETATTR(vp, &va, td->td_ucred)) 167d07a6d3fSPoul-Henning Kamp return (0); 168d07a6d3fSPoul-Henning Kamp size = va.va_size; 169d07a6d3fSPoul-Henning Kamp } 170d07a6d3fSPoul-Henning Kamp } 171d07a6d3fSPoul-Henning Kamp 1723364c323SKonstantin Belousov object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred); 173d07a6d3fSPoul-Henning Kamp /* 174d07a6d3fSPoul-Henning Kamp * Dereference the reference we just created. This assumes 175d07a6d3fSPoul-Henning Kamp * that the object is associated with the vp. 176d07a6d3fSPoul-Henning Kamp */ 17789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 178d07a6d3fSPoul-Henning Kamp object->ref_count--; 17989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 180d07a6d3fSPoul-Henning Kamp vrele(vp); 181d07a6d3fSPoul-Henning Kamp 182d07a6d3fSPoul-Henning Kamp KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object")); 183d07a6d3fSPoul-Henning Kamp 184d07a6d3fSPoul-Henning Kamp return (0); 185d07a6d3fSPoul-Henning Kamp } 186d07a6d3fSPoul-Henning Kamp 1877146d6cbSPoul-Henning Kamp void 1887146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp) 1897146d6cbSPoul-Henning Kamp { 1907146d6cbSPoul-Henning Kamp struct vm_object *obj; 1917146d6cbSPoul-Henning Kamp 1927146d6cbSPoul-Henning Kamp obj = vp->v_object; 1937146d6cbSPoul-Henning Kamp if (obj == NULL) 1947146d6cbSPoul-Henning Kamp return; 19557fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject"); 19689f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 1972a339d9eSKonstantin Belousov umtx_shm_object_terminated(obj); 1987146d6cbSPoul-Henning Kamp if (obj->ref_count == 0) { 1997146d6cbSPoul-Henning Kamp /* 2007146d6cbSPoul-Henning Kamp * don't double-terminate the object 2017146d6cbSPoul-Henning Kamp */ 20290880a1bSKonstantin Belousov if ((obj->flags & OBJ_DEAD) == 0) { 2037146d6cbSPoul-Henning Kamp vm_object_terminate(obj); 20490880a1bSKonstantin Belousov } else { 20590880a1bSKonstantin Belousov /* 20690880a1bSKonstantin Belousov * Waiters were already handled during object 20790880a1bSKonstantin Belousov * termination. The exclusive vnode lock hopefully 20890880a1bSKonstantin Belousov * prevented new waiters from referencing the dying 20990880a1bSKonstantin Belousov * object. 21090880a1bSKonstantin Belousov */ 21190880a1bSKonstantin Belousov KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0, 21290880a1bSKonstantin Belousov ("OBJ_DISCONNECTWNT set obj %p flags %x", 21390880a1bSKonstantin Belousov obj, obj->flags)); 21490880a1bSKonstantin Belousov vp->v_object = NULL; 21589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 21690880a1bSKonstantin Belousov } 2177146d6cbSPoul-Henning Kamp } else { 2187146d6cbSPoul-Henning Kamp /* 2197146d6cbSPoul-Henning Kamp * Woe to the process that tries to page now :-). 2207146d6cbSPoul-Henning Kamp */ 2217146d6cbSPoul-Henning Kamp vm_pager_deallocate(obj); 22289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 2237146d6cbSPoul-Henning Kamp } 22490880a1bSKonstantin Belousov KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object)); 2257146d6cbSPoul-Henning Kamp } 2267146d6cbSPoul-Henning Kamp 2277146d6cbSPoul-Henning Kamp 228df8bae1dSRodney W. Grimes /* 229df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 230df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 231990ab7adSAlan Cox * 232990ab7adSAlan Cox * MPSAFE 233df8bae1dSRodney W. Grimes */ 23424a1cce3SDavid Greenman vm_object_t 2356cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 2363364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 237df8bae1dSRodney W. Grimes { 23806cb7259SDavid Greenman vm_object_t object; 239df8bae1dSRodney W. Grimes struct vnode *vp; 240df8bae1dSRodney W. Grimes 241df8bae1dSRodney W. Grimes /* 242df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 243df8bae1dSRodney W. Grimes */ 244df8bae1dSRodney W. Grimes if (handle == NULL) 245df8bae1dSRodney W. Grimes return (NULL); 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 24839d38f93SDavid Greenman 24939d38f93SDavid Greenman /* 25039d38f93SDavid Greenman * If the object is being terminated, wait for it to 25139d38f93SDavid Greenman * go away. 25239d38f93SDavid Greenman */ 2532ac78f0eSStephan Uphoff retry: 2541ca58953SAlan Cox while ((object = vp->v_object) != NULL) { 25589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2561ca58953SAlan Cox if ((object->flags & OBJ_DEAD) == 0) 2571ca58953SAlan Cox break; 25819187819SAlan Cox vm_object_set_flag(object, OBJ_DISCONNECTWNT); 2590dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0); 26024a1cce3SDavid Greenman } 2610d94caffSDavid Greenman 2626ded8427SKonstantin Belousov KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference")); 2632be70f79SJohn Dyson 26424a1cce3SDavid Greenman if (object == NULL) { 265df8bae1dSRodney W. Grimes /* 2662ac78f0eSStephan Uphoff * Add an object of the appropriate size 267df8bae1dSRodney W. Grimes */ 2686cde7a16SDavid Greenman object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 269bbc0ec52SDavid Greenman 2706cde7a16SDavid Greenman object->un_pager.vnp.vnp_size = size; 27184110e7eSKonstantin Belousov object->un_pager.vnp.writemappings = 0; 272e5818a53SJeff Roberson object->domain.dr_policy = vnode_domainset; 27326f9a767SRodney W. Grimes 27424a1cce3SDavid Greenman object->handle = handle; 27511be8415SStephan Uphoff VI_LOCK(vp); 2762ac78f0eSStephan Uphoff if (vp->v_object != NULL) { 2772ac78f0eSStephan Uphoff /* 2782ac78f0eSStephan Uphoff * Object has been created while we were sleeping 2792ac78f0eSStephan Uphoff */ 28011be8415SStephan Uphoff VI_UNLOCK(vp); 2819cddade7SKonstantin Belousov VM_OBJECT_WLOCK(object); 2829cddade7SKonstantin Belousov KASSERT(object->ref_count == 1, 2839cddade7SKonstantin Belousov ("leaked ref %p %d", object, object->ref_count)); 2849cddade7SKonstantin Belousov object->type = OBJT_DEAD; 2859cddade7SKonstantin Belousov object->ref_count = 0; 2869cddade7SKonstantin Belousov VM_OBJECT_WUNLOCK(object); 2872ac78f0eSStephan Uphoff vm_object_destroy(object); 2882ac78f0eSStephan Uphoff goto retry; 289df8bae1dSRodney W. Grimes } 2902ac78f0eSStephan Uphoff vp->v_object = object; 29111be8415SStephan Uphoff VI_UNLOCK(vp); 29211be8415SStephan Uphoff } else { 2932ac78f0eSStephan Uphoff object->ref_count++; 2943d653db0SAlan Cox #if VM_NRESERVLEVEL > 0 2953d653db0SAlan Cox vm_object_color(object, 0); 2963d653db0SAlan Cox #endif 29789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 29811be8415SStephan Uphoff } 2996ff51a36SMateusz Guzik vrefact(vp); 30024a1cce3SDavid Greenman return (object); 301df8bae1dSRodney W. Grimes } 302df8bae1dSRodney W. Grimes 303658ad5ffSAlan Cox /* 304658ad5ffSAlan Cox * The object must be locked. 305658ad5ffSAlan Cox */ 306f708ef1bSPoul-Henning Kamp static void 3077ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object) 30824a1cce3SDavid Greenman { 309b9f180d1SKonstantin Belousov struct vnode *vp; 310b9f180d1SKonstantin Belousov int refs; 311df8bae1dSRodney W. Grimes 312b9f180d1SKonstantin Belousov vp = object->handle; 31324a1cce3SDavid Greenman if (vp == NULL) 31424a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 31524a1cce3SDavid Greenman 31689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 31766095752SJohn Dyson vm_object_pip_wait(object, "vnpdea"); 318b9f180d1SKonstantin Belousov refs = object->ref_count; 31924a1cce3SDavid Greenman 32024a1cce3SDavid Greenman object->handle = NULL; 32195461b45SJohn Dyson object->type = OBJT_DEAD; 32219187819SAlan Cox if (object->flags & OBJ_DISCONNECTWNT) { 32319187819SAlan Cox vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 32419187819SAlan Cox wakeup(object); 32519187819SAlan Cox } 32657fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc"); 32784110e7eSKonstantin Belousov if (object->un_pager.vnp.writemappings > 0) { 32884110e7eSKonstantin Belousov object->un_pager.vnp.writemappings = 0; 32978022527SKonstantin Belousov VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 330b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 331b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 33284110e7eSKonstantin Belousov } 333aa2cabb9SDavid Greenman vp->v_object = NULL; 33478022527SKonstantin Belousov VI_LOCK(vp); 33578022527SKonstantin Belousov 33678022527SKonstantin Belousov /* 33778022527SKonstantin Belousov * vm_map_entry_set_vnode_text() cannot reach this vnode by 33878022527SKonstantin Belousov * following object->handle. Clear all text references now. 33978022527SKonstantin Belousov * This also clears the transient references from 34078022527SKonstantin Belousov * kern_execve(), which is fine because dead_vnodeops uses nop 34178022527SKonstantin Belousov * for VOP_UNSET_TEXT(). 34278022527SKonstantin Belousov */ 34378022527SKonstantin Belousov if (vp->v_writecount < 0) 34478022527SKonstantin Belousov vp->v_writecount = 0; 34578022527SKonstantin Belousov VI_UNLOCK(vp); 34689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 347b9f180d1SKonstantin Belousov while (refs-- > 0) 348b9f180d1SKonstantin Belousov vunref(vp); 34989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 350df8bae1dSRodney W. Grimes } 35126f9a767SRodney W. Grimes 352f708ef1bSPoul-Henning Kamp static boolean_t 3537ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 3547ebba1f8SGleb Smirnoff int *after) 355df8bae1dSRodney W. Grimes { 35624a1cce3SDavid Greenman struct vnode *vp = object->handle; 35798b0c789SPoul-Henning Kamp daddr_t bn; 358*4153054aSJeff Roberson uintptr_t lockstate; 3593af76890SPoul-Henning Kamp int err; 360170db9c6SJohn Dyson daddr_t reqblock; 3612c4488fcSJohn Dyson int poff; 3622c4488fcSJohn Dyson int bsize; 363d63596ceSJohn Dyson int pagesperblock, blocksperpage; 364df8bae1dSRodney W. Grimes 365*4153054aSJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 36624579ca1SMatthew Dillon /* 36724579ca1SMatthew Dillon * If no vp or vp is doomed or marked transparent to VM, we do not 36824579ca1SMatthew Dillon * have the page. 36924579ca1SMatthew Dillon */ 370b73f64c4SJeff Roberson if (vp == NULL || vp->v_iflag & VI_DOOMED) 37147221757SJohn Dyson return FALSE; 372df8bae1dSRodney W. Grimes /* 373b73f64c4SJeff Roberson * If the offset is beyond end of file we do 3740d94caffSDavid Greenman * not have the page. 375df8bae1dSRodney W. Grimes */ 376b73f64c4SJeff Roberson if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 3774abc71c0SDavid Greenman return FALSE; 378df8bae1dSRodney W. Grimes 379eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 380170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 381d63596ceSJohn Dyson blocksperpage = 0; 382d63596ceSJohn Dyson if (pagesperblock > 0) { 383a316d390SJohn Dyson reqblock = pindex / pagesperblock; 384d63596ceSJohn Dyson } else { 385d63596ceSJohn Dyson blocksperpage = (PAGE_SIZE / bsize); 386d63596ceSJohn Dyson reqblock = pindex * blocksperpage; 387d63596ceSJohn Dyson } 388*4153054aSJeff Roberson lockstate = VM_OBJECT_DROP(object); 389ec794849SPoul-Henning Kamp err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 390*4153054aSJeff Roberson VM_OBJECT_PICKUP(object, lockstate); 3910d94caffSDavid Greenman if (err) 39224a1cce3SDavid Greenman return TRUE; 3936eab77f2SJohn Dyson if (bn == -1) 394ced399eeSJohn Dyson return FALSE; 395d63596ceSJohn Dyson if (pagesperblock > 0) { 396a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 397170db9c6SJohn Dyson if (before) { 398170db9c6SJohn Dyson *before *= pagesperblock; 399170db9c6SJohn Dyson *before += poff; 400170db9c6SJohn Dyson } 401170db9c6SJohn Dyson if (after) { 40284d31376SGleb Smirnoff /* 40384d31376SGleb Smirnoff * The BMAP vop can report a partial block in the 404d2596d17SGleb Smirnoff * 'after', but must not report blocks after EOF. 40584d31376SGleb Smirnoff * Assert the latter, and truncate 'after' in case 40684d31376SGleb Smirnoff * of the former. 40784d31376SGleb Smirnoff */ 408d2596d17SGleb Smirnoff KASSERT((reqblock + *after) * pagesperblock < 409d2596d17SGleb Smirnoff roundup2(object->size, pagesperblock), 41084d31376SGleb Smirnoff ("%s: reqblock %jd after %d size %ju", __func__, 41184d31376SGleb Smirnoff (intmax_t )reqblock, *after, 41284d31376SGleb Smirnoff (uintmax_t )object->size)); 413170db9c6SJohn Dyson *after *= pagesperblock; 41484d31376SGleb Smirnoff *after += pagesperblock - (poff + 1); 41584d31376SGleb Smirnoff if (pindex + *after >= object->size) 41684d31376SGleb Smirnoff *after = object->size - 1 - pindex; 417170db9c6SJohn Dyson } 418d63596ceSJohn Dyson } else { 419d63596ceSJohn Dyson if (before) { 420d63596ceSJohn Dyson *before /= blocksperpage; 421d63596ceSJohn Dyson } 422d63596ceSJohn Dyson 423d63596ceSJohn Dyson if (after) { 424d63596ceSJohn Dyson *after /= blocksperpage; 425d63596ceSJohn Dyson } 426d63596ceSJohn Dyson } 427ced399eeSJohn Dyson return TRUE; 428df8bae1dSRodney W. Grimes } 429df8bae1dSRodney W. Grimes 430df8bae1dSRodney W. Grimes /* 431df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 43224a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 433df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 434df8bae1dSRodney W. Grimes * 435df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 436df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 437df8bae1dSRodney W. Grimes */ 438df8bae1dSRodney W. Grimes void 4397ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize) 440df8bae1dSRodney W. Grimes { 4412a8f9ab5SAlan Cox vm_object_t object; 4422a8f9ab5SAlan Cox vm_page_t m; 443c576d121SLuoqi Chen vm_pindex_t nobjsize; 444df8bae1dSRodney W. Grimes 4452a8f9ab5SAlan Cox if ((object = vp->v_object) == NULL) 446df8bae1dSRodney W. Grimes return; 447cb61d698SKonstantin Belousov /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */ 44889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 4499b8851faSKonstantin Belousov if (object->type == OBJT_DEAD) { 4509b8851faSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 4519b8851faSKonstantin Belousov return; 4529b8851faSKonstantin Belousov } 4539b8851faSKonstantin Belousov KASSERT(object->type == OBJT_VNODE, 4549b8851faSKonstantin Belousov ("not vnode-backed object %p", object)); 4552a8f9ab5SAlan Cox if (nsize == object->un_pager.vnp.vnp_size) { 456df8bae1dSRodney W. Grimes /* 457df8bae1dSRodney W. Grimes * Hasn't changed size 458df8bae1dSRodney W. Grimes */ 45989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 460df8bae1dSRodney W. Grimes return; 4612a8f9ab5SAlan Cox } 462c576d121SLuoqi Chen nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 4632a8f9ab5SAlan Cox if (nsize < object->un_pager.vnp.vnp_size) { 464df8bae1dSRodney W. Grimes /* 465bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 466df8bae1dSRodney W. Grimes */ 4672a8f9ab5SAlan Cox if (nobjsize < object->size) 468c576d121SLuoqi Chen vm_object_page_remove(object, nobjsize, object->size, 4696bbee8e2SAlan Cox 0); 470bbc0ec52SDavid Greenman /* 471bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 4723ebeaf59SMatthew Dillon * only partially backed by the vnode. 4733ebeaf59SMatthew Dillon * 4743ebeaf59SMatthew Dillon * XXX for some reason (I don't know yet), if we take a 4753ebeaf59SMatthew Dillon * completely invalid page and mark it partially valid 4763ebeaf59SMatthew Dillon * it can screw up NFS reads, so we don't allow the case. 477bbc0ec52SDavid Greenman */ 4782a8f9ab5SAlan Cox if ((nsize & PAGE_MASK) && 4791b26eb10SAlan Cox (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 4801b26eb10SAlan Cox m->valid != 0) { 4812b6b0df7SMatthew Dillon int base = (int)nsize & PAGE_MASK; 4822b6b0df7SMatthew Dillon int size = PAGE_SIZE - base; 4832b6b0df7SMatthew Dillon 4842b6b0df7SMatthew Dillon /* 4852b6b0df7SMatthew Dillon * Clear out partial-page garbage in case 4862b6b0df7SMatthew Dillon * the page has been mapped. 4872b6b0df7SMatthew Dillon */ 488fff6062aSAlan Cox pmap_zero_page_area(m, base, size); 4892b6b0df7SMatthew Dillon 4902b6b0df7SMatthew Dillon /* 4913c33df62SAlan Cox * Update the valid bits to reflect the blocks that 4923c33df62SAlan Cox * have been zeroed. Some of these valid bits may 4933c33df62SAlan Cox * have already been set. 4943c33df62SAlan Cox */ 495dc874f98SKonstantin Belousov vm_page_set_valid_range(m, base, size); 4963c33df62SAlan Cox 4973c33df62SAlan Cox /* 4983c33df62SAlan Cox * Round "base" to the next block boundary so that the 4993c33df62SAlan Cox * dirty bit for a partially zeroed block is not 5003c33df62SAlan Cox * cleared. 5013c33df62SAlan Cox */ 5023c33df62SAlan Cox base = roundup2(base, DEV_BSIZE); 5033c33df62SAlan Cox 5043c33df62SAlan Cox /* 5053c33df62SAlan Cox * Clear out partial-page dirty bits. 5063ebeaf59SMatthew Dillon * 5073ebeaf59SMatthew Dillon * note that we do not clear out the valid 5083ebeaf59SMatthew Dillon * bits. This would prevent bogus_page 5093ebeaf59SMatthew Dillon * replacement from working properly. 5102b6b0df7SMatthew Dillon */ 5113c33df62SAlan Cox vm_page_clear_dirty(m, base, PAGE_SIZE - base); 512bbc0ec52SDavid Greenman } 513bbc0ec52SDavid Greenman } 514a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 515c576d121SLuoqi Chen object->size = nobjsize; 51689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 517df8bae1dSRodney W. Grimes } 518df8bae1dSRodney W. Grimes 51926f9a767SRodney W. Grimes /* 52026f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 52126f9a767SRodney W. Grimes * file address 52226f9a767SRodney W. Grimes */ 523bff76343SAlan Cox static int 524bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 525bff76343SAlan Cox int *run) 52626f9a767SRodney W. Grimes { 52726f9a767SRodney W. Grimes int bsize; 52826f9a767SRodney W. Grimes int err; 529a316d390SJohn Dyson daddr_t vblock; 530f3aad9a6SBjoern A. Zeeb daddr_t voffset; 53126f9a767SRodney W. Grimes 5322ad036b6SAlan Cox if (address < 0) 5330d94caffSDavid Greenman return -1; 5340d94caffSDavid Greenman 535b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 5362c4488fcSJohn Dyson return -1; 5372c4488fcSJohn Dyson 53826f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 53926f9a767SRodney W. Grimes vblock = address / bsize; 54026f9a767SRodney W. Grimes voffset = address % bsize; 54126f9a767SRodney W. Grimes 542bff76343SAlan Cox err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 543bff76343SAlan Cox if (err == 0) { 544bff76343SAlan Cox if (*rtaddress != -1) 545bff76343SAlan Cox *rtaddress += voffset / DEV_BSIZE; 546efc68ce1SDavid Greenman if (run) { 547efc68ce1SDavid Greenman *run += 1; 548efc68ce1SDavid Greenman *run *= bsize / PAGE_SIZE; 549efc68ce1SDavid Greenman *run -= voffset / PAGE_SIZE; 550efc68ce1SDavid Greenman } 551efc68ce1SDavid Greenman } 55226f9a767SRodney W. Grimes 553bff76343SAlan Cox return (err); 55426f9a767SRodney W. Grimes } 55526f9a767SRodney W. Grimes 55626f9a767SRodney W. Grimes /* 55726f9a767SRodney W. Grimes * small block filesystem vnode pager input 55826f9a767SRodney W. Grimes */ 559f708ef1bSPoul-Henning Kamp static int 5607ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m) 56126f9a767SRodney W. Grimes { 5629c83534dSPoul-Henning Kamp struct vnode *vp; 5639c83534dSPoul-Henning Kamp struct bufobj *bo; 56426f9a767SRodney W. Grimes struct buf *bp; 5659e0ddbd0SAlan Cox struct sf_buf *sf; 566f3aad9a6SBjoern A. Zeeb daddr_t fileaddr; 56726f9a767SRodney W. Grimes vm_offset_t bsize; 568561cc9fcSKonstantin Belousov vm_page_bits_t bits; 569561cc9fcSKonstantin Belousov int error, i; 57026f9a767SRodney W. Grimes 571561cc9fcSKonstantin Belousov error = 0; 57224a1cce3SDavid Greenman vp = object->handle; 573b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 5742c4488fcSJohn Dyson return VM_PAGER_BAD; 5752c4488fcSJohn Dyson 57626f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 5770bdb7528SDavid Greenman 5789c83534dSPoul-Henning Kamp VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 57926f9a767SRodney W. Grimes 5809e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 58126f9a767SRodney W. Grimes 58226f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 58333c67741SMatthew Dillon vm_ooffset_t address; 584bbc0ec52SDavid Greenman 5850d53a17bSAlan Cox bits = vm_page_bits(i * bsize, bsize); 5860d53a17bSAlan Cox if (m->valid & bits) 58726f9a767SRodney W. Grimes continue; 58826f9a767SRodney W. Grimes 58933c67741SMatthew Dillon address = IDX_TO_OFF(m->pindex) + i * bsize; 59033c67741SMatthew Dillon if (address >= object->un_pager.vnp.vnp_size) { 59133c67741SMatthew Dillon fileaddr = -1; 59233c67741SMatthew Dillon } else { 593bff76343SAlan Cox error = vnode_pager_addr(vp, address, &fileaddr, NULL); 594bff76343SAlan Cox if (error) 595bff76343SAlan Cox break; 59633c67741SMatthew Dillon } 59726f9a767SRodney W. Grimes if (fileaddr != -1) { 598756a5412SGleb Smirnoff bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK); 59926f9a767SRodney W. Grimes 60026f9a767SRodney W. Grimes /* build a minimal buffer header */ 60121144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 6026a4b5823SPoul-Henning Kamp bp->b_iodone = bdone; 603bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 604bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 605a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 606a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 6079e0ddbd0SAlan Cox bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 608187f0071SDavid Greenman bp->b_blkno = fileaddr; 6099c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 6101faacf5dSKirk McKusick bp->b_vp = vp; 61126f9a767SRodney W. Grimes bp->b_bcount = bsize; 61226f9a767SRodney W. Grimes bp->b_bufsize = bsize; 6132b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 6145bd65606SJohn Baldwin atomic_add_long(&runningbufspace, bp->b_runningbufspace); 61526f9a767SRodney W. Grimes 61626f9a767SRodney W. Grimes /* do the input */ 6172c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 618b792bebeSPoul-Henning Kamp bstrategy(bp); 61926f9a767SRodney W. Grimes 6206a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnsrd"); 6216a4b5823SPoul-Henning Kamp 622c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 62326f9a767SRodney W. Grimes error = EIO; 62426f9a767SRodney W. Grimes 62526f9a767SRodney W. Grimes /* 62626f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 62726f9a767SRodney W. Grimes */ 6281faacf5dSKirk McKusick bp->b_vp = NULL; 6299c83534dSPoul-Henning Kamp pbrelbo(bp); 630756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 63126f9a767SRodney W. Grimes if (error) 63226f9a767SRodney W. Grimes break; 6330d53a17bSAlan Cox } else 6349e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 6350d53a17bSAlan Cox KASSERT((m->dirty & bits) == 0, 6360d53a17bSAlan Cox ("vnode_pager_input_smlfs: page %p is dirty", m)); 63789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 6380d53a17bSAlan Cox m->valid |= bits; 63989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 64026f9a767SRodney W. Grimes } 6419e0ddbd0SAlan Cox sf_buf_free(sf); 64226f9a767SRodney W. Grimes if (error) { 643a83c285cSDavid Greenman return VM_PAGER_ERROR; 64426f9a767SRodney W. Grimes } 64526f9a767SRodney W. Grimes return VM_PAGER_OK; 64626f9a767SRodney W. Grimes } 64726f9a767SRodney W. Grimes 64826f9a767SRodney W. Grimes /* 649475e8cc3SPoul-Henning Kamp * old style vnode pager input routine 65026f9a767SRodney W. Grimes */ 651f708ef1bSPoul-Henning Kamp static int 6527ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m) 65326f9a767SRodney W. Grimes { 654df8bae1dSRodney W. Grimes struct uio auio; 655df8bae1dSRodney W. Grimes struct iovec aiov; 65626f9a767SRodney W. Grimes int error; 65726f9a767SRodney W. Grimes int size; 6589e0ddbd0SAlan Cox struct sf_buf *sf; 659342a1480SJohn Baldwin struct vnode *vp; 660df8bae1dSRodney W. Grimes 66189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 66226f9a767SRodney W. Grimes error = 0; 663bbc0ec52SDavid Greenman 664df8bae1dSRodney W. Grimes /* 66526f9a767SRodney W. Grimes * Return failure if beyond current EOF 66626f9a767SRodney W. Grimes */ 667a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 66826f9a767SRodney W. Grimes return VM_PAGER_BAD; 66926f9a767SRodney W. Grimes } else { 67026f9a767SRodney W. Grimes size = PAGE_SIZE; 671a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 672a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 67352051abcSAlan Cox vp = object->handle; 67489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 6750bdb7528SDavid Greenman 67626f9a767SRodney W. Grimes /* 677df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 678df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 679df8bae1dSRodney W. Grimes */ 6809e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 6810bdb7528SDavid Greenman 6829e0ddbd0SAlan Cox aiov.iov_base = (caddr_t)sf_buf_kva(sf); 683df8bae1dSRodney W. Grimes aiov.iov_len = size; 684df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 685df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 686a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 687df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 68826f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 689df8bae1dSRodney W. Grimes auio.uio_resid = size; 690b40ce416SJulian Elischer auio.uio_td = curthread; 69126f9a767SRodney W. Grimes 692a854ed98SJohn Baldwin error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 693df8bae1dSRodney W. Grimes if (!error) { 69454d92145SMatthew Dillon int count = size - auio.uio_resid; 695df8bae1dSRodney W. Grimes 696df8bae1dSRodney W. Grimes if (count == 0) 697df8bae1dSRodney W. Grimes error = EINVAL; 69826f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 6999e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + count, 7009e0ddbd0SAlan Cox PAGE_SIZE - count); 701df8bae1dSRodney W. Grimes } 7029e0ddbd0SAlan Cox sf_buf_free(sf); 7031b26eb10SAlan Cox 70489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 705df8bae1dSRodney W. Grimes } 7060d53a17bSAlan Cox KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m)); 7076e3a3f38SRobert V. Baron if (!error) 7086e3a3f38SRobert V. Baron m->valid = VM_PAGE_BITS_ALL; 709a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 71026f9a767SRodney W. Grimes } 71126f9a767SRodney W. Grimes 71226f9a767SRodney W. Grimes /* 71326f9a767SRodney W. Grimes * generic vnode pager input routine 71426f9a767SRodney W. Grimes */ 715170db9c6SJohn Dyson 716ce75f2c3SMike Smith /* 71723955314SAlfred Perlstein * Local media VFS's that do not implement their own VOP_GETPAGES 71847e151ddSRobert Drehmel * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 71947e151ddSRobert Drehmel * to implement the previous behaviour. 720ce75f2c3SMike Smith * 721ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 722ce75f2c3SMike Smith * backing vp's VOP_GETPAGES. 723ce75f2c3SMike Smith */ 724f708ef1bSPoul-Henning Kamp static int 725b0cd2017SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind, 726b0cd2017SGleb Smirnoff int *rahead) 72724a1cce3SDavid Greenman { 728170db9c6SJohn Dyson struct vnode *vp; 729b0cd2017SGleb Smirnoff int rtval; 73095e5e988SJohn Dyson 731170db9c6SJohn Dyson vp = object->handle; 73289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 733b0cd2017SGleb Smirnoff rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead); 73423955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 73523955314SAlfred Perlstein ("vnode_pager: FS getpages not implemented\n")); 73689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 737170db9c6SJohn Dyson return rtval; 738170db9c6SJohn Dyson } 739170db9c6SJohn Dyson 74090effb23SGleb Smirnoff static int 74190effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count, 742b0cd2017SGleb Smirnoff int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg) 74390effb23SGleb Smirnoff { 74490effb23SGleb Smirnoff struct vnode *vp; 74590effb23SGleb Smirnoff int rtval; 74690effb23SGleb Smirnoff 74790effb23SGleb Smirnoff vp = object->handle; 74890effb23SGleb Smirnoff VM_OBJECT_WUNLOCK(object); 749b0cd2017SGleb Smirnoff rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg); 75090effb23SGleb Smirnoff KASSERT(rtval != EOPNOTSUPP, 75190effb23SGleb Smirnoff ("vnode_pager: FS getpages_async not implemented\n")); 75290effb23SGleb Smirnoff VM_OBJECT_WLOCK(object); 75390effb23SGleb Smirnoff return (rtval); 75490effb23SGleb Smirnoff } 75590effb23SGleb Smirnoff 756ce75f2c3SMike Smith /* 75790effb23SGleb Smirnoff * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for 75890effb23SGleb Smirnoff * local filesystems, where partially valid pages can only occur at 75990effb23SGleb Smirnoff * the end of file. 760d15b55c5SKonstantin Belousov */ 761d15b55c5SKonstantin Belousov int 762d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap) 763d15b55c5SKonstantin Belousov { 76490effb23SGleb Smirnoff 765b0cd2017SGleb Smirnoff return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 766b0cd2017SGleb Smirnoff ap->a_rbehind, ap->a_rahead, NULL, NULL)); 76790effb23SGleb Smirnoff } 76890effb23SGleb Smirnoff 76990effb23SGleb Smirnoff int 77090effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap) 77190effb23SGleb Smirnoff { 77290effb23SGleb Smirnoff 773b0cd2017SGleb Smirnoff return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 774b0cd2017SGleb Smirnoff ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg)); 775d15b55c5SKonstantin Belousov } 776d15b55c5SKonstantin Belousov 777d15b55c5SKonstantin Belousov /* 778ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 779ce75f2c3SMike Smith * own vnodes if they fail to implement VOP_GETPAGES. 780ce75f2c3SMike Smith */ 781ce75f2c3SMike Smith int 782b0cd2017SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count, 783b0cd2017SGleb Smirnoff int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg) 784170db9c6SJohn Dyson { 785ce75f2c3SMike Smith vm_object_t object; 7869c83534dSPoul-Henning Kamp struct bufobj *bo; 7870bdb7528SDavid Greenman struct buf *bp; 788b0cd2017SGleb Smirnoff off_t foff; 789e48b82bdSGleb Smirnoff #ifdef INVARIANTS 790e48b82bdSGleb Smirnoff off_t blkno0; 791e48b82bdSGleb Smirnoff #endif 792756a5412SGleb Smirnoff int bsize, pagesperblock; 793b0cd2017SGleb Smirnoff int error, before, after, rbehind, rahead, poff, i; 794b0cd2017SGleb Smirnoff int bytecount, secmask; 795ce75f2c3SMike Smith 7969c83534dSPoul-Henning Kamp KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 797b0cd2017SGleb Smirnoff ("%s does not support devices", __func__)); 798b0cd2017SGleb Smirnoff 799b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 800eac91e32SKonstantin Belousov return (VM_PAGER_BAD); 8012c4488fcSJohn Dyson 802eac91e32SKonstantin Belousov object = vp->v_object; 803b0cd2017SGleb Smirnoff foff = IDX_TO_OFF(m[0]->pindex); 80426f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 805b0cd2017SGleb Smirnoff pagesperblock = bsize / PAGE_SIZE; 806b0cd2017SGleb Smirnoff 807b0cd2017SGleb Smirnoff KASSERT(foff < object->un_pager.vnp.vnp_size, 808b0cd2017SGleb Smirnoff ("%s: page %p offset beyond vp %p size", __func__, m[0], vp)); 80940a51684SJason A. Harmening KASSERT(count <= nitems(bp->b_pages), 810b0cd2017SGleb Smirnoff ("%s: requested %d pages", __func__, count)); 811b0cd2017SGleb Smirnoff 812b0cd2017SGleb Smirnoff /* 813b0cd2017SGleb Smirnoff * The last page has valid blocks. Invalid part can only 814b0cd2017SGleb Smirnoff * exist at the end of file, and the page is made fully valid 815b0cd2017SGleb Smirnoff * by zeroing in vm_pager_get_pages(). 816b0cd2017SGleb Smirnoff */ 817b0cd2017SGleb Smirnoff if (m[count - 1]->valid != 0 && --count == 0) { 818b0cd2017SGleb Smirnoff if (iodone != NULL) 819b0cd2017SGleb Smirnoff iodone(arg, m, 1, 0); 820b0cd2017SGleb Smirnoff return (VM_PAGER_OK); 821b0cd2017SGleb Smirnoff } 822bbc0ec52SDavid Greenman 823756a5412SGleb Smirnoff bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK); 82473e9030eSGleb Smirnoff 82526f9a767SRodney W. Grimes /* 826e122dfc1SGleb Smirnoff * Get the underlying device blocks for the file with VOP_BMAP(). 827e122dfc1SGleb Smirnoff * If the file system doesn't support VOP_BMAP, use old way of 828e122dfc1SGleb Smirnoff * getting pages via VOP_READ. 82926f9a767SRodney W. Grimes */ 830b0cd2017SGleb Smirnoff error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before); 8311de11f1aSAlan Cox if (error == EOPNOTSUPP) { 832756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 83389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 834b0cd2017SGleb Smirnoff for (i = 0; i < count; i++) { 83583c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 83683c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodepgsin); 837b0cd2017SGleb Smirnoff error = vnode_pager_input_old(object, m[i]); 838b0cd2017SGleb Smirnoff if (error) 839b0cd2017SGleb Smirnoff break; 840b0cd2017SGleb Smirnoff } 84189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 84252051abcSAlan Cox return (error); 8431de11f1aSAlan Cox } else if (error != 0) { 844756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 8451de11f1aSAlan Cox return (VM_PAGER_ERROR); 846b0cd2017SGleb Smirnoff } 847bbc0ec52SDavid Greenman 84826f9a767SRodney W. Grimes /* 849b0cd2017SGleb Smirnoff * If the file system supports BMAP, but blocksize is smaller 850b0cd2017SGleb Smirnoff * than a page size, then use special small filesystem code. 85126f9a767SRodney W. Grimes */ 852b0cd2017SGleb Smirnoff if (pagesperblock == 0) { 853756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 854b0cd2017SGleb Smirnoff for (i = 0; i < count; i++) { 85583c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 85683c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodepgsin); 857b0cd2017SGleb Smirnoff error = vnode_pager_input_smlfs(object, m[i]); 858b0cd2017SGleb Smirnoff if (error) 859b0cd2017SGleb Smirnoff break; 860b0cd2017SGleb Smirnoff } 861b0cd2017SGleb Smirnoff return (error); 86226f9a767SRodney W. Grimes } 8638d17e694SJulian Elischer 86426f9a767SRodney W. Grimes /* 865b0cd2017SGleb Smirnoff * A sparse file can be encountered only for a single page request, 866763df3ecSPedro F. Giffuni * which may not be preceded by call to vm_pager_haspage(). 867a7fecb4dSAlan Cox */ 868b0cd2017SGleb Smirnoff if (bp->b_blkno == -1) { 869b0cd2017SGleb Smirnoff KASSERT(count == 1, 870b0cd2017SGleb Smirnoff ("%s: array[%d] request to a sparse file %p", __func__, 871b0cd2017SGleb Smirnoff count, vp)); 872756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 873b0cd2017SGleb Smirnoff pmap_zero_page(m[0]); 874b0cd2017SGleb Smirnoff KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty", 875b0cd2017SGleb Smirnoff __func__, m[0])); 876a7fecb4dSAlan Cox VM_OBJECT_WLOCK(object); 877b0cd2017SGleb Smirnoff m[0]->valid = VM_PAGE_BITS_ALL; 87889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 879f4f83da0SAlan Cox return (VM_PAGER_OK); 880b0cd2017SGleb Smirnoff } 881b0cd2017SGleb Smirnoff 882e48b82bdSGleb Smirnoff #ifdef INVARIANTS 883e48b82bdSGleb Smirnoff blkno0 = bp->b_blkno; 884e48b82bdSGleb Smirnoff #endif 885b0cd2017SGleb Smirnoff bp->b_blkno += (foff % bsize) / DEV_BSIZE; 886b0cd2017SGleb Smirnoff 887b0cd2017SGleb Smirnoff /* Recalculate blocks available after/before to pages. */ 888b0cd2017SGleb Smirnoff poff = (foff % bsize) / PAGE_SIZE; 889b0cd2017SGleb Smirnoff before *= pagesperblock; 890b0cd2017SGleb Smirnoff before += poff; 891b0cd2017SGleb Smirnoff after *= pagesperblock; 892b0cd2017SGleb Smirnoff after += pagesperblock - (poff + 1); 893b0cd2017SGleb Smirnoff if (m[0]->pindex + after >= object->size) 894b0cd2017SGleb Smirnoff after = object->size - 1 - m[0]->pindex; 895b0cd2017SGleb Smirnoff KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d", 896b0cd2017SGleb Smirnoff __func__, count, after + 1)); 897b0cd2017SGleb Smirnoff after -= count - 1; 898b0cd2017SGleb Smirnoff 899b0cd2017SGleb Smirnoff /* Trim requested rbehind/rahead to possible values. */ 900b0cd2017SGleb Smirnoff rbehind = a_rbehind ? *a_rbehind : 0; 901b0cd2017SGleb Smirnoff rahead = a_rahead ? *a_rahead : 0; 902b0cd2017SGleb Smirnoff rbehind = min(rbehind, before); 903b0cd2017SGleb Smirnoff rbehind = min(rbehind, m[0]->pindex); 904b0cd2017SGleb Smirnoff rahead = min(rahead, after); 905b0cd2017SGleb Smirnoff rahead = min(rahead, object->size - m[count - 1]->pindex); 906e48b82bdSGleb Smirnoff /* 907e48b82bdSGleb Smirnoff * Check that total amount of pages fit into buf. Trim rbehind and 908e48b82bdSGleb Smirnoff * rahead evenly if not. 909e48b82bdSGleb Smirnoff */ 910e48b82bdSGleb Smirnoff if (rbehind + rahead + count > nitems(bp->b_pages)) { 911e48b82bdSGleb Smirnoff int trim, sum; 912e48b82bdSGleb Smirnoff 913e48b82bdSGleb Smirnoff trim = rbehind + rahead + count - nitems(bp->b_pages) + 1; 914e48b82bdSGleb Smirnoff sum = rbehind + rahead; 915e48b82bdSGleb Smirnoff if (rbehind == before) { 916e48b82bdSGleb Smirnoff /* Roundup rbehind trim to block size. */ 917e48b82bdSGleb Smirnoff rbehind -= roundup(trim * rbehind / sum, pagesperblock); 918e48b82bdSGleb Smirnoff if (rbehind < 0) 919e48b82bdSGleb Smirnoff rbehind = 0; 920e48b82bdSGleb Smirnoff } else 921e48b82bdSGleb Smirnoff rbehind -= trim * rbehind / sum; 922e48b82bdSGleb Smirnoff rahead -= trim * rahead / sum; 923e48b82bdSGleb Smirnoff } 924e48b82bdSGleb Smirnoff KASSERT(rbehind + rahead + count <= nitems(bp->b_pages), 925b0cd2017SGleb Smirnoff ("%s: behind %d ahead %d count %d", __func__, 926b0cd2017SGleb Smirnoff rbehind, rahead, count)); 927b0cd2017SGleb Smirnoff 928b0cd2017SGleb Smirnoff /* 929b0cd2017SGleb Smirnoff * Fill in the bp->b_pages[] array with requested and optional 930b0cd2017SGleb Smirnoff * read behind or read ahead pages. Read behind pages are looked 931b0cd2017SGleb Smirnoff * up in a backward direction, down to a first cached page. Same 932b0cd2017SGleb Smirnoff * for read ahead pages, but there is no need to shift the array 933b0cd2017SGleb Smirnoff * in case of encountering a cached page. 934b0cd2017SGleb Smirnoff */ 935b0cd2017SGleb Smirnoff i = bp->b_npages = 0; 936b0cd2017SGleb Smirnoff if (rbehind) { 937b0cd2017SGleb Smirnoff vm_pindex_t startpindex, tpindex; 938b0cd2017SGleb Smirnoff vm_page_t p; 939b0cd2017SGleb Smirnoff 940a7fecb4dSAlan Cox VM_OBJECT_WLOCK(object); 941b0cd2017SGleb Smirnoff startpindex = m[0]->pindex - rbehind; 942b0cd2017SGleb Smirnoff if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL && 943b0cd2017SGleb Smirnoff p->pindex >= startpindex) 944b0cd2017SGleb Smirnoff startpindex = p->pindex + 1; 945b0cd2017SGleb Smirnoff 946b0cd2017SGleb Smirnoff /* tpindex is unsigned; beware of numeric underflow. */ 947b0cd2017SGleb Smirnoff for (tpindex = m[0]->pindex - 1; 948b0cd2017SGleb Smirnoff tpindex >= startpindex && tpindex < m[0]->pindex; 949b0cd2017SGleb Smirnoff tpindex--, i++) { 9507667839aSAlan Cox p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 951b0cd2017SGleb Smirnoff if (p == NULL) { 952b0cd2017SGleb Smirnoff /* Shift the array. */ 953b0cd2017SGleb Smirnoff for (int j = 0; j < i; j++) 954b0cd2017SGleb Smirnoff bp->b_pages[j] = bp->b_pages[j + 955b0cd2017SGleb Smirnoff tpindex + 1 - startpindex]; 956b0cd2017SGleb Smirnoff break; 957b0cd2017SGleb Smirnoff } 958b0cd2017SGleb Smirnoff bp->b_pages[tpindex - startpindex] = p; 959a7fecb4dSAlan Cox } 9600bdb7528SDavid Greenman 961b0cd2017SGleb Smirnoff bp->b_pgbefore = i; 962b0cd2017SGleb Smirnoff bp->b_npages += i; 963b0cd2017SGleb Smirnoff bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE; 964b0cd2017SGleb Smirnoff } else 965b0cd2017SGleb Smirnoff bp->b_pgbefore = 0; 966b0cd2017SGleb Smirnoff 967b0cd2017SGleb Smirnoff /* Requested pages. */ 968b0cd2017SGleb Smirnoff for (int j = 0; j < count; j++, i++) 969b0cd2017SGleb Smirnoff bp->b_pages[i] = m[j]; 970b0cd2017SGleb Smirnoff bp->b_npages += count; 971b0cd2017SGleb Smirnoff 972b0cd2017SGleb Smirnoff if (rahead) { 973b0cd2017SGleb Smirnoff vm_pindex_t endpindex, tpindex; 974b0cd2017SGleb Smirnoff vm_page_t p; 975b0cd2017SGleb Smirnoff 976b0cd2017SGleb Smirnoff if (!VM_OBJECT_WOWNED(object)) 977eac91e32SKonstantin Belousov VM_OBJECT_WLOCK(object); 978b0cd2017SGleb Smirnoff endpindex = m[count - 1]->pindex + rahead + 1; 979b0cd2017SGleb Smirnoff if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL && 980b0cd2017SGleb Smirnoff p->pindex < endpindex) 981b0cd2017SGleb Smirnoff endpindex = p->pindex; 982b0cd2017SGleb Smirnoff if (endpindex > object->size) 983b0cd2017SGleb Smirnoff endpindex = object->size; 984b0cd2017SGleb Smirnoff 985b0cd2017SGleb Smirnoff for (tpindex = m[count - 1]->pindex + 1; 986b0cd2017SGleb Smirnoff tpindex < endpindex; i++, tpindex++) { 9877667839aSAlan Cox p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 988b0cd2017SGleb Smirnoff if (p == NULL) 989b0cd2017SGleb Smirnoff break; 990b0cd2017SGleb Smirnoff bp->b_pages[i] = p; 991eac91e32SKonstantin Belousov } 992b0cd2017SGleb Smirnoff 993b0cd2017SGleb Smirnoff bp->b_pgafter = i - bp->b_npages; 994b0cd2017SGleb Smirnoff bp->b_npages = i; 995b0cd2017SGleb Smirnoff } else 996b0cd2017SGleb Smirnoff bp->b_pgafter = 0; 997b0cd2017SGleb Smirnoff 998b0cd2017SGleb Smirnoff if (VM_OBJECT_WOWNED(object)) 999eac91e32SKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1000b0cd2017SGleb Smirnoff 1001b0cd2017SGleb Smirnoff /* Report back actual behind/ahead read. */ 1002b0cd2017SGleb Smirnoff if (a_rbehind) 1003b0cd2017SGleb Smirnoff *a_rbehind = bp->b_pgbefore; 1004b0cd2017SGleb Smirnoff if (a_rahead) 1005b0cd2017SGleb Smirnoff *a_rahead = bp->b_pgafter; 1006b0cd2017SGleb Smirnoff 1007e48b82bdSGleb Smirnoff #ifdef INVARIANTS 1008dcc0ff5aSGleb Smirnoff KASSERT(bp->b_npages <= nitems(bp->b_pages), 1009b0cd2017SGleb Smirnoff ("%s: buf %p overflowed", __func__, bp)); 10104f56243aSGleb Smirnoff for (int j = 1, prev = 0; j < bp->b_npages; j++) { 10111e0c121fSGleb Smirnoff if (bp->b_pages[j] == bogus_page) 10121e0c121fSGleb Smirnoff continue; 10131e0c121fSGleb Smirnoff KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex == 10141e0c121fSGleb Smirnoff j - prev, ("%s: pages array not consecutive, bp %p", 10151e0c121fSGleb Smirnoff __func__, bp)); 10161e0c121fSGleb Smirnoff prev = j; 10171e0c121fSGleb Smirnoff } 1018e48b82bdSGleb Smirnoff #endif 1019eac91e32SKonstantin Belousov 10200d94caffSDavid Greenman /* 1021b0cd2017SGleb Smirnoff * Recalculate first offset and bytecount with regards to read behind. 1022b0cd2017SGleb Smirnoff * Truncate bytecount to vnode real size and round up physical size 1023b0cd2017SGleb Smirnoff * for real devices. 102426f9a767SRodney W. Grimes */ 1025b0cd2017SGleb Smirnoff foff = IDX_TO_OFF(bp->b_pages[0]->pindex); 1026b0cd2017SGleb Smirnoff bytecount = bp->b_npages << PAGE_SHIFT; 1027b0cd2017SGleb Smirnoff if ((foff + bytecount) > object->un_pager.vnp.vnp_size) 1028b0cd2017SGleb Smirnoff bytecount = object->un_pager.vnp.vnp_size - foff; 1029eac91e32SKonstantin Belousov secmask = bo->bo_bsize - 1; 10306229cc50SPoul-Henning Kamp KASSERT(secmask < PAGE_SIZE && secmask > 0, 1031b0cd2017SGleb Smirnoff ("%s: sector size %d too large", __func__, secmask + 1)); 1032b0cd2017SGleb Smirnoff bytecount = (bytecount + secmask) & ~secmask; 103326f9a767SRodney W. Grimes 103426f9a767SRodney W. Grimes /* 1035b0cd2017SGleb Smirnoff * And map the pages to be read into the kva, if the filesystem 10366ce697dcSKonstantin Belousov * requires mapped buffers. 103726f9a767SRodney W. Grimes */ 10382a5eef69SGleb Smirnoff if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 && 10396ce697dcSKonstantin Belousov unmapped_buf_allowed) { 10406ce697dcSKonstantin Belousov bp->b_data = unmapped_buf; 10416ce697dcSKonstantin Belousov bp->b_offset = 0; 1042fade8dd7SJeff Roberson } else { 1043fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 1044b0cd2017SGleb Smirnoff pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages); 1045fade8dd7SJeff Roberson } 104626f9a767SRodney W. Grimes 1047b0cd2017SGleb Smirnoff /* Build a minimal buffer header. */ 104821144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 1049bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 1050bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 1051a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 1052a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 10539c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 10541faacf5dSKirk McKusick bp->b_vp = vp; 1055b0cd2017SGleb Smirnoff bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount; 10562c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 1057e48b82bdSGleb Smirnoff KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) == 1058e48b82bdSGleb Smirnoff (blkno0 - bp->b_blkno) * DEV_BSIZE + 1059e48b82bdSGleb Smirnoff IDX_TO_OFF(m[0]->pindex) % bsize, 1060e48b82bdSGleb Smirnoff ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju " 1061e48b82bdSGleb Smirnoff "blkno0 %ju b_blkno %ju", bsize, 1062e48b82bdSGleb Smirnoff (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex, 1063e48b82bdSGleb Smirnoff (uintmax_t)blkno0, (uintmax_t)bp->b_blkno)); 106490effb23SGleb Smirnoff 1065b0cd2017SGleb Smirnoff atomic_add_long(&runningbufspace, bp->b_runningbufspace); 106683c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 106783c9dea1SGleb Smirnoff VM_CNT_ADD(v_vnodepgsin, bp->b_npages); 1068b0cd2017SGleb Smirnoff 106990effb23SGleb Smirnoff if (iodone != NULL) { /* async */ 1070b0cd2017SGleb Smirnoff bp->b_pgiodone = iodone; 107190effb23SGleb Smirnoff bp->b_caller1 = arg; 107290effb23SGleb Smirnoff bp->b_iodone = vnode_pager_generic_getpages_done_async; 107390effb23SGleb Smirnoff bp->b_flags |= B_ASYNC; 107490effb23SGleb Smirnoff BUF_KERNPROC(bp); 1075b792bebeSPoul-Henning Kamp bstrategy(bp); 1076b0cd2017SGleb Smirnoff return (VM_PAGER_OK); 107790effb23SGleb Smirnoff } else { 107890effb23SGleb Smirnoff bp->b_iodone = bdone; 107990effb23SGleb Smirnoff bstrategy(bp); 10806a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnread"); 108190effb23SGleb Smirnoff error = vnode_pager_generic_getpages_done(bp); 10821bb5ad63SGleb Smirnoff for (i = 0; i < bp->b_npages; i++) 10836ce697dcSKonstantin Belousov bp->b_pages[i] = NULL; 10841faacf5dSKirk McKusick bp->b_vp = NULL; 10859c83534dSPoul-Henning Kamp pbrelbo(bp); 1086756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 108790effb23SGleb Smirnoff return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK); 108890effb23SGleb Smirnoff } 1089b0cd2017SGleb Smirnoff } 109090effb23SGleb Smirnoff 109190effb23SGleb Smirnoff static void 109290effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp) 109390effb23SGleb Smirnoff { 109490effb23SGleb Smirnoff int error; 109590effb23SGleb Smirnoff 109690effb23SGleb Smirnoff error = vnode_pager_generic_getpages_done(bp); 1097b0cd2017SGleb Smirnoff /* Run the iodone upon the requested range. */ 1098b0cd2017SGleb Smirnoff bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore, 1099b0cd2017SGleb Smirnoff bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error); 110090effb23SGleb Smirnoff for (int i = 0; i < bp->b_npages; i++) 110190effb23SGleb Smirnoff bp->b_pages[i] = NULL; 110290effb23SGleb Smirnoff bp->b_vp = NULL; 110390effb23SGleb Smirnoff pbrelbo(bp); 1104756a5412SGleb Smirnoff uma_zfree(vnode_pbuf_zone, bp); 110590effb23SGleb Smirnoff } 110690effb23SGleb Smirnoff 110790effb23SGleb Smirnoff static int 110890effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp) 110990effb23SGleb Smirnoff { 111090effb23SGleb Smirnoff vm_object_t object; 111190effb23SGleb Smirnoff off_t tfoff, nextoff; 111290effb23SGleb Smirnoff int i, error; 111390effb23SGleb Smirnoff 111490effb23SGleb Smirnoff error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0; 111590effb23SGleb Smirnoff object = bp->b_vp->v_object; 111690effb23SGleb Smirnoff 111790effb23SGleb Smirnoff if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) { 1118fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 1119fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 1120fade8dd7SJeff Roberson pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, 112190effb23SGleb Smirnoff bp->b_npages); 112290effb23SGleb Smirnoff } 1123fade8dd7SJeff Roberson bzero(bp->b_data + bp->b_bcount, 112490effb23SGleb Smirnoff PAGE_SIZE * bp->b_npages - bp->b_bcount); 112590effb23SGleb Smirnoff } 1126fade8dd7SJeff Roberson if (buf_mapped(bp)) { 1127fade8dd7SJeff Roberson pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1128fade8dd7SJeff Roberson bp->b_data = unmapped_buf; 112990effb23SGleb Smirnoff } 113026f9a767SRodney W. Grimes 113189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 113290effb23SGleb Smirnoff for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex); 113390effb23SGleb Smirnoff i < bp->b_npages; i++, tfoff = nextoff) { 11348f9110f6SJohn Dyson vm_page_t mt; 11358f9110f6SJohn Dyson 11368f9110f6SJohn Dyson nextoff = tfoff + PAGE_SIZE; 113790effb23SGleb Smirnoff mt = bp->b_pages[i]; 11388f9110f6SJohn Dyson 113954746b67SDmitrij Tejblum if (nextoff <= object->un_pager.vnp.vnp_size) { 11408d17e694SJulian Elischer /* 11418d17e694SJulian Elischer * Read filled up entire page. 11428d17e694SJulian Elischer */ 11438f9110f6SJohn Dyson mt->valid = VM_PAGE_BITS_ALL; 1144016a3c93SAlan Cox KASSERT(mt->dirty == 0, 114579f0deb9SGleb Smirnoff ("%s: page %p is dirty", __func__, mt)); 1146016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(mt), 114779f0deb9SGleb Smirnoff ("%s: page %p is mapped", __func__, mt)); 11488f9110f6SJohn Dyson } else { 11498d17e694SJulian Elischer /* 115042eb4108SAlan Cox * Read did not fill up entire page. 11518d17e694SJulian Elischer * 11528d17e694SJulian Elischer * Currently we do not set the entire page valid, 11538d17e694SJulian Elischer * we just try to clear the piece that we couldn't 11548d17e694SJulian Elischer * read. 11558d17e694SJulian Elischer */ 1156dc874f98SKonstantin Belousov vm_page_set_valid_range(mt, 0, 115754746b67SDmitrij Tejblum object->un_pager.vnp.vnp_size - tfoff); 115842eb4108SAlan Cox KASSERT((mt->dirty & vm_page_bits(0, 115942eb4108SAlan Cox object->un_pager.vnp.vnp_size - tfoff)) == 0, 116079f0deb9SGleb Smirnoff ("%s: page %p is dirty", __func__, mt)); 11618f9110f6SJohn Dyson } 11628f9110f6SJohn Dyson 1163b0cd2017SGleb Smirnoff if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter) 1164b6c00483SKonstantin Belousov vm_page_readahead_finish(mt); 116503679e23SAlan Cox } 116689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 116790effb23SGleb Smirnoff if (error != 0) 116890effb23SGleb Smirnoff printf("%s: I/O read error %d\n", __func__, error); 116990effb23SGleb Smirnoff 117090effb23SGleb Smirnoff return (error); 117126f9a767SRodney W. Grimes } 117226f9a767SRodney W. Grimes 1173ce75f2c3SMike Smith /* 1174ce75f2c3SMike Smith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 1175ce75f2c3SMike Smith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 1176ce75f2c3SMike Smith * vnode_pager_generic_putpages() to implement the previous behaviour. 1177ce75f2c3SMike Smith * 1178ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 1179ce75f2c3SMike Smith * backing vp's VOP_PUTPAGES. 1180ce75f2c3SMike Smith */ 1181e4542174SMatthew Dillon static void 11827ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count, 118333cad9e9SKonstantin Belousov int flags, int *rtvals) 1184170db9c6SJohn Dyson { 1185170db9c6SJohn Dyson int rtval; 1186170db9c6SJohn Dyson struct vnode *vp; 118786ffbd76SMike Smith int bytes = count * PAGE_SIZE; 1188ad980522SJohn Dyson 11890e3cdf2cSAlan Cox /* 11900e3cdf2cSAlan Cox * Force synchronous operation if we are extremely low on memory 11910e3cdf2cSAlan Cox * to prevent a low-memory deadlock. VOP operations often need to 11920e3cdf2cSAlan Cox * allocate more memory to initiate the I/O ( i.e. do a BMAP 11930e3cdf2cSAlan Cox * operation ). The swapper handles the case by limiting the amount 11940e3cdf2cSAlan Cox * of asynchronous I/O, but that sort of solution doesn't scale well 11950e3cdf2cSAlan Cox * for the vnode pager without a lot of work. 11960e3cdf2cSAlan Cox * 11970e3cdf2cSAlan Cox * Also, the backing vnode's iodone routine may not wake the pageout 11980e3cdf2cSAlan Cox * daemon up. This should be probably be addressed XXX. 11990e3cdf2cSAlan Cox */ 12000e3cdf2cSAlan Cox 1201e2068d0bSJeff Roberson if (vm_page_count_min()) 120233cad9e9SKonstantin Belousov flags |= VM_PAGER_PUT_SYNC; 12030e3cdf2cSAlan Cox 12040e3cdf2cSAlan Cox /* 12050e3cdf2cSAlan Cox * Call device-specific putpages function 12060e3cdf2cSAlan Cox */ 1207170db9c6SJohn Dyson vp = object->handle; 120889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 120933cad9e9SKonstantin Belousov rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals); 121023955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 121123955314SAlfred Perlstein ("vnode_pager: stale FS putpages\n")); 121289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1213170db9c6SJohn Dyson } 1214170db9c6SJohn Dyson 121505877a85SKonstantin Belousov static int 121605877a85SKonstantin Belousov vn_off2bidx(vm_ooffset_t offset) 121705877a85SKonstantin Belousov { 121805877a85SKonstantin Belousov 121905877a85SKonstantin Belousov return ((offset & PAGE_MASK) / DEV_BSIZE); 122005877a85SKonstantin Belousov } 122105877a85SKonstantin Belousov 122205877a85SKonstantin Belousov static bool 122305877a85SKonstantin Belousov vn_dirty_blk(vm_page_t m, vm_ooffset_t offset) 122405877a85SKonstantin Belousov { 122505877a85SKonstantin Belousov 122605877a85SKonstantin Belousov KASSERT(IDX_TO_OFF(m->pindex) <= offset && 122705877a85SKonstantin Belousov offset < IDX_TO_OFF(m->pindex + 1), 122805877a85SKonstantin Belousov ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex, 122905877a85SKonstantin Belousov (uintmax_t)offset)); 123005877a85SKonstantin Belousov return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0); 123105877a85SKonstantin Belousov } 1232ce75f2c3SMike Smith 123326f9a767SRodney W. Grimes /* 1234ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 12354491ea91SEivind Eklund * own vnodes if they fail to implement VOP_PUTPAGES. 12362b6b0df7SMatthew Dillon * 12372b6b0df7SMatthew Dillon * This is typically called indirectly via the pageout daemon and 1238763df3ecSPedro F. Giffuni * clustering has already typically occurred, so in general we ask the 12392b6b0df7SMatthew Dillon * underlying filesystem to write the data out asynchronously rather 12402b6b0df7SMatthew Dillon * then delayed. 124126f9a767SRodney W. Grimes */ 1242ce75f2c3SMike Smith int 1243c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount, 1244c46b90e9SAlan Cox int flags, int *rtvals) 124526f9a767SRodney W. Grimes { 1246ce75f2c3SMike Smith vm_object_t object; 1247c46b90e9SAlan Cox vm_page_t m; 124805877a85SKonstantin Belousov vm_ooffset_t maxblksz, next_offset, poffset, prev_offset; 1249f6b04d2bSDavid Greenman struct uio auio; 1250f6b04d2bSDavid Greenman struct iovec aiov; 125105877a85SKonstantin Belousov off_t prev_resid, wrsz; 1252e6c44f65SKonstantin Belousov int count, error, i, maxsize, ncount, pgoff, ppscheck; 125305877a85SKonstantin Belousov bool in_hole; 1254dd498befSPaul Saab static struct timeval lastfail; 1255dd498befSPaul Saab static int curfail; 125626f9a767SRodney W. Grimes 1257ce75f2c3SMike Smith object = vp->v_object; 1258ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 1259ce75f2c3SMike Smith 126026f9a767SRodney W. Grimes for (i = 0; i < count; i++) 1261031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_ERROR; 126226f9a767SRodney W. Grimes 1263c46b90e9SAlan Cox if ((int64_t)ma[0]->pindex < 0) { 1264e6c44f65SKonstantin Belousov printf("vnode_pager_generic_putpages: " 1265e6c44f65SKonstantin Belousov "attempt to write meta-data 0x%jx(%lx)\n", 1266e6c44f65SKonstantin Belousov (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty); 1267f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 1268e6c44f65SKonstantin Belousov return (VM_PAGER_BAD); 12690d94caffSDavid Greenman } 12700bdb7528SDavid Greenman 1271f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 1272f6b04d2bSDavid Greenman ncount = count; 127326f9a767SRodney W. Grimes 1274c46b90e9SAlan Cox poffset = IDX_TO_OFF(ma[0]->pindex); 127500a6f47fSMatthew Dillon 127600a6f47fSMatthew Dillon /* 127700a6f47fSMatthew Dillon * If the page-aligned write is larger then the actual file we 1278763df3ecSPedro F. Giffuni * have to invalidate pages occurring beyond the file EOF. However, 127900a6f47fSMatthew Dillon * there is an edge case where a file may not be page-aligned where 128000a6f47fSMatthew Dillon * the last page is partially invalid. In this case the filesystem 128100a6f47fSMatthew Dillon * may not properly clear the dirty bits for the entire page (which 128200a6f47fSMatthew Dillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 128300a6f47fSMatthew Dillon * With the page locked we are free to fix-up the dirty bits here. 12843ebeaf59SMatthew Dillon * 12853ebeaf59SMatthew Dillon * We do not under any circumstances truncate the valid bits, as 12863ebeaf59SMatthew Dillon * this will screw up bogus page replacement. 128700a6f47fSMatthew Dillon */ 1288b3d4ab66SKonstantin Belousov VM_OBJECT_RLOCK(object); 1289a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 1290b3d4ab66SKonstantin Belousov if (!VM_OBJECT_TRYUPGRADE(object)) { 1291b3d4ab66SKonstantin Belousov VM_OBJECT_RUNLOCK(object); 1292b3d4ab66SKonstantin Belousov VM_OBJECT_WLOCK(object); 1293b3d4ab66SKonstantin Belousov if (maxsize + poffset <= object->un_pager.vnp.vnp_size) 1294b3d4ab66SKonstantin Belousov goto downgrade; 1295b3d4ab66SKonstantin Belousov } 129600a6f47fSMatthew Dillon if (object->un_pager.vnp.vnp_size > poffset) { 1297a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 1298aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 129900a6f47fSMatthew Dillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1300938cdc42SKonstantin Belousov pgoff = roundup2(pgoff, DEV_BSIZE); 1301938cdc42SKonstantin Belousov 1302c46b90e9SAlan Cox /* 1303c46b90e9SAlan Cox * If the object is locked and the following 1304c46b90e9SAlan Cox * conditions hold, then the page's dirty 1305c46b90e9SAlan Cox * field cannot be concurrently changed by a 1306c46b90e9SAlan Cox * pmap operation. 1307c46b90e9SAlan Cox */ 1308c46b90e9SAlan Cox m = ma[ncount - 1]; 1309c7aebda8SAttilio Rao vm_page_assert_sbusied(m); 13106031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1311c46b90e9SAlan Cox ("vnode_pager_generic_putpages: page %p is not read-only", m)); 1312e6c44f65SKonstantin Belousov MPASS(m->dirty != 0); 1313c46b90e9SAlan Cox vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 1314c46b90e9SAlan Cox pgoff); 131500a6f47fSMatthew Dillon } 131600a6f47fSMatthew Dillon } else { 131700a6f47fSMatthew Dillon maxsize = 0; 131800a6f47fSMatthew Dillon ncount = 0; 131900a6f47fSMatthew Dillon } 1320e6c44f65SKonstantin Belousov for (i = ncount; i < count; i++) 1321f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 1322b3d4ab66SKonstantin Belousov downgrade: 1323b3d4ab66SKonstantin Belousov VM_OBJECT_LOCK_DOWNGRADE(object); 1324f6b04d2bSDavid Greenman } 132526f9a767SRodney W. Grimes 1326f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 1327f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 1328f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 1329e6c44f65SKonstantin Belousov auio.uio_td = NULL; 133005877a85SKonstantin Belousov maxblksz = roundup2(poffset + maxsize, DEV_BSIZE); 133105877a85SKonstantin Belousov 133205877a85SKonstantin Belousov for (prev_offset = poffset; prev_offset < maxblksz;) { 133305877a85SKonstantin Belousov /* Skip clean blocks. */ 133405877a85SKonstantin Belousov for (in_hole = true; in_hole && prev_offset < maxblksz;) { 133505877a85SKonstantin Belousov m = ma[OFF_TO_IDX(prev_offset - poffset)]; 133605877a85SKonstantin Belousov for (i = vn_off2bidx(prev_offset); 133705877a85SKonstantin Belousov i < sizeof(vm_page_bits_t) * NBBY && 133805877a85SKonstantin Belousov prev_offset < maxblksz; i++) { 133905877a85SKonstantin Belousov if (vn_dirty_blk(m, prev_offset)) { 134005877a85SKonstantin Belousov in_hole = false; 134105877a85SKonstantin Belousov break; 134205877a85SKonstantin Belousov } 134305877a85SKonstantin Belousov prev_offset += DEV_BSIZE; 134405877a85SKonstantin Belousov } 134505877a85SKonstantin Belousov } 134605877a85SKonstantin Belousov if (in_hole) 134705877a85SKonstantin Belousov goto write_done; 134805877a85SKonstantin Belousov 134905877a85SKonstantin Belousov /* Find longest run of dirty blocks. */ 135005877a85SKonstantin Belousov for (next_offset = prev_offset; next_offset < maxblksz;) { 135105877a85SKonstantin Belousov m = ma[OFF_TO_IDX(next_offset - poffset)]; 135205877a85SKonstantin Belousov for (i = vn_off2bidx(next_offset); 135305877a85SKonstantin Belousov i < sizeof(vm_page_bits_t) * NBBY && 135405877a85SKonstantin Belousov next_offset < maxblksz; i++) { 135505877a85SKonstantin Belousov if (!vn_dirty_blk(m, next_offset)) 135605877a85SKonstantin Belousov goto start_write; 135705877a85SKonstantin Belousov next_offset += DEV_BSIZE; 135805877a85SKonstantin Belousov } 135905877a85SKonstantin Belousov } 136005877a85SKonstantin Belousov start_write: 136105877a85SKonstantin Belousov if (next_offset > poffset + maxsize) 136205877a85SKonstantin Belousov next_offset = poffset + maxsize; 136305877a85SKonstantin Belousov 136405877a85SKonstantin Belousov /* 136505877a85SKonstantin Belousov * Getting here requires finding a dirty block in the 136605877a85SKonstantin Belousov * 'skip clean blocks' loop. 136705877a85SKonstantin Belousov */ 136805877a85SKonstantin Belousov MPASS(prev_offset < next_offset); 136905877a85SKonstantin Belousov 1370b3d4ab66SKonstantin Belousov VM_OBJECT_RUNLOCK(object); 137105877a85SKonstantin Belousov aiov.iov_base = NULL; 137205877a85SKonstantin Belousov auio.uio_iovcnt = 1; 137305877a85SKonstantin Belousov auio.uio_offset = prev_offset; 137405877a85SKonstantin Belousov prev_resid = auio.uio_resid = aiov.iov_len = next_offset - 137505877a85SKonstantin Belousov prev_offset; 137605877a85SKonstantin Belousov error = VOP_WRITE(vp, &auio, 137705877a85SKonstantin Belousov vnode_pager_putpages_ioflags(flags), curthread->td_ucred); 137805877a85SKonstantin Belousov 137905877a85SKonstantin Belousov wrsz = prev_resid - auio.uio_resid; 138005877a85SKonstantin Belousov if (wrsz == 0) { 138105877a85SKonstantin Belousov if (ppsratecheck(&lastfail, &curfail, 1) != 0) { 138205877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: " 138305877a85SKonstantin Belousov "zero-length write at %ju resid %zd\n", 138405877a85SKonstantin Belousov auio.uio_offset, auio.uio_resid); 138505877a85SKonstantin Belousov } 1386b3d4ab66SKonstantin Belousov VM_OBJECT_RLOCK(object); 138705877a85SKonstantin Belousov break; 138805877a85SKonstantin Belousov } 138905877a85SKonstantin Belousov 139005877a85SKonstantin Belousov /* Adjust the starting offset for next iteration. */ 139105877a85SKonstantin Belousov prev_offset += wrsz; 139205877a85SKonstantin Belousov MPASS(auio.uio_offset == prev_offset); 1393f6b04d2bSDavid Greenman 13943dbb0ca6SKonstantin Belousov ppscheck = 0; 139505877a85SKonstantin Belousov if (error != 0 && (ppscheck = ppsratecheck(&lastfail, 139605877a85SKonstantin Belousov &curfail, 1)) != 0) 139705877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: I/O error %d\n", 139805877a85SKonstantin Belousov error); 1399e6c44f65SKonstantin Belousov if (auio.uio_resid != 0 && (ppscheck != 0 || 1400e6c44f65SKonstantin Belousov ppsratecheck(&lastfail, &curfail, 1) != 0)) 140105877a85SKonstantin Belousov vn_printf(vp, "vnode_pager_putpages: residual I/O %zd " 140205877a85SKonstantin Belousov "at %ju\n", auio.uio_resid, 140305877a85SKonstantin Belousov (uintmax_t)ma[0]->pindex); 1404b3d4ab66SKonstantin Belousov VM_OBJECT_RLOCK(object); 140505877a85SKonstantin Belousov if (error != 0 || auio.uio_resid != 0) 140605877a85SKonstantin Belousov break; 140705877a85SKonstantin Belousov } 140805877a85SKonstantin Belousov write_done: 140905877a85SKonstantin Belousov /* Mark completely processed pages. */ 141005877a85SKonstantin Belousov for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++) 141126f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 141205877a85SKonstantin Belousov /* Mark partial EOF page. */ 141305877a85SKonstantin Belousov if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0) 141405877a85SKonstantin Belousov rtvals[i++] = VM_PAGER_OK; 141505877a85SKonstantin Belousov /* Unwritten pages in range, free bonus if the page is clean. */ 141605877a85SKonstantin Belousov for (; i < ncount; i++) 141705877a85SKonstantin Belousov rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR; 1418b3d4ab66SKonstantin Belousov VM_OBJECT_RUNLOCK(object); 141905877a85SKonstantin Belousov VM_CNT_ADD(v_vnodepgsout, i); 142005877a85SKonstantin Belousov VM_CNT_INC(v_vnodeout); 1421e6c44f65SKonstantin Belousov return (rtvals[0]); 142226f9a767SRodney W. Grimes } 1423031ec8c1SKonstantin Belousov 142465b9599aSKonstantin Belousov int 142565b9599aSKonstantin Belousov vnode_pager_putpages_ioflags(int pager_flags) 142665b9599aSKonstantin Belousov { 142765b9599aSKonstantin Belousov int ioflags; 142865b9599aSKonstantin Belousov 142965b9599aSKonstantin Belousov /* 143065b9599aSKonstantin Belousov * Pageouts are already clustered, use IO_ASYNC to force a 143165b9599aSKonstantin Belousov * bawrite() rather then a bdwrite() to prevent paging I/O 143265b9599aSKonstantin Belousov * from saturating the buffer cache. Dummy-up the sequential 143365b9599aSKonstantin Belousov * heuristic to cause large ranges to cluster. If neither 143465b9599aSKonstantin Belousov * IO_SYNC or IO_ASYNC is set, the system decides how to 143565b9599aSKonstantin Belousov * cluster. 143665b9599aSKonstantin Belousov */ 143765b9599aSKonstantin Belousov ioflags = IO_VMIO; 143865b9599aSKonstantin Belousov if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0) 143965b9599aSKonstantin Belousov ioflags |= IO_SYNC; 144065b9599aSKonstantin Belousov else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0) 144165b9599aSKonstantin Belousov ioflags |= IO_ASYNC; 144265b9599aSKonstantin Belousov ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0; 144365b9599aSKonstantin Belousov ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0; 144465b9599aSKonstantin Belousov ioflags |= IO_SEQMAX << IO_SEQSHIFT; 144565b9599aSKonstantin Belousov return (ioflags); 144665b9599aSKonstantin Belousov } 144765b9599aSKonstantin Belousov 1448555b7bb4SKonstantin Belousov /* 1449555b7bb4SKonstantin Belousov * vnode_pager_undirty_pages(). 1450555b7bb4SKonstantin Belousov * 1451555b7bb4SKonstantin Belousov * A helper to mark pages as clean after pageout that was possibly 1452555b7bb4SKonstantin Belousov * done with a short write. The lpos argument specifies the page run 1453555b7bb4SKonstantin Belousov * length in bytes, and the written argument specifies how many bytes 1454555b7bb4SKonstantin Belousov * were actually written. eof is the offset past the last valid byte 1455555b7bb4SKonstantin Belousov * in the vnode using the absolute file position of the first byte in 1456555b7bb4SKonstantin Belousov * the run as the base from which it is computed. 1457555b7bb4SKonstantin Belousov */ 1458031ec8c1SKonstantin Belousov void 1459555b7bb4SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof, 1460555b7bb4SKonstantin Belousov int lpos) 1461031ec8c1SKonstantin Belousov { 14629d17da3bSKonstantin Belousov vm_object_t obj; 1463555b7bb4SKonstantin Belousov int i, pos, pos_devb; 1464031ec8c1SKonstantin Belousov 1465555b7bb4SKonstantin Belousov if (written == 0 && eof >= lpos) 14669d17da3bSKonstantin Belousov return; 14679d17da3bSKonstantin Belousov obj = ma[0]->object; 146889f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 1469031ec8c1SKonstantin Belousov for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) { 1470031ec8c1SKonstantin Belousov if (pos < trunc_page(written)) { 1471031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_OK; 1472031ec8c1SKonstantin Belousov vm_page_undirty(ma[i]); 1473031ec8c1SKonstantin Belousov } else { 1474031ec8c1SKonstantin Belousov /* Partially written page. */ 1475031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_AGAIN; 1476031ec8c1SKonstantin Belousov vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK); 1477031ec8c1SKonstantin Belousov } 1478031ec8c1SKonstantin Belousov } 1479555b7bb4SKonstantin Belousov if (eof >= lpos) /* avoid truncation */ 1480555b7bb4SKonstantin Belousov goto done; 1481555b7bb4SKonstantin Belousov for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) { 1482555b7bb4SKonstantin Belousov if (pos != trunc_page(pos)) { 1483555b7bb4SKonstantin Belousov /* 1484555b7bb4SKonstantin Belousov * The page contains the last valid byte in 1485555b7bb4SKonstantin Belousov * the vnode, mark the rest of the page as 1486555b7bb4SKonstantin Belousov * clean, potentially making the whole page 1487555b7bb4SKonstantin Belousov * clean. 1488555b7bb4SKonstantin Belousov */ 1489555b7bb4SKonstantin Belousov pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE); 1490555b7bb4SKonstantin Belousov vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE - 1491555b7bb4SKonstantin Belousov pos_devb); 1492555b7bb4SKonstantin Belousov 1493555b7bb4SKonstantin Belousov /* 1494555b7bb4SKonstantin Belousov * If the page was cleaned, report the pageout 1495555b7bb4SKonstantin Belousov * on it as successful. msync() no longer 1496555b7bb4SKonstantin Belousov * needs to write out the page, endlessly 1497555b7bb4SKonstantin Belousov * creating write requests and dirty buffers. 1498555b7bb4SKonstantin Belousov */ 1499555b7bb4SKonstantin Belousov if (ma[i]->dirty == 0) 1500555b7bb4SKonstantin Belousov rtvals[i] = VM_PAGER_OK; 1501555b7bb4SKonstantin Belousov 1502555b7bb4SKonstantin Belousov pos = round_page(pos); 1503555b7bb4SKonstantin Belousov } else { 1504555b7bb4SKonstantin Belousov /* vm_pageout_flush() clears dirty */ 1505555b7bb4SKonstantin Belousov rtvals[i] = VM_PAGER_BAD; 1506555b7bb4SKonstantin Belousov pos += PAGE_SIZE; 1507555b7bb4SKonstantin Belousov } 1508555b7bb4SKonstantin Belousov } 1509555b7bb4SKonstantin Belousov done: 151089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 1511031ec8c1SKonstantin Belousov } 151284110e7eSKonstantin Belousov 151384110e7eSKonstantin Belousov void 151484110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start, 151584110e7eSKonstantin Belousov vm_offset_t end) 151684110e7eSKonstantin Belousov { 151784110e7eSKonstantin Belousov struct vnode *vp; 151884110e7eSKonstantin Belousov vm_ooffset_t old_wm; 151984110e7eSKonstantin Belousov 152089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 152184110e7eSKonstantin Belousov if (object->type != OBJT_VNODE) { 152289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 152384110e7eSKonstantin Belousov return; 152484110e7eSKonstantin Belousov } 152584110e7eSKonstantin Belousov old_wm = object->un_pager.vnp.writemappings; 152684110e7eSKonstantin Belousov object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start; 152784110e7eSKonstantin Belousov vp = object->handle; 152884110e7eSKonstantin Belousov if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) { 152978022527SKonstantin Belousov ASSERT_VOP_LOCKED(vp, "v_writecount inc"); 153078022527SKonstantin Belousov VOP_ADD_WRITECOUNT_CHECKED(vp, 1); 1531b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 1532b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 153384110e7eSKonstantin Belousov } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) { 153478022527SKonstantin Belousov ASSERT_VOP_LOCKED(vp, "v_writecount dec"); 153578022527SKonstantin Belousov VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 1536b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 1537b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 153884110e7eSKonstantin Belousov } 153989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 154084110e7eSKonstantin Belousov } 154184110e7eSKonstantin Belousov 154284110e7eSKonstantin Belousov void 154384110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start, 154484110e7eSKonstantin Belousov vm_offset_t end) 154584110e7eSKonstantin Belousov { 154684110e7eSKonstantin Belousov struct vnode *vp; 154784110e7eSKonstantin Belousov struct mount *mp; 154884110e7eSKonstantin Belousov vm_offset_t inc; 154984110e7eSKonstantin Belousov 155089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 155184110e7eSKonstantin Belousov 155284110e7eSKonstantin Belousov /* 155384110e7eSKonstantin Belousov * First, recheck the object type to account for the race when 155484110e7eSKonstantin Belousov * the vnode is reclaimed. 155584110e7eSKonstantin Belousov */ 155684110e7eSKonstantin Belousov if (object->type != OBJT_VNODE) { 155789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 155884110e7eSKonstantin Belousov return; 155984110e7eSKonstantin Belousov } 156084110e7eSKonstantin Belousov 156184110e7eSKonstantin Belousov /* 156284110e7eSKonstantin Belousov * Optimize for the case when writemappings is not going to 156384110e7eSKonstantin Belousov * zero. 156484110e7eSKonstantin Belousov */ 156584110e7eSKonstantin Belousov inc = end - start; 156684110e7eSKonstantin Belousov if (object->un_pager.vnp.writemappings != inc) { 156784110e7eSKonstantin Belousov object->un_pager.vnp.writemappings -= inc; 156889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 156984110e7eSKonstantin Belousov return; 157084110e7eSKonstantin Belousov } 157184110e7eSKonstantin Belousov 157284110e7eSKonstantin Belousov vp = object->handle; 157384110e7eSKonstantin Belousov vhold(vp); 157489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 157584110e7eSKonstantin Belousov mp = NULL; 157684110e7eSKonstantin Belousov vn_start_write(vp, &mp, V_WAIT); 157778022527SKonstantin Belousov vn_lock(vp, LK_SHARED | LK_RETRY); 157884110e7eSKonstantin Belousov 157984110e7eSKonstantin Belousov /* 158084110e7eSKonstantin Belousov * Decrement the object's writemappings, by swapping the start 158184110e7eSKonstantin Belousov * and end arguments for vnode_pager_update_writecount(). If 158284110e7eSKonstantin Belousov * there was not a race with vnode reclaimation, then the 158384110e7eSKonstantin Belousov * vnode's v_writecount is decremented. 158484110e7eSKonstantin Belousov */ 158584110e7eSKonstantin Belousov vnode_pager_update_writecount(object, end, start); 158684110e7eSKonstantin Belousov VOP_UNLOCK(vp, 0); 158784110e7eSKonstantin Belousov vdrop(vp); 158884110e7eSKonstantin Belousov if (mp != NULL) 158984110e7eSKonstantin Belousov vn_finished_write(mp); 159084110e7eSKonstantin Belousov } 1591