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 | ||
d698aa45 MP |
28 | static 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 | ||
1da177e4 LT |
33 | /* |
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 | /* | |
8ee2767a | 44 | * Reserve 1MB for each snapshot initially (with minimum of 1 page). |
1da177e4 | 45 | */ |
8ee2767a | 46 | #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1) |
1da177e4 | 47 | |
cd45daff MP |
48 | /* |
49 | * The size of the mempool used to track chunks in use. | |
50 | */ | |
51 | #define MIN_IOS 256 | |
52 | ||
ccc45ea8 JB |
53 | #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 | ||
191437a5 | 57 | struct dm_exception_table { |
ccc45ea8 JB |
58 | uint32_t hash_mask; |
59 | unsigned hash_shift; | |
60 | struct list_head *table; | |
61 | }; | |
62 | ||
63 | struct dm_snapshot { | |
64 | struct rw_semaphore lock; | |
65 | ||
66 | struct dm_dev *origin; | |
fc56f6fb MS |
67 | struct dm_dev *cow; |
68 | ||
69 | struct dm_target *ti; | |
ccc45ea8 JB |
70 | |
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 | ||
c26655ca MS |
80 | /* Whether or not owning mapped_device is suspended */ |
81 | int suspended; | |
82 | ||
ccc45ea8 JB |
83 | mempool_t *pending_pool; |
84 | ||
85 | atomic_t pending_exceptions_count; | |
86 | ||
191437a5 JB |
87 | struct dm_exception_table pending; |
88 | struct dm_exception_table complete; | |
ccc45ea8 JB |
89 | |
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]; | |
1e03f97e MP |
109 | |
110 | /* Wait for events based on state_bits */ | |
111 | unsigned long state_bits; | |
9fe86254 MP |
112 | |
113 | /* Range of chunks currently being merged. */ | |
114 | chunk_t first_merging_chunk; | |
115 | int num_merging_chunks; | |
116 | ||
117 | /* | |
118 | * Incoming bios that overlap with chunks being merged must wait | |
119 | * for them to be committed. | |
120 | */ | |
121 | struct bio_list bios_queued_during_merge; | |
ccc45ea8 JB |
122 | }; |
123 | ||
1e03f97e MP |
124 | /* |
125 | * state_bits: | |
126 | * RUNNING_MERGE - Merge operation is in progress. | |
127 | * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped; | |
128 | * cleared afterwards. | |
129 | */ | |
130 | #define RUNNING_MERGE 0 | |
131 | #define SHUTDOWN_MERGE 1 | |
132 | ||
fc56f6fb MS |
133 | struct dm_dev *dm_snap_cow(struct dm_snapshot *s) |
134 | { | |
135 | return s->cow; | |
136 | } | |
137 | EXPORT_SYMBOL(dm_snap_cow); | |
138 | ||
c642f9e0 | 139 | static struct workqueue_struct *ksnapd; |
c4028958 | 140 | static void flush_queued_bios(struct work_struct *work); |
ca3a931f | 141 | |
ccc45ea8 JB |
142 | static sector_t chunk_to_sector(struct dm_exception_store *store, |
143 | chunk_t chunk) | |
144 | { | |
145 | return chunk << store->chunk_shift; | |
146 | } | |
147 | ||
148 | static int bdev_equal(struct block_device *lhs, struct block_device *rhs) | |
149 | { | |
150 | /* | |
151 | * There is only ever one instance of a particular block | |
152 | * device so we can compare pointers safely. | |
153 | */ | |
154 | return lhs == rhs; | |
155 | } | |
156 | ||
028867ac | 157 | struct dm_snap_pending_exception { |
1d4989c8 | 158 | struct dm_exception e; |
1da177e4 LT |
159 | |
160 | /* | |
161 | * Origin buffers waiting for this to complete are held | |
162 | * in a bio list | |
163 | */ | |
164 | struct bio_list origin_bios; | |
165 | struct bio_list snapshot_bios; | |
166 | ||
1da177e4 LT |
167 | /* Pointer back to snapshot context */ |
168 | struct dm_snapshot *snap; | |
169 | ||
170 | /* | |
171 | * 1 indicates the exception has already been sent to | |
172 | * kcopyd. | |
173 | */ | |
174 | int started; | |
175 | }; | |
176 | ||
177 | /* | |
178 | * Hash table mapping origin volumes to lists of snapshots and | |
179 | * a lock to protect it | |
180 | */ | |
e18b890b CL |
181 | static struct kmem_cache *exception_cache; |
182 | static struct kmem_cache *pending_cache; | |
1da177e4 | 183 | |
cd45daff MP |
184 | struct dm_snap_tracked_chunk { |
185 | struct hlist_node node; | |
186 | chunk_t chunk; | |
187 | }; | |
188 | ||
189 | static struct kmem_cache *tracked_chunk_cache; | |
190 | ||
191 | static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s, | |
192 | chunk_t chunk) | |
193 | { | |
194 | struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool, | |
195 | GFP_NOIO); | |
196 | unsigned long flags; | |
197 | ||
198 | c->chunk = chunk; | |
199 | ||
200 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); | |
201 | hlist_add_head(&c->node, | |
202 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]); | |
203 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); | |
204 | ||
205 | return c; | |
206 | } | |
207 | ||
208 | static void stop_tracking_chunk(struct dm_snapshot *s, | |
209 | struct dm_snap_tracked_chunk *c) | |
210 | { | |
211 | unsigned long flags; | |
212 | ||
213 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); | |
214 | hlist_del(&c->node); | |
215 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); | |
216 | ||
217 | mempool_free(c, s->tracked_chunk_pool); | |
218 | } | |
219 | ||
a8d41b59 MP |
220 | static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk) |
221 | { | |
222 | struct dm_snap_tracked_chunk *c; | |
223 | struct hlist_node *hn; | |
224 | int found = 0; | |
225 | ||
226 | spin_lock_irq(&s->tracked_chunk_lock); | |
227 | ||
228 | hlist_for_each_entry(c, hn, | |
229 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) { | |
230 | if (c->chunk == chunk) { | |
231 | found = 1; | |
232 | break; | |
233 | } | |
234 | } | |
235 | ||
236 | spin_unlock_irq(&s->tracked_chunk_lock); | |
237 | ||
238 | return found; | |
239 | } | |
240 | ||
615d1eb9 MS |
241 | /* |
242 | * This conflicting I/O is extremely improbable in the caller, | |
243 | * so msleep(1) is sufficient and there is no need for a wait queue. | |
244 | */ | |
245 | static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk) | |
246 | { | |
247 | while (__chunk_is_tracked(s, chunk)) | |
248 | msleep(1); | |
249 | } | |
250 | ||
1da177e4 LT |
251 | /* |
252 | * One of these per registered origin, held in the snapshot_origins hash | |
253 | */ | |
254 | struct origin { | |
255 | /* The origin device */ | |
256 | struct block_device *bdev; | |
257 | ||
258 | struct list_head hash_list; | |
259 | ||
260 | /* List of snapshots for this origin */ | |
261 | struct list_head snapshots; | |
262 | }; | |
263 | ||
264 | /* | |
265 | * Size of the hash table for origin volumes. If we make this | |
266 | * the size of the minors list then it should be nearly perfect | |
267 | */ | |
268 | #define ORIGIN_HASH_SIZE 256 | |
269 | #define ORIGIN_MASK 0xFF | |
270 | static struct list_head *_origins; | |
271 | static struct rw_semaphore _origins_lock; | |
272 | ||
273 | static int init_origin_hash(void) | |
274 | { | |
275 | int i; | |
276 | ||
277 | _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head), | |
278 | GFP_KERNEL); | |
279 | if (!_origins) { | |
72d94861 | 280 | DMERR("unable to allocate memory"); |
1da177e4 LT |
281 | return -ENOMEM; |
282 | } | |
283 | ||
284 | for (i = 0; i < ORIGIN_HASH_SIZE; i++) | |
285 | INIT_LIST_HEAD(_origins + i); | |
286 | init_rwsem(&_origins_lock); | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | static void exit_origin_hash(void) | |
292 | { | |
293 | kfree(_origins); | |
294 | } | |
295 | ||
028867ac | 296 | static unsigned origin_hash(struct block_device *bdev) |
1da177e4 LT |
297 | { |
298 | return bdev->bd_dev & ORIGIN_MASK; | |
299 | } | |
300 | ||
301 | static struct origin *__lookup_origin(struct block_device *origin) | |
302 | { | |
303 | struct list_head *ol; | |
304 | struct origin *o; | |
305 | ||
306 | ol = &_origins[origin_hash(origin)]; | |
307 | list_for_each_entry (o, ol, hash_list) | |
308 | if (bdev_equal(o->bdev, origin)) | |
309 | return o; | |
310 | ||
311 | return NULL; | |
312 | } | |
313 | ||
314 | static void __insert_origin(struct origin *o) | |
315 | { | |
316 | struct list_head *sl = &_origins[origin_hash(o->bdev)]; | |
317 | list_add_tail(&o->hash_list, sl); | |
318 | } | |
319 | ||
c1f0c183 MS |
320 | /* |
321 | * _origins_lock must be held when calling this function. | |
322 | * Returns number of snapshots registered using the supplied cow device, plus: | |
323 | * snap_src - a snapshot suitable for use as a source of exception handover | |
324 | * snap_dest - a snapshot capable of receiving exception handover. | |
9d3b15c4 MP |
325 | * snap_merge - an existing snapshot-merge target linked to the same origin. |
326 | * There can be at most one snapshot-merge target. The parameter is optional. | |
c1f0c183 | 327 | * |
9d3b15c4 | 328 | * Possible return values and states of snap_src and snap_dest. |
c1f0c183 MS |
329 | * 0: NULL, NULL - first new snapshot |
330 | * 1: snap_src, NULL - normal snapshot | |
331 | * 2: snap_src, snap_dest - waiting for handover | |
332 | * 2: snap_src, NULL - handed over, waiting for old to be deleted | |
333 | * 1: NULL, snap_dest - source got destroyed without handover | |
334 | */ | |
335 | static int __find_snapshots_sharing_cow(struct dm_snapshot *snap, | |
336 | struct dm_snapshot **snap_src, | |
9d3b15c4 MP |
337 | struct dm_snapshot **snap_dest, |
338 | struct dm_snapshot **snap_merge) | |
c1f0c183 MS |
339 | { |
340 | struct dm_snapshot *s; | |
341 | struct origin *o; | |
342 | int count = 0; | |
343 | int active; | |
344 | ||
345 | o = __lookup_origin(snap->origin->bdev); | |
346 | if (!o) | |
347 | goto out; | |
348 | ||
349 | list_for_each_entry(s, &o->snapshots, list) { | |
9d3b15c4 MP |
350 | if (dm_target_is_snapshot_merge(s->ti) && snap_merge) |
351 | *snap_merge = s; | |
c1f0c183 MS |
352 | if (!bdev_equal(s->cow->bdev, snap->cow->bdev)) |
353 | continue; | |
354 | ||
355 | down_read(&s->lock); | |
356 | active = s->active; | |
357 | up_read(&s->lock); | |
358 | ||
359 | if (active) { | |
360 | if (snap_src) | |
361 | *snap_src = s; | |
362 | } else if (snap_dest) | |
363 | *snap_dest = s; | |
364 | ||
365 | count++; | |
366 | } | |
367 | ||
368 | out: | |
369 | return count; | |
370 | } | |
371 | ||
372 | /* | |
373 | * On success, returns 1 if this snapshot is a handover destination, | |
374 | * otherwise returns 0. | |
375 | */ | |
376 | static int __validate_exception_handover(struct dm_snapshot *snap) | |
377 | { | |
378 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; | |
9d3b15c4 | 379 | struct dm_snapshot *snap_merge = NULL; |
c1f0c183 MS |
380 | |
381 | /* Does snapshot need exceptions handed over to it? */ | |
9d3b15c4 MP |
382 | if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest, |
383 | &snap_merge) == 2) || | |
c1f0c183 MS |
384 | snap_dest) { |
385 | snap->ti->error = "Snapshot cow pairing for exception " | |
386 | "table handover failed"; | |
387 | return -EINVAL; | |
388 | } | |
389 | ||
390 | /* | |
391 | * If no snap_src was found, snap cannot become a handover | |
392 | * destination. | |
393 | */ | |
394 | if (!snap_src) | |
395 | return 0; | |
396 | ||
9d3b15c4 MP |
397 | /* |
398 | * Non-snapshot-merge handover? | |
399 | */ | |
400 | if (!dm_target_is_snapshot_merge(snap->ti)) | |
401 | return 1; | |
402 | ||
403 | /* | |
404 | * Do not allow more than one merging snapshot. | |
405 | */ | |
406 | if (snap_merge) { | |
407 | snap->ti->error = "A snapshot is already merging."; | |
408 | return -EINVAL; | |
409 | } | |
410 | ||
1e03f97e MP |
411 | if (!snap_src->store->type->prepare_merge || |
412 | !snap_src->store->type->commit_merge) { | |
413 | snap->ti->error = "Snapshot exception store does not " | |
414 | "support snapshot-merge."; | |
415 | return -EINVAL; | |
416 | } | |
417 | ||
c1f0c183 MS |
418 | return 1; |
419 | } | |
420 | ||
421 | static void __insert_snapshot(struct origin *o, struct dm_snapshot *s) | |
422 | { | |
423 | struct dm_snapshot *l; | |
424 | ||
425 | /* Sort the list according to chunk size, largest-first smallest-last */ | |
426 | list_for_each_entry(l, &o->snapshots, list) | |
427 | if (l->store->chunk_size < s->store->chunk_size) | |
428 | break; | |
429 | list_add_tail(&s->list, &l->list); | |
430 | } | |
431 | ||
1da177e4 LT |
432 | /* |
433 | * Make a note of the snapshot and its origin so we can look it | |
434 | * up when the origin has a write on it. | |
c1f0c183 MS |
435 | * |
436 | * Also validate snapshot exception store handovers. | |
437 | * On success, returns 1 if this registration is a handover destination, | |
438 | * otherwise returns 0. | |
1da177e4 LT |
439 | */ |
440 | static int register_snapshot(struct dm_snapshot *snap) | |
441 | { | |
c1f0c183 | 442 | struct origin *o, *new_o = NULL; |
1da177e4 | 443 | struct block_device *bdev = snap->origin->bdev; |
c1f0c183 | 444 | int r = 0; |
1da177e4 | 445 | |
60c856c8 MP |
446 | new_o = kmalloc(sizeof(*new_o), GFP_KERNEL); |
447 | if (!new_o) | |
448 | return -ENOMEM; | |
449 | ||
1da177e4 | 450 | down_write(&_origins_lock); |
1da177e4 | 451 | |
c1f0c183 MS |
452 | r = __validate_exception_handover(snap); |
453 | if (r < 0) { | |
454 | kfree(new_o); | |
455 | goto out; | |
456 | } | |
457 | ||
458 | o = __lookup_origin(bdev); | |
60c856c8 MP |
459 | if (o) |
460 | kfree(new_o); | |
461 | else { | |
1da177e4 | 462 | /* New origin */ |
60c856c8 | 463 | o = new_o; |
1da177e4 LT |
464 | |
465 | /* Initialise the struct */ | |
466 | INIT_LIST_HEAD(&o->snapshots); | |
467 | o->bdev = bdev; | |
468 | ||
469 | __insert_origin(o); | |
470 | } | |
471 | ||
c1f0c183 MS |
472 | __insert_snapshot(o, snap); |
473 | ||
474 | out: | |
475 | up_write(&_origins_lock); | |
476 | ||
477 | return r; | |
478 | } | |
479 | ||
480 | /* | |
481 | * Move snapshot to correct place in list according to chunk size. | |
482 | */ | |
483 | static void reregister_snapshot(struct dm_snapshot *s) | |
484 | { | |
485 | struct block_device *bdev = s->origin->bdev; | |
486 | ||
487 | down_write(&_origins_lock); | |
488 | ||
489 | list_del(&s->list); | |
490 | __insert_snapshot(__lookup_origin(bdev), s); | |
1da177e4 LT |
491 | |
492 | up_write(&_origins_lock); | |
1da177e4 LT |
493 | } |
494 | ||
495 | static void unregister_snapshot(struct dm_snapshot *s) | |
496 | { | |
497 | struct origin *o; | |
498 | ||
499 | down_write(&_origins_lock); | |
500 | o = __lookup_origin(s->origin->bdev); | |
501 | ||
502 | list_del(&s->list); | |
c1f0c183 | 503 | if (o && list_empty(&o->snapshots)) { |
1da177e4 LT |
504 | list_del(&o->hash_list); |
505 | kfree(o); | |
506 | } | |
507 | ||
508 | up_write(&_origins_lock); | |
509 | } | |
510 | ||
511 | /* | |
512 | * Implementation of the exception hash tables. | |
d74f81f8 MB |
513 | * The lowest hash_shift bits of the chunk number are ignored, allowing |
514 | * some consecutive chunks to be grouped together. | |
1da177e4 | 515 | */ |
3510cb94 JB |
516 | static int dm_exception_table_init(struct dm_exception_table *et, |
517 | uint32_t size, unsigned hash_shift) | |
1da177e4 LT |
518 | { |
519 | unsigned int i; | |
520 | ||
d74f81f8 | 521 | et->hash_shift = hash_shift; |
1da177e4 LT |
522 | et->hash_mask = size - 1; |
523 | et->table = dm_vcalloc(size, sizeof(struct list_head)); | |
524 | if (!et->table) | |
525 | return -ENOMEM; | |
526 | ||
527 | for (i = 0; i < size; i++) | |
528 | INIT_LIST_HEAD(et->table + i); | |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
3510cb94 JB |
533 | static void dm_exception_table_exit(struct dm_exception_table *et, |
534 | struct kmem_cache *mem) | |
1da177e4 LT |
535 | { |
536 | struct list_head *slot; | |
1d4989c8 | 537 | struct dm_exception *ex, *next; |
1da177e4 LT |
538 | int i, size; |
539 | ||
540 | size = et->hash_mask + 1; | |
541 | for (i = 0; i < size; i++) { | |
542 | slot = et->table + i; | |
543 | ||
544 | list_for_each_entry_safe (ex, next, slot, hash_list) | |
545 | kmem_cache_free(mem, ex); | |
546 | } | |
547 | ||
548 | vfree(et->table); | |
549 | } | |
550 | ||
191437a5 | 551 | static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk) |
1da177e4 | 552 | { |
d74f81f8 | 553 | return (chunk >> et->hash_shift) & et->hash_mask; |
1da177e4 LT |
554 | } |
555 | ||
3510cb94 | 556 | static void dm_remove_exception(struct dm_exception *e) |
1da177e4 LT |
557 | { |
558 | list_del(&e->hash_list); | |
559 | } | |
560 | ||
561 | /* | |
562 | * Return the exception data for a sector, or NULL if not | |
563 | * remapped. | |
564 | */ | |
3510cb94 JB |
565 | static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et, |
566 | chunk_t chunk) | |
1da177e4 LT |
567 | { |
568 | struct list_head *slot; | |
1d4989c8 | 569 | struct dm_exception *e; |
1da177e4 LT |
570 | |
571 | slot = &et->table[exception_hash(et, chunk)]; | |
572 | list_for_each_entry (e, slot, hash_list) | |
d74f81f8 MB |
573 | if (chunk >= e->old_chunk && |
574 | chunk <= e->old_chunk + dm_consecutive_chunk_count(e)) | |
1da177e4 LT |
575 | return e; |
576 | ||
577 | return NULL; | |
578 | } | |
579 | ||
3510cb94 | 580 | static struct dm_exception *alloc_completed_exception(void) |
1da177e4 | 581 | { |
1d4989c8 | 582 | struct dm_exception *e; |
1da177e4 LT |
583 | |
584 | e = kmem_cache_alloc(exception_cache, GFP_NOIO); | |
585 | if (!e) | |
586 | e = kmem_cache_alloc(exception_cache, GFP_ATOMIC); | |
587 | ||
588 | return e; | |
589 | } | |
590 | ||
3510cb94 | 591 | static void free_completed_exception(struct dm_exception *e) |
1da177e4 LT |
592 | { |
593 | kmem_cache_free(exception_cache, e); | |
594 | } | |
595 | ||
92e86812 | 596 | static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s) |
1da177e4 | 597 | { |
92e86812 MP |
598 | struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool, |
599 | GFP_NOIO); | |
600 | ||
879129d2 | 601 | atomic_inc(&s->pending_exceptions_count); |
92e86812 MP |
602 | pe->snap = s; |
603 | ||
604 | return pe; | |
1da177e4 LT |
605 | } |
606 | ||
028867ac | 607 | static void free_pending_exception(struct dm_snap_pending_exception *pe) |
1da177e4 | 608 | { |
879129d2 MP |
609 | struct dm_snapshot *s = pe->snap; |
610 | ||
611 | mempool_free(pe, s->pending_pool); | |
612 | smp_mb__before_atomic_dec(); | |
613 | atomic_dec(&s->pending_exceptions_count); | |
1da177e4 LT |
614 | } |
615 | ||
3510cb94 JB |
616 | static void dm_insert_exception(struct dm_exception_table *eh, |
617 | struct dm_exception *new_e) | |
d74f81f8 | 618 | { |
d74f81f8 | 619 | struct list_head *l; |
1d4989c8 | 620 | struct dm_exception *e = NULL; |
d74f81f8 MB |
621 | |
622 | l = &eh->table[exception_hash(eh, new_e->old_chunk)]; | |
623 | ||
624 | /* Add immediately if this table doesn't support consecutive chunks */ | |
625 | if (!eh->hash_shift) | |
626 | goto out; | |
627 | ||
628 | /* List is ordered by old_chunk */ | |
629 | list_for_each_entry_reverse(e, l, hash_list) { | |
630 | /* Insert after an existing chunk? */ | |
631 | if (new_e->old_chunk == (e->old_chunk + | |
632 | dm_consecutive_chunk_count(e) + 1) && | |
633 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) + | |
634 | dm_consecutive_chunk_count(e) + 1)) { | |
635 | dm_consecutive_chunk_count_inc(e); | |
3510cb94 | 636 | free_completed_exception(new_e); |
d74f81f8 MB |
637 | return; |
638 | } | |
639 | ||
640 | /* Insert before an existing chunk? */ | |
641 | if (new_e->old_chunk == (e->old_chunk - 1) && | |
642 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) { | |
643 | dm_consecutive_chunk_count_inc(e); | |
644 | e->old_chunk--; | |
645 | e->new_chunk--; | |
3510cb94 | 646 | free_completed_exception(new_e); |
d74f81f8 MB |
647 | return; |
648 | } | |
649 | ||
650 | if (new_e->old_chunk > e->old_chunk) | |
651 | break; | |
652 | } | |
653 | ||
654 | out: | |
655 | list_add(&new_e->hash_list, e ? &e->hash_list : l); | |
656 | } | |
657 | ||
a159c1ac JB |
658 | /* |
659 | * Callback used by the exception stores to load exceptions when | |
660 | * initialising. | |
661 | */ | |
662 | static int dm_add_exception(void *context, chunk_t old, chunk_t new) | |
1da177e4 | 663 | { |
a159c1ac | 664 | struct dm_snapshot *s = context; |
1d4989c8 | 665 | struct dm_exception *e; |
1da177e4 | 666 | |
3510cb94 | 667 | e = alloc_completed_exception(); |
1da177e4 LT |
668 | if (!e) |
669 | return -ENOMEM; | |
670 | ||
671 | e->old_chunk = old; | |
d74f81f8 MB |
672 | |
673 | /* Consecutive_count is implicitly initialised to zero */ | |
1da177e4 | 674 | e->new_chunk = new; |
d74f81f8 | 675 | |
3510cb94 | 676 | dm_insert_exception(&s->complete, e); |
d74f81f8 | 677 | |
1da177e4 LT |
678 | return 0; |
679 | } | |
680 | ||
7e201b35 MP |
681 | #define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r))) |
682 | ||
683 | /* | |
684 | * Return a minimum chunk size of all snapshots that have the specified origin. | |
685 | * Return zero if the origin has no snapshots. | |
686 | */ | |
687 | static sector_t __minimum_chunk_size(struct origin *o) | |
688 | { | |
689 | struct dm_snapshot *snap; | |
690 | unsigned chunk_size = 0; | |
691 | ||
692 | if (o) | |
693 | list_for_each_entry(snap, &o->snapshots, list) | |
694 | chunk_size = min_not_zero(chunk_size, | |
695 | snap->store->chunk_size); | |
696 | ||
697 | return chunk_size; | |
698 | } | |
699 | ||
1da177e4 LT |
700 | /* |
701 | * Hard coded magic. | |
702 | */ | |
703 | static int calc_max_buckets(void) | |
704 | { | |
705 | /* use a fixed size of 2MB */ | |
706 | unsigned long mem = 2 * 1024 * 1024; | |
707 | mem /= sizeof(struct list_head); | |
708 | ||
709 | return mem; | |
710 | } | |
711 | ||
1da177e4 LT |
712 | /* |
713 | * Allocate room for a suitable hash table. | |
714 | */ | |
fee1998e | 715 | static int init_hash_tables(struct dm_snapshot *s) |
1da177e4 LT |
716 | { |
717 | sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets; | |
718 | ||
719 | /* | |
720 | * Calculate based on the size of the original volume or | |
721 | * the COW volume... | |
722 | */ | |
fc56f6fb | 723 | cow_dev_size = get_dev_size(s->cow->bdev); |
1da177e4 LT |
724 | origin_dev_size = get_dev_size(s->origin->bdev); |
725 | max_buckets = calc_max_buckets(); | |
726 | ||
fee1998e | 727 | hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift; |
1da177e4 LT |
728 | hash_size = min(hash_size, max_buckets); |
729 | ||
8e87b9b8 MP |
730 | if (hash_size < 64) |
731 | hash_size = 64; | |
8defd830 | 732 | hash_size = rounddown_pow_of_two(hash_size); |
3510cb94 JB |
733 | if (dm_exception_table_init(&s->complete, hash_size, |
734 | DM_CHUNK_CONSECUTIVE_BITS)) | |
1da177e4 LT |
735 | return -ENOMEM; |
736 | ||
737 | /* | |
738 | * Allocate hash table for in-flight exceptions | |
739 | * Make this smaller than the real hash table | |
740 | */ | |
741 | hash_size >>= 3; | |
742 | if (hash_size < 64) | |
743 | hash_size = 64; | |
744 | ||
3510cb94 JB |
745 | if (dm_exception_table_init(&s->pending, hash_size, 0)) { |
746 | dm_exception_table_exit(&s->complete, exception_cache); | |
1da177e4 LT |
747 | return -ENOMEM; |
748 | } | |
749 | ||
750 | return 0; | |
751 | } | |
752 | ||
1e03f97e MP |
753 | static void merge_shutdown(struct dm_snapshot *s) |
754 | { | |
755 | clear_bit_unlock(RUNNING_MERGE, &s->state_bits); | |
756 | smp_mb__after_clear_bit(); | |
757 | wake_up_bit(&s->state_bits, RUNNING_MERGE); | |
758 | } | |
759 | ||
9fe86254 MP |
760 | static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s) |
761 | { | |
762 | s->first_merging_chunk = 0; | |
763 | s->num_merging_chunks = 0; | |
764 | ||
765 | return bio_list_get(&s->bios_queued_during_merge); | |
766 | } | |
767 | ||
1e03f97e MP |
768 | /* |
769 | * Remove one chunk from the index of completed exceptions. | |
770 | */ | |
771 | static int __remove_single_exception_chunk(struct dm_snapshot *s, | |
772 | chunk_t old_chunk) | |
773 | { | |
774 | struct dm_exception *e; | |
775 | ||
1e03f97e MP |
776 | e = dm_lookup_exception(&s->complete, old_chunk); |
777 | if (!e) { | |
778 | DMERR("Corruption detected: exception for block %llu is " | |
779 | "on disk but not in memory", | |
780 | (unsigned long long)old_chunk); | |
781 | return -EINVAL; | |
782 | } | |
783 | ||
784 | /* | |
785 | * If this is the only chunk using this exception, remove exception. | |
786 | */ | |
787 | if (!dm_consecutive_chunk_count(e)) { | |
788 | dm_remove_exception(e); | |
789 | free_completed_exception(e); | |
790 | return 0; | |
791 | } | |
792 | ||
793 | /* | |
794 | * The chunk may be either at the beginning or the end of a | |
795 | * group of consecutive chunks - never in the middle. We are | |
796 | * removing chunks in the opposite order to that in which they | |
797 | * were added, so this should always be true. | |
798 | * Decrement the consecutive chunk counter and adjust the | |
799 | * starting point if necessary. | |
800 | */ | |
801 | if (old_chunk == e->old_chunk) { | |
802 | e->old_chunk++; | |
803 | e->new_chunk++; | |
804 | } else if (old_chunk != e->old_chunk + | |
805 | dm_consecutive_chunk_count(e)) { | |
806 | DMERR("Attempt to merge block %llu from the " | |
807 | "middle of a chunk range [%llu - %llu]", | |
808 | (unsigned long long)old_chunk, | |
809 | (unsigned long long)e->old_chunk, | |
810 | (unsigned long long) | |
811 | e->old_chunk + dm_consecutive_chunk_count(e)); | |
812 | return -EINVAL; | |
813 | } | |
814 | ||
815 | dm_consecutive_chunk_count_dec(e); | |
816 | ||
817 | return 0; | |
818 | } | |
819 | ||
9fe86254 MP |
820 | static void flush_bios(struct bio *bio); |
821 | ||
822 | static int remove_single_exception_chunk(struct dm_snapshot *s) | |
1e03f97e | 823 | { |
9fe86254 MP |
824 | struct bio *b = NULL; |
825 | int r; | |
826 | chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1; | |
1e03f97e MP |
827 | |
828 | down_write(&s->lock); | |
9fe86254 MP |
829 | |
830 | /* | |
831 | * Process chunks (and associated exceptions) in reverse order | |
832 | * so that dm_consecutive_chunk_count_dec() accounting works. | |
833 | */ | |
834 | do { | |
835 | r = __remove_single_exception_chunk(s, old_chunk); | |
836 | if (r) | |
837 | goto out; | |
838 | } while (old_chunk-- > s->first_merging_chunk); | |
839 | ||
840 | b = __release_queued_bios_after_merge(s); | |
841 | ||
842 | out: | |
1e03f97e | 843 | up_write(&s->lock); |
9fe86254 MP |
844 | if (b) |
845 | flush_bios(b); | |
1e03f97e MP |
846 | |
847 | return r; | |
848 | } | |
849 | ||
850 | static void merge_callback(int read_err, unsigned long write_err, | |
851 | void *context); | |
852 | ||
853 | static void snapshot_merge_next_chunks(struct dm_snapshot *s) | |
854 | { | |
855 | int r; | |
856 | chunk_t old_chunk, new_chunk; | |
857 | struct dm_io_region src, dest; | |
858 | ||
859 | BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits)); | |
860 | if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits))) | |
861 | goto shut; | |
862 | ||
863 | /* | |
864 | * valid flag never changes during merge, so no lock required. | |
865 | */ | |
866 | if (!s->valid) { | |
867 | DMERR("Snapshot is invalid: can't merge"); | |
868 | goto shut; | |
869 | } | |
870 | ||
871 | r = s->store->type->prepare_merge(s->store, &old_chunk, &new_chunk); | |
872 | if (r <= 0) { | |
873 | if (r < 0) | |
874 | DMERR("Read error in exception store: " | |
875 | "shutting down merge"); | |
876 | goto shut; | |
877 | } | |
878 | ||
879 | /* TODO: use larger I/O size once we verify that kcopyd handles it */ | |
880 | ||
1e03f97e MP |
881 | dest.bdev = s->origin->bdev; |
882 | dest.sector = chunk_to_sector(s->store, old_chunk); | |
883 | dest.count = min((sector_t)s->store->chunk_size, | |
884 | get_dev_size(dest.bdev) - dest.sector); | |
885 | ||
886 | src.bdev = s->cow->bdev; | |
887 | src.sector = chunk_to_sector(s->store, new_chunk); | |
888 | src.count = dest.count; | |
889 | ||
9fe86254 MP |
890 | down_write(&s->lock); |
891 | s->first_merging_chunk = old_chunk; | |
892 | s->num_merging_chunks = 1; | |
893 | up_write(&s->lock); | |
894 | ||
17aa0332 | 895 | __check_for_conflicting_io(s, old_chunk); |
9fe86254 | 896 | |
1e03f97e MP |
897 | dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s); |
898 | return; | |
899 | ||
900 | shut: | |
901 | merge_shutdown(s); | |
902 | } | |
903 | ||
9fe86254 MP |
904 | static void error_bios(struct bio *bio); |
905 | ||
1e03f97e MP |
906 | static void merge_callback(int read_err, unsigned long write_err, void *context) |
907 | { | |
908 | struct dm_snapshot *s = context; | |
9fe86254 | 909 | struct bio *b = NULL; |
1e03f97e MP |
910 | |
911 | if (read_err || write_err) { | |
912 | if (read_err) | |
913 | DMERR("Read error: shutting down merge."); | |
914 | else | |
915 | DMERR("Write error: shutting down merge."); | |
916 | goto shut; | |
917 | } | |
918 | ||
9fe86254 MP |
919 | if (s->store->type->commit_merge(s->store, |
920 | s->num_merging_chunks) < 0) { | |
1e03f97e MP |
921 | DMERR("Write error in exception store: shutting down merge"); |
922 | goto shut; | |
923 | } | |
924 | ||
9fe86254 MP |
925 | if (remove_single_exception_chunk(s) < 0) |
926 | goto shut; | |
927 | ||
1e03f97e MP |
928 | snapshot_merge_next_chunks(s); |
929 | ||
930 | return; | |
931 | ||
932 | shut: | |
9fe86254 MP |
933 | down_write(&s->lock); |
934 | b = __release_queued_bios_after_merge(s); | |
935 | up_write(&s->lock); | |
936 | error_bios(b); | |
937 | ||
1e03f97e MP |
938 | merge_shutdown(s); |
939 | } | |
940 | ||
941 | static void start_merge(struct dm_snapshot *s) | |
942 | { | |
943 | if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits)) | |
944 | snapshot_merge_next_chunks(s); | |
945 | } | |
946 | ||
947 | static int wait_schedule(void *ptr) | |
948 | { | |
949 | schedule(); | |
950 | ||
951 | return 0; | |
952 | } | |
953 | ||
954 | /* | |
955 | * Stop the merging process and wait until it finishes. | |
956 | */ | |
957 | static void stop_merge(struct dm_snapshot *s) | |
958 | { | |
959 | set_bit(SHUTDOWN_MERGE, &s->state_bits); | |
960 | wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule, | |
961 | TASK_UNINTERRUPTIBLE); | |
962 | clear_bit(SHUTDOWN_MERGE, &s->state_bits); | |
963 | } | |
964 | ||
1da177e4 LT |
965 | /* |
966 | * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size> | |
967 | */ | |
968 | static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
969 | { | |
970 | struct dm_snapshot *s; | |
cd45daff | 971 | int i; |
1da177e4 | 972 | int r = -EINVAL; |
fc56f6fb | 973 | char *origin_path, *cow_path; |
10b8106a MS |
974 | unsigned args_used, num_flush_requests = 1; |
975 | fmode_t origin_mode = FMODE_READ; | |
1da177e4 | 976 | |
4c7e3bf4 | 977 | if (argc != 4) { |
72d94861 | 978 | ti->error = "requires exactly 4 arguments"; |
1da177e4 | 979 | r = -EINVAL; |
fc56f6fb | 980 | goto bad; |
1da177e4 LT |
981 | } |
982 | ||
10b8106a MS |
983 | if (dm_target_is_snapshot_merge(ti)) { |
984 | num_flush_requests = 2; | |
985 | origin_mode = FMODE_WRITE; | |
986 | } | |
987 | ||
1da177e4 | 988 | origin_path = argv[0]; |
fee1998e JB |
989 | argv++; |
990 | argc--; | |
1da177e4 | 991 | |
fc56f6fb MS |
992 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
993 | if (!s) { | |
994 | ti->error = "Cannot allocate snapshot context private " | |
995 | "structure"; | |
996 | r = -ENOMEM; | |
997 | goto bad; | |
998 | } | |
999 | ||
1000 | cow_path = argv[0]; | |
1001 | argv++; | |
1002 | argc--; | |
1003 | ||
1004 | r = dm_get_device(ti, cow_path, 0, 0, | |
1005 | FMODE_READ | FMODE_WRITE, &s->cow); | |
1006 | if (r) { | |
1007 | ti->error = "Cannot get COW device"; | |
1008 | goto bad_cow; | |
1009 | } | |
1010 | ||
1011 | r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store); | |
fee1998e JB |
1012 | if (r) { |
1013 | ti->error = "Couldn't create exception store"; | |
1da177e4 | 1014 | r = -EINVAL; |
fc56f6fb | 1015 | goto bad_store; |
1da177e4 LT |
1016 | } |
1017 | ||
fee1998e JB |
1018 | argv += args_used; |
1019 | argc -= args_used; | |
1020 | ||
10b8106a | 1021 | r = dm_get_device(ti, origin_path, 0, ti->len, origin_mode, &s->origin); |
1da177e4 LT |
1022 | if (r) { |
1023 | ti->error = "Cannot get origin device"; | |
fee1998e | 1024 | goto bad_origin; |
1da177e4 LT |
1025 | } |
1026 | ||
fc56f6fb | 1027 | s->ti = ti; |
1da177e4 | 1028 | s->valid = 1; |
aa14edeb | 1029 | s->active = 0; |
c26655ca | 1030 | s->suspended = 0; |
879129d2 | 1031 | atomic_set(&s->pending_exceptions_count, 0); |
1da177e4 | 1032 | init_rwsem(&s->lock); |
c1f0c183 | 1033 | INIT_LIST_HEAD(&s->list); |
ca3a931f | 1034 | spin_lock_init(&s->pe_lock); |
1e03f97e | 1035 | s->state_bits = 0; |
9fe86254 MP |
1036 | s->first_merging_chunk = 0; |
1037 | s->num_merging_chunks = 0; | |
1038 | bio_list_init(&s->bios_queued_during_merge); | |
1da177e4 LT |
1039 | |
1040 | /* Allocate hash table for COW data */ | |
fee1998e | 1041 | if (init_hash_tables(s)) { |
1da177e4 LT |
1042 | ti->error = "Unable to allocate hash table space"; |
1043 | r = -ENOMEM; | |
fee1998e | 1044 | goto bad_hash_tables; |
1da177e4 LT |
1045 | } |
1046 | ||
eb69aca5 | 1047 | r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client); |
1da177e4 LT |
1048 | if (r) { |
1049 | ti->error = "Could not create kcopyd client"; | |
fee1998e | 1050 | goto bad_kcopyd; |
1da177e4 LT |
1051 | } |
1052 | ||
92e86812 MP |
1053 | s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache); |
1054 | if (!s->pending_pool) { | |
1055 | ti->error = "Could not allocate mempool for pending exceptions"; | |
fee1998e | 1056 | goto bad_pending_pool; |
92e86812 MP |
1057 | } |
1058 | ||
cd45daff MP |
1059 | s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS, |
1060 | tracked_chunk_cache); | |
1061 | if (!s->tracked_chunk_pool) { | |
1062 | ti->error = "Could not allocate tracked_chunk mempool for " | |
1063 | "tracking reads"; | |
92e86812 | 1064 | goto bad_tracked_chunk_pool; |
cd45daff MP |
1065 | } |
1066 | ||
1067 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) | |
1068 | INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]); | |
1069 | ||
1070 | spin_lock_init(&s->tracked_chunk_lock); | |
1071 | ||
c1f0c183 MS |
1072 | bio_list_init(&s->queued_bios); |
1073 | INIT_WORK(&s->queued_bios_work, flush_queued_bios); | |
1074 | ||
1075 | ti->private = s; | |
10b8106a | 1076 | ti->num_flush_requests = num_flush_requests; |
c1f0c183 MS |
1077 | |
1078 | /* Add snapshot to the list of snapshots for this origin */ | |
1079 | /* Exceptions aren't triggered till snapshot_resume() is called */ | |
1080 | r = register_snapshot(s); | |
1081 | if (r == -ENOMEM) { | |
1082 | ti->error = "Snapshot origin struct allocation failed"; | |
1083 | goto bad_load_and_register; | |
1084 | } else if (r < 0) { | |
1085 | /* invalid handover, register_snapshot has set ti->error */ | |
1086 | goto bad_load_and_register; | |
1087 | } | |
1088 | ||
1089 | /* | |
1090 | * Metadata must only be loaded into one table at once, so skip this | |
1091 | * if metadata will be handed over during resume. | |
1092 | * Chunk size will be set during the handover - set it to zero to | |
1093 | * ensure it's ignored. | |
1094 | */ | |
1095 | if (r > 0) { | |
1096 | s->store->chunk_size = 0; | |
1097 | return 0; | |
1098 | } | |
1099 | ||
493df71c JB |
1100 | r = s->store->type->read_metadata(s->store, dm_add_exception, |
1101 | (void *)s); | |
0764147b | 1102 | if (r < 0) { |
f9cea4f7 | 1103 | ti->error = "Failed to read snapshot metadata"; |
c1f0c183 | 1104 | goto bad_read_metadata; |
0764147b MB |
1105 | } else if (r > 0) { |
1106 | s->valid = 0; | |
1107 | DMWARN("Snapshot is marked invalid."); | |
f9cea4f7 | 1108 | } |
aa14edeb | 1109 | |
3f2412dc MP |
1110 | if (!s->store->chunk_size) { |
1111 | ti->error = "Chunk size not set"; | |
c1f0c183 | 1112 | goto bad_read_metadata; |
1da177e4 | 1113 | } |
d0216849 | 1114 | ti->split_io = s->store->chunk_size; |
1da177e4 LT |
1115 | |
1116 | return 0; | |
1117 | ||
c1f0c183 MS |
1118 | bad_read_metadata: |
1119 | unregister_snapshot(s); | |
1120 | ||
fee1998e | 1121 | bad_load_and_register: |
cd45daff MP |
1122 | mempool_destroy(s->tracked_chunk_pool); |
1123 | ||
fee1998e | 1124 | bad_tracked_chunk_pool: |
92e86812 MP |
1125 | mempool_destroy(s->pending_pool); |
1126 | ||
fee1998e | 1127 | bad_pending_pool: |
eb69aca5 | 1128 | dm_kcopyd_client_destroy(s->kcopyd_client); |
1da177e4 | 1129 | |
fee1998e | 1130 | bad_kcopyd: |
3510cb94 JB |
1131 | dm_exception_table_exit(&s->pending, pending_cache); |
1132 | dm_exception_table_exit(&s->complete, exception_cache); | |
1da177e4 | 1133 | |
fee1998e | 1134 | bad_hash_tables: |
1da177e4 LT |
1135 | dm_put_device(ti, s->origin); |
1136 | ||
fee1998e | 1137 | bad_origin: |
fc56f6fb | 1138 | dm_exception_store_destroy(s->store); |
1da177e4 | 1139 | |
fc56f6fb MS |
1140 | bad_store: |
1141 | dm_put_device(ti, s->cow); | |
fee1998e | 1142 | |
fc56f6fb MS |
1143 | bad_cow: |
1144 | kfree(s); | |
1145 | ||
1146 | bad: | |
1da177e4 LT |
1147 | return r; |
1148 | } | |
1149 | ||
31c93a0c MB |
1150 | static void __free_exceptions(struct dm_snapshot *s) |
1151 | { | |
eb69aca5 | 1152 | dm_kcopyd_client_destroy(s->kcopyd_client); |
31c93a0c MB |
1153 | s->kcopyd_client = NULL; |
1154 | ||
3510cb94 JB |
1155 | dm_exception_table_exit(&s->pending, pending_cache); |
1156 | dm_exception_table_exit(&s->complete, exception_cache); | |
31c93a0c MB |
1157 | } |
1158 | ||
c1f0c183 MS |
1159 | static void __handover_exceptions(struct dm_snapshot *snap_src, |
1160 | struct dm_snapshot *snap_dest) | |
1161 | { | |
1162 | union { | |
1163 | struct dm_exception_table table_swap; | |
1164 | struct dm_exception_store *store_swap; | |
1165 | } u; | |
1166 | ||
1167 | /* | |
1168 | * Swap all snapshot context information between the two instances. | |
1169 | */ | |
1170 | u.table_swap = snap_dest->complete; | |
1171 | snap_dest->complete = snap_src->complete; | |
1172 | snap_src->complete = u.table_swap; | |
1173 | ||
1174 | u.store_swap = snap_dest->store; | |
1175 | snap_dest->store = snap_src->store; | |
1176 | snap_src->store = u.store_swap; | |
1177 | ||
1178 | snap_dest->store->snap = snap_dest; | |
1179 | snap_src->store->snap = snap_src; | |
1180 | ||
1181 | snap_dest->ti->split_io = snap_dest->store->chunk_size; | |
1182 | snap_dest->valid = snap_src->valid; | |
1183 | ||
1184 | /* | |
1185 | * Set source invalid to ensure it receives no further I/O. | |
1186 | */ | |
1187 | snap_src->valid = 0; | |
1188 | } | |
1189 | ||
1da177e4 LT |
1190 | static void snapshot_dtr(struct dm_target *ti) |
1191 | { | |
cd45daff MP |
1192 | #ifdef CONFIG_DM_DEBUG |
1193 | int i; | |
1194 | #endif | |
028867ac | 1195 | struct dm_snapshot *s = ti->private; |
c1f0c183 | 1196 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
1da177e4 | 1197 | |
ca3a931f AK |
1198 | flush_workqueue(ksnapd); |
1199 | ||
c1f0c183 MS |
1200 | down_read(&_origins_lock); |
1201 | /* Check whether exception handover must be cancelled */ | |
9d3b15c4 | 1202 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
c1f0c183 MS |
1203 | if (snap_src && snap_dest && (s == snap_src)) { |
1204 | down_write(&snap_dest->lock); | |
1205 | snap_dest->valid = 0; | |
1206 | up_write(&snap_dest->lock); | |
1207 | DMERR("Cancelling snapshot handover."); | |
1208 | } | |
1209 | up_read(&_origins_lock); | |
1210 | ||
1e03f97e MP |
1211 | if (dm_target_is_snapshot_merge(ti)) |
1212 | stop_merge(s); | |
1213 | ||
138728dc AK |
1214 | /* Prevent further origin writes from using this snapshot. */ |
1215 | /* After this returns there can be no new kcopyd jobs. */ | |
1da177e4 LT |
1216 | unregister_snapshot(s); |
1217 | ||
879129d2 | 1218 | while (atomic_read(&s->pending_exceptions_count)) |
90fa1527 | 1219 | msleep(1); |
879129d2 MP |
1220 | /* |
1221 | * Ensure instructions in mempool_destroy aren't reordered | |
1222 | * before atomic_read. | |
1223 | */ | |
1224 | smp_mb(); | |
1225 | ||
cd45daff MP |
1226 | #ifdef CONFIG_DM_DEBUG |
1227 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) | |
1228 | BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i])); | |
1229 | #endif | |
1230 | ||
1231 | mempool_destroy(s->tracked_chunk_pool); | |
1232 | ||
31c93a0c | 1233 | __free_exceptions(s); |
1da177e4 | 1234 | |
92e86812 MP |
1235 | mempool_destroy(s->pending_pool); |
1236 | ||
1da177e4 | 1237 | dm_put_device(ti, s->origin); |
fee1998e JB |
1238 | |
1239 | dm_exception_store_destroy(s->store); | |
138728dc | 1240 | |
fc56f6fb MS |
1241 | dm_put_device(ti, s->cow); |
1242 | ||
1da177e4 LT |
1243 | kfree(s); |
1244 | } | |
1245 | ||
1246 | /* | |
1247 | * Flush a list of buffers. | |
1248 | */ | |
1249 | static void flush_bios(struct bio *bio) | |
1250 | { | |
1251 | struct bio *n; | |
1252 | ||
1253 | while (bio) { | |
1254 | n = bio->bi_next; | |
1255 | bio->bi_next = NULL; | |
1256 | generic_make_request(bio); | |
1257 | bio = n; | |
1258 | } | |
1259 | } | |
1260 | ||
c4028958 | 1261 | static void flush_queued_bios(struct work_struct *work) |
ca3a931f | 1262 | { |
c4028958 DH |
1263 | struct dm_snapshot *s = |
1264 | container_of(work, struct dm_snapshot, queued_bios_work); | |
ca3a931f AK |
1265 | struct bio *queued_bios; |
1266 | unsigned long flags; | |
1267 | ||
1268 | spin_lock_irqsave(&s->pe_lock, flags); | |
1269 | queued_bios = bio_list_get(&s->queued_bios); | |
1270 | spin_unlock_irqrestore(&s->pe_lock, flags); | |
1271 | ||
1272 | flush_bios(queued_bios); | |
1273 | } | |
1274 | ||
515ad66c MP |
1275 | static int do_origin(struct dm_dev *origin, struct bio *bio); |
1276 | ||
1277 | /* | |
1278 | * Flush a list of buffers. | |
1279 | */ | |
1280 | static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio) | |
1281 | { | |
1282 | struct bio *n; | |
1283 | int r; | |
1284 | ||
1285 | while (bio) { | |
1286 | n = bio->bi_next; | |
1287 | bio->bi_next = NULL; | |
1288 | r = do_origin(s->origin, bio); | |
1289 | if (r == DM_MAPIO_REMAPPED) | |
1290 | generic_make_request(bio); | |
1291 | bio = n; | |
1292 | } | |
1293 | } | |
1294 | ||
1da177e4 LT |
1295 | /* |
1296 | * Error a list of buffers. | |
1297 | */ | |
1298 | static void error_bios(struct bio *bio) | |
1299 | { | |
1300 | struct bio *n; | |
1301 | ||
1302 | while (bio) { | |
1303 | n = bio->bi_next; | |
1304 | bio->bi_next = NULL; | |
6712ecf8 | 1305 | bio_io_error(bio); |
1da177e4 LT |
1306 | bio = n; |
1307 | } | |
1308 | } | |
1309 | ||
695368ac | 1310 | static void __invalidate_snapshot(struct dm_snapshot *s, int err) |
76df1c65 AK |
1311 | { |
1312 | if (!s->valid) | |
1313 | return; | |
1314 | ||
1315 | if (err == -EIO) | |
1316 | DMERR("Invalidating snapshot: Error reading/writing."); | |
1317 | else if (err == -ENOMEM) | |
1318 | DMERR("Invalidating snapshot: Unable to allocate exception."); | |
1319 | ||
493df71c JB |
1320 | if (s->store->type->drop_snapshot) |
1321 | s->store->type->drop_snapshot(s->store); | |
76df1c65 AK |
1322 | |
1323 | s->valid = 0; | |
1324 | ||
fc56f6fb | 1325 | dm_table_event(s->ti->table); |
76df1c65 AK |
1326 | } |
1327 | ||
028867ac | 1328 | static void pending_complete(struct dm_snap_pending_exception *pe, int success) |
1da177e4 | 1329 | { |
1d4989c8 | 1330 | struct dm_exception *e; |
1da177e4 | 1331 | struct dm_snapshot *s = pe->snap; |
9d493fa8 AK |
1332 | struct bio *origin_bios = NULL; |
1333 | struct bio *snapshot_bios = NULL; | |
1334 | int error = 0; | |
1da177e4 | 1335 | |
76df1c65 AK |
1336 | if (!success) { |
1337 | /* Read/write error - snapshot is unusable */ | |
1da177e4 | 1338 | down_write(&s->lock); |
695368ac | 1339 | __invalidate_snapshot(s, -EIO); |
9d493fa8 | 1340 | error = 1; |
76df1c65 AK |
1341 | goto out; |
1342 | } | |
1343 | ||
3510cb94 | 1344 | e = alloc_completed_exception(); |
76df1c65 | 1345 | if (!e) { |
1da177e4 | 1346 | down_write(&s->lock); |
695368ac | 1347 | __invalidate_snapshot(s, -ENOMEM); |
9d493fa8 | 1348 | error = 1; |
76df1c65 AK |
1349 | goto out; |
1350 | } | |
1351 | *e = pe->e; | |
1da177e4 | 1352 | |
76df1c65 AK |
1353 | down_write(&s->lock); |
1354 | if (!s->valid) { | |
3510cb94 | 1355 | free_completed_exception(e); |
9d493fa8 | 1356 | error = 1; |
76df1c65 | 1357 | goto out; |
1da177e4 LT |
1358 | } |
1359 | ||
615d1eb9 MS |
1360 | /* Check for conflicting reads */ |
1361 | __check_for_conflicting_io(s, pe->e.old_chunk); | |
a8d41b59 | 1362 | |
9d493fa8 AK |
1363 | /* |
1364 | * Add a proper exception, and remove the | |
1365 | * in-flight exception from the list. | |
1366 | */ | |
3510cb94 | 1367 | dm_insert_exception(&s->complete, e); |
76df1c65 | 1368 | |
1da177e4 | 1369 | out: |
3510cb94 | 1370 | dm_remove_exception(&pe->e); |
9d493fa8 | 1371 | snapshot_bios = bio_list_get(&pe->snapshot_bios); |
515ad66c MP |
1372 | origin_bios = bio_list_get(&pe->origin_bios); |
1373 | free_pending_exception(pe); | |
1da177e4 | 1374 | |
9d493fa8 AK |
1375 | up_write(&s->lock); |
1376 | ||
1377 | /* Submit any pending write bios */ | |
1378 | if (error) | |
1379 | error_bios(snapshot_bios); | |
1380 | else | |
1381 | flush_bios(snapshot_bios); | |
1382 | ||
515ad66c | 1383 | retry_origin_bios(s, origin_bios); |
1da177e4 LT |
1384 | } |
1385 | ||
1386 | static void commit_callback(void *context, int success) | |
1387 | { | |
028867ac AK |
1388 | struct dm_snap_pending_exception *pe = context; |
1389 | ||
1da177e4 LT |
1390 | pending_complete(pe, success); |
1391 | } | |
1392 | ||
1393 | /* | |
1394 | * Called when the copy I/O has finished. kcopyd actually runs | |
1395 | * this code so don't block. | |
1396 | */ | |
4cdc1d1f | 1397 | static void copy_callback(int read_err, unsigned long write_err, void *context) |
1da177e4 | 1398 | { |
028867ac | 1399 | struct dm_snap_pending_exception *pe = context; |
1da177e4 LT |
1400 | struct dm_snapshot *s = pe->snap; |
1401 | ||
1402 | if (read_err || write_err) | |
1403 | pending_complete(pe, 0); | |
1404 | ||
1405 | else | |
1406 | /* Update the metadata if we are persistent */ | |
493df71c JB |
1407 | s->store->type->commit_exception(s->store, &pe->e, |
1408 | commit_callback, pe); | |
1da177e4 LT |
1409 | } |
1410 | ||
1411 | /* | |
1412 | * Dispatches the copy operation to kcopyd. | |
1413 | */ | |
028867ac | 1414 | static void start_copy(struct dm_snap_pending_exception *pe) |
1da177e4 LT |
1415 | { |
1416 | struct dm_snapshot *s = pe->snap; | |
22a1ceb1 | 1417 | struct dm_io_region src, dest; |
1da177e4 LT |
1418 | struct block_device *bdev = s->origin->bdev; |
1419 | sector_t dev_size; | |
1420 | ||
1421 | dev_size = get_dev_size(bdev); | |
1422 | ||
1423 | src.bdev = bdev; | |
71fab00a | 1424 | src.sector = chunk_to_sector(s->store, pe->e.old_chunk); |
df96eee6 | 1425 | src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector); |
1da177e4 | 1426 | |
fc56f6fb | 1427 | dest.bdev = s->cow->bdev; |
71fab00a | 1428 | dest.sector = chunk_to_sector(s->store, pe->e.new_chunk); |
1da177e4 LT |
1429 | dest.count = src.count; |
1430 | ||
1431 | /* Hand over to kcopyd */ | |
eb69aca5 | 1432 | dm_kcopyd_copy(s->kcopyd_client, |
1da177e4 LT |
1433 | &src, 1, &dest, 0, copy_callback, pe); |
1434 | } | |
1435 | ||
2913808e MP |
1436 | static struct dm_snap_pending_exception * |
1437 | __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk) | |
1438 | { | |
3510cb94 | 1439 | struct dm_exception *e = dm_lookup_exception(&s->pending, chunk); |
2913808e MP |
1440 | |
1441 | if (!e) | |
1442 | return NULL; | |
1443 | ||
1444 | return container_of(e, struct dm_snap_pending_exception, e); | |
1445 | } | |
1446 | ||
1da177e4 LT |
1447 | /* |
1448 | * Looks to see if this snapshot already has a pending exception | |
1449 | * for this chunk, otherwise it allocates a new one and inserts | |
1450 | * it into the pending table. | |
1451 | * | |
1452 | * NOTE: a write lock must be held on snap->lock before calling | |
1453 | * this. | |
1454 | */ | |
028867ac | 1455 | static struct dm_snap_pending_exception * |
c6621392 MP |
1456 | __find_pending_exception(struct dm_snapshot *s, |
1457 | struct dm_snap_pending_exception *pe, chunk_t chunk) | |
1da177e4 | 1458 | { |
c6621392 | 1459 | struct dm_snap_pending_exception *pe2; |
1da177e4 | 1460 | |
2913808e MP |
1461 | pe2 = __lookup_pending_exception(s, chunk); |
1462 | if (pe2) { | |
76df1c65 | 1463 | free_pending_exception(pe); |
2913808e | 1464 | return pe2; |
1da177e4 LT |
1465 | } |
1466 | ||
76df1c65 AK |
1467 | pe->e.old_chunk = chunk; |
1468 | bio_list_init(&pe->origin_bios); | |
1469 | bio_list_init(&pe->snapshot_bios); | |
76df1c65 AK |
1470 | pe->started = 0; |
1471 | ||
493df71c | 1472 | if (s->store->type->prepare_exception(s->store, &pe->e)) { |
76df1c65 AK |
1473 | free_pending_exception(pe); |
1474 | return NULL; | |
1475 | } | |
1476 | ||
3510cb94 | 1477 | dm_insert_exception(&s->pending, &pe->e); |
76df1c65 | 1478 | |
1da177e4 LT |
1479 | return pe; |
1480 | } | |
1481 | ||
1d4989c8 | 1482 | static void remap_exception(struct dm_snapshot *s, struct dm_exception *e, |
d74f81f8 | 1483 | struct bio *bio, chunk_t chunk) |
1da177e4 | 1484 | { |
fc56f6fb | 1485 | bio->bi_bdev = s->cow->bdev; |
71fab00a JB |
1486 | bio->bi_sector = chunk_to_sector(s->store, |
1487 | dm_chunk_number(e->new_chunk) + | |
1488 | (chunk - e->old_chunk)) + | |
1489 | (bio->bi_sector & | |
1490 | s->store->chunk_mask); | |
1da177e4 LT |
1491 | } |
1492 | ||
1493 | static int snapshot_map(struct dm_target *ti, struct bio *bio, | |
1494 | union map_info *map_context) | |
1495 | { | |
1d4989c8 | 1496 | struct dm_exception *e; |
028867ac | 1497 | struct dm_snapshot *s = ti->private; |
d2a7ad29 | 1498 | int r = DM_MAPIO_REMAPPED; |
1da177e4 | 1499 | chunk_t chunk; |
028867ac | 1500 | struct dm_snap_pending_exception *pe = NULL; |
1da177e4 | 1501 | |
494b3ee7 | 1502 | if (unlikely(bio_empty_barrier(bio))) { |
fc56f6fb | 1503 | bio->bi_bdev = s->cow->bdev; |
494b3ee7 MP |
1504 | return DM_MAPIO_REMAPPED; |
1505 | } | |
1506 | ||
71fab00a | 1507 | chunk = sector_to_chunk(s->store, bio->bi_sector); |
1da177e4 LT |
1508 | |
1509 | /* Full snapshots are not usable */ | |
76df1c65 | 1510 | /* To get here the table must be live so s->active is always set. */ |
1da177e4 | 1511 | if (!s->valid) |
f6a80ea8 | 1512 | return -EIO; |
1da177e4 | 1513 | |
ba40a2aa AK |
1514 | /* FIXME: should only take write lock if we need |
1515 | * to copy an exception */ | |
1516 | down_write(&s->lock); | |
1517 | ||
1518 | if (!s->valid) { | |
1519 | r = -EIO; | |
1520 | goto out_unlock; | |
1521 | } | |
1522 | ||
1523 | /* If the block is already remapped - use that, else remap it */ | |
3510cb94 | 1524 | e = dm_lookup_exception(&s->complete, chunk); |
ba40a2aa | 1525 | if (e) { |
d74f81f8 | 1526 | remap_exception(s, e, bio, chunk); |
ba40a2aa AK |
1527 | goto out_unlock; |
1528 | } | |
1529 | ||
1da177e4 LT |
1530 | /* |
1531 | * Write to snapshot - higher level takes care of RW/RO | |
1532 | * flags so we should only get this if we are | |
1533 | * writeable. | |
1534 | */ | |
1535 | if (bio_rw(bio) == WRITE) { | |
2913808e | 1536 | pe = __lookup_pending_exception(s, chunk); |
76df1c65 | 1537 | if (!pe) { |
c6621392 MP |
1538 | up_write(&s->lock); |
1539 | pe = alloc_pending_exception(s); | |
1540 | down_write(&s->lock); | |
1541 | ||
1542 | if (!s->valid) { | |
1543 | free_pending_exception(pe); | |
1544 | r = -EIO; | |
1545 | goto out_unlock; | |
1546 | } | |
1547 | ||
3510cb94 | 1548 | e = dm_lookup_exception(&s->complete, chunk); |
35bf659b MP |
1549 | if (e) { |
1550 | free_pending_exception(pe); | |
1551 | remap_exception(s, e, bio, chunk); | |
1552 | goto out_unlock; | |
1553 | } | |
1554 | ||
c6621392 | 1555 | pe = __find_pending_exception(s, pe, chunk); |
2913808e MP |
1556 | if (!pe) { |
1557 | __invalidate_snapshot(s, -ENOMEM); | |
1558 | r = -EIO; | |
1559 | goto out_unlock; | |
1560 | } | |
1da177e4 LT |
1561 | } |
1562 | ||
d74f81f8 | 1563 | remap_exception(s, &pe->e, bio, chunk); |
76df1c65 AK |
1564 | bio_list_add(&pe->snapshot_bios, bio); |
1565 | ||
d2a7ad29 | 1566 | r = DM_MAPIO_SUBMITTED; |
ba40a2aa | 1567 | |
76df1c65 AK |
1568 | if (!pe->started) { |
1569 | /* this is protected by snap->lock */ | |
1570 | pe->started = 1; | |
ba40a2aa | 1571 | up_write(&s->lock); |
76df1c65 | 1572 | start_copy(pe); |
ba40a2aa AK |
1573 | goto out; |
1574 | } | |
cd45daff | 1575 | } else { |
ba40a2aa | 1576 | bio->bi_bdev = s->origin->bdev; |
cd45daff MP |
1577 | map_context->ptr = track_chunk(s, chunk); |
1578 | } | |
1da177e4 | 1579 | |
ba40a2aa AK |
1580 | out_unlock: |
1581 | up_write(&s->lock); | |
1582 | out: | |
1da177e4 LT |
1583 | return r; |
1584 | } | |
1585 | ||
3452c2a1 MP |
1586 | /* |
1587 | * A snapshot-merge target behaves like a combination of a snapshot | |
1588 | * target and a snapshot-origin target. It only generates new | |
1589 | * exceptions in other snapshots and not in the one that is being | |
1590 | * merged. | |
1591 | * | |
1592 | * For each chunk, if there is an existing exception, it is used to | |
1593 | * redirect I/O to the cow device. Otherwise I/O is sent to the origin, | |
1594 | * which in turn might generate exceptions in other snapshots. | |
9fe86254 MP |
1595 | * If merging is currently taking place on the chunk in question, the |
1596 | * I/O is deferred by adding it to s->bios_queued_during_merge. | |
3452c2a1 MP |
1597 | */ |
1598 | static int snapshot_merge_map(struct dm_target *ti, struct bio *bio, | |
1599 | union map_info *map_context) | |
1600 | { | |
1601 | struct dm_exception *e; | |
1602 | struct dm_snapshot *s = ti->private; | |
1603 | int r = DM_MAPIO_REMAPPED; | |
1604 | chunk_t chunk; | |
1605 | ||
10b8106a MS |
1606 | if (unlikely(bio_empty_barrier(bio))) { |
1607 | if (!map_context->flush_request) | |
1608 | bio->bi_bdev = s->origin->bdev; | |
1609 | else | |
1610 | bio->bi_bdev = s->cow->bdev; | |
1611 | map_context->ptr = NULL; | |
1612 | return DM_MAPIO_REMAPPED; | |
1613 | } | |
1614 | ||
3452c2a1 MP |
1615 | chunk = sector_to_chunk(s->store, bio->bi_sector); |
1616 | ||
9fe86254 | 1617 | down_write(&s->lock); |
3452c2a1 MP |
1618 | |
1619 | /* Full snapshots are not usable */ | |
1620 | if (!s->valid) { | |
1621 | r = -EIO; | |
1622 | goto out_unlock; | |
1623 | } | |
1624 | ||
1625 | /* If the block is already remapped - use that */ | |
1626 | e = dm_lookup_exception(&s->complete, chunk); | |
1627 | if (e) { | |
9fe86254 MP |
1628 | /* Queue writes overlapping with chunks being merged */ |
1629 | if (bio_rw(bio) == WRITE && | |
1630 | chunk >= s->first_merging_chunk && | |
1631 | chunk < (s->first_merging_chunk + | |
1632 | s->num_merging_chunks)) { | |
1633 | bio->bi_bdev = s->origin->bdev; | |
1634 | bio_list_add(&s->bios_queued_during_merge, bio); | |
1635 | r = DM_MAPIO_SUBMITTED; | |
1636 | goto out_unlock; | |
1637 | } | |
17aa0332 | 1638 | |
3452c2a1 | 1639 | remap_exception(s, e, bio, chunk); |
17aa0332 MP |
1640 | |
1641 | if (bio_rw(bio) == WRITE) | |
1642 | map_context->ptr = track_chunk(s, chunk); | |
3452c2a1 MP |
1643 | goto out_unlock; |
1644 | } | |
1645 | ||
1646 | bio->bi_bdev = s->origin->bdev; | |
1647 | ||
1648 | if (bio_rw(bio) == WRITE) { | |
9fe86254 | 1649 | up_write(&s->lock); |
3452c2a1 MP |
1650 | return do_origin(s->origin, bio); |
1651 | } | |
1652 | ||
1653 | out_unlock: | |
9fe86254 | 1654 | up_write(&s->lock); |
3452c2a1 MP |
1655 | |
1656 | return r; | |
1657 | } | |
1658 | ||
cd45daff MP |
1659 | static int snapshot_end_io(struct dm_target *ti, struct bio *bio, |
1660 | int error, union map_info *map_context) | |
1661 | { | |
1662 | struct dm_snapshot *s = ti->private; | |
1663 | struct dm_snap_tracked_chunk *c = map_context->ptr; | |
1664 | ||
1665 | if (c) | |
1666 | stop_tracking_chunk(s, c); | |
1667 | ||
1668 | return 0; | |
1669 | } | |
1670 | ||
1e03f97e MP |
1671 | static void snapshot_merge_presuspend(struct dm_target *ti) |
1672 | { | |
1673 | struct dm_snapshot *s = ti->private; | |
1674 | ||
1675 | stop_merge(s); | |
1676 | } | |
1677 | ||
c26655ca MS |
1678 | static void snapshot_postsuspend(struct dm_target *ti) |
1679 | { | |
1680 | struct dm_snapshot *s = ti->private; | |
1681 | ||
1682 | down_write(&s->lock); | |
1683 | s->suspended = 1; | |
1684 | up_write(&s->lock); | |
1685 | } | |
1686 | ||
c1f0c183 MS |
1687 | static int snapshot_preresume(struct dm_target *ti) |
1688 | { | |
1689 | int r = 0; | |
1690 | struct dm_snapshot *s = ti->private; | |
1691 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; | |
1692 | ||
1693 | down_read(&_origins_lock); | |
9d3b15c4 | 1694 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
c1f0c183 MS |
1695 | if (snap_src && snap_dest) { |
1696 | down_read(&snap_src->lock); | |
1697 | if (s == snap_src) { | |
1698 | DMERR("Unable to resume snapshot source until " | |
1699 | "handover completes."); | |
1700 | r = -EINVAL; | |
1701 | } else if (!snap_src->suspended) { | |
1702 | DMERR("Unable to perform snapshot handover until " | |
1703 | "source is suspended."); | |
1704 | r = -EINVAL; | |
1705 | } | |
1706 | up_read(&snap_src->lock); | |
1707 | } | |
1708 | up_read(&_origins_lock); | |
1709 | ||
1710 | return r; | |
1711 | } | |
1712 | ||
1da177e4 LT |
1713 | static void snapshot_resume(struct dm_target *ti) |
1714 | { | |
028867ac | 1715 | struct dm_snapshot *s = ti->private; |
c1f0c183 MS |
1716 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
1717 | ||
1718 | down_read(&_origins_lock); | |
9d3b15c4 | 1719 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
c1f0c183 MS |
1720 | if (snap_src && snap_dest) { |
1721 | down_write(&snap_src->lock); | |
1722 | down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING); | |
1723 | __handover_exceptions(snap_src, snap_dest); | |
1724 | up_write(&snap_dest->lock); | |
1725 | up_write(&snap_src->lock); | |
1726 | } | |
1727 | up_read(&_origins_lock); | |
1728 | ||
1729 | /* Now we have correct chunk size, reregister */ | |
1730 | reregister_snapshot(s); | |
1da177e4 | 1731 | |
aa14edeb AK |
1732 | down_write(&s->lock); |
1733 | s->active = 1; | |
c26655ca | 1734 | s->suspended = 0; |
aa14edeb | 1735 | up_write(&s->lock); |
1da177e4 LT |
1736 | } |
1737 | ||
1e03f97e MP |
1738 | static sector_t get_origin_minimum_chunksize(struct block_device *bdev) |
1739 | { | |
1740 | sector_t min_chunksize; | |
1741 | ||
1742 | down_read(&_origins_lock); | |
1743 | min_chunksize = __minimum_chunk_size(__lookup_origin(bdev)); | |
1744 | up_read(&_origins_lock); | |
1745 | ||
1746 | return min_chunksize; | |
1747 | } | |
1748 | ||
1749 | static void snapshot_merge_resume(struct dm_target *ti) | |
1750 | { | |
1751 | struct dm_snapshot *s = ti->private; | |
1752 | ||
1753 | /* | |
1754 | * Handover exceptions from existing snapshot. | |
1755 | */ | |
1756 | snapshot_resume(ti); | |
1757 | ||
1758 | /* | |
1759 | * snapshot-merge acts as an origin, so set ti->split_io | |
1760 | */ | |
1761 | ti->split_io = get_origin_minimum_chunksize(s->origin->bdev); | |
1762 | ||
1763 | start_merge(s); | |
1764 | } | |
1765 | ||
1da177e4 LT |
1766 | static int snapshot_status(struct dm_target *ti, status_type_t type, |
1767 | char *result, unsigned int maxlen) | |
1768 | { | |
2e4a31df | 1769 | unsigned sz = 0; |
028867ac | 1770 | struct dm_snapshot *snap = ti->private; |
1da177e4 LT |
1771 | |
1772 | switch (type) { | |
1773 | case STATUSTYPE_INFO: | |
94e76572 MP |
1774 | |
1775 | down_write(&snap->lock); | |
1776 | ||
1da177e4 | 1777 | if (!snap->valid) |
2e4a31df | 1778 | DMEMIT("Invalid"); |
1da177e4 | 1779 | else { |
985903bb MS |
1780 | if (snap->store->type->usage) { |
1781 | sector_t total_sectors, sectors_allocated, | |
1782 | metadata_sectors; | |
1783 | snap->store->type->usage(snap->store, | |
1784 | &total_sectors, | |
1785 | §ors_allocated, | |
1786 | &metadata_sectors); | |
1787 | DMEMIT("%llu/%llu %llu", | |
1788 | (unsigned long long)sectors_allocated, | |
1789 | (unsigned long long)total_sectors, | |
1790 | (unsigned long long)metadata_sectors); | |
1da177e4 LT |
1791 | } |
1792 | else | |
2e4a31df | 1793 | DMEMIT("Unknown"); |
1da177e4 | 1794 | } |
94e76572 MP |
1795 | |
1796 | up_write(&snap->lock); | |
1797 | ||
1da177e4 LT |
1798 | break; |
1799 | ||
1800 | case STATUSTYPE_TABLE: | |
1801 | /* | |
1802 | * kdevname returns a static pointer so we need | |
1803 | * to make private copies if the output is to | |
1804 | * make sense. | |
1805 | */ | |
fc56f6fb | 1806 | DMEMIT("%s %s", snap->origin->name, snap->cow->name); |
1e302a92 JB |
1807 | snap->store->type->status(snap->store, type, result + sz, |
1808 | maxlen - sz); | |
1da177e4 LT |
1809 | break; |
1810 | } | |
1811 | ||
1812 | return 0; | |
1813 | } | |
1814 | ||
8811f46c MS |
1815 | static int snapshot_iterate_devices(struct dm_target *ti, |
1816 | iterate_devices_callout_fn fn, void *data) | |
1817 | { | |
1818 | struct dm_snapshot *snap = ti->private; | |
1819 | ||
1820 | return fn(ti, snap->origin, 0, ti->len, data); | |
1821 | } | |
1822 | ||
1823 | ||
1da177e4 LT |
1824 | /*----------------------------------------------------------------- |
1825 | * Origin methods | |
1826 | *---------------------------------------------------------------*/ | |
9eaae8ff MP |
1827 | |
1828 | /* | |
1829 | * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any | |
1830 | * supplied bio was ignored. The caller may submit it immediately. | |
1831 | * (No remapping actually occurs as the origin is always a direct linear | |
1832 | * map.) | |
1833 | * | |
1834 | * If further exceptions are required, DM_MAPIO_SUBMITTED is returned | |
1835 | * and any supplied bio is added to a list to be submitted once all | |
1836 | * the necessary exceptions exist. | |
1837 | */ | |
1838 | static int __origin_write(struct list_head *snapshots, sector_t sector, | |
1839 | struct bio *bio) | |
1da177e4 | 1840 | { |
515ad66c | 1841 | int r = DM_MAPIO_REMAPPED; |
1da177e4 | 1842 | struct dm_snapshot *snap; |
1d4989c8 | 1843 | struct dm_exception *e; |
515ad66c MP |
1844 | struct dm_snap_pending_exception *pe; |
1845 | struct dm_snap_pending_exception *pe_to_start_now = NULL; | |
1846 | struct dm_snap_pending_exception *pe_to_start_last = NULL; | |
1da177e4 LT |
1847 | chunk_t chunk; |
1848 | ||
1849 | /* Do all the snapshots on this origin */ | |
1850 | list_for_each_entry (snap, snapshots, list) { | |
3452c2a1 MP |
1851 | /* |
1852 | * Don't make new exceptions in a merging snapshot | |
1853 | * because it has effectively been deleted | |
1854 | */ | |
1855 | if (dm_target_is_snapshot_merge(snap->ti)) | |
1856 | continue; | |
1857 | ||
76df1c65 AK |
1858 | down_write(&snap->lock); |
1859 | ||
aa14edeb AK |
1860 | /* Only deal with valid and active snapshots */ |
1861 | if (!snap->valid || !snap->active) | |
76df1c65 | 1862 | goto next_snapshot; |
1da177e4 | 1863 | |
d5e404c1 | 1864 | /* Nothing to do if writing beyond end of snapshot */ |
9eaae8ff | 1865 | if (sector >= dm_table_get_size(snap->ti->table)) |
76df1c65 | 1866 | goto next_snapshot; |
1da177e4 LT |
1867 | |
1868 | /* | |
1869 | * Remember, different snapshots can have | |
1870 | * different chunk sizes. | |
1871 | */ | |
9eaae8ff | 1872 | chunk = sector_to_chunk(snap->store, sector); |
1da177e4 LT |
1873 | |
1874 | /* | |
1875 | * Check exception table to see if block | |
1876 | * is already remapped in this snapshot | |
1877 | * and trigger an exception if not. | |
1878 | */ | |
3510cb94 | 1879 | e = dm_lookup_exception(&snap->complete, chunk); |
76df1c65 AK |
1880 | if (e) |
1881 | goto next_snapshot; | |
1882 | ||
2913808e | 1883 | pe = __lookup_pending_exception(snap, chunk); |
76df1c65 | 1884 | if (!pe) { |
c6621392 MP |
1885 | up_write(&snap->lock); |
1886 | pe = alloc_pending_exception(snap); | |
1887 | down_write(&snap->lock); | |
1888 | ||
1889 | if (!snap->valid) { | |
1890 | free_pending_exception(pe); | |
1891 | goto next_snapshot; | |
1892 | } | |
1893 | ||
3510cb94 | 1894 | e = dm_lookup_exception(&snap->complete, chunk); |
35bf659b MP |
1895 | if (e) { |
1896 | free_pending_exception(pe); | |
1897 | goto next_snapshot; | |
1898 | } | |
1899 | ||
c6621392 | 1900 | pe = __find_pending_exception(snap, pe, chunk); |
2913808e MP |
1901 | if (!pe) { |
1902 | __invalidate_snapshot(snap, -ENOMEM); | |
1903 | goto next_snapshot; | |
1904 | } | |
76df1c65 AK |
1905 | } |
1906 | ||
515ad66c | 1907 | r = DM_MAPIO_SUBMITTED; |
76df1c65 | 1908 | |
515ad66c MP |
1909 | /* |
1910 | * If an origin bio was supplied, queue it to wait for the | |
1911 | * completion of this exception, and start this one last, | |
1912 | * at the end of the function. | |
1913 | */ | |
1914 | if (bio) { | |
1915 | bio_list_add(&pe->origin_bios, bio); | |
1916 | bio = NULL; | |
76df1c65 | 1917 | |
515ad66c MP |
1918 | if (!pe->started) { |
1919 | pe->started = 1; | |
1920 | pe_to_start_last = pe; | |
1921 | } | |
76df1c65 AK |
1922 | } |
1923 | ||
1924 | if (!pe->started) { | |
1925 | pe->started = 1; | |
515ad66c | 1926 | pe_to_start_now = pe; |
1da177e4 LT |
1927 | } |
1928 | ||
76df1c65 | 1929 | next_snapshot: |
1da177e4 | 1930 | up_write(&snap->lock); |
1da177e4 | 1931 | |
515ad66c MP |
1932 | if (pe_to_start_now) { |
1933 | start_copy(pe_to_start_now); | |
1934 | pe_to_start_now = NULL; | |
1935 | } | |
b4b610f6 AK |
1936 | } |
1937 | ||
1da177e4 | 1938 | /* |
515ad66c MP |
1939 | * Submit the exception against which the bio is queued last, |
1940 | * to give the other exceptions a head start. | |
1da177e4 | 1941 | */ |
515ad66c MP |
1942 | if (pe_to_start_last) |
1943 | start_copy(pe_to_start_last); | |
1da177e4 LT |
1944 | |
1945 | return r; | |
1946 | } | |
1947 | ||
1948 | /* | |
1949 | * Called on a write from the origin driver. | |
1950 | */ | |
1951 | static int do_origin(struct dm_dev *origin, struct bio *bio) | |
1952 | { | |
1953 | struct origin *o; | |
d2a7ad29 | 1954 | int r = DM_MAPIO_REMAPPED; |
1da177e4 LT |
1955 | |
1956 | down_read(&_origins_lock); | |
1957 | o = __lookup_origin(origin->bdev); | |
1958 | if (o) | |
9eaae8ff | 1959 | r = __origin_write(&o->snapshots, bio->bi_sector, bio); |
1da177e4 LT |
1960 | up_read(&_origins_lock); |
1961 | ||
1962 | return r; | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * Origin: maps a linear range of a device, with hooks for snapshotting. | |
1967 | */ | |
1968 | ||
1969 | /* | |
1970 | * Construct an origin mapping: <dev_path> | |
1971 | * The context for an origin is merely a 'struct dm_dev *' | |
1972 | * pointing to the real device. | |
1973 | */ | |
1974 | static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
1975 | { | |
1976 | int r; | |
1977 | struct dm_dev *dev; | |
1978 | ||
1979 | if (argc != 1) { | |
72d94861 | 1980 | ti->error = "origin: incorrect number of arguments"; |
1da177e4 LT |
1981 | return -EINVAL; |
1982 | } | |
1983 | ||
1984 | r = dm_get_device(ti, argv[0], 0, ti->len, | |
1985 | dm_table_get_mode(ti->table), &dev); | |
1986 | if (r) { | |
1987 | ti->error = "Cannot get target device"; | |
1988 | return r; | |
1989 | } | |
1990 | ||
1991 | ti->private = dev; | |
494b3ee7 MP |
1992 | ti->num_flush_requests = 1; |
1993 | ||
1da177e4 LT |
1994 | return 0; |
1995 | } | |
1996 | ||
1997 | static void origin_dtr(struct dm_target *ti) | |
1998 | { | |
028867ac | 1999 | struct dm_dev *dev = ti->private; |
1da177e4 LT |
2000 | dm_put_device(ti, dev); |
2001 | } | |
2002 | ||
2003 | static int origin_map(struct dm_target *ti, struct bio *bio, | |
2004 | union map_info *map_context) | |
2005 | { | |
028867ac | 2006 | struct dm_dev *dev = ti->private; |
1da177e4 LT |
2007 | bio->bi_bdev = dev->bdev; |
2008 | ||
494b3ee7 MP |
2009 | if (unlikely(bio_empty_barrier(bio))) |
2010 | return DM_MAPIO_REMAPPED; | |
2011 | ||
1da177e4 | 2012 | /* Only tell snapshots if this is a write */ |
d2a7ad29 | 2013 | return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED; |
1da177e4 LT |
2014 | } |
2015 | ||
1da177e4 LT |
2016 | /* |
2017 | * Set the target "split_io" field to the minimum of all the snapshots' | |
2018 | * chunk sizes. | |
2019 | */ | |
2020 | static void origin_resume(struct dm_target *ti) | |
2021 | { | |
028867ac | 2022 | struct dm_dev *dev = ti->private; |
1da177e4 | 2023 | |
1e03f97e | 2024 | ti->split_io = get_origin_minimum_chunksize(dev->bdev); |
1da177e4 LT |
2025 | } |
2026 | ||
2027 | static int origin_status(struct dm_target *ti, status_type_t type, char *result, | |
2028 | unsigned int maxlen) | |
2029 | { | |
028867ac | 2030 | struct dm_dev *dev = ti->private; |
1da177e4 LT |
2031 | |
2032 | switch (type) { | |
2033 | case STATUSTYPE_INFO: | |
2034 | result[0] = '\0'; | |
2035 | break; | |
2036 | ||
2037 | case STATUSTYPE_TABLE: | |
2038 | snprintf(result, maxlen, "%s", dev->name); | |
2039 | break; | |
2040 | } | |
2041 | ||
2042 | return 0; | |
2043 | } | |
2044 | ||
8811f46c MS |
2045 | static int origin_iterate_devices(struct dm_target *ti, |
2046 | iterate_devices_callout_fn fn, void *data) | |
2047 | { | |
2048 | struct dm_dev *dev = ti->private; | |
2049 | ||
2050 | return fn(ti, dev, 0, ti->len, data); | |
2051 | } | |
2052 | ||
1da177e4 LT |
2053 | static struct target_type origin_target = { |
2054 | .name = "snapshot-origin", | |
8811f46c | 2055 | .version = {1, 7, 0}, |
1da177e4 LT |
2056 | .module = THIS_MODULE, |
2057 | .ctr = origin_ctr, | |
2058 | .dtr = origin_dtr, | |
2059 | .map = origin_map, | |
2060 | .resume = origin_resume, | |
2061 | .status = origin_status, | |
8811f46c | 2062 | .iterate_devices = origin_iterate_devices, |
1da177e4 LT |
2063 | }; |
2064 | ||
2065 | static struct target_type snapshot_target = { | |
2066 | .name = "snapshot", | |
c26655ca | 2067 | .version = {1, 9, 0}, |
1da177e4 LT |
2068 | .module = THIS_MODULE, |
2069 | .ctr = snapshot_ctr, | |
2070 | .dtr = snapshot_dtr, | |
2071 | .map = snapshot_map, | |
cd45daff | 2072 | .end_io = snapshot_end_io, |
c26655ca | 2073 | .postsuspend = snapshot_postsuspend, |
c1f0c183 | 2074 | .preresume = snapshot_preresume, |
1da177e4 LT |
2075 | .resume = snapshot_resume, |
2076 | .status = snapshot_status, | |
8811f46c | 2077 | .iterate_devices = snapshot_iterate_devices, |
1da177e4 LT |
2078 | }; |
2079 | ||
d698aa45 MP |
2080 | static struct target_type merge_target = { |
2081 | .name = dm_snapshot_merge_target_name, | |
2082 | .version = {1, 0, 0}, | |
2083 | .module = THIS_MODULE, | |
2084 | .ctr = snapshot_ctr, | |
2085 | .dtr = snapshot_dtr, | |
3452c2a1 | 2086 | .map = snapshot_merge_map, |
d698aa45 | 2087 | .end_io = snapshot_end_io, |
1e03f97e | 2088 | .presuspend = snapshot_merge_presuspend, |
d698aa45 MP |
2089 | .postsuspend = snapshot_postsuspend, |
2090 | .preresume = snapshot_preresume, | |
1e03f97e | 2091 | .resume = snapshot_merge_resume, |
d698aa45 MP |
2092 | .status = snapshot_status, |
2093 | .iterate_devices = snapshot_iterate_devices, | |
2094 | }; | |
2095 | ||
1da177e4 LT |
2096 | static int __init dm_snapshot_init(void) |
2097 | { | |
2098 | int r; | |
2099 | ||
4db6bfe0 AK |
2100 | r = dm_exception_store_init(); |
2101 | if (r) { | |
2102 | DMERR("Failed to initialize exception stores"); | |
2103 | return r; | |
2104 | } | |
2105 | ||
1da177e4 | 2106 | r = dm_register_target(&snapshot_target); |
d698aa45 | 2107 | if (r < 0) { |
1da177e4 | 2108 | DMERR("snapshot target register failed %d", r); |
034a186d | 2109 | goto bad_register_snapshot_target; |
1da177e4 LT |
2110 | } |
2111 | ||
2112 | r = dm_register_target(&origin_target); | |
2113 | if (r < 0) { | |
72d94861 | 2114 | DMERR("Origin target register failed %d", r); |
d698aa45 MP |
2115 | goto bad_register_origin_target; |
2116 | } | |
2117 | ||
2118 | r = dm_register_target(&merge_target); | |
2119 | if (r < 0) { | |
2120 | DMERR("Merge target register failed %d", r); | |
2121 | goto bad_register_merge_target; | |
1da177e4 LT |
2122 | } |
2123 | ||
2124 | r = init_origin_hash(); | |
2125 | if (r) { | |
2126 | DMERR("init_origin_hash failed."); | |
d698aa45 | 2127 | goto bad_origin_hash; |
1da177e4 LT |
2128 | } |
2129 | ||
1d4989c8 | 2130 | exception_cache = KMEM_CACHE(dm_exception, 0); |
1da177e4 LT |
2131 | if (!exception_cache) { |
2132 | DMERR("Couldn't create exception cache."); | |
2133 | r = -ENOMEM; | |
d698aa45 | 2134 | goto bad_exception_cache; |
1da177e4 LT |
2135 | } |
2136 | ||
028867ac | 2137 | pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0); |
1da177e4 LT |
2138 | if (!pending_cache) { |
2139 | DMERR("Couldn't create pending cache."); | |
2140 | r = -ENOMEM; | |
d698aa45 | 2141 | goto bad_pending_cache; |
1da177e4 LT |
2142 | } |
2143 | ||
cd45daff MP |
2144 | tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0); |
2145 | if (!tracked_chunk_cache) { | |
2146 | DMERR("Couldn't create cache to track chunks in use."); | |
2147 | r = -ENOMEM; | |
d698aa45 | 2148 | goto bad_tracked_chunk_cache; |
cd45daff MP |
2149 | } |
2150 | ||
ca3a931f AK |
2151 | ksnapd = create_singlethread_workqueue("ksnapd"); |
2152 | if (!ksnapd) { | |
2153 | DMERR("Failed to create ksnapd workqueue."); | |
2154 | r = -ENOMEM; | |
92e86812 | 2155 | goto bad_pending_pool; |
ca3a931f AK |
2156 | } |
2157 | ||
1da177e4 LT |
2158 | return 0; |
2159 | ||
4db6bfe0 | 2160 | bad_pending_pool: |
cd45daff | 2161 | kmem_cache_destroy(tracked_chunk_cache); |
d698aa45 | 2162 | bad_tracked_chunk_cache: |
1da177e4 | 2163 | kmem_cache_destroy(pending_cache); |
d698aa45 | 2164 | bad_pending_cache: |
1da177e4 | 2165 | kmem_cache_destroy(exception_cache); |
d698aa45 | 2166 | bad_exception_cache: |
1da177e4 | 2167 | exit_origin_hash(); |
d698aa45 MP |
2168 | bad_origin_hash: |
2169 | dm_unregister_target(&merge_target); | |
2170 | bad_register_merge_target: | |
1da177e4 | 2171 | dm_unregister_target(&origin_target); |
d698aa45 | 2172 | bad_register_origin_target: |
1da177e4 | 2173 | dm_unregister_target(&snapshot_target); |
034a186d JB |
2174 | bad_register_snapshot_target: |
2175 | dm_exception_store_exit(); | |
d698aa45 | 2176 | |
1da177e4 LT |
2177 | return r; |
2178 | } | |
2179 | ||
2180 | static void __exit dm_snapshot_exit(void) | |
2181 | { | |
ca3a931f AK |
2182 | destroy_workqueue(ksnapd); |
2183 | ||
10d3bd09 MP |
2184 | dm_unregister_target(&snapshot_target); |
2185 | dm_unregister_target(&origin_target); | |
d698aa45 | 2186 | dm_unregister_target(&merge_target); |
1da177e4 LT |
2187 | |
2188 | exit_origin_hash(); | |
1da177e4 LT |
2189 | kmem_cache_destroy(pending_cache); |
2190 | kmem_cache_destroy(exception_cache); | |
cd45daff | 2191 | kmem_cache_destroy(tracked_chunk_cache); |
4db6bfe0 AK |
2192 | |
2193 | dm_exception_store_exit(); | |
1da177e4 LT |
2194 | } |
2195 | ||
2196 | /* Module hooks */ | |
2197 | module_init(dm_snapshot_init); | |
2198 | module_exit(dm_snapshot_exit); | |
2199 | ||
2200 | MODULE_DESCRIPTION(DM_NAME " snapshot target"); | |
2201 | MODULE_AUTHOR("Joe Thornber"); | |
2202 | MODULE_LICENSE("GPL"); |