blob: 59d9ef6f50512a4e6eccf42e28f659472701ad94 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dm-snapshot.c
3 *
4 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
5 *
6 * This file is released under the GPL.
7 */
8
9#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/device-mapper.h>
Mikulas Patocka90fa1522009-01-06 03:04:54 +000011#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/fs.h>
13#include <linux/init.h>
14#include <linux/kdev_t.h>
15#include <linux/list.h>
16#include <linux/mempool.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/vmalloc.h>
vignesh babu6f3c3f02007-10-19 22:38:44 +010020#include <linux/log2.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010021#include <linux/dm-kcopyd.h>
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010022#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jonathan Brassowaea53d92009-01-06 03:05:15 +000024#include "dm-exception-store.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "snapshots"
27
Mikulas Patockad698aa42009-12-10 23:52:30 +000028static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
29
30#define dm_target_is_snapshot_merge(ti) \
31 ((ti)->type->name == dm_snapshot_merge_target_name)
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * The percentage increment we will wake up users at
35 */
36#define WAKE_UP_PERCENT 5
37
38/*
39 * kcopyd priority of snapshot operations
40 */
41#define SNAPSHOT_COPY_PRIORITY 2
42
43/*
Milan Broz8ee27672008-04-24 21:42:36 +010044 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
Milan Broz8ee27672008-04-24 21:42:36 +010046#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Mikulas Patockacd45daf2008-07-21 12:00:32 +010048/*
49 * The size of the mempool used to track chunks in use.
50 */
51#define MIN_IOS 256
52
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010053#define DM_TRACKED_CHUNK_HASH_SIZE 16
54#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
55 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
56
Jon Brassow191437a2009-12-10 23:52:10 +000057struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010058 uint32_t hash_mask;
59 unsigned hash_shift;
60 struct list_head *table;
61};
62
63struct dm_snapshot {
64 struct rw_semaphore lock;
65
66 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000067 struct dm_dev *cow;
68
69 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010070
71 /* List of snapshots per Origin */
72 struct list_head list;
73
74 /* You can't use a snapshot if this is 0 (e.g. if full) */
75 int valid;
76
77 /* Origin writes don't trigger exceptions until this is set */
78 int active;
79
Mike Snitzerc26655c2009-12-10 23:52:12 +000080 /* Whether or not owning mapped_device is suspended */
81 int suspended;
82
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010083 mempool_t *pending_pool;
84
85 atomic_t pending_exceptions_count;
86
Jon Brassow191437a2009-12-10 23:52:10 +000087 struct dm_exception_table pending;
88 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010089
90 /*
91 * pe_lock protects all pending_exception operations and access
92 * as well as the snapshot_bios list.
93 */
94 spinlock_t pe_lock;
95
96 /* The on disk metadata handler */
97 struct dm_exception_store *store;
98
99 struct dm_kcopyd_client *kcopyd_client;
100
101 /* Queue of snapshot writes for ksnapd to flush */
102 struct bio_list queued_bios;
103 struct work_struct queued_bios_work;
104
105 /* Chunks with outstanding reads */
106 mempool_t *tracked_chunk_pool;
107 spinlock_t tracked_chunk_lock;
108 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
109};
110
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000111struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
112{
113 return s->cow;
114}
115EXPORT_SYMBOL(dm_snap_cow);
116
Adrian Bunkc642f9e2006-12-08 02:41:13 -0800117static struct workqueue_struct *ksnapd;
David Howellsc4028952006-11-22 14:57:56 +0000118static void flush_queued_bios(struct work_struct *work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700119
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100120static sector_t chunk_to_sector(struct dm_exception_store *store,
121 chunk_t chunk)
122{
123 return chunk << store->chunk_shift;
124}
125
126static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
127{
128 /*
129 * There is only ever one instance of a particular block
130 * device so we can compare pointers safely.
131 */
132 return lhs == rhs;
133}
134
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100135struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000136 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /*
139 * Origin buffers waiting for this to complete are held
140 * in a bio list
141 */
142 struct bio_list origin_bios;
143 struct bio_list snapshot_bios;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Pointer back to snapshot context */
146 struct dm_snapshot *snap;
147
148 /*
149 * 1 indicates the exception has already been sent to
150 * kcopyd.
151 */
152 int started;
153};
154
155/*
156 * Hash table mapping origin volumes to lists of snapshots and
157 * a lock to protect it
158 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800159static struct kmem_cache *exception_cache;
160static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100162struct dm_snap_tracked_chunk {
163 struct hlist_node node;
164 chunk_t chunk;
165};
166
167static struct kmem_cache *tracked_chunk_cache;
168
169static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
170 chunk_t chunk)
171{
172 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
173 GFP_NOIO);
174 unsigned long flags;
175
176 c->chunk = chunk;
177
178 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
179 hlist_add_head(&c->node,
180 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
181 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
182
183 return c;
184}
185
186static void stop_tracking_chunk(struct dm_snapshot *s,
187 struct dm_snap_tracked_chunk *c)
188{
189 unsigned long flags;
190
191 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
192 hlist_del(&c->node);
193 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
194
195 mempool_free(c, s->tracked_chunk_pool);
196}
197
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100198static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
199{
200 struct dm_snap_tracked_chunk *c;
201 struct hlist_node *hn;
202 int found = 0;
203
204 spin_lock_irq(&s->tracked_chunk_lock);
205
206 hlist_for_each_entry(c, hn,
207 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
208 if (c->chunk == chunk) {
209 found = 1;
210 break;
211 }
212 }
213
214 spin_unlock_irq(&s->tracked_chunk_lock);
215
216 return found;
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000220 * This conflicting I/O is extremely improbable in the caller,
221 * so msleep(1) is sufficient and there is no need for a wait queue.
222 */
223static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
224{
225 while (__chunk_is_tracked(s, chunk))
226 msleep(1);
227}
228
229/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * One of these per registered origin, held in the snapshot_origins hash
231 */
232struct origin {
233 /* The origin device */
234 struct block_device *bdev;
235
236 struct list_head hash_list;
237
238 /* List of snapshots for this origin */
239 struct list_head snapshots;
240};
241
242/*
243 * Size of the hash table for origin volumes. If we make this
244 * the size of the minors list then it should be nearly perfect
245 */
246#define ORIGIN_HASH_SIZE 256
247#define ORIGIN_MASK 0xFF
248static struct list_head *_origins;
249static struct rw_semaphore _origins_lock;
250
251static int init_origin_hash(void)
252{
253 int i;
254
255 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
256 GFP_KERNEL);
257 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700258 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -ENOMEM;
260 }
261
262 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
263 INIT_LIST_HEAD(_origins + i);
264 init_rwsem(&_origins_lock);
265
266 return 0;
267}
268
269static void exit_origin_hash(void)
270{
271 kfree(_origins);
272}
273
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100274static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 return bdev->bd_dev & ORIGIN_MASK;
277}
278
279static struct origin *__lookup_origin(struct block_device *origin)
280{
281 struct list_head *ol;
282 struct origin *o;
283
284 ol = &_origins[origin_hash(origin)];
285 list_for_each_entry (o, ol, hash_list)
286 if (bdev_equal(o->bdev, origin))
287 return o;
288
289 return NULL;
290}
291
292static void __insert_origin(struct origin *o)
293{
294 struct list_head *sl = &_origins[origin_hash(o->bdev)];
295 list_add_tail(&o->hash_list, sl);
296}
297
298/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000299 * _origins_lock must be held when calling this function.
300 * Returns number of snapshots registered using the supplied cow device, plus:
301 * snap_src - a snapshot suitable for use as a source of exception handover
302 * snap_dest - a snapshot capable of receiving exception handover.
303 *
304 * Possible return values and states:
305 * 0: NULL, NULL - first new snapshot
306 * 1: snap_src, NULL - normal snapshot
307 * 2: snap_src, snap_dest - waiting for handover
308 * 2: snap_src, NULL - handed over, waiting for old to be deleted
309 * 1: NULL, snap_dest - source got destroyed without handover
310 */
311static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
312 struct dm_snapshot **snap_src,
313 struct dm_snapshot **snap_dest)
314{
315 struct dm_snapshot *s;
316 struct origin *o;
317 int count = 0;
318 int active;
319
320 o = __lookup_origin(snap->origin->bdev);
321 if (!o)
322 goto out;
323
324 list_for_each_entry(s, &o->snapshots, list) {
325 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
326 continue;
327
328 down_read(&s->lock);
329 active = s->active;
330 up_read(&s->lock);
331
332 if (active) {
333 if (snap_src)
334 *snap_src = s;
335 } else if (snap_dest)
336 *snap_dest = s;
337
338 count++;
339 }
340
341out:
342 return count;
343}
344
345/*
346 * On success, returns 1 if this snapshot is a handover destination,
347 * otherwise returns 0.
348 */
349static int __validate_exception_handover(struct dm_snapshot *snap)
350{
351 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
352
353 /* Does snapshot need exceptions handed over to it? */
354 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest) == 2) ||
355 snap_dest) {
356 snap->ti->error = "Snapshot cow pairing for exception "
357 "table handover failed";
358 return -EINVAL;
359 }
360
361 /*
362 * If no snap_src was found, snap cannot become a handover
363 * destination.
364 */
365 if (!snap_src)
366 return 0;
367
368 return 1;
369}
370
371static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
372{
373 struct dm_snapshot *l;
374
375 /* Sort the list according to chunk size, largest-first smallest-last */
376 list_for_each_entry(l, &o->snapshots, list)
377 if (l->store->chunk_size < s->store->chunk_size)
378 break;
379 list_add_tail(&s->list, &l->list);
380}
381
382/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * Make a note of the snapshot and its origin so we can look it
384 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000385 *
386 * Also validate snapshot exception store handovers.
387 * On success, returns 1 if this registration is a handover destination,
388 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390static int register_snapshot(struct dm_snapshot *snap)
391{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000392 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000394 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000396 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
397 if (!new_o)
398 return -ENOMEM;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000402 r = __validate_exception_handover(snap);
403 if (r < 0) {
404 kfree(new_o);
405 goto out;
406 }
407
408 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000409 if (o)
410 kfree(new_o);
411 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000413 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* Initialise the struct */
416 INIT_LIST_HEAD(&o->snapshots);
417 o->bdev = bdev;
418
419 __insert_origin(o);
420 }
421
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000422 __insert_snapshot(o, snap);
423
424out:
425 up_write(&_origins_lock);
426
427 return r;
428}
429
430/*
431 * Move snapshot to correct place in list according to chunk size.
432 */
433static void reregister_snapshot(struct dm_snapshot *s)
434{
435 struct block_device *bdev = s->origin->bdev;
436
437 down_write(&_origins_lock);
438
439 list_del(&s->list);
440 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static void unregister_snapshot(struct dm_snapshot *s)
446{
447 struct origin *o;
448
449 down_write(&_origins_lock);
450 o = __lookup_origin(s->origin->bdev);
451
452 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000453 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 list_del(&o->hash_list);
455 kfree(o);
456 }
457
458 up_write(&_origins_lock);
459}
460
461/*
462 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000463 * The lowest hash_shift bits of the chunk number are ignored, allowing
464 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000466static int dm_exception_table_init(struct dm_exception_table *et,
467 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 unsigned int i;
470
Milan Brozd74f81f2008-02-08 02:11:27 +0000471 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 et->hash_mask = size - 1;
473 et->table = dm_vcalloc(size, sizeof(struct list_head));
474 if (!et->table)
475 return -ENOMEM;
476
477 for (i = 0; i < size; i++)
478 INIT_LIST_HEAD(et->table + i);
479
480 return 0;
481}
482
Jon Brassow3510cb92009-12-10 23:52:11 +0000483static void dm_exception_table_exit(struct dm_exception_table *et,
484 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000487 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int i, size;
489
490 size = et->hash_mask + 1;
491 for (i = 0; i < size; i++) {
492 slot = et->table + i;
493
494 list_for_each_entry_safe (ex, next, slot, hash_list)
495 kmem_cache_free(mem, ex);
496 }
497
498 vfree(et->table);
499}
500
Jon Brassow191437a2009-12-10 23:52:10 +0000501static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Milan Brozd74f81f2008-02-08 02:11:27 +0000503 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Jon Brassow3510cb92009-12-10 23:52:11 +0000506static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 list_del(&e->hash_list);
509}
510
511/*
512 * Return the exception data for a sector, or NULL if not
513 * remapped.
514 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000515static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
516 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000519 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 slot = &et->table[exception_hash(et, chunk)];
522 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000523 if (chunk >= e->old_chunk &&
524 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return e;
526
527 return NULL;
528}
529
Jon Brassow3510cb92009-12-10 23:52:11 +0000530static struct dm_exception *alloc_completed_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000532 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
535 if (!e)
536 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
537
538 return e;
539}
540
Jon Brassow3510cb92009-12-10 23:52:11 +0000541static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 kmem_cache_free(exception_cache, e);
544}
545
Mikulas Patocka92e86812008-07-21 12:00:35 +0100546static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100548 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
549 GFP_NOIO);
550
Mikulas Patocka879129d22008-10-30 13:33:16 +0000551 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100552 pe->snap = s;
553
554 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100557static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000559 struct dm_snapshot *s = pe->snap;
560
561 mempool_free(pe, s->pending_pool);
562 smp_mb__before_atomic_dec();
563 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Jon Brassow3510cb92009-12-10 23:52:11 +0000566static void dm_insert_exception(struct dm_exception_table *eh,
567 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000568{
Milan Brozd74f81f2008-02-08 02:11:27 +0000569 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000570 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000571
572 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
573
574 /* Add immediately if this table doesn't support consecutive chunks */
575 if (!eh->hash_shift)
576 goto out;
577
578 /* List is ordered by old_chunk */
579 list_for_each_entry_reverse(e, l, hash_list) {
580 /* Insert after an existing chunk? */
581 if (new_e->old_chunk == (e->old_chunk +
582 dm_consecutive_chunk_count(e) + 1) &&
583 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
584 dm_consecutive_chunk_count(e) + 1)) {
585 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000586 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000587 return;
588 }
589
590 /* Insert before an existing chunk? */
591 if (new_e->old_chunk == (e->old_chunk - 1) &&
592 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
593 dm_consecutive_chunk_count_inc(e);
594 e->old_chunk--;
595 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000596 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000597 return;
598 }
599
600 if (new_e->old_chunk > e->old_chunk)
601 break;
602 }
603
604out:
605 list_add(&new_e->hash_list, e ? &e->hash_list : l);
606}
607
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000608/*
609 * Callback used by the exception stores to load exceptions when
610 * initialising.
611 */
612static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000614 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000615 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Jon Brassow3510cb92009-12-10 23:52:11 +0000617 e = alloc_completed_exception();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!e)
619 return -ENOMEM;
620
621 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000622
623 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000625
Jon Brassow3510cb92009-12-10 23:52:11 +0000626 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629}
630
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000631#define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
632
633/*
634 * Return a minimum chunk size of all snapshots that have the specified origin.
635 * Return zero if the origin has no snapshots.
636 */
637static sector_t __minimum_chunk_size(struct origin *o)
638{
639 struct dm_snapshot *snap;
640 unsigned chunk_size = 0;
641
642 if (o)
643 list_for_each_entry(snap, &o->snapshots, list)
644 chunk_size = min_not_zero(chunk_size,
645 snap->store->chunk_size);
646
647 return chunk_size;
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/*
651 * Hard coded magic.
652 */
653static int calc_max_buckets(void)
654{
655 /* use a fixed size of 2MB */
656 unsigned long mem = 2 * 1024 * 1024;
657 mem /= sizeof(struct list_head);
658
659 return mem;
660}
661
662/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * Allocate room for a suitable hash table.
664 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100665static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
668
669 /*
670 * Calculate based on the size of the original volume or
671 * the COW volume...
672 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000673 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 origin_dev_size = get_dev_size(s->origin->bdev);
675 max_buckets = calc_max_buckets();
676
Jonathan Brassowfee19982009-04-02 19:55:34 +0100677 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 hash_size = min(hash_size, max_buckets);
679
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000680 if (hash_size < 64)
681 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000682 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000683 if (dm_exception_table_init(&s->complete, hash_size,
684 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -ENOMEM;
686
687 /*
688 * Allocate hash table for in-flight exceptions
689 * Make this smaller than the real hash table
690 */
691 hash_size >>= 3;
692 if (hash_size < 64)
693 hash_size = 64;
694
Jon Brassow3510cb92009-12-10 23:52:11 +0000695 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
696 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -ENOMEM;
698 }
699
700 return 0;
701}
702
703/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
705 */
706static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
707{
708 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100709 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000711 char *origin_path, *cow_path;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100712 unsigned args_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700714 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700715 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000717 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 origin_path = argv[0];
Jonathan Brassowfee19982009-04-02 19:55:34 +0100721 argv++;
722 argc--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +0100725 if (!s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 ti->error = "Cannot allocate snapshot context private "
727 "structure";
728 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000729 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000732 cow_path = argv[0];
733 argv++;
734 argc--;
735
736 r = dm_get_device(ti, cow_path, 0, 0,
737 FMODE_READ | FMODE_WRITE, &s->cow);
738 if (r) {
739 ti->error = "Cannot get COW device";
740 goto bad_cow;
741 }
742
743 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
744 if (r) {
745 ti->error = "Couldn't create exception store";
746 r = -EINVAL;
747 goto bad_store;
748 }
749
750 argv += args_used;
751 argc -= args_used;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
754 if (r) {
755 ti->error = "Cannot get origin device";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100756 goto bad_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000759 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800761 s->active = 0;
Mike Snitzerc26655c2009-12-10 23:52:12 +0000762 s->suspended = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +0000763 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000765 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700766 spin_lock_init(&s->pe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100769 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 ti->error = "Unable to allocate hash table space";
771 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100772 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100775 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (r) {
777 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100778 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Mikulas Patocka92e86812008-07-21 12:00:35 +0100781 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
782 if (!s->pending_pool) {
783 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100784 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +0100785 }
786
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100787 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
788 tracked_chunk_cache);
789 if (!s->tracked_chunk_pool) {
790 ti->error = "Could not allocate tracked_chunk mempool for "
791 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +0100792 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100793 }
794
795 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
796 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
797
798 spin_lock_init(&s->tracked_chunk_lock);
799
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000800 bio_list_init(&s->queued_bios);
801 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
802
803 ti->private = s;
804 ti->num_flush_requests = 1;
805
806 /* Add snapshot to the list of snapshots for this origin */
807 /* Exceptions aren't triggered till snapshot_resume() is called */
808 r = register_snapshot(s);
809 if (r == -ENOMEM) {
810 ti->error = "Snapshot origin struct allocation failed";
811 goto bad_load_and_register;
812 } else if (r < 0) {
813 /* invalid handover, register_snapshot has set ti->error */
814 goto bad_load_and_register;
815 }
816
817 /*
818 * Metadata must only be loaded into one table at once, so skip this
819 * if metadata will be handed over during resume.
820 * Chunk size will be set during the handover - set it to zero to
821 * ensure it's ignored.
822 */
823 if (r > 0) {
824 s->store->chunk_size = 0;
825 return 0;
826 }
827
Jonathan Brassow493df712009-04-02 19:55:31 +0100828 r = s->store->type->read_metadata(s->store, dm_add_exception,
829 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +0100830 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700831 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000832 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +0100833 } else if (r > 0) {
834 s->valid = 0;
835 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700836 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800837
Mikulas Patocka3f2412d2009-10-16 23:18:16 +0100838 if (!s->store->chunk_size) {
839 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000840 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +0100841 }
Jonathan Brassowd0216842009-04-02 19:55:32 +0100842 ti->split_io = s->store->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 return 0;
845
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000846bad_read_metadata:
847 unregister_snapshot(s);
848
Jonathan Brassowfee19982009-04-02 19:55:34 +0100849bad_load_and_register:
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100850 mempool_destroy(s->tracked_chunk_pool);
851
Jonathan Brassowfee19982009-04-02 19:55:34 +0100852bad_tracked_chunk_pool:
Mikulas Patocka92e86812008-07-21 12:00:35 +0100853 mempool_destroy(s->pending_pool);
854
Jonathan Brassowfee19982009-04-02 19:55:34 +0100855bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100856 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Jonathan Brassowfee19982009-04-02 19:55:34 +0100858bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +0000859 dm_exception_table_exit(&s->pending, pending_cache);
860 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Jonathan Brassowfee19982009-04-02 19:55:34 +0100862bad_hash_tables:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 dm_put_device(ti, s->origin);
864
Jonathan Brassowfee19982009-04-02 19:55:34 +0100865bad_origin:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000866 dm_exception_store_destroy(s->store);
867
868bad_store:
869 dm_put_device(ti, s->cow);
870
871bad_cow:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 kfree(s);
873
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000874bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return r;
876}
877
Milan Broz31c93a02006-12-08 02:41:11 -0800878static void __free_exceptions(struct dm_snapshot *s)
879{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100880 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -0800881 s->kcopyd_client = NULL;
882
Jon Brassow3510cb92009-12-10 23:52:11 +0000883 dm_exception_table_exit(&s->pending, pending_cache);
884 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -0800885}
886
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000887static void __handover_exceptions(struct dm_snapshot *snap_src,
888 struct dm_snapshot *snap_dest)
889{
890 union {
891 struct dm_exception_table table_swap;
892 struct dm_exception_store *store_swap;
893 } u;
894
895 /*
896 * Swap all snapshot context information between the two instances.
897 */
898 u.table_swap = snap_dest->complete;
899 snap_dest->complete = snap_src->complete;
900 snap_src->complete = u.table_swap;
901
902 u.store_swap = snap_dest->store;
903 snap_dest->store = snap_src->store;
904 snap_src->store = u.store_swap;
905
906 snap_dest->store->snap = snap_dest;
907 snap_src->store->snap = snap_src;
908
909 snap_dest->ti->split_io = snap_dest->store->chunk_size;
910 snap_dest->valid = snap_src->valid;
911
912 /*
913 * Set source invalid to ensure it receives no further I/O.
914 */
915 snap_src->valid = 0;
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918static void snapshot_dtr(struct dm_target *ti)
919{
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100920#ifdef CONFIG_DM_DEBUG
921 int i;
922#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100923 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000924 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700926 flush_workqueue(ksnapd);
927
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000928 down_read(&_origins_lock);
929 /* Check whether exception handover must be cancelled */
930 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest);
931 if (snap_src && snap_dest && (s == snap_src)) {
932 down_write(&snap_dest->lock);
933 snap_dest->valid = 0;
934 up_write(&snap_dest->lock);
935 DMERR("Cancelling snapshot handover.");
936 }
937 up_read(&_origins_lock);
938
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800939 /* Prevent further origin writes from using this snapshot. */
940 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 unregister_snapshot(s);
942
Mikulas Patocka879129d22008-10-30 13:33:16 +0000943 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000944 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +0000945 /*
946 * Ensure instructions in mempool_destroy aren't reordered
947 * before atomic_read.
948 */
949 smp_mb();
950
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100951#ifdef CONFIG_DM_DEBUG
952 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
953 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
954#endif
955
956 mempool_destroy(s->tracked_chunk_pool);
957
Milan Broz31c93a02006-12-08 02:41:11 -0800958 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Mikulas Patocka92e86812008-07-21 12:00:35 +0100960 mempool_destroy(s->pending_pool);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 dm_put_device(ti, s->origin);
Jonathan Brassowfee19982009-04-02 19:55:34 +0100963
964 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800965
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000966 dm_put_device(ti, s->cow);
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 kfree(s);
969}
970
971/*
972 * Flush a list of buffers.
973 */
974static void flush_bios(struct bio *bio)
975{
976 struct bio *n;
977
978 while (bio) {
979 n = bio->bi_next;
980 bio->bi_next = NULL;
981 generic_make_request(bio);
982 bio = n;
983 }
984}
985
David Howellsc4028952006-11-22 14:57:56 +0000986static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700987{
David Howellsc4028952006-11-22 14:57:56 +0000988 struct dm_snapshot *s =
989 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700990 struct bio *queued_bios;
991 unsigned long flags;
992
993 spin_lock_irqsave(&s->pe_lock, flags);
994 queued_bios = bio_list_get(&s->queued_bios);
995 spin_unlock_irqrestore(&s->pe_lock, flags);
996
997 flush_bios(queued_bios);
998}
999
Mikulas Patocka515ad662009-12-10 23:52:30 +00001000static int do_origin(struct dm_dev *origin, struct bio *bio);
1001
1002/*
1003 * Flush a list of buffers.
1004 */
1005static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1006{
1007 struct bio *n;
1008 int r;
1009
1010 while (bio) {
1011 n = bio->bi_next;
1012 bio->bi_next = NULL;
1013 r = do_origin(s->origin, bio);
1014 if (r == DM_MAPIO_REMAPPED)
1015 generic_make_request(bio);
1016 bio = n;
1017 }
1018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/*
1021 * Error a list of buffers.
1022 */
1023static void error_bios(struct bio *bio)
1024{
1025 struct bio *n;
1026
1027 while (bio) {
1028 n = bio->bi_next;
1029 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001030 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 bio = n;
1032 }
1033}
1034
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001035static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001036{
1037 if (!s->valid)
1038 return;
1039
1040 if (err == -EIO)
1041 DMERR("Invalidating snapshot: Error reading/writing.");
1042 else if (err == -ENOMEM)
1043 DMERR("Invalidating snapshot: Unable to allocate exception.");
1044
Jonathan Brassow493df712009-04-02 19:55:31 +01001045 if (s->store->type->drop_snapshot)
1046 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001047
1048 s->valid = 0;
1049
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001050 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001051}
1052
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001053static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001055 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001057 struct bio *origin_bios = NULL;
1058 struct bio *snapshot_bios = NULL;
1059 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001061 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* Read/write error - snapshot is unusable */
1063 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001064 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001065 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Jon Brassow3510cb92009-12-10 23:52:11 +00001069 e = alloc_completed_exception();
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001070 if (!e) {
1071 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001072 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001073 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001074 goto out;
1075 }
1076 *e = pe->e;
1077
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001078 down_write(&s->lock);
1079 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +00001080 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001081 error = 1;
1082 goto out;
1083 }
1084
Mike Snitzer615d1eb2009-12-10 23:52:29 +00001085 /* Check for conflicting reads */
1086 __check_for_conflicting_io(s, pe->e.old_chunk);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001087
1088 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001089 * Add a proper exception, and remove the
1090 * in-flight exception from the list.
1091 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001092 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 out:
Jon Brassow3510cb92009-12-10 23:52:11 +00001095 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001096 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001097 origin_bios = bio_list_get(&pe->origin_bios);
1098 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001100 up_write(&s->lock);
1101
1102 /* Submit any pending write bios */
1103 if (error)
1104 error_bios(snapshot_bios);
1105 else
1106 flush_bios(snapshot_bios);
1107
Mikulas Patocka515ad662009-12-10 23:52:30 +00001108 retry_origin_bios(s, origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111static void commit_callback(void *context, int success)
1112{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001113 struct dm_snap_pending_exception *pe = context;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 pending_complete(pe, success);
1116}
1117
1118/*
1119 * Called when the copy I/O has finished. kcopyd actually runs
1120 * this code so don't block.
1121 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001122static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001124 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct dm_snapshot *s = pe->snap;
1126
1127 if (read_err || write_err)
1128 pending_complete(pe, 0);
1129
1130 else
1131 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +01001132 s->store->type->commit_exception(s->store, &pe->e,
1133 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
1136/*
1137 * Dispatches the copy operation to kcopyd.
1138 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001139static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001142 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 struct block_device *bdev = s->origin->bdev;
1144 sector_t dev_size;
1145
1146 dev_size = get_dev_size(bdev);
1147
1148 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001149 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001150 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001152 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001153 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 dest.count = src.count;
1155
1156 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001157 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 &src, 1, &dest, 0, copy_callback, pe);
1159}
1160
Mikulas Patocka29138082009-04-02 19:55:25 +01001161static struct dm_snap_pending_exception *
1162__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1163{
Jon Brassow3510cb92009-12-10 23:52:11 +00001164 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001165
1166 if (!e)
1167 return NULL;
1168
1169 return container_of(e, struct dm_snap_pending_exception, e);
1170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172/*
1173 * Looks to see if this snapshot already has a pending exception
1174 * for this chunk, otherwise it allocates a new one and inserts
1175 * it into the pending table.
1176 *
1177 * NOTE: a write lock must be held on snap->lock before calling
1178 * this.
1179 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001180static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001181__find_pending_exception(struct dm_snapshot *s,
1182 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Mikulas Patockac6621392009-04-02 19:55:25 +01001184 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001185
Mikulas Patocka29138082009-04-02 19:55:25 +01001186 pe2 = __lookup_pending_exception(s, chunk);
1187 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001188 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001189 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001190 }
1191
1192 pe->e.old_chunk = chunk;
1193 bio_list_init(&pe->origin_bios);
1194 bio_list_init(&pe->snapshot_bios);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001195 pe->started = 0;
1196
Jonathan Brassow493df712009-04-02 19:55:31 +01001197 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001198 free_pending_exception(pe);
1199 return NULL;
1200 }
1201
Jon Brassow3510cb92009-12-10 23:52:11 +00001202 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return pe;
1205}
1206
Jon Brassow1d4989c2009-12-10 23:52:10 +00001207static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001208 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001210 bio->bi_bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001211 bio->bi_sector = chunk_to_sector(s->store,
1212 dm_chunk_number(e->new_chunk) +
1213 (chunk - e->old_chunk)) +
1214 (bio->bi_sector &
1215 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218static int snapshot_map(struct dm_target *ti, struct bio *bio,
1219 union map_info *map_context)
1220{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001221 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001222 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001223 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001225 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001227 if (unlikely(bio_empty_barrier(bio))) {
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001228 bio->bi_bdev = s->cow->bdev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001229 return DM_MAPIO_REMAPPED;
1230 }
1231
Jonathan Brassow71fab002009-04-02 19:55:33 +01001232 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001235 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001237 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001239 /* FIXME: should only take write lock if we need
1240 * to copy an exception */
1241 down_write(&s->lock);
1242
1243 if (!s->valid) {
1244 r = -EIO;
1245 goto out_unlock;
1246 }
1247
1248 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001249 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001250 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001251 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001252 goto out_unlock;
1253 }
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /*
1256 * Write to snapshot - higher level takes care of RW/RO
1257 * flags so we should only get this if we are
1258 * writeable.
1259 */
1260 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001261 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001262 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001263 up_write(&s->lock);
1264 pe = alloc_pending_exception(s);
1265 down_write(&s->lock);
1266
1267 if (!s->valid) {
1268 free_pending_exception(pe);
1269 r = -EIO;
1270 goto out_unlock;
1271 }
1272
Jon Brassow3510cb92009-12-10 23:52:11 +00001273 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001274 if (e) {
1275 free_pending_exception(pe);
1276 remap_exception(s, e, bio, chunk);
1277 goto out_unlock;
1278 }
1279
Mikulas Patockac6621392009-04-02 19:55:25 +01001280 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001281 if (!pe) {
1282 __invalidate_snapshot(s, -ENOMEM);
1283 r = -EIO;
1284 goto out_unlock;
1285 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001286 }
1287
Milan Brozd74f81f2008-02-08 02:11:27 +00001288 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001289 bio_list_add(&pe->snapshot_bios, bio);
1290
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001291 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001292
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001293 if (!pe->started) {
1294 /* this is protected by snap->lock */
1295 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001296 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001297 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001298 goto out;
1299 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001300 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001301 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001302 map_context->ptr = track_chunk(s, chunk);
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001305 out_unlock:
1306 up_write(&s->lock);
1307 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return r;
1309}
1310
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001311/*
1312 * A snapshot-merge target behaves like a combination of a snapshot
1313 * target and a snapshot-origin target. It only generates new
1314 * exceptions in other snapshots and not in the one that is being
1315 * merged.
1316 *
1317 * For each chunk, if there is an existing exception, it is used to
1318 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1319 * which in turn might generate exceptions in other snapshots.
1320 */
1321static int snapshot_merge_map(struct dm_target *ti, struct bio *bio,
1322 union map_info *map_context)
1323{
1324 struct dm_exception *e;
1325 struct dm_snapshot *s = ti->private;
1326 int r = DM_MAPIO_REMAPPED;
1327 chunk_t chunk;
1328
1329 chunk = sector_to_chunk(s->store, bio->bi_sector);
1330
1331 down_read(&s->lock);
1332
1333 /* Full snapshots are not usable */
1334 if (!s->valid) {
1335 r = -EIO;
1336 goto out_unlock;
1337 }
1338
1339 /* If the block is already remapped - use that */
1340 e = dm_lookup_exception(&s->complete, chunk);
1341 if (e) {
1342 remap_exception(s, e, bio, chunk);
1343 goto out_unlock;
1344 }
1345
1346 bio->bi_bdev = s->origin->bdev;
1347
1348 if (bio_rw(bio) == WRITE) {
1349 up_read(&s->lock);
1350 return do_origin(s->origin, bio);
1351 }
1352
1353out_unlock:
1354 up_read(&s->lock);
1355
1356 return r;
1357}
1358
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001359static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1360 int error, union map_info *map_context)
1361{
1362 struct dm_snapshot *s = ti->private;
1363 struct dm_snap_tracked_chunk *c = map_context->ptr;
1364
1365 if (c)
1366 stop_tracking_chunk(s, c);
1367
1368 return 0;
1369}
1370
Mike Snitzerc26655c2009-12-10 23:52:12 +00001371static void snapshot_postsuspend(struct dm_target *ti)
1372{
1373 struct dm_snapshot *s = ti->private;
1374
1375 down_write(&s->lock);
1376 s->suspended = 1;
1377 up_write(&s->lock);
1378}
1379
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001380static int snapshot_preresume(struct dm_target *ti)
1381{
1382 int r = 0;
1383 struct dm_snapshot *s = ti->private;
1384 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1385
1386 down_read(&_origins_lock);
1387 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest);
1388 if (snap_src && snap_dest) {
1389 down_read(&snap_src->lock);
1390 if (s == snap_src) {
1391 DMERR("Unable to resume snapshot source until "
1392 "handover completes.");
1393 r = -EINVAL;
1394 } else if (!snap_src->suspended) {
1395 DMERR("Unable to perform snapshot handover until "
1396 "source is suspended.");
1397 r = -EINVAL;
1398 }
1399 up_read(&snap_src->lock);
1400 }
1401 up_read(&_origins_lock);
1402
1403 return r;
1404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406static void snapshot_resume(struct dm_target *ti)
1407{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001408 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001409 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1410
1411 down_read(&_origins_lock);
1412 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest);
1413 if (snap_src && snap_dest) {
1414 down_write(&snap_src->lock);
1415 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1416 __handover_exceptions(snap_src, snap_dest);
1417 up_write(&snap_dest->lock);
1418 up_write(&snap_src->lock);
1419 }
1420 up_read(&_origins_lock);
1421
1422 /* Now we have correct chunk size, reregister */
1423 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001425 down_write(&s->lock);
1426 s->active = 1;
Mike Snitzerc26655c2009-12-10 23:52:12 +00001427 s->suspended = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001428 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431static int snapshot_status(struct dm_target *ti, status_type_t type,
1432 char *result, unsigned int maxlen)
1433{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001434 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001435 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 switch (type) {
1438 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00001439
1440 down_write(&snap->lock);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001443 DMEMIT("Invalid");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00001445 if (snap->store->type->usage) {
1446 sector_t total_sectors, sectors_allocated,
1447 metadata_sectors;
1448 snap->store->type->usage(snap->store,
1449 &total_sectors,
1450 &sectors_allocated,
1451 &metadata_sectors);
1452 DMEMIT("%llu/%llu %llu",
1453 (unsigned long long)sectors_allocated,
1454 (unsigned long long)total_sectors,
1455 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001458 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00001460
1461 up_write(&snap->lock);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464
1465 case STATUSTYPE_TABLE:
1466 /*
1467 * kdevname returns a static pointer so we need
1468 * to make private copies if the output is to
1469 * make sense.
1470 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001471 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001472 snap->store->type->status(snap->store, type, result + sz,
1473 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475 }
1476
1477 return 0;
1478}
1479
Mike Snitzer8811f462009-09-04 20:40:19 +01001480static int snapshot_iterate_devices(struct dm_target *ti,
1481 iterate_devices_callout_fn fn, void *data)
1482{
1483 struct dm_snapshot *snap = ti->private;
1484
1485 return fn(ti, snap->origin, 0, ti->len, data);
1486}
1487
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/*-----------------------------------------------------------------
1490 * Origin methods
1491 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001492
1493/*
1494 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
1495 * supplied bio was ignored. The caller may submit it immediately.
1496 * (No remapping actually occurs as the origin is always a direct linear
1497 * map.)
1498 *
1499 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
1500 * and any supplied bio is added to a list to be submitted once all
1501 * the necessary exceptions exist.
1502 */
1503static int __origin_write(struct list_head *snapshots, sector_t sector,
1504 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Mikulas Patocka515ad662009-12-10 23:52:30 +00001506 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001508 struct dm_exception *e;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001509 struct dm_snap_pending_exception *pe;
1510 struct dm_snap_pending_exception *pe_to_start_now = NULL;
1511 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 chunk_t chunk;
1513
1514 /* Do all the snapshots on this origin */
1515 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001516 /*
1517 * Don't make new exceptions in a merging snapshot
1518 * because it has effectively been deleted
1519 */
1520 if (dm_target_is_snapshot_merge(snap->ti))
1521 continue;
1522
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001523 down_write(&snap->lock);
1524
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001525 /* Only deal with valid and active snapshots */
1526 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001527 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001529 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001530 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001531 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 /*
1534 * Remember, different snapshots can have
1535 * different chunk sizes.
1536 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001537 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /*
1540 * Check exception table to see if block
1541 * is already remapped in this snapshot
1542 * and trigger an exception if not.
1543 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001544 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001545 if (e)
1546 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Mikulas Patocka29138082009-04-02 19:55:25 +01001548 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001549 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001550 up_write(&snap->lock);
1551 pe = alloc_pending_exception(snap);
1552 down_write(&snap->lock);
1553
1554 if (!snap->valid) {
1555 free_pending_exception(pe);
1556 goto next_snapshot;
1557 }
1558
Jon Brassow3510cb92009-12-10 23:52:11 +00001559 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001560 if (e) {
1561 free_pending_exception(pe);
1562 goto next_snapshot;
1563 }
1564
Mikulas Patockac6621392009-04-02 19:55:25 +01001565 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001566 if (!pe) {
1567 __invalidate_snapshot(snap, -ENOMEM);
1568 goto next_snapshot;
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
Mikulas Patocka515ad662009-12-10 23:52:30 +00001572 r = DM_MAPIO_SUBMITTED;
1573
1574 /*
1575 * If an origin bio was supplied, queue it to wait for the
1576 * completion of this exception, and start this one last,
1577 * at the end of the function.
1578 */
1579 if (bio) {
1580 bio_list_add(&pe->origin_bios, bio);
1581 bio = NULL;
1582
1583 if (!pe->started) {
1584 pe->started = 1;
1585 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001586 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001587 }
1588
1589 if (!pe->started) {
1590 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001591 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001592 }
1593
1594 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Mikulas Patocka515ad662009-12-10 23:52:30 +00001597 if (pe_to_start_now) {
1598 start_copy(pe_to_start_now);
1599 pe_to_start_now = NULL;
1600 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001601 }
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00001604 * Submit the exception against which the bio is queued last,
1605 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00001607 if (pe_to_start_last)
1608 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 return r;
1611}
1612
1613/*
1614 * Called on a write from the origin driver.
1615 */
1616static int do_origin(struct dm_dev *origin, struct bio *bio)
1617{
1618 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001619 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 down_read(&_origins_lock);
1622 o = __lookup_origin(origin->bdev);
1623 if (o)
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001624 r = __origin_write(&o->snapshots, bio->bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 up_read(&_origins_lock);
1626
1627 return r;
1628}
1629
1630/*
1631 * Origin: maps a linear range of a device, with hooks for snapshotting.
1632 */
1633
1634/*
1635 * Construct an origin mapping: <dev_path>
1636 * The context for an origin is merely a 'struct dm_dev *'
1637 * pointing to the real device.
1638 */
1639static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1640{
1641 int r;
1642 struct dm_dev *dev;
1643
1644 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001645 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return -EINVAL;
1647 }
1648
1649 r = dm_get_device(ti, argv[0], 0, ti->len,
1650 dm_table_get_mode(ti->table), &dev);
1651 if (r) {
1652 ti->error = "Cannot get target device";
1653 return r;
1654 }
1655
1656 ti->private = dev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001657 ti->num_flush_requests = 1;
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return 0;
1660}
1661
1662static void origin_dtr(struct dm_target *ti)
1663{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001664 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 dm_put_device(ti, dev);
1666}
1667
1668static int origin_map(struct dm_target *ti, struct bio *bio,
1669 union map_info *map_context)
1670{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001671 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 bio->bi_bdev = dev->bdev;
1673
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001674 if (unlikely(bio_empty_barrier(bio)))
1675 return DM_MAPIO_REMAPPED;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001678 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681/*
1682 * Set the target "split_io" field to the minimum of all the snapshots'
1683 * chunk sizes.
1684 */
1685static void origin_resume(struct dm_target *ti)
1686{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001687 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 down_read(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Mikulas Patocka7e201b32009-12-10 23:52:08 +00001691 ti->split_io = __minimum_chunk_size(__lookup_origin(dev->bdev));
1692
1693 up_read(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1697 unsigned int maxlen)
1698{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001699 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 switch (type) {
1702 case STATUSTYPE_INFO:
1703 result[0] = '\0';
1704 break;
1705
1706 case STATUSTYPE_TABLE:
1707 snprintf(result, maxlen, "%s", dev->name);
1708 break;
1709 }
1710
1711 return 0;
1712}
1713
Mike Snitzer8811f462009-09-04 20:40:19 +01001714static int origin_iterate_devices(struct dm_target *ti,
1715 iterate_devices_callout_fn fn, void *data)
1716{
1717 struct dm_dev *dev = ti->private;
1718
1719 return fn(ti, dev, 0, ti->len, data);
1720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722static struct target_type origin_target = {
1723 .name = "snapshot-origin",
Mike Snitzer8811f462009-09-04 20:40:19 +01001724 .version = {1, 7, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 .module = THIS_MODULE,
1726 .ctr = origin_ctr,
1727 .dtr = origin_dtr,
1728 .map = origin_map,
1729 .resume = origin_resume,
1730 .status = origin_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01001731 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732};
1733
1734static struct target_type snapshot_target = {
1735 .name = "snapshot",
Mike Snitzerc26655c2009-12-10 23:52:12 +00001736 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 .module = THIS_MODULE,
1738 .ctr = snapshot_ctr,
1739 .dtr = snapshot_dtr,
1740 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001741 .end_io = snapshot_end_io,
Mike Snitzerc26655c2009-12-10 23:52:12 +00001742 .postsuspend = snapshot_postsuspend,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001743 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 .resume = snapshot_resume,
1745 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01001746 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747};
1748
Mikulas Patockad698aa42009-12-10 23:52:30 +00001749static struct target_type merge_target = {
1750 .name = dm_snapshot_merge_target_name,
1751 .version = {1, 0, 0},
1752 .module = THIS_MODULE,
1753 .ctr = snapshot_ctr,
1754 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001755 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00001756 .end_io = snapshot_end_io,
1757 .postsuspend = snapshot_postsuspend,
1758 .preresume = snapshot_preresume,
1759 .resume = snapshot_resume,
1760 .status = snapshot_status,
1761 .iterate_devices = snapshot_iterate_devices,
1762};
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764static int __init dm_snapshot_init(void)
1765{
1766 int r;
1767
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001768 r = dm_exception_store_init();
1769 if (r) {
1770 DMERR("Failed to initialize exception stores");
1771 return r;
1772 }
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 r = dm_register_target(&snapshot_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00001775 if (r < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 DMERR("snapshot target register failed %d", r);
Jonathan Brassow034a1862009-10-16 23:18:14 +01001777 goto bad_register_snapshot_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779
1780 r = dm_register_target(&origin_target);
1781 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001782 DMERR("Origin target register failed %d", r);
Mikulas Patockad698aa42009-12-10 23:52:30 +00001783 goto bad_register_origin_target;
1784 }
1785
1786 r = dm_register_target(&merge_target);
1787 if (r < 0) {
1788 DMERR("Merge target register failed %d", r);
1789 goto bad_register_merge_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791
1792 r = init_origin_hash();
1793 if (r) {
1794 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00001795 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
Jon Brassow1d4989c2009-12-10 23:52:10 +00001798 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (!exception_cache) {
1800 DMERR("Couldn't create exception cache.");
1801 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00001802 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001805 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (!pending_cache) {
1807 DMERR("Couldn't create pending cache.");
1808 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00001809 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001812 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1813 if (!tracked_chunk_cache) {
1814 DMERR("Couldn't create cache to track chunks in use.");
1815 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00001816 goto bad_tracked_chunk_cache;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001817 }
1818
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001819 ksnapd = create_singlethread_workqueue("ksnapd");
1820 if (!ksnapd) {
1821 DMERR("Failed to create ksnapd workqueue.");
1822 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001823 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001824 }
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return 0;
1827
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001828bad_pending_pool:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001829 kmem_cache_destroy(tracked_chunk_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00001830bad_tracked_chunk_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 kmem_cache_destroy(pending_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00001832bad_pending_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 kmem_cache_destroy(exception_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00001834bad_exception_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 exit_origin_hash();
Mikulas Patockad698aa42009-12-10 23:52:30 +00001836bad_origin_hash:
1837 dm_unregister_target(&merge_target);
1838bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00001840bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01001842bad_register_snapshot_target:
1843 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return r;
1846}
1847
1848static void __exit dm_snapshot_exit(void)
1849{
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001850 destroy_workqueue(ksnapd);
1851
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001852 dm_unregister_target(&snapshot_target);
1853 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00001854 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 kmem_cache_destroy(pending_cache);
1858 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001859 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001860
1861 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864/* Module hooks */
1865module_init(dm_snapshot_init);
1866module_exit(dm_snapshot_exit);
1867
1868MODULE_DESCRIPTION(DM_NAME " snapshot target");
1869MODULE_AUTHOR("Joe Thornber");
1870MODULE_LICENSE("GPL");