blob: b2d9d1ac28adb4554537fd3568ae4fe52e748a41 [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>
12#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "dm-snap.h"
24#include "dm-bio-list.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
Adrian Bunkc642f9e2006-12-08 02:41:13 -080048static struct workqueue_struct *ksnapd;
David Howellsc4028952006-11-22 14:57:56 +000049static void flush_queued_bios(struct work_struct *work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070050
Alasdair G Kergon028867a2007-07-12 17:26:32 +010051struct dm_snap_pending_exception {
52 struct dm_snap_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /*
55 * Origin buffers waiting for this to complete are held
56 * in a bio list
57 */
58 struct bio_list origin_bios;
59 struct bio_list snapshot_bios;
60
61 /*
Alasdair G Kergoneccf0812006-03-27 01:17:42 -080062 * Short-term queue of pending exceptions prior to submission.
63 */
64 struct list_head list;
65
66 /*
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080067 * The primary pending_exception is the one that holds
Alasdair G Kergon4b832e82006-10-03 01:15:30 -070068 * the ref_count and the list of origin_bios for a
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080069 * group of pending_exceptions. It is always last to get freed.
70 * These fields get set up when writing to the origin.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010072 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080073
74 /*
75 * Number of pending_exceptions processing this chunk.
76 * When this drops to zero we must complete the origin bios.
77 * If incrementing or decrementing this, hold pe->snap->lock for
78 * the sibling concerned and not pe->primary_pe->snap->lock unless
79 * they are the same.
80 */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -070081 atomic_t ref_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* Pointer back to snapshot context */
84 struct dm_snapshot *snap;
85
86 /*
87 * 1 indicates the exception has already been sent to
88 * kcopyd.
89 */
90 int started;
91};
92
93/*
94 * Hash table mapping origin volumes to lists of snapshots and
95 * a lock to protect it
96 */
Christoph Lametere18b8902006-12-06 20:33:20 -080097static struct kmem_cache *exception_cache;
98static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100100struct dm_snap_tracked_chunk {
101 struct hlist_node node;
102 chunk_t chunk;
103};
104
105static struct kmem_cache *tracked_chunk_cache;
106
107static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
108 chunk_t chunk)
109{
110 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
111 GFP_NOIO);
112 unsigned long flags;
113
114 c->chunk = chunk;
115
116 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
117 hlist_add_head(&c->node,
118 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
119 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
120
121 return c;
122}
123
124static void stop_tracking_chunk(struct dm_snapshot *s,
125 struct dm_snap_tracked_chunk *c)
126{
127 unsigned long flags;
128
129 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
130 hlist_del(&c->node);
131 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
132
133 mempool_free(c, s->tracked_chunk_pool);
134}
135
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100136static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
137{
138 struct dm_snap_tracked_chunk *c;
139 struct hlist_node *hn;
140 int found = 0;
141
142 spin_lock_irq(&s->tracked_chunk_lock);
143
144 hlist_for_each_entry(c, hn,
145 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
146 if (c->chunk == chunk) {
147 found = 1;
148 break;
149 }
150 }
151
152 spin_unlock_irq(&s->tracked_chunk_lock);
153
154 return found;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
158 * One of these per registered origin, held in the snapshot_origins hash
159 */
160struct origin {
161 /* The origin device */
162 struct block_device *bdev;
163
164 struct list_head hash_list;
165
166 /* List of snapshots for this origin */
167 struct list_head snapshots;
168};
169
170/*
171 * Size of the hash table for origin volumes. If we make this
172 * the size of the minors list then it should be nearly perfect
173 */
174#define ORIGIN_HASH_SIZE 256
175#define ORIGIN_MASK 0xFF
176static struct list_head *_origins;
177static struct rw_semaphore _origins_lock;
178
179static int init_origin_hash(void)
180{
181 int i;
182
183 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
184 GFP_KERNEL);
185 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700186 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -ENOMEM;
188 }
189
190 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
191 INIT_LIST_HEAD(_origins + i);
192 init_rwsem(&_origins_lock);
193
194 return 0;
195}
196
197static void exit_origin_hash(void)
198{
199 kfree(_origins);
200}
201
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100202static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 return bdev->bd_dev & ORIGIN_MASK;
205}
206
207static struct origin *__lookup_origin(struct block_device *origin)
208{
209 struct list_head *ol;
210 struct origin *o;
211
212 ol = &_origins[origin_hash(origin)];
213 list_for_each_entry (o, ol, hash_list)
214 if (bdev_equal(o->bdev, origin))
215 return o;
216
217 return NULL;
218}
219
220static void __insert_origin(struct origin *o)
221{
222 struct list_head *sl = &_origins[origin_hash(o->bdev)];
223 list_add_tail(&o->hash_list, sl);
224}
225
226/*
227 * Make a note of the snapshot and its origin so we can look it
228 * up when the origin has a write on it.
229 */
230static int register_snapshot(struct dm_snapshot *snap)
231{
232 struct origin *o;
233 struct block_device *bdev = snap->origin->bdev;
234
235 down_write(&_origins_lock);
236 o = __lookup_origin(bdev);
237
238 if (!o) {
239 /* New origin */
240 o = kmalloc(sizeof(*o), GFP_KERNEL);
241 if (!o) {
242 up_write(&_origins_lock);
243 return -ENOMEM;
244 }
245
246 /* Initialise the struct */
247 INIT_LIST_HEAD(&o->snapshots);
248 o->bdev = bdev;
249
250 __insert_origin(o);
251 }
252
253 list_add_tail(&snap->list, &o->snapshots);
254
255 up_write(&_origins_lock);
256 return 0;
257}
258
259static void unregister_snapshot(struct dm_snapshot *s)
260{
261 struct origin *o;
262
263 down_write(&_origins_lock);
264 o = __lookup_origin(s->origin->bdev);
265
266 list_del(&s->list);
267 if (list_empty(&o->snapshots)) {
268 list_del(&o->hash_list);
269 kfree(o);
270 }
271
272 up_write(&_origins_lock);
273}
274
275/*
276 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000277 * The lowest hash_shift bits of the chunk number are ignored, allowing
278 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000280static int init_exception_table(struct exception_table *et, uint32_t size,
281 unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 unsigned int i;
284
Milan Brozd74f81f2008-02-08 02:11:27 +0000285 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 et->hash_mask = size - 1;
287 et->table = dm_vcalloc(size, sizeof(struct list_head));
288 if (!et->table)
289 return -ENOMEM;
290
291 for (i = 0; i < size; i++)
292 INIT_LIST_HEAD(et->table + i);
293
294 return 0;
295}
296
Christoph Lametere18b8902006-12-06 20:33:20 -0800297static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100300 struct dm_snap_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int i, size;
302
303 size = et->hash_mask + 1;
304 for (i = 0; i < size; i++) {
305 slot = et->table + i;
306
307 list_for_each_entry_safe (ex, next, slot, hash_list)
308 kmem_cache_free(mem, ex);
309 }
310
311 vfree(et->table);
312}
313
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100314static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Milan Brozd74f81f2008-02-08 02:11:27 +0000316 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100319static void insert_exception(struct exception_table *eh,
320 struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
323 list_add(&e->hash_list, l);
324}
325
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100326static void remove_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 list_del(&e->hash_list);
329}
330
331/*
332 * Return the exception data for a sector, or NULL if not
333 * remapped.
334 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100335static struct dm_snap_exception *lookup_exception(struct exception_table *et,
336 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100339 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 slot = &et->table[exception_hash(et, chunk)];
342 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000343 if (chunk >= e->old_chunk &&
344 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return e;
346
347 return NULL;
348}
349
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100350static struct dm_snap_exception *alloc_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100352 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
355 if (!e)
356 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
357
358 return e;
359}
360
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100361static void free_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 kmem_cache_free(exception_cache, e);
364}
365
Mikulas Patocka92e86812008-07-21 12:00:35 +0100366static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100368 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
369 GFP_NOIO);
370
371 pe->snap = s;
372
373 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100376static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100378 mempool_free(pe, pe->snap->pending_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Milan Brozd74f81f2008-02-08 02:11:27 +0000381static void insert_completed_exception(struct dm_snapshot *s,
382 struct dm_snap_exception *new_e)
383{
384 struct exception_table *eh = &s->complete;
385 struct list_head *l;
386 struct dm_snap_exception *e = NULL;
387
388 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
389
390 /* Add immediately if this table doesn't support consecutive chunks */
391 if (!eh->hash_shift)
392 goto out;
393
394 /* List is ordered by old_chunk */
395 list_for_each_entry_reverse(e, l, hash_list) {
396 /* Insert after an existing chunk? */
397 if (new_e->old_chunk == (e->old_chunk +
398 dm_consecutive_chunk_count(e) + 1) &&
399 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
400 dm_consecutive_chunk_count(e) + 1)) {
401 dm_consecutive_chunk_count_inc(e);
402 free_exception(new_e);
403 return;
404 }
405
406 /* Insert before an existing chunk? */
407 if (new_e->old_chunk == (e->old_chunk - 1) &&
408 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
409 dm_consecutive_chunk_count_inc(e);
410 e->old_chunk--;
411 e->new_chunk--;
412 free_exception(new_e);
413 return;
414 }
415
416 if (new_e->old_chunk > e->old_chunk)
417 break;
418 }
419
420out:
421 list_add(&new_e->hash_list, e ? &e->hash_list : l);
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
425{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100426 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 e = alloc_exception();
429 if (!e)
430 return -ENOMEM;
431
432 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000433
434 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000436
437 insert_completed_exception(s, e);
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440}
441
442/*
443 * Hard coded magic.
444 */
445static int calc_max_buckets(void)
446{
447 /* use a fixed size of 2MB */
448 unsigned long mem = 2 * 1024 * 1024;
449 mem /= sizeof(struct list_head);
450
451 return mem;
452}
453
454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * Allocate room for a suitable hash table.
456 */
457static int init_hash_tables(struct dm_snapshot *s)
458{
459 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
460
461 /*
462 * Calculate based on the size of the original volume or
463 * the COW volume...
464 */
465 cow_dev_size = get_dev_size(s->cow->bdev);
466 origin_dev_size = get_dev_size(s->origin->bdev);
467 max_buckets = calc_max_buckets();
468
469 hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
470 hash_size = min(hash_size, max_buckets);
471
Robert P. J. Day8defd832008-02-08 02:10:06 +0000472 hash_size = rounddown_pow_of_two(hash_size);
Milan Brozd74f81f2008-02-08 02:11:27 +0000473 if (init_exception_table(&s->complete, hash_size,
474 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -ENOMEM;
476
477 /*
478 * Allocate hash table for in-flight exceptions
479 * Make this smaller than the real hash table
480 */
481 hash_size >>= 3;
482 if (hash_size < 64)
483 hash_size = 64;
484
Milan Brozd74f81f2008-02-08 02:11:27 +0000485 if (init_exception_table(&s->pending, hash_size, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 exit_exception_table(&s->complete, exception_cache);
487 return -ENOMEM;
488 }
489
490 return 0;
491}
492
493/*
494 * Round a number up to the nearest 'size' boundary. size must
495 * be a power of 2.
496 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100497static ulong round_up(ulong n, ulong size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 size--;
500 return (n + size) & ~size;
501}
502
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700503static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
504 char **error)
505{
506 unsigned long chunk_size;
507 char *value;
508
509 chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
510 if (*chunk_size_arg == '\0' || *value != '\0') {
511 *error = "Invalid chunk size";
512 return -EINVAL;
513 }
514
515 if (!chunk_size) {
516 s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
517 return 0;
518 }
519
520 /*
521 * Chunk size must be multiple of page size. Silently
522 * round up if it's not.
523 */
524 chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
525
526 /* Check chunk_size is a power of 2 */
vignesh babu6f3c3f02007-10-19 22:38:44 +0100527 if (!is_power_of_2(chunk_size)) {
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700528 *error = "Chunk size is not a power of 2";
529 return -EINVAL;
530 }
531
532 /* Validate the chunk size against the device block size */
533 if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
534 *error = "Chunk size is not a multiple of device blocksize";
535 return -EINVAL;
536 }
537
538 s->chunk_size = chunk_size;
539 s->chunk_mask = chunk_size - 1;
540 s->chunk_shift = ffs(chunk_size) - 1;
541
542 return 0;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/*
546 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
547 */
548static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
549{
550 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100551 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int r = -EINVAL;
553 char persistent;
554 char *origin_path;
555 char *cow_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700557 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700558 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 r = -EINVAL;
560 goto bad1;
561 }
562
563 origin_path = argv[0];
564 cow_path = argv[1];
565 persistent = toupper(*argv[2]);
566
567 if (persistent != 'P' && persistent != 'N') {
568 ti->error = "Persistent flag is not P or N";
569 r = -EINVAL;
570 goto bad1;
571 }
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 s = kmalloc(sizeof(*s), GFP_KERNEL);
574 if (s == NULL) {
575 ti->error = "Cannot allocate snapshot context private "
576 "structure";
577 r = -ENOMEM;
578 goto bad1;
579 }
580
581 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
582 if (r) {
583 ti->error = "Cannot get origin device";
584 goto bad2;
585 }
586
587 r = dm_get_device(ti, cow_path, 0, 0,
588 FMODE_READ | FMODE_WRITE, &s->cow);
589 if (r) {
590 dm_put_device(ti, s->origin);
591 ti->error = "Cannot get COW device";
592 goto bad2;
593 }
594
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700595 r = set_chunk_size(s, argv[3], &ti->error);
596 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto bad3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 s->type = persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800602 s->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 init_rwsem(&s->lock);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700604 spin_lock_init(&s->pe_lock);
Mikulas Patocka72727ba2008-04-24 21:43:11 +0100605 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* Allocate hash table for COW data */
608 if (init_hash_tables(s)) {
609 ti->error = "Unable to allocate hash table space";
610 r = -ENOMEM;
611 goto bad3;
612 }
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 s->store.snap = s;
615
616 if (persistent == 'P')
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700617 r = dm_create_persistent(&s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 else
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700619 r = dm_create_transient(&s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (r) {
622 ti->error = "Couldn't create exception store";
623 r = -EINVAL;
624 goto bad4;
625 }
626
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100627 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (r) {
629 ti->error = "Could not create kcopyd client";
630 goto bad5;
631 }
632
Mikulas Patocka92e86812008-07-21 12:00:35 +0100633 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
634 if (!s->pending_pool) {
635 ti->error = "Could not allocate mempool for pending exceptions";
636 goto bad6;
637 }
638
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100639 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
640 tracked_chunk_cache);
641 if (!s->tracked_chunk_pool) {
642 ti->error = "Could not allocate tracked_chunk mempool for "
643 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +0100644 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100645 }
646
647 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
648 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
649
650 spin_lock_init(&s->tracked_chunk_lock);
651
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800652 /* Metadata must only be loaded into one table at once */
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700653 r = s->store.read_metadata(&s->store);
Milan Broz07641472007-07-12 17:28:13 +0100654 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700655 ti->error = "Failed to read snapshot metadata";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100656 goto bad_load_and_register;
Milan Broz07641472007-07-12 17:28:13 +0100657 } else if (r > 0) {
658 s->valid = 0;
659 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700660 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800661
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700662 bio_list_init(&s->queued_bios);
David Howellsc4028952006-11-22 14:57:56 +0000663 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Add snapshot to the list of snapshots for this origin */
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800666 /* Exceptions aren't triggered till snapshot_resume() is called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (register_snapshot(s)) {
668 r = -EINVAL;
669 ti->error = "Cannot register snapshot origin";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100670 goto bad_load_and_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 ti->private = s;
Alasdair G Kergonc51c2752006-06-26 00:27:18 -0700674 ti->split_io = s->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 return 0;
677
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100678 bad_load_and_register:
679 mempool_destroy(s->tracked_chunk_pool);
680
Mikulas Patocka92e86812008-07-21 12:00:35 +0100681 bad_tracked_chunk_pool:
682 mempool_destroy(s->pending_pool);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 bad6:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100685 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 bad5:
688 s->store.destroy(&s->store);
689
690 bad4:
691 exit_exception_table(&s->pending, pending_cache);
692 exit_exception_table(&s->complete, exception_cache);
693
694 bad3:
695 dm_put_device(ti, s->cow);
696 dm_put_device(ti, s->origin);
697
698 bad2:
699 kfree(s);
700
701 bad1:
702 return r;
703}
704
Milan Broz31c93a02006-12-08 02:41:11 -0800705static void __free_exceptions(struct dm_snapshot *s)
706{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100707 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -0800708 s->kcopyd_client = NULL;
709
710 exit_exception_table(&s->pending, pending_cache);
711 exit_exception_table(&s->complete, exception_cache);
712
713 s->store.destroy(&s->store);
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static void snapshot_dtr(struct dm_target *ti)
717{
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100718#ifdef CONFIG_DM_DEBUG
719 int i;
720#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100721 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700723 flush_workqueue(ksnapd);
724
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800725 /* Prevent further origin writes from using this snapshot. */
726 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 unregister_snapshot(s);
728
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100729#ifdef CONFIG_DM_DEBUG
730 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
731 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
732#endif
733
734 mempool_destroy(s->tracked_chunk_pool);
735
Milan Broz31c93a02006-12-08 02:41:11 -0800736 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Mikulas Patocka92e86812008-07-21 12:00:35 +0100738 mempool_destroy(s->pending_pool);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 dm_put_device(ti, s->origin);
741 dm_put_device(ti, s->cow);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 kfree(s);
744}
745
746/*
747 * Flush a list of buffers.
748 */
749static void flush_bios(struct bio *bio)
750{
751 struct bio *n;
752
753 while (bio) {
754 n = bio->bi_next;
755 bio->bi_next = NULL;
756 generic_make_request(bio);
757 bio = n;
758 }
759}
760
David Howellsc4028952006-11-22 14:57:56 +0000761static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700762{
David Howellsc4028952006-11-22 14:57:56 +0000763 struct dm_snapshot *s =
764 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700765 struct bio *queued_bios;
766 unsigned long flags;
767
768 spin_lock_irqsave(&s->pe_lock, flags);
769 queued_bios = bio_list_get(&s->queued_bios);
770 spin_unlock_irqrestore(&s->pe_lock, flags);
771
772 flush_bios(queued_bios);
773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/*
776 * Error a list of buffers.
777 */
778static void error_bios(struct bio *bio)
779{
780 struct bio *n;
781
782 while (bio) {
783 n = bio->bi_next;
784 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200785 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 bio = n;
787 }
788}
789
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700790static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800791{
792 if (!s->valid)
793 return;
794
795 if (err == -EIO)
796 DMERR("Invalidating snapshot: Error reading/writing.");
797 else if (err == -ENOMEM)
798 DMERR("Invalidating snapshot: Unable to allocate exception.");
799
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800800 if (s->store.drop_snapshot)
801 s->store.drop_snapshot(&s->store);
802
803 s->valid = 0;
804
Mikulas Patocka72727ba2008-04-24 21:43:11 +0100805 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800806}
807
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100808static void get_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700809{
810 atomic_inc(&pe->ref_count);
811}
812
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100813static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700814{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100815 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700816 struct bio *origin_bios = NULL;
817
818 primary_pe = pe->primary_pe;
819
820 /*
821 * If this pe is involved in a write to the origin and
822 * it is the last sibling to complete then release
823 * the bios for the original write to the origin.
824 */
825 if (primary_pe &&
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100826 atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700827 origin_bios = bio_list_get(&primary_pe->origin_bios);
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100828 free_pending_exception(primary_pe);
829 }
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700830
831 /*
832 * Free the pe if it's not linked to an origin write or if
833 * it's not itself a primary pe.
834 */
835 if (!primary_pe || primary_pe != pe)
836 free_pending_exception(pe);
837
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700838 return origin_bios;
839}
840
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100841static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100843 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700845 struct bio *origin_bios = NULL;
846 struct bio *snapshot_bios = NULL;
847 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800849 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* Read/write error - snapshot is unusable */
851 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700852 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700853 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800857 e = alloc_exception();
858 if (!e) {
859 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700860 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700861 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800862 goto out;
863 }
864 *e = pe->e;
865
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700866 down_write(&s->lock);
867 if (!s->valid) {
868 free_exception(e);
869 error = 1;
870 goto out;
871 }
872
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800873 /*
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100874 * Check for conflicting reads. This is extremely improbable,
875 * so yield() is sufficient and there is no need for a wait queue.
876 */
877 while (__chunk_is_tracked(s, pe->e.old_chunk))
878 yield();
879
880 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800881 * Add a proper exception, and remove the
882 * in-flight exception from the list.
883 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000884 insert_completed_exception(s, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 out:
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700887 remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700888 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700889 origin_bios = put_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700891 up_write(&s->lock);
892
893 /* Submit any pending write bios */
894 if (error)
895 error_bios(snapshot_bios);
896 else
897 flush_bios(snapshot_bios);
898
899 flush_bios(origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static void commit_callback(void *context, int success)
903{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100904 struct dm_snap_pending_exception *pe = context;
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 pending_complete(pe, success);
907}
908
909/*
910 * Called when the copy I/O has finished. kcopyd actually runs
911 * this code so don't block.
912 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700913static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100915 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct dm_snapshot *s = pe->snap;
917
918 if (read_err || write_err)
919 pending_complete(pe, 0);
920
921 else
922 /* Update the metadata if we are persistent */
923 s->store.commit_exception(&s->store, &pe->e, commit_callback,
924 pe);
925}
926
927/*
928 * Dispatches the copy operation to kcopyd.
929 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100930static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100933 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct block_device *bdev = s->origin->bdev;
935 sector_t dev_size;
936
937 dev_size = get_dev_size(bdev);
938
939 src.bdev = bdev;
940 src.sector = chunk_to_sector(s, pe->e.old_chunk);
941 src.count = min(s->chunk_size, dev_size - src.sector);
942
943 dest.bdev = s->cow->bdev;
944 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
945 dest.count = src.count;
946
947 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100948 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 &src, 1, &dest, 0, copy_callback, pe);
950}
951
952/*
953 * Looks to see if this snapshot already has a pending exception
954 * for this chunk, otherwise it allocates a new one and inserts
955 * it into the pending table.
956 *
957 * NOTE: a write lock must be held on snap->lock before calling
958 * this.
959 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100960static struct dm_snap_pending_exception *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961__find_pending_exception(struct dm_snapshot *s, struct bio *bio)
962{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100963 struct dm_snap_exception *e;
964 struct dm_snap_pending_exception *pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
966
967 /*
968 * Is there a pending exception for this already ?
969 */
970 e = lookup_exception(&s->pending, chunk);
971 if (e) {
972 /* cast the exception to a pending exception */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100973 pe = container_of(e, struct dm_snap_pending_exception, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800974 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800977 /*
978 * Create a new pending exception, we don't want
979 * to hold the lock while we do this.
980 */
981 up_write(&s->lock);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100982 pe = alloc_pending_exception(s);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800983 down_write(&s->lock);
984
985 if (!s->valid) {
986 free_pending_exception(pe);
987 return NULL;
988 }
989
990 e = lookup_exception(&s->pending, chunk);
991 if (e) {
992 free_pending_exception(pe);
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100993 pe = container_of(e, struct dm_snap_pending_exception, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800994 goto out;
995 }
996
997 pe->e.old_chunk = chunk;
998 bio_list_init(&pe->origin_bios);
999 bio_list_init(&pe->snapshot_bios);
1000 pe->primary_pe = NULL;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001001 atomic_set(&pe->ref_count, 0);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001002 pe->started = 0;
1003
1004 if (s->store.prepare_exception(&s->store, &pe->e)) {
1005 free_pending_exception(pe);
1006 return NULL;
1007 }
1008
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001009 get_pending_exception(pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001010 insert_exception(&s->pending, &pe->e);
1011
1012 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return pe;
1014}
1015
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001016static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001017 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 bio->bi_bdev = s->cow->bdev;
Milan Brozd74f81f2008-02-08 02:11:27 +00001020 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
1021 (chunk - e->old_chunk)) +
1022 (bio->bi_sector & s->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025static int snapshot_map(struct dm_target *ti, struct bio *bio,
1026 union map_info *map_context)
1027{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001028 struct dm_snap_exception *e;
1029 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001030 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001032 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 chunk = sector_to_chunk(s, bio->bi_sector);
1035
1036 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001037 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001039 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001041 /* FIXME: should only take write lock if we need
1042 * to copy an exception */
1043 down_write(&s->lock);
1044
1045 if (!s->valid) {
1046 r = -EIO;
1047 goto out_unlock;
1048 }
1049
1050 /* If the block is already remapped - use that, else remap it */
1051 e = lookup_exception(&s->complete, chunk);
1052 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001053 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001054 goto out_unlock;
1055 }
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
1058 * Write to snapshot - higher level takes care of RW/RO
1059 * flags so we should only get this if we are
1060 * writeable.
1061 */
1062 if (bio_rw(bio) == WRITE) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001063 pe = __find_pending_exception(s, bio);
1064 if (!pe) {
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001065 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001066 r = -EIO;
1067 goto out_unlock;
1068 }
1069
Milan Brozd74f81f2008-02-08 02:11:27 +00001070 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001071 bio_list_add(&pe->snapshot_bios, bio);
1072
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001073 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001074
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001075 if (!pe->started) {
1076 /* this is protected by snap->lock */
1077 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001078 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001079 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001080 goto out;
1081 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001082 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001083 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001084 map_context->ptr = track_chunk(s, chunk);
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001087 out_unlock:
1088 up_write(&s->lock);
1089 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return r;
1091}
1092
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001093static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1094 int error, union map_info *map_context)
1095{
1096 struct dm_snapshot *s = ti->private;
1097 struct dm_snap_tracked_chunk *c = map_context->ptr;
1098
1099 if (c)
1100 stop_tracking_chunk(s, c);
1101
1102 return 0;
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105static void snapshot_resume(struct dm_target *ti)
1106{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001107 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001109 down_write(&s->lock);
1110 s->active = 1;
1111 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114static int snapshot_status(struct dm_target *ti, status_type_t type,
1115 char *result, unsigned int maxlen)
1116{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001117 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 switch (type) {
1120 case STATUSTYPE_INFO:
1121 if (!snap->valid)
1122 snprintf(result, maxlen, "Invalid");
1123 else {
1124 if (snap->store.fraction_full) {
1125 sector_t numerator, denominator;
1126 snap->store.fraction_full(&snap->store,
1127 &numerator,
1128 &denominator);
Andrew Morton4ee218c2006-03-27 01:17:48 -08001129 snprintf(result, maxlen, "%llu/%llu",
1130 (unsigned long long)numerator,
1131 (unsigned long long)denominator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 else
1134 snprintf(result, maxlen, "Unknown");
1135 }
1136 break;
1137
1138 case STATUSTYPE_TABLE:
1139 /*
1140 * kdevname returns a static pointer so we need
1141 * to make private copies if the output is to
1142 * make sense.
1143 */
Andrew Morton4ee218c2006-03-27 01:17:48 -08001144 snprintf(result, maxlen, "%s %s %c %llu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 snap->origin->name, snap->cow->name,
Andrew Morton4ee218c2006-03-27 01:17:48 -08001146 snap->type,
1147 (unsigned long long)snap->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 }
1150
1151 return 0;
1152}
1153
1154/*-----------------------------------------------------------------
1155 * Origin methods
1156 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157static int __origin_write(struct list_head *snapshots, struct bio *bio)
1158{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001159 int r = DM_MAPIO_REMAPPED, first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct dm_snapshot *snap;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001161 struct dm_snap_exception *e;
1162 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 chunk_t chunk;
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001164 LIST_HEAD(pe_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /* Do all the snapshots on this origin */
1167 list_for_each_entry (snap, snapshots, list) {
1168
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001169 down_write(&snap->lock);
1170
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001171 /* Only deal with valid and active snapshots */
1172 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001173 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001175 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka72727ba2008-04-24 21:43:11 +01001176 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001177 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /*
1180 * Remember, different snapshots can have
1181 * different chunk sizes.
1182 */
1183 chunk = sector_to_chunk(snap, bio->bi_sector);
1184
1185 /*
1186 * Check exception table to see if block
1187 * is already remapped in this snapshot
1188 * and trigger an exception if not.
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001189 *
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001190 * ref_count is initialised to 1 so pending_complete()
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001191 * won't destroy the primary_pe while we're inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 */
1193 e = lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001194 if (e)
1195 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001197 pe = __find_pending_exception(snap, bio);
1198 if (!pe) {
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001199 __invalidate_snapshot(snap, -ENOMEM);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001200 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001203 if (!primary_pe) {
1204 /*
1205 * Either every pe here has same
1206 * primary_pe or none has one yet.
1207 */
1208 if (pe->primary_pe)
1209 primary_pe = pe->primary_pe;
1210 else {
1211 primary_pe = pe;
1212 first = 1;
1213 }
1214
1215 bio_list_add(&primary_pe->origin_bios, bio);
1216
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001217 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001218 }
1219
1220 if (!pe->primary_pe) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001221 pe->primary_pe = primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001222 get_pending_exception(primary_pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001223 }
1224
1225 if (!pe->started) {
1226 pe->started = 1;
1227 list_add_tail(&pe->list, &pe_queue);
1228 }
1229
1230 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 up_write(&snap->lock);
1232 }
1233
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001234 if (!primary_pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001235 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001236
1237 /*
1238 * If this is the first time we're processing this chunk and
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001239 * ref_count is now 1 it means all the pending exceptions
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001240 * got completed while we were in the loop above, so it falls to
1241 * us here to remove the primary_pe and submit any origin_bios.
1242 */
1243
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001244 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001245 flush_bios(bio_list_get(&primary_pe->origin_bios));
1246 free_pending_exception(primary_pe);
1247 /* If we got here, pe_queue is necessarily empty. */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001248 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001249 }
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * Now that we have a complete pe list we can start the copying.
1253 */
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001254 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1255 start_copy(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 return r;
1258}
1259
1260/*
1261 * Called on a write from the origin driver.
1262 */
1263static int do_origin(struct dm_dev *origin, struct bio *bio)
1264{
1265 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001266 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 down_read(&_origins_lock);
1269 o = __lookup_origin(origin->bdev);
1270 if (o)
1271 r = __origin_write(&o->snapshots, bio);
1272 up_read(&_origins_lock);
1273
1274 return r;
1275}
1276
1277/*
1278 * Origin: maps a linear range of a device, with hooks for snapshotting.
1279 */
1280
1281/*
1282 * Construct an origin mapping: <dev_path>
1283 * The context for an origin is merely a 'struct dm_dev *'
1284 * pointing to the real device.
1285 */
1286static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1287{
1288 int r;
1289 struct dm_dev *dev;
1290
1291 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001292 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return -EINVAL;
1294 }
1295
1296 r = dm_get_device(ti, argv[0], 0, ti->len,
1297 dm_table_get_mode(ti->table), &dev);
1298 if (r) {
1299 ti->error = "Cannot get target device";
1300 return r;
1301 }
1302
1303 ti->private = dev;
1304 return 0;
1305}
1306
1307static void origin_dtr(struct dm_target *ti)
1308{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001309 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 dm_put_device(ti, dev);
1311}
1312
1313static int origin_map(struct dm_target *ti, struct bio *bio,
1314 union map_info *map_context)
1315{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001316 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 bio->bi_bdev = dev->bdev;
1318
1319 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001320 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1324
1325/*
1326 * Set the target "split_io" field to the minimum of all the snapshots'
1327 * chunk sizes.
1328 */
1329static void origin_resume(struct dm_target *ti)
1330{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001331 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct dm_snapshot *snap;
1333 struct origin *o;
1334 chunk_t chunk_size = 0;
1335
1336 down_read(&_origins_lock);
1337 o = __lookup_origin(dev->bdev);
1338 if (o)
1339 list_for_each_entry (snap, &o->snapshots, list)
1340 chunk_size = min_not_zero(chunk_size, snap->chunk_size);
1341 up_read(&_origins_lock);
1342
1343 ti->split_io = chunk_size;
1344}
1345
1346static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1347 unsigned int maxlen)
1348{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001349 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 switch (type) {
1352 case STATUSTYPE_INFO:
1353 result[0] = '\0';
1354 break;
1355
1356 case STATUSTYPE_TABLE:
1357 snprintf(result, maxlen, "%s", dev->name);
1358 break;
1359 }
1360
1361 return 0;
1362}
1363
1364static struct target_type origin_target = {
1365 .name = "snapshot-origin",
Milan Brozd74f81f2008-02-08 02:11:27 +00001366 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 .module = THIS_MODULE,
1368 .ctr = origin_ctr,
1369 .dtr = origin_dtr,
1370 .map = origin_map,
1371 .resume = origin_resume,
1372 .status = origin_status,
1373};
1374
1375static struct target_type snapshot_target = {
1376 .name = "snapshot",
Milan Brozd74f81f2008-02-08 02:11:27 +00001377 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 .module = THIS_MODULE,
1379 .ctr = snapshot_ctr,
1380 .dtr = snapshot_dtr,
1381 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001382 .end_io = snapshot_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 .resume = snapshot_resume,
1384 .status = snapshot_status,
1385};
1386
1387static int __init dm_snapshot_init(void)
1388{
1389 int r;
1390
1391 r = dm_register_target(&snapshot_target);
1392 if (r) {
1393 DMERR("snapshot target register failed %d", r);
1394 return r;
1395 }
1396
1397 r = dm_register_target(&origin_target);
1398 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001399 DMERR("Origin target register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto bad1;
1401 }
1402
1403 r = init_origin_hash();
1404 if (r) {
1405 DMERR("init_origin_hash failed.");
1406 goto bad2;
1407 }
1408
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001409 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (!exception_cache) {
1411 DMERR("Couldn't create exception cache.");
1412 r = -ENOMEM;
1413 goto bad3;
1414 }
1415
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001416 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (!pending_cache) {
1418 DMERR("Couldn't create pending cache.");
1419 r = -ENOMEM;
1420 goto bad4;
1421 }
1422
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001423 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1424 if (!tracked_chunk_cache) {
1425 DMERR("Couldn't create cache to track chunks in use.");
1426 r = -ENOMEM;
1427 goto bad5;
1428 }
1429
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001430 ksnapd = create_singlethread_workqueue("ksnapd");
1431 if (!ksnapd) {
1432 DMERR("Failed to create ksnapd workqueue.");
1433 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001434 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001435 }
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return 0;
1438
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001439 bad_pending_pool:
1440 kmem_cache_destroy(tracked_chunk_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 bad5:
1442 kmem_cache_destroy(pending_cache);
1443 bad4:
1444 kmem_cache_destroy(exception_cache);
1445 bad3:
1446 exit_origin_hash();
1447 bad2:
1448 dm_unregister_target(&origin_target);
1449 bad1:
1450 dm_unregister_target(&snapshot_target);
1451 return r;
1452}
1453
1454static void __exit dm_snapshot_exit(void)
1455{
1456 int r;
1457
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001458 destroy_workqueue(ksnapd);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 r = dm_unregister_target(&snapshot_target);
1461 if (r)
1462 DMERR("snapshot unregister failed %d", r);
1463
1464 r = dm_unregister_target(&origin_target);
1465 if (r)
1466 DMERR("origin unregister failed %d", r);
1467
1468 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 kmem_cache_destroy(pending_cache);
1470 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001471 kmem_cache_destroy(tracked_chunk_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
1474/* Module hooks */
1475module_init(dm_snapshot_init);
1476module_exit(dm_snapshot_exit);
1477
1478MODULE_DESCRIPTION(DM_NAME " snapshot target");
1479MODULE_AUTHOR("Joe Thornber");
1480MODULE_LICENSE("GPL");