dm-exception-store.h 5.7 KB

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