lru_cache.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mm.h>
  3. #include "lru_cache.h"
  4. #include "messages.h"
  5. /*
  6. * Initialize a cache object.
  7. *
  8. * @cache: The cache.
  9. * @max_size: Maximum size (number of entries) for the cache.
  10. * Use 0 for unlimited size, it's the user's responsibility to
  11. * trim the cache in that case.
  12. */
  13. void btrfs_lru_cache_init(struct btrfs_lru_cache *cache, unsigned int max_size)
  14. {
  15. INIT_LIST_HEAD(&cache->lru_list);
  16. mt_init(&cache->entries);
  17. cache->size = 0;
  18. cache->max_size = max_size;
  19. }
  20. static struct btrfs_lru_cache_entry *match_entry(struct list_head *head, u64 key,
  21. u64 gen)
  22. {
  23. struct btrfs_lru_cache_entry *entry;
  24. list_for_each_entry(entry, head, list) {
  25. if (entry->key == key && entry->gen == gen)
  26. return entry;
  27. }
  28. return NULL;
  29. }
  30. /*
  31. * Lookup for an entry in the cache.
  32. *
  33. * @cache: The cache.
  34. * @key: The key of the entry we are looking for.
  35. * @gen: Generation associated to the key.
  36. *
  37. * Returns the entry associated with the key or NULL if none found.
  38. */
  39. struct btrfs_lru_cache_entry *btrfs_lru_cache_lookup(struct btrfs_lru_cache *cache,
  40. u64 key, u64 gen)
  41. {
  42. struct list_head *head;
  43. struct btrfs_lru_cache_entry *entry;
  44. head = mtree_load(&cache->entries, key);
  45. if (!head)
  46. return NULL;
  47. entry = match_entry(head, key, gen);
  48. if (entry)
  49. list_move_tail(&entry->lru_list, &cache->lru_list);
  50. return entry;
  51. }
  52. /*
  53. * Remove an entry from the cache.
  54. *
  55. * @cache: The cache to remove from.
  56. * @entry: The entry to remove from the cache.
  57. *
  58. * Note: this also frees the memory used by the entry.
  59. */
  60. void btrfs_lru_cache_remove(struct btrfs_lru_cache *cache,
  61. struct btrfs_lru_cache_entry *entry)
  62. {
  63. struct list_head *prev = entry->list.prev;
  64. ASSERT(cache->size > 0);
  65. ASSERT(!mtree_empty(&cache->entries));
  66. list_del(&entry->list);
  67. list_del(&entry->lru_list);
  68. if (list_empty(prev)) {
  69. struct list_head *head;
  70. /*
  71. * If previous element in the list entry->list is now empty, it
  72. * means it's a head entry not pointing to any cached entries,
  73. * so remove it from the maple tree and free it.
  74. */
  75. head = mtree_erase(&cache->entries, entry->key);
  76. ASSERT(head == prev);
  77. kfree(head);
  78. }
  79. kfree(entry);
  80. cache->size--;
  81. }
  82. /*
  83. * Store an entry in the cache.
  84. *
  85. * @cache: The cache.
  86. * @entry: The entry to store.
  87. *
  88. * Returns 0 on success and < 0 on error.
  89. */
  90. int btrfs_lru_cache_store(struct btrfs_lru_cache *cache,
  91. struct btrfs_lru_cache_entry *new_entry,
  92. gfp_t gfp)
  93. {
  94. const u64 key = new_entry->key;
  95. struct list_head *head;
  96. int ret;
  97. head = kmalloc(sizeof(*head), gfp);
  98. if (!head)
  99. return -ENOMEM;
  100. ret = mtree_insert(&cache->entries, key, head, gfp);
  101. if (ret == 0) {
  102. INIT_LIST_HEAD(head);
  103. list_add_tail(&new_entry->list, head);
  104. } else if (ret == -EEXIST) {
  105. kfree(head);
  106. head = mtree_load(&cache->entries, key);
  107. ASSERT(head != NULL);
  108. if (match_entry(head, key, new_entry->gen) != NULL)
  109. return -EEXIST;
  110. list_add_tail(&new_entry->list, head);
  111. } else if (ret < 0) {
  112. kfree(head);
  113. return ret;
  114. }
  115. if (cache->max_size > 0 && cache->size == cache->max_size) {
  116. struct btrfs_lru_cache_entry *lru_entry;
  117. lru_entry = list_first_entry(&cache->lru_list,
  118. struct btrfs_lru_cache_entry,
  119. lru_list);
  120. btrfs_lru_cache_remove(cache, lru_entry);
  121. }
  122. list_add_tail(&new_entry->lru_list, &cache->lru_list);
  123. cache->size++;
  124. return 0;
  125. }
  126. /*
  127. * Empty a cache.
  128. *
  129. * @cache: The cache to empty.
  130. *
  131. * Removes all entries from the cache.
  132. */
  133. void btrfs_lru_cache_clear(struct btrfs_lru_cache *cache)
  134. {
  135. struct btrfs_lru_cache_entry *entry;
  136. struct btrfs_lru_cache_entry *tmp;
  137. list_for_each_entry_safe(entry, tmp, &cache->lru_list, lru_list)
  138. btrfs_lru_cache_remove(cache, entry);
  139. ASSERT(cache->size == 0);
  140. ASSERT(mtree_empty(&cache->entries));
  141. }