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$
   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/buf.h>
   60 #include <sys/vmmeter.h>
   61 
   62 #include <vm/vm.h>
   63 #include <vm/vm_prot.h>
   64 #include <vm/vm_object.h>
   65 #include <vm/vm_page.h>
   66 #include <vm/vm_pager.h>
   67 #include <vm/vm_map.h>
   68 #include <vm/vnode_pager.h>
   69 #include <vm/vm_extern.h>
   70 
   71 static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
   72                                          int *run));
   73 static void vnode_pager_iodone __P((struct buf *bp));
   74 static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
   75 static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
   76 static void vnode_pager_dealloc __P((vm_object_t));
   77 static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
   78 static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
   79 static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
   80 
   81 struct pagerops vnodepagerops = {
   82         NULL,
   83         vnode_pager_alloc,
   84         vnode_pager_dealloc,
   85         vnode_pager_getpages,
   86         vnode_pager_putpages,
   87         vnode_pager_haspage,
   88         NULL
   89 };
   90 
   91 
   92 /*
   93  * Allocate (or lookup) pager for a vnode.
   94  * Handle is a vnode pointer.
   95  */
   96 vm_object_t
   97 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
   98                   vm_ooffset_t offset)
   99 {
  100         vm_object_t object;
  101         struct vnode *vp;
  102 
  103         /*
  104          * Pageout to vnode, no can do yet.
  105          */
  106         if (handle == NULL)
  107                 return (NULL);
  108 
  109         vp = (struct vnode *) handle;
  110 
  111         /*
  112          * Prevent race condition when allocating the object. This
  113          * can happen with NFS vnodes since the nfsnode isn't locked.
  114          */
  115         while (vp->v_flag & VOLOCK) {
  116                 vp->v_flag |= VOWANT;
  117                 tsleep(vp, PVM, "vnpobj", 0);
  118         }
  119         vp->v_flag |= VOLOCK;
  120 
  121         /*
  122          * If the object is being terminated, wait for it to
  123          * go away.
  124          */
  125         while (((object = vp->v_object) != NULL) &&
  126                 (object->flags & OBJ_DEAD)) {
  127                 tsleep(object, PVM, "vadead", 0);
  128         }
  129 
  130         if (vp->v_usecount == 0)
  131                 panic("vnode_pager_alloc: no vnode reference");
  132 
  133         if (object == NULL) {
  134                 /*
  135                  * And an object of the appropriate size
  136                  */
  137                 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
  138                 object->flags = 0;
  139 
  140                 object->un_pager.vnp.vnp_size = size;
  141 
  142                 object->handle = handle;
  143                 vp->v_object = object;
  144                 vp->v_usecount++;
  145         } else {
  146                 object->ref_count++;
  147                 vp->v_usecount++;
  148         }
  149 
  150         vp->v_flag &= ~VOLOCK;
  151         if (vp->v_flag & VOWANT) {
  152                 vp->v_flag &= ~VOWANT;
  153                 wakeup(vp);
  154         }
  155         return (object);
  156 }
  157 
  158 static void
  159 vnode_pager_dealloc(object)
  160         vm_object_t object;
  161 {
  162         register struct vnode *vp = object->handle;
  163 
  164         if (vp == NULL)
  165                 panic("vnode_pager_dealloc: pager already dealloced");
  166 
  167         vm_object_pip_wait(object, "vnpdea");
  168 
  169         object->handle = NULL;
  170         object->type = OBJT_DEAD;
  171         vp->v_object = NULL;
  172         vp->v_flag &= ~(VTEXT | VOBJBUF);
  173 }
  174 
  175 static boolean_t
  176 vnode_pager_haspage(object, pindex, before, after)
  177         vm_object_t object;
  178         vm_pindex_t pindex;
  179         int *before;
  180         int *after;
  181 {
  182         struct vnode *vp = object->handle;
  183         daddr_t bn;
  184         int err;
  185         daddr_t reqblock;
  186         int poff;
  187         int bsize;
  188         int pagesperblock, blocksperpage;
  189 
  190         if ((vp == NULL) || (vp->v_flag & VDOOMED))
  191                 return FALSE;
  192 
  193         /*
  194          * If filesystem no longer mounted or offset beyond end of file we do
  195          * not have the page.
  196          */
  197         if ((vp->v_mount == NULL) ||
  198                 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
  199                 return FALSE;
  200 
  201         bsize = vp->v_mount->mnt_stat.f_iosize;
  202         pagesperblock = bsize / PAGE_SIZE;
  203         blocksperpage = 0;
  204         if (pagesperblock > 0) {
  205                 reqblock = pindex / pagesperblock;
  206         } else {
  207                 blocksperpage = (PAGE_SIZE / bsize);
  208                 reqblock = pindex * blocksperpage;
  209         }
  210         err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
  211                 after, before);
  212         if (err)
  213                 return TRUE;
  214         if ( bn == -1)
  215                 return FALSE;
  216         if (pagesperblock > 0) {
  217                 poff = pindex - (reqblock * pagesperblock);
  218                 if (before) {
  219                         *before *= pagesperblock;
  220                         *before += poff;
  221                 }
  222                 if (after) {
  223                         int numafter;
  224                         *after *= pagesperblock;
  225                         numafter = pagesperblock - (poff + 1);
  226                         if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
  227                                 numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
  228                         }
  229                         *after += numafter;
  230                 }
  231         } else {
  232                 if (before) {
  233                         *before /= blocksperpage;
  234                 }
  235 
  236                 if (after) {
  237                         *after /= blocksperpage;
  238                 }
  239         }
  240         return TRUE;
  241 }
  242 
  243 /*
  244  * Lets the VM system know about a change in size for a file.
  245  * We adjust our own internal size and flush any cached pages in
  246  * the associated object that are affected by the size change.
  247  *
  248  * Note: this routine may be invoked as a result of a pager put
  249  * operation (possibly at object termination time), so we must be careful.
  250  */
  251 void
  252 vnode_pager_setsize(vp, nsize)
  253         struct vnode *vp;
  254         vm_ooffset_t nsize;
  255 {
  256         vm_pindex_t nobjsize;
  257         vm_object_t object = vp->v_object;
  258 
  259         if (object == NULL)
  260                 return;
  261 
  262         /*
  263          * Hasn't changed size
  264          */
  265         if (nsize == object->un_pager.vnp.vnp_size)
  266                 return;
  267 
  268         nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
  269 
  270         /*
  271          * File has shrunk. Toss any cached pages beyond the new EOF.
  272          */
  273         if (nsize < object->un_pager.vnp.vnp_size) {
  274                 vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
  275                 if (nobjsize < object->size) {
  276                         vm_object_page_remove(object, nobjsize, object->size,
  277                                 FALSE);
  278                 }
  279                 /*
  280                  * this gets rid of garbage at the end of a page that is now
  281                  * only partially backed by the vnode...
  282                  */
  283                 if (nsize & PAGE_MASK) {
  284                         vm_offset_t kva;
  285                         vm_page_t m;
  286 
  287                         m = vm_page_lookup(object, OFF_TO_IDX(nsize));
  288                         if (m) {
  289                                 kva = vm_pager_map_page(m);
  290                                 bzero((caddr_t) kva + (nsize & PAGE_MASK),
  291                                     (int) (round_page(nsize) - nsize));
  292                                 vm_pager_unmap_page(kva);
  293                         }
  294                 }
  295         }
  296         object->un_pager.vnp.vnp_size = nsize;
  297         object->size = nobjsize;
  298 }
  299 
  300 void
  301 vnode_pager_freepage(m)
  302         vm_page_t m;
  303 {
  304         vm_page_free(m);
  305 }
  306 
  307 /*
  308  * calculate the linear (byte) disk address of specified virtual
  309  * file address
  310  */
  311 static vm_offset_t
  312 vnode_pager_addr(vp, address, run)
  313         struct vnode *vp;
  314         vm_ooffset_t address;
  315         int *run;
  316 {
  317         int rtaddress;
  318         int bsize;
  319         daddr_t block;
  320         struct vnode *rtvp;
  321         int err;
  322         daddr_t vblock;
  323         int voffset;
  324 
  325         if ((int) address < 0)
  326                 return -1;
  327 
  328         if (vp->v_mount == NULL)
  329                 return -1;
  330 
  331         bsize = vp->v_mount->mnt_stat.f_iosize;
  332         vblock = address / bsize;
  333         voffset = address % bsize;
  334 
  335         err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
  336 
  337         if (err || (block == -1))
  338                 rtaddress = -1;
  339         else {
  340                 rtaddress = block + voffset / DEV_BSIZE;
  341                 if( run) {
  342                         *run += 1;
  343                         *run *= bsize/PAGE_SIZE;
  344                         *run -= voffset/PAGE_SIZE;
  345                 }
  346         }
  347 
  348         return rtaddress;
  349 }
  350 
  351 /*
  352  * interrupt routine for I/O completion
  353  */
  354 static void
  355 vnode_pager_iodone(bp)
  356         struct buf *bp;
  357 {
  358         bp->b_flags |= B_DONE;
  359         wakeup(bp);
  360 }
  361 
  362 /*
  363  * small block file system vnode pager input
  364  */
  365 static int
  366 vnode_pager_input_smlfs(object, m)
  367         vm_object_t object;
  368         vm_page_t m;
  369 {
  370         int i;
  371         int s;
  372         struct vnode *dp, *vp;
  373         struct buf *bp;
  374         vm_offset_t kva;
  375         int fileaddr;
  376         vm_offset_t bsize;
  377         int error = 0;
  378 
  379         vp = object->handle;
  380         if (vp->v_mount == NULL)
  381                 return VM_PAGER_BAD;
  382 
  383         bsize = vp->v_mount->mnt_stat.f_iosize;
  384 
  385 
  386         VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
  387 
  388         kva = vm_pager_map_page(m);
  389 
  390         for (i = 0; i < PAGE_SIZE / bsize; i++) {
  391 
  392                 if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
  393                         continue;
  394 
  395                 fileaddr = vnode_pager_addr(vp,
  396                         IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
  397                 if (fileaddr != -1) {
  398                         bp = getpbuf();
  399 
  400                         /* build a minimal buffer header */
  401                         bp->b_flags = B_BUSY | B_READ | B_CALL;
  402                         bp->b_iodone = vnode_pager_iodone;
  403                         bp->b_proc = curproc;
  404                         bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
  405                         if (bp->b_rcred != NOCRED)
  406                                 crhold(bp->b_rcred);
  407                         if (bp->b_wcred != NOCRED)
  408                                 crhold(bp->b_wcred);
  409                         bp->b_data = (caddr_t) kva + i * bsize;
  410                         bp->b_blkno = fileaddr;
  411                         pbgetvp(dp, bp);
  412                         bp->b_bcount = bsize;
  413                         bp->b_bufsize = bsize;
  414 
  415                         /* do the input */
  416                         VOP_STRATEGY(bp->b_vp, bp);
  417 
  418                         /* we definitely need to be at splvm here */
  419 
  420                         s = splvm();
  421                         while ((bp->b_flags & B_DONE) == 0) {
  422                                 tsleep(bp, PVM, "vnsrd", 0);
  423                         }
  424                         splx(s);
  425                         if ((bp->b_flags & B_ERROR) != 0)
  426                                 error = EIO;
  427 
  428                         /*
  429                          * free the buffer header back to the swap buffer pool
  430                          */
  431                         relpbuf(bp);
  432                         if (error)
  433                                 break;
  434 
  435                         vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
  436                 } else {
  437                         vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
  438                         bzero((caddr_t) kva + i * bsize, bsize);
  439                 }
  440         }
  441         vm_pager_unmap_page(kva);
  442         pmap_clear_modify(VM_PAGE_TO_PHYS(m));
  443         vm_page_flag_clear(m, PG_ZERO);
  444         if (error) {
  445                 return VM_PAGER_ERROR;
  446         }
  447         return VM_PAGER_OK;
  448 
  449 }
  450 
  451 
  452 /*
  453  * old style vnode pager output routine
  454  */
  455 static int
  456 vnode_pager_input_old(object, m)
  457         vm_object_t object;
  458         vm_page_t m;
  459 {
  460         struct uio auio;
  461         struct iovec aiov;
  462         int error;
  463         int size;
  464         vm_offset_t kva;
  465 
  466         error = 0;
  467 
  468         /*
  469          * Return failure if beyond current EOF
  470          */
  471         if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
  472                 return VM_PAGER_BAD;
  473         } else {
  474                 size = PAGE_SIZE;
  475                 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
  476                         size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
  477 
  478                 /*
  479                  * Allocate a kernel virtual address and initialize so that
  480                  * we can use VOP_READ/WRITE routines.
  481                  */
  482                 kva = vm_pager_map_page(m);
  483 
  484                 aiov.iov_base = (caddr_t) kva;
  485                 aiov.iov_len = size;
  486                 auio.uio_iov = &aiov;
  487                 auio.uio_iovcnt = 1;
  488                 auio.uio_offset = IDX_TO_OFF(m->pindex);
  489                 auio.uio_segflg = UIO_SYSSPACE;
  490                 auio.uio_rw = UIO_READ;
  491                 auio.uio_resid = size;
  492                 auio.uio_procp = curproc;
  493 
  494                 error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
  495                 if (!error) {
  496                         register int count = size - auio.uio_resid;
  497 
  498                         if (count == 0)
  499                                 error = EINVAL;
  500                         else if (count != PAGE_SIZE)
  501                                 bzero((caddr_t) kva + count, PAGE_SIZE - count);
  502                 }
  503                 vm_pager_unmap_page(kva);
  504         }
  505         pmap_clear_modify(VM_PAGE_TO_PHYS(m));
  506         m->dirty = 0;
  507         vm_page_flag_clear(m, PG_ZERO);
  508         if (!error)
  509                 m->valid = VM_PAGE_BITS_ALL;
  510         return error ? VM_PAGER_ERROR : VM_PAGER_OK;
  511 }
  512 
  513 /*
  514  * generic vnode pager input routine
  515  */
  516 
  517 /*
  518  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
  519  * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to
  520  * vnode_pager_generic_getpages() to implement the previous behaviour.
  521  *
  522  * All other FS's should use the bypass to get to the local media
  523  * backing vp's VOP_GETPAGES.
  524  */
  525 static int
  526 vnode_pager_getpages(object, m, count, reqpage)
  527         vm_object_t object;
  528         vm_page_t *m;
  529         int count;
  530         int reqpage;
  531 {
  532         int rtval;
  533         struct vnode *vp;
  534         int bytes = count * PAGE_SIZE;
  535 
  536         vp = object->handle;
  537         /* 
  538          * XXX temporary diagnostic message to help track stale FS code,
  539          * Returning EOPNOTSUPP from here may make things unhappy.
  540          */
  541         rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
  542         if (rtval == EOPNOTSUPP) {
  543             printf("vnode_pager: *** WARNING *** stale FS getpages\n");
  544             rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage);
  545         }
  546         return rtval;
  547 }
  548 
  549 
  550 /*
  551  * This is now called from local media FS's to operate against their
  552  * own vnodes if they fail to implement VOP_GETPAGES.
  553  */
  554 int
  555 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
  556         struct vnode *vp;
  557         vm_page_t *m;
  558         int bytecount;
  559         int reqpage;
  560 {
  561         vm_object_t object;
  562         vm_offset_t kva;
  563         off_t foff, tfoff, nextoff;
  564         int i, size, bsize, first, firstaddr;
  565         struct vnode *dp;
  566         int runpg;
  567         int runend;
  568         struct buf *bp;
  569         int s;
  570         int count;
  571         int error = 0;
  572 
  573         object = vp->v_object;
  574         count = bytecount / PAGE_SIZE;
  575 
  576         if (vp->v_mount == NULL)
  577                 return VM_PAGER_BAD;
  578 
  579         bsize = vp->v_mount->mnt_stat.f_iosize;
  580 
  581         /* get the UNDERLYING device for the file with VOP_BMAP() */
  582 
  583         /*
  584          * originally, we did not check for an error return value -- assuming
  585          * an fs always has a bmap entry point -- that assumption is wrong!!!
  586          */
  587         foff = IDX_TO_OFF(m[reqpage]->pindex);
  588 
  589         /*
  590          * if we can't bmap, use old VOP code
  591          */
  592         if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
  593                 for (i = 0; i < count; i++) {
  594                         if (i != reqpage) {
  595                                 vnode_pager_freepage(m[i]);
  596                         }
  597                 }
  598                 cnt.v_vnodein++;
  599                 cnt.v_vnodepgsin++;
  600                 return vnode_pager_input_old(object, m[reqpage]);
  601 
  602                 /*
  603                  * if the blocksize is smaller than a page size, then use
  604                  * special small filesystem code.  NFS sometimes has a small
  605                  * blocksize, but it can handle large reads itself.
  606                  */
  607         } else if ((PAGE_SIZE / bsize) > 1 &&
  608             (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
  609                 for (i = 0; i < count; i++) {
  610                         if (i != reqpage) {
  611                                 vnode_pager_freepage(m[i]);
  612                         }
  613                 }
  614                 cnt.v_vnodein++;
  615                 cnt.v_vnodepgsin++;
  616                 return vnode_pager_input_smlfs(object, m[reqpage]);
  617         }
  618         /*
  619          * if ANY DEV_BSIZE blocks are valid on a large filesystem block
  620          * then, the entire page is valid --
  621          * XXX no it isn't
  622          */
  623 
  624         if (m[reqpage]->valid != VM_PAGE_BITS_ALL)
  625             m[reqpage]->valid = 0;
  626 
  627         if (m[reqpage]->valid) {
  628                 m[reqpage]->valid = VM_PAGE_BITS_ALL;
  629                 for (i = 0; i < count; i++) {
  630                         if (i != reqpage)
  631                                 vnode_pager_freepage(m[i]);
  632                 }
  633                 return VM_PAGER_OK;
  634         }
  635 
  636         /*
  637          * here on direct device I/O
  638          */
  639 
  640         firstaddr = -1;
  641         /*
  642          * calculate the run that includes the required page
  643          */
  644         for(first = 0, i = 0; i < count; i = runend) {
  645                 firstaddr = vnode_pager_addr(vp,
  646                         IDX_TO_OFF(m[i]->pindex), &runpg);
  647                 if (firstaddr == -1) {
  648                         if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
  649                                 /* XXX no %qd in kernel. */
  650                                 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx",
  651                                  firstaddr, (u_long)(foff >> 32),
  652                                  (u_long)(u_int32_t)foff,
  653                                  (u_long)(u_int32_t)
  654                                  (object->un_pager.vnp.vnp_size >> 32),
  655                                  (u_long)(u_int32_t)
  656                                  object->un_pager.vnp.vnp_size);
  657                         }
  658                         vnode_pager_freepage(m[i]);
  659                         runend = i + 1;
  660                         first = runend;
  661                         continue;
  662                 }
  663                 runend = i + runpg;
  664                 if (runend <= reqpage) {
  665                         int j;
  666                         for (j = i; j < runend; j++) {
  667                                 vnode_pager_freepage(m[j]);
  668                         }
  669                 } else {
  670                         if (runpg < (count - first)) {
  671                                 for (i = first + runpg; i < count; i++)
  672                                         vnode_pager_freepage(m[i]);
  673                                 count = first + runpg;
  674                         }
  675                         break;
  676                 }
  677                 first = runend;
  678         }
  679 
  680         /*
  681          * the first and last page have been calculated now, move input pages
  682          * to be zero based...
  683          */
  684         if (first != 0) {
  685                 for (i = first; i < count; i++) {
  686                         m[i - first] = m[i];
  687                 }
  688                 count -= first;
  689                 reqpage -= first;
  690         }
  691 
  692         /*
  693          * calculate the file virtual address for the transfer
  694          */
  695         foff = IDX_TO_OFF(m[0]->pindex);
  696 
  697         /*
  698          * calculate the size of the transfer
  699          */
  700         size = count * PAGE_SIZE;
  701         if ((foff + size) > object->un_pager.vnp.vnp_size)
  702                 size = object->un_pager.vnp.vnp_size - foff;
  703 
  704         /*
  705          * round up physical size for real devices
  706          */
  707         if (dp->v_type == VBLK || dp->v_type == VCHR)
  708                 size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
  709 
  710         bp = getpbuf();
  711         kva = (vm_offset_t) bp->b_data;
  712 
  713         /*
  714          * and map the pages to be read into the kva
  715          */
  716         pmap_qenter(kva, m, count);
  717 
  718         /* build a minimal buffer header */
  719         bp->b_flags = B_BUSY | B_READ | B_CALL;
  720         bp->b_iodone = vnode_pager_iodone;
  721         /* B_PHYS is not set, but it is nice to fill this in */
  722         bp->b_proc = curproc;
  723         bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
  724         if (bp->b_rcred != NOCRED)
  725                 crhold(bp->b_rcred);
  726         if (bp->b_wcred != NOCRED)
  727                 crhold(bp->b_wcred);
  728         bp->b_blkno = firstaddr;
  729         pbgetvp(dp, bp);
  730         bp->b_bcount = size;
  731         bp->b_bufsize = size;
  732 
  733         cnt.v_vnodein++;
  734         cnt.v_vnodepgsin += count;
  735 
  736         /* do the input */
  737         VOP_STRATEGY(bp->b_vp, bp);
  738 
  739         s = splvm();
  740         /* we definitely need to be at splvm here */
  741 
  742         while ((bp->b_flags & B_DONE) == 0) {
  743                 tsleep(bp, PVM, "vnread", 0);
  744         }
  745         splx(s);
  746         if ((bp->b_flags & B_ERROR) != 0)
  747                 error = EIO;
  748 
  749         if (!error) {
  750                 if (size != count * PAGE_SIZE)
  751                         bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
  752         }
  753         pmap_qremove(kva, count);
  754 
  755         /*
  756          * free the buffer header back to the swap buffer pool
  757          */
  758         relpbuf(bp);
  759 
  760         for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
  761                 vm_page_t mt;
  762 
  763                 nextoff = tfoff + PAGE_SIZE;
  764                 mt = m[i];
  765 
  766                 if (nextoff <= size) {
  767                         mt->valid = VM_PAGE_BITS_ALL;
  768                         mt->dirty = 0;
  769                         pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
  770                 } else {
  771                         int nvalid = ((size + DEV_BSIZE - 1) - tfoff) & ~(DEV_BSIZE - 1);
  772                         vm_page_set_validclean(mt, 0, nvalid);
  773                 }
  774                 
  775                 vm_page_flag_clear(mt, PG_ZERO);
  776                 if (i != reqpage) {
  777 
  778                         /*
  779                          * whether or not to leave the page activated is up in
  780                          * the air, but we should put the page on a page queue
  781                          * somewhere. (it already is in the object). Result:
  782                          * It appears that emperical results show that
  783                          * deactivating pages is best.
  784                          */
  785 
  786                         /*
  787                          * just in case someone was asking for this page we
  788                          * now tell them that it is ok to use
  789                          */
  790                         if (!error) {
  791                                 if (mt->flags & PG_WANTED)
  792                                         vm_page_activate(mt);
  793                                 else
  794                                         vm_page_deactivate(mt);
  795                                 vm_page_wakeup(mt);
  796                         } else {
  797                                 vnode_pager_freepage(mt);
  798                         }
  799                 }
  800         }
  801         if (error) {
  802                 printf("vnode_pager_getpages: I/O read error\n");
  803         }
  804         return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
  805 }
  806 
  807 /*
  808  * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
  809  * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
  810  * vnode_pager_generic_putpages() to implement the previous behaviour.
  811  *
  812  * All other FS's should use the bypass to get to the local media
  813  * backing vp's VOP_PUTPAGES.
  814  */
  815 static int
  816 vnode_pager_putpages(object, m, count, sync, rtvals)
  817         vm_object_t object;
  818         vm_page_t *m;
  819         int count;
  820         boolean_t sync;
  821         int *rtvals;
  822 {
  823         int rtval;
  824         struct vnode *vp;
  825         int bytes = count * PAGE_SIZE;
  826 
  827         /*
  828          * Force synchronous operation if we are extremely low on memory
  829          * to prevent a low-memory deadlock.  VOP operations often need to
  830          * allocate more memory to initiate the I/O ( i.e. do a BMAP 
  831          * operation ).  The swapper handles the case by limiting the amount
  832          * of asynchronous I/O, but that sort of solution doesn't scale well
  833          * for the vnode pager without a lot of work.
  834          *
  835          * Also, the backing vnode's iodone routine may not wake the pageout
  836          * daemon up.  This should be probably be addressed XXX.
  837          */
  838 
  839         if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
  840                 sync |= OBJPC_SYNC;
  841 
  842         /*
  843          * Call device-specific putpages function
  844          */
  845 
  846         vp = object->handle;
  847         rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
  848         if (rtval == EOPNOTSUPP) {
  849             printf("vnode_pager: *** WARNING *** stale FS putpages\n");
  850             rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals);
  851         }
  852         return rtval;
  853 }
  854 
  855 
  856 /*
  857  * This is now called from local media FS's to operate against their
  858  * own vnodes if they fail to implement VOP_GETPAGES.
  859  */
  860 int
  861 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
  862         struct vnode *vp;
  863         vm_page_t *m;
  864         int bytecount;
  865         int flags;
  866         int *rtvals;
  867 {
  868         int i;
  869         vm_object_t object;
  870         int count;
  871 
  872         int maxsize, ncount;
  873         vm_ooffset_t poffset;
  874         struct uio auio;
  875         struct iovec aiov;
  876         int error;
  877         int ioflags;
  878 
  879         object = vp->v_object;
  880         count = bytecount / PAGE_SIZE;
  881 
  882         for (i = 0; i < count; i++)
  883                 rtvals[i] = VM_PAGER_AGAIN;
  884 
  885         if ((int) m[0]->pindex < 0) {
  886                 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n",
  887                         m[0]->pindex, m[0]->dirty);
  888                 rtvals[0] = VM_PAGER_BAD;
  889                 return VM_PAGER_BAD;
  890         }
  891 
  892         maxsize = count * PAGE_SIZE;
  893         ncount = count;
  894 
  895         poffset = IDX_TO_OFF(m[0]->pindex);
  896         if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
  897                 if (object->un_pager.vnp.vnp_size > poffset)
  898                         maxsize = object->un_pager.vnp.vnp_size - poffset;
  899                 else
  900                         maxsize = 0;
  901                 ncount = btoc(maxsize);
  902                 if (ncount < count) {
  903                         for (i = ncount; i < count; i++) {
  904                                 rtvals[i] = VM_PAGER_BAD;
  905                         }
  906                 }
  907         }
  908 
  909         ioflags = IO_VMIO;
  910         ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: 0;
  911         ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
  912 
  913         aiov.iov_base = (caddr_t) 0;
  914         aiov.iov_len = maxsize;
  915         auio.uio_iov = &aiov;
  916         auio.uio_iovcnt = 1;
  917         auio.uio_offset = poffset;
  918         auio.uio_segflg = UIO_NOCOPY;
  919         auio.uio_rw = UIO_WRITE;
  920         auio.uio_resid = maxsize;
  921         auio.uio_procp = (struct proc *) 0;
  922         error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred);
  923         cnt.v_vnodeout++;
  924         cnt.v_vnodepgsout += ncount;
  925 
  926         if (error) {
  927                 printf("vnode_pager_putpages: I/O error %d\n", error);
  928         }
  929         if (auio.uio_resid) {
  930                 printf("vnode_pager_putpages: residual I/O %d at %lu\n",
  931                     auio.uio_resid, (u_long)m[0]->pindex);
  932         }
  933         for (i = 0; i < ncount; i++) {
  934                 rtvals[i] = VM_PAGER_OK;
  935         }
  936         return rtvals[0];
  937 }
  938 
  939 struct vnode *
  940 vnode_pager_lock(object)
  941         vm_object_t object;
  942 {
  943         struct proc *p = curproc;       /* XXX */
  944 
  945         for (; object != NULL; object = object->backing_object) {
  946                 if (object->type != OBJT_VNODE)
  947                         continue;
  948                 if (object->flags & OBJ_DEAD)
  949                         return NULL;
  950 
  951                 while (vget(object->handle,
  952                         LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) {
  953                         if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE))
  954                                 return NULL;
  955                         printf("vnode_pager_lock: retrying\n");
  956                 }
  957                 return object->handle;
  958         }
  959         return NULL;
  960 }

Cache object: 3f54a5ec54ffe4f27d7f4077f4af6616


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