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 78bff76343SAlan Cox static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 79bff76343SAlan Cox daddr_t *rtaddress, int *run); 8011caded3SAlfred Perlstein static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 8111caded3SAlfred Perlstein static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 8211caded3SAlfred Perlstein static void vnode_pager_dealloc(vm_object_t); 8311caded3SAlfred Perlstein static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int); 8411caded3SAlfred Perlstein static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 8511caded3SAlfred Perlstein static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 86d07a6d3fSPoul-Henning Kamp static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t); 870b8253a7SBruce Evans 88df8bae1dSRodney W. Grimes struct pagerops vnodepagerops = { 894e658600SPoul-Henning Kamp .pgo_alloc = vnode_pager_alloc, 904e658600SPoul-Henning Kamp .pgo_dealloc = vnode_pager_dealloc, 914e658600SPoul-Henning Kamp .pgo_getpages = vnode_pager_getpages, 924e658600SPoul-Henning Kamp .pgo_putpages = vnode_pager_putpages, 934e658600SPoul-Henning Kamp .pgo_haspage = vnode_pager_haspage, 94df8bae1dSRodney W. Grimes }; 95df8bae1dSRodney W. Grimes 96b62b9b64SJohn Baldwin int vnode_pbuf_freecnt; 971c7c3c6aSMatthew Dillon 98d07a6d3fSPoul-Henning Kamp /* Create the VM system backing object for this vnode */ 99d07a6d3fSPoul-Henning Kamp int 100731959b1SYaroslav Tykhiy vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td) 101d07a6d3fSPoul-Henning Kamp { 102d07a6d3fSPoul-Henning Kamp vm_object_t object; 103d07a6d3fSPoul-Henning Kamp vm_ooffset_t size = isize; 104d07a6d3fSPoul-Henning Kamp struct vattr va; 105d07a6d3fSPoul-Henning Kamp 106d07a6d3fSPoul-Henning Kamp if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE) 107d07a6d3fSPoul-Henning Kamp return (0); 108d07a6d3fSPoul-Henning Kamp 109d07a6d3fSPoul-Henning Kamp while ((object = vp->v_object) != NULL) { 110d07a6d3fSPoul-Henning Kamp VM_OBJECT_LOCK(object); 111d07a6d3fSPoul-Henning Kamp if (!(object->flags & OBJ_DEAD)) { 112d07a6d3fSPoul-Henning Kamp VM_OBJECT_UNLOCK(object); 113d07a6d3fSPoul-Henning Kamp return (0); 114d07a6d3fSPoul-Henning Kamp } 115d07a6d3fSPoul-Henning Kamp VOP_UNLOCK(vp, 0, td); 116d07a6d3fSPoul-Henning Kamp vm_object_set_flag(object, OBJ_DISCONNECTWNT); 117d07a6d3fSPoul-Henning Kamp msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0); 118d07a6d3fSPoul-Henning Kamp vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 119d07a6d3fSPoul-Henning Kamp } 120d07a6d3fSPoul-Henning Kamp 121d07a6d3fSPoul-Henning Kamp if (size == 0) { 122d07a6d3fSPoul-Henning Kamp if (vn_isdisk(vp, NULL)) { 123d07a6d3fSPoul-Henning Kamp size = IDX_TO_OFF(INT_MAX); 124d07a6d3fSPoul-Henning Kamp } else { 125d07a6d3fSPoul-Henning Kamp if (VOP_GETATTR(vp, &va, td->td_ucred, td) != 0) 126d07a6d3fSPoul-Henning Kamp return (0); 127d07a6d3fSPoul-Henning Kamp size = va.va_size; 128d07a6d3fSPoul-Henning Kamp } 129d07a6d3fSPoul-Henning Kamp } 130d07a6d3fSPoul-Henning Kamp 131d07a6d3fSPoul-Henning Kamp object = vnode_pager_alloc(vp, size, 0, 0); 132d07a6d3fSPoul-Henning Kamp /* 133d07a6d3fSPoul-Henning Kamp * Dereference the reference we just created. This assumes 134d07a6d3fSPoul-Henning Kamp * that the object is associated with the vp. 135d07a6d3fSPoul-Henning Kamp */ 136d07a6d3fSPoul-Henning Kamp VM_OBJECT_LOCK(object); 137d07a6d3fSPoul-Henning Kamp object->ref_count--; 138d07a6d3fSPoul-Henning Kamp VM_OBJECT_UNLOCK(object); 139d07a6d3fSPoul-Henning Kamp vrele(vp); 140d07a6d3fSPoul-Henning Kamp 141d07a6d3fSPoul-Henning Kamp KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object")); 142d07a6d3fSPoul-Henning Kamp 143d07a6d3fSPoul-Henning Kamp return (0); 144d07a6d3fSPoul-Henning Kamp } 145d07a6d3fSPoul-Henning Kamp 1467146d6cbSPoul-Henning Kamp void 1477146d6cbSPoul-Henning Kamp vnode_destroy_vobject(struct vnode *vp) 1487146d6cbSPoul-Henning Kamp { 1497146d6cbSPoul-Henning Kamp struct vm_object *obj; 1507146d6cbSPoul-Henning Kamp 1517146d6cbSPoul-Henning Kamp obj = vp->v_object; 1527146d6cbSPoul-Henning Kamp if (obj == NULL) 1537146d6cbSPoul-Henning Kamp return; 15457fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject"); 1557146d6cbSPoul-Henning Kamp VM_OBJECT_LOCK(obj); 1567146d6cbSPoul-Henning Kamp if (obj->ref_count == 0) { 1577146d6cbSPoul-Henning Kamp /* 1587146d6cbSPoul-Henning Kamp * vclean() may be called twice. The first time 1597146d6cbSPoul-Henning Kamp * removes the primary reference to the object, 1607146d6cbSPoul-Henning Kamp * the second time goes one further and is a 1617146d6cbSPoul-Henning Kamp * special-case to terminate the object. 1627146d6cbSPoul-Henning Kamp * 1637146d6cbSPoul-Henning Kamp * don't double-terminate the object 1647146d6cbSPoul-Henning Kamp */ 1657146d6cbSPoul-Henning Kamp if ((obj->flags & OBJ_DEAD) == 0) 1667146d6cbSPoul-Henning Kamp vm_object_terminate(obj); 1677146d6cbSPoul-Henning Kamp else 1687146d6cbSPoul-Henning Kamp VM_OBJECT_UNLOCK(obj); 1697146d6cbSPoul-Henning Kamp } else { 1707146d6cbSPoul-Henning Kamp /* 1717146d6cbSPoul-Henning Kamp * Woe to the process that tries to page now :-). 1727146d6cbSPoul-Henning Kamp */ 1737146d6cbSPoul-Henning Kamp vm_pager_deallocate(obj); 1747146d6cbSPoul-Henning Kamp VM_OBJECT_UNLOCK(obj); 1757146d6cbSPoul-Henning Kamp } 1766e4b2820SJeff Roberson vp->v_object = NULL; 1777146d6cbSPoul-Henning Kamp } 1787146d6cbSPoul-Henning Kamp 1797146d6cbSPoul-Henning Kamp 180df8bae1dSRodney W. Grimes /* 181df8bae1dSRodney W. Grimes * Allocate (or lookup) pager for a vnode. 182df8bae1dSRodney W. Grimes * Handle is a vnode pointer. 183990ab7adSAlan Cox * 184990ab7adSAlan Cox * MPSAFE 185df8bae1dSRodney W. Grimes */ 18624a1cce3SDavid Greenman vm_object_t 1876cde7a16SDavid Greenman vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 188b9dcd593SBruce Evans vm_ooffset_t offset) 189df8bae1dSRodney W. Grimes { 19006cb7259SDavid Greenman vm_object_t object; 191df8bae1dSRodney W. Grimes struct vnode *vp; 192df8bae1dSRodney W. Grimes 193df8bae1dSRodney W. Grimes /* 194df8bae1dSRodney W. Grimes * Pageout to vnode, no can do yet. 195df8bae1dSRodney W. Grimes */ 196df8bae1dSRodney W. Grimes if (handle == NULL) 197df8bae1dSRodney W. Grimes return (NULL); 198df8bae1dSRodney W. Grimes 199df8bae1dSRodney W. Grimes vp = (struct vnode *) handle; 20039d38f93SDavid Greenman 20157fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_pager_alloc"); 20263e7e60dSJeff Roberson 20339d38f93SDavid Greenman /* 20439d38f93SDavid Greenman * If the object is being terminated, wait for it to 20539d38f93SDavid Greenman * go away. 20639d38f93SDavid Greenman */ 2071ca58953SAlan Cox while ((object = vp->v_object) != NULL) { 2081ca58953SAlan Cox VM_OBJECT_LOCK(object); 2091ca58953SAlan Cox if ((object->flags & OBJ_DEAD) == 0) 2101ca58953SAlan Cox break; 21119187819SAlan Cox vm_object_set_flag(object, OBJ_DISCONNECTWNT); 2121ca58953SAlan Cox msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0); 21324a1cce3SDavid Greenman } 2140d94caffSDavid Greenman 2152be70f79SJohn Dyson if (vp->v_usecount == 0) 2162be70f79SJohn Dyson panic("vnode_pager_alloc: no vnode reference"); 2172be70f79SJohn Dyson 21824a1cce3SDavid Greenman if (object == NULL) { 219df8bae1dSRodney W. Grimes /* 220df8bae1dSRodney W. Grimes * And an object of the appropriate size 221df8bae1dSRodney W. Grimes */ 2226cde7a16SDavid Greenman object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 223bbc0ec52SDavid Greenman 2246cde7a16SDavid Greenman object->un_pager.vnp.vnp_size = size; 22526f9a767SRodney W. Grimes 22624a1cce3SDavid Greenman object->handle = handle; 227ed4fe4f4SJeff Roberson if (VFS_NEEDSGIANT(vp->v_mount)) 228ed4fe4f4SJeff Roberson vm_object_set_flag(object, OBJ_NEEDGIANT); 22924a1cce3SDavid Greenman vp->v_object = object; 230df8bae1dSRodney W. Grimes } else { 23195e5e988SJohn Dyson object->ref_count++; 2321ca58953SAlan Cox VM_OBJECT_UNLOCK(object); 233df8bae1dSRodney W. Grimes } 2347747c038SJeff Roberson vref(vp); 23524a1cce3SDavid Greenman return (object); 236df8bae1dSRodney W. Grimes } 237df8bae1dSRodney W. Grimes 238658ad5ffSAlan Cox /* 239658ad5ffSAlan Cox * The object must be locked. 240658ad5ffSAlan Cox */ 241f708ef1bSPoul-Henning Kamp static void 24224a1cce3SDavid Greenman vnode_pager_dealloc(object) 2430d94caffSDavid Greenman vm_object_t object; 24424a1cce3SDavid Greenman { 24554d92145SMatthew Dillon struct vnode *vp = object->handle; 246df8bae1dSRodney W. Grimes 24724a1cce3SDavid Greenman if (vp == NULL) 24824a1cce3SDavid Greenman panic("vnode_pager_dealloc: pager already dealloced"); 24924a1cce3SDavid Greenman 250658ad5ffSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 25166095752SJohn Dyson vm_object_pip_wait(object, "vnpdea"); 25224a1cce3SDavid Greenman 25324a1cce3SDavid Greenman object->handle = NULL; 25495461b45SJohn Dyson object->type = OBJT_DEAD; 25519187819SAlan Cox if (object->flags & OBJ_DISCONNECTWNT) { 25619187819SAlan Cox vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 25719187819SAlan Cox wakeup(object); 25819187819SAlan Cox } 25957fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc"); 260aa2cabb9SDavid Greenman vp->v_object = NULL; 26135764be3SPoul-Henning Kamp vp->v_vflag &= ~VV_TEXT; 262df8bae1dSRodney W. Grimes } 26326f9a767SRodney W. Grimes 264f708ef1bSPoul-Henning Kamp static boolean_t 265a316d390SJohn Dyson vnode_pager_haspage(object, pindex, before, after) 26624a1cce3SDavid Greenman vm_object_t object; 267a316d390SJohn Dyson vm_pindex_t pindex; 26824a1cce3SDavid Greenman int *before; 26924a1cce3SDavid Greenman int *after; 270df8bae1dSRodney W. Grimes { 27124a1cce3SDavid Greenman struct vnode *vp = object->handle; 27298b0c789SPoul-Henning Kamp daddr_t bn; 2733af76890SPoul-Henning Kamp int err; 274170db9c6SJohn Dyson daddr_t reqblock; 2752c4488fcSJohn Dyson int poff; 2762c4488fcSJohn Dyson int bsize; 277d63596ceSJohn Dyson int pagesperblock, blocksperpage; 278ae51ff11SJeff Roberson int vfslocked; 279df8bae1dSRodney W. Grimes 280f29ba63eSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 28124579ca1SMatthew Dillon /* 28224579ca1SMatthew Dillon * If no vp or vp is doomed or marked transparent to VM, we do not 28324579ca1SMatthew Dillon * have the page. 28424579ca1SMatthew Dillon */ 285b73f64c4SJeff Roberson if (vp == NULL || vp->v_iflag & VI_DOOMED) 28647221757SJohn Dyson return FALSE; 287df8bae1dSRodney W. Grimes /* 288b73f64c4SJeff Roberson * If the offset is beyond end of file we do 2890d94caffSDavid Greenman * not have the page. 290df8bae1dSRodney W. Grimes */ 291b73f64c4SJeff Roberson if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 2924abc71c0SDavid Greenman return FALSE; 293df8bae1dSRodney W. Grimes 294eed2d59bSDavid Greenman bsize = vp->v_mount->mnt_stat.f_iosize; 295170db9c6SJohn Dyson pagesperblock = bsize / PAGE_SIZE; 296d63596ceSJohn Dyson blocksperpage = 0; 297d63596ceSJohn Dyson if (pagesperblock > 0) { 298a316d390SJohn Dyson reqblock = pindex / pagesperblock; 299d63596ceSJohn Dyson } else { 300d63596ceSJohn Dyson blocksperpage = (PAGE_SIZE / bsize); 301d63596ceSJohn Dyson reqblock = pindex * blocksperpage; 302d63596ceSJohn Dyson } 303f29ba63eSAlan Cox VM_OBJECT_UNLOCK(object); 304ae51ff11SJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 305ec794849SPoul-Henning Kamp err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 306ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 307f29ba63eSAlan Cox VM_OBJECT_LOCK(object); 3080d94caffSDavid Greenman if (err) 30924a1cce3SDavid Greenman return TRUE; 3106eab77f2SJohn Dyson if (bn == -1) 311ced399eeSJohn Dyson return FALSE; 312d63596ceSJohn Dyson if (pagesperblock > 0) { 313a316d390SJohn Dyson poff = pindex - (reqblock * pagesperblock); 314170db9c6SJohn Dyson if (before) { 315170db9c6SJohn Dyson *before *= pagesperblock; 316170db9c6SJohn Dyson *before += poff; 317170db9c6SJohn Dyson } 318170db9c6SJohn Dyson if (after) { 319b1fc01b7SJohn Dyson int numafter; 320170db9c6SJohn Dyson *after *= pagesperblock; 321b1fc01b7SJohn Dyson numafter = pagesperblock - (poff + 1); 32247e151ddSRobert Drehmel if (IDX_TO_OFF(pindex + numafter) > 32347e151ddSRobert Drehmel object->un_pager.vnp.vnp_size) { 32447e151ddSRobert Drehmel numafter = 32547e151ddSRobert Drehmel OFF_TO_IDX(object->un_pager.vnp.vnp_size) - 32647e151ddSRobert Drehmel pindex; 327b1fc01b7SJohn Dyson } 328b1fc01b7SJohn Dyson *after += numafter; 329170db9c6SJohn Dyson } 330d63596ceSJohn Dyson } else { 331d63596ceSJohn Dyson if (before) { 332d63596ceSJohn Dyson *before /= blocksperpage; 333d63596ceSJohn Dyson } 334d63596ceSJohn Dyson 335d63596ceSJohn Dyson if (after) { 336d63596ceSJohn Dyson *after /= blocksperpage; 337d63596ceSJohn Dyson } 338d63596ceSJohn Dyson } 339ced399eeSJohn Dyson return TRUE; 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes 342df8bae1dSRodney W. Grimes /* 343df8bae1dSRodney W. Grimes * Lets the VM system know about a change in size for a file. 34424a1cce3SDavid Greenman * We adjust our own internal size and flush any cached pages in 345df8bae1dSRodney W. Grimes * the associated object that are affected by the size change. 346df8bae1dSRodney W. Grimes * 347df8bae1dSRodney W. Grimes * Note: this routine may be invoked as a result of a pager put 348df8bae1dSRodney W. Grimes * operation (possibly at object termination time), so we must be careful. 349df8bae1dSRodney W. Grimes */ 350df8bae1dSRodney W. Grimes void 351df8bae1dSRodney W. Grimes vnode_pager_setsize(vp, nsize) 352df8bae1dSRodney W. Grimes struct vnode *vp; 353a316d390SJohn Dyson vm_ooffset_t nsize; 354df8bae1dSRodney W. Grimes { 3552a8f9ab5SAlan Cox vm_object_t object; 3562a8f9ab5SAlan Cox vm_page_t m; 357c576d121SLuoqi Chen vm_pindex_t nobjsize; 358df8bae1dSRodney W. Grimes 3592a8f9ab5SAlan Cox if ((object = vp->v_object) == NULL) 360df8bae1dSRodney W. Grimes return; 3612a8f9ab5SAlan Cox VM_OBJECT_LOCK(object); 3622a8f9ab5SAlan Cox if (nsize == object->un_pager.vnp.vnp_size) { 363df8bae1dSRodney W. Grimes /* 364df8bae1dSRodney W. Grimes * Hasn't changed size 365df8bae1dSRodney W. Grimes */ 3662a8f9ab5SAlan Cox VM_OBJECT_UNLOCK(object); 367df8bae1dSRodney W. Grimes return; 3682a8f9ab5SAlan Cox } 369c576d121SLuoqi Chen nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 3702a8f9ab5SAlan Cox if (nsize < object->un_pager.vnp.vnp_size) { 371df8bae1dSRodney W. Grimes /* 372bbc0ec52SDavid Greenman * File has shrunk. Toss any cached pages beyond the new EOF. 373df8bae1dSRodney W. Grimes */ 3742a8f9ab5SAlan Cox if (nobjsize < object->size) 375c576d121SLuoqi Chen vm_object_page_remove(object, nobjsize, object->size, 376c576d121SLuoqi Chen FALSE); 377bbc0ec52SDavid Greenman /* 378bbc0ec52SDavid Greenman * this gets rid of garbage at the end of a page that is now 3793ebeaf59SMatthew Dillon * only partially backed by the vnode. 3803ebeaf59SMatthew Dillon * 3813ebeaf59SMatthew Dillon * XXX for some reason (I don't know yet), if we take a 3823ebeaf59SMatthew Dillon * completely invalid page and mark it partially valid 3833ebeaf59SMatthew Dillon * it can screw up NFS reads, so we don't allow the case. 384bbc0ec52SDavid Greenman */ 3852a8f9ab5SAlan Cox if ((nsize & PAGE_MASK) && 3861b26eb10SAlan Cox (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 3871b26eb10SAlan Cox m->valid != 0) { 3882b6b0df7SMatthew Dillon int base = (int)nsize & PAGE_MASK; 3892b6b0df7SMatthew Dillon int size = PAGE_SIZE - base; 3902b6b0df7SMatthew Dillon 3912b6b0df7SMatthew Dillon /* 3922b6b0df7SMatthew Dillon * Clear out partial-page garbage in case 3932b6b0df7SMatthew Dillon * the page has been mapped. 3942b6b0df7SMatthew Dillon */ 395fff6062aSAlan Cox pmap_zero_page_area(m, base, size); 3962b6b0df7SMatthew Dillon 3972b6b0df7SMatthew Dillon /* 3983ebeaf59SMatthew Dillon * XXX work around SMP data integrity race 3993ebeaf59SMatthew Dillon * by unmapping the page from user processes. 4003ebeaf59SMatthew Dillon * The garbage we just cleared may be mapped 4013ebeaf59SMatthew Dillon * to a user process running on another cpu 4023ebeaf59SMatthew Dillon * and this code is not running through normal 4033ebeaf59SMatthew Dillon * I/O channels which handle SMP issues for 4043ebeaf59SMatthew Dillon * us, so unmap page to synchronize all cpus. 4053ebeaf59SMatthew Dillon * 4063ebeaf59SMatthew Dillon * XXX should vm_pager_unmap_page() have 4073ebeaf59SMatthew Dillon * dealt with this? 4083ebeaf59SMatthew Dillon */ 4091b26eb10SAlan Cox vm_page_lock_queues(); 4104fec79beSAlan Cox pmap_remove_all(m); 4113ebeaf59SMatthew Dillon 4123ebeaf59SMatthew Dillon /* 4132b6b0df7SMatthew Dillon * Clear out partial-page dirty bits. This 4142b6b0df7SMatthew Dillon * has the side effect of setting the valid 4152b6b0df7SMatthew Dillon * bits, but that is ok. There are a bunch 4162b6b0df7SMatthew Dillon * of places in the VM system where we expected 4172b6b0df7SMatthew Dillon * m->dirty == VM_PAGE_BITS_ALL. The file EOF 4182b6b0df7SMatthew Dillon * case is one of them. If the page is still 4192b6b0df7SMatthew Dillon * partially dirty, make it fully dirty. 4203ebeaf59SMatthew Dillon * 4213ebeaf59SMatthew Dillon * note that we do not clear out the valid 4223ebeaf59SMatthew Dillon * bits. This would prevent bogus_page 4233ebeaf59SMatthew Dillon * replacement from working properly. 4242b6b0df7SMatthew Dillon */ 4252b6b0df7SMatthew Dillon vm_page_set_validclean(m, base, size); 4262b6b0df7SMatthew Dillon if (m->dirty != 0) 4272b6b0df7SMatthew Dillon m->dirty = VM_PAGE_BITS_ALL; 4282a8f9ab5SAlan Cox vm_page_unlock_queues(); 429bbc0ec52SDavid Greenman } 430bbc0ec52SDavid Greenman } 431a316d390SJohn Dyson object->un_pager.vnp.vnp_size = nsize; 432c576d121SLuoqi Chen object->size = nobjsize; 4332a8f9ab5SAlan Cox VM_OBJECT_UNLOCK(object); 434df8bae1dSRodney W. Grimes } 435df8bae1dSRodney W. Grimes 43626f9a767SRodney W. Grimes /* 43726f9a767SRodney W. Grimes * calculate the linear (byte) disk address of specified virtual 43826f9a767SRodney W. Grimes * file address 43926f9a767SRodney W. Grimes */ 440bff76343SAlan Cox static int 441bff76343SAlan Cox vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 442bff76343SAlan Cox int *run) 44326f9a767SRodney W. Grimes { 44426f9a767SRodney W. Grimes int bsize; 44526f9a767SRodney W. Grimes int err; 446a316d390SJohn Dyson daddr_t vblock; 447f3aad9a6SBjoern A. Zeeb daddr_t voffset; 44826f9a767SRodney W. Grimes 4492ad036b6SAlan Cox if (address < 0) 4500d94caffSDavid Greenman return -1; 4510d94caffSDavid Greenman 452b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 4532c4488fcSJohn Dyson return -1; 4542c4488fcSJohn Dyson 45526f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 45626f9a767SRodney W. Grimes vblock = address / bsize; 45726f9a767SRodney W. Grimes voffset = address % bsize; 45826f9a767SRodney W. Grimes 459bff76343SAlan Cox err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 460bff76343SAlan Cox if (err == 0) { 461bff76343SAlan Cox if (*rtaddress != -1) 462bff76343SAlan Cox *rtaddress += voffset / DEV_BSIZE; 463efc68ce1SDavid Greenman if (run) { 464efc68ce1SDavid Greenman *run += 1; 465efc68ce1SDavid Greenman *run *= bsize/PAGE_SIZE; 466efc68ce1SDavid Greenman *run -= voffset/PAGE_SIZE; 467efc68ce1SDavid Greenman } 468efc68ce1SDavid Greenman } 46926f9a767SRodney W. Grimes 470bff76343SAlan Cox return (err); 47126f9a767SRodney W. Grimes } 47226f9a767SRodney W. Grimes 47326f9a767SRodney W. Grimes /* 47426f9a767SRodney W. Grimes * small block filesystem vnode pager input 47526f9a767SRodney W. Grimes */ 476f708ef1bSPoul-Henning Kamp static int 47724a1cce3SDavid Greenman vnode_pager_input_smlfs(object, m) 47824a1cce3SDavid Greenman vm_object_t object; 47926f9a767SRodney W. Grimes vm_page_t m; 48026f9a767SRodney W. Grimes { 48126f9a767SRodney W. Grimes int i; 4829c83534dSPoul-Henning Kamp struct vnode *vp; 4839c83534dSPoul-Henning Kamp struct bufobj *bo; 48426f9a767SRodney W. Grimes struct buf *bp; 4859e0ddbd0SAlan Cox struct sf_buf *sf; 486f3aad9a6SBjoern A. Zeeb daddr_t fileaddr; 48726f9a767SRodney W. Grimes vm_offset_t bsize; 48826f9a767SRodney W. Grimes int error = 0; 48926f9a767SRodney W. Grimes 49024a1cce3SDavid Greenman vp = object->handle; 491b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 4922c4488fcSJohn Dyson return VM_PAGER_BAD; 4932c4488fcSJohn Dyson 49426f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 4950bdb7528SDavid Greenman 4969c83534dSPoul-Henning Kamp VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 49726f9a767SRodney W. Grimes 4989e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 49926f9a767SRodney W. Grimes 50026f9a767SRodney W. Grimes for (i = 0; i < PAGE_SIZE / bsize; i++) { 50133c67741SMatthew Dillon vm_ooffset_t address; 502bbc0ec52SDavid Greenman 503897a45efSDmitrij Tejblum if (vm_page_bits(i * bsize, bsize) & m->valid) 50426f9a767SRodney W. Grimes continue; 50526f9a767SRodney W. Grimes 50633c67741SMatthew Dillon address = IDX_TO_OFF(m->pindex) + i * bsize; 50733c67741SMatthew Dillon if (address >= object->un_pager.vnp.vnp_size) { 50833c67741SMatthew Dillon fileaddr = -1; 50933c67741SMatthew Dillon } else { 510bff76343SAlan Cox error = vnode_pager_addr(vp, address, &fileaddr, NULL); 511bff76343SAlan Cox if (error) 512bff76343SAlan Cox break; 51333c67741SMatthew Dillon } 51426f9a767SRodney W. Grimes if (fileaddr != -1) { 5151c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 51626f9a767SRodney W. Grimes 51726f9a767SRodney W. Grimes /* build a minimal buffer header */ 51821144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 5196a4b5823SPoul-Henning Kamp bp->b_iodone = bdone; 520bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 521bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 522a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 523a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 5249e0ddbd0SAlan Cox bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 525187f0071SDavid Greenman bp->b_blkno = fileaddr; 5269c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 52726f9a767SRodney W. Grimes bp->b_bcount = bsize; 52826f9a767SRodney W. Grimes bp->b_bufsize = bsize; 5292b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 5304f12e0acSSuleiman Souhlal atomic_add_int(&runningbufspace, bp->b_runningbufspace); 53126f9a767SRodney W. Grimes 53226f9a767SRodney W. Grimes /* do the input */ 5332c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 534b792bebeSPoul-Henning Kamp bstrategy(bp); 53526f9a767SRodney W. Grimes 5366a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnsrd"); 5376a4b5823SPoul-Henning Kamp 538c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 53926f9a767SRodney W. Grimes error = EIO; 54026f9a767SRodney W. Grimes 54126f9a767SRodney W. Grimes /* 54226f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 54326f9a767SRodney W. Grimes */ 5449c83534dSPoul-Henning Kamp pbrelbo(bp); 5451c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 54626f9a767SRodney W. Grimes if (error) 54726f9a767SRodney W. Grimes break; 5480d94caffSDavid Greenman 5492bf43e43SAlan Cox VM_OBJECT_LOCK(object); 550178949e0SAlan Cox vm_page_lock_queues(); 551aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 552178949e0SAlan Cox vm_page_unlock_queues(); 5532bf43e43SAlan Cox VM_OBJECT_UNLOCK(object); 55426f9a767SRodney W. Grimes } else { 5552bf43e43SAlan Cox VM_OBJECT_LOCK(object); 556178949e0SAlan Cox vm_page_lock_queues(); 557aa8de40aSPoul-Henning Kamp vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 558178949e0SAlan Cox vm_page_unlock_queues(); 5592bf43e43SAlan Cox VM_OBJECT_UNLOCK(object); 5609e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 56126f9a767SRodney W. Grimes } 56226f9a767SRodney W. Grimes } 5639e0ddbd0SAlan Cox sf_buf_free(sf); 56485e01243SAlan Cox vm_page_lock_queues(); 5650385347cSPeter Wemm pmap_clear_modify(m); 56685e01243SAlan Cox vm_page_unlock_queues(); 56726f9a767SRodney W. Grimes if (error) { 568a83c285cSDavid Greenman return VM_PAGER_ERROR; 56926f9a767SRodney W. Grimes } 57026f9a767SRodney W. Grimes return VM_PAGER_OK; 57126f9a767SRodney W. Grimes 57226f9a767SRodney W. Grimes } 57326f9a767SRodney W. Grimes 57426f9a767SRodney W. Grimes 57526f9a767SRodney W. Grimes /* 576475e8cc3SPoul-Henning Kamp * old style vnode pager input routine 57726f9a767SRodney W. Grimes */ 578f708ef1bSPoul-Henning Kamp static int 57924a1cce3SDavid Greenman vnode_pager_input_old(object, m) 58024a1cce3SDavid Greenman vm_object_t object; 58126f9a767SRodney W. Grimes vm_page_t m; 58226f9a767SRodney W. Grimes { 583df8bae1dSRodney W. Grimes struct uio auio; 584df8bae1dSRodney W. Grimes struct iovec aiov; 58526f9a767SRodney W. Grimes int error; 58626f9a767SRodney W. Grimes int size; 5879e0ddbd0SAlan Cox struct sf_buf *sf; 588342a1480SJohn Baldwin struct vnode *vp; 589df8bae1dSRodney W. Grimes 59052051abcSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 59126f9a767SRodney W. Grimes error = 0; 592bbc0ec52SDavid Greenman 593df8bae1dSRodney W. Grimes /* 59426f9a767SRodney W. Grimes * Return failure if beyond current EOF 59526f9a767SRodney W. Grimes */ 596a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 59726f9a767SRodney W. Grimes return VM_PAGER_BAD; 59826f9a767SRodney W. Grimes } else { 59926f9a767SRodney W. Grimes size = PAGE_SIZE; 600a316d390SJohn Dyson if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 601a316d390SJohn Dyson size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 60252051abcSAlan Cox vp = object->handle; 60352051abcSAlan Cox VM_OBJECT_UNLOCK(object); 6040bdb7528SDavid Greenman 60526f9a767SRodney W. Grimes /* 606df8bae1dSRodney W. Grimes * Allocate a kernel virtual address and initialize so that 607df8bae1dSRodney W. Grimes * we can use VOP_READ/WRITE routines. 608df8bae1dSRodney W. Grimes */ 6099e0ddbd0SAlan Cox sf = sf_buf_alloc(m, 0); 6100bdb7528SDavid Greenman 6119e0ddbd0SAlan Cox aiov.iov_base = (caddr_t)sf_buf_kva(sf); 612df8bae1dSRodney W. Grimes aiov.iov_len = size; 613df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 614df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 615a316d390SJohn Dyson auio.uio_offset = IDX_TO_OFF(m->pindex); 616df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 61726f9a767SRodney W. Grimes auio.uio_rw = UIO_READ; 618df8bae1dSRodney W. Grimes auio.uio_resid = size; 619b40ce416SJulian Elischer auio.uio_td = curthread; 62026f9a767SRodney W. Grimes 621a854ed98SJohn Baldwin error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 622df8bae1dSRodney W. Grimes if (!error) { 62354d92145SMatthew Dillon int count = size - auio.uio_resid; 624df8bae1dSRodney W. Grimes 625df8bae1dSRodney W. Grimes if (count == 0) 626df8bae1dSRodney W. Grimes error = EINVAL; 62726f9a767SRodney W. Grimes else if (count != PAGE_SIZE) 6289e0ddbd0SAlan Cox bzero((caddr_t)sf_buf_kva(sf) + count, 6299e0ddbd0SAlan Cox PAGE_SIZE - count); 630df8bae1dSRodney W. Grimes } 6319e0ddbd0SAlan Cox sf_buf_free(sf); 6321b26eb10SAlan Cox 6331b26eb10SAlan Cox VM_OBJECT_LOCK(object); 634df8bae1dSRodney W. Grimes } 63585e01243SAlan Cox vm_page_lock_queues(); 6360385347cSPeter Wemm pmap_clear_modify(m); 6372c28a105SAlan Cox vm_page_undirty(m); 6381b26eb10SAlan Cox vm_page_unlock_queues(); 6396e3a3f38SRobert V. Baron if (!error) 6406e3a3f38SRobert V. Baron m->valid = VM_PAGE_BITS_ALL; 641a83c285cSDavid Greenman return error ? VM_PAGER_ERROR : VM_PAGER_OK; 64226f9a767SRodney W. Grimes } 64326f9a767SRodney W. Grimes 64426f9a767SRodney W. Grimes /* 64526f9a767SRodney W. Grimes * generic vnode pager input routine 64626f9a767SRodney W. Grimes */ 647170db9c6SJohn Dyson 648ce75f2c3SMike Smith /* 64923955314SAlfred Perlstein * Local media VFS's that do not implement their own VOP_GETPAGES 65047e151ddSRobert Drehmel * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 65147e151ddSRobert Drehmel * to implement the previous behaviour. 652ce75f2c3SMike Smith * 653ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 654ce75f2c3SMike Smith * backing vp's VOP_GETPAGES. 655ce75f2c3SMike Smith */ 656f708ef1bSPoul-Henning Kamp static int 65724a1cce3SDavid Greenman vnode_pager_getpages(object, m, count, reqpage) 65826f9a767SRodney W. Grimes vm_object_t object; 65924a1cce3SDavid Greenman vm_page_t *m; 66024a1cce3SDavid Greenman int count; 66124a1cce3SDavid Greenman int reqpage; 66224a1cce3SDavid Greenman { 663170db9c6SJohn Dyson int rtval; 664170db9c6SJohn Dyson struct vnode *vp; 66586ffbd76SMike Smith int bytes = count * PAGE_SIZE; 666ae51ff11SJeff Roberson int vfslocked; 66795e5e988SJohn Dyson 668170db9c6SJohn Dyson vp = object->handle; 6698630c117SAlan Cox VM_OBJECT_UNLOCK(object); 670ae51ff11SJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 67186ffbd76SMike Smith rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 67223955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 67323955314SAlfred Perlstein ("vnode_pager: FS getpages not implemented\n")); 674ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 6758630c117SAlan Cox VM_OBJECT_LOCK(object); 676170db9c6SJohn Dyson return rtval; 677170db9c6SJohn Dyson } 678170db9c6SJohn Dyson 679ce75f2c3SMike Smith /* 680ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 681ce75f2c3SMike Smith * own vnodes if they fail to implement VOP_GETPAGES. 682ce75f2c3SMike Smith */ 683ce75f2c3SMike Smith int 684ce75f2c3SMike Smith vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 685ce75f2c3SMike Smith struct vnode *vp; 686170db9c6SJohn Dyson vm_page_t *m; 687ce75f2c3SMike Smith int bytecount; 688170db9c6SJohn Dyson int reqpage; 689170db9c6SJohn Dyson { 690ce75f2c3SMike Smith vm_object_t object; 691a316d390SJohn Dyson vm_offset_t kva; 6928f9110f6SJohn Dyson off_t foff, tfoff, nextoff; 693f3aad9a6SBjoern A. Zeeb int i, j, size, bsize, first; 694f4f83da0SAlan Cox daddr_t firstaddr, reqblock; 6959c83534dSPoul-Henning Kamp struct bufobj *bo; 696efc68ce1SDavid Greenman int runpg; 697efc68ce1SDavid Greenman int runend; 6980bdb7528SDavid Greenman struct buf *bp; 699ce75f2c3SMike Smith int count; 7001de11f1aSAlan Cox int error; 70126f9a767SRodney W. Grimes 702ce75f2c3SMike Smith object = vp->v_object; 703ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 704ce75f2c3SMike Smith 7059c83534dSPoul-Henning Kamp KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 7069c83534dSPoul-Henning Kamp ("vnode_pager_generic_getpages does not support devices")); 707b73f64c4SJeff Roberson if (vp->v_iflag & VI_DOOMED) 7082c4488fcSJohn Dyson return VM_PAGER_BAD; 7092c4488fcSJohn Dyson 71026f9a767SRodney W. Grimes bsize = vp->v_mount->mnt_stat.f_iosize; 71126f9a767SRodney W. Grimes 71226f9a767SRodney W. Grimes /* get the UNDERLYING device for the file with VOP_BMAP() */ 713bbc0ec52SDavid Greenman 71426f9a767SRodney W. Grimes /* 715bbc0ec52SDavid Greenman * originally, we did not check for an error return value -- assuming 716bbc0ec52SDavid Greenman * an fs always has a bmap entry point -- that assumption is wrong!!! 71726f9a767SRodney W. Grimes */ 718a316d390SJohn Dyson foff = IDX_TO_OFF(m[reqpage]->pindex); 719bbc0ec52SDavid Greenman 72026f9a767SRodney W. Grimes /* 72116f62314SDavid Greenman * if we can't bmap, use old VOP code 72226f9a767SRodney W. Grimes */ 7231de11f1aSAlan Cox error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL); 7241de11f1aSAlan Cox if (error == EOPNOTSUPP) { 72531953be9SAlan Cox VM_OBJECT_LOCK(object); 726e43c2eabSAlan Cox vm_page_lock_queues(); 727e43c2eabSAlan Cox for (i = 0; i < count; i++) 728e43c2eabSAlan Cox if (i != reqpage) 729d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 730e43c2eabSAlan Cox vm_page_unlock_queues(); 731b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodein); 732b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodepgsin); 73352051abcSAlan Cox error = vnode_pager_input_old(object, m[reqpage]); 73452051abcSAlan Cox VM_OBJECT_UNLOCK(object); 73552051abcSAlan Cox return (error); 7361de11f1aSAlan Cox } else if (error != 0) { 7371de11f1aSAlan Cox VM_OBJECT_LOCK(object); 7381de11f1aSAlan Cox vm_page_lock_queues(); 7391de11f1aSAlan Cox for (i = 0; i < count; i++) 7401de11f1aSAlan Cox if (i != reqpage) 7411de11f1aSAlan Cox vm_page_free(m[i]); 7421de11f1aSAlan Cox vm_page_unlock_queues(); 7431de11f1aSAlan Cox VM_OBJECT_UNLOCK(object); 7441de11f1aSAlan Cox return (VM_PAGER_ERROR); 745bbc0ec52SDavid Greenman 74626f9a767SRodney W. Grimes /* 74726f9a767SRodney W. Grimes * if the blocksize is smaller than a page size, then use 74826f9a767SRodney W. Grimes * special small filesystem code. NFS sometimes has a small 74926f9a767SRodney W. Grimes * blocksize, but it can handle large reads itself. 75026f9a767SRodney W. Grimes */ 75126f9a767SRodney W. Grimes } else if ((PAGE_SIZE / bsize) > 1 && 752500b04a2SBruce Evans (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 75331953be9SAlan Cox VM_OBJECT_LOCK(object); 754e43c2eabSAlan Cox vm_page_lock_queues(); 755e43c2eabSAlan Cox for (i = 0; i < count; i++) 756e43c2eabSAlan Cox if (i != reqpage) 757d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 758e43c2eabSAlan Cox vm_page_unlock_queues(); 75931953be9SAlan Cox VM_OBJECT_UNLOCK(object); 760b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodein); 761b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodepgsin); 76224a1cce3SDavid Greenman return vnode_pager_input_smlfs(object, m[reqpage]); 76326f9a767SRodney W. Grimes } 7648d17e694SJulian Elischer 76526f9a767SRodney W. Grimes /* 7668d17e694SJulian Elischer * If we have a completely valid page available to us, we can 7678d17e694SJulian Elischer * clean up and return. Otherwise we have to re-read the 7688d17e694SJulian Elischer * media. 7690d94caffSDavid Greenman */ 77031953be9SAlan Cox VM_OBJECT_LOCK(object); 7718b575f6cSAlan Cox if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 772e43c2eabSAlan Cox vm_page_lock_queues(); 773e43c2eabSAlan Cox for (i = 0; i < count; i++) 7740d94caffSDavid Greenman if (i != reqpage) 775d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 776e43c2eabSAlan Cox vm_page_unlock_queues(); 77731953be9SAlan Cox VM_OBJECT_UNLOCK(object); 7780d94caffSDavid Greenman return VM_PAGER_OK; 779f4f83da0SAlan Cox } else if (reqblock == -1) { 780f4f83da0SAlan Cox pmap_zero_page(m[reqpage]); 781f4f83da0SAlan Cox vm_page_undirty(m[reqpage]); 782f4f83da0SAlan Cox m[reqpage]->valid = VM_PAGE_BITS_ALL; 783f4f83da0SAlan Cox vm_page_lock_queues(); 784f4f83da0SAlan Cox for (i = 0; i < count; i++) 785f4f83da0SAlan Cox if (i != reqpage) 786f4f83da0SAlan Cox vm_page_free(m[i]); 787f4f83da0SAlan Cox vm_page_unlock_queues(); 788f4f83da0SAlan Cox VM_OBJECT_UNLOCK(object); 789f4f83da0SAlan Cox 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) { 803bff76343SAlan Cox if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr, 804bff76343SAlan Cox &runpg) != 0) { 805bff76343SAlan Cox VM_OBJECT_LOCK(object); 806bff76343SAlan Cox vm_page_lock_queues(); 807bff76343SAlan Cox for (; i < count; i++) 808bff76343SAlan Cox if (i != reqpage) 809bff76343SAlan Cox vm_page_free(m[i]); 810bff76343SAlan Cox vm_page_unlock_queues(); 811bff76343SAlan Cox VM_OBJECT_UNLOCK(object); 812bff76343SAlan Cox return (VM_PAGER_ERROR); 813bff76343SAlan Cox } 814efc68ce1SDavid Greenman if (firstaddr == -1) { 81531953be9SAlan Cox VM_OBJECT_LOCK(object); 81624a1cce3SDavid Greenman if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 817f3aad9a6SBjoern A. Zeeb panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx", 818f3aad9a6SBjoern A. Zeeb (intmax_t)firstaddr, (uintmax_t)(foff >> 32), 819bf1001faSMaxime Henrion (uintmax_t)foff, 820bf1001faSMaxime Henrion (uintmax_t) 821fc62ef1fSBruce Evans (object->un_pager.vnp.vnp_size >> 32), 822bf1001faSMaxime Henrion (uintmax_t)object->un_pager.vnp.vnp_size); 823efc68ce1SDavid Greenman } 824e43c2eabSAlan Cox vm_page_lock_queues(); 825d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 826e43c2eabSAlan Cox vm_page_unlock_queues(); 82731953be9SAlan Cox VM_OBJECT_UNLOCK(object); 828efc68ce1SDavid Greenman runend = i + 1; 829efc68ce1SDavid Greenman first = runend; 830efc68ce1SDavid Greenman continue; 831efc68ce1SDavid Greenman } 832efc68ce1SDavid Greenman runend = i + runpg; 833efc68ce1SDavid Greenman if (runend <= reqpage) { 83431953be9SAlan Cox VM_OBJECT_LOCK(object); 835e43c2eabSAlan Cox vm_page_lock_queues(); 836e43c2eabSAlan Cox for (j = i; j < runend; j++) 837d8d5fa88SAlfred Perlstein vm_page_free(m[j]); 838e43c2eabSAlan Cox vm_page_unlock_queues(); 83931953be9SAlan Cox VM_OBJECT_UNLOCK(object); 84026f9a767SRodney W. Grimes } else { 841efc68ce1SDavid Greenman if (runpg < (count - first)) { 84231953be9SAlan Cox VM_OBJECT_LOCK(object); 843e43c2eabSAlan Cox vm_page_lock_queues(); 844efc68ce1SDavid Greenman for (i = first + runpg; i < count; i++) 845d8d5fa88SAlfred Perlstein vm_page_free(m[i]); 846e43c2eabSAlan Cox vm_page_unlock_queues(); 84731953be9SAlan Cox VM_OBJECT_UNLOCK(object); 848efc68ce1SDavid Greenman count = first + runpg; 84926f9a767SRodney W. Grimes } 850efc68ce1SDavid Greenman break; 85126f9a767SRodney W. Grimes } 852efc68ce1SDavid Greenman first = runend; 853efc68ce1SDavid Greenman } 85426f9a767SRodney W. Grimes 85526f9a767SRodney W. Grimes /* 856bbc0ec52SDavid Greenman * the first and last page have been calculated now, move input pages 857bbc0ec52SDavid Greenman * to be zero based... 85826f9a767SRodney W. Grimes */ 85926f9a767SRodney W. Grimes if (first != 0) { 8607e2393ffSAlan Cox m += first; 86126f9a767SRodney W. Grimes count -= first; 86226f9a767SRodney W. Grimes reqpage -= first; 86326f9a767SRodney W. Grimes } 864efc68ce1SDavid Greenman 86526f9a767SRodney W. Grimes /* 86626f9a767SRodney W. Grimes * calculate the file virtual address for the transfer 86726f9a767SRodney W. Grimes */ 868a316d390SJohn Dyson foff = IDX_TO_OFF(m[0]->pindex); 86926f9a767SRodney W. Grimes 87026f9a767SRodney W. Grimes /* 87126f9a767SRodney W. Grimes * calculate the size of the transfer 87226f9a767SRodney W. Grimes */ 87326f9a767SRodney W. Grimes size = count * PAGE_SIZE; 8741a31a6c3SPoul-Henning Kamp KASSERT(count > 0, ("zero count")); 87524a1cce3SDavid Greenman if ((foff + size) > object->un_pager.vnp.vnp_size) 87624a1cce3SDavid Greenman size = object->un_pager.vnp.vnp_size - foff; 8771a31a6c3SPoul-Henning Kamp KASSERT(size > 0, ("zero size")); 87826f9a767SRodney W. Grimes 87926f9a767SRodney W. Grimes /* 88024579ca1SMatthew Dillon * round up physical size for real devices. 88126f9a767SRodney W. Grimes */ 8829c83534dSPoul-Henning Kamp if (1) { 8839c83534dSPoul-Henning Kamp int secmask = bo->bo_bsize - 1; 8846229cc50SPoul-Henning Kamp KASSERT(secmask < PAGE_SIZE && secmask > 0, 8856229cc50SPoul-Henning Kamp ("vnode_pager_generic_getpages: sector size %d too large", 8866229cc50SPoul-Henning Kamp secmask + 1)); 88724579ca1SMatthew Dillon size = (size + secmask) & ~secmask; 88824579ca1SMatthew Dillon } 88926f9a767SRodney W. Grimes 8901c7c3c6aSMatthew Dillon bp = getpbuf(&vnode_pbuf_freecnt); 89116f62314SDavid Greenman kva = (vm_offset_t) bp->b_data; 89216f62314SDavid Greenman 89326f9a767SRodney W. Grimes /* 89426f9a767SRodney W. Grimes * and map the pages to be read into the kva 89526f9a767SRodney W. Grimes */ 89616f62314SDavid Greenman pmap_qenter(kva, m, count); 89726f9a767SRodney W. Grimes 89826f9a767SRodney W. Grimes /* build a minimal buffer header */ 89921144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 9006a4b5823SPoul-Henning Kamp bp->b_iodone = bdone; 901bd78ceceSJohn Baldwin KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 902bd78ceceSJohn Baldwin KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 903a854ed98SJohn Baldwin bp->b_rcred = crhold(curthread->td_ucred); 904a854ed98SJohn Baldwin bp->b_wcred = crhold(curthread->td_ucred); 905187f0071SDavid Greenman bp->b_blkno = firstaddr; 9069c83534dSPoul-Henning Kamp pbgetbo(bo, bp); 90726f9a767SRodney W. Grimes bp->b_bcount = size; 90826f9a767SRodney W. Grimes bp->b_bufsize = size; 9092b6b0df7SMatthew Dillon bp->b_runningbufspace = bp->b_bufsize; 9104f12e0acSSuleiman Souhlal atomic_add_int(&runningbufspace, bp->b_runningbufspace); 91126f9a767SRodney W. Grimes 912b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodein); 913b4b70819SAttilio Rao PCPU_ADD(cnt.v_vnodepgsin, count); 914976e77fcSDavid Greenman 91526f9a767SRodney W. Grimes /* do the input */ 9162c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 917b792bebeSPoul-Henning Kamp bstrategy(bp); 918976e77fcSDavid Greenman 9196a4b5823SPoul-Henning Kamp bwait(bp, PVM, "vnread"); 92026f9a767SRodney W. Grimes 921c244d2deSPoul-Henning Kamp if ((bp->b_ioflags & BIO_ERROR) != 0) 92226f9a767SRodney W. Grimes error = EIO; 92326f9a767SRodney W. Grimes 92426f9a767SRodney W. Grimes if (!error) { 92526f9a767SRodney W. Grimes if (size != count * PAGE_SIZE) 92626f9a767SRodney W. Grimes bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 92726f9a767SRodney W. Grimes } 92816f62314SDavid Greenman pmap_qremove(kva, count); 92926f9a767SRodney W. Grimes 93026f9a767SRodney W. Grimes /* 93126f9a767SRodney W. Grimes * free the buffer header back to the swap buffer pool 93226f9a767SRodney W. Grimes */ 9339c83534dSPoul-Henning Kamp pbrelbo(bp); 9341c7c3c6aSMatthew Dillon relpbuf(bp, &vnode_pbuf_freecnt); 93526f9a767SRodney W. Grimes 93631953be9SAlan Cox VM_OBJECT_LOCK(object); 9379d522888SAlan Cox vm_page_lock_queues(); 9388f9110f6SJohn Dyson for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 9398f9110f6SJohn Dyson vm_page_t mt; 9408f9110f6SJohn Dyson 9418f9110f6SJohn Dyson nextoff = tfoff + PAGE_SIZE; 9428f9110f6SJohn Dyson mt = m[i]; 9438f9110f6SJohn Dyson 94454746b67SDmitrij Tejblum if (nextoff <= object->un_pager.vnp.vnp_size) { 9458d17e694SJulian Elischer /* 9468d17e694SJulian Elischer * Read filled up entire page. 9478d17e694SJulian Elischer */ 9488f9110f6SJohn Dyson mt->valid = VM_PAGE_BITS_ALL; 9492c28a105SAlan Cox vm_page_undirty(mt); /* should be an assert? XXX */ 9500385347cSPeter Wemm pmap_clear_modify(mt); 9518f9110f6SJohn Dyson } else { 9528d17e694SJulian Elischer /* 9538d17e694SJulian Elischer * Read did not fill up entire page. Since this 9548d17e694SJulian Elischer * is getpages, the page may be mapped, so we have 9558d17e694SJulian Elischer * to zero the invalid portions of the page even 9568d17e694SJulian Elischer * though we aren't setting them valid. 9578d17e694SJulian Elischer * 9588d17e694SJulian Elischer * Currently we do not set the entire page valid, 9598d17e694SJulian Elischer * we just try to clear the piece that we couldn't 9608d17e694SJulian Elischer * read. 9618d17e694SJulian Elischer */ 96254746b67SDmitrij Tejblum vm_page_set_validclean(mt, 0, 96354746b67SDmitrij Tejblum object->un_pager.vnp.vnp_size - tfoff); 9644221e284SAlan Cox /* handled by vm_fault now */ 9654221e284SAlan Cox /* vm_page_zero_invalid(mt, FALSE); */ 9668f9110f6SJohn Dyson } 9678f9110f6SJohn Dyson 96826f9a767SRodney W. Grimes if (i != reqpage) { 969bbc0ec52SDavid Greenman 97026f9a767SRodney W. Grimes /* 971bbc0ec52SDavid Greenman * whether or not to leave the page activated is up in 972bbc0ec52SDavid Greenman * the air, but we should put the page on a page queue 973bbc0ec52SDavid Greenman * somewhere. (it already is in the object). Result: 974956f3135SPhilippe Charnier * It appears that empirical results show that 975bbc0ec52SDavid Greenman * deactivating pages is best. 97626f9a767SRodney W. Grimes */ 977bbc0ec52SDavid Greenman 97826f9a767SRodney W. Grimes /* 979bbc0ec52SDavid Greenman * just in case someone was asking for this page we 980bbc0ec52SDavid Greenman * now tell them that it is ok to use 98126f9a767SRodney W. Grimes */ 98226f9a767SRodney W. Grimes if (!error) { 9835786be7cSAlan Cox if (mt->oflags & VPO_WANTED) 9848f9110f6SJohn Dyson vm_page_activate(mt); 98595461b45SJohn Dyson else 9868f9110f6SJohn Dyson vm_page_deactivate(mt); 987e69763a3SDoug Rabson vm_page_wakeup(mt); 98826f9a767SRodney W. Grimes } else { 989d8d5fa88SAlfred Perlstein vm_page_free(mt); 99026f9a767SRodney W. Grimes } 99126f9a767SRodney W. Grimes } 99226f9a767SRodney W. Grimes } 9939d522888SAlan Cox vm_page_unlock_queues(); 99431953be9SAlan Cox VM_OBJECT_UNLOCK(object); 99526f9a767SRodney W. Grimes if (error) { 99624a1cce3SDavid Greenman printf("vnode_pager_getpages: I/O read error\n"); 99726f9a767SRodney W. Grimes } 998a83c285cSDavid Greenman return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 99926f9a767SRodney W. Grimes } 100026f9a767SRodney W. Grimes 1001ce75f2c3SMike Smith /* 1002ce75f2c3SMike Smith * EOPNOTSUPP is no longer legal. For local media VFS's that do not 1003ce75f2c3SMike Smith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 1004ce75f2c3SMike Smith * vnode_pager_generic_putpages() to implement the previous behaviour. 1005ce75f2c3SMike Smith * 1006ce75f2c3SMike Smith * All other FS's should use the bypass to get to the local media 1007ce75f2c3SMike Smith * backing vp's VOP_PUTPAGES. 1008ce75f2c3SMike Smith */ 1009e4542174SMatthew Dillon static void 1010170db9c6SJohn Dyson vnode_pager_putpages(object, m, count, sync, rtvals) 1011170db9c6SJohn Dyson vm_object_t object; 1012170db9c6SJohn Dyson vm_page_t *m; 1013170db9c6SJohn Dyson int count; 1014170db9c6SJohn Dyson boolean_t sync; 1015170db9c6SJohn Dyson int *rtvals; 1016170db9c6SJohn Dyson { 1017170db9c6SJohn Dyson int rtval; 1018170db9c6SJohn Dyson struct vnode *vp; 1019f2a2857bSKirk McKusick struct mount *mp; 102086ffbd76SMike Smith int bytes = count * PAGE_SIZE; 1021ad980522SJohn Dyson 10220e3cdf2cSAlan Cox /* 10230e3cdf2cSAlan Cox * Force synchronous operation if we are extremely low on memory 10240e3cdf2cSAlan Cox * to prevent a low-memory deadlock. VOP operations often need to 10250e3cdf2cSAlan Cox * allocate more memory to initiate the I/O ( i.e. do a BMAP 10260e3cdf2cSAlan Cox * operation ). The swapper handles the case by limiting the amount 10270e3cdf2cSAlan Cox * of asynchronous I/O, but that sort of solution doesn't scale well 10280e3cdf2cSAlan Cox * for the vnode pager without a lot of work. 10290e3cdf2cSAlan Cox * 10300e3cdf2cSAlan Cox * Also, the backing vnode's iodone routine may not wake the pageout 10310e3cdf2cSAlan Cox * daemon up. This should be probably be addressed XXX. 10320e3cdf2cSAlan Cox */ 10330e3cdf2cSAlan Cox 10342feb50bfSAttilio Rao if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 10350e3cdf2cSAlan Cox sync |= OBJPC_SYNC; 10360e3cdf2cSAlan Cox 10370e3cdf2cSAlan Cox /* 10380e3cdf2cSAlan Cox * Call device-specific putpages function 10390e3cdf2cSAlan Cox */ 1040170db9c6SJohn Dyson vp = object->handle; 10412e3b314dSAlan Cox VM_OBJECT_UNLOCK(object); 1042f2a2857bSKirk McKusick if (vp->v_type != VREG) 1043f2a2857bSKirk McKusick mp = NULL; 104486ffbd76SMike Smith rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 104523955314SAlfred Perlstein KASSERT(rtval != EOPNOTSUPP, 104623955314SAlfred Perlstein ("vnode_pager: stale FS putpages\n")); 10472e3b314dSAlan Cox VM_OBJECT_LOCK(object); 1048170db9c6SJohn Dyson } 1049170db9c6SJohn Dyson 1050ce75f2c3SMike Smith 105126f9a767SRodney W. Grimes /* 1052ce75f2c3SMike Smith * This is now called from local media FS's to operate against their 10534491ea91SEivind Eklund * own vnodes if they fail to implement VOP_PUTPAGES. 10542b6b0df7SMatthew Dillon * 10552b6b0df7SMatthew Dillon * This is typically called indirectly via the pageout daemon and 10562b6b0df7SMatthew Dillon * clustering has already typically occured, so in general we ask the 10572b6b0df7SMatthew Dillon * underlying filesystem to write the data out asynchronously rather 10582b6b0df7SMatthew Dillon * then delayed. 105926f9a767SRodney W. Grimes */ 1060ce75f2c3SMike Smith int 10618f9110f6SJohn Dyson vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 1062ce75f2c3SMike Smith struct vnode *vp; 106326f9a767SRodney W. Grimes vm_page_t *m; 1064ce75f2c3SMike Smith int bytecount; 10658f9110f6SJohn Dyson int flags; 106626f9a767SRodney W. Grimes int *rtvals; 106726f9a767SRodney W. Grimes { 1068f6b04d2bSDavid Greenman int i; 1069ce75f2c3SMike Smith vm_object_t object; 1070ce75f2c3SMike Smith int count; 107126f9a767SRodney W. Grimes 1072f6b04d2bSDavid Greenman int maxsize, ncount; 1073a316d390SJohn Dyson vm_ooffset_t poffset; 1074f6b04d2bSDavid Greenman struct uio auio; 1075f6b04d2bSDavid Greenman struct iovec aiov; 1076f6b04d2bSDavid Greenman int error; 10778f9110f6SJohn Dyson int ioflags; 1078dd498befSPaul Saab int ppscheck = 0; 1079dd498befSPaul Saab static struct timeval lastfail; 1080dd498befSPaul Saab static int curfail; 108126f9a767SRodney W. Grimes 1082ce75f2c3SMike Smith object = vp->v_object; 1083ce75f2c3SMike Smith count = bytecount / PAGE_SIZE; 1084ce75f2c3SMike Smith 108526f9a767SRodney W. Grimes for (i = 0; i < count; i++) 108626f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_AGAIN; 108726f9a767SRodney W. Grimes 1088d8fed1d0SAlan Cox if ((int64_t)m[0]->pindex < 0) { 108923562e4bSMarcel Moolenaar printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n", 109023562e4bSMarcel Moolenaar (long)m[0]->pindex, (u_long)m[0]->dirty); 1091f6b04d2bSDavid Greenman rtvals[0] = VM_PAGER_BAD; 1092f6b04d2bSDavid Greenman return VM_PAGER_BAD; 10930d94caffSDavid Greenman } 10940bdb7528SDavid Greenman 1095f6b04d2bSDavid Greenman maxsize = count * PAGE_SIZE; 1096f6b04d2bSDavid Greenman ncount = count; 109726f9a767SRodney W. Grimes 1098a316d390SJohn Dyson poffset = IDX_TO_OFF(m[0]->pindex); 109900a6f47fSMatthew Dillon 110000a6f47fSMatthew Dillon /* 110100a6f47fSMatthew Dillon * If the page-aligned write is larger then the actual file we 110200a6f47fSMatthew Dillon * have to invalidate pages occuring beyond the file EOF. However, 110300a6f47fSMatthew Dillon * there is an edge case where a file may not be page-aligned where 110400a6f47fSMatthew Dillon * the last page is partially invalid. In this case the filesystem 110500a6f47fSMatthew Dillon * may not properly clear the dirty bits for the entire page (which 110600a6f47fSMatthew Dillon * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 110700a6f47fSMatthew Dillon * With the page locked we are free to fix-up the dirty bits here. 11083ebeaf59SMatthew Dillon * 11093ebeaf59SMatthew Dillon * We do not under any circumstances truncate the valid bits, as 11103ebeaf59SMatthew Dillon * this will screw up bogus page replacement. 111100a6f47fSMatthew Dillon */ 1112a316d390SJohn Dyson if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 111300a6f47fSMatthew Dillon if (object->un_pager.vnp.vnp_size > poffset) { 111400a6f47fSMatthew Dillon int pgoff; 111500a6f47fSMatthew Dillon 1116a316d390SJohn Dyson maxsize = object->un_pager.vnp.vnp_size - poffset; 1117aa8de40aSPoul-Henning Kamp ncount = btoc(maxsize); 111800a6f47fSMatthew Dillon if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1119b7ad744dSAlan Cox vm_page_lock_queues(); 112000a6f47fSMatthew Dillon vm_page_clear_dirty(m[ncount - 1], pgoff, 112100a6f47fSMatthew Dillon PAGE_SIZE - pgoff); 1122b7ad744dSAlan Cox vm_page_unlock_queues(); 112300a6f47fSMatthew Dillon } 112400a6f47fSMatthew Dillon } else { 112500a6f47fSMatthew Dillon maxsize = 0; 112600a6f47fSMatthew Dillon ncount = 0; 112700a6f47fSMatthew Dillon } 1128f6b04d2bSDavid Greenman if (ncount < count) { 1129f6b04d2bSDavid Greenman for (i = ncount; i < count; i++) { 1130f6b04d2bSDavid Greenman rtvals[i] = VM_PAGER_BAD; 1131f6b04d2bSDavid Greenman } 1132f6b04d2bSDavid Greenman } 1133f6b04d2bSDavid Greenman } 113426f9a767SRodney W. Grimes 11352b6b0df7SMatthew Dillon /* 11362b6b0df7SMatthew Dillon * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 11372b6b0df7SMatthew Dillon * rather then a bdwrite() to prevent paging I/O from saturating 113843b7990eSMatthew Dillon * the buffer cache. Dummy-up the sequential heuristic to cause 113943b7990eSMatthew Dillon * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 114043b7990eSMatthew Dillon * the system decides how to cluster. 11412b6b0df7SMatthew Dillon */ 11428f9110f6SJohn Dyson ioflags = IO_VMIO; 114343b7990eSMatthew Dillon if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 114443b7990eSMatthew Dillon ioflags |= IO_SYNC; 114543b7990eSMatthew Dillon else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 114643b7990eSMatthew Dillon ioflags |= IO_ASYNC; 11478f9110f6SJohn Dyson ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 114843b7990eSMatthew Dillon ioflags |= IO_SEQMAX << IO_SEQSHIFT; 1149f6b04d2bSDavid Greenman 1150f6b04d2bSDavid Greenman aiov.iov_base = (caddr_t) 0; 1151f6b04d2bSDavid Greenman aiov.iov_len = maxsize; 1152f6b04d2bSDavid Greenman auio.uio_iov = &aiov; 1153f6b04d2bSDavid Greenman auio.uio_iovcnt = 1; 1154a316d390SJohn Dyson auio.uio_offset = poffset; 1155f6b04d2bSDavid Greenman auio.uio_segflg = UIO_NOCOPY; 1156f6b04d2bSDavid Greenman auio.uio_rw = UIO_WRITE; 1157f6b04d2bSDavid Greenman auio.uio_resid = maxsize; 1158b40ce416SJulian Elischer auio.uio_td = (struct thread *) 0; 1159a854ed98SJohn Baldwin error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1160b4b70819SAttilio Rao PCPU_INC(cnt.v_vnodeout); 1161b4b70819SAttilio Rao PCPU_ADD(cnt.v_vnodepgsout, ncount); 1162f6b04d2bSDavid Greenman 1163f6b04d2bSDavid Greenman if (error) { 1164dd498befSPaul Saab if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1))) 116524a1cce3SDavid Greenman printf("vnode_pager_putpages: I/O error %d\n", error); 1166f6b04d2bSDavid Greenman } 1167f6b04d2bSDavid Greenman if (auio.uio_resid) { 1168dd498befSPaul Saab if (ppscheck || ppsratecheck(&lastfail, &curfail, 1)) 1169ac1e407bSBruce Evans printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1170ac1e407bSBruce Evans auio.uio_resid, (u_long)m[0]->pindex); 117126f9a767SRodney W. Grimes } 1172ffc82b0aSJohn Dyson for (i = 0; i < ncount; i++) { 117326f9a767SRodney W. Grimes rtvals[i] = VM_PAGER_OK; 117426f9a767SRodney W. Grimes } 1175f6b04d2bSDavid Greenman return rtvals[0]; 117626f9a767SRodney W. Grimes } 1177f6b04d2bSDavid Greenman 1178f6b04d2bSDavid Greenman struct vnode * 1179417a26a1SAlan Cox vnode_pager_lock(vm_object_t first_object) 118024a1cce3SDavid Greenman { 1181417a26a1SAlan Cox struct vnode *vp; 1182417a26a1SAlan Cox vm_object_t backing_object, object; 1183996c772fSJohn Dyson 1184417a26a1SAlan Cox VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 1185417a26a1SAlan Cox for (object = first_object; object != NULL; object = backing_object) { 1186417a26a1SAlan Cox if (object->type != OBJT_VNODE) { 1187417a26a1SAlan Cox if ((backing_object = object->backing_object) != NULL) 1188417a26a1SAlan Cox VM_OBJECT_LOCK(backing_object); 1189417a26a1SAlan Cox if (object != first_object) 1190417a26a1SAlan Cox VM_OBJECT_UNLOCK(object); 1191f6b04d2bSDavid Greenman continue; 1192417a26a1SAlan Cox } 1193417a26a1SAlan Cox retry: 1194e6b961ffSJohn Baldwin if (object->flags & OBJ_DEAD) { 1195417a26a1SAlan Cox if (object != first_object) 1196417a26a1SAlan Cox VM_OBJECT_UNLOCK(object); 119747221757SJohn Dyson return NULL; 1198e6b961ffSJohn Baldwin } 1199417a26a1SAlan Cox vp = object->handle; 1200417a26a1SAlan Cox VI_LOCK(vp); 1201417a26a1SAlan Cox VM_OBJECT_UNLOCK(object); 1202417a26a1SAlan Cox if (first_object != object) 1203417a26a1SAlan Cox VM_OBJECT_UNLOCK(first_object); 1204ed4fe4f4SJeff Roberson VFS_ASSERT_GIANT(vp->v_mount); 1205f247a524SJeff Roberson if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | 1206417a26a1SAlan Cox LK_RETRY | LK_SHARED, curthread)) { 1207417a26a1SAlan Cox VM_OBJECT_LOCK(first_object); 1208417a26a1SAlan Cox if (object != first_object) 1209417a26a1SAlan Cox VM_OBJECT_LOCK(object); 1210417a26a1SAlan Cox if (object->type != OBJT_VNODE) { 1211417a26a1SAlan Cox if (object != first_object) 1212417a26a1SAlan Cox VM_OBJECT_UNLOCK(object); 1213bef608bdSJohn Dyson return NULL; 1214417a26a1SAlan Cox } 121547221757SJohn Dyson printf("vnode_pager_lock: retrying\n"); 1216417a26a1SAlan Cox goto retry; 121747221757SJohn Dyson } 1218417a26a1SAlan Cox VM_OBJECT_LOCK(first_object); 1219417a26a1SAlan Cox return (vp); 122026f9a767SRodney W. Grimes } 122124a1cce3SDavid Greenman return NULL; 1222f6b04d2bSDavid Greenman } 1223