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 7211caded3SAlfred Perlstein static void vnode_pager_init(void); 7311caded3SAlfred Perlstein static vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 7411caded3SAlfred Perlstein int *run); 7511caded3SAlfred Perlstein static void vnode_pager_iodone(struct buf *bp); 7611caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 7711caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 7811caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t); 7911caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int); 8011caded3SAlfred Perlstein static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 8111caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 820b8253a7SBruce Evans 83df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 84b62b9b64SJohn Baldwin vnode_pager_init, 85df8bae1dSRodney W. Grimes vnode_pager_alloc, 86df8bae1dSRodney W. Grimes vnode_pager_dealloc, 8724a1cce3SDavid Greenman vnode_pager_getpages, 8824a1cce3SDavid Greenman vnode_pager_putpages, 8924a1cce3SDavid Greenman vnode_pager_haspage, 9024a1cce3SDavid Greenman NULL 91df8bae1dSRodney W. Grimes }; 92df8bae1dSRodney W. Grimes 93b62b9b64SJohn Baldwin int vnode_pbuf_freecnt; 941c7c3c6aSMatthew Dillon 95b62b9b64SJohn Baldwin void 96b62b9b64SJohn Baldwin vnode_pager_init(void) 97b62b9b64SJohn Baldwin { 98b62b9b64SJohn Baldwin 99b62b9b64SJohn Baldwin vnode_pbuf_freecnt = nswbuf / 2 + 1; 100b62b9b64SJohn Baldwin } 101170db9c6SJohn Dyson 102df8bae1dSRodney W. Grimes /* 103df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 104df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 105df8bae1dSRodney W. Grimes */ 10624a1cce3SDavid Greenman vm_object_t 1076cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 108b9dcd593SBruce Evans vm_ooffset_t offset) 109df8bae1dSRodney W. Grimes { 11006cb7259SDavid Greenman vm_object_t object; 111df8bae1dSRodney W. Grimes struct vnode *vp; 112df8bae1dSRodney W. Grimes 1130cddd8f0SMatthew Dillon GIANT_REQUIRED; 1140cddd8f0SMatthew Dillon 115df8bae1dSRodney W. Grimes /* 116df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 117df8bae1dSRodney W. Grimes */ 118df8bae1dSRodney W. Grimes if (handle == NULL) 119df8bae1dSRodney W. Grimes return (NULL); 120df8bae1dSRodney W. Grimes 121df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 12239d38f93SDavid Greenman 12339d38f93SDavid Greenman /* 12439d38f93SDavid Greenman * Prevent race condition when allocating the object. This 12539d38f93SDavid Greenman * can happen with NFS vnodes since the nfsnode isn't locked. 12639d38f93SDavid Greenman */ 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; 13239d38f93SDavid Greenman 13339d38f93SDavid Greenman /* 13439d38f93SDavid Greenman * If the object is being terminated, wait for it to 13539d38f93SDavid Greenman * go away. 13639d38f93SDavid Greenman */ 137bd7e5f99SJohn Dyson while (((object = vp->v_object) != NULL) && 138bd7e5f99SJohn Dyson (object->flags & OBJ_DEAD)) { 1390cddd8f0SMatthew Dillon tsleep(object, PVM, "vadead", 0); 14024a1cce3SDavid Greenman } 1410d94caffSDavid Greenman 1422be70f79SJohn Dyson if (vp->v_usecount == 0) 1432be70f79SJohn Dyson panic("vnode_pager_alloc: no vnode reference"); 1442be70f79SJohn Dyson 14524a1cce3SDavid Greenman if (object == NULL) { 146df8bae1dSRodney W. Grimes /* 147df8bae1dSRodney W. Grimes * And an object of the appropriate size 148df8bae1dSRodney W. Grimes */ 1496cde7a16SDavid Greenman object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 150ad5dd234SJohn Dyson object->flags = 0; 151bbc0ec52SDavid Greenman 1526cde7a16SDavid Greenman object->un_pager.vnp.vnp_size = size; 15326f9a767SRodney W. Grimes 15424a1cce3SDavid Greenman object->handle = handle; 15524a1cce3SDavid Greenman vp->v_object = object; 15695e5e988SJohn Dyson vp->v_usecount++; 157df8bae1dSRodney W. Grimes } else { 15895e5e988SJohn Dyson object->ref_count++; 15995e5e988SJohn Dyson vp->v_usecount++; 160df8bae1dSRodney W. Grimes } 16139d38f93SDavid Greenman 16239d38f93SDavid Greenman vp->v_flag &= ~VOLOCK; 16339d38f93SDavid Greenman if (vp->v_flag & VOWANT) { 16439d38f93SDavid Greenman vp->v_flag &= ~VOWANT; 16539d38f93SDavid Greenman wakeup(vp); 16639d38f93SDavid Greenman } 16724a1cce3SDavid Greenman return (object); 168df8bae1dSRodney W. Grimes } 169df8bae1dSRodney W. Grimes 170f708ef1bSPoul-Henning Kamp static void 17124a1cce3SDavid Greenman vnode_pager_dealloc(object) 1720d94caffSDavid Greenman vm_object_t object; 17324a1cce3SDavid Greenman { 17454d92145SMatthew Dillon struct vnode *vp = object->handle; 175df8bae1dSRodney W. Grimes 1760cddd8f0SMatthew Dillon GIANT_REQUIRED; 17724a1cce3SDavid Greenman if (vp == NULL) 17824a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 17924a1cce3SDavid Greenman 18066095752SJohn Dyson vm_object_pip_wait(object, "vnpdea"); 18124a1cce3SDavid Greenman 18224a1cce3SDavid Greenman object->handle = NULL; 18395461b45SJohn Dyson object->type = OBJT_DEAD; 184aa2cabb9SDavid Greenman vp->v_object = NULL; 18595e5e988SJohn Dyson vp->v_flag &= ~(VTEXT | VOBJBUF); 186df8bae1dSRodney W. Grimes } 18726f9a767SRodney W. Grimes 188f708ef1bSPoul-Henning Kamp static boolean_t 189a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after) 19024a1cce3SDavid Greenman vm_object_t object; 191a316d390SJohn Dyson vm_pindex_t pindex; 19224a1cce3SDavid Greenman int *before; 19324a1cce3SDavid Greenman int *after; 194df8bae1dSRodney W. Grimes { 19524a1cce3SDavid Greenman struct vnode *vp = object->handle; 1960d2af521SKirk McKusick daddr64_t bn; 1973af76890SPoul-Henning Kamp int err; 198170db9c6SJohn Dyson daddr_t reqblock; 1992c4488fcSJohn Dyson int poff; 2002c4488fcSJohn Dyson int bsize; 201d63596ceSJohn Dyson int pagesperblock, blocksperpage; 202df8bae1dSRodney W. Grimes 2030cddd8f0SMatthew Dillon GIANT_REQUIRED; 20424579ca1SMatthew Dillon /* 20524579ca1SMatthew Dillon * If no vp or vp is doomed or marked transparent to VM, we do not 20624579ca1SMatthew Dillon * have the page. 20724579ca1SMatthew Dillon */ 20847221757SJohn Dyson if ((vp == NULL) || (vp->v_flag & VDOOMED)) 20947221757SJohn Dyson return FALSE; 21047221757SJohn Dyson 211df8bae1dSRodney W. Grimes /* 2120d94caffSDavid Greenman * If filesystem no longer mounted or offset beyond end of file we do 2130d94caffSDavid Greenman * not have the page. 214df8bae1dSRodney W. Grimes */ 215a316d390SJohn Dyson if ((vp->v_mount == NULL) || 216a316d390SJohn Dyson (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 2174abc71c0SDavid Greenman return FALSE; 218df8bae1dSRodney W. Grimes 219eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 220170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 221d63596ceSJohn Dyson blocksperpage = 0; 222d63596ceSJohn Dyson if (pagesperblock > 0) { 223a316d390SJohn Dyson reqblock = pindex / pagesperblock; 224d63596ceSJohn Dyson } else { 225d63596ceSJohn Dyson blocksperpage = (PAGE_SIZE / bsize); 226d63596ceSJohn Dyson reqblock = pindex * blocksperpage; 227d63596ceSJohn Dyson } 228170db9c6SJohn Dyson err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 229170db9c6SJohn Dyson after, before); 2300d94caffSDavid Greenman if (err) 23124a1cce3SDavid Greenman return TRUE; 2326eab77f2SJohn Dyson if (bn == -1) 233ced399eeSJohn Dyson return FALSE; 234d63596ceSJohn Dyson if (pagesperblock > 0) { 235a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 236170db9c6SJohn Dyson if (before) { 237170db9c6SJohn Dyson *before *= pagesperblock; 238170db9c6SJohn Dyson *before += poff; 239170db9c6SJohn Dyson } 240170db9c6SJohn Dyson if (after) { 241b1fc01b7SJohn Dyson int numafter; 242170db9c6SJohn Dyson *after *= pagesperblock; 243b1fc01b7SJohn Dyson numafter = pagesperblock - (poff + 1); 244a316d390SJohn Dyson if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 245a316d390SJohn Dyson numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 246b1fc01b7SJohn Dyson } 247b1fc01b7SJohn Dyson *after += numafter; 248170db9c6SJohn Dyson } 249d63596ceSJohn Dyson } else { 250d63596ceSJohn Dyson if (before) { 251d63596ceSJohn Dyson *before /= blocksperpage; 252d63596ceSJohn Dyson } 253d63596ceSJohn Dyson 254d63596ceSJohn Dyson if (after) { 255d63596ceSJohn Dyson *after /= blocksperpage; 256d63596ceSJohn Dyson } 257d63596ceSJohn Dyson } 258ced399eeSJohn Dyson return TRUE; 259df8bae1dSRodney W. Grimes } 260df8bae1dSRodney W. Grimes 261df8bae1dSRodney W. Grimes /* 262df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 26324a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 264df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 265df8bae1dSRodney W. Grimes * 266df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 267df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 268df8bae1dSRodney W. Grimes */ 269df8bae1dSRodney W. Grimes void 270df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize) 271df8bae1dSRodney W. Grimes struct vnode *vp; 272a316d390SJohn Dyson vm_ooffset_t nsize; 273df8bae1dSRodney W. Grimes { 274c576d121SLuoqi Chen vm_pindex_t nobjsize; 27524a1cce3SDavid Greenman vm_object_t object = vp->v_object; 276df8bae1dSRodney W. Grimes 2770cddd8f0SMatthew Dillon GIANT_REQUIRED; 2780cddd8f0SMatthew Dillon 27924a1cce3SDavid Greenman if (object == NULL) 280df8bae1dSRodney W. Grimes return; 281bbc0ec52SDavid Greenman 282df8bae1dSRodney W. Grimes /* 283df8bae1dSRodney W. Grimes * Hasn't changed size 284df8bae1dSRodney W. Grimes */ 28524a1cce3SDavid Greenman if (nsize == object->un_pager.vnp.vnp_size) 286df8bae1dSRodney W. Grimes return; 287bbc0ec52SDavid Greenman 288c576d121SLuoqi Chen nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 289c576d121SLuoqi Chen 290df8bae1dSRodney W. Grimes /* 291bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 292df8bae1dSRodney W. Grimes */ 29324a1cce3SDavid Greenman if (nsize < object->un_pager.vnp.vnp_size) { 2941efb74fbSJohn Dyson vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size); 295c576d121SLuoqi Chen if (nobjsize < object->size) { 296c576d121SLuoqi Chen vm_object_page_remove(object, nobjsize, object->size, 297c576d121SLuoqi Chen FALSE); 2980d94caffSDavid Greenman } 299bbc0ec52SDavid Greenman /* 300bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 3013ebeaf59SMatthew Dillon * only partially backed by the vnode. 3023ebeaf59SMatthew Dillon * 3033ebeaf59SMatthew Dillon * XXX for some reason (I don't know yet), if we take a 3043ebeaf59SMatthew Dillon * completely invalid page and mark it partially valid 3053ebeaf59SMatthew Dillon * it can screw up NFS reads, so we don't allow the case. 306bbc0ec52SDavid Greenman */ 307bbc0ec52SDavid Greenman if (nsize & PAGE_MASK) { 308bbc0ec52SDavid Greenman vm_offset_t kva; 309bbc0ec52SDavid Greenman vm_page_t m; 310bbc0ec52SDavid Greenman 311a316d390SJohn Dyson m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 3123ebeaf59SMatthew Dillon if (m && m->valid) { 3132b6b0df7SMatthew Dillon int base = (int)nsize & PAGE_MASK; 3142b6b0df7SMatthew Dillon int size = PAGE_SIZE - base; 3152b6b0df7SMatthew Dillon 3162b6b0df7SMatthew Dillon /* 3172b6b0df7SMatthew Dillon * Clear out partial-page garbage in case 3182b6b0df7SMatthew Dillon * the page has been mapped. 3192b6b0df7SMatthew Dillon */ 320bbc0ec52SDavid Greenman kva = vm_pager_map_page(m); 3212b6b0df7SMatthew Dillon bzero((caddr_t)kva + base, size); 322bbc0ec52SDavid Greenman vm_pager_unmap_page(kva); 3232b6b0df7SMatthew Dillon 3242b6b0df7SMatthew Dillon /* 3253ebeaf59SMatthew Dillon * XXX work around SMP data integrity race 3263ebeaf59SMatthew Dillon * by unmapping the page from user processes. 3273ebeaf59SMatthew Dillon * The garbage we just cleared may be mapped 3283ebeaf59SMatthew Dillon * to a user process running on another cpu 3293ebeaf59SMatthew Dillon * and this code is not running through normal 3303ebeaf59SMatthew Dillon * I/O channels which handle SMP issues for 3313ebeaf59SMatthew Dillon * us, so unmap page to synchronize all cpus. 3323ebeaf59SMatthew Dillon * 3333ebeaf59SMatthew Dillon * XXX should vm_pager_unmap_page() have 3343ebeaf59SMatthew Dillon * dealt with this? 3353ebeaf59SMatthew Dillon */ 3363ebeaf59SMatthew Dillon vm_page_protect(m, VM_PROT_NONE); 3373ebeaf59SMatthew Dillon 3383ebeaf59SMatthew Dillon /* 3392b6b0df7SMatthew Dillon * Clear out partial-page dirty bits. This 3402b6b0df7SMatthew Dillon * has the side effect of setting the valid 3412b6b0df7SMatthew Dillon * bits, but that is ok. There are a bunch 3422b6b0df7SMatthew Dillon * of places in the VM system where we expected 3432b6b0df7SMatthew Dillon * m->dirty == VM_PAGE_BITS_ALL. The file EOF 3442b6b0df7SMatthew Dillon * case is one of them. If the page is still 3452b6b0df7SMatthew Dillon * partially dirty, make it fully dirty. 3463ebeaf59SMatthew Dillon * 3473ebeaf59SMatthew Dillon * note that we do not clear out the valid 3483ebeaf59SMatthew Dillon * bits. This would prevent bogus_page 3493ebeaf59SMatthew Dillon * replacement from working properly. 3502b6b0df7SMatthew Dillon */ 3512b6b0df7SMatthew Dillon vm_page_set_validclean(m, base, size); 3522b6b0df7SMatthew Dillon if (m->dirty != 0) 3532b6b0df7SMatthew Dillon m->dirty = VM_PAGE_BITS_ALL; 354bbc0ec52SDavid Greenman } 355bbc0ec52SDavid Greenman } 356bbc0ec52SDavid Greenman } 357a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 358c576d121SLuoqi Chen object->size = nobjsize; 359df8bae1dSRodney W. Grimes } 360df8bae1dSRodney W. Grimes 36126f9a767SRodney W. Grimes /* 36226f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 36326f9a767SRodney W. Grimes * file address 36426f9a767SRodney W. Grimes */ 365f708ef1bSPoul-Henning Kamp static vm_offset_t 366efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run) 36726f9a767SRodney W. Grimes struct vnode *vp; 368a316d390SJohn Dyson vm_ooffset_t address; 369efc68ce1SDavid Greenman int *run; 37026f9a767SRodney W. Grimes { 37126f9a767SRodney W. Grimes int rtaddress; 37226f9a767SRodney W. Grimes int bsize; 3730d2af521SKirk McKusick daddr64_t block; 37426f9a767SRodney W. Grimes struct vnode *rtvp; 37526f9a767SRodney W. Grimes int err; 376a316d390SJohn Dyson daddr_t vblock; 377a316d390SJohn Dyson int voffset; 37826f9a767SRodney W. Grimes 3790cddd8f0SMatthew Dillon GIANT_REQUIRED; 3800d94caffSDavid Greenman if ((int) address < 0) 3810d94caffSDavid Greenman return -1; 3820d94caffSDavid Greenman 3832c4488fcSJohn Dyson if (vp->v_mount == NULL) 3842c4488fcSJohn Dyson return -1; 3852c4488fcSJohn Dyson 38626f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 38726f9a767SRodney W. Grimes vblock = address / bsize; 38826f9a767SRodney W. Grimes voffset = address % bsize; 38926f9a767SRodney W. Grimes 390c83ebe77SJohn Dyson err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 39126f9a767SRodney W. Grimes 392efc68ce1SDavid Greenman if (err || (block == -1)) 39326f9a767SRodney W. Grimes rtaddress = -1; 394efc68ce1SDavid Greenman else { 395187f0071SDavid Greenman rtaddress = block + voffset / DEV_BSIZE; 396efc68ce1SDavid Greenman if (run) { 397efc68ce1SDavid Greenman *run += 1; 398efc68ce1SDavid Greenman *run *= bsize/PAGE_SIZE; 399efc68ce1SDavid Greenman *run -= voffset/PAGE_SIZE; 400efc68ce1SDavid Greenman } 401efc68ce1SDavid Greenman } 40226f9a767SRodney W. Grimes 40326f9a767SRodney W. Grimes return rtaddress; 40426f9a767SRodney W. Grimes } 40526f9a767SRodney W. Grimes 40626f9a767SRodney W. Grimes /* 40726f9a767SRodney W. Grimes * interrupt routine for I/O completion 40826f9a767SRodney W. Grimes */ 409f708ef1bSPoul-Henning Kamp static void 41026f9a767SRodney W. Grimes vnode_pager_iodone(bp) 41126f9a767SRodney W. Grimes struct buf *bp; 41226f9a767SRodney W. Grimes { 41326f9a767SRodney W. Grimes bp->b_flags |= B_DONE; 41424a1cce3SDavid Greenman wakeup(bp); 41526f9a767SRodney W. Grimes } 41626f9a767SRodney W. Grimes 41726f9a767SRodney W. Grimes /* 41826f9a767SRodney W. Grimes * small block file system vnode pager input 41926f9a767SRodney W. Grimes */ 420f708ef1bSPoul-Henning Kamp static int 42124a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m) 42224a1cce3SDavid Greenman vm_object_t object; 42326f9a767SRodney W. Grimes vm_page_t m; 42426f9a767SRodney W. Grimes { 42526f9a767SRodney W. Grimes int i; 42626f9a767SRodney W. Grimes int s; 42726f9a767SRodney W. Grimes struct vnode *dp, *vp; 42826f9a767SRodney W. Grimes struct buf *bp; 42926f9a767SRodney W. Grimes vm_offset_t kva; 43026f9a767SRodney W. Grimes int fileaddr; 43126f9a767SRodney W. Grimes vm_offset_t bsize; 43226f9a767SRodney W. Grimes int error = 0; 43326f9a767SRodney W. Grimes 4340cddd8f0SMatthew Dillon GIANT_REQUIRED; 4350cddd8f0SMatthew Dillon 43624a1cce3SDavid Greenman vp = object->handle; 4372c4488fcSJohn Dyson if (vp->v_mount == NULL) 4382c4488fcSJohn Dyson return VM_PAGER_BAD; 4392c4488fcSJohn Dyson 44026f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 4410bdb7528SDavid Greenman 442c83ebe77SJohn Dyson VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 44326f9a767SRodney W. Grimes 44426f9a767SRodney W. Grimes kva = vm_pager_map_page(m); 44526f9a767SRodney W. Grimes 44626f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 44733c67741SMatthew Dillon vm_ooffset_t address; 448bbc0ec52SDavid Greenman 449897a45efSDmitrij Tejblum if (vm_page_bits(i * bsize, bsize) & m->valid) 45026f9a767SRodney W. Grimes continue; 45126f9a767SRodney W. Grimes 45233c67741SMatthew Dillon address = IDX_TO_OFF(m->pindex) + i * bsize; 45333c67741SMatthew Dillon if (address >= object->un_pager.vnp.vnp_size) { 45433c67741SMatthew Dillon fileaddr = -1; 45533c67741SMatthew Dillon } else { 45633c67741SMatthew Dillon fileaddr = vnode_pager_addr(vp, address, NULL); 45733c67741SMatthew Dillon } 45826f9a767SRodney W. Grimes if (fileaddr != -1) { 4591c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 46026f9a767SRodney W. Grimes 46126f9a767SRodney W. Grimes /* build a minimal buffer header */ 46221144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 46326f9a767SRodney W. Grimes bp->b_iodone = vnode_pager_iodone; 464bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 465bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 466a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 467a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 468ab3f7469SPoul-Henning Kamp bp->b_data = (caddr_t) kva + i * bsize; 469187f0071SDavid Greenman bp->b_blkno = fileaddr; 4700d94caffSDavid Greenman pbgetvp(dp, bp); 47126f9a767SRodney W. Grimes bp->b_bcount = bsize; 47226f9a767SRodney W. Grimes bp->b_bufsize = bsize; 4732b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 4742b6b0df7SMatthew Dillon runningbufspace += bp->b_runningbufspace; 47526f9a767SRodney W. Grimes 47626f9a767SRodney W. Grimes /* do the input */ 477b99c307aSPoul-Henning Kamp BUF_STRATEGY(bp); 47826f9a767SRodney W. Grimes 479e47ed70bSJohn Dyson /* we definitely need to be at splvm here */ 48026f9a767SRodney W. Grimes 481e47ed70bSJohn Dyson s = splvm(); 48226f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 483aa2cabb9SDavid Greenman tsleep(bp, PVM, "vnsrd", 0); 48426f9a767SRodney W. Grimes } 48526f9a767SRodney W. Grimes splx(s); 486c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 48726f9a767SRodney W. Grimes error = EIO; 48826f9a767SRodney W. Grimes 48926f9a767SRodney W. Grimes /* 49026f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 49126f9a767SRodney W. Grimes */ 4921c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 49326f9a767SRodney W. Grimes if (error) 49426f9a767SRodney W. Grimes break; 4950d94caffSDavid Greenman 496aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 49726f9a767SRodney W. Grimes } else { 498aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 49926f9a767SRodney W. Grimes bzero((caddr_t) kva + i * bsize, bsize); 50026f9a767SRodney W. Grimes } 50126f9a767SRodney W. Grimes } 50226f9a767SRodney W. Grimes vm_pager_unmap_page(kva); 5030385347cSPeter Wemm pmap_clear_modify(m); 504e69763a3SDoug Rabson vm_page_flag_clear(m, PG_ZERO); 50526f9a767SRodney W. Grimes if (error) { 506a83c285cSDavid Greenman return VM_PAGER_ERROR; 50726f9a767SRodney W. Grimes } 50826f9a767SRodney W. Grimes return VM_PAGER_OK; 50926f9a767SRodney W. Grimes 51026f9a767SRodney W. Grimes } 51126f9a767SRodney W. Grimes 51226f9a767SRodney W. Grimes 51326f9a767SRodney W. Grimes /* 51426f9a767SRodney W. Grimes * old style vnode pager output routine 51526f9a767SRodney W. Grimes */ 516f708ef1bSPoul-Henning Kamp static int 51724a1cce3SDavid Greenman vnode_pager_input_old(object, m) 51824a1cce3SDavid Greenman vm_object_t object; 51926f9a767SRodney W. Grimes vm_page_t m; 52026f9a767SRodney W. Grimes { 521df8bae1dSRodney W. Grimes struct uio auio; 522df8bae1dSRodney W. Grimes struct iovec aiov; 52326f9a767SRodney W. Grimes int error; 52426f9a767SRodney W. Grimes int size; 52526f9a767SRodney W. Grimes vm_offset_t kva; 526342a1480SJohn Baldwin struct vnode *vp; 527df8bae1dSRodney W. Grimes 5280cddd8f0SMatthew Dillon GIANT_REQUIRED; 52926f9a767SRodney W. Grimes error = 0; 530bbc0ec52SDavid Greenman 531df8bae1dSRodney W. Grimes /* 53226f9a767SRodney W. Grimes * Return failure if beyond current EOF 53326f9a767SRodney W. Grimes */ 534a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 53526f9a767SRodney W. Grimes return VM_PAGER_BAD; 53626f9a767SRodney W. Grimes } else { 53726f9a767SRodney W. Grimes size = PAGE_SIZE; 538a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 539a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 5400bdb7528SDavid Greenman 54126f9a767SRodney W. Grimes /* 542df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 543df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 544df8bae1dSRodney W. Grimes */ 54526f9a767SRodney W. Grimes kva = vm_pager_map_page(m); 5460bdb7528SDavid Greenman 547342a1480SJohn Baldwin vp = object->handle; 548df8bae1dSRodney W. Grimes aiov.iov_base = (caddr_t) kva; 549df8bae1dSRodney W. Grimes aiov.iov_len = size; 550df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 551df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 552a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 553df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 55426f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 555df8bae1dSRodney W. Grimes auio.uio_resid = size; 556b40ce416SJulian Elischer auio.uio_td = curthread; 55726f9a767SRodney W. Grimes 558a854ed98SJohn Baldwin error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 559df8bae1dSRodney W. Grimes if (!error) { 56054d92145SMatthew Dillon int count = size - auio.uio_resid; 561df8bae1dSRodney W. Grimes 562df8bae1dSRodney W. Grimes if (count == 0) 563df8bae1dSRodney W. Grimes error = EINVAL; 56426f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 56526f9a767SRodney W. Grimes bzero((caddr_t) kva + count, PAGE_SIZE - count); 566df8bae1dSRodney W. Grimes } 56726f9a767SRodney W. Grimes vm_pager_unmap_page(kva); 568df8bae1dSRodney W. Grimes } 5690385347cSPeter Wemm pmap_clear_modify(m); 5702c28a105SAlan Cox vm_page_undirty(m); 571e69763a3SDoug Rabson vm_page_flag_clear(m, PG_ZERO); 5726e3a3f38SRobert V. Baron if (!error) 5736e3a3f38SRobert V. Baron m->valid = VM_PAGE_BITS_ALL; 574a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 57526f9a767SRodney W. Grimes } 57626f9a767SRodney W. Grimes 57726f9a767SRodney W. Grimes /* 57826f9a767SRodney W. Grimes * generic vnode pager input routine 57926f9a767SRodney W. Grimes */ 580170db9c6SJohn Dyson 581ce75f2c3SMike Smith /* 58223955314SAlfred Perlstein * Local media VFS's that do not implement their own VOP_GETPAGES 58323955314SAlfred Perlstein * should have their VOP_GETPAGES should call to 584ce75f2c3SMike Smith * vnode_pager_generic_getpages() to implement the previous behaviour. 585ce75f2c3SMike Smith * 586ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 587ce75f2c3SMike Smith * backing vp's VOP_GETPAGES. 588ce75f2c3SMike Smith */ 589f708ef1bSPoul-Henning Kamp static int 59024a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage) 59126f9a767SRodney W. Grimes vm_object_t object; 59224a1cce3SDavid Greenman vm_page_t *m; 59324a1cce3SDavid Greenman int count; 59424a1cce3SDavid Greenman int reqpage; 59524a1cce3SDavid Greenman { 596170db9c6SJohn Dyson int rtval; 597170db9c6SJohn Dyson struct vnode *vp; 59886ffbd76SMike Smith int bytes = count * PAGE_SIZE; 59995e5e988SJohn Dyson 6000cddd8f0SMatthew Dillon GIANT_REQUIRED; 601170db9c6SJohn Dyson vp = object->handle; 60286ffbd76SMike Smith rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 60323955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 60423955314SAlfred Perlstein ("vnode_pager: FS getpages not implemented\n")); 605170db9c6SJohn Dyson return rtval; 606170db9c6SJohn Dyson } 607170db9c6SJohn Dyson 608ce75f2c3SMike Smith 609ce75f2c3SMike Smith /* 610ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 611ce75f2c3SMike Smith * own vnodes if they fail to implement VOP_GETPAGES. 612ce75f2c3SMike Smith */ 613ce75f2c3SMike Smith int 614ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 615ce75f2c3SMike Smith struct vnode *vp; 616170db9c6SJohn Dyson vm_page_t *m; 617ce75f2c3SMike Smith int bytecount; 618170db9c6SJohn Dyson int reqpage; 619170db9c6SJohn Dyson { 620ce75f2c3SMike Smith vm_object_t object; 621a316d390SJohn Dyson vm_offset_t kva; 6228f9110f6SJohn Dyson off_t foff, tfoff, nextoff; 62324a1cce3SDavid Greenman int i, size, bsize, first, firstaddr; 624ce75f2c3SMike Smith struct vnode *dp; 625efc68ce1SDavid Greenman int runpg; 626efc68ce1SDavid Greenman int runend; 6270bdb7528SDavid Greenman struct buf *bp; 62826f9a767SRodney W. Grimes int s; 629ce75f2c3SMike Smith int count; 63026f9a767SRodney W. Grimes int error = 0; 63126f9a767SRodney W. Grimes 6320cddd8f0SMatthew Dillon GIANT_REQUIRED; 633ce75f2c3SMike Smith object = vp->v_object; 634ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 635ce75f2c3SMike Smith 6362c4488fcSJohn Dyson if (vp->v_mount == NULL) 6372c4488fcSJohn Dyson return VM_PAGER_BAD; 6382c4488fcSJohn Dyson 63926f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 64026f9a767SRodney W. Grimes 64126f9a767SRodney W. Grimes /* get the UNDERLYING device for the file with VOP_BMAP() */ 642bbc0ec52SDavid Greenman 64326f9a767SRodney W. Grimes /* 644bbc0ec52SDavid Greenman * originally, we did not check for an error return value -- assuming 645bbc0ec52SDavid Greenman * an fs always has a bmap entry point -- that assumption is wrong!!! 64626f9a767SRodney W. Grimes */ 647a316d390SJohn Dyson foff = IDX_TO_OFF(m[reqpage]->pindex); 648bbc0ec52SDavid Greenman 64926f9a767SRodney W. Grimes /* 65016f62314SDavid Greenman * if we can't bmap, use old VOP code 65126f9a767SRodney W. Grimes */ 652c83ebe77SJohn Dyson if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 65326f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 65426f9a767SRodney W. Grimes if (i != reqpage) { 655d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 65626f9a767SRodney W. Grimes } 65726f9a767SRodney W. Grimes } 658976e77fcSDavid Greenman cnt.v_vnodein++; 659976e77fcSDavid Greenman cnt.v_vnodepgsin++; 66024a1cce3SDavid Greenman return vnode_pager_input_old(object, m[reqpage]); 661bbc0ec52SDavid Greenman 66226f9a767SRodney W. Grimes /* 66326f9a767SRodney W. Grimes * if the blocksize is smaller than a page size, then use 66426f9a767SRodney W. Grimes * special small filesystem code. NFS sometimes has a small 66526f9a767SRodney W. Grimes * blocksize, but it can handle large reads itself. 66626f9a767SRodney W. Grimes */ 66726f9a767SRodney W. Grimes } else if ((PAGE_SIZE / bsize) > 1 && 668500b04a2SBruce Evans (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 66926f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 67026f9a767SRodney W. Grimes if (i != reqpage) { 671d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 67226f9a767SRodney W. Grimes } 67326f9a767SRodney W. Grimes } 674976e77fcSDavid Greenman cnt.v_vnodein++; 675976e77fcSDavid Greenman cnt.v_vnodepgsin++; 67624a1cce3SDavid Greenman return vnode_pager_input_smlfs(object, m[reqpage]); 67726f9a767SRodney W. Grimes } 6788d17e694SJulian Elischer 67926f9a767SRodney W. Grimes /* 6808d17e694SJulian Elischer * If we have a completely valid page available to us, we can 6818d17e694SJulian Elischer * clean up and return. Otherwise we have to re-read the 6828d17e694SJulian Elischer * media. 6830d94caffSDavid Greenman */ 6848d17e694SJulian Elischer if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 6850d94caffSDavid Greenman for (i = 0; i < count; i++) { 6860d94caffSDavid Greenman if (i != reqpage) 687d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 6880d94caffSDavid Greenman } 6890d94caffSDavid Greenman return VM_PAGER_OK; 6900d94caffSDavid Greenman } 6918d17e694SJulian Elischer m[reqpage]->valid = 0; 6920bdb7528SDavid Greenman 6930d94caffSDavid Greenman /* 69426f9a767SRodney W. Grimes * here on direct device I/O 69526f9a767SRodney W. Grimes */ 696efc68ce1SDavid Greenman firstaddr = -1; 697a1287949SEivind Eklund 69826f9a767SRodney W. Grimes /* 699efc68ce1SDavid Greenman * calculate the run that includes the required page 70026f9a767SRodney W. Grimes */ 701efc68ce1SDavid Greenman for (first = 0, i = 0; i < count; i = runend) { 702a316d390SJohn Dyson firstaddr = vnode_pager_addr(vp, 703a316d390SJohn Dyson IDX_TO_OFF(m[i]->pindex), &runpg); 704efc68ce1SDavid Greenman if (firstaddr == -1) { 70524a1cce3SDavid Greenman if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 706fc62ef1fSBruce Evans /* XXX no %qd in kernel. */ 707fc62ef1fSBruce Evans panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx", 708fc62ef1fSBruce Evans firstaddr, (u_long)(foff >> 32), 709fc62ef1fSBruce Evans (u_long)(u_int32_t)foff, 710fc62ef1fSBruce Evans (u_long)(u_int32_t) 711fc62ef1fSBruce Evans (object->un_pager.vnp.vnp_size >> 32), 712fc62ef1fSBruce Evans (u_long)(u_int32_t) 713fc62ef1fSBruce Evans object->un_pager.vnp.vnp_size); 714efc68ce1SDavid Greenman } 715d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 716efc68ce1SDavid Greenman runend = i + 1; 717efc68ce1SDavid Greenman first = runend; 718efc68ce1SDavid Greenman continue; 719efc68ce1SDavid Greenman } 720efc68ce1SDavid Greenman runend = i + runpg; 721efc68ce1SDavid Greenman if (runend <= reqpage) { 722efc68ce1SDavid Greenman int j; 723efc68ce1SDavid Greenman for (j = i; j < runend; j++) { 724d8d5fa88SAlfred Perlstein vm_page_free(m[j]); 725efc68ce1SDavid Greenman } 72626f9a767SRodney W. Grimes } else { 727efc68ce1SDavid Greenman if (runpg < (count - first)) { 728efc68ce1SDavid Greenman for (i = first + runpg; i < count; i++) 729d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 730efc68ce1SDavid Greenman count = first + runpg; 73126f9a767SRodney W. Grimes } 732efc68ce1SDavid Greenman break; 73326f9a767SRodney W. Grimes } 734efc68ce1SDavid Greenman first = runend; 735efc68ce1SDavid Greenman } 73626f9a767SRodney W. Grimes 73726f9a767SRodney W. Grimes /* 738bbc0ec52SDavid Greenman * the first and last page have been calculated now, move input pages 739bbc0ec52SDavid Greenman * to be zero based... 74026f9a767SRodney W. Grimes */ 74126f9a767SRodney W. Grimes if (first != 0) { 74226f9a767SRodney W. Grimes for (i = first; i < count; i++) { 74326f9a767SRodney W. Grimes m[i - first] = m[i]; 74426f9a767SRodney W. Grimes } 74526f9a767SRodney W. Grimes count -= first; 74626f9a767SRodney W. Grimes reqpage -= first; 74726f9a767SRodney W. Grimes } 748efc68ce1SDavid Greenman 74926f9a767SRodney W. Grimes /* 75026f9a767SRodney W. Grimes * calculate the file virtual address for the transfer 75126f9a767SRodney W. Grimes */ 752a316d390SJohn Dyson foff = IDX_TO_OFF(m[0]->pindex); 75326f9a767SRodney W. Grimes 75426f9a767SRodney W. Grimes /* 75526f9a767SRodney W. Grimes * calculate the size of the transfer 75626f9a767SRodney W. Grimes */ 75726f9a767SRodney W. Grimes size = count * PAGE_SIZE; 75824a1cce3SDavid Greenman if ((foff + size) > object->un_pager.vnp.vnp_size) 75924a1cce3SDavid Greenman size = object->un_pager.vnp.vnp_size - foff; 76026f9a767SRodney W. Grimes 76126f9a767SRodney W. Grimes /* 76224579ca1SMatthew Dillon * round up physical size for real devices. 76326f9a767SRodney W. Grimes */ 76424579ca1SMatthew Dillon if (dp->v_type == VBLK || dp->v_type == VCHR) { 76524579ca1SMatthew Dillon int secmask = dp->v_rdev->si_bsize_phys - 1; 76624579ca1SMatthew Dillon KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1)); 76724579ca1SMatthew Dillon size = (size + secmask) & ~secmask; 76824579ca1SMatthew Dillon } 76926f9a767SRodney W. Grimes 7701c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 77116f62314SDavid Greenman kva = (vm_offset_t) bp->b_data; 77216f62314SDavid Greenman 77326f9a767SRodney W. Grimes /* 77426f9a767SRodney W. Grimes * and map the pages to be read into the kva 77526f9a767SRodney W. Grimes */ 77616f62314SDavid Greenman pmap_qenter(kva, m, count); 77726f9a767SRodney W. Grimes 77826f9a767SRodney W. Grimes /* build a minimal buffer header */ 77921144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 78026f9a767SRodney W. Grimes bp->b_iodone = vnode_pager_iodone; 78126f9a767SRodney W. Grimes /* B_PHYS is not set, but it is nice to fill this in */ 782bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 783bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 784a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 785a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 786187f0071SDavid Greenman bp->b_blkno = firstaddr; 7870d94caffSDavid Greenman pbgetvp(dp, bp); 78826f9a767SRodney W. Grimes bp->b_bcount = size; 78926f9a767SRodney W. Grimes bp->b_bufsize = size; 7902b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 7912b6b0df7SMatthew Dillon runningbufspace += bp->b_runningbufspace; 79226f9a767SRodney W. Grimes 793976e77fcSDavid Greenman cnt.v_vnodein++; 794976e77fcSDavid Greenman cnt.v_vnodepgsin += count; 795976e77fcSDavid Greenman 79626f9a767SRodney W. Grimes /* do the input */ 797b99c307aSPoul-Henning Kamp BUF_STRATEGY(bp); 798976e77fcSDavid Greenman 799e47ed70bSJohn Dyson s = splvm(); 800e47ed70bSJohn Dyson /* we definitely need to be at splvm here */ 80126f9a767SRodney W. Grimes 80226f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 803aa2cabb9SDavid Greenman tsleep(bp, PVM, "vnread", 0); 80426f9a767SRodney W. Grimes } 80526f9a767SRodney W. Grimes splx(s); 806c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 80726f9a767SRodney W. Grimes error = EIO; 80826f9a767SRodney W. Grimes 80926f9a767SRodney W. Grimes if (!error) { 81026f9a767SRodney W. Grimes if (size != count * PAGE_SIZE) 81126f9a767SRodney W. Grimes bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 81226f9a767SRodney W. Grimes } 81316f62314SDavid Greenman pmap_qremove(kva, count); 81426f9a767SRodney W. Grimes 81526f9a767SRodney W. Grimes /* 81626f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 81726f9a767SRodney W. Grimes */ 8181c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 81926f9a767SRodney W. Grimes 8208f9110f6SJohn Dyson for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 8218f9110f6SJohn Dyson vm_page_t mt; 8228f9110f6SJohn Dyson 8238f9110f6SJohn Dyson nextoff = tfoff + PAGE_SIZE; 8248f9110f6SJohn Dyson mt = m[i]; 8258f9110f6SJohn Dyson 82654746b67SDmitrij Tejblum if (nextoff <= object->un_pager.vnp.vnp_size) { 8278d17e694SJulian Elischer /* 8288d17e694SJulian Elischer * Read filled up entire page. 8298d17e694SJulian Elischer */ 8308f9110f6SJohn Dyson mt->valid = VM_PAGE_BITS_ALL; 8312c28a105SAlan Cox vm_page_undirty(mt); /* should be an assert? XXX */ 8320385347cSPeter Wemm pmap_clear_modify(mt); 8338f9110f6SJohn Dyson } else { 8348d17e694SJulian Elischer /* 8358d17e694SJulian Elischer * Read did not fill up entire page. Since this 8368d17e694SJulian Elischer * is getpages, the page may be mapped, so we have 8378d17e694SJulian Elischer * to zero the invalid portions of the page even 8388d17e694SJulian Elischer * though we aren't setting them valid. 8398d17e694SJulian Elischer * 8408d17e694SJulian Elischer * Currently we do not set the entire page valid, 8418d17e694SJulian Elischer * we just try to clear the piece that we couldn't 8428d17e694SJulian Elischer * read. 8438d17e694SJulian Elischer */ 84454746b67SDmitrij Tejblum vm_page_set_validclean(mt, 0, 84554746b67SDmitrij Tejblum object->un_pager.vnp.vnp_size - tfoff); 8464221e284SAlan Cox /* handled by vm_fault now */ 8474221e284SAlan Cox /* vm_page_zero_invalid(mt, FALSE); */ 8488f9110f6SJohn Dyson } 8498f9110f6SJohn Dyson 850e69763a3SDoug Rabson vm_page_flag_clear(mt, PG_ZERO); 85126f9a767SRodney W. Grimes if (i != reqpage) { 852bbc0ec52SDavid Greenman 85326f9a767SRodney W. Grimes /* 854bbc0ec52SDavid Greenman * whether or not to leave the page activated is up in 855bbc0ec52SDavid Greenman * the air, but we should put the page on a page queue 856bbc0ec52SDavid Greenman * somewhere. (it already is in the object). Result: 857956f3135SPhilippe Charnier * It appears that empirical results show that 858bbc0ec52SDavid Greenman * deactivating pages is best. 85926f9a767SRodney W. Grimes */ 860bbc0ec52SDavid Greenman 86126f9a767SRodney W. Grimes /* 862bbc0ec52SDavid Greenman * just in case someone was asking for this page we 863bbc0ec52SDavid Greenman * now tell them that it is ok to use 86426f9a767SRodney W. Grimes */ 86526f9a767SRodney W. Grimes if (!error) { 8668f9110f6SJohn Dyson if (mt->flags & PG_WANTED) 8678f9110f6SJohn Dyson vm_page_activate(mt); 86895461b45SJohn Dyson else 8698f9110f6SJohn Dyson vm_page_deactivate(mt); 870e69763a3SDoug Rabson vm_page_wakeup(mt); 87126f9a767SRodney W. Grimes } else { 872d8d5fa88SAlfred Perlstein vm_page_free(mt); 87326f9a767SRodney W. Grimes } 87426f9a767SRodney W. Grimes } 87526f9a767SRodney W. Grimes } 87626f9a767SRodney W. Grimes if (error) { 87724a1cce3SDavid Greenman printf("vnode_pager_getpages: I/O read error\n"); 87826f9a767SRodney W. Grimes } 879a83c285cSDavid Greenman return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 88026f9a767SRodney W. Grimes } 88126f9a767SRodney W. Grimes 882ce75f2c3SMike Smith /* 883ce75f2c3SMike Smith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 884ce75f2c3SMike Smith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 885ce75f2c3SMike Smith * vnode_pager_generic_putpages() to implement the previous behaviour. 886ce75f2c3SMike Smith * 887ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 888ce75f2c3SMike Smith * backing vp's VOP_PUTPAGES. 889ce75f2c3SMike Smith */ 890e4542174SMatthew Dillon static void 891170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals) 892170db9c6SJohn Dyson vm_object_t object; 893170db9c6SJohn Dyson vm_page_t *m; 894170db9c6SJohn Dyson int count; 895170db9c6SJohn Dyson boolean_t sync; 896170db9c6SJohn Dyson int *rtvals; 897170db9c6SJohn Dyson { 898170db9c6SJohn Dyson int rtval; 899170db9c6SJohn Dyson struct vnode *vp; 900f2a2857bSKirk McKusick struct mount *mp; 90186ffbd76SMike Smith int bytes = count * PAGE_SIZE; 902ad980522SJohn Dyson 9030cddd8f0SMatthew Dillon GIANT_REQUIRED; 9040e3cdf2cSAlan Cox /* 9050e3cdf2cSAlan Cox * Force synchronous operation if we are extremely low on memory 9060e3cdf2cSAlan Cox * to prevent a low-memory deadlock. VOP operations often need to 9070e3cdf2cSAlan Cox * allocate more memory to initiate the I/O ( i.e. do a BMAP 9080e3cdf2cSAlan Cox * operation ). The swapper handles the case by limiting the amount 9090e3cdf2cSAlan Cox * of asynchronous I/O, but that sort of solution doesn't scale well 9100e3cdf2cSAlan Cox * for the vnode pager without a lot of work. 9110e3cdf2cSAlan Cox * 9120e3cdf2cSAlan Cox * Also, the backing vnode's iodone routine may not wake the pageout 9130e3cdf2cSAlan Cox * daemon up. This should be probably be addressed XXX. 9140e3cdf2cSAlan Cox */ 9150e3cdf2cSAlan Cox 9160e3cdf2cSAlan Cox if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 9170e3cdf2cSAlan Cox sync |= OBJPC_SYNC; 9180e3cdf2cSAlan Cox 9190e3cdf2cSAlan Cox /* 9200e3cdf2cSAlan Cox * Call device-specific putpages function 9210e3cdf2cSAlan Cox */ 922170db9c6SJohn Dyson vp = object->handle; 923f2a2857bSKirk McKusick if (vp->v_type != VREG) 924f2a2857bSKirk McKusick mp = NULL; 925f2a2857bSKirk McKusick (void)vn_start_write(vp, &mp, V_WAIT); 92686ffbd76SMike Smith rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 92723955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 92823955314SAlfred Perlstein ("vnode_pager: stale FS putpages\n")); 929f2a2857bSKirk McKusick vn_finished_write(mp); 930170db9c6SJohn Dyson } 931170db9c6SJohn Dyson 932ce75f2c3SMike Smith 93326f9a767SRodney W. Grimes /* 934ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 9354491ea91SEivind Eklund * own vnodes if they fail to implement VOP_PUTPAGES. 9362b6b0df7SMatthew Dillon * 9372b6b0df7SMatthew Dillon * This is typically called indirectly via the pageout daemon and 9382b6b0df7SMatthew Dillon * clustering has already typically occured, so in general we ask the 9392b6b0df7SMatthew Dillon * underlying filesystem to write the data out asynchronously rather 9402b6b0df7SMatthew Dillon * then delayed. 94126f9a767SRodney W. Grimes */ 942ce75f2c3SMike Smith int 9438f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 944ce75f2c3SMike Smith struct vnode *vp; 94526f9a767SRodney W. Grimes vm_page_t *m; 946ce75f2c3SMike Smith int bytecount; 9478f9110f6SJohn Dyson int flags; 94826f9a767SRodney W. Grimes int *rtvals; 94926f9a767SRodney W. Grimes { 950f6b04d2bSDavid Greenman int i; 951ce75f2c3SMike Smith vm_object_t object; 952ce75f2c3SMike Smith int count; 95326f9a767SRodney W. Grimes 954f6b04d2bSDavid Greenman int maxsize, ncount; 955a316d390SJohn Dyson vm_ooffset_t poffset; 956f6b04d2bSDavid Greenman struct uio auio; 957f6b04d2bSDavid Greenman struct iovec aiov; 958f6b04d2bSDavid Greenman int error; 9598f9110f6SJohn Dyson int ioflags; 96026f9a767SRodney W. Grimes 9610cddd8f0SMatthew Dillon GIANT_REQUIRED; 962ce75f2c3SMike Smith object = vp->v_object; 963ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 964ce75f2c3SMike Smith 96526f9a767SRodney W. Grimes for (i = 0; i < count; i++) 96626f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_AGAIN; 96726f9a767SRodney W. Grimes 968a316d390SJohn Dyson if ((int) m[0]->pindex < 0) { 9693efc015bSPeter Wemm printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n", 9703efc015bSPeter Wemm (long)m[0]->pindex, m[0]->dirty); 971f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 972f6b04d2bSDavid Greenman return VM_PAGER_BAD; 9730d94caffSDavid Greenman } 9740bdb7528SDavid Greenman 975f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 976f6b04d2bSDavid Greenman ncount = count; 97726f9a767SRodney W. Grimes 978a316d390SJohn Dyson poffset = IDX_TO_OFF(m[0]->pindex); 97900a6f47fSMatthew Dillon 98000a6f47fSMatthew Dillon /* 98100a6f47fSMatthew Dillon * If the page-aligned write is larger then the actual file we 98200a6f47fSMatthew Dillon * have to invalidate pages occuring beyond the file EOF. However, 98300a6f47fSMatthew Dillon * there is an edge case where a file may not be page-aligned where 98400a6f47fSMatthew Dillon * the last page is partially invalid. In this case the filesystem 98500a6f47fSMatthew Dillon * may not properly clear the dirty bits for the entire page (which 98600a6f47fSMatthew Dillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 98700a6f47fSMatthew Dillon * With the page locked we are free to fix-up the dirty bits here. 9883ebeaf59SMatthew Dillon * 9893ebeaf59SMatthew Dillon * We do not under any circumstances truncate the valid bits, as 9903ebeaf59SMatthew Dillon * this will screw up bogus page replacement. 99100a6f47fSMatthew Dillon */ 992a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 99300a6f47fSMatthew Dillon if (object->un_pager.vnp.vnp_size > poffset) { 99400a6f47fSMatthew Dillon int pgoff; 99500a6f47fSMatthew Dillon 996a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 997aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 99800a6f47fSMatthew Dillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 99900a6f47fSMatthew Dillon vm_page_clear_dirty(m[ncount - 1], pgoff, 100000a6f47fSMatthew Dillon PAGE_SIZE - pgoff); 100100a6f47fSMatthew Dillon } 100200a6f47fSMatthew Dillon } else { 100300a6f47fSMatthew Dillon maxsize = 0; 100400a6f47fSMatthew Dillon ncount = 0; 100500a6f47fSMatthew Dillon } 1006f6b04d2bSDavid Greenman if (ncount < count) { 1007f6b04d2bSDavid Greenman for (i = ncount; i < count; i++) { 1008f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 1009f6b04d2bSDavid Greenman } 1010f6b04d2bSDavid Greenman } 1011f6b04d2bSDavid Greenman } 101226f9a767SRodney W. Grimes 10132b6b0df7SMatthew Dillon /* 10142b6b0df7SMatthew Dillon * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 10152b6b0df7SMatthew Dillon * rather then a bdwrite() to prevent paging I/O from saturating 10162b6b0df7SMatthew Dillon * the buffer cache. 10172b6b0df7SMatthew Dillon */ 10188f9110f6SJohn Dyson ioflags = IO_VMIO; 10192b6b0df7SMatthew Dillon ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: IO_ASYNC; 10208f9110f6SJohn Dyson ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 1021f6b04d2bSDavid Greenman 1022f6b04d2bSDavid Greenman aiov.iov_base = (caddr_t) 0; 1023f6b04d2bSDavid Greenman aiov.iov_len = maxsize; 1024f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 1025f6b04d2bSDavid Greenman auio.uio_iovcnt = 1; 1026a316d390SJohn Dyson auio.uio_offset = poffset; 1027f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 1028f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 1029f6b04d2bSDavid Greenman auio.uio_resid = maxsize; 1030b40ce416SJulian Elischer auio.uio_td = (struct thread *) 0; 1031a854ed98SJohn Baldwin error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1032976e77fcSDavid Greenman cnt.v_vnodeout++; 1033f6b04d2bSDavid Greenman cnt.v_vnodepgsout += ncount; 1034f6b04d2bSDavid Greenman 1035f6b04d2bSDavid Greenman if (error) { 103624a1cce3SDavid Greenman printf("vnode_pager_putpages: I/O error %d\n", error); 1037f6b04d2bSDavid Greenman } 1038f6b04d2bSDavid Greenman if (auio.uio_resid) { 1039ac1e407bSBruce Evans printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1040ac1e407bSBruce Evans auio.uio_resid, (u_long)m[0]->pindex); 104126f9a767SRodney W. Grimes } 1042ffc82b0aSJohn Dyson for (i = 0; i < ncount; i++) { 104326f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 104426f9a767SRodney W. Grimes } 1045f6b04d2bSDavid Greenman return rtvals[0]; 104626f9a767SRodney W. Grimes } 1047f6b04d2bSDavid Greenman 1048f6b04d2bSDavid Greenman struct vnode * 104924a1cce3SDavid Greenman vnode_pager_lock(object) 105024a1cce3SDavid Greenman vm_object_t object; 105124a1cce3SDavid Greenman { 1052b40ce416SJulian Elischer struct thread *td = curthread; /* XXX */ 1053996c772fSJohn Dyson 10540cddd8f0SMatthew Dillon GIANT_REQUIRED; 10550cddd8f0SMatthew Dillon 105624a1cce3SDavid Greenman for (; object != NULL; object = object->backing_object) { 105724a1cce3SDavid Greenman if (object->type != OBJT_VNODE) 1058f6b04d2bSDavid Greenman continue; 1059e6b961ffSJohn Baldwin if (object->flags & OBJ_DEAD) { 106047221757SJohn Dyson return NULL; 1061e6b961ffSJohn Baldwin } 1062f6b04d2bSDavid Greenman 1063e6b961ffSJohn Baldwin /* XXX; If object->handle can change, we need to cache it. */ 106447221757SJohn Dyson while (vget(object->handle, 1065b40ce416SJulian Elischer LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, td)){ 1066bef608bdSJohn Dyson if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE)) 1067bef608bdSJohn Dyson return NULL; 106847221757SJohn Dyson printf("vnode_pager_lock: retrying\n"); 106947221757SJohn Dyson } 107024a1cce3SDavid Greenman return object->handle; 107126f9a767SRodney W. Grimes } 107224a1cce3SDavid Greenman return NULL; 1073f6b04d2bSDavid Greenman } 1074