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

Cache object: 4c34820c2f2e1434023ba4f3c1cb2b19


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