dm-snap.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. struct list_head *table;
  17. };
  18. /*
  19. * The snapshot code deals with largish chunks of the disk at a
  20. * time. Typically 64k - 256k.
  21. */
  22. /* FIXME: can we get away with limiting these to a uint32_t ? */
  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. */
  28. struct exception {
  29. struct list_head hash_list;
  30. chunk_t old_chunk;
  31. chunk_t new_chunk;
  32. };
  33. /*
  34. * Abstraction to handle the meta/layout of exception stores (the
  35. * COW device).
  36. */
  37. struct exception_store {
  38. /*
  39. * Destroys this object when you've finished with it.
  40. */
  41. void (*destroy) (struct exception_store *store);
  42. /*
  43. * The target shouldn't read the COW device until this is
  44. * called.
  45. */
  46. int (*read_metadata) (struct exception_store *store);
  47. /*
  48. * Find somewhere to store the next exception.
  49. */
  50. int (*prepare_exception) (struct exception_store *store,
  51. struct exception *e);
  52. /*
  53. * Update the metadata with this exception.
  54. */
  55. void (*commit_exception) (struct exception_store *store,
  56. struct exception *e,
  57. void (*callback) (void *, int success),
  58. void *callback_context);
  59. /*
  60. * The snapshot is invalid, note this in the metadata.
  61. */
  62. void (*drop_snapshot) (struct exception_store *store);
  63. /*
  64. * Return how full the snapshot is.
  65. */
  66. void (*fraction_full) (struct exception_store *store,
  67. sector_t *numerator,
  68. sector_t *denominator);
  69. struct dm_snapshot *snap;
  70. void *context;
  71. };
  72. struct dm_snapshot {
  73. struct rw_semaphore lock;
  74. struct dm_table *table;
  75. struct dm_dev *origin;
  76. struct dm_dev *cow;
  77. /* List of snapshots per Origin */
  78. struct list_head list;
  79. /* Size of data blocks saved - must be a power of 2 */
  80. chunk_t chunk_size;
  81. chunk_t chunk_mask;
  82. chunk_t chunk_shift;
  83. /* You can't use a snapshot if this is 0 (e.g. if full) */
  84. int valid;
  85. /* Origin writes don't trigger exceptions until this is set */
  86. int active;
  87. /* Used for display of table */
  88. char type;
  89. /* The last percentage we notified */
  90. int last_percent;
  91. struct exception_table pending;
  92. struct exception_table complete;
  93. /*
  94. * pe_lock protects all pending_exception operations and access
  95. * as well as the snapshot_bios list.
  96. */
  97. spinlock_t pe_lock;
  98. /* The on disk metadata handler */
  99. struct exception_store store;
  100. struct kcopyd_client *kcopyd_client;
  101. /* Queue of snapshot writes for ksnapd to flush */
  102. struct bio_list queued_bios;
  103. struct work_struct queued_bios_work;
  104. };
  105. /*
  106. * Used by the exception stores to load exceptions hen
  107. * initialising.
  108. */
  109. int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new);
  110. /*
  111. * Constructor and destructor for the default persistent
  112. * store.
  113. */
  114. int dm_create_persistent(struct exception_store *store);
  115. int dm_create_transient(struct exception_store *store);
  116. /*
  117. * Return the number of sectors in the device.
  118. */
  119. static inline sector_t get_dev_size(struct block_device *bdev)
  120. {
  121. return bdev->bd_inode->i_size >> SECTOR_SHIFT;
  122. }
  123. static inline chunk_t sector_to_chunk(struct dm_snapshot *s, sector_t sector)
  124. {
  125. return (sector & ~s->chunk_mask) >> s->chunk_shift;
  126. }
  127. static inline sector_t chunk_to_sector(struct dm_snapshot *s, chunk_t chunk)
  128. {
  129. return chunk << s->chunk_shift;
  130. }
  131. static inline int bdev_equal(struct block_device *lhs, struct block_device *rhs)
  132. {
  133. /*
  134. * There is only ever one instance of a particular block
  135. * device so we can compare pointers safely.
  136. */
  137. return lhs == rhs;
  138. }
  139. #endif