blob: 746603b42f86d988bef1715dfc3a5b2e51a225f8 [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{
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000232 struct origin *o, *new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct block_device *bdev = snap->origin->bdev;
234
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000235 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
236 if (!new_o)
237 return -ENOMEM;
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 down_write(&_origins_lock);
240 o = __lookup_origin(bdev);
241
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000242 if (o)
243 kfree(new_o);
244 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000246 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Initialise the struct */
249 INIT_LIST_HEAD(&o->snapshots);
250 o->bdev = bdev;
251
252 __insert_origin(o);
253 }
254
255 list_add_tail(&snap->list, &o->snapshots);
256
257 up_write(&_origins_lock);
258 return 0;
259}
260
261static void unregister_snapshot(struct dm_snapshot *s)
262{
263 struct origin *o;
264
265 down_write(&_origins_lock);
266 o = __lookup_origin(s->origin->bdev);
267
268 list_del(&s->list);
269 if (list_empty(&o->snapshots)) {
270 list_del(&o->hash_list);
271 kfree(o);
272 }
273
274 up_write(&_origins_lock);
275}
276
277/*
278 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000279 * The lowest hash_shift bits of the chunk number are ignored, allowing
280 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000282static int init_exception_table(struct exception_table *et, uint32_t size,
283 unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 unsigned int i;
286
Milan Brozd74f81f2008-02-08 02:11:27 +0000287 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 et->hash_mask = size - 1;
289 et->table = dm_vcalloc(size, sizeof(struct list_head));
290 if (!et->table)
291 return -ENOMEM;
292
293 for (i = 0; i < size; i++)
294 INIT_LIST_HEAD(et->table + i);
295
296 return 0;
297}
298
Christoph Lametere18b8902006-12-06 20:33:20 -0800299static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100302 struct dm_snap_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 int i, size;
304
305 size = et->hash_mask + 1;
306 for (i = 0; i < size; i++) {
307 slot = et->table + i;
308
309 list_for_each_entry_safe (ex, next, slot, hash_list)
310 kmem_cache_free(mem, ex);
311 }
312
313 vfree(et->table);
314}
315
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100316static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Milan Brozd74f81f2008-02-08 02:11:27 +0000318 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100321static void insert_exception(struct exception_table *eh,
322 struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
325 list_add(&e->hash_list, l);
326}
327
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100328static void remove_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 list_del(&e->hash_list);
331}
332
333/*
334 * Return the exception data for a sector, or NULL if not
335 * remapped.
336 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100337static struct dm_snap_exception *lookup_exception(struct exception_table *et,
338 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100341 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 slot = &et->table[exception_hash(et, chunk)];
344 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000345 if (chunk >= e->old_chunk &&
346 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return e;
348
349 return NULL;
350}
351
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100352static struct dm_snap_exception *alloc_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100354 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
357 if (!e)
358 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
359
360 return e;
361}
362
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100363static void free_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 kmem_cache_free(exception_cache, e);
366}
367
Mikulas Patocka92e86812008-07-21 12:00:35 +0100368static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100370 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
371 GFP_NOIO);
372
373 pe->snap = s;
374
375 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100378static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100380 mempool_free(pe, pe->snap->pending_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Milan Brozd74f81f2008-02-08 02:11:27 +0000383static void insert_completed_exception(struct dm_snapshot *s,
384 struct dm_snap_exception *new_e)
385{
386 struct exception_table *eh = &s->complete;
387 struct list_head *l;
388 struct dm_snap_exception *e = NULL;
389
390 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
391
392 /* Add immediately if this table doesn't support consecutive chunks */
393 if (!eh->hash_shift)
394 goto out;
395
396 /* List is ordered by old_chunk */
397 list_for_each_entry_reverse(e, l, hash_list) {
398 /* Insert after an existing chunk? */
399 if (new_e->old_chunk == (e->old_chunk +
400 dm_consecutive_chunk_count(e) + 1) &&
401 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
402 dm_consecutive_chunk_count(e) + 1)) {
403 dm_consecutive_chunk_count_inc(e);
404 free_exception(new_e);
405 return;
406 }
407
408 /* Insert before an existing chunk? */
409 if (new_e->old_chunk == (e->old_chunk - 1) &&
410 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
411 dm_consecutive_chunk_count_inc(e);
412 e->old_chunk--;
413 e->new_chunk--;
414 free_exception(new_e);
415 return;
416 }
417
418 if (new_e->old_chunk > e->old_chunk)
419 break;
420 }
421
422out:
423 list_add(&new_e->hash_list, e ? &e->hash_list : l);
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
427{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100428 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 e = alloc_exception();
431 if (!e)
432 return -ENOMEM;
433
434 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000435
436 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000438
439 insert_completed_exception(s, e);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442}
443
444/*
445 * Hard coded magic.
446 */
447static int calc_max_buckets(void)
448{
449 /* use a fixed size of 2MB */
450 unsigned long mem = 2 * 1024 * 1024;
451 mem /= sizeof(struct list_head);
452
453 return mem;
454}
455
456/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * Allocate room for a suitable hash table.
458 */
459static int init_hash_tables(struct dm_snapshot *s)
460{
461 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
462
463 /*
464 * Calculate based on the size of the original volume or
465 * the COW volume...
466 */
467 cow_dev_size = get_dev_size(s->cow->bdev);
468 origin_dev_size = get_dev_size(s->origin->bdev);
469 max_buckets = calc_max_buckets();
470
471 hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
472 hash_size = min(hash_size, max_buckets);
473
Robert P. J. Day8defd832008-02-08 02:10:06 +0000474 hash_size = rounddown_pow_of_two(hash_size);
Milan Brozd74f81f2008-02-08 02:11:27 +0000475 if (init_exception_table(&s->complete, hash_size,
476 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -ENOMEM;
478
479 /*
480 * Allocate hash table for in-flight exceptions
481 * Make this smaller than the real hash table
482 */
483 hash_size >>= 3;
484 if (hash_size < 64)
485 hash_size = 64;
486
Milan Brozd74f81f2008-02-08 02:11:27 +0000487 if (init_exception_table(&s->pending, hash_size, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 exit_exception_table(&s->complete, exception_cache);
489 return -ENOMEM;
490 }
491
492 return 0;
493}
494
495/*
496 * Round a number up to the nearest 'size' boundary. size must
497 * be a power of 2.
498 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100499static ulong round_up(ulong n, ulong size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 size--;
502 return (n + size) & ~size;
503}
504
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700505static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
506 char **error)
507{
508 unsigned long chunk_size;
509 char *value;
510
511 chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
512 if (*chunk_size_arg == '\0' || *value != '\0') {
513 *error = "Invalid chunk size";
514 return -EINVAL;
515 }
516
517 if (!chunk_size) {
518 s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
519 return 0;
520 }
521
522 /*
523 * Chunk size must be multiple of page size. Silently
524 * round up if it's not.
525 */
526 chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
527
528 /* Check chunk_size is a power of 2 */
vignesh babu6f3c3f02007-10-19 22:38:44 +0100529 if (!is_power_of_2(chunk_size)) {
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700530 *error = "Chunk size is not a power of 2";
531 return -EINVAL;
532 }
533
534 /* Validate the chunk size against the device block size */
535 if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
536 *error = "Chunk size is not a multiple of device blocksize";
537 return -EINVAL;
538 }
539
540 s->chunk_size = chunk_size;
541 s->chunk_mask = chunk_size - 1;
542 s->chunk_shift = ffs(chunk_size) - 1;
543
544 return 0;
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/*
548 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
549 */
550static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
551{
552 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100553 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int r = -EINVAL;
555 char persistent;
556 char *origin_path;
557 char *cow_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700559 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700560 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 r = -EINVAL;
562 goto bad1;
563 }
564
565 origin_path = argv[0];
566 cow_path = argv[1];
567 persistent = toupper(*argv[2]);
568
569 if (persistent != 'P' && persistent != 'N') {
570 ti->error = "Persistent flag is not P or N";
571 r = -EINVAL;
572 goto bad1;
573 }
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 s = kmalloc(sizeof(*s), GFP_KERNEL);
576 if (s == NULL) {
577 ti->error = "Cannot allocate snapshot context private "
578 "structure";
579 r = -ENOMEM;
580 goto bad1;
581 }
582
583 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
584 if (r) {
585 ti->error = "Cannot get origin device";
586 goto bad2;
587 }
588
589 r = dm_get_device(ti, cow_path, 0, 0,
590 FMODE_READ | FMODE_WRITE, &s->cow);
591 if (r) {
592 dm_put_device(ti, s->origin);
593 ti->error = "Cannot get COW device";
594 goto bad2;
595 }
596
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700597 r = set_chunk_size(s, argv[3], &ti->error);
598 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto bad3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 s->type = persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800604 s->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 init_rwsem(&s->lock);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700606 spin_lock_init(&s->pe_lock);
Mikulas Patocka72727ba2008-04-24 21:43:11 +0100607 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* Allocate hash table for COW data */
610 if (init_hash_tables(s)) {
611 ti->error = "Unable to allocate hash table space";
612 r = -ENOMEM;
613 goto bad3;
614 }
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 s->store.snap = s;
617
618 if (persistent == 'P')
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700619 r = dm_create_persistent(&s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 else
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700621 r = dm_create_transient(&s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (r) {
624 ti->error = "Couldn't create exception store";
625 r = -EINVAL;
626 goto bad4;
627 }
628
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100629 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (r) {
631 ti->error = "Could not create kcopyd client";
632 goto bad5;
633 }
634
Mikulas Patocka92e86812008-07-21 12:00:35 +0100635 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
636 if (!s->pending_pool) {
637 ti->error = "Could not allocate mempool for pending exceptions";
638 goto bad6;
639 }
640
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100641 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
642 tracked_chunk_cache);
643 if (!s->tracked_chunk_pool) {
644 ti->error = "Could not allocate tracked_chunk mempool for "
645 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +0100646 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100647 }
648
649 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
650 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
651
652 spin_lock_init(&s->tracked_chunk_lock);
653
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800654 /* Metadata must only be loaded into one table at once */
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700655 r = s->store.read_metadata(&s->store);
Milan Broz07641472007-07-12 17:28:13 +0100656 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700657 ti->error = "Failed to read snapshot metadata";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100658 goto bad_load_and_register;
Milan Broz07641472007-07-12 17:28:13 +0100659 } else if (r > 0) {
660 s->valid = 0;
661 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700662 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800663
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700664 bio_list_init(&s->queued_bios);
David Howellsc4028952006-11-22 14:57:56 +0000665 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Add snapshot to the list of snapshots for this origin */
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800668 /* Exceptions aren't triggered till snapshot_resume() is called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (register_snapshot(s)) {
670 r = -EINVAL;
671 ti->error = "Cannot register snapshot origin";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100672 goto bad_load_and_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
675 ti->private = s;
Alasdair G Kergonc51c2752006-06-26 00:27:18 -0700676 ti->split_io = s->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 return 0;
679
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100680 bad_load_and_register:
681 mempool_destroy(s->tracked_chunk_pool);
682
Mikulas Patocka92e86812008-07-21 12:00:35 +0100683 bad_tracked_chunk_pool:
684 mempool_destroy(s->pending_pool);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 bad6:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100687 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 bad5:
690 s->store.destroy(&s->store);
691
692 bad4:
693 exit_exception_table(&s->pending, pending_cache);
694 exit_exception_table(&s->complete, exception_cache);
695
696 bad3:
697 dm_put_device(ti, s->cow);
698 dm_put_device(ti, s->origin);
699
700 bad2:
701 kfree(s);
702
703 bad1:
704 return r;
705}
706
Milan Broz31c93a02006-12-08 02:41:11 -0800707static void __free_exceptions(struct dm_snapshot *s)
708{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100709 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -0800710 s->kcopyd_client = NULL;
711
712 exit_exception_table(&s->pending, pending_cache);
713 exit_exception_table(&s->complete, exception_cache);
714
715 s->store.destroy(&s->store);
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static void snapshot_dtr(struct dm_target *ti)
719{
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100720#ifdef CONFIG_DM_DEBUG
721 int i;
722#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100723 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700725 flush_workqueue(ksnapd);
726
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800727 /* Prevent further origin writes from using this snapshot. */
728 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unregister_snapshot(s);
730
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100731#ifdef CONFIG_DM_DEBUG
732 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
733 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
734#endif
735
736 mempool_destroy(s->tracked_chunk_pool);
737
Milan Broz31c93a02006-12-08 02:41:11 -0800738 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Mikulas Patocka92e86812008-07-21 12:00:35 +0100740 mempool_destroy(s->pending_pool);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dm_put_device(ti, s->origin);
743 dm_put_device(ti, s->cow);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 kfree(s);
746}
747
748/*
749 * Flush a list of buffers.
750 */
751static void flush_bios(struct bio *bio)
752{
753 struct bio *n;
754
755 while (bio) {
756 n = bio->bi_next;
757 bio->bi_next = NULL;
758 generic_make_request(bio);
759 bio = n;
760 }
761}
762
David Howellsc4028952006-11-22 14:57:56 +0000763static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700764{
David Howellsc4028952006-11-22 14:57:56 +0000765 struct dm_snapshot *s =
766 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700767 struct bio *queued_bios;
768 unsigned long flags;
769
770 spin_lock_irqsave(&s->pe_lock, flags);
771 queued_bios = bio_list_get(&s->queued_bios);
772 spin_unlock_irqrestore(&s->pe_lock, flags);
773
774 flush_bios(queued_bios);
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
778 * Error a list of buffers.
779 */
780static void error_bios(struct bio *bio)
781{
782 struct bio *n;
783
784 while (bio) {
785 n = bio->bi_next;
786 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200787 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 bio = n;
789 }
790}
791
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700792static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800793{
794 if (!s->valid)
795 return;
796
797 if (err == -EIO)
798 DMERR("Invalidating snapshot: Error reading/writing.");
799 else if (err == -ENOMEM)
800 DMERR("Invalidating snapshot: Unable to allocate exception.");
801
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800802 if (s->store.drop_snapshot)
803 s->store.drop_snapshot(&s->store);
804
805 s->valid = 0;
806
Mikulas Patocka72727ba2008-04-24 21:43:11 +0100807 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800808}
809
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100810static void get_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700811{
812 atomic_inc(&pe->ref_count);
813}
814
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100815static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700816{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100817 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700818 struct bio *origin_bios = NULL;
819
820 primary_pe = pe->primary_pe;
821
822 /*
823 * If this pe is involved in a write to the origin and
824 * it is the last sibling to complete then release
825 * the bios for the original write to the origin.
826 */
827 if (primary_pe &&
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100828 atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700829 origin_bios = bio_list_get(&primary_pe->origin_bios);
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100830 free_pending_exception(primary_pe);
831 }
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700832
833 /*
834 * Free the pe if it's not linked to an origin write or if
835 * it's not itself a primary pe.
836 */
837 if (!primary_pe || primary_pe != pe)
838 free_pending_exception(pe);
839
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700840 return origin_bios;
841}
842
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100843static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100845 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700847 struct bio *origin_bios = NULL;
848 struct bio *snapshot_bios = NULL;
849 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800851 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Read/write error - snapshot is unusable */
853 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700854 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700855 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800856 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800859 e = alloc_exception();
860 if (!e) {
861 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700862 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700863 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800864 goto out;
865 }
866 *e = pe->e;
867
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700868 down_write(&s->lock);
869 if (!s->valid) {
870 free_exception(e);
871 error = 1;
872 goto out;
873 }
874
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800875 /*
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100876 * Check for conflicting reads. This is extremely improbable,
877 * so yield() is sufficient and there is no need for a wait queue.
878 */
879 while (__chunk_is_tracked(s, pe->e.old_chunk))
880 yield();
881
882 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800883 * Add a proper exception, and remove the
884 * in-flight exception from the list.
885 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000886 insert_completed_exception(s, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 out:
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700889 remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700890 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700891 origin_bios = put_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700893 up_write(&s->lock);
894
895 /* Submit any pending write bios */
896 if (error)
897 error_bios(snapshot_bios);
898 else
899 flush_bios(snapshot_bios);
900
901 flush_bios(origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904static void commit_callback(void *context, int success)
905{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100906 struct dm_snap_pending_exception *pe = context;
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 pending_complete(pe, success);
909}
910
911/*
912 * Called when the copy I/O has finished. kcopyd actually runs
913 * this code so don't block.
914 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700915static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100917 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct dm_snapshot *s = pe->snap;
919
920 if (read_err || write_err)
921 pending_complete(pe, 0);
922
923 else
924 /* Update the metadata if we are persistent */
925 s->store.commit_exception(&s->store, &pe->e, commit_callback,
926 pe);
927}
928
929/*
930 * Dispatches the copy operation to kcopyd.
931 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100932static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100935 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct block_device *bdev = s->origin->bdev;
937 sector_t dev_size;
938
939 dev_size = get_dev_size(bdev);
940
941 src.bdev = bdev;
942 src.sector = chunk_to_sector(s, pe->e.old_chunk);
943 src.count = min(s->chunk_size, dev_size - src.sector);
944
945 dest.bdev = s->cow->bdev;
946 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
947 dest.count = src.count;
948
949 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100950 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 &src, 1, &dest, 0, copy_callback, pe);
952}
953
954/*
955 * Looks to see if this snapshot already has a pending exception
956 * for this chunk, otherwise it allocates a new one and inserts
957 * it into the pending table.
958 *
959 * NOTE: a write lock must be held on snap->lock before calling
960 * this.
961 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100962static struct dm_snap_pending_exception *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963__find_pending_exception(struct dm_snapshot *s, struct bio *bio)
964{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100965 struct dm_snap_exception *e;
966 struct dm_snap_pending_exception *pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
968
969 /*
970 * Is there a pending exception for this already ?
971 */
972 e = lookup_exception(&s->pending, chunk);
973 if (e) {
974 /* cast the exception to a pending exception */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100975 pe = container_of(e, struct dm_snap_pending_exception, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800976 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800979 /*
980 * Create a new pending exception, we don't want
981 * to hold the lock while we do this.
982 */
983 up_write(&s->lock);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100984 pe = alloc_pending_exception(s);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800985 down_write(&s->lock);
986
987 if (!s->valid) {
988 free_pending_exception(pe);
989 return NULL;
990 }
991
992 e = lookup_exception(&s->pending, chunk);
993 if (e) {
994 free_pending_exception(pe);
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100995 pe = container_of(e, struct dm_snap_pending_exception, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800996 goto out;
997 }
998
999 pe->e.old_chunk = chunk;
1000 bio_list_init(&pe->origin_bios);
1001 bio_list_init(&pe->snapshot_bios);
1002 pe->primary_pe = NULL;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001003 atomic_set(&pe->ref_count, 0);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001004 pe->started = 0;
1005
1006 if (s->store.prepare_exception(&s->store, &pe->e)) {
1007 free_pending_exception(pe);
1008 return NULL;
1009 }
1010
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001011 get_pending_exception(pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001012 insert_exception(&s->pending, &pe->e);
1013
1014 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return pe;
1016}
1017
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001018static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001019 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 bio->bi_bdev = s->cow->bdev;
Milan Brozd74f81f2008-02-08 02:11:27 +00001022 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
1023 (chunk - e->old_chunk)) +
1024 (bio->bi_sector & s->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027static int snapshot_map(struct dm_target *ti, struct bio *bio,
1028 union map_info *map_context)
1029{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001030 struct dm_snap_exception *e;
1031 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001032 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001034 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 chunk = sector_to_chunk(s, bio->bi_sector);
1037
1038 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001039 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001041 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001043 /* FIXME: should only take write lock if we need
1044 * to copy an exception */
1045 down_write(&s->lock);
1046
1047 if (!s->valid) {
1048 r = -EIO;
1049 goto out_unlock;
1050 }
1051
1052 /* If the block is already remapped - use that, else remap it */
1053 e = lookup_exception(&s->complete, chunk);
1054 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001055 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001056 goto out_unlock;
1057 }
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /*
1060 * Write to snapshot - higher level takes care of RW/RO
1061 * flags so we should only get this if we are
1062 * writeable.
1063 */
1064 if (bio_rw(bio) == WRITE) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001065 pe = __find_pending_exception(s, bio);
1066 if (!pe) {
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001067 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001068 r = -EIO;
1069 goto out_unlock;
1070 }
1071
Milan Brozd74f81f2008-02-08 02:11:27 +00001072 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001073 bio_list_add(&pe->snapshot_bios, bio);
1074
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001075 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001076
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001077 if (!pe->started) {
1078 /* this is protected by snap->lock */
1079 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001080 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001081 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001082 goto out;
1083 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001084 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001085 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001086 map_context->ptr = track_chunk(s, chunk);
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001089 out_unlock:
1090 up_write(&s->lock);
1091 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return r;
1093}
1094
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001095static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1096 int error, union map_info *map_context)
1097{
1098 struct dm_snapshot *s = ti->private;
1099 struct dm_snap_tracked_chunk *c = map_context->ptr;
1100
1101 if (c)
1102 stop_tracking_chunk(s, c);
1103
1104 return 0;
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107static void snapshot_resume(struct dm_target *ti)
1108{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001109 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001111 down_write(&s->lock);
1112 s->active = 1;
1113 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
1116static int snapshot_status(struct dm_target *ti, status_type_t type,
1117 char *result, unsigned int maxlen)
1118{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001119 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 switch (type) {
1122 case STATUSTYPE_INFO:
1123 if (!snap->valid)
1124 snprintf(result, maxlen, "Invalid");
1125 else {
1126 if (snap->store.fraction_full) {
1127 sector_t numerator, denominator;
1128 snap->store.fraction_full(&snap->store,
1129 &numerator,
1130 &denominator);
Andrew Morton4ee218c2006-03-27 01:17:48 -08001131 snprintf(result, maxlen, "%llu/%llu",
1132 (unsigned long long)numerator,
1133 (unsigned long long)denominator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 else
1136 snprintf(result, maxlen, "Unknown");
1137 }
1138 break;
1139
1140 case STATUSTYPE_TABLE:
1141 /*
1142 * kdevname returns a static pointer so we need
1143 * to make private copies if the output is to
1144 * make sense.
1145 */
Andrew Morton4ee218c2006-03-27 01:17:48 -08001146 snprintf(result, maxlen, "%s %s %c %llu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 snap->origin->name, snap->cow->name,
Andrew Morton4ee218c2006-03-27 01:17:48 -08001148 snap->type,
1149 (unsigned long long)snap->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
1151 }
1152
1153 return 0;
1154}
1155
1156/*-----------------------------------------------------------------
1157 * Origin methods
1158 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159static int __origin_write(struct list_head *snapshots, struct bio *bio)
1160{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001161 int r = DM_MAPIO_REMAPPED, first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct dm_snapshot *snap;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001163 struct dm_snap_exception *e;
1164 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 chunk_t chunk;
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001166 LIST_HEAD(pe_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /* Do all the snapshots on this origin */
1169 list_for_each_entry (snap, snapshots, list) {
1170
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001171 down_write(&snap->lock);
1172
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001173 /* Only deal with valid and active snapshots */
1174 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001175 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001177 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka72727ba2008-04-24 21:43:11 +01001178 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001179 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /*
1182 * Remember, different snapshots can have
1183 * different chunk sizes.
1184 */
1185 chunk = sector_to_chunk(snap, bio->bi_sector);
1186
1187 /*
1188 * Check exception table to see if block
1189 * is already remapped in this snapshot
1190 * and trigger an exception if not.
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001191 *
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001192 * ref_count is initialised to 1 so pending_complete()
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001193 * won't destroy the primary_pe while we're inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
1195 e = lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001196 if (e)
1197 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001199 pe = __find_pending_exception(snap, bio);
1200 if (!pe) {
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001201 __invalidate_snapshot(snap, -ENOMEM);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001202 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001205 if (!primary_pe) {
1206 /*
1207 * Either every pe here has same
1208 * primary_pe or none has one yet.
1209 */
1210 if (pe->primary_pe)
1211 primary_pe = pe->primary_pe;
1212 else {
1213 primary_pe = pe;
1214 first = 1;
1215 }
1216
1217 bio_list_add(&primary_pe->origin_bios, bio);
1218
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001219 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001220 }
1221
1222 if (!pe->primary_pe) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001223 pe->primary_pe = primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001224 get_pending_exception(primary_pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001225 }
1226
1227 if (!pe->started) {
1228 pe->started = 1;
1229 list_add_tail(&pe->list, &pe_queue);
1230 }
1231
1232 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 up_write(&snap->lock);
1234 }
1235
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001236 if (!primary_pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001237 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001238
1239 /*
1240 * If this is the first time we're processing this chunk and
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001241 * ref_count is now 1 it means all the pending exceptions
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001242 * got completed while we were in the loop above, so it falls to
1243 * us here to remove the primary_pe and submit any origin_bios.
1244 */
1245
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001246 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001247 flush_bios(bio_list_get(&primary_pe->origin_bios));
1248 free_pending_exception(primary_pe);
1249 /* If we got here, pe_queue is necessarily empty. */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001250 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001251 }
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * Now that we have a complete pe list we can start the copying.
1255 */
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001256 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1257 start_copy(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 return r;
1260}
1261
1262/*
1263 * Called on a write from the origin driver.
1264 */
1265static int do_origin(struct dm_dev *origin, struct bio *bio)
1266{
1267 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001268 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 down_read(&_origins_lock);
1271 o = __lookup_origin(origin->bdev);
1272 if (o)
1273 r = __origin_write(&o->snapshots, bio);
1274 up_read(&_origins_lock);
1275
1276 return r;
1277}
1278
1279/*
1280 * Origin: maps a linear range of a device, with hooks for snapshotting.
1281 */
1282
1283/*
1284 * Construct an origin mapping: <dev_path>
1285 * The context for an origin is merely a 'struct dm_dev *'
1286 * pointing to the real device.
1287 */
1288static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1289{
1290 int r;
1291 struct dm_dev *dev;
1292
1293 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001294 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -EINVAL;
1296 }
1297
1298 r = dm_get_device(ti, argv[0], 0, ti->len,
1299 dm_table_get_mode(ti->table), &dev);
1300 if (r) {
1301 ti->error = "Cannot get target device";
1302 return r;
1303 }
1304
1305 ti->private = dev;
1306 return 0;
1307}
1308
1309static void origin_dtr(struct dm_target *ti)
1310{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001311 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 dm_put_device(ti, dev);
1313}
1314
1315static int origin_map(struct dm_target *ti, struct bio *bio,
1316 union map_info *map_context)
1317{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001318 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 bio->bi_bdev = dev->bdev;
1320
1321 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001322 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1326
1327/*
1328 * Set the target "split_io" field to the minimum of all the snapshots'
1329 * chunk sizes.
1330 */
1331static void origin_resume(struct dm_target *ti)
1332{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001333 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct dm_snapshot *snap;
1335 struct origin *o;
1336 chunk_t chunk_size = 0;
1337
1338 down_read(&_origins_lock);
1339 o = __lookup_origin(dev->bdev);
1340 if (o)
1341 list_for_each_entry (snap, &o->snapshots, list)
1342 chunk_size = min_not_zero(chunk_size, snap->chunk_size);
1343 up_read(&_origins_lock);
1344
1345 ti->split_io = chunk_size;
1346}
1347
1348static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1349 unsigned int maxlen)
1350{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001351 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 switch (type) {
1354 case STATUSTYPE_INFO:
1355 result[0] = '\0';
1356 break;
1357
1358 case STATUSTYPE_TABLE:
1359 snprintf(result, maxlen, "%s", dev->name);
1360 break;
1361 }
1362
1363 return 0;
1364}
1365
1366static struct target_type origin_target = {
1367 .name = "snapshot-origin",
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 = origin_ctr,
1371 .dtr = origin_dtr,
1372 .map = origin_map,
1373 .resume = origin_resume,
1374 .status = origin_status,
1375};
1376
1377static struct target_type snapshot_target = {
1378 .name = "snapshot",
Milan Brozd74f81f2008-02-08 02:11:27 +00001379 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 .module = THIS_MODULE,
1381 .ctr = snapshot_ctr,
1382 .dtr = snapshot_dtr,
1383 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001384 .end_io = snapshot_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 .resume = snapshot_resume,
1386 .status = snapshot_status,
1387};
1388
1389static int __init dm_snapshot_init(void)
1390{
1391 int r;
1392
1393 r = dm_register_target(&snapshot_target);
1394 if (r) {
1395 DMERR("snapshot target register failed %d", r);
1396 return r;
1397 }
1398
1399 r = dm_register_target(&origin_target);
1400 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001401 DMERR("Origin target register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto bad1;
1403 }
1404
1405 r = init_origin_hash();
1406 if (r) {
1407 DMERR("init_origin_hash failed.");
1408 goto bad2;
1409 }
1410
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001411 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (!exception_cache) {
1413 DMERR("Couldn't create exception cache.");
1414 r = -ENOMEM;
1415 goto bad3;
1416 }
1417
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001418 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!pending_cache) {
1420 DMERR("Couldn't create pending cache.");
1421 r = -ENOMEM;
1422 goto bad4;
1423 }
1424
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001425 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1426 if (!tracked_chunk_cache) {
1427 DMERR("Couldn't create cache to track chunks in use.");
1428 r = -ENOMEM;
1429 goto bad5;
1430 }
1431
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001432 ksnapd = create_singlethread_workqueue("ksnapd");
1433 if (!ksnapd) {
1434 DMERR("Failed to create ksnapd workqueue.");
1435 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001436 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001437 }
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return 0;
1440
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001441 bad_pending_pool:
1442 kmem_cache_destroy(tracked_chunk_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 bad5:
1444 kmem_cache_destroy(pending_cache);
1445 bad4:
1446 kmem_cache_destroy(exception_cache);
1447 bad3:
1448 exit_origin_hash();
1449 bad2:
1450 dm_unregister_target(&origin_target);
1451 bad1:
1452 dm_unregister_target(&snapshot_target);
1453 return r;
1454}
1455
1456static void __exit dm_snapshot_exit(void)
1457{
1458 int r;
1459
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001460 destroy_workqueue(ksnapd);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 r = dm_unregister_target(&snapshot_target);
1463 if (r)
1464 DMERR("snapshot unregister failed %d", r);
1465
1466 r = dm_unregister_target(&origin_target);
1467 if (r)
1468 DMERR("origin unregister failed %d", r);
1469
1470 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 kmem_cache_destroy(pending_cache);
1472 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001473 kmem_cache_destroy(tracked_chunk_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476/* Module hooks */
1477module_init(dm_snapshot_init);
1478module_exit(dm_snapshot_exit);
1479
1480MODULE_DESCRIPTION(DM_NAME " snapshot target");
1481MODULE_AUTHOR("Joe Thornber");
1482MODULE_LICENSE("GPL");