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/uvm/uvm_vnode.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 /*      $NetBSD: uvm_vnode.c,v 1.77.2.1 2007/02/17 23:27:53 tron Exp $  */
    2 
    3 /*
    4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
    5  * Copyright (c) 1991, 1993
    6  *      The Regents of the University of California.
    7  * Copyright (c) 1990 University of Utah.
    8  *
    9  * All rights reserved.
   10  *
   11  * This code is derived from software contributed to Berkeley by
   12  * the Systems Programming Group of the University of Utah Computer
   13  * Science Department.
   14  *
   15  * Redistribution and use in source and binary forms, with or without
   16  * modification, are permitted provided that the following conditions
   17  * are met:
   18  * 1. Redistributions of source code must retain the above copyright
   19  *    notice, this list of conditions and the following disclaimer.
   20  * 2. Redistributions in binary form must reproduce the above copyright
   21  *    notice, this list of conditions and the following disclaimer in the
   22  *    documentation and/or other materials provided with the distribution.
   23  * 3. All advertising materials mentioning features or use of this software
   24  *    must display the following acknowledgement:
   25  *      This product includes software developed by Charles D. Cranor,
   26  *      Washington University, the University of California, Berkeley and
   27  *      its contributors.
   28  * 4. Neither the name of the University nor the names of its contributors
   29  *    may be used to endorse or promote products derived from this software
   30  *    without specific prior written permission.
   31  *
   32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   42  * SUCH DAMAGE.
   43  *
   44  *      @(#)vnode_pager.c       8.8 (Berkeley) 2/13/94
   45  * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
   46  */
   47 
   48 /*
   49  * uvm_vnode.c: the vnode pager.
   50  */
   51 
   52 #include <sys/cdefs.h>
   53 __KERNEL_RCSID(0, "$NetBSD: uvm_vnode.c,v 1.77.2.1 2007/02/17 23:27:53 tron Exp $");
   54 
   55 #include "fs_nfs.h"
   56 #include "opt_uvmhist.h"
   57 #include "opt_ddb.h"
   58 
   59 #include <sys/param.h>
   60 #include <sys/systm.h>
   61 #include <sys/kernel.h>
   62 #include <sys/proc.h>
   63 #include <sys/malloc.h>
   64 #include <sys/vnode.h>
   65 #include <sys/disklabel.h>
   66 #include <sys/ioctl.h>
   67 #include <sys/fcntl.h>
   68 #include <sys/conf.h>
   69 #include <sys/pool.h>
   70 #include <sys/mount.h>
   71 
   72 #include <miscfs/specfs/specdev.h>
   73 
   74 #include <uvm/uvm.h>
   75 #include <uvm/uvm_readahead.h>
   76 
   77 /*
   78  * functions
   79  */
   80 
   81 static void     uvn_detach(struct uvm_object *);
   82 static int      uvn_get(struct uvm_object *, voff_t, struct vm_page **, int *,
   83                         int, vm_prot_t, int, int);
   84 static int      uvn_put(struct uvm_object *, voff_t, voff_t, int);
   85 static void     uvn_reference(struct uvm_object *);
   86 
   87 static int      uvn_findpage(struct uvm_object *, voff_t, struct vm_page **,
   88                              int);
   89 
   90 /*
   91  * master pager structure
   92  */
   93 
   94 struct uvm_pagerops uvm_vnodeops = {
   95         NULL,
   96         uvn_reference,
   97         uvn_detach,
   98         NULL,
   99         uvn_get,
  100         uvn_put,
  101 };
  102 
  103 /*
  104  * the ops!
  105  */
  106 
  107 /*
  108  * uvn_attach
  109  *
  110  * attach a vnode structure to a VM object.  if the vnode is already
  111  * attached, then just bump the reference count by one and return the
  112  * VM object.   if not already attached, attach and return the new VM obj.
  113  * the "accessprot" tells the max access the attaching thread wants to
  114  * our pages.
  115  *
  116  * => caller must _not_ already be holding the lock on the uvm_object.
  117  * => in fact, nothing should be locked so that we can sleep here.
  118  * => note that uvm_object is first thing in vnode structure, so their
  119  *    pointers are equiv.
  120  */
  121 
  122 struct uvm_object *
  123 uvn_attach(void *arg, vm_prot_t accessprot)
  124 {
  125         struct vnode *vp = arg;
  126         struct uvm_object *uobj = &vp->v_uobj;
  127         struct vattr vattr;
  128         const struct bdevsw *bdev;
  129         int result;
  130         struct partinfo pi;
  131         voff_t used_vnode_size;
  132         UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
  133 
  134         UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
  135         used_vnode_size = (voff_t)0;
  136 
  137         /*
  138          * first get a lock on the uobj.
  139          */
  140 
  141         simple_lock(&uobj->vmobjlock);
  142         while (vp->v_flag & VXLOCK) {
  143                 vp->v_flag |= VXWANT;
  144                 UVMHIST_LOG(maphist, "  SLEEPING on blocked vn",0,0,0,0);
  145                 UVM_UNLOCK_AND_WAIT(uobj, &uobj->vmobjlock, FALSE,
  146                     "uvn_attach", 0);
  147                 simple_lock(&uobj->vmobjlock);
  148                 UVMHIST_LOG(maphist,"  WOKE UP",0,0,0,0);
  149         }
  150 
  151         /*
  152          * if we're mapping a BLK device, make sure it is a disk.
  153          */
  154         if (vp->v_type == VBLK) {
  155                 bdev = bdevsw_lookup(vp->v_rdev);
  156                 if (bdev == NULL || bdev->d_type != D_DISK) {
  157                         simple_unlock(&uobj->vmobjlock);
  158                         UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)",
  159                                     0,0,0,0);
  160                         return(NULL);
  161                 }
  162         }
  163         KASSERT(vp->v_type == VREG || vp->v_type == VBLK);
  164 
  165         /*
  166          * set up our idea of the size
  167          * if this hasn't been done already.
  168          */
  169         if (vp->v_size == VSIZENOTSET) {
  170 
  171 
  172         vp->v_flag |= VXLOCK;
  173         simple_unlock(&uobj->vmobjlock); /* drop lock in case we sleep */
  174                 /* XXX: curproc? */
  175         if (vp->v_type == VBLK) {
  176                 /*
  177                  * We could implement this as a specfs getattr call, but:
  178                  *
  179                  *      (1) VOP_GETATTR() would get the file system
  180                  *          vnode operation, not the specfs operation.
  181                  *
  182                  *      (2) All we want is the size, anyhow.
  183                  */
  184                 bdev = bdevsw_lookup(vp->v_rdev);
  185                 if (bdev != NULL) {
  186                         result = (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART,
  187                                                   (caddr_t)&pi, FREAD, curlwp);
  188                 } else {
  189                         result = ENXIO;
  190                 }
  191                 if (result == 0) {
  192                         /* XXX should remember blocksize */
  193                         used_vnode_size = (voff_t)pi.disklab->d_secsize *
  194                             (voff_t)pi.part->p_size;
  195                 }
  196         } else {
  197                 result = VOP_GETATTR(vp, &vattr, curlwp->l_cred, curlwp);
  198                 if (result == 0)
  199                         used_vnode_size = vattr.va_size;
  200         }
  201 
  202         /* relock object */
  203         simple_lock(&uobj->vmobjlock);
  204 
  205         if (vp->v_flag & VXWANT) {
  206                 wakeup(vp);
  207         }
  208         vp->v_flag &= ~(VXLOCK|VXWANT);
  209 
  210         if (result != 0) {
  211                 simple_unlock(&uobj->vmobjlock);
  212                 UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
  213                 return(NULL);
  214         }
  215         vp->v_size = used_vnode_size;
  216 
  217         }
  218 
  219         simple_unlock(&uobj->vmobjlock);
  220         UVMHIST_LOG(maphist,"<- done, refcnt=%d", vp->v_usecount,
  221             0, 0, 0);
  222         return uobj;
  223 }
  224 
  225 
  226 /*
  227  * uvn_reference
  228  *
  229  * duplicate a reference to a VM object.  Note that the reference
  230  * count must already be at least one (the passed in reference) so
  231  * there is no chance of the uvn being killed or locked out here.
  232  *
  233  * => caller must call with object unlocked.
  234  * => caller must be using the same accessprot as was used at attach time
  235  */
  236 
  237 static void
  238 uvn_reference(struct uvm_object *uobj)
  239 {
  240         VREF((struct vnode *)uobj);
  241 }
  242 
  243 
  244 /*
  245  * uvn_detach
  246  *
  247  * remove a reference to a VM object.
  248  *
  249  * => caller must call with object unlocked and map locked.
  250  */
  251 
  252 static void
  253 uvn_detach(struct uvm_object *uobj)
  254 {
  255         vrele((struct vnode *)uobj);
  256 }
  257 
  258 /*
  259  * uvn_put: flush page data to backing store.
  260  *
  261  * => object must be locked on entry!   VOP_PUTPAGES must unlock it.
  262  * => flags: PGO_SYNCIO -- use sync. I/O
  263  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
  264  */
  265 
  266 static int
  267 uvn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
  268 {
  269         struct vnode *vp = (struct vnode *)uobj;
  270         int error;
  271 
  272         LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
  273         error = VOP_PUTPAGES(vp, offlo, offhi, flags);
  274         LOCK_ASSERT(!simple_lock_held(&vp->v_interlock));
  275         return error;
  276 }
  277 
  278 
  279 /*
  280  * uvn_get: get pages (synchronously) from backing store
  281  *
  282  * => prefer map unlocked (not required)
  283  * => object must be locked!  we will _unlock_ it before starting any I/O.
  284  * => flags: PGO_ALLPAGES: get all of the pages
  285  *           PGO_LOCKED: fault data structures are locked
  286  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
  287  * => NOTE: caller must check for released pages!!
  288  */
  289 
  290 static int
  291 uvn_get(struct uvm_object *uobj, voff_t offset,
  292     struct vm_page **pps /* IN/OUT */,
  293     int *npagesp /* IN (OUT if PGO_LOCKED)*/,
  294     int centeridx, vm_prot_t access_type, int advice, int flags)
  295 {
  296         struct vnode *vp = (struct vnode *)uobj;
  297         int error;
  298 
  299         UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
  300 
  301         UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
  302 
  303         if ((access_type & VM_PROT_WRITE) == 0 && (flags & PGO_LOCKED) == 0) {
  304                 simple_unlock(&vp->v_interlock);
  305                 vn_ra_allocctx(vp);
  306                 uvm_ra_request(vp->v_ractx, advice, uobj, offset,
  307                     *npagesp << PAGE_SHIFT);
  308                 simple_lock(&vp->v_interlock);
  309         }
  310 
  311         error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
  312                              access_type, advice, flags);
  313 
  314         LOCK_ASSERT(((flags & PGO_LOCKED) != 0 &&
  315                      simple_lock_held(&vp->v_interlock)) ||
  316                     ((flags & PGO_LOCKED) == 0 &&
  317                      !simple_lock_held(&vp->v_interlock)));
  318         return error;
  319 }
  320 
  321 
  322 /*
  323  * uvn_findpages:
  324  * return the page for the uobj and offset requested, allocating if needed.
  325  * => uobj must be locked.
  326  * => returned pages will be BUSY.
  327  */
  328 
  329 int
  330 uvn_findpages(struct uvm_object *uobj, voff_t offset, int *npagesp,
  331     struct vm_page **pgs, int flags)
  332 {
  333         int i, count, found, npages, rv;
  334 
  335         count = found = 0;
  336         npages = *npagesp;
  337         if (flags & UFP_BACKWARD) {
  338                 for (i = npages - 1; i >= 0; i--, offset -= PAGE_SIZE) {
  339                         rv = uvn_findpage(uobj, offset, &pgs[i], flags);
  340                         if (rv == 0) {
  341                                 if (flags & UFP_DIRTYONLY)
  342                                         break;
  343                         } else
  344                                 found++;
  345                         count++;
  346                 }
  347         } else {
  348                 for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
  349                         rv = uvn_findpage(uobj, offset, &pgs[i], flags);
  350                         if (rv == 0) {
  351                                 if (flags & UFP_DIRTYONLY)
  352                                         break;
  353                         } else
  354                                 found++;
  355                         count++;
  356                 }
  357         }
  358         *npagesp = count;
  359         return (found);
  360 }
  361 
  362 static int
  363 uvn_findpage(struct uvm_object *uobj, voff_t offset, struct vm_page **pgp,
  364     int flags)
  365 {
  366         struct vm_page *pg;
  367         boolean_t dirty;
  368         UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
  369         UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
  370 
  371         if (*pgp != NULL) {
  372                 UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
  373                 return 0;
  374         }
  375         for (;;) {
  376                 /* look for an existing page */
  377                 pg = uvm_pagelookup(uobj, offset);
  378 
  379                 /* nope?  allocate one now */
  380                 if (pg == NULL) {
  381                         if (flags & UFP_NOALLOC) {
  382                                 UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
  383                                 return 0;
  384                         }
  385                         pg = uvm_pagealloc(uobj, offset, NULL, 0);
  386                         if (pg == NULL) {
  387                                 if (flags & UFP_NOWAIT) {
  388                                         UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
  389                                         return 0;
  390                                 }
  391                                 simple_unlock(&uobj->vmobjlock);
  392                                 uvm_wait("uvn_fp1");
  393                                 simple_lock(&uobj->vmobjlock);
  394                                 continue;
  395                         }
  396                         UVMHIST_LOG(ubchist, "alloced %p", pg,0,0,0);
  397                         break;
  398                 } else if (flags & UFP_NOCACHE) {
  399                         UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
  400                         return 0;
  401                 }
  402 
  403                 /* page is there, see if we need to wait on it */
  404                 if ((pg->flags & PG_BUSY) != 0) {
  405                         if (flags & UFP_NOWAIT) {
  406                                 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
  407                                 return 0;
  408                         }
  409                         pg->flags |= PG_WANTED;
  410                         UVMHIST_LOG(ubchist, "wait %p", pg,0,0,0);
  411                         UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
  412                                             "uvn_fp2", 0);
  413                         simple_lock(&uobj->vmobjlock);
  414                         continue;
  415                 }
  416 
  417                 /* skip PG_RDONLY pages if requested */
  418                 if ((flags & UFP_NORDONLY) && (pg->flags & PG_RDONLY)) {
  419                         UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
  420                         return 0;
  421                 }
  422 
  423                 /* stop on clean pages if requested */
  424                 if (flags & UFP_DIRTYONLY) {
  425                         dirty = pmap_clear_modify(pg) ||
  426                                 (pg->flags & PG_CLEAN) == 0;
  427                         pg->flags |= PG_CLEAN;
  428                         if (!dirty) {
  429                                 UVMHIST_LOG(ubchist, "dirtonly", 0,0,0,0);
  430                                 return 0;
  431                         }
  432                 }
  433 
  434                 /* mark the page BUSY and we're done. */
  435                 pg->flags |= PG_BUSY;
  436                 UVM_PAGE_OWN(pg, "uvn_findpage");
  437                 UVMHIST_LOG(ubchist, "found %p", pg,0,0,0);
  438                 break;
  439         }
  440         *pgp = pg;
  441         return 1;
  442 }
  443 
  444 /*
  445  * uvm_vnp_setsize: grow or shrink a vnode uobj
  446  *
  447  * grow   => just update size value
  448  * shrink => toss un-needed pages
  449  *
  450  * => we assume that the caller has a reference of some sort to the
  451  *      vnode in question so that it will not be yanked out from under
  452  *      us.
  453  */
  454 
  455 void
  456 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
  457 {
  458         struct uvm_object *uobj = &vp->v_uobj;
  459         voff_t pgend = round_page(newsize);
  460         voff_t oldsize;
  461         UVMHIST_FUNC("uvm_vnp_setsize"); UVMHIST_CALLED(ubchist);
  462 
  463         simple_lock(&uobj->vmobjlock);
  464         UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
  465             vp, vp->v_size, newsize, 0);
  466 
  467         /*
  468          * now check if the size has changed: if we shrink we had better
  469          * toss some pages...
  470          */
  471 
  472         oldsize = vp->v_size;
  473         if (oldsize > pgend && oldsize != VSIZENOTSET) {
  474                 (void) uvn_put(uobj, pgend, 0, PGO_FREE | PGO_SYNCIO);
  475                 simple_lock(&uobj->vmobjlock);
  476         }
  477         vp->v_size = newsize;
  478         simple_unlock(&uobj->vmobjlock);
  479 }
  480 
  481 /*
  482  * uvm_vnp_zerorange:  set a range of bytes in a file to zero.
  483  */
  484 
  485 void
  486 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
  487 {
  488         void *win;
  489         int flags;
  490 
  491         /*
  492          * XXXUBC invent kzero() and use it
  493          */
  494 
  495         while (len) {
  496                 vsize_t bytelen = len;
  497 
  498                 win = ubc_alloc(&vp->v_uobj, off, &bytelen, UVM_ADV_NORMAL,
  499                     UBC_WRITE);
  500                 memset(win, 0, bytelen);
  501                 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
  502                 ubc_release(win, flags);
  503 
  504                 off += bytelen;
  505                 len -= bytelen;
  506         }
  507 }
  508 
  509 boolean_t
  510 uvn_text_p(struct uvm_object *uobj)
  511 {
  512         struct vnode *vp = (struct vnode *)uobj;
  513 
  514         return (vp->v_flag & VEXECMAP) != 0;
  515 }
  516 
  517 boolean_t
  518 uvn_clean_p(struct uvm_object *uobj)
  519 {
  520         struct vnode *vp = (struct vnode *)uobj;
  521 
  522         return (vp->v_flag & VONWORKLST) == 0;
  523 }
  524 
  525 boolean_t
  526 uvn_needs_writefault_p(struct uvm_object *uobj)
  527 {
  528         struct vnode *vp = (struct vnode *)uobj;
  529 
  530         return uvn_clean_p(uobj) ||
  531             (vp->v_flag & (VWRITEMAP|VWRITEMAPDIRTY)) == VWRITEMAP;
  532 }

Cache object: f36f83ca355c766ac4197ad84e02bfd5


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