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 21df8bae1dSRodney W. Grimes * 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 411efb74fbSJohn Dyson * $Id: vnode_pager.c,v 1.76 1997/12/02 21:07:20 phk Exp $ 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> 5924a1cce3SDavid Greenman #include <sys/buf.h> 60efeaf95aSDavid Greenman #include <sys/vmmeter.h> 61df8bae1dSRodney W. Grimes 62df8bae1dSRodney W. Grimes #include <vm/vm.h> 63efeaf95aSDavid Greenman #include <vm/vm_prot.h> 64efeaf95aSDavid Greenman #include <vm/vm_object.h> 65df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 6624a1cce3SDavid Greenman #include <vm/vm_pager.h> 671efb74fbSJohn Dyson #include <vm/vm_map.h> 68df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h> 69efeaf95aSDavid Greenman #include <vm/vm_extern.h> 70df8bae1dSRodney W. Grimes 71f708ef1bSPoul-Henning Kamp static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address, 720b8253a7SBruce Evans int *run)); 73f708ef1bSPoul-Henning Kamp static void vnode_pager_iodone __P((struct buf *bp)); 74f708ef1bSPoul-Henning Kamp static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m)); 75f708ef1bSPoul-Henning Kamp static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m)); 76f708ef1bSPoul-Henning Kamp static void vnode_pager_dealloc __P((vm_object_t)); 77f708ef1bSPoul-Henning Kamp static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 78f708ef1bSPoul-Henning Kamp static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *)); 79f708ef1bSPoul-Henning Kamp static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 800b8253a7SBruce Evans 81df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 8224a1cce3SDavid Greenman NULL, 83df8bae1dSRodney W. Grimes vnode_pager_alloc, 84df8bae1dSRodney W. Grimes vnode_pager_dealloc, 8524a1cce3SDavid Greenman vnode_pager_getpages, 8624a1cce3SDavid Greenman vnode_pager_putpages, 8724a1cce3SDavid Greenman vnode_pager_haspage, 8824a1cce3SDavid Greenman NULL 89df8bae1dSRodney W. Grimes }; 90df8bae1dSRodney W. Grimes 910b8253a7SBruce Evans static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m, 920b8253a7SBruce Evans int count, int reqpage)); 930b8253a7SBruce Evans static int vnode_pager_leaf_putpages __P((vm_object_t object, vm_page_t *m, 940b8253a7SBruce Evans int count, boolean_t sync, 950b8253a7SBruce Evans int *rtvals)); 96170db9c6SJohn Dyson 97df8bae1dSRodney W. Grimes /* 98df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 99df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 100df8bae1dSRodney W. Grimes */ 10124a1cce3SDavid Greenman vm_object_t 102b9dcd593SBruce Evans vnode_pager_alloc(void *handle, vm_size_t size, vm_prot_t prot, 103b9dcd593SBruce Evans vm_ooffset_t offset) 104df8bae1dSRodney W. Grimes { 10506cb7259SDavid Greenman vm_object_t object; 106df8bae1dSRodney W. Grimes struct vnode *vp; 107df8bae1dSRodney W. Grimes 108df8bae1dSRodney W. Grimes /* 109df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 110df8bae1dSRodney W. Grimes */ 111df8bae1dSRodney W. Grimes if (handle == NULL) 112df8bae1dSRodney W. Grimes return (NULL); 113df8bae1dSRodney W. Grimes 114df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 11539d38f93SDavid Greenman 11639d38f93SDavid Greenman /* 11739d38f93SDavid Greenman * Prevent race condition when allocating the object. This 11839d38f93SDavid Greenman * can happen with NFS vnodes since the nfsnode isn't locked. 11939d38f93SDavid Greenman */ 12039d38f93SDavid Greenman while (vp->v_flag & VOLOCK) { 12139d38f93SDavid Greenman vp->v_flag |= VOWANT; 12239d38f93SDavid Greenman tsleep(vp, PVM, "vnpobj", 0); 12339d38f93SDavid Greenman } 12439d38f93SDavid Greenman vp->v_flag |= VOLOCK; 12539d38f93SDavid Greenman 12639d38f93SDavid Greenman /* 12739d38f93SDavid Greenman * If the object is being terminated, wait for it to 12839d38f93SDavid Greenman * go away. 12939d38f93SDavid Greenman */ 130bd7e5f99SJohn Dyson while (((object = vp->v_object) != NULL) && 131bd7e5f99SJohn Dyson (object->flags & OBJ_DEAD)) { 132aa2cabb9SDavid Greenman tsleep(object, PVM, "vadead", 0); 13324a1cce3SDavid Greenman } 1340d94caffSDavid Greenman 13524a1cce3SDavid Greenman if (object == NULL) { 136df8bae1dSRodney W. Grimes /* 137df8bae1dSRodney W. Grimes * And an object of the appropriate size 138df8bae1dSRodney W. Grimes */ 139a316d390SJohn Dyson object = vm_object_allocate(OBJT_VNODE, size); 140ad5dd234SJohn Dyson if (vp->v_type == VREG) 1414bb62461SDavid Greenman object->flags = OBJ_CANPERSIST; 142ad5dd234SJohn Dyson else 143ad5dd234SJohn Dyson object->flags = 0; 144bbc0ec52SDavid Greenman 14509841510SDavid Greenman if (vp->v_usecount == 0) 14609841510SDavid Greenman panic("vnode_pager_alloc: no vnode reference"); 147df8bae1dSRodney W. Grimes /* 14824a1cce3SDavid Greenman * Hold a reference to the vnode and initialize object data. 149df8bae1dSRodney W. Grimes */ 1506476c0d2SJohn Dyson vp->v_usecount++; 151a316d390SJohn Dyson object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE; 15226f9a767SRodney W. Grimes 15324a1cce3SDavid Greenman object->handle = handle; 15424a1cce3SDavid Greenman vp->v_object = object; 155df8bae1dSRodney W. Grimes } else { 156df8bae1dSRodney W. Grimes /* 15724a1cce3SDavid Greenman * vm_object_reference() will remove the object from the cache if 15824a1cce3SDavid Greenman * found and gain a reference to the object. 159df8bae1dSRodney W. Grimes */ 16024a1cce3SDavid Greenman vm_object_reference(object); 161df8bae1dSRodney W. Grimes } 16239d38f93SDavid Greenman 163f6b04d2bSDavid Greenman if (vp->v_type == VREG) 164f6b04d2bSDavid Greenman vp->v_flag |= VVMIO; 16539d38f93SDavid Greenman 16639d38f93SDavid Greenman vp->v_flag &= ~VOLOCK; 16739d38f93SDavid Greenman if (vp->v_flag & VOWANT) { 16839d38f93SDavid Greenman vp->v_flag &= ~VOWANT; 16939d38f93SDavid Greenman wakeup(vp); 17039d38f93SDavid Greenman } 17124a1cce3SDavid Greenman return (object); 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes 174f708ef1bSPoul-Henning Kamp static void 17524a1cce3SDavid Greenman vnode_pager_dealloc(object) 1760d94caffSDavid Greenman vm_object_t object; 17724a1cce3SDavid Greenman { 17824a1cce3SDavid Greenman register struct vnode *vp = object->handle; 179df8bae1dSRodney W. Grimes 18024a1cce3SDavid Greenman if (vp == NULL) 18124a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 18224a1cce3SDavid Greenman 18324a1cce3SDavid Greenman if (object->paging_in_progress) { 1840d94caffSDavid Greenman int s = splbio(); 1850d94caffSDavid Greenman while (object->paging_in_progress) { 186c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 1870d94caffSDavid Greenman tsleep(object, PVM, "vnpdea", 0); 1880d94caffSDavid Greenman } 1890d94caffSDavid Greenman splx(s); 19024a1cce3SDavid Greenman } 19124a1cce3SDavid Greenman 19224a1cce3SDavid Greenman object->handle = NULL; 1930d94caffSDavid Greenman 194aa2cabb9SDavid Greenman vp->v_object = NULL; 1958e58bf68SDavid Greenman vp->v_flag &= ~(VTEXT | VVMIO); 196df8bae1dSRodney W. Grimes vrele(vp); 197df8bae1dSRodney W. Grimes } 19826f9a767SRodney W. Grimes 199f708ef1bSPoul-Henning Kamp static boolean_t 200a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after) 20124a1cce3SDavid Greenman vm_object_t object; 202a316d390SJohn Dyson vm_pindex_t pindex; 20324a1cce3SDavid Greenman int *before; 20424a1cce3SDavid Greenman int *after; 205df8bae1dSRodney W. Grimes { 20624a1cce3SDavid Greenman struct vnode *vp = object->handle; 207df8bae1dSRodney W. Grimes daddr_t bn; 2083af76890SPoul-Henning Kamp int err; 209170db9c6SJohn Dyson daddr_t reqblock; 2102c4488fcSJohn Dyson int poff; 2112c4488fcSJohn Dyson int bsize; 212d63596ceSJohn Dyson int pagesperblock, blocksperpage; 213df8bae1dSRodney W. Grimes 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 } 231170db9c6SJohn Dyson err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 232170db9c6SJohn Dyson after, before); 2330d94caffSDavid Greenman if (err) 23424a1cce3SDavid Greenman return TRUE; 2356eab77f2SJohn Dyson if ( bn == -1) 236ced399eeSJohn Dyson return FALSE; 237d63596ceSJohn Dyson if (pagesperblock > 0) { 238a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 239170db9c6SJohn Dyson if (before) { 240170db9c6SJohn Dyson *before *= pagesperblock; 241170db9c6SJohn Dyson *before += poff; 242170db9c6SJohn Dyson } 243170db9c6SJohn Dyson if (after) { 244b1fc01b7SJohn Dyson int numafter; 245170db9c6SJohn Dyson *after *= pagesperblock; 246b1fc01b7SJohn Dyson numafter = pagesperblock - (poff + 1); 247a316d390SJohn Dyson if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 248a316d390SJohn Dyson numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 249b1fc01b7SJohn Dyson } 250b1fc01b7SJohn Dyson *after += numafter; 251170db9c6SJohn Dyson } 252d63596ceSJohn Dyson } else { 253d63596ceSJohn Dyson if (before) { 254d63596ceSJohn Dyson *before /= blocksperpage; 255d63596ceSJohn Dyson } 256d63596ceSJohn Dyson 257d63596ceSJohn Dyson if (after) { 258d63596ceSJohn Dyson *after /= blocksperpage; 259d63596ceSJohn Dyson } 260d63596ceSJohn Dyson } 261ced399eeSJohn Dyson return TRUE; 262df8bae1dSRodney W. Grimes } 263df8bae1dSRodney W. Grimes 264df8bae1dSRodney W. Grimes /* 265df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 26624a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 267df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 268df8bae1dSRodney W. Grimes * 269df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 270df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 271df8bae1dSRodney W. Grimes */ 272df8bae1dSRodney W. Grimes void 273df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize) 274df8bae1dSRodney W. Grimes struct vnode *vp; 275a316d390SJohn Dyson vm_ooffset_t nsize; 276df8bae1dSRodney W. Grimes { 27724a1cce3SDavid Greenman vm_object_t object = vp->v_object; 278df8bae1dSRodney W. Grimes 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 288df8bae1dSRodney W. Grimes /* 289bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 290df8bae1dSRodney W. Grimes */ 29124a1cce3SDavid Greenman if (nsize < object->un_pager.vnp.vnp_size) { 292a316d390SJohn Dyson vm_ooffset_t nsizerounded; 293aa8de40aSPoul-Henning Kamp nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_MASK)); 294a316d390SJohn Dyson if (nsizerounded < object->un_pager.vnp.vnp_size) { 2951efb74fbSJohn Dyson vm_pindex_t st, end; 2961efb74fbSJohn Dyson st = OFF_TO_IDX(nsize + PAGE_MASK); 2971efb74fbSJohn Dyson end = OFF_TO_IDX(object->un_pager.vnp.vnp_size); 2981efb74fbSJohn Dyson 2991efb74fbSJohn Dyson vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size); 3001efb74fbSJohn Dyson vm_object_page_remove(object, st, end, FALSE); 3010d94caffSDavid Greenman } 302bbc0ec52SDavid Greenman /* 303bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 304bbc0ec52SDavid Greenman * only partially backed by the vnode... 305bbc0ec52SDavid Greenman */ 306bbc0ec52SDavid Greenman if (nsize & PAGE_MASK) { 307bbc0ec52SDavid Greenman vm_offset_t kva; 308bbc0ec52SDavid Greenman vm_page_t m; 309bbc0ec52SDavid Greenman 310a316d390SJohn Dyson m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 311bbc0ec52SDavid Greenman if (m) { 312bbc0ec52SDavid Greenman kva = vm_pager_map_page(m); 313bbc0ec52SDavid Greenman bzero((caddr_t) kva + (nsize & PAGE_MASK), 314a316d390SJohn Dyson (int) (round_page(nsize) - nsize)); 315bbc0ec52SDavid Greenman vm_pager_unmap_page(kva); 316bbc0ec52SDavid Greenman } 317bbc0ec52SDavid Greenman } 318bbc0ec52SDavid Greenman } 319a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 320aa8de40aSPoul-Henning Kamp object->size = OFF_TO_IDX(nsize + PAGE_MASK); 321df8bae1dSRodney W. Grimes } 322df8bae1dSRodney W. Grimes 323df8bae1dSRodney W. Grimes void 324df8bae1dSRodney W. Grimes vnode_pager_umount(mp) 325df8bae1dSRodney W. Grimes register struct mount *mp; 326df8bae1dSRodney W. Grimes { 327996c772fSJohn Dyson struct proc *p = curproc; /* XXX */ 32824a1cce3SDavid Greenman struct vnode *vp, *nvp; 329df8bae1dSRodney W. Grimes 33024a1cce3SDavid Greenman loop: 33124a1cce3SDavid Greenman for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 33224a1cce3SDavid Greenman /* 33324a1cce3SDavid Greenman * Vnode can be reclaimed by getnewvnode() while we 33424a1cce3SDavid Greenman * traverse the list. 33524a1cce3SDavid Greenman */ 33624a1cce3SDavid Greenman if (vp->v_mount != mp) 33724a1cce3SDavid Greenman goto loop; 33824a1cce3SDavid Greenman 339df8bae1dSRodney W. Grimes /* 340bbc0ec52SDavid Greenman * Save the next pointer now since uncaching may terminate the 34124a1cce3SDavid Greenman * object and render vnode invalid 342df8bae1dSRodney W. Grimes */ 34324a1cce3SDavid Greenman nvp = vp->v_mntvnodes.le_next; 34424a1cce3SDavid Greenman 34524a1cce3SDavid Greenman if (vp->v_object != NULL) { 346996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 347996c772fSJohn Dyson vnode_pager_uncache(vp, p); 348996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 349c01a9b8cSDavid Greenman } 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes } 352df8bae1dSRodney W. Grimes 353df8bae1dSRodney W. Grimes /* 354df8bae1dSRodney W. Grimes * Remove vnode associated object from the object cache. 355c01a9b8cSDavid Greenman * This routine must be called with the vnode locked. 356df8bae1dSRodney W. Grimes * 357c01a9b8cSDavid Greenman * XXX unlock the vnode. 358c01a9b8cSDavid Greenman * We must do this since uncaching the object may result in its 359c01a9b8cSDavid Greenman * destruction which may initiate paging activity which may necessitate 360c01a9b8cSDavid Greenman * re-locking the vnode. 36126f9a767SRodney W. Grimes */ 36224a1cce3SDavid Greenman void 363996c772fSJohn Dyson vnode_pager_uncache(vp, p) 36424a1cce3SDavid Greenman struct vnode *vp; 365996c772fSJohn Dyson struct proc *p; 36626f9a767SRodney W. Grimes { 36724a1cce3SDavid Greenman vm_object_t object; 36826f9a767SRodney W. Grimes 36926f9a767SRodney W. Grimes /* 37026f9a767SRodney W. Grimes * Not a mapped vnode 37126f9a767SRodney W. Grimes */ 372aa2cabb9SDavid Greenman object = vp->v_object; 3738e58bf68SDavid Greenman if (object == NULL) 37424a1cce3SDavid Greenman return; 3750d94caffSDavid Greenman 37624a1cce3SDavid Greenman vm_object_reference(object); 3771efb74fbSJohn Dyson vm_freeze_copyopts(object, 0, object->size); 3789fea9a6fSJohn Dyson 3799fea9a6fSJohn Dyson /* 3809fea9a6fSJohn Dyson * XXX We really should handle locking on 3819fea9a6fSJohn Dyson * VBLK devices... 3829fea9a6fSJohn Dyson */ 3839fea9a6fSJohn Dyson if (vp->v_type != VBLK) 384996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 38526f9a767SRodney W. Grimes pager_cache(object, FALSE); 3869fea9a6fSJohn Dyson if (vp->v_type != VBLK) 387996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 38824a1cce3SDavid Greenman return; 38926f9a767SRodney W. Grimes } 390df8bae1dSRodney W. Grimes 39126f9a767SRodney W. Grimes 39226f9a767SRodney W. Grimes void 39326f9a767SRodney W. Grimes vnode_pager_freepage(m) 39426f9a767SRodney W. Grimes vm_page_t m; 395df8bae1dSRodney W. Grimes { 39626f9a767SRodney W. Grimes PAGE_WAKEUP(m); 39726f9a767SRodney W. Grimes vm_page_free(m); 39826f9a767SRodney W. Grimes } 39926f9a767SRodney W. Grimes 40026f9a767SRodney W. Grimes /* 40126f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 40226f9a767SRodney W. Grimes * file address 40326f9a767SRodney W. Grimes */ 404f708ef1bSPoul-Henning Kamp static vm_offset_t 405efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run) 40626f9a767SRodney W. Grimes struct vnode *vp; 407a316d390SJohn Dyson vm_ooffset_t address; 408efc68ce1SDavid Greenman int *run; 40926f9a767SRodney W. Grimes { 41026f9a767SRodney W. Grimes int rtaddress; 41126f9a767SRodney W. Grimes int bsize; 412a316d390SJohn Dyson daddr_t block; 41326f9a767SRodney W. Grimes struct vnode *rtvp; 41426f9a767SRodney W. Grimes int err; 415a316d390SJohn Dyson daddr_t vblock; 416a316d390SJohn Dyson int voffset; 41726f9a767SRodney W. Grimes 4180d94caffSDavid Greenman if ((int) address < 0) 4190d94caffSDavid Greenman return -1; 4200d94caffSDavid Greenman 4212c4488fcSJohn Dyson if (vp->v_mount == NULL) 4222c4488fcSJohn Dyson return -1; 4232c4488fcSJohn Dyson 42426f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 42526f9a767SRodney W. Grimes vblock = address / bsize; 42626f9a767SRodney W. Grimes voffset = address % bsize; 42726f9a767SRodney W. Grimes 428c83ebe77SJohn Dyson err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 42926f9a767SRodney W. Grimes 430efc68ce1SDavid Greenman if (err || (block == -1)) 43126f9a767SRodney W. Grimes rtaddress = -1; 432efc68ce1SDavid Greenman else { 433187f0071SDavid Greenman rtaddress = block + voffset / DEV_BSIZE; 434efc68ce1SDavid Greenman if( run) { 435efc68ce1SDavid Greenman *run += 1; 436efc68ce1SDavid Greenman *run *= bsize/PAGE_SIZE; 437efc68ce1SDavid Greenman *run -= voffset/PAGE_SIZE; 438efc68ce1SDavid Greenman } 439efc68ce1SDavid Greenman } 44026f9a767SRodney W. Grimes 44126f9a767SRodney W. Grimes return rtaddress; 44226f9a767SRodney W. Grimes } 44326f9a767SRodney W. Grimes 44426f9a767SRodney W. Grimes /* 44526f9a767SRodney W. Grimes * interrupt routine for I/O completion 44626f9a767SRodney W. Grimes */ 447f708ef1bSPoul-Henning Kamp static void 44826f9a767SRodney W. Grimes vnode_pager_iodone(bp) 44926f9a767SRodney W. Grimes struct buf *bp; 45026f9a767SRodney W. Grimes { 45126f9a767SRodney W. Grimes bp->b_flags |= B_DONE; 45224a1cce3SDavid Greenman wakeup(bp); 45326f9a767SRodney W. Grimes } 45426f9a767SRodney W. Grimes 45526f9a767SRodney W. Grimes /* 45626f9a767SRodney W. Grimes * small block file system vnode pager input 45726f9a767SRodney W. Grimes */ 458f708ef1bSPoul-Henning Kamp static int 45924a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m) 46024a1cce3SDavid Greenman vm_object_t object; 46126f9a767SRodney W. Grimes vm_page_t m; 46226f9a767SRodney W. Grimes { 46326f9a767SRodney W. Grimes int i; 46426f9a767SRodney W. Grimes int s; 46526f9a767SRodney W. Grimes struct vnode *dp, *vp; 46626f9a767SRodney W. Grimes struct buf *bp; 46726f9a767SRodney W. Grimes vm_offset_t kva; 46826f9a767SRodney W. Grimes int fileaddr; 46926f9a767SRodney W. Grimes vm_offset_t bsize; 47026f9a767SRodney W. Grimes int error = 0; 47126f9a767SRodney W. Grimes 47224a1cce3SDavid Greenman vp = object->handle; 4732c4488fcSJohn Dyson if (vp->v_mount == NULL) 4742c4488fcSJohn Dyson return VM_PAGER_BAD; 4752c4488fcSJohn Dyson 47626f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 47726f9a767SRodney W. Grimes 4780bdb7528SDavid Greenman 479c83ebe77SJohn Dyson VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 48026f9a767SRodney W. Grimes 48126f9a767SRodney W. Grimes kva = vm_pager_map_page(m); 48226f9a767SRodney W. Grimes 48326f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 484bbc0ec52SDavid Greenman 485a316d390SJohn Dyson if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid)) 48626f9a767SRodney W. Grimes continue; 48726f9a767SRodney W. Grimes 488a316d390SJohn Dyson fileaddr = vnode_pager_addr(vp, 489a316d390SJohn Dyson IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 49026f9a767SRodney W. Grimes if (fileaddr != -1) { 49126f9a767SRodney W. Grimes bp = getpbuf(); 49226f9a767SRodney W. Grimes 49326f9a767SRodney W. Grimes /* build a minimal buffer header */ 49426f9a767SRodney W. Grimes bp->b_flags = B_BUSY | B_READ | B_CALL; 49526f9a767SRodney W. Grimes bp->b_iodone = vnode_pager_iodone; 49626f9a767SRodney W. Grimes bp->b_proc = curproc; 49726f9a767SRodney W. Grimes bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 49826f9a767SRodney W. Grimes if (bp->b_rcred != NOCRED) 49926f9a767SRodney W. Grimes crhold(bp->b_rcred); 50026f9a767SRodney W. Grimes if (bp->b_wcred != NOCRED) 50126f9a767SRodney W. Grimes crhold(bp->b_wcred); 502ab3f7469SPoul-Henning Kamp bp->b_data = (caddr_t) kva + i * bsize; 503187f0071SDavid Greenman bp->b_blkno = fileaddr; 5040d94caffSDavid Greenman pbgetvp(dp, bp); 50526f9a767SRodney W. Grimes bp->b_bcount = bsize; 50626f9a767SRodney W. Grimes bp->b_bufsize = bsize; 50726f9a767SRodney W. Grimes 50826f9a767SRodney W. Grimes /* do the input */ 50926f9a767SRodney W. Grimes VOP_STRATEGY(bp); 51026f9a767SRodney W. Grimes 51126f9a767SRodney W. Grimes /* we definitely need to be at splbio here */ 51226f9a767SRodney W. Grimes 51326f9a767SRodney W. Grimes s = splbio(); 51426f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 515aa2cabb9SDavid Greenman tsleep(bp, PVM, "vnsrd", 0); 51626f9a767SRodney W. Grimes } 51726f9a767SRodney W. Grimes splx(s); 51826f9a767SRodney W. Grimes if ((bp->b_flags & B_ERROR) != 0) 51926f9a767SRodney W. Grimes error = EIO; 52026f9a767SRodney W. Grimes 52126f9a767SRodney W. Grimes /* 52226f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 52326f9a767SRodney W. Grimes */ 52426f9a767SRodney W. Grimes relpbuf(bp); 52526f9a767SRodney W. Grimes if (error) 52626f9a767SRodney W. Grimes break; 5270d94caffSDavid Greenman 528aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 52926f9a767SRodney W. Grimes } else { 530aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 53126f9a767SRodney W. Grimes bzero((caddr_t) kva + i * bsize, bsize); 53226f9a767SRodney W. Grimes } 53326f9a767SRodney W. Grimes } 53426f9a767SRodney W. Grimes vm_pager_unmap_page(kva); 53567bf6868SJohn Dyson pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 536b1fc01b7SJohn Dyson m->flags &= ~PG_ZERO; 53726f9a767SRodney W. Grimes if (error) { 538a83c285cSDavid Greenman return VM_PAGER_ERROR; 53926f9a767SRodney W. Grimes } 54026f9a767SRodney W. Grimes return VM_PAGER_OK; 54126f9a767SRodney W. Grimes 54226f9a767SRodney W. Grimes } 54326f9a767SRodney W. Grimes 54426f9a767SRodney W. Grimes 54526f9a767SRodney W. Grimes /* 54626f9a767SRodney W. Grimes * old style vnode pager output routine 54726f9a767SRodney W. Grimes */ 548f708ef1bSPoul-Henning Kamp static int 54924a1cce3SDavid Greenman vnode_pager_input_old(object, m) 55024a1cce3SDavid Greenman vm_object_t object; 55126f9a767SRodney W. Grimes vm_page_t m; 55226f9a767SRodney W. Grimes { 553df8bae1dSRodney W. Grimes struct uio auio; 554df8bae1dSRodney W. Grimes struct iovec aiov; 55526f9a767SRodney W. Grimes int error; 55626f9a767SRodney W. Grimes int size; 55726f9a767SRodney W. Grimes vm_offset_t kva; 558df8bae1dSRodney W. Grimes 55926f9a767SRodney W. Grimes error = 0; 560bbc0ec52SDavid Greenman 561df8bae1dSRodney W. Grimes /* 56226f9a767SRodney W. Grimes * Return failure if beyond current EOF 56326f9a767SRodney W. Grimes */ 564a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 56526f9a767SRodney W. Grimes return VM_PAGER_BAD; 56626f9a767SRodney W. Grimes } else { 56726f9a767SRodney W. Grimes size = PAGE_SIZE; 568a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 569a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 5700bdb7528SDavid Greenman 57126f9a767SRodney W. Grimes /* 572df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 573df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 574df8bae1dSRodney W. Grimes */ 57526f9a767SRodney W. Grimes kva = vm_pager_map_page(m); 5760bdb7528SDavid Greenman 577df8bae1dSRodney W. Grimes aiov.iov_base = (caddr_t) kva; 578df8bae1dSRodney W. Grimes aiov.iov_len = size; 579df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 580df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 581a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 582df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 58326f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 584df8bae1dSRodney W. Grimes auio.uio_resid = size; 585df8bae1dSRodney W. Grimes auio.uio_procp = (struct proc *) 0; 58626f9a767SRodney W. Grimes 58724a1cce3SDavid Greenman error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 588df8bae1dSRodney W. Grimes if (!error) { 589df8bae1dSRodney W. Grimes register int count = size - auio.uio_resid; 590df8bae1dSRodney W. Grimes 591df8bae1dSRodney W. Grimes if (count == 0) 592df8bae1dSRodney W. Grimes error = EINVAL; 59326f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 59426f9a767SRodney W. Grimes bzero((caddr_t) kva + count, PAGE_SIZE - count); 595df8bae1dSRodney W. Grimes } 59626f9a767SRodney W. Grimes vm_pager_unmap_page(kva); 597df8bae1dSRodney W. Grimes } 59867bf6868SJohn Dyson pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 5990d94caffSDavid Greenman m->dirty = 0; 600b1fc01b7SJohn Dyson m->flags &= ~PG_ZERO; 601a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 60226f9a767SRodney W. Grimes } 60326f9a767SRodney W. Grimes 60426f9a767SRodney W. Grimes /* 60526f9a767SRodney W. Grimes * generic vnode pager input routine 60626f9a767SRodney W. Grimes */ 607170db9c6SJohn Dyson 608f708ef1bSPoul-Henning Kamp static int 60924a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage) 61026f9a767SRodney W. Grimes vm_object_t object; 61124a1cce3SDavid Greenman vm_page_t *m; 61224a1cce3SDavid Greenman int count; 61324a1cce3SDavid Greenman int reqpage; 61424a1cce3SDavid Greenman { 615170db9c6SJohn Dyson int rtval; 616170db9c6SJohn Dyson struct vnode *vp; 617ad980522SJohn Dyson if (object->flags & OBJ_VNODE_GONE) 618ad980522SJohn Dyson return VM_PAGER_ERROR; 619170db9c6SJohn Dyson vp = object->handle; 6202c4488fcSJohn Dyson rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0); 621170db9c6SJohn Dyson if (rtval == EOPNOTSUPP) 6220b8253a7SBruce Evans return vnode_pager_leaf_getpages(object, m, count, reqpage); 623170db9c6SJohn Dyson else 624170db9c6SJohn Dyson return rtval; 625170db9c6SJohn Dyson } 626170db9c6SJohn Dyson 627170db9c6SJohn Dyson static int 628170db9c6SJohn Dyson vnode_pager_leaf_getpages(object, m, count, reqpage) 629170db9c6SJohn Dyson vm_object_t object; 630170db9c6SJohn Dyson vm_page_t *m; 631170db9c6SJohn Dyson int count; 632170db9c6SJohn Dyson int reqpage; 633170db9c6SJohn Dyson { 634a316d390SJohn Dyson vm_offset_t kva; 635a316d390SJohn Dyson off_t foff; 63624a1cce3SDavid Greenman int i, size, bsize, first, firstaddr; 63726f9a767SRodney W. Grimes struct vnode *dp, *vp; 638efc68ce1SDavid Greenman int runpg; 639efc68ce1SDavid Greenman int runend; 6400bdb7528SDavid Greenman struct buf *bp; 64126f9a767SRodney W. Grimes int s; 64226f9a767SRodney W. Grimes int error = 0; 64326f9a767SRodney W. Grimes 64424a1cce3SDavid Greenman vp = object->handle; 6452c4488fcSJohn Dyson if (vp->v_mount == NULL) 6462c4488fcSJohn Dyson return VM_PAGER_BAD; 6472c4488fcSJohn Dyson 64826f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 64926f9a767SRodney W. Grimes 65026f9a767SRodney W. Grimes /* get the UNDERLYING device for the file with VOP_BMAP() */ 651bbc0ec52SDavid Greenman 65226f9a767SRodney W. Grimes /* 653bbc0ec52SDavid Greenman * originally, we did not check for an error return value -- assuming 654bbc0ec52SDavid Greenman * an fs always has a bmap entry point -- that assumption is wrong!!! 65526f9a767SRodney W. Grimes */ 656a316d390SJohn Dyson foff = IDX_TO_OFF(m[reqpage]->pindex); 657bbc0ec52SDavid Greenman 65826f9a767SRodney W. Grimes /* 65916f62314SDavid Greenman * if we can't bmap, use old VOP code 66026f9a767SRodney W. Grimes */ 661c83ebe77SJohn Dyson if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 66226f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 66326f9a767SRodney W. Grimes if (i != reqpage) { 66426f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 66526f9a767SRodney W. Grimes } 66626f9a767SRodney W. Grimes } 667976e77fcSDavid Greenman cnt.v_vnodein++; 668976e77fcSDavid Greenman cnt.v_vnodepgsin++; 66924a1cce3SDavid Greenman return vnode_pager_input_old(object, m[reqpage]); 670bbc0ec52SDavid Greenman 67126f9a767SRodney W. Grimes /* 67226f9a767SRodney W. Grimes * if the blocksize is smaller than a page size, then use 67326f9a767SRodney W. Grimes * special small filesystem code. NFS sometimes has a small 67426f9a767SRodney W. Grimes * blocksize, but it can handle large reads itself. 67526f9a767SRodney W. Grimes */ 67626f9a767SRodney W. Grimes } else if ((PAGE_SIZE / bsize) > 1 && 67726f9a767SRodney W. Grimes (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) { 67826f9a767SRodney W. Grimes 67926f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 68026f9a767SRodney W. Grimes if (i != reqpage) { 68126f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 68226f9a767SRodney W. Grimes } 68326f9a767SRodney W. Grimes } 684976e77fcSDavid Greenman cnt.v_vnodein++; 685976e77fcSDavid Greenman cnt.v_vnodepgsin++; 68624a1cce3SDavid Greenman return vnode_pager_input_smlfs(object, m[reqpage]); 68726f9a767SRodney W. Grimes } 68826f9a767SRodney W. Grimes /* 6890d94caffSDavid Greenman * if ANY DEV_BSIZE blocks are valid on a large filesystem block 6900d94caffSDavid Greenman * then, the entire page is valid -- 69132ad9cb5SDoug Rabson * XXX no it isn't 6920d94caffSDavid Greenman */ 69332ad9cb5SDoug Rabson 69432ad9cb5SDoug Rabson if (m[reqpage]->valid != VM_PAGE_BITS_ALL) 69532ad9cb5SDoug Rabson m[reqpage]->valid = 0; 69632ad9cb5SDoug Rabson 6970d94caffSDavid Greenman if (m[reqpage]->valid) { 6980d94caffSDavid Greenman m[reqpage]->valid = VM_PAGE_BITS_ALL; 6990d94caffSDavid Greenman for (i = 0; i < count; i++) { 7000d94caffSDavid Greenman if (i != reqpage) 7010d94caffSDavid Greenman vnode_pager_freepage(m[i]); 7020d94caffSDavid Greenman } 7030d94caffSDavid Greenman return VM_PAGER_OK; 7040d94caffSDavid Greenman } 7050bdb7528SDavid Greenman 7060d94caffSDavid Greenman /* 70726f9a767SRodney W. Grimes * here on direct device I/O 70826f9a767SRodney W. Grimes */ 70926f9a767SRodney W. Grimes 710efc68ce1SDavid Greenman firstaddr = -1; 71126f9a767SRodney W. Grimes /* 712efc68ce1SDavid Greenman * calculate the run that includes the required page 71326f9a767SRodney W. Grimes */ 714efc68ce1SDavid Greenman for(first = 0, i = 0; i < count; i = runend) { 715a316d390SJohn Dyson firstaddr = vnode_pager_addr(vp, 716a316d390SJohn Dyson IDX_TO_OFF(m[i]->pindex), &runpg); 717efc68ce1SDavid Greenman if (firstaddr == -1) { 71824a1cce3SDavid Greenman if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 71924a1cce3SDavid Greenman panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d", 72024a1cce3SDavid Greenman firstaddr, foff, object->un_pager.vnp.vnp_size); 721efc68ce1SDavid Greenman } 72226f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 723efc68ce1SDavid Greenman runend = i + 1; 724efc68ce1SDavid Greenman first = runend; 725efc68ce1SDavid Greenman continue; 726efc68ce1SDavid Greenman } 727efc68ce1SDavid Greenman runend = i + runpg; 728efc68ce1SDavid Greenman if (runend <= reqpage) { 729efc68ce1SDavid Greenman int j; 730efc68ce1SDavid Greenman for (j = i; j < runend; j++) { 731efc68ce1SDavid Greenman vnode_pager_freepage(m[j]); 732efc68ce1SDavid Greenman } 73326f9a767SRodney W. Grimes } else { 734efc68ce1SDavid Greenman if (runpg < (count - first)) { 735efc68ce1SDavid Greenman for (i = first + runpg; i < count; i++) 73626f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 737efc68ce1SDavid Greenman count = first + runpg; 73826f9a767SRodney W. Grimes } 739efc68ce1SDavid Greenman break; 74026f9a767SRodney W. Grimes } 741efc68ce1SDavid Greenman first = runend; 742efc68ce1SDavid Greenman } 74326f9a767SRodney W. Grimes 74426f9a767SRodney W. Grimes /* 745bbc0ec52SDavid Greenman * the first and last page have been calculated now, move input pages 746bbc0ec52SDavid Greenman * to be zero based... 74726f9a767SRodney W. Grimes */ 74826f9a767SRodney W. Grimes if (first != 0) { 74926f9a767SRodney W. Grimes for (i = first; i < count; i++) { 75026f9a767SRodney W. Grimes m[i - first] = m[i]; 75126f9a767SRodney W. Grimes } 75226f9a767SRodney W. Grimes count -= first; 75326f9a767SRodney W. Grimes reqpage -= first; 75426f9a767SRodney W. Grimes } 755efc68ce1SDavid Greenman 75626f9a767SRodney W. Grimes /* 75726f9a767SRodney W. Grimes * calculate the file virtual address for the transfer 75826f9a767SRodney W. Grimes */ 759a316d390SJohn Dyson foff = IDX_TO_OFF(m[0]->pindex); 76026f9a767SRodney W. Grimes 76126f9a767SRodney W. Grimes /* 76226f9a767SRodney W. Grimes * calculate the size of the transfer 76326f9a767SRodney W. Grimes */ 76426f9a767SRodney W. Grimes size = count * PAGE_SIZE; 76524a1cce3SDavid Greenman if ((foff + size) > object->un_pager.vnp.vnp_size) 76624a1cce3SDavid Greenman size = object->un_pager.vnp.vnp_size - foff; 76726f9a767SRodney W. Grimes 76826f9a767SRodney W. Grimes /* 76926f9a767SRodney W. Grimes * round up physical size for real devices 77026f9a767SRodney W. Grimes */ 77126f9a767SRodney W. Grimes if (dp->v_type == VBLK || dp->v_type == VCHR) 77226f9a767SRodney W. Grimes size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 77326f9a767SRodney W. Grimes 7746d40c3d3SDavid Greenman bp = getpbuf(); 77516f62314SDavid Greenman kva = (vm_offset_t) bp->b_data; 77616f62314SDavid Greenman 77726f9a767SRodney W. Grimes /* 77826f9a767SRodney W. Grimes * and map the pages to be read into the kva 77926f9a767SRodney W. Grimes */ 78016f62314SDavid Greenman pmap_qenter(kva, m, count); 78126f9a767SRodney W. Grimes 78226f9a767SRodney W. Grimes /* build a minimal buffer header */ 78326f9a767SRodney W. Grimes bp->b_flags = B_BUSY | B_READ | B_CALL; 78426f9a767SRodney W. Grimes bp->b_iodone = vnode_pager_iodone; 78526f9a767SRodney W. Grimes /* B_PHYS is not set, but it is nice to fill this in */ 78626f9a767SRodney W. Grimes bp->b_proc = curproc; 78726f9a767SRodney W. Grimes bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 78826f9a767SRodney W. Grimes if (bp->b_rcred != NOCRED) 78926f9a767SRodney W. Grimes crhold(bp->b_rcred); 79026f9a767SRodney W. Grimes if (bp->b_wcred != NOCRED) 79126f9a767SRodney W. Grimes crhold(bp->b_wcred); 792187f0071SDavid Greenman bp->b_blkno = firstaddr; 7930d94caffSDavid Greenman pbgetvp(dp, bp); 79426f9a767SRodney W. Grimes bp->b_bcount = size; 79526f9a767SRodney W. Grimes bp->b_bufsize = size; 79626f9a767SRodney W. Grimes 797976e77fcSDavid Greenman cnt.v_vnodein++; 798976e77fcSDavid Greenman cnt.v_vnodepgsin += count; 799976e77fcSDavid Greenman 80026f9a767SRodney W. Grimes /* do the input */ 80126f9a767SRodney W. Grimes VOP_STRATEGY(bp); 802976e77fcSDavid Greenman 80326f9a767SRodney W. Grimes s = splbio(); 80426f9a767SRodney W. Grimes /* we definitely need to be at splbio here */ 80526f9a767SRodney W. Grimes 80626f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 807aa2cabb9SDavid Greenman tsleep(bp, PVM, "vnread", 0); 80826f9a767SRodney W. Grimes } 80926f9a767SRodney W. Grimes splx(s); 81026f9a767SRodney W. Grimes if ((bp->b_flags & B_ERROR) != 0) 81126f9a767SRodney W. Grimes error = EIO; 81226f9a767SRodney W. Grimes 81326f9a767SRodney W. Grimes if (!error) { 81426f9a767SRodney W. Grimes if (size != count * PAGE_SIZE) 81526f9a767SRodney W. Grimes bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 81626f9a767SRodney W. Grimes } 81716f62314SDavid Greenman pmap_qremove(kva, count); 81826f9a767SRodney W. Grimes 81926f9a767SRodney W. Grimes /* 82026f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 82126f9a767SRodney W. Grimes */ 82226f9a767SRodney W. Grimes relpbuf(bp); 82326f9a767SRodney W. Grimes 82426f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 82567bf6868SJohn Dyson pmap_clear_modify(VM_PAGE_TO_PHYS(m[i])); 8260d94caffSDavid Greenman m[i]->dirty = 0; 8270d94caffSDavid Greenman m[i]->valid = VM_PAGE_BITS_ALL; 828b1fc01b7SJohn Dyson m[i]->flags &= ~PG_ZERO; 82926f9a767SRodney W. Grimes if (i != reqpage) { 830bbc0ec52SDavid Greenman 83126f9a767SRodney W. Grimes /* 832bbc0ec52SDavid Greenman * whether or not to leave the page activated is up in 833bbc0ec52SDavid Greenman * the air, but we should put the page on a page queue 834bbc0ec52SDavid Greenman * somewhere. (it already is in the object). Result: 835bbc0ec52SDavid Greenman * It appears that emperical results show that 836bbc0ec52SDavid Greenman * deactivating pages is best. 83726f9a767SRodney W. Grimes */ 838bbc0ec52SDavid Greenman 83926f9a767SRodney W. Grimes /* 840bbc0ec52SDavid Greenman * just in case someone was asking for this page we 841bbc0ec52SDavid Greenman * now tell them that it is ok to use 84226f9a767SRodney W. Grimes */ 84326f9a767SRodney W. Grimes if (!error) { 84426f9a767SRodney W. Grimes vm_page_deactivate(m[i]); 84526f9a767SRodney W. Grimes PAGE_WAKEUP(m[i]); 84626f9a767SRodney W. Grimes } else { 84726f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 84826f9a767SRodney W. Grimes } 84926f9a767SRodney W. Grimes } 85026f9a767SRodney W. Grimes } 85126f9a767SRodney W. Grimes if (error) { 85224a1cce3SDavid Greenman printf("vnode_pager_getpages: I/O read error\n"); 85326f9a767SRodney W. Grimes } 854a83c285cSDavid Greenman return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 85526f9a767SRodney W. Grimes } 85626f9a767SRodney W. Grimes 857f708ef1bSPoul-Henning Kamp static int 858170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals) 859170db9c6SJohn Dyson vm_object_t object; 860170db9c6SJohn Dyson vm_page_t *m; 861170db9c6SJohn Dyson int count; 862170db9c6SJohn Dyson boolean_t sync; 863170db9c6SJohn Dyson int *rtvals; 864170db9c6SJohn Dyson { 865170db9c6SJohn Dyson int rtval; 866170db9c6SJohn Dyson struct vnode *vp; 867ad980522SJohn Dyson 868ad980522SJohn Dyson if (object->flags & OBJ_VNODE_GONE) 869ad980522SJohn Dyson return VM_PAGER_ERROR; 870ad980522SJohn Dyson 871170db9c6SJohn Dyson vp = object->handle; 8722c4488fcSJohn Dyson rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0); 873170db9c6SJohn Dyson if (rtval == EOPNOTSUPP) 8740b8253a7SBruce Evans return vnode_pager_leaf_putpages(object, m, count, sync, rtvals); 875170db9c6SJohn Dyson else 876170db9c6SJohn Dyson return rtval; 877170db9c6SJohn Dyson } 878170db9c6SJohn Dyson 87926f9a767SRodney W. Grimes /* 88026f9a767SRodney W. Grimes * generic vnode pager output routine 88126f9a767SRodney W. Grimes */ 882170db9c6SJohn Dyson static int 883170db9c6SJohn Dyson vnode_pager_leaf_putpages(object, m, count, sync, rtvals) 88424a1cce3SDavid Greenman vm_object_t object; 88526f9a767SRodney W. Grimes vm_page_t *m; 88626f9a767SRodney W. Grimes int count; 88724a1cce3SDavid Greenman boolean_t sync; 88826f9a767SRodney W. Grimes int *rtvals; 88926f9a767SRodney W. Grimes { 890f6b04d2bSDavid Greenman int i; 89126f9a767SRodney W. Grimes 892f6b04d2bSDavid Greenman struct vnode *vp; 893f6b04d2bSDavid Greenman int maxsize, ncount; 894a316d390SJohn Dyson vm_ooffset_t poffset; 895f6b04d2bSDavid Greenman struct uio auio; 896f6b04d2bSDavid Greenman struct iovec aiov; 897f6b04d2bSDavid Greenman int error; 89826f9a767SRodney W. Grimes 89924a1cce3SDavid Greenman vp = object->handle;; 90026f9a767SRodney W. Grimes for (i = 0; i < count; i++) 90126f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_AGAIN; 90226f9a767SRodney W. Grimes 903a316d390SJohn Dyson if ((int) m[0]->pindex < 0) { 904a316d390SJohn Dyson printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty); 905f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 906f6b04d2bSDavid Greenman return VM_PAGER_BAD; 9070d94caffSDavid Greenman } 9080bdb7528SDavid Greenman 909f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 910f6b04d2bSDavid Greenman ncount = count; 91126f9a767SRodney W. Grimes 912a316d390SJohn Dyson poffset = IDX_TO_OFF(m[0]->pindex); 913a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 914a316d390SJohn Dyson if (object->un_pager.vnp.vnp_size > poffset) 915a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 9165f55e841SDavid Greenman else 9175f55e841SDavid Greenman maxsize = 0; 918aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 919f6b04d2bSDavid Greenman if (ncount < count) { 920f6b04d2bSDavid Greenman for (i = ncount; i < count; i++) { 921f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 922f6b04d2bSDavid Greenman } 923a316d390SJohn Dyson #ifdef BOGUS 924f6b04d2bSDavid Greenman if (ncount == 0) { 925a316d390SJohn Dyson printf("vnode_pager_putpages: write past end of file: %d, %lu\n", 926a316d390SJohn Dyson poffset, 927a316d390SJohn Dyson (unsigned long) object->un_pager.vnp.vnp_size); 92826f9a767SRodney W. Grimes return rtvals[0]; 92926f9a767SRodney W. Grimes } 930a316d390SJohn Dyson #endif 931f6b04d2bSDavid Greenman } 932f6b04d2bSDavid Greenman } 93326f9a767SRodney W. Grimes 93426f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 9355f55e841SDavid Greenman m[i]->busy++; 936f6b04d2bSDavid Greenman m[i]->flags &= ~PG_BUSY; 93726f9a767SRodney W. Grimes } 938f6b04d2bSDavid Greenman 939f6b04d2bSDavid Greenman aiov.iov_base = (caddr_t) 0; 940f6b04d2bSDavid Greenman aiov.iov_len = maxsize; 941f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 942f6b04d2bSDavid Greenman auio.uio_iovcnt = 1; 943a316d390SJohn Dyson auio.uio_offset = poffset; 944f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 945f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 946f6b04d2bSDavid Greenman auio.uio_resid = maxsize; 947f6b04d2bSDavid Greenman auio.uio_procp = (struct proc *) 0; 948a316d390SJohn Dyson error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred); 949976e77fcSDavid Greenman cnt.v_vnodeout++; 950f6b04d2bSDavid Greenman cnt.v_vnodepgsout += ncount; 951f6b04d2bSDavid Greenman 952f6b04d2bSDavid Greenman if (error) { 95324a1cce3SDavid Greenman printf("vnode_pager_putpages: I/O error %d\n", error); 954f6b04d2bSDavid Greenman } 955f6b04d2bSDavid Greenman if (auio.uio_resid) { 956f708ef1bSPoul-Henning Kamp printf("vnode_pager_putpages: residual I/O %d at %ld\n", 957a316d390SJohn Dyson auio.uio_resid, m[0]->pindex); 95826f9a767SRodney W. Grimes } 95926f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 9605f55e841SDavid Greenman m[i]->busy--; 961f6b04d2bSDavid Greenman if (i < ncount) { 96226f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 96326f9a767SRodney W. Grimes } 964f6b04d2bSDavid Greenman if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED)) 96524a1cce3SDavid Greenman wakeup(m[i]); 96626f9a767SRodney W. Grimes } 967f6b04d2bSDavid Greenman return rtvals[0]; 96826f9a767SRodney W. Grimes } 969f6b04d2bSDavid Greenman 970f6b04d2bSDavid Greenman struct vnode * 97124a1cce3SDavid Greenman vnode_pager_lock(object) 97224a1cce3SDavid Greenman vm_object_t object; 97324a1cce3SDavid Greenman { 974996c772fSJohn Dyson struct proc *p = curproc; /* XXX */ 975996c772fSJohn Dyson 97624a1cce3SDavid Greenman for (; object != NULL; object = object->backing_object) { 97724a1cce3SDavid Greenman if (object->type != OBJT_VNODE) 978f6b04d2bSDavid Greenman continue; 979f6b04d2bSDavid Greenman 980e7b0208fSJohn Dyson vn_lock(object->handle, 981e7b0208fSJohn Dyson LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p); 98224a1cce3SDavid Greenman return object->handle; 98326f9a767SRodney W. Grimes } 98424a1cce3SDavid Greenman return NULL; 985f6b04d2bSDavid Greenman } 986