dm-cache-policy.h 5.2 KB

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