dm-snap.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * dm-snapshot.c
  3. *
  4. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #ifndef DM_SNAPSHOT_H
  9. #define DM_SNAPSHOT_H
  10. #include "dm.h"
  11. #include "dm-bio-list.h"
  12. #include <linux/blkdev.h>
  13. #include <linux/workqueue.h>
  14. struct exception_table {
  15. uint32_t hash_mask;
  16. unsigned hash_shift;
  17. struct list_head *table;
  18. };
  19. /*
  20. * The snapshot code deals with largish chunks of the disk at a
  21. * time. Typically 32k - 512k.
  22. */
  23. typedef sector_t chunk_t;
  24. /*
  25. * An exception is used where an old chunk of data has been
  26. * replaced by a new one.
  27. * If chunk_t is 64 bits in size, the top 8 bits of new_chunk hold the number
  28. * of chunks that follow contiguously. Remaining bits hold the number of the
  29. * chunk within the device.
  30. */
  31. struct dm_snap_exception {
  32. struct list_head hash_list;
  33. chunk_t old_chunk;
  34. chunk_t new_chunk;
  35. };
  36. /*
  37. * Funtions to manipulate consecutive chunks
  38. */
  39. # if defined(CONFIG_LBD) || (BITS_PER_LONG == 64)
  40. # define DM_CHUNK_CONSECUTIVE_BITS 8
  41. # define DM_CHUNK_NUMBER_BITS 56
  42. static inline chunk_t dm_chunk_number(chunk_t chunk)
  43. {
  44. return chunk & (chunk_t)((1ULL << DM_CHUNK_NUMBER_BITS) - 1ULL);
  45. }
  46. static inline unsigned dm_consecutive_chunk_count(struct dm_snap_exception *e)
  47. {
  48. return e->new_chunk >> DM_CHUNK_NUMBER_BITS;
  49. }
  50. static inline void dm_consecutive_chunk_count_inc(struct dm_snap_exception *e)
  51. {
  52. e->new_chunk += (1ULL << DM_CHUNK_NUMBER_BITS);
  53. BUG_ON(!dm_consecutive_chunk_count(e));
  54. }
  55. # else
  56. # define DM_CHUNK_CONSECUTIVE_BITS 0
  57. static inline chunk_t dm_chunk_number(chunk_t chunk)
  58. {
  59. return chunk;
  60. }
  61. static inline unsigned dm_consecutive_chunk_count(struct dm_snap_exception *e)
  62. {
  63. return 0;
  64. }
  65. static inline void dm_consecutive_chunk_count_inc(struct dm_snap_exception *e)
  66. {
  67. }
  68. # endif
  69. /*
  70. * Abstraction to handle the meta/layout of exception stores (the
  71. * COW device).
  72. */
  73. struct exception_store {
  74. /*
  75. * Destroys this object when you've finished with it.
  76. */
  77. void (*destroy) (struct exception_store *store);
  78. /*
  79. * The target shouldn't read the COW device until this is
  80. * called.
  81. */
  82. int (*read_metadata) (struct exception_store *store);
  83. /*
  84. * Find somewhere to store the next exception.
  85. */
  86. int (*prepare_exception) (struct exception_store *store,
  87. struct dm_snap_exception *e);
  88. /*
  89. * Update the metadata with this exception.
  90. */
  91. void (*commit_exception) (struct exception_store *store,
  92. struct dm_snap_exception *e,
  93. void (*callback) (void *, int success),
  94. void *callback_context);
  95. /*
  96. * The snapshot is invalid, note this in the metadata.
  97. */
  98. void (*drop_snapshot) (struct exception_store *store);
  99. /*
  100. * Return how full the snapshot is.
  101. */
  102. void (*fraction_full) (struct exception_store *store,
  103. sector_t *numerator,
  104. sector_t *denominator);
  105. struct dm_snapshot *snap;
  106. void *context;
  107. };
  108. #define DM_TRACKED_CHUNK_HASH_SIZE 16
  109. #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
  110. (DM_TRACKED_CHUNK_HASH_SIZE - 1))
  111. struct dm_snapshot {
  112. struct rw_semaphore lock;
  113. struct dm_target *ti;
  114. struct dm_dev *origin;
  115. struct dm_dev *cow;
  116. /* List of snapshots per Origin */
  117. struct list_head list;
  118. /* Size of data blocks saved - must be a power of 2 */
  119. chunk_t chunk_size;
  120. chunk_t chunk_mask;
  121. chunk_t chunk_shift;
  122. /* You can't use a snapshot if this is 0 (e.g. if full) */
  123. int valid;
  124. /* Origin writes don't trigger exceptions until this is set */
  125. int active;
  126. /* Used for display of table */
  127. char type;
  128. /* The last percentage we notified */
  129. int last_percent;
  130. mempool_t *pending_pool;
  131. struct exception_table pending;
  132. struct exception_table complete;
  133. /*
  134. * pe_lock protects all pending_exception operations and access
  135. * as well as the snapshot_bios list.
  136. */
  137. spinlock_t pe_lock;
  138. /* The on disk metadata handler */
  139. struct exception_store store;
  140. struct dm_kcopyd_client *kcopyd_client;
  141. /* Queue of snapshot writes for ksnapd to flush */
  142. struct bio_list queued_bios;
  143. struct work_struct queued_bios_work;
  144. /* Chunks with outstanding reads */
  145. mempool_t *tracked_chunk_pool;
  146. spinlock_t tracked_chunk_lock;
  147. struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
  148. };
  149. /*
  150. * Used by the exception stores to load exceptions hen
  151. * initialising.
  152. */
  153. int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new);
  154. /*
  155. * Constructor and destructor for the default persistent
  156. * store.
  157. */
  158. int dm_create_persistent(struct exception_store *store);
  159. int dm_create_transient(struct exception_store *store);
  160. /*
  161. * Return the number of sectors in the device.
  162. */
  163. static inline sector_t get_dev_size(struct block_device *bdev)
  164. {
  165. return bdev->bd_inode->i_size >> SECTOR_SHIFT;
  166. }
  167. static inline chunk_t sector_to_chunk(struct dm_snapshot *s, sector_t sector)
  168. {
  169. return (sector & ~s->chunk_mask) >> s->chunk_shift;
  170. }
  171. static inline sector_t chunk_to_sector(struct dm_snapshot *s, chunk_t chunk)
  172. {
  173. return chunk << s->chunk_shift;
  174. }
  175. static inline int bdev_equal(struct block_device *lhs, struct block_device *rhs)
  176. {
  177. /*
  178. * There is only ever one instance of a particular block
  179. * device so we can compare pointers safely.
  180. */
  181. return lhs == rhs;
  182. }
  183. #endif