160727d8bSWarner Losh /*- 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 41df8bae1dSRodney W. Grimes */ 42df8bae1dSRodney W. Grimes 43df8bae1dSRodney W. Grimes /* 44df8bae1dSRodney W. Grimes * Page to/from files (vnodes). 45df8bae1dSRodney W. Grimes */ 46df8bae1dSRodney W. Grimes 4726f9a767SRodney W. Grimes /* 4826f9a767SRodney W. Grimes * TODO: 4924a1cce3SDavid Greenman * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 50f6b04d2bSDavid Greenman * greatly re-simplify the vnode_pager. 5126f9a767SRodney W. Grimes */ 5226f9a767SRodney W. Grimes 53874651b1SDavid E. O'Brien #include <sys/cdefs.h> 54874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 55874651b1SDavid E. O'Brien 56df8bae1dSRodney W. Grimes #include <sys/param.h> 57df8bae1dSRodney W. Grimes #include <sys/systm.h> 58df8bae1dSRodney W. Grimes #include <sys/proc.h> 59df8bae1dSRodney W. Grimes #include <sys/vnode.h> 60df8bae1dSRodney W. Grimes #include <sys/mount.h> 619626b608SPoul-Henning Kamp #include <sys/bio.h> 6224a1cce3SDavid Greenman #include <sys/buf.h> 63efeaf95aSDavid Greenman #include <sys/vmmeter.h> 64d07a6d3fSPoul-Henning Kamp #include <sys/limits.h> 6524579ca1SMatthew Dillon #include <sys/conf.h> 669e0ddbd0SAlan Cox #include <sys/sf_buf.h> 67df8bae1dSRodney W. Grimes 684f12e0acSSuleiman Souhlal #include <machine/atomic.h> 694f12e0acSSuleiman Souhlal 70df8bae1dSRodney W. Grimes #include <vm/vm.h> 71efeaf95aSDavid Greenman #include <vm/vm_object.h> 72df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 7324a1cce3SDavid Greenman #include <vm/vm_pager.h> 741efb74fbSJohn Dyson #include <vm/vm_map.h> 75df8bae1dSRodney W. Grimes #include <vm/vnode_pager.h> 76efeaf95aSDavid Greenman #include <vm/vm_extern.h> 77df8bae1dSRodney W. Grimes 7811caded3SAlfred Perlstein static void vnode_pager_init(void); 79f3aad9a6SBjoern A. Zeeb static daddr_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 8011caded3SAlfred Perlstein int *run); 8111caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 8211caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 8311caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t); 8411caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int); 8511caded3SAlfred Perlstein static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 8611caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 87d07a6d3fSPoul-Henning Kamp static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t); 880b8253a7SBruce Evans 89df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 904e658600SPoul-Henning Kamp .pgo_init = vnode_pager_init, 914e658600SPoul-Henning Kamp .pgo_alloc = vnode_pager_alloc, 924e658600SPoul-Henning Kamp .pgo_dealloc = vnode_pager_dealloc, 934e658600SPoul-Henning Kamp .pgo_getpages = vnode_pager_getpages, 944e658600SPoul-Henning Kamp .pgo_putpages = vnode_pager_putpages, 954e658600SPoul-Henning Kamp .pgo_haspage = vnode_pager_haspage, 96df8bae1dSRodney W. Grimes }; 97df8bae1dSRodney W. Grimes 98b62b9b64SJohn Baldwin int vnode_pbuf_freecnt; 991c7c3c6aSMatthew Dillon 10037c84183SPoul-Henning Kamp static void 101b62b9b64SJohn Baldwin vnode_pager_init(void) 102b62b9b64SJohn Baldwin { 103b62b9b64SJohn Baldwin 104b62b9b64SJohn Baldwin vnode_pbuf_freecnt = nswbuf / 2 + 1; 105b62b9b64SJohn Baldwin } 106170db9c6SJohn Dyson 107d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */ 108d07a6d3fSPoul-Henning Kamp int 109d07a6d3fSPoul-Henning Kamp vnode_create_vobject(struct vnode *vp, size_t isize, struct thread *td) 110d07a6d3fSPoul-Henning Kamp { 111d07a6d3fSPoul-Henning Kamp vm_object_t object; 112d07a6d3fSPoul-Henning Kamp vm_ooffset_t size = isize; 113d07a6d3fSPoul-Henning Kamp struct vattr va; 114d07a6d3fSPoul-Henning Kamp 115d07a6d3fSPoul-Henning Kamp if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE) 116d07a6d3fSPoul-Henning Kamp return (0); 117d07a6d3fSPoul-Henning Kamp 118d07a6d3fSPoul-Henning Kamp while ((object = vp->v_object) != NULL) { 119d07a6d3fSPoul-Henning Kamp VM_OBJECT_LOCK(object); 120d07a6d3fSPoul-Henning Kamp if (!(object->flags & OBJ_DEAD)) { 121d07a6d3fSPoul-Henning Kamp VM_OBJECT_UNLOCK(object); 122d07a6d3fSPoul-Henning Kamp return (0); 123d07a6d3fSPoul-Henning Kamp } 124d07a6d3fSPoul-Henning Kamp VOP_UNLOCK(vp, 0, td); 125d07a6d3fSPoul-Henning Kamp vm_object_set_flag(object, OBJ_DISCONNECTWNT); 126d07a6d3fSPoul-Henning Kamp msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0); 127d07a6d3fSPoul-Henning Kamp vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 128d07a6d3fSPoul-Henning Kamp } 129d07a6d3fSPoul-Henning Kamp 130d07a6d3fSPoul-Henning Kamp if (size == 0) { 131d07a6d3fSPoul-Henning Kamp if (vn_isdisk(vp, NULL)) { 132d07a6d3fSPoul-Henning Kamp size = IDX_TO_OFF(INT_MAX); 133d07a6d3fSPoul-Henning Kamp } else { 134d07a6d3fSPoul-Henning Kamp if (VOP_GETATTR(vp, &va, td->td_ucred, td) != 0) 135d07a6d3fSPoul-Henning Kamp return (0); 136d07a6d3fSPoul-Henning Kamp size = va.va_size; 137d07a6d3fSPoul-Henning Kamp } 138d07a6d3fSPoul-Henning Kamp } 139d07a6d3fSPoul-Henning Kamp 140d07a6d3fSPoul-Henning Kamp object = vnode_pager_alloc(vp, size, 0, 0); 141d07a6d3fSPoul-Henning Kamp /* 142d07a6d3fSPoul-Henning Kamp * Dereference the reference we just created. This assumes 143d07a6d3fSPoul-Henning Kamp * that the object is associated with the vp. 144d07a6d3fSPoul-Henning Kamp */ 145d07a6d3fSPoul-Henning Kamp VM_OBJECT_LOCK(object); 146d07a6d3fSPoul-Henning Kamp object->ref_count--; 147d07a6d3fSPoul-Henning Kamp VM_OBJECT_UNLOCK(object); 148d07a6d3fSPoul-Henning Kamp vrele(vp); 149d07a6d3fSPoul-Henning Kamp 150d07a6d3fSPoul-Henning Kamp KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object")); 151d07a6d3fSPoul-Henning Kamp 152d07a6d3fSPoul-Henning Kamp return (0); 153d07a6d3fSPoul-Henning Kamp } 154d07a6d3fSPoul-Henning Kamp 1557146d6cbSPoul-Henning Kamp void 1567146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp) 1577146d6cbSPoul-Henning Kamp { 1587146d6cbSPoul-Henning Kamp struct vm_object *obj; 1597146d6cbSPoul-Henning Kamp 1607146d6cbSPoul-Henning Kamp obj = vp->v_object; 1617146d6cbSPoul-Henning Kamp if (obj == NULL) 1627146d6cbSPoul-Henning Kamp return; 163493d78b3SJeff Roberson ASSERT_VOP_LOCKED(vp, "vnode_destroy_vobject"); 1647146d6cbSPoul-Henning Kamp VM_OBJECT_LOCK(obj); 1657146d6cbSPoul-Henning Kamp if (obj->ref_count == 0) { 1667146d6cbSPoul-Henning Kamp /* 1677146d6cbSPoul-Henning Kamp * vclean() may be called twice. The first time 1687146d6cbSPoul-Henning Kamp * removes the primary reference to the object, 1697146d6cbSPoul-Henning Kamp * the second time goes one further and is a 1707146d6cbSPoul-Henning Kamp * special-case to terminate the object. 1717146d6cbSPoul-Henning Kamp * 1727146d6cbSPoul-Henning Kamp * don't double-terminate the object 1737146d6cbSPoul-Henning Kamp */ 1747146d6cbSPoul-Henning Kamp if ((obj->flags & OBJ_DEAD) == 0) 1757146d6cbSPoul-Henning Kamp vm_object_terminate(obj); 1767146d6cbSPoul-Henning Kamp else 1777146d6cbSPoul-Henning Kamp VM_OBJECT_UNLOCK(obj); 1787146d6cbSPoul-Henning Kamp } else { 1797146d6cbSPoul-Henning Kamp /* 1807146d6cbSPoul-Henning Kamp * Woe to the process that tries to page now :-). 1817146d6cbSPoul-Henning Kamp */ 1827146d6cbSPoul-Henning Kamp vm_pager_deallocate(obj); 1837146d6cbSPoul-Henning Kamp VM_OBJECT_UNLOCK(obj); 1847146d6cbSPoul-Henning Kamp } 1856e4b2820SJeff Roberson vp->v_object = NULL; 1867146d6cbSPoul-Henning Kamp } 1877146d6cbSPoul-Henning Kamp 1887146d6cbSPoul-Henning Kamp 189df8bae1dSRodney W. Grimes /* 190df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 191df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 192990ab7adSAlan Cox * 193990ab7adSAlan Cox * MPSAFE 194df8bae1dSRodney W. Grimes */ 19524a1cce3SDavid Greenman vm_object_t 1966cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 197b9dcd593SBruce Evans vm_ooffset_t offset) 198df8bae1dSRodney W. Grimes { 19906cb7259SDavid Greenman vm_object_t object; 200df8bae1dSRodney W. Grimes struct vnode *vp; 201df8bae1dSRodney W. Grimes 202df8bae1dSRodney W. Grimes /* 203df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 204df8bae1dSRodney W. Grimes */ 205df8bae1dSRodney W. Grimes if (handle == NULL) 206df8bae1dSRodney W. Grimes return (NULL); 207df8bae1dSRodney W. Grimes 208df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 20939d38f93SDavid Greenman 21063e7e60dSJeff Roberson ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc"); 21163e7e60dSJeff Roberson 21239d38f93SDavid Greenman /* 21339d38f93SDavid Greenman * If the object is being terminated, wait for it to 21439d38f93SDavid Greenman * go away. 21539d38f93SDavid Greenman */ 2161ca58953SAlan Cox while ((object = vp->v_object) != NULL) { 2171ca58953SAlan Cox VM_OBJECT_LOCK(object); 2181ca58953SAlan Cox if ((object->flags & OBJ_DEAD) == 0) 2191ca58953SAlan Cox break; 22019187819SAlan Cox vm_object_set_flag(object, OBJ_DISCONNECTWNT); 2211ca58953SAlan Cox msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0); 22224a1cce3SDavid Greenman } 2230d94caffSDavid Greenman 2242be70f79SJohn Dyson if (vp->v_usecount == 0) 2252be70f79SJohn Dyson panic("vnode_pager_alloc: no vnode reference"); 2262be70f79SJohn Dyson 22724a1cce3SDavid Greenman if (object == NULL) { 228df8bae1dSRodney W. Grimes /* 229df8bae1dSRodney W. Grimes * And an object of the appropriate size 230df8bae1dSRodney W. Grimes */ 2316cde7a16SDavid Greenman object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 232bbc0ec52SDavid Greenman 2336cde7a16SDavid Greenman object->un_pager.vnp.vnp_size = size; 23426f9a767SRodney W. Grimes 23524a1cce3SDavid Greenman object->handle = handle; 236ed4fe4f4SJeff Roberson if (VFS_NEEDSGIANT(vp->v_mount)) 237ed4fe4f4SJeff Roberson vm_object_set_flag(object, OBJ_NEEDGIANT); 23824a1cce3SDavid Greenman vp->v_object = object; 239df8bae1dSRodney W. Grimes } else { 24095e5e988SJohn Dyson object->ref_count++; 2411ca58953SAlan Cox VM_OBJECT_UNLOCK(object); 242df8bae1dSRodney W. Grimes } 2437747c038SJeff Roberson vref(vp); 24424a1cce3SDavid Greenman return (object); 245df8bae1dSRodney W. Grimes } 246df8bae1dSRodney W. Grimes 247658ad5ffSAlan Cox /* 248658ad5ffSAlan Cox * The object must be locked. 249658ad5ffSAlan Cox */ 250f708ef1bSPoul-Henning Kamp static void 25124a1cce3SDavid Greenman vnode_pager_dealloc(object) 2520d94caffSDavid Greenman vm_object_t object; 25324a1cce3SDavid Greenman { 25454d92145SMatthew Dillon struct vnode *vp = object->handle; 255df8bae1dSRodney W. Grimes 25624a1cce3SDavid Greenman if (vp == NULL) 25724a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 25824a1cce3SDavid Greenman 259658ad5ffSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 26066095752SJohn Dyson vm_object_pip_wait(object, "vnpdea"); 26124a1cce3SDavid Greenman 26224a1cce3SDavid Greenman object->handle = NULL; 26395461b45SJohn Dyson object->type = OBJT_DEAD; 26419187819SAlan Cox if (object->flags & OBJ_DISCONNECTWNT) { 26519187819SAlan Cox vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 26619187819SAlan Cox wakeup(object); 26719187819SAlan Cox } 268e6e370a7SJeff Roberson ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc"); 269aa2cabb9SDavid Greenman vp->v_object = NULL; 27035764be3SPoul-Henning Kamp vp->v_vflag &= ~VV_TEXT; 271df8bae1dSRodney W. Grimes } 27226f9a767SRodney W. Grimes 273f708ef1bSPoul-Henning Kamp static boolean_t 274a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after) 27524a1cce3SDavid Greenman vm_object_t object; 276a316d390SJohn Dyson vm_pindex_t pindex; 27724a1cce3SDavid Greenman int *before; 27824a1cce3SDavid Greenman int *after; 279df8bae1dSRodney W. Grimes { 28024a1cce3SDavid Greenman struct vnode *vp = object->handle; 28198b0c789SPoul-Henning Kamp daddr_t bn; 2823af76890SPoul-Henning Kamp int err; 283170db9c6SJohn Dyson daddr_t reqblock; 2842c4488fcSJohn Dyson int poff; 2852c4488fcSJohn Dyson int bsize; 286d63596ceSJohn Dyson int pagesperblock, blocksperpage; 287ae51ff11SJeff Roberson int vfslocked; 288df8bae1dSRodney W. Grimes 289f29ba63eSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 29024579ca1SMatthew Dillon /* 29124579ca1SMatthew Dillon * If no vp or vp is doomed or marked transparent to VM, we do not 29224579ca1SMatthew Dillon * have the page. 29324579ca1SMatthew Dillon */ 294e6e370a7SJeff Roberson if (vp == NULL) 29547221757SJohn Dyson return FALSE; 29647221757SJohn Dyson 29763e7e60dSJeff Roberson VI_LOCK(vp); 29863e7e60dSJeff Roberson if (vp->v_iflag & VI_DOOMED) { 29963e7e60dSJeff Roberson VI_UNLOCK(vp); 300e6e370a7SJeff Roberson return FALSE; 30163e7e60dSJeff Roberson } 30263e7e60dSJeff Roberson VI_UNLOCK(vp); 303df8bae1dSRodney W. Grimes /* 3040d94caffSDavid Greenman * If filesystem no longer mounted or offset beyond end of file we do 3050d94caffSDavid Greenman * not have the page. 306df8bae1dSRodney W. Grimes */ 307a316d390SJohn Dyson if ((vp->v_mount == NULL) || 308a316d390SJohn Dyson (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 3094abc71c0SDavid Greenman return FALSE; 310df8bae1dSRodney W. Grimes 311eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 312170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 313d63596ceSJohn Dyson blocksperpage = 0; 314d63596ceSJohn Dyson if (pagesperblock > 0) { 315a316d390SJohn Dyson reqblock = pindex / pagesperblock; 316d63596ceSJohn Dyson } else { 317d63596ceSJohn Dyson blocksperpage = (PAGE_SIZE / bsize); 318d63596ceSJohn Dyson reqblock = pindex * blocksperpage; 319d63596ceSJohn Dyson } 320f29ba63eSAlan Cox VM_OBJECT_UNLOCK(object); 321ae51ff11SJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 322ec794849SPoul-Henning Kamp err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 323ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 324f29ba63eSAlan Cox VM_OBJECT_LOCK(object); 3250d94caffSDavid Greenman if (err) 32624a1cce3SDavid Greenman return TRUE; 3276eab77f2SJohn Dyson if (bn == -1) 328ced399eeSJohn Dyson return FALSE; 329d63596ceSJohn Dyson if (pagesperblock > 0) { 330a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 331170db9c6SJohn Dyson if (before) { 332170db9c6SJohn Dyson *before *= pagesperblock; 333170db9c6SJohn Dyson *before += poff; 334170db9c6SJohn Dyson } 335170db9c6SJohn Dyson if (after) { 336b1fc01b7SJohn Dyson int numafter; 337170db9c6SJohn Dyson *after *= pagesperblock; 338b1fc01b7SJohn Dyson numafter = pagesperblock - (poff + 1); 33947e151ddSRobert Drehmel if (IDX_TO_OFF(pindex + numafter) > 34047e151ddSRobert Drehmel object->un_pager.vnp.vnp_size) { 34147e151ddSRobert Drehmel numafter = 34247e151ddSRobert Drehmel OFF_TO_IDX(object->un_pager.vnp.vnp_size) - 34347e151ddSRobert Drehmel pindex; 344b1fc01b7SJohn Dyson } 345b1fc01b7SJohn Dyson *after += numafter; 346170db9c6SJohn Dyson } 347d63596ceSJohn Dyson } else { 348d63596ceSJohn Dyson if (before) { 349d63596ceSJohn Dyson *before /= blocksperpage; 350d63596ceSJohn Dyson } 351d63596ceSJohn Dyson 352d63596ceSJohn Dyson if (after) { 353d63596ceSJohn Dyson *after /= blocksperpage; 354d63596ceSJohn Dyson } 355d63596ceSJohn Dyson } 356ced399eeSJohn Dyson return TRUE; 357df8bae1dSRodney W. Grimes } 358df8bae1dSRodney W. Grimes 359df8bae1dSRodney W. Grimes /* 360df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 36124a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 362df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 363df8bae1dSRodney W. Grimes * 364df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 365df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 366df8bae1dSRodney W. Grimes */ 367df8bae1dSRodney W. Grimes void 368df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize) 369df8bae1dSRodney W. Grimes struct vnode *vp; 370a316d390SJohn Dyson vm_ooffset_t nsize; 371df8bae1dSRodney W. Grimes { 3722a8f9ab5SAlan Cox vm_object_t object; 3732a8f9ab5SAlan Cox vm_page_t m; 374c576d121SLuoqi Chen vm_pindex_t nobjsize; 375df8bae1dSRodney W. Grimes 3762a8f9ab5SAlan Cox if ((object = vp->v_object) == NULL) 377df8bae1dSRodney W. Grimes return; 3782a8f9ab5SAlan Cox VM_OBJECT_LOCK(object); 3792a8f9ab5SAlan Cox if (nsize == object->un_pager.vnp.vnp_size) { 380df8bae1dSRodney W. Grimes /* 381df8bae1dSRodney W. Grimes * Hasn't changed size 382df8bae1dSRodney W. Grimes */ 3832a8f9ab5SAlan Cox VM_OBJECT_UNLOCK(object); 384df8bae1dSRodney W. Grimes return; 3852a8f9ab5SAlan Cox } 386c576d121SLuoqi Chen nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 3872a8f9ab5SAlan Cox if (nsize < object->un_pager.vnp.vnp_size) { 388df8bae1dSRodney W. Grimes /* 389bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 390df8bae1dSRodney W. Grimes */ 3912a8f9ab5SAlan Cox if (nobjsize < object->size) 392c576d121SLuoqi Chen vm_object_page_remove(object, nobjsize, object->size, 393c576d121SLuoqi Chen FALSE); 394bbc0ec52SDavid Greenman /* 395bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 3963ebeaf59SMatthew Dillon * only partially backed by the vnode. 3973ebeaf59SMatthew Dillon * 3983ebeaf59SMatthew Dillon * XXX for some reason (I don't know yet), if we take a 3993ebeaf59SMatthew Dillon * completely invalid page and mark it partially valid 4003ebeaf59SMatthew Dillon * it can screw up NFS reads, so we don't allow the case. 401bbc0ec52SDavid Greenman */ 4022a8f9ab5SAlan Cox if ((nsize & PAGE_MASK) && 4031b26eb10SAlan Cox (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 4041b26eb10SAlan Cox m->valid != 0) { 4052b6b0df7SMatthew Dillon int base = (int)nsize & PAGE_MASK; 4062b6b0df7SMatthew Dillon int size = PAGE_SIZE - base; 4072b6b0df7SMatthew Dillon 4082b6b0df7SMatthew Dillon /* 4092b6b0df7SMatthew Dillon * Clear out partial-page garbage in case 4102b6b0df7SMatthew Dillon * the page has been mapped. 4112b6b0df7SMatthew Dillon */ 412fff6062aSAlan Cox pmap_zero_page_area(m, base, size); 4132b6b0df7SMatthew Dillon 4142b6b0df7SMatthew Dillon /* 4153ebeaf59SMatthew Dillon * XXX work around SMP data integrity race 4163ebeaf59SMatthew Dillon * by unmapping the page from user processes. 4173ebeaf59SMatthew Dillon * The garbage we just cleared may be mapped 4183ebeaf59SMatthew Dillon * to a user process running on another cpu 4193ebeaf59SMatthew Dillon * and this code is not running through normal 4203ebeaf59SMatthew Dillon * I/O channels which handle SMP issues for 4213ebeaf59SMatthew Dillon * us, so unmap page to synchronize all cpus. 4223ebeaf59SMatthew Dillon * 4233ebeaf59SMatthew Dillon * XXX should vm_pager_unmap_page() have 4243ebeaf59SMatthew Dillon * dealt with this? 4253ebeaf59SMatthew Dillon */ 4261b26eb10SAlan Cox vm_page_lock_queues(); 4274fec79beSAlan Cox pmap_remove_all(m); 4283ebeaf59SMatthew Dillon 4293ebeaf59SMatthew Dillon /* 4302b6b0df7SMatthew Dillon * Clear out partial-page dirty bits. This 4312b6b0df7SMatthew Dillon * has the side effect of setting the valid 4322b6b0df7SMatthew Dillon * bits, but that is ok. There are a bunch 4332b6b0df7SMatthew Dillon * of places in the VM system where we expected 4342b6b0df7SMatthew Dillon * m->dirty == VM_PAGE_BITS_ALL. The file EOF 4352b6b0df7SMatthew Dillon * case is one of them. If the page is still 4362b6b0df7SMatthew Dillon * partially dirty, make it fully dirty. 4373ebeaf59SMatthew Dillon * 4383ebeaf59SMatthew Dillon * note that we do not clear out the valid 4393ebeaf59SMatthew Dillon * bits. This would prevent bogus_page 4403ebeaf59SMatthew Dillon * replacement from working properly. 4412b6b0df7SMatthew Dillon */ 4422b6b0df7SMatthew Dillon vm_page_set_validclean(m, base, size); 4432b6b0df7SMatthew Dillon if (m->dirty != 0) 4442b6b0df7SMatthew Dillon m->dirty = VM_PAGE_BITS_ALL; 4452a8f9ab5SAlan Cox vm_page_unlock_queues(); 446bbc0ec52SDavid Greenman } 447bbc0ec52SDavid Greenman } 448a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 449c576d121SLuoqi Chen object->size = nobjsize; 4502a8f9ab5SAlan Cox VM_OBJECT_UNLOCK(object); 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes 45326f9a767SRodney W. Grimes /* 45426f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 45526f9a767SRodney W. Grimes * file address 45626f9a767SRodney W. Grimes */ 457f3aad9a6SBjoern A. Zeeb static daddr_t 458efc68ce1SDavid Greenman vnode_pager_addr(vp, address, run) 45926f9a767SRodney W. Grimes struct vnode *vp; 460a316d390SJohn Dyson vm_ooffset_t address; 461efc68ce1SDavid Greenman int *run; 46226f9a767SRodney W. Grimes { 463f3aad9a6SBjoern A. Zeeb daddr_t rtaddress; 46426f9a767SRodney W. Grimes int bsize; 46598b0c789SPoul-Henning Kamp daddr_t block; 46626f9a767SRodney W. Grimes int err; 467a316d390SJohn Dyson daddr_t vblock; 468f3aad9a6SBjoern A. Zeeb daddr_t voffset; 46926f9a767SRodney W. Grimes 4702ad036b6SAlan Cox if (address < 0) 4710d94caffSDavid Greenman return -1; 4720d94caffSDavid Greenman 4732c4488fcSJohn Dyson if (vp->v_mount == NULL) 4742c4488fcSJohn Dyson return -1; 4752c4488fcSJohn Dyson 47626f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 47726f9a767SRodney W. Grimes vblock = address / bsize; 47826f9a767SRodney W. Grimes voffset = address % bsize; 47926f9a767SRodney W. Grimes 480ec794849SPoul-Henning Kamp err = VOP_BMAP(vp, vblock, NULL, &block, run, NULL); 48126f9a767SRodney W. Grimes 482efc68ce1SDavid Greenman if (err || (block == -1)) 48326f9a767SRodney W. Grimes rtaddress = -1; 484efc68ce1SDavid Greenman else { 485187f0071SDavid Greenman rtaddress = block + voffset / DEV_BSIZE; 486efc68ce1SDavid Greenman if (run) { 487efc68ce1SDavid Greenman *run += 1; 488efc68ce1SDavid Greenman *run *= bsize/PAGE_SIZE; 489efc68ce1SDavid Greenman *run -= voffset/PAGE_SIZE; 490efc68ce1SDavid Greenman } 491efc68ce1SDavid Greenman } 49226f9a767SRodney W. Grimes 49326f9a767SRodney W. Grimes return rtaddress; 49426f9a767SRodney W. Grimes } 49526f9a767SRodney W. Grimes 49626f9a767SRodney W. Grimes /* 49726f9a767SRodney W. Grimes * small block filesystem vnode pager input 49826f9a767SRodney W. Grimes */ 499f708ef1bSPoul-Henning Kamp static int 50024a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m) 50124a1cce3SDavid Greenman vm_object_t object; 50226f9a767SRodney W. Grimes vm_page_t m; 50326f9a767SRodney W. Grimes { 50426f9a767SRodney W. Grimes int i; 5059c83534dSPoul-Henning Kamp struct vnode *vp; 5069c83534dSPoul-Henning Kamp struct bufobj *bo; 50726f9a767SRodney W. Grimes struct buf *bp; 5089e0ddbd0SAlan Cox struct sf_buf *sf; 509f3aad9a6SBjoern A. Zeeb daddr_t fileaddr; 51026f9a767SRodney W. Grimes vm_offset_t bsize; 51126f9a767SRodney W. Grimes int error = 0; 51226f9a767SRodney W. Grimes 51324a1cce3SDavid Greenman vp = object->handle; 5142c4488fcSJohn Dyson if (vp->v_mount == NULL) 5152c4488fcSJohn Dyson return VM_PAGER_BAD; 5162c4488fcSJohn Dyson 51726f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 5180bdb7528SDavid Greenman 5199c83534dSPoul-Henning Kamp VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 52026f9a767SRodney W. Grimes 5219e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 52226f9a767SRodney W. Grimes 52326f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 52433c67741SMatthew Dillon vm_ooffset_t address; 525bbc0ec52SDavid Greenman 526897a45efSDmitrij Tejblum if (vm_page_bits(i * bsize, bsize) & m->valid) 52726f9a767SRodney W. Grimes continue; 52826f9a767SRodney W. Grimes 52933c67741SMatthew Dillon address = IDX_TO_OFF(m->pindex) + i * bsize; 53033c67741SMatthew Dillon if (address >= object->un_pager.vnp.vnp_size) { 53133c67741SMatthew Dillon fileaddr = -1; 53233c67741SMatthew Dillon } else { 53333c67741SMatthew Dillon fileaddr = vnode_pager_addr(vp, address, NULL); 53433c67741SMatthew Dillon } 53526f9a767SRodney W. Grimes if (fileaddr != -1) { 5361c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 53726f9a767SRodney W. Grimes 53826f9a767SRodney W. Grimes /* build a minimal buffer header */ 53921144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 5406a4b5823SPoul-Henning Kamp bp->b_iodone = bdone; 541bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 542bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 543a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 544a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 5459e0ddbd0SAlan Cox bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 546187f0071SDavid Greenman bp->b_blkno = fileaddr; 5479c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 54826f9a767SRodney W. Grimes bp->b_bcount = bsize; 54926f9a767SRodney W. Grimes bp->b_bufsize = bsize; 5502b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 5514f12e0acSSuleiman Souhlal atomic_add_int(&runningbufspace, bp->b_runningbufspace); 55226f9a767SRodney W. Grimes 55326f9a767SRodney W. Grimes /* do the input */ 5542c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 555b792bebeSPoul-Henning Kamp bstrategy(bp); 55626f9a767SRodney W. Grimes 5576a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnsrd"); 5586a4b5823SPoul-Henning Kamp 559c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 56026f9a767SRodney W. Grimes error = EIO; 56126f9a767SRodney W. Grimes 56226f9a767SRodney W. Grimes /* 56326f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 56426f9a767SRodney W. Grimes */ 5659c83534dSPoul-Henning Kamp pbrelbo(bp); 5661c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 56726f9a767SRodney W. Grimes if (error) 56826f9a767SRodney W. Grimes break; 5690d94caffSDavid Greenman 5702bf43e43SAlan Cox VM_OBJECT_LOCK(object); 571178949e0SAlan Cox vm_page_lock_queues(); 572aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 573178949e0SAlan Cox vm_page_unlock_queues(); 5742bf43e43SAlan Cox VM_OBJECT_UNLOCK(object); 57526f9a767SRodney W. Grimes } else { 5762bf43e43SAlan Cox VM_OBJECT_LOCK(object); 577178949e0SAlan Cox vm_page_lock_queues(); 578aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 579178949e0SAlan Cox vm_page_unlock_queues(); 5802bf43e43SAlan Cox VM_OBJECT_UNLOCK(object); 5819e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 58226f9a767SRodney W. Grimes } 58326f9a767SRodney W. Grimes } 5849e0ddbd0SAlan Cox sf_buf_free(sf); 58585e01243SAlan Cox vm_page_lock_queues(); 5860385347cSPeter Wemm pmap_clear_modify(m); 58785e01243SAlan Cox vm_page_unlock_queues(); 58826f9a767SRodney W. Grimes if (error) { 589a83c285cSDavid Greenman return VM_PAGER_ERROR; 59026f9a767SRodney W. Grimes } 59126f9a767SRodney W. Grimes return VM_PAGER_OK; 59226f9a767SRodney W. Grimes 59326f9a767SRodney W. Grimes } 59426f9a767SRodney W. Grimes 59526f9a767SRodney W. Grimes 59626f9a767SRodney W. Grimes /* 597475e8cc3SPoul-Henning Kamp * old style vnode pager input routine 59826f9a767SRodney W. Grimes */ 599f708ef1bSPoul-Henning Kamp static int 60024a1cce3SDavid Greenman vnode_pager_input_old(object, m) 60124a1cce3SDavid Greenman vm_object_t object; 60226f9a767SRodney W. Grimes vm_page_t m; 60326f9a767SRodney W. Grimes { 604df8bae1dSRodney W. Grimes struct uio auio; 605df8bae1dSRodney W. Grimes struct iovec aiov; 60626f9a767SRodney W. Grimes int error; 60726f9a767SRodney W. Grimes int size; 6089e0ddbd0SAlan Cox struct sf_buf *sf; 609342a1480SJohn Baldwin struct vnode *vp; 610df8bae1dSRodney W. Grimes 61152051abcSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 61226f9a767SRodney W. Grimes error = 0; 613bbc0ec52SDavid Greenman 614df8bae1dSRodney W. Grimes /* 61526f9a767SRodney W. Grimes * Return failure if beyond current EOF 61626f9a767SRodney W. Grimes */ 617a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 61826f9a767SRodney W. Grimes return VM_PAGER_BAD; 61926f9a767SRodney W. Grimes } else { 62026f9a767SRodney W. Grimes size = PAGE_SIZE; 621a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 622a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 62352051abcSAlan Cox vp = object->handle; 62452051abcSAlan Cox VM_OBJECT_UNLOCK(object); 6250bdb7528SDavid Greenman 62626f9a767SRodney W. Grimes /* 627df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 628df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 629df8bae1dSRodney W. Grimes */ 6309e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 6310bdb7528SDavid Greenman 6329e0ddbd0SAlan Cox aiov.iov_base = (caddr_t)sf_buf_kva(sf); 633df8bae1dSRodney W. Grimes aiov.iov_len = size; 634df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 635df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 636a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 637df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 63826f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 639df8bae1dSRodney W. Grimes auio.uio_resid = size; 640b40ce416SJulian Elischer auio.uio_td = curthread; 64126f9a767SRodney W. Grimes 642a854ed98SJohn Baldwin error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 643df8bae1dSRodney W. Grimes if (!error) { 64454d92145SMatthew Dillon int count = size - auio.uio_resid; 645df8bae1dSRodney W. Grimes 646df8bae1dSRodney W. Grimes if (count == 0) 647df8bae1dSRodney W. Grimes error = EINVAL; 64826f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 6499e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + count, 6509e0ddbd0SAlan Cox PAGE_SIZE - count); 651df8bae1dSRodney W. Grimes } 6529e0ddbd0SAlan Cox sf_buf_free(sf); 6531b26eb10SAlan Cox 6541b26eb10SAlan Cox VM_OBJECT_LOCK(object); 655df8bae1dSRodney W. Grimes } 65685e01243SAlan Cox vm_page_lock_queues(); 6570385347cSPeter Wemm pmap_clear_modify(m); 6582c28a105SAlan Cox vm_page_undirty(m); 6591b26eb10SAlan Cox vm_page_unlock_queues(); 6606e3a3f38SRobert V. Baron if (!error) 6616e3a3f38SRobert V. Baron m->valid = VM_PAGE_BITS_ALL; 662a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 66326f9a767SRodney W. Grimes } 66426f9a767SRodney W. Grimes 66526f9a767SRodney W. Grimes /* 66626f9a767SRodney W. Grimes * generic vnode pager input routine 66726f9a767SRodney W. Grimes */ 668170db9c6SJohn Dyson 669ce75f2c3SMike Smith /* 67023955314SAlfred Perlstein * Local media VFS's that do not implement their own VOP_GETPAGES 67147e151ddSRobert Drehmel * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 67247e151ddSRobert Drehmel * to implement the previous behaviour. 673ce75f2c3SMike Smith * 674ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 675ce75f2c3SMike Smith * backing vp's VOP_GETPAGES. 676ce75f2c3SMike Smith */ 677f708ef1bSPoul-Henning Kamp static int 67824a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage) 67926f9a767SRodney W. Grimes vm_object_t object; 68024a1cce3SDavid Greenman vm_page_t *m; 68124a1cce3SDavid Greenman int count; 68224a1cce3SDavid Greenman int reqpage; 68324a1cce3SDavid Greenman { 684170db9c6SJohn Dyson int rtval; 685170db9c6SJohn Dyson struct vnode *vp; 68686ffbd76SMike Smith int bytes = count * PAGE_SIZE; 687ae51ff11SJeff Roberson int vfslocked; 68895e5e988SJohn Dyson 689170db9c6SJohn Dyson vp = object->handle; 6908630c117SAlan Cox VM_OBJECT_UNLOCK(object); 691ae51ff11SJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 69286ffbd76SMike Smith rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 69323955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 69423955314SAlfred Perlstein ("vnode_pager: FS getpages not implemented\n")); 695ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 6968630c117SAlan Cox VM_OBJECT_LOCK(object); 697170db9c6SJohn Dyson return rtval; 698170db9c6SJohn Dyson } 699170db9c6SJohn Dyson 700ce75f2c3SMike Smith /* 701ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 702ce75f2c3SMike Smith * own vnodes if they fail to implement VOP_GETPAGES. 703ce75f2c3SMike Smith */ 704ce75f2c3SMike Smith int 705ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 706ce75f2c3SMike Smith struct vnode *vp; 707170db9c6SJohn Dyson vm_page_t *m; 708ce75f2c3SMike Smith int bytecount; 709170db9c6SJohn Dyson int reqpage; 710170db9c6SJohn Dyson { 711ce75f2c3SMike Smith vm_object_t object; 712a316d390SJohn Dyson vm_offset_t kva; 7138f9110f6SJohn Dyson off_t foff, tfoff, nextoff; 714f3aad9a6SBjoern A. Zeeb int i, j, size, bsize, first; 715f3aad9a6SBjoern A. Zeeb daddr_t firstaddr; 7169c83534dSPoul-Henning Kamp struct bufobj *bo; 717efc68ce1SDavid Greenman int runpg; 718efc68ce1SDavid Greenman int runend; 7190bdb7528SDavid Greenman struct buf *bp; 720ce75f2c3SMike Smith int count; 72126f9a767SRodney W. Grimes int error = 0; 72226f9a767SRodney W. Grimes 723ce75f2c3SMike Smith object = vp->v_object; 724ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 725ce75f2c3SMike Smith 7269c83534dSPoul-Henning Kamp KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 7279c83534dSPoul-Henning Kamp ("vnode_pager_generic_getpages does not support devices")); 7282c4488fcSJohn Dyson if (vp->v_mount == NULL) 7292c4488fcSJohn Dyson return VM_PAGER_BAD; 7302c4488fcSJohn Dyson 73126f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 73226f9a767SRodney W. Grimes 73326f9a767SRodney W. Grimes /* get the UNDERLYING device for the file with VOP_BMAP() */ 734bbc0ec52SDavid Greenman 73526f9a767SRodney W. Grimes /* 736bbc0ec52SDavid Greenman * originally, we did not check for an error return value -- assuming 737bbc0ec52SDavid Greenman * an fs always has a bmap entry point -- that assumption is wrong!!! 73826f9a767SRodney W. Grimes */ 739a316d390SJohn Dyson foff = IDX_TO_OFF(m[reqpage]->pindex); 740bbc0ec52SDavid Greenman 74126f9a767SRodney W. Grimes /* 74216f62314SDavid Greenman * if we can't bmap, use old VOP code 74326f9a767SRodney W. Grimes */ 7449c83534dSPoul-Henning Kamp if (VOP_BMAP(vp, 0, &bo, 0, NULL, NULL)) { 74531953be9SAlan Cox VM_OBJECT_LOCK(object); 746e43c2eabSAlan Cox vm_page_lock_queues(); 747e43c2eabSAlan Cox for (i = 0; i < count; i++) 748e43c2eabSAlan Cox if (i != reqpage) 749d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 750e43c2eabSAlan Cox vm_page_unlock_queues(); 751976e77fcSDavid Greenman cnt.v_vnodein++; 752976e77fcSDavid Greenman cnt.v_vnodepgsin++; 75352051abcSAlan Cox error = vnode_pager_input_old(object, m[reqpage]); 75452051abcSAlan Cox VM_OBJECT_UNLOCK(object); 75552051abcSAlan Cox return (error); 756bbc0ec52SDavid Greenman 75726f9a767SRodney W. Grimes /* 75826f9a767SRodney W. Grimes * if the blocksize is smaller than a page size, then use 75926f9a767SRodney W. Grimes * special small filesystem code. NFS sometimes has a small 76026f9a767SRodney W. Grimes * blocksize, but it can handle large reads itself. 76126f9a767SRodney W. Grimes */ 76226f9a767SRodney W. Grimes } else if ((PAGE_SIZE / bsize) > 1 && 763500b04a2SBruce Evans (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 76431953be9SAlan Cox VM_OBJECT_LOCK(object); 765e43c2eabSAlan Cox vm_page_lock_queues(); 766e43c2eabSAlan Cox for (i = 0; i < count; i++) 767e43c2eabSAlan Cox if (i != reqpage) 768d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 769e43c2eabSAlan Cox vm_page_unlock_queues(); 77031953be9SAlan Cox VM_OBJECT_UNLOCK(object); 771976e77fcSDavid Greenman cnt.v_vnodein++; 772976e77fcSDavid Greenman cnt.v_vnodepgsin++; 77324a1cce3SDavid Greenman return vnode_pager_input_smlfs(object, m[reqpage]); 77426f9a767SRodney W. Grimes } 7758d17e694SJulian Elischer 77626f9a767SRodney W. Grimes /* 7778d17e694SJulian Elischer * If we have a completely valid page available to us, we can 7788d17e694SJulian Elischer * clean up and return. Otherwise we have to re-read the 7798d17e694SJulian Elischer * media. 7800d94caffSDavid Greenman */ 78131953be9SAlan Cox VM_OBJECT_LOCK(object); 7828b575f6cSAlan Cox if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 783e43c2eabSAlan Cox vm_page_lock_queues(); 784e43c2eabSAlan Cox for (i = 0; i < count; i++) 7850d94caffSDavid Greenman if (i != reqpage) 786d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 787e43c2eabSAlan Cox vm_page_unlock_queues(); 78831953be9SAlan Cox VM_OBJECT_UNLOCK(object); 7890d94caffSDavid Greenman return VM_PAGER_OK; 7900d94caffSDavid Greenman } 7918d17e694SJulian Elischer m[reqpage]->valid = 0; 7928b575f6cSAlan Cox VM_OBJECT_UNLOCK(object); 7930bdb7528SDavid Greenman 7940d94caffSDavid Greenman /* 79526f9a767SRodney W. Grimes * here on direct device I/O 79626f9a767SRodney W. Grimes */ 797efc68ce1SDavid Greenman firstaddr = -1; 798a1287949SEivind Eklund 79926f9a767SRodney W. Grimes /* 800efc68ce1SDavid Greenman * calculate the run that includes the required page 80126f9a767SRodney W. Grimes */ 802efc68ce1SDavid Greenman for (first = 0, i = 0; i < count; i = runend) { 803a316d390SJohn Dyson firstaddr = vnode_pager_addr(vp, 804a316d390SJohn Dyson IDX_TO_OFF(m[i]->pindex), &runpg); 805efc68ce1SDavid Greenman if (firstaddr == -1) { 80631953be9SAlan Cox VM_OBJECT_LOCK(object); 80724a1cce3SDavid Greenman if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 808f3aad9a6SBjoern A. Zeeb panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx", 809f3aad9a6SBjoern A. Zeeb (intmax_t)firstaddr, (uintmax_t)(foff >> 32), 810bf1001faSMaxime Henrion (uintmax_t)foff, 811bf1001faSMaxime Henrion (uintmax_t) 812fc62ef1fSBruce Evans (object->un_pager.vnp.vnp_size >> 32), 813bf1001faSMaxime Henrion (uintmax_t)object->un_pager.vnp.vnp_size); 814efc68ce1SDavid Greenman } 815e43c2eabSAlan Cox vm_page_lock_queues(); 816d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 817e43c2eabSAlan Cox vm_page_unlock_queues(); 81831953be9SAlan Cox VM_OBJECT_UNLOCK(object); 819efc68ce1SDavid Greenman runend = i + 1; 820efc68ce1SDavid Greenman first = runend; 821efc68ce1SDavid Greenman continue; 822efc68ce1SDavid Greenman } 823efc68ce1SDavid Greenman runend = i + runpg; 824efc68ce1SDavid Greenman if (runend <= reqpage) { 82531953be9SAlan Cox VM_OBJECT_LOCK(object); 826e43c2eabSAlan Cox vm_page_lock_queues(); 827e43c2eabSAlan Cox for (j = i; j < runend; j++) 828d8d5fa88SAlfred Perlstein vm_page_free(m[j]); 829e43c2eabSAlan Cox vm_page_unlock_queues(); 83031953be9SAlan Cox VM_OBJECT_UNLOCK(object); 83126f9a767SRodney W. Grimes } else { 832efc68ce1SDavid Greenman if (runpg < (count - first)) { 83331953be9SAlan Cox VM_OBJECT_LOCK(object); 834e43c2eabSAlan Cox vm_page_lock_queues(); 835efc68ce1SDavid Greenman for (i = first + runpg; i < count; i++) 836d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 837e43c2eabSAlan Cox vm_page_unlock_queues(); 83831953be9SAlan Cox VM_OBJECT_UNLOCK(object); 839efc68ce1SDavid Greenman count = first + runpg; 84026f9a767SRodney W. Grimes } 841efc68ce1SDavid Greenman break; 84226f9a767SRodney W. Grimes } 843efc68ce1SDavid Greenman first = runend; 844efc68ce1SDavid Greenman } 84526f9a767SRodney W. Grimes 84626f9a767SRodney W. Grimes /* 847bbc0ec52SDavid Greenman * the first and last page have been calculated now, move input pages 848bbc0ec52SDavid Greenman * to be zero based... 84926f9a767SRodney W. Grimes */ 85026f9a767SRodney W. Grimes if (first != 0) { 85126f9a767SRodney W. Grimes for (i = first; i < count; i++) { 85226f9a767SRodney W. Grimes m[i - first] = m[i]; 85326f9a767SRodney W. Grimes } 85426f9a767SRodney W. Grimes count -= first; 85526f9a767SRodney W. Grimes reqpage -= first; 85626f9a767SRodney W. Grimes } 857efc68ce1SDavid Greenman 85826f9a767SRodney W. Grimes /* 85926f9a767SRodney W. Grimes * calculate the file virtual address for the transfer 86026f9a767SRodney W. Grimes */ 861a316d390SJohn Dyson foff = IDX_TO_OFF(m[0]->pindex); 86226f9a767SRodney W. Grimes 86326f9a767SRodney W. Grimes /* 86426f9a767SRodney W. Grimes * calculate the size of the transfer 86526f9a767SRodney W. Grimes */ 86626f9a767SRodney W. Grimes size = count * PAGE_SIZE; 8671a31a6c3SPoul-Henning Kamp KASSERT(count > 0, ("zero count")); 86824a1cce3SDavid Greenman if ((foff + size) > object->un_pager.vnp.vnp_size) 86924a1cce3SDavid Greenman size = object->un_pager.vnp.vnp_size - foff; 8701a31a6c3SPoul-Henning Kamp KASSERT(size > 0, ("zero size")); 87126f9a767SRodney W. Grimes 87226f9a767SRodney W. Grimes /* 87324579ca1SMatthew Dillon * round up physical size for real devices. 87426f9a767SRodney W. Grimes */ 8759c83534dSPoul-Henning Kamp if (1) { 8769c83534dSPoul-Henning Kamp int secmask = bo->bo_bsize - 1; 8776229cc50SPoul-Henning Kamp KASSERT(secmask < PAGE_SIZE && secmask > 0, 8786229cc50SPoul-Henning Kamp ("vnode_pager_generic_getpages: sector size %d too large", 8796229cc50SPoul-Henning Kamp secmask + 1)); 88024579ca1SMatthew Dillon size = (size + secmask) & ~secmask; 88124579ca1SMatthew Dillon } 88226f9a767SRodney W. Grimes 8831c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 88416f62314SDavid Greenman kva = (vm_offset_t) bp->b_data; 88516f62314SDavid Greenman 88626f9a767SRodney W. Grimes /* 88726f9a767SRodney W. Grimes * and map the pages to be read into the kva 88826f9a767SRodney W. Grimes */ 88916f62314SDavid Greenman pmap_qenter(kva, m, count); 89026f9a767SRodney W. Grimes 89126f9a767SRodney W. Grimes /* build a minimal buffer header */ 89221144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 8936a4b5823SPoul-Henning Kamp bp->b_iodone = bdone; 894bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 895bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 896a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 897a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 898187f0071SDavid Greenman bp->b_blkno = firstaddr; 8999c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 90026f9a767SRodney W. Grimes bp->b_bcount = size; 90126f9a767SRodney W. Grimes bp->b_bufsize = size; 9022b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 9034f12e0acSSuleiman Souhlal atomic_add_int(&runningbufspace, bp->b_runningbufspace); 90426f9a767SRodney W. Grimes 905976e77fcSDavid Greenman cnt.v_vnodein++; 906976e77fcSDavid Greenman cnt.v_vnodepgsin += count; 907976e77fcSDavid Greenman 90826f9a767SRodney W. Grimes /* do the input */ 9092c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 910b792bebeSPoul-Henning Kamp bstrategy(bp); 911976e77fcSDavid Greenman 9126a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnread"); 91326f9a767SRodney W. Grimes 914c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 91526f9a767SRodney W. Grimes error = EIO; 91626f9a767SRodney W. Grimes 91726f9a767SRodney W. Grimes if (!error) { 91826f9a767SRodney W. Grimes if (size != count * PAGE_SIZE) 91926f9a767SRodney W. Grimes bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 92026f9a767SRodney W. Grimes } 92116f62314SDavid Greenman pmap_qremove(kva, count); 92226f9a767SRodney W. Grimes 92326f9a767SRodney W. Grimes /* 92426f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 92526f9a767SRodney W. Grimes */ 9269c83534dSPoul-Henning Kamp pbrelbo(bp); 9271c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 92826f9a767SRodney W. Grimes 92931953be9SAlan Cox VM_OBJECT_LOCK(object); 9309d522888SAlan Cox vm_page_lock_queues(); 9318f9110f6SJohn Dyson for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 9328f9110f6SJohn Dyson vm_page_t mt; 9338f9110f6SJohn Dyson 9348f9110f6SJohn Dyson nextoff = tfoff + PAGE_SIZE; 9358f9110f6SJohn Dyson mt = m[i]; 9368f9110f6SJohn Dyson 93754746b67SDmitrij Tejblum if (nextoff <= object->un_pager.vnp.vnp_size) { 9388d17e694SJulian Elischer /* 9398d17e694SJulian Elischer * Read filled up entire page. 9408d17e694SJulian Elischer */ 9418f9110f6SJohn Dyson mt->valid = VM_PAGE_BITS_ALL; 9422c28a105SAlan Cox vm_page_undirty(mt); /* should be an assert? XXX */ 9430385347cSPeter Wemm pmap_clear_modify(mt); 9448f9110f6SJohn Dyson } else { 9458d17e694SJulian Elischer /* 9468d17e694SJulian Elischer * Read did not fill up entire page. Since this 9478d17e694SJulian Elischer * is getpages, the page may be mapped, so we have 9488d17e694SJulian Elischer * to zero the invalid portions of the page even 9498d17e694SJulian Elischer * though we aren't setting them valid. 9508d17e694SJulian Elischer * 9518d17e694SJulian Elischer * Currently we do not set the entire page valid, 9528d17e694SJulian Elischer * we just try to clear the piece that we couldn't 9538d17e694SJulian Elischer * read. 9548d17e694SJulian Elischer */ 95554746b67SDmitrij Tejblum vm_page_set_validclean(mt, 0, 95654746b67SDmitrij Tejblum object->un_pager.vnp.vnp_size - tfoff); 9574221e284SAlan Cox /* handled by vm_fault now */ 9584221e284SAlan Cox /* vm_page_zero_invalid(mt, FALSE); */ 9598f9110f6SJohn Dyson } 9608f9110f6SJohn Dyson 96126f9a767SRodney W. Grimes if (i != reqpage) { 962bbc0ec52SDavid Greenman 96326f9a767SRodney W. Grimes /* 964bbc0ec52SDavid Greenman * whether or not to leave the page activated is up in 965bbc0ec52SDavid Greenman * the air, but we should put the page on a page queue 966bbc0ec52SDavid Greenman * somewhere. (it already is in the object). Result: 967956f3135SPhilippe Charnier * It appears that empirical results show that 968bbc0ec52SDavid Greenman * deactivating pages is best. 96926f9a767SRodney W. Grimes */ 970bbc0ec52SDavid Greenman 97126f9a767SRodney W. Grimes /* 972bbc0ec52SDavid Greenman * just in case someone was asking for this page we 973bbc0ec52SDavid Greenman * now tell them that it is ok to use 97426f9a767SRodney W. Grimes */ 97526f9a767SRodney W. Grimes if (!error) { 9768f9110f6SJohn Dyson if (mt->flags & PG_WANTED) 9778f9110f6SJohn Dyson vm_page_activate(mt); 97895461b45SJohn Dyson else 9798f9110f6SJohn Dyson vm_page_deactivate(mt); 980e69763a3SDoug Rabson vm_page_wakeup(mt); 98126f9a767SRodney W. Grimes } else { 982d8d5fa88SAlfred Perlstein vm_page_free(mt); 98326f9a767SRodney W. Grimes } 98426f9a767SRodney W. Grimes } 98526f9a767SRodney W. Grimes } 9869d522888SAlan Cox vm_page_unlock_queues(); 98731953be9SAlan Cox VM_OBJECT_UNLOCK(object); 98826f9a767SRodney W. Grimes if (error) { 98924a1cce3SDavid Greenman printf("vnode_pager_getpages: I/O read error\n"); 99026f9a767SRodney W. Grimes } 991a83c285cSDavid Greenman return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 99226f9a767SRodney W. Grimes } 99326f9a767SRodney W. Grimes 994ce75f2c3SMike Smith /* 995ce75f2c3SMike Smith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 996ce75f2c3SMike Smith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 997ce75f2c3SMike Smith * vnode_pager_generic_putpages() to implement the previous behaviour. 998ce75f2c3SMike Smith * 999ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 1000ce75f2c3SMike Smith * backing vp's VOP_PUTPAGES. 1001ce75f2c3SMike Smith */ 1002e4542174SMatthew Dillon static void 1003170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals) 1004170db9c6SJohn Dyson vm_object_t object; 1005170db9c6SJohn Dyson vm_page_t *m; 1006170db9c6SJohn Dyson int count; 1007170db9c6SJohn Dyson boolean_t sync; 1008170db9c6SJohn Dyson int *rtvals; 1009170db9c6SJohn Dyson { 1010170db9c6SJohn Dyson int rtval; 1011170db9c6SJohn Dyson struct vnode *vp; 1012f2a2857bSKirk McKusick struct mount *mp; 101386ffbd76SMike Smith int bytes = count * PAGE_SIZE; 1014ad980522SJohn Dyson 10150e3cdf2cSAlan Cox /* 10160e3cdf2cSAlan Cox * Force synchronous operation if we are extremely low on memory 10170e3cdf2cSAlan Cox * to prevent a low-memory deadlock. VOP operations often need to 10180e3cdf2cSAlan Cox * allocate more memory to initiate the I/O ( i.e. do a BMAP 10190e3cdf2cSAlan Cox * operation ). The swapper handles the case by limiting the amount 10200e3cdf2cSAlan Cox * of asynchronous I/O, but that sort of solution doesn't scale well 10210e3cdf2cSAlan Cox * for the vnode pager without a lot of work. 10220e3cdf2cSAlan Cox * 10230e3cdf2cSAlan Cox * Also, the backing vnode's iodone routine may not wake the pageout 10240e3cdf2cSAlan Cox * daemon up. This should be probably be addressed XXX. 10250e3cdf2cSAlan Cox */ 10260e3cdf2cSAlan Cox 10270e3cdf2cSAlan Cox if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 10280e3cdf2cSAlan Cox sync |= OBJPC_SYNC; 10290e3cdf2cSAlan Cox 10300e3cdf2cSAlan Cox /* 10310e3cdf2cSAlan Cox * Call device-specific putpages function 10320e3cdf2cSAlan Cox */ 1033170db9c6SJohn Dyson vp = object->handle; 10342e3b314dSAlan Cox VM_OBJECT_UNLOCK(object); 1035f2a2857bSKirk McKusick if (vp->v_type != VREG) 1036f2a2857bSKirk McKusick mp = NULL; 1037f2a2857bSKirk McKusick (void)vn_start_write(vp, &mp, V_WAIT); 103886ffbd76SMike Smith rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 103923955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 104023955314SAlfred Perlstein ("vnode_pager: stale FS putpages\n")); 1041f2a2857bSKirk McKusick vn_finished_write(mp); 10422e3b314dSAlan Cox VM_OBJECT_LOCK(object); 1043170db9c6SJohn Dyson } 1044170db9c6SJohn Dyson 1045ce75f2c3SMike Smith 104626f9a767SRodney W. Grimes /* 1047ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 10484491ea91SEivind Eklund * own vnodes if they fail to implement VOP_PUTPAGES. 10492b6b0df7SMatthew Dillon * 10502b6b0df7SMatthew Dillon * This is typically called indirectly via the pageout daemon and 10512b6b0df7SMatthew Dillon * clustering has already typically occured, so in general we ask the 10522b6b0df7SMatthew Dillon * underlying filesystem to write the data out asynchronously rather 10532b6b0df7SMatthew Dillon * then delayed. 105426f9a767SRodney W. Grimes */ 1055ce75f2c3SMike Smith int 10568f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 1057ce75f2c3SMike Smith struct vnode *vp; 105826f9a767SRodney W. Grimes vm_page_t *m; 1059ce75f2c3SMike Smith int bytecount; 10608f9110f6SJohn Dyson int flags; 106126f9a767SRodney W. Grimes int *rtvals; 106226f9a767SRodney W. Grimes { 1063f6b04d2bSDavid Greenman int i; 1064ce75f2c3SMike Smith vm_object_t object; 1065ce75f2c3SMike Smith int count; 106626f9a767SRodney W. Grimes 1067f6b04d2bSDavid Greenman int maxsize, ncount; 1068a316d390SJohn Dyson vm_ooffset_t poffset; 1069f6b04d2bSDavid Greenman struct uio auio; 1070f6b04d2bSDavid Greenman struct iovec aiov; 1071f6b04d2bSDavid Greenman int error; 10728f9110f6SJohn Dyson int ioflags; 107326f9a767SRodney W. Grimes 1074ce75f2c3SMike Smith object = vp->v_object; 1075ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 1076ce75f2c3SMike Smith 107726f9a767SRodney W. Grimes for (i = 0; i < count; i++) 107826f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_AGAIN; 107926f9a767SRodney W. Grimes 1080d8fed1d0SAlan Cox if ((int64_t)m[0]->pindex < 0) { 108123562e4bSMarcel Moolenaar printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n", 108223562e4bSMarcel Moolenaar (long)m[0]->pindex, (u_long)m[0]->dirty); 1083f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 1084f6b04d2bSDavid Greenman return VM_PAGER_BAD; 10850d94caffSDavid Greenman } 10860bdb7528SDavid Greenman 1087f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 1088f6b04d2bSDavid Greenman ncount = count; 108926f9a767SRodney W. Grimes 1090a316d390SJohn Dyson poffset = IDX_TO_OFF(m[0]->pindex); 109100a6f47fSMatthew Dillon 109200a6f47fSMatthew Dillon /* 109300a6f47fSMatthew Dillon * If the page-aligned write is larger then the actual file we 109400a6f47fSMatthew Dillon * have to invalidate pages occuring beyond the file EOF. However, 109500a6f47fSMatthew Dillon * there is an edge case where a file may not be page-aligned where 109600a6f47fSMatthew Dillon * the last page is partially invalid. In this case the filesystem 109700a6f47fSMatthew Dillon * may not properly clear the dirty bits for the entire page (which 109800a6f47fSMatthew Dillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 109900a6f47fSMatthew Dillon * With the page locked we are free to fix-up the dirty bits here. 11003ebeaf59SMatthew Dillon * 11013ebeaf59SMatthew Dillon * We do not under any circumstances truncate the valid bits, as 11023ebeaf59SMatthew Dillon * this will screw up bogus page replacement. 110300a6f47fSMatthew Dillon */ 1104a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 110500a6f47fSMatthew Dillon if (object->un_pager.vnp.vnp_size > poffset) { 110600a6f47fSMatthew Dillon int pgoff; 110700a6f47fSMatthew Dillon 1108a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 1109aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 111000a6f47fSMatthew Dillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1111b7ad744dSAlan Cox vm_page_lock_queues(); 111200a6f47fSMatthew Dillon vm_page_clear_dirty(m[ncount - 1], pgoff, 111300a6f47fSMatthew Dillon PAGE_SIZE - pgoff); 1114b7ad744dSAlan Cox vm_page_unlock_queues(); 111500a6f47fSMatthew Dillon } 111600a6f47fSMatthew Dillon } else { 111700a6f47fSMatthew Dillon maxsize = 0; 111800a6f47fSMatthew Dillon ncount = 0; 111900a6f47fSMatthew Dillon } 1120f6b04d2bSDavid Greenman if (ncount < count) { 1121f6b04d2bSDavid Greenman for (i = ncount; i < count; i++) { 1122f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 1123f6b04d2bSDavid Greenman } 1124f6b04d2bSDavid Greenman } 1125f6b04d2bSDavid Greenman } 112626f9a767SRodney W. Grimes 11272b6b0df7SMatthew Dillon /* 11282b6b0df7SMatthew Dillon * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 11292b6b0df7SMatthew Dillon * rather then a bdwrite() to prevent paging I/O from saturating 113043b7990eSMatthew Dillon * the buffer cache. Dummy-up the sequential heuristic to cause 113143b7990eSMatthew Dillon * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 113243b7990eSMatthew Dillon * the system decides how to cluster. 11332b6b0df7SMatthew Dillon */ 11348f9110f6SJohn Dyson ioflags = IO_VMIO; 113543b7990eSMatthew Dillon if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 113643b7990eSMatthew Dillon ioflags |= IO_SYNC; 113743b7990eSMatthew Dillon else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 113843b7990eSMatthew Dillon ioflags |= IO_ASYNC; 11398f9110f6SJohn Dyson ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 114043b7990eSMatthew Dillon ioflags |= IO_SEQMAX << IO_SEQSHIFT; 1141f6b04d2bSDavid Greenman 1142f6b04d2bSDavid Greenman aiov.iov_base = (caddr_t) 0; 1143f6b04d2bSDavid Greenman aiov.iov_len = maxsize; 1144f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 1145f6b04d2bSDavid Greenman auio.uio_iovcnt = 1; 1146a316d390SJohn Dyson auio.uio_offset = poffset; 1147f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 1148f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 1149f6b04d2bSDavid Greenman auio.uio_resid = maxsize; 1150b40ce416SJulian Elischer auio.uio_td = (struct thread *) 0; 1151a854ed98SJohn Baldwin error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1152976e77fcSDavid Greenman cnt.v_vnodeout++; 1153f6b04d2bSDavid Greenman cnt.v_vnodepgsout += ncount; 1154f6b04d2bSDavid Greenman 1155f6b04d2bSDavid Greenman if (error) { 115624a1cce3SDavid Greenman printf("vnode_pager_putpages: I/O error %d\n", error); 1157f6b04d2bSDavid Greenman } 1158f6b04d2bSDavid Greenman if (auio.uio_resid) { 1159ac1e407bSBruce Evans printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1160ac1e407bSBruce Evans auio.uio_resid, (u_long)m[0]->pindex); 116126f9a767SRodney W. Grimes } 1162ffc82b0aSJohn Dyson for (i = 0; i < ncount; i++) { 116326f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 116426f9a767SRodney W. Grimes } 1165f6b04d2bSDavid Greenman return rtvals[0]; 116626f9a767SRodney W. Grimes } 1167f6b04d2bSDavid Greenman 1168f6b04d2bSDavid Greenman struct vnode * 1169417a26a1SAlan Cox vnode_pager_lock(vm_object_t first_object) 117024a1cce3SDavid Greenman { 1171417a26a1SAlan Cox struct vnode *vp; 1172417a26a1SAlan Cox vm_object_t backing_object, object; 1173996c772fSJohn Dyson 1174417a26a1SAlan Cox VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 1175417a26a1SAlan Cox for (object = first_object; object != NULL; object = backing_object) { 1176417a26a1SAlan Cox if (object->type != OBJT_VNODE) { 1177417a26a1SAlan Cox if ((backing_object = object->backing_object) != NULL) 1178417a26a1SAlan Cox VM_OBJECT_LOCK(backing_object); 1179417a26a1SAlan Cox if (object != first_object) 1180417a26a1SAlan Cox VM_OBJECT_UNLOCK(object); 1181f6b04d2bSDavid Greenman continue; 1182417a26a1SAlan Cox } 1183417a26a1SAlan Cox retry: 1184e6b961ffSJohn Baldwin if (object->flags & OBJ_DEAD) { 1185417a26a1SAlan Cox if (object != first_object) 1186417a26a1SAlan Cox VM_OBJECT_UNLOCK(object); 118747221757SJohn Dyson return NULL; 1188e6b961ffSJohn Baldwin } 1189417a26a1SAlan Cox vp = object->handle; 1190417a26a1SAlan Cox VI_LOCK(vp); 1191417a26a1SAlan Cox VM_OBJECT_UNLOCK(object); 1192417a26a1SAlan Cox if (first_object != object) 1193417a26a1SAlan Cox VM_OBJECT_UNLOCK(first_object); 1194ed4fe4f4SJeff Roberson VFS_ASSERT_GIANT(vp->v_mount); 1195f247a524SJeff Roberson if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | 1196417a26a1SAlan Cox LK_RETRY | LK_SHARED, curthread)) { 1197417a26a1SAlan Cox VM_OBJECT_LOCK(first_object); 1198417a26a1SAlan Cox if (object != first_object) 1199417a26a1SAlan Cox VM_OBJECT_LOCK(object); 1200417a26a1SAlan Cox if (object->type != OBJT_VNODE) { 1201417a26a1SAlan Cox if (object != first_object) 1202417a26a1SAlan Cox VM_OBJECT_UNLOCK(object); 1203bef608bdSJohn Dyson return NULL; 1204417a26a1SAlan Cox } 120547221757SJohn Dyson printf("vnode_pager_lock: retrying\n"); 1206417a26a1SAlan Cox goto retry; 120747221757SJohn Dyson } 1208417a26a1SAlan Cox VM_OBJECT_LOCK(first_object); 1209417a26a1SAlan Cox return (vp); 121026f9a767SRodney W. Grimes } 121124a1cce3SDavid Greenman return NULL; 1212f6b04d2bSDavid Greenman } 1213