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