dm-thin-metadata.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2010-2011 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #ifndef DM_THIN_METADATA_H
  8. #define DM_THIN_METADATA_H
  9. #include "persistent-data/dm-block-manager.h"
  10. #include "persistent-data/dm-space-map.h"
  11. #include "persistent-data/dm-space-map-metadata.h"
  12. #define THIN_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE
  13. /*
  14. * The metadata device is currently limited in size.
  15. */
  16. #define THIN_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS
  17. /*
  18. * A metadata device larger than 16GB triggers a warning.
  19. */
  20. #define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
  21. /*----------------------------------------------------------------*/
  22. /*
  23. * Thin metadata superblock flags.
  24. */
  25. #define THIN_METADATA_NEEDS_CHECK_FLAG (1 << 0)
  26. struct dm_pool_metadata;
  27. struct dm_thin_device;
  28. /*
  29. * Device identifier
  30. */
  31. typedef uint64_t dm_thin_id;
  32. /*
  33. * Reopens or creates a new, empty metadata volume.
  34. */
  35. struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
  36. sector_t data_block_size,
  37. bool format_device);
  38. int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
  39. /*
  40. * Compat feature flags. Any incompat flags beyond the ones
  41. * specified below will prevent use of the thin metadata.
  42. */
  43. #define THIN_FEATURE_COMPAT_SUPP 0UL
  44. #define THIN_FEATURE_COMPAT_RO_SUPP 0UL
  45. #define THIN_FEATURE_INCOMPAT_SUPP 0UL
  46. /*
  47. * Device creation/deletion.
  48. */
  49. int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev);
  50. /*
  51. * An internal snapshot.
  52. *
  53. * You can only snapshot a quiesced origin i.e. one that is either
  54. * suspended or not instanced at all.
  55. */
  56. int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev,
  57. dm_thin_id origin);
  58. /*
  59. * Deletes a virtual device from the metadata. It _is_ safe to call this
  60. * when that device is open. Operations on that device will just start
  61. * failing. You still need to call close() on the device.
  62. */
  63. int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
  64. dm_thin_id dev);
  65. /*
  66. * Commits _all_ metadata changes: device creation, deletion, mapping
  67. * updates.
  68. */
  69. int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
  70. /*
  71. * Discards all uncommitted changes. Rereads the superblock, rolling back
  72. * to the last good transaction. Thin devices remain open.
  73. * dm_thin_aborted_changes() tells you if they had uncommitted changes.
  74. *
  75. * If this call fails it's only useful to call dm_pool_metadata_close().
  76. * All other methods will fail with -EINVAL.
  77. */
  78. int dm_pool_abort_metadata(struct dm_pool_metadata *pmd);
  79. /*
  80. * Set/get userspace transaction id.
  81. */
  82. int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
  83. uint64_t current_id,
  84. uint64_t new_id);
  85. int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  86. uint64_t *result);
  87. /*
  88. * Hold/get root for userspace transaction.
  89. *
  90. * The metadata snapshot is a copy of the current superblock (minus the
  91. * space maps). Userland can access the data structures for READ
  92. * operations only. A small performance hit is incurred by providing this
  93. * copy of the metadata to userland due to extra copy-on-write operations
  94. * on the metadata nodes. Release this as soon as you finish with it.
  95. */
  96. int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
  97. int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
  98. int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
  99. dm_block_t *result);
  100. /*
  101. * Actions on a single virtual device.
  102. */
  103. /*
  104. * Opening the same device more than once will fail with -EBUSY.
  105. */
  106. int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
  107. struct dm_thin_device **td);
  108. int dm_pool_close_thin_device(struct dm_thin_device *td);
  109. dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
  110. struct dm_thin_lookup_result {
  111. dm_block_t block;
  112. bool shared:1;
  113. };
  114. /*
  115. * Returns:
  116. * -EWOULDBLOCK iff @can_issue_io is set and would issue IO
  117. * -ENODATA iff that mapping is not present.
  118. * 0 success
  119. */
  120. int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
  121. int can_issue_io, struct dm_thin_lookup_result *result);
  122. /*
  123. * Retrieve the next run of contiguously mapped blocks. Useful for working
  124. * out where to break up IO. Returns 0 on success, < 0 on error.
  125. */
  126. int dm_thin_find_mapped_range(struct dm_thin_device *td,
  127. dm_block_t begin, dm_block_t end,
  128. dm_block_t *thin_begin, dm_block_t *thin_end,
  129. dm_block_t *pool_begin, bool *maybe_shared);
  130. /*
  131. * Obtain an unused block.
  132. */
  133. int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
  134. /*
  135. * Insert or remove block.
  136. */
  137. int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
  138. dm_block_t data_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