The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vnode_pager.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1990 University of Utah.
    3  * Copyright (c) 1991 The Regents of the University of California.
    4  * All rights reserved.
    5  * Copyright (c) 1993, 1994 John S. Dyson
    6  * Copyright (c) 1995, David Greenman
    7  *
    8  * This code is derived from software contributed to Berkeley by
    9  * the Systems Programming Group of the University of Utah Computer
   10  * Science Department.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. All advertising materials mentioning features or use of this software
   21  *    must display the following acknowledgement:
   22  *      This product includes software developed by the University of
   23  *      California, Berkeley and its contributors.
   24  * 4. Neither the name of the University nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   38  * SUCH DAMAGE.
   39  *
   40  *      from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91
   41  */
   42 
   43 /*
   44  * Page to/from files (vnodes).
   45  */
   46 
   47 /*
   48  * TODO:
   49  *      Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
   50  *      greatly re-simplify the vnode_pager.
   51  */
   52 
   53 #include <sys/cdefs.h>
   54 __FBSDID("$FreeBSD: releng/9.1/sys/vm/vnode_pager.c 234466 2012-04-19 18:03:24Z mckusick $");
   55 
   56 #include <sys/param.h>
   57 #include <sys/systm.h>
   58 #include <sys/proc.h>
   59 #include <sys/vnode.h>
   60 #include <sys/mount.h>
   61 #include <sys/bio.h>
   62 #include <sys/buf.h>
   63 #include <sys/vmmeter.h>
   64 #include <sys/limits.h>
   65 #include <sys/conf.h>
   66 #include <sys/sf_buf.h>
   67 
   68 #include <machine/atomic.h>
   69 
   70 #include <vm/vm.h>
   71 #include <vm/vm_object.h>
   72 #include <vm/vm_page.h>
   73 #include <vm/vm_pager.h>
   74 #include <vm/vm_map.h>
   75 #include <vm/vnode_pager.h>
   76 #include <vm/vm_extern.h>
   77 
   78 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
   79     daddr_t *rtaddress, int *run);
   80 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
   81 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
   82 static void vnode_pager_dealloc(vm_object_t);
   83 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
   84 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
   85 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
   86 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
   87     vm_ooffset_t, struct ucred *cred);
   88 
   89 struct pagerops vnodepagerops = {
   90         .pgo_alloc =    vnode_pager_alloc,
   91         .pgo_dealloc =  vnode_pager_dealloc,
   92         .pgo_getpages = vnode_pager_getpages,
   93         .pgo_putpages = vnode_pager_putpages,
   94         .pgo_haspage =  vnode_pager_haspage,
   95 };
   96 
   97 int vnode_pbuf_freecnt;
   98 
   99 /* Create the VM system backing object for this vnode */
  100 int
  101 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
  102 {
  103         vm_object_t object;
  104         vm_ooffset_t size = isize;
  105         struct vattr va;
  106 
  107         if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
  108                 return (0);
  109 
  110         while ((object = vp->v_object) != NULL) {
  111                 VM_OBJECT_LOCK(object);
  112                 if (!(object->flags & OBJ_DEAD)) {
  113                         VM_OBJECT_UNLOCK(object);
  114                         return (0);
  115                 }
  116                 VOP_UNLOCK(vp, 0);
  117                 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
  118                 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0);
  119                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  120         }
  121 
  122         if (size == 0) {
  123                 if (vn_isdisk(vp, NULL)) {
  124                         size = IDX_TO_OFF(INT_MAX);
  125                 } else {
  126                         if (VOP_GETATTR(vp, &va, td->td_ucred))
  127                                 return (0);
  128                         size = va.va_size;
  129                 }
  130         }
  131 
  132         object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
  133         /*
  134          * Dereference the reference we just created.  This assumes
  135          * that the object is associated with the vp.
  136          */
  137         VM_OBJECT_LOCK(object);
  138         object->ref_count--;
  139         VM_OBJECT_UNLOCK(object);
  140         vrele(vp);
  141 
  142         KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
  143 
  144         return (0);
  145 }
  146 
  147 void
  148 vnode_destroy_vobject(struct vnode *vp)
  149 {
  150         struct vm_object *obj;
  151 
  152         obj = vp->v_object;
  153         if (obj == NULL)
  154                 return;
  155         ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
  156         VM_OBJECT_LOCK(obj);
  157         if (obj->ref_count == 0) {
  158                 /*
  159                  * vclean() may be called twice. The first time
  160                  * removes the primary reference to the object,
  161                  * the second time goes one further and is a
  162                  * special-case to terminate the object.
  163                  *
  164                  * don't double-terminate the object
  165                  */
  166                 if ((obj->flags & OBJ_DEAD) == 0)
  167                         vm_object_terminate(obj);
  168                 else
  169                         VM_OBJECT_UNLOCK(obj);
  170         } else {
  171                 /*
  172                  * Woe to the process that tries to page now :-).
  173                  */
  174                 vm_pager_deallocate(obj);
  175                 VM_OBJECT_UNLOCK(obj);
  176         }
  177         vp->v_object = NULL;
  178 }
  179 
  180 
  181 /*
  182  * Allocate (or lookup) pager for a vnode.
  183  * Handle is a vnode pointer.
  184  *
  185  * MPSAFE
  186  */
  187 vm_object_t
  188 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
  189     vm_ooffset_t offset, struct ucred *cred)
  190 {
  191         vm_object_t object;
  192         struct vnode *vp;
  193 
  194         /*
  195          * Pageout to vnode, no can do yet.
  196          */
  197         if (handle == NULL)
  198                 return (NULL);
  199 
  200         vp = (struct vnode *) handle;
  201 
  202         /*
  203          * If the object is being terminated, wait for it to
  204          * go away.
  205          */
  206 retry:
  207         while ((object = vp->v_object) != NULL) {
  208                 VM_OBJECT_LOCK(object);
  209                 if ((object->flags & OBJ_DEAD) == 0)
  210                         break;
  211                 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
  212                 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
  213         }
  214 
  215         if (vp->v_usecount == 0)
  216                 panic("vnode_pager_alloc: no vnode reference");
  217 
  218         if (object == NULL) {
  219                 /*
  220                  * Add an object of the appropriate size
  221                  */
  222                 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
  223 
  224                 object->un_pager.vnp.vnp_size = size;
  225                 object->un_pager.vnp.writemappings = 0;
  226 
  227                 object->handle = handle;
  228                 VI_LOCK(vp);
  229                 if (vp->v_object != NULL) {
  230                         /*
  231                          * Object has been created while we were sleeping
  232                          */
  233                         VI_UNLOCK(vp);
  234                         vm_object_destroy(object);
  235                         goto retry;
  236                 }
  237                 vp->v_object = object;
  238                 VI_UNLOCK(vp);
  239         } else {
  240                 object->ref_count++;
  241                 VM_OBJECT_UNLOCK(object);
  242         }
  243         vref(vp);
  244         return (object);
  245 }
  246 
  247 /*
  248  *      The object must be locked.
  249  */
  250 static void
  251 vnode_pager_dealloc(object)
  252         vm_object_t object;
  253 {
  254         struct vnode *vp;
  255         int refs;
  256 
  257         vp = object->handle;
  258         if (vp == NULL)
  259                 panic("vnode_pager_dealloc: pager already dealloced");
  260 
  261         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  262         vm_object_pip_wait(object, "vnpdea");
  263         refs = object->ref_count;
  264 
  265         object->handle = NULL;
  266         object->type = OBJT_DEAD;
  267         if (object->flags & OBJ_DISCONNECTWNT) {
  268                 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
  269                 wakeup(object);
  270         }
  271         ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
  272         if (object->un_pager.vnp.writemappings > 0) {
  273                 object->un_pager.vnp.writemappings = 0;
  274                 vp->v_writecount--;
  275         }
  276         vp->v_object = NULL;
  277         vp->v_vflag &= ~VV_TEXT;
  278         VM_OBJECT_UNLOCK(object);
  279         while (refs-- > 0)
  280                 vunref(vp);
  281         VM_OBJECT_LOCK(object);
  282 }
  283 
  284 static boolean_t
  285 vnode_pager_haspage(object, pindex, before, after)
  286         vm_object_t object;
  287         vm_pindex_t pindex;
  288         int *before;
  289         int *after;
  290 {
  291         struct vnode *vp = object->handle;
  292         daddr_t bn;
  293         int err;
  294         daddr_t reqblock;
  295         int poff;
  296         int bsize;
  297         int pagesperblock, blocksperpage;
  298         int vfslocked;
  299 
  300         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  301         /*
  302          * If no vp or vp is doomed or marked transparent to VM, we do not
  303          * have the page.
  304          */
  305         if (vp == NULL || vp->v_iflag & VI_DOOMED)
  306                 return FALSE;
  307         /*
  308          * If the offset is beyond end of file we do
  309          * not have the page.
  310          */
  311         if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
  312                 return FALSE;
  313 
  314         bsize = vp->v_mount->mnt_stat.f_iosize;
  315         pagesperblock = bsize / PAGE_SIZE;
  316         blocksperpage = 0;
  317         if (pagesperblock > 0) {
  318                 reqblock = pindex / pagesperblock;
  319         } else {
  320                 blocksperpage = (PAGE_SIZE / bsize);
  321                 reqblock = pindex * blocksperpage;
  322         }
  323         VM_OBJECT_UNLOCK(object);
  324         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  325         err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
  326         VFS_UNLOCK_GIANT(vfslocked);
  327         VM_OBJECT_LOCK(object);
  328         if (err)
  329                 return TRUE;
  330         if (bn == -1)
  331                 return FALSE;
  332         if (pagesperblock > 0) {
  333                 poff = pindex - (reqblock * pagesperblock);
  334                 if (before) {
  335                         *before *= pagesperblock;
  336                         *before += poff;
  337                 }
  338                 if (after) {
  339                         int numafter;
  340                         *after *= pagesperblock;
  341                         numafter = pagesperblock - (poff + 1);
  342                         if (IDX_TO_OFF(pindex + numafter) >
  343                             object->un_pager.vnp.vnp_size) {
  344                                 numafter =
  345                                     OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
  346                                     pindex;
  347                         }
  348                         *after += numafter;
  349                 }
  350         } else {
  351                 if (before) {
  352                         *before /= blocksperpage;
  353                 }
  354 
  355                 if (after) {
  356                         *after /= blocksperpage;
  357                 }
  358         }
  359         return TRUE;
  360 }
  361 
  362 /*
  363  * Lets the VM system know about a change in size for a file.
  364  * We adjust our own internal size and flush any cached pages in
  365  * the associated object that are affected by the size change.
  366  *
  367  * Note: this routine may be invoked as a result of a pager put
  368  * operation (possibly at object termination time), so we must be careful.
  369  */
  370 void
  371 vnode_pager_setsize(vp, nsize)
  372         struct vnode *vp;
  373         vm_ooffset_t nsize;
  374 {
  375         vm_object_t object;
  376         vm_page_t m;
  377         vm_pindex_t nobjsize;
  378 
  379         if ((object = vp->v_object) == NULL)
  380                 return;
  381 /*      ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
  382         VM_OBJECT_LOCK(object);
  383         if (nsize == object->un_pager.vnp.vnp_size) {
  384                 /*
  385                  * Hasn't changed size
  386                  */
  387                 VM_OBJECT_UNLOCK(object);
  388                 return;
  389         }
  390         nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
  391         if (nsize < object->un_pager.vnp.vnp_size) {
  392                 /*
  393                  * File has shrunk. Toss any cached pages beyond the new EOF.
  394                  */
  395                 if (nobjsize < object->size)
  396                         vm_object_page_remove(object, nobjsize, object->size,
  397                             0);
  398                 /*
  399                  * this gets rid of garbage at the end of a page that is now
  400                  * only partially backed by the vnode.
  401                  *
  402                  * XXX for some reason (I don't know yet), if we take a
  403                  * completely invalid page and mark it partially valid
  404                  * it can screw up NFS reads, so we don't allow the case.
  405                  */
  406                 if ((nsize & PAGE_MASK) &&
  407                     (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
  408                     m->valid != 0) {
  409                         int base = (int)nsize & PAGE_MASK;
  410                         int size = PAGE_SIZE - base;
  411 
  412                         /*
  413                          * Clear out partial-page garbage in case
  414                          * the page has been mapped.
  415                          */
  416                         pmap_zero_page_area(m, base, size);
  417 
  418                         /*
  419                          * Update the valid bits to reflect the blocks that
  420                          * have been zeroed.  Some of these valid bits may
  421                          * have already been set.
  422                          */
  423                         vm_page_set_valid(m, base, size);
  424 
  425                         /*
  426                          * Round "base" to the next block boundary so that the
  427                          * dirty bit for a partially zeroed block is not
  428                          * cleared.
  429                          */
  430                         base = roundup2(base, DEV_BSIZE);
  431 
  432                         /*
  433                          * Clear out partial-page dirty bits.
  434                          *
  435                          * note that we do not clear out the valid
  436                          * bits.  This would prevent bogus_page
  437                          * replacement from working properly.
  438                          */
  439                         vm_page_clear_dirty(m, base, PAGE_SIZE - base);
  440                 } else if ((nsize & PAGE_MASK) &&
  441                     __predict_false(object->cache != NULL)) {
  442                         vm_page_cache_free(object, OFF_TO_IDX(nsize),
  443                             nobjsize);
  444                 }
  445         }
  446         object->un_pager.vnp.vnp_size = nsize;
  447         object->size = nobjsize;
  448         VM_OBJECT_UNLOCK(object);
  449 }
  450 
  451 /*
  452  * calculate the linear (byte) disk address of specified virtual
  453  * file address
  454  */
  455 static int
  456 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
  457     int *run)
  458 {
  459         int bsize;
  460         int err;
  461         daddr_t vblock;
  462         daddr_t voffset;
  463 
  464         if (address < 0)
  465                 return -1;
  466 
  467         if (vp->v_iflag & VI_DOOMED)
  468                 return -1;
  469 
  470         bsize = vp->v_mount->mnt_stat.f_iosize;
  471         vblock = address / bsize;
  472         voffset = address % bsize;
  473 
  474         err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
  475         if (err == 0) {
  476                 if (*rtaddress != -1)
  477                         *rtaddress += voffset / DEV_BSIZE;
  478                 if (run) {
  479                         *run += 1;
  480                         *run *= bsize/PAGE_SIZE;
  481                         *run -= voffset/PAGE_SIZE;
  482                 }
  483         }
  484 
  485         return (err);
  486 }
  487 
  488 /*
  489  * small block filesystem vnode pager input
  490  */
  491 static int
  492 vnode_pager_input_smlfs(object, m)
  493         vm_object_t object;
  494         vm_page_t m;
  495 {
  496         struct vnode *vp;
  497         struct bufobj *bo;
  498         struct buf *bp;
  499         struct sf_buf *sf;
  500         daddr_t fileaddr;
  501         vm_offset_t bsize;
  502         vm_page_bits_t bits;
  503         int error, i;
  504 
  505         error = 0;
  506         vp = object->handle;
  507         if (vp->v_iflag & VI_DOOMED)
  508                 return VM_PAGER_BAD;
  509 
  510         bsize = vp->v_mount->mnt_stat.f_iosize;
  511 
  512         VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
  513 
  514         sf = sf_buf_alloc(m, 0);
  515 
  516         for (i = 0; i < PAGE_SIZE / bsize; i++) {
  517                 vm_ooffset_t address;
  518 
  519                 bits = vm_page_bits(i * bsize, bsize);
  520                 if (m->valid & bits)
  521                         continue;
  522 
  523                 address = IDX_TO_OFF(m->pindex) + i * bsize;
  524                 if (address >= object->un_pager.vnp.vnp_size) {
  525                         fileaddr = -1;
  526                 } else {
  527                         error = vnode_pager_addr(vp, address, &fileaddr, NULL);
  528                         if (error)
  529                                 break;
  530                 }
  531                 if (fileaddr != -1) {
  532                         bp = getpbuf(&vnode_pbuf_freecnt);
  533 
  534                         /* build a minimal buffer header */
  535                         bp->b_iocmd = BIO_READ;
  536                         bp->b_iodone = bdone;
  537                         KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
  538                         KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
  539                         bp->b_rcred = crhold(curthread->td_ucred);
  540                         bp->b_wcred = crhold(curthread->td_ucred);
  541                         bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
  542                         bp->b_blkno = fileaddr;
  543                         pbgetbo(bo, bp);
  544                         bp->b_vp = vp;
  545                         bp->b_bcount = bsize;
  546                         bp->b_bufsize = bsize;
  547                         bp->b_runningbufspace = bp->b_bufsize;
  548                         atomic_add_long(&runningbufspace, bp->b_runningbufspace);
  549 
  550                         /* do the input */
  551                         bp->b_iooffset = dbtob(bp->b_blkno);
  552                         bstrategy(bp);
  553 
  554                         bwait(bp, PVM, "vnsrd");
  555 
  556                         if ((bp->b_ioflags & BIO_ERROR) != 0)
  557                                 error = EIO;
  558 
  559                         /*
  560                          * free the buffer header back to the swap buffer pool
  561                          */
  562                         bp->b_vp = NULL;
  563                         pbrelbo(bp);
  564                         relpbuf(bp, &vnode_pbuf_freecnt);
  565                         if (error)
  566                                 break;
  567                 } else
  568                         bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
  569                 KASSERT((m->dirty & bits) == 0,
  570                     ("vnode_pager_input_smlfs: page %p is dirty", m));
  571                 VM_OBJECT_LOCK(object);
  572                 m->valid |= bits;
  573                 VM_OBJECT_UNLOCK(object);
  574         }
  575         sf_buf_free(sf);
  576         if (error) {
  577                 return VM_PAGER_ERROR;
  578         }
  579         return VM_PAGER_OK;
  580 }
  581 
  582 /*
  583  * old style vnode pager input routine
  584  */
  585 static int
  586 vnode_pager_input_old(object, m)
  587         vm_object_t object;
  588         vm_page_t m;
  589 {
  590         struct uio auio;
  591         struct iovec aiov;
  592         int error;
  593         int size;
  594         struct sf_buf *sf;
  595         struct vnode *vp;
  596 
  597         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  598         error = 0;
  599 
  600         /*
  601          * Return failure if beyond current EOF
  602          */
  603         if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
  604                 return VM_PAGER_BAD;
  605         } else {
  606                 size = PAGE_SIZE;
  607                 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
  608                         size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
  609                 vp = object->handle;
  610                 VM_OBJECT_UNLOCK(object);
  611 
  612                 /*
  613                  * Allocate a kernel virtual address and initialize so that
  614                  * we can use VOP_READ/WRITE routines.
  615                  */
  616                 sf = sf_buf_alloc(m, 0);
  617 
  618                 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
  619                 aiov.iov_len = size;
  620                 auio.uio_iov = &aiov;
  621                 auio.uio_iovcnt = 1;
  622                 auio.uio_offset = IDX_TO_OFF(m->pindex);
  623                 auio.uio_segflg = UIO_SYSSPACE;
  624                 auio.uio_rw = UIO_READ;
  625                 auio.uio_resid = size;
  626                 auio.uio_td = curthread;
  627 
  628                 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
  629                 if (!error) {
  630                         int count = size - auio.uio_resid;
  631 
  632                         if (count == 0)
  633                                 error = EINVAL;
  634                         else if (count != PAGE_SIZE)
  635                                 bzero((caddr_t)sf_buf_kva(sf) + count,
  636                                     PAGE_SIZE - count);
  637                 }
  638                 sf_buf_free(sf);
  639 
  640                 VM_OBJECT_LOCK(object);
  641         }
  642         KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
  643         if (!error)
  644                 m->valid = VM_PAGE_BITS_ALL;
  645         return error ? VM_PAGER_ERROR : VM_PAGER_OK;
  646 }
  647 
  648 /*
  649  * generic vnode pager input routine
  650  */
  651 
  652 /*
  653  * Local media VFS's that do not implement their own VOP_GETPAGES
  654  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
  655  * to implement the previous behaviour.
  656  *
  657  * All other FS's should use the bypass to get to the local media
  658  * backing vp's VOP_GETPAGES.
  659  */
  660 static int
  661 vnode_pager_getpages(object, m, count, reqpage)
  662         vm_object_t object;
  663         vm_page_t *m;
  664         int count;
  665         int reqpage;
  666 {
  667         int rtval;
  668         struct vnode *vp;
  669         int bytes = count * PAGE_SIZE;
  670         int vfslocked;
  671 
  672         vp = object->handle;
  673         VM_OBJECT_UNLOCK(object);
  674         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  675         rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
  676         KASSERT(rtval != EOPNOTSUPP,
  677             ("vnode_pager: FS getpages not implemented\n"));
  678         VFS_UNLOCK_GIANT(vfslocked);
  679         VM_OBJECT_LOCK(object);
  680         return rtval;
  681 }
  682 
  683 /*
  684  * This is now called from local media FS's to operate against their
  685  * own vnodes if they fail to implement VOP_GETPAGES.
  686  */
  687 int
  688 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
  689         struct vnode *vp;
  690         vm_page_t *m;
  691         int bytecount;
  692         int reqpage;
  693 {
  694         vm_object_t object;
  695         vm_offset_t kva;
  696         off_t foff, tfoff, nextoff;
  697         int i, j, size, bsize, first;
  698         daddr_t firstaddr, reqblock;
  699         struct bufobj *bo;
  700         int runpg;
  701         int runend;
  702         struct buf *bp;
  703         int count;
  704         int error;
  705 
  706         object = vp->v_object;
  707         count = bytecount / PAGE_SIZE;
  708 
  709         KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
  710             ("vnode_pager_generic_getpages does not support devices"));
  711         if (vp->v_iflag & VI_DOOMED)
  712                 return VM_PAGER_BAD;
  713 
  714         bsize = vp->v_mount->mnt_stat.f_iosize;
  715 
  716         /* get the UNDERLYING device for the file with VOP_BMAP() */
  717 
  718         /*
  719          * originally, we did not check for an error return value -- assuming
  720          * an fs always has a bmap entry point -- that assumption is wrong!!!
  721          */
  722         foff = IDX_TO_OFF(m[reqpage]->pindex);
  723 
  724         /*
  725          * if we can't bmap, use old VOP code
  726          */
  727         error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
  728         if (error == EOPNOTSUPP) {
  729                 VM_OBJECT_LOCK(object);
  730                 
  731                 for (i = 0; i < count; i++)
  732                         if (i != reqpage) {
  733                                 vm_page_lock(m[i]);
  734                                 vm_page_free(m[i]);
  735                                 vm_page_unlock(m[i]);
  736                         }
  737                 PCPU_INC(cnt.v_vnodein);
  738                 PCPU_INC(cnt.v_vnodepgsin);
  739                 error = vnode_pager_input_old(object, m[reqpage]);
  740                 VM_OBJECT_UNLOCK(object);
  741                 return (error);
  742         } else if (error != 0) {
  743                 VM_OBJECT_LOCK(object);
  744                 for (i = 0; i < count; i++)
  745                         if (i != reqpage) {
  746                                 vm_page_lock(m[i]);
  747                                 vm_page_free(m[i]);
  748                                 vm_page_unlock(m[i]);
  749                         }
  750                 VM_OBJECT_UNLOCK(object);
  751                 return (VM_PAGER_ERROR);
  752 
  753                 /*
  754                  * if the blocksize is smaller than a page size, then use
  755                  * special small filesystem code.  NFS sometimes has a small
  756                  * blocksize, but it can handle large reads itself.
  757                  */
  758         } else if ((PAGE_SIZE / bsize) > 1 &&
  759             (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
  760                 VM_OBJECT_LOCK(object);
  761                 for (i = 0; i < count; i++)
  762                         if (i != reqpage) {
  763                                 vm_page_lock(m[i]);
  764                                 vm_page_free(m[i]);
  765                                 vm_page_unlock(m[i]);
  766                         }
  767                 VM_OBJECT_UNLOCK(object);
  768                 PCPU_INC(cnt.v_vnodein);
  769                 PCPU_INC(cnt.v_vnodepgsin);
  770                 return vnode_pager_input_smlfs(object, m[reqpage]);
  771         }
  772 
  773         /*
  774          * If we have a completely valid page available to us, we can
  775          * clean up and return.  Otherwise we have to re-read the
  776          * media.
  777          */
  778         VM_OBJECT_LOCK(object);
  779         if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
  780                 for (i = 0; i < count; i++)
  781                         if (i != reqpage) {
  782                                 vm_page_lock(m[i]);
  783                                 vm_page_free(m[i]);
  784                                 vm_page_unlock(m[i]);
  785                         }
  786                 VM_OBJECT_UNLOCK(object);
  787                 return VM_PAGER_OK;
  788         } else if (reqblock == -1) {
  789                 pmap_zero_page(m[reqpage]);
  790                 KASSERT(m[reqpage]->dirty == 0,
  791                     ("vnode_pager_generic_getpages: page %p is dirty", m));
  792                 m[reqpage]->valid = VM_PAGE_BITS_ALL;
  793                 for (i = 0; i < count; i++)
  794                         if (i != reqpage) {
  795                                 vm_page_lock(m[i]);
  796                                 vm_page_free(m[i]);
  797                                 vm_page_unlock(m[i]);
  798                         }
  799                 VM_OBJECT_UNLOCK(object);
  800                 return (VM_PAGER_OK);
  801         }
  802         m[reqpage]->valid = 0;
  803         VM_OBJECT_UNLOCK(object);
  804 
  805         /*
  806          * here on direct device I/O
  807          */
  808         firstaddr = -1;
  809 
  810         /*
  811          * calculate the run that includes the required page
  812          */
  813         for (first = 0, i = 0; i < count; i = runend) {
  814                 if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
  815                     &runpg) != 0) {
  816                         VM_OBJECT_LOCK(object);
  817                         for (; i < count; i++)
  818                                 if (i != reqpage) {
  819                                         vm_page_lock(m[i]);
  820                                         vm_page_free(m[i]);
  821                                         vm_page_unlock(m[i]);
  822                                 }
  823                         VM_OBJECT_UNLOCK(object);
  824                         return (VM_PAGER_ERROR);
  825                 }
  826                 if (firstaddr == -1) {
  827                         VM_OBJECT_LOCK(object);
  828                         if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
  829                                 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
  830                                     (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
  831                                     (uintmax_t)foff,
  832                                     (uintmax_t)
  833                                     (object->un_pager.vnp.vnp_size >> 32),
  834                                     (uintmax_t)object->un_pager.vnp.vnp_size);
  835                         }
  836                         vm_page_lock(m[i]);
  837                         vm_page_free(m[i]);
  838                         vm_page_unlock(m[i]);
  839                         VM_OBJECT_UNLOCK(object);
  840                         runend = i + 1;
  841                         first = runend;
  842                         continue;
  843                 }
  844                 runend = i + runpg;
  845                 if (runend <= reqpage) {
  846                         VM_OBJECT_LOCK(object);
  847                         for (j = i; j < runend; j++) {
  848                                 vm_page_lock(m[j]);
  849                                 vm_page_free(m[j]);
  850                                 vm_page_unlock(m[j]);
  851                         }
  852                         VM_OBJECT_UNLOCK(object);
  853                 } else {
  854                         if (runpg < (count - first)) {
  855                                 VM_OBJECT_LOCK(object);
  856                                 for (i = first + runpg; i < count; i++) {
  857                                         vm_page_lock(m[i]);
  858                                         vm_page_free(m[i]);
  859                                         vm_page_unlock(m[i]);
  860                                 }
  861                                 VM_OBJECT_UNLOCK(object);
  862                                 count = first + runpg;
  863                         }
  864                         break;
  865                 }
  866                 first = runend;
  867         }
  868 
  869         /*
  870          * the first and last page have been calculated now, move input pages
  871          * to be zero based...
  872          */
  873         if (first != 0) {
  874                 m += first;
  875                 count -= first;
  876                 reqpage -= first;
  877         }
  878 
  879         /*
  880          * calculate the file virtual address for the transfer
  881          */
  882         foff = IDX_TO_OFF(m[0]->pindex);
  883 
  884         /*
  885          * calculate the size of the transfer
  886          */
  887         size = count * PAGE_SIZE;
  888         KASSERT(count > 0, ("zero count"));
  889         if ((foff + size) > object->un_pager.vnp.vnp_size)
  890                 size = object->un_pager.vnp.vnp_size - foff;
  891         KASSERT(size > 0, ("zero size"));
  892 
  893         /*
  894          * round up physical size for real devices.
  895          */
  896         if (1) {
  897                 int secmask = bo->bo_bsize - 1;
  898                 KASSERT(secmask < PAGE_SIZE && secmask > 0,
  899                     ("vnode_pager_generic_getpages: sector size %d too large",
  900                     secmask + 1));
  901                 size = (size + secmask) & ~secmask;
  902         }
  903 
  904         bp = getpbuf(&vnode_pbuf_freecnt);
  905         kva = (vm_offset_t) bp->b_data;
  906 
  907         /*
  908          * and map the pages to be read into the kva
  909          */
  910         pmap_qenter(kva, m, count);
  911 
  912         /* build a minimal buffer header */
  913         bp->b_iocmd = BIO_READ;
  914         bp->b_iodone = bdone;
  915         KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
  916         KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
  917         bp->b_rcred = crhold(curthread->td_ucred);
  918         bp->b_wcred = crhold(curthread->td_ucred);
  919         bp->b_blkno = firstaddr;
  920         pbgetbo(bo, bp);
  921         bp->b_vp = vp;
  922         bp->b_bcount = size;
  923         bp->b_bufsize = size;
  924         bp->b_runningbufspace = bp->b_bufsize;
  925         atomic_add_long(&runningbufspace, bp->b_runningbufspace);
  926 
  927         PCPU_INC(cnt.v_vnodein);
  928         PCPU_ADD(cnt.v_vnodepgsin, count);
  929 
  930         /* do the input */
  931         bp->b_iooffset = dbtob(bp->b_blkno);
  932         bstrategy(bp);
  933 
  934         bwait(bp, PVM, "vnread");
  935 
  936         if ((bp->b_ioflags & BIO_ERROR) != 0)
  937                 error = EIO;
  938 
  939         if (!error) {
  940                 if (size != count * PAGE_SIZE)
  941                         bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
  942         }
  943         pmap_qremove(kva, count);
  944 
  945         /*
  946          * free the buffer header back to the swap buffer pool
  947          */
  948         bp->b_vp = NULL;
  949         pbrelbo(bp);
  950         relpbuf(bp, &vnode_pbuf_freecnt);
  951 
  952         VM_OBJECT_LOCK(object);
  953         for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
  954                 vm_page_t mt;
  955 
  956                 nextoff = tfoff + PAGE_SIZE;
  957                 mt = m[i];
  958 
  959                 if (nextoff <= object->un_pager.vnp.vnp_size) {
  960                         /*
  961                          * Read filled up entire page.
  962                          */
  963                         mt->valid = VM_PAGE_BITS_ALL;
  964                         KASSERT(mt->dirty == 0,
  965                             ("vnode_pager_generic_getpages: page %p is dirty",
  966                             mt));
  967                         KASSERT(!pmap_page_is_mapped(mt),
  968                             ("vnode_pager_generic_getpages: page %p is mapped",
  969                             mt));
  970                 } else {
  971                         /*
  972                          * Read did not fill up entire page.
  973                          *
  974                          * Currently we do not set the entire page valid,
  975                          * we just try to clear the piece that we couldn't
  976                          * read.
  977                          */
  978                         vm_page_set_valid(mt, 0,
  979                             object->un_pager.vnp.vnp_size - tfoff);
  980                         KASSERT((mt->dirty & vm_page_bits(0,
  981                             object->un_pager.vnp.vnp_size - tfoff)) == 0,
  982                             ("vnode_pager_generic_getpages: page %p is dirty",
  983                             mt));
  984                 }
  985                 
  986                 if (i != reqpage) {
  987 
  988                         /*
  989                          * whether or not to leave the page activated is up in
  990                          * the air, but we should put the page on a page queue
  991                          * somewhere. (it already is in the object). Result:
  992                          * It appears that empirical results show that
  993                          * deactivating pages is best.
  994                          */
  995 
  996                         /*
  997                          * just in case someone was asking for this page we
  998                          * now tell them that it is ok to use
  999                          */
 1000                         if (!error) {
 1001                                 if (mt->oflags & VPO_WANTED) {
 1002                                         vm_page_lock(mt);
 1003                                         vm_page_activate(mt);
 1004                                         vm_page_unlock(mt);
 1005                                 } else {
 1006                                         vm_page_lock(mt);
 1007                                         vm_page_deactivate(mt);
 1008                                         vm_page_unlock(mt);
 1009                                 }
 1010                                 vm_page_wakeup(mt);
 1011                         } else {
 1012                                 vm_page_lock(mt);
 1013                                 vm_page_free(mt);
 1014                                 vm_page_unlock(mt);
 1015                         }
 1016                 }
 1017         }
 1018         VM_OBJECT_UNLOCK(object);
 1019         if (error) {
 1020                 printf("vnode_pager_getpages: I/O read error\n");
 1021         }
 1022         return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
 1023 }
 1024 
 1025 /*
 1026  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
 1027  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
 1028  * vnode_pager_generic_putpages() to implement the previous behaviour.
 1029  *
 1030  * All other FS's should use the bypass to get to the local media
 1031  * backing vp's VOP_PUTPAGES.
 1032  */
 1033 static void
 1034 vnode_pager_putpages(object, m, count, sync, rtvals)
 1035         vm_object_t object;
 1036         vm_page_t *m;
 1037         int count;
 1038         boolean_t sync;
 1039         int *rtvals;
 1040 {
 1041         int rtval;
 1042         struct vnode *vp;
 1043         int bytes = count * PAGE_SIZE;
 1044 
 1045         /*
 1046          * Force synchronous operation if we are extremely low on memory
 1047          * to prevent a low-memory deadlock.  VOP operations often need to
 1048          * allocate more memory to initiate the I/O ( i.e. do a BMAP 
 1049          * operation ).  The swapper handles the case by limiting the amount
 1050          * of asynchronous I/O, but that sort of solution doesn't scale well
 1051          * for the vnode pager without a lot of work.
 1052          *
 1053          * Also, the backing vnode's iodone routine may not wake the pageout
 1054          * daemon up.  This should be probably be addressed XXX.
 1055          */
 1056 
 1057         if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
 1058                 sync |= OBJPC_SYNC;
 1059 
 1060         /*
 1061          * Call device-specific putpages function
 1062          */
 1063         vp = object->handle;
 1064         VM_OBJECT_UNLOCK(object);
 1065         rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
 1066         KASSERT(rtval != EOPNOTSUPP, 
 1067             ("vnode_pager: stale FS putpages\n"));
 1068         VM_OBJECT_LOCK(object);
 1069 }
 1070 
 1071 
 1072 /*
 1073  * This is now called from local media FS's to operate against their
 1074  * own vnodes if they fail to implement VOP_PUTPAGES.
 1075  *
 1076  * This is typically called indirectly via the pageout daemon and
 1077  * clustering has already typically occured, so in general we ask the
 1078  * underlying filesystem to write the data out asynchronously rather
 1079  * then delayed.
 1080  */
 1081 int
 1082 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
 1083     int flags, int *rtvals)
 1084 {
 1085         int i;
 1086         vm_object_t object;
 1087         vm_page_t m;
 1088         int count;
 1089 
 1090         int maxsize, ncount;
 1091         vm_ooffset_t poffset;
 1092         struct uio auio;
 1093         struct iovec aiov;
 1094         int error;
 1095         int ioflags;
 1096         int ppscheck = 0;
 1097         static struct timeval lastfail;
 1098         static int curfail;
 1099 
 1100         object = vp->v_object;
 1101         count = bytecount / PAGE_SIZE;
 1102 
 1103         for (i = 0; i < count; i++)
 1104                 rtvals[i] = VM_PAGER_ERROR;
 1105 
 1106         if ((int64_t)ma[0]->pindex < 0) {
 1107                 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
 1108                     (long)ma[0]->pindex, (u_long)ma[0]->dirty);
 1109                 rtvals[0] = VM_PAGER_BAD;
 1110                 return VM_PAGER_BAD;
 1111         }
 1112 
 1113         maxsize = count * PAGE_SIZE;
 1114         ncount = count;
 1115 
 1116         poffset = IDX_TO_OFF(ma[0]->pindex);
 1117 
 1118         /*
 1119          * If the page-aligned write is larger then the actual file we
 1120          * have to invalidate pages occuring beyond the file EOF.  However,
 1121          * there is an edge case where a file may not be page-aligned where
 1122          * the last page is partially invalid.  In this case the filesystem
 1123          * may not properly clear the dirty bits for the entire page (which
 1124          * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
 1125          * With the page locked we are free to fix-up the dirty bits here.
 1126          *
 1127          * We do not under any circumstances truncate the valid bits, as
 1128          * this will screw up bogus page replacement.
 1129          */
 1130         VM_OBJECT_LOCK(object);
 1131         if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
 1132                 if (object->un_pager.vnp.vnp_size > poffset) {
 1133                         int pgoff;
 1134 
 1135                         maxsize = object->un_pager.vnp.vnp_size - poffset;
 1136                         ncount = btoc(maxsize);
 1137                         if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
 1138                                 /*
 1139                                  * If the object is locked and the following
 1140                                  * conditions hold, then the page's dirty
 1141                                  * field cannot be concurrently changed by a
 1142                                  * pmap operation.
 1143                                  */
 1144                                 m = ma[ncount - 1];
 1145                                 KASSERT(m->busy > 0,
 1146                 ("vnode_pager_generic_putpages: page %p is not busy", m));
 1147                                 KASSERT((m->aflags & PGA_WRITEABLE) == 0,
 1148                 ("vnode_pager_generic_putpages: page %p is not read-only", m));
 1149                                 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
 1150                                     pgoff);
 1151                         }
 1152                 } else {
 1153                         maxsize = 0;
 1154                         ncount = 0;
 1155                 }
 1156                 if (ncount < count) {
 1157                         for (i = ncount; i < count; i++) {
 1158                                 rtvals[i] = VM_PAGER_BAD;
 1159                         }
 1160                 }
 1161         }
 1162         VM_OBJECT_UNLOCK(object);
 1163 
 1164         /*
 1165          * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
 1166          * rather then a bdwrite() to prevent paging I/O from saturating 
 1167          * the buffer cache.  Dummy-up the sequential heuristic to cause
 1168          * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
 1169          * the system decides how to cluster.
 1170          */
 1171         ioflags = IO_VMIO;
 1172         if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
 1173                 ioflags |= IO_SYNC;
 1174         else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
 1175                 ioflags |= IO_ASYNC;
 1176         ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
 1177         ioflags |= IO_SEQMAX << IO_SEQSHIFT;
 1178 
 1179         aiov.iov_base = (caddr_t) 0;
 1180         aiov.iov_len = maxsize;
 1181         auio.uio_iov = &aiov;
 1182         auio.uio_iovcnt = 1;
 1183         auio.uio_offset = poffset;
 1184         auio.uio_segflg = UIO_NOCOPY;
 1185         auio.uio_rw = UIO_WRITE;
 1186         auio.uio_resid = maxsize;
 1187         auio.uio_td = (struct thread *) 0;
 1188         error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
 1189         PCPU_INC(cnt.v_vnodeout);
 1190         PCPU_ADD(cnt.v_vnodepgsout, ncount);
 1191 
 1192         if (error) {
 1193                 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
 1194                         printf("vnode_pager_putpages: I/O error %d\n", error);
 1195         }
 1196         if (auio.uio_resid) {
 1197                 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
 1198                         printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
 1199                             auio.uio_resid, (u_long)ma[0]->pindex);
 1200         }
 1201         for (i = 0; i < ncount; i++) {
 1202                 rtvals[i] = VM_PAGER_OK;
 1203         }
 1204         return rtvals[0];
 1205 }
 1206 
 1207 void
 1208 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
 1209 {
 1210         vm_object_t obj;
 1211         int i, pos;
 1212 
 1213         if (written == 0)
 1214                 return;
 1215         obj = ma[0]->object;
 1216         VM_OBJECT_LOCK(obj);
 1217         for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
 1218                 if (pos < trunc_page(written)) {
 1219                         rtvals[i] = VM_PAGER_OK;
 1220                         vm_page_undirty(ma[i]);
 1221                 } else {
 1222                         /* Partially written page. */
 1223                         rtvals[i] = VM_PAGER_AGAIN;
 1224                         vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
 1225                 }
 1226         }
 1227         VM_OBJECT_UNLOCK(obj);
 1228 }
 1229 
 1230 void
 1231 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
 1232     vm_offset_t end)
 1233 {
 1234         struct vnode *vp;
 1235         vm_ooffset_t old_wm;
 1236 
 1237         VM_OBJECT_LOCK(object);
 1238         if (object->type != OBJT_VNODE) {
 1239                 VM_OBJECT_UNLOCK(object);
 1240                 return;
 1241         }
 1242         old_wm = object->un_pager.vnp.writemappings;
 1243         object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
 1244         vp = object->handle;
 1245         if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
 1246                 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
 1247                 vp->v_writecount++;
 1248         } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
 1249                 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
 1250                 vp->v_writecount--;
 1251         }
 1252         VM_OBJECT_UNLOCK(object);
 1253 }
 1254 
 1255 void
 1256 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
 1257     vm_offset_t end)
 1258 {
 1259         struct vnode *vp;
 1260         struct mount *mp;
 1261         vm_offset_t inc;
 1262         int vfslocked;
 1263 
 1264         VM_OBJECT_LOCK(object);
 1265 
 1266         /*
 1267          * First, recheck the object type to account for the race when
 1268          * the vnode is reclaimed.
 1269          */
 1270         if (object->type != OBJT_VNODE) {
 1271                 VM_OBJECT_UNLOCK(object);
 1272                 return;
 1273         }
 1274 
 1275         /*
 1276          * Optimize for the case when writemappings is not going to
 1277          * zero.
 1278          */
 1279         inc = end - start;
 1280         if (object->un_pager.vnp.writemappings != inc) {
 1281                 object->un_pager.vnp.writemappings -= inc;
 1282                 VM_OBJECT_UNLOCK(object);
 1283                 return;
 1284         }
 1285 
 1286         vp = object->handle;
 1287         vhold(vp);
 1288         VM_OBJECT_UNLOCK(object);
 1289         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1290         mp = NULL;
 1291         vn_start_write(vp, &mp, V_WAIT);
 1292         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 1293 
 1294         /*
 1295          * Decrement the object's writemappings, by swapping the start
 1296          * and end arguments for vnode_pager_update_writecount().  If
 1297          * there was not a race with vnode reclaimation, then the
 1298          * vnode's v_writecount is decremented.
 1299          */
 1300         vnode_pager_update_writecount(object, end, start);
 1301         VOP_UNLOCK(vp, 0);
 1302         vdrop(vp);
 1303         if (mp != NULL)
 1304                 vn_finished_write(mp);
 1305         VFS_UNLOCK_GIANT(vfslocked);
 1306 }

Cache object: 48b5a0c5f417423e6131306d41d13b1a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.