1*0a6a1f1dSLionel Sambuc /* $NetBSD: ulfs_dirhash.h,v 1.8 2015/09/21 01:24:23 dholland Exp $ */ 284d9c625SLionel Sambuc /* from NetBSD: dirhash.h,v 1.6 2008/06/04 11:33:19 ad Exp */ 384d9c625SLionel Sambuc 484d9c625SLionel Sambuc /* 584d9c625SLionel Sambuc * Copyright (c) 2001 Ian Dowse. All rights reserved. 684d9c625SLionel Sambuc * 784d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without 884d9c625SLionel Sambuc * modification, are permitted provided that the following conditions 984d9c625SLionel Sambuc * are met: 1084d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 1184d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer. 1284d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 1384d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 1484d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution. 1584d9c625SLionel Sambuc * 1684d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1784d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1884d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1984d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2084d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2184d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2284d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2384d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2484d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2584d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2684d9c625SLionel Sambuc * SUCH DAMAGE. 2784d9c625SLionel Sambuc * 2884d9c625SLionel Sambuc * $FreeBSD: src/sys/ufs/ufs/dirhash.h,v 1.2.2.2 2004/12/08 11:54:13 dwmalone Exp $ 2984d9c625SLionel Sambuc */ 3084d9c625SLionel Sambuc 3184d9c625SLionel Sambuc #ifndef _UFS_LFS_ULFS_DIRHASH_H_ 3284d9c625SLionel Sambuc #define _UFS_LFS_ULFS_DIRHASH_H_ 3384d9c625SLionel Sambuc 3484d9c625SLionel Sambuc /* 3584d9c625SLionel Sambuc * For fast operations on large directories, we maintain a hash 3684d9c625SLionel Sambuc * that maps the file name to the offset of the directory entry within 3784d9c625SLionel Sambuc * the directory file. 3884d9c625SLionel Sambuc * 3984d9c625SLionel Sambuc * The hashing uses a dumb spillover to the next free slot on 4084d9c625SLionel Sambuc * collisions, so we must keep the utilisation low to avoid 4184d9c625SLionel Sambuc * long linear searches. Deleted entries that are not the last 4284d9c625SLionel Sambuc * in a chain must be marked DIRHASH_DEL. 4384d9c625SLionel Sambuc * 4484d9c625SLionel Sambuc * We also maintain information about free space in each block 4584d9c625SLionel Sambuc * to speed up creations. 4684d9c625SLionel Sambuc */ 4784d9c625SLionel Sambuc #define DIRHASH_EMPTY (-1) /* entry unused */ 4884d9c625SLionel Sambuc #define DIRHASH_DEL (-2) /* deleted entry; may be part of chain */ 4984d9c625SLionel Sambuc 5084d9c625SLionel Sambuc #define DIRALIGN 4 51*0a6a1f1dSLionel Sambuc #define DH_NFSTATS (LFS_MAXDIRENTRYSIZE / DIRALIGN) 5284d9c625SLionel Sambuc /* max DIRALIGN words in a directory entry */ 5384d9c625SLionel Sambuc 5484d9c625SLionel Sambuc /* 5584d9c625SLionel Sambuc * Dirhash uses a score mechanism to achieve a hybrid between a 5684d9c625SLionel Sambuc * least-recently-used and a least-often-used algorithm for entry 5784d9c625SLionel Sambuc * recycling. The score is incremented when a directory is used, and 5884d9c625SLionel Sambuc * decremented when the directory is a candidate for recycling. When 5984d9c625SLionel Sambuc * the score reaches zero, the hash is recycled. Hashes are linked 6084d9c625SLionel Sambuc * together on a TAILQ list, and hashes with higher scores filter 6184d9c625SLionel Sambuc * towards the tail (most recently used) end of the list. 6284d9c625SLionel Sambuc * 6384d9c625SLionel Sambuc * New hash entries are given an inital score of DH_SCOREINIT and are 6484d9c625SLionel Sambuc * placed at the most-recently-used end of the list. This helps a lot 6584d9c625SLionel Sambuc * in the worst-case case scenario where every directory access is 6684d9c625SLionel Sambuc * to a directory that is not hashed (i.e. the working set of hash 6784d9c625SLionel Sambuc * candidates is much larger than the configured memry limit). In this 6884d9c625SLionel Sambuc * case it limits the number of hash builds to 1/DH_SCOREINIT of the 6984d9c625SLionel Sambuc * number of accesses. 7084d9c625SLionel Sambuc */ 7184d9c625SLionel Sambuc #define DH_SCOREINIT 8 /* initial dh_score when dirhash built */ 7284d9c625SLionel Sambuc #define DH_SCOREMAX 64 /* max dh_score value */ 7384d9c625SLionel Sambuc 7484d9c625SLionel Sambuc /* 7584d9c625SLionel Sambuc * The main hash table has 2 levels. It is an array of pointers to 7684d9c625SLionel Sambuc * blocks of DH_NBLKOFF offsets. 7784d9c625SLionel Sambuc */ 7884d9c625SLionel Sambuc #define DH_BLKOFFSHIFT 8 7984d9c625SLionel Sambuc #define DH_NBLKOFF (1 << DH_BLKOFFSHIFT) 8084d9c625SLionel Sambuc #define DH_BLKOFFMASK (DH_NBLKOFF - 1) 8184d9c625SLionel Sambuc 8284d9c625SLionel Sambuc #define DH_ENTRY(dh, slot) \ 8384d9c625SLionel Sambuc ((dh)->dh_hash[(slot) >> DH_BLKOFFSHIFT][(slot) & DH_BLKOFFMASK]) 8484d9c625SLionel Sambuc 8584d9c625SLionel Sambuc struct dirhash { 8684d9c625SLionel Sambuc kmutex_t dh_lock; /* protects all fields except dh_list */ 8784d9c625SLionel Sambuc 8884d9c625SLionel Sambuc doff_t **dh_hash; /* the hash array (2-level) */ 8984d9c625SLionel Sambuc size_t dh_hashsz; 9084d9c625SLionel Sambuc int dh_narrays; /* number of entries in dh_hash */ 9184d9c625SLionel Sambuc int dh_hlen; /* total slots in the 2-level hash array */ 9284d9c625SLionel Sambuc int dh_hused; /* entries in use */ 9384d9c625SLionel Sambuc 9484d9c625SLionel Sambuc u_int8_t *dh_blkfree; /* free DIRALIGN words in each dir block */ 9584d9c625SLionel Sambuc size_t dh_blkfreesz; 9684d9c625SLionel Sambuc int dh_nblk; /* size of dh_blkfree array */ 9784d9c625SLionel Sambuc int dh_dirblks; /* number of DIRBLKSIZ blocks in dir */ 9884d9c625SLionel Sambuc int dh_firstfree[DH_NFSTATS + 1]; /* first blk with N words free */ 9984d9c625SLionel Sambuc 10084d9c625SLionel Sambuc int dh_seqopt; /* sequential access optimisation enabled */ 10184d9c625SLionel Sambuc doff_t dh_seqoff; /* sequential access optimisation offset */ 10284d9c625SLionel Sambuc 10384d9c625SLionel Sambuc int dh_score; /* access count for this dirhash */ 10484d9c625SLionel Sambuc 10584d9c625SLionel Sambuc int dh_onlist; /* true if on the ulfsdirhash_list chain */ 10684d9c625SLionel Sambuc 10784d9c625SLionel Sambuc /* Protected by ulfsdirhash_lock. */ 10884d9c625SLionel Sambuc TAILQ_ENTRY(dirhash) dh_list; /* chain of all dirhashes */ 10984d9c625SLionel Sambuc }; 11084d9c625SLionel Sambuc 11184d9c625SLionel Sambuc 11284d9c625SLionel Sambuc /* 11384d9c625SLionel Sambuc * Dirhash functions. 11484d9c625SLionel Sambuc */ 11584d9c625SLionel Sambuc int ulfsdirhash_build(struct inode *); 11684d9c625SLionel Sambuc doff_t ulfsdirhash_findfree(struct inode *, int, int *); 11784d9c625SLionel Sambuc doff_t ulfsdirhash_enduseful(struct inode *); 11884d9c625SLionel Sambuc int ulfsdirhash_lookup(struct inode *, const char *, int, doff_t *, 11984d9c625SLionel Sambuc struct buf **, doff_t *); 12084d9c625SLionel Sambuc void ulfsdirhash_newblk(struct inode *, doff_t); 121*0a6a1f1dSLionel Sambuc void ulfsdirhash_add(struct inode *, LFS_DIRHEADER *, doff_t); 122*0a6a1f1dSLionel Sambuc void ulfsdirhash_remove(struct inode *, LFS_DIRHEADER *, doff_t); 123*0a6a1f1dSLionel Sambuc void ulfsdirhash_move(struct inode *, LFS_DIRHEADER *, doff_t, doff_t); 12484d9c625SLionel Sambuc void ulfsdirhash_dirtrunc(struct inode *, doff_t); 12584d9c625SLionel Sambuc void ulfsdirhash_free(struct inode *); 12684d9c625SLionel Sambuc void ulfsdirhash_checkblock(struct inode *, char *, doff_t); 12784d9c625SLionel Sambuc void ulfsdirhash_init(void); 12884d9c625SLionel Sambuc void ulfsdirhash_done(void); 12984d9c625SLionel Sambuc 13084d9c625SLionel Sambuc #endif /* !_UFS_LFS_ULFS_DIRHASH_H_ */ 131