mbcache.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_MBCACHE_H
  3. #define _LINUX_MBCACHE_H
  4. #include <linux/hash.h>
  5. #include <linux/list_bl.h>
  6. #include <linux/list.h>
  7. #include <linux/atomic.h>
  8. #include <linux/fs.h>
  9. struct mb_cache;
  10. /* Cache entry flags */
  11. enum {
  12. MBE_REFERENCED_B = 0,
  13. MBE_REUSABLE_B
  14. };
  15. struct mb_cache_entry {
  16. /* List of entries in cache - protected by cache->c_list_lock */
  17. struct list_head e_list;
  18. /*
  19. * Hash table list - protected by hash chain bitlock. The entry is
  20. * guaranteed to be hashed while e_refcnt > 0.
  21. */
  22. struct hlist_bl_node e_hash_list;
  23. /*
  24. * Entry refcount. Once it reaches zero, entry is unhashed and freed.
  25. * While refcount > 0, the entry is guaranteed to stay in the hash and
  26. * e.g. mb_cache_entry_try_delete() will fail.
  27. */
  28. atomic_t e_refcnt;
  29. /* Key in hash - stable during lifetime of the entry */
  30. u32 e_key;
  31. unsigned long e_flags;
  32. /* User provided value - stable during lifetime of the entry */
  33. u64 e_value;
  34. };
  35. struct mb_cache *mb_cache_create(int bucket_bits);
  36. void mb_cache_destroy(struct mb_cache *cache);
  37. int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
  38. u64 value, bool reusable);
  39. void __mb_cache_entry_free(struct mb_cache *cache,
  40. struct mb_cache_entry *entry);
  41. void mb_cache_entry_wait_unused(struct mb_cache_entry *entry);
  42. static inline void mb_cache_entry_put(struct mb_cache *cache,
  43. struct mb_cache_entry *entry)
  44. {
  45. unsigned int cnt = atomic_dec_return(&entry->e_refcnt);
  46. if (cnt > 0) {
  47. if (cnt <= 2)
  48. wake_up_var(&entry->e_refcnt);
  49. return;
  50. }
  51. __mb_cache_entry_free(cache, entry);
  52. }
  53. struct mb_cache_entry *mb_cache_entry_delete_or_get(struct mb_cache *cache,
  54. u32 key, u64 value);
  55. struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *cache, u32 key,
  56. u64 value);
  57. struct mb_cache_entry *mb_cache_entry_find_first(struct mb_cache *cache,
  58. u32 key);
  59. struct mb_cache_entry *mb_cache_entry_find_next(struct mb_cache *cache,
  60. struct mb_cache_entry *entry);
  61. void mb_cache_entry_touch(struct mb_cache *cache,
  62. struct mb_cache_entry *entry);
  63. #endif /* _LINUX_MBCACHE_H */