vnode_pager.c revision 193303
1139825Simp/*- 21541Srgrimes * Copyright (c) 1990 University of Utah. 31549Srgrimes * Copyright (c) 1991 The Regents of the University of California. 41549Srgrimes * All rights reserved. 59507Sdg * Copyright (c) 1993, 1994 John S. Dyson 69507Sdg * Copyright (c) 1995, David Greenman 71541Srgrimes * 81541Srgrimes * This code is derived from software contributed to Berkeley by 91541Srgrimes * the Systems Programming Group of the University of Utah Computer 101541Srgrimes * Science Department. 111541Srgrimes * 121541Srgrimes * Redistribution and use in source and binary forms, with or without 131541Srgrimes * modification, are permitted provided that the following conditions 141541Srgrimes * are met: 151541Srgrimes * 1. Redistributions of source code must retain the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer. 171541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 181541Srgrimes * notice, this list of conditions and the following disclaimer in the 191541Srgrimes * documentation and/or other materials provided with the distribution. 201541Srgrimes * 3. All advertising materials mentioning features or use of this software 2158705Scharnier * must display the following acknowledgement: 221541Srgrimes * This product includes software developed by the University of 231541Srgrimes * California, Berkeley and its contributors. 241541Srgrimes * 4. Neither the name of the University nor the names of its contributors 251541Srgrimes * may be used to endorse or promote products derived from this software 261541Srgrimes * without specific prior written permission. 271541Srgrimes * 281541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 291541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 301541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 311541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 321541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 331541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 341541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 351541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 361541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 371541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 381541Srgrimes * SUCH DAMAGE. 391541Srgrimes * 401549Srgrimes * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91 411541Srgrimes */ 421541Srgrimes 431541Srgrimes/* 441541Srgrimes * Page to/from files (vnodes). 451541Srgrimes */ 461541Srgrimes 471549Srgrimes/* 481549Srgrimes * TODO: 499507Sdg * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 507695Sdg * greatly re-simplify the vnode_pager. 511549Srgrimes */ 521549Srgrimes 53116226Sobrien#include <sys/cdefs.h> 54116226Sobrien__FBSDID("$FreeBSD: head/sys/vm/vnode_pager.c 193303 2009-06-02 08:02:27Z alc $"); 55116226Sobrien 561541Srgrimes#include <sys/param.h> 571541Srgrimes#include <sys/systm.h> 581541Srgrimes#include <sys/proc.h> 591541Srgrimes#include <sys/vnode.h> 601541Srgrimes#include <sys/mount.h> 6160041Sphk#include <sys/bio.h> 629507Sdg#include <sys/buf.h> 6312662Sdg#include <sys/vmmeter.h> 64140767Sphk#include <sys/limits.h> 6551340Sdillon#include <sys/conf.h> 66127926Salc#include <sys/sf_buf.h> 671541Srgrimes 68148875Sssouhlal#include <machine/atomic.h> 69148875Sssouhlal 701541Srgrimes#include <vm/vm.h> 7112662Sdg#include <vm/vm_object.h> 721541Srgrimes#include <vm/vm_page.h> 739507Sdg#include <vm/vm_pager.h> 7431853Sdyson#include <vm/vm_map.h> 751541Srgrimes#include <vm/vnode_pager.h> 7612662Sdg#include <vm/vm_extern.h> 771541Srgrimes 78163359Salcstatic int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 79163359Salc daddr_t *rtaddress, int *run); 8092727Salfredstatic int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 8192727Salfredstatic int vnode_pager_input_old(vm_object_t object, vm_page_t m); 8292727Salfredstatic void vnode_pager_dealloc(vm_object_t); 8392727Salfredstatic int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int); 8492727Salfredstatic void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 8592727Salfredstatic boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 86140767Sphkstatic vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t); 8711943Sbde 881541Srgrimesstruct pagerops vnodepagerops = { 89118466Sphk .pgo_alloc = vnode_pager_alloc, 90118466Sphk .pgo_dealloc = vnode_pager_dealloc, 91118466Sphk .pgo_getpages = vnode_pager_getpages, 92118466Sphk .pgo_putpages = vnode_pager_putpages, 93118466Sphk .pgo_haspage = vnode_pager_haspage, 941541Srgrimes}; 951541Srgrimes 9679127Sjhbint vnode_pbuf_freecnt; 9710556Sdyson 98140767Sphk/* Create the VM system backing object for this vnode */ 99140767Sphkint 100155177Syarvnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td) 101140767Sphk{ 102140767Sphk vm_object_t object; 103140767Sphk vm_ooffset_t size = isize; 104140767Sphk struct vattr va; 105140767Sphk 106140767Sphk if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE) 107140767Sphk return (0); 108140767Sphk 109140767Sphk while ((object = vp->v_object) != NULL) { 110140767Sphk VM_OBJECT_LOCK(object); 111140767Sphk if (!(object->flags & OBJ_DEAD)) { 112140767Sphk VM_OBJECT_UNLOCK(object); 113140767Sphk return (0); 114140767Sphk } 115175294Sattilio VOP_UNLOCK(vp, 0); 116140767Sphk vm_object_set_flag(object, OBJ_DISCONNECTWNT); 117140767Sphk msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0); 118175202Sattilio vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 119140767Sphk } 120140767Sphk 121140767Sphk if (size == 0) { 122140767Sphk if (vn_isdisk(vp, NULL)) { 123140767Sphk size = IDX_TO_OFF(INT_MAX); 124140767Sphk } else { 125182371Sattilio if (VOP_GETATTR(vp, &va, td->td_ucred)) 126140767Sphk return (0); 127140767Sphk size = va.va_size; 128140767Sphk } 129140767Sphk } 130140767Sphk 131140767Sphk object = vnode_pager_alloc(vp, size, 0, 0); 132140767Sphk /* 133140767Sphk * Dereference the reference we just created. This assumes 134140767Sphk * that the object is associated with the vp. 135140767Sphk */ 136140767Sphk VM_OBJECT_LOCK(object); 137140767Sphk object->ref_count--; 138140767Sphk VM_OBJECT_UNLOCK(object); 139140767Sphk vrele(vp); 140140767Sphk 141140767Sphk KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object")); 142140767Sphk 143140767Sphk return (0); 144140767Sphk} 145140767Sphk 146140929Sphkvoid 147140929Sphkvnode_destroy_vobject(struct vnode *vp) 148140929Sphk{ 149140929Sphk struct vm_object *obj; 150140929Sphk 151140929Sphk obj = vp->v_object; 152140929Sphk if (obj == NULL) 153140929Sphk return; 154171599Spjd ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject"); 155140929Sphk VM_OBJECT_LOCK(obj); 156140929Sphk if (obj->ref_count == 0) { 157140929Sphk /* 158140929Sphk * vclean() may be called twice. The first time 159140929Sphk * removes the primary reference to the object, 160140929Sphk * the second time goes one further and is a 161140929Sphk * special-case to terminate the object. 162140929Sphk * 163140929Sphk * don't double-terminate the object 164140929Sphk */ 165140929Sphk if ((obj->flags & OBJ_DEAD) == 0) 166140929Sphk vm_object_terminate(obj); 167140929Sphk else 168140929Sphk VM_OBJECT_UNLOCK(obj); 169140929Sphk } else { 170140929Sphk /* 171140929Sphk * Woe to the process that tries to page now :-). 172140929Sphk */ 173140929Sphk vm_pager_deallocate(obj); 174140929Sphk VM_OBJECT_UNLOCK(obj); 175140929Sphk } 176144610Sjeff vp->v_object = NULL; 177140929Sphk} 178140929Sphk 179140929Sphk 1801541Srgrimes/* 1811541Srgrimes * Allocate (or lookup) pager for a vnode. 1821541Srgrimes * Handle is a vnode pointer. 18398604Salc * 18498604Salc * MPSAFE 1851541Srgrimes */ 1869507Sdgvm_object_t 18740286Sdgvnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 18828751Sbde vm_ooffset_t offset) 1891541Srgrimes{ 1909456Sdg vm_object_t object; 1911541Srgrimes struct vnode *vp; 1921541Srgrimes 1931541Srgrimes /* 1941541Srgrimes * Pageout to vnode, no can do yet. 1951541Srgrimes */ 1961541Srgrimes if (handle == NULL) 1971827Sdg return (NULL); 1981541Srgrimes 1999411Sdg vp = (struct vnode *) handle; 2009411Sdg 2011541Srgrimes /* 2029411Sdg * If the object is being terminated, wait for it to 2039411Sdg * go away. 2049411Sdg */ 205179159Supsretry: 206114074Salc while ((object = vp->v_object) != NULL) { 207114074Salc VM_OBJECT_LOCK(object); 208181020Sjhb if ((object->flags & OBJ_DEAD) == 0) 209114074Salc break; 210137297Salc vm_object_set_flag(object, OBJ_DISCONNECTWNT); 211114074Salc msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0); 2129507Sdg } 2135455Sdg 21432071Sdyson if (vp->v_usecount == 0) 21532071Sdyson panic("vnode_pager_alloc: no vnode reference"); 21632071Sdyson 2179507Sdg if (object == NULL) { 2181541Srgrimes /* 219179159Sups * Add an object of the appropriate size 2201541Srgrimes */ 22140286Sdg object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 2221827Sdg 22340286Sdg object->un_pager.vnp.vnp_size = size; 2241549Srgrimes 2259507Sdg object->handle = handle; 226179765Sups VI_LOCK(vp); 227179765Sups if (vp->v_object != NULL) { 228179159Sups /* 229179159Sups * Object has been created while we were sleeping 230179159Sups */ 231179765Sups VI_UNLOCK(vp); 232179159Sups vm_object_destroy(object); 233179159Sups goto retry; 234179159Sups } 2359507Sdg vp->v_object = object; 236179765Sups VI_UNLOCK(vp); 237179765Sups } else { 23832286Sdyson object->ref_count++; 239179765Sups VM_OBJECT_UNLOCK(object); 240179765Sups } 241143559Sjeff vref(vp); 2429507Sdg return (object); 2431541Srgrimes} 2441541Srgrimes 245114774Salc/* 246114774Salc * The object must be locked. 247114774Salc */ 24812820Sphkstatic void 2499507Sdgvnode_pager_dealloc(object) 2509507Sdg vm_object_t object; 2511541Srgrimes{ 25279242Sdillon struct vnode *vp = object->handle; 2531541Srgrimes 2549507Sdg if (vp == NULL) 2559507Sdg panic("vnode_pager_dealloc: pager already dealloced"); 2569507Sdg 257114774Salc VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 25833817Sdyson vm_object_pip_wait(object, "vnpdea"); 2591541Srgrimes 2609507Sdg object->handle = NULL; 26133109Sdyson object->type = OBJT_DEAD; 262137297Salc if (object->flags & OBJ_DISCONNECTWNT) { 263137297Salc vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 264137297Salc wakeup(object); 265137297Salc } 266171599Spjd ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc"); 2679507Sdg vp->v_object = NULL; 268140734Sphk vp->v_vflag &= ~VV_TEXT; 2691549Srgrimes} 2701541Srgrimes 27112820Sphkstatic boolean_t 27212767Sdysonvnode_pager_haspage(object, pindex, before, after) 2739507Sdg vm_object_t object; 27412767Sdyson vm_pindex_t pindex; 2759507Sdg int *before; 2769507Sdg int *after; 2771541Srgrimes{ 2789507Sdg struct vnode *vp = object->handle; 27996572Sphk daddr_t bn; 28012423Sphk int err; 28110556Sdyson daddr_t reqblock; 28211701Sdyson int poff; 28311701Sdyson int bsize; 28412914Sdyson int pagesperblock, blocksperpage; 285140723Sjeff int vfslocked; 2861541Srgrimes 287116695Salc VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 28851340Sdillon /* 28951340Sdillon * If no vp or vp is doomed or marked transparent to VM, we do not 29051340Sdillon * have the page. 29151340Sdillon */ 292155384Sjeff if (vp == NULL || vp->v_iflag & VI_DOOMED) 29332585Sdyson return FALSE; 2941541Srgrimes /* 295155384Sjeff * If the offset is beyond end of file we do 2965455Sdg * not have the page. 2971541Srgrimes */ 298155384Sjeff if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 2994797Sdg return FALSE; 3001541Srgrimes 30111576Sdg bsize = vp->v_mount->mnt_stat.f_iosize; 30210556Sdyson pagesperblock = bsize / PAGE_SIZE; 30312914Sdyson blocksperpage = 0; 30412914Sdyson if (pagesperblock > 0) { 30512914Sdyson reqblock = pindex / pagesperblock; 30612914Sdyson } else { 30712914Sdyson blocksperpage = (PAGE_SIZE / bsize); 30812914Sdyson reqblock = pindex * blocksperpage; 30912914Sdyson } 310116695Salc VM_OBJECT_UNLOCK(object); 311140723Sjeff vfslocked = VFS_LOCK_GIANT(vp->v_mount); 312119045Sphk err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 313140723Sjeff VFS_UNLOCK_GIANT(vfslocked); 314116695Salc VM_OBJECT_LOCK(object); 3158876Srgrimes if (err) 3169507Sdg return TRUE; 31792029Seivind if (bn == -1) 31810576Sdyson return FALSE; 31912914Sdyson if (pagesperblock > 0) { 32012914Sdyson poff = pindex - (reqblock * pagesperblock); 32112914Sdyson if (before) { 32212914Sdyson *before *= pagesperblock; 32312914Sdyson *before += poff; 32410669Sdyson } 32512914Sdyson if (after) { 32612914Sdyson int numafter; 32712914Sdyson *after *= pagesperblock; 32812914Sdyson numafter = pagesperblock - (poff + 1); 32999211Srobert if (IDX_TO_OFF(pindex + numafter) > 33099211Srobert object->un_pager.vnp.vnp_size) { 33199211Srobert numafter = 33299211Srobert OFF_TO_IDX(object->un_pager.vnp.vnp_size) - 33399211Srobert pindex; 33412914Sdyson } 33512914Sdyson *after += numafter; 33612914Sdyson } 33712914Sdyson } else { 33812914Sdyson if (before) { 33912914Sdyson *before /= blocksperpage; 34012914Sdyson } 34112914Sdyson 34212914Sdyson if (after) { 34312914Sdyson *after /= blocksperpage; 34412914Sdyson } 34510556Sdyson } 34610576Sdyson return TRUE; 3471541Srgrimes} 3481541Srgrimes 3491541Srgrimes/* 3501541Srgrimes * Lets the VM system know about a change in size for a file. 3519507Sdg * We adjust our own internal size and flush any cached pages in 3521541Srgrimes * the associated object that are affected by the size change. 3531541Srgrimes * 3541541Srgrimes * Note: this routine may be invoked as a result of a pager put 3551541Srgrimes * operation (possibly at object termination time), so we must be careful. 3561541Srgrimes */ 3571541Srgrimesvoid 3581541Srgrimesvnode_pager_setsize(vp, nsize) 3591541Srgrimes struct vnode *vp; 36012767Sdyson vm_ooffset_t nsize; 3611541Srgrimes{ 362116167Salc vm_object_t object; 363116167Salc vm_page_t m; 36438542Sluoqi vm_pindex_t nobjsize; 3651541Srgrimes 366116167Salc if ((object = vp->v_object) == NULL) 3671541Srgrimes return; 368188386Skib/* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */ 369116167Salc VM_OBJECT_LOCK(object); 370116167Salc if (nsize == object->un_pager.vnp.vnp_size) { 371116167Salc /* 372116167Salc * Hasn't changed size 373116167Salc */ 374116167Salc VM_OBJECT_UNLOCK(object); 3753374Sdg return; 376116167Salc } 37738542Sluoqi nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 3789507Sdg if (nsize < object->un_pager.vnp.vnp_size) { 379116167Salc /* 380116167Salc * File has shrunk. Toss any cached pages beyond the new EOF. 381116167Salc */ 382116167Salc if (nobjsize < object->size) 38338542Sluoqi vm_object_page_remove(object, nobjsize, object->size, 384116167Salc FALSE); 3851827Sdg /* 3861827Sdg * this gets rid of garbage at the end of a page that is now 38787834Sdillon * only partially backed by the vnode. 38887834Sdillon * 38987834Sdillon * XXX for some reason (I don't know yet), if we take a 39087834Sdillon * completely invalid page and mark it partially valid 39187834Sdillon * it can screw up NFS reads, so we don't allow the case. 3921827Sdg */ 393116167Salc if ((nsize & PAGE_MASK) && 394121230Salc (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 395121230Salc m->valid != 0) { 396121230Salc int base = (int)nsize & PAGE_MASK; 397121230Salc int size = PAGE_SIZE - base; 39870374Sdillon 399121230Salc /* 400121230Salc * Clear out partial-page garbage in case 401121230Salc * the page has been mapped. 402121230Salc */ 403121230Salc pmap_zero_page_area(m, base, size); 40470374Sdillon 405121230Salc /* 406193303Salc * Update the valid bits to reflect the blocks that 407193303Salc * have been zeroed. Some of these valid bits may 408193303Salc * have already been set. 409193303Salc */ 410193303Salc vm_page_set_valid(m, base, size); 411193303Salc 412193303Salc /* 413193303Salc * Round "base" to the next block boundary so that the 414193303Salc * dirty bit for a partially zeroed block is not 415193303Salc * cleared. 416193303Salc */ 417193303Salc base = roundup2(base, DEV_BSIZE); 418193303Salc 419193303Salc /* 420193303Salc * Clear out partial-page dirty bits. 421121230Salc * 422121230Salc * note that we do not clear out the valid 423121230Salc * bits. This would prevent bogus_page 424121230Salc * replacement from working properly. 425121230Salc */ 426173846Salc vm_page_lock_queues(); 427193303Salc vm_page_clear_dirty(m, base, PAGE_SIZE - base); 428116167Salc vm_page_unlock_queues(); 429172875Salc } else if ((nsize & PAGE_MASK) && 430172875Salc __predict_false(object->cache != NULL)) { 431172875Salc vm_page_cache_free(object, OFF_TO_IDX(nsize), 432172875Salc nobjsize); 4331827Sdg } 4341541Srgrimes } 43512767Sdyson object->un_pager.vnp.vnp_size = nsize; 43638542Sluoqi object->size = nobjsize; 437116167Salc VM_OBJECT_UNLOCK(object); 4381541Srgrimes} 4391541Srgrimes 4401549Srgrimes/* 4411549Srgrimes * calculate the linear (byte) disk address of specified virtual 4421549Srgrimes * file address 4431549Srgrimes */ 444163359Salcstatic int 445163359Salcvnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 446163359Salc int *run) 4471549Srgrimes{ 4485455Sdg int bsize; 4495455Sdg int err; 45012767Sdyson daddr_t vblock; 451146340Sbz daddr_t voffset; 4521549Srgrimes 453138531Salc if (address < 0) 4545455Sdg return -1; 4555455Sdg 456155384Sjeff if (vp->v_iflag & VI_DOOMED) 45711701Sdyson return -1; 45811701Sdyson 4591549Srgrimes bsize = vp->v_mount->mnt_stat.f_iosize; 4601549Srgrimes vblock = address / bsize; 4611549Srgrimes voffset = address % bsize; 4621549Srgrimes 463163359Salc err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 464163359Salc if (err == 0) { 465163359Salc if (*rtaddress != -1) 466163359Salc *rtaddress += voffset / DEV_BSIZE; 46792029Seivind if (run) { 4686151Sdg *run += 1; 4696151Sdg *run *= bsize/PAGE_SIZE; 4706151Sdg *run -= voffset/PAGE_SIZE; 4716151Sdg } 4726151Sdg } 4731549Srgrimes 474163359Salc return (err); 4751549Srgrimes} 4761549Srgrimes 4771549Srgrimes/* 47896755Strhodes * small block filesystem vnode pager input 4791549Srgrimes */ 48012820Sphkstatic int 4819507Sdgvnode_pager_input_smlfs(object, m) 4829507Sdg vm_object_t object; 4831549Srgrimes vm_page_t m; 4841549Srgrimes{ 485191935Salc int bits, i; 486137726Sphk struct vnode *vp; 487137726Sphk struct bufobj *bo; 4881549Srgrimes struct buf *bp; 489127926Salc struct sf_buf *sf; 490146340Sbz daddr_t fileaddr; 4911549Srgrimes vm_offset_t bsize; 4925455Sdg int error = 0; 4931549Srgrimes 4949507Sdg vp = object->handle; 495155384Sjeff if (vp->v_iflag & VI_DOOMED) 49611701Sdyson return VM_PAGER_BAD; 49711701Sdyson 4981549Srgrimes bsize = vp->v_mount->mnt_stat.f_iosize; 4991549Srgrimes 500137726Sphk VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 5011549Srgrimes 502127926Salc sf = sf_buf_alloc(m, 0); 5031549Srgrimes 5041827Sdg for (i = 0; i < PAGE_SIZE / bsize; i++) { 50586092Sdillon vm_ooffset_t address; 5061827Sdg 507191935Salc bits = vm_page_bits(i * bsize, bsize); 508191935Salc if (m->valid & bits) 5095455Sdg continue; 5101549Srgrimes 51186092Sdillon address = IDX_TO_OFF(m->pindex) + i * bsize; 51286092Sdillon if (address >= object->un_pager.vnp.vnp_size) { 51386092Sdillon fileaddr = -1; 51486092Sdillon } else { 515163359Salc error = vnode_pager_addr(vp, address, &fileaddr, NULL); 516163359Salc if (error) 517163359Salc break; 51886092Sdillon } 5191827Sdg if (fileaddr != -1) { 52042957Sdillon bp = getpbuf(&vnode_pbuf_freecnt); 5211549Srgrimes 5221827Sdg /* build a minimal buffer header */ 52358345Sphk bp->b_iocmd = BIO_READ; 524119092Sphk bp->b_iodone = bdone; 52584827Sjhb KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 52684827Sjhb KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 52791406Sjhb bp->b_rcred = crhold(curthread->td_ucred); 52891406Sjhb bp->b_wcred = crhold(curthread->td_ucred); 529127926Salc bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 5306626Sdg bp->b_blkno = fileaddr; 531137726Sphk pbgetbo(bo, bp); 5321549Srgrimes bp->b_bcount = bsize; 5331549Srgrimes bp->b_bufsize = bsize; 53470374Sdillon bp->b_runningbufspace = bp->b_bufsize; 535189595Sjhb atomic_add_long(&runningbufspace, bp->b_runningbufspace); 5361827Sdg 5371827Sdg /* do the input */ 538121205Sphk bp->b_iooffset = dbtob(bp->b_blkno); 539136927Sphk bstrategy(bp); 5401549Srgrimes 541119092Sphk bwait(bp, PVM, "vnsrd"); 542119092Sphk 54358934Sphk if ((bp->b_ioflags & BIO_ERROR) != 0) 5441549Srgrimes error = EIO; 5451549Srgrimes 5461827Sdg /* 5471827Sdg * free the buffer header back to the swap buffer pool 5481827Sdg */ 549137726Sphk pbrelbo(bp); 55042957Sdillon relpbuf(bp, &vnode_pbuf_freecnt); 5511827Sdg if (error) 5521549Srgrimes break; 553191935Salc } else 554127926Salc bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 555191935Salc KASSERT((m->dirty & bits) == 0, 556191935Salc ("vnode_pager_input_smlfs: page %p is dirty", m)); 557191935Salc VM_OBJECT_LOCK(object); 558191935Salc m->valid |= bits; 559191935Salc VM_OBJECT_UNLOCK(object); 5601549Srgrimes } 561127926Salc sf_buf_free(sf); 5621827Sdg if (error) { 5634207Sdg return VM_PAGER_ERROR; 5641549Srgrimes } 5651549Srgrimes return VM_PAGER_OK; 5661549Srgrimes} 5671549Srgrimes 5681549Srgrimes/* 569139296Sphk * old style vnode pager input routine 5701549Srgrimes */ 57112820Sphkstatic int 5729507Sdgvnode_pager_input_old(object, m) 5739507Sdg vm_object_t object; 5741549Srgrimes vm_page_t m; 5751549Srgrimes{ 5761541Srgrimes struct uio auio; 5771541Srgrimes struct iovec aiov; 5785455Sdg int error; 5795455Sdg int size; 580127926Salc struct sf_buf *sf; 58177398Sjhb struct vnode *vp; 5821549Srgrimes 583121495Salc VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 5841549Srgrimes error = 0; 5851827Sdg 5861549Srgrimes /* 5871549Srgrimes * Return failure if beyond current EOF 5881549Srgrimes */ 58912767Sdyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 5901549Srgrimes return VM_PAGER_BAD; 5911549Srgrimes } else { 5921549Srgrimes size = PAGE_SIZE; 59312767Sdyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 59412767Sdyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 595121495Salc vp = object->handle; 596121495Salc VM_OBJECT_UNLOCK(object); 5977178Sdg 5985455Sdg /* 5995455Sdg * Allocate a kernel virtual address and initialize so that 6005455Sdg * we can use VOP_READ/WRITE routines. 6015455Sdg */ 602127926Salc sf = sf_buf_alloc(m, 0); 6037178Sdg 604127926Salc aiov.iov_base = (caddr_t)sf_buf_kva(sf); 6051549Srgrimes aiov.iov_len = size; 6061549Srgrimes auio.uio_iov = &aiov; 6071549Srgrimes auio.uio_iovcnt = 1; 60812767Sdyson auio.uio_offset = IDX_TO_OFF(m->pindex); 6091549Srgrimes auio.uio_segflg = UIO_SYSSPACE; 6101549Srgrimes auio.uio_rw = UIO_READ; 6111549Srgrimes auio.uio_resid = size; 61283366Sjulian auio.uio_td = curthread; 6131549Srgrimes 61491406Sjhb error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 6151549Srgrimes if (!error) { 61679242Sdillon int count = size - auio.uio_resid; 6171549Srgrimes 6181549Srgrimes if (count == 0) 6191549Srgrimes error = EINVAL; 6201549Srgrimes else if (count != PAGE_SIZE) 621127926Salc bzero((caddr_t)sf_buf_kva(sf) + count, 622127926Salc PAGE_SIZE - count); 6231549Srgrimes } 624127926Salc sf_buf_free(sf); 625121230Salc 626121230Salc VM_OBJECT_LOCK(object); 6271549Srgrimes } 628191935Salc KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m)); 62939739Srvb if (!error) 63039739Srvb m->valid = VM_PAGE_BITS_ALL; 6314207Sdg return error ? VM_PAGER_ERROR : VM_PAGER_OK; 6321549Srgrimes} 6331549Srgrimes 6341549Srgrimes/* 6351549Srgrimes * generic vnode pager input routine 6361549Srgrimes */ 63710556Sdyson 63833847Smsmith/* 63976827Salfred * Local media VFS's that do not implement their own VOP_GETPAGES 64099211Srobert * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 64199211Srobert * to implement the previous behaviour. 64233847Smsmith * 64333847Smsmith * All other FS's should use the bypass to get to the local media 64433847Smsmith * backing vp's VOP_GETPAGES. 64533847Smsmith */ 64612820Sphkstatic int 6479507Sdgvnode_pager_getpages(object, m, count, reqpage) 6489507Sdg vm_object_t object; 6491549Srgrimes vm_page_t *m; 6509507Sdg int count; 6519507Sdg int reqpage; 6521549Srgrimes{ 65310556Sdyson int rtval; 65410556Sdyson struct vnode *vp; 65534403Smsmith int bytes = count * PAGE_SIZE; 656140723Sjeff int vfslocked; 65732286Sdyson 65810556Sdyson vp = object->handle; 659116279Salc VM_OBJECT_UNLOCK(object); 660140723Sjeff vfslocked = VFS_LOCK_GIANT(vp->v_mount); 66134403Smsmith rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 66276827Salfred KASSERT(rtval != EOPNOTSUPP, 66376827Salfred ("vnode_pager: FS getpages not implemented\n")); 664140723Sjeff VFS_UNLOCK_GIANT(vfslocked); 665116279Salc VM_OBJECT_LOCK(object); 66633847Smsmith return rtval; 66710556Sdyson} 66810556Sdyson 66933847Smsmith/* 67033847Smsmith * This is now called from local media FS's to operate against their 67133847Smsmith * own vnodes if they fail to implement VOP_GETPAGES. 67233847Smsmith */ 67333847Smsmithint 67433847Smsmithvnode_pager_generic_getpages(vp, m, bytecount, reqpage) 67533847Smsmith struct vnode *vp; 67610556Sdyson vm_page_t *m; 67733847Smsmith int bytecount; 67810556Sdyson int reqpage; 67910556Sdyson{ 68033847Smsmith vm_object_t object; 68112767Sdyson vm_offset_t kva; 68234206Sdyson off_t foff, tfoff, nextoff; 683146340Sbz int i, j, size, bsize, first; 684163140Salc daddr_t firstaddr, reqblock; 685137726Sphk struct bufobj *bo; 6866151Sdg int runpg; 6876151Sdg int runend; 6887178Sdg struct buf *bp; 68933847Smsmith int count; 690163210Salc int error; 6911549Srgrimes 69233847Smsmith object = vp->v_object; 69333847Smsmith count = bytecount / PAGE_SIZE; 69433847Smsmith 695137726Sphk KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 696137726Sphk ("vnode_pager_generic_getpages does not support devices")); 697155384Sjeff if (vp->v_iflag & VI_DOOMED) 69811701Sdyson return VM_PAGER_BAD; 69911701Sdyson 7001549Srgrimes bsize = vp->v_mount->mnt_stat.f_iosize; 7011549Srgrimes 7021549Srgrimes /* get the UNDERLYING device for the file with VOP_BMAP() */ 7031827Sdg 7041549Srgrimes /* 7051827Sdg * originally, we did not check for an error return value -- assuming 7061827Sdg * an fs always has a bmap entry point -- that assumption is wrong!!! 7071549Srgrimes */ 70812767Sdyson foff = IDX_TO_OFF(m[reqpage]->pindex); 7091827Sdg 7101549Srgrimes /* 7111887Sdg * if we can't bmap, use old VOP code 7121549Srgrimes */ 713163210Salc error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL); 714163210Salc if (error == EOPNOTSUPP) { 715116512Salc VM_OBJECT_LOCK(object); 716100832Salc vm_page_lock_queues(); 717100832Salc for (i = 0; i < count; i++) 718100832Salc if (i != reqpage) 71975692Salfred vm_page_free(m[i]); 720100832Salc vm_page_unlock_queues(); 721170292Sattilio PCPU_INC(cnt.v_vnodein); 722170292Sattilio PCPU_INC(cnt.v_vnodepgsin); 723121495Salc error = vnode_pager_input_old(object, m[reqpage]); 724121495Salc VM_OBJECT_UNLOCK(object); 725121495Salc return (error); 726163210Salc } else if (error != 0) { 727163210Salc VM_OBJECT_LOCK(object); 728163210Salc vm_page_lock_queues(); 729163210Salc for (i = 0; i < count; i++) 730163210Salc if (i != reqpage) 731163210Salc vm_page_free(m[i]); 732163210Salc vm_page_unlock_queues(); 733163210Salc VM_OBJECT_UNLOCK(object); 734163210Salc return (VM_PAGER_ERROR); 7351549Srgrimes 7361827Sdg /* 7371827Sdg * if the blocksize is smaller than a page size, then use 7381827Sdg * special small filesystem code. NFS sometimes has a small 7391827Sdg * blocksize, but it can handle large reads itself. 7401827Sdg */ 7411827Sdg } else if ((PAGE_SIZE / bsize) > 1 && 74238866Sbde (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 743116512Salc VM_OBJECT_LOCK(object); 744100832Salc vm_page_lock_queues(); 745100832Salc for (i = 0; i < count; i++) 746100832Salc if (i != reqpage) 74775692Salfred vm_page_free(m[i]); 748100832Salc vm_page_unlock_queues(); 749116512Salc VM_OBJECT_UNLOCK(object); 750170292Sattilio PCPU_INC(cnt.v_vnodein); 751170292Sattilio PCPU_INC(cnt.v_vnodepgsin); 7529507Sdg return vnode_pager_input_smlfs(object, m[reqpage]); 7531549Srgrimes } 75445347Sjulian 7551549Srgrimes /* 75645347Sjulian * If we have a completely valid page available to us, we can 75745347Sjulian * clean up and return. Otherwise we have to re-read the 75845347Sjulian * media. 7591549Srgrimes */ 760121227Salc VM_OBJECT_LOCK(object); 76145347Sjulian if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 762100832Salc vm_page_lock_queues(); 763100832Salc for (i = 0; i < count; i++) 7645455Sdg if (i != reqpage) 76575692Salfred vm_page_free(m[i]); 766100832Salc vm_page_unlock_queues(); 767116512Salc VM_OBJECT_UNLOCK(object); 7685455Sdg return VM_PAGER_OK; 769163140Salc } else if (reqblock == -1) { 770163140Salc pmap_zero_page(m[reqpage]); 771192010Salc KASSERT(m[reqpage]->dirty == 0, 772192010Salc ("vnode_pager_generic_getpages: page %p is dirty", m)); 773163140Salc m[reqpage]->valid = VM_PAGE_BITS_ALL; 774163140Salc vm_page_lock_queues(); 775163140Salc for (i = 0; i < count; i++) 776163140Salc if (i != reqpage) 777163140Salc vm_page_free(m[i]); 778163140Salc vm_page_unlock_queues(); 779163140Salc VM_OBJECT_UNLOCK(object); 780163140Salc return (VM_PAGER_OK); 7811549Srgrimes } 78245347Sjulian m[reqpage]->valid = 0; 783121227Salc VM_OBJECT_UNLOCK(object); 7847178Sdg 7855455Sdg /* 7865455Sdg * here on direct device I/O 7875455Sdg */ 78892029Seivind firstaddr = -1; 7891549Srgrimes 7901549Srgrimes /* 7916151Sdg * calculate the run that includes the required page 7921549Srgrimes */ 79392029Seivind for (first = 0, i = 0; i < count; i = runend) { 794163359Salc if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr, 795163359Salc &runpg) != 0) { 796163359Salc VM_OBJECT_LOCK(object); 797163359Salc vm_page_lock_queues(); 798163359Salc for (; i < count; i++) 799163359Salc if (i != reqpage) 800163359Salc vm_page_free(m[i]); 801163359Salc vm_page_unlock_queues(); 802163359Salc VM_OBJECT_UNLOCK(object); 803163359Salc return (VM_PAGER_ERROR); 804163359Salc } 8056151Sdg if (firstaddr == -1) { 806116512Salc VM_OBJECT_LOCK(object); 8079507Sdg if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 808146340Sbz panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx", 809146340Sbz (intmax_t)firstaddr, (uintmax_t)(foff >> 32), 810106603Smux (uintmax_t)foff, 811106603Smux (uintmax_t) 812106603Smux (object->un_pager.vnp.vnp_size >> 32), 813106603Smux (uintmax_t)object->un_pager.vnp.vnp_size); 8146151Sdg } 815100832Salc vm_page_lock_queues(); 81675692Salfred vm_page_free(m[i]); 817100832Salc vm_page_unlock_queues(); 818116512Salc VM_OBJECT_UNLOCK(object); 8196151Sdg runend = i + 1; 8206151Sdg first = runend; 8216151Sdg continue; 8221549Srgrimes } 8236151Sdg runend = i + runpg; 8249507Sdg if (runend <= reqpage) { 825116512Salc VM_OBJECT_LOCK(object); 826100832Salc vm_page_lock_queues(); 827100832Salc for (j = i; j < runend; j++) 82875692Salfred vm_page_free(m[j]); 829100832Salc vm_page_unlock_queues(); 830116512Salc VM_OBJECT_UNLOCK(object); 8311549Srgrimes } else { 8329507Sdg if (runpg < (count - first)) { 833116512Salc VM_OBJECT_LOCK(object); 834100832Salc vm_page_lock_queues(); 8359507Sdg for (i = first + runpg; i < count; i++) 83675692Salfred vm_page_free(m[i]); 837100832Salc vm_page_unlock_queues(); 838116512Salc VM_OBJECT_UNLOCK(object); 8396151Sdg count = first + runpg; 8406151Sdg } 8416151Sdg break; 8421549Srgrimes } 8436151Sdg first = runend; 8441549Srgrimes } 8451549Srgrimes 8461549Srgrimes /* 8471827Sdg * the first and last page have been calculated now, move input pages 8481827Sdg * to be zero based... 8491549Srgrimes */ 8501549Srgrimes if (first != 0) { 851163361Salc m += first; 8521549Srgrimes count -= first; 8531549Srgrimes reqpage -= first; 8541549Srgrimes } 8556151Sdg 8561549Srgrimes /* 8571549Srgrimes * calculate the file virtual address for the transfer 8581549Srgrimes */ 85912767Sdyson foff = IDX_TO_OFF(m[0]->pindex); 8601827Sdg 8611549Srgrimes /* 8621549Srgrimes * calculate the size of the transfer 8631549Srgrimes */ 8641549Srgrimes size = count * PAGE_SIZE; 865134892Sphk KASSERT(count > 0, ("zero count")); 8669507Sdg if ((foff + size) > object->un_pager.vnp.vnp_size) 8679507Sdg size = object->un_pager.vnp.vnp_size - foff; 868134892Sphk KASSERT(size > 0, ("zero size")); 8691549Srgrimes 8701549Srgrimes /* 87151340Sdillon * round up physical size for real devices. 8721549Srgrimes */ 873137726Sphk if (1) { 874137726Sphk int secmask = bo->bo_bsize - 1; 875136977Sphk KASSERT(secmask < PAGE_SIZE && secmask > 0, 876136977Sphk ("vnode_pager_generic_getpages: sector size %d too large", 877136977Sphk secmask + 1)); 87851340Sdillon size = (size + secmask) & ~secmask; 87951340Sdillon } 8801549Srgrimes 88142957Sdillon bp = getpbuf(&vnode_pbuf_freecnt); 8825455Sdg kva = (vm_offset_t) bp->b_data; 8831887Sdg 8841549Srgrimes /* 8851549Srgrimes * and map the pages to be read into the kva 8861549Srgrimes */ 8871887Sdg pmap_qenter(kva, m, count); 8881549Srgrimes 8891549Srgrimes /* build a minimal buffer header */ 89058345Sphk bp->b_iocmd = BIO_READ; 891119092Sphk bp->b_iodone = bdone; 89284827Sjhb KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 89384827Sjhb KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 89491406Sjhb bp->b_rcred = crhold(curthread->td_ucred); 89591406Sjhb bp->b_wcred = crhold(curthread->td_ucred); 8966626Sdg bp->b_blkno = firstaddr; 897137726Sphk pbgetbo(bo, bp); 8981549Srgrimes bp->b_bcount = size; 8991549Srgrimes bp->b_bufsize = size; 90070374Sdillon bp->b_runningbufspace = bp->b_bufsize; 901189595Sjhb atomic_add_long(&runningbufspace, bp->b_runningbufspace); 9021549Srgrimes 903170292Sattilio PCPU_INC(cnt.v_vnodein); 904170292Sattilio PCPU_ADD(cnt.v_vnodepgsin, count); 9053612Sdg 9061549Srgrimes /* do the input */ 907121205Sphk bp->b_iooffset = dbtob(bp->b_blkno); 908136927Sphk bstrategy(bp); 9093612Sdg 910119092Sphk bwait(bp, PVM, "vnread"); 9111549Srgrimes 91258934Sphk if ((bp->b_ioflags & BIO_ERROR) != 0) 9131549Srgrimes error = EIO; 9141549Srgrimes 9151549Srgrimes if (!error) { 9161549Srgrimes if (size != count * PAGE_SIZE) 9171827Sdg bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 9181549Srgrimes } 9195455Sdg pmap_qremove(kva, count); 9201549Srgrimes 9211549Srgrimes /* 9221549Srgrimes * free the buffer header back to the swap buffer pool 9231549Srgrimes */ 924137726Sphk pbrelbo(bp); 92542957Sdillon relpbuf(bp, &vnode_pbuf_freecnt); 9261549Srgrimes 927116512Salc VM_OBJECT_LOCK(object); 928100736Salc vm_page_lock_queues(); 92934206Sdyson for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 93034206Sdyson vm_page_t mt; 93134206Sdyson 93234206Sdyson nextoff = tfoff + PAGE_SIZE; 93334206Sdyson mt = m[i]; 93434206Sdyson 93547239Sdt if (nextoff <= object->un_pager.vnp.vnp_size) { 93645347Sjulian /* 93745347Sjulian * Read filled up entire page. 93845347Sjulian */ 93934206Sdyson mt->valid = VM_PAGE_BITS_ALL; 940191478Salc KASSERT(mt->dirty == 0, 941191478Salc ("vnode_pager_generic_getpages: page %p is dirty", 942191478Salc mt)); 943191478Salc KASSERT(!pmap_page_is_mapped(mt), 944191478Salc ("vnode_pager_generic_getpages: page %p is mapped", 945191478Salc mt)); 94634206Sdyson } else { 94745347Sjulian /* 948192134Salc * Read did not fill up entire page. 94945347Sjulian * 95045347Sjulian * Currently we do not set the entire page valid, 95145347Sjulian * we just try to clear the piece that we couldn't 95245347Sjulian * read. 95345347Sjulian */ 954192134Salc vm_page_set_valid(mt, 0, 95547239Sdt object->un_pager.vnp.vnp_size - tfoff); 956192134Salc KASSERT((mt->dirty & vm_page_bits(0, 957192134Salc object->un_pager.vnp.vnp_size - tfoff)) == 0, 958192134Salc ("vnode_pager_generic_getpages: page %p is dirty", 959192134Salc mt)); 96034206Sdyson } 96134206Sdyson 9621549Srgrimes if (i != reqpage) { 9631827Sdg 9641549Srgrimes /* 9651827Sdg * whether or not to leave the page activated is up in 9661827Sdg * the air, but we should put the page on a page queue 9671827Sdg * somewhere. (it already is in the object). Result: 96858634Scharnier * It appears that empirical results show that 9691827Sdg * deactivating pages is best. 9701549Srgrimes */ 9711827Sdg 9721549Srgrimes /* 9731827Sdg * just in case someone was asking for this page we 9741827Sdg * now tell them that it is ok to use 9751549Srgrimes */ 9761549Srgrimes if (!error) { 977161125Salc if (mt->oflags & VPO_WANTED) 97834206Sdyson vm_page_activate(mt); 97933109Sdyson else 98034206Sdyson vm_page_deactivate(mt); 98138799Sdfr vm_page_wakeup(mt); 9821549Srgrimes } else { 98375692Salfred vm_page_free(mt); 9841549Srgrimes } 9851549Srgrimes } 9861549Srgrimes } 987100736Salc vm_page_unlock_queues(); 988116512Salc VM_OBJECT_UNLOCK(object); 9891549Srgrimes if (error) { 9909507Sdg printf("vnode_pager_getpages: I/O read error\n"); 9911549Srgrimes } 9924207Sdg return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 9931549Srgrimes} 9941549Srgrimes 99533847Smsmith/* 99633847Smsmith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 99733847Smsmith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 99833847Smsmith * vnode_pager_generic_putpages() to implement the previous behaviour. 99933847Smsmith * 100033847Smsmith * All other FS's should use the bypass to get to the local media 100133847Smsmith * backing vp's VOP_PUTPAGES. 100233847Smsmith */ 100343129Sdillonstatic void 100410556Sdysonvnode_pager_putpages(object, m, count, sync, rtvals) 100510556Sdyson vm_object_t object; 100610556Sdyson vm_page_t *m; 100710556Sdyson int count; 100810556Sdyson boolean_t sync; 100910556Sdyson int *rtvals; 101010556Sdyson{ 101110556Sdyson int rtval; 101210556Sdyson struct vnode *vp; 101362976Smckusick struct mount *mp; 101434403Smsmith int bytes = count * PAGE_SIZE; 101518973Sdyson 101644321Salc /* 101744321Salc * Force synchronous operation if we are extremely low on memory 101844321Salc * to prevent a low-memory deadlock. VOP operations often need to 101944321Salc * allocate more memory to initiate the I/O ( i.e. do a BMAP 102044321Salc * operation ). The swapper handles the case by limiting the amount 102144321Salc * of asynchronous I/O, but that sort of solution doesn't scale well 102244321Salc * for the vnode pager without a lot of work. 102344321Salc * 102444321Salc * Also, the backing vnode's iodone routine may not wake the pageout 102544321Salc * daemon up. This should be probably be addressed XXX. 102644321Salc */ 102744321Salc 1028170170Sattilio if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 102944321Salc sync |= OBJPC_SYNC; 103044321Salc 103144321Salc /* 103244321Salc * Call device-specific putpages function 103344321Salc */ 103410556Sdyson vp = object->handle; 1035121455Salc VM_OBJECT_UNLOCK(object); 103662976Smckusick if (vp->v_type != VREG) 103762976Smckusick mp = NULL; 103834403Smsmith rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 103976827Salfred KASSERT(rtval != EOPNOTSUPP, 104076827Salfred ("vnode_pager: stale FS putpages\n")); 1041121455Salc VM_OBJECT_LOCK(object); 104210556Sdyson} 104310556Sdyson 104433847Smsmith 10451549Srgrimes/* 104633847Smsmith * This is now called from local media FS's to operate against their 104745057Seivind * own vnodes if they fail to implement VOP_PUTPAGES. 104870374Sdillon * 104970374Sdillon * This is typically called indirectly via the pageout daemon and 105070374Sdillon * clustering has already typically occured, so in general we ask the 105170374Sdillon * underlying filesystem to write the data out asynchronously rather 105270374Sdillon * then delayed. 10531549Srgrimes */ 105433847Smsmithint 105534206Sdysonvnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 105633847Smsmith struct vnode *vp; 10571549Srgrimes vm_page_t *m; 105833847Smsmith int bytecount; 105934206Sdyson int flags; 10605455Sdg int *rtvals; 10611549Srgrimes{ 10627695Sdg int i; 106333847Smsmith vm_object_t object; 106433847Smsmith int count; 10651549Srgrimes 10667695Sdg int maxsize, ncount; 106712767Sdyson vm_ooffset_t poffset; 10687695Sdg struct uio auio; 10697695Sdg struct iovec aiov; 10707695Sdg int error; 107134206Sdyson int ioflags; 1072151951Sps int ppscheck = 0; 1073151951Sps static struct timeval lastfail; 1074151951Sps static int curfail; 10751549Srgrimes 107633847Smsmith object = vp->v_object; 107733847Smsmith count = bytecount / PAGE_SIZE; 107833847Smsmith 10791827Sdg for (i = 0; i < count; i++) 10801549Srgrimes rtvals[i] = VM_PAGER_AGAIN; 10811549Srgrimes 1082138406Salc if ((int64_t)m[0]->pindex < 0) { 1083119544Smarcel printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n", 1084119544Smarcel (long)m[0]->pindex, (u_long)m[0]->dirty); 10857695Sdg rtvals[0] = VM_PAGER_BAD; 10867695Sdg return VM_PAGER_BAD; 10875455Sdg } 10887178Sdg 10897695Sdg maxsize = count * PAGE_SIZE; 10907695Sdg ncount = count; 10911549Srgrimes 109212767Sdyson poffset = IDX_TO_OFF(m[0]->pindex); 109384854Sdillon 109484854Sdillon /* 109584854Sdillon * If the page-aligned write is larger then the actual file we 109684854Sdillon * have to invalidate pages occuring beyond the file EOF. However, 109784854Sdillon * there is an edge case where a file may not be page-aligned where 109884854Sdillon * the last page is partially invalid. In this case the filesystem 109984854Sdillon * may not properly clear the dirty bits for the entire page (which 110084854Sdillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 110184854Sdillon * With the page locked we are free to fix-up the dirty bits here. 110287834Sdillon * 110387834Sdillon * We do not under any circumstances truncate the valid bits, as 110487834Sdillon * this will screw up bogus page replacement. 110584854Sdillon */ 110612767Sdyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 110784854Sdillon if (object->un_pager.vnp.vnp_size > poffset) { 110884854Sdillon int pgoff; 110984854Sdillon 111012767Sdyson maxsize = object->un_pager.vnp.vnp_size - poffset; 111184854Sdillon ncount = btoc(maxsize); 111284854Sdillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1113119370Salc vm_page_lock_queues(); 111484854Sdillon vm_page_clear_dirty(m[ncount - 1], pgoff, 111584854Sdillon PAGE_SIZE - pgoff); 1116119370Salc vm_page_unlock_queues(); 111784854Sdillon } 111884854Sdillon } else { 11198585Sdg maxsize = 0; 112084854Sdillon ncount = 0; 112184854Sdillon } 11228585Sdg if (ncount < count) { 11238585Sdg for (i = ncount; i < count; i++) { 11247695Sdg rtvals[i] = VM_PAGER_BAD; 11251549Srgrimes } 11261549Srgrimes } 11271541Srgrimes } 11287695Sdg 112970374Sdillon /* 113070374Sdillon * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 113170374Sdillon * rather then a bdwrite() to prevent paging I/O from saturating 1132108358Sdillon * the buffer cache. Dummy-up the sequential heuristic to cause 1133108358Sdillon * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 1134108358Sdillon * the system decides how to cluster. 113570374Sdillon */ 113634206Sdyson ioflags = IO_VMIO; 1137108358Sdillon if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 1138108358Sdillon ioflags |= IO_SYNC; 1139108358Sdillon else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 1140108358Sdillon ioflags |= IO_ASYNC; 114134206Sdyson ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 1142108358Sdillon ioflags |= IO_SEQMAX << IO_SEQSHIFT; 11431827Sdg 11447695Sdg aiov.iov_base = (caddr_t) 0; 11457695Sdg aiov.iov_len = maxsize; 11467695Sdg auio.uio_iov = &aiov; 11477695Sdg auio.uio_iovcnt = 1; 114812767Sdyson auio.uio_offset = poffset; 11497695Sdg auio.uio_segflg = UIO_NOCOPY; 11507695Sdg auio.uio_rw = UIO_WRITE; 11517695Sdg auio.uio_resid = maxsize; 115283366Sjulian auio.uio_td = (struct thread *) 0; 115391406Sjhb error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1154170292Sattilio PCPU_INC(cnt.v_vnodeout); 1155170292Sattilio PCPU_ADD(cnt.v_vnodepgsout, ncount); 11563612Sdg 11578585Sdg if (error) { 1158151951Sps if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1))) 1159151951Sps printf("vnode_pager_putpages: I/O error %d\n", error); 11607695Sdg } 11618585Sdg if (auio.uio_resid) { 1162151951Sps if (ppscheck || ppsratecheck(&lastfail, &curfail, 1)) 1163151951Sps printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1164151951Sps auio.uio_resid, (u_long)m[0]->pindex); 11657695Sdg } 116633936Sdyson for (i = 0; i < ncount; i++) { 116733936Sdyson rtvals[i] = VM_PAGER_OK; 11687695Sdg } 11697695Sdg return rtvals[0]; 11707695Sdg} 1171