dm-thin-metadata.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #define THIN_METADATA_BLOCK_SIZE 4096
  11. /*
  12. * The metadata device is currently limited in size.
  13. *
  14. * We have one block of index, which can hold 255 index entries. Each
  15. * index entry contains allocation info about 16k metadata blocks.
  16. */
  17. #define THIN_METADATA_MAX_SECTORS (255 * (1 << 14) * (THIN_METADATA_BLOCK_SIZE / (1 << SECTOR_SHIFT)))
  18. /*
  19. * A metadata device larger than 16GB triggers a warning.
  20. */
  21. #define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
  22. /*----------------------------------------------------------------*/
  23. struct dm_pool_metadata;
  24. struct dm_thin_device;
  25. /*
  26. * Device identifier
  27. */
  28. typedef uint64_t dm_thin_id;
  29. /*
  30. * Reopens or creates a new, empty metadata volume.
  31. */
  32. struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
  33. sector_t data_block_size,
  34. bool format_device);
  35. int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
  36. /*
  37. * Compat feature flags. Any incompat flags beyond the ones
  38. * specified below will prevent use of the thin metadata.
  39. */
  40. #define THIN_FEATURE_COMPAT_SUPP 0UL
  41. #define THIN_FEATURE_COMPAT_RO_SUPP 0UL
  42. #define THIN_FEATURE_INCOMPAT_SUPP 0UL
  43. /*
  44. * Device creation/deletion.
  45. */
  46. int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev);
  47. /*
  48. * An internal snapshot.
  49. *
  50. * You can only snapshot a quiesced origin i.e. one that is either
  51. * suspended or not instanced at all.
  52. */
  53. int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev,
  54. dm_thin_id origin);
  55. /*
  56. * Deletes a virtual device from the metadata. It _is_ safe to call this
  57. * when that device is open. Operations on that device will just start
  58. * failing. You still need to call close() on the device.
  59. */
  60. int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
  61. dm_thin_id dev);
  62. /*
  63. * Commits _all_ metadata changes: device creation, deletion, mapping
  64. * updates.
  65. */
  66. int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
  67. /*
  68. * Discards all uncommitted changes. Rereads the superblock, rolling back
  69. * to the last good transaction. Thin devices remain open.
  70. * dm_thin_aborted_changes() tells you if they had uncommitted changes.
  71. *
  72. * If this call fails it's only useful to call dm_pool_metadata_close().
  73. * All other methods will fail with -EINVAL.
  74. */
  75. int dm_pool_abort_metadata(struct dm_pool_metadata *pmd);
  76. /*
  77. * Set/get userspace transaction id.
  78. */
  79. int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
  80. uint64_t current_id,
  81. uint64_t new_id);
  82. int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  83. uint64_t *result);
  84. /*
  85. * Hold/get root for userspace transaction.
  86. *
  87. * The metadata snapshot is a copy of the current superblock (minus the
  88. * space maps). Userland can access the data structures for READ
  89. * operations only. A small performance hit is incurred by providing this
  90. * copy of the metadata to userland due to extra copy-on-write operations
  91. * on the metadata nodes. Release this as soon as you finish with it.
  92. */
  93. int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
  94. int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
  95. int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
  96. dm_block_t *result);
  97. /*
  98. * Actions on a single virtual device.
  99. */
  100. /*
  101. * Opening the same device more than once will fail with -EBUSY.
  102. */
  103. int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
  104. struct dm_thin_device **td);
  105. int dm_pool_close_thin_device(struct dm_thin_device *td);
  106. dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
  107. struct dm_thin_lookup_result {
  108. dm_block_t block;
  109. unsigned shared:1;
  110. };
  111. /*
  112. * Returns:
  113. * -EWOULDBLOCK iff @can_block is set and would block.
  114. * -ENODATA iff that mapping is not present.
  115. * 0 success
  116. */
  117. int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
  118. int can_block, struct dm_thin_lookup_result *result);
  119. /*
  120. * Obtain an unused block.
  121. */
  122. int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
  123. /*
  124. * Insert or remove block.
  125. */
  126. int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
  127. dm_block_t data_block);
  128. int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block);
  129. /*
  130. * Queries.
  131. */
  132. bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
  133. bool dm_thin_aborted_changes(struct dm_thin_device *td);
  134. int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
  135. dm_block_t *highest_mapped);
  136. int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result);
  137. int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd,
  138. dm_block_t *result);
  139. int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
  140. dm_block_t *result);
  141. int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
  142. dm_block_t *result);
  143. int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result);
  144. int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result);
  145. /*
  146. * Returns -ENOSPC if the new size is too small and already allocated
  147. * blocks would be lost.
  148. */
  149. int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
  150. int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
  151. /*
  152. * Flicks the underlying block manager into read only mode, so you know
  153. * that nothing is changing.
  154. */
  155. void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd);
  156. int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
  157. dm_block_t threshold,
  158. dm_sm_threshold_fn fn,
  159. void *context);
  160. /*----------------------------------------------------------------*/
  161. #endif