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