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. /*
  12. * Little inline functions that simplify calling the policy methods.
  13. */
  14. static inline int policy_map(struct dm_cache_policy *p, dm_oblock_t oblock,
  15. bool can_block, bool can_migrate, bool discarded_oblock,
  16. struct bio *bio, struct policy_locker *locker,
  17. struct policy_result *result)
  18. {
  19. return p->map(p, oblock, can_block, can_migrate, discarded_oblock, bio, locker, result);
  20. }
  21. static inline int policy_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock)
  22. {
  23. BUG_ON(!p->lookup);
  24. return p->lookup(p, oblock, cblock);
  25. }
  26. static inline void policy_set_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
  27. {
  28. if (p->set_dirty)
  29. p->set_dirty(p, oblock);
  30. }
  31. static inline void policy_clear_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
  32. {
  33. if (p->clear_dirty)
  34. p->clear_dirty(p, oblock);
  35. }
  36. static inline int policy_load_mapping(struct dm_cache_policy *p,
  37. dm_oblock_t oblock, dm_cblock_t cblock,
  38. uint32_t hint, bool hint_valid)
  39. {
  40. return p->load_mapping(p, oblock, cblock, hint, hint_valid);
  41. }
  42. static inline uint32_t policy_get_hint(struct dm_cache_policy *p,
  43. dm_cblock_t cblock)
  44. {
  45. return p->get_hint ? p->get_hint(p, cblock) : 0;
  46. }
  47. static inline int policy_writeback_work(struct dm_cache_policy *p,
  48. dm_oblock_t *oblock,
  49. dm_cblock_t *cblock,
  50. bool critical_only)
  51. {
  52. return p->writeback_work ? p->writeback_work(p, oblock, cblock, critical_only) : -ENOENT;
  53. }
  54. static inline void policy_remove_mapping(struct dm_cache_policy *p, dm_oblock_t oblock)
  55. {
  56. p->remove_mapping(p, oblock);
  57. }
  58. static inline int policy_remove_cblock(struct dm_cache_policy *p, dm_cblock_t cblock)
  59. {
  60. return p->remove_cblock(p, cblock);
  61. }
  62. static inline void policy_force_mapping(struct dm_cache_policy *p,
  63. dm_oblock_t current_oblock, dm_oblock_t new_oblock)
  64. {
  65. return p->force_mapping(p, current_oblock, new_oblock);
  66. }
  67. static inline dm_cblock_t policy_residency(struct dm_cache_policy *p)
  68. {
  69. return p->residency(p);
  70. }
  71. static inline void policy_tick(struct dm_cache_policy *p, bool can_block)
  72. {
  73. if (p->tick)
  74. return p->tick(p, can_block);
  75. }
  76. static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result,
  77. unsigned maxlen, ssize_t *sz_ptr)
  78. {
  79. ssize_t sz = *sz_ptr;
  80. if (p->emit_config_values)
  81. return p->emit_config_values(p, result, maxlen, sz_ptr);
  82. DMEMIT("0 ");
  83. *sz_ptr = sz;
  84. return 0;
  85. }
  86. static inline int policy_set_config_value(struct dm_cache_policy *p,
  87. const char *key, const char *value)
  88. {
  89. return p->set_config_value ? p->set_config_value(p, key, value) : -EINVAL;
  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 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 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 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 *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 */