Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 9 | #ifndef DM_SNAPSHOT_H |
| 10 | #define DM_SNAPSHOT_H |
| 11 | |
Mikulas Patocka | 586e80e | 2008-10-21 17:44:59 +0100 | [diff] [blame] | 12 | #include <linux/device-mapper.h> |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 13 | #include "dm-bio-list.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/blkdev.h> |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 15 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | struct exception_table { |
| 18 | uint32_t hash_mask; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 19 | unsigned hash_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | struct list_head *table; |
| 21 | }; |
| 22 | |
| 23 | /* |
| 24 | * The snapshot code deals with largish chunks of the disk at a |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 25 | * time. Typically 32k - 512k. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | typedef sector_t chunk_t; |
| 28 | |
| 29 | /* |
| 30 | * An exception is used where an old chunk of data has been |
| 31 | * replaced by a new one. |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 32 | * If chunk_t is 64 bits in size, the top 8 bits of new_chunk hold the number |
| 33 | * of chunks that follow contiguously. Remaining bits hold the number of the |
| 34 | * chunk within the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 36 | struct dm_snap_exception { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | struct list_head hash_list; |
| 38 | |
| 39 | chunk_t old_chunk; |
| 40 | chunk_t new_chunk; |
| 41 | }; |
| 42 | |
| 43 | /* |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 44 | * Funtions to manipulate consecutive chunks |
| 45 | */ |
| 46 | # if defined(CONFIG_LBD) || (BITS_PER_LONG == 64) |
| 47 | # define DM_CHUNK_CONSECUTIVE_BITS 8 |
| 48 | # define DM_CHUNK_NUMBER_BITS 56 |
| 49 | |
| 50 | static inline chunk_t dm_chunk_number(chunk_t chunk) |
| 51 | { |
| 52 | return chunk & (chunk_t)((1ULL << DM_CHUNK_NUMBER_BITS) - 1ULL); |
| 53 | } |
| 54 | |
| 55 | static inline unsigned dm_consecutive_chunk_count(struct dm_snap_exception *e) |
| 56 | { |
| 57 | return e->new_chunk >> DM_CHUNK_NUMBER_BITS; |
| 58 | } |
| 59 | |
| 60 | static inline void dm_consecutive_chunk_count_inc(struct dm_snap_exception *e) |
| 61 | { |
| 62 | e->new_chunk += (1ULL << DM_CHUNK_NUMBER_BITS); |
| 63 | |
| 64 | BUG_ON(!dm_consecutive_chunk_count(e)); |
| 65 | } |
| 66 | |
| 67 | # else |
| 68 | # define DM_CHUNK_CONSECUTIVE_BITS 0 |
| 69 | |
| 70 | static inline chunk_t dm_chunk_number(chunk_t chunk) |
| 71 | { |
| 72 | return chunk; |
| 73 | } |
| 74 | |
| 75 | static inline unsigned dm_consecutive_chunk_count(struct dm_snap_exception *e) |
| 76 | { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static inline void dm_consecutive_chunk_count_inc(struct dm_snap_exception *e) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | # endif |
| 85 | |
| 86 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * Abstraction to handle the meta/layout of exception stores (the |
| 88 | * COW device). |
| 89 | */ |
| 90 | struct exception_store { |
| 91 | |
| 92 | /* |
| 93 | * Destroys this object when you've finished with it. |
| 94 | */ |
| 95 | void (*destroy) (struct exception_store *store); |
| 96 | |
| 97 | /* |
| 98 | * The target shouldn't read the COW device until this is |
| 99 | * called. |
| 100 | */ |
| 101 | int (*read_metadata) (struct exception_store *store); |
| 102 | |
| 103 | /* |
| 104 | * Find somewhere to store the next exception. |
| 105 | */ |
| 106 | int (*prepare_exception) (struct exception_store *store, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 107 | struct dm_snap_exception *e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * Update the metadata with this exception. |
| 111 | */ |
| 112 | void (*commit_exception) (struct exception_store *store, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 113 | struct dm_snap_exception *e, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | void (*callback) (void *, int success), |
| 115 | void *callback_context); |
| 116 | |
| 117 | /* |
| 118 | * The snapshot is invalid, note this in the metadata. |
| 119 | */ |
| 120 | void (*drop_snapshot) (struct exception_store *store); |
| 121 | |
| 122 | /* |
| 123 | * Return how full the snapshot is. |
| 124 | */ |
| 125 | void (*fraction_full) (struct exception_store *store, |
| 126 | sector_t *numerator, |
| 127 | sector_t *denominator); |
| 128 | |
| 129 | struct dm_snapshot *snap; |
| 130 | void *context; |
| 131 | }; |
| 132 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 133 | #define DM_TRACKED_CHUNK_HASH_SIZE 16 |
| 134 | #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \ |
| 135 | (DM_TRACKED_CHUNK_HASH_SIZE - 1)) |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | struct dm_snapshot { |
| 138 | struct rw_semaphore lock; |
Mikulas Patocka | 72727ba | 2008-04-24 21:43:11 +0100 | [diff] [blame] | 139 | struct dm_target *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | struct dm_dev *origin; |
| 142 | struct dm_dev *cow; |
| 143 | |
| 144 | /* List of snapshots per Origin */ |
| 145 | struct list_head list; |
| 146 | |
| 147 | /* Size of data blocks saved - must be a power of 2 */ |
| 148 | chunk_t chunk_size; |
| 149 | chunk_t chunk_mask; |
| 150 | chunk_t chunk_shift; |
| 151 | |
| 152 | /* You can't use a snapshot if this is 0 (e.g. if full) */ |
| 153 | int valid; |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 154 | |
| 155 | /* Origin writes don't trigger exceptions until this is set */ |
| 156 | int active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | /* Used for display of table */ |
| 159 | char type; |
| 160 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 161 | mempool_t *pending_pool; |
| 162 | |
Mikulas Patocka | 879129d2 | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 163 | atomic_t pending_exceptions_count; |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | struct exception_table pending; |
| 166 | struct exception_table complete; |
| 167 | |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 168 | /* |
| 169 | * pe_lock protects all pending_exception operations and access |
| 170 | * as well as the snapshot_bios list. |
| 171 | */ |
| 172 | spinlock_t pe_lock; |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | /* The on disk metadata handler */ |
| 175 | struct exception_store store; |
| 176 | |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 177 | struct dm_kcopyd_client *kcopyd_client; |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 178 | |
| 179 | /* Queue of snapshot writes for ksnapd to flush */ |
| 180 | struct bio_list queued_bios; |
| 181 | struct work_struct queued_bios_work; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 182 | |
| 183 | /* Chunks with outstanding reads */ |
| 184 | mempool_t *tracked_chunk_pool; |
| 185 | spinlock_t tracked_chunk_lock; |
| 186 | struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | /* |
| 190 | * Used by the exception stores to load exceptions hen |
| 191 | * initialising. |
| 192 | */ |
| 193 | int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new); |
| 194 | |
| 195 | /* |
| 196 | * Constructor and destructor for the default persistent |
| 197 | * store. |
| 198 | */ |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 199 | int dm_create_persistent(struct exception_store *store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 201 | int dm_create_transient(struct exception_store *store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * Return the number of sectors in the device. |
| 205 | */ |
| 206 | static inline sector_t get_dev_size(struct block_device *bdev) |
| 207 | { |
| 208 | return bdev->bd_inode->i_size >> SECTOR_SHIFT; |
| 209 | } |
| 210 | |
| 211 | static inline chunk_t sector_to_chunk(struct dm_snapshot *s, sector_t sector) |
| 212 | { |
| 213 | return (sector & ~s->chunk_mask) >> s->chunk_shift; |
| 214 | } |
| 215 | |
| 216 | static inline sector_t chunk_to_sector(struct dm_snapshot *s, chunk_t chunk) |
| 217 | { |
| 218 | return chunk << s->chunk_shift; |
| 219 | } |
| 220 | |
| 221 | static inline int bdev_equal(struct block_device *lhs, struct block_device *rhs) |
| 222 | { |
| 223 | /* |
| 224 | * There is only ever one instance of a particular block |
| 225 | * device so we can compare pointers safely. |
| 226 | */ |
| 227 | return lhs == rhs; |
| 228 | } |
| 229 | |
| 230 | #endif |