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