dm-cache-policy-internal.h 4.4 KB

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