160727d8bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 326f9a767SRodney W. Grimes * Copyright (c) 1991 The Regents of the University of California. 426f9a767SRodney W. Grimes * All rights reserved. 526f9a767SRodney W. Grimes * Copyright (c) 1993, 1994 John S. Dyson 624a1cce3SDavid Greenman * Copyright (c) 1995, David Greenman 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 9df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 10df8bae1dSRodney W. Grimes * Science Department. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 20df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 215929bcfaSPhilippe Charnier * must display the following acknowledgement: 22df8bae1dSRodney W. Grimes * This product includes software developed by the University of 23df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 24df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 25df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 26df8bae1dSRodney W. Grimes * without specific prior written permission. 27df8bae1dSRodney W. Grimes * 28df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38df8bae1dSRodney W. Grimes * SUCH DAMAGE. 39df8bae1dSRodney W. Grimes * 4026f9a767SRodney W. Grimes * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91 41df8bae1dSRodney W. Grimes */ 42df8bae1dSRodney W. Grimes 43df8bae1dSRodney W. Grimes /* 44df8bae1dSRodney W. Grimes * Page to/from files (vnodes). 45df8bae1dSRodney W. Grimes */ 46df8bae1dSRodney W. Grimes 4726f9a767SRodney W. Grimes /* 4826f9a767SRodney W. Grimes * TODO: 4924a1cce3SDavid Greenman * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 50f6b04d2bSDavid Greenman * greatly re-simplify the vnode_pager. 5126f9a767SRodney W. Grimes */ 5226f9a767SRodney W. Grimes 53874651b1SDavid E. O'Brien #include <sys/cdefs.h> 54874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 55874651b1SDavid E. O'Brien 563d653db0SAlan Cox #include "opt_vm.h" 573d653db0SAlan Cox 58df8bae1dSRodney W. Grimes #include <sys/param.h> 59df8bae1dSRodney W. Grimes #include <sys/systm.h> 60df8bae1dSRodney W. Grimes #include <sys/proc.h> 61df8bae1dSRodney W. Grimes #include <sys/vnode.h> 62df8bae1dSRodney W. Grimes #include <sys/mount.h> 639626b608SPoul-Henning Kamp #include <sys/bio.h> 6424a1cce3SDavid Greenman #include <sys/buf.h> 65efeaf95aSDavid Greenman #include <sys/vmmeter.h> 66d07a6d3fSPoul-Henning Kamp #include <sys/limits.h> 6724579ca1SMatthew Dillon #include <sys/conf.h> 6889f6b863SAttilio Rao #include <sys/rwlock.h> 699e0ddbd0SAlan Cox #include <sys/sf_buf.h> 70df8bae1dSRodney W. Grimes 714f12e0acSSuleiman Souhlal #include <machine/atomic.h> 724f12e0acSSuleiman Souhlal 73df8bae1dSRodney W. Grimes #include <vm/vm.h> 741c771f92SKonstantin Belousov #include <vm/vm_param.h> 75efeaf95aSDavid Greenman #include <vm/vm_object.h> 76df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 7724a1cce3SDavid Greenman #include <vm/vm_pager.h> 781efb74fbSJohn Dyson #include <vm/vm_map.h> 79df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h> 80efeaf95aSDavid Greenman #include <vm/vm_extern.h> 81df8bae1dSRodney W. Grimes 82bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 83bff76343SAlan Cox daddr_t *rtaddress, int *run); 8411caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 8511caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 8611caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t); 8790effb23SGleb Smirnoff static int vnode_pager_local_getpages0(struct vnode *, vm_page_t *, int, int, 8890effb23SGleb Smirnoff vop_getpages_iodone_t, void *); 8911caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int); 9090effb23SGleb Smirnoff static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int, 9190effb23SGleb Smirnoff vop_getpages_iodone_t, void *); 9233cad9e9SKonstantin Belousov static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 9311caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 943364c323SKonstantin Belousov static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 953364c323SKonstantin Belousov vm_ooffset_t, struct ucred *cred); 9690effb23SGleb Smirnoff static int vnode_pager_generic_getpages_done(struct buf *); 9790effb23SGleb Smirnoff static void vnode_pager_generic_getpages_done_async(struct buf *); 980b8253a7SBruce Evans 99df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 1004e658600SPoul-Henning Kamp .pgo_alloc = vnode_pager_alloc, 1014e658600SPoul-Henning Kamp .pgo_dealloc = vnode_pager_dealloc, 1024e658600SPoul-Henning Kamp .pgo_getpages = vnode_pager_getpages, 10390effb23SGleb Smirnoff .pgo_getpages_async = vnode_pager_getpages_async, 1044e658600SPoul-Henning Kamp .pgo_putpages = vnode_pager_putpages, 1054e658600SPoul-Henning Kamp .pgo_haspage = vnode_pager_haspage, 106df8bae1dSRodney W. Grimes }; 107df8bae1dSRodney W. Grimes 108b62b9b64SJohn Baldwin int vnode_pbuf_freecnt; 10973e9030eSGleb Smirnoff int vnode_async_pbuf_freecnt; 1101c7c3c6aSMatthew Dillon 111d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */ 112d07a6d3fSPoul-Henning Kamp int 113731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td) 114d07a6d3fSPoul-Henning Kamp { 115d07a6d3fSPoul-Henning Kamp vm_object_t object; 116d07a6d3fSPoul-Henning Kamp vm_ooffset_t size = isize; 117d07a6d3fSPoul-Henning Kamp struct vattr va; 118d07a6d3fSPoul-Henning Kamp 119d07a6d3fSPoul-Henning Kamp if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE) 120d07a6d3fSPoul-Henning Kamp return (0); 121d07a6d3fSPoul-Henning Kamp 122d07a6d3fSPoul-Henning Kamp while ((object = vp->v_object) != NULL) { 12389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 124d07a6d3fSPoul-Henning Kamp if (!(object->flags & OBJ_DEAD)) { 12589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 126d07a6d3fSPoul-Henning Kamp return (0); 127d07a6d3fSPoul-Henning Kamp } 12822db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 129d07a6d3fSPoul-Henning Kamp vm_object_set_flag(object, OBJ_DISCONNECTWNT); 1300dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0); 131cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 132d07a6d3fSPoul-Henning Kamp } 133d07a6d3fSPoul-Henning Kamp 134d07a6d3fSPoul-Henning Kamp if (size == 0) { 135d07a6d3fSPoul-Henning Kamp if (vn_isdisk(vp, NULL)) { 136d07a6d3fSPoul-Henning Kamp size = IDX_TO_OFF(INT_MAX); 137d07a6d3fSPoul-Henning Kamp } else { 1380359a12eSAttilio Rao if (VOP_GETATTR(vp, &va, td->td_ucred)) 139d07a6d3fSPoul-Henning Kamp return (0); 140d07a6d3fSPoul-Henning Kamp size = va.va_size; 141d07a6d3fSPoul-Henning Kamp } 142d07a6d3fSPoul-Henning Kamp } 143d07a6d3fSPoul-Henning Kamp 1443364c323SKonstantin Belousov object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred); 145d07a6d3fSPoul-Henning Kamp /* 146d07a6d3fSPoul-Henning Kamp * Dereference the reference we just created. This assumes 147d07a6d3fSPoul-Henning Kamp * that the object is associated with the vp. 148d07a6d3fSPoul-Henning Kamp */ 14989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 150d07a6d3fSPoul-Henning Kamp object->ref_count--; 15189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 152d07a6d3fSPoul-Henning Kamp vrele(vp); 153d07a6d3fSPoul-Henning Kamp 154d07a6d3fSPoul-Henning Kamp KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object")); 155d07a6d3fSPoul-Henning Kamp 156d07a6d3fSPoul-Henning Kamp return (0); 157d07a6d3fSPoul-Henning Kamp } 158d07a6d3fSPoul-Henning Kamp 1597146d6cbSPoul-Henning Kamp void 1607146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp) 1617146d6cbSPoul-Henning Kamp { 1627146d6cbSPoul-Henning Kamp struct vm_object *obj; 1637146d6cbSPoul-Henning Kamp 1647146d6cbSPoul-Henning Kamp obj = vp->v_object; 1657146d6cbSPoul-Henning Kamp if (obj == NULL) 1667146d6cbSPoul-Henning Kamp return; 16757fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject"); 16889f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 1697146d6cbSPoul-Henning Kamp if (obj->ref_count == 0) { 1707146d6cbSPoul-Henning Kamp /* 1717146d6cbSPoul-Henning Kamp * don't double-terminate the object 1727146d6cbSPoul-Henning Kamp */ 1737146d6cbSPoul-Henning Kamp if ((obj->flags & OBJ_DEAD) == 0) 1747146d6cbSPoul-Henning Kamp vm_object_terminate(obj); 1757146d6cbSPoul-Henning Kamp else 17689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 1777146d6cbSPoul-Henning Kamp } else { 1787146d6cbSPoul-Henning Kamp /* 1797146d6cbSPoul-Henning Kamp * Woe to the process that tries to page now :-). 1807146d6cbSPoul-Henning Kamp */ 1817146d6cbSPoul-Henning Kamp vm_pager_deallocate(obj); 18289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 1837146d6cbSPoul-Henning Kamp } 1846e4b2820SJeff Roberson vp->v_object = NULL; 1857146d6cbSPoul-Henning Kamp } 1867146d6cbSPoul-Henning Kamp 1877146d6cbSPoul-Henning Kamp 188df8bae1dSRodney W. Grimes /* 189df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 190df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 191990ab7adSAlan Cox * 192990ab7adSAlan Cox * MPSAFE 193df8bae1dSRodney W. Grimes */ 19424a1cce3SDavid Greenman vm_object_t 1956cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 1963364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 197df8bae1dSRodney W. Grimes { 19806cb7259SDavid Greenman vm_object_t object; 199df8bae1dSRodney W. Grimes struct vnode *vp; 200df8bae1dSRodney W. Grimes 201df8bae1dSRodney W. Grimes /* 202df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 203df8bae1dSRodney W. Grimes */ 204df8bae1dSRodney W. Grimes if (handle == NULL) 205df8bae1dSRodney W. Grimes return (NULL); 206df8bae1dSRodney W. Grimes 207df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 20839d38f93SDavid Greenman 20939d38f93SDavid Greenman /* 21039d38f93SDavid Greenman * If the object is being terminated, wait for it to 21139d38f93SDavid Greenman * go away. 21239d38f93SDavid Greenman */ 2132ac78f0eSStephan Uphoff retry: 2141ca58953SAlan Cox while ((object = vp->v_object) != NULL) { 21589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2161ca58953SAlan Cox if ((object->flags & OBJ_DEAD) == 0) 2171ca58953SAlan Cox break; 21819187819SAlan Cox vm_object_set_flag(object, OBJ_DISCONNECTWNT); 2190dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0); 22024a1cce3SDavid Greenman } 2210d94caffSDavid Greenman 2226ded8427SKonstantin Belousov KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference")); 2232be70f79SJohn Dyson 22424a1cce3SDavid Greenman if (object == NULL) { 225df8bae1dSRodney W. Grimes /* 2262ac78f0eSStephan Uphoff * Add an object of the appropriate size 227df8bae1dSRodney W. Grimes */ 2286cde7a16SDavid Greenman object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 229bbc0ec52SDavid Greenman 2306cde7a16SDavid Greenman object->un_pager.vnp.vnp_size = size; 23184110e7eSKonstantin Belousov object->un_pager.vnp.writemappings = 0; 23226f9a767SRodney W. Grimes 23324a1cce3SDavid Greenman object->handle = handle; 23411be8415SStephan Uphoff VI_LOCK(vp); 2352ac78f0eSStephan Uphoff if (vp->v_object != NULL) { 2362ac78f0eSStephan Uphoff /* 2372ac78f0eSStephan Uphoff * Object has been created while we were sleeping 2382ac78f0eSStephan Uphoff */ 23911be8415SStephan Uphoff VI_UNLOCK(vp); 2402ac78f0eSStephan Uphoff vm_object_destroy(object); 2412ac78f0eSStephan Uphoff goto retry; 242df8bae1dSRodney W. Grimes } 2432ac78f0eSStephan Uphoff vp->v_object = object; 24411be8415SStephan Uphoff VI_UNLOCK(vp); 24511be8415SStephan Uphoff } else { 2462ac78f0eSStephan Uphoff object->ref_count++; 2473d653db0SAlan Cox #if VM_NRESERVLEVEL > 0 2483d653db0SAlan Cox vm_object_color(object, 0); 2493d653db0SAlan Cox #endif 25089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 25111be8415SStephan Uphoff } 2527747c038SJeff Roberson vref(vp); 25324a1cce3SDavid Greenman return (object); 254df8bae1dSRodney W. Grimes } 255df8bae1dSRodney W. Grimes 256658ad5ffSAlan Cox /* 257658ad5ffSAlan Cox * The object must be locked. 258658ad5ffSAlan Cox */ 259f708ef1bSPoul-Henning Kamp static void 2607ebba1f8SGleb Smirnoff vnode_pager_dealloc(vm_object_t object) 26124a1cce3SDavid Greenman { 262b9f180d1SKonstantin Belousov struct vnode *vp; 263b9f180d1SKonstantin Belousov int refs; 264df8bae1dSRodney W. Grimes 265b9f180d1SKonstantin Belousov vp = object->handle; 26624a1cce3SDavid Greenman if (vp == NULL) 26724a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 26824a1cce3SDavid Greenman 26989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 27066095752SJohn Dyson vm_object_pip_wait(object, "vnpdea"); 271b9f180d1SKonstantin Belousov refs = object->ref_count; 27224a1cce3SDavid Greenman 27324a1cce3SDavid Greenman object->handle = NULL; 27495461b45SJohn Dyson object->type = OBJT_DEAD; 27519187819SAlan Cox if (object->flags & OBJ_DISCONNECTWNT) { 27619187819SAlan Cox vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 27719187819SAlan Cox wakeup(object); 27819187819SAlan Cox } 27957fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc"); 28084110e7eSKonstantin Belousov if (object->un_pager.vnp.writemappings > 0) { 28184110e7eSKonstantin Belousov object->un_pager.vnp.writemappings = 0; 282140dedb8SKonstantin Belousov VOP_ADD_WRITECOUNT(vp, -1); 283b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 284b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 28584110e7eSKonstantin Belousov } 286aa2cabb9SDavid Greenman vp->v_object = NULL; 287877d24acSKonstantin Belousov VOP_UNSET_TEXT(vp); 28889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 289b9f180d1SKonstantin Belousov while (refs-- > 0) 290b9f180d1SKonstantin Belousov vunref(vp); 29189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 292df8bae1dSRodney W. Grimes } 29326f9a767SRodney W. Grimes 294f708ef1bSPoul-Henning Kamp static boolean_t 2957ebba1f8SGleb Smirnoff vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 2967ebba1f8SGleb Smirnoff int *after) 297df8bae1dSRodney W. Grimes { 29824a1cce3SDavid Greenman struct vnode *vp = object->handle; 29998b0c789SPoul-Henning Kamp daddr_t bn; 3003af76890SPoul-Henning Kamp int err; 301170db9c6SJohn Dyson daddr_t reqblock; 3022c4488fcSJohn Dyson int poff; 3032c4488fcSJohn Dyson int bsize; 304d63596ceSJohn Dyson int pagesperblock, blocksperpage; 305df8bae1dSRodney W. Grimes 30689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 30724579ca1SMatthew Dillon /* 30824579ca1SMatthew Dillon * If no vp or vp is doomed or marked transparent to VM, we do not 30924579ca1SMatthew Dillon * have the page. 31024579ca1SMatthew Dillon */ 311b73f64c4SJeff Roberson if (vp == NULL || vp->v_iflag & VI_DOOMED) 31247221757SJohn Dyson return FALSE; 313df8bae1dSRodney W. Grimes /* 314b73f64c4SJeff Roberson * If the offset is beyond end of file we do 3150d94caffSDavid Greenman * not have the page. 316df8bae1dSRodney W. Grimes */ 317b73f64c4SJeff Roberson if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 3184abc71c0SDavid Greenman return FALSE; 319df8bae1dSRodney W. Grimes 320eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 321170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 322d63596ceSJohn Dyson blocksperpage = 0; 323d63596ceSJohn Dyson if (pagesperblock > 0) { 324a316d390SJohn Dyson reqblock = pindex / pagesperblock; 325d63596ceSJohn Dyson } else { 326d63596ceSJohn Dyson blocksperpage = (PAGE_SIZE / bsize); 327d63596ceSJohn Dyson reqblock = pindex * blocksperpage; 328d63596ceSJohn Dyson } 32989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 330ec794849SPoul-Henning Kamp err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 33189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 3320d94caffSDavid Greenman if (err) 33324a1cce3SDavid Greenman return TRUE; 3346eab77f2SJohn Dyson if (bn == -1) 335ced399eeSJohn Dyson return FALSE; 336d63596ceSJohn Dyson if (pagesperblock > 0) { 337a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 338170db9c6SJohn Dyson if (before) { 339170db9c6SJohn Dyson *before *= pagesperblock; 340170db9c6SJohn Dyson *before += poff; 341170db9c6SJohn Dyson } 342170db9c6SJohn Dyson if (after) { 34384d31376SGleb Smirnoff /* 34484d31376SGleb Smirnoff * The BMAP vop can report a partial block in the 345*d2596d17SGleb Smirnoff * 'after', but must not report blocks after EOF. 34684d31376SGleb Smirnoff * Assert the latter, and truncate 'after' in case 34784d31376SGleb Smirnoff * of the former. 34884d31376SGleb Smirnoff */ 349*d2596d17SGleb Smirnoff KASSERT((reqblock + *after) * pagesperblock < 350*d2596d17SGleb Smirnoff roundup2(object->size, pagesperblock), 35184d31376SGleb Smirnoff ("%s: reqblock %jd after %d size %ju", __func__, 35284d31376SGleb Smirnoff (intmax_t )reqblock, *after, 35384d31376SGleb Smirnoff (uintmax_t )object->size)); 354170db9c6SJohn Dyson *after *= pagesperblock; 35584d31376SGleb Smirnoff *after += pagesperblock - (poff + 1); 35684d31376SGleb Smirnoff if (pindex + *after >= object->size) 35784d31376SGleb Smirnoff *after = object->size - 1 - pindex; 358170db9c6SJohn Dyson } 359d63596ceSJohn Dyson } else { 360d63596ceSJohn Dyson if (before) { 361d63596ceSJohn Dyson *before /= blocksperpage; 362d63596ceSJohn Dyson } 363d63596ceSJohn Dyson 364d63596ceSJohn Dyson if (after) { 365d63596ceSJohn Dyson *after /= blocksperpage; 366d63596ceSJohn Dyson } 367d63596ceSJohn Dyson } 368ced399eeSJohn Dyson return TRUE; 369df8bae1dSRodney W. Grimes } 370df8bae1dSRodney W. Grimes 371df8bae1dSRodney W. Grimes /* 372df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 37324a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 374df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 375df8bae1dSRodney W. Grimes * 376df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 377df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 378df8bae1dSRodney W. Grimes */ 379df8bae1dSRodney W. Grimes void 3807ebba1f8SGleb Smirnoff vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize) 381df8bae1dSRodney W. Grimes { 3822a8f9ab5SAlan Cox vm_object_t object; 3832a8f9ab5SAlan Cox vm_page_t m; 384c576d121SLuoqi Chen vm_pindex_t nobjsize; 385df8bae1dSRodney W. Grimes 3862a8f9ab5SAlan Cox if ((object = vp->v_object) == NULL) 387df8bae1dSRodney W. Grimes return; 388cb61d698SKonstantin Belousov /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */ 38989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 3909b8851faSKonstantin Belousov if (object->type == OBJT_DEAD) { 3919b8851faSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 3929b8851faSKonstantin Belousov return; 3939b8851faSKonstantin Belousov } 3949b8851faSKonstantin Belousov KASSERT(object->type == OBJT_VNODE, 3959b8851faSKonstantin Belousov ("not vnode-backed object %p", object)); 3962a8f9ab5SAlan Cox if (nsize == object->un_pager.vnp.vnp_size) { 397df8bae1dSRodney W. Grimes /* 398df8bae1dSRodney W. Grimes * Hasn't changed size 399df8bae1dSRodney W. Grimes */ 40089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 401df8bae1dSRodney W. Grimes return; 4022a8f9ab5SAlan Cox } 403c576d121SLuoqi Chen nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 4042a8f9ab5SAlan Cox if (nsize < object->un_pager.vnp.vnp_size) { 405df8bae1dSRodney W. Grimes /* 406bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 407df8bae1dSRodney W. Grimes */ 4082a8f9ab5SAlan Cox if (nobjsize < object->size) 409c576d121SLuoqi Chen vm_object_page_remove(object, nobjsize, object->size, 4106bbee8e2SAlan Cox 0); 411bbc0ec52SDavid Greenman /* 412bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 4133ebeaf59SMatthew Dillon * only partially backed by the vnode. 4143ebeaf59SMatthew Dillon * 4153ebeaf59SMatthew Dillon * XXX for some reason (I don't know yet), if we take a 4163ebeaf59SMatthew Dillon * completely invalid page and mark it partially valid 4173ebeaf59SMatthew Dillon * it can screw up NFS reads, so we don't allow the case. 418bbc0ec52SDavid Greenman */ 4192a8f9ab5SAlan Cox if ((nsize & PAGE_MASK) && 4201b26eb10SAlan Cox (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 4211b26eb10SAlan Cox m->valid != 0) { 4222b6b0df7SMatthew Dillon int base = (int)nsize & PAGE_MASK; 4232b6b0df7SMatthew Dillon int size = PAGE_SIZE - base; 4242b6b0df7SMatthew Dillon 4252b6b0df7SMatthew Dillon /* 4262b6b0df7SMatthew Dillon * Clear out partial-page garbage in case 4272b6b0df7SMatthew Dillon * the page has been mapped. 4282b6b0df7SMatthew Dillon */ 429fff6062aSAlan Cox pmap_zero_page_area(m, base, size); 4302b6b0df7SMatthew Dillon 4312b6b0df7SMatthew Dillon /* 4323c33df62SAlan Cox * Update the valid bits to reflect the blocks that 4333c33df62SAlan Cox * have been zeroed. Some of these valid bits may 4343c33df62SAlan Cox * have already been set. 4353c33df62SAlan Cox */ 436dc874f98SKonstantin Belousov vm_page_set_valid_range(m, base, size); 4373c33df62SAlan Cox 4383c33df62SAlan Cox /* 4393c33df62SAlan Cox * Round "base" to the next block boundary so that the 4403c33df62SAlan Cox * dirty bit for a partially zeroed block is not 4413c33df62SAlan Cox * cleared. 4423c33df62SAlan Cox */ 4433c33df62SAlan Cox base = roundup2(base, DEV_BSIZE); 4443c33df62SAlan Cox 4453c33df62SAlan Cox /* 4463c33df62SAlan Cox * Clear out partial-page dirty bits. 4473ebeaf59SMatthew Dillon * 4483ebeaf59SMatthew Dillon * note that we do not clear out the valid 4493ebeaf59SMatthew Dillon * bits. This would prevent bogus_page 4503ebeaf59SMatthew Dillon * replacement from working properly. 4512b6b0df7SMatthew Dillon */ 4523c33df62SAlan Cox vm_page_clear_dirty(m, base, PAGE_SIZE - base); 4530ab3c7a5SAlan Cox } else if ((nsize & PAGE_MASK) && 454db9ba578SAttilio Rao vm_page_is_cached(object, OFF_TO_IDX(nsize))) { 4550ab3c7a5SAlan Cox vm_page_cache_free(object, OFF_TO_IDX(nsize), 4560ab3c7a5SAlan Cox nobjsize); 457bbc0ec52SDavid Greenman } 458bbc0ec52SDavid Greenman } 459a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 460c576d121SLuoqi Chen object->size = nobjsize; 46189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 462df8bae1dSRodney W. Grimes } 463df8bae1dSRodney W. Grimes 46426f9a767SRodney W. Grimes /* 46526f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 46626f9a767SRodney W. Grimes * file address 46726f9a767SRodney W. Grimes */ 468bff76343SAlan Cox static int 469bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 470bff76343SAlan Cox int *run) 47126f9a767SRodney W. Grimes { 47226f9a767SRodney W. Grimes int bsize; 47326f9a767SRodney W. Grimes int err; 474a316d390SJohn Dyson daddr_t vblock; 475f3aad9a6SBjoern A. Zeeb daddr_t voffset; 47626f9a767SRodney W. Grimes 4772ad036b6SAlan Cox if (address < 0) 4780d94caffSDavid Greenman return -1; 4790d94caffSDavid Greenman 480b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 4812c4488fcSJohn Dyson return -1; 4822c4488fcSJohn Dyson 48326f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 48426f9a767SRodney W. Grimes vblock = address / bsize; 48526f9a767SRodney W. Grimes voffset = address % bsize; 48626f9a767SRodney W. Grimes 487bff76343SAlan Cox err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 488bff76343SAlan Cox if (err == 0) { 489bff76343SAlan Cox if (*rtaddress != -1) 490bff76343SAlan Cox *rtaddress += voffset / DEV_BSIZE; 491efc68ce1SDavid Greenman if (run) { 492efc68ce1SDavid Greenman *run += 1; 493efc68ce1SDavid Greenman *run *= bsize/PAGE_SIZE; 494efc68ce1SDavid Greenman *run -= voffset/PAGE_SIZE; 495efc68ce1SDavid Greenman } 496efc68ce1SDavid Greenman } 49726f9a767SRodney W. Grimes 498bff76343SAlan Cox return (err); 49926f9a767SRodney W. Grimes } 50026f9a767SRodney W. Grimes 50126f9a767SRodney W. Grimes /* 50226f9a767SRodney W. Grimes * small block filesystem vnode pager input 50326f9a767SRodney W. Grimes */ 504f708ef1bSPoul-Henning Kamp static int 5057ebba1f8SGleb Smirnoff vnode_pager_input_smlfs(vm_object_t object, vm_page_t m) 50626f9a767SRodney W. Grimes { 5079c83534dSPoul-Henning Kamp struct vnode *vp; 5089c83534dSPoul-Henning Kamp struct bufobj *bo; 50926f9a767SRodney W. Grimes struct buf *bp; 5109e0ddbd0SAlan Cox struct sf_buf *sf; 511f3aad9a6SBjoern A. Zeeb daddr_t fileaddr; 51226f9a767SRodney W. Grimes vm_offset_t bsize; 513561cc9fcSKonstantin Belousov vm_page_bits_t bits; 514561cc9fcSKonstantin Belousov int error, i; 51526f9a767SRodney W. Grimes 516561cc9fcSKonstantin Belousov error = 0; 51724a1cce3SDavid Greenman vp = object->handle; 518b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 5192c4488fcSJohn Dyson return VM_PAGER_BAD; 5202c4488fcSJohn Dyson 52126f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 5220bdb7528SDavid Greenman 5239c83534dSPoul-Henning Kamp VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 52426f9a767SRodney W. Grimes 5259e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 52626f9a767SRodney W. Grimes 52726f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 52833c67741SMatthew Dillon vm_ooffset_t address; 529bbc0ec52SDavid Greenman 5300d53a17bSAlan Cox bits = vm_page_bits(i * bsize, bsize); 5310d53a17bSAlan Cox if (m->valid & bits) 53226f9a767SRodney W. Grimes continue; 53326f9a767SRodney W. Grimes 53433c67741SMatthew Dillon address = IDX_TO_OFF(m->pindex) + i * bsize; 53533c67741SMatthew Dillon if (address >= object->un_pager.vnp.vnp_size) { 53633c67741SMatthew Dillon fileaddr = -1; 53733c67741SMatthew Dillon } else { 538bff76343SAlan Cox error = vnode_pager_addr(vp, address, &fileaddr, NULL); 539bff76343SAlan Cox if (error) 540bff76343SAlan Cox break; 54133c67741SMatthew Dillon } 54226f9a767SRodney W. Grimes if (fileaddr != -1) { 5431c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 54426f9a767SRodney W. Grimes 54526f9a767SRodney W. Grimes /* build a minimal buffer header */ 54621144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 5476a4b5823SPoul-Henning Kamp bp->b_iodone = bdone; 548bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 549bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 550a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 551a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 5529e0ddbd0SAlan Cox bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 553187f0071SDavid Greenman bp->b_blkno = fileaddr; 5549c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 5551faacf5dSKirk McKusick bp->b_vp = vp; 55626f9a767SRodney W. Grimes bp->b_bcount = bsize; 55726f9a767SRodney W. Grimes bp->b_bufsize = bsize; 5582b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 5595bd65606SJohn Baldwin atomic_add_long(&runningbufspace, bp->b_runningbufspace); 56026f9a767SRodney W. Grimes 56126f9a767SRodney W. Grimes /* do the input */ 5622c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 563b792bebeSPoul-Henning Kamp bstrategy(bp); 56426f9a767SRodney W. Grimes 5656a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnsrd"); 5666a4b5823SPoul-Henning Kamp 567c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 56826f9a767SRodney W. Grimes error = EIO; 56926f9a767SRodney W. Grimes 57026f9a767SRodney W. Grimes /* 57126f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 57226f9a767SRodney W. Grimes */ 5731faacf5dSKirk McKusick bp->b_vp = NULL; 5749c83534dSPoul-Henning Kamp pbrelbo(bp); 5751c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 57626f9a767SRodney W. Grimes if (error) 57726f9a767SRodney W. Grimes break; 5780d53a17bSAlan Cox } else 5799e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 5800d53a17bSAlan Cox KASSERT((m->dirty & bits) == 0, 5810d53a17bSAlan Cox ("vnode_pager_input_smlfs: page %p is dirty", m)); 58289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 5830d53a17bSAlan Cox m->valid |= bits; 58489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 58526f9a767SRodney W. Grimes } 5869e0ddbd0SAlan Cox sf_buf_free(sf); 58726f9a767SRodney W. Grimes if (error) { 588a83c285cSDavid Greenman return VM_PAGER_ERROR; 58926f9a767SRodney W. Grimes } 59026f9a767SRodney W. Grimes return VM_PAGER_OK; 59126f9a767SRodney W. Grimes } 59226f9a767SRodney W. Grimes 59326f9a767SRodney W. Grimes /* 594475e8cc3SPoul-Henning Kamp * old style vnode pager input routine 59526f9a767SRodney W. Grimes */ 596f708ef1bSPoul-Henning Kamp static int 5977ebba1f8SGleb Smirnoff vnode_pager_input_old(vm_object_t object, vm_page_t m) 59826f9a767SRodney W. Grimes { 599df8bae1dSRodney W. Grimes struct uio auio; 600df8bae1dSRodney W. Grimes struct iovec aiov; 60126f9a767SRodney W. Grimes int error; 60226f9a767SRodney W. Grimes int size; 6039e0ddbd0SAlan Cox struct sf_buf *sf; 604342a1480SJohn Baldwin struct vnode *vp; 605df8bae1dSRodney W. Grimes 60689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 60726f9a767SRodney W. Grimes error = 0; 608bbc0ec52SDavid Greenman 609df8bae1dSRodney W. Grimes /* 61026f9a767SRodney W. Grimes * Return failure if beyond current EOF 61126f9a767SRodney W. Grimes */ 612a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 61326f9a767SRodney W. Grimes return VM_PAGER_BAD; 61426f9a767SRodney W. Grimes } else { 61526f9a767SRodney W. Grimes size = PAGE_SIZE; 616a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 617a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 61852051abcSAlan Cox vp = object->handle; 61989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 6200bdb7528SDavid Greenman 62126f9a767SRodney W. Grimes /* 622df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 623df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 624df8bae1dSRodney W. Grimes */ 6259e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 6260bdb7528SDavid Greenman 6279e0ddbd0SAlan Cox aiov.iov_base = (caddr_t)sf_buf_kva(sf); 628df8bae1dSRodney W. Grimes aiov.iov_len = size; 629df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 630df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 631a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 632df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 63326f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 634df8bae1dSRodney W. Grimes auio.uio_resid = size; 635b40ce416SJulian Elischer auio.uio_td = curthread; 63626f9a767SRodney W. Grimes 637a854ed98SJohn Baldwin error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 638df8bae1dSRodney W. Grimes if (!error) { 63954d92145SMatthew Dillon int count = size - auio.uio_resid; 640df8bae1dSRodney W. Grimes 641df8bae1dSRodney W. Grimes if (count == 0) 642df8bae1dSRodney W. Grimes error = EINVAL; 64326f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 6449e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + count, 6459e0ddbd0SAlan Cox PAGE_SIZE - count); 646df8bae1dSRodney W. Grimes } 6479e0ddbd0SAlan Cox sf_buf_free(sf); 6481b26eb10SAlan Cox 64989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 650df8bae1dSRodney W. Grimes } 6510d53a17bSAlan Cox KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m)); 6526e3a3f38SRobert V. Baron if (!error) 6536e3a3f38SRobert V. Baron m->valid = VM_PAGE_BITS_ALL; 654a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 65526f9a767SRodney W. Grimes } 65626f9a767SRodney W. Grimes 65726f9a767SRodney W. Grimes /* 65826f9a767SRodney W. Grimes * generic vnode pager input routine 65926f9a767SRodney W. Grimes */ 660170db9c6SJohn Dyson 661ce75f2c3SMike Smith /* 66223955314SAlfred Perlstein * Local media VFS's that do not implement their own VOP_GETPAGES 66347e151ddSRobert Drehmel * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 66447e151ddSRobert Drehmel * to implement the previous behaviour. 665ce75f2c3SMike Smith * 666ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 667ce75f2c3SMike Smith * backing vp's VOP_GETPAGES. 668ce75f2c3SMike Smith */ 669f708ef1bSPoul-Henning Kamp static int 6707ebba1f8SGleb Smirnoff vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage) 67124a1cce3SDavid Greenman { 672170db9c6SJohn Dyson int rtval; 673170db9c6SJohn Dyson struct vnode *vp; 67486ffbd76SMike Smith int bytes = count * PAGE_SIZE; 67595e5e988SJohn Dyson 676170db9c6SJohn Dyson vp = object->handle; 67789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 67827ad26d8SGleb Smirnoff rtval = VOP_GETPAGES(vp, m, bytes, reqpage); 67923955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 68023955314SAlfred Perlstein ("vnode_pager: FS getpages not implemented\n")); 68189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 682170db9c6SJohn Dyson return rtval; 683170db9c6SJohn Dyson } 684170db9c6SJohn Dyson 68590effb23SGleb Smirnoff static int 68690effb23SGleb Smirnoff vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count, 68790effb23SGleb Smirnoff int reqpage, vop_getpages_iodone_t iodone, void *arg) 68890effb23SGleb Smirnoff { 68990effb23SGleb Smirnoff struct vnode *vp; 69090effb23SGleb Smirnoff int rtval; 69190effb23SGleb Smirnoff 69290effb23SGleb Smirnoff vp = object->handle; 69390effb23SGleb Smirnoff VM_OBJECT_WUNLOCK(object); 694f6d6b5e2SGleb Smirnoff rtval = VOP_GETPAGES_ASYNC(vp, m, count * PAGE_SIZE, reqpage, 69590effb23SGleb Smirnoff iodone, arg); 69690effb23SGleb Smirnoff KASSERT(rtval != EOPNOTSUPP, 69790effb23SGleb Smirnoff ("vnode_pager: FS getpages_async not implemented\n")); 69890effb23SGleb Smirnoff VM_OBJECT_WLOCK(object); 69990effb23SGleb Smirnoff return (rtval); 70090effb23SGleb Smirnoff } 70190effb23SGleb Smirnoff 702ce75f2c3SMike Smith /* 70390effb23SGleb Smirnoff * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for 70490effb23SGleb Smirnoff * local filesystems, where partially valid pages can only occur at 70590effb23SGleb Smirnoff * the end of file. 706d15b55c5SKonstantin Belousov */ 707d15b55c5SKonstantin Belousov int 708d15b55c5SKonstantin Belousov vnode_pager_local_getpages(struct vop_getpages_args *ap) 709d15b55c5SKonstantin Belousov { 71090effb23SGleb Smirnoff 71190effb23SGleb Smirnoff return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count, 71290effb23SGleb Smirnoff ap->a_reqpage, NULL, NULL)); 71390effb23SGleb Smirnoff } 71490effb23SGleb Smirnoff 71590effb23SGleb Smirnoff int 71690effb23SGleb Smirnoff vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap) 71790effb23SGleb Smirnoff { 71890effb23SGleb Smirnoff 71990effb23SGleb Smirnoff return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count, 72090effb23SGleb Smirnoff ap->a_reqpage, ap->a_iodone, ap->a_arg)); 72190effb23SGleb Smirnoff } 72290effb23SGleb Smirnoff 72390effb23SGleb Smirnoff static int 72490effb23SGleb Smirnoff vnode_pager_local_getpages0(struct vnode *vp, vm_page_t *m, int bytecount, 72590effb23SGleb Smirnoff int reqpage, vop_getpages_iodone_t iodone, void *arg) 72690effb23SGleb Smirnoff { 727d15b55c5SKonstantin Belousov vm_page_t mreq; 728d15b55c5SKonstantin Belousov 72990effb23SGleb Smirnoff mreq = m[reqpage]; 730d15b55c5SKonstantin Belousov 731d15b55c5SKonstantin Belousov /* 732d15b55c5SKonstantin Belousov * Since the caller has busied the requested page, that page's valid 733d15b55c5SKonstantin Belousov * field will not be changed by other threads. 734d15b55c5SKonstantin Belousov */ 735d15b55c5SKonstantin Belousov vm_page_assert_xbusied(mreq); 736d15b55c5SKonstantin Belousov 737d15b55c5SKonstantin Belousov /* 738d15b55c5SKonstantin Belousov * The requested page has valid blocks. Invalid part can only 739d15b55c5SKonstantin Belousov * exist at the end of file, and the page is made fully valid 7402c0cb026SGleb Smirnoff * by zeroing in vm_pager_get_pages(). Free non-requested 741d15b55c5SKonstantin Belousov * pages, since no i/o is done to read its content. 742d15b55c5SKonstantin Belousov */ 743d15b55c5SKonstantin Belousov if (mreq->valid != 0) { 74490effb23SGleb Smirnoff vm_pager_free_nonreq(mreq->object, m, reqpage, 7454d6481a4SGleb Smirnoff round_page(bytecount) / PAGE_SIZE, FALSE); 74690effb23SGleb Smirnoff if (iodone != NULL) 74790effb23SGleb Smirnoff iodone(arg, m, reqpage, 0); 748d15b55c5SKonstantin Belousov return (VM_PAGER_OK); 749d15b55c5SKonstantin Belousov } 750d15b55c5SKonstantin Belousov 75190effb23SGleb Smirnoff return (vnode_pager_generic_getpages(vp, m, bytecount, reqpage, 75290effb23SGleb Smirnoff iodone, arg)); 753d15b55c5SKonstantin Belousov } 754d15b55c5SKonstantin Belousov 755d15b55c5SKonstantin Belousov /* 756ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 757ce75f2c3SMike Smith * own vnodes if they fail to implement VOP_GETPAGES. 758ce75f2c3SMike Smith */ 759ce75f2c3SMike Smith int 7607ebba1f8SGleb Smirnoff vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount, 76190effb23SGleb Smirnoff int reqpage, vop_getpages_iodone_t iodone, void *arg) 762170db9c6SJohn Dyson { 763ce75f2c3SMike Smith vm_object_t object; 76490effb23SGleb Smirnoff off_t foff; 76573e9030eSGleb Smirnoff int i, j, size, bsize, first, *freecnt; 766f4f83da0SAlan Cox daddr_t firstaddr, reqblock; 7679c83534dSPoul-Henning Kamp struct bufobj *bo; 768efc68ce1SDavid Greenman int runpg; 769efc68ce1SDavid Greenman int runend; 7700bdb7528SDavid Greenman struct buf *bp; 771ce75f2c3SMike Smith int count; 7721de11f1aSAlan Cox int error; 77326f9a767SRodney W. Grimes 774ce75f2c3SMike Smith object = vp->v_object; 775ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 776ce75f2c3SMike Smith 7779c83534dSPoul-Henning Kamp KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 7789c83534dSPoul-Henning Kamp ("vnode_pager_generic_getpages does not support devices")); 779b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 7802c4488fcSJohn Dyson return VM_PAGER_BAD; 7812c4488fcSJohn Dyson 78226f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 783a316d390SJohn Dyson foff = IDX_TO_OFF(m[reqpage]->pindex); 784bbc0ec52SDavid Greenman 78541c895a8SGleb Smirnoff /* 78641c895a8SGleb Smirnoff * Synchronous and asynchronous paging operations use different 78741c895a8SGleb Smirnoff * free pbuf counters. This is done to avoid asynchronous requests 78841c895a8SGleb Smirnoff * to consume all pbufs. 78941c895a8SGleb Smirnoff * Allocate the pbuf at the very beginning of the function, so that 79041c895a8SGleb Smirnoff * if we are low on certain kind of pbufs don't even proceed to BMAP, 79141c895a8SGleb Smirnoff * but sleep. 79241c895a8SGleb Smirnoff */ 79373e9030eSGleb Smirnoff freecnt = iodone != NULL ? 79473e9030eSGleb Smirnoff &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt; 79573e9030eSGleb Smirnoff bp = getpbuf(freecnt); 79673e9030eSGleb Smirnoff 79726f9a767SRodney W. Grimes /* 798e122dfc1SGleb Smirnoff * Get the underlying device blocks for the file with VOP_BMAP(). 799e122dfc1SGleb Smirnoff * If the file system doesn't support VOP_BMAP, use old way of 800e122dfc1SGleb Smirnoff * getting pages via VOP_READ. 80126f9a767SRodney W. Grimes */ 8021de11f1aSAlan Cox error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL); 8031de11f1aSAlan Cox if (error == EOPNOTSUPP) { 80473e9030eSGleb Smirnoff relpbuf(bp, freecnt); 80589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 806e43c2eabSAlan Cox for (i = 0; i < count; i++) 8072965a453SKip Macy if (i != reqpage) { 8082965a453SKip Macy vm_page_lock(m[i]); 809d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 8102965a453SKip Macy vm_page_unlock(m[i]); 8112965a453SKip Macy } 812b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodein); 813b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodepgsin); 81452051abcSAlan Cox error = vnode_pager_input_old(object, m[reqpage]); 81589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 81652051abcSAlan Cox return (error); 8171de11f1aSAlan Cox } else if (error != 0) { 81873e9030eSGleb Smirnoff relpbuf(bp, freecnt); 8194d6481a4SGleb Smirnoff vm_pager_free_nonreq(object, m, reqpage, count, FALSE); 8201de11f1aSAlan Cox return (VM_PAGER_ERROR); 821bbc0ec52SDavid Greenman 82226f9a767SRodney W. Grimes /* 82326f9a767SRodney W. Grimes * if the blocksize is smaller than a page size, then use 82426f9a767SRodney W. Grimes * special small filesystem code. NFS sometimes has a small 82526f9a767SRodney W. Grimes * blocksize, but it can handle large reads itself. 82626f9a767SRodney W. Grimes */ 82726f9a767SRodney W. Grimes } else if ((PAGE_SIZE / bsize) > 1 && 828500b04a2SBruce Evans (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 82973e9030eSGleb Smirnoff relpbuf(bp, freecnt); 8304d6481a4SGleb Smirnoff vm_pager_free_nonreq(object, m, reqpage, count, FALSE); 831b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodein); 832b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodepgsin); 83324a1cce3SDavid Greenman return vnode_pager_input_smlfs(object, m[reqpage]); 83426f9a767SRodney W. Grimes } 8358d17e694SJulian Elischer 83626f9a767SRodney W. Grimes /* 837a7fecb4dSAlan Cox * Since the caller has busied the requested page, that page's valid 838a7fecb4dSAlan Cox * field will not be changed by other threads. 839a7fecb4dSAlan Cox */ 840a7fecb4dSAlan Cox vm_page_assert_xbusied(m[reqpage]); 841a7fecb4dSAlan Cox 842a7fecb4dSAlan Cox /* 8438d17e694SJulian Elischer * If we have a completely valid page available to us, we can 8448d17e694SJulian Elischer * clean up and return. Otherwise we have to re-read the 8458d17e694SJulian Elischer * media. 8460d94caffSDavid Greenman */ 8478b575f6cSAlan Cox if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 84873e9030eSGleb Smirnoff relpbuf(bp, freecnt); 8494d6481a4SGleb Smirnoff vm_pager_free_nonreq(object, m, reqpage, count, FALSE); 850a7fecb4dSAlan Cox return (VM_PAGER_OK); 851f4f83da0SAlan Cox } else if (reqblock == -1) { 85273e9030eSGleb Smirnoff relpbuf(bp, freecnt); 853f4f83da0SAlan Cox pmap_zero_page(m[reqpage]); 85412aa4fdcSAlan Cox KASSERT(m[reqpage]->dirty == 0, 85512aa4fdcSAlan Cox ("vnode_pager_generic_getpages: page %p is dirty", m)); 856a7fecb4dSAlan Cox VM_OBJECT_WLOCK(object); 857f4f83da0SAlan Cox m[reqpage]->valid = VM_PAGE_BITS_ALL; 8584d6481a4SGleb Smirnoff vm_pager_free_nonreq(object, m, reqpage, count, TRUE); 85989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 860f4f83da0SAlan Cox return (VM_PAGER_OK); 861a7fecb4dSAlan Cox } else if (m[reqpage]->valid != 0) { 862a7fecb4dSAlan Cox VM_OBJECT_WLOCK(object); 8638d17e694SJulian Elischer m[reqpage]->valid = 0; 86489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 865a7fecb4dSAlan Cox } 8660bdb7528SDavid Greenman 8670d94caffSDavid Greenman /* 86826f9a767SRodney W. Grimes * here on direct device I/O 86926f9a767SRodney W. Grimes */ 870efc68ce1SDavid Greenman firstaddr = -1; 871a1287949SEivind Eklund 87226f9a767SRodney W. Grimes /* 873efc68ce1SDavid Greenman * calculate the run that includes the required page 87426f9a767SRodney W. Grimes */ 875efc68ce1SDavid Greenman for (first = 0, i = 0; i < count; i = runend) { 876bff76343SAlan Cox if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr, 877bff76343SAlan Cox &runpg) != 0) { 87873e9030eSGleb Smirnoff relpbuf(bp, freecnt); 8794d6481a4SGleb Smirnoff /* The requested page may be out of range. */ 8804d6481a4SGleb Smirnoff vm_pager_free_nonreq(object, m + i, reqpage - i, 8814d6481a4SGleb Smirnoff count - i, FALSE); 882bff76343SAlan Cox return (VM_PAGER_ERROR); 883bff76343SAlan Cox } 884efc68ce1SDavid Greenman if (firstaddr == -1) { 88589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 88624a1cce3SDavid Greenman if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 887f3aad9a6SBjoern A. Zeeb panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx", 888f3aad9a6SBjoern A. Zeeb (intmax_t)firstaddr, (uintmax_t)(foff >> 32), 889bf1001faSMaxime Henrion (uintmax_t)foff, 890bf1001faSMaxime Henrion (uintmax_t) 891fc62ef1fSBruce Evans (object->un_pager.vnp.vnp_size >> 32), 892bf1001faSMaxime Henrion (uintmax_t)object->un_pager.vnp.vnp_size); 893efc68ce1SDavid Greenman } 8942965a453SKip Macy vm_page_lock(m[i]); 895d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 8962965a453SKip Macy vm_page_unlock(m[i]); 89789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 898efc68ce1SDavid Greenman runend = i + 1; 899efc68ce1SDavid Greenman first = runend; 900efc68ce1SDavid Greenman continue; 901efc68ce1SDavid Greenman } 902efc68ce1SDavid Greenman runend = i + runpg; 903efc68ce1SDavid Greenman if (runend <= reqpage) { 90489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 9052965a453SKip Macy for (j = i; j < runend; j++) { 9062965a453SKip Macy vm_page_lock(m[j]); 907d8d5fa88SAlfred Perlstein vm_page_free(m[j]); 9082965a453SKip Macy vm_page_unlock(m[j]); 9092965a453SKip Macy } 91089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 91126f9a767SRodney W. Grimes } else { 912efc68ce1SDavid Greenman if (runpg < (count - first)) { 91389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 9142965a453SKip Macy for (i = first + runpg; i < count; i++) { 9152965a453SKip Macy vm_page_lock(m[i]); 916d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 9172965a453SKip Macy vm_page_unlock(m[i]); 9182965a453SKip Macy } 91989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 920efc68ce1SDavid Greenman count = first + runpg; 92126f9a767SRodney W. Grimes } 922efc68ce1SDavid Greenman break; 92326f9a767SRodney W. Grimes } 924efc68ce1SDavid Greenman first = runend; 925efc68ce1SDavid Greenman } 92626f9a767SRodney W. Grimes 92726f9a767SRodney W. Grimes /* 928bbc0ec52SDavid Greenman * the first and last page have been calculated now, move input pages 929bbc0ec52SDavid Greenman * to be zero based... 93026f9a767SRodney W. Grimes */ 93126f9a767SRodney W. Grimes if (first != 0) { 9327e2393ffSAlan Cox m += first; 93326f9a767SRodney W. Grimes count -= first; 93426f9a767SRodney W. Grimes reqpage -= first; 93526f9a767SRodney W. Grimes } 936efc68ce1SDavid Greenman 93726f9a767SRodney W. Grimes /* 93826f9a767SRodney W. Grimes * calculate the file virtual address for the transfer 93926f9a767SRodney W. Grimes */ 940a316d390SJohn Dyson foff = IDX_TO_OFF(m[0]->pindex); 94126f9a767SRodney W. Grimes 94226f9a767SRodney W. Grimes /* 94326f9a767SRodney W. Grimes * calculate the size of the transfer 94426f9a767SRodney W. Grimes */ 94526f9a767SRodney W. Grimes size = count * PAGE_SIZE; 9461a31a6c3SPoul-Henning Kamp KASSERT(count > 0, ("zero count")); 94724a1cce3SDavid Greenman if ((foff + size) > object->un_pager.vnp.vnp_size) 94824a1cce3SDavid Greenman size = object->un_pager.vnp.vnp_size - foff; 9491a31a6c3SPoul-Henning Kamp KASSERT(size > 0, ("zero size")); 95026f9a767SRodney W. Grimes 95126f9a767SRodney W. Grimes /* 95224579ca1SMatthew Dillon * round up physical size for real devices. 95326f9a767SRodney W. Grimes */ 9549c83534dSPoul-Henning Kamp if (1) { 9559c83534dSPoul-Henning Kamp int secmask = bo->bo_bsize - 1; 9566229cc50SPoul-Henning Kamp KASSERT(secmask < PAGE_SIZE && secmask > 0, 9576229cc50SPoul-Henning Kamp ("vnode_pager_generic_getpages: sector size %d too large", 9586229cc50SPoul-Henning Kamp secmask + 1)); 95924579ca1SMatthew Dillon size = (size + secmask) & ~secmask; 96024579ca1SMatthew Dillon } 96126f9a767SRodney W. Grimes 96290effb23SGleb Smirnoff bp->b_kvaalloc = bp->b_data; 96316f62314SDavid Greenman 96426f9a767SRodney W. Grimes /* 9656ce697dcSKonstantin Belousov * and map the pages to be read into the kva, if the filesystem 9666ce697dcSKonstantin Belousov * requires mapped buffers. 96726f9a767SRodney W. Grimes */ 9682a5eef69SGleb Smirnoff if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 && 9696ce697dcSKonstantin Belousov unmapped_buf_allowed) { 9706ce697dcSKonstantin Belousov bp->b_data = unmapped_buf; 9716ce697dcSKonstantin Belousov bp->b_kvabase = unmapped_buf; 9726ce697dcSKonstantin Belousov bp->b_offset = 0; 9736ce697dcSKonstantin Belousov bp->b_flags |= B_UNMAPPED; 9746ce697dcSKonstantin Belousov } else 97590effb23SGleb Smirnoff pmap_qenter((vm_offset_t)bp->b_kvaalloc, m, count); 97626f9a767SRodney W. Grimes 97726f9a767SRodney W. Grimes /* build a minimal buffer header */ 97821144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 979bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 980bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 981a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 982a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 983187f0071SDavid Greenman bp->b_blkno = firstaddr; 9849c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 9851faacf5dSKirk McKusick bp->b_vp = vp; 98626f9a767SRodney W. Grimes bp->b_bcount = size; 98726f9a767SRodney W. Grimes bp->b_bufsize = size; 9882b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 98990effb23SGleb Smirnoff for (i = 0; i < count; i++) 99090effb23SGleb Smirnoff bp->b_pages[i] = m[i]; 99190effb23SGleb Smirnoff bp->b_npages = count; 99290effb23SGleb Smirnoff bp->b_pager.pg_reqpage = reqpage; 9935bd65606SJohn Baldwin atomic_add_long(&runningbufspace, bp->b_runningbufspace); 99426f9a767SRodney W. Grimes 995b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodein); 996b4b70819SAttilio Rao PCPU_ADD(cnt.v_vnodepgsin, count); 997976e77fcSDavid Greenman 99826f9a767SRodney W. Grimes /* do the input */ 9992c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 100090effb23SGleb Smirnoff 100190effb23SGleb Smirnoff if (iodone != NULL) { /* async */ 100290effb23SGleb Smirnoff bp->b_pager.pg_iodone = iodone; 100390effb23SGleb Smirnoff bp->b_caller1 = arg; 100490effb23SGleb Smirnoff bp->b_iodone = vnode_pager_generic_getpages_done_async; 100590effb23SGleb Smirnoff bp->b_flags |= B_ASYNC; 100690effb23SGleb Smirnoff BUF_KERNPROC(bp); 1007b792bebeSPoul-Henning Kamp bstrategy(bp); 100890effb23SGleb Smirnoff /* Good bye! */ 100990effb23SGleb Smirnoff } else { 101090effb23SGleb Smirnoff bp->b_iodone = bdone; 101190effb23SGleb Smirnoff bstrategy(bp); 10126a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnread"); 101390effb23SGleb Smirnoff error = vnode_pager_generic_getpages_done(bp); 10141bb5ad63SGleb Smirnoff for (i = 0; i < bp->b_npages; i++) 10156ce697dcSKonstantin Belousov bp->b_pages[i] = NULL; 10161faacf5dSKirk McKusick bp->b_vp = NULL; 10179c83534dSPoul-Henning Kamp pbrelbo(bp); 10181c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 101990effb23SGleb Smirnoff } 102090effb23SGleb Smirnoff 102190effb23SGleb Smirnoff return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK); 102290effb23SGleb Smirnoff } 102390effb23SGleb Smirnoff 102490effb23SGleb Smirnoff static void 102590effb23SGleb Smirnoff vnode_pager_generic_getpages_done_async(struct buf *bp) 102690effb23SGleb Smirnoff { 102790effb23SGleb Smirnoff int error; 102890effb23SGleb Smirnoff 102990effb23SGleb Smirnoff error = vnode_pager_generic_getpages_done(bp); 103090effb23SGleb Smirnoff bp->b_pager.pg_iodone(bp->b_caller1, bp->b_pages, 103190effb23SGleb Smirnoff bp->b_pager.pg_reqpage, error); 103290effb23SGleb Smirnoff for (int i = 0; i < bp->b_npages; i++) 103390effb23SGleb Smirnoff bp->b_pages[i] = NULL; 103490effb23SGleb Smirnoff bp->b_vp = NULL; 103590effb23SGleb Smirnoff pbrelbo(bp); 103673e9030eSGleb Smirnoff relpbuf(bp, &vnode_async_pbuf_freecnt); 103790effb23SGleb Smirnoff } 103890effb23SGleb Smirnoff 103990effb23SGleb Smirnoff static int 104090effb23SGleb Smirnoff vnode_pager_generic_getpages_done(struct buf *bp) 104190effb23SGleb Smirnoff { 104290effb23SGleb Smirnoff vm_object_t object; 104390effb23SGleb Smirnoff off_t tfoff, nextoff; 104490effb23SGleb Smirnoff int i, error; 104590effb23SGleb Smirnoff 104690effb23SGleb Smirnoff error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0; 104790effb23SGleb Smirnoff object = bp->b_vp->v_object; 104890effb23SGleb Smirnoff 104990effb23SGleb Smirnoff if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) { 105090effb23SGleb Smirnoff if ((bp->b_flags & B_UNMAPPED) != 0) { 105190effb23SGleb Smirnoff bp->b_flags &= ~B_UNMAPPED; 105290effb23SGleb Smirnoff pmap_qenter((vm_offset_t)bp->b_kvaalloc, bp->b_pages, 105390effb23SGleb Smirnoff bp->b_npages); 105490effb23SGleb Smirnoff } 105590effb23SGleb Smirnoff bzero(bp->b_kvaalloc + bp->b_bcount, 105690effb23SGleb Smirnoff PAGE_SIZE * bp->b_npages - bp->b_bcount); 105790effb23SGleb Smirnoff } 105890effb23SGleb Smirnoff if ((bp->b_flags & B_UNMAPPED) == 0) 105990effb23SGleb Smirnoff pmap_qremove((vm_offset_t)bp->b_kvaalloc, bp->b_npages); 106090effb23SGleb Smirnoff if ((bp->b_vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0) { 106190effb23SGleb Smirnoff bp->b_data = bp->b_kvaalloc; 106290effb23SGleb Smirnoff bp->b_kvabase = bp->b_kvaalloc; 106390effb23SGleb Smirnoff bp->b_flags &= ~B_UNMAPPED; 106490effb23SGleb Smirnoff } 106526f9a767SRodney W. Grimes 106689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 106790effb23SGleb Smirnoff for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex); 106890effb23SGleb Smirnoff i < bp->b_npages; i++, tfoff = nextoff) { 10698f9110f6SJohn Dyson vm_page_t mt; 10708f9110f6SJohn Dyson 10718f9110f6SJohn Dyson nextoff = tfoff + PAGE_SIZE; 107290effb23SGleb Smirnoff mt = bp->b_pages[i]; 10738f9110f6SJohn Dyson 107454746b67SDmitrij Tejblum if (nextoff <= object->un_pager.vnp.vnp_size) { 10758d17e694SJulian Elischer /* 10768d17e694SJulian Elischer * Read filled up entire page. 10778d17e694SJulian Elischer */ 10788f9110f6SJohn Dyson mt->valid = VM_PAGE_BITS_ALL; 1079016a3c93SAlan Cox KASSERT(mt->dirty == 0, 108079f0deb9SGleb Smirnoff ("%s: page %p is dirty", __func__, mt)); 1081016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(mt), 108279f0deb9SGleb Smirnoff ("%s: page %p is mapped", __func__, mt)); 10838f9110f6SJohn Dyson } else { 10848d17e694SJulian Elischer /* 108542eb4108SAlan Cox * Read did not fill up entire page. 10868d17e694SJulian Elischer * 10878d17e694SJulian Elischer * Currently we do not set the entire page valid, 10888d17e694SJulian Elischer * we just try to clear the piece that we couldn't 10898d17e694SJulian Elischer * read. 10908d17e694SJulian Elischer */ 1091dc874f98SKonstantin Belousov vm_page_set_valid_range(mt, 0, 109254746b67SDmitrij Tejblum object->un_pager.vnp.vnp_size - tfoff); 109342eb4108SAlan Cox KASSERT((mt->dirty & vm_page_bits(0, 109442eb4108SAlan Cox object->un_pager.vnp.vnp_size - tfoff)) == 0, 109579f0deb9SGleb Smirnoff ("%s: page %p is dirty", __func__, mt)); 10968f9110f6SJohn Dyson } 10978f9110f6SJohn Dyson 109890effb23SGleb Smirnoff if (i != bp->b_pager.pg_reqpage) 1099b6c00483SKonstantin Belousov vm_page_readahead_finish(mt); 110003679e23SAlan Cox } 110189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 110290effb23SGleb Smirnoff if (error != 0) 110390effb23SGleb Smirnoff printf("%s: I/O read error %d\n", __func__, error); 110490effb23SGleb Smirnoff 110590effb23SGleb Smirnoff return (error); 110626f9a767SRodney W. Grimes } 110726f9a767SRodney W. Grimes 1108ce75f2c3SMike Smith /* 1109ce75f2c3SMike Smith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 1110ce75f2c3SMike Smith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 1111ce75f2c3SMike Smith * vnode_pager_generic_putpages() to implement the previous behaviour. 1112ce75f2c3SMike Smith * 1113ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 1114ce75f2c3SMike Smith * backing vp's VOP_PUTPAGES. 1115ce75f2c3SMike Smith */ 1116e4542174SMatthew Dillon static void 11177ebba1f8SGleb Smirnoff vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count, 111833cad9e9SKonstantin Belousov int flags, int *rtvals) 1119170db9c6SJohn Dyson { 1120170db9c6SJohn Dyson int rtval; 1121170db9c6SJohn Dyson struct vnode *vp; 112286ffbd76SMike Smith int bytes = count * PAGE_SIZE; 1123ad980522SJohn Dyson 11240e3cdf2cSAlan Cox /* 11250e3cdf2cSAlan Cox * Force synchronous operation if we are extremely low on memory 11260e3cdf2cSAlan Cox * to prevent a low-memory deadlock. VOP operations often need to 11270e3cdf2cSAlan Cox * allocate more memory to initiate the I/O ( i.e. do a BMAP 11280e3cdf2cSAlan Cox * operation ). The swapper handles the case by limiting the amount 11290e3cdf2cSAlan Cox * of asynchronous I/O, but that sort of solution doesn't scale well 11300e3cdf2cSAlan Cox * for the vnode pager without a lot of work. 11310e3cdf2cSAlan Cox * 11320e3cdf2cSAlan Cox * Also, the backing vnode's iodone routine may not wake the pageout 11330e3cdf2cSAlan Cox * daemon up. This should be probably be addressed XXX. 11340e3cdf2cSAlan Cox */ 11350e3cdf2cSAlan Cox 113633cad9e9SKonstantin Belousov if (vm_cnt.v_free_count + vm_cnt.v_cache_count < 113744f1c916SBryan Drewery vm_cnt.v_pageout_free_min) 113833cad9e9SKonstantin Belousov flags |= VM_PAGER_PUT_SYNC; 11390e3cdf2cSAlan Cox 11400e3cdf2cSAlan Cox /* 11410e3cdf2cSAlan Cox * Call device-specific putpages function 11420e3cdf2cSAlan Cox */ 1143170db9c6SJohn Dyson vp = object->handle; 114489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 114533cad9e9SKonstantin Belousov rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals); 114623955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 114723955314SAlfred Perlstein ("vnode_pager: stale FS putpages\n")); 114889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1149170db9c6SJohn Dyson } 1150170db9c6SJohn Dyson 1151ce75f2c3SMike Smith 115226f9a767SRodney W. Grimes /* 1153ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 11544491ea91SEivind Eklund * own vnodes if they fail to implement VOP_PUTPAGES. 11552b6b0df7SMatthew Dillon * 11562b6b0df7SMatthew Dillon * This is typically called indirectly via the pageout daemon and 11572b6b0df7SMatthew Dillon * clustering has already typically occured, so in general we ask the 11582b6b0df7SMatthew Dillon * underlying filesystem to write the data out asynchronously rather 11592b6b0df7SMatthew Dillon * then delayed. 116026f9a767SRodney W. Grimes */ 1161ce75f2c3SMike Smith int 1162c46b90e9SAlan Cox vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount, 1163c46b90e9SAlan Cox int flags, int *rtvals) 116426f9a767SRodney W. Grimes { 1165f6b04d2bSDavid Greenman int i; 1166ce75f2c3SMike Smith vm_object_t object; 1167c46b90e9SAlan Cox vm_page_t m; 1168ce75f2c3SMike Smith int count; 116926f9a767SRodney W. Grimes 1170f6b04d2bSDavid Greenman int maxsize, ncount; 1171a316d390SJohn Dyson vm_ooffset_t poffset; 1172f6b04d2bSDavid Greenman struct uio auio; 1173f6b04d2bSDavid Greenman struct iovec aiov; 1174f6b04d2bSDavid Greenman int error; 11758f9110f6SJohn Dyson int ioflags; 1176dd498befSPaul Saab int ppscheck = 0; 1177dd498befSPaul Saab static struct timeval lastfail; 1178dd498befSPaul Saab static int curfail; 117926f9a767SRodney W. Grimes 1180ce75f2c3SMike Smith object = vp->v_object; 1181ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 1182ce75f2c3SMike Smith 118326f9a767SRodney W. Grimes for (i = 0; i < count; i++) 1184031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_ERROR; 118526f9a767SRodney W. Grimes 1186c46b90e9SAlan Cox if ((int64_t)ma[0]->pindex < 0) { 118723562e4bSMarcel Moolenaar printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n", 1188c46b90e9SAlan Cox (long)ma[0]->pindex, (u_long)ma[0]->dirty); 1189f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 1190f6b04d2bSDavid Greenman return VM_PAGER_BAD; 11910d94caffSDavid Greenman } 11920bdb7528SDavid Greenman 1193f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 1194f6b04d2bSDavid Greenman ncount = count; 119526f9a767SRodney W. Grimes 1196c46b90e9SAlan Cox poffset = IDX_TO_OFF(ma[0]->pindex); 119700a6f47fSMatthew Dillon 119800a6f47fSMatthew Dillon /* 119900a6f47fSMatthew Dillon * If the page-aligned write is larger then the actual file we 120000a6f47fSMatthew Dillon * have to invalidate pages occuring beyond the file EOF. However, 120100a6f47fSMatthew Dillon * there is an edge case where a file may not be page-aligned where 120200a6f47fSMatthew Dillon * the last page is partially invalid. In this case the filesystem 120300a6f47fSMatthew Dillon * may not properly clear the dirty bits for the entire page (which 120400a6f47fSMatthew Dillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 120500a6f47fSMatthew Dillon * With the page locked we are free to fix-up the dirty bits here. 12063ebeaf59SMatthew Dillon * 12073ebeaf59SMatthew Dillon * We do not under any circumstances truncate the valid bits, as 12083ebeaf59SMatthew Dillon * this will screw up bogus page replacement. 120900a6f47fSMatthew Dillon */ 121089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1211a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 121200a6f47fSMatthew Dillon if (object->un_pager.vnp.vnp_size > poffset) { 121300a6f47fSMatthew Dillon int pgoff; 121400a6f47fSMatthew Dillon 1215a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 1216aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 121700a6f47fSMatthew Dillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1218c46b90e9SAlan Cox /* 1219c46b90e9SAlan Cox * If the object is locked and the following 1220c46b90e9SAlan Cox * conditions hold, then the page's dirty 1221c46b90e9SAlan Cox * field cannot be concurrently changed by a 1222c46b90e9SAlan Cox * pmap operation. 1223c46b90e9SAlan Cox */ 1224c46b90e9SAlan Cox m = ma[ncount - 1]; 1225c7aebda8SAttilio Rao vm_page_assert_sbusied(m); 12266031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1227c46b90e9SAlan Cox ("vnode_pager_generic_putpages: page %p is not read-only", m)); 1228c46b90e9SAlan Cox vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 1229c46b90e9SAlan Cox pgoff); 123000a6f47fSMatthew Dillon } 123100a6f47fSMatthew Dillon } else { 123200a6f47fSMatthew Dillon maxsize = 0; 123300a6f47fSMatthew Dillon ncount = 0; 123400a6f47fSMatthew Dillon } 1235f6b04d2bSDavid Greenman if (ncount < count) { 1236f6b04d2bSDavid Greenman for (i = ncount; i < count; i++) { 1237f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 1238f6b04d2bSDavid Greenman } 1239f6b04d2bSDavid Greenman } 1240f6b04d2bSDavid Greenman } 124189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 124226f9a767SRodney W. Grimes 12432b6b0df7SMatthew Dillon /* 12442b6b0df7SMatthew Dillon * pageouts are already clustered, use IO_ASYNC to force a bawrite() 12452b6b0df7SMatthew Dillon * rather then a bdwrite() to prevent paging I/O from saturating 124643b7990eSMatthew Dillon * the buffer cache. Dummy-up the sequential heuristic to cause 124743b7990eSMatthew Dillon * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 124843b7990eSMatthew Dillon * the system decides how to cluster. 12492b6b0df7SMatthew Dillon */ 12508f9110f6SJohn Dyson ioflags = IO_VMIO; 125143b7990eSMatthew Dillon if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 125243b7990eSMatthew Dillon ioflags |= IO_SYNC; 125343b7990eSMatthew Dillon else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 125443b7990eSMatthew Dillon ioflags |= IO_ASYNC; 12558f9110f6SJohn Dyson ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 125643b7990eSMatthew Dillon ioflags |= IO_SEQMAX << IO_SEQSHIFT; 1257f6b04d2bSDavid Greenman 1258f6b04d2bSDavid Greenman aiov.iov_base = (caddr_t) 0; 1259f6b04d2bSDavid Greenman aiov.iov_len = maxsize; 1260f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 1261f6b04d2bSDavid Greenman auio.uio_iovcnt = 1; 1262a316d390SJohn Dyson auio.uio_offset = poffset; 1263f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 1264f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 1265f6b04d2bSDavid Greenman auio.uio_resid = maxsize; 1266b40ce416SJulian Elischer auio.uio_td = (struct thread *) 0; 1267a854ed98SJohn Baldwin error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1268b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodeout); 1269b4b70819SAttilio Rao PCPU_ADD(cnt.v_vnodepgsout, ncount); 1270f6b04d2bSDavid Greenman 1271f6b04d2bSDavid Greenman if (error) { 1272dd498befSPaul Saab if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1))) 127324a1cce3SDavid Greenman printf("vnode_pager_putpages: I/O error %d\n", error); 1274f6b04d2bSDavid Greenman } 1275f6b04d2bSDavid Greenman if (auio.uio_resid) { 1276dd498befSPaul Saab if (ppscheck || ppsratecheck(&lastfail, &curfail, 1)) 12779f80ce04SKonstantin Belousov printf("vnode_pager_putpages: residual I/O %zd at %lu\n", 1278c46b90e9SAlan Cox auio.uio_resid, (u_long)ma[0]->pindex); 127926f9a767SRodney W. Grimes } 1280ffc82b0aSJohn Dyson for (i = 0; i < ncount; i++) { 128126f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 128226f9a767SRodney W. Grimes } 1283f6b04d2bSDavid Greenman return rtvals[0]; 128426f9a767SRodney W. Grimes } 1285031ec8c1SKonstantin Belousov 1286031ec8c1SKonstantin Belousov void 1287031ec8c1SKonstantin Belousov vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written) 1288031ec8c1SKonstantin Belousov { 12899d17da3bSKonstantin Belousov vm_object_t obj; 1290031ec8c1SKonstantin Belousov int i, pos; 1291031ec8c1SKonstantin Belousov 12929d17da3bSKonstantin Belousov if (written == 0) 12939d17da3bSKonstantin Belousov return; 12949d17da3bSKonstantin Belousov obj = ma[0]->object; 129589f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 1296031ec8c1SKonstantin Belousov for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) { 1297031ec8c1SKonstantin Belousov if (pos < trunc_page(written)) { 1298031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_OK; 1299031ec8c1SKonstantin Belousov vm_page_undirty(ma[i]); 1300031ec8c1SKonstantin Belousov } else { 1301031ec8c1SKonstantin Belousov /* Partially written page. */ 1302031ec8c1SKonstantin Belousov rtvals[i] = VM_PAGER_AGAIN; 1303031ec8c1SKonstantin Belousov vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK); 1304031ec8c1SKonstantin Belousov } 1305031ec8c1SKonstantin Belousov } 130689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 1307031ec8c1SKonstantin Belousov } 130884110e7eSKonstantin Belousov 130984110e7eSKonstantin Belousov void 131084110e7eSKonstantin Belousov vnode_pager_update_writecount(vm_object_t object, vm_offset_t start, 131184110e7eSKonstantin Belousov vm_offset_t end) 131284110e7eSKonstantin Belousov { 131384110e7eSKonstantin Belousov struct vnode *vp; 131484110e7eSKonstantin Belousov vm_ooffset_t old_wm; 131584110e7eSKonstantin Belousov 131689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 131784110e7eSKonstantin Belousov if (object->type != OBJT_VNODE) { 131889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 131984110e7eSKonstantin Belousov return; 132084110e7eSKonstantin Belousov } 132184110e7eSKonstantin Belousov old_wm = object->un_pager.vnp.writemappings; 132284110e7eSKonstantin Belousov object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start; 132384110e7eSKonstantin Belousov vp = object->handle; 132484110e7eSKonstantin Belousov if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) { 132584110e7eSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "v_writecount inc"); 1326140dedb8SKonstantin Belousov VOP_ADD_WRITECOUNT(vp, 1); 1327b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 1328b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 132984110e7eSKonstantin Belousov } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) { 133084110e7eSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "v_writecount dec"); 1331140dedb8SKonstantin Belousov VOP_ADD_WRITECOUNT(vp, -1); 1332b47f6241SJohn Baldwin CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 1333b47f6241SJohn Baldwin __func__, vp, vp->v_writecount); 133484110e7eSKonstantin Belousov } 133589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 133684110e7eSKonstantin Belousov } 133784110e7eSKonstantin Belousov 133884110e7eSKonstantin Belousov void 133984110e7eSKonstantin Belousov vnode_pager_release_writecount(vm_object_t object, vm_offset_t start, 134084110e7eSKonstantin Belousov vm_offset_t end) 134184110e7eSKonstantin Belousov { 134284110e7eSKonstantin Belousov struct vnode *vp; 134384110e7eSKonstantin Belousov struct mount *mp; 134484110e7eSKonstantin Belousov vm_offset_t inc; 134584110e7eSKonstantin Belousov 134689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 134784110e7eSKonstantin Belousov 134884110e7eSKonstantin Belousov /* 134984110e7eSKonstantin Belousov * First, recheck the object type to account for the race when 135084110e7eSKonstantin Belousov * the vnode is reclaimed. 135184110e7eSKonstantin Belousov */ 135284110e7eSKonstantin Belousov if (object->type != OBJT_VNODE) { 135389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 135484110e7eSKonstantin Belousov return; 135584110e7eSKonstantin Belousov } 135684110e7eSKonstantin Belousov 135784110e7eSKonstantin Belousov /* 135884110e7eSKonstantin Belousov * Optimize for the case when writemappings is not going to 135984110e7eSKonstantin Belousov * zero. 136084110e7eSKonstantin Belousov */ 136184110e7eSKonstantin Belousov inc = end - start; 136284110e7eSKonstantin Belousov if (object->un_pager.vnp.writemappings != inc) { 136384110e7eSKonstantin Belousov object->un_pager.vnp.writemappings -= inc; 136489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 136584110e7eSKonstantin Belousov return; 136684110e7eSKonstantin Belousov } 136784110e7eSKonstantin Belousov 136884110e7eSKonstantin Belousov vp = object->handle; 136984110e7eSKonstantin Belousov vhold(vp); 137089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 137184110e7eSKonstantin Belousov mp = NULL; 137284110e7eSKonstantin Belousov vn_start_write(vp, &mp, V_WAIT); 137384110e7eSKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 137484110e7eSKonstantin Belousov 137584110e7eSKonstantin Belousov /* 137684110e7eSKonstantin Belousov * Decrement the object's writemappings, by swapping the start 137784110e7eSKonstantin Belousov * and end arguments for vnode_pager_update_writecount(). If 137884110e7eSKonstantin Belousov * there was not a race with vnode reclaimation, then the 137984110e7eSKonstantin Belousov * vnode's v_writecount is decremented. 138084110e7eSKonstantin Belousov */ 138184110e7eSKonstantin Belousov vnode_pager_update_writecount(object, end, start); 138284110e7eSKonstantin Belousov VOP_UNLOCK(vp, 0); 138384110e7eSKonstantin Belousov vdrop(vp); 138484110e7eSKonstantin Belousov if (mp != NULL) 138584110e7eSKonstantin Belousov vn_finished_write(mp); 138684110e7eSKonstantin Belousov } 1387