dm-thin-metadata.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2010-2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #ifndef DM_THIN_METADATA_H
  7. #define DM_THIN_METADATA_H
  8. #include "persistent-data/dm-block-manager.h"
  9. #include "persistent-data/dm-space-map.h"
  10. #include "persistent-data/dm-space-map-metadata.h"
  11. #define THIN_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE
  12. /*
  13. * The metadata device is currently limited in size.
  14. */
  15. #define THIN_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS
  16. /*
  17. * A metadata device larger than 16GB triggers a warning.
  18. */
  19. #define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
  20. /*----------------------------------------------------------------*/
  21. /*
  22. * Thin metadata superblock flags.
  23. */
  24. #define THIN_METADATA_NEEDS_CHECK_FLAG (1 << 0)
  25. struct dm_pool_metadata;
  26. struct dm_thin_device;
  27. /*
  28. * Device identifier
  29. */
  30. typedef uint64_t dm_thin_id;
  31. /*
  32. * Reopens or creates a new, empty metadata volume.
  33. */
  34. struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
  35. sector_t data_block_size,
  36. bool format_device);
  37. int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
  38. /*
  39. * Compat feature flags. Any incompat flags beyond the ones
  40. * specified below will prevent use of the thin metadata.
  41. */
  42. #define THIN_FEATURE_COMPAT_SUPP 0UL
  43. #define THIN_FEATURE_COMPAT_RO_SUPP 0UL
  44. #define THIN_FEATURE_INCOMPAT_SUPP 0UL
  45. /*
  46. * Device creation/deletion.
  47. */
  48. int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev);
  49. /*
  50. * An internal snapshot.
  51. *
  52. * You can only snapshot a quiesced origin i.e. one that is either
  53. * suspended or not instanced at all.
  54. */
  55. int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev,
  56. dm_thin_id origin);
  57. /*
  58. * Deletes a virtual device from the metadata. It _is_ safe to call this
  59. * when that device is open. Operations on that device will just start
  60. * failing. You still need to call close() on the device.
  61. */
  62. int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
  63. dm_thin_id dev);
  64. /*
  65. * Commits _all_ metadata changes: device creation, deletion, mapping
  66. * updates.
  67. */
  68. int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
  69. /*
  70. * Discards all uncommitted changes. Rereads the superblock, rolling back
  71. * to the last good transaction. Thin devices remain open.
  72. * dm_thin_aborted_changes() tells you if they had uncommitted changes.
  73. *
  74. * If this call fails it's only useful to call dm_pool_metadata_close().
  75. * All other methods will fail with -EINVAL.
  76. */
  77. int dm_pool_abort_metadata(struct dm_pool_metadata *pmd);
  78. /*
  79. * Set/get userspace transaction id.
  80. */
  81. int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
  82. uint64_t current_id,
  83. uint64_t new_id);
  84. int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  85. uint64_t *result);
  86. /*
  87. * Hold/get root for userspace transaction.
  88. *
  89. * The metadata snapshot is a copy of the current superblock (minus the
  90. * space maps). Userland can access the data structures for READ
  91. * operations only. A small performance hit is incurred by providing this
  92. * copy of the metadata to userland due to extra copy-on-write operations
  93. * on the metadata nodes. Release this as soon as you finish with it.
  94. */
  95. int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
  96. int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
  97. int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
  98. dm_block_t *result);
  99. /*
  100. * Actions on a single virtual device.
  101. */
  102. /*
  103. * Opening the same device more than once will fail with -EBUSY.
  104. */
  105. int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
  106. struct dm_thin_device **td);
  107. int dm_pool_close_thin_device(struct dm_thin_device *td);
  108. dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
  109. struct dm_thin_lookup_result {
  110. dm_block_t block;
  111. bool shared:1;
  112. };
  113. /*
  114. * Returns:
  115. * -EWOULDBLOCK iff @can_issue_io is set and would issue IO
  116. * -ENODATA iff that mapping is not present.
  117. * 0 success
  118. */
  119. int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
  120. int can_issue_io, struct dm_thin_lookup_result *result);
  121. /*
  122. * Retrieve the next run of contiguously mapped blocks. Useful for working
  123. * out where to break up IO. Returns 0 on success, < 0 on error.
  124. */
  125. int dm_thin_find_mapped_range(struct dm_thin_device *td,
  126. dm_block_t begin, dm_block_t end,
  127. dm_block_t *thin_begin, dm_block_t *thin_end,
  128. dm_block_t *pool_begin, bool *maybe_shared);
  129. /*
  130. * Obtain an unused block.
  131. */
  132. int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
  133. /*
  134. * Insert or remove block.
  135. */
  136. int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
  137. dm_block_t data_block);
  138. int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block);
  139. int dm_thin_remove_range(struct dm_thin_device *td,
  140. dm_block_t begin, dm_block_t end);
  141. /*
  142. * Queries.
  143. */
  144. bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
  145. bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd);
  146. bool dm_thin_aborted_changes(struct dm_thin_device *td);
  147. int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
  148. dm_block_t *highest_mapped);
  149. int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result);
  150. int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd,
  151. dm_block_t *result);
  152. int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
  153. dm_block_t *result);
  154. int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
  155. dm_block_t *result);
  156. int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result);
  157. int dm_pool_block_is_shared(struct dm_pool_metadata *pmd, dm_block_t b, bool *result);
  158. int dm_pool_inc_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e);
  159. int dm_pool_dec_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e);
  160. /*
  161. * Returns -ENOSPC if the new size is too small and already allocated
  162. * blocks would be lost.
  163. */
  164. int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
  165. int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
  166. /*
  167. * Flicks the underlying block manager into read only mode, so you know
  168. * that nothing is changing.
  169. */
  170. void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd);
  171. void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd);
  172. int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
  173. dm_block_t threshold,
  174. dm_sm_threshold_fn fn,
  175. void *context);
  176. /*
  177. * Updates the superblock immediately.
  178. */
  179. int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd);
  180. bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd);
  181. /*
  182. * Issue any prefetches that may be useful.
  183. */
  184. void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd);
  185. /* Pre-commit callback */
  186. typedef int (*dm_pool_pre_commit_fn)(void *context);
  187. void dm_pool_register_pre_commit_callback(struct dm_pool_metadata *pmd,
  188. dm_pool_pre_commit_fn fn,
  189. void *context);
  190. /*----------------------------------------------------------------*/
  191. #endif