dm-exception-store.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  4. * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
  5. *
  6. * Device-mapper snapshot exception store.
  7. *
  8. * This file is released under the GPL.
  9. */
  10. #ifndef _LINUX_DM_EXCEPTION_STORE
  11. #define _LINUX_DM_EXCEPTION_STORE
  12. #include <linux/blkdev.h>
  13. #include <linux/list_bl.h>
  14. #include <linux/device-mapper.h>
  15. /*
  16. * The snapshot code deals with largish chunks of the disk at a
  17. * time. Typically 32k - 512k.
  18. */
  19. typedef sector_t chunk_t;
  20. /*
  21. * An exception is used where an old chunk of data has been
  22. * replaced by a new one.
  23. * If chunk_t is 64 bits in size, the top 8 bits of new_chunk hold the number
  24. * of chunks that follow contiguously. Remaining bits hold the number of the
  25. * chunk within the device.
  26. */
  27. struct dm_exception {
  28. struct hlist_node hash_list;
  29. chunk_t old_chunk;
  30. chunk_t new_chunk;
  31. };
  32. /*
  33. * Abstraction to handle the meta/layout of exception stores (the
  34. * COW device).
  35. */
  36. struct dm_exception_store;
  37. struct dm_exception_store_type {
  38. const char *name;
  39. struct module *module;
  40. int (*ctr)(struct dm_exception_store *store, char *options);
  41. /*
  42. * Destroys this object when you've finished with it.
  43. */
  44. void (*dtr)(struct dm_exception_store *store);
  45. /*
  46. * The target shouldn't read the COW device until this is
  47. * called. As exceptions are read from the COW, they are
  48. * reported back via the callback.
  49. */
  50. int (*read_metadata)(struct dm_exception_store *store,
  51. int (*callback)(void *callback_context,
  52. chunk_t old, chunk_t new),
  53. void *callback_context);
  54. /*
  55. * Find somewhere to store the next exception.
  56. */
  57. int (*prepare_exception)(struct dm_exception_store *store,
  58. struct dm_exception *e);
  59. /*
  60. * Update the metadata with this exception.
  61. */
  62. void (*commit_exception)(struct dm_exception_store *store,
  63. struct dm_exception *e, int valid,
  64. void (*callback)(void *, int success),
  65. void *callback_context);
  66. /*
  67. * Returns 0 if the exception store is empty.
  68. *
  69. * If there are exceptions still to be merged, sets
  70. * *last_old_chunk and *last_new_chunk to the most recent
  71. * still-to-be-merged chunk and returns the number of
  72. * consecutive previous ones.
  73. */
  74. int (*prepare_merge)(struct dm_exception_store *store,
  75. chunk_t *last_old_chunk, chunk_t *last_new_chunk);
  76. /*
  77. * Clear the last n exceptions.
  78. * nr_merged must be <= the value returned by prepare_merge.
  79. */
  80. int (*commit_merge)(struct dm_exception_store *store, int nr_merged);
  81. /*
  82. * The snapshot is invalid, note this in the metadata.
  83. */
  84. void (*drop_snapshot)(struct dm_exception_store *store);
  85. unsigned int (*status)(struct dm_exception_store *store,
  86. status_type_t status, char *result,
  87. unsigned int maxlen);
  88. /*
  89. * Return how full the snapshot is.
  90. */
  91. void (*usage)(struct dm_exception_store *store,
  92. sector_t *total_sectors, sector_t *sectors_allocated,
  93. sector_t *metadata_sectors);
  94. /* For internal device-mapper use only. */
  95. struct list_head list;
  96. };
  97. struct dm_snapshot;
  98. struct dm_exception_store {
  99. struct dm_exception_store_type *type;
  100. struct dm_snapshot *snap;
  101. /* Size of data blocks saved - must be a power of 2 */
  102. unsigned int chunk_size;
  103. unsigned int chunk_mask;
  104. unsigned int chunk_shift;
  105. void *context;
  106. bool userspace_supports_overflow;
  107. };
  108. /*
  109. * Obtain the origin or cow device used by a given snapshot.
  110. */
  111. struct dm_dev *dm_snap_origin(struct dm_snapshot *snap);
  112. struct dm_dev *dm_snap_cow(struct dm_snapshot *snap);
  113. /*
  114. * Funtions to manipulate consecutive chunks
  115. */
  116. #define DM_CHUNK_CONSECUTIVE_BITS 8
  117. #define DM_CHUNK_NUMBER_BITS 56
  118. static inline chunk_t dm_chunk_number(chunk_t chunk)
  119. {
  120. return chunk & (chunk_t)((1ULL << DM_CHUNK_NUMBER_BITS) - 1ULL);
  121. }
  122. static inline unsigned int dm_consecutive_chunk_count(struct dm_exception *e)
  123. {
  124. return e->new_chunk >> DM_CHUNK_NUMBER_BITS;
  125. }
  126. static inline void dm_consecutive_chunk_count_inc(struct dm_exception *e)
  127. {
  128. e->new_chunk += (1ULL << DM_CHUNK_NUMBER_BITS);
  129. BUG_ON(!dm_consecutive_chunk_count(e));
  130. }
  131. static inline void dm_consecutive_chunk_count_dec(struct dm_exception *e)
  132. {
  133. BUG_ON(!dm_consecutive_chunk_count(e));
  134. e->new_chunk -= (1ULL << DM_CHUNK_NUMBER_BITS);
  135. }
  136. /*
  137. * Return the number of sectors in the device.
  138. */
  139. static inline sector_t get_dev_size(struct block_device *bdev)
  140. {
  141. return bdev_nr_sectors(bdev);
  142. }
  143. static inline chunk_t sector_to_chunk(struct dm_exception_store *store,
  144. sector_t sector)
  145. {
  146. return sector >> store->chunk_shift;
  147. }
  148. int dm_exception_store_type_register(struct dm_exception_store_type *type);
  149. int dm_exception_store_type_unregister(struct dm_exception_store_type *type);
  150. int dm_exception_store_set_chunk_size(struct dm_exception_store *store,
  151. unsigned int chunk_size,
  152. char **error);
  153. int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
  154. struct dm_snapshot *snap,
  155. unsigned int *args_used,
  156. struct dm_exception_store **store);
  157. void dm_exception_store_destroy(struct dm_exception_store *store);
  158. int dm_exception_store_init(void);
  159. void dm_exception_store_exit(void);
  160. /*
  161. * Two exception store implementations.
  162. */
  163. int dm_persistent_snapshot_init(void);
  164. void dm_persistent_snapshot_exit(void);
  165. int dm_transient_snapshot_init(void);
  166. void dm_transient_snapshot_exit(void);
  167. #endif /* _LINUX_DM_EXCEPTION_STORE */