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/ufs/ufs/dirhash.h

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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2001 Ian Dowse.  All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  *
   27  * $FreeBSD$
   28  */
   29 
   30 #ifndef _UFS_UFS_DIRHASH_H_
   31 #define _UFS_UFS_DIRHASH_H_
   32 
   33 #include <sys/_lock.h>
   34 #include <sys/_sx.h>
   35 
   36 /*
   37  * For fast operations on large directories, we maintain a hash
   38  * that maps the file name to the offset of the directory entry within
   39  * the directory file.
   40  *
   41  * The hashing uses a dumb spillover to the next free slot on
   42  * collisions, so we must keep the utilisation low to avoid
   43  * long linear searches. Deleted entries that are not the last
   44  * in a chain must be marked DIRHASH_DEL.
   45  *
   46  * We also maintain information about free space in each block
   47  * to speed up creations.
   48  */
   49 #define DIRHASH_EMPTY   (-1)    /* entry unused */
   50 #define DIRHASH_DEL     (-2)    /* deleted entry; may be part of chain */
   51 
   52 #define DIRALIGN        4
   53 #define DH_NFSTATS      (DIRECTSIZ(UFS_MAXNAMLEN + 1) / DIRALIGN)
   54                                  /* max DIRALIGN words in a directory entry */
   55 
   56 /*
   57  * Dirhash uses a score mechanism to achieve a hybrid between a
   58  * least-recently-used and a least-often-used algorithm for entry
   59  * recycling. The score is incremented when a directory is used, and
   60  * decremented when the directory is a candidate for recycling. When
   61  * the score reaches zero, the hash is recycled. Hashes are linked
   62  * together on a TAILQ list, and hashes with higher scores filter
   63  * towards the tail (most recently used) end of the list.
   64  *
   65  * New hash entries are given an initial score of DH_SCOREINIT and are
   66  * placed at the most-recently-used end of the list. This helps a lot
   67  * in the worst-case case scenario where every directory access is
   68  * to a directory that is not hashed (i.e. the working set of hash
   69  * candidates is much larger than the configured memory limit). In this
   70  * case it limits the number of hash builds to 1/DH_SCOREINIT of the
   71  * number of accesses.
   72  */ 
   73 #define DH_SCOREINIT    8       /* initial dh_score when dirhash built */
   74 #define DH_SCOREMAX     64      /* max dh_score value */
   75 
   76 /*
   77  * The main hash table has 2 levels. It is an array of pointers to
   78  * blocks of DH_NBLKOFF offsets.
   79  */
   80 #define DH_BLKOFFSHIFT  8
   81 #define DH_NBLKOFF      (1 << DH_BLKOFFSHIFT)
   82 #define DH_BLKOFFMASK   (DH_NBLKOFF - 1)
   83 
   84 #define DH_ENTRY(dh, slot) \
   85     ((dh)->dh_hash[(slot) >> DH_BLKOFFSHIFT][(slot) & DH_BLKOFFMASK])
   86 
   87 struct dirhash {
   88         struct sx dh_lock;      /* protects all fields except list & score */
   89         int     dh_refcount;
   90 
   91         doff_t  **dh_hash;      /* the hash array (2-level) */
   92         int     dh_narrays;     /* number of entries in dh_hash */
   93         int     dh_hlen;        /* total slots in the 2-level hash array */
   94         int     dh_hused;       /* entries in use */
   95         int     dh_memreq;      /* Memory used. */
   96 
   97         /* Free space statistics. XXX assumes DIRBLKSIZ is 512. */
   98         u_int8_t *dh_blkfree;   /* free DIRALIGN words in each dir block */
   99         int     dh_nblk;        /* size of dh_blkfree array */
  100         int     dh_dirblks;     /* number of DIRBLKSIZ blocks in dir */
  101         int     dh_firstfree[DH_NFSTATS + 1]; /* first blk with N words free */
  102 
  103         doff_t  dh_seqoff;      /* sequential access optimisation offset */
  104 
  105         int     dh_score;       /* access count for this dirhash */
  106 
  107         int     dh_onlist;      /* true if on the ufsdirhash_list chain */
  108 
  109         time_t  dh_lastused;    /* time the dirhash was last read or written*/
  110 
  111         /* Protected by ufsdirhash_mtx. */
  112         TAILQ_ENTRY(dirhash) dh_list;   /* chain of all dirhashes */
  113 };
  114 
  115 /*
  116  * Dirhash functions.
  117  */
  118 void    ufsdirhash_init(void);
  119 void    ufsdirhash_uninit(void);
  120 int     ufsdirhash_build(struct inode *);
  121 doff_t  ufsdirhash_findfree(struct inode *, int, int *);
  122 doff_t  ufsdirhash_enduseful(struct inode *);
  123 int     ufsdirhash_lookup(struct inode *, char *, int, doff_t *, struct buf **,
  124             doff_t *);
  125 void    ufsdirhash_newblk(struct inode *, doff_t);
  126 void    ufsdirhash_add(struct inode *, struct direct *, doff_t);
  127 void    ufsdirhash_remove(struct inode *, struct direct *, doff_t);
  128 void    ufsdirhash_move(struct inode *, struct direct *, doff_t, doff_t);
  129 void    ufsdirhash_dirtrunc(struct inode *, doff_t);
  130 void    ufsdirhash_free(struct inode *);
  131 
  132 void    ufsdirhash_checkblock(struct inode *, char *, doff_t);
  133 
  134 #endif /* !_UFS_UFS_DIRHASH_H_ */

Cache object: b3c7a09825ed4979577f9423f9c757e7


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