vnode_pager.c revision 163359
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 163359 2006-10-14 22:09:03Z 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 } 115140767Sphk VOP_UNLOCK(vp, 0, td); 116140767Sphk vm_object_set_flag(object, OBJ_DISCONNECTWNT); 117140767Sphk msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0); 118140767Sphk vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 119140767Sphk } 120140767Sphk 121140767Sphk if (size == 0) { 122140767Sphk if (vn_isdisk(vp, NULL)) { 123140767Sphk size = IDX_TO_OFF(INT_MAX); 124140767Sphk } else { 125140767Sphk if (VOP_GETATTR(vp, &va, td->td_ucred, td) != 0) 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; 154143505Sjeff ASSERT_VOP_LOCKED(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 201103923Sjeff ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc"); 202103923Sjeff 2031541Srgrimes /* 2049411Sdg * If the object is being terminated, wait for it to 2059411Sdg * go away. 2069411Sdg */ 207114074Salc while ((object = vp->v_object) != NULL) { 208114074Salc VM_OBJECT_LOCK(object); 209114074Salc if ((object->flags & OBJ_DEAD) == 0) 210114074Salc break; 211137297Salc vm_object_set_flag(object, OBJ_DISCONNECTWNT); 212114074Salc msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0); 2139507Sdg } 2145455Sdg 21532071Sdyson if (vp->v_usecount == 0) 21632071Sdyson panic("vnode_pager_alloc: no vnode reference"); 21732071Sdyson 2189507Sdg if (object == NULL) { 2191541Srgrimes /* 2201541Srgrimes * And an object of the appropriate size 2211541Srgrimes */ 22240286Sdg object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 2231827Sdg 22440286Sdg object->un_pager.vnp.vnp_size = size; 2251549Srgrimes 2269507Sdg object->handle = handle; 227145826Sjeff if (VFS_NEEDSGIANT(vp->v_mount)) 228145826Sjeff vm_object_set_flag(object, OBJ_NEEDGIANT); 2299507Sdg vp->v_object = object; 2301541Srgrimes } else { 23132286Sdyson object->ref_count++; 232114074Salc VM_OBJECT_UNLOCK(object); 2331541Srgrimes } 234143559Sjeff vref(vp); 2359507Sdg return (object); 2361541Srgrimes} 2371541Srgrimes 238114774Salc/* 239114774Salc * The object must be locked. 240114774Salc */ 24112820Sphkstatic void 2429507Sdgvnode_pager_dealloc(object) 2439507Sdg vm_object_t object; 2441541Srgrimes{ 24579242Sdillon struct vnode *vp = object->handle; 2461541Srgrimes 2479507Sdg if (vp == NULL) 2489507Sdg panic("vnode_pager_dealloc: pager already dealloced"); 2499507Sdg 250114774Salc VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 25133817Sdyson vm_object_pip_wait(object, "vnpdea"); 2521541Srgrimes 2539507Sdg object->handle = NULL; 25433109Sdyson object->type = OBJT_DEAD; 255137297Salc if (object->flags & OBJ_DISCONNECTWNT) { 256137297Salc vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 257137297Salc wakeup(object); 258137297Salc } 259101308Sjeff ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc"); 2609507Sdg vp->v_object = NULL; 261140734Sphk vp->v_vflag &= ~VV_TEXT; 2621549Srgrimes} 2631541Srgrimes 26412820Sphkstatic boolean_t 26512767Sdysonvnode_pager_haspage(object, pindex, before, after) 2669507Sdg vm_object_t object; 26712767Sdyson vm_pindex_t pindex; 2689507Sdg int *before; 2699507Sdg int *after; 2701541Srgrimes{ 2719507Sdg struct vnode *vp = object->handle; 27296572Sphk daddr_t bn; 27312423Sphk int err; 27410556Sdyson daddr_t reqblock; 27511701Sdyson int poff; 27611701Sdyson int bsize; 27712914Sdyson int pagesperblock, blocksperpage; 278140723Sjeff int vfslocked; 2791541Srgrimes 280116695Salc VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 28151340Sdillon /* 28251340Sdillon * If no vp or vp is doomed or marked transparent to VM, we do not 28351340Sdillon * have the page. 28451340Sdillon */ 285155384Sjeff if (vp == NULL || vp->v_iflag & VI_DOOMED) 28632585Sdyson return FALSE; 2871541Srgrimes /* 288155384Sjeff * If the offset is beyond end of file we do 2895455Sdg * not have the page. 2901541Srgrimes */ 291155384Sjeff if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 2924797Sdg return FALSE; 2931541Srgrimes 29411576Sdg bsize = vp->v_mount->mnt_stat.f_iosize; 29510556Sdyson pagesperblock = bsize / PAGE_SIZE; 29612914Sdyson blocksperpage = 0; 29712914Sdyson if (pagesperblock > 0) { 29812914Sdyson reqblock = pindex / pagesperblock; 29912914Sdyson } else { 30012914Sdyson blocksperpage = (PAGE_SIZE / bsize); 30112914Sdyson reqblock = pindex * blocksperpage; 30212914Sdyson } 303116695Salc VM_OBJECT_UNLOCK(object); 304140723Sjeff vfslocked = VFS_LOCK_GIANT(vp->v_mount); 305119045Sphk err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 306140723Sjeff VFS_UNLOCK_GIANT(vfslocked); 307116695Salc VM_OBJECT_LOCK(object); 3088876Srgrimes if (err) 3099507Sdg return TRUE; 31092029Seivind if (bn == -1) 31110576Sdyson return FALSE; 31212914Sdyson if (pagesperblock > 0) { 31312914Sdyson poff = pindex - (reqblock * pagesperblock); 31412914Sdyson if (before) { 31512914Sdyson *before *= pagesperblock; 31612914Sdyson *before += poff; 31710669Sdyson } 31812914Sdyson if (after) { 31912914Sdyson int numafter; 32012914Sdyson *after *= pagesperblock; 32112914Sdyson numafter = pagesperblock - (poff + 1); 32299211Srobert if (IDX_TO_OFF(pindex + numafter) > 32399211Srobert object->un_pager.vnp.vnp_size) { 32499211Srobert numafter = 32599211Srobert OFF_TO_IDX(object->un_pager.vnp.vnp_size) - 32699211Srobert pindex; 32712914Sdyson } 32812914Sdyson *after += numafter; 32912914Sdyson } 33012914Sdyson } else { 33112914Sdyson if (before) { 33212914Sdyson *before /= blocksperpage; 33312914Sdyson } 33412914Sdyson 33512914Sdyson if (after) { 33612914Sdyson *after /= blocksperpage; 33712914Sdyson } 33810556Sdyson } 33910576Sdyson return TRUE; 3401541Srgrimes} 3411541Srgrimes 3421541Srgrimes/* 3431541Srgrimes * Lets the VM system know about a change in size for a file. 3449507Sdg * We adjust our own internal size and flush any cached pages in 3451541Srgrimes * the associated object that are affected by the size change. 3461541Srgrimes * 3471541Srgrimes * Note: this routine may be invoked as a result of a pager put 3481541Srgrimes * operation (possibly at object termination time), so we must be careful. 3491541Srgrimes */ 3501541Srgrimesvoid 3511541Srgrimesvnode_pager_setsize(vp, nsize) 3521541Srgrimes struct vnode *vp; 35312767Sdyson vm_ooffset_t nsize; 3541541Srgrimes{ 355116167Salc vm_object_t object; 356116167Salc vm_page_t m; 35738542Sluoqi vm_pindex_t nobjsize; 3581541Srgrimes 359116167Salc if ((object = vp->v_object) == NULL) 3601541Srgrimes return; 361116167Salc VM_OBJECT_LOCK(object); 362116167Salc if (nsize == object->un_pager.vnp.vnp_size) { 363116167Salc /* 364116167Salc * Hasn't changed size 365116167Salc */ 366116167Salc VM_OBJECT_UNLOCK(object); 3673374Sdg return; 368116167Salc } 36938542Sluoqi nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 3709507Sdg if (nsize < object->un_pager.vnp.vnp_size) { 371116167Salc /* 372116167Salc * File has shrunk. Toss any cached pages beyond the new EOF. 373116167Salc */ 374116167Salc if (nobjsize < object->size) 37538542Sluoqi vm_object_page_remove(object, nobjsize, object->size, 376116167Salc FALSE); 3771827Sdg /* 3781827Sdg * this gets rid of garbage at the end of a page that is now 37987834Sdillon * only partially backed by the vnode. 38087834Sdillon * 38187834Sdillon * XXX for some reason (I don't know yet), if we take a 38287834Sdillon * completely invalid page and mark it partially valid 38387834Sdillon * it can screw up NFS reads, so we don't allow the case. 3841827Sdg */ 385116167Salc if ((nsize & PAGE_MASK) && 386121230Salc (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 387121230Salc m->valid != 0) { 388121230Salc int base = (int)nsize & PAGE_MASK; 389121230Salc int size = PAGE_SIZE - base; 39070374Sdillon 391121230Salc /* 392121230Salc * Clear out partial-page garbage in case 393121230Salc * the page has been mapped. 394121230Salc */ 395121230Salc pmap_zero_page_area(m, base, size); 39670374Sdillon 397121230Salc /* 398121230Salc * XXX work around SMP data integrity race 399121230Salc * by unmapping the page from user processes. 400121230Salc * The garbage we just cleared may be mapped 401121230Salc * to a user process running on another cpu 402121230Salc * and this code is not running through normal 403121230Salc * I/O channels which handle SMP issues for 404121230Salc * us, so unmap page to synchronize all cpus. 405121230Salc * 406121230Salc * XXX should vm_pager_unmap_page() have 407121230Salc * dealt with this? 408121230Salc */ 409121230Salc vm_page_lock_queues(); 410121230Salc pmap_remove_all(m); 41187834Sdillon 412121230Salc /* 413121230Salc * Clear out partial-page dirty bits. This 414121230Salc * has the side effect of setting the valid 415121230Salc * bits, but that is ok. There are a bunch 416121230Salc * of places in the VM system where we expected 417121230Salc * m->dirty == VM_PAGE_BITS_ALL. The file EOF 418121230Salc * case is one of them. If the page is still 419121230Salc * partially dirty, make it fully dirty. 420121230Salc * 421121230Salc * note that we do not clear out the valid 422121230Salc * bits. This would prevent bogus_page 423121230Salc * replacement from working properly. 424121230Salc */ 425121230Salc vm_page_set_validclean(m, base, size); 426121230Salc if (m->dirty != 0) 427121230Salc m->dirty = VM_PAGE_BITS_ALL; 428116167Salc vm_page_unlock_queues(); 4291827Sdg } 4301541Srgrimes } 43112767Sdyson object->un_pager.vnp.vnp_size = nsize; 43238542Sluoqi object->size = nobjsize; 433116167Salc VM_OBJECT_UNLOCK(object); 4341541Srgrimes} 4351541Srgrimes 4361549Srgrimes/* 4371549Srgrimes * calculate the linear (byte) disk address of specified virtual 4381549Srgrimes * file address 4391549Srgrimes */ 440163359Salcstatic int 441163359Salcvnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 442163359Salc int *run) 4431549Srgrimes{ 4445455Sdg int bsize; 4455455Sdg int err; 44612767Sdyson daddr_t vblock; 447146340Sbz daddr_t voffset; 4481549Srgrimes 449138531Salc if (address < 0) 4505455Sdg return -1; 4515455Sdg 452155384Sjeff if (vp->v_iflag & VI_DOOMED) 45311701Sdyson return -1; 45411701Sdyson 4551549Srgrimes bsize = vp->v_mount->mnt_stat.f_iosize; 4561549Srgrimes vblock = address / bsize; 4571549Srgrimes voffset = address % bsize; 4581549Srgrimes 459163359Salc err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 460163359Salc if (err == 0) { 461163359Salc if (*rtaddress != -1) 462163359Salc *rtaddress += voffset / DEV_BSIZE; 46392029Seivind if (run) { 4646151Sdg *run += 1; 4656151Sdg *run *= bsize/PAGE_SIZE; 4666151Sdg *run -= voffset/PAGE_SIZE; 4676151Sdg } 4686151Sdg } 4691549Srgrimes 470163359Salc return (err); 4711549Srgrimes} 4721549Srgrimes 4731549Srgrimes/* 47496755Strhodes * small block filesystem vnode pager input 4751549Srgrimes */ 47612820Sphkstatic int 4779507Sdgvnode_pager_input_smlfs(object, m) 4789507Sdg vm_object_t object; 4791549Srgrimes vm_page_t m; 4801549Srgrimes{ 4815455Sdg int i; 482137726Sphk struct vnode *vp; 483137726Sphk struct bufobj *bo; 4841549Srgrimes struct buf *bp; 485127926Salc struct sf_buf *sf; 486146340Sbz daddr_t fileaddr; 4871549Srgrimes vm_offset_t bsize; 4885455Sdg int error = 0; 4891549Srgrimes 4909507Sdg vp = object->handle; 491155384Sjeff if (vp->v_iflag & VI_DOOMED) 49211701Sdyson return VM_PAGER_BAD; 49311701Sdyson 4941549Srgrimes bsize = vp->v_mount->mnt_stat.f_iosize; 4951549Srgrimes 496137726Sphk VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 4971549Srgrimes 498127926Salc sf = sf_buf_alloc(m, 0); 4991549Srgrimes 5001827Sdg for (i = 0; i < PAGE_SIZE / bsize; i++) { 50186092Sdillon vm_ooffset_t address; 5021827Sdg 50345561Sdt if (vm_page_bits(i * bsize, bsize) & m->valid) 5045455Sdg continue; 5051549Srgrimes 50686092Sdillon address = IDX_TO_OFF(m->pindex) + i * bsize; 50786092Sdillon if (address >= object->un_pager.vnp.vnp_size) { 50886092Sdillon fileaddr = -1; 50986092Sdillon } else { 510163359Salc error = vnode_pager_addr(vp, address, &fileaddr, NULL); 511163359Salc if (error) 512163359Salc break; 51386092Sdillon } 5141827Sdg if (fileaddr != -1) { 51542957Sdillon bp = getpbuf(&vnode_pbuf_freecnt); 5161549Srgrimes 5171827Sdg /* build a minimal buffer header */ 51858345Sphk bp->b_iocmd = BIO_READ; 519119092Sphk bp->b_iodone = bdone; 52084827Sjhb KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 52184827Sjhb KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 52291406Sjhb bp->b_rcred = crhold(curthread->td_ucred); 52391406Sjhb bp->b_wcred = crhold(curthread->td_ucred); 524127926Salc bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 5256626Sdg bp->b_blkno = fileaddr; 526137726Sphk pbgetbo(bo, bp); 5271549Srgrimes bp->b_bcount = bsize; 5281549Srgrimes bp->b_bufsize = bsize; 52970374Sdillon bp->b_runningbufspace = bp->b_bufsize; 530148875Sssouhlal atomic_add_int(&runningbufspace, bp->b_runningbufspace); 5311827Sdg 5321827Sdg /* do the input */ 533121205Sphk bp->b_iooffset = dbtob(bp->b_blkno); 534136927Sphk bstrategy(bp); 5351549Srgrimes 536119092Sphk bwait(bp, PVM, "vnsrd"); 537119092Sphk 53858934Sphk if ((bp->b_ioflags & BIO_ERROR) != 0) 5391549Srgrimes error = EIO; 5401549Srgrimes 5411827Sdg /* 5421827Sdg * free the buffer header back to the swap buffer pool 5431827Sdg */ 544137726Sphk pbrelbo(bp); 54542957Sdillon relpbuf(bp, &vnode_pbuf_freecnt); 5461827Sdg if (error) 5471549Srgrimes break; 5485455Sdg 549121264Salc VM_OBJECT_LOCK(object); 550107189Salc vm_page_lock_queues(); 55115583Sphk vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 552107189Salc vm_page_unlock_queues(); 553121264Salc VM_OBJECT_UNLOCK(object); 5541549Srgrimes } else { 555121264Salc VM_OBJECT_LOCK(object); 556107189Salc vm_page_lock_queues(); 55715583Sphk vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 558107189Salc vm_page_unlock_queues(); 559121264Salc VM_OBJECT_UNLOCK(object); 560127926Salc bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 5611549Srgrimes } 5621549Srgrimes } 563127926Salc sf_buf_free(sf); 564107347Salc vm_page_lock_queues(); 56560755Speter pmap_clear_modify(m); 566107347Salc vm_page_unlock_queues(); 5671827Sdg if (error) { 5684207Sdg return VM_PAGER_ERROR; 5691549Srgrimes } 5701549Srgrimes return VM_PAGER_OK; 5711549Srgrimes 5721549Srgrimes} 5731549Srgrimes 5741549Srgrimes 5751549Srgrimes/* 576139296Sphk * old style vnode pager input routine 5771549Srgrimes */ 57812820Sphkstatic int 5799507Sdgvnode_pager_input_old(object, m) 5809507Sdg vm_object_t object; 5811549Srgrimes vm_page_t m; 5821549Srgrimes{ 5831541Srgrimes struct uio auio; 5841541Srgrimes struct iovec aiov; 5855455Sdg int error; 5865455Sdg int size; 587127926Salc struct sf_buf *sf; 58877398Sjhb struct vnode *vp; 5891549Srgrimes 590121495Salc VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 5911549Srgrimes error = 0; 5921827Sdg 5931549Srgrimes /* 5941549Srgrimes * Return failure if beyond current EOF 5951549Srgrimes */ 59612767Sdyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 5971549Srgrimes return VM_PAGER_BAD; 5981549Srgrimes } else { 5991549Srgrimes size = PAGE_SIZE; 60012767Sdyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 60112767Sdyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 602121495Salc vp = object->handle; 603121495Salc VM_OBJECT_UNLOCK(object); 6047178Sdg 6055455Sdg /* 6065455Sdg * Allocate a kernel virtual address and initialize so that 6075455Sdg * we can use VOP_READ/WRITE routines. 6085455Sdg */ 609127926Salc sf = sf_buf_alloc(m, 0); 6107178Sdg 611127926Salc aiov.iov_base = (caddr_t)sf_buf_kva(sf); 6121549Srgrimes aiov.iov_len = size; 6131549Srgrimes auio.uio_iov = &aiov; 6141549Srgrimes auio.uio_iovcnt = 1; 61512767Sdyson auio.uio_offset = IDX_TO_OFF(m->pindex); 6161549Srgrimes auio.uio_segflg = UIO_SYSSPACE; 6171549Srgrimes auio.uio_rw = UIO_READ; 6181549Srgrimes auio.uio_resid = size; 61983366Sjulian auio.uio_td = curthread; 6201549Srgrimes 62191406Sjhb error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 6221549Srgrimes if (!error) { 62379242Sdillon int count = size - auio.uio_resid; 6241549Srgrimes 6251549Srgrimes if (count == 0) 6261549Srgrimes error = EINVAL; 6271549Srgrimes else if (count != PAGE_SIZE) 628127926Salc bzero((caddr_t)sf_buf_kva(sf) + count, 629127926Salc PAGE_SIZE - count); 6301549Srgrimes } 631127926Salc sf_buf_free(sf); 632121230Salc 633121230Salc VM_OBJECT_LOCK(object); 6341549Srgrimes } 635107347Salc vm_page_lock_queues(); 63660755Speter pmap_clear_modify(m); 63749945Salc vm_page_undirty(m); 638121230Salc vm_page_unlock_queues(); 63939739Srvb if (!error) 64039739Srvb m->valid = VM_PAGE_BITS_ALL; 6414207Sdg return error ? VM_PAGER_ERROR : VM_PAGER_OK; 6421549Srgrimes} 6431549Srgrimes 6441549Srgrimes/* 6451549Srgrimes * generic vnode pager input routine 6461549Srgrimes */ 64710556Sdyson 64833847Smsmith/* 64976827Salfred * Local media VFS's that do not implement their own VOP_GETPAGES 65099211Srobert * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 65199211Srobert * to implement the previous behaviour. 65233847Smsmith * 65333847Smsmith * All other FS's should use the bypass to get to the local media 65433847Smsmith * backing vp's VOP_GETPAGES. 65533847Smsmith */ 65612820Sphkstatic int 6579507Sdgvnode_pager_getpages(object, m, count, reqpage) 6589507Sdg vm_object_t object; 6591549Srgrimes vm_page_t *m; 6609507Sdg int count; 6619507Sdg int reqpage; 6621549Srgrimes{ 66310556Sdyson int rtval; 66410556Sdyson struct vnode *vp; 66534403Smsmith int bytes = count * PAGE_SIZE; 666140723Sjeff int vfslocked; 66732286Sdyson 66810556Sdyson vp = object->handle; 669116279Salc VM_OBJECT_UNLOCK(object); 670140723Sjeff vfslocked = VFS_LOCK_GIANT(vp->v_mount); 67134403Smsmith rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 67276827Salfred KASSERT(rtval != EOPNOTSUPP, 67376827Salfred ("vnode_pager: FS getpages not implemented\n")); 674140723Sjeff VFS_UNLOCK_GIANT(vfslocked); 675116279Salc VM_OBJECT_LOCK(object); 67633847Smsmith return rtval; 67710556Sdyson} 67810556Sdyson 67933847Smsmith/* 68033847Smsmith * This is now called from local media FS's to operate against their 68133847Smsmith * own vnodes if they fail to implement VOP_GETPAGES. 68233847Smsmith */ 68333847Smsmithint 68433847Smsmithvnode_pager_generic_getpages(vp, m, bytecount, reqpage) 68533847Smsmith struct vnode *vp; 68610556Sdyson vm_page_t *m; 68733847Smsmith int bytecount; 68810556Sdyson int reqpage; 68910556Sdyson{ 69033847Smsmith vm_object_t object; 69112767Sdyson vm_offset_t kva; 69234206Sdyson off_t foff, tfoff, nextoff; 693146340Sbz int i, j, size, bsize, first; 694163140Salc daddr_t firstaddr, reqblock; 695137726Sphk struct bufobj *bo; 6966151Sdg int runpg; 6976151Sdg int runend; 6987178Sdg struct buf *bp; 69933847Smsmith int count; 700163210Salc int error; 7011549Srgrimes 70233847Smsmith object = vp->v_object; 70333847Smsmith count = bytecount / PAGE_SIZE; 70433847Smsmith 705137726Sphk KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 706137726Sphk ("vnode_pager_generic_getpages does not support devices")); 707155384Sjeff if (vp->v_iflag & VI_DOOMED) 70811701Sdyson return VM_PAGER_BAD; 70911701Sdyson 7101549Srgrimes bsize = vp->v_mount->mnt_stat.f_iosize; 7111549Srgrimes 7121549Srgrimes /* get the UNDERLYING device for the file with VOP_BMAP() */ 7131827Sdg 7141549Srgrimes /* 7151827Sdg * originally, we did not check for an error return value -- assuming 7161827Sdg * an fs always has a bmap entry point -- that assumption is wrong!!! 7171549Srgrimes */ 71812767Sdyson foff = IDX_TO_OFF(m[reqpage]->pindex); 7191827Sdg 7201549Srgrimes /* 7211887Sdg * if we can't bmap, use old VOP code 7221549Srgrimes */ 723163210Salc error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL); 724163210Salc if (error == EOPNOTSUPP) { 725116512Salc VM_OBJECT_LOCK(object); 726100832Salc vm_page_lock_queues(); 727100832Salc for (i = 0; i < count; i++) 728100832Salc if (i != reqpage) 72975692Salfred vm_page_free(m[i]); 730100832Salc vm_page_unlock_queues(); 7313612Sdg cnt.v_vnodein++; 7323612Sdg cnt.v_vnodepgsin++; 733121495Salc error = vnode_pager_input_old(object, m[reqpage]); 734121495Salc VM_OBJECT_UNLOCK(object); 735121495Salc return (error); 736163210Salc } else if (error != 0) { 737163210Salc VM_OBJECT_LOCK(object); 738163210Salc vm_page_lock_queues(); 739163210Salc for (i = 0; i < count; i++) 740163210Salc if (i != reqpage) 741163210Salc vm_page_free(m[i]); 742163210Salc vm_page_unlock_queues(); 743163210Salc VM_OBJECT_UNLOCK(object); 744163210Salc return (VM_PAGER_ERROR); 7451549Srgrimes 7461827Sdg /* 7471827Sdg * if the blocksize is smaller than a page size, then use 7481827Sdg * special small filesystem code. NFS sometimes has a small 7491827Sdg * blocksize, but it can handle large reads itself. 7501827Sdg */ 7511827Sdg } else if ((PAGE_SIZE / bsize) > 1 && 75238866Sbde (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 753116512Salc VM_OBJECT_LOCK(object); 754100832Salc vm_page_lock_queues(); 755100832Salc for (i = 0; i < count; i++) 756100832Salc if (i != reqpage) 75775692Salfred vm_page_free(m[i]); 758100832Salc vm_page_unlock_queues(); 759116512Salc VM_OBJECT_UNLOCK(object); 7603612Sdg cnt.v_vnodein++; 7613612Sdg cnt.v_vnodepgsin++; 7629507Sdg return vnode_pager_input_smlfs(object, m[reqpage]); 7631549Srgrimes } 76445347Sjulian 7651549Srgrimes /* 76645347Sjulian * If we have a completely valid page available to us, we can 76745347Sjulian * clean up and return. Otherwise we have to re-read the 76845347Sjulian * media. 7691549Srgrimes */ 770121227Salc VM_OBJECT_LOCK(object); 77145347Sjulian if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 772100832Salc vm_page_lock_queues(); 773100832Salc for (i = 0; i < count; i++) 7745455Sdg if (i != reqpage) 77575692Salfred vm_page_free(m[i]); 776100832Salc vm_page_unlock_queues(); 777116512Salc VM_OBJECT_UNLOCK(object); 7785455Sdg return VM_PAGER_OK; 779163140Salc } else if (reqblock == -1) { 780163140Salc pmap_zero_page(m[reqpage]); 781163140Salc vm_page_undirty(m[reqpage]); 782163140Salc m[reqpage]->valid = VM_PAGE_BITS_ALL; 783163140Salc vm_page_lock_queues(); 784163140Salc for (i = 0; i < count; i++) 785163140Salc if (i != reqpage) 786163140Salc vm_page_free(m[i]); 787163140Salc vm_page_unlock_queues(); 788163140Salc VM_OBJECT_UNLOCK(object); 789163140Salc return (VM_PAGER_OK); 7901549Srgrimes } 79145347Sjulian m[reqpage]->valid = 0; 792121227Salc VM_OBJECT_UNLOCK(object); 7937178Sdg 7945455Sdg /* 7955455Sdg * here on direct device I/O 7965455Sdg */ 79792029Seivind firstaddr = -1; 7981549Srgrimes 7991549Srgrimes /* 8006151Sdg * calculate the run that includes the required page 8011549Srgrimes */ 80292029Seivind for (first = 0, i = 0; i < count; i = runend) { 803163359Salc if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr, 804163359Salc &runpg) != 0) { 805163359Salc VM_OBJECT_LOCK(object); 806163359Salc vm_page_lock_queues(); 807163359Salc for (; i < count; i++) 808163359Salc if (i != reqpage) 809163359Salc vm_page_free(m[i]); 810163359Salc vm_page_unlock_queues(); 811163359Salc VM_OBJECT_UNLOCK(object); 812163359Salc return (VM_PAGER_ERROR); 813163359Salc } 8146151Sdg if (firstaddr == -1) { 815116512Salc VM_OBJECT_LOCK(object); 8169507Sdg if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 817146340Sbz panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx", 818146340Sbz (intmax_t)firstaddr, (uintmax_t)(foff >> 32), 819106603Smux (uintmax_t)foff, 820106603Smux (uintmax_t) 821106603Smux (object->un_pager.vnp.vnp_size >> 32), 822106603Smux (uintmax_t)object->un_pager.vnp.vnp_size); 8236151Sdg } 824100832Salc vm_page_lock_queues(); 82575692Salfred vm_page_free(m[i]); 826100832Salc vm_page_unlock_queues(); 827116512Salc VM_OBJECT_UNLOCK(object); 8286151Sdg runend = i + 1; 8296151Sdg first = runend; 8306151Sdg continue; 8311549Srgrimes } 8326151Sdg runend = i + runpg; 8339507Sdg if (runend <= reqpage) { 834116512Salc VM_OBJECT_LOCK(object); 835100832Salc vm_page_lock_queues(); 836100832Salc for (j = i; j < runend; j++) 83775692Salfred vm_page_free(m[j]); 838100832Salc vm_page_unlock_queues(); 839116512Salc VM_OBJECT_UNLOCK(object); 8401549Srgrimes } else { 8419507Sdg if (runpg < (count - first)) { 842116512Salc VM_OBJECT_LOCK(object); 843100832Salc vm_page_lock_queues(); 8449507Sdg for (i = first + runpg; i < count; i++) 84575692Salfred vm_page_free(m[i]); 846100832Salc vm_page_unlock_queues(); 847116512Salc VM_OBJECT_UNLOCK(object); 8486151Sdg count = first + runpg; 8496151Sdg } 8506151Sdg break; 8511549Srgrimes } 8526151Sdg first = runend; 8531549Srgrimes } 8541549Srgrimes 8551549Srgrimes /* 8561827Sdg * the first and last page have been calculated now, move input pages 8571827Sdg * to be zero based... 8581549Srgrimes */ 8591549Srgrimes if (first != 0) { 8601549Srgrimes for (i = first; i < count; i++) { 8611549Srgrimes m[i - first] = m[i]; 8621549Srgrimes } 8631549Srgrimes count -= first; 8641549Srgrimes reqpage -= first; 8651549Srgrimes } 8666151Sdg 8671549Srgrimes /* 8681549Srgrimes * calculate the file virtual address for the transfer 8691549Srgrimes */ 87012767Sdyson foff = IDX_TO_OFF(m[0]->pindex); 8711827Sdg 8721549Srgrimes /* 8731549Srgrimes * calculate the size of the transfer 8741549Srgrimes */ 8751549Srgrimes size = count * PAGE_SIZE; 876134892Sphk KASSERT(count > 0, ("zero count")); 8779507Sdg if ((foff + size) > object->un_pager.vnp.vnp_size) 8789507Sdg size = object->un_pager.vnp.vnp_size - foff; 879134892Sphk KASSERT(size > 0, ("zero size")); 8801549Srgrimes 8811549Srgrimes /* 88251340Sdillon * round up physical size for real devices. 8831549Srgrimes */ 884137726Sphk if (1) { 885137726Sphk int secmask = bo->bo_bsize - 1; 886136977Sphk KASSERT(secmask < PAGE_SIZE && secmask > 0, 887136977Sphk ("vnode_pager_generic_getpages: sector size %d too large", 888136977Sphk secmask + 1)); 88951340Sdillon size = (size + secmask) & ~secmask; 89051340Sdillon } 8911549Srgrimes 89242957Sdillon bp = getpbuf(&vnode_pbuf_freecnt); 8935455Sdg kva = (vm_offset_t) bp->b_data; 8941887Sdg 8951549Srgrimes /* 8961549Srgrimes * and map the pages to be read into the kva 8971549Srgrimes */ 8981887Sdg pmap_qenter(kva, m, count); 8991549Srgrimes 9001549Srgrimes /* build a minimal buffer header */ 90158345Sphk bp->b_iocmd = BIO_READ; 902119092Sphk bp->b_iodone = bdone; 90384827Sjhb KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 90484827Sjhb KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 90591406Sjhb bp->b_rcred = crhold(curthread->td_ucred); 90691406Sjhb bp->b_wcred = crhold(curthread->td_ucred); 9076626Sdg bp->b_blkno = firstaddr; 908137726Sphk pbgetbo(bo, bp); 9091549Srgrimes bp->b_bcount = size; 9101549Srgrimes bp->b_bufsize = size; 91170374Sdillon bp->b_runningbufspace = bp->b_bufsize; 912148875Sssouhlal atomic_add_int(&runningbufspace, bp->b_runningbufspace); 9131549Srgrimes 9143612Sdg cnt.v_vnodein++; 9153612Sdg cnt.v_vnodepgsin += count; 9163612Sdg 9171549Srgrimes /* do the input */ 918121205Sphk bp->b_iooffset = dbtob(bp->b_blkno); 919136927Sphk bstrategy(bp); 9203612Sdg 921119092Sphk bwait(bp, PVM, "vnread"); 9221549Srgrimes 92358934Sphk if ((bp->b_ioflags & BIO_ERROR) != 0) 9241549Srgrimes error = EIO; 9251549Srgrimes 9261549Srgrimes if (!error) { 9271549Srgrimes if (size != count * PAGE_SIZE) 9281827Sdg bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 9291549Srgrimes } 9305455Sdg pmap_qremove(kva, count); 9311549Srgrimes 9321549Srgrimes /* 9331549Srgrimes * free the buffer header back to the swap buffer pool 9341549Srgrimes */ 935137726Sphk pbrelbo(bp); 93642957Sdillon relpbuf(bp, &vnode_pbuf_freecnt); 9371549Srgrimes 938116512Salc VM_OBJECT_LOCK(object); 939100736Salc vm_page_lock_queues(); 94034206Sdyson for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 94134206Sdyson vm_page_t mt; 94234206Sdyson 94334206Sdyson nextoff = tfoff + PAGE_SIZE; 94434206Sdyson mt = m[i]; 94534206Sdyson 94647239Sdt if (nextoff <= object->un_pager.vnp.vnp_size) { 94745347Sjulian /* 94845347Sjulian * Read filled up entire page. 94945347Sjulian */ 95034206Sdyson mt->valid = VM_PAGE_BITS_ALL; 95149945Salc vm_page_undirty(mt); /* should be an assert? XXX */ 95260755Speter pmap_clear_modify(mt); 95334206Sdyson } else { 95445347Sjulian /* 95545347Sjulian * Read did not fill up entire page. Since this 95645347Sjulian * is getpages, the page may be mapped, so we have 95745347Sjulian * to zero the invalid portions of the page even 95845347Sjulian * though we aren't setting them valid. 95945347Sjulian * 96045347Sjulian * Currently we do not set the entire page valid, 96145347Sjulian * we just try to clear the piece that we couldn't 96245347Sjulian * read. 96345347Sjulian */ 96447239Sdt vm_page_set_validclean(mt, 0, 96547239Sdt object->un_pager.vnp.vnp_size - tfoff); 96646349Salc /* handled by vm_fault now */ 96746349Salc /* vm_page_zero_invalid(mt, FALSE); */ 96834206Sdyson } 96934206Sdyson 9701549Srgrimes if (i != reqpage) { 9711827Sdg 9721549Srgrimes /* 9731827Sdg * whether or not to leave the page activated is up in 9741827Sdg * the air, but we should put the page on a page queue 9751827Sdg * somewhere. (it already is in the object). Result: 97658634Scharnier * It appears that empirical results show that 9771827Sdg * deactivating pages is best. 9781549Srgrimes */ 9791827Sdg 9801549Srgrimes /* 9811827Sdg * just in case someone was asking for this page we 9821827Sdg * now tell them that it is ok to use 9831549Srgrimes */ 9841549Srgrimes if (!error) { 985161125Salc if (mt->oflags & VPO_WANTED) 98634206Sdyson vm_page_activate(mt); 98733109Sdyson else 98834206Sdyson vm_page_deactivate(mt); 98938799Sdfr vm_page_wakeup(mt); 9901549Srgrimes } else { 99175692Salfred vm_page_free(mt); 9921549Srgrimes } 9931549Srgrimes } 9941549Srgrimes } 995100736Salc vm_page_unlock_queues(); 996116512Salc VM_OBJECT_UNLOCK(object); 9971549Srgrimes if (error) { 9989507Sdg printf("vnode_pager_getpages: I/O read error\n"); 9991549Srgrimes } 10004207Sdg return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 10011549Srgrimes} 10021549Srgrimes 100333847Smsmith/* 100433847Smsmith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 100533847Smsmith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 100633847Smsmith * vnode_pager_generic_putpages() to implement the previous behaviour. 100733847Smsmith * 100833847Smsmith * All other FS's should use the bypass to get to the local media 100933847Smsmith * backing vp's VOP_PUTPAGES. 101033847Smsmith */ 101143129Sdillonstatic void 101210556Sdysonvnode_pager_putpages(object, m, count, sync, rtvals) 101310556Sdyson vm_object_t object; 101410556Sdyson vm_page_t *m; 101510556Sdyson int count; 101610556Sdyson boolean_t sync; 101710556Sdyson int *rtvals; 101810556Sdyson{ 101910556Sdyson int rtval; 102010556Sdyson struct vnode *vp; 102162976Smckusick struct mount *mp; 102234403Smsmith int bytes = count * PAGE_SIZE; 102318973Sdyson 102444321Salc /* 102544321Salc * Force synchronous operation if we are extremely low on memory 102644321Salc * to prevent a low-memory deadlock. VOP operations often need to 102744321Salc * allocate more memory to initiate the I/O ( i.e. do a BMAP 102844321Salc * operation ). The swapper handles the case by limiting the amount 102944321Salc * of asynchronous I/O, but that sort of solution doesn't scale well 103044321Salc * for the vnode pager without a lot of work. 103144321Salc * 103244321Salc * Also, the backing vnode's iodone routine may not wake the pageout 103344321Salc * daemon up. This should be probably be addressed XXX. 103444321Salc */ 103544321Salc 103644321Salc if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 103744321Salc sync |= OBJPC_SYNC; 103844321Salc 103944321Salc /* 104044321Salc * Call device-specific putpages function 104144321Salc */ 104210556Sdyson vp = object->handle; 1043121455Salc VM_OBJECT_UNLOCK(object); 104462976Smckusick if (vp->v_type != VREG) 104562976Smckusick mp = NULL; 104634403Smsmith rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 104776827Salfred KASSERT(rtval != EOPNOTSUPP, 104876827Salfred ("vnode_pager: stale FS putpages\n")); 1049121455Salc VM_OBJECT_LOCK(object); 105010556Sdyson} 105110556Sdyson 105233847Smsmith 10531549Srgrimes/* 105433847Smsmith * This is now called from local media FS's to operate against their 105545057Seivind * own vnodes if they fail to implement VOP_PUTPAGES. 105670374Sdillon * 105770374Sdillon * This is typically called indirectly via the pageout daemon and 105870374Sdillon * clustering has already typically occured, so in general we ask the 105970374Sdillon * underlying filesystem to write the data out asynchronously rather 106070374Sdillon * then delayed. 10611549Srgrimes */ 106233847Smsmithint 106334206Sdysonvnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 106433847Smsmith struct vnode *vp; 10651549Srgrimes vm_page_t *m; 106633847Smsmith int bytecount; 106734206Sdyson int flags; 10685455Sdg int *rtvals; 10691549Srgrimes{ 10707695Sdg int i; 107133847Smsmith vm_object_t object; 107233847Smsmith int count; 10731549Srgrimes 10747695Sdg int maxsize, ncount; 107512767Sdyson vm_ooffset_t poffset; 10767695Sdg struct uio auio; 10777695Sdg struct iovec aiov; 10787695Sdg int error; 107934206Sdyson int ioflags; 1080151951Sps int ppscheck = 0; 1081151951Sps static struct timeval lastfail; 1082151951Sps static int curfail; 10831549Srgrimes 108433847Smsmith object = vp->v_object; 108533847Smsmith count = bytecount / PAGE_SIZE; 108633847Smsmith 10871827Sdg for (i = 0; i < count; i++) 10881549Srgrimes rtvals[i] = VM_PAGER_AGAIN; 10891549Srgrimes 1090138406Salc if ((int64_t)m[0]->pindex < 0) { 1091119544Smarcel printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n", 1092119544Smarcel (long)m[0]->pindex, (u_long)m[0]->dirty); 10937695Sdg rtvals[0] = VM_PAGER_BAD; 10947695Sdg return VM_PAGER_BAD; 10955455Sdg } 10967178Sdg 10977695Sdg maxsize = count * PAGE_SIZE; 10987695Sdg ncount = count; 10991549Srgrimes 110012767Sdyson poffset = IDX_TO_OFF(m[0]->pindex); 110184854Sdillon 110284854Sdillon /* 110384854Sdillon * If the page-aligned write is larger then the actual file we 110484854Sdillon * have to invalidate pages occuring beyond the file EOF. However, 110584854Sdillon * there is an edge case where a file may not be page-aligned where 110684854Sdillon * the last page is partially invalid. In this case the filesystem 110784854Sdillon * may not properly clear the dirty bits for the entire page (which 110884854Sdillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 110984854Sdillon * With the page locked we are free to fix-up the dirty bits here. 111087834Sdillon * 111187834Sdillon * We do not under any circumstances truncate the valid bits, as 111287834Sdillon * this will screw up bogus page replacement. 111384854Sdillon */ 111412767Sdyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 111584854Sdillon if (object->un_pager.vnp.vnp_size > poffset) { 111684854Sdillon int pgoff; 111784854Sdillon 111812767Sdyson maxsize = object->un_pager.vnp.vnp_size - poffset; 111984854Sdillon ncount = btoc(maxsize); 112084854Sdillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1121119370Salc vm_page_lock_queues(); 112284854Sdillon vm_page_clear_dirty(m[ncount - 1], pgoff, 112384854Sdillon PAGE_SIZE - pgoff); 1124119370Salc vm_page_unlock_queues(); 112584854Sdillon } 112684854Sdillon } else { 11278585Sdg maxsize = 0; 112884854Sdillon ncount = 0; 112984854Sdillon } 11308585Sdg if (ncount < count) { 11318585Sdg for (i = ncount; i < count; i++) { 11327695Sdg rtvals[i] = VM_PAGER_BAD; 11331549Srgrimes } 11341549Srgrimes } 11351541Srgrimes } 11367695Sdg 113770374Sdillon /* 113870374Sdillon * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 113970374Sdillon * rather then a bdwrite() to prevent paging I/O from saturating 1140108358Sdillon * the buffer cache. Dummy-up the sequential heuristic to cause 1141108358Sdillon * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 1142108358Sdillon * the system decides how to cluster. 114370374Sdillon */ 114434206Sdyson ioflags = IO_VMIO; 1145108358Sdillon if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 1146108358Sdillon ioflags |= IO_SYNC; 1147108358Sdillon else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 1148108358Sdillon ioflags |= IO_ASYNC; 114934206Sdyson ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 1150108358Sdillon ioflags |= IO_SEQMAX << IO_SEQSHIFT; 11511827Sdg 11527695Sdg aiov.iov_base = (caddr_t) 0; 11537695Sdg aiov.iov_len = maxsize; 11547695Sdg auio.uio_iov = &aiov; 11557695Sdg auio.uio_iovcnt = 1; 115612767Sdyson auio.uio_offset = poffset; 11577695Sdg auio.uio_segflg = UIO_NOCOPY; 11587695Sdg auio.uio_rw = UIO_WRITE; 11597695Sdg auio.uio_resid = maxsize; 116083366Sjulian auio.uio_td = (struct thread *) 0; 116191406Sjhb error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 11623612Sdg cnt.v_vnodeout++; 11637695Sdg cnt.v_vnodepgsout += ncount; 11643612Sdg 11658585Sdg if (error) { 1166151951Sps if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1))) 1167151951Sps printf("vnode_pager_putpages: I/O error %d\n", error); 11687695Sdg } 11698585Sdg if (auio.uio_resid) { 1170151951Sps if (ppscheck || ppsratecheck(&lastfail, &curfail, 1)) 1171151951Sps printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1172151951Sps auio.uio_resid, (u_long)m[0]->pindex); 11737695Sdg } 117433936Sdyson for (i = 0; i < ncount; i++) { 117533936Sdyson rtvals[i] = VM_PAGER_OK; 11767695Sdg } 11777695Sdg return rtvals[0]; 11787695Sdg} 11791549Srgrimes 11807695Sdgstruct vnode * 1181120183Salcvnode_pager_lock(vm_object_t first_object) 11829507Sdg{ 1183120183Salc struct vnode *vp; 1184120183Salc vm_object_t backing_object, object; 118522521Sdyson 1186120183Salc VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 1187120183Salc for (object = first_object; object != NULL; object = backing_object) { 1188120183Salc if (object->type != OBJT_VNODE) { 1189120183Salc if ((backing_object = object->backing_object) != NULL) 1190120183Salc VM_OBJECT_LOCK(backing_object); 1191120183Salc if (object != first_object) 1192120183Salc VM_OBJECT_UNLOCK(object); 11937695Sdg continue; 1194120183Salc } 1195120183Salc retry: 119677094Sjhb if (object->flags & OBJ_DEAD) { 1197120183Salc if (object != first_object) 1198120183Salc VM_OBJECT_UNLOCK(object); 119932585Sdyson return NULL; 120077094Sjhb } 1201120183Salc vp = object->handle; 1202120183Salc VI_LOCK(vp); 1203120183Salc VM_OBJECT_UNLOCK(object); 1204120183Salc if (first_object != object) 1205120183Salc VM_OBJECT_UNLOCK(first_object); 1206145826Sjeff VFS_ASSERT_GIANT(vp->v_mount); 1207144367Sjeff if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | 1208120183Salc LK_RETRY | LK_SHARED, curthread)) { 1209120183Salc VM_OBJECT_LOCK(first_object); 1210120183Salc if (object != first_object) 1211120183Salc VM_OBJECT_LOCK(object); 1212120183Salc if (object->type != OBJT_VNODE) { 1213120183Salc if (object != first_object) 1214120183Salc VM_OBJECT_UNLOCK(object); 121534611Sdyson return NULL; 1216120183Salc } 121732585Sdyson printf("vnode_pager_lock: retrying\n"); 1218120183Salc goto retry; 121932585Sdyson } 1220120183Salc VM_OBJECT_LOCK(first_object); 1221120183Salc return (vp); 12221549Srgrimes } 12239507Sdg return NULL; 12247695Sdg} 1225