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/fs/nullfs/null_subr.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) 1992, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software donated to Berkeley by
    6  * Jan-Simon Pendry.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)null_subr.c 8.7 (Berkeley) 5/14/95
   33  *
   34  * $FreeBSD: releng/6.4/sys/fs/nullfs/null_subr.c 172899 2007-10-23 03:38:32Z daichi $
   35  */
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/kernel.h>
   40 #include <sys/lock.h>
   41 #include <sys/mutex.h>
   42 #include <sys/malloc.h>
   43 #include <sys/mount.h>
   44 #include <sys/proc.h>
   45 #include <sys/vnode.h>
   46 
   47 #include <fs/nullfs/null.h>
   48 
   49 #define LOG2_SIZEVNODE 8                /* log2(sizeof struct vnode) */
   50 #define NNULLNODECACHE 16
   51 
   52 /*
   53  * Null layer cache:
   54  * Each cache entry holds a reference to the lower vnode
   55  * along with a pointer to the alias vnode.  When an
   56  * entry is added the lower vnode is VREF'd.  When the
   57  * alias is removed the lower vnode is vrele'd.
   58  */
   59 
   60 #define NULL_NHASH(vp) \
   61         (&null_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & null_node_hash])
   62 
   63 static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
   64 static u_long null_node_hash;
   65 struct mtx null_hashmtx;
   66 
   67 static MALLOC_DEFINE(M_NULLFSHASH, "NULLFS hash", "NULLFS hash table");
   68 MALLOC_DEFINE(M_NULLFSNODE, "NULLFS node", "NULLFS vnode private part");
   69 
   70 static struct vnode * null_hashget(struct mount *, struct vnode *);
   71 static struct vnode * null_hashins(struct mount *, struct null_node *);
   72 
   73 /*
   74  * Initialise cache headers
   75  */
   76 int
   77 nullfs_init(vfsp)
   78         struct vfsconf *vfsp;
   79 {
   80 
   81         NULLFSDEBUG("nullfs_init\n");           /* printed during system boot */
   82         null_node_hashtbl = hashinit(NNULLNODECACHE, M_NULLFSHASH, &null_node_hash);
   83         mtx_init(&null_hashmtx, "nullhs", NULL, MTX_DEF);
   84         return (0);
   85 }
   86 
   87 int
   88 nullfs_uninit(vfsp)
   89         struct vfsconf *vfsp;
   90 {
   91 
   92         mtx_destroy(&null_hashmtx);
   93         free(null_node_hashtbl, M_NULLFSHASH);
   94         return (0);
   95 }
   96 
   97 /*
   98  * Return a VREF'ed alias for lower vnode if already exists, else 0.
   99  * Lower vnode should be locked on entry and will be left locked on exit.
  100  */
  101 static struct vnode *
  102 null_hashget(mp, lowervp)
  103         struct mount *mp;
  104         struct vnode *lowervp;
  105 {
  106         struct thread *td = curthread;  /* XXX */
  107         struct null_node_hashhead *hd;
  108         struct null_node *a;
  109         struct vnode *vp;
  110         int error;
  111 
  112         ASSERT_VOP_LOCKED(lowervp, "null_hashget");
  113 
  114         /*
  115          * Find hash base, and then search the (two-way) linked
  116          * list looking for a null_node structure which is referencing
  117          * the lower vnode.  If found, the increment the null_node
  118          * reference count (but NOT the lower vnode's VREF counter).
  119          */
  120         hd = NULL_NHASH(lowervp);
  121         mtx_lock(&null_hashmtx);
  122         LIST_FOREACH(a, hd, null_hash) {
  123                 if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
  124                         vp = NULLTOV(a);
  125                         VI_LOCK(vp);
  126                         mtx_unlock(&null_hashmtx);
  127                         /*
  128                          * We need to clear the OWEINACT flag here as this
  129                          * may lead vget() to try to lock our vnode which
  130                          * is already locked via lowervp.
  131                          */
  132                         vp->v_iflag &= ~VI_OWEINACT;
  133                         error = vget(vp, LK_INTERLOCK, td);
  134                         /*
  135                          * Since we have the lower node locked the nullfs
  136                          * node can not be in the process of recycling.  If
  137                          * it had been recycled before we grabed the lower
  138                          * lock it would not have been found on the hash.
  139                          */
  140                         if (error)
  141                                 panic("null_hashget: vget error %d", error);
  142                         return (vp);
  143                 }
  144         }
  145         mtx_unlock(&null_hashmtx);
  146         return (NULLVP);
  147 }
  148 
  149 /*
  150  * Act like null_hashget, but add passed null_node to hash if no existing
  151  * node found.
  152  */
  153 static struct vnode *
  154 null_hashins(mp, xp)
  155         struct mount *mp;
  156         struct null_node *xp;
  157 {
  158         struct thread *td = curthread;  /* XXX */
  159         struct null_node_hashhead *hd;
  160         struct null_node *oxp;
  161         struct vnode *ovp;
  162         int error;
  163 
  164         hd = NULL_NHASH(xp->null_lowervp);
  165         mtx_lock(&null_hashmtx);
  166         LIST_FOREACH(oxp, hd, null_hash) {
  167                 if (oxp->null_lowervp == xp->null_lowervp &&
  168                     NULLTOV(oxp)->v_mount == mp) {
  169                         /*
  170                          * See null_hashget for a description of this
  171                          * operation.
  172                          */
  173                         ovp = NULLTOV(oxp);
  174                         VI_LOCK(ovp);
  175                         mtx_unlock(&null_hashmtx);
  176                         ovp->v_iflag &= ~VI_OWEINACT;
  177                         error = vget(ovp, LK_INTERLOCK, td);
  178                         if (error)
  179                                 panic("null_hashins: vget error %d", error);
  180                         return (ovp);
  181                 }
  182         }
  183         LIST_INSERT_HEAD(hd, xp, null_hash);
  184         mtx_unlock(&null_hashmtx);
  185         return (NULLVP);
  186 }
  187 
  188 /*
  189  * Make a new or get existing nullfs node.
  190  * Vp is the alias vnode, lowervp is the lower vnode.
  191  * 
  192  * The lowervp assumed to be locked and having "spare" reference. This routine
  193  * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers"
  194  * the caller's "spare" reference to created nullfs vnode.
  195  */
  196 int
  197 null_nodeget(mp, lowervp, vpp)
  198         struct mount *mp;
  199         struct vnode *lowervp;
  200         struct vnode **vpp;
  201 {
  202         struct null_node *xp;
  203         struct vnode *vp;
  204         int error;
  205 
  206         /* Lookup the hash firstly */
  207         *vpp = null_hashget(mp, lowervp);
  208         if (*vpp != NULL) {
  209                 vrele(lowervp);
  210                 return (0);
  211         }
  212 
  213         /*
  214          * We do not serialize vnode creation, instead we will check for
  215          * duplicates later, when adding new vnode to hash.
  216          *
  217          * Note that duplicate can only appear in hash if the lowervp is
  218          * locked LK_SHARED.
  219          */
  220 
  221         /*
  222          * Do the MALLOC before the getnewvnode since doing so afterward
  223          * might cause a bogus v_data pointer to get dereferenced
  224          * elsewhere if MALLOC should block.
  225          */
  226         MALLOC(xp, struct null_node *, sizeof(struct null_node),
  227             M_NULLFSNODE, M_WAITOK);
  228 
  229         error = getnewvnode("null", mp, &null_vnodeops, &vp);
  230         if (error) {
  231                 FREE(xp, M_NULLFSNODE);
  232                 return (error);
  233         }
  234 
  235         xp->null_vnode = vp;
  236         xp->null_lowervp = lowervp;
  237         vp->v_type = lowervp->v_type;
  238         vp->v_data = xp;
  239         vp->v_vnlock = lowervp->v_vnlock;
  240         if (vp->v_vnlock == NULL)
  241                 panic("null_nodeget: Passed a NULL vnlock.\n");
  242         /*
  243          * Atomically insert our new node into the hash or vget existing 
  244          * if someone else has beaten us to it.
  245          */
  246         *vpp = null_hashins(mp, xp);
  247         if (*vpp != NULL) {
  248                 vrele(lowervp);
  249                 vp->v_vnlock = &vp->v_lock;
  250                 xp->null_lowervp = NULL;
  251                 vrele(vp);
  252                 return (0);
  253         }
  254         *vpp = vp;
  255 
  256         return (0);
  257 }
  258 
  259 /*
  260  * Remove node from hash.
  261  */
  262 void
  263 null_hashrem(xp)
  264         struct null_node *xp;
  265 {
  266 
  267         mtx_lock(&null_hashmtx);
  268         LIST_REMOVE(xp, null_hash);
  269         mtx_unlock(&null_hashmtx);
  270 }
  271 
  272 #ifdef DIAGNOSTIC
  273 
  274 #ifdef KDB
  275 #define null_checkvp_barrier    1
  276 #else
  277 #define null_checkvp_barrier    0
  278 #endif
  279 
  280 struct vnode *
  281 null_checkvp(vp, fil, lno)
  282         struct vnode *vp;
  283         char *fil;
  284         int lno;
  285 {
  286         int interlock = 0;
  287         struct null_node *a = VTONULL(vp);
  288 #ifdef notyet
  289         /*
  290          * Can't do this check because vop_reclaim runs
  291          * with a funny vop vector.
  292          */
  293         if (vp->v_op != null_vnodeop_p) {
  294                 printf ("null_checkvp: on non-null-node\n");
  295                 while (null_checkvp_barrier) /*WAIT*/ ;
  296                 panic("null_checkvp");
  297         };
  298 #endif
  299         if (a->null_lowervp == NULLVP) {
  300                 /* Should never happen */
  301                 int i; u_long *p;
  302                 printf("vp = %p, ZERO ptr\n", (void *)vp);
  303                 for (p = (u_long *) a, i = 0; i < 8; i++)
  304                         printf(" %lx", p[i]);
  305                 printf("\n");
  306                 /* wait for debugger */
  307                 while (null_checkvp_barrier) /*WAIT*/ ;
  308                 panic("null_checkvp");
  309         }
  310         if (mtx_owned(VI_MTX(vp)) != 0) {
  311                 VI_UNLOCK(vp);
  312                 interlock = 1;
  313         }
  314         if (vrefcnt(a->null_lowervp) < 1) {
  315                 int i; u_long *p;
  316                 printf("vp = %p, unref'ed lowervp\n", (void *)vp);
  317                 for (p = (u_long *) a, i = 0; i < 8; i++)
  318                         printf(" %lx", p[i]);
  319                 printf("\n");
  320                 /* wait for debugger */
  321                 while (null_checkvp_barrier) /*WAIT*/ ;
  322                 panic ("null with unref'ed lowervp");
  323         };
  324         if (interlock != 0)
  325                 VI_LOCK(vp);
  326 #ifdef notyet
  327         printf("null %x/%d -> %x/%d [%s, %d]\n",
  328                 NULLTOV(a), vrefcnt(NULLTOV(a)),
  329                 a->null_lowervp, vrefcnt(a->null_lowervp),
  330                 fil, lno);
  331 #endif
  332         return a->null_lowervp;
  333 }
  334 #endif

Cache object: 296a3ed6b0ec1011a2251cee6ae228de


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