dm-thin-metadata.h 5.5 KB

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