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/kern/vfs_hash.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2005 Poul-Henning Kamp
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD: stable/10/sys/kern/vfs_hash.c 300140 2016-05-18 11:58:16Z kib $");
   30 
   31 #include <sys/param.h>
   32 #include <sys/systm.h>
   33 #include <sys/kernel.h>
   34 #include <sys/malloc.h>
   35 #include <sys/mount.h>
   36 #include <sys/vnode.h>
   37 
   38 static MALLOC_DEFINE(M_VFS_HASH, "vfs_hash", "VFS hash table");
   39 
   40 static LIST_HEAD(vfs_hash_head, vnode)  *vfs_hash_tbl;
   41 static LIST_HEAD(,vnode)                vfs_hash_side;
   42 static u_long                           vfs_hash_mask;
   43 static struct mtx                       vfs_hash_mtx;
   44 
   45 static void
   46 vfs_hashinit(void *dummy __unused)
   47 {
   48 
   49         vfs_hash_tbl = hashinit(desiredvnodes, M_VFS_HASH, &vfs_hash_mask);
   50         mtx_init(&vfs_hash_mtx, "vfs hash", NULL, MTX_DEF);
   51         LIST_INIT(&vfs_hash_side);
   52 }
   53 
   54 /* Must be SI_ORDER_SECOND so desiredvnodes is available */
   55 SYSINIT(vfs_hash, SI_SUB_VFS, SI_ORDER_SECOND, vfs_hashinit, NULL);
   56 
   57 u_int
   58 vfs_hash_index(struct vnode *vp)
   59 {
   60 
   61         return (vp->v_hash + vp->v_mount->mnt_hashseed);
   62 }
   63 
   64 static struct vfs_hash_head *
   65 vfs_hash_bucket(const struct mount *mp, u_int hash)
   66 {
   67 
   68         return (&vfs_hash_tbl[(hash + mp->mnt_hashseed) & vfs_hash_mask]);
   69 }
   70 
   71 int
   72 vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td,
   73     struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
   74 {
   75         struct vnode *vp;
   76         int error;
   77 
   78         while (1) {
   79                 mtx_lock(&vfs_hash_mtx);
   80                 LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) {
   81                         if (vp->v_hash != hash)
   82                                 continue;
   83                         if (vp->v_mount != mp)
   84                                 continue;
   85                         if (fn != NULL && fn(vp, arg))
   86                                 continue;
   87                         VI_LOCK(vp);
   88                         mtx_unlock(&vfs_hash_mtx);
   89                         error = vget(vp, flags | LK_INTERLOCK, td);
   90                         if (error == ENOENT && (flags & LK_NOWAIT) == 0)
   91                                 break;
   92                         if (error)
   93                                 return (error);
   94                         *vpp = vp;
   95                         return (0);
   96                 }
   97                 if (vp == NULL) {
   98                         mtx_unlock(&vfs_hash_mtx);
   99                         *vpp = NULL;
  100                         return (0);
  101                 }
  102         }
  103 }
  104 
  105 void
  106 vfs_hash_ref(const struct mount *mp, u_int hash, struct thread *td,
  107     struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
  108 {
  109         struct vnode *vp;
  110 
  111         while (1) {
  112                 mtx_lock(&vfs_hash_mtx);
  113                 LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) {
  114                         if (vp->v_hash != hash)
  115                                 continue;
  116                         if (vp->v_mount != mp)
  117                                 continue;
  118                         if (fn != NULL && fn(vp, arg))
  119                                 continue;
  120                         vhold(vp);
  121                         mtx_unlock(&vfs_hash_mtx);
  122                         vref(vp);
  123                         vdrop(vp);
  124                         *vpp = vp;
  125                         return;
  126                 }
  127                 if (vp == NULL) {
  128                         mtx_unlock(&vfs_hash_mtx);
  129                         *vpp = NULL;
  130                         return;
  131                 }
  132         }
  133 }
  134 
  135 void
  136 vfs_hash_remove(struct vnode *vp)
  137 {
  138 
  139         mtx_lock(&vfs_hash_mtx);
  140         LIST_REMOVE(vp, v_hashlist);
  141         mtx_unlock(&vfs_hash_mtx);
  142 }
  143 
  144 int
  145 vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td,
  146     struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
  147 {
  148         struct vnode *vp2;
  149         int error;
  150 
  151         *vpp = NULL;
  152         while (1) {
  153                 mtx_lock(&vfs_hash_mtx);
  154                 LIST_FOREACH(vp2,
  155                     vfs_hash_bucket(vp->v_mount, hash), v_hashlist) {
  156                         if (vp2->v_hash != hash)
  157                                 continue;
  158                         if (vp2->v_mount != vp->v_mount)
  159                                 continue;
  160                         if (fn != NULL && fn(vp2, arg))
  161                                 continue;
  162                         VI_LOCK(vp2);
  163                         mtx_unlock(&vfs_hash_mtx);
  164                         error = vget(vp2, flags | LK_INTERLOCK, td);
  165                         if (error == ENOENT && (flags & LK_NOWAIT) == 0)
  166                                 break;
  167                         mtx_lock(&vfs_hash_mtx);
  168                         LIST_INSERT_HEAD(&vfs_hash_side, vp, v_hashlist);
  169                         mtx_unlock(&vfs_hash_mtx);
  170                         vput(vp);
  171                         if (!error)
  172                                 *vpp = vp2;
  173                         return (error);
  174                 }
  175                 if (vp2 == NULL)
  176                         break;
  177                         
  178         }
  179         vp->v_hash = hash;
  180         LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
  181         mtx_unlock(&vfs_hash_mtx);
  182         return (0);
  183 }
  184 
  185 void
  186 vfs_hash_rehash(struct vnode *vp, u_int hash)
  187 {
  188 
  189         mtx_lock(&vfs_hash_mtx);
  190         LIST_REMOVE(vp, v_hashlist);
  191         LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
  192         vp->v_hash = hash;
  193         mtx_unlock(&vfs_hash_mtx);
  194 }
  195 
  196 void
  197 vfs_hash_changesize(int newmaxvnodes)
  198 {
  199         struct vfs_hash_head *vfs_hash_newtbl, *vfs_hash_oldtbl;
  200         u_long vfs_hash_newmask, vfs_hash_oldmask;
  201         struct vnode *vp;
  202         int i;
  203 
  204         vfs_hash_newtbl = hashinit(newmaxvnodes, M_VFS_HASH,
  205                 &vfs_hash_newmask);
  206         /* If same hash table size, nothing to do */
  207         if (vfs_hash_mask == vfs_hash_newmask) {
  208                 free(vfs_hash_newtbl, M_VFS_HASH);
  209                 return;
  210         }
  211         /*
  212          * Move everything from the old hash table to the new table.
  213          * None of the vnodes in the table can be recycled because to
  214          * do so, they have to be removed from the hash table.
  215          */
  216         mtx_lock(&vfs_hash_mtx);
  217         vfs_hash_oldtbl = vfs_hash_tbl;
  218         vfs_hash_oldmask = vfs_hash_mask;
  219         vfs_hash_tbl = vfs_hash_newtbl;
  220         vfs_hash_mask = vfs_hash_newmask;
  221         for (i = 0; i <= vfs_hash_oldmask; i++) {
  222                 while ((vp = LIST_FIRST(&vfs_hash_oldtbl[i])) != NULL) {
  223                         LIST_REMOVE(vp, v_hashlist);
  224                         LIST_INSERT_HEAD(
  225                             vfs_hash_bucket(vp->v_mount, vp->v_hash),
  226                             vp, v_hashlist);
  227                 }
  228         }
  229         mtx_unlock(&vfs_hash_mtx);
  230         free(vfs_hash_oldtbl, M_VFS_HASH);
  231 }

Cache object: 3a5d8e117a92aa031ef6c95836cae99e


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