blob: f4fd0cee9c3d43f1c06ec288af0d7476bf4f4538 [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 -070099static mempool_t *pending_pool;
100
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{
233 struct origin *o;
234 struct block_device *bdev = snap->origin->bdev;
235
236 down_write(&_origins_lock);
237 o = __lookup_origin(bdev);
238
239 if (!o) {
240 /* New origin */
241 o = kmalloc(sizeof(*o), GFP_KERNEL);
242 if (!o) {
243 up_write(&_origins_lock);
244 return -ENOMEM;
245 }
246
247 /* Initialise the struct */
248 INIT_LIST_HEAD(&o->snapshots);
249 o->bdev = bdev;
250
251 __insert_origin(o);
252 }
253
254 list_add_tail(&snap->list, &o->snapshots);
255
256 up_write(&_origins_lock);
257 return 0;
258}
259
260static void unregister_snapshot(struct dm_snapshot *s)
261{
262 struct origin *o;
263
264 down_write(&_origins_lock);
265 o = __lookup_origin(s->origin->bdev);
266
267 list_del(&s->list);
268 if (list_empty(&o->snapshots)) {
269 list_del(&o->hash_list);
270 kfree(o);
271 }
272
273 up_write(&_origins_lock);
274}
275
276/*
277 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000278 * The lowest hash_shift bits of the chunk number are ignored, allowing
279 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000281static int init_exception_table(struct exception_table *et, uint32_t size,
282 unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 unsigned int i;
285
Milan Brozd74f81f2008-02-08 02:11:27 +0000286 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 et->hash_mask = size - 1;
288 et->table = dm_vcalloc(size, sizeof(struct list_head));
289 if (!et->table)
290 return -ENOMEM;
291
292 for (i = 0; i < size; i++)
293 INIT_LIST_HEAD(et->table + i);
294
295 return 0;
296}
297
Christoph Lametere18b8902006-12-06 20:33:20 -0800298static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100301 struct dm_snap_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 int i, size;
303
304 size = et->hash_mask + 1;
305 for (i = 0; i < size; i++) {
306 slot = et->table + i;
307
308 list_for_each_entry_safe (ex, next, slot, hash_list)
309 kmem_cache_free(mem, ex);
310 }
311
312 vfree(et->table);
313}
314
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100315static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Milan Brozd74f81f2008-02-08 02:11:27 +0000317 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100320static void insert_exception(struct exception_table *eh,
321 struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
324 list_add(&e->hash_list, l);
325}
326
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100327static void remove_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 list_del(&e->hash_list);
330}
331
332/*
333 * Return the exception data for a sector, or NULL if not
334 * remapped.
335 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100336static struct dm_snap_exception *lookup_exception(struct exception_table *et,
337 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100340 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 slot = &et->table[exception_hash(et, chunk)];
343 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000344 if (chunk >= e->old_chunk &&
345 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return e;
347
348 return NULL;
349}
350
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100351static struct dm_snap_exception *alloc_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100353 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
356 if (!e)
357 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
358
359 return e;
360}
361
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100362static void free_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 kmem_cache_free(exception_cache, e);
365}
366
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100367static struct dm_snap_pending_exception *alloc_pending_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 return mempool_alloc(pending_pool, GFP_NOIO);
370}
371
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100372static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 mempool_free(pe, pending_pool);
375}
376
Milan Brozd74f81f2008-02-08 02:11:27 +0000377static void insert_completed_exception(struct dm_snapshot *s,
378 struct dm_snap_exception *new_e)
379{
380 struct exception_table *eh = &s->complete;
381 struct list_head *l;
382 struct dm_snap_exception *e = NULL;
383
384 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
385
386 /* Add immediately if this table doesn't support consecutive chunks */
387 if (!eh->hash_shift)
388 goto out;
389
390 /* List is ordered by old_chunk */
391 list_for_each_entry_reverse(e, l, hash_list) {
392 /* Insert after an existing chunk? */
393 if (new_e->old_chunk == (e->old_chunk +
394 dm_consecutive_chunk_count(e) + 1) &&
395 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
396 dm_consecutive_chunk_count(e) + 1)) {
397 dm_consecutive_chunk_count_inc(e);
398 free_exception(new_e);
399 return;
400 }
401
402 /* Insert before an existing chunk? */
403 if (new_e->old_chunk == (e->old_chunk - 1) &&
404 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
405 dm_consecutive_chunk_count_inc(e);
406 e->old_chunk--;
407 e->new_chunk--;
408 free_exception(new_e);
409 return;
410 }
411
412 if (new_e->old_chunk > e->old_chunk)
413 break;
414 }
415
416out:
417 list_add(&new_e->hash_list, e ? &e->hash_list : l);
418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
421{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100422 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 e = alloc_exception();
425 if (!e)
426 return -ENOMEM;
427
428 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000429
430 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000432
433 insert_completed_exception(s, e);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 0;
436}
437
438/*
439 * Hard coded magic.
440 */
441static int calc_max_buckets(void)
442{
443 /* use a fixed size of 2MB */
444 unsigned long mem = 2 * 1024 * 1024;
445 mem /= sizeof(struct list_head);
446
447 return mem;
448}
449
450/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * Allocate room for a suitable hash table.
452 */
453static int init_hash_tables(struct dm_snapshot *s)
454{
455 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
456
457 /*
458 * Calculate based on the size of the original volume or
459 * the COW volume...
460 */
461 cow_dev_size = get_dev_size(s->cow->bdev);
462 origin_dev_size = get_dev_size(s->origin->bdev);
463 max_buckets = calc_max_buckets();
464
465 hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
466 hash_size = min(hash_size, max_buckets);
467
Robert P. J. Day8defd832008-02-08 02:10:06 +0000468 hash_size = rounddown_pow_of_two(hash_size);
Milan Brozd74f81f2008-02-08 02:11:27 +0000469 if (init_exception_table(&s->complete, hash_size,
470 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -ENOMEM;
472
473 /*
474 * Allocate hash table for in-flight exceptions
475 * Make this smaller than the real hash table
476 */
477 hash_size >>= 3;
478 if (hash_size < 64)
479 hash_size = 64;
480
Milan Brozd74f81f2008-02-08 02:11:27 +0000481 if (init_exception_table(&s->pending, hash_size, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 exit_exception_table(&s->complete, exception_cache);
483 return -ENOMEM;
484 }
485
486 return 0;
487}
488
489/*
490 * Round a number up to the nearest 'size' boundary. size must
491 * be a power of 2.
492 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100493static ulong round_up(ulong n, ulong size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 size--;
496 return (n + size) & ~size;
497}
498
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700499static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
500 char **error)
501{
502 unsigned long chunk_size;
503 char *value;
504
505 chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
506 if (*chunk_size_arg == '\0' || *value != '\0') {
507 *error = "Invalid chunk size";
508 return -EINVAL;
509 }
510
511 if (!chunk_size) {
512 s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
513 return 0;
514 }
515
516 /*
517 * Chunk size must be multiple of page size. Silently
518 * round up if it's not.
519 */
520 chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
521
522 /* Check chunk_size is a power of 2 */
vignesh babu6f3c3f02007-10-19 22:38:44 +0100523 if (!is_power_of_2(chunk_size)) {
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700524 *error = "Chunk size is not a power of 2";
525 return -EINVAL;
526 }
527
528 /* Validate the chunk size against the device block size */
529 if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
530 *error = "Chunk size is not a multiple of device blocksize";
531 return -EINVAL;
532 }
533
534 s->chunk_size = chunk_size;
535 s->chunk_mask = chunk_size - 1;
536 s->chunk_shift = ffs(chunk_size) - 1;
537
538 return 0;
539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*
542 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
543 */
544static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
545{
546 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100547 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int r = -EINVAL;
549 char persistent;
550 char *origin_path;
551 char *cow_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700553 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700554 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 r = -EINVAL;
556 goto bad1;
557 }
558
559 origin_path = argv[0];
560 cow_path = argv[1];
561 persistent = toupper(*argv[2]);
562
563 if (persistent != 'P' && persistent != 'N') {
564 ti->error = "Persistent flag is not P or N";
565 r = -EINVAL;
566 goto bad1;
567 }
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 s = kmalloc(sizeof(*s), GFP_KERNEL);
570 if (s == NULL) {
571 ti->error = "Cannot allocate snapshot context private "
572 "structure";
573 r = -ENOMEM;
574 goto bad1;
575 }
576
577 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
578 if (r) {
579 ti->error = "Cannot get origin device";
580 goto bad2;
581 }
582
583 r = dm_get_device(ti, cow_path, 0, 0,
584 FMODE_READ | FMODE_WRITE, &s->cow);
585 if (r) {
586 dm_put_device(ti, s->origin);
587 ti->error = "Cannot get COW device";
588 goto bad2;
589 }
590
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700591 r = set_chunk_size(s, argv[3], &ti->error);
592 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto bad3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 s->type = persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800598 s->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 s->last_percent = 0;
600 init_rwsem(&s->lock);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700601 spin_lock_init(&s->pe_lock);
Mikulas Patocka72727ba2008-04-24 21:43:11 +0100602 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* Allocate hash table for COW data */
605 if (init_hash_tables(s)) {
606 ti->error = "Unable to allocate hash table space";
607 r = -ENOMEM;
608 goto bad3;
609 }
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 s->store.snap = s;
612
613 if (persistent == 'P')
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700614 r = dm_create_persistent(&s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 else
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700616 r = dm_create_transient(&s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (r) {
619 ti->error = "Couldn't create exception store";
620 r = -EINVAL;
621 goto bad4;
622 }
623
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100624 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (r) {
626 ti->error = "Could not create kcopyd client";
627 goto bad5;
628 }
629
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100630 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
631 tracked_chunk_cache);
632 if (!s->tracked_chunk_pool) {
633 ti->error = "Could not allocate tracked_chunk mempool for "
634 "tracking reads";
635 goto bad6;
636 }
637
638 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
639 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
640
641 spin_lock_init(&s->tracked_chunk_lock);
642
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800643 /* Metadata must only be loaded into one table at once */
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700644 r = s->store.read_metadata(&s->store);
Milan Broz07641472007-07-12 17:28:13 +0100645 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700646 ti->error = "Failed to read snapshot metadata";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100647 goto bad_load_and_register;
Milan Broz07641472007-07-12 17:28:13 +0100648 } else if (r > 0) {
649 s->valid = 0;
650 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700651 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800652
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700653 bio_list_init(&s->queued_bios);
David Howellsc4028952006-11-22 14:57:56 +0000654 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Add snapshot to the list of snapshots for this origin */
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800657 /* Exceptions aren't triggered till snapshot_resume() is called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (register_snapshot(s)) {
659 r = -EINVAL;
660 ti->error = "Cannot register snapshot origin";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100661 goto bad_load_and_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 ti->private = s;
Alasdair G Kergonc51c2752006-06-26 00:27:18 -0700665 ti->split_io = s->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 return 0;
668
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100669 bad_load_and_register:
670 mempool_destroy(s->tracked_chunk_pool);
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 bad6:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100673 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 bad5:
676 s->store.destroy(&s->store);
677
678 bad4:
679 exit_exception_table(&s->pending, pending_cache);
680 exit_exception_table(&s->complete, exception_cache);
681
682 bad3:
683 dm_put_device(ti, s->cow);
684 dm_put_device(ti, s->origin);
685
686 bad2:
687 kfree(s);
688
689 bad1:
690 return r;
691}
692
Milan Broz31c93a02006-12-08 02:41:11 -0800693static void __free_exceptions(struct dm_snapshot *s)
694{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100695 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -0800696 s->kcopyd_client = NULL;
697
698 exit_exception_table(&s->pending, pending_cache);
699 exit_exception_table(&s->complete, exception_cache);
700
701 s->store.destroy(&s->store);
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static void snapshot_dtr(struct dm_target *ti)
705{
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100706#ifdef CONFIG_DM_DEBUG
707 int i;
708#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100709 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700711 flush_workqueue(ksnapd);
712
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800713 /* Prevent further origin writes from using this snapshot. */
714 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unregister_snapshot(s);
716
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100717#ifdef CONFIG_DM_DEBUG
718 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
719 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
720#endif
721
722 mempool_destroy(s->tracked_chunk_pool);
723
Milan Broz31c93a02006-12-08 02:41:11 -0800724 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 dm_put_device(ti, s->origin);
727 dm_put_device(ti, s->cow);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 kfree(s);
730}
731
732/*
733 * Flush a list of buffers.
734 */
735static void flush_bios(struct bio *bio)
736{
737 struct bio *n;
738
739 while (bio) {
740 n = bio->bi_next;
741 bio->bi_next = NULL;
742 generic_make_request(bio);
743 bio = n;
744 }
745}
746
David Howellsc4028952006-11-22 14:57:56 +0000747static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700748{
David Howellsc4028952006-11-22 14:57:56 +0000749 struct dm_snapshot *s =
750 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700751 struct bio *queued_bios;
752 unsigned long flags;
753
754 spin_lock_irqsave(&s->pe_lock, flags);
755 queued_bios = bio_list_get(&s->queued_bios);
756 spin_unlock_irqrestore(&s->pe_lock, flags);
757
758 flush_bios(queued_bios);
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/*
762 * Error a list of buffers.
763 */
764static void error_bios(struct bio *bio)
765{
766 struct bio *n;
767
768 while (bio) {
769 n = bio->bi_next;
770 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200771 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 bio = n;
773 }
774}
775
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700776static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800777{
778 if (!s->valid)
779 return;
780
781 if (err == -EIO)
782 DMERR("Invalidating snapshot: Error reading/writing.");
783 else if (err == -ENOMEM)
784 DMERR("Invalidating snapshot: Unable to allocate exception.");
785
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800786 if (s->store.drop_snapshot)
787 s->store.drop_snapshot(&s->store);
788
789 s->valid = 0;
790
Mikulas Patocka72727ba2008-04-24 21:43:11 +0100791 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800792}
793
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100794static void get_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700795{
796 atomic_inc(&pe->ref_count);
797}
798
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100799static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700800{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100801 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700802 struct bio *origin_bios = NULL;
803
804 primary_pe = pe->primary_pe;
805
806 /*
807 * If this pe is involved in a write to the origin and
808 * it is the last sibling to complete then release
809 * the bios for the original write to the origin.
810 */
811 if (primary_pe &&
812 atomic_dec_and_test(&primary_pe->ref_count))
813 origin_bios = bio_list_get(&primary_pe->origin_bios);
814
815 /*
816 * Free the pe if it's not linked to an origin write or if
817 * it's not itself a primary pe.
818 */
819 if (!primary_pe || primary_pe != pe)
820 free_pending_exception(pe);
821
822 /*
823 * Free the primary pe if nothing references it.
824 */
825 if (primary_pe && !atomic_read(&primary_pe->ref_count))
826 free_pending_exception(primary_pe);
827
828 return origin_bios;
829}
830
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100831static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100833 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700835 struct bio *origin_bios = NULL;
836 struct bio *snapshot_bios = NULL;
837 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800839 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* Read/write error - snapshot is unusable */
841 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700842 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700843 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800847 e = alloc_exception();
848 if (!e) {
849 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700850 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700851 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800852 goto out;
853 }
854 *e = pe->e;
855
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700856 down_write(&s->lock);
857 if (!s->valid) {
858 free_exception(e);
859 error = 1;
860 goto out;
861 }
862
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800863 /*
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100864 * Check for conflicting reads. This is extremely improbable,
865 * so yield() is sufficient and there is no need for a wait queue.
866 */
867 while (__chunk_is_tracked(s, pe->e.old_chunk))
868 yield();
869
870 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800871 * Add a proper exception, and remove the
872 * in-flight exception from the list.
873 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000874 insert_completed_exception(s, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 out:
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700877 remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700878 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700879 origin_bios = put_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700881 up_write(&s->lock);
882
883 /* Submit any pending write bios */
884 if (error)
885 error_bios(snapshot_bios);
886 else
887 flush_bios(snapshot_bios);
888
889 flush_bios(origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static void commit_callback(void *context, int success)
893{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100894 struct dm_snap_pending_exception *pe = context;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 pending_complete(pe, success);
897}
898
899/*
900 * Called when the copy I/O has finished. kcopyd actually runs
901 * this code so don't block.
902 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700903static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100905 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct dm_snapshot *s = pe->snap;
907
908 if (read_err || write_err)
909 pending_complete(pe, 0);
910
911 else
912 /* Update the metadata if we are persistent */
913 s->store.commit_exception(&s->store, &pe->e, commit_callback,
914 pe);
915}
916
917/*
918 * Dispatches the copy operation to kcopyd.
919 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100920static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100923 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct block_device *bdev = s->origin->bdev;
925 sector_t dev_size;
926
927 dev_size = get_dev_size(bdev);
928
929 src.bdev = bdev;
930 src.sector = chunk_to_sector(s, pe->e.old_chunk);
931 src.count = min(s->chunk_size, dev_size - src.sector);
932
933 dest.bdev = s->cow->bdev;
934 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
935 dest.count = src.count;
936
937 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100938 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 &src, 1, &dest, 0, copy_callback, pe);
940}
941
942/*
943 * Looks to see if this snapshot already has a pending exception
944 * for this chunk, otherwise it allocates a new one and inserts
945 * it into the pending table.
946 *
947 * NOTE: a write lock must be held on snap->lock before calling
948 * this.
949 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100950static struct dm_snap_pending_exception *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951__find_pending_exception(struct dm_snapshot *s, struct bio *bio)
952{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100953 struct dm_snap_exception *e;
954 struct dm_snap_pending_exception *pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
956
957 /*
958 * Is there a pending exception for this already ?
959 */
960 e = lookup_exception(&s->pending, chunk);
961 if (e) {
962 /* cast the exception to a pending exception */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100963 pe = container_of(e, struct dm_snap_pending_exception, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800967 /*
968 * Create a new pending exception, we don't want
969 * to hold the lock while we do this.
970 */
971 up_write(&s->lock);
972 pe = alloc_pending_exception();
973 down_write(&s->lock);
974
975 if (!s->valid) {
976 free_pending_exception(pe);
977 return NULL;
978 }
979
980 e = lookup_exception(&s->pending, chunk);
981 if (e) {
982 free_pending_exception(pe);
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100983 pe = container_of(e, struct dm_snap_pending_exception, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800984 goto out;
985 }
986
987 pe->e.old_chunk = chunk;
988 bio_list_init(&pe->origin_bios);
989 bio_list_init(&pe->snapshot_bios);
990 pe->primary_pe = NULL;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700991 atomic_set(&pe->ref_count, 0);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800992 pe->snap = s;
993 pe->started = 0;
994
995 if (s->store.prepare_exception(&s->store, &pe->e)) {
996 free_pending_exception(pe);
997 return NULL;
998 }
999
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001000 get_pending_exception(pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001001 insert_exception(&s->pending, &pe->e);
1002
1003 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return pe;
1005}
1006
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001007static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001008 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 bio->bi_bdev = s->cow->bdev;
Milan Brozd74f81f2008-02-08 02:11:27 +00001011 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
1012 (chunk - e->old_chunk)) +
1013 (bio->bi_sector & s->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016static int snapshot_map(struct dm_target *ti, struct bio *bio,
1017 union map_info *map_context)
1018{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001019 struct dm_snap_exception *e;
1020 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001021 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001023 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 chunk = sector_to_chunk(s, bio->bi_sector);
1026
1027 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001028 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001030 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001032 /* FIXME: should only take write lock if we need
1033 * to copy an exception */
1034 down_write(&s->lock);
1035
1036 if (!s->valid) {
1037 r = -EIO;
1038 goto out_unlock;
1039 }
1040
1041 /* If the block is already remapped - use that, else remap it */
1042 e = lookup_exception(&s->complete, chunk);
1043 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001044 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001045 goto out_unlock;
1046 }
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * Write to snapshot - higher level takes care of RW/RO
1050 * flags so we should only get this if we are
1051 * writeable.
1052 */
1053 if (bio_rw(bio) == WRITE) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001054 pe = __find_pending_exception(s, bio);
1055 if (!pe) {
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001056 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001057 r = -EIO;
1058 goto out_unlock;
1059 }
1060
Milan Brozd74f81f2008-02-08 02:11:27 +00001061 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001062 bio_list_add(&pe->snapshot_bios, bio);
1063
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001064 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001065
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001066 if (!pe->started) {
1067 /* this is protected by snap->lock */
1068 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001069 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001070 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001071 goto out;
1072 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001073 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001074 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001075 map_context->ptr = track_chunk(s, chunk);
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001078 out_unlock:
1079 up_write(&s->lock);
1080 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return r;
1082}
1083
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001084static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1085 int error, union map_info *map_context)
1086{
1087 struct dm_snapshot *s = ti->private;
1088 struct dm_snap_tracked_chunk *c = map_context->ptr;
1089
1090 if (c)
1091 stop_tracking_chunk(s, c);
1092
1093 return 0;
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static void snapshot_resume(struct dm_target *ti)
1097{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001098 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001100 down_write(&s->lock);
1101 s->active = 1;
1102 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105static int snapshot_status(struct dm_target *ti, status_type_t type,
1106 char *result, unsigned int maxlen)
1107{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001108 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 switch (type) {
1111 case STATUSTYPE_INFO:
1112 if (!snap->valid)
1113 snprintf(result, maxlen, "Invalid");
1114 else {
1115 if (snap->store.fraction_full) {
1116 sector_t numerator, denominator;
1117 snap->store.fraction_full(&snap->store,
1118 &numerator,
1119 &denominator);
Andrew Morton4ee218c2006-03-27 01:17:48 -08001120 snprintf(result, maxlen, "%llu/%llu",
1121 (unsigned long long)numerator,
1122 (unsigned long long)denominator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 else
1125 snprintf(result, maxlen, "Unknown");
1126 }
1127 break;
1128
1129 case STATUSTYPE_TABLE:
1130 /*
1131 * kdevname returns a static pointer so we need
1132 * to make private copies if the output is to
1133 * make sense.
1134 */
Andrew Morton4ee218c2006-03-27 01:17:48 -08001135 snprintf(result, maxlen, "%s %s %c %llu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 snap->origin->name, snap->cow->name,
Andrew Morton4ee218c2006-03-27 01:17:48 -08001137 snap->type,
1138 (unsigned long long)snap->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140 }
1141
1142 return 0;
1143}
1144
1145/*-----------------------------------------------------------------
1146 * Origin methods
1147 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148static int __origin_write(struct list_head *snapshots, struct bio *bio)
1149{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001150 int r = DM_MAPIO_REMAPPED, first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct dm_snapshot *snap;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001152 struct dm_snap_exception *e;
1153 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 chunk_t chunk;
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001155 LIST_HEAD(pe_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /* Do all the snapshots on this origin */
1158 list_for_each_entry (snap, snapshots, list) {
1159
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001160 down_write(&snap->lock);
1161
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001162 /* Only deal with valid and active snapshots */
1163 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001164 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001166 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka72727ba2008-04-24 21:43:11 +01001167 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001168 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /*
1171 * Remember, different snapshots can have
1172 * different chunk sizes.
1173 */
1174 chunk = sector_to_chunk(snap, bio->bi_sector);
1175
1176 /*
1177 * Check exception table to see if block
1178 * is already remapped in this snapshot
1179 * and trigger an exception if not.
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001180 *
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001181 * ref_count is initialised to 1 so pending_complete()
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001182 * won't destroy the primary_pe while we're inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
1184 e = lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001185 if (e)
1186 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001188 pe = __find_pending_exception(snap, bio);
1189 if (!pe) {
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001190 __invalidate_snapshot(snap, -ENOMEM);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001191 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001194 if (!primary_pe) {
1195 /*
1196 * Either every pe here has same
1197 * primary_pe or none has one yet.
1198 */
1199 if (pe->primary_pe)
1200 primary_pe = pe->primary_pe;
1201 else {
1202 primary_pe = pe;
1203 first = 1;
1204 }
1205
1206 bio_list_add(&primary_pe->origin_bios, bio);
1207
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001208 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001209 }
1210
1211 if (!pe->primary_pe) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001212 pe->primary_pe = primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001213 get_pending_exception(primary_pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001214 }
1215
1216 if (!pe->started) {
1217 pe->started = 1;
1218 list_add_tail(&pe->list, &pe_queue);
1219 }
1220
1221 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 up_write(&snap->lock);
1223 }
1224
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001225 if (!primary_pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001226 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001227
1228 /*
1229 * If this is the first time we're processing this chunk and
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001230 * ref_count is now 1 it means all the pending exceptions
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001231 * got completed while we were in the loop above, so it falls to
1232 * us here to remove the primary_pe and submit any origin_bios.
1233 */
1234
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001235 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001236 flush_bios(bio_list_get(&primary_pe->origin_bios));
1237 free_pending_exception(primary_pe);
1238 /* If we got here, pe_queue is necessarily empty. */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001239 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001240 }
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * Now that we have a complete pe list we can start the copying.
1244 */
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001245 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1246 start_copy(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 return r;
1249}
1250
1251/*
1252 * Called on a write from the origin driver.
1253 */
1254static int do_origin(struct dm_dev *origin, struct bio *bio)
1255{
1256 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001257 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 down_read(&_origins_lock);
1260 o = __lookup_origin(origin->bdev);
1261 if (o)
1262 r = __origin_write(&o->snapshots, bio);
1263 up_read(&_origins_lock);
1264
1265 return r;
1266}
1267
1268/*
1269 * Origin: maps a linear range of a device, with hooks for snapshotting.
1270 */
1271
1272/*
1273 * Construct an origin mapping: <dev_path>
1274 * The context for an origin is merely a 'struct dm_dev *'
1275 * pointing to the real device.
1276 */
1277static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1278{
1279 int r;
1280 struct dm_dev *dev;
1281
1282 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001283 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EINVAL;
1285 }
1286
1287 r = dm_get_device(ti, argv[0], 0, ti->len,
1288 dm_table_get_mode(ti->table), &dev);
1289 if (r) {
1290 ti->error = "Cannot get target device";
1291 return r;
1292 }
1293
1294 ti->private = dev;
1295 return 0;
1296}
1297
1298static void origin_dtr(struct dm_target *ti)
1299{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001300 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 dm_put_device(ti, dev);
1302}
1303
1304static int origin_map(struct dm_target *ti, struct bio *bio,
1305 union map_info *map_context)
1306{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001307 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 bio->bi_bdev = dev->bdev;
1309
1310 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001311 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1315
1316/*
1317 * Set the target "split_io" field to the minimum of all the snapshots'
1318 * chunk sizes.
1319 */
1320static void origin_resume(struct dm_target *ti)
1321{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001322 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 struct dm_snapshot *snap;
1324 struct origin *o;
1325 chunk_t chunk_size = 0;
1326
1327 down_read(&_origins_lock);
1328 o = __lookup_origin(dev->bdev);
1329 if (o)
1330 list_for_each_entry (snap, &o->snapshots, list)
1331 chunk_size = min_not_zero(chunk_size, snap->chunk_size);
1332 up_read(&_origins_lock);
1333
1334 ti->split_io = chunk_size;
1335}
1336
1337static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1338 unsigned int maxlen)
1339{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001340 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 switch (type) {
1343 case STATUSTYPE_INFO:
1344 result[0] = '\0';
1345 break;
1346
1347 case STATUSTYPE_TABLE:
1348 snprintf(result, maxlen, "%s", dev->name);
1349 break;
1350 }
1351
1352 return 0;
1353}
1354
1355static struct target_type origin_target = {
1356 .name = "snapshot-origin",
Milan Brozd74f81f2008-02-08 02:11:27 +00001357 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 .module = THIS_MODULE,
1359 .ctr = origin_ctr,
1360 .dtr = origin_dtr,
1361 .map = origin_map,
1362 .resume = origin_resume,
1363 .status = origin_status,
1364};
1365
1366static struct target_type snapshot_target = {
1367 .name = "snapshot",
Milan Brozd74f81f2008-02-08 02:11:27 +00001368 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 .module = THIS_MODULE,
1370 .ctr = snapshot_ctr,
1371 .dtr = snapshot_dtr,
1372 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001373 .end_io = snapshot_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 .resume = snapshot_resume,
1375 .status = snapshot_status,
1376};
1377
1378static int __init dm_snapshot_init(void)
1379{
1380 int r;
1381
1382 r = dm_register_target(&snapshot_target);
1383 if (r) {
1384 DMERR("snapshot target register failed %d", r);
1385 return r;
1386 }
1387
1388 r = dm_register_target(&origin_target);
1389 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001390 DMERR("Origin target register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 goto bad1;
1392 }
1393
1394 r = init_origin_hash();
1395 if (r) {
1396 DMERR("init_origin_hash failed.");
1397 goto bad2;
1398 }
1399
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001400 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (!exception_cache) {
1402 DMERR("Couldn't create exception cache.");
1403 r = -ENOMEM;
1404 goto bad3;
1405 }
1406
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001407 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (!pending_cache) {
1409 DMERR("Couldn't create pending cache.");
1410 r = -ENOMEM;
1411 goto bad4;
1412 }
1413
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001414 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1415 if (!tracked_chunk_cache) {
1416 DMERR("Couldn't create cache to track chunks in use.");
1417 r = -ENOMEM;
1418 goto bad5;
1419 }
1420
Matthew Dobson93d23412006-03-26 01:37:50 -08001421 pending_pool = mempool_create_slab_pool(128, pending_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!pending_pool) {
1423 DMERR("Couldn't create pending pool.");
1424 r = -ENOMEM;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001425 goto bad_pending_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001428 ksnapd = create_singlethread_workqueue("ksnapd");
1429 if (!ksnapd) {
1430 DMERR("Failed to create ksnapd workqueue.");
1431 r = -ENOMEM;
1432 goto bad6;
1433 }
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return 0;
1436
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001437 bad6:
1438 mempool_destroy(pending_pool);
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();
1469 mempool_destroy(pending_pool);
1470 kmem_cache_destroy(pending_cache);
1471 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001472 kmem_cache_destroy(tracked_chunk_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
1475/* Module hooks */
1476module_init(dm_snapshot_init);
1477module_exit(dm_snapshot_exit);
1478
1479MODULE_DESCRIPTION(DM_NAME " snapshot target");
1480MODULE_AUTHOR("Joe Thornber");
1481MODULE_LICENSE("GPL");