blob: fd04caa9034076772e125baab829496f3f3377d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/device-mapper.h>
Mikulas Patocka90fa1522009-01-06 03:04:54 +000011#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/fs.h>
13#include <linux/init.h>
14#include <linux/kdev_t.h>
15#include <linux/list.h>
16#include <linux/mempool.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/vmalloc.h>
vignesh babu6f3c3f02007-10-19 22:38:44 +010020#include <linux/log2.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010021#include <linux/dm-kcopyd.h>
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010022#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jonathan Brassowaea53d92009-01-06 03:05:15 +000024#include "dm-exception-store.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "snapshots"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
29 * The percentage increment we will wake up users at
30 */
31#define WAKE_UP_PERCENT 5
32
33/*
34 * kcopyd priority of snapshot operations
35 */
36#define SNAPSHOT_COPY_PRIORITY 2
37
38/*
Milan Broz8ee27672008-04-24 21:42:36 +010039 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
Milan Broz8ee27672008-04-24 21:42:36 +010041#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mikulas Patockacd45daf2008-07-21 12:00:32 +010043/*
44 * The size of the mempool used to track chunks in use.
45 */
46#define MIN_IOS 256
47
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010048#define DM_TRACKED_CHUNK_HASH_SIZE 16
49#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
50 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
51
Jon Brassow191437a2009-12-10 23:52:10 +000052struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010053 uint32_t hash_mask;
54 unsigned hash_shift;
55 struct list_head *table;
56};
57
58struct dm_snapshot {
59 struct rw_semaphore lock;
60
61 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000062 struct dm_dev *cow;
63
64 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010065
66 /* List of snapshots per Origin */
67 struct list_head list;
68
69 /* You can't use a snapshot if this is 0 (e.g. if full) */
70 int valid;
71
72 /* Origin writes don't trigger exceptions until this is set */
73 int active;
74
Mike Snitzerc26655c2009-12-10 23:52:12 +000075 /* Whether or not owning mapped_device is suspended */
76 int suspended;
77
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010078 mempool_t *pending_pool;
79
80 atomic_t pending_exceptions_count;
81
Jon Brassow191437a2009-12-10 23:52:10 +000082 struct dm_exception_table pending;
83 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010084
85 /*
86 * pe_lock protects all pending_exception operations and access
87 * as well as the snapshot_bios list.
88 */
89 spinlock_t pe_lock;
90
91 /* The on disk metadata handler */
92 struct dm_exception_store *store;
93
94 struct dm_kcopyd_client *kcopyd_client;
95
96 /* Queue of snapshot writes for ksnapd to flush */
97 struct bio_list queued_bios;
98 struct work_struct queued_bios_work;
99
100 /* Chunks with outstanding reads */
101 mempool_t *tracked_chunk_pool;
102 spinlock_t tracked_chunk_lock;
103 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
104};
105
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000106struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
107{
108 return s->cow;
109}
110EXPORT_SYMBOL(dm_snap_cow);
111
Adrian Bunkc642f9e2006-12-08 02:41:13 -0800112static struct workqueue_struct *ksnapd;
David Howellsc4028952006-11-22 14:57:56 +0000113static void flush_queued_bios(struct work_struct *work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700114
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100115static sector_t chunk_to_sector(struct dm_exception_store *store,
116 chunk_t chunk)
117{
118 return chunk << store->chunk_shift;
119}
120
121static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
122{
123 /*
124 * There is only ever one instance of a particular block
125 * device so we can compare pointers safely.
126 */
127 return lhs == rhs;
128}
129
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100130struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000131 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /*
134 * Origin buffers waiting for this to complete are held
135 * in a bio list
136 */
137 struct bio_list origin_bios;
138 struct bio_list snapshot_bios;
139
140 /*
Alasdair G Kergoneccf0812006-03-27 01:17:42 -0800141 * Short-term queue of pending exceptions prior to submission.
142 */
143 struct list_head list;
144
145 /*
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -0800146 * The primary pending_exception is the one that holds
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700147 * the ref_count and the list of origin_bios for a
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -0800148 * group of pending_exceptions. It is always last to get freed.
149 * These fields get set up when writing to the origin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100151 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -0800152
153 /*
154 * Number of pending_exceptions processing this chunk.
155 * When this drops to zero we must complete the origin bios.
156 * If incrementing or decrementing this, hold pe->snap->lock for
157 * the sibling concerned and not pe->primary_pe->snap->lock unless
158 * they are the same.
159 */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700160 atomic_t ref_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Pointer back to snapshot context */
163 struct dm_snapshot *snap;
164
165 /*
166 * 1 indicates the exception has already been sent to
167 * kcopyd.
168 */
169 int started;
170};
171
172/*
173 * Hash table mapping origin volumes to lists of snapshots and
174 * a lock to protect it
175 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800176static struct kmem_cache *exception_cache;
177static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100179struct dm_snap_tracked_chunk {
180 struct hlist_node node;
181 chunk_t chunk;
182};
183
184static struct kmem_cache *tracked_chunk_cache;
185
186static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
187 chunk_t chunk)
188{
189 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
190 GFP_NOIO);
191 unsigned long flags;
192
193 c->chunk = chunk;
194
195 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
196 hlist_add_head(&c->node,
197 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
198 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
199
200 return c;
201}
202
203static void stop_tracking_chunk(struct dm_snapshot *s,
204 struct dm_snap_tracked_chunk *c)
205{
206 unsigned long flags;
207
208 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
209 hlist_del(&c->node);
210 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
211
212 mempool_free(c, s->tracked_chunk_pool);
213}
214
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100215static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
216{
217 struct dm_snap_tracked_chunk *c;
218 struct hlist_node *hn;
219 int found = 0;
220
221 spin_lock_irq(&s->tracked_chunk_lock);
222
223 hlist_for_each_entry(c, hn,
224 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
225 if (c->chunk == chunk) {
226 found = 1;
227 break;
228 }
229 }
230
231 spin_unlock_irq(&s->tracked_chunk_lock);
232
233 return found;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
237 * One of these per registered origin, held in the snapshot_origins hash
238 */
239struct origin {
240 /* The origin device */
241 struct block_device *bdev;
242
243 struct list_head hash_list;
244
245 /* List of snapshots for this origin */
246 struct list_head snapshots;
247};
248
249/*
250 * Size of the hash table for origin volumes. If we make this
251 * the size of the minors list then it should be nearly perfect
252 */
253#define ORIGIN_HASH_SIZE 256
254#define ORIGIN_MASK 0xFF
255static struct list_head *_origins;
256static struct rw_semaphore _origins_lock;
257
258static int init_origin_hash(void)
259{
260 int i;
261
262 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
263 GFP_KERNEL);
264 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700265 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -ENOMEM;
267 }
268
269 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
270 INIT_LIST_HEAD(_origins + i);
271 init_rwsem(&_origins_lock);
272
273 return 0;
274}
275
276static void exit_origin_hash(void)
277{
278 kfree(_origins);
279}
280
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100281static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 return bdev->bd_dev & ORIGIN_MASK;
284}
285
286static struct origin *__lookup_origin(struct block_device *origin)
287{
288 struct list_head *ol;
289 struct origin *o;
290
291 ol = &_origins[origin_hash(origin)];
292 list_for_each_entry (o, ol, hash_list)
293 if (bdev_equal(o->bdev, origin))
294 return o;
295
296 return NULL;
297}
298
299static void __insert_origin(struct origin *o)
300{
301 struct list_head *sl = &_origins[origin_hash(o->bdev)];
302 list_add_tail(&o->hash_list, sl);
303}
304
305/*
306 * Make a note of the snapshot and its origin so we can look it
307 * up when the origin has a write on it.
308 */
309static int register_snapshot(struct dm_snapshot *snap)
310{
Mikulas Patocka6d45d932009-10-16 23:18:14 +0100311 struct dm_snapshot *l;
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000312 struct origin *o, *new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct block_device *bdev = snap->origin->bdev;
314
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000315 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
316 if (!new_o)
317 return -ENOMEM;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 down_write(&_origins_lock);
320 o = __lookup_origin(bdev);
321
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000322 if (o)
323 kfree(new_o);
324 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000326 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Initialise the struct */
329 INIT_LIST_HEAD(&o->snapshots);
330 o->bdev = bdev;
331
332 __insert_origin(o);
333 }
334
Mikulas Patocka6d45d932009-10-16 23:18:14 +0100335 /* Sort the list according to chunk size, largest-first smallest-last */
336 list_for_each_entry(l, &o->snapshots, list)
337 if (l->store->chunk_size < snap->store->chunk_size)
338 break;
339 list_add_tail(&snap->list, &l->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 up_write(&_origins_lock);
342 return 0;
343}
344
345static void unregister_snapshot(struct dm_snapshot *s)
346{
347 struct origin *o;
348
349 down_write(&_origins_lock);
350 o = __lookup_origin(s->origin->bdev);
351
352 list_del(&s->list);
353 if (list_empty(&o->snapshots)) {
354 list_del(&o->hash_list);
355 kfree(o);
356 }
357
358 up_write(&_origins_lock);
359}
360
361/*
362 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000363 * The lowest hash_shift bits of the chunk number are ignored, allowing
364 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000366static int dm_exception_table_init(struct dm_exception_table *et,
367 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 unsigned int i;
370
Milan Brozd74f81f2008-02-08 02:11:27 +0000371 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 et->hash_mask = size - 1;
373 et->table = dm_vcalloc(size, sizeof(struct list_head));
374 if (!et->table)
375 return -ENOMEM;
376
377 for (i = 0; i < size; i++)
378 INIT_LIST_HEAD(et->table + i);
379
380 return 0;
381}
382
Jon Brassow3510cb92009-12-10 23:52:11 +0000383static void dm_exception_table_exit(struct dm_exception_table *et,
384 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000387 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int i, size;
389
390 size = et->hash_mask + 1;
391 for (i = 0; i < size; i++) {
392 slot = et->table + i;
393
394 list_for_each_entry_safe (ex, next, slot, hash_list)
395 kmem_cache_free(mem, ex);
396 }
397
398 vfree(et->table);
399}
400
Jon Brassow191437a2009-12-10 23:52:10 +0000401static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Milan Brozd74f81f2008-02-08 02:11:27 +0000403 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Jon Brassow3510cb92009-12-10 23:52:11 +0000406static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 list_del(&e->hash_list);
409}
410
411/*
412 * Return the exception data for a sector, or NULL if not
413 * remapped.
414 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000415static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
416 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000419 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 slot = &et->table[exception_hash(et, chunk)];
422 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000423 if (chunk >= e->old_chunk &&
424 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return e;
426
427 return NULL;
428}
429
Jon Brassow3510cb92009-12-10 23:52:11 +0000430static struct dm_exception *alloc_completed_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000432 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
435 if (!e)
436 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
437
438 return e;
439}
440
Jon Brassow3510cb92009-12-10 23:52:11 +0000441static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 kmem_cache_free(exception_cache, e);
444}
445
Mikulas Patocka92e86812008-07-21 12:00:35 +0100446static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100448 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
449 GFP_NOIO);
450
Mikulas Patocka879129d22008-10-30 13:33:16 +0000451 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100452 pe->snap = s;
453
454 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100457static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000459 struct dm_snapshot *s = pe->snap;
460
461 mempool_free(pe, s->pending_pool);
462 smp_mb__before_atomic_dec();
463 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Jon Brassow3510cb92009-12-10 23:52:11 +0000466static void dm_insert_exception(struct dm_exception_table *eh,
467 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000468{
Milan Brozd74f81f2008-02-08 02:11:27 +0000469 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000470 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000471
472 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
473
474 /* Add immediately if this table doesn't support consecutive chunks */
475 if (!eh->hash_shift)
476 goto out;
477
478 /* List is ordered by old_chunk */
479 list_for_each_entry_reverse(e, l, hash_list) {
480 /* Insert after an existing chunk? */
481 if (new_e->old_chunk == (e->old_chunk +
482 dm_consecutive_chunk_count(e) + 1) &&
483 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
484 dm_consecutive_chunk_count(e) + 1)) {
485 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000486 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000487 return;
488 }
489
490 /* Insert before an existing chunk? */
491 if (new_e->old_chunk == (e->old_chunk - 1) &&
492 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
493 dm_consecutive_chunk_count_inc(e);
494 e->old_chunk--;
495 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000496 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000497 return;
498 }
499
500 if (new_e->old_chunk > e->old_chunk)
501 break;
502 }
503
504out:
505 list_add(&new_e->hash_list, e ? &e->hash_list : l);
506}
507
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000508/*
509 * Callback used by the exception stores to load exceptions when
510 * initialising.
511 */
512static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000514 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000515 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jon Brassow3510cb92009-12-10 23:52:11 +0000517 e = alloc_completed_exception();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!e)
519 return -ENOMEM;
520
521 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000522
523 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000525
Jon Brassow3510cb92009-12-10 23:52:11 +0000526 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529}
530
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000531#define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
532
533/*
534 * Return a minimum chunk size of all snapshots that have the specified origin.
535 * Return zero if the origin has no snapshots.
536 */
537static sector_t __minimum_chunk_size(struct origin *o)
538{
539 struct dm_snapshot *snap;
540 unsigned chunk_size = 0;
541
542 if (o)
543 list_for_each_entry(snap, &o->snapshots, list)
544 chunk_size = min_not_zero(chunk_size,
545 snap->store->chunk_size);
546
547 return chunk_size;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/*
551 * Hard coded magic.
552 */
553static int calc_max_buckets(void)
554{
555 /* use a fixed size of 2MB */
556 unsigned long mem = 2 * 1024 * 1024;
557 mem /= sizeof(struct list_head);
558
559 return mem;
560}
561
562/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * Allocate room for a suitable hash table.
564 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100565static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
568
569 /*
570 * Calculate based on the size of the original volume or
571 * the COW volume...
572 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000573 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 origin_dev_size = get_dev_size(s->origin->bdev);
575 max_buckets = calc_max_buckets();
576
Jonathan Brassowfee19982009-04-02 19:55:34 +0100577 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 hash_size = min(hash_size, max_buckets);
579
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000580 if (hash_size < 64)
581 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000582 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000583 if (dm_exception_table_init(&s->complete, hash_size,
584 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -ENOMEM;
586
587 /*
588 * Allocate hash table for in-flight exceptions
589 * Make this smaller than the real hash table
590 */
591 hash_size >>= 3;
592 if (hash_size < 64)
593 hash_size = 64;
594
Jon Brassow3510cb92009-12-10 23:52:11 +0000595 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
596 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -ENOMEM;
598 }
599
600 return 0;
601}
602
603/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
605 */
606static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
607{
608 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100609 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000611 char *origin_path, *cow_path;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100612 unsigned args_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700614 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700615 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000617 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 origin_path = argv[0];
Jonathan Brassowfee19982009-04-02 19:55:34 +0100621 argv++;
622 argc--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +0100625 if (!s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 ti->error = "Cannot allocate snapshot context private "
627 "structure";
628 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000629 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000632 cow_path = argv[0];
633 argv++;
634 argc--;
635
636 r = dm_get_device(ti, cow_path, 0, 0,
637 FMODE_READ | FMODE_WRITE, &s->cow);
638 if (r) {
639 ti->error = "Cannot get COW device";
640 goto bad_cow;
641 }
642
643 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
644 if (r) {
645 ti->error = "Couldn't create exception store";
646 r = -EINVAL;
647 goto bad_store;
648 }
649
650 argv += args_used;
651 argc -= args_used;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
654 if (r) {
655 ti->error = "Cannot get origin device";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100656 goto bad_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000659 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800661 s->active = 0;
Mike Snitzerc26655c2009-12-10 23:52:12 +0000662 s->suspended = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +0000663 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 init_rwsem(&s->lock);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700665 spin_lock_init(&s->pe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100668 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ti->error = "Unable to allocate hash table space";
670 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100671 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100674 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (r) {
676 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100677 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Mikulas Patocka92e86812008-07-21 12:00:35 +0100680 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
681 if (!s->pending_pool) {
682 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100683 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +0100684 }
685
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100686 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
687 tracked_chunk_cache);
688 if (!s->tracked_chunk_pool) {
689 ti->error = "Could not allocate tracked_chunk mempool for "
690 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +0100691 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100692 }
693
694 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
695 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
696
697 spin_lock_init(&s->tracked_chunk_lock);
698
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800699 /* Metadata must only be loaded into one table at once */
Jonathan Brassow493df712009-04-02 19:55:31 +0100700 r = s->store->type->read_metadata(s->store, dm_add_exception,
701 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +0100702 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700703 ti->error = "Failed to read snapshot metadata";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100704 goto bad_load_and_register;
Milan Broz07641472007-07-12 17:28:13 +0100705 } else if (r > 0) {
706 s->valid = 0;
707 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700708 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800709
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700710 bio_list_init(&s->queued_bios);
David Howellsc4028952006-11-22 14:57:56 +0000711 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700712
Mikulas Patocka3f2412d2009-10-16 23:18:16 +0100713 if (!s->store->chunk_size) {
714 ti->error = "Chunk size not set";
715 goto bad_load_and_register;
716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* Add snapshot to the list of snapshots for this origin */
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800719 /* Exceptions aren't triggered till snapshot_resume() is called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (register_snapshot(s)) {
721 r = -EINVAL;
722 ti->error = "Cannot register snapshot origin";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100723 goto bad_load_and_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
726 ti->private = s;
Jonathan Brassowd0216842009-04-02 19:55:32 +0100727 ti->split_io = s->store->chunk_size;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +0100728 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 return 0;
731
Jonathan Brassowfee19982009-04-02 19:55:34 +0100732bad_load_and_register:
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100733 mempool_destroy(s->tracked_chunk_pool);
734
Jonathan Brassowfee19982009-04-02 19:55:34 +0100735bad_tracked_chunk_pool:
Mikulas Patocka92e86812008-07-21 12:00:35 +0100736 mempool_destroy(s->pending_pool);
737
Jonathan Brassowfee19982009-04-02 19:55:34 +0100738bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100739 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jonathan Brassowfee19982009-04-02 19:55:34 +0100741bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +0000742 dm_exception_table_exit(&s->pending, pending_cache);
743 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Jonathan Brassowfee19982009-04-02 19:55:34 +0100745bad_hash_tables:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 dm_put_device(ti, s->origin);
747
Jonathan Brassowfee19982009-04-02 19:55:34 +0100748bad_origin:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000749 dm_exception_store_destroy(s->store);
750
751bad_store:
752 dm_put_device(ti, s->cow);
753
754bad_cow:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 kfree(s);
756
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000757bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return r;
759}
760
Milan Broz31c93a02006-12-08 02:41:11 -0800761static void __free_exceptions(struct dm_snapshot *s)
762{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100763 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -0800764 s->kcopyd_client = NULL;
765
Jon Brassow3510cb92009-12-10 23:52:11 +0000766 dm_exception_table_exit(&s->pending, pending_cache);
767 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -0800768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static void snapshot_dtr(struct dm_target *ti)
771{
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100772#ifdef CONFIG_DM_DEBUG
773 int i;
774#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100775 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700777 flush_workqueue(ksnapd);
778
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800779 /* Prevent further origin writes from using this snapshot. */
780 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 unregister_snapshot(s);
782
Mikulas Patocka879129d22008-10-30 13:33:16 +0000783 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000784 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +0000785 /*
786 * Ensure instructions in mempool_destroy aren't reordered
787 * before atomic_read.
788 */
789 smp_mb();
790
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100791#ifdef CONFIG_DM_DEBUG
792 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
793 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
794#endif
795
796 mempool_destroy(s->tracked_chunk_pool);
797
Milan Broz31c93a02006-12-08 02:41:11 -0800798 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Mikulas Patocka92e86812008-07-21 12:00:35 +0100800 mempool_destroy(s->pending_pool);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 dm_put_device(ti, s->origin);
Jonathan Brassowfee19982009-04-02 19:55:34 +0100803
804 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800805
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000806 dm_put_device(ti, s->cow);
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 kfree(s);
809}
810
811/*
812 * Flush a list of buffers.
813 */
814static void flush_bios(struct bio *bio)
815{
816 struct bio *n;
817
818 while (bio) {
819 n = bio->bi_next;
820 bio->bi_next = NULL;
821 generic_make_request(bio);
822 bio = n;
823 }
824}
825
David Howellsc4028952006-11-22 14:57:56 +0000826static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700827{
David Howellsc4028952006-11-22 14:57:56 +0000828 struct dm_snapshot *s =
829 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700830 struct bio *queued_bios;
831 unsigned long flags;
832
833 spin_lock_irqsave(&s->pe_lock, flags);
834 queued_bios = bio_list_get(&s->queued_bios);
835 spin_unlock_irqrestore(&s->pe_lock, flags);
836
837 flush_bios(queued_bios);
838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/*
841 * Error a list of buffers.
842 */
843static void error_bios(struct bio *bio)
844{
845 struct bio *n;
846
847 while (bio) {
848 n = bio->bi_next;
849 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200850 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 bio = n;
852 }
853}
854
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700855static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800856{
857 if (!s->valid)
858 return;
859
860 if (err == -EIO)
861 DMERR("Invalidating snapshot: Error reading/writing.");
862 else if (err == -ENOMEM)
863 DMERR("Invalidating snapshot: Unable to allocate exception.");
864
Jonathan Brassow493df712009-04-02 19:55:31 +0100865 if (s->store->type->drop_snapshot)
866 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800867
868 s->valid = 0;
869
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000870 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800871}
872
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100873static void get_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700874{
875 atomic_inc(&pe->ref_count);
876}
877
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100878static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700879{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100880 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700881 struct bio *origin_bios = NULL;
882
883 primary_pe = pe->primary_pe;
884
885 /*
886 * If this pe is involved in a write to the origin and
887 * it is the last sibling to complete then release
888 * the bios for the original write to the origin.
889 */
890 if (primary_pe &&
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100891 atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700892 origin_bios = bio_list_get(&primary_pe->origin_bios);
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100893 free_pending_exception(primary_pe);
894 }
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700895
896 /*
897 * Free the pe if it's not linked to an origin write or if
898 * it's not itself a primary pe.
899 */
900 if (!primary_pe || primary_pe != pe)
901 free_pending_exception(pe);
902
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700903 return origin_bios;
904}
905
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100906static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000908 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700910 struct bio *origin_bios = NULL;
911 struct bio *snapshot_bios = NULL;
912 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800914 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* Read/write error - snapshot is unusable */
916 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700917 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700918 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
Jon Brassow3510cb92009-12-10 23:52:11 +0000922 e = alloc_completed_exception();
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800923 if (!e) {
924 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700925 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700926 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800927 goto out;
928 }
929 *e = pe->e;
930
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700931 down_write(&s->lock);
932 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +0000933 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700934 error = 1;
935 goto out;
936 }
937
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800938 /*
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100939 * Check for conflicting reads. This is extremely improbable,
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000940 * so msleep(1) is sufficient and there is no need for a wait queue.
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100941 */
942 while (__chunk_is_tracked(s, pe->e.old_chunk))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000943 msleep(1);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100944
945 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800946 * Add a proper exception, and remove the
947 * in-flight exception from the list.
948 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000949 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 out:
Jon Brassow3510cb92009-12-10 23:52:11 +0000952 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700953 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700954 origin_bios = put_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700956 up_write(&s->lock);
957
958 /* Submit any pending write bios */
959 if (error)
960 error_bios(snapshot_bios);
961 else
962 flush_bios(snapshot_bios);
963
964 flush_bios(origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967static void commit_callback(void *context, int success)
968{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100969 struct dm_snap_pending_exception *pe = context;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 pending_complete(pe, success);
972}
973
974/*
975 * Called when the copy I/O has finished. kcopyd actually runs
976 * this code so don't block.
977 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700978static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100980 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct dm_snapshot *s = pe->snap;
982
983 if (read_err || write_err)
984 pending_complete(pe, 0);
985
986 else
987 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +0100988 s->store->type->commit_exception(s->store, &pe->e,
989 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992/*
993 * Dispatches the copy operation to kcopyd.
994 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100995static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100998 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 struct block_device *bdev = s->origin->bdev;
1000 sector_t dev_size;
1001
1002 dev_size = get_dev_size(bdev);
1003
1004 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001005 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001006 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001008 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001009 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 dest.count = src.count;
1011
1012 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001013 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 &src, 1, &dest, 0, copy_callback, pe);
1015}
1016
Mikulas Patocka29138082009-04-02 19:55:25 +01001017static struct dm_snap_pending_exception *
1018__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1019{
Jon Brassow3510cb92009-12-10 23:52:11 +00001020 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001021
1022 if (!e)
1023 return NULL;
1024
1025 return container_of(e, struct dm_snap_pending_exception, e);
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028/*
1029 * Looks to see if this snapshot already has a pending exception
1030 * for this chunk, otherwise it allocates a new one and inserts
1031 * it into the pending table.
1032 *
1033 * NOTE: a write lock must be held on snap->lock before calling
1034 * this.
1035 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001036static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001037__find_pending_exception(struct dm_snapshot *s,
1038 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Mikulas Patockac6621392009-04-02 19:55:25 +01001040 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001041
Mikulas Patocka29138082009-04-02 19:55:25 +01001042 pe2 = __lookup_pending_exception(s, chunk);
1043 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001044 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001045 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001046 }
1047
1048 pe->e.old_chunk = chunk;
1049 bio_list_init(&pe->origin_bios);
1050 bio_list_init(&pe->snapshot_bios);
1051 pe->primary_pe = NULL;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001052 atomic_set(&pe->ref_count, 0);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001053 pe->started = 0;
1054
Jonathan Brassow493df712009-04-02 19:55:31 +01001055 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001056 free_pending_exception(pe);
1057 return NULL;
1058 }
1059
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001060 get_pending_exception(pe);
Jon Brassow3510cb92009-12-10 23:52:11 +00001061 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return pe;
1064}
1065
Jon Brassow1d4989c2009-12-10 23:52:10 +00001066static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001067 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001069 bio->bi_bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001070 bio->bi_sector = chunk_to_sector(s->store,
1071 dm_chunk_number(e->new_chunk) +
1072 (chunk - e->old_chunk)) +
1073 (bio->bi_sector &
1074 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077static int snapshot_map(struct dm_target *ti, struct bio *bio,
1078 union map_info *map_context)
1079{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001080 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001081 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001082 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001084 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001086 if (unlikely(bio_empty_barrier(bio))) {
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001087 bio->bi_bdev = s->cow->bdev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001088 return DM_MAPIO_REMAPPED;
1089 }
1090
Jonathan Brassow71fab002009-04-02 19:55:33 +01001091 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001094 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001096 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001098 /* FIXME: should only take write lock if we need
1099 * to copy an exception */
1100 down_write(&s->lock);
1101
1102 if (!s->valid) {
1103 r = -EIO;
1104 goto out_unlock;
1105 }
1106
1107 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001108 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001109 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001110 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001111 goto out_unlock;
1112 }
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /*
1115 * Write to snapshot - higher level takes care of RW/RO
1116 * flags so we should only get this if we are
1117 * writeable.
1118 */
1119 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001120 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001121 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001122 up_write(&s->lock);
1123 pe = alloc_pending_exception(s);
1124 down_write(&s->lock);
1125
1126 if (!s->valid) {
1127 free_pending_exception(pe);
1128 r = -EIO;
1129 goto out_unlock;
1130 }
1131
Jon Brassow3510cb92009-12-10 23:52:11 +00001132 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001133 if (e) {
1134 free_pending_exception(pe);
1135 remap_exception(s, e, bio, chunk);
1136 goto out_unlock;
1137 }
1138
Mikulas Patockac6621392009-04-02 19:55:25 +01001139 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001140 if (!pe) {
1141 __invalidate_snapshot(s, -ENOMEM);
1142 r = -EIO;
1143 goto out_unlock;
1144 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001145 }
1146
Milan Brozd74f81f2008-02-08 02:11:27 +00001147 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001148 bio_list_add(&pe->snapshot_bios, bio);
1149
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001150 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001151
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001152 if (!pe->started) {
1153 /* this is protected by snap->lock */
1154 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001155 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001156 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001157 goto out;
1158 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001159 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001160 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001161 map_context->ptr = track_chunk(s, chunk);
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001164 out_unlock:
1165 up_write(&s->lock);
1166 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return r;
1168}
1169
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001170static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1171 int error, union map_info *map_context)
1172{
1173 struct dm_snapshot *s = ti->private;
1174 struct dm_snap_tracked_chunk *c = map_context->ptr;
1175
1176 if (c)
1177 stop_tracking_chunk(s, c);
1178
1179 return 0;
1180}
1181
Mike Snitzerc26655c2009-12-10 23:52:12 +00001182static void snapshot_postsuspend(struct dm_target *ti)
1183{
1184 struct dm_snapshot *s = ti->private;
1185
1186 down_write(&s->lock);
1187 s->suspended = 1;
1188 up_write(&s->lock);
1189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static void snapshot_resume(struct dm_target *ti)
1192{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001193 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001195 down_write(&s->lock);
1196 s->active = 1;
Mike Snitzerc26655c2009-12-10 23:52:12 +00001197 s->suspended = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001198 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static int snapshot_status(struct dm_target *ti, status_type_t type,
1202 char *result, unsigned int maxlen)
1203{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001204 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001205 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 switch (type) {
1208 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00001209
1210 down_write(&snap->lock);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001213 DMEMIT("Invalid");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00001215 if (snap->store->type->usage) {
1216 sector_t total_sectors, sectors_allocated,
1217 metadata_sectors;
1218 snap->store->type->usage(snap->store,
1219 &total_sectors,
1220 &sectors_allocated,
1221 &metadata_sectors);
1222 DMEMIT("%llu/%llu %llu",
1223 (unsigned long long)sectors_allocated,
1224 (unsigned long long)total_sectors,
1225 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001228 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00001230
1231 up_write(&snap->lock);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234
1235 case STATUSTYPE_TABLE:
1236 /*
1237 * kdevname returns a static pointer so we need
1238 * to make private copies if the output is to
1239 * make sense.
1240 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001241 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001242 snap->store->type->status(snap->store, type, result + sz,
1243 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 break;
1245 }
1246
1247 return 0;
1248}
1249
Mike Snitzer8811f462009-09-04 20:40:19 +01001250static int snapshot_iterate_devices(struct dm_target *ti,
1251 iterate_devices_callout_fn fn, void *data)
1252{
1253 struct dm_snapshot *snap = ti->private;
1254
1255 return fn(ti, snap->origin, 0, ti->len, data);
1256}
1257
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259/*-----------------------------------------------------------------
1260 * Origin methods
1261 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262static int __origin_write(struct list_head *snapshots, struct bio *bio)
1263{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001264 int r = DM_MAPIO_REMAPPED, first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001266 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001267 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 chunk_t chunk;
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001269 LIST_HEAD(pe_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Do all the snapshots on this origin */
1272 list_for_each_entry (snap, snapshots, list) {
1273
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001274 down_write(&snap->lock);
1275
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001276 /* Only deal with valid and active snapshots */
1277 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001278 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001280 /* Nothing to do if writing beyond end of snapshot */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001281 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001282 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /*
1285 * Remember, different snapshots can have
1286 * different chunk sizes.
1287 */
Jonathan Brassow71fab002009-04-02 19:55:33 +01001288 chunk = sector_to_chunk(snap->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /*
1291 * Check exception table to see if block
1292 * is already remapped in this snapshot
1293 * and trigger an exception if not.
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001294 *
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001295 * ref_count is initialised to 1 so pending_complete()
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001296 * won't destroy the primary_pe while we're inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001298 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001299 if (e)
1300 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Mikulas Patocka29138082009-04-02 19:55:25 +01001302 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001303 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001304 up_write(&snap->lock);
1305 pe = alloc_pending_exception(snap);
1306 down_write(&snap->lock);
1307
1308 if (!snap->valid) {
1309 free_pending_exception(pe);
1310 goto next_snapshot;
1311 }
1312
Jon Brassow3510cb92009-12-10 23:52:11 +00001313 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001314 if (e) {
1315 free_pending_exception(pe);
1316 goto next_snapshot;
1317 }
1318
Mikulas Patockac6621392009-04-02 19:55:25 +01001319 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001320 if (!pe) {
1321 __invalidate_snapshot(snap, -ENOMEM);
1322 goto next_snapshot;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001326 if (!primary_pe) {
1327 /*
1328 * Either every pe here has same
1329 * primary_pe or none has one yet.
1330 */
1331 if (pe->primary_pe)
1332 primary_pe = pe->primary_pe;
1333 else {
1334 primary_pe = pe;
1335 first = 1;
1336 }
1337
1338 bio_list_add(&primary_pe->origin_bios, bio);
1339
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001340 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001341 }
1342
1343 if (!pe->primary_pe) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001344 pe->primary_pe = primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001345 get_pending_exception(primary_pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001346 }
1347
1348 if (!pe->started) {
1349 pe->started = 1;
1350 list_add_tail(&pe->list, &pe_queue);
1351 }
1352
1353 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 up_write(&snap->lock);
1355 }
1356
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001357 if (!primary_pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001358 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001359
1360 /*
1361 * If this is the first time we're processing this chunk and
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001362 * ref_count is now 1 it means all the pending exceptions
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001363 * got completed while we were in the loop above, so it falls to
1364 * us here to remove the primary_pe and submit any origin_bios.
1365 */
1366
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001367 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001368 flush_bios(bio_list_get(&primary_pe->origin_bios));
1369 free_pending_exception(primary_pe);
1370 /* If we got here, pe_queue is necessarily empty. */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001371 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001372 }
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /*
1375 * Now that we have a complete pe list we can start the copying.
1376 */
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001377 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1378 start_copy(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 return r;
1381}
1382
1383/*
1384 * Called on a write from the origin driver.
1385 */
1386static int do_origin(struct dm_dev *origin, struct bio *bio)
1387{
1388 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001389 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 down_read(&_origins_lock);
1392 o = __lookup_origin(origin->bdev);
1393 if (o)
1394 r = __origin_write(&o->snapshots, bio);
1395 up_read(&_origins_lock);
1396
1397 return r;
1398}
1399
1400/*
1401 * Origin: maps a linear range of a device, with hooks for snapshotting.
1402 */
1403
1404/*
1405 * Construct an origin mapping: <dev_path>
1406 * The context for an origin is merely a 'struct dm_dev *'
1407 * pointing to the real device.
1408 */
1409static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1410{
1411 int r;
1412 struct dm_dev *dev;
1413
1414 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001415 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -EINVAL;
1417 }
1418
1419 r = dm_get_device(ti, argv[0], 0, ti->len,
1420 dm_table_get_mode(ti->table), &dev);
1421 if (r) {
1422 ti->error = "Cannot get target device";
1423 return r;
1424 }
1425
1426 ti->private = dev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001427 ti->num_flush_requests = 1;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return 0;
1430}
1431
1432static void origin_dtr(struct dm_target *ti)
1433{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001434 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 dm_put_device(ti, dev);
1436}
1437
1438static int origin_map(struct dm_target *ti, struct bio *bio,
1439 union map_info *map_context)
1440{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001441 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 bio->bi_bdev = dev->bdev;
1443
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001444 if (unlikely(bio_empty_barrier(bio)))
1445 return DM_MAPIO_REMAPPED;
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001448 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/*
1452 * Set the target "split_io" field to the minimum of all the snapshots'
1453 * chunk sizes.
1454 */
1455static void origin_resume(struct dm_target *ti)
1456{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001457 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 down_read(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Mikulas Patocka7e201b32009-12-10 23:52:08 +00001461 ti->split_io = __minimum_chunk_size(__lookup_origin(dev->bdev));
1462
1463 up_read(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1467 unsigned int maxlen)
1468{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001469 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 switch (type) {
1472 case STATUSTYPE_INFO:
1473 result[0] = '\0';
1474 break;
1475
1476 case STATUSTYPE_TABLE:
1477 snprintf(result, maxlen, "%s", dev->name);
1478 break;
1479 }
1480
1481 return 0;
1482}
1483
Mike Snitzer8811f462009-09-04 20:40:19 +01001484static int origin_iterate_devices(struct dm_target *ti,
1485 iterate_devices_callout_fn fn, void *data)
1486{
1487 struct dm_dev *dev = ti->private;
1488
1489 return fn(ti, dev, 0, ti->len, data);
1490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492static struct target_type origin_target = {
1493 .name = "snapshot-origin",
Mike Snitzer8811f462009-09-04 20:40:19 +01001494 .version = {1, 7, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .module = THIS_MODULE,
1496 .ctr = origin_ctr,
1497 .dtr = origin_dtr,
1498 .map = origin_map,
1499 .resume = origin_resume,
1500 .status = origin_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01001501 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502};
1503
1504static struct target_type snapshot_target = {
1505 .name = "snapshot",
Mike Snitzerc26655c2009-12-10 23:52:12 +00001506 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 .module = THIS_MODULE,
1508 .ctr = snapshot_ctr,
1509 .dtr = snapshot_dtr,
1510 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001511 .end_io = snapshot_end_io,
Mike Snitzerc26655c2009-12-10 23:52:12 +00001512 .postsuspend = snapshot_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 .resume = snapshot_resume,
1514 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01001515 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516};
1517
1518static int __init dm_snapshot_init(void)
1519{
1520 int r;
1521
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001522 r = dm_exception_store_init();
1523 if (r) {
1524 DMERR("Failed to initialize exception stores");
1525 return r;
1526 }
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 r = dm_register_target(&snapshot_target);
1529 if (r) {
1530 DMERR("snapshot target register failed %d", r);
Jonathan Brassow034a1862009-10-16 23:18:14 +01001531 goto bad_register_snapshot_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533
1534 r = dm_register_target(&origin_target);
1535 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001536 DMERR("Origin target register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 goto bad1;
1538 }
1539
1540 r = init_origin_hash();
1541 if (r) {
1542 DMERR("init_origin_hash failed.");
1543 goto bad2;
1544 }
1545
Jon Brassow1d4989c2009-12-10 23:52:10 +00001546 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (!exception_cache) {
1548 DMERR("Couldn't create exception cache.");
1549 r = -ENOMEM;
1550 goto bad3;
1551 }
1552
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001553 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (!pending_cache) {
1555 DMERR("Couldn't create pending cache.");
1556 r = -ENOMEM;
1557 goto bad4;
1558 }
1559
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001560 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1561 if (!tracked_chunk_cache) {
1562 DMERR("Couldn't create cache to track chunks in use.");
1563 r = -ENOMEM;
1564 goto bad5;
1565 }
1566
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001567 ksnapd = create_singlethread_workqueue("ksnapd");
1568 if (!ksnapd) {
1569 DMERR("Failed to create ksnapd workqueue.");
1570 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001571 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001572 }
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return 0;
1575
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001576bad_pending_pool:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001577 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001578bad5:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 kmem_cache_destroy(pending_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001580bad4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 kmem_cache_destroy(exception_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001582bad3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 exit_origin_hash();
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001584bad2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 dm_unregister_target(&origin_target);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001586bad1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01001588
1589bad_register_snapshot_target:
1590 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return r;
1592}
1593
1594static void __exit dm_snapshot_exit(void)
1595{
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001596 destroy_workqueue(ksnapd);
1597
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001598 dm_unregister_target(&snapshot_target);
1599 dm_unregister_target(&origin_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 kmem_cache_destroy(pending_cache);
1603 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001604 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001605
1606 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
1609/* Module hooks */
1610module_init(dm_snapshot_init);
1611module_exit(dm_snapshot_exit);
1612
1613MODULE_DESCRIPTION(DM_NAME " snapshot target");
1614MODULE_AUTHOR("Joe Thornber");
1615MODULE_LICENSE("GPL");