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/8.0/sys/vm/vnode_pager.c 194990 2009-06-25 18:46:30Z kib $");
   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 
  226                 object->handle = handle;
  227                 VI_LOCK(vp);
  228                 if (vp->v_object != NULL) {
  229                         /*
  230                          * Object has been created while we were sleeping
  231                          */
  232                         VI_UNLOCK(vp);
  233                         vm_object_destroy(object);
  234                         goto retry;
  235                 }
  236                 vp->v_object = object;
  237                 VI_UNLOCK(vp);
  238         } else {
  239                 object->ref_count++;
  240                 VM_OBJECT_UNLOCK(object);
  241         }
  242         vref(vp);
  243         return (object);
  244 }
  245 
  246 /*
  247  *      The object must be locked.
  248  */
  249 static void
  250 vnode_pager_dealloc(object)
  251         vm_object_t object;
  252 {
  253         struct vnode *vp = object->handle;
  254 
  255         if (vp == NULL)
  256                 panic("vnode_pager_dealloc: pager already dealloced");
  257 
  258         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  259         vm_object_pip_wait(object, "vnpdea");
  260 
  261         object->handle = NULL;
  262         object->type = OBJT_DEAD;
  263         if (object->flags & OBJ_DISCONNECTWNT) {
  264                 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
  265                 wakeup(object);
  266         }
  267         ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
  268         vp->v_object = NULL;
  269         vp->v_vflag &= ~VV_TEXT;
  270 }
  271 
  272 static boolean_t
  273 vnode_pager_haspage(object, pindex, before, after)
  274         vm_object_t object;
  275         vm_pindex_t pindex;
  276         int *before;
  277         int *after;
  278 {
  279         struct vnode *vp = object->handle;
  280         daddr_t bn;
  281         int err;
  282         daddr_t reqblock;
  283         int poff;
  284         int bsize;
  285         int pagesperblock, blocksperpage;
  286         int vfslocked;
  287 
  288         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  289         /*
  290          * If no vp or vp is doomed or marked transparent to VM, we do not
  291          * have the page.
  292          */
  293         if (vp == NULL || vp->v_iflag & VI_DOOMED)
  294                 return FALSE;
  295         /*
  296          * If the offset is beyond end of file we do
  297          * not have the page.
  298          */
  299         if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
  300                 return FALSE;
  301 
  302         bsize = vp->v_mount->mnt_stat.f_iosize;
  303         pagesperblock = bsize / PAGE_SIZE;
  304         blocksperpage = 0;
  305         if (pagesperblock > 0) {
  306                 reqblock = pindex / pagesperblock;
  307         } else {
  308                 blocksperpage = (PAGE_SIZE / bsize);
  309                 reqblock = pindex * blocksperpage;
  310         }
  311         VM_OBJECT_UNLOCK(object);
  312         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  313         err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
  314         VFS_UNLOCK_GIANT(vfslocked);
  315         VM_OBJECT_LOCK(object);
  316         if (err)
  317                 return TRUE;
  318         if (bn == -1)
  319                 return FALSE;
  320         if (pagesperblock > 0) {
  321                 poff = pindex - (reqblock * pagesperblock);
  322                 if (before) {
  323                         *before *= pagesperblock;
  324                         *before += poff;
  325                 }
  326                 if (after) {
  327                         int numafter;
  328                         *after *= pagesperblock;
  329                         numafter = pagesperblock - (poff + 1);
  330                         if (IDX_TO_OFF(pindex + numafter) >
  331                             object->un_pager.vnp.vnp_size) {
  332                                 numafter =
  333                                     OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
  334                                     pindex;
  335                         }
  336                         *after += numafter;
  337                 }
  338         } else {
  339                 if (before) {
  340                         *before /= blocksperpage;
  341                 }
  342 
  343                 if (after) {
  344                         *after /= blocksperpage;
  345                 }
  346         }
  347         return TRUE;
  348 }
  349 
  350 /*
  351  * Lets the VM system know about a change in size for a file.
  352  * We adjust our own internal size and flush any cached pages in
  353  * the associated object that are affected by the size change.
  354  *
  355  * Note: this routine may be invoked as a result of a pager put
  356  * operation (possibly at object termination time), so we must be careful.
  357  */
  358 void
  359 vnode_pager_setsize(vp, nsize)
  360         struct vnode *vp;
  361         vm_ooffset_t nsize;
  362 {
  363         vm_object_t object;
  364         vm_page_t m;
  365         vm_pindex_t nobjsize;
  366 
  367         if ((object = vp->v_object) == NULL)
  368                 return;
  369 /*      ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
  370         VM_OBJECT_LOCK(object);
  371         if (nsize == object->un_pager.vnp.vnp_size) {
  372                 /*
  373                  * Hasn't changed size
  374                  */
  375                 VM_OBJECT_UNLOCK(object);
  376                 return;
  377         }
  378         nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
  379         if (nsize < object->un_pager.vnp.vnp_size) {
  380                 /*
  381                  * File has shrunk. Toss any cached pages beyond the new EOF.
  382                  */
  383                 if (nobjsize < object->size)
  384                         vm_object_page_remove(object, nobjsize, object->size,
  385                             FALSE);
  386                 /*
  387                  * this gets rid of garbage at the end of a page that is now
  388                  * only partially backed by the vnode.
  389                  *
  390                  * XXX for some reason (I don't know yet), if we take a
  391                  * completely invalid page and mark it partially valid
  392                  * it can screw up NFS reads, so we don't allow the case.
  393                  */
  394                 if ((nsize & PAGE_MASK) &&
  395                     (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
  396                     m->valid != 0) {
  397                         int base = (int)nsize & PAGE_MASK;
  398                         int size = PAGE_SIZE - base;
  399 
  400                         /*
  401                          * Clear out partial-page garbage in case
  402                          * the page has been mapped.
  403                          */
  404                         pmap_zero_page_area(m, base, size);
  405 
  406                         /*
  407                          * Update the valid bits to reflect the blocks that
  408                          * have been zeroed.  Some of these valid bits may
  409                          * have already been set.
  410                          */
  411                         vm_page_set_valid(m, base, size);
  412 
  413                         /*
  414                          * Round "base" to the next block boundary so that the
  415                          * dirty bit for a partially zeroed block is not
  416                          * cleared.
  417                          */
  418                         base = roundup2(base, DEV_BSIZE);
  419 
  420                         /*
  421                          * Clear out partial-page dirty bits.
  422                          *
  423                          * note that we do not clear out the valid
  424                          * bits.  This would prevent bogus_page
  425                          * replacement from working properly.
  426                          */
  427                         vm_page_lock_queues();
  428                         vm_page_clear_dirty(m, base, PAGE_SIZE - base);
  429                         vm_page_unlock_queues();
  430                 } else if ((nsize & PAGE_MASK) &&
  431                     __predict_false(object->cache != NULL)) {
  432                         vm_page_cache_free(object, OFF_TO_IDX(nsize),
  433                             nobjsize);
  434                 }
  435         }
  436         object->un_pager.vnp.vnp_size = nsize;
  437         object->size = nobjsize;
  438         VM_OBJECT_UNLOCK(object);
  439 }
  440 
  441 /*
  442  * calculate the linear (byte) disk address of specified virtual
  443  * file address
  444  */
  445 static int
  446 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
  447     int *run)
  448 {
  449         int bsize;
  450         int err;
  451         daddr_t vblock;
  452         daddr_t voffset;
  453 
  454         if (address < 0)
  455                 return -1;
  456 
  457         if (vp->v_iflag & VI_DOOMED)
  458                 return -1;
  459 
  460         bsize = vp->v_mount->mnt_stat.f_iosize;
  461         vblock = address / bsize;
  462         voffset = address % bsize;
  463 
  464         err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
  465         if (err == 0) {
  466                 if (*rtaddress != -1)
  467                         *rtaddress += voffset / DEV_BSIZE;
  468                 if (run) {
  469                         *run += 1;
  470                         *run *= bsize/PAGE_SIZE;
  471                         *run -= voffset/PAGE_SIZE;
  472                 }
  473         }
  474 
  475         return (err);
  476 }
  477 
  478 /*
  479  * small block filesystem vnode pager input
  480  */
  481 static int
  482 vnode_pager_input_smlfs(object, m)
  483         vm_object_t object;
  484         vm_page_t m;
  485 {
  486         int bits, i;
  487         struct vnode *vp;
  488         struct bufobj *bo;
  489         struct buf *bp;
  490         struct sf_buf *sf;
  491         daddr_t fileaddr;
  492         vm_offset_t bsize;
  493         int error = 0;
  494 
  495         vp = object->handle;
  496         if (vp->v_iflag & VI_DOOMED)
  497                 return VM_PAGER_BAD;
  498 
  499         bsize = vp->v_mount->mnt_stat.f_iosize;
  500 
  501         VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
  502 
  503         sf = sf_buf_alloc(m, 0);
  504 
  505         for (i = 0; i < PAGE_SIZE / bsize; i++) {
  506                 vm_ooffset_t address;
  507 
  508                 bits = vm_page_bits(i * bsize, bsize);
  509                 if (m->valid & bits)
  510                         continue;
  511 
  512                 address = IDX_TO_OFF(m->pindex) + i * bsize;
  513                 if (address >= object->un_pager.vnp.vnp_size) {
  514                         fileaddr = -1;
  515                 } else {
  516                         error = vnode_pager_addr(vp, address, &fileaddr, NULL);
  517                         if (error)
  518                                 break;
  519                 }
  520                 if (fileaddr != -1) {
  521                         bp = getpbuf(&vnode_pbuf_freecnt);
  522 
  523                         /* build a minimal buffer header */
  524                         bp->b_iocmd = BIO_READ;
  525                         bp->b_iodone = bdone;
  526                         KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
  527                         KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
  528                         bp->b_rcred = crhold(curthread->td_ucred);
  529                         bp->b_wcred = crhold(curthread->td_ucred);
  530                         bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
  531                         bp->b_blkno = fileaddr;
  532                         pbgetbo(bo, bp);
  533                         bp->b_bcount = bsize;
  534                         bp->b_bufsize = bsize;
  535                         bp->b_runningbufspace = bp->b_bufsize;
  536                         atomic_add_long(&runningbufspace, bp->b_runningbufspace);
  537 
  538                         /* do the input */
  539                         bp->b_iooffset = dbtob(bp->b_blkno);
  540                         bstrategy(bp);
  541 
  542                         bwait(bp, PVM, "vnsrd");
  543 
  544                         if ((bp->b_ioflags & BIO_ERROR) != 0)
  545                                 error = EIO;
  546 
  547                         /*
  548                          * free the buffer header back to the swap buffer pool
  549                          */
  550                         pbrelbo(bp);
  551                         relpbuf(bp, &vnode_pbuf_freecnt);
  552                         if (error)
  553                                 break;
  554                 } else
  555                         bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
  556                 KASSERT((m->dirty & bits) == 0,
  557                     ("vnode_pager_input_smlfs: page %p is dirty", m));
  558                 VM_OBJECT_LOCK(object);
  559                 m->valid |= bits;
  560                 VM_OBJECT_UNLOCK(object);
  561         }
  562         sf_buf_free(sf);
  563         if (error) {
  564                 return VM_PAGER_ERROR;
  565         }
  566         return VM_PAGER_OK;
  567 }
  568 
  569 /*
  570  * old style vnode pager input routine
  571  */
  572 static int
  573 vnode_pager_input_old(object, m)
  574         vm_object_t object;
  575         vm_page_t m;
  576 {
  577         struct uio auio;
  578         struct iovec aiov;
  579         int error;
  580         int size;
  581         struct sf_buf *sf;
  582         struct vnode *vp;
  583 
  584         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  585         error = 0;
  586 
  587         /*
  588          * Return failure if beyond current EOF
  589          */
  590         if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
  591                 return VM_PAGER_BAD;
  592         } else {
  593                 size = PAGE_SIZE;
  594                 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
  595                         size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
  596                 vp = object->handle;
  597                 VM_OBJECT_UNLOCK(object);
  598 
  599                 /*
  600                  * Allocate a kernel virtual address and initialize so that
  601                  * we can use VOP_READ/WRITE routines.
  602                  */
  603                 sf = sf_buf_alloc(m, 0);
  604 
  605                 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
  606                 aiov.iov_len = size;
  607                 auio.uio_iov = &aiov;
  608                 auio.uio_iovcnt = 1;
  609                 auio.uio_offset = IDX_TO_OFF(m->pindex);
  610                 auio.uio_segflg = UIO_SYSSPACE;
  611                 auio.uio_rw = UIO_READ;
  612                 auio.uio_resid = size;
  613                 auio.uio_td = curthread;
  614 
  615                 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
  616                 if (!error) {
  617                         int count = size - auio.uio_resid;
  618 
  619                         if (count == 0)
  620                                 error = EINVAL;
  621                         else if (count != PAGE_SIZE)
  622                                 bzero((caddr_t)sf_buf_kva(sf) + count,
  623                                     PAGE_SIZE - count);
  624                 }
  625                 sf_buf_free(sf);
  626 
  627                 VM_OBJECT_LOCK(object);
  628         }
  629         KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
  630         if (!error)
  631                 m->valid = VM_PAGE_BITS_ALL;
  632         return error ? VM_PAGER_ERROR : VM_PAGER_OK;
  633 }
  634 
  635 /*
  636  * generic vnode pager input routine
  637  */
  638 
  639 /*
  640  * Local media VFS's that do not implement their own VOP_GETPAGES
  641  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
  642  * to implement the previous behaviour.
  643  *
  644  * All other FS's should use the bypass to get to the local media
  645  * backing vp's VOP_GETPAGES.
  646  */
  647 static int
  648 vnode_pager_getpages(object, m, count, reqpage)
  649         vm_object_t object;
  650         vm_page_t *m;
  651         int count;
  652         int reqpage;
  653 {
  654         int rtval;
  655         struct vnode *vp;
  656         int bytes = count * PAGE_SIZE;
  657         int vfslocked;
  658 
  659         vp = object->handle;
  660         VM_OBJECT_UNLOCK(object);
  661         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  662         rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
  663         KASSERT(rtval != EOPNOTSUPP,
  664             ("vnode_pager: FS getpages not implemented\n"));
  665         VFS_UNLOCK_GIANT(vfslocked);
  666         VM_OBJECT_LOCK(object);
  667         return rtval;
  668 }
  669 
  670 /*
  671  * This is now called from local media FS's to operate against their
  672  * own vnodes if they fail to implement VOP_GETPAGES.
  673  */
  674 int
  675 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
  676         struct vnode *vp;
  677         vm_page_t *m;
  678         int bytecount;
  679         int reqpage;
  680 {
  681         vm_object_t object;
  682         vm_offset_t kva;
  683         off_t foff, tfoff, nextoff;
  684         int i, j, size, bsize, first;
  685         daddr_t firstaddr, reqblock;
  686         struct bufobj *bo;
  687         int runpg;
  688         int runend;
  689         struct buf *bp;
  690         int count;
  691         int error;
  692 
  693         object = vp->v_object;
  694         count = bytecount / PAGE_SIZE;
  695 
  696         KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
  697             ("vnode_pager_generic_getpages does not support devices"));
  698         if (vp->v_iflag & VI_DOOMED)
  699                 return VM_PAGER_BAD;
  700 
  701         bsize = vp->v_mount->mnt_stat.f_iosize;
  702 
  703         /* get the UNDERLYING device for the file with VOP_BMAP() */
  704 
  705         /*
  706          * originally, we did not check for an error return value -- assuming
  707          * an fs always has a bmap entry point -- that assumption is wrong!!!
  708          */
  709         foff = IDX_TO_OFF(m[reqpage]->pindex);
  710 
  711         /*
  712          * if we can't bmap, use old VOP code
  713          */
  714         error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
  715         if (error == EOPNOTSUPP) {
  716                 VM_OBJECT_LOCK(object);
  717                 vm_page_lock_queues();
  718                 for (i = 0; i < count; i++)
  719                         if (i != reqpage)
  720                                 vm_page_free(m[i]);
  721                 vm_page_unlock_queues();
  722                 PCPU_INC(cnt.v_vnodein);
  723                 PCPU_INC(cnt.v_vnodepgsin);
  724                 error = vnode_pager_input_old(object, m[reqpage]);
  725                 VM_OBJECT_UNLOCK(object);
  726                 return (error);
  727         } else if (error != 0) {
  728                 VM_OBJECT_LOCK(object);
  729                 vm_page_lock_queues();
  730                 for (i = 0; i < count; i++)
  731                         if (i != reqpage)
  732                                 vm_page_free(m[i]);
  733                 vm_page_unlock_queues();
  734                 VM_OBJECT_UNLOCK(object);
  735                 return (VM_PAGER_ERROR);
  736 
  737                 /*
  738                  * if the blocksize is smaller than a page size, then use
  739                  * special small filesystem code.  NFS sometimes has a small
  740                  * blocksize, but it can handle large reads itself.
  741                  */
  742         } else if ((PAGE_SIZE / bsize) > 1 &&
  743             (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
  744                 VM_OBJECT_LOCK(object);
  745                 vm_page_lock_queues();
  746                 for (i = 0; i < count; i++)
  747                         if (i != reqpage)
  748                                 vm_page_free(m[i]);
  749                 vm_page_unlock_queues();
  750                 VM_OBJECT_UNLOCK(object);
  751                 PCPU_INC(cnt.v_vnodein);
  752                 PCPU_INC(cnt.v_vnodepgsin);
  753                 return vnode_pager_input_smlfs(object, m[reqpage]);
  754         }
  755 
  756         /*
  757          * If we have a completely valid page available to us, we can
  758          * clean up and return.  Otherwise we have to re-read the
  759          * media.
  760          */
  761         VM_OBJECT_LOCK(object);
  762         if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
  763                 vm_page_lock_queues();
  764                 for (i = 0; i < count; i++)
  765                         if (i != reqpage)
  766                                 vm_page_free(m[i]);
  767                 vm_page_unlock_queues();
  768                 VM_OBJECT_UNLOCK(object);
  769                 return VM_PAGER_OK;
  770         } else if (reqblock == -1) {
  771                 pmap_zero_page(m[reqpage]);
  772                 KASSERT(m[reqpage]->dirty == 0,
  773                     ("vnode_pager_generic_getpages: page %p is dirty", m));
  774                 m[reqpage]->valid = VM_PAGE_BITS_ALL;
  775                 vm_page_lock_queues();
  776                 for (i = 0; i < count; i++)
  777                         if (i != reqpage)
  778                                 vm_page_free(m[i]);
  779                 vm_page_unlock_queues();
  780                 VM_OBJECT_UNLOCK(object);
  781                 return (VM_PAGER_OK);
  782         }
  783         m[reqpage]->valid = 0;
  784         VM_OBJECT_UNLOCK(object);
  785 
  786         /*
  787          * here on direct device I/O
  788          */
  789         firstaddr = -1;
  790 
  791         /*
  792          * calculate the run that includes the required page
  793          */
  794         for (first = 0, i = 0; i < count; i = runend) {
  795                 if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
  796                     &runpg) != 0) {
  797                         VM_OBJECT_LOCK(object);
  798                         vm_page_lock_queues();
  799                         for (; i < count; i++)
  800                                 if (i != reqpage)
  801                                         vm_page_free(m[i]);
  802                         vm_page_unlock_queues();
  803                         VM_OBJECT_UNLOCK(object);
  804                         return (VM_PAGER_ERROR);
  805                 }
  806                 if (firstaddr == -1) {
  807                         VM_OBJECT_LOCK(object);
  808                         if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
  809                                 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
  810                                     (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
  811                                     (uintmax_t)foff,
  812                                     (uintmax_t)
  813                                     (object->un_pager.vnp.vnp_size >> 32),
  814                                     (uintmax_t)object->un_pager.vnp.vnp_size);
  815                         }
  816                         vm_page_lock_queues();
  817                         vm_page_free(m[i]);
  818                         vm_page_unlock_queues();
  819                         VM_OBJECT_UNLOCK(object);
  820                         runend = i + 1;
  821                         first = runend;
  822                         continue;
  823                 }
  824                 runend = i + runpg;
  825                 if (runend <= reqpage) {
  826                         VM_OBJECT_LOCK(object);
  827                         vm_page_lock_queues();
  828                         for (j = i; j < runend; j++)
  829                                 vm_page_free(m[j]);
  830                         vm_page_unlock_queues();
  831                         VM_OBJECT_UNLOCK(object);
  832                 } else {
  833                         if (runpg < (count - first)) {
  834                                 VM_OBJECT_LOCK(object);
  835                                 vm_page_lock_queues();
  836                                 for (i = first + runpg; i < count; i++)
  837                                         vm_page_free(m[i]);
  838                                 vm_page_unlock_queues();
  839                                 VM_OBJECT_UNLOCK(object);
  840                                 count = first + runpg;
  841                         }
  842                         break;
  843                 }
  844                 first = runend;
  845         }
  846 
  847         /*
  848          * the first and last page have been calculated now, move input pages
  849          * to be zero based...
  850          */
  851         if (first != 0) {
  852                 m += first;
  853                 count -= first;
  854                 reqpage -= first;
  855         }
  856 
  857         /*
  858          * calculate the file virtual address for the transfer
  859          */
  860         foff = IDX_TO_OFF(m[0]->pindex);
  861 
  862         /*
  863          * calculate the size of the transfer
  864          */
  865         size = count * PAGE_SIZE;
  866         KASSERT(count > 0, ("zero count"));
  867         if ((foff + size) > object->un_pager.vnp.vnp_size)
  868                 size = object->un_pager.vnp.vnp_size - foff;
  869         KASSERT(size > 0, ("zero size"));
  870 
  871         /*
  872          * round up physical size for real devices.
  873          */
  874         if (1) {
  875                 int secmask = bo->bo_bsize - 1;
  876                 KASSERT(secmask < PAGE_SIZE && secmask > 0,
  877                     ("vnode_pager_generic_getpages: sector size %d too large",
  878                     secmask + 1));
  879                 size = (size + secmask) & ~secmask;
  880         }
  881 
  882         bp = getpbuf(&vnode_pbuf_freecnt);
  883         kva = (vm_offset_t) bp->b_data;
  884 
  885         /*
  886          * and map the pages to be read into the kva
  887          */
  888         pmap_qenter(kva, m, count);
  889 
  890         /* build a minimal buffer header */
  891         bp->b_iocmd = BIO_READ;
  892         bp->b_iodone = bdone;
  893         KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
  894         KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
  895         bp->b_rcred = crhold(curthread->td_ucred);
  896         bp->b_wcred = crhold(curthread->td_ucred);
  897         bp->b_blkno = firstaddr;
  898         pbgetbo(bo, bp);
  899         bp->b_bcount = size;
  900         bp->b_bufsize = size;
  901         bp->b_runningbufspace = bp->b_bufsize;
  902         atomic_add_long(&runningbufspace, bp->b_runningbufspace);
  903 
  904         PCPU_INC(cnt.v_vnodein);
  905         PCPU_ADD(cnt.v_vnodepgsin, count);
  906 
  907         /* do the input */
  908         bp->b_iooffset = dbtob(bp->b_blkno);
  909         bstrategy(bp);
  910 
  911         bwait(bp, PVM, "vnread");
  912 
  913         if ((bp->b_ioflags & BIO_ERROR) != 0)
  914                 error = EIO;
  915 
  916         if (!error) {
  917                 if (size != count * PAGE_SIZE)
  918                         bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
  919         }
  920         pmap_qremove(kva, count);
  921 
  922         /*
  923          * free the buffer header back to the swap buffer pool
  924          */
  925         pbrelbo(bp);
  926         relpbuf(bp, &vnode_pbuf_freecnt);
  927 
  928         VM_OBJECT_LOCK(object);
  929         vm_page_lock_queues();
  930         for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
  931                 vm_page_t mt;
  932 
  933                 nextoff = tfoff + PAGE_SIZE;
  934                 mt = m[i];
  935 
  936                 if (nextoff <= object->un_pager.vnp.vnp_size) {
  937                         /*
  938                          * Read filled up entire page.
  939                          */
  940                         mt->valid = VM_PAGE_BITS_ALL;
  941                         KASSERT(mt->dirty == 0,
  942                             ("vnode_pager_generic_getpages: page %p is dirty",
  943                             mt));
  944                         KASSERT(!pmap_page_is_mapped(mt),
  945                             ("vnode_pager_generic_getpages: page %p is mapped",
  946                             mt));
  947                 } else {
  948                         /*
  949                          * Read did not fill up entire page.
  950                          *
  951                          * Currently we do not set the entire page valid,
  952                          * we just try to clear the piece that we couldn't
  953                          * read.
  954                          */
  955                         vm_page_set_valid(mt, 0,
  956                             object->un_pager.vnp.vnp_size - tfoff);
  957                         KASSERT((mt->dirty & vm_page_bits(0,
  958                             object->un_pager.vnp.vnp_size - tfoff)) == 0,
  959                             ("vnode_pager_generic_getpages: page %p is dirty",
  960                             mt));
  961                 }
  962                 
  963                 if (i != reqpage) {
  964 
  965                         /*
  966                          * whether or not to leave the page activated is up in
  967                          * the air, but we should put the page on a page queue
  968                          * somewhere. (it already is in the object). Result:
  969                          * It appears that empirical results show that
  970                          * deactivating pages is best.
  971                          */
  972 
  973                         /*
  974                          * just in case someone was asking for this page we
  975                          * now tell them that it is ok to use
  976                          */
  977                         if (!error) {
  978                                 if (mt->oflags & VPO_WANTED)
  979                                         vm_page_activate(mt);
  980                                 else
  981                                         vm_page_deactivate(mt);
  982                                 vm_page_wakeup(mt);
  983                         } else {
  984                                 vm_page_free(mt);
  985                         }
  986                 }
  987         }
  988         vm_page_unlock_queues();
  989         VM_OBJECT_UNLOCK(object);
  990         if (error) {
  991                 printf("vnode_pager_getpages: I/O read error\n");
  992         }
  993         return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
  994 }
  995 
  996 /*
  997  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
  998  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
  999  * vnode_pager_generic_putpages() to implement the previous behaviour.
 1000  *
 1001  * All other FS's should use the bypass to get to the local media
 1002  * backing vp's VOP_PUTPAGES.
 1003  */
 1004 static void
 1005 vnode_pager_putpages(object, m, count, sync, rtvals)
 1006         vm_object_t object;
 1007         vm_page_t *m;
 1008         int count;
 1009         boolean_t sync;
 1010         int *rtvals;
 1011 {
 1012         int rtval;
 1013         struct vnode *vp;
 1014         struct mount *mp;
 1015         int bytes = count * PAGE_SIZE;
 1016 
 1017         /*
 1018          * Force synchronous operation if we are extremely low on memory
 1019          * to prevent a low-memory deadlock.  VOP operations often need to
 1020          * allocate more memory to initiate the I/O ( i.e. do a BMAP 
 1021          * operation ).  The swapper handles the case by limiting the amount
 1022          * of asynchronous I/O, but that sort of solution doesn't scale well
 1023          * for the vnode pager without a lot of work.
 1024          *
 1025          * Also, the backing vnode's iodone routine may not wake the pageout
 1026          * daemon up.  This should be probably be addressed XXX.
 1027          */
 1028 
 1029         if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
 1030                 sync |= OBJPC_SYNC;
 1031 
 1032         /*
 1033          * Call device-specific putpages function
 1034          */
 1035         vp = object->handle;
 1036         VM_OBJECT_UNLOCK(object);
 1037         if (vp->v_type != VREG)
 1038                 mp = NULL;
 1039         rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
 1040         KASSERT(rtval != EOPNOTSUPP, 
 1041             ("vnode_pager: stale FS putpages\n"));
 1042         VM_OBJECT_LOCK(object);
 1043 }
 1044 
 1045 
 1046 /*
 1047  * This is now called from local media FS's to operate against their
 1048  * own vnodes if they fail to implement VOP_PUTPAGES.
 1049  *
 1050  * This is typically called indirectly via the pageout daemon and
 1051  * clustering has already typically occured, so in general we ask the
 1052  * underlying filesystem to write the data out asynchronously rather
 1053  * then delayed.
 1054  */
 1055 int
 1056 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
 1057         struct vnode *vp;
 1058         vm_page_t *m;
 1059         int bytecount;
 1060         int flags;
 1061         int *rtvals;
 1062 {
 1063         int i;
 1064         vm_object_t object;
 1065         int count;
 1066 
 1067         int maxsize, ncount;
 1068         vm_ooffset_t poffset;
 1069         struct uio auio;
 1070         struct iovec aiov;
 1071         int error;
 1072         int ioflags;
 1073         int ppscheck = 0;
 1074         static struct timeval lastfail;
 1075         static int curfail;
 1076 
 1077         object = vp->v_object;
 1078         count = bytecount / PAGE_SIZE;
 1079 
 1080         for (i = 0; i < count; i++)
 1081                 rtvals[i] = VM_PAGER_AGAIN;
 1082 
 1083         if ((int64_t)m[0]->pindex < 0) {
 1084                 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
 1085                         (long)m[0]->pindex, (u_long)m[0]->dirty);
 1086                 rtvals[0] = VM_PAGER_BAD;
 1087                 return VM_PAGER_BAD;
 1088         }
 1089 
 1090         maxsize = count * PAGE_SIZE;
 1091         ncount = count;
 1092 
 1093         poffset = IDX_TO_OFF(m[0]->pindex);
 1094 
 1095         /*
 1096          * If the page-aligned write is larger then the actual file we
 1097          * have to invalidate pages occuring beyond the file EOF.  However,
 1098          * there is an edge case where a file may not be page-aligned where
 1099          * the last page is partially invalid.  In this case the filesystem
 1100          * may not properly clear the dirty bits for the entire page (which
 1101          * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
 1102          * With the page locked we are free to fix-up the dirty bits here.
 1103          *
 1104          * We do not under any circumstances truncate the valid bits, as
 1105          * this will screw up bogus page replacement.
 1106          */
 1107         if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
 1108                 if (object->un_pager.vnp.vnp_size > poffset) {
 1109                         int pgoff;
 1110 
 1111                         maxsize = object->un_pager.vnp.vnp_size - poffset;
 1112                         ncount = btoc(maxsize);
 1113                         if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
 1114                                 vm_page_lock_queues();
 1115                                 vm_page_clear_dirty(m[ncount - 1], pgoff,
 1116                                         PAGE_SIZE - pgoff);
 1117                                 vm_page_unlock_queues();
 1118                         }
 1119                 } else {
 1120                         maxsize = 0;
 1121                         ncount = 0;
 1122                 }
 1123                 if (ncount < count) {
 1124                         for (i = ncount; i < count; i++) {
 1125                                 rtvals[i] = VM_PAGER_BAD;
 1126                         }
 1127                 }
 1128         }
 1129 
 1130         /*
 1131          * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
 1132          * rather then a bdwrite() to prevent paging I/O from saturating 
 1133          * the buffer cache.  Dummy-up the sequential heuristic to cause
 1134          * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
 1135          * the system decides how to cluster.
 1136          */
 1137         ioflags = IO_VMIO;
 1138         if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
 1139                 ioflags |= IO_SYNC;
 1140         else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
 1141                 ioflags |= IO_ASYNC;
 1142         ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
 1143         ioflags |= IO_SEQMAX << IO_SEQSHIFT;
 1144 
 1145         aiov.iov_base = (caddr_t) 0;
 1146         aiov.iov_len = maxsize;
 1147         auio.uio_iov = &aiov;
 1148         auio.uio_iovcnt = 1;
 1149         auio.uio_offset = poffset;
 1150         auio.uio_segflg = UIO_NOCOPY;
 1151         auio.uio_rw = UIO_WRITE;
 1152         auio.uio_resid = maxsize;
 1153         auio.uio_td = (struct thread *) 0;
 1154         error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
 1155         PCPU_INC(cnt.v_vnodeout);
 1156         PCPU_ADD(cnt.v_vnodepgsout, ncount);
 1157 
 1158         if (error) {
 1159                 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
 1160                         printf("vnode_pager_putpages: I/O error %d\n", error);
 1161         }
 1162         if (auio.uio_resid) {
 1163                 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
 1164                         printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
 1165                             auio.uio_resid, (u_long)m[0]->pindex);
 1166         }
 1167         for (i = 0; i < ncount; i++) {
 1168                 rtvals[i] = VM_PAGER_OK;
 1169         }
 1170         return rtvals[0];
 1171 }

Cache object: 7a17e362e872ffefd772248f022d6c3e


[ 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.