dm-snap.h 3.4 KB

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