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: src/sys/vm/vnode_pager.c,v 1.196.2.8 2005/08/16 15:09:44 sobomax Exp $");
   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/conf.h>
   65 #include <sys/sf_buf.h>
   66 
   67 #include <machine/atomic.h>
   68 
   69 #include <vm/vm.h>
   70 #include <vm/vm_object.h>
   71 #include <vm/vm_page.h>
   72 #include <vm/vm_pager.h>
   73 #include <vm/vm_map.h>
   74 #include <vm/vnode_pager.h>
   75 #include <vm/vm_extern.h>
   76 
   77 static daddr_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
   78                                          int *run);
   79 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
   80 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
   81 static void vnode_pager_dealloc(vm_object_t);
   82 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
   83 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
   84 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
   85 
   86 struct pagerops vnodepagerops = {
   87         .pgo_alloc =    vnode_pager_alloc,
   88         .pgo_dealloc =  vnode_pager_dealloc,
   89         .pgo_getpages = vnode_pager_getpages,
   90         .pgo_putpages = vnode_pager_putpages,
   91         .pgo_haspage =  vnode_pager_haspage,
   92 };
   93 
   94 int vnode_pbuf_freecnt;
   95 
   96 /*
   97  * Allocate (or lookup) pager for a vnode.
   98  * Handle is a vnode pointer.
   99  *
  100  * MPSAFE
  101  */
  102 vm_object_t
  103 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
  104                   vm_ooffset_t offset)
  105 {
  106         vm_object_t object;
  107         struct vnode *vp;
  108 
  109         /*
  110          * Pageout to vnode, no can do yet.
  111          */
  112         if (handle == NULL)
  113                 return (NULL);
  114 
  115         vp = (struct vnode *) handle;
  116 
  117         ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
  118 
  119         /*
  120          * Prevent race condition when allocating the object. This
  121          * can happen with NFS vnodes since the nfsnode isn't locked.
  122          */
  123         VI_LOCK(vp);
  124         while (vp->v_iflag & VI_OLOCK) {
  125                 vp->v_iflag |= VI_OWANT;
  126                 msleep(vp, VI_MTX(vp), PVM, "vnpobj", 0);
  127         }
  128         vp->v_iflag |= VI_OLOCK;
  129         VI_UNLOCK(vp);
  130 
  131         /*
  132          * If the object is being terminated, wait for it to
  133          * go away.
  134          */
  135         while ((object = vp->v_object) != NULL) {
  136                 VM_OBJECT_LOCK(object);
  137                 if ((object->flags & OBJ_DEAD) == 0)
  138                         break;
  139                 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
  140                 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
  141         }
  142 
  143         if (vp->v_usecount == 0)
  144                 panic("vnode_pager_alloc: no vnode reference");
  145 
  146         if (object == NULL) {
  147                 /*
  148                  * And an object of the appropriate size
  149                  */
  150                 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
  151 
  152                 object->un_pager.vnp.vnp_size = size;
  153 
  154                 object->handle = handle;
  155                 vp->v_object = object;
  156         } else {
  157                 object->ref_count++;
  158                 VM_OBJECT_UNLOCK(object);
  159         }
  160         VI_LOCK(vp);
  161         vp->v_usecount++;
  162         vp->v_iflag &= ~VI_OLOCK;
  163         if (vp->v_iflag & VI_OWANT) {
  164                 vp->v_iflag &= ~VI_OWANT;
  165                 wakeup(vp);
  166         }
  167         VI_UNLOCK(vp);
  168         return (object);
  169 }
  170 
  171 /*
  172  *      The object must be locked.
  173  */
  174 static void
  175 vnode_pager_dealloc(object)
  176         vm_object_t object;
  177 {
  178         struct vnode *vp = object->handle;
  179 
  180         if (vp == NULL)
  181                 panic("vnode_pager_dealloc: pager already dealloced");
  182 
  183         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  184         vm_object_pip_wait(object, "vnpdea");
  185 
  186         object->handle = NULL;
  187         object->type = OBJT_DEAD;
  188         if (object->flags & OBJ_DISCONNECTWNT) {
  189                 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
  190                 wakeup(object);
  191         }
  192         ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc");
  193         vp->v_object = NULL;
  194         vp->v_vflag &= ~(VV_TEXT | VV_OBJBUF);
  195 }
  196 
  197 static boolean_t
  198 vnode_pager_haspage(object, pindex, before, after)
  199         vm_object_t object;
  200         vm_pindex_t pindex;
  201         int *before;
  202         int *after;
  203 {
  204         struct vnode *vp = object->handle;
  205         daddr_t bn;
  206         int err;
  207         daddr_t reqblock;
  208         int poff;
  209         int bsize;
  210         int pagesperblock, blocksperpage;
  211 
  212         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  213         /*
  214          * If no vp or vp is doomed or marked transparent to VM, we do not
  215          * have the page.
  216          */
  217         if (vp == NULL)
  218                 return FALSE;
  219 
  220         VI_LOCK(vp);
  221         if (vp->v_iflag & VI_DOOMED) {
  222                 VI_UNLOCK(vp);
  223                 return FALSE;
  224         }
  225         VI_UNLOCK(vp);
  226         /*
  227          * If filesystem no longer mounted or offset beyond end of file we do
  228          * not have the page.
  229          */
  230         if ((vp->v_mount == NULL) ||
  231             (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
  232                 return FALSE;
  233 
  234         bsize = vp->v_mount->mnt_stat.f_iosize;
  235         pagesperblock = bsize / PAGE_SIZE;
  236         blocksperpage = 0;
  237         if (pagesperblock > 0) {
  238                 reqblock = pindex / pagesperblock;
  239         } else {
  240                 blocksperpage = (PAGE_SIZE / bsize);
  241                 reqblock = pindex * blocksperpage;
  242         }
  243         VM_OBJECT_UNLOCK(object);
  244         mtx_lock(&Giant);
  245         err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
  246         mtx_unlock(&Giant);
  247         VM_OBJECT_LOCK(object);
  248         if (err)
  249                 return TRUE;
  250         if (bn == -1)
  251                 return FALSE;
  252         if (pagesperblock > 0) {
  253                 poff = pindex - (reqblock * pagesperblock);
  254                 if (before) {
  255                         *before *= pagesperblock;
  256                         *before += poff;
  257                 }
  258                 if (after) {
  259                         int numafter;
  260                         *after *= pagesperblock;
  261                         numafter = pagesperblock - (poff + 1);
  262                         if (IDX_TO_OFF(pindex + numafter) >
  263                             object->un_pager.vnp.vnp_size) {
  264                                 numafter =
  265                                     OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
  266                                     pindex;
  267                         }
  268                         *after += numafter;
  269                 }
  270         } else {
  271                 if (before) {
  272                         *before /= blocksperpage;
  273                 }
  274 
  275                 if (after) {
  276                         *after /= blocksperpage;
  277                 }
  278         }
  279         return TRUE;
  280 }
  281 
  282 /*
  283  * Lets the VM system know about a change in size for a file.
  284  * We adjust our own internal size and flush any cached pages in
  285  * the associated object that are affected by the size change.
  286  *
  287  * Note: this routine may be invoked as a result of a pager put
  288  * operation (possibly at object termination time), so we must be careful.
  289  */
  290 void
  291 vnode_pager_setsize(vp, nsize)
  292         struct vnode *vp;
  293         vm_ooffset_t nsize;
  294 {
  295         vm_object_t object;
  296         vm_page_t m;
  297         vm_pindex_t nobjsize;
  298 
  299         if ((object = vp->v_object) == NULL)
  300                 return;
  301         VM_OBJECT_LOCK(object);
  302         if (nsize == object->un_pager.vnp.vnp_size) {
  303                 /*
  304                  * Hasn't changed size
  305                  */
  306                 VM_OBJECT_UNLOCK(object);
  307                 return;
  308         }
  309         nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
  310         if (nsize < object->un_pager.vnp.vnp_size) {
  311                 /*
  312                  * File has shrunk. Toss any cached pages beyond the new EOF.
  313                  */
  314                 if (nobjsize < object->size)
  315                         vm_object_page_remove(object, nobjsize, object->size,
  316                             FALSE);
  317                 /*
  318                  * this gets rid of garbage at the end of a page that is now
  319                  * only partially backed by the vnode.
  320                  *
  321                  * XXX for some reason (I don't know yet), if we take a
  322                  * completely invalid page and mark it partially valid
  323                  * it can screw up NFS reads, so we don't allow the case.
  324                  */
  325                 if ((nsize & PAGE_MASK) &&
  326                     (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
  327                     m->valid != 0) {
  328                         int base = (int)nsize & PAGE_MASK;
  329                         int size = PAGE_SIZE - base;
  330 
  331                         /*
  332                          * Clear out partial-page garbage in case
  333                          * the page has been mapped.
  334                          */
  335                         pmap_zero_page_area(m, base, size);
  336 
  337                         /*
  338                          * XXX work around SMP data integrity race
  339                          * by unmapping the page from user processes.
  340                          * The garbage we just cleared may be mapped
  341                          * to a user process running on another cpu
  342                          * and this code is not running through normal
  343                          * I/O channels which handle SMP issues for
  344                          * us, so unmap page to synchronize all cpus.
  345                          *
  346                          * XXX should vm_pager_unmap_page() have
  347                          * dealt with this?
  348                          */
  349                         vm_page_lock_queues();
  350                         pmap_remove_all(m);
  351 
  352                         /*
  353                          * Clear out partial-page dirty bits.  This
  354                          * has the side effect of setting the valid
  355                          * bits, but that is ok.  There are a bunch
  356                          * of places in the VM system where we expected
  357                          * m->dirty == VM_PAGE_BITS_ALL.  The file EOF
  358                          * case is one of them.  If the page is still
  359                          * partially dirty, make it fully dirty.
  360                          *
  361                          * note that we do not clear out the valid
  362                          * bits.  This would prevent bogus_page
  363                          * replacement from working properly.
  364                          */
  365                         vm_page_set_validclean(m, base, size);
  366                         if (m->dirty != 0)
  367                                 m->dirty = VM_PAGE_BITS_ALL;
  368                         vm_page_unlock_queues();
  369                 }
  370         }
  371         object->un_pager.vnp.vnp_size = nsize;
  372         object->size = nobjsize;
  373         VM_OBJECT_UNLOCK(object);
  374 }
  375 
  376 /*
  377  * calculate the linear (byte) disk address of specified virtual
  378  * file address
  379  */
  380 static daddr_t
  381 vnode_pager_addr(vp, address, run)
  382         struct vnode *vp;
  383         vm_ooffset_t address;
  384         int *run;
  385 {
  386         daddr_t rtaddress;
  387         int bsize;
  388         daddr_t block;
  389         int err;
  390         daddr_t vblock;
  391         daddr_t voffset;
  392 
  393         GIANT_REQUIRED;
  394         if (address < 0)
  395                 return -1;
  396 
  397         if (vp->v_mount == NULL)
  398                 return -1;
  399 
  400         bsize = vp->v_mount->mnt_stat.f_iosize;
  401         vblock = address / bsize;
  402         voffset = address % bsize;
  403 
  404         err = VOP_BMAP(vp, vblock, NULL, &block, run, NULL);
  405 
  406         if (err || (block == -1))
  407                 rtaddress = -1;
  408         else {
  409                 rtaddress = block + voffset / DEV_BSIZE;
  410                 if (run) {
  411                         *run += 1;
  412                         *run *= bsize/PAGE_SIZE;
  413                         *run -= voffset/PAGE_SIZE;
  414                 }
  415         }
  416 
  417         return rtaddress;
  418 }
  419 
  420 /*
  421  * small block filesystem vnode pager input
  422  */
  423 static int
  424 vnode_pager_input_smlfs(object, m)
  425         vm_object_t object;
  426         vm_page_t m;
  427 {
  428         int i;
  429         struct vnode *dp, *vp;
  430         struct buf *bp;
  431         struct sf_buf *sf;
  432         daddr_t fileaddr;
  433         vm_offset_t bsize;
  434         int error = 0;
  435 
  436         GIANT_REQUIRED;
  437 
  438         vp = object->handle;
  439         if (vp->v_mount == NULL)
  440                 return VM_PAGER_BAD;
  441 
  442         bsize = vp->v_mount->mnt_stat.f_iosize;
  443 
  444         VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
  445 
  446         sf = sf_buf_alloc(m, 0);
  447 
  448         for (i = 0; i < PAGE_SIZE / bsize; i++) {
  449                 vm_ooffset_t address;
  450 
  451                 if (vm_page_bits(i * bsize, bsize) & m->valid)
  452                         continue;
  453 
  454                 address = IDX_TO_OFF(m->pindex) + i * bsize;
  455                 if (address >= object->un_pager.vnp.vnp_size) {
  456                         fileaddr = -1;
  457                 } else {
  458                         fileaddr = vnode_pager_addr(vp, address, NULL);
  459                 }
  460                 if (fileaddr != -1) {
  461                         bp = getpbuf(&vnode_pbuf_freecnt);
  462 
  463                         /* build a minimal buffer header */
  464                         bp->b_iocmd = BIO_READ;
  465                         bp->b_iodone = bdone;
  466                         KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
  467                         KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
  468                         bp->b_rcred = crhold(curthread->td_ucred);
  469                         bp->b_wcred = crhold(curthread->td_ucred);
  470                         bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
  471                         bp->b_blkno = fileaddr;
  472                         pbgetvp(dp, bp);
  473                         bp->b_bcount = bsize;
  474                         bp->b_bufsize = bsize;
  475                         bp->b_runningbufspace = bp->b_bufsize;
  476                         atomic_add_int(&runningbufspace, bp->b_runningbufspace);
  477 
  478                         /* do the input */
  479                         bp->b_iooffset = dbtob(bp->b_blkno);
  480                         if (dp->v_type == VCHR)
  481                                 VOP_SPECSTRATEGY(bp->b_vp, bp);
  482                         else
  483                                 VOP_STRATEGY(bp->b_vp, bp);
  484 
  485                         /* we definitely need to be at splvm here */
  486 
  487                         bwait(bp, PVM, "vnsrd");
  488 
  489                         if ((bp->b_ioflags & BIO_ERROR) != 0)
  490                                 error = EIO;
  491 
  492                         /*
  493                          * free the buffer header back to the swap buffer pool
  494                          */
  495                         relpbuf(bp, &vnode_pbuf_freecnt);
  496                         if (error)
  497                                 break;
  498 
  499                         VM_OBJECT_LOCK(object);
  500                         vm_page_lock_queues();
  501                         vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
  502                         vm_page_unlock_queues();
  503                         VM_OBJECT_UNLOCK(object);
  504                 } else {
  505                         VM_OBJECT_LOCK(object);
  506                         vm_page_lock_queues();
  507                         vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
  508                         vm_page_unlock_queues();
  509                         VM_OBJECT_UNLOCK(object);
  510                         bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
  511                 }
  512         }
  513         sf_buf_free(sf);
  514         vm_page_lock_queues();
  515         pmap_clear_modify(m);
  516         vm_page_unlock_queues();
  517         if (error) {
  518                 return VM_PAGER_ERROR;
  519         }
  520         return VM_PAGER_OK;
  521 
  522 }
  523 
  524 
  525 /*
  526  * old style vnode pager input routine
  527  */
  528 static int
  529 vnode_pager_input_old(object, m)
  530         vm_object_t object;
  531         vm_page_t m;
  532 {
  533         struct uio auio;
  534         struct iovec aiov;
  535         int error;
  536         int size;
  537         struct sf_buf *sf;
  538         struct vnode *vp;
  539 
  540         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  541         error = 0;
  542 
  543         /*
  544          * Return failure if beyond current EOF
  545          */
  546         if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
  547                 return VM_PAGER_BAD;
  548         } else {
  549                 size = PAGE_SIZE;
  550                 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
  551                         size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
  552                 vp = object->handle;
  553                 VM_OBJECT_UNLOCK(object);
  554 
  555                 /*
  556                  * Allocate a kernel virtual address and initialize so that
  557                  * we can use VOP_READ/WRITE routines.
  558                  */
  559                 sf = sf_buf_alloc(m, 0);
  560 
  561                 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
  562                 aiov.iov_len = size;
  563                 auio.uio_iov = &aiov;
  564                 auio.uio_iovcnt = 1;
  565                 auio.uio_offset = IDX_TO_OFF(m->pindex);
  566                 auio.uio_segflg = UIO_SYSSPACE;
  567                 auio.uio_rw = UIO_READ;
  568                 auio.uio_resid = size;
  569                 auio.uio_td = curthread;
  570 
  571                 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
  572                 if (!error) {
  573                         int count = size - auio.uio_resid;
  574 
  575                         if (count == 0)
  576                                 error = EINVAL;
  577                         else if (count != PAGE_SIZE)
  578                                 bzero((caddr_t)sf_buf_kva(sf) + count,
  579                                     PAGE_SIZE - count);
  580                 }
  581                 sf_buf_free(sf);
  582 
  583                 VM_OBJECT_LOCK(object);
  584         }
  585         vm_page_lock_queues();
  586         pmap_clear_modify(m);
  587         vm_page_undirty(m);
  588         vm_page_unlock_queues();
  589         if (!error)
  590                 m->valid = VM_PAGE_BITS_ALL;
  591         return error ? VM_PAGER_ERROR : VM_PAGER_OK;
  592 }
  593 
  594 /*
  595  * generic vnode pager input routine
  596  */
  597 
  598 /*
  599  * Local media VFS's that do not implement their own VOP_GETPAGES
  600  * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
  601  * to implement the previous behaviour.
  602  *
  603  * All other FS's should use the bypass to get to the local media
  604  * backing vp's VOP_GETPAGES.
  605  */
  606 static int
  607 vnode_pager_getpages(object, m, count, reqpage)
  608         vm_object_t object;
  609         vm_page_t *m;
  610         int count;
  611         int reqpage;
  612 {
  613         int rtval;
  614         struct vnode *vp;
  615         int bytes = count * PAGE_SIZE;
  616 
  617         vp = object->handle;
  618         VM_OBJECT_UNLOCK(object);
  619         mtx_lock(&Giant);
  620         rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
  621         KASSERT(rtval != EOPNOTSUPP,
  622             ("vnode_pager: FS getpages not implemented\n"));
  623         mtx_unlock(&Giant);
  624         VM_OBJECT_LOCK(object);
  625         return rtval;
  626 }
  627 
  628 /*
  629  * This is now called from local media FS's to operate against their
  630  * own vnodes if they fail to implement VOP_GETPAGES.
  631  */
  632 int
  633 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
  634         struct vnode *vp;
  635         vm_page_t *m;
  636         int bytecount;
  637         int reqpage;
  638 {
  639         vm_object_t object;
  640         vm_offset_t kva;
  641         off_t foff, tfoff, nextoff;
  642         int i, j, size, bsize, first;
  643         daddr_t firstaddr;
  644         struct vnode *dp;
  645         int runpg;
  646         int runend;
  647         struct buf *bp;
  648         int count;
  649         int error = 0;
  650 
  651         GIANT_REQUIRED;
  652         object = vp->v_object;
  653         count = bytecount / PAGE_SIZE;
  654 
  655         if (vp->v_mount == NULL)
  656                 return VM_PAGER_BAD;
  657 
  658         bsize = vp->v_mount->mnt_stat.f_iosize;
  659 
  660         /* get the UNDERLYING device for the file with VOP_BMAP() */
  661 
  662         /*
  663          * originally, we did not check for an error return value -- assuming
  664          * an fs always has a bmap entry point -- that assumption is wrong!!!
  665          */
  666         foff = IDX_TO_OFF(m[reqpage]->pindex);
  667 
  668         /*
  669          * if we can't bmap, use old VOP code
  670          */
  671         if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
  672                 VM_OBJECT_LOCK(object);
  673                 vm_page_lock_queues();
  674                 for (i = 0; i < count; i++)
  675                         if (i != reqpage)
  676                                 vm_page_free(m[i]);
  677                 vm_page_unlock_queues();
  678                 cnt.v_vnodein++;
  679                 cnt.v_vnodepgsin++;
  680                 error = vnode_pager_input_old(object, m[reqpage]);
  681                 VM_OBJECT_UNLOCK(object);
  682                 return (error);
  683 
  684                 /*
  685                  * if the blocksize is smaller than a page size, then use
  686                  * special small filesystem code.  NFS sometimes has a small
  687                  * blocksize, but it can handle large reads itself.
  688                  */
  689         } else if ((PAGE_SIZE / bsize) > 1 &&
  690             (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
  691                 VM_OBJECT_LOCK(object);
  692                 vm_page_lock_queues();
  693                 for (i = 0; i < count; i++)
  694                         if (i != reqpage)
  695                                 vm_page_free(m[i]);
  696                 vm_page_unlock_queues();
  697                 VM_OBJECT_UNLOCK(object);
  698                 cnt.v_vnodein++;
  699                 cnt.v_vnodepgsin++;
  700                 return vnode_pager_input_smlfs(object, m[reqpage]);
  701         }
  702 
  703         /*
  704          * If we have a completely valid page available to us, we can
  705          * clean up and return.  Otherwise we have to re-read the
  706          * media.
  707          */
  708         VM_OBJECT_LOCK(object);
  709         if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
  710                 vm_page_lock_queues();
  711                 for (i = 0; i < count; i++)
  712                         if (i != reqpage)
  713                                 vm_page_free(m[i]);
  714                 vm_page_unlock_queues();
  715                 VM_OBJECT_UNLOCK(object);
  716                 return VM_PAGER_OK;
  717         }
  718         m[reqpage]->valid = 0;
  719         VM_OBJECT_UNLOCK(object);
  720 
  721         /*
  722          * here on direct device I/O
  723          */
  724         firstaddr = -1;
  725 
  726         /*
  727          * calculate the run that includes the required page
  728          */
  729         for (first = 0, i = 0; i < count; i = runend) {
  730                 firstaddr = vnode_pager_addr(vp,
  731                         IDX_TO_OFF(m[i]->pindex), &runpg);
  732                 if (firstaddr == -1) {
  733                         VM_OBJECT_LOCK(object);
  734                         if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
  735                                 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
  736                                     (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
  737                                     (uintmax_t)foff,
  738                                     (uintmax_t)
  739                                     (object->un_pager.vnp.vnp_size >> 32),
  740                                     (uintmax_t)object->un_pager.vnp.vnp_size);
  741                         }
  742                         vm_page_lock_queues();
  743                         vm_page_free(m[i]);
  744                         vm_page_unlock_queues();
  745                         VM_OBJECT_UNLOCK(object);
  746                         runend = i + 1;
  747                         first = runend;
  748                         continue;
  749                 }
  750                 runend = i + runpg;
  751                 if (runend <= reqpage) {
  752                         VM_OBJECT_LOCK(object);
  753                         vm_page_lock_queues();
  754                         for (j = i; j < runend; j++)
  755                                 vm_page_free(m[j]);
  756                         vm_page_unlock_queues();
  757                         VM_OBJECT_UNLOCK(object);
  758                 } else {
  759                         if (runpg < (count - first)) {
  760                                 VM_OBJECT_LOCK(object);
  761                                 vm_page_lock_queues();
  762                                 for (i = first + runpg; i < count; i++)
  763                                         vm_page_free(m[i]);
  764                                 vm_page_unlock_queues();
  765                                 VM_OBJECT_UNLOCK(object);
  766                                 count = first + runpg;
  767                         }
  768                         break;
  769                 }
  770                 first = runend;
  771         }
  772 
  773         /*
  774          * the first and last page have been calculated now, move input pages
  775          * to be zero based...
  776          */
  777         if (first != 0) {
  778                 for (i = first; i < count; i++) {
  779                         m[i - first] = m[i];
  780                 }
  781                 count -= first;
  782                 reqpage -= first;
  783         }
  784 
  785         /*
  786          * calculate the file virtual address for the transfer
  787          */
  788         foff = IDX_TO_OFF(m[0]->pindex);
  789 
  790         /*
  791          * calculate the size of the transfer
  792          */
  793         size = count * PAGE_SIZE;
  794         if ((foff + size) > object->un_pager.vnp.vnp_size)
  795                 size = object->un_pager.vnp.vnp_size - foff;
  796 
  797         /*
  798          * round up physical size for real devices.
  799          */
  800         if (dp->v_type == VBLK || dp->v_type == VCHR) {
  801                 int secmask = dp->v_rdev->si_bsize_phys - 1;
  802                 KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
  803                 size = (size + secmask) & ~secmask;
  804         }
  805 
  806         bp = getpbuf(&vnode_pbuf_freecnt);
  807         kva = (vm_offset_t) bp->b_data;
  808 
  809         /*
  810          * and map the pages to be read into the kva
  811          */
  812         pmap_qenter(kva, m, count);
  813 
  814         /* build a minimal buffer header */
  815         bp->b_iocmd = BIO_READ;
  816         bp->b_iodone = bdone;
  817         KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
  818         KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
  819         bp->b_rcred = crhold(curthread->td_ucred);
  820         bp->b_wcred = crhold(curthread->td_ucred);
  821         bp->b_blkno = firstaddr;
  822         pbgetvp(dp, bp);
  823         bp->b_bcount = size;
  824         bp->b_bufsize = size;
  825         bp->b_runningbufspace = bp->b_bufsize;
  826         atomic_add_int(&runningbufspace, bp->b_runningbufspace);
  827 
  828         cnt.v_vnodein++;
  829         cnt.v_vnodepgsin += count;
  830 
  831         /* do the input */
  832         bp->b_iooffset = dbtob(bp->b_blkno);
  833         if (dp->v_type == VCHR)
  834                 VOP_SPECSTRATEGY(bp->b_vp, bp);
  835         else
  836                 VOP_STRATEGY(bp->b_vp, bp);
  837 
  838         bwait(bp, PVM, "vnread");
  839 
  840         if ((bp->b_ioflags & BIO_ERROR) != 0)
  841                 error = EIO;
  842 
  843         if (!error) {
  844                 if (size != count * PAGE_SIZE)
  845                         bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
  846         }
  847         pmap_qremove(kva, count);
  848 
  849         /*
  850          * free the buffer header back to the swap buffer pool
  851          */
  852         relpbuf(bp, &vnode_pbuf_freecnt);
  853 
  854         VM_OBJECT_LOCK(object);
  855         vm_page_lock_queues();
  856         for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
  857                 vm_page_t mt;
  858 
  859                 nextoff = tfoff + PAGE_SIZE;
  860                 mt = m[i];
  861 
  862                 if (nextoff <= object->un_pager.vnp.vnp_size) {
  863                         /*
  864                          * Read filled up entire page.
  865                          */
  866                         mt->valid = VM_PAGE_BITS_ALL;
  867                         vm_page_undirty(mt);    /* should be an assert? XXX */
  868                         pmap_clear_modify(mt);
  869                 } else {
  870                         /*
  871                          * Read did not fill up entire page.  Since this
  872                          * is getpages, the page may be mapped, so we have
  873                          * to zero the invalid portions of the page even
  874                          * though we aren't setting them valid.
  875                          *
  876                          * Currently we do not set the entire page valid,
  877                          * we just try to clear the piece that we couldn't
  878                          * read.
  879                          */
  880                         vm_page_set_validclean(mt, 0,
  881                             object->un_pager.vnp.vnp_size - tfoff);
  882                         /* handled by vm_fault now */
  883                         /* vm_page_zero_invalid(mt, FALSE); */
  884                 }
  885                 
  886                 if (i != reqpage) {
  887 
  888                         /*
  889                          * whether or not to leave the page activated is up in
  890                          * the air, but we should put the page on a page queue
  891                          * somewhere. (it already is in the object). Result:
  892                          * It appears that empirical results show that
  893                          * deactivating pages is best.
  894                          */
  895 
  896                         /*
  897                          * just in case someone was asking for this page we
  898                          * now tell them that it is ok to use
  899                          */
  900                         if (!error) {
  901                                 if (mt->flags & PG_WANTED)
  902                                         vm_page_activate(mt);
  903                                 else
  904                                         vm_page_deactivate(mt);
  905                                 vm_page_wakeup(mt);
  906                         } else {
  907                                 vm_page_free(mt);
  908                         }
  909                 }
  910         }
  911         vm_page_unlock_queues();
  912         VM_OBJECT_UNLOCK(object);
  913         if (error) {
  914                 printf("vnode_pager_getpages: I/O read error\n");
  915         }
  916         return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
  917 }
  918 
  919 /*
  920  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
  921  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
  922  * vnode_pager_generic_putpages() to implement the previous behaviour.
  923  *
  924  * All other FS's should use the bypass to get to the local media
  925  * backing vp's VOP_PUTPAGES.
  926  */
  927 static void
  928 vnode_pager_putpages(object, m, count, sync, rtvals)
  929         vm_object_t object;
  930         vm_page_t *m;
  931         int count;
  932         boolean_t sync;
  933         int *rtvals;
  934 {
  935         int rtval;
  936         struct vnode *vp;
  937         struct mount *mp;
  938         int bytes = count * PAGE_SIZE;
  939 
  940         GIANT_REQUIRED;
  941         /*
  942          * Force synchronous operation if we are extremely low on memory
  943          * to prevent a low-memory deadlock.  VOP operations often need to
  944          * allocate more memory to initiate the I/O ( i.e. do a BMAP 
  945          * operation ).  The swapper handles the case by limiting the amount
  946          * of asynchronous I/O, but that sort of solution doesn't scale well
  947          * for the vnode pager without a lot of work.
  948          *
  949          * Also, the backing vnode's iodone routine may not wake the pageout
  950          * daemon up.  This should be probably be addressed XXX.
  951          */
  952 
  953         if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
  954                 sync |= OBJPC_SYNC;
  955 
  956         /*
  957          * Call device-specific putpages function
  958          */
  959         vp = object->handle;
  960         VM_OBJECT_UNLOCK(object);
  961         if (vp->v_type != VREG)
  962                 mp = NULL;
  963         (void)vn_start_write(vp, &mp, V_WAIT);
  964         rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
  965         KASSERT(rtval != EOPNOTSUPP, 
  966             ("vnode_pager: stale FS putpages\n"));
  967         vn_finished_write(mp);
  968         VM_OBJECT_LOCK(object);
  969 }
  970 
  971 
  972 /*
  973  * This is now called from local media FS's to operate against their
  974  * own vnodes if they fail to implement VOP_PUTPAGES.
  975  *
  976  * This is typically called indirectly via the pageout daemon and
  977  * clustering has already typically occured, so in general we ask the
  978  * underlying filesystem to write the data out asynchronously rather
  979  * then delayed.
  980  */
  981 int
  982 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
  983         struct vnode *vp;
  984         vm_page_t *m;
  985         int bytecount;
  986         int flags;
  987         int *rtvals;
  988 {
  989         int i;
  990         vm_object_t object;
  991         int count;
  992 
  993         int maxsize, ncount;
  994         vm_ooffset_t poffset;
  995         struct uio auio;
  996         struct iovec aiov;
  997         int error;
  998         int ioflags;
  999 
 1000         GIANT_REQUIRED;
 1001         object = vp->v_object;
 1002         count = bytecount / PAGE_SIZE;
 1003 
 1004         for (i = 0; i < count; i++)
 1005                 rtvals[i] = VM_PAGER_AGAIN;
 1006 
 1007         if ((int64_t)m[0]->pindex < 0) {
 1008                 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
 1009                         (long)m[0]->pindex, (u_long)m[0]->dirty);
 1010                 rtvals[0] = VM_PAGER_BAD;
 1011                 return VM_PAGER_BAD;
 1012         }
 1013 
 1014         maxsize = count * PAGE_SIZE;
 1015         ncount = count;
 1016 
 1017         poffset = IDX_TO_OFF(m[0]->pindex);
 1018 
 1019         /*
 1020          * If the page-aligned write is larger then the actual file we
 1021          * have to invalidate pages occuring beyond the file EOF.  However,
 1022          * there is an edge case where a file may not be page-aligned where
 1023          * the last page is partially invalid.  In this case the filesystem
 1024          * may not properly clear the dirty bits for the entire page (which
 1025          * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
 1026          * With the page locked we are free to fix-up the dirty bits here.
 1027          *
 1028          * We do not under any circumstances truncate the valid bits, as
 1029          * this will screw up bogus page replacement.
 1030          */
 1031         if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
 1032                 if (object->un_pager.vnp.vnp_size > poffset) {
 1033                         int pgoff;
 1034 
 1035                         maxsize = object->un_pager.vnp.vnp_size - poffset;
 1036                         ncount = btoc(maxsize);
 1037                         if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
 1038                                 vm_page_lock_queues();
 1039                                 vm_page_clear_dirty(m[ncount - 1], pgoff,
 1040                                         PAGE_SIZE - pgoff);
 1041                                 vm_page_unlock_queues();
 1042                         }
 1043                 } else {
 1044                         maxsize = 0;
 1045                         ncount = 0;
 1046                 }
 1047                 if (ncount < count) {
 1048                         for (i = ncount; i < count; i++) {
 1049                                 rtvals[i] = VM_PAGER_BAD;
 1050                         }
 1051                 }
 1052         }
 1053 
 1054         /*
 1055          * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
 1056          * rather then a bdwrite() to prevent paging I/O from saturating 
 1057          * the buffer cache.  Dummy-up the sequential heuristic to cause
 1058          * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
 1059          * the system decides how to cluster.
 1060          */
 1061         ioflags = IO_VMIO;
 1062         if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
 1063                 ioflags |= IO_SYNC;
 1064         else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
 1065                 ioflags |= IO_ASYNC;
 1066         ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
 1067         ioflags |= IO_SEQMAX << IO_SEQSHIFT;
 1068 
 1069         aiov.iov_base = (caddr_t) 0;
 1070         aiov.iov_len = maxsize;
 1071         auio.uio_iov = &aiov;
 1072         auio.uio_iovcnt = 1;
 1073         auio.uio_offset = poffset;
 1074         auio.uio_segflg = UIO_NOCOPY;
 1075         auio.uio_rw = UIO_WRITE;
 1076         auio.uio_resid = maxsize;
 1077         auio.uio_td = (struct thread *) 0;
 1078         error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
 1079         cnt.v_vnodeout++;
 1080         cnt.v_vnodepgsout += ncount;
 1081 
 1082         if (error) {
 1083                 printf("vnode_pager_putpages: I/O error %d\n", error);
 1084         }
 1085         if (auio.uio_resid) {
 1086                 printf("vnode_pager_putpages: residual I/O %d at %lu\n",
 1087                     auio.uio_resid, (u_long)m[0]->pindex);
 1088         }
 1089         for (i = 0; i < ncount; i++) {
 1090                 rtvals[i] = VM_PAGER_OK;
 1091         }
 1092         return rtvals[0];
 1093 }
 1094 
 1095 struct vnode *
 1096 vnode_pager_lock(vm_object_t first_object)
 1097 {
 1098         struct vnode *vp;
 1099         vm_object_t backing_object, object;
 1100 
 1101         VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
 1102         for (object = first_object; object != NULL; object = backing_object) {
 1103                 if (object->type != OBJT_VNODE) {
 1104                         if ((backing_object = object->backing_object) != NULL)
 1105                                 VM_OBJECT_LOCK(backing_object);
 1106                         if (object != first_object)
 1107                                 VM_OBJECT_UNLOCK(object);
 1108                         continue;
 1109                 }
 1110         retry:
 1111                 if (object->flags & OBJ_DEAD) {
 1112                         if (object != first_object)
 1113                                 VM_OBJECT_UNLOCK(object);
 1114                         return NULL;
 1115                 }
 1116                 vp = object->handle;
 1117                 VI_LOCK(vp);
 1118                 VM_OBJECT_UNLOCK(object);
 1119                 if (first_object != object)
 1120                         VM_OBJECT_UNLOCK(first_object);
 1121                 if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | LK_NOPAUSE |
 1122                     LK_RETRY | LK_SHARED, curthread)) {
 1123                         VM_OBJECT_LOCK(first_object);
 1124                         if (object != first_object)
 1125                                 VM_OBJECT_LOCK(object);
 1126                         if (object->type != OBJT_VNODE) {
 1127                                 if (object != first_object)
 1128                                         VM_OBJECT_UNLOCK(object);
 1129                                 return NULL;
 1130                         }
 1131                         printf("vnode_pager_lock: retrying\n");
 1132                         goto retry;
 1133                 }
 1134                 VM_OBJECT_LOCK(first_object);
 1135                 return (vp);
 1136         }
 1137         return NULL;
 1138 }

Cache object: 693d3b33e74c6fab997949bc4675c12f


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