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 41eed2d59bSDavid Greenman * $Id: vnode_pager.c,v 1.49 1995/09/12 14:42:43 dyson 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> 560d94caffSDavid Greenman #include <sys/kernel.h> 57df8bae1dSRodney W. Grimes #include <sys/proc.h> 58df8bae1dSRodney W. Grimes #include <sys/malloc.h> 59df8bae1dSRodney W. Grimes #include <sys/vnode.h> 60df8bae1dSRodney W. Grimes #include <sys/uio.h> 61df8bae1dSRodney W. Grimes #include <sys/mount.h> 6224a1cce3SDavid Greenman #include <sys/buf.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #include <vm/vm.h> 65df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 6624a1cce3SDavid Greenman #include <vm/vm_pager.h> 67df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h> 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 7024a1cce3SDavid Greenman NULL, 71df8bae1dSRodney W. Grimes vnode_pager_alloc, 72df8bae1dSRodney W. Grimes vnode_pager_dealloc, 7324a1cce3SDavid Greenman vnode_pager_getpages, 7424a1cce3SDavid Greenman vnode_pager_putpages, 7524a1cce3SDavid Greenman vnode_pager_haspage, 7624a1cce3SDavid Greenman NULL 77df8bae1dSRodney W. Grimes }; 78df8bae1dSRodney W. Grimes 79170db9c6SJohn Dyson static int vnode_pager_leaf_getpages(); 80170db9c6SJohn Dyson 81170db9c6SJohn Dyson static int vnode_pager_leaf_putpages(); 82df8bae1dSRodney W. Grimes /* 83df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 84df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 85df8bae1dSRodney W. Grimes */ 8624a1cce3SDavid Greenman vm_object_t 8726f9a767SRodney W. Grimes vnode_pager_alloc(handle, size, prot, offset) 88ee3a64c9SDavid Greenman void *handle; 89df8bae1dSRodney W. Grimes vm_size_t size; 90df8bae1dSRodney W. Grimes vm_prot_t prot; 9126f9a767SRodney W. Grimes vm_offset_t offset; 92df8bae1dSRodney W. Grimes { 9306cb7259SDavid Greenman vm_object_t object; 94df8bae1dSRodney W. Grimes struct vnode *vp; 95df8bae1dSRodney W. Grimes 96df8bae1dSRodney W. Grimes /* 97df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 98df8bae1dSRodney W. Grimes */ 99df8bae1dSRodney W. Grimes if (handle == NULL) 100df8bae1dSRodney W. Grimes return (NULL); 101df8bae1dSRodney W. Grimes 102df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 10339d38f93SDavid Greenman 10439d38f93SDavid Greenman /* 10539d38f93SDavid Greenman * Prevent race condition when allocating the object. This 10639d38f93SDavid Greenman * can happen with NFS vnodes since the nfsnode isn't locked. 10739d38f93SDavid Greenman */ 10839d38f93SDavid Greenman while (vp->v_flag & VOLOCK) { 10939d38f93SDavid Greenman vp->v_flag |= VOWANT; 11039d38f93SDavid Greenman tsleep(vp, PVM, "vnpobj", 0); 11139d38f93SDavid Greenman } 11239d38f93SDavid Greenman vp->v_flag |= VOLOCK; 11339d38f93SDavid Greenman 11439d38f93SDavid Greenman /* 11539d38f93SDavid Greenman * If the object is being terminated, wait for it to 11639d38f93SDavid Greenman * go away. 11739d38f93SDavid Greenman */ 11824a1cce3SDavid Greenman while (((object = vp->v_object) != NULL) && (object->flags & OBJ_DEAD)) { 119aa2cabb9SDavid Greenman tsleep(object, PVM, "vadead", 0); 12024a1cce3SDavid Greenman } 1210d94caffSDavid Greenman 12224a1cce3SDavid Greenman if (object == NULL) { 123df8bae1dSRodney W. Grimes /* 124df8bae1dSRodney W. Grimes * And an object of the appropriate size 125df8bae1dSRodney W. Grimes */ 12624a1cce3SDavid Greenman object = vm_object_allocate(OBJT_VNODE, round_page(size)); 1274bb62461SDavid Greenman object->flags = OBJ_CANPERSIST; 128bbc0ec52SDavid Greenman 129df8bae1dSRodney W. Grimes /* 13024a1cce3SDavid Greenman * Hold a reference to the vnode and initialize object data. 131df8bae1dSRodney W. Grimes */ 132df8bae1dSRodney W. Grimes VREF(vp); 13324a1cce3SDavid Greenman object->un_pager.vnp.vnp_size = size; 13426f9a767SRodney W. Grimes 13524a1cce3SDavid Greenman object->handle = handle; 13624a1cce3SDavid Greenman vp->v_object = object; 137df8bae1dSRodney W. Grimes } else { 138df8bae1dSRodney W. Grimes /* 13924a1cce3SDavid Greenman * vm_object_reference() will remove the object from the cache if 14024a1cce3SDavid Greenman * found and gain a reference to the object. 141df8bae1dSRodney W. Grimes */ 14224a1cce3SDavid Greenman vm_object_reference(object); 143df8bae1dSRodney W. Grimes } 14439d38f93SDavid Greenman 145f6b04d2bSDavid Greenman if (vp->v_type == VREG) 146f6b04d2bSDavid Greenman vp->v_flag |= VVMIO; 14739d38f93SDavid Greenman 14839d38f93SDavid Greenman vp->v_flag &= ~VOLOCK; 14939d38f93SDavid Greenman if (vp->v_flag & VOWANT) { 15039d38f93SDavid Greenman vp->v_flag &= ~VOWANT; 15139d38f93SDavid Greenman wakeup(vp); 15239d38f93SDavid Greenman } 15324a1cce3SDavid Greenman return (object); 154df8bae1dSRodney W. Grimes } 155df8bae1dSRodney W. Grimes 15626f9a767SRodney W. Grimes void 15724a1cce3SDavid Greenman vnode_pager_dealloc(object) 1580d94caffSDavid Greenman vm_object_t object; 15924a1cce3SDavid Greenman { 16024a1cce3SDavid Greenman register struct vnode *vp = object->handle; 161df8bae1dSRodney W. Grimes 16224a1cce3SDavid Greenman if (vp == NULL) 16324a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 16424a1cce3SDavid Greenman 16524a1cce3SDavid Greenman if (object->paging_in_progress) { 1660d94caffSDavid Greenman int s = splbio(); 1670d94caffSDavid Greenman while (object->paging_in_progress) { 168c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 1690d94caffSDavid Greenman tsleep(object, PVM, "vnpdea", 0); 1700d94caffSDavid Greenman } 1710d94caffSDavid Greenman splx(s); 17224a1cce3SDavid Greenman } 17324a1cce3SDavid Greenman 17424a1cce3SDavid Greenman object->handle = NULL; 1750d94caffSDavid Greenman 176aa2cabb9SDavid Greenman vp->v_object = NULL; 1778e58bf68SDavid Greenman vp->v_flag &= ~(VTEXT | VVMIO); 17800072442SDavid Greenman vp->v_flag |= VAGE; 179df8bae1dSRodney W. Grimes vrele(vp); 180df8bae1dSRodney W. Grimes } 18126f9a767SRodney W. Grimes 18226f9a767SRodney W. Grimes boolean_t 18324a1cce3SDavid Greenman vnode_pager_haspage(object, offset, before, after) 18424a1cce3SDavid Greenman vm_object_t object; 185df8bae1dSRodney W. Grimes vm_offset_t offset; 18624a1cce3SDavid Greenman int *before; 18724a1cce3SDavid Greenman int *after; 188df8bae1dSRodney W. Grimes { 18924a1cce3SDavid Greenman struct vnode *vp = object->handle; 190df8bae1dSRodney W. Grimes daddr_t bn; 191170db9c6SJohn Dyson daddr_t reqblock; 192eed2d59bSDavid Greenman int err, run, poff, bsize, pagesperblock; 193df8bae1dSRodney W. Grimes 194df8bae1dSRodney W. Grimes /* 1950d94caffSDavid Greenman * If filesystem no longer mounted or offset beyond end of file we do 1960d94caffSDavid Greenman * not have the page. 197df8bae1dSRodney W. Grimes */ 19824a1cce3SDavid Greenman if ((vp->v_mount == NULL) || (offset >= object->un_pager.vnp.vnp_size)) 1994abc71c0SDavid Greenman return FALSE; 200df8bae1dSRodney W. Grimes 201eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 202170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 203170db9c6SJohn Dyson reqblock = offset / bsize; 204170db9c6SJohn Dyson err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 205170db9c6SJohn Dyson after, before); 2060d94caffSDavid Greenman if (err) 20724a1cce3SDavid Greenman return TRUE; 2086eab77f2SJohn Dyson if ( bn == -1) 209ced399eeSJohn Dyson return FALSE; 210170db9c6SJohn Dyson poff = (offset - (reqblock * bsize)) / PAGE_SIZE; 211170db9c6SJohn Dyson if (before) { 212170db9c6SJohn Dyson *before *= pagesperblock; 213170db9c6SJohn Dyson *before += poff; 214170db9c6SJohn Dyson } 215170db9c6SJohn Dyson if (after) { 216b1fc01b7SJohn Dyson int numafter; 217170db9c6SJohn Dyson *after *= pagesperblock; 218b1fc01b7SJohn Dyson numafter = pagesperblock - (poff + 1); 219b1fc01b7SJohn Dyson if (offset + numafter * PAGE_SIZE > object->un_pager.vnp.vnp_size) { 220b1fc01b7SJohn Dyson numafter = (object->un_pager.vnp.vnp_size - offset)/PAGE_SIZE; 221b1fc01b7SJohn Dyson } 222b1fc01b7SJohn Dyson *after += numafter; 223170db9c6SJohn Dyson } 224ced399eeSJohn Dyson return TRUE; 225df8bae1dSRodney W. Grimes } 226df8bae1dSRodney W. Grimes 227df8bae1dSRodney W. Grimes /* 228df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 22924a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 230df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 231df8bae1dSRodney W. Grimes * 232df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 233df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 234df8bae1dSRodney W. Grimes */ 235df8bae1dSRodney W. Grimes void 236df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize) 237df8bae1dSRodney W. Grimes struct vnode *vp; 238df8bae1dSRodney W. Grimes u_long nsize; 239df8bae1dSRodney W. Grimes { 24024a1cce3SDavid Greenman vm_object_t object = vp->v_object; 241df8bae1dSRodney W. Grimes 24224a1cce3SDavid Greenman if (object == NULL) 243df8bae1dSRodney W. Grimes return; 244bbc0ec52SDavid Greenman 245df8bae1dSRodney W. Grimes /* 246df8bae1dSRodney W. Grimes * Hasn't changed size 247df8bae1dSRodney W. Grimes */ 24824a1cce3SDavid Greenman if (nsize == object->un_pager.vnp.vnp_size) 249df8bae1dSRodney W. Grimes return; 250bbc0ec52SDavid Greenman 251df8bae1dSRodney W. Grimes /* 252bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 253df8bae1dSRodney W. Grimes */ 25424a1cce3SDavid Greenman if (nsize < object->un_pager.vnp.vnp_size) { 25524a1cce3SDavid Greenman if (round_page((vm_offset_t) nsize) < object->un_pager.vnp.vnp_size) { 256df8bae1dSRodney W. Grimes vm_object_page_remove(object, 25724a1cce3SDavid Greenman round_page((vm_offset_t) nsize), object->un_pager.vnp.vnp_size, FALSE); 2580d94caffSDavid Greenman } 259bbc0ec52SDavid Greenman /* 260bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 261bbc0ec52SDavid Greenman * only partially backed by the vnode... 262bbc0ec52SDavid Greenman */ 263bbc0ec52SDavid Greenman if (nsize & PAGE_MASK) { 264bbc0ec52SDavid Greenman vm_offset_t kva; 265bbc0ec52SDavid Greenman vm_page_t m; 266bbc0ec52SDavid Greenman 267bbc0ec52SDavid Greenman m = vm_page_lookup(object, trunc_page((vm_offset_t) nsize)); 268bbc0ec52SDavid Greenman if (m) { 269bbc0ec52SDavid Greenman kva = vm_pager_map_page(m); 270bbc0ec52SDavid Greenman bzero((caddr_t) kva + (nsize & PAGE_MASK), 271bbc0ec52SDavid Greenman round_page(nsize) - nsize); 272bbc0ec52SDavid Greenman vm_pager_unmap_page(kva); 273bbc0ec52SDavid Greenman } 274bbc0ec52SDavid Greenman } 275bbc0ec52SDavid Greenman } 27624a1cce3SDavid Greenman object->un_pager.vnp.vnp_size = (vm_offset_t) nsize; 277bbc0ec52SDavid Greenman object->size = round_page(nsize); 278df8bae1dSRodney W. Grimes } 279df8bae1dSRodney W. Grimes 280df8bae1dSRodney W. Grimes void 281df8bae1dSRodney W. Grimes vnode_pager_umount(mp) 282df8bae1dSRodney W. Grimes register struct mount *mp; 283df8bae1dSRodney W. Grimes { 28424a1cce3SDavid Greenman struct vnode *vp, *nvp; 285df8bae1dSRodney W. Grimes 28624a1cce3SDavid Greenman loop: 28724a1cce3SDavid Greenman for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 28824a1cce3SDavid Greenman /* 28924a1cce3SDavid Greenman * Vnode can be reclaimed by getnewvnode() while we 29024a1cce3SDavid Greenman * traverse the list. 29124a1cce3SDavid Greenman */ 29224a1cce3SDavid Greenman if (vp->v_mount != mp) 29324a1cce3SDavid Greenman goto loop; 29424a1cce3SDavid Greenman 295df8bae1dSRodney W. Grimes /* 296bbc0ec52SDavid Greenman * Save the next pointer now since uncaching may terminate the 29724a1cce3SDavid Greenman * object and render vnode invalid 298df8bae1dSRodney W. Grimes */ 29924a1cce3SDavid Greenman nvp = vp->v_mntvnodes.le_next; 30024a1cce3SDavid Greenman 30124a1cce3SDavid Greenman if (vp->v_object != NULL) { 302c01a9b8cSDavid Greenman VOP_LOCK(vp); 30324a1cce3SDavid Greenman vnode_pager_uncache(vp); 304c01a9b8cSDavid Greenman VOP_UNLOCK(vp); 305c01a9b8cSDavid Greenman } 306df8bae1dSRodney W. Grimes } 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * Remove vnode associated object from the object cache. 311c01a9b8cSDavid Greenman * This routine must be called with the vnode locked. 312df8bae1dSRodney W. Grimes * 313c01a9b8cSDavid Greenman * XXX unlock the vnode. 314c01a9b8cSDavid Greenman * We must do this since uncaching the object may result in its 315c01a9b8cSDavid Greenman * destruction which may initiate paging activity which may necessitate 316c01a9b8cSDavid Greenman * re-locking the vnode. 31726f9a767SRodney W. Grimes */ 31824a1cce3SDavid Greenman void 31926f9a767SRodney W. Grimes vnode_pager_uncache(vp) 32024a1cce3SDavid Greenman struct vnode *vp; 32126f9a767SRodney W. Grimes { 32224a1cce3SDavid Greenman vm_object_t object; 32326f9a767SRodney W. Grimes 32426f9a767SRodney W. Grimes /* 32526f9a767SRodney W. Grimes * Not a mapped vnode 32626f9a767SRodney W. Grimes */ 327aa2cabb9SDavid Greenman object = vp->v_object; 3288e58bf68SDavid Greenman if (object == NULL) 32924a1cce3SDavid Greenman return; 3300d94caffSDavid Greenman 33124a1cce3SDavid Greenman vm_object_reference(object); 332c01a9b8cSDavid Greenman VOP_UNLOCK(vp); 33326f9a767SRodney W. Grimes pager_cache(object, FALSE); 334c01a9b8cSDavid Greenman VOP_LOCK(vp); 33524a1cce3SDavid Greenman return; 33626f9a767SRodney W. Grimes } 337df8bae1dSRodney W. Grimes 33826f9a767SRodney W. Grimes 33926f9a767SRodney W. Grimes void 34026f9a767SRodney W. Grimes vnode_pager_freepage(m) 34126f9a767SRodney W. Grimes vm_page_t m; 342df8bae1dSRodney W. Grimes { 34326f9a767SRodney W. Grimes PAGE_WAKEUP(m); 34426f9a767SRodney W. Grimes vm_page_free(m); 34526f9a767SRodney W. Grimes } 34626f9a767SRodney W. Grimes 34726f9a767SRodney W. Grimes /* 34826f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 34926f9a767SRodney W. Grimes * file address 35026f9a767SRodney W. Grimes */ 35126f9a767SRodney W. Grimes vm_offset_t 352efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run) 35326f9a767SRodney W. Grimes struct vnode *vp; 35426f9a767SRodney W. Grimes vm_offset_t address; 355efc68ce1SDavid Greenman int *run; 35626f9a767SRodney W. Grimes { 35726f9a767SRodney W. Grimes int rtaddress; 35826f9a767SRodney W. Grimes int bsize; 35926f9a767SRodney W. Grimes vm_offset_t block; 36026f9a767SRodney W. Grimes struct vnode *rtvp; 36126f9a767SRodney W. Grimes int err; 36226f9a767SRodney W. Grimes int vblock, voffset; 36326f9a767SRodney W. Grimes 3640d94caffSDavid Greenman if ((int) address < 0) 3650d94caffSDavid Greenman return -1; 3660d94caffSDavid Greenman 36726f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 36826f9a767SRodney W. Grimes vblock = address / bsize; 36926f9a767SRodney W. Grimes voffset = address % bsize; 37026f9a767SRodney W. Grimes 371c83ebe77SJohn Dyson err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 37226f9a767SRodney W. Grimes 373efc68ce1SDavid Greenman if (err || (block == -1)) 37426f9a767SRodney W. Grimes rtaddress = -1; 375efc68ce1SDavid Greenman else { 376187f0071SDavid Greenman rtaddress = block + voffset / DEV_BSIZE; 377efc68ce1SDavid Greenman if( run) { 378efc68ce1SDavid Greenman *run += 1; 379efc68ce1SDavid Greenman *run *= bsize/PAGE_SIZE; 380efc68ce1SDavid Greenman *run -= voffset/PAGE_SIZE; 381efc68ce1SDavid Greenman } 382efc68ce1SDavid Greenman } 38326f9a767SRodney W. Grimes 38426f9a767SRodney W. Grimes return rtaddress; 38526f9a767SRodney W. Grimes } 38626f9a767SRodney W. Grimes 38726f9a767SRodney W. Grimes /* 38826f9a767SRodney W. Grimes * interrupt routine for I/O completion 38926f9a767SRodney W. Grimes */ 39026f9a767SRodney W. Grimes void 39126f9a767SRodney W. Grimes vnode_pager_iodone(bp) 39226f9a767SRodney W. Grimes struct buf *bp; 39326f9a767SRodney W. Grimes { 39426f9a767SRodney W. Grimes bp->b_flags |= B_DONE; 39524a1cce3SDavid Greenman wakeup(bp); 39626f9a767SRodney W. Grimes } 39726f9a767SRodney W. Grimes 39826f9a767SRodney W. Grimes /* 39926f9a767SRodney W. Grimes * small block file system vnode pager input 40026f9a767SRodney W. Grimes */ 40126f9a767SRodney W. Grimes int 40224a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m) 40324a1cce3SDavid Greenman vm_object_t object; 40426f9a767SRodney W. Grimes vm_page_t m; 40526f9a767SRodney W. Grimes { 40626f9a767SRodney W. Grimes int i; 40726f9a767SRodney W. Grimes int s; 40826f9a767SRodney W. Grimes struct vnode *dp, *vp; 40926f9a767SRodney W. Grimes struct buf *bp; 41026f9a767SRodney W. Grimes vm_offset_t kva; 41126f9a767SRodney W. Grimes int fileaddr; 41226f9a767SRodney W. Grimes vm_offset_t bsize; 41326f9a767SRodney W. Grimes int error = 0; 41426f9a767SRodney W. Grimes 41524a1cce3SDavid Greenman vp = object->handle; 41626f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 41726f9a767SRodney W. Grimes 4180bdb7528SDavid Greenman 419c83ebe77SJohn Dyson VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 42026f9a767SRodney W. Grimes 42126f9a767SRodney W. Grimes kva = vm_pager_map_page(m); 42226f9a767SRodney W. Grimes 42326f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 424bbc0ec52SDavid Greenman 4250d94caffSDavid Greenman if ((vm_page_bits(m->offset + i * bsize, bsize) & m->valid)) 42626f9a767SRodney W. Grimes continue; 42726f9a767SRodney W. Grimes 428efc68ce1SDavid Greenman fileaddr = vnode_pager_addr(vp, m->offset + i * bsize, (int *)0); 42926f9a767SRodney W. Grimes if (fileaddr != -1) { 43026f9a767SRodney W. Grimes bp = getpbuf(); 43126f9a767SRodney W. Grimes 43226f9a767SRodney W. Grimes /* build a minimal buffer header */ 43326f9a767SRodney W. Grimes bp->b_flags = B_BUSY | B_READ | B_CALL; 43426f9a767SRodney W. Grimes bp->b_iodone = vnode_pager_iodone; 43526f9a767SRodney W. Grimes bp->b_proc = curproc; 43626f9a767SRodney W. Grimes bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 43726f9a767SRodney W. Grimes if (bp->b_rcred != NOCRED) 43826f9a767SRodney W. Grimes crhold(bp->b_rcred); 43926f9a767SRodney W. Grimes if (bp->b_wcred != NOCRED) 44026f9a767SRodney W. Grimes crhold(bp->b_wcred); 44126f9a767SRodney W. Grimes bp->b_un.b_addr = (caddr_t) kva + i * bsize; 442187f0071SDavid Greenman bp->b_blkno = fileaddr; 4430d94caffSDavid Greenman pbgetvp(dp, bp); 44426f9a767SRodney W. Grimes bp->b_bcount = bsize; 44526f9a767SRodney W. Grimes bp->b_bufsize = bsize; 44626f9a767SRodney W. Grimes 44726f9a767SRodney W. Grimes /* do the input */ 44826f9a767SRodney W. Grimes VOP_STRATEGY(bp); 44926f9a767SRodney W. Grimes 45026f9a767SRodney W. Grimes /* we definitely need to be at splbio here */ 45126f9a767SRodney W. Grimes 45226f9a767SRodney W. Grimes s = splbio(); 45326f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 454aa2cabb9SDavid Greenman tsleep(bp, PVM, "vnsrd", 0); 45526f9a767SRodney W. Grimes } 45626f9a767SRodney W. Grimes splx(s); 45726f9a767SRodney W. Grimes if ((bp->b_flags & B_ERROR) != 0) 45826f9a767SRodney W. Grimes error = EIO; 45926f9a767SRodney W. Grimes 46026f9a767SRodney W. Grimes /* 46126f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 46226f9a767SRodney W. Grimes */ 46326f9a767SRodney W. Grimes relpbuf(bp); 46426f9a767SRodney W. Grimes if (error) 46526f9a767SRodney W. Grimes break; 4660d94caffSDavid Greenman 467170db9c6SJohn Dyson vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize); 46826f9a767SRodney W. Grimes } else { 469b1fc01b7SJohn Dyson vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize); 47026f9a767SRodney W. Grimes bzero((caddr_t) kva + i * bsize, bsize); 47126f9a767SRodney W. Grimes } 47226f9a767SRodney W. Grimes } 47326f9a767SRodney W. Grimes vm_pager_unmap_page(kva); 4740d94caffSDavid Greenman pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 475b1fc01b7SJohn Dyson m->flags &= ~PG_ZERO; 47626f9a767SRodney W. Grimes if (error) { 477a83c285cSDavid Greenman return VM_PAGER_ERROR; 47826f9a767SRodney W. Grimes } 47926f9a767SRodney W. Grimes return VM_PAGER_OK; 48026f9a767SRodney W. Grimes 48126f9a767SRodney W. Grimes } 48226f9a767SRodney W. Grimes 48326f9a767SRodney W. Grimes 48426f9a767SRodney W. Grimes /* 48526f9a767SRodney W. Grimes * old style vnode pager output routine 48626f9a767SRodney W. Grimes */ 48726f9a767SRodney W. Grimes int 48824a1cce3SDavid Greenman vnode_pager_input_old(object, m) 48924a1cce3SDavid Greenman vm_object_t object; 49026f9a767SRodney W. Grimes vm_page_t m; 49126f9a767SRodney W. Grimes { 492df8bae1dSRodney W. Grimes struct uio auio; 493df8bae1dSRodney W. Grimes struct iovec aiov; 49426f9a767SRodney W. Grimes int error; 49526f9a767SRodney W. Grimes int size; 49626f9a767SRodney W. Grimes vm_offset_t kva; 497df8bae1dSRodney W. Grimes 49826f9a767SRodney W. Grimes error = 0; 499bbc0ec52SDavid Greenman 500df8bae1dSRodney W. Grimes /* 50126f9a767SRodney W. Grimes * Return failure if beyond current EOF 50226f9a767SRodney W. Grimes */ 50324a1cce3SDavid Greenman if (m->offset >= object->un_pager.vnp.vnp_size) { 50426f9a767SRodney W. Grimes return VM_PAGER_BAD; 50526f9a767SRodney W. Grimes } else { 50626f9a767SRodney W. Grimes size = PAGE_SIZE; 50724a1cce3SDavid Greenman if (m->offset + size > object->un_pager.vnp.vnp_size) 50824a1cce3SDavid Greenman size = object->un_pager.vnp.vnp_size - m->offset; 5090bdb7528SDavid Greenman 51026f9a767SRodney W. Grimes /* 511df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 512df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 513df8bae1dSRodney W. Grimes */ 51426f9a767SRodney W. Grimes kva = vm_pager_map_page(m); 5150bdb7528SDavid Greenman 516df8bae1dSRodney W. Grimes aiov.iov_base = (caddr_t) kva; 517df8bae1dSRodney W. Grimes aiov.iov_len = size; 518df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 519df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 5200d94caffSDavid Greenman auio.uio_offset = m->offset; 521df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 52226f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 523df8bae1dSRodney W. Grimes auio.uio_resid = size; 524df8bae1dSRodney W. Grimes auio.uio_procp = (struct proc *) 0; 52526f9a767SRodney W. Grimes 52624a1cce3SDavid Greenman error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 527df8bae1dSRodney W. Grimes if (!error) { 528df8bae1dSRodney W. Grimes register int count = size - auio.uio_resid; 529df8bae1dSRodney W. Grimes 530df8bae1dSRodney W. Grimes if (count == 0) 531df8bae1dSRodney W. Grimes error = EINVAL; 53226f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 53326f9a767SRodney W. Grimes bzero((caddr_t) kva + count, PAGE_SIZE - count); 534df8bae1dSRodney W. Grimes } 53526f9a767SRodney W. Grimes vm_pager_unmap_page(kva); 536df8bae1dSRodney W. Grimes } 53726f9a767SRodney W. Grimes pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 5380d94caffSDavid Greenman m->dirty = 0; 539b1fc01b7SJohn Dyson m->flags &= ~PG_ZERO; 540a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 54126f9a767SRodney W. Grimes } 54226f9a767SRodney W. Grimes 54326f9a767SRodney W. Grimes /* 54426f9a767SRodney W. Grimes * generic vnode pager input routine 54526f9a767SRodney W. Grimes */ 546170db9c6SJohn Dyson 54726f9a767SRodney W. Grimes int 54824a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage) 54926f9a767SRodney W. Grimes vm_object_t object; 55024a1cce3SDavid Greenman vm_page_t *m; 55124a1cce3SDavid Greenman int count; 55224a1cce3SDavid Greenman int reqpage; 55324a1cce3SDavid Greenman { 554170db9c6SJohn Dyson int rtval; 555170db9c6SJohn Dyson struct vnode *vp; 556170db9c6SJohn Dyson vp = object->handle; 557170db9c6SJohn Dyson rtval = VOP_GETPAGES(vp, m, count, reqpage); 558170db9c6SJohn Dyson if (rtval == EOPNOTSUPP) 559170db9c6SJohn Dyson return vnode_pager_leaf_getpages(object, m, count, reqpage); 560170db9c6SJohn Dyson else 561170db9c6SJohn Dyson return rtval; 562170db9c6SJohn Dyson } 563170db9c6SJohn Dyson 564170db9c6SJohn Dyson static int 565170db9c6SJohn Dyson vnode_pager_leaf_getpages(object, m, count, reqpage) 566170db9c6SJohn Dyson vm_object_t object; 567170db9c6SJohn Dyson vm_page_t *m; 568170db9c6SJohn Dyson int count; 569170db9c6SJohn Dyson int reqpage; 570170db9c6SJohn Dyson { 57124a1cce3SDavid Greenman vm_offset_t kva, foff; 57224a1cce3SDavid Greenman int i, size, bsize, first, firstaddr; 57326f9a767SRodney W. Grimes struct vnode *dp, *vp; 574efc68ce1SDavid Greenman int runpg; 575efc68ce1SDavid Greenman int runend; 5760bdb7528SDavid Greenman struct buf *bp; 57726f9a767SRodney W. Grimes int s; 57826f9a767SRodney W. Grimes int error = 0; 57926f9a767SRodney W. Grimes 58024a1cce3SDavid Greenman vp = object->handle; 58126f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 58226f9a767SRodney W. Grimes 58326f9a767SRodney W. Grimes /* get the UNDERLYING device for the file with VOP_BMAP() */ 584bbc0ec52SDavid Greenman 58526f9a767SRodney W. Grimes /* 586bbc0ec52SDavid Greenman * originally, we did not check for an error return value -- assuming 587bbc0ec52SDavid Greenman * an fs always has a bmap entry point -- that assumption is wrong!!! 58826f9a767SRodney W. Grimes */ 5890d94caffSDavid Greenman foff = m[reqpage]->offset; 590bbc0ec52SDavid Greenman 59126f9a767SRodney W. Grimes /* 59216f62314SDavid Greenman * if we can't bmap, use old VOP code 59326f9a767SRodney W. Grimes */ 594c83ebe77SJohn Dyson if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 59526f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 59626f9a767SRodney W. Grimes if (i != reqpage) { 59726f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 59826f9a767SRodney W. Grimes } 59926f9a767SRodney W. Grimes } 600976e77fcSDavid Greenman cnt.v_vnodein++; 601976e77fcSDavid Greenman cnt.v_vnodepgsin++; 60224a1cce3SDavid Greenman return vnode_pager_input_old(object, m[reqpage]); 603bbc0ec52SDavid Greenman 60426f9a767SRodney W. Grimes /* 60526f9a767SRodney W. Grimes * if the blocksize is smaller than a page size, then use 60626f9a767SRodney W. Grimes * special small filesystem code. NFS sometimes has a small 60726f9a767SRodney W. Grimes * blocksize, but it can handle large reads itself. 60826f9a767SRodney W. Grimes */ 60926f9a767SRodney W. Grimes } else if ((PAGE_SIZE / bsize) > 1 && 61026f9a767SRodney W. Grimes (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) { 61126f9a767SRodney W. Grimes 61226f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 61326f9a767SRodney W. Grimes if (i != reqpage) { 61426f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 61526f9a767SRodney W. Grimes } 61626f9a767SRodney W. Grimes } 617976e77fcSDavid Greenman cnt.v_vnodein++; 618976e77fcSDavid Greenman cnt.v_vnodepgsin++; 61924a1cce3SDavid Greenman return vnode_pager_input_smlfs(object, m[reqpage]); 62026f9a767SRodney W. Grimes } 62126f9a767SRodney W. Grimes /* 6220d94caffSDavid Greenman * if ANY DEV_BSIZE blocks are valid on a large filesystem block 6230d94caffSDavid Greenman * then, the entire page is valid -- 6240d94caffSDavid Greenman */ 6250d94caffSDavid Greenman if (m[reqpage]->valid) { 6260d94caffSDavid Greenman m[reqpage]->valid = VM_PAGE_BITS_ALL; 6270d94caffSDavid Greenman for (i = 0; i < count; i++) { 6280d94caffSDavid Greenman if (i != reqpage) 6290d94caffSDavid Greenman vnode_pager_freepage(m[i]); 6300d94caffSDavid Greenman } 6310d94caffSDavid Greenman return VM_PAGER_OK; 6320d94caffSDavid Greenman } 6330bdb7528SDavid Greenman 6340d94caffSDavid Greenman /* 63526f9a767SRodney W. Grimes * here on direct device I/O 63626f9a767SRodney W. Grimes */ 63726f9a767SRodney W. Grimes 638efc68ce1SDavid Greenman firstaddr = -1; 63926f9a767SRodney W. Grimes /* 640efc68ce1SDavid Greenman * calculate the run that includes the required page 64126f9a767SRodney W. Grimes */ 642efc68ce1SDavid Greenman for(first = 0, i = 0; i < count; i = runend) { 643efc68ce1SDavid Greenman firstaddr = vnode_pager_addr(vp, m[i]->offset, &runpg); 644efc68ce1SDavid Greenman if (firstaddr == -1) { 64524a1cce3SDavid Greenman if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 64624a1cce3SDavid Greenman panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d", 64724a1cce3SDavid Greenman firstaddr, foff, object->un_pager.vnp.vnp_size); 648efc68ce1SDavid Greenman } 64926f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 650efc68ce1SDavid Greenman runend = i + 1; 651efc68ce1SDavid Greenman first = runend; 652efc68ce1SDavid Greenman continue; 653efc68ce1SDavid Greenman } 654efc68ce1SDavid Greenman runend = i + runpg; 655efc68ce1SDavid Greenman if (runend <= reqpage) { 656efc68ce1SDavid Greenman int j; 657efc68ce1SDavid Greenman for (j = i; j < runend; j++) { 658efc68ce1SDavid Greenman vnode_pager_freepage(m[j]); 659efc68ce1SDavid Greenman } 66026f9a767SRodney W. Grimes } else { 661efc68ce1SDavid Greenman if (runpg < (count - first)) { 662efc68ce1SDavid Greenman for (i = first + runpg; i < count; i++) 66326f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 664efc68ce1SDavid Greenman count = first + runpg; 66526f9a767SRodney W. Grimes } 666efc68ce1SDavid Greenman break; 66726f9a767SRodney W. Grimes } 668efc68ce1SDavid Greenman first = runend; 669efc68ce1SDavid Greenman } 67026f9a767SRodney W. Grimes 67126f9a767SRodney W. Grimes /* 672bbc0ec52SDavid Greenman * the first and last page have been calculated now, move input pages 673bbc0ec52SDavid Greenman * to be zero based... 67426f9a767SRodney W. Grimes */ 67526f9a767SRodney W. Grimes if (first != 0) { 67626f9a767SRodney W. Grimes for (i = first; i < count; i++) { 67726f9a767SRodney W. Grimes m[i - first] = m[i]; 67826f9a767SRodney W. Grimes } 67926f9a767SRodney W. Grimes count -= first; 68026f9a767SRodney W. Grimes reqpage -= first; 68126f9a767SRodney W. Grimes } 682efc68ce1SDavid Greenman 68326f9a767SRodney W. Grimes /* 68426f9a767SRodney W. Grimes * calculate the file virtual address for the transfer 68526f9a767SRodney W. Grimes */ 6860d94caffSDavid Greenman foff = m[0]->offset; 68726f9a767SRodney W. Grimes 68826f9a767SRodney W. Grimes /* 68926f9a767SRodney W. Grimes * calculate the size of the transfer 69026f9a767SRodney W. Grimes */ 69126f9a767SRodney W. Grimes size = count * PAGE_SIZE; 69224a1cce3SDavid Greenman if ((foff + size) > object->un_pager.vnp.vnp_size) 69324a1cce3SDavid Greenman size = object->un_pager.vnp.vnp_size - foff; 69426f9a767SRodney W. Grimes 69526f9a767SRodney W. Grimes /* 69626f9a767SRodney W. Grimes * round up physical size for real devices 69726f9a767SRodney W. Grimes */ 69826f9a767SRodney W. Grimes if (dp->v_type == VBLK || dp->v_type == VCHR) 69926f9a767SRodney W. Grimes size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 70026f9a767SRodney W. Grimes 7016d40c3d3SDavid Greenman bp = getpbuf(); 70216f62314SDavid Greenman kva = (vm_offset_t) bp->b_data; 70316f62314SDavid Greenman 70426f9a767SRodney W. Grimes /* 70526f9a767SRodney W. Grimes * and map the pages to be read into the kva 70626f9a767SRodney W. Grimes */ 70716f62314SDavid Greenman pmap_qenter(kva, m, count); 70826f9a767SRodney W. Grimes 70926f9a767SRodney W. Grimes /* build a minimal buffer header */ 71026f9a767SRodney W. Grimes bp->b_flags = B_BUSY | B_READ | B_CALL; 71126f9a767SRodney W. Grimes bp->b_iodone = vnode_pager_iodone; 71226f9a767SRodney W. Grimes /* B_PHYS is not set, but it is nice to fill this in */ 71326f9a767SRodney W. Grimes bp->b_proc = curproc; 71426f9a767SRodney W. Grimes bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 71526f9a767SRodney W. Grimes if (bp->b_rcred != NOCRED) 71626f9a767SRodney W. Grimes crhold(bp->b_rcred); 71726f9a767SRodney W. Grimes if (bp->b_wcred != NOCRED) 71826f9a767SRodney W. Grimes crhold(bp->b_wcred); 719187f0071SDavid Greenman bp->b_blkno = firstaddr; 7200d94caffSDavid Greenman pbgetvp(dp, bp); 72126f9a767SRodney W. Grimes bp->b_bcount = size; 72226f9a767SRodney W. Grimes bp->b_bufsize = size; 72326f9a767SRodney W. Grimes 724976e77fcSDavid Greenman cnt.v_vnodein++; 725976e77fcSDavid Greenman cnt.v_vnodepgsin += count; 726976e77fcSDavid Greenman 72726f9a767SRodney W. Grimes /* do the input */ 72826f9a767SRodney W. Grimes VOP_STRATEGY(bp); 729976e77fcSDavid Greenman 73026f9a767SRodney W. Grimes s = splbio(); 73126f9a767SRodney W. Grimes /* we definitely need to be at splbio here */ 73226f9a767SRodney W. Grimes 73326f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 734aa2cabb9SDavid Greenman tsleep(bp, PVM, "vnread", 0); 73526f9a767SRodney W. Grimes } 73626f9a767SRodney W. Grimes splx(s); 73726f9a767SRodney W. Grimes if ((bp->b_flags & B_ERROR) != 0) 73826f9a767SRodney W. Grimes error = EIO; 73926f9a767SRodney W. Grimes 74026f9a767SRodney W. Grimes if (!error) { 74126f9a767SRodney W. Grimes if (size != count * PAGE_SIZE) 74226f9a767SRodney W. Grimes bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 74326f9a767SRodney W. Grimes } 74416f62314SDavid Greenman pmap_qremove(kva, count); 74526f9a767SRodney W. Grimes 74626f9a767SRodney W. Grimes /* 74726f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 74826f9a767SRodney W. Grimes */ 74926f9a767SRodney W. Grimes relpbuf(bp); 75026f9a767SRodney W. Grimes 75126f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 752fff93ab6SDavid Greenman pmap_clear_modify(VM_PAGE_TO_PHYS(m[i])); 7530d94caffSDavid Greenman m[i]->dirty = 0; 7540d94caffSDavid Greenman m[i]->valid = VM_PAGE_BITS_ALL; 755b1fc01b7SJohn Dyson m[i]->flags &= ~PG_ZERO; 75626f9a767SRodney W. Grimes if (i != reqpage) { 757bbc0ec52SDavid Greenman 75826f9a767SRodney W. Grimes /* 759bbc0ec52SDavid Greenman * whether or not to leave the page activated is up in 760bbc0ec52SDavid Greenman * the air, but we should put the page on a page queue 761bbc0ec52SDavid Greenman * somewhere. (it already is in the object). Result: 762bbc0ec52SDavid Greenman * It appears that emperical results show that 763bbc0ec52SDavid Greenman * deactivating pages is best. 76426f9a767SRodney W. Grimes */ 765bbc0ec52SDavid Greenman 76626f9a767SRodney W. Grimes /* 767bbc0ec52SDavid Greenman * just in case someone was asking for this page we 768bbc0ec52SDavid Greenman * now tell them that it is ok to use 76926f9a767SRodney W. Grimes */ 77026f9a767SRodney W. Grimes if (!error) { 77126f9a767SRodney W. Grimes vm_page_deactivate(m[i]); 77226f9a767SRodney W. Grimes PAGE_WAKEUP(m[i]); 77326f9a767SRodney W. Grimes } else { 77426f9a767SRodney W. Grimes vnode_pager_freepage(m[i]); 77526f9a767SRodney W. Grimes } 77626f9a767SRodney W. Grimes } 77726f9a767SRodney W. Grimes } 77826f9a767SRodney W. Grimes if (error) { 77924a1cce3SDavid Greenman printf("vnode_pager_getpages: I/O read error\n"); 78026f9a767SRodney W. Grimes } 781a83c285cSDavid Greenman return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 78226f9a767SRodney W. Grimes } 78326f9a767SRodney W. Grimes 784170db9c6SJohn Dyson int 785170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals) 786170db9c6SJohn Dyson vm_object_t object; 787170db9c6SJohn Dyson vm_page_t *m; 788170db9c6SJohn Dyson int count; 789170db9c6SJohn Dyson boolean_t sync; 790170db9c6SJohn Dyson int *rtvals; 791170db9c6SJohn Dyson { 792170db9c6SJohn Dyson int rtval; 793170db9c6SJohn Dyson struct vnode *vp; 794170db9c6SJohn Dyson vp = object->handle; 795170db9c6SJohn Dyson rtval = VOP_PUTPAGES(vp, m, count, sync, rtvals); 796170db9c6SJohn Dyson if (rtval == EOPNOTSUPP) 797170db9c6SJohn Dyson return vnode_pager_leaf_putpages(object, m, count, sync, rtvals); 798170db9c6SJohn Dyson else 799170db9c6SJohn Dyson return rtval; 800170db9c6SJohn Dyson } 801170db9c6SJohn Dyson 80226f9a767SRodney W. Grimes /* 80326f9a767SRodney W. Grimes * generic vnode pager output routine 80426f9a767SRodney W. Grimes */ 805170db9c6SJohn Dyson static int 806170db9c6SJohn Dyson vnode_pager_leaf_putpages(object, m, count, sync, rtvals) 80724a1cce3SDavid Greenman vm_object_t object; 80826f9a767SRodney W. Grimes vm_page_t *m; 80926f9a767SRodney W. Grimes int count; 81024a1cce3SDavid Greenman boolean_t sync; 81126f9a767SRodney W. Grimes int *rtvals; 81226f9a767SRodney W. Grimes { 813f6b04d2bSDavid Greenman int i; 81426f9a767SRodney W. Grimes 815f6b04d2bSDavid Greenman struct vnode *vp; 816f6b04d2bSDavid Greenman int maxsize, ncount; 817f6b04d2bSDavid Greenman struct uio auio; 818f6b04d2bSDavid Greenman struct iovec aiov; 819f6b04d2bSDavid Greenman int error; 82026f9a767SRodney W. Grimes 82124a1cce3SDavid Greenman vp = object->handle;; 82226f9a767SRodney W. Grimes for (i = 0; i < count; i++) 82326f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_AGAIN; 82426f9a767SRodney W. Grimes 8250d94caffSDavid Greenman if ((int) m[0]->offset < 0) { 82624a1cce3SDavid Greenman printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->offset, m[0]->dirty); 827f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 828f6b04d2bSDavid Greenman return VM_PAGER_BAD; 8290d94caffSDavid Greenman } 8300bdb7528SDavid Greenman 831f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 832f6b04d2bSDavid Greenman ncount = count; 83326f9a767SRodney W. Grimes 83424a1cce3SDavid Greenman if (maxsize + m[0]->offset > object->un_pager.vnp.vnp_size) { 83524a1cce3SDavid Greenman if (object->un_pager.vnp.vnp_size > m[0]->offset) 83624a1cce3SDavid Greenman maxsize = object->un_pager.vnp.vnp_size - m[0]->offset; 8375f55e841SDavid Greenman else 8385f55e841SDavid Greenman maxsize = 0; 839f6b04d2bSDavid Greenman ncount = (maxsize + PAGE_SIZE - 1) / PAGE_SIZE; 840f6b04d2bSDavid Greenman if (ncount < count) { 841f6b04d2bSDavid Greenman for (i = ncount; i < count; i++) { 842f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 843f6b04d2bSDavid Greenman } 844f6b04d2bSDavid Greenman if (ncount == 0) { 84524a1cce3SDavid Greenman printf("vnode_pager_putpages: write past end of file: %d, %d\n", 84624a1cce3SDavid Greenman m[0]->offset, object->un_pager.vnp.vnp_size); 84726f9a767SRodney W. Grimes return rtvals[0]; 84826f9a767SRodney W. Grimes } 849f6b04d2bSDavid Greenman } 850f6b04d2bSDavid Greenman } 85126f9a767SRodney W. Grimes 85226f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 8535f55e841SDavid Greenman m[i]->busy++; 854f6b04d2bSDavid Greenman m[i]->flags &= ~PG_BUSY; 85526f9a767SRodney W. Grimes } 856f6b04d2bSDavid Greenman 857f6b04d2bSDavid Greenman aiov.iov_base = (caddr_t) 0; 858f6b04d2bSDavid Greenman aiov.iov_len = maxsize; 859f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 860f6b04d2bSDavid Greenman auio.uio_iovcnt = 1; 861f6b04d2bSDavid Greenman auio.uio_offset = m[0]->offset; 862f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 863f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 864f6b04d2bSDavid Greenman auio.uio_resid = maxsize; 865f6b04d2bSDavid Greenman auio.uio_procp = (struct proc *) 0; 866f6b04d2bSDavid Greenman error = VOP_WRITE(vp, &auio, IO_VMIO, curproc->p_ucred); 867976e77fcSDavid Greenman cnt.v_vnodeout++; 868f6b04d2bSDavid Greenman cnt.v_vnodepgsout += ncount; 869f6b04d2bSDavid Greenman 870f6b04d2bSDavid Greenman if (error) { 87124a1cce3SDavid Greenman printf("vnode_pager_putpages: I/O error %d\n", error); 872f6b04d2bSDavid Greenman } 873f6b04d2bSDavid Greenman if (auio.uio_resid) { 87424a1cce3SDavid Greenman printf("vnode_pager_putpages: residual I/O %d at %d\n", auio.uio_resid, m[0]->offset); 87526f9a767SRodney W. Grimes } 87626f9a767SRodney W. Grimes for (i = 0; i < count; i++) { 8775f55e841SDavid Greenman m[i]->busy--; 878f6b04d2bSDavid Greenman if (i < ncount) { 87926f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 88026f9a767SRodney W. Grimes } 881f6b04d2bSDavid Greenman if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED)) 88224a1cce3SDavid Greenman wakeup(m[i]); 88326f9a767SRodney W. Grimes } 884f6b04d2bSDavid Greenman return rtvals[0]; 88526f9a767SRodney W. Grimes } 886f6b04d2bSDavid Greenman 887f6b04d2bSDavid Greenman struct vnode * 88824a1cce3SDavid Greenman vnode_pager_lock(object) 88924a1cce3SDavid Greenman vm_object_t object; 89024a1cce3SDavid Greenman { 89124a1cce3SDavid Greenman for (; object != NULL; object = object->backing_object) { 89224a1cce3SDavid Greenman if (object->type != OBJT_VNODE) 893f6b04d2bSDavid Greenman continue; 894f6b04d2bSDavid Greenman 89524a1cce3SDavid Greenman VOP_LOCK(object->handle); 89624a1cce3SDavid Greenman return object->handle; 89726f9a767SRodney W. Grimes } 89824a1cce3SDavid Greenman return NULL; 899f6b04d2bSDavid Greenman } 900