blob: 4ceedd4f22afc849d91ab92ac1ea96f22e40871c [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/ctype.h>
11#include <linux/device-mapper.h>
Mikulas Patocka90fa1522009-01-06 03:04:54 +000012#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/fs.h>
14#include <linux/init.h>
15#include <linux/kdev_t.h>
16#include <linux/list.h>
17#include <linux/mempool.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/vmalloc.h>
vignesh babu6f3c3f02007-10-19 22:38:44 +010021#include <linux/log2.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010022#include <linux/dm-kcopyd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "dm-snap.h"
25#include "dm-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "snapshots"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * The percentage increment we will wake up users at
31 */
32#define WAKE_UP_PERCENT 5
33
34/*
35 * kcopyd priority of snapshot operations
36 */
37#define SNAPSHOT_COPY_PRIORITY 2
38
39/*
Milan Broz8ee27672008-04-24 21:42:36 +010040 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Milan Broz8ee27672008-04-24 21:42:36 +010042#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Mikulas Patockacd45daf2008-07-21 12:00:32 +010044/*
45 * The size of the mempool used to track chunks in use.
46 */
47#define MIN_IOS 256
48
Adrian Bunkc642f9e2006-12-08 02:41:13 -080049static struct workqueue_struct *ksnapd;
David Howellsc4028952006-11-22 14:57:56 +000050static void flush_queued_bios(struct work_struct *work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070051
Alasdair G Kergon028867a2007-07-12 17:26:32 +010052struct dm_snap_pending_exception {
53 struct dm_snap_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 /*
56 * Origin buffers waiting for this to complete are held
57 * in a bio list
58 */
59 struct bio_list origin_bios;
60 struct bio_list snapshot_bios;
61
62 /*
Alasdair G Kergoneccf0812006-03-27 01:17:42 -080063 * Short-term queue of pending exceptions prior to submission.
64 */
65 struct list_head list;
66
67 /*
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080068 * The primary pending_exception is the one that holds
Alasdair G Kergon4b832e82006-10-03 01:15:30 -070069 * the ref_count and the list of origin_bios for a
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080070 * group of pending_exceptions. It is always last to get freed.
71 * These fields get set up when writing to the origin.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010073 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080074
75 /*
76 * Number of pending_exceptions processing this chunk.
77 * When this drops to zero we must complete the origin bios.
78 * If incrementing or decrementing this, hold pe->snap->lock for
79 * the sibling concerned and not pe->primary_pe->snap->lock unless
80 * they are the same.
81 */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -070082 atomic_t ref_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /* Pointer back to snapshot context */
85 struct dm_snapshot *snap;
86
87 /*
88 * 1 indicates the exception has already been sent to
89 * kcopyd.
90 */
91 int started;
92};
93
94/*
95 * Hash table mapping origin volumes to lists of snapshots and
96 * a lock to protect it
97 */
Christoph Lametere18b8902006-12-06 20:33:20 -080098static struct kmem_cache *exception_cache;
99static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100101struct dm_snap_tracked_chunk {
102 struct hlist_node node;
103 chunk_t chunk;
104};
105
106static struct kmem_cache *tracked_chunk_cache;
107
108static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
109 chunk_t chunk)
110{
111 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
112 GFP_NOIO);
113 unsigned long flags;
114
115 c->chunk = chunk;
116
117 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
118 hlist_add_head(&c->node,
119 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
120 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
121
122 return c;
123}
124
125static void stop_tracking_chunk(struct dm_snapshot *s,
126 struct dm_snap_tracked_chunk *c)
127{
128 unsigned long flags;
129
130 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
131 hlist_del(&c->node);
132 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
133
134 mempool_free(c, s->tracked_chunk_pool);
135}
136
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100137static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
138{
139 struct dm_snap_tracked_chunk *c;
140 struct hlist_node *hn;
141 int found = 0;
142
143 spin_lock_irq(&s->tracked_chunk_lock);
144
145 hlist_for_each_entry(c, hn,
146 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
147 if (c->chunk == chunk) {
148 found = 1;
149 break;
150 }
151 }
152
153 spin_unlock_irq(&s->tracked_chunk_lock);
154
155 return found;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 * One of these per registered origin, held in the snapshot_origins hash
160 */
161struct origin {
162 /* The origin device */
163 struct block_device *bdev;
164
165 struct list_head hash_list;
166
167 /* List of snapshots for this origin */
168 struct list_head snapshots;
169};
170
171/*
172 * Size of the hash table for origin volumes. If we make this
173 * the size of the minors list then it should be nearly perfect
174 */
175#define ORIGIN_HASH_SIZE 256
176#define ORIGIN_MASK 0xFF
177static struct list_head *_origins;
178static struct rw_semaphore _origins_lock;
179
180static int init_origin_hash(void)
181{
182 int i;
183
184 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
185 GFP_KERNEL);
186 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700187 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -ENOMEM;
189 }
190
191 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
192 INIT_LIST_HEAD(_origins + i);
193 init_rwsem(&_origins_lock);
194
195 return 0;
196}
197
198static void exit_origin_hash(void)
199{
200 kfree(_origins);
201}
202
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100203static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 return bdev->bd_dev & ORIGIN_MASK;
206}
207
208static struct origin *__lookup_origin(struct block_device *origin)
209{
210 struct list_head *ol;
211 struct origin *o;
212
213 ol = &_origins[origin_hash(origin)];
214 list_for_each_entry (o, ol, hash_list)
215 if (bdev_equal(o->bdev, origin))
216 return o;
217
218 return NULL;
219}
220
221static void __insert_origin(struct origin *o)
222{
223 struct list_head *sl = &_origins[origin_hash(o->bdev)];
224 list_add_tail(&o->hash_list, sl);
225}
226
227/*
228 * Make a note of the snapshot and its origin so we can look it
229 * up when the origin has a write on it.
230 */
231static int register_snapshot(struct dm_snapshot *snap)
232{
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000233 struct origin *o, *new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct block_device *bdev = snap->origin->bdev;
235
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000236 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
237 if (!new_o)
238 return -ENOMEM;
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 down_write(&_origins_lock);
241 o = __lookup_origin(bdev);
242
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000243 if (o)
244 kfree(new_o);
245 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000247 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* Initialise the struct */
250 INIT_LIST_HEAD(&o->snapshots);
251 o->bdev = bdev;
252
253 __insert_origin(o);
254 }
255
256 list_add_tail(&snap->list, &o->snapshots);
257
258 up_write(&_origins_lock);
259 return 0;
260}
261
262static void unregister_snapshot(struct dm_snapshot *s)
263{
264 struct origin *o;
265
266 down_write(&_origins_lock);
267 o = __lookup_origin(s->origin->bdev);
268
269 list_del(&s->list);
270 if (list_empty(&o->snapshots)) {
271 list_del(&o->hash_list);
272 kfree(o);
273 }
274
275 up_write(&_origins_lock);
276}
277
278/*
279 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000280 * The lowest hash_shift bits of the chunk number are ignored, allowing
281 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000283static int init_exception_table(struct exception_table *et, uint32_t size,
284 unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 unsigned int i;
287
Milan Brozd74f81f2008-02-08 02:11:27 +0000288 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 et->hash_mask = size - 1;
290 et->table = dm_vcalloc(size, sizeof(struct list_head));
291 if (!et->table)
292 return -ENOMEM;
293
294 for (i = 0; i < size; i++)
295 INIT_LIST_HEAD(et->table + i);
296
297 return 0;
298}
299
Christoph Lametere18b8902006-12-06 20:33:20 -0800300static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100303 struct dm_snap_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int i, size;
305
306 size = et->hash_mask + 1;
307 for (i = 0; i < size; i++) {
308 slot = et->table + i;
309
310 list_for_each_entry_safe (ex, next, slot, hash_list)
311 kmem_cache_free(mem, ex);
312 }
313
314 vfree(et->table);
315}
316
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100317static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Milan Brozd74f81f2008-02-08 02:11:27 +0000319 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100322static void insert_exception(struct exception_table *eh,
323 struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
326 list_add(&e->hash_list, l);
327}
328
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100329static void remove_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 list_del(&e->hash_list);
332}
333
334/*
335 * Return the exception data for a sector, or NULL if not
336 * remapped.
337 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100338static struct dm_snap_exception *lookup_exception(struct exception_table *et,
339 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100342 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 slot = &et->table[exception_hash(et, chunk)];
345 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000346 if (chunk >= e->old_chunk &&
347 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return e;
349
350 return NULL;
351}
352
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100353static struct dm_snap_exception *alloc_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100355 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
358 if (!e)
359 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
360
361 return e;
362}
363
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100364static void free_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 kmem_cache_free(exception_cache, e);
367}
368
Mikulas Patocka92e86812008-07-21 12:00:35 +0100369static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100371 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
372 GFP_NOIO);
373
Mikulas Patocka879129d22008-10-30 13:33:16 +0000374 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100375 pe->snap = s;
376
377 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100380static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000382 struct dm_snapshot *s = pe->snap;
383
384 mempool_free(pe, s->pending_pool);
385 smp_mb__before_atomic_dec();
386 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Milan Brozd74f81f2008-02-08 02:11:27 +0000389static void insert_completed_exception(struct dm_snapshot *s,
390 struct dm_snap_exception *new_e)
391{
392 struct exception_table *eh = &s->complete;
393 struct list_head *l;
394 struct dm_snap_exception *e = NULL;
395
396 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
397
398 /* Add immediately if this table doesn't support consecutive chunks */
399 if (!eh->hash_shift)
400 goto out;
401
402 /* List is ordered by old_chunk */
403 list_for_each_entry_reverse(e, l, hash_list) {
404 /* Insert after an existing chunk? */
405 if (new_e->old_chunk == (e->old_chunk +
406 dm_consecutive_chunk_count(e) + 1) &&
407 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
408 dm_consecutive_chunk_count(e) + 1)) {
409 dm_consecutive_chunk_count_inc(e);
410 free_exception(new_e);
411 return;
412 }
413
414 /* Insert before an existing chunk? */
415 if (new_e->old_chunk == (e->old_chunk - 1) &&
416 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
417 dm_consecutive_chunk_count_inc(e);
418 e->old_chunk--;
419 e->new_chunk--;
420 free_exception(new_e);
421 return;
422 }
423
424 if (new_e->old_chunk > e->old_chunk)
425 break;
426 }
427
428out:
429 list_add(&new_e->hash_list, e ? &e->hash_list : l);
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
433{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100434 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 e = alloc_exception();
437 if (!e)
438 return -ENOMEM;
439
440 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000441
442 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000444
445 insert_completed_exception(s, e);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448}
449
450/*
451 * Hard coded magic.
452 */
453static int calc_max_buckets(void)
454{
455 /* use a fixed size of 2MB */
456 unsigned long mem = 2 * 1024 * 1024;
457 mem /= sizeof(struct list_head);
458
459 return mem;
460}
461
462/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * Allocate room for a suitable hash table.
464 */
465static int init_hash_tables(struct dm_snapshot *s)
466{
467 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
468
469 /*
470 * Calculate based on the size of the original volume or
471 * the COW volume...
472 */
473 cow_dev_size = get_dev_size(s->cow->bdev);
474 origin_dev_size = get_dev_size(s->origin->bdev);
475 max_buckets = calc_max_buckets();
476
477 hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
478 hash_size = min(hash_size, max_buckets);
479
Robert P. J. Day8defd832008-02-08 02:10:06 +0000480 hash_size = rounddown_pow_of_two(hash_size);
Milan Brozd74f81f2008-02-08 02:11:27 +0000481 if (init_exception_table(&s->complete, hash_size,
482 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -ENOMEM;
484
485 /*
486 * Allocate hash table for in-flight exceptions
487 * Make this smaller than the real hash table
488 */
489 hash_size >>= 3;
490 if (hash_size < 64)
491 hash_size = 64;
492
Milan Brozd74f81f2008-02-08 02:11:27 +0000493 if (init_exception_table(&s->pending, hash_size, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 exit_exception_table(&s->complete, exception_cache);
495 return -ENOMEM;
496 }
497
498 return 0;
499}
500
501/*
502 * Round a number up to the nearest 'size' boundary. size must
503 * be a power of 2.
504 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100505static ulong round_up(ulong n, ulong size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 size--;
508 return (n + size) & ~size;
509}
510
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700511static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
512 char **error)
513{
514 unsigned long chunk_size;
515 char *value;
516
517 chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
518 if (*chunk_size_arg == '\0' || *value != '\0') {
519 *error = "Invalid chunk size";
520 return -EINVAL;
521 }
522
523 if (!chunk_size) {
524 s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
525 return 0;
526 }
527
528 /*
529 * Chunk size must be multiple of page size. Silently
530 * round up if it's not.
531 */
532 chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
533
534 /* Check chunk_size is a power of 2 */
vignesh babu6f3c3f02007-10-19 22:38:44 +0100535 if (!is_power_of_2(chunk_size)) {
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700536 *error = "Chunk size is not a power of 2";
537 return -EINVAL;
538 }
539
540 /* Validate the chunk size against the device block size */
541 if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
542 *error = "Chunk size is not a multiple of device blocksize";
543 return -EINVAL;
544 }
545
546 s->chunk_size = chunk_size;
547 s->chunk_mask = chunk_size - 1;
548 s->chunk_shift = ffs(chunk_size) - 1;
549
550 return 0;
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/*
554 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
555 */
556static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
557{
558 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100559 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int r = -EINVAL;
561 char persistent;
562 char *origin_path;
563 char *cow_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700565 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700566 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 r = -EINVAL;
568 goto bad1;
569 }
570
571 origin_path = argv[0];
572 cow_path = argv[1];
573 persistent = toupper(*argv[2]);
574
575 if (persistent != 'P' && persistent != 'N') {
576 ti->error = "Persistent flag is not P or N";
577 r = -EINVAL;
578 goto bad1;
579 }
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 s = kmalloc(sizeof(*s), GFP_KERNEL);
582 if (s == NULL) {
583 ti->error = "Cannot allocate snapshot context private "
584 "structure";
585 r = -ENOMEM;
586 goto bad1;
587 }
588
589 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
590 if (r) {
591 ti->error = "Cannot get origin device";
592 goto bad2;
593 }
594
595 r = dm_get_device(ti, cow_path, 0, 0,
596 FMODE_READ | FMODE_WRITE, &s->cow);
597 if (r) {
598 dm_put_device(ti, s->origin);
599 ti->error = "Cannot get COW device";
600 goto bad2;
601 }
602
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700603 r = set_chunk_size(s, argv[3], &ti->error);
604 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto bad3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 s->type = persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800610 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +0000611 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 init_rwsem(&s->lock);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700613 spin_lock_init(&s->pe_lock);
Mikulas Patocka72727ba2008-04-24 21:43:11 +0100614 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* Allocate hash table for COW data */
617 if (init_hash_tables(s)) {
618 ti->error = "Unable to allocate hash table space";
619 r = -ENOMEM;
620 goto bad3;
621 }
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 s->store.snap = s;
624
625 if (persistent == 'P')
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700626 r = dm_create_persistent(&s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 else
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700628 r = dm_create_transient(&s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (r) {
631 ti->error = "Couldn't create exception store";
632 r = -EINVAL;
633 goto bad4;
634 }
635
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100636 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (r) {
638 ti->error = "Could not create kcopyd client";
639 goto bad5;
640 }
641
Mikulas Patocka92e86812008-07-21 12:00:35 +0100642 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
643 if (!s->pending_pool) {
644 ti->error = "Could not allocate mempool for pending exceptions";
645 goto bad6;
646 }
647
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100648 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
649 tracked_chunk_cache);
650 if (!s->tracked_chunk_pool) {
651 ti->error = "Could not allocate tracked_chunk mempool for "
652 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +0100653 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100654 }
655
656 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
657 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
658
659 spin_lock_init(&s->tracked_chunk_lock);
660
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800661 /* Metadata must only be loaded into one table at once */
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700662 r = s->store.read_metadata(&s->store);
Milan Broz07641472007-07-12 17:28:13 +0100663 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700664 ti->error = "Failed to read snapshot metadata";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100665 goto bad_load_and_register;
Milan Broz07641472007-07-12 17:28:13 +0100666 } else if (r > 0) {
667 s->valid = 0;
668 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700669 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800670
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700671 bio_list_init(&s->queued_bios);
David Howellsc4028952006-11-22 14:57:56 +0000672 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* Add snapshot to the list of snapshots for this origin */
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800675 /* Exceptions aren't triggered till snapshot_resume() is called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (register_snapshot(s)) {
677 r = -EINVAL;
678 ti->error = "Cannot register snapshot origin";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100679 goto bad_load_and_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 ti->private = s;
Alasdair G Kergonc51c2752006-06-26 00:27:18 -0700683 ti->split_io = s->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 return 0;
686
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100687 bad_load_and_register:
688 mempool_destroy(s->tracked_chunk_pool);
689
Mikulas Patocka92e86812008-07-21 12:00:35 +0100690 bad_tracked_chunk_pool:
691 mempool_destroy(s->pending_pool);
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 bad6:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100694 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 bad5:
697 s->store.destroy(&s->store);
698
699 bad4:
700 exit_exception_table(&s->pending, pending_cache);
701 exit_exception_table(&s->complete, exception_cache);
702
703 bad3:
704 dm_put_device(ti, s->cow);
705 dm_put_device(ti, s->origin);
706
707 bad2:
708 kfree(s);
709
710 bad1:
711 return r;
712}
713
Milan Broz31c93a02006-12-08 02:41:11 -0800714static void __free_exceptions(struct dm_snapshot *s)
715{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100716 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -0800717 s->kcopyd_client = NULL;
718
719 exit_exception_table(&s->pending, pending_cache);
720 exit_exception_table(&s->complete, exception_cache);
721
722 s->store.destroy(&s->store);
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static void snapshot_dtr(struct dm_target *ti)
726{
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100727#ifdef CONFIG_DM_DEBUG
728 int i;
729#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100730 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700732 flush_workqueue(ksnapd);
733
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800734 /* Prevent further origin writes from using this snapshot. */
735 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 unregister_snapshot(s);
737
Mikulas Patocka879129d22008-10-30 13:33:16 +0000738 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000739 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +0000740 /*
741 * Ensure instructions in mempool_destroy aren't reordered
742 * before atomic_read.
743 */
744 smp_mb();
745
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100746#ifdef CONFIG_DM_DEBUG
747 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
748 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
749#endif
750
751 mempool_destroy(s->tracked_chunk_pool);
752
Milan Broz31c93a02006-12-08 02:41:11 -0800753 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Mikulas Patocka92e86812008-07-21 12:00:35 +0100755 mempool_destroy(s->pending_pool);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 dm_put_device(ti, s->origin);
758 dm_put_device(ti, s->cow);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 kfree(s);
761}
762
763/*
764 * Flush a list of buffers.
765 */
766static void flush_bios(struct bio *bio)
767{
768 struct bio *n;
769
770 while (bio) {
771 n = bio->bi_next;
772 bio->bi_next = NULL;
773 generic_make_request(bio);
774 bio = n;
775 }
776}
777
David Howellsc4028952006-11-22 14:57:56 +0000778static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700779{
David Howellsc4028952006-11-22 14:57:56 +0000780 struct dm_snapshot *s =
781 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700782 struct bio *queued_bios;
783 unsigned long flags;
784
785 spin_lock_irqsave(&s->pe_lock, flags);
786 queued_bios = bio_list_get(&s->queued_bios);
787 spin_unlock_irqrestore(&s->pe_lock, flags);
788
789 flush_bios(queued_bios);
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792/*
793 * Error a list of buffers.
794 */
795static void error_bios(struct bio *bio)
796{
797 struct bio *n;
798
799 while (bio) {
800 n = bio->bi_next;
801 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200802 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 bio = n;
804 }
805}
806
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700807static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800808{
809 if (!s->valid)
810 return;
811
812 if (err == -EIO)
813 DMERR("Invalidating snapshot: Error reading/writing.");
814 else if (err == -ENOMEM)
815 DMERR("Invalidating snapshot: Unable to allocate exception.");
816
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800817 if (s->store.drop_snapshot)
818 s->store.drop_snapshot(&s->store);
819
820 s->valid = 0;
821
Mikulas Patocka72727ba2008-04-24 21:43:11 +0100822 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800823}
824
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100825static void get_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700826{
827 atomic_inc(&pe->ref_count);
828}
829
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100830static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700831{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100832 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700833 struct bio *origin_bios = NULL;
834
835 primary_pe = pe->primary_pe;
836
837 /*
838 * If this pe is involved in a write to the origin and
839 * it is the last sibling to complete then release
840 * the bios for the original write to the origin.
841 */
842 if (primary_pe &&
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100843 atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700844 origin_bios = bio_list_get(&primary_pe->origin_bios);
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100845 free_pending_exception(primary_pe);
846 }
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700847
848 /*
849 * Free the pe if it's not linked to an origin write or if
850 * it's not itself a primary pe.
851 */
852 if (!primary_pe || primary_pe != pe)
853 free_pending_exception(pe);
854
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700855 return origin_bios;
856}
857
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100858static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100860 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700862 struct bio *origin_bios = NULL;
863 struct bio *snapshot_bios = NULL;
864 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800866 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* Read/write error - snapshot is unusable */
868 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700869 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700870 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800874 e = alloc_exception();
875 if (!e) {
876 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700877 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700878 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800879 goto out;
880 }
881 *e = pe->e;
882
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700883 down_write(&s->lock);
884 if (!s->valid) {
885 free_exception(e);
886 error = 1;
887 goto out;
888 }
889
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800890 /*
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100891 * Check for conflicting reads. This is extremely improbable,
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000892 * so msleep(1) is sufficient and there is no need for a wait queue.
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100893 */
894 while (__chunk_is_tracked(s, pe->e.old_chunk))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000895 msleep(1);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100896
897 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800898 * Add a proper exception, and remove the
899 * in-flight exception from the list.
900 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000901 insert_completed_exception(s, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 out:
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700904 remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700905 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700906 origin_bios = put_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700908 up_write(&s->lock);
909
910 /* Submit any pending write bios */
911 if (error)
912 error_bios(snapshot_bios);
913 else
914 flush_bios(snapshot_bios);
915
916 flush_bios(origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919static void commit_callback(void *context, int success)
920{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100921 struct dm_snap_pending_exception *pe = context;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 pending_complete(pe, success);
924}
925
926/*
927 * Called when the copy I/O has finished. kcopyd actually runs
928 * this code so don't block.
929 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700930static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100932 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 struct dm_snapshot *s = pe->snap;
934
935 if (read_err || write_err)
936 pending_complete(pe, 0);
937
938 else
939 /* Update the metadata if we are persistent */
940 s->store.commit_exception(&s->store, &pe->e, commit_callback,
941 pe);
942}
943
944/*
945 * Dispatches the copy operation to kcopyd.
946 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100947static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100950 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct block_device *bdev = s->origin->bdev;
952 sector_t dev_size;
953
954 dev_size = get_dev_size(bdev);
955
956 src.bdev = bdev;
957 src.sector = chunk_to_sector(s, pe->e.old_chunk);
958 src.count = min(s->chunk_size, dev_size - src.sector);
959
960 dest.bdev = s->cow->bdev;
961 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
962 dest.count = src.count;
963
964 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100965 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 &src, 1, &dest, 0, copy_callback, pe);
967}
968
969/*
970 * Looks to see if this snapshot already has a pending exception
971 * for this chunk, otherwise it allocates a new one and inserts
972 * it into the pending table.
973 *
974 * NOTE: a write lock must be held on snap->lock before calling
975 * this.
976 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100977static struct dm_snap_pending_exception *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978__find_pending_exception(struct dm_snapshot *s, struct bio *bio)
979{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100980 struct dm_snap_exception *e;
981 struct dm_snap_pending_exception *pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
983
984 /*
985 * Is there a pending exception for this already ?
986 */
987 e = lookup_exception(&s->pending, chunk);
988 if (e) {
989 /* cast the exception to a pending exception */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100990 pe = container_of(e, struct dm_snap_pending_exception, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800994 /*
995 * Create a new pending exception, we don't want
996 * to hold the lock while we do this.
997 */
998 up_write(&s->lock);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100999 pe = alloc_pending_exception(s);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001000 down_write(&s->lock);
1001
1002 if (!s->valid) {
1003 free_pending_exception(pe);
1004 return NULL;
1005 }
1006
1007 e = lookup_exception(&s->pending, chunk);
1008 if (e) {
1009 free_pending_exception(pe);
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001010 pe = container_of(e, struct dm_snap_pending_exception, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001011 goto out;
1012 }
1013
1014 pe->e.old_chunk = chunk;
1015 bio_list_init(&pe->origin_bios);
1016 bio_list_init(&pe->snapshot_bios);
1017 pe->primary_pe = NULL;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001018 atomic_set(&pe->ref_count, 0);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001019 pe->started = 0;
1020
1021 if (s->store.prepare_exception(&s->store, &pe->e)) {
1022 free_pending_exception(pe);
1023 return NULL;
1024 }
1025
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001026 get_pending_exception(pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001027 insert_exception(&s->pending, &pe->e);
1028
1029 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return pe;
1031}
1032
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001033static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001034 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 bio->bi_bdev = s->cow->bdev;
Milan Brozd74f81f2008-02-08 02:11:27 +00001037 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
1038 (chunk - e->old_chunk)) +
1039 (bio->bi_sector & s->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042static int snapshot_map(struct dm_target *ti, struct bio *bio,
1043 union map_info *map_context)
1044{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001045 struct dm_snap_exception *e;
1046 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001047 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001049 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 chunk = sector_to_chunk(s, bio->bi_sector);
1052
1053 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001054 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001056 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001058 /* FIXME: should only take write lock if we need
1059 * to copy an exception */
1060 down_write(&s->lock);
1061
1062 if (!s->valid) {
1063 r = -EIO;
1064 goto out_unlock;
1065 }
1066
1067 /* If the block is already remapped - use that, else remap it */
1068 e = lookup_exception(&s->complete, chunk);
1069 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001070 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001071 goto out_unlock;
1072 }
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * Write to snapshot - higher level takes care of RW/RO
1076 * flags so we should only get this if we are
1077 * writeable.
1078 */
1079 if (bio_rw(bio) == WRITE) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001080 pe = __find_pending_exception(s, bio);
1081 if (!pe) {
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001082 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001083 r = -EIO;
1084 goto out_unlock;
1085 }
1086
Milan Brozd74f81f2008-02-08 02:11:27 +00001087 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001088 bio_list_add(&pe->snapshot_bios, bio);
1089
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001090 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001091
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001092 if (!pe->started) {
1093 /* this is protected by snap->lock */
1094 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001095 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001096 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001097 goto out;
1098 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001099 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001100 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001101 map_context->ptr = track_chunk(s, chunk);
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001104 out_unlock:
1105 up_write(&s->lock);
1106 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return r;
1108}
1109
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001110static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1111 int error, union map_info *map_context)
1112{
1113 struct dm_snapshot *s = ti->private;
1114 struct dm_snap_tracked_chunk *c = map_context->ptr;
1115
1116 if (c)
1117 stop_tracking_chunk(s, c);
1118
1119 return 0;
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122static void snapshot_resume(struct dm_target *ti)
1123{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001124 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001126 down_write(&s->lock);
1127 s->active = 1;
1128 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131static int snapshot_status(struct dm_target *ti, status_type_t type,
1132 char *result, unsigned int maxlen)
1133{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001134 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 switch (type) {
1137 case STATUSTYPE_INFO:
1138 if (!snap->valid)
1139 snprintf(result, maxlen, "Invalid");
1140 else {
1141 if (snap->store.fraction_full) {
1142 sector_t numerator, denominator;
1143 snap->store.fraction_full(&snap->store,
1144 &numerator,
1145 &denominator);
Andrew Morton4ee218c2006-03-27 01:17:48 -08001146 snprintf(result, maxlen, "%llu/%llu",
1147 (unsigned long long)numerator,
1148 (unsigned long long)denominator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150 else
1151 snprintf(result, maxlen, "Unknown");
1152 }
1153 break;
1154
1155 case STATUSTYPE_TABLE:
1156 /*
1157 * kdevname returns a static pointer so we need
1158 * to make private copies if the output is to
1159 * make sense.
1160 */
Andrew Morton4ee218c2006-03-27 01:17:48 -08001161 snprintf(result, maxlen, "%s %s %c %llu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 snap->origin->name, snap->cow->name,
Andrew Morton4ee218c2006-03-27 01:17:48 -08001163 snap->type,
1164 (unsigned long long)snap->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 break;
1166 }
1167
1168 return 0;
1169}
1170
1171/*-----------------------------------------------------------------
1172 * Origin methods
1173 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174static int __origin_write(struct list_head *snapshots, struct bio *bio)
1175{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001176 int r = DM_MAPIO_REMAPPED, first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 struct dm_snapshot *snap;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001178 struct dm_snap_exception *e;
1179 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 chunk_t chunk;
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001181 LIST_HEAD(pe_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* Do all the snapshots on this origin */
1184 list_for_each_entry (snap, snapshots, list) {
1185
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001186 down_write(&snap->lock);
1187
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001188 /* Only deal with valid and active snapshots */
1189 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001190 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001192 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka72727ba2008-04-24 21:43:11 +01001193 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001194 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 /*
1197 * Remember, different snapshots can have
1198 * different chunk sizes.
1199 */
1200 chunk = sector_to_chunk(snap, bio->bi_sector);
1201
1202 /*
1203 * Check exception table to see if block
1204 * is already remapped in this snapshot
1205 * and trigger an exception if not.
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001206 *
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001207 * ref_count is initialised to 1 so pending_complete()
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001208 * won't destroy the primary_pe while we're inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
1210 e = lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001211 if (e)
1212 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001214 pe = __find_pending_exception(snap, bio);
1215 if (!pe) {
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001216 __invalidate_snapshot(snap, -ENOMEM);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001217 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001220 if (!primary_pe) {
1221 /*
1222 * Either every pe here has same
1223 * primary_pe or none has one yet.
1224 */
1225 if (pe->primary_pe)
1226 primary_pe = pe->primary_pe;
1227 else {
1228 primary_pe = pe;
1229 first = 1;
1230 }
1231
1232 bio_list_add(&primary_pe->origin_bios, bio);
1233
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001234 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001235 }
1236
1237 if (!pe->primary_pe) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001238 pe->primary_pe = primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001239 get_pending_exception(primary_pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001240 }
1241
1242 if (!pe->started) {
1243 pe->started = 1;
1244 list_add_tail(&pe->list, &pe_queue);
1245 }
1246
1247 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 up_write(&snap->lock);
1249 }
1250
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001251 if (!primary_pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001252 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001253
1254 /*
1255 * If this is the first time we're processing this chunk and
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001256 * ref_count is now 1 it means all the pending exceptions
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001257 * got completed while we were in the loop above, so it falls to
1258 * us here to remove the primary_pe and submit any origin_bios.
1259 */
1260
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001261 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001262 flush_bios(bio_list_get(&primary_pe->origin_bios));
1263 free_pending_exception(primary_pe);
1264 /* If we got here, pe_queue is necessarily empty. */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001265 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001266 }
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /*
1269 * Now that we have a complete pe list we can start the copying.
1270 */
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001271 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1272 start_copy(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 return r;
1275}
1276
1277/*
1278 * Called on a write from the origin driver.
1279 */
1280static int do_origin(struct dm_dev *origin, struct bio *bio)
1281{
1282 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001283 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 down_read(&_origins_lock);
1286 o = __lookup_origin(origin->bdev);
1287 if (o)
1288 r = __origin_write(&o->snapshots, bio);
1289 up_read(&_origins_lock);
1290
1291 return r;
1292}
1293
1294/*
1295 * Origin: maps a linear range of a device, with hooks for snapshotting.
1296 */
1297
1298/*
1299 * Construct an origin mapping: <dev_path>
1300 * The context for an origin is merely a 'struct dm_dev *'
1301 * pointing to the real device.
1302 */
1303static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1304{
1305 int r;
1306 struct dm_dev *dev;
1307
1308 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001309 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EINVAL;
1311 }
1312
1313 r = dm_get_device(ti, argv[0], 0, ti->len,
1314 dm_table_get_mode(ti->table), &dev);
1315 if (r) {
1316 ti->error = "Cannot get target device";
1317 return r;
1318 }
1319
1320 ti->private = dev;
1321 return 0;
1322}
1323
1324static void origin_dtr(struct dm_target *ti)
1325{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001326 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 dm_put_device(ti, dev);
1328}
1329
1330static int origin_map(struct dm_target *ti, struct bio *bio,
1331 union map_info *map_context)
1332{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001333 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 bio->bi_bdev = dev->bdev;
1335
1336 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001337 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1341
1342/*
1343 * Set the target "split_io" field to the minimum of all the snapshots'
1344 * chunk sizes.
1345 */
1346static void origin_resume(struct dm_target *ti)
1347{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001348 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 struct dm_snapshot *snap;
1350 struct origin *o;
1351 chunk_t chunk_size = 0;
1352
1353 down_read(&_origins_lock);
1354 o = __lookup_origin(dev->bdev);
1355 if (o)
1356 list_for_each_entry (snap, &o->snapshots, list)
1357 chunk_size = min_not_zero(chunk_size, snap->chunk_size);
1358 up_read(&_origins_lock);
1359
1360 ti->split_io = chunk_size;
1361}
1362
1363static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1364 unsigned int maxlen)
1365{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001366 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 switch (type) {
1369 case STATUSTYPE_INFO:
1370 result[0] = '\0';
1371 break;
1372
1373 case STATUSTYPE_TABLE:
1374 snprintf(result, maxlen, "%s", dev->name);
1375 break;
1376 }
1377
1378 return 0;
1379}
1380
1381static struct target_type origin_target = {
1382 .name = "snapshot-origin",
Milan Brozd74f81f2008-02-08 02:11:27 +00001383 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 .module = THIS_MODULE,
1385 .ctr = origin_ctr,
1386 .dtr = origin_dtr,
1387 .map = origin_map,
1388 .resume = origin_resume,
1389 .status = origin_status,
1390};
1391
1392static struct target_type snapshot_target = {
1393 .name = "snapshot",
Milan Brozd74f81f2008-02-08 02:11:27 +00001394 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 .module = THIS_MODULE,
1396 .ctr = snapshot_ctr,
1397 .dtr = snapshot_dtr,
1398 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001399 .end_io = snapshot_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 .resume = snapshot_resume,
1401 .status = snapshot_status,
1402};
1403
1404static int __init dm_snapshot_init(void)
1405{
1406 int r;
1407
1408 r = dm_register_target(&snapshot_target);
1409 if (r) {
1410 DMERR("snapshot target register failed %d", r);
1411 return r;
1412 }
1413
1414 r = dm_register_target(&origin_target);
1415 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001416 DMERR("Origin target register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto bad1;
1418 }
1419
1420 r = init_origin_hash();
1421 if (r) {
1422 DMERR("init_origin_hash failed.");
1423 goto bad2;
1424 }
1425
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001426 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (!exception_cache) {
1428 DMERR("Couldn't create exception cache.");
1429 r = -ENOMEM;
1430 goto bad3;
1431 }
1432
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001433 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!pending_cache) {
1435 DMERR("Couldn't create pending cache.");
1436 r = -ENOMEM;
1437 goto bad4;
1438 }
1439
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001440 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1441 if (!tracked_chunk_cache) {
1442 DMERR("Couldn't create cache to track chunks in use.");
1443 r = -ENOMEM;
1444 goto bad5;
1445 }
1446
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001447 ksnapd = create_singlethread_workqueue("ksnapd");
1448 if (!ksnapd) {
1449 DMERR("Failed to create ksnapd workqueue.");
1450 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001451 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001452 }
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001456 bad_pending_pool:
1457 kmem_cache_destroy(tracked_chunk_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 bad5:
1459 kmem_cache_destroy(pending_cache);
1460 bad4:
1461 kmem_cache_destroy(exception_cache);
1462 bad3:
1463 exit_origin_hash();
1464 bad2:
1465 dm_unregister_target(&origin_target);
1466 bad1:
1467 dm_unregister_target(&snapshot_target);
1468 return r;
1469}
1470
1471static void __exit dm_snapshot_exit(void)
1472{
1473 int r;
1474
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001475 destroy_workqueue(ksnapd);
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 r = dm_unregister_target(&snapshot_target);
1478 if (r)
1479 DMERR("snapshot unregister failed %d", r);
1480
1481 r = dm_unregister_target(&origin_target);
1482 if (r)
1483 DMERR("origin unregister failed %d", r);
1484
1485 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 kmem_cache_destroy(pending_cache);
1487 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001488 kmem_cache_destroy(tracked_chunk_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491/* Module hooks */
1492module_init(dm_snapshot_init);
1493module_exit(dm_snapshot_exit);
1494
1495MODULE_DESCRIPTION(DM_NAME " snapshot target");
1496MODULE_AUTHOR("Joe Thornber");
1497MODULE_LICENSE("GPL");