dm-cache-policy.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_H
  8. #define DM_CACHE_POLICY_H
  9. #include "dm-cache-block-types.h"
  10. #include <linux/device-mapper.h>
  11. /*----------------------------------------------------------------*/
  12. /*
  13. * The cache policy makes the important decisions about which blocks get to
  14. * live on the faster cache device.
  15. */
  16. enum policy_operation {
  17. POLICY_PROMOTE,
  18. POLICY_DEMOTE,
  19. POLICY_WRITEBACK
  20. };
  21. /*
  22. * This is the instruction passed back to the core target.
  23. */
  24. struct policy_work {
  25. enum policy_operation op;
  26. dm_oblock_t oblock;
  27. dm_cblock_t cblock;
  28. };
  29. /*
  30. * The cache policy object. It is envisaged that this structure will be
  31. * embedded in a bigger, policy specific structure (ie. use container_of()).
  32. */
  33. struct dm_cache_policy {
  34. /*
  35. * Destroys this object.
  36. */
  37. void (*destroy)(struct dm_cache_policy *p);
  38. /*
  39. * Find the location of a block.
  40. *
  41. * Must not block.
  42. *
  43. * Returns 0 if in cache (cblock will be set), -ENOENT if not, < 0 for
  44. * other errors (-EWOULDBLOCK would be typical). data_dir should be
  45. * READ or WRITE. fast_copy should be set if migrating this block would
  46. * be 'cheap' somehow (eg, discarded data). background_queued will be set
  47. * if a migration has just been queued.
  48. */
  49. int (*lookup)(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock,
  50. int data_dir, bool fast_copy, bool *background_queued);
  51. /*
  52. * Sometimes the core target can optimise a migration, eg, the
  53. * block may be discarded, or the bio may cover an entire block.
  54. * In order to optimise it needs the migration immediately though
  55. * so it knows to do something different with the bio.
  56. *
  57. * This method is optional (policy-internal will fallback to using
  58. * lookup).
  59. */
  60. int (*lookup_with_work)(struct dm_cache_policy *p,
  61. dm_oblock_t oblock, dm_cblock_t *cblock,
  62. int data_dir, bool fast_copy,
  63. struct policy_work **work);
  64. /*
  65. * Retrieves background work. Returns -ENODATA when there's no
  66. * background work.
  67. */
  68. int (*get_background_work)(struct dm_cache_policy *p, bool idle,
  69. struct policy_work **result);
  70. /*
  71. * You must pass in the same work pointer that you were given, not
  72. * a copy.
  73. */
  74. void (*complete_background_work)(struct dm_cache_policy *p,
  75. struct policy_work *work,
  76. bool success);
  77. void (*set_dirty)(struct dm_cache_policy *p, dm_cblock_t cblock);
  78. void (*clear_dirty)(struct dm_cache_policy *p, dm_cblock_t cblock);
  79. /*
  80. * Called when a cache target is first created. Used to load a
  81. * mapping from the metadata device into the policy.
  82. */
  83. int (*load_mapping)(struct dm_cache_policy *p, dm_oblock_t oblock,
  84. dm_cblock_t cblock, bool dirty,
  85. uint32_t hint, bool hint_valid);
  86. /*
  87. * Drops the mapping, irrespective of whether it's clean or dirty.
  88. * Returns -ENODATA if cblock is not mapped.
  89. */
  90. int (*invalidate_mapping)(struct dm_cache_policy *p, dm_cblock_t cblock);
  91. /*
  92. * Gets the hint for a given cblock. Called in a single threaded
  93. * context. So no locking required.
  94. */
  95. uint32_t (*get_hint)(struct dm_cache_policy *p, dm_cblock_t cblock);
  96. /*
  97. * How full is the cache?
  98. */
  99. dm_cblock_t (*residency)(struct dm_cache_policy *p);
  100. /*
  101. * Because of where we sit in the block layer, we can be asked to
  102. * map a lot of little bios that are all in the same block (no
  103. * queue merging has occurred). To stop the policy being fooled by
  104. * these, the core target sends regular tick() calls to the policy.
  105. * The policy should only count an entry as hit once per tick.
  106. *
  107. * This method is optional.
  108. */
  109. void (*tick)(struct dm_cache_policy *p, bool can_block);
  110. /*
  111. * Configuration.
  112. */
  113. int (*emit_config_values)(struct dm_cache_policy *p, char *result,
  114. unsigned int maxlen, ssize_t *sz_ptr);
  115. int (*set_config_value)(struct dm_cache_policy *p,
  116. const char *key, const char *value);
  117. void (*allow_migrations)(struct dm_cache_policy *p, bool allow);
  118. /*
  119. * Book keeping ptr for the policy register, not for general use.
  120. */
  121. void *private;
  122. };
  123. /*----------------------------------------------------------------*/
  124. /*
  125. * We maintain a little register of the different policy types.
  126. */
  127. #define CACHE_POLICY_NAME_SIZE 16
  128. #define CACHE_POLICY_VERSION_SIZE 3
  129. struct dm_cache_policy_type {
  130. /* For use by the register code only. */
  131. struct list_head list;
  132. /*
  133. * Policy writers should fill in these fields. The name field is
  134. * what gets passed on the target line to select your policy.
  135. */
  136. char name[CACHE_POLICY_NAME_SIZE];
  137. unsigned int version[CACHE_POLICY_VERSION_SIZE];
  138. /*
  139. * For use by an alias dm_cache_policy_type to point to the
  140. * real dm_cache_policy_type.
  141. */
  142. struct dm_cache_policy_type *real;
  143. /*
  144. * Policies may store a hint for each cache block.
  145. * Currently the size of this hint must be 0 or 4 bytes but we
  146. * expect to relax this in future.
  147. */
  148. size_t hint_size;
  149. struct module *owner;
  150. struct dm_cache_policy *(*create)(dm_cblock_t cache_size,
  151. sector_t origin_size,
  152. sector_t block_size);
  153. };
  154. int dm_cache_policy_register(struct dm_cache_policy_type *type);
  155. void dm_cache_policy_unregister(struct dm_cache_policy_type *type);
  156. /*----------------------------------------------------------------*/
  157. #endif /* DM_CACHE_POLICY_H */