1df8bae1dSRodney W. Grimes /* 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 41c3aac50fSPeter Wemm * $FreeBSD$ 42df8bae1dSRodney W. Grimes */ 43df8bae1dSRodney W. Grimes 44df8bae1dSRodney W. Grimes /* 45df8bae1dSRodney W. Grimes * Page to/from files (vnodes). 46df8bae1dSRodney W. Grimes */ 47df8bae1dSRodney W. Grimes 4826f9a767SRodney W. Grimes /* 4926f9a767SRodney W. Grimes * TODO: 5024a1cce3SDavid Greenman * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 51f6b04d2bSDavid Greenman * greatly re-simplify the vnode_pager. 5226f9a767SRodney W. Grimes */ 5326f9a767SRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <sys/param.h> 55df8bae1dSRodney W. Grimes #include <sys/systm.h> 56df8bae1dSRodney W. Grimes #include <sys/proc.h> 57df8bae1dSRodney W. Grimes #include <sys/vnode.h> 58df8bae1dSRodney W. Grimes #include <sys/mount.h> 599626b608SPoul-Henning Kamp #include <sys/bio.h> 6024a1cce3SDavid Greenman #include <sys/buf.h> 61efeaf95aSDavid Greenman #include <sys/vmmeter.h> 6224579ca1SMatthew Dillon #include <sys/conf.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #include <vm/vm.h> 65efeaf95aSDavid Greenman #include <vm/vm_object.h> 66df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 6724a1cce3SDavid Greenman #include <vm/vm_pager.h> 681efb74fbSJohn Dyson #include <vm/vm_map.h> 69df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h> 70efeaf95aSDavid Greenman #include <vm/vm_extern.h> 71df8bae1dSRodney W. Grimes 72f708ef1bSPoul-Henning Kamp static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address, 730b8253a7SBruce Evans int *run)); 74f708ef1bSPoul-Henning Kamp static void vnode_pager_iodone __P((struct buf *bp)); 75f708ef1bSPoul-Henning Kamp static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m)); 76f708ef1bSPoul-Henning Kamp static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m)); 77f708ef1bSPoul-Henning Kamp static void vnode_pager_dealloc __P((vm_object_t)); 78f708ef1bSPoul-Henning Kamp static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 79e4542174SMatthew Dillon static void vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *)); 80f708ef1bSPoul-Henning Kamp static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 810b8253a7SBruce Evans 82df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 8324a1cce3SDavid Greenman NULL, 84df8bae1dSRodney W. Grimes vnode_pager_alloc, 85df8bae1dSRodney W. Grimes vnode_pager_dealloc, 8624a1cce3SDavid Greenman vnode_pager_getpages, 8724a1cce3SDavid Greenman vnode_pager_putpages, 8824a1cce3SDavid Greenman vnode_pager_haspage, 8924a1cce3SDavid Greenman NULL 90df8bae1dSRodney W. Grimes }; 91df8bae1dSRodney W. Grimes 921c7c3c6aSMatthew Dillon int vnode_pbuf_freecnt = -1; /* start out unlimited */ 931c7c3c6aSMatthew Dillon 94170db9c6SJohn Dyson 95df8bae1dSRodney W. Grimes /* 96df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 97df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 98df8bae1dSRodney W. Grimes */ 9924a1cce3SDavid Greenman vm_object_t 1006cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 101b9dcd593SBruce Evans vm_ooffset_t offset) 102df8bae1dSRodney W. Grimes { 10306cb7259SDavid Greenman vm_object_t object; 104df8bae1dSRodney W. Grimes struct vnode *vp; 105df8bae1dSRodney W. Grimes 106e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 107df8bae1dSRodney W. Grimes /* 108df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 109df8bae1dSRodney W. Grimes */ 110df8bae1dSRodney W. Grimes if (handle == NULL) 111df8bae1dSRodney W. Grimes return (NULL); 112df8bae1dSRodney W. Grimes 1131c7c3c6aSMatthew Dillon /* 1141c7c3c6aSMatthew Dillon * XXX hack - This initialization should be put somewhere else. 1151c7c3c6aSMatthew Dillon */ 1161c7c3c6aSMatthew Dillon if (vnode_pbuf_freecnt < 0) { 1171c7c3c6aSMatthew Dillon vnode_pbuf_freecnt = nswbuf / 2 + 1; 1181c7c3c6aSMatthew Dillon } 1191c7c3c6aSMatthew Dillon 120df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 12139d38f93SDavid Greenman 12239d38f93SDavid Greenman /* 12339d38f93SDavid Greenman * Prevent race condition when allocating the object. This 12439d38f93SDavid Greenman * can happen with NFS vnodes since the nfsnode isn't locked. 12539d38f93SDavid Greenman */ 12623955314SAlfred Perlstein mtx_unlock(&vm_mtx); 12739d38f93SDavid Greenman while (vp->v_flag & VOLOCK) { 12839d38f93SDavid Greenman vp->v_flag |= VOWANT; 12939d38f93SDavid Greenman tsleep(vp, PVM, "vnpobj", 0); 13039d38f93SDavid Greenman } 13139d38f93SDavid Greenman vp->v_flag |= VOLOCK; 13223955314SAlfred Perlstein mtx_lock(&vm_mtx); 13339d38f93SDavid Greenman 13439d38f93SDavid Greenman /* 13539d38f93SDavid Greenman * If the object is being terminated, wait for it to 13639d38f93SDavid Greenman * go away. 13739d38f93SDavid Greenman */ 138bd7e5f99SJohn Dyson while (((object = vp->v_object) != NULL) && 139bd7e5f99SJohn Dyson (object->flags & OBJ_DEAD)) { 14023955314SAlfred Perlstein msleep(object, &vm_mtx, PVM, "vadead", 0); 14124a1cce3SDavid Greenman } 1420d94caffSDavid Greenman 1432be70f79SJohn Dyson if (vp->v_usecount == 0) 1442be70f79SJohn Dyson panic("vnode_pager_alloc: no vnode reference"); 1452be70f79SJohn Dyson 14624a1cce3SDavid Greenman if (object == NULL) { 147df8bae1dSRodney W. Grimes /* 148df8bae1dSRodney W. Grimes * And an object of the appropriate size 149df8bae1dSRodney W. Grimes */ 1506cde7a16SDavid Greenman object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 151ad5dd234SJohn Dyson object->flags = 0; 152bbc0ec52SDavid Greenman 1536cde7a16SDavid Greenman object->un_pager.vnp.vnp_size = size; 15426f9a767SRodney W. Grimes 15524a1cce3SDavid Greenman object->handle = handle; 15624a1cce3SDavid Greenman vp->v_object = object; 15795e5e988SJohn Dyson vp->v_usecount++; 158df8bae1dSRodney W. Grimes } else { 15995e5e988SJohn Dyson object->ref_count++; 16095e5e988SJohn Dyson vp->v_usecount++; 161df8bae1dSRodney W. Grimes } 16239d38f93SDavid Greenman 16323955314SAlfred Perlstein mtx_unlock(&vm_mtx); 16439d38f93SDavid Greenman vp->v_flag &= ~VOLOCK; 16539d38f93SDavid Greenman if (vp->v_flag & VOWANT) { 16639d38f93SDavid Greenman vp->v_flag &= ~VOWANT; 16739d38f93SDavid Greenman wakeup(vp); 16839d38f93SDavid Greenman } 16923955314SAlfred Perlstein mtx_lock(&vm_mtx); 17024a1cce3SDavid Greenman return (object); 171df8bae1dSRodney W. Grimes } 172df8bae1dSRodney W. Grimes 173f708ef1bSPoul-Henning Kamp static void 17424a1cce3SDavid Greenman vnode_pager_dealloc(object) 1750d94caffSDavid Greenman vm_object_t object; 17624a1cce3SDavid Greenman { 17724a1cce3SDavid Greenman register struct vnode *vp = object->handle; 178df8bae1dSRodney W. Grimes 179e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 18024a1cce3SDavid Greenman if (vp == NULL) 18124a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 18224a1cce3SDavid Greenman 18366095752SJohn Dyson vm_object_pip_wait(object, "vnpdea"); 18424a1cce3SDavid Greenman 18524a1cce3SDavid Greenman object->handle = NULL; 18695461b45SJohn Dyson object->type = OBJT_DEAD; 187aa2cabb9SDavid Greenman vp->v_object = NULL; 18895e5e988SJohn Dyson vp->v_flag &= ~(VTEXT | VOBJBUF); 189df8bae1dSRodney W. Grimes } 19026f9a767SRodney W. Grimes 191f708ef1bSPoul-Henning Kamp static boolean_t 192a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after) 19324a1cce3SDavid Greenman vm_object_t object; 194a316d390SJohn Dyson vm_pindex_t pindex; 19524a1cce3SDavid Greenman int *before; 19624a1cce3SDavid Greenman int *after; 197df8bae1dSRodney W. Grimes { 19824a1cce3SDavid Greenman struct vnode *vp = object->handle; 199df8bae1dSRodney W. Grimes daddr_t bn; 2003af76890SPoul-Henning Kamp int err; 201170db9c6SJohn Dyson daddr_t reqblock; 2022c4488fcSJohn Dyson int poff; 2032c4488fcSJohn Dyson int bsize; 204d63596ceSJohn Dyson int pagesperblock, blocksperpage; 205df8bae1dSRodney W. Grimes 206e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 20724579ca1SMatthew Dillon /* 20824579ca1SMatthew Dillon * If no vp or vp is doomed or marked transparent to VM, we do not 20924579ca1SMatthew Dillon * have the page. 21024579ca1SMatthew Dillon */ 21147221757SJohn Dyson if ((vp == NULL) || (vp->v_flag & VDOOMED)) 21247221757SJohn Dyson return FALSE; 21347221757SJohn Dyson 214df8bae1dSRodney W. Grimes /* 2150d94caffSDavid Greenman * If filesystem no longer mounted or offset beyond end of file we do 2160d94caffSDavid Greenman * not have the page. 217df8bae1dSRodney W. Grimes */ 218a316d390SJohn Dyson if ((vp->v_mount == NULL) || 219a316d390SJohn Dyson (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 2204abc71c0SDavid Greenman return FALSE; 221df8bae1dSRodney W. Grimes 222eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 223170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 224d63596ceSJohn Dyson blocksperpage = 0; 225d63596ceSJohn Dyson if (pagesperblock > 0) { 226a316d390SJohn Dyson reqblock = pindex / pagesperblock; 227d63596ceSJohn Dyson } else { 228d63596ceSJohn Dyson blocksperpage = (PAGE_SIZE / bsize); 229d63596ceSJohn Dyson reqblock = pindex * blocksperpage; 230d63596ceSJohn Dyson } 23123955314SAlfred Perlstein mtx_unlock(&vm_mtx); 232170db9c6SJohn Dyson err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 233170db9c6SJohn Dyson after, before); 23423955314SAlfred Perlstein mtx_lock(&vm_mtx); 2350d94caffSDavid Greenman if (err) 23624a1cce3SDavid Greenman return TRUE; 2376eab77f2SJohn Dyson if ( bn == -1) 238ced399eeSJohn Dyson return FALSE; 239d63596ceSJohn Dyson if (pagesperblock > 0) { 240a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 241170db9c6SJohn Dyson if (before) { 242170db9c6SJohn Dyson *before *= pagesperblock; 243170db9c6SJohn Dyson *before += poff; 244170db9c6SJohn Dyson } 245170db9c6SJohn Dyson if (after) { 246b1fc01b7SJohn Dyson int numafter; 247170db9c6SJohn Dyson *after *= pagesperblock; 248b1fc01b7SJohn Dyson numafter = pagesperblock - (poff + 1); 249a316d390SJohn Dyson if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 250a316d390SJohn Dyson numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 251b1fc01b7SJohn Dyson } 252b1fc01b7SJohn Dyson *after += numafter; 253170db9c6SJohn Dyson } 254d63596ceSJohn Dyson } else { 255d63596ceSJohn Dyson if (before) { 256d63596ceSJohn Dyson *before /= blocksperpage; 257d63596ceSJohn Dyson } 258d63596ceSJohn Dyson 259d63596ceSJohn Dyson if (after) { 260d63596ceSJohn Dyson *after /= blocksperpage; 261d63596ceSJohn Dyson } 262d63596ceSJohn Dyson } 263ced399eeSJohn Dyson return TRUE; 264df8bae1dSRodney W. Grimes } 265df8bae1dSRodney W. Grimes 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 26824a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 269df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 270df8bae1dSRodney W. Grimes * 271df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 272df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 273df8bae1dSRodney W. Grimes */ 274df8bae1dSRodney W. Grimes void 275df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize) 276df8bae1dSRodney W. Grimes struct vnode *vp; 277a316d390SJohn Dyson vm_ooffset_t nsize; 278df8bae1dSRodney W. Grimes { 279c576d121SLuoqi Chen vm_pindex_t nobjsize; 28024a1cce3SDavid Greenman vm_object_t object = vp->v_object; 281df8bae1dSRodney W. Grimes 28224a1cce3SDavid Greenman if (object == NULL) 283df8bae1dSRodney W. Grimes return; 284bbc0ec52SDavid Greenman 285df8bae1dSRodney W. Grimes /* 286df8bae1dSRodney W. Grimes * Hasn't changed size 287df8bae1dSRodney W. Grimes */ 28824a1cce3SDavid Greenman if (nsize == object->un_pager.vnp.vnp_size) 289df8bae1dSRodney W. Grimes return; 290bbc0ec52SDavid Greenman 291c576d121SLuoqi Chen nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 292c576d121SLuoqi Chen 293df8bae1dSRodney W. Grimes /* 294bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 295df8bae1dSRodney W. Grimes */ 29624a1cce3SDavid Greenman if (nsize < object->un_pager.vnp.vnp_size) { 29723955314SAlfred Perlstein int hadvmlock; 29823955314SAlfred Perlstein 29923955314SAlfred Perlstein hadvmlock = mtx_owned(&vm_mtx); 30023955314SAlfred Perlstein if (!hadvmlock) 30123955314SAlfred Perlstein mtx_lock(&vm_mtx); 3021efb74fbSJohn Dyson vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size); 303c576d121SLuoqi Chen if (nobjsize < object->size) { 304c576d121SLuoqi Chen vm_object_page_remove(object, nobjsize, object->size, 305c576d121SLuoqi Chen FALSE); 3060d94caffSDavid Greenman } 307bbc0ec52SDavid Greenman /* 308bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 309bbc0ec52SDavid Greenman * only partially backed by the vnode... 310bbc0ec52SDavid Greenman */ 311bbc0ec52SDavid Greenman if (nsize & PAGE_MASK) { 312bbc0ec52SDavid Greenman vm_offset_t kva; 313bbc0ec52SDavid Greenman vm_page_t m; 314bbc0ec52SDavid Greenman 315a316d390SJohn Dyson m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 316bbc0ec52SDavid Greenman if (m) { 3172b6b0df7SMatthew Dillon int base = (int)nsize & PAGE_MASK; 3182b6b0df7SMatthew Dillon int size = PAGE_SIZE - base; 3192b6b0df7SMatthew Dillon 3202b6b0df7SMatthew Dillon /* 3212b6b0df7SMatthew Dillon * Clear out partial-page garbage in case 3222b6b0df7SMatthew Dillon * the page has been mapped. 3232b6b0df7SMatthew Dillon */ 324bbc0ec52SDavid Greenman kva = vm_pager_map_page(m); 3252b6b0df7SMatthew Dillon bzero((caddr_t)kva + base, size); 326bbc0ec52SDavid Greenman vm_pager_unmap_page(kva); 3272b6b0df7SMatthew Dillon 3282b6b0df7SMatthew Dillon /* 3292b6b0df7SMatthew Dillon * Clear out partial-page dirty bits. This 3302b6b0df7SMatthew Dillon * has the side effect of setting the valid 3312b6b0df7SMatthew Dillon * bits, but that is ok. There are a bunch 3322b6b0df7SMatthew Dillon * of places in the VM system where we expected 3332b6b0df7SMatthew Dillon * m->dirty == VM_PAGE_BITS_ALL. The file EOF 3342b6b0df7SMatthew Dillon * case is one of them. If the page is still 3352b6b0df7SMatthew Dillon * partially dirty, make it fully dirty. 3362b6b0df7SMatthew Dillon */ 3372b6b0df7SMatthew Dillon vm_page_set_validclean(m, base, size); 3382b6b0df7SMatthew Dillon if (m->dirty != 0) 3392b6b0df7SMatthew Dillon m->dirty = VM_PAGE_BITS_ALL; 340bbc0ec52SDavid Greenman } 341bbc0ec52SDavid Greenman } 34223955314SAlfred Perlstein if (!hadvmlock) 34323955314SAlfred Perlstein mtx_unlock(&vm_mtx); 344bbc0ec52SDavid Greenman } 345a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 346c576d121SLuoqi Chen object->size = nobjsize; 347df8bae1dSRodney W. Grimes } 348df8bae1dSRodney W. Grimes 34926f9a767SRodney W. Grimes /* 35026f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 35126f9a767SRodney W. Grimes * file address 35226f9a767SRodney W. Grimes */ 353f708ef1bSPoul-Henning Kamp static vm_offset_t 354efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run) 35526f9a767SRodney W. Grimes struct vnode *vp; 356a316d390SJohn Dyson vm_ooffset_t address; 357efc68ce1SDavid Greenman int *run; 35826f9a767SRodney W. Grimes { 35926f9a767SRodney W. Grimes int rtaddress; 36026f9a767SRodney W. Grimes int bsize; 361a316d390SJohn Dyson daddr_t block; 36226f9a767SRodney W. Grimes struct vnode *rtvp; 36326f9a767SRodney W. Grimes int err; 364a316d390SJohn Dyson daddr_t vblock; 365a316d390SJohn Dyson int voffset; 36626f9a767SRodney W. Grimes 367e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 3680d94caffSDavid Greenman if ((int) address < 0) 3690d94caffSDavid Greenman return -1; 3700d94caffSDavid Greenman 3712c4488fcSJohn Dyson if (vp->v_mount == NULL) 3722c4488fcSJohn Dyson return -1; 3732c4488fcSJohn Dyson 37426f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 37526f9a767SRodney W. Grimes vblock = address / bsize; 37626f9a767SRodney W. Grimes voffset = address % bsize; 377342a1480SJohn Baldwin mtx_unlock(&vm_mtx); 37826f9a767SRodney W. Grimes 379c83ebe77SJohn Dyson err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 38026f9a767SRodney W. Grimes 381342a1480SJohn Baldwin mtx_lock(&vm_mtx); 382efc68ce1SDavid Greenman if (err || (block == -1)) 38326f9a767SRodney W. Grimes rtaddress = -1; 384efc68ce1SDavid Greenman else { 385187f0071SDavid Greenman rtaddress = block + voffset / DEV_BSIZE; 386efc68ce1SDavid Greenman if( run) { 387efc68ce1SDavid Greenman *run += 1; 388efc68ce1SDavid Greenman *run *= bsize/PAGE_SIZE; 389efc68ce1SDavid Greenman *run -= voffset/PAGE_SIZE; 390efc68ce1SDavid Greenman } 391efc68ce1SDavid Greenman } 39226f9a767SRodney W. Grimes 39326f9a767SRodney W. Grimes return rtaddress; 39426f9a767SRodney W. Grimes } 39526f9a767SRodney W. Grimes 39626f9a767SRodney W. Grimes /* 39726f9a767SRodney W. Grimes * interrupt routine for I/O completion 39826f9a767SRodney W. Grimes */ 399f708ef1bSPoul-Henning Kamp static void 40026f9a767SRodney W. Grimes vnode_pager_iodone(bp) 40126f9a767SRodney W. Grimes struct buf *bp; 40226f9a767SRodney W. Grimes { 40326f9a767SRodney W. Grimes bp->b_flags |= B_DONE; 40424a1cce3SDavid Greenman wakeup(bp); 40526f9a767SRodney W. Grimes } 40626f9a767SRodney W. Grimes 40726f9a767SRodney W. Grimes /* 40826f9a767SRodney W. Grimes * small block file system vnode pager input 40926f9a767SRodney W. Grimes */ 410f708ef1bSPoul-Henning Kamp static int 41124a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m) 41224a1cce3SDavid Greenman vm_object_t object; 41326f9a767SRodney W. Grimes vm_page_t m; 41426f9a767SRodney W. Grimes { 41526f9a767SRodney W. Grimes int i; 41626f9a767SRodney W. Grimes int s; 41726f9a767SRodney W. Grimes struct vnode *dp, *vp; 41826f9a767SRodney W. Grimes struct buf *bp; 41926f9a767SRodney W. Grimes vm_offset_t kva; 42026f9a767SRodney W. Grimes int fileaddr; 42126f9a767SRodney W. Grimes vm_offset_t bsize; 42226f9a767SRodney W. Grimes int error = 0; 42326f9a767SRodney W. Grimes 424e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 42524a1cce3SDavid Greenman vp = object->handle; 4262c4488fcSJohn Dyson if (vp->v_mount == NULL) 4272c4488fcSJohn Dyson return VM_PAGER_BAD; 4282c4488fcSJohn Dyson 42926f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 430342a1480SJohn Baldwin mtx_unlock(&vm_mtx); 4310bdb7528SDavid Greenman 432c83ebe77SJohn Dyson VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 43326f9a767SRodney W. Grimes 434342a1480SJohn Baldwin mtx_lock(&vm_mtx); 43526f9a767SRodney W. Grimes kva = vm_pager_map_page(m); 43626f9a767SRodney W. Grimes 43726f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 438bbc0ec52SDavid Greenman 439897a45efSDmitrij Tejblum if (vm_page_bits(i * bsize, bsize) & m->valid) 44026f9a767SRodney W. Grimes continue; 44126f9a767SRodney W. Grimes 442a316d390SJohn Dyson fileaddr = vnode_pager_addr(vp, 443a316d390SJohn Dyson IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 44426f9a767SRodney W. Grimes if (fileaddr != -1) { 445342a1480SJohn Baldwin mtx_unlock(&vm_mtx); 4461c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 44726f9a767SRodney W. Grimes 44826f9a767SRodney W. Grimes /* build a minimal buffer header */ 44921144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 45026f9a767SRodney W. Grimes bp->b_iodone = vnode_pager_iodone; 451b0eeea20SPoul-Henning Kamp bp->b_rcred = bp->b_wcred = curproc->p_ucred; 45226f9a767SRodney W. Grimes if (bp->b_rcred != NOCRED) 45326f9a767SRodney W. Grimes crhold(bp->b_rcred); 45426f9a767SRodney W. Grimes if (bp->b_wcred != NOCRED) 45526f9a767SRodney W. Grimes crhold(bp->b_wcred); 456ab3f7469SPoul-Henning Kamp bp->b_data = (caddr_t) kva + i * bsize; 457187f0071SDavid Greenman bp->b_blkno = fileaddr; 4580d94caffSDavid Greenman pbgetvp(dp, bp); 45926f9a767SRodney W. Grimes bp->b_bcount = bsize; 46026f9a767SRodney W. Grimes bp->b_bufsize = bsize; 4612b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 4622b6b0df7SMatthew Dillon runningbufspace += bp->b_runningbufspace; 46326f9a767SRodney W. Grimes 46426f9a767SRodney W. Grimes /* do the input */ 465b99c307aSPoul-Henning Kamp BUF_STRATEGY(bp); 46626f9a767SRodney W. Grimes 467e47ed70bSJohn Dyson /* we definitely need to be at splvm here */ 46826f9a767SRodney W. Grimes 469e47ed70bSJohn Dyson s = splvm(); 47026f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 471aa2cabb9SDavid Greenman tsleep(bp, PVM, "vnsrd", 0); 47226f9a767SRodney W. Grimes } 47326f9a767SRodney W. Grimes splx(s); 474c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 47526f9a767SRodney W. Grimes error = EIO; 47626f9a767SRodney W. Grimes 47726f9a767SRodney W. Grimes /* 47826f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 47926f9a767SRodney W. Grimes */ 4801c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 481342a1480SJohn Baldwin mtx_lock(&vm_mtx); 48226f9a767SRodney W. Grimes if (error) 48326f9a767SRodney W. Grimes break; 4840d94caffSDavid Greenman 485aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 48626f9a767SRodney W. Grimes } else { 487aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 48826f9a767SRodney W. Grimes bzero((caddr_t) kva + i * bsize, bsize); 48926f9a767SRodney W. Grimes } 49026f9a767SRodney W. Grimes } 49126f9a767SRodney W. Grimes vm_pager_unmap_page(kva); 4920385347cSPeter Wemm pmap_clear_modify(m); 493e69763a3SDoug Rabson vm_page_flag_clear(m, PG_ZERO); 49426f9a767SRodney W. Grimes if (error) { 495a83c285cSDavid Greenman return VM_PAGER_ERROR; 49626f9a767SRodney W. Grimes } 49726f9a767SRodney W. Grimes return VM_PAGER_OK; 49826f9a767SRodney W. Grimes 49926f9a767SRodney W. Grimes } 50026f9a767SRodney W. Grimes 50126f9a767SRodney W. Grimes 50226f9a767SRodney W. Grimes /* 50326f9a767SRodney W. Grimes * old style vnode pager output routine 50426f9a767SRodney W. Grimes */ 505f708ef1bSPoul-Henning Kamp static int 50624a1cce3SDavid Greenman vnode_pager_input_old(object, m) 50724a1cce3SDavid Greenman vm_object_t object; 50826f9a767SRodney W. Grimes vm_page_t m; 50926f9a767SRodney W. Grimes { 510df8bae1dSRodney W. Grimes struct uio auio; 511df8bae1dSRodney W. Grimes struct iovec aiov; 51226f9a767SRodney W. Grimes int error; 51326f9a767SRodney W. Grimes int size; 51426f9a767SRodney W. Grimes vm_offset_t kva; 515342a1480SJohn Baldwin struct vnode *vp; 516df8bae1dSRodney W. Grimes 517e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 51826f9a767SRodney W. Grimes error = 0; 519bbc0ec52SDavid Greenman 520df8bae1dSRodney W. Grimes /* 52126f9a767SRodney W. Grimes * Return failure if beyond current EOF 52226f9a767SRodney W. Grimes */ 523a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 52426f9a767SRodney W. Grimes return VM_PAGER_BAD; 52526f9a767SRodney W. Grimes } else { 52626f9a767SRodney W. Grimes size = PAGE_SIZE; 527a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 528a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 5290bdb7528SDavid Greenman 53026f9a767SRodney W. Grimes /* 531df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 532df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 533df8bae1dSRodney W. Grimes */ 53426f9a767SRodney W. Grimes kva = vm_pager_map_page(m); 5350bdb7528SDavid Greenman 536342a1480SJohn Baldwin vp = object->handle; 537342a1480SJohn Baldwin mtx_unlock(&vm_mtx); 538df8bae1dSRodney W. Grimes aiov.iov_base = (caddr_t) kva; 539df8bae1dSRodney W. Grimes aiov.iov_len = size; 540df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 541df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 542a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 543df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 54426f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 545df8bae1dSRodney W. Grimes auio.uio_resid = size; 546af1f63c7SRobert V. Baron auio.uio_procp = curproc; 54726f9a767SRodney W. Grimes 548342a1480SJohn Baldwin error = VOP_READ(vp, &auio, 0, curproc->p_ucred); 549df8bae1dSRodney W. Grimes if (!error) { 550df8bae1dSRodney W. Grimes register int count = size - auio.uio_resid; 551df8bae1dSRodney W. Grimes 552df8bae1dSRodney W. Grimes if (count == 0) 553df8bae1dSRodney W. Grimes error = EINVAL; 55426f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 55526f9a767SRodney W. Grimes bzero((caddr_t) kva + count, PAGE_SIZE - count); 556df8bae1dSRodney W. Grimes } 557342a1480SJohn Baldwin mtx_lock(&vm_mtx); 55826f9a767SRodney W. Grimes vm_pager_unmap_page(kva); 559df8bae1dSRodney W. Grimes } 5600385347cSPeter Wemm pmap_clear_modify(m); 5612c28a105SAlan Cox vm_page_undirty(m); 562e69763a3SDoug Rabson vm_page_flag_clear(m, PG_ZERO); 5636e3a3f38SRobert V. Baron if (!error) 5646e3a3f38SRobert V. Baron m->valid = VM_PAGE_BITS_ALL; 565a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 56626f9a767SRodney W. Grimes } 56726f9a767SRodney W. Grimes 56826f9a767SRodney W. Grimes /* 56926f9a767SRodney W. Grimes * generic vnode pager input routine 57026f9a767SRodney W. Grimes */ 571170db9c6SJohn Dyson 572ce75f2c3SMike Smith /* 57323955314SAlfred Perlstein * Local media VFS's that do not implement their own VOP_GETPAGES 57423955314SAlfred Perlstein * should have their VOP_GETPAGES should call to 575ce75f2c3SMike Smith * vnode_pager_generic_getpages() to implement the previous behaviour. 576ce75f2c3SMike Smith * 577ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 578ce75f2c3SMike Smith * backing vp's VOP_GETPAGES. 579ce75f2c3SMike Smith */ 580f708ef1bSPoul-Henning Kamp static int 58124a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage) 58226f9a767SRodney W. Grimes vm_object_t object; 58324a1cce3SDavid Greenman vm_page_t *m; 58424a1cce3SDavid Greenman int count; 58524a1cce3SDavid Greenman int reqpage; 58624a1cce3SDavid Greenman { 587170db9c6SJohn Dyson int rtval; 588170db9c6SJohn Dyson struct vnode *vp; 58986ffbd76SMike Smith int bytes = count * PAGE_SIZE; 59095e5e988SJohn Dyson 591e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 592170db9c6SJohn Dyson vp = object->handle; 59386ffbd76SMike Smith rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 59423955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 59523955314SAlfred Perlstein ("vnode_pager: FS getpages not implemented\n")); 596170db9c6SJohn Dyson return rtval; 597170db9c6SJohn Dyson } 598170db9c6SJohn Dyson 599ce75f2c3SMike Smith 600ce75f2c3SMike Smith /* 601ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 602ce75f2c3SMike Smith * own vnodes if they fail to implement VOP_GETPAGES. 603ce75f2c3SMike Smith */ 604ce75f2c3SMike Smith int 605ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 606ce75f2c3SMike Smith struct vnode *vp; 607170db9c6SJohn Dyson vm_page_t *m; 608ce75f2c3SMike Smith int bytecount; 609170db9c6SJohn Dyson int reqpage; 610170db9c6SJohn Dyson { 611ce75f2c3SMike Smith vm_object_t object; 612a316d390SJohn Dyson vm_offset_t kva; 6138f9110f6SJohn Dyson off_t foff, tfoff, nextoff; 61424a1cce3SDavid Greenman int i, size, bsize, first, firstaddr; 615ce75f2c3SMike Smith struct vnode *dp; 616efc68ce1SDavid Greenman int runpg; 617efc68ce1SDavid Greenman int runend; 6180bdb7528SDavid Greenman struct buf *bp; 61926f9a767SRodney W. Grimes int s; 620ce75f2c3SMike Smith int count; 62126f9a767SRodney W. Grimes int error = 0; 62226f9a767SRodney W. Grimes 623e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 624ce75f2c3SMike Smith object = vp->v_object; 625ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 626ce75f2c3SMike Smith 6272c4488fcSJohn Dyson if (vp->v_mount == NULL) 6282c4488fcSJohn Dyson return VM_PAGER_BAD; 6292c4488fcSJohn Dyson 63026f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 63126f9a767SRodney W. Grimes 63226f9a767SRodney W. Grimes /* get the UNDERLYING device for the file with VOP_BMAP() */ 633bbc0ec52SDavid Greenman 63426f9a767SRodney W. Grimes /* 635bbc0ec52SDavid Greenman * originally, we did not check for an error return value -- assuming 636bbc0ec52SDavid Greenman * an fs always has a bmap entry point -- that assumption is wrong!!! 63726f9a767SRodney W. Grimes */ 638a316d390SJohn Dyson foff = IDX_TO_OFF(m[reqpage]->pindex); 639bbc0ec52SDavid Greenman 64026f9a767SRodney W. Grimes /* 64116f62314SDavid Greenman * if we can't bmap, use old VOP code 64226f9a767SRodney W. Grimes */ 643342a1480SJohn Baldwin mtx_unlock(&vm_mtx); 644c83ebe77SJohn Dyson if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 645342a1480SJohn Baldwin mtx_lock(&vm_mtx); 64626f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 64726f9a767SRodney W. Grimes if (i != reqpage) { 648d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 64926f9a767SRodney W. Grimes } 65026f9a767SRodney W. Grimes } 651976e77fcSDavid Greenman cnt.v_vnodein++; 652976e77fcSDavid Greenman cnt.v_vnodepgsin++; 65324a1cce3SDavid Greenman return vnode_pager_input_old(object, m[reqpage]); 654bbc0ec52SDavid Greenman 65526f9a767SRodney W. Grimes /* 65626f9a767SRodney W. Grimes * if the blocksize is smaller than a page size, then use 65726f9a767SRodney W. Grimes * special small filesystem code. NFS sometimes has a small 65826f9a767SRodney W. Grimes * blocksize, but it can handle large reads itself. 65926f9a767SRodney W. Grimes */ 66026f9a767SRodney W. Grimes } else if ((PAGE_SIZE / bsize) > 1 && 661500b04a2SBruce Evans (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 662342a1480SJohn Baldwin mtx_lock(&vm_mtx); 66326f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 66426f9a767SRodney W. Grimes if (i != reqpage) { 665d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 66626f9a767SRodney W. Grimes } 66726f9a767SRodney W. Grimes } 668976e77fcSDavid Greenman cnt.v_vnodein++; 669976e77fcSDavid Greenman cnt.v_vnodepgsin++; 67024a1cce3SDavid Greenman return vnode_pager_input_smlfs(object, m[reqpage]); 67126f9a767SRodney W. Grimes } 672342a1480SJohn Baldwin mtx_lock(&vm_mtx); 6738d17e694SJulian Elischer 67426f9a767SRodney W. Grimes /* 6758d17e694SJulian Elischer * If we have a completely valid page available to us, we can 6768d17e694SJulian Elischer * clean up and return. Otherwise we have to re-read the 6778d17e694SJulian Elischer * media. 6780d94caffSDavid Greenman */ 67932ad9cb5SDoug Rabson 6808d17e694SJulian Elischer if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 6810d94caffSDavid Greenman for (i = 0; i < count; i++) { 6820d94caffSDavid Greenman if (i != reqpage) 683d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 6840d94caffSDavid Greenman } 6850d94caffSDavid Greenman return VM_PAGER_OK; 6860d94caffSDavid Greenman } 6878d17e694SJulian Elischer m[reqpage]->valid = 0; 6880bdb7528SDavid Greenman 6890d94caffSDavid Greenman /* 69026f9a767SRodney W. Grimes * here on direct device I/O 69126f9a767SRodney W. Grimes */ 69226f9a767SRodney W. Grimes 693efc68ce1SDavid Greenman firstaddr = -1; 69426f9a767SRodney W. Grimes /* 695efc68ce1SDavid Greenman * calculate the run that includes the required page 69626f9a767SRodney W. Grimes */ 697efc68ce1SDavid Greenman for(first = 0, i = 0; i < count; i = runend) { 698a316d390SJohn Dyson firstaddr = vnode_pager_addr(vp, 699a316d390SJohn Dyson IDX_TO_OFF(m[i]->pindex), &runpg); 700efc68ce1SDavid Greenman if (firstaddr == -1) { 70124a1cce3SDavid Greenman if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 702fc62ef1fSBruce Evans /* XXX no %qd in kernel. */ 703fc62ef1fSBruce Evans panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx", 704fc62ef1fSBruce Evans firstaddr, (u_long)(foff >> 32), 705fc62ef1fSBruce Evans (u_long)(u_int32_t)foff, 706fc62ef1fSBruce Evans (u_long)(u_int32_t) 707fc62ef1fSBruce Evans (object->un_pager.vnp.vnp_size >> 32), 708fc62ef1fSBruce Evans (u_long)(u_int32_t) 709fc62ef1fSBruce Evans object->un_pager.vnp.vnp_size); 710efc68ce1SDavid Greenman } 711d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 712efc68ce1SDavid Greenman runend = i + 1; 713efc68ce1SDavid Greenman first = runend; 714efc68ce1SDavid Greenman continue; 715efc68ce1SDavid Greenman } 716efc68ce1SDavid Greenman runend = i + runpg; 717efc68ce1SDavid Greenman if (runend <= reqpage) { 718efc68ce1SDavid Greenman int j; 719efc68ce1SDavid Greenman for (j = i; j < runend; j++) { 720d8d5fa88SAlfred Perlstein vm_page_free(m[j]); 721efc68ce1SDavid Greenman } 72226f9a767SRodney W. Grimes } else { 723efc68ce1SDavid Greenman if (runpg < (count - first)) { 724efc68ce1SDavid Greenman for (i = first + runpg; i < count; i++) 725d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 726efc68ce1SDavid Greenman count = first + runpg; 72726f9a767SRodney W. Grimes } 728efc68ce1SDavid Greenman break; 72926f9a767SRodney W. Grimes } 730efc68ce1SDavid Greenman first = runend; 731efc68ce1SDavid Greenman } 73226f9a767SRodney W. Grimes 73326f9a767SRodney W. Grimes /* 734bbc0ec52SDavid Greenman * the first and last page have been calculated now, move input pages 735bbc0ec52SDavid Greenman * to be zero based... 73626f9a767SRodney W. Grimes */ 73726f9a767SRodney W. Grimes if (first != 0) { 73826f9a767SRodney W. Grimes for (i = first; i < count; i++) { 73926f9a767SRodney W. Grimes m[i - first] = m[i]; 74026f9a767SRodney W. Grimes } 74126f9a767SRodney W. Grimes count -= first; 74226f9a767SRodney W. Grimes reqpage -= first; 74326f9a767SRodney W. Grimes } 744efc68ce1SDavid Greenman 74526f9a767SRodney W. Grimes /* 74626f9a767SRodney W. Grimes * calculate the file virtual address for the transfer 74726f9a767SRodney W. Grimes */ 748a316d390SJohn Dyson foff = IDX_TO_OFF(m[0]->pindex); 74926f9a767SRodney W. Grimes 75026f9a767SRodney W. Grimes /* 75126f9a767SRodney W. Grimes * calculate the size of the transfer 75226f9a767SRodney W. Grimes */ 75326f9a767SRodney W. Grimes size = count * PAGE_SIZE; 75424a1cce3SDavid Greenman if ((foff + size) > object->un_pager.vnp.vnp_size) 75524a1cce3SDavid Greenman size = object->un_pager.vnp.vnp_size - foff; 75626f9a767SRodney W. Grimes 75726f9a767SRodney W. Grimes /* 75824579ca1SMatthew Dillon * round up physical size for real devices. 75926f9a767SRodney W. Grimes */ 76024579ca1SMatthew Dillon if (dp->v_type == VBLK || dp->v_type == VCHR) { 76124579ca1SMatthew Dillon int secmask = dp->v_rdev->si_bsize_phys - 1; 76224579ca1SMatthew Dillon KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1)); 76324579ca1SMatthew Dillon size = (size + secmask) & ~secmask; 76424579ca1SMatthew Dillon } 76526f9a767SRodney W. Grimes 7661c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 76716f62314SDavid Greenman kva = (vm_offset_t) bp->b_data; 76816f62314SDavid Greenman 76926f9a767SRodney W. Grimes /* 77026f9a767SRodney W. Grimes * and map the pages to be read into the kva 77126f9a767SRodney W. Grimes */ 77216f62314SDavid Greenman pmap_qenter(kva, m, count); 773342a1480SJohn Baldwin mtx_unlock(&vm_mtx); 77426f9a767SRodney W. Grimes 77526f9a767SRodney W. Grimes /* build a minimal buffer header */ 77621144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 77726f9a767SRodney W. Grimes bp->b_iodone = vnode_pager_iodone; 77826f9a767SRodney W. Grimes /* B_PHYS is not set, but it is nice to fill this in */ 779b0eeea20SPoul-Henning Kamp bp->b_rcred = bp->b_wcred = curproc->p_ucred; 78026f9a767SRodney W. Grimes if (bp->b_rcred != NOCRED) 78126f9a767SRodney W. Grimes crhold(bp->b_rcred); 78226f9a767SRodney W. Grimes if (bp->b_wcred != NOCRED) 78326f9a767SRodney W. Grimes crhold(bp->b_wcred); 784187f0071SDavid Greenman bp->b_blkno = firstaddr; 7850d94caffSDavid Greenman pbgetvp(dp, bp); 78626f9a767SRodney W. Grimes bp->b_bcount = size; 78726f9a767SRodney W. Grimes bp->b_bufsize = size; 7882b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 7892b6b0df7SMatthew Dillon runningbufspace += bp->b_runningbufspace; 79026f9a767SRodney W. Grimes 791976e77fcSDavid Greenman cnt.v_vnodein++; 792976e77fcSDavid Greenman cnt.v_vnodepgsin += count; 793976e77fcSDavid Greenman 79426f9a767SRodney W. Grimes /* do the input */ 795b99c307aSPoul-Henning Kamp BUF_STRATEGY(bp); 796976e77fcSDavid Greenman 797e47ed70bSJohn Dyson s = splvm(); 798e47ed70bSJohn Dyson /* we definitely need to be at splvm here */ 79926f9a767SRodney W. Grimes 80026f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 801aa2cabb9SDavid Greenman tsleep(bp, PVM, "vnread", 0); 80226f9a767SRodney W. Grimes } 80326f9a767SRodney W. Grimes splx(s); 804c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 80526f9a767SRodney W. Grimes error = EIO; 80626f9a767SRodney W. Grimes 80726f9a767SRodney W. Grimes if (!error) { 80826f9a767SRodney W. Grimes if (size != count * PAGE_SIZE) 80926f9a767SRodney W. Grimes bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 81026f9a767SRodney W. Grimes } 811342a1480SJohn Baldwin mtx_lock(&vm_mtx); 81216f62314SDavid Greenman pmap_qremove(kva, count); 81326f9a767SRodney W. Grimes 81426f9a767SRodney W. Grimes /* 81526f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 81626f9a767SRodney W. Grimes */ 8171c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 81826f9a767SRodney W. Grimes 8198f9110f6SJohn Dyson for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 8208f9110f6SJohn Dyson vm_page_t mt; 8218f9110f6SJohn Dyson 8228f9110f6SJohn Dyson nextoff = tfoff + PAGE_SIZE; 8238f9110f6SJohn Dyson mt = m[i]; 8248f9110f6SJohn Dyson 82554746b67SDmitrij Tejblum if (nextoff <= object->un_pager.vnp.vnp_size) { 8268d17e694SJulian Elischer /* 8278d17e694SJulian Elischer * Read filled up entire page. 8288d17e694SJulian Elischer */ 8298f9110f6SJohn Dyson mt->valid = VM_PAGE_BITS_ALL; 8302c28a105SAlan Cox vm_page_undirty(mt); /* should be an assert? XXX */ 8310385347cSPeter Wemm pmap_clear_modify(mt); 8328f9110f6SJohn Dyson } else { 8338d17e694SJulian Elischer /* 8348d17e694SJulian Elischer * Read did not fill up entire page. Since this 8358d17e694SJulian Elischer * is getpages, the page may be mapped, so we have 8368d17e694SJulian Elischer * to zero the invalid portions of the page even 8378d17e694SJulian Elischer * though we aren't setting them valid. 8388d17e694SJulian Elischer * 8398d17e694SJulian Elischer * Currently we do not set the entire page valid, 8408d17e694SJulian Elischer * we just try to clear the piece that we couldn't 8418d17e694SJulian Elischer * read. 8428d17e694SJulian Elischer */ 84354746b67SDmitrij Tejblum vm_page_set_validclean(mt, 0, 84454746b67SDmitrij Tejblum object->un_pager.vnp.vnp_size - tfoff); 8454221e284SAlan Cox /* handled by vm_fault now */ 8464221e284SAlan Cox /* vm_page_zero_invalid(mt, FALSE); */ 8478f9110f6SJohn Dyson } 8488f9110f6SJohn Dyson 849e69763a3SDoug Rabson vm_page_flag_clear(mt, PG_ZERO); 85026f9a767SRodney W. Grimes if (i != reqpage) { 851bbc0ec52SDavid Greenman 85226f9a767SRodney W. Grimes /* 853bbc0ec52SDavid Greenman * whether or not to leave the page activated is up in 854bbc0ec52SDavid Greenman * the air, but we should put the page on a page queue 855bbc0ec52SDavid Greenman * somewhere. (it already is in the object). Result: 856956f3135SPhilippe Charnier * It appears that empirical results show that 857bbc0ec52SDavid Greenman * deactivating pages is best. 85826f9a767SRodney W. Grimes */ 859bbc0ec52SDavid Greenman 86026f9a767SRodney W. Grimes /* 861bbc0ec52SDavid Greenman * just in case someone was asking for this page we 862bbc0ec52SDavid Greenman * now tell them that it is ok to use 86326f9a767SRodney W. Grimes */ 86426f9a767SRodney W. Grimes if (!error) { 8658f9110f6SJohn Dyson if (mt->flags & PG_WANTED) 8668f9110f6SJohn Dyson vm_page_activate(mt); 86795461b45SJohn Dyson else 8688f9110f6SJohn Dyson vm_page_deactivate(mt); 869e69763a3SDoug Rabson vm_page_wakeup(mt); 87026f9a767SRodney W. Grimes } else { 871d8d5fa88SAlfred Perlstein vm_page_free(mt); 87226f9a767SRodney W. Grimes } 87326f9a767SRodney W. Grimes } 87426f9a767SRodney W. Grimes } 87526f9a767SRodney W. Grimes if (error) { 87624a1cce3SDavid Greenman printf("vnode_pager_getpages: I/O read error\n"); 87726f9a767SRodney W. Grimes } 878a83c285cSDavid Greenman return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 87926f9a767SRodney W. Grimes } 88026f9a767SRodney W. Grimes 881ce75f2c3SMike Smith /* 882ce75f2c3SMike Smith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 883ce75f2c3SMike Smith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 884ce75f2c3SMike Smith * vnode_pager_generic_putpages() to implement the previous behaviour. 885ce75f2c3SMike Smith * 886ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 887ce75f2c3SMike Smith * backing vp's VOP_PUTPAGES. 888ce75f2c3SMike Smith */ 889e4542174SMatthew Dillon static void 890170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals) 891170db9c6SJohn Dyson vm_object_t object; 892170db9c6SJohn Dyson vm_page_t *m; 893170db9c6SJohn Dyson int count; 894170db9c6SJohn Dyson boolean_t sync; 895170db9c6SJohn Dyson int *rtvals; 896170db9c6SJohn Dyson { 897170db9c6SJohn Dyson int rtval; 898170db9c6SJohn Dyson struct vnode *vp; 899f2a2857bSKirk McKusick struct mount *mp; 90086ffbd76SMike Smith int bytes = count * PAGE_SIZE; 901ad980522SJohn Dyson 902e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 9030e3cdf2cSAlan Cox /* 9040e3cdf2cSAlan Cox * Force synchronous operation if we are extremely low on memory 9050e3cdf2cSAlan Cox * to prevent a low-memory deadlock. VOP operations often need to 9060e3cdf2cSAlan Cox * allocate more memory to initiate the I/O ( i.e. do a BMAP 9070e3cdf2cSAlan Cox * operation ). The swapper handles the case by limiting the amount 9080e3cdf2cSAlan Cox * of asynchronous I/O, but that sort of solution doesn't scale well 9090e3cdf2cSAlan Cox * for the vnode pager without a lot of work. 9100e3cdf2cSAlan Cox * 9110e3cdf2cSAlan Cox * Also, the backing vnode's iodone routine may not wake the pageout 9120e3cdf2cSAlan Cox * daemon up. This should be probably be addressed XXX. 9130e3cdf2cSAlan Cox */ 9140e3cdf2cSAlan Cox 9150e3cdf2cSAlan Cox if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 9160e3cdf2cSAlan Cox sync |= OBJPC_SYNC; 9170e3cdf2cSAlan Cox 9180e3cdf2cSAlan Cox /* 9190e3cdf2cSAlan Cox * Call device-specific putpages function 9200e3cdf2cSAlan Cox */ 9210e3cdf2cSAlan Cox 922170db9c6SJohn Dyson vp = object->handle; 923e6b961ffSJohn Baldwin mtx_unlock(&vm_mtx); 924f2a2857bSKirk McKusick if (vp->v_type != VREG) 925f2a2857bSKirk McKusick mp = NULL; 926f2a2857bSKirk McKusick (void)vn_start_write(vp, &mp, V_WAIT); 92723955314SAlfred Perlstein mtx_lock(&vm_mtx); 92886ffbd76SMike Smith rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 92923955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 93023955314SAlfred Perlstein ("vnode_pager: stale FS putpages\n")); 93123955314SAlfred Perlstein mtx_unlock(&vm_mtx); 932f2a2857bSKirk McKusick vn_finished_write(mp); 93323955314SAlfred Perlstein mtx_lock(&vm_mtx); 934170db9c6SJohn Dyson } 935170db9c6SJohn Dyson 936ce75f2c3SMike Smith 93726f9a767SRodney W. Grimes /* 938ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 9394491ea91SEivind Eklund * own vnodes if they fail to implement VOP_PUTPAGES. 9402b6b0df7SMatthew Dillon * 9412b6b0df7SMatthew Dillon * This is typically called indirectly via the pageout daemon and 9422b6b0df7SMatthew Dillon * clustering has already typically occured, so in general we ask the 9432b6b0df7SMatthew Dillon * underlying filesystem to write the data out asynchronously rather 9442b6b0df7SMatthew Dillon * then delayed. 94526f9a767SRodney W. Grimes */ 946ce75f2c3SMike Smith int 9478f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 948ce75f2c3SMike Smith struct vnode *vp; 94926f9a767SRodney W. Grimes vm_page_t *m; 950ce75f2c3SMike Smith int bytecount; 9518f9110f6SJohn Dyson int flags; 95226f9a767SRodney W. Grimes int *rtvals; 95326f9a767SRodney W. Grimes { 954f6b04d2bSDavid Greenman int i; 955ce75f2c3SMike Smith vm_object_t object; 956ce75f2c3SMike Smith int count; 95726f9a767SRodney W. Grimes 958f6b04d2bSDavid Greenman int maxsize, ncount; 959a316d390SJohn Dyson vm_ooffset_t poffset; 960f6b04d2bSDavid Greenman struct uio auio; 961f6b04d2bSDavid Greenman struct iovec aiov; 962f6b04d2bSDavid Greenman int error; 9638f9110f6SJohn Dyson int ioflags; 96426f9a767SRodney W. Grimes 965e6b961ffSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 966ce75f2c3SMike Smith object = vp->v_object; 967ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 968ce75f2c3SMike Smith 96926f9a767SRodney W. Grimes for (i = 0; i < count; i++) 97026f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_AGAIN; 97126f9a767SRodney W. Grimes 972a316d390SJohn Dyson if ((int) m[0]->pindex < 0) { 9733efc015bSPeter Wemm printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n", 9743efc015bSPeter Wemm (long)m[0]->pindex, m[0]->dirty); 975f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 976f6b04d2bSDavid Greenman return VM_PAGER_BAD; 9770d94caffSDavid Greenman } 9780bdb7528SDavid Greenman 979f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 980f6b04d2bSDavid Greenman ncount = count; 98126f9a767SRodney W. Grimes 982a316d390SJohn Dyson poffset = IDX_TO_OFF(m[0]->pindex); 983a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 984a316d390SJohn Dyson if (object->un_pager.vnp.vnp_size > poffset) 985a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 9865f55e841SDavid Greenman else 9875f55e841SDavid Greenman maxsize = 0; 988aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 989f6b04d2bSDavid Greenman if (ncount < count) { 990f6b04d2bSDavid Greenman for (i = ncount; i < count; i++) { 991f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 992f6b04d2bSDavid Greenman } 993f6b04d2bSDavid Greenman } 994f6b04d2bSDavid Greenman } 995342a1480SJohn Baldwin mtx_unlock(&vm_mtx); 99626f9a767SRodney W. Grimes 9972b6b0df7SMatthew Dillon /* 9982b6b0df7SMatthew Dillon * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 9992b6b0df7SMatthew Dillon * rather then a bdwrite() to prevent paging I/O from saturating 10002b6b0df7SMatthew Dillon * the buffer cache. 10012b6b0df7SMatthew Dillon */ 10028f9110f6SJohn Dyson ioflags = IO_VMIO; 10032b6b0df7SMatthew Dillon ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: IO_ASYNC; 10048f9110f6SJohn Dyson ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 1005f6b04d2bSDavid Greenman 1006f6b04d2bSDavid Greenman aiov.iov_base = (caddr_t) 0; 1007f6b04d2bSDavid Greenman aiov.iov_len = maxsize; 1008f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 1009f6b04d2bSDavid Greenman auio.uio_iovcnt = 1; 1010a316d390SJohn Dyson auio.uio_offset = poffset; 1011f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 1012f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 1013f6b04d2bSDavid Greenman auio.uio_resid = maxsize; 1014f6b04d2bSDavid Greenman auio.uio_procp = (struct proc *) 0; 10158f9110f6SJohn Dyson error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred); 1016342a1480SJohn Baldwin mtx_lock(&vm_mtx); 1017976e77fcSDavid Greenman cnt.v_vnodeout++; 1018f6b04d2bSDavid Greenman cnt.v_vnodepgsout += ncount; 1019f6b04d2bSDavid Greenman 1020f6b04d2bSDavid Greenman if (error) { 102124a1cce3SDavid Greenman printf("vnode_pager_putpages: I/O error %d\n", error); 1022f6b04d2bSDavid Greenman } 1023f6b04d2bSDavid Greenman if (auio.uio_resid) { 1024ac1e407bSBruce Evans printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1025ac1e407bSBruce Evans auio.uio_resid, (u_long)m[0]->pindex); 102626f9a767SRodney W. Grimes } 1027ffc82b0aSJohn Dyson for (i = 0; i < ncount; i++) { 102826f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 102926f9a767SRodney W. Grimes } 1030f6b04d2bSDavid Greenman return rtvals[0]; 103126f9a767SRodney W. Grimes } 1032f6b04d2bSDavid Greenman 1033f6b04d2bSDavid Greenman struct vnode * 103424a1cce3SDavid Greenman vnode_pager_lock(object) 103524a1cce3SDavid Greenman vm_object_t object; 103624a1cce3SDavid Greenman { 1037996c772fSJohn Dyson struct proc *p = curproc; /* XXX */ 1038996c772fSJohn Dyson 103923955314SAlfred Perlstein mtx_assert(&vm_mtx, MA_NOTOWNED); 104023955314SAlfred Perlstein mtx_assert(&Giant, MA_OWNED); 1041e6b961ffSJohn Baldwin mtx_lock(&vm_mtx); 104224a1cce3SDavid Greenman for (; object != NULL; object = object->backing_object) { 104324a1cce3SDavid Greenman if (object->type != OBJT_VNODE) 1044f6b04d2bSDavid Greenman continue; 1045e6b961ffSJohn Baldwin if (object->flags & OBJ_DEAD) { 1046e6b961ffSJohn Baldwin mtx_unlock(&vm_mtx); 104747221757SJohn Dyson return NULL; 1048e6b961ffSJohn Baldwin } 1049f6b04d2bSDavid Greenman 1050e6b961ffSJohn Baldwin mtx_unlock(&vm_mtx); 1051e6b961ffSJohn Baldwin /* XXX; If object->handle can change, we need to cache it. */ 105247221757SJohn Dyson while (vget(object->handle, 105347221757SJohn Dyson LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) { 1054bef608bdSJohn Dyson if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE)) 1055bef608bdSJohn Dyson return NULL; 105647221757SJohn Dyson printf("vnode_pager_lock: retrying\n"); 105747221757SJohn Dyson } 105824a1cce3SDavid Greenman return object->handle; 105926f9a767SRodney W. Grimes } 1060e6b961ffSJohn Baldwin mtx_unlock(&vm_mtx); 106124a1cce3SDavid Greenman return NULL; 1062f6b04d2bSDavid Greenman } 1063