]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
1da177e4 | 10 | #include <linux/device-mapper.h> |
90fa1527 | 11 | #include <linux/delay.h> |
1da177e4 LT |
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> | |
6f3c3f0a | 20 | #include <linux/log2.h> |
a765e20e | 21 | #include <linux/dm-kcopyd.h> |
ccc45ea8 | 22 | #include <linux/workqueue.h> |
1da177e4 | 23 | |
aea53d92 | 24 | #include "dm-exception-store.h" |
1da177e4 | 25 | |
72d94861 AK |
26 | #define DM_MSG_PREFIX "snapshots" |
27 | ||
1da177e4 LT |
28 | /* |
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 | /* | |
8ee2767a | 39 | * Reserve 1MB for each snapshot initially (with minimum of 1 page). |
1da177e4 | 40 | */ |
8ee2767a | 41 | #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1) |
1da177e4 | 42 | |
cd45daff MP |
43 | /* |
44 | * The size of the mempool used to track chunks in use. | |
45 | */ | |
46 | #define MIN_IOS 256 | |
47 | ||
ccc45ea8 JB |
48 | #define DM_TRACKED_CHUNK_HASH_SIZE 16 |
49 | #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \ | |
50 | (DM_TRACKED_CHUNK_HASH_SIZE - 1)) | |
51 | ||
191437a5 | 52 | struct dm_exception_table { |
ccc45ea8 JB |
53 | uint32_t hash_mask; |
54 | unsigned hash_shift; | |
55 | struct list_head *table; | |
56 | }; | |
57 | ||
58 | struct dm_snapshot { | |
59 | struct rw_semaphore lock; | |
60 | ||
61 | struct dm_dev *origin; | |
fc56f6fb MS |
62 | struct dm_dev *cow; |
63 | ||
64 | struct dm_target *ti; | |
ccc45ea8 JB |
65 | |
66 | /* List of snapshots per Origin */ | |
67 | struct list_head list; | |
68 | ||
69 | /* You can't use a snapshot if this is 0 (e.g. if full) */ | |
70 | int valid; | |
71 | ||
72 | /* Origin writes don't trigger exceptions until this is set */ | |
73 | int active; | |
74 | ||
c26655ca MS |
75 | /* Whether or not owning mapped_device is suspended */ |
76 | int suspended; | |
77 | ||
ccc45ea8 JB |
78 | mempool_t *pending_pool; |
79 | ||
80 | atomic_t pending_exceptions_count; | |
81 | ||
191437a5 JB |
82 | struct dm_exception_table pending; |
83 | struct dm_exception_table complete; | |
ccc45ea8 JB |
84 | |
85 | /* | |
86 | * pe_lock protects all pending_exception operations and access | |
87 | * as well as the snapshot_bios list. | |
88 | */ | |
89 | spinlock_t pe_lock; | |
90 | ||
91 | /* The on disk metadata handler */ | |
92 | struct dm_exception_store *store; | |
93 | ||
94 | struct dm_kcopyd_client *kcopyd_client; | |
95 | ||
96 | /* Queue of snapshot writes for ksnapd to flush */ | |
97 | struct bio_list queued_bios; | |
98 | struct work_struct queued_bios_work; | |
99 | ||
100 | /* Chunks with outstanding reads */ | |
101 | mempool_t *tracked_chunk_pool; | |
102 | spinlock_t tracked_chunk_lock; | |
103 | struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE]; | |
104 | }; | |
105 | ||
fc56f6fb MS |
106 | struct dm_dev *dm_snap_cow(struct dm_snapshot *s) |
107 | { | |
108 | return s->cow; | |
109 | } | |
110 | EXPORT_SYMBOL(dm_snap_cow); | |
111 | ||
c642f9e0 | 112 | static struct workqueue_struct *ksnapd; |
c4028958 | 113 | static void flush_queued_bios(struct work_struct *work); |
ca3a931f | 114 | |
ccc45ea8 JB |
115 | static sector_t chunk_to_sector(struct dm_exception_store *store, |
116 | chunk_t chunk) | |
117 | { | |
118 | return chunk << store->chunk_shift; | |
119 | } | |
120 | ||
121 | static int bdev_equal(struct block_device *lhs, struct block_device *rhs) | |
122 | { | |
123 | /* | |
124 | * There is only ever one instance of a particular block | |
125 | * device so we can compare pointers safely. | |
126 | */ | |
127 | return lhs == rhs; | |
128 | } | |
129 | ||
028867ac | 130 | struct dm_snap_pending_exception { |
1d4989c8 | 131 | struct dm_exception e; |
1da177e4 LT |
132 | |
133 | /* | |
134 | * Origin buffers waiting for this to complete are held | |
135 | * in a bio list | |
136 | */ | |
137 | struct bio_list origin_bios; | |
138 | struct bio_list snapshot_bios; | |
139 | ||
eccf0817 AK |
140 | /* |
141 | * Short-term queue of pending exceptions prior to submission. | |
142 | */ | |
143 | struct list_head list; | |
144 | ||
1da177e4 | 145 | /* |
b4b610f6 | 146 | * The primary pending_exception is the one that holds |
4b832e8d | 147 | * the ref_count and the list of origin_bios for a |
b4b610f6 AK |
148 | * group of pending_exceptions. It is always last to get freed. |
149 | * These fields get set up when writing to the origin. | |
1da177e4 | 150 | */ |
028867ac | 151 | struct dm_snap_pending_exception *primary_pe; |
b4b610f6 AK |
152 | |
153 | /* | |
154 | * Number of pending_exceptions processing this chunk. | |
155 | * When this drops to zero we must complete the origin bios. | |
156 | * If incrementing or decrementing this, hold pe->snap->lock for | |
157 | * the sibling concerned and not pe->primary_pe->snap->lock unless | |
158 | * they are the same. | |
159 | */ | |
4b832e8d | 160 | atomic_t ref_count; |
1da177e4 LT |
161 | |
162 | /* Pointer back to snapshot context */ | |
163 | struct dm_snapshot *snap; | |
164 | ||
165 | /* | |
166 | * 1 indicates the exception has already been sent to | |
167 | * kcopyd. | |
168 | */ | |
169 | int started; | |
170 | }; | |
171 | ||
172 | /* | |
173 | * Hash table mapping origin volumes to lists of snapshots and | |
174 | * a lock to protect it | |
175 | */ | |
e18b890b CL |
176 | static struct kmem_cache *exception_cache; |
177 | static struct kmem_cache *pending_cache; | |
1da177e4 | 178 | |
cd45daff MP |
179 | struct dm_snap_tracked_chunk { |
180 | struct hlist_node node; | |
181 | chunk_t chunk; | |
182 | }; | |
183 | ||
184 | static struct kmem_cache *tracked_chunk_cache; | |
185 | ||
186 | static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s, | |
187 | chunk_t chunk) | |
188 | { | |
189 | struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool, | |
190 | GFP_NOIO); | |
191 | unsigned long flags; | |
192 | ||
193 | c->chunk = chunk; | |
194 | ||
195 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); | |
196 | hlist_add_head(&c->node, | |
197 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]); | |
198 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); | |
199 | ||
200 | return c; | |
201 | } | |
202 | ||
203 | static void stop_tracking_chunk(struct dm_snapshot *s, | |
204 | struct dm_snap_tracked_chunk *c) | |
205 | { | |
206 | unsigned long flags; | |
207 | ||
208 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); | |
209 | hlist_del(&c->node); | |
210 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); | |
211 | ||
212 | mempool_free(c, s->tracked_chunk_pool); | |
213 | } | |
214 | ||
a8d41b59 MP |
215 | static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk) |
216 | { | |
217 | struct dm_snap_tracked_chunk *c; | |
218 | struct hlist_node *hn; | |
219 | int found = 0; | |
220 | ||
221 | spin_lock_irq(&s->tracked_chunk_lock); | |
222 | ||
223 | hlist_for_each_entry(c, hn, | |
224 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) { | |
225 | if (c->chunk == chunk) { | |
226 | found = 1; | |
227 | break; | |
228 | } | |
229 | } | |
230 | ||
231 | spin_unlock_irq(&s->tracked_chunk_lock); | |
232 | ||
233 | return found; | |
234 | } | |
235 | ||
1da177e4 LT |
236 | /* |
237 | * One of these per registered origin, held in the snapshot_origins hash | |
238 | */ | |
239 | struct origin { | |
240 | /* The origin device */ | |
241 | struct block_device *bdev; | |
242 | ||
243 | struct list_head hash_list; | |
244 | ||
245 | /* List of snapshots for this origin */ | |
246 | struct list_head snapshots; | |
247 | }; | |
248 | ||
249 | /* | |
250 | * Size of the hash table for origin volumes. If we make this | |
251 | * the size of the minors list then it should be nearly perfect | |
252 | */ | |
253 | #define ORIGIN_HASH_SIZE 256 | |
254 | #define ORIGIN_MASK 0xFF | |
255 | static struct list_head *_origins; | |
256 | static struct rw_semaphore _origins_lock; | |
257 | ||
258 | static int init_origin_hash(void) | |
259 | { | |
260 | int i; | |
261 | ||
262 | _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head), | |
263 | GFP_KERNEL); | |
264 | if (!_origins) { | |
72d94861 | 265 | DMERR("unable to allocate memory"); |
1da177e4 LT |
266 | return -ENOMEM; |
267 | } | |
268 | ||
269 | for (i = 0; i < ORIGIN_HASH_SIZE; i++) | |
270 | INIT_LIST_HEAD(_origins + i); | |
271 | init_rwsem(&_origins_lock); | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
276 | static void exit_origin_hash(void) | |
277 | { | |
278 | kfree(_origins); | |
279 | } | |
280 | ||
028867ac | 281 | static unsigned origin_hash(struct block_device *bdev) |
1da177e4 LT |
282 | { |
283 | return bdev->bd_dev & ORIGIN_MASK; | |
284 | } | |
285 | ||
286 | static struct origin *__lookup_origin(struct block_device *origin) | |
287 | { | |
288 | struct list_head *ol; | |
289 | struct origin *o; | |
290 | ||
291 | ol = &_origins[origin_hash(origin)]; | |
292 | list_for_each_entry (o, ol, hash_list) | |
293 | if (bdev_equal(o->bdev, origin)) | |
294 | return o; | |
295 | ||
296 | return NULL; | |
297 | } | |
298 | ||
299 | static void __insert_origin(struct origin *o) | |
300 | { | |
301 | struct list_head *sl = &_origins[origin_hash(o->bdev)]; | |
302 | list_add_tail(&o->hash_list, sl); | |
303 | } | |
304 | ||
305 | /* | |
306 | * Make a note of the snapshot and its origin so we can look it | |
307 | * up when the origin has a write on it. | |
308 | */ | |
309 | static int register_snapshot(struct dm_snapshot *snap) | |
310 | { | |
6d45d93e | 311 | struct dm_snapshot *l; |
60c856c8 | 312 | struct origin *o, *new_o; |
1da177e4 LT |
313 | struct block_device *bdev = snap->origin->bdev; |
314 | ||
60c856c8 MP |
315 | new_o = kmalloc(sizeof(*new_o), GFP_KERNEL); |
316 | if (!new_o) | |
317 | return -ENOMEM; | |
318 | ||
1da177e4 LT |
319 | down_write(&_origins_lock); |
320 | o = __lookup_origin(bdev); | |
321 | ||
60c856c8 MP |
322 | if (o) |
323 | kfree(new_o); | |
324 | else { | |
1da177e4 | 325 | /* New origin */ |
60c856c8 | 326 | o = new_o; |
1da177e4 LT |
327 | |
328 | /* Initialise the struct */ | |
329 | INIT_LIST_HEAD(&o->snapshots); | |
330 | o->bdev = bdev; | |
331 | ||
332 | __insert_origin(o); | |
333 | } | |
334 | ||
6d45d93e MP |
335 | /* Sort the list according to chunk size, largest-first smallest-last */ |
336 | list_for_each_entry(l, &o->snapshots, list) | |
337 | if (l->store->chunk_size < snap->store->chunk_size) | |
338 | break; | |
339 | list_add_tail(&snap->list, &l->list); | |
1da177e4 LT |
340 | |
341 | up_write(&_origins_lock); | |
342 | return 0; | |
343 | } | |
344 | ||
345 | static void unregister_snapshot(struct dm_snapshot *s) | |
346 | { | |
347 | struct origin *o; | |
348 | ||
349 | down_write(&_origins_lock); | |
350 | o = __lookup_origin(s->origin->bdev); | |
351 | ||
352 | list_del(&s->list); | |
353 | if (list_empty(&o->snapshots)) { | |
354 | list_del(&o->hash_list); | |
355 | kfree(o); | |
356 | } | |
357 | ||
358 | up_write(&_origins_lock); | |
359 | } | |
360 | ||
361 | /* | |
362 | * Implementation of the exception hash tables. | |
d74f81f8 MB |
363 | * The lowest hash_shift bits of the chunk number are ignored, allowing |
364 | * some consecutive chunks to be grouped together. | |
1da177e4 | 365 | */ |
3510cb94 JB |
366 | static int dm_exception_table_init(struct dm_exception_table *et, |
367 | uint32_t size, unsigned hash_shift) | |
1da177e4 LT |
368 | { |
369 | unsigned int i; | |
370 | ||
d74f81f8 | 371 | et->hash_shift = hash_shift; |
1da177e4 LT |
372 | et->hash_mask = size - 1; |
373 | et->table = dm_vcalloc(size, sizeof(struct list_head)); | |
374 | if (!et->table) | |
375 | return -ENOMEM; | |
376 | ||
377 | for (i = 0; i < size; i++) | |
378 | INIT_LIST_HEAD(et->table + i); | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
3510cb94 JB |
383 | static void dm_exception_table_exit(struct dm_exception_table *et, |
384 | struct kmem_cache *mem) | |
1da177e4 LT |
385 | { |
386 | struct list_head *slot; | |
1d4989c8 | 387 | struct dm_exception *ex, *next; |
1da177e4 LT |
388 | int i, size; |
389 | ||
390 | size = et->hash_mask + 1; | |
391 | for (i = 0; i < size; i++) { | |
392 | slot = et->table + i; | |
393 | ||
394 | list_for_each_entry_safe (ex, next, slot, hash_list) | |
395 | kmem_cache_free(mem, ex); | |
396 | } | |
397 | ||
398 | vfree(et->table); | |
399 | } | |
400 | ||
191437a5 | 401 | static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk) |
1da177e4 | 402 | { |
d74f81f8 | 403 | return (chunk >> et->hash_shift) & et->hash_mask; |
1da177e4 LT |
404 | } |
405 | ||
3510cb94 | 406 | static void dm_remove_exception(struct dm_exception *e) |
1da177e4 LT |
407 | { |
408 | list_del(&e->hash_list); | |
409 | } | |
410 | ||
411 | /* | |
412 | * Return the exception data for a sector, or NULL if not | |
413 | * remapped. | |
414 | */ | |
3510cb94 JB |
415 | static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et, |
416 | chunk_t chunk) | |
1da177e4 LT |
417 | { |
418 | struct list_head *slot; | |
1d4989c8 | 419 | struct dm_exception *e; |
1da177e4 LT |
420 | |
421 | slot = &et->table[exception_hash(et, chunk)]; | |
422 | list_for_each_entry (e, slot, hash_list) | |
d74f81f8 MB |
423 | if (chunk >= e->old_chunk && |
424 | chunk <= e->old_chunk + dm_consecutive_chunk_count(e)) | |
1da177e4 LT |
425 | return e; |
426 | ||
427 | return NULL; | |
428 | } | |
429 | ||
3510cb94 | 430 | static struct dm_exception *alloc_completed_exception(void) |
1da177e4 | 431 | { |
1d4989c8 | 432 | struct dm_exception *e; |
1da177e4 LT |
433 | |
434 | e = kmem_cache_alloc(exception_cache, GFP_NOIO); | |
435 | if (!e) | |
436 | e = kmem_cache_alloc(exception_cache, GFP_ATOMIC); | |
437 | ||
438 | return e; | |
439 | } | |
440 | ||
3510cb94 | 441 | static void free_completed_exception(struct dm_exception *e) |
1da177e4 LT |
442 | { |
443 | kmem_cache_free(exception_cache, e); | |
444 | } | |
445 | ||
92e86812 | 446 | static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s) |
1da177e4 | 447 | { |
92e86812 MP |
448 | struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool, |
449 | GFP_NOIO); | |
450 | ||
879129d2 | 451 | atomic_inc(&s->pending_exceptions_count); |
92e86812 MP |
452 | pe->snap = s; |
453 | ||
454 | return pe; | |
1da177e4 LT |
455 | } |
456 | ||
028867ac | 457 | static void free_pending_exception(struct dm_snap_pending_exception *pe) |
1da177e4 | 458 | { |
879129d2 MP |
459 | struct dm_snapshot *s = pe->snap; |
460 | ||
461 | mempool_free(pe, s->pending_pool); | |
462 | smp_mb__before_atomic_dec(); | |
463 | atomic_dec(&s->pending_exceptions_count); | |
1da177e4 LT |
464 | } |
465 | ||
3510cb94 JB |
466 | static void dm_insert_exception(struct dm_exception_table *eh, |
467 | struct dm_exception *new_e) | |
d74f81f8 | 468 | { |
d74f81f8 | 469 | struct list_head *l; |
1d4989c8 | 470 | struct dm_exception *e = NULL; |
d74f81f8 MB |
471 | |
472 | l = &eh->table[exception_hash(eh, new_e->old_chunk)]; | |
473 | ||
474 | /* Add immediately if this table doesn't support consecutive chunks */ | |
475 | if (!eh->hash_shift) | |
476 | goto out; | |
477 | ||
478 | /* List is ordered by old_chunk */ | |
479 | list_for_each_entry_reverse(e, l, hash_list) { | |
480 | /* Insert after an existing chunk? */ | |
481 | if (new_e->old_chunk == (e->old_chunk + | |
482 | dm_consecutive_chunk_count(e) + 1) && | |
483 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) + | |
484 | dm_consecutive_chunk_count(e) + 1)) { | |
485 | dm_consecutive_chunk_count_inc(e); | |
3510cb94 | 486 | free_completed_exception(new_e); |
d74f81f8 MB |
487 | return; |
488 | } | |
489 | ||
490 | /* Insert before an existing chunk? */ | |
491 | if (new_e->old_chunk == (e->old_chunk - 1) && | |
492 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) { | |
493 | dm_consecutive_chunk_count_inc(e); | |
494 | e->old_chunk--; | |
495 | e->new_chunk--; | |
3510cb94 | 496 | free_completed_exception(new_e); |
d74f81f8 MB |
497 | return; |
498 | } | |
499 | ||
500 | if (new_e->old_chunk > e->old_chunk) | |
501 | break; | |
502 | } | |
503 | ||
504 | out: | |
505 | list_add(&new_e->hash_list, e ? &e->hash_list : l); | |
506 | } | |
507 | ||
a159c1ac JB |
508 | /* |
509 | * Callback used by the exception stores to load exceptions when | |
510 | * initialising. | |
511 | */ | |
512 | static int dm_add_exception(void *context, chunk_t old, chunk_t new) | |
1da177e4 | 513 | { |
a159c1ac | 514 | struct dm_snapshot *s = context; |
1d4989c8 | 515 | struct dm_exception *e; |
1da177e4 | 516 | |
3510cb94 | 517 | e = alloc_completed_exception(); |
1da177e4 LT |
518 | if (!e) |
519 | return -ENOMEM; | |
520 | ||
521 | e->old_chunk = old; | |
d74f81f8 MB |
522 | |
523 | /* Consecutive_count is implicitly initialised to zero */ | |
1da177e4 | 524 | e->new_chunk = new; |
d74f81f8 | 525 | |
3510cb94 | 526 | dm_insert_exception(&s->complete, e); |
d74f81f8 | 527 | |
1da177e4 LT |
528 | return 0; |
529 | } | |
530 | ||
7e201b35 MP |
531 | #define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r))) |
532 | ||
533 | /* | |
534 | * Return a minimum chunk size of all snapshots that have the specified origin. | |
535 | * Return zero if the origin has no snapshots. | |
536 | */ | |
537 | static sector_t __minimum_chunk_size(struct origin *o) | |
538 | { | |
539 | struct dm_snapshot *snap; | |
540 | unsigned chunk_size = 0; | |
541 | ||
542 | if (o) | |
543 | list_for_each_entry(snap, &o->snapshots, list) | |
544 | chunk_size = min_not_zero(chunk_size, | |
545 | snap->store->chunk_size); | |
546 | ||
547 | return chunk_size; | |
548 | } | |
549 | ||
1da177e4 LT |
550 | /* |
551 | * Hard coded magic. | |
552 | */ | |
553 | static int calc_max_buckets(void) | |
554 | { | |
555 | /* use a fixed size of 2MB */ | |
556 | unsigned long mem = 2 * 1024 * 1024; | |
557 | mem /= sizeof(struct list_head); | |
558 | ||
559 | return mem; | |
560 | } | |
561 | ||
1da177e4 LT |
562 | /* |
563 | * Allocate room for a suitable hash table. | |
564 | */ | |
fee1998e | 565 | static int init_hash_tables(struct dm_snapshot *s) |
1da177e4 LT |
566 | { |
567 | sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets; | |
568 | ||
569 | /* | |
570 | * Calculate based on the size of the original volume or | |
571 | * the COW volume... | |
572 | */ | |
fc56f6fb | 573 | cow_dev_size = get_dev_size(s->cow->bdev); |
1da177e4 LT |
574 | origin_dev_size = get_dev_size(s->origin->bdev); |
575 | max_buckets = calc_max_buckets(); | |
576 | ||
fee1998e | 577 | hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift; |
1da177e4 LT |
578 | hash_size = min(hash_size, max_buckets); |
579 | ||
8e87b9b8 MP |
580 | if (hash_size < 64) |
581 | hash_size = 64; | |
8defd830 | 582 | hash_size = rounddown_pow_of_two(hash_size); |
3510cb94 JB |
583 | if (dm_exception_table_init(&s->complete, hash_size, |
584 | DM_CHUNK_CONSECUTIVE_BITS)) | |
1da177e4 LT |
585 | return -ENOMEM; |
586 | ||
587 | /* | |
588 | * Allocate hash table for in-flight exceptions | |
589 | * Make this smaller than the real hash table | |
590 | */ | |
591 | hash_size >>= 3; | |
592 | if (hash_size < 64) | |
593 | hash_size = 64; | |
594 | ||
3510cb94 JB |
595 | if (dm_exception_table_init(&s->pending, hash_size, 0)) { |
596 | dm_exception_table_exit(&s->complete, exception_cache); | |
1da177e4 LT |
597 | return -ENOMEM; |
598 | } | |
599 | ||
600 | return 0; | |
601 | } | |
602 | ||
1da177e4 LT |
603 | /* |
604 | * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size> | |
605 | */ | |
606 | static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
607 | { | |
608 | struct dm_snapshot *s; | |
cd45daff | 609 | int i; |
1da177e4 | 610 | int r = -EINVAL; |
fc56f6fb | 611 | char *origin_path, *cow_path; |
fee1998e | 612 | unsigned args_used; |
1da177e4 | 613 | |
4c7e3bf4 | 614 | if (argc != 4) { |
72d94861 | 615 | ti->error = "requires exactly 4 arguments"; |
1da177e4 | 616 | r = -EINVAL; |
fc56f6fb | 617 | goto bad; |
1da177e4 LT |
618 | } |
619 | ||
620 | origin_path = argv[0]; | |
fee1998e JB |
621 | argv++; |
622 | argc--; | |
1da177e4 | 623 | |
fc56f6fb MS |
624 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
625 | if (!s) { | |
626 | ti->error = "Cannot allocate snapshot context private " | |
627 | "structure"; | |
628 | r = -ENOMEM; | |
629 | goto bad; | |
630 | } | |
631 | ||
632 | cow_path = argv[0]; | |
633 | argv++; | |
634 | argc--; | |
635 | ||
636 | r = dm_get_device(ti, cow_path, 0, 0, | |
637 | FMODE_READ | FMODE_WRITE, &s->cow); | |
638 | if (r) { | |
639 | ti->error = "Cannot get COW device"; | |
640 | goto bad_cow; | |
641 | } | |
642 | ||
643 | r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store); | |
fee1998e JB |
644 | if (r) { |
645 | ti->error = "Couldn't create exception store"; | |
1da177e4 | 646 | r = -EINVAL; |
fc56f6fb | 647 | goto bad_store; |
1da177e4 LT |
648 | } |
649 | ||
fee1998e JB |
650 | argv += args_used; |
651 | argc -= args_used; | |
652 | ||
1da177e4 LT |
653 | r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin); |
654 | if (r) { | |
655 | ti->error = "Cannot get origin device"; | |
fee1998e | 656 | goto bad_origin; |
1da177e4 LT |
657 | } |
658 | ||
fc56f6fb | 659 | s->ti = ti; |
1da177e4 | 660 | s->valid = 1; |
aa14edeb | 661 | s->active = 0; |
c26655ca | 662 | s->suspended = 0; |
879129d2 | 663 | atomic_set(&s->pending_exceptions_count, 0); |
1da177e4 | 664 | init_rwsem(&s->lock); |
ca3a931f | 665 | spin_lock_init(&s->pe_lock); |
1da177e4 LT |
666 | |
667 | /* Allocate hash table for COW data */ | |
fee1998e | 668 | if (init_hash_tables(s)) { |
1da177e4 LT |
669 | ti->error = "Unable to allocate hash table space"; |
670 | r = -ENOMEM; | |
fee1998e | 671 | goto bad_hash_tables; |
1da177e4 LT |
672 | } |
673 | ||
eb69aca5 | 674 | r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client); |
1da177e4 LT |
675 | if (r) { |
676 | ti->error = "Could not create kcopyd client"; | |
fee1998e | 677 | goto bad_kcopyd; |
1da177e4 LT |
678 | } |
679 | ||
92e86812 MP |
680 | s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache); |
681 | if (!s->pending_pool) { | |
682 | ti->error = "Could not allocate mempool for pending exceptions"; | |
fee1998e | 683 | goto bad_pending_pool; |
92e86812 MP |
684 | } |
685 | ||
cd45daff MP |
686 | s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS, |
687 | tracked_chunk_cache); | |
688 | if (!s->tracked_chunk_pool) { | |
689 | ti->error = "Could not allocate tracked_chunk mempool for " | |
690 | "tracking reads"; | |
92e86812 | 691 | goto bad_tracked_chunk_pool; |
cd45daff MP |
692 | } |
693 | ||
694 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) | |
695 | INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]); | |
696 | ||
697 | spin_lock_init(&s->tracked_chunk_lock); | |
698 | ||
aa14edeb | 699 | /* Metadata must only be loaded into one table at once */ |
493df71c JB |
700 | r = s->store->type->read_metadata(s->store, dm_add_exception, |
701 | (void *)s); | |
0764147b | 702 | if (r < 0) { |
f9cea4f7 | 703 | ti->error = "Failed to read snapshot metadata"; |
cd45daff | 704 | goto bad_load_and_register; |
0764147b MB |
705 | } else if (r > 0) { |
706 | s->valid = 0; | |
707 | DMWARN("Snapshot is marked invalid."); | |
f9cea4f7 | 708 | } |
aa14edeb | 709 | |
ca3a931f | 710 | bio_list_init(&s->queued_bios); |
c4028958 | 711 | INIT_WORK(&s->queued_bios_work, flush_queued_bios); |
ca3a931f | 712 | |
3f2412dc MP |
713 | if (!s->store->chunk_size) { |
714 | ti->error = "Chunk size not set"; | |
715 | goto bad_load_and_register; | |
716 | } | |
717 | ||
1da177e4 | 718 | /* Add snapshot to the list of snapshots for this origin */ |
aa14edeb | 719 | /* Exceptions aren't triggered till snapshot_resume() is called */ |
1da177e4 LT |
720 | if (register_snapshot(s)) { |
721 | r = -EINVAL; | |
722 | ti->error = "Cannot register snapshot origin"; | |
cd45daff | 723 | goto bad_load_and_register; |
1da177e4 LT |
724 | } |
725 | ||
726 | ti->private = s; | |
d0216849 | 727 | ti->split_io = s->store->chunk_size; |
494b3ee7 | 728 | ti->num_flush_requests = 1; |
1da177e4 LT |
729 | |
730 | return 0; | |
731 | ||
fee1998e | 732 | bad_load_and_register: |
cd45daff MP |
733 | mempool_destroy(s->tracked_chunk_pool); |
734 | ||
fee1998e | 735 | bad_tracked_chunk_pool: |
92e86812 MP |
736 | mempool_destroy(s->pending_pool); |
737 | ||
fee1998e | 738 | bad_pending_pool: |
eb69aca5 | 739 | dm_kcopyd_client_destroy(s->kcopyd_client); |
1da177e4 | 740 | |
fee1998e | 741 | bad_kcopyd: |
3510cb94 JB |
742 | dm_exception_table_exit(&s->pending, pending_cache); |
743 | dm_exception_table_exit(&s->complete, exception_cache); | |
1da177e4 | 744 | |
fee1998e | 745 | bad_hash_tables: |
1da177e4 LT |
746 | dm_put_device(ti, s->origin); |
747 | ||
fee1998e | 748 | bad_origin: |
fc56f6fb | 749 | dm_exception_store_destroy(s->store); |
1da177e4 | 750 | |
fc56f6fb MS |
751 | bad_store: |
752 | dm_put_device(ti, s->cow); | |
fee1998e | 753 | |
fc56f6fb MS |
754 | bad_cow: |
755 | kfree(s); | |
756 | ||
757 | bad: | |
1da177e4 LT |
758 | return r; |
759 | } | |
760 | ||
31c93a0c MB |
761 | static void __free_exceptions(struct dm_snapshot *s) |
762 | { | |
eb69aca5 | 763 | dm_kcopyd_client_destroy(s->kcopyd_client); |
31c93a0c MB |
764 | s->kcopyd_client = NULL; |
765 | ||
3510cb94 JB |
766 | dm_exception_table_exit(&s->pending, pending_cache); |
767 | dm_exception_table_exit(&s->complete, exception_cache); | |
31c93a0c MB |
768 | } |
769 | ||
1da177e4 LT |
770 | static void snapshot_dtr(struct dm_target *ti) |
771 | { | |
cd45daff MP |
772 | #ifdef CONFIG_DM_DEBUG |
773 | int i; | |
774 | #endif | |
028867ac | 775 | struct dm_snapshot *s = ti->private; |
1da177e4 | 776 | |
ca3a931f AK |
777 | flush_workqueue(ksnapd); |
778 | ||
138728dc AK |
779 | /* Prevent further origin writes from using this snapshot. */ |
780 | /* After this returns there can be no new kcopyd jobs. */ | |
1da177e4 LT |
781 | unregister_snapshot(s); |
782 | ||
879129d2 | 783 | while (atomic_read(&s->pending_exceptions_count)) |
90fa1527 | 784 | msleep(1); |
879129d2 MP |
785 | /* |
786 | * Ensure instructions in mempool_destroy aren't reordered | |
787 | * before atomic_read. | |
788 | */ | |
789 | smp_mb(); | |
790 | ||
cd45daff MP |
791 | #ifdef CONFIG_DM_DEBUG |
792 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) | |
793 | BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i])); | |
794 | #endif | |
795 | ||
796 | mempool_destroy(s->tracked_chunk_pool); | |
797 | ||
31c93a0c | 798 | __free_exceptions(s); |
1da177e4 | 799 | |
92e86812 MP |
800 | mempool_destroy(s->pending_pool); |
801 | ||
1da177e4 | 802 | dm_put_device(ti, s->origin); |
fee1998e JB |
803 | |
804 | dm_exception_store_destroy(s->store); | |
138728dc | 805 | |
fc56f6fb MS |
806 | dm_put_device(ti, s->cow); |
807 | ||
1da177e4 LT |
808 | kfree(s); |
809 | } | |
810 | ||
811 | /* | |
812 | * Flush a list of buffers. | |
813 | */ | |
814 | static void flush_bios(struct bio *bio) | |
815 | { | |
816 | struct bio *n; | |
817 | ||
818 | while (bio) { | |
819 | n = bio->bi_next; | |
820 | bio->bi_next = NULL; | |
821 | generic_make_request(bio); | |
822 | bio = n; | |
823 | } | |
824 | } | |
825 | ||
c4028958 | 826 | static void flush_queued_bios(struct work_struct *work) |
ca3a931f | 827 | { |
c4028958 DH |
828 | struct dm_snapshot *s = |
829 | container_of(work, struct dm_snapshot, queued_bios_work); | |
ca3a931f AK |
830 | struct bio *queued_bios; |
831 | unsigned long flags; | |
832 | ||
833 | spin_lock_irqsave(&s->pe_lock, flags); | |
834 | queued_bios = bio_list_get(&s->queued_bios); | |
835 | spin_unlock_irqrestore(&s->pe_lock, flags); | |
836 | ||
837 | flush_bios(queued_bios); | |
838 | } | |
839 | ||
1da177e4 LT |
840 | /* |
841 | * Error a list of buffers. | |
842 | */ | |
843 | static void error_bios(struct bio *bio) | |
844 | { | |
845 | struct bio *n; | |
846 | ||
847 | while (bio) { | |
848 | n = bio->bi_next; | |
849 | bio->bi_next = NULL; | |
6712ecf8 | 850 | bio_io_error(bio); |
1da177e4 LT |
851 | bio = n; |
852 | } | |
853 | } | |
854 | ||
695368ac | 855 | static void __invalidate_snapshot(struct dm_snapshot *s, int err) |
76df1c65 AK |
856 | { |
857 | if (!s->valid) | |
858 | return; | |
859 | ||
860 | if (err == -EIO) | |
861 | DMERR("Invalidating snapshot: Error reading/writing."); | |
862 | else if (err == -ENOMEM) | |
863 | DMERR("Invalidating snapshot: Unable to allocate exception."); | |
864 | ||
493df71c JB |
865 | if (s->store->type->drop_snapshot) |
866 | s->store->type->drop_snapshot(s->store); | |
76df1c65 AK |
867 | |
868 | s->valid = 0; | |
869 | ||
fc56f6fb | 870 | dm_table_event(s->ti->table); |
76df1c65 AK |
871 | } |
872 | ||
028867ac | 873 | static void get_pending_exception(struct dm_snap_pending_exception *pe) |
4b832e8d AK |
874 | { |
875 | atomic_inc(&pe->ref_count); | |
876 | } | |
877 | ||
028867ac | 878 | static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe) |
4b832e8d | 879 | { |
028867ac | 880 | struct dm_snap_pending_exception *primary_pe; |
4b832e8d AK |
881 | struct bio *origin_bios = NULL; |
882 | ||
883 | primary_pe = pe->primary_pe; | |
884 | ||
885 | /* | |
886 | * If this pe is involved in a write to the origin and | |
887 | * it is the last sibling to complete then release | |
888 | * the bios for the original write to the origin. | |
889 | */ | |
890 | if (primary_pe && | |
7c5f78b9 | 891 | atomic_dec_and_test(&primary_pe->ref_count)) { |
4b832e8d | 892 | origin_bios = bio_list_get(&primary_pe->origin_bios); |
7c5f78b9 MP |
893 | free_pending_exception(primary_pe); |
894 | } | |
4b832e8d AK |
895 | |
896 | /* | |
897 | * Free the pe if it's not linked to an origin write or if | |
898 | * it's not itself a primary pe. | |
899 | */ | |
900 | if (!primary_pe || primary_pe != pe) | |
901 | free_pending_exception(pe); | |
902 | ||
4b832e8d AK |
903 | return origin_bios; |
904 | } | |
905 | ||
028867ac | 906 | static void pending_complete(struct dm_snap_pending_exception *pe, int success) |
1da177e4 | 907 | { |
1d4989c8 | 908 | struct dm_exception *e; |
1da177e4 | 909 | struct dm_snapshot *s = pe->snap; |
9d493fa8 AK |
910 | struct bio *origin_bios = NULL; |
911 | struct bio *snapshot_bios = NULL; | |
912 | int error = 0; | |
1da177e4 | 913 | |
76df1c65 AK |
914 | if (!success) { |
915 | /* Read/write error - snapshot is unusable */ | |
1da177e4 | 916 | down_write(&s->lock); |
695368ac | 917 | __invalidate_snapshot(s, -EIO); |
9d493fa8 | 918 | error = 1; |
76df1c65 AK |
919 | goto out; |
920 | } | |
921 | ||
3510cb94 | 922 | e = alloc_completed_exception(); |
76df1c65 | 923 | if (!e) { |
1da177e4 | 924 | down_write(&s->lock); |
695368ac | 925 | __invalidate_snapshot(s, -ENOMEM); |
9d493fa8 | 926 | error = 1; |
76df1c65 AK |
927 | goto out; |
928 | } | |
929 | *e = pe->e; | |
1da177e4 | 930 | |
76df1c65 AK |
931 | down_write(&s->lock); |
932 | if (!s->valid) { | |
3510cb94 | 933 | free_completed_exception(e); |
9d493fa8 | 934 | error = 1; |
76df1c65 | 935 | goto out; |
1da177e4 LT |
936 | } |
937 | ||
a8d41b59 MP |
938 | /* |
939 | * Check for conflicting reads. This is extremely improbable, | |
90fa1527 | 940 | * so msleep(1) is sufficient and there is no need for a wait queue. |
a8d41b59 MP |
941 | */ |
942 | while (__chunk_is_tracked(s, pe->e.old_chunk)) | |
90fa1527 | 943 | msleep(1); |
a8d41b59 | 944 | |
9d493fa8 AK |
945 | /* |
946 | * Add a proper exception, and remove the | |
947 | * in-flight exception from the list. | |
948 | */ | |
3510cb94 | 949 | dm_insert_exception(&s->complete, e); |
76df1c65 | 950 | |
1da177e4 | 951 | out: |
3510cb94 | 952 | dm_remove_exception(&pe->e); |
9d493fa8 | 953 | snapshot_bios = bio_list_get(&pe->snapshot_bios); |
4b832e8d | 954 | origin_bios = put_pending_exception(pe); |
1da177e4 | 955 | |
9d493fa8 AK |
956 | up_write(&s->lock); |
957 | ||
958 | /* Submit any pending write bios */ | |
959 | if (error) | |
960 | error_bios(snapshot_bios); | |
961 | else | |
962 | flush_bios(snapshot_bios); | |
963 | ||
964 | flush_bios(origin_bios); | |
1da177e4 LT |
965 | } |
966 | ||
967 | static void commit_callback(void *context, int success) | |
968 | { | |
028867ac AK |
969 | struct dm_snap_pending_exception *pe = context; |
970 | ||
1da177e4 LT |
971 | pending_complete(pe, success); |
972 | } | |
973 | ||
974 | /* | |
975 | * Called when the copy I/O has finished. kcopyd actually runs | |
976 | * this code so don't block. | |
977 | */ | |
4cdc1d1f | 978 | static void copy_callback(int read_err, unsigned long write_err, void *context) |
1da177e4 | 979 | { |
028867ac | 980 | struct dm_snap_pending_exception *pe = context; |
1da177e4 LT |
981 | struct dm_snapshot *s = pe->snap; |
982 | ||
983 | if (read_err || write_err) | |
984 | pending_complete(pe, 0); | |
985 | ||
986 | else | |
987 | /* Update the metadata if we are persistent */ | |
493df71c JB |
988 | s->store->type->commit_exception(s->store, &pe->e, |
989 | commit_callback, pe); | |
1da177e4 LT |
990 | } |
991 | ||
992 | /* | |
993 | * Dispatches the copy operation to kcopyd. | |
994 | */ | |
028867ac | 995 | static void start_copy(struct dm_snap_pending_exception *pe) |
1da177e4 LT |
996 | { |
997 | struct dm_snapshot *s = pe->snap; | |
22a1ceb1 | 998 | struct dm_io_region src, dest; |
1da177e4 LT |
999 | struct block_device *bdev = s->origin->bdev; |
1000 | sector_t dev_size; | |
1001 | ||
1002 | dev_size = get_dev_size(bdev); | |
1003 | ||
1004 | src.bdev = bdev; | |
71fab00a | 1005 | src.sector = chunk_to_sector(s->store, pe->e.old_chunk); |
df96eee6 | 1006 | src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector); |
1da177e4 | 1007 | |
fc56f6fb | 1008 | dest.bdev = s->cow->bdev; |
71fab00a | 1009 | dest.sector = chunk_to_sector(s->store, pe->e.new_chunk); |
1da177e4 LT |
1010 | dest.count = src.count; |
1011 | ||
1012 | /* Hand over to kcopyd */ | |
eb69aca5 | 1013 | dm_kcopyd_copy(s->kcopyd_client, |
1da177e4 LT |
1014 | &src, 1, &dest, 0, copy_callback, pe); |
1015 | } | |
1016 | ||
2913808e MP |
1017 | static struct dm_snap_pending_exception * |
1018 | __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk) | |
1019 | { | |
3510cb94 | 1020 | struct dm_exception *e = dm_lookup_exception(&s->pending, chunk); |
2913808e MP |
1021 | |
1022 | if (!e) | |
1023 | return NULL; | |
1024 | ||
1025 | return container_of(e, struct dm_snap_pending_exception, e); | |
1026 | } | |
1027 | ||
1da177e4 LT |
1028 | /* |
1029 | * Looks to see if this snapshot already has a pending exception | |
1030 | * for this chunk, otherwise it allocates a new one and inserts | |
1031 | * it into the pending table. | |
1032 | * | |
1033 | * NOTE: a write lock must be held on snap->lock before calling | |
1034 | * this. | |
1035 | */ | |
028867ac | 1036 | static struct dm_snap_pending_exception * |
c6621392 MP |
1037 | __find_pending_exception(struct dm_snapshot *s, |
1038 | struct dm_snap_pending_exception *pe, chunk_t chunk) | |
1da177e4 | 1039 | { |
c6621392 | 1040 | struct dm_snap_pending_exception *pe2; |
1da177e4 | 1041 | |
2913808e MP |
1042 | pe2 = __lookup_pending_exception(s, chunk); |
1043 | if (pe2) { | |
76df1c65 | 1044 | free_pending_exception(pe); |
2913808e | 1045 | return pe2; |
1da177e4 LT |
1046 | } |
1047 | ||
76df1c65 AK |
1048 | pe->e.old_chunk = chunk; |
1049 | bio_list_init(&pe->origin_bios); | |
1050 | bio_list_init(&pe->snapshot_bios); | |
1051 | pe->primary_pe = NULL; | |
4b832e8d | 1052 | atomic_set(&pe->ref_count, 0); |
76df1c65 AK |
1053 | pe->started = 0; |
1054 | ||
493df71c | 1055 | if (s->store->type->prepare_exception(s->store, &pe->e)) { |
76df1c65 AK |
1056 | free_pending_exception(pe); |
1057 | return NULL; | |
1058 | } | |
1059 | ||
4b832e8d | 1060 | get_pending_exception(pe); |
3510cb94 | 1061 | dm_insert_exception(&s->pending, &pe->e); |
76df1c65 | 1062 | |
1da177e4 LT |
1063 | return pe; |
1064 | } | |
1065 | ||
1d4989c8 | 1066 | static void remap_exception(struct dm_snapshot *s, struct dm_exception *e, |
d74f81f8 | 1067 | struct bio *bio, chunk_t chunk) |
1da177e4 | 1068 | { |
fc56f6fb | 1069 | bio->bi_bdev = s->cow->bdev; |
71fab00a JB |
1070 | bio->bi_sector = chunk_to_sector(s->store, |
1071 | dm_chunk_number(e->new_chunk) + | |
1072 | (chunk - e->old_chunk)) + | |
1073 | (bio->bi_sector & | |
1074 | s->store->chunk_mask); | |
1da177e4 LT |
1075 | } |
1076 | ||
1077 | static int snapshot_map(struct dm_target *ti, struct bio *bio, | |
1078 | union map_info *map_context) | |
1079 | { | |
1d4989c8 | 1080 | struct dm_exception *e; |
028867ac | 1081 | struct dm_snapshot *s = ti->private; |
d2a7ad29 | 1082 | int r = DM_MAPIO_REMAPPED; |
1da177e4 | 1083 | chunk_t chunk; |
028867ac | 1084 | struct dm_snap_pending_exception *pe = NULL; |
1da177e4 | 1085 | |
494b3ee7 | 1086 | if (unlikely(bio_empty_barrier(bio))) { |
fc56f6fb | 1087 | bio->bi_bdev = s->cow->bdev; |
494b3ee7 MP |
1088 | return DM_MAPIO_REMAPPED; |
1089 | } | |
1090 | ||
71fab00a | 1091 | chunk = sector_to_chunk(s->store, bio->bi_sector); |
1da177e4 LT |
1092 | |
1093 | /* Full snapshots are not usable */ | |
76df1c65 | 1094 | /* To get here the table must be live so s->active is always set. */ |
1da177e4 | 1095 | if (!s->valid) |
f6a80ea8 | 1096 | return -EIO; |
1da177e4 | 1097 | |
ba40a2aa AK |
1098 | /* FIXME: should only take write lock if we need |
1099 | * to copy an exception */ | |
1100 | down_write(&s->lock); | |
1101 | ||
1102 | if (!s->valid) { | |
1103 | r = -EIO; | |
1104 | goto out_unlock; | |
1105 | } | |
1106 | ||
1107 | /* If the block is already remapped - use that, else remap it */ | |
3510cb94 | 1108 | e = dm_lookup_exception(&s->complete, chunk); |
ba40a2aa | 1109 | if (e) { |
d74f81f8 | 1110 | remap_exception(s, e, bio, chunk); |
ba40a2aa AK |
1111 | goto out_unlock; |
1112 | } | |
1113 | ||
1da177e4 LT |
1114 | /* |
1115 | * Write to snapshot - higher level takes care of RW/RO | |
1116 | * flags so we should only get this if we are | |
1117 | * writeable. | |
1118 | */ | |
1119 | if (bio_rw(bio) == WRITE) { | |
2913808e | 1120 | pe = __lookup_pending_exception(s, chunk); |
76df1c65 | 1121 | if (!pe) { |
c6621392 MP |
1122 | up_write(&s->lock); |
1123 | pe = alloc_pending_exception(s); | |
1124 | down_write(&s->lock); | |
1125 | ||
1126 | if (!s->valid) { | |
1127 | free_pending_exception(pe); | |
1128 | r = -EIO; | |
1129 | goto out_unlock; | |
1130 | } | |
1131 | ||
3510cb94 | 1132 | e = dm_lookup_exception(&s->complete, chunk); |
35bf659b MP |
1133 | if (e) { |
1134 | free_pending_exception(pe); | |
1135 | remap_exception(s, e, bio, chunk); | |
1136 | goto out_unlock; | |
1137 | } | |
1138 | ||
c6621392 | 1139 | pe = __find_pending_exception(s, pe, chunk); |
2913808e MP |
1140 | if (!pe) { |
1141 | __invalidate_snapshot(s, -ENOMEM); | |
1142 | r = -EIO; | |
1143 | goto out_unlock; | |
1144 | } | |
1da177e4 LT |
1145 | } |
1146 | ||
d74f81f8 | 1147 | remap_exception(s, &pe->e, bio, chunk); |
76df1c65 AK |
1148 | bio_list_add(&pe->snapshot_bios, bio); |
1149 | ||
d2a7ad29 | 1150 | r = DM_MAPIO_SUBMITTED; |
ba40a2aa | 1151 | |
76df1c65 AK |
1152 | if (!pe->started) { |
1153 | /* this is protected by snap->lock */ | |
1154 | pe->started = 1; | |
ba40a2aa | 1155 | up_write(&s->lock); |
76df1c65 | 1156 | start_copy(pe); |
ba40a2aa AK |
1157 | goto out; |
1158 | } | |
cd45daff | 1159 | } else { |
ba40a2aa | 1160 | bio->bi_bdev = s->origin->bdev; |
cd45daff MP |
1161 | map_context->ptr = track_chunk(s, chunk); |
1162 | } | |
1da177e4 | 1163 | |
ba40a2aa AK |
1164 | out_unlock: |
1165 | up_write(&s->lock); | |
1166 | out: | |
1da177e4 LT |
1167 | return r; |
1168 | } | |
1169 | ||
cd45daff MP |
1170 | static int snapshot_end_io(struct dm_target *ti, struct bio *bio, |
1171 | int error, union map_info *map_context) | |
1172 | { | |
1173 | struct dm_snapshot *s = ti->private; | |
1174 | struct dm_snap_tracked_chunk *c = map_context->ptr; | |
1175 | ||
1176 | if (c) | |
1177 | stop_tracking_chunk(s, c); | |
1178 | ||
1179 | return 0; | |
1180 | } | |
1181 | ||
c26655ca MS |
1182 | static void snapshot_postsuspend(struct dm_target *ti) |
1183 | { | |
1184 | struct dm_snapshot *s = ti->private; | |
1185 | ||
1186 | down_write(&s->lock); | |
1187 | s->suspended = 1; | |
1188 | up_write(&s->lock); | |
1189 | } | |
1190 | ||
1da177e4 LT |
1191 | static void snapshot_resume(struct dm_target *ti) |
1192 | { | |
028867ac | 1193 | struct dm_snapshot *s = ti->private; |
1da177e4 | 1194 | |
aa14edeb AK |
1195 | down_write(&s->lock); |
1196 | s->active = 1; | |
c26655ca | 1197 | s->suspended = 0; |
aa14edeb | 1198 | up_write(&s->lock); |
1da177e4 LT |
1199 | } |
1200 | ||
1201 | static int snapshot_status(struct dm_target *ti, status_type_t type, | |
1202 | char *result, unsigned int maxlen) | |
1203 | { | |
2e4a31df | 1204 | unsigned sz = 0; |
028867ac | 1205 | struct dm_snapshot *snap = ti->private; |
1da177e4 LT |
1206 | |
1207 | switch (type) { | |
1208 | case STATUSTYPE_INFO: | |
94e76572 MP |
1209 | |
1210 | down_write(&snap->lock); | |
1211 | ||
1da177e4 | 1212 | if (!snap->valid) |
2e4a31df | 1213 | DMEMIT("Invalid"); |
1da177e4 | 1214 | else { |
985903bb MS |
1215 | if (snap->store->type->usage) { |
1216 | sector_t total_sectors, sectors_allocated, | |
1217 | metadata_sectors; | |
1218 | snap->store->type->usage(snap->store, | |
1219 | &total_sectors, | |
1220 | §ors_allocated, | |
1221 | &metadata_sectors); | |
1222 | DMEMIT("%llu/%llu %llu", | |
1223 | (unsigned long long)sectors_allocated, | |
1224 | (unsigned long long)total_sectors, | |
1225 | (unsigned long long)metadata_sectors); | |
1da177e4 LT |
1226 | } |
1227 | else | |
2e4a31df | 1228 | DMEMIT("Unknown"); |
1da177e4 | 1229 | } |
94e76572 MP |
1230 | |
1231 | up_write(&snap->lock); | |
1232 | ||
1da177e4 LT |
1233 | break; |
1234 | ||
1235 | case STATUSTYPE_TABLE: | |
1236 | /* | |
1237 | * kdevname returns a static pointer so we need | |
1238 | * to make private copies if the output is to | |
1239 | * make sense. | |
1240 | */ | |
fc56f6fb | 1241 | DMEMIT("%s %s", snap->origin->name, snap->cow->name); |
1e302a92 JB |
1242 | snap->store->type->status(snap->store, type, result + sz, |
1243 | maxlen - sz); | |
1da177e4 LT |
1244 | break; |
1245 | } | |
1246 | ||
1247 | return 0; | |
1248 | } | |
1249 | ||
8811f46c MS |
1250 | static int snapshot_iterate_devices(struct dm_target *ti, |
1251 | iterate_devices_callout_fn fn, void *data) | |
1252 | { | |
1253 | struct dm_snapshot *snap = ti->private; | |
1254 | ||
1255 | return fn(ti, snap->origin, 0, ti->len, data); | |
1256 | } | |
1257 | ||
1258 | ||
1da177e4 LT |
1259 | /*----------------------------------------------------------------- |
1260 | * Origin methods | |
1261 | *---------------------------------------------------------------*/ | |
1da177e4 LT |
1262 | static int __origin_write(struct list_head *snapshots, struct bio *bio) |
1263 | { | |
d2a7ad29 | 1264 | int r = DM_MAPIO_REMAPPED, first = 0; |
1da177e4 | 1265 | struct dm_snapshot *snap; |
1d4989c8 | 1266 | struct dm_exception *e; |
028867ac | 1267 | struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL; |
1da177e4 | 1268 | chunk_t chunk; |
eccf0817 | 1269 | LIST_HEAD(pe_queue); |
1da177e4 LT |
1270 | |
1271 | /* Do all the snapshots on this origin */ | |
1272 | list_for_each_entry (snap, snapshots, list) { | |
1273 | ||
76df1c65 AK |
1274 | down_write(&snap->lock); |
1275 | ||
aa14edeb AK |
1276 | /* Only deal with valid and active snapshots */ |
1277 | if (!snap->valid || !snap->active) | |
76df1c65 | 1278 | goto next_snapshot; |
1da177e4 | 1279 | |
d5e404c1 | 1280 | /* Nothing to do if writing beyond end of snapshot */ |
fc56f6fb | 1281 | if (bio->bi_sector >= dm_table_get_size(snap->ti->table)) |
76df1c65 | 1282 | goto next_snapshot; |
1da177e4 LT |
1283 | |
1284 | /* | |
1285 | * Remember, different snapshots can have | |
1286 | * different chunk sizes. | |
1287 | */ | |
71fab00a | 1288 | chunk = sector_to_chunk(snap->store, bio->bi_sector); |
1da177e4 LT |
1289 | |
1290 | /* | |
1291 | * Check exception table to see if block | |
1292 | * is already remapped in this snapshot | |
1293 | * and trigger an exception if not. | |
b4b610f6 | 1294 | * |
4b832e8d | 1295 | * ref_count is initialised to 1 so pending_complete() |
b4b610f6 | 1296 | * won't destroy the primary_pe while we're inside this loop. |
1da177e4 | 1297 | */ |
3510cb94 | 1298 | e = dm_lookup_exception(&snap->complete, chunk); |
76df1c65 AK |
1299 | if (e) |
1300 | goto next_snapshot; | |
1301 | ||
2913808e | 1302 | pe = __lookup_pending_exception(snap, chunk); |
76df1c65 | 1303 | if (!pe) { |
c6621392 MP |
1304 | up_write(&snap->lock); |
1305 | pe = alloc_pending_exception(snap); | |
1306 | down_write(&snap->lock); | |
1307 | ||
1308 | if (!snap->valid) { | |
1309 | free_pending_exception(pe); | |
1310 | goto next_snapshot; | |
1311 | } | |
1312 | ||
3510cb94 | 1313 | e = dm_lookup_exception(&snap->complete, chunk); |
35bf659b MP |
1314 | if (e) { |
1315 | free_pending_exception(pe); | |
1316 | goto next_snapshot; | |
1317 | } | |
1318 | ||
c6621392 | 1319 | pe = __find_pending_exception(snap, pe, chunk); |
2913808e MP |
1320 | if (!pe) { |
1321 | __invalidate_snapshot(snap, -ENOMEM); | |
1322 | goto next_snapshot; | |
1323 | } | |
76df1c65 AK |
1324 | } |
1325 | ||
1326 | if (!primary_pe) { | |
1327 | /* | |
1328 | * Either every pe here has same | |
1329 | * primary_pe or none has one yet. | |
1330 | */ | |
1331 | if (pe->primary_pe) | |
1332 | primary_pe = pe->primary_pe; | |
1333 | else { | |
1334 | primary_pe = pe; | |
1335 | first = 1; | |
1da177e4 | 1336 | } |
76df1c65 AK |
1337 | |
1338 | bio_list_add(&primary_pe->origin_bios, bio); | |
1339 | ||
d2a7ad29 | 1340 | r = DM_MAPIO_SUBMITTED; |
76df1c65 AK |
1341 | } |
1342 | ||
1343 | if (!pe->primary_pe) { | |
76df1c65 | 1344 | pe->primary_pe = primary_pe; |
4b832e8d | 1345 | get_pending_exception(primary_pe); |
76df1c65 AK |
1346 | } |
1347 | ||
1348 | if (!pe->started) { | |
1349 | pe->started = 1; | |
1350 | list_add_tail(&pe->list, &pe_queue); | |
1da177e4 LT |
1351 | } |
1352 | ||
76df1c65 | 1353 | next_snapshot: |
1da177e4 LT |
1354 | up_write(&snap->lock); |
1355 | } | |
1356 | ||
b4b610f6 | 1357 | if (!primary_pe) |
4b832e8d | 1358 | return r; |
b4b610f6 AK |
1359 | |
1360 | /* | |
1361 | * If this is the first time we're processing this chunk and | |
4b832e8d | 1362 | * ref_count is now 1 it means all the pending exceptions |
b4b610f6 AK |
1363 | * got completed while we were in the loop above, so it falls to |
1364 | * us here to remove the primary_pe and submit any origin_bios. | |
1365 | */ | |
1366 | ||
4b832e8d | 1367 | if (first && atomic_dec_and_test(&primary_pe->ref_count)) { |
b4b610f6 AK |
1368 | flush_bios(bio_list_get(&primary_pe->origin_bios)); |
1369 | free_pending_exception(primary_pe); | |
1370 | /* If we got here, pe_queue is necessarily empty. */ | |
4b832e8d | 1371 | return r; |
b4b610f6 AK |
1372 | } |
1373 | ||
1da177e4 LT |
1374 | /* |
1375 | * Now that we have a complete pe list we can start the copying. | |
1376 | */ | |
eccf0817 AK |
1377 | list_for_each_entry_safe(pe, next_pe, &pe_queue, list) |
1378 | start_copy(pe); | |
1da177e4 LT |
1379 | |
1380 | return r; | |
1381 | } | |
1382 | ||
1383 | /* | |
1384 | * Called on a write from the origin driver. | |
1385 | */ | |
1386 | static int do_origin(struct dm_dev *origin, struct bio *bio) | |
1387 | { | |
1388 | struct origin *o; | |
d2a7ad29 | 1389 | int r = DM_MAPIO_REMAPPED; |
1da177e4 LT |
1390 | |
1391 | down_read(&_origins_lock); | |
1392 | o = __lookup_origin(origin->bdev); | |
1393 | if (o) | |
1394 | r = __origin_write(&o->snapshots, bio); | |
1395 | up_read(&_origins_lock); | |
1396 | ||
1397 | return r; | |
1398 | } | |
1399 | ||
1400 | /* | |
1401 | * Origin: maps a linear range of a device, with hooks for snapshotting. | |
1402 | */ | |
1403 | ||
1404 | /* | |
1405 | * Construct an origin mapping: <dev_path> | |
1406 | * The context for an origin is merely a 'struct dm_dev *' | |
1407 | * pointing to the real device. | |
1408 | */ | |
1409 | static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
1410 | { | |
1411 | int r; | |
1412 | struct dm_dev *dev; | |
1413 | ||
1414 | if (argc != 1) { | |
72d94861 | 1415 | ti->error = "origin: incorrect number of arguments"; |
1da177e4 LT |
1416 | return -EINVAL; |
1417 | } | |
1418 | ||
1419 | r = dm_get_device(ti, argv[0], 0, ti->len, | |
1420 | dm_table_get_mode(ti->table), &dev); | |
1421 | if (r) { | |
1422 | ti->error = "Cannot get target device"; | |
1423 | return r; | |
1424 | } | |
1425 | ||
1426 | ti->private = dev; | |
494b3ee7 MP |
1427 | ti->num_flush_requests = 1; |
1428 | ||
1da177e4 LT |
1429 | return 0; |
1430 | } | |
1431 | ||
1432 | static void origin_dtr(struct dm_target *ti) | |
1433 | { | |
028867ac | 1434 | struct dm_dev *dev = ti->private; |
1da177e4 LT |
1435 | dm_put_device(ti, dev); |
1436 | } | |
1437 | ||
1438 | static int origin_map(struct dm_target *ti, struct bio *bio, | |
1439 | union map_info *map_context) | |
1440 | { | |
028867ac | 1441 | struct dm_dev *dev = ti->private; |
1da177e4 LT |
1442 | bio->bi_bdev = dev->bdev; |
1443 | ||
494b3ee7 MP |
1444 | if (unlikely(bio_empty_barrier(bio))) |
1445 | return DM_MAPIO_REMAPPED; | |
1446 | ||
1da177e4 | 1447 | /* Only tell snapshots if this is a write */ |
d2a7ad29 | 1448 | return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED; |
1da177e4 LT |
1449 | } |
1450 | ||
1da177e4 LT |
1451 | /* |
1452 | * Set the target "split_io" field to the minimum of all the snapshots' | |
1453 | * chunk sizes. | |
1454 | */ | |
1455 | static void origin_resume(struct dm_target *ti) | |
1456 | { | |
028867ac | 1457 | struct dm_dev *dev = ti->private; |
1da177e4 LT |
1458 | |
1459 | down_read(&_origins_lock); | |
1da177e4 | 1460 | |
7e201b35 MP |
1461 | ti->split_io = __minimum_chunk_size(__lookup_origin(dev->bdev)); |
1462 | ||
1463 | up_read(&_origins_lock); | |
1da177e4 LT |
1464 | } |
1465 | ||
1466 | static int origin_status(struct dm_target *ti, status_type_t type, char *result, | |
1467 | unsigned int maxlen) | |
1468 | { | |
028867ac | 1469 | struct dm_dev *dev = ti->private; |
1da177e4 LT |
1470 | |
1471 | switch (type) { | |
1472 | case STATUSTYPE_INFO: | |
1473 | result[0] = '\0'; | |
1474 | break; | |
1475 | ||
1476 | case STATUSTYPE_TABLE: | |
1477 | snprintf(result, maxlen, "%s", dev->name); | |
1478 | break; | |
1479 | } | |
1480 | ||
1481 | return 0; | |
1482 | } | |
1483 | ||
8811f46c MS |
1484 | static int origin_iterate_devices(struct dm_target *ti, |
1485 | iterate_devices_callout_fn fn, void *data) | |
1486 | { | |
1487 | struct dm_dev *dev = ti->private; | |
1488 | ||
1489 | return fn(ti, dev, 0, ti->len, data); | |
1490 | } | |
1491 | ||
1da177e4 LT |
1492 | static struct target_type origin_target = { |
1493 | .name = "snapshot-origin", | |
8811f46c | 1494 | .version = {1, 7, 0}, |
1da177e4 LT |
1495 | .module = THIS_MODULE, |
1496 | .ctr = origin_ctr, | |
1497 | .dtr = origin_dtr, | |
1498 | .map = origin_map, | |
1499 | .resume = origin_resume, | |
1500 | .status = origin_status, | |
8811f46c | 1501 | .iterate_devices = origin_iterate_devices, |
1da177e4 LT |
1502 | }; |
1503 | ||
1504 | static struct target_type snapshot_target = { | |
1505 | .name = "snapshot", | |
c26655ca | 1506 | .version = {1, 9, 0}, |
1da177e4 LT |
1507 | .module = THIS_MODULE, |
1508 | .ctr = snapshot_ctr, | |
1509 | .dtr = snapshot_dtr, | |
1510 | .map = snapshot_map, | |
cd45daff | 1511 | .end_io = snapshot_end_io, |
c26655ca | 1512 | .postsuspend = snapshot_postsuspend, |
1da177e4 LT |
1513 | .resume = snapshot_resume, |
1514 | .status = snapshot_status, | |
8811f46c | 1515 | .iterate_devices = snapshot_iterate_devices, |
1da177e4 LT |
1516 | }; |
1517 | ||
1518 | static int __init dm_snapshot_init(void) | |
1519 | { | |
1520 | int r; | |
1521 | ||
4db6bfe0 AK |
1522 | r = dm_exception_store_init(); |
1523 | if (r) { | |
1524 | DMERR("Failed to initialize exception stores"); | |
1525 | return r; | |
1526 | } | |
1527 | ||
1da177e4 LT |
1528 | r = dm_register_target(&snapshot_target); |
1529 | if (r) { | |
1530 | DMERR("snapshot target register failed %d", r); | |
034a186d | 1531 | goto bad_register_snapshot_target; |
1da177e4 LT |
1532 | } |
1533 | ||
1534 | r = dm_register_target(&origin_target); | |
1535 | if (r < 0) { | |
72d94861 | 1536 | DMERR("Origin target register failed %d", r); |
1da177e4 LT |
1537 | goto bad1; |
1538 | } | |
1539 | ||
1540 | r = init_origin_hash(); | |
1541 | if (r) { | |
1542 | DMERR("init_origin_hash failed."); | |
1543 | goto bad2; | |
1544 | } | |
1545 | ||
1d4989c8 | 1546 | exception_cache = KMEM_CACHE(dm_exception, 0); |
1da177e4 LT |
1547 | if (!exception_cache) { |
1548 | DMERR("Couldn't create exception cache."); | |
1549 | r = -ENOMEM; | |
1550 | goto bad3; | |
1551 | } | |
1552 | ||
028867ac | 1553 | pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0); |
1da177e4 LT |
1554 | if (!pending_cache) { |
1555 | DMERR("Couldn't create pending cache."); | |
1556 | r = -ENOMEM; | |
1557 | goto bad4; | |
1558 | } | |
1559 | ||
cd45daff MP |
1560 | tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0); |
1561 | if (!tracked_chunk_cache) { | |
1562 | DMERR("Couldn't create cache to track chunks in use."); | |
1563 | r = -ENOMEM; | |
1564 | goto bad5; | |
1565 | } | |
1566 | ||
ca3a931f AK |
1567 | ksnapd = create_singlethread_workqueue("ksnapd"); |
1568 | if (!ksnapd) { | |
1569 | DMERR("Failed to create ksnapd workqueue."); | |
1570 | r = -ENOMEM; | |
92e86812 | 1571 | goto bad_pending_pool; |
ca3a931f AK |
1572 | } |
1573 | ||
1da177e4 LT |
1574 | return 0; |
1575 | ||
4db6bfe0 | 1576 | bad_pending_pool: |
cd45daff | 1577 | kmem_cache_destroy(tracked_chunk_cache); |
4db6bfe0 | 1578 | bad5: |
1da177e4 | 1579 | kmem_cache_destroy(pending_cache); |
4db6bfe0 | 1580 | bad4: |
1da177e4 | 1581 | kmem_cache_destroy(exception_cache); |
4db6bfe0 | 1582 | bad3: |
1da177e4 | 1583 | exit_origin_hash(); |
4db6bfe0 | 1584 | bad2: |
1da177e4 | 1585 | dm_unregister_target(&origin_target); |
4db6bfe0 | 1586 | bad1: |
1da177e4 | 1587 | dm_unregister_target(&snapshot_target); |
034a186d JB |
1588 | |
1589 | bad_register_snapshot_target: | |
1590 | dm_exception_store_exit(); | |
1da177e4 LT |
1591 | return r; |
1592 | } | |
1593 | ||
1594 | static void __exit dm_snapshot_exit(void) | |
1595 | { | |
ca3a931f AK |
1596 | destroy_workqueue(ksnapd); |
1597 | ||
10d3bd09 MP |
1598 | dm_unregister_target(&snapshot_target); |
1599 | dm_unregister_target(&origin_target); | |
1da177e4 LT |
1600 | |
1601 | exit_origin_hash(); | |
1da177e4 LT |
1602 | kmem_cache_destroy(pending_cache); |
1603 | kmem_cache_destroy(exception_cache); | |
cd45daff | 1604 | kmem_cache_destroy(tracked_chunk_cache); |
4db6bfe0 AK |
1605 | |
1606 | dm_exception_store_exit(); | |
1da177e4 LT |
1607 | } |
1608 | ||
1609 | /* Module hooks */ | |
1610 | module_init(dm_snapshot_init); | |
1611 | module_exit(dm_snapshot_exit); | |
1612 | ||
1613 | MODULE_DESCRIPTION(DM_NAME " snapshot target"); | |
1614 | MODULE_AUTHOR("Joe Thornber"); | |
1615 | MODULE_LICENSE("GPL"); |