dm-cache-policy-internal.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 Red Hat. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #ifndef DM_CACHE_POLICY_INTERNAL_H
  8. #define DM_CACHE_POLICY_INTERNAL_H
  9. #include <linux/vmalloc.h>
  10. #include "dm-cache-policy.h"
  11. /*----------------------------------------------------------------*/
  12. static inline int policy_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock,
  13. int data_dir, bool fast_copy, bool *background_queued)
  14. {
  15. return p->lookup(p, oblock, cblock, data_dir, fast_copy, background_queued);
  16. }
  17. static inline int policy_lookup_with_work(struct dm_cache_policy *p,
  18. dm_oblock_t oblock, dm_cblock_t *cblock,
  19. int data_dir, bool fast_copy,
  20. struct policy_work **work)
  21. {
  22. if (!p->lookup_with_work) {
  23. *work = NULL;
  24. return p->lookup(p, oblock, cblock, data_dir, fast_copy, NULL);
  25. }
  26. return p->lookup_with_work(p, oblock, cblock, data_dir, fast_copy, work);
  27. }
  28. static inline int policy_get_background_work(struct dm_cache_policy *p,
  29. bool idle, struct policy_work **result)
  30. {
  31. return p->get_background_work(p, idle, result);
  32. }
  33. static inline void policy_complete_background_work(struct dm_cache_policy *p,
  34. struct policy_work *work,
  35. bool success)
  36. {
  37. return p->complete_background_work(p, work, success);
  38. }
  39. static inline void policy_set_dirty(struct dm_cache_policy *p, dm_cblock_t cblock)
  40. {
  41. p->set_dirty(p, cblock);
  42. }
  43. static inline void policy_clear_dirty(struct dm_cache_policy *p, dm_cblock_t cblock)
  44. {
  45. p->clear_dirty(p, cblock);
  46. }
  47. static inline int policy_load_mapping(struct dm_cache_policy *p,
  48. dm_oblock_t oblock, dm_cblock_t cblock,
  49. bool dirty, uint32_t hint, bool hint_valid)
  50. {
  51. return p->load_mapping(p, oblock, cblock, dirty, hint, hint_valid);
  52. }
  53. static inline int policy_invalidate_mapping(struct dm_cache_policy *p,
  54. dm_cblock_t cblock)
  55. {
  56. return p->invalidate_mapping(p, cblock);
  57. }
  58. static inline uint32_t policy_get_hint(struct dm_cache_policy *p,
  59. dm_cblock_t cblock)
  60. {
  61. return p->get_hint ? p->get_hint(p, cblock) : 0;
  62. }
  63. static inline dm_cblock_t policy_residency(struct dm_cache_policy *p)
  64. {
  65. return p->residency(p);
  66. }
  67. static inline void policy_tick(struct dm_cache_policy *p, bool can_block)
  68. {
  69. if (p->tick)
  70. return p->tick(p, can_block);
  71. }
  72. static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result,
  73. unsigned int maxlen, ssize_t *sz_ptr)
  74. {
  75. ssize_t sz = *sz_ptr;
  76. if (p->emit_config_values)
  77. return p->emit_config_values(p, result, maxlen, sz_ptr);
  78. DMEMIT("0 ");
  79. *sz_ptr = sz;
  80. return 0;
  81. }
  82. static inline int policy_set_config_value(struct dm_cache_policy *p,
  83. const char *key, const char *value)
  84. {
  85. return p->set_config_value ? p->set_config_value(p, key, value) : -EINVAL;
  86. }
  87. static inline void policy_allow_migrations(struct dm_cache_policy *p, bool allow)
  88. {
  89. return p->allow_migrations(p, allow);
  90. }
  91. /*----------------------------------------------------------------*/
  92. /*
  93. * Some utility functions commonly used by policies and the core target.
  94. */
  95. static inline size_t bitset_size_in_bytes(unsigned int nr_entries)
  96. {
  97. return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
  98. }
  99. static inline unsigned long *alloc_bitset(unsigned int nr_entries)
  100. {
  101. size_t s = bitset_size_in_bytes(nr_entries);
  102. return vzalloc(s);
  103. }
  104. static inline void clear_bitset(void *bitset, unsigned int nr_entries)
  105. {
  106. size_t s = bitset_size_in_bytes(nr_entries);
  107. memset(bitset, 0, s);
  108. }
  109. static inline void free_bitset(unsigned long *bits)
  110. {
  111. vfree(bits);
  112. }
  113. /*----------------------------------------------------------------*/
  114. /*
  115. * Creates a new cache policy given a policy name, a cache size, an origin size and the block size.
  116. */
  117. struct dm_cache_policy *dm_cache_policy_create(const char *name, dm_cblock_t cache_size,
  118. sector_t origin_size, sector_t block_size);
  119. /*
  120. * Destroys the policy. This drops references to the policy module as well
  121. * as calling it's destroy method. So always use this rather than calling
  122. * the policy->destroy method directly.
  123. */
  124. void dm_cache_policy_destroy(struct dm_cache_policy *p);
  125. /*
  126. * In case we've forgotten.
  127. */
  128. const char *dm_cache_policy_get_name(struct dm_cache_policy *p);
  129. const unsigned int *dm_cache_policy_get_version(struct dm_cache_policy *p);
  130. size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p);
  131. /*----------------------------------------------------------------*/
  132. #endif /* DM_CACHE_POLICY_INTERNAL_H */