dirhash.h 5.0 KB

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