]>
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> | |
10 | #include <linux/config.h> | |
11 | #include <linux/ctype.h> | |
12 | #include <linux/device-mapper.h> | |
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> | |
21 | ||
22 | #include "dm-snap.h" | |
23 | #include "dm-bio-list.h" | |
24 | #include "kcopyd.h" | |
25 | ||
26 | /* | |
27 | * The percentage increment we will wake up users at | |
28 | */ | |
29 | #define WAKE_UP_PERCENT 5 | |
30 | ||
31 | /* | |
32 | * kcopyd priority of snapshot operations | |
33 | */ | |
34 | #define SNAPSHOT_COPY_PRIORITY 2 | |
35 | ||
36 | /* | |
37 | * Each snapshot reserves this many pages for io | |
38 | */ | |
39 | #define SNAPSHOT_PAGES 256 | |
40 | ||
41 | struct pending_exception { | |
42 | struct exception e; | |
43 | ||
44 | /* | |
45 | * Origin buffers waiting for this to complete are held | |
46 | * in a bio list | |
47 | */ | |
48 | struct bio_list origin_bios; | |
49 | struct bio_list snapshot_bios; | |
50 | ||
eccf0817 AK |
51 | /* |
52 | * Short-term queue of pending exceptions prior to submission. | |
53 | */ | |
54 | struct list_head list; | |
55 | ||
1da177e4 | 56 | /* |
b4b610f6 AK |
57 | * The primary pending_exception is the one that holds |
58 | * the sibling_count and the list of origin_bios for a | |
59 | * group of pending_exceptions. It is always last to get freed. | |
60 | * These fields get set up when writing to the origin. | |
1da177e4 | 61 | */ |
b4b610f6 AK |
62 | struct pending_exception *primary_pe; |
63 | ||
64 | /* | |
65 | * Number of pending_exceptions processing this chunk. | |
66 | * When this drops to zero we must complete the origin bios. | |
67 | * If incrementing or decrementing this, hold pe->snap->lock for | |
68 | * the sibling concerned and not pe->primary_pe->snap->lock unless | |
69 | * they are the same. | |
70 | */ | |
71 | atomic_t sibling_count; | |
1da177e4 LT |
72 | |
73 | /* Pointer back to snapshot context */ | |
74 | struct dm_snapshot *snap; | |
75 | ||
76 | /* | |
77 | * 1 indicates the exception has already been sent to | |
78 | * kcopyd. | |
79 | */ | |
80 | int started; | |
81 | }; | |
82 | ||
83 | /* | |
84 | * Hash table mapping origin volumes to lists of snapshots and | |
85 | * a lock to protect it | |
86 | */ | |
87 | static kmem_cache_t *exception_cache; | |
88 | static kmem_cache_t *pending_cache; | |
89 | static mempool_t *pending_pool; | |
90 | ||
91 | /* | |
92 | * One of these per registered origin, held in the snapshot_origins hash | |
93 | */ | |
94 | struct origin { | |
95 | /* The origin device */ | |
96 | struct block_device *bdev; | |
97 | ||
98 | struct list_head hash_list; | |
99 | ||
100 | /* List of snapshots for this origin */ | |
101 | struct list_head snapshots; | |
102 | }; | |
103 | ||
104 | /* | |
105 | * Size of the hash table for origin volumes. If we make this | |
106 | * the size of the minors list then it should be nearly perfect | |
107 | */ | |
108 | #define ORIGIN_HASH_SIZE 256 | |
109 | #define ORIGIN_MASK 0xFF | |
110 | static struct list_head *_origins; | |
111 | static struct rw_semaphore _origins_lock; | |
112 | ||
113 | static int init_origin_hash(void) | |
114 | { | |
115 | int i; | |
116 | ||
117 | _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head), | |
118 | GFP_KERNEL); | |
119 | if (!_origins) { | |
120 | DMERR("Device mapper: Snapshot: unable to allocate memory"); | |
121 | return -ENOMEM; | |
122 | } | |
123 | ||
124 | for (i = 0; i < ORIGIN_HASH_SIZE; i++) | |
125 | INIT_LIST_HEAD(_origins + i); | |
126 | init_rwsem(&_origins_lock); | |
127 | ||
128 | return 0; | |
129 | } | |
130 | ||
131 | static void exit_origin_hash(void) | |
132 | { | |
133 | kfree(_origins); | |
134 | } | |
135 | ||
136 | static inline unsigned int origin_hash(struct block_device *bdev) | |
137 | { | |
138 | return bdev->bd_dev & ORIGIN_MASK; | |
139 | } | |
140 | ||
141 | static struct origin *__lookup_origin(struct block_device *origin) | |
142 | { | |
143 | struct list_head *ol; | |
144 | struct origin *o; | |
145 | ||
146 | ol = &_origins[origin_hash(origin)]; | |
147 | list_for_each_entry (o, ol, hash_list) | |
148 | if (bdev_equal(o->bdev, origin)) | |
149 | return o; | |
150 | ||
151 | return NULL; | |
152 | } | |
153 | ||
154 | static void __insert_origin(struct origin *o) | |
155 | { | |
156 | struct list_head *sl = &_origins[origin_hash(o->bdev)]; | |
157 | list_add_tail(&o->hash_list, sl); | |
158 | } | |
159 | ||
160 | /* | |
161 | * Make a note of the snapshot and its origin so we can look it | |
162 | * up when the origin has a write on it. | |
163 | */ | |
164 | static int register_snapshot(struct dm_snapshot *snap) | |
165 | { | |
166 | struct origin *o; | |
167 | struct block_device *bdev = snap->origin->bdev; | |
168 | ||
169 | down_write(&_origins_lock); | |
170 | o = __lookup_origin(bdev); | |
171 | ||
172 | if (!o) { | |
173 | /* New origin */ | |
174 | o = kmalloc(sizeof(*o), GFP_KERNEL); | |
175 | if (!o) { | |
176 | up_write(&_origins_lock); | |
177 | return -ENOMEM; | |
178 | } | |
179 | ||
180 | /* Initialise the struct */ | |
181 | INIT_LIST_HEAD(&o->snapshots); | |
182 | o->bdev = bdev; | |
183 | ||
184 | __insert_origin(o); | |
185 | } | |
186 | ||
187 | list_add_tail(&snap->list, &o->snapshots); | |
188 | ||
189 | up_write(&_origins_lock); | |
190 | return 0; | |
191 | } | |
192 | ||
193 | static void unregister_snapshot(struct dm_snapshot *s) | |
194 | { | |
195 | struct origin *o; | |
196 | ||
197 | down_write(&_origins_lock); | |
198 | o = __lookup_origin(s->origin->bdev); | |
199 | ||
200 | list_del(&s->list); | |
201 | if (list_empty(&o->snapshots)) { | |
202 | list_del(&o->hash_list); | |
203 | kfree(o); | |
204 | } | |
205 | ||
206 | up_write(&_origins_lock); | |
207 | } | |
208 | ||
209 | /* | |
210 | * Implementation of the exception hash tables. | |
211 | */ | |
212 | static int init_exception_table(struct exception_table *et, uint32_t size) | |
213 | { | |
214 | unsigned int i; | |
215 | ||
216 | et->hash_mask = size - 1; | |
217 | et->table = dm_vcalloc(size, sizeof(struct list_head)); | |
218 | if (!et->table) | |
219 | return -ENOMEM; | |
220 | ||
221 | for (i = 0; i < size; i++) | |
222 | INIT_LIST_HEAD(et->table + i); | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
227 | static void exit_exception_table(struct exception_table *et, kmem_cache_t *mem) | |
228 | { | |
229 | struct list_head *slot; | |
230 | struct exception *ex, *next; | |
231 | int i, size; | |
232 | ||
233 | size = et->hash_mask + 1; | |
234 | for (i = 0; i < size; i++) { | |
235 | slot = et->table + i; | |
236 | ||
237 | list_for_each_entry_safe (ex, next, slot, hash_list) | |
238 | kmem_cache_free(mem, ex); | |
239 | } | |
240 | ||
241 | vfree(et->table); | |
242 | } | |
243 | ||
244 | static inline uint32_t exception_hash(struct exception_table *et, chunk_t chunk) | |
245 | { | |
246 | return chunk & et->hash_mask; | |
247 | } | |
248 | ||
249 | static void insert_exception(struct exception_table *eh, struct exception *e) | |
250 | { | |
251 | struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)]; | |
252 | list_add(&e->hash_list, l); | |
253 | } | |
254 | ||
255 | static inline void remove_exception(struct exception *e) | |
256 | { | |
257 | list_del(&e->hash_list); | |
258 | } | |
259 | ||
260 | /* | |
261 | * Return the exception data for a sector, or NULL if not | |
262 | * remapped. | |
263 | */ | |
264 | static struct exception *lookup_exception(struct exception_table *et, | |
265 | chunk_t chunk) | |
266 | { | |
267 | struct list_head *slot; | |
268 | struct exception *e; | |
269 | ||
270 | slot = &et->table[exception_hash(et, chunk)]; | |
271 | list_for_each_entry (e, slot, hash_list) | |
272 | if (e->old_chunk == chunk) | |
273 | return e; | |
274 | ||
275 | return NULL; | |
276 | } | |
277 | ||
278 | static inline struct exception *alloc_exception(void) | |
279 | { | |
280 | struct exception *e; | |
281 | ||
282 | e = kmem_cache_alloc(exception_cache, GFP_NOIO); | |
283 | if (!e) | |
284 | e = kmem_cache_alloc(exception_cache, GFP_ATOMIC); | |
285 | ||
286 | return e; | |
287 | } | |
288 | ||
289 | static inline void free_exception(struct exception *e) | |
290 | { | |
291 | kmem_cache_free(exception_cache, e); | |
292 | } | |
293 | ||
294 | static inline struct pending_exception *alloc_pending_exception(void) | |
295 | { | |
296 | return mempool_alloc(pending_pool, GFP_NOIO); | |
297 | } | |
298 | ||
299 | static inline void free_pending_exception(struct pending_exception *pe) | |
300 | { | |
301 | mempool_free(pe, pending_pool); | |
302 | } | |
303 | ||
304 | int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new) | |
305 | { | |
306 | struct exception *e; | |
307 | ||
308 | e = alloc_exception(); | |
309 | if (!e) | |
310 | return -ENOMEM; | |
311 | ||
312 | e->old_chunk = old; | |
313 | e->new_chunk = new; | |
314 | insert_exception(&s->complete, e); | |
315 | return 0; | |
316 | } | |
317 | ||
318 | /* | |
319 | * Hard coded magic. | |
320 | */ | |
321 | static int calc_max_buckets(void) | |
322 | { | |
323 | /* use a fixed size of 2MB */ | |
324 | unsigned long mem = 2 * 1024 * 1024; | |
325 | mem /= sizeof(struct list_head); | |
326 | ||
327 | return mem; | |
328 | } | |
329 | ||
330 | /* | |
331 | * Rounds a number down to a power of 2. | |
332 | */ | |
333 | static inline uint32_t round_down(uint32_t n) | |
334 | { | |
335 | while (n & (n - 1)) | |
336 | n &= (n - 1); | |
337 | return n; | |
338 | } | |
339 | ||
340 | /* | |
341 | * Allocate room for a suitable hash table. | |
342 | */ | |
343 | static int init_hash_tables(struct dm_snapshot *s) | |
344 | { | |
345 | sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets; | |
346 | ||
347 | /* | |
348 | * Calculate based on the size of the original volume or | |
349 | * the COW volume... | |
350 | */ | |
351 | cow_dev_size = get_dev_size(s->cow->bdev); | |
352 | origin_dev_size = get_dev_size(s->origin->bdev); | |
353 | max_buckets = calc_max_buckets(); | |
354 | ||
355 | hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift; | |
356 | hash_size = min(hash_size, max_buckets); | |
357 | ||
358 | /* Round it down to a power of 2 */ | |
359 | hash_size = round_down(hash_size); | |
360 | if (init_exception_table(&s->complete, hash_size)) | |
361 | return -ENOMEM; | |
362 | ||
363 | /* | |
364 | * Allocate hash table for in-flight exceptions | |
365 | * Make this smaller than the real hash table | |
366 | */ | |
367 | hash_size >>= 3; | |
368 | if (hash_size < 64) | |
369 | hash_size = 64; | |
370 | ||
371 | if (init_exception_table(&s->pending, hash_size)) { | |
372 | exit_exception_table(&s->complete, exception_cache); | |
373 | return -ENOMEM; | |
374 | } | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
379 | /* | |
380 | * Round a number up to the nearest 'size' boundary. size must | |
381 | * be a power of 2. | |
382 | */ | |
383 | static inline ulong round_up(ulong n, ulong size) | |
384 | { | |
385 | size--; | |
386 | return (n + size) & ~size; | |
387 | } | |
388 | ||
2d38fe20 AK |
389 | static void read_snapshot_metadata(struct dm_snapshot *s) |
390 | { | |
2d38fe20 AK |
391 | if (s->store.read_metadata(&s->store)) { |
392 | down_write(&s->lock); | |
393 | s->valid = 0; | |
394 | up_write(&s->lock); | |
395 | } | |
2d38fe20 AK |
396 | } |
397 | ||
1da177e4 LT |
398 | /* |
399 | * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size> | |
400 | */ | |
401 | static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
402 | { | |
403 | struct dm_snapshot *s; | |
404 | unsigned long chunk_size; | |
405 | int r = -EINVAL; | |
406 | char persistent; | |
407 | char *origin_path; | |
408 | char *cow_path; | |
409 | char *value; | |
410 | int blocksize; | |
411 | ||
412 | if (argc < 4) { | |
413 | ti->error = "dm-snapshot: requires exactly 4 arguments"; | |
414 | r = -EINVAL; | |
415 | goto bad1; | |
416 | } | |
417 | ||
418 | origin_path = argv[0]; | |
419 | cow_path = argv[1]; | |
420 | persistent = toupper(*argv[2]); | |
421 | ||
422 | if (persistent != 'P' && persistent != 'N') { | |
423 | ti->error = "Persistent flag is not P or N"; | |
424 | r = -EINVAL; | |
425 | goto bad1; | |
426 | } | |
427 | ||
428 | chunk_size = simple_strtoul(argv[3], &value, 10); | |
429 | if (chunk_size == 0 || value == NULL) { | |
430 | ti->error = "Invalid chunk size"; | |
431 | r = -EINVAL; | |
432 | goto bad1; | |
433 | } | |
434 | ||
435 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
436 | if (s == NULL) { | |
437 | ti->error = "Cannot allocate snapshot context private " | |
438 | "structure"; | |
439 | r = -ENOMEM; | |
440 | goto bad1; | |
441 | } | |
442 | ||
443 | r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin); | |
444 | if (r) { | |
445 | ti->error = "Cannot get origin device"; | |
446 | goto bad2; | |
447 | } | |
448 | ||
449 | r = dm_get_device(ti, cow_path, 0, 0, | |
450 | FMODE_READ | FMODE_WRITE, &s->cow); | |
451 | if (r) { | |
452 | dm_put_device(ti, s->origin); | |
453 | ti->error = "Cannot get COW device"; | |
454 | goto bad2; | |
455 | } | |
456 | ||
457 | /* | |
458 | * Chunk size must be multiple of page size. Silently | |
459 | * round up if it's not. | |
460 | */ | |
461 | chunk_size = round_up(chunk_size, PAGE_SIZE >> 9); | |
462 | ||
463 | /* Validate the chunk size against the device block size */ | |
464 | blocksize = s->cow->bdev->bd_disk->queue->hardsect_size; | |
465 | if (chunk_size % (blocksize >> 9)) { | |
466 | ti->error = "Chunk size is not a multiple of device blocksize"; | |
467 | r = -EINVAL; | |
468 | goto bad3; | |
469 | } | |
470 | ||
471 | /* Check chunk_size is a power of 2 */ | |
472 | if (chunk_size & (chunk_size - 1)) { | |
473 | ti->error = "Chunk size is not a power of 2"; | |
474 | r = -EINVAL; | |
475 | goto bad3; | |
476 | } | |
477 | ||
478 | s->chunk_size = chunk_size; | |
479 | s->chunk_mask = chunk_size - 1; | |
480 | s->type = persistent; | |
481 | s->chunk_shift = ffs(chunk_size) - 1; | |
482 | ||
483 | s->valid = 1; | |
aa14edeb | 484 | s->active = 0; |
1da177e4 LT |
485 | s->last_percent = 0; |
486 | init_rwsem(&s->lock); | |
487 | s->table = ti->table; | |
488 | ||
489 | /* Allocate hash table for COW data */ | |
490 | if (init_hash_tables(s)) { | |
491 | ti->error = "Unable to allocate hash table space"; | |
492 | r = -ENOMEM; | |
493 | goto bad3; | |
494 | } | |
495 | ||
496 | /* | |
497 | * Check the persistent flag - done here because we need the iobuf | |
498 | * to check the LV header | |
499 | */ | |
500 | s->store.snap = s; | |
501 | ||
502 | if (persistent == 'P') | |
503 | r = dm_create_persistent(&s->store, chunk_size); | |
504 | else | |
505 | r = dm_create_transient(&s->store, s, blocksize); | |
506 | ||
507 | if (r) { | |
508 | ti->error = "Couldn't create exception store"; | |
509 | r = -EINVAL; | |
510 | goto bad4; | |
511 | } | |
512 | ||
513 | r = kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client); | |
514 | if (r) { | |
515 | ti->error = "Could not create kcopyd client"; | |
516 | goto bad5; | |
517 | } | |
518 | ||
aa14edeb AK |
519 | /* Metadata must only be loaded into one table at once */ |
520 | read_snapshot_metadata(s); | |
521 | ||
1da177e4 | 522 | /* Add snapshot to the list of snapshots for this origin */ |
aa14edeb | 523 | /* Exceptions aren't triggered till snapshot_resume() is called */ |
1da177e4 LT |
524 | if (register_snapshot(s)) { |
525 | r = -EINVAL; | |
526 | ti->error = "Cannot register snapshot origin"; | |
527 | goto bad6; | |
528 | } | |
529 | ||
530 | ti->private = s; | |
531 | ti->split_io = chunk_size; | |
532 | ||
533 | return 0; | |
534 | ||
535 | bad6: | |
536 | kcopyd_client_destroy(s->kcopyd_client); | |
537 | ||
538 | bad5: | |
539 | s->store.destroy(&s->store); | |
540 | ||
541 | bad4: | |
542 | exit_exception_table(&s->pending, pending_cache); | |
543 | exit_exception_table(&s->complete, exception_cache); | |
544 | ||
545 | bad3: | |
546 | dm_put_device(ti, s->cow); | |
547 | dm_put_device(ti, s->origin); | |
548 | ||
549 | bad2: | |
550 | kfree(s); | |
551 | ||
552 | bad1: | |
553 | return r; | |
554 | } | |
555 | ||
556 | static void snapshot_dtr(struct dm_target *ti) | |
557 | { | |
558 | struct dm_snapshot *s = (struct dm_snapshot *) ti->private; | |
559 | ||
560 | unregister_snapshot(s); | |
561 | ||
562 | exit_exception_table(&s->pending, pending_cache); | |
563 | exit_exception_table(&s->complete, exception_cache); | |
564 | ||
565 | /* Deallocate memory used */ | |
566 | s->store.destroy(&s->store); | |
567 | ||
568 | dm_put_device(ti, s->origin); | |
569 | dm_put_device(ti, s->cow); | |
570 | kcopyd_client_destroy(s->kcopyd_client); | |
571 | kfree(s); | |
572 | } | |
573 | ||
574 | /* | |
575 | * Flush a list of buffers. | |
576 | */ | |
577 | static void flush_bios(struct bio *bio) | |
578 | { | |
579 | struct bio *n; | |
580 | ||
581 | while (bio) { | |
582 | n = bio->bi_next; | |
583 | bio->bi_next = NULL; | |
584 | generic_make_request(bio); | |
585 | bio = n; | |
586 | } | |
587 | } | |
588 | ||
589 | /* | |
590 | * Error a list of buffers. | |
591 | */ | |
592 | static void error_bios(struct bio *bio) | |
593 | { | |
594 | struct bio *n; | |
595 | ||
596 | while (bio) { | |
597 | n = bio->bi_next; | |
598 | bio->bi_next = NULL; | |
599 | bio_io_error(bio, bio->bi_size); | |
600 | bio = n; | |
601 | } | |
602 | } | |
603 | ||
604 | static struct bio *__flush_bios(struct pending_exception *pe) | |
605 | { | |
b4b610f6 AK |
606 | /* |
607 | * If this pe is involved in a write to the origin and | |
608 | * it is the last sibling to complete then release | |
609 | * the bios for the original write to the origin. | |
1da177e4 | 610 | */ |
b4b610f6 AK |
611 | |
612 | if (pe->primary_pe && | |
613 | atomic_dec_and_test(&pe->primary_pe->sibling_count)) | |
614 | return bio_list_get(&pe->primary_pe->origin_bios); | |
1da177e4 LT |
615 | |
616 | return NULL; | |
617 | } | |
618 | ||
619 | static void pending_complete(struct pending_exception *pe, int success) | |
620 | { | |
621 | struct exception *e; | |
b4b610f6 | 622 | struct pending_exception *primary_pe; |
1da177e4 LT |
623 | struct dm_snapshot *s = pe->snap; |
624 | struct bio *flush = NULL; | |
625 | ||
626 | if (success) { | |
627 | e = alloc_exception(); | |
628 | if (!e) { | |
629 | DMWARN("Unable to allocate exception."); | |
630 | down_write(&s->lock); | |
631 | s->store.drop_snapshot(&s->store); | |
632 | s->valid = 0; | |
633 | flush = __flush_bios(pe); | |
634 | up_write(&s->lock); | |
635 | ||
636 | error_bios(bio_list_get(&pe->snapshot_bios)); | |
637 | goto out; | |
638 | } | |
639 | *e = pe->e; | |
640 | ||
641 | /* | |
642 | * Add a proper exception, and remove the | |
643 | * in-flight exception from the list. | |
644 | */ | |
645 | down_write(&s->lock); | |
646 | insert_exception(&s->complete, e); | |
647 | remove_exception(&pe->e); | |
648 | flush = __flush_bios(pe); | |
649 | ||
650 | /* Submit any pending write bios */ | |
651 | up_write(&s->lock); | |
652 | ||
653 | flush_bios(bio_list_get(&pe->snapshot_bios)); | |
654 | } else { | |
655 | /* Read/write error - snapshot is unusable */ | |
656 | down_write(&s->lock); | |
657 | if (s->valid) | |
658 | DMERR("Error reading/writing snapshot"); | |
659 | s->store.drop_snapshot(&s->store); | |
660 | s->valid = 0; | |
661 | remove_exception(&pe->e); | |
662 | flush = __flush_bios(pe); | |
663 | up_write(&s->lock); | |
664 | ||
665 | error_bios(bio_list_get(&pe->snapshot_bios)); | |
666 | ||
667 | dm_table_event(s->table); | |
668 | } | |
669 | ||
670 | out: | |
b4b610f6 AK |
671 | primary_pe = pe->primary_pe; |
672 | ||
673 | /* | |
674 | * Free the pe if it's not linked to an origin write or if | |
675 | * it's not itself a primary pe. | |
676 | */ | |
677 | if (!primary_pe || primary_pe != pe) | |
678 | free_pending_exception(pe); | |
679 | ||
680 | /* | |
681 | * Free the primary pe if nothing references it. | |
682 | */ | |
683 | if (primary_pe && !atomic_read(&primary_pe->sibling_count)) | |
684 | free_pending_exception(primary_pe); | |
1da177e4 LT |
685 | |
686 | if (flush) | |
687 | flush_bios(flush); | |
688 | } | |
689 | ||
690 | static void commit_callback(void *context, int success) | |
691 | { | |
692 | struct pending_exception *pe = (struct pending_exception *) context; | |
693 | pending_complete(pe, success); | |
694 | } | |
695 | ||
696 | /* | |
697 | * Called when the copy I/O has finished. kcopyd actually runs | |
698 | * this code so don't block. | |
699 | */ | |
700 | static void copy_callback(int read_err, unsigned int write_err, void *context) | |
701 | { | |
702 | struct pending_exception *pe = (struct pending_exception *) context; | |
703 | struct dm_snapshot *s = pe->snap; | |
704 | ||
705 | if (read_err || write_err) | |
706 | pending_complete(pe, 0); | |
707 | ||
708 | else | |
709 | /* Update the metadata if we are persistent */ | |
710 | s->store.commit_exception(&s->store, &pe->e, commit_callback, | |
711 | pe); | |
712 | } | |
713 | ||
714 | /* | |
715 | * Dispatches the copy operation to kcopyd. | |
716 | */ | |
858119e1 | 717 | static void start_copy(struct pending_exception *pe) |
1da177e4 LT |
718 | { |
719 | struct dm_snapshot *s = pe->snap; | |
720 | struct io_region src, dest; | |
721 | struct block_device *bdev = s->origin->bdev; | |
722 | sector_t dev_size; | |
723 | ||
724 | dev_size = get_dev_size(bdev); | |
725 | ||
726 | src.bdev = bdev; | |
727 | src.sector = chunk_to_sector(s, pe->e.old_chunk); | |
728 | src.count = min(s->chunk_size, dev_size - src.sector); | |
729 | ||
730 | dest.bdev = s->cow->bdev; | |
731 | dest.sector = chunk_to_sector(s, pe->e.new_chunk); | |
732 | dest.count = src.count; | |
733 | ||
734 | /* Hand over to kcopyd */ | |
735 | kcopyd_copy(s->kcopyd_client, | |
736 | &src, 1, &dest, 0, copy_callback, pe); | |
737 | } | |
738 | ||
739 | /* | |
740 | * Looks to see if this snapshot already has a pending exception | |
741 | * for this chunk, otherwise it allocates a new one and inserts | |
742 | * it into the pending table. | |
743 | * | |
744 | * NOTE: a write lock must be held on snap->lock before calling | |
745 | * this. | |
746 | */ | |
747 | static struct pending_exception * | |
748 | __find_pending_exception(struct dm_snapshot *s, struct bio *bio) | |
749 | { | |
750 | struct exception *e; | |
751 | struct pending_exception *pe; | |
752 | chunk_t chunk = sector_to_chunk(s, bio->bi_sector); | |
753 | ||
754 | /* | |
755 | * Is there a pending exception for this already ? | |
756 | */ | |
757 | e = lookup_exception(&s->pending, chunk); | |
758 | if (e) { | |
759 | /* cast the exception to a pending exception */ | |
760 | pe = container_of(e, struct pending_exception, e); | |
761 | ||
762 | } else { | |
763 | /* | |
764 | * Create a new pending exception, we don't want | |
765 | * to hold the lock while we do this. | |
766 | */ | |
767 | up_write(&s->lock); | |
768 | pe = alloc_pending_exception(); | |
769 | down_write(&s->lock); | |
770 | ||
771 | e = lookup_exception(&s->pending, chunk); | |
772 | if (e) { | |
773 | free_pending_exception(pe); | |
774 | pe = container_of(e, struct pending_exception, e); | |
775 | } else { | |
776 | pe->e.old_chunk = chunk; | |
777 | bio_list_init(&pe->origin_bios); | |
778 | bio_list_init(&pe->snapshot_bios); | |
b4b610f6 AK |
779 | pe->primary_pe = NULL; |
780 | atomic_set(&pe->sibling_count, 1); | |
1da177e4 LT |
781 | pe->snap = s; |
782 | pe->started = 0; | |
783 | ||
784 | if (s->store.prepare_exception(&s->store, &pe->e)) { | |
785 | free_pending_exception(pe); | |
786 | s->valid = 0; | |
787 | return NULL; | |
788 | } | |
789 | ||
790 | insert_exception(&s->pending, &pe->e); | |
791 | } | |
792 | } | |
793 | ||
794 | return pe; | |
795 | } | |
796 | ||
797 | static inline void remap_exception(struct dm_snapshot *s, struct exception *e, | |
798 | struct bio *bio) | |
799 | { | |
800 | bio->bi_bdev = s->cow->bdev; | |
801 | bio->bi_sector = chunk_to_sector(s, e->new_chunk) + | |
802 | (bio->bi_sector & s->chunk_mask); | |
803 | } | |
804 | ||
805 | static int snapshot_map(struct dm_target *ti, struct bio *bio, | |
806 | union map_info *map_context) | |
807 | { | |
808 | struct exception *e; | |
809 | struct dm_snapshot *s = (struct dm_snapshot *) ti->private; | |
810 | int r = 1; | |
811 | chunk_t chunk; | |
812 | struct pending_exception *pe; | |
813 | ||
814 | chunk = sector_to_chunk(s, bio->bi_sector); | |
815 | ||
816 | /* Full snapshots are not usable */ | |
817 | if (!s->valid) | |
f6a80ea8 | 818 | return -EIO; |
1da177e4 | 819 | |
4aac0a63 AK |
820 | if (unlikely(bio_barrier(bio))) |
821 | return -EOPNOTSUPP; | |
822 | ||
1da177e4 LT |
823 | /* |
824 | * Write to snapshot - higher level takes care of RW/RO | |
825 | * flags so we should only get this if we are | |
826 | * writeable. | |
827 | */ | |
828 | if (bio_rw(bio) == WRITE) { | |
829 | ||
830 | /* FIXME: should only take write lock if we need | |
831 | * to copy an exception */ | |
832 | down_write(&s->lock); | |
833 | ||
834 | /* If the block is already remapped - use that, else remap it */ | |
835 | e = lookup_exception(&s->complete, chunk); | |
836 | if (e) { | |
837 | remap_exception(s, e, bio); | |
838 | up_write(&s->lock); | |
839 | ||
840 | } else { | |
841 | pe = __find_pending_exception(s, bio); | |
842 | ||
843 | if (!pe) { | |
844 | if (s->store.drop_snapshot) | |
845 | s->store.drop_snapshot(&s->store); | |
846 | s->valid = 0; | |
847 | r = -EIO; | |
848 | up_write(&s->lock); | |
849 | } else { | |
850 | remap_exception(s, &pe->e, bio); | |
851 | bio_list_add(&pe->snapshot_bios, bio); | |
852 | ||
853 | if (!pe->started) { | |
854 | /* this is protected by snap->lock */ | |
855 | pe->started = 1; | |
856 | up_write(&s->lock); | |
857 | start_copy(pe); | |
858 | } else | |
859 | up_write(&s->lock); | |
860 | r = 0; | |
861 | } | |
862 | } | |
863 | ||
864 | } else { | |
865 | /* | |
866 | * FIXME: this read path scares me because we | |
867 | * always use the origin when we have a pending | |
868 | * exception. However I can't think of a | |
869 | * situation where this is wrong - ejt. | |
870 | */ | |
871 | ||
872 | /* Do reads */ | |
873 | down_read(&s->lock); | |
874 | ||
875 | /* See if it it has been remapped */ | |
876 | e = lookup_exception(&s->complete, chunk); | |
877 | if (e) | |
878 | remap_exception(s, e, bio); | |
879 | else | |
880 | bio->bi_bdev = s->origin->bdev; | |
881 | ||
882 | up_read(&s->lock); | |
883 | } | |
884 | ||
885 | return r; | |
886 | } | |
887 | ||
888 | static void snapshot_resume(struct dm_target *ti) | |
889 | { | |
890 | struct dm_snapshot *s = (struct dm_snapshot *) ti->private; | |
891 | ||
aa14edeb AK |
892 | down_write(&s->lock); |
893 | s->active = 1; | |
894 | up_write(&s->lock); | |
1da177e4 LT |
895 | } |
896 | ||
897 | static int snapshot_status(struct dm_target *ti, status_type_t type, | |
898 | char *result, unsigned int maxlen) | |
899 | { | |
900 | struct dm_snapshot *snap = (struct dm_snapshot *) ti->private; | |
901 | ||
902 | switch (type) { | |
903 | case STATUSTYPE_INFO: | |
904 | if (!snap->valid) | |
905 | snprintf(result, maxlen, "Invalid"); | |
906 | else { | |
907 | if (snap->store.fraction_full) { | |
908 | sector_t numerator, denominator; | |
909 | snap->store.fraction_full(&snap->store, | |
910 | &numerator, | |
911 | &denominator); | |
912 | snprintf(result, maxlen, | |
913 | SECTOR_FORMAT "/" SECTOR_FORMAT, | |
914 | numerator, denominator); | |
915 | } | |
916 | else | |
917 | snprintf(result, maxlen, "Unknown"); | |
918 | } | |
919 | break; | |
920 | ||
921 | case STATUSTYPE_TABLE: | |
922 | /* | |
923 | * kdevname returns a static pointer so we need | |
924 | * to make private copies if the output is to | |
925 | * make sense. | |
926 | */ | |
927 | snprintf(result, maxlen, "%s %s %c " SECTOR_FORMAT, | |
928 | snap->origin->name, snap->cow->name, | |
929 | snap->type, snap->chunk_size); | |
930 | break; | |
931 | } | |
932 | ||
933 | return 0; | |
934 | } | |
935 | ||
936 | /*----------------------------------------------------------------- | |
937 | * Origin methods | |
938 | *---------------------------------------------------------------*/ | |
1da177e4 LT |
939 | static int __origin_write(struct list_head *snapshots, struct bio *bio) |
940 | { | |
b4b610f6 | 941 | int r = 1, first = 0; |
1da177e4 LT |
942 | struct dm_snapshot *snap; |
943 | struct exception *e; | |
b4b610f6 | 944 | struct pending_exception *pe, *next_pe, *primary_pe = NULL; |
1da177e4 | 945 | chunk_t chunk; |
eccf0817 | 946 | LIST_HEAD(pe_queue); |
1da177e4 LT |
947 | |
948 | /* Do all the snapshots on this origin */ | |
949 | list_for_each_entry (snap, snapshots, list) { | |
950 | ||
aa14edeb AK |
951 | /* Only deal with valid and active snapshots */ |
952 | if (!snap->valid || !snap->active) | |
1da177e4 LT |
953 | continue; |
954 | ||
d5e404c1 AK |
955 | /* Nothing to do if writing beyond end of snapshot */ |
956 | if (bio->bi_sector >= dm_table_get_size(snap->table)) | |
957 | continue; | |
958 | ||
1da177e4 LT |
959 | down_write(&snap->lock); |
960 | ||
961 | /* | |
962 | * Remember, different snapshots can have | |
963 | * different chunk sizes. | |
964 | */ | |
965 | chunk = sector_to_chunk(snap, bio->bi_sector); | |
966 | ||
967 | /* | |
968 | * Check exception table to see if block | |
969 | * is already remapped in this snapshot | |
970 | * and trigger an exception if not. | |
b4b610f6 AK |
971 | * |
972 | * sibling_count is initialised to 1 so pending_complete() | |
973 | * won't destroy the primary_pe while we're inside this loop. | |
1da177e4 LT |
974 | */ |
975 | e = lookup_exception(&snap->complete, chunk); | |
976 | if (!e) { | |
977 | pe = __find_pending_exception(snap, bio); | |
978 | if (!pe) { | |
979 | snap->store.drop_snapshot(&snap->store); | |
980 | snap->valid = 0; | |
981 | ||
982 | } else { | |
b4b610f6 AK |
983 | if (!primary_pe) { |
984 | /* | |
985 | * Either every pe here has same | |
986 | * primary_pe or none has one yet. | |
987 | */ | |
988 | if (pe->primary_pe) | |
989 | primary_pe = pe->primary_pe; | |
990 | else { | |
991 | primary_pe = pe; | |
992 | first = 1; | |
993 | } | |
994 | ||
995 | bio_list_add(&primary_pe->origin_bios, | |
996 | bio); | |
eccf0817 | 997 | r = 0; |
eccf0817 | 998 | } |
b4b610f6 AK |
999 | if (!pe->primary_pe) { |
1000 | atomic_inc(&primary_pe->sibling_count); | |
1001 | pe->primary_pe = primary_pe; | |
1002 | } | |
eccf0817 AK |
1003 | if (!pe->started) { |
1004 | pe->started = 1; | |
1005 | list_add_tail(&pe->list, &pe_queue); | |
1006 | } | |
1da177e4 LT |
1007 | } |
1008 | } | |
1009 | ||
1010 | up_write(&snap->lock); | |
1011 | } | |
1012 | ||
b4b610f6 AK |
1013 | if (!primary_pe) |
1014 | goto out; | |
1015 | ||
1016 | /* | |
1017 | * If this is the first time we're processing this chunk and | |
1018 | * sibling_count is now 1 it means all the pending exceptions | |
1019 | * got completed while we were in the loop above, so it falls to | |
1020 | * us here to remove the primary_pe and submit any origin_bios. | |
1021 | */ | |
1022 | ||
1023 | if (first && atomic_dec_and_test(&primary_pe->sibling_count)) { | |
1024 | flush_bios(bio_list_get(&primary_pe->origin_bios)); | |
1025 | free_pending_exception(primary_pe); | |
1026 | /* If we got here, pe_queue is necessarily empty. */ | |
1027 | goto out; | |
1028 | } | |
1029 | ||
1da177e4 LT |
1030 | /* |
1031 | * Now that we have a complete pe list we can start the copying. | |
1032 | */ | |
eccf0817 AK |
1033 | list_for_each_entry_safe(pe, next_pe, &pe_queue, list) |
1034 | start_copy(pe); | |
1da177e4 | 1035 | |
b4b610f6 | 1036 | out: |
1da177e4 LT |
1037 | return r; |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * Called on a write from the origin driver. | |
1042 | */ | |
1043 | static int do_origin(struct dm_dev *origin, struct bio *bio) | |
1044 | { | |
1045 | struct origin *o; | |
1046 | int r = 1; | |
1047 | ||
1048 | down_read(&_origins_lock); | |
1049 | o = __lookup_origin(origin->bdev); | |
1050 | if (o) | |
1051 | r = __origin_write(&o->snapshots, bio); | |
1052 | up_read(&_origins_lock); | |
1053 | ||
1054 | return r; | |
1055 | } | |
1056 | ||
1057 | /* | |
1058 | * Origin: maps a linear range of a device, with hooks for snapshotting. | |
1059 | */ | |
1060 | ||
1061 | /* | |
1062 | * Construct an origin mapping: <dev_path> | |
1063 | * The context for an origin is merely a 'struct dm_dev *' | |
1064 | * pointing to the real device. | |
1065 | */ | |
1066 | static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
1067 | { | |
1068 | int r; | |
1069 | struct dm_dev *dev; | |
1070 | ||
1071 | if (argc != 1) { | |
1072 | ti->error = "dm-origin: incorrect number of arguments"; | |
1073 | return -EINVAL; | |
1074 | } | |
1075 | ||
1076 | r = dm_get_device(ti, argv[0], 0, ti->len, | |
1077 | dm_table_get_mode(ti->table), &dev); | |
1078 | if (r) { | |
1079 | ti->error = "Cannot get target device"; | |
1080 | return r; | |
1081 | } | |
1082 | ||
1083 | ti->private = dev; | |
1084 | return 0; | |
1085 | } | |
1086 | ||
1087 | static void origin_dtr(struct dm_target *ti) | |
1088 | { | |
1089 | struct dm_dev *dev = (struct dm_dev *) ti->private; | |
1090 | dm_put_device(ti, dev); | |
1091 | } | |
1092 | ||
1093 | static int origin_map(struct dm_target *ti, struct bio *bio, | |
1094 | union map_info *map_context) | |
1095 | { | |
1096 | struct dm_dev *dev = (struct dm_dev *) ti->private; | |
1097 | bio->bi_bdev = dev->bdev; | |
1098 | ||
4aac0a63 AK |
1099 | if (unlikely(bio_barrier(bio))) |
1100 | return -EOPNOTSUPP; | |
1101 | ||
1da177e4 LT |
1102 | /* Only tell snapshots if this is a write */ |
1103 | return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : 1; | |
1104 | } | |
1105 | ||
1106 | #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r)) | |
1107 | ||
1108 | /* | |
1109 | * Set the target "split_io" field to the minimum of all the snapshots' | |
1110 | * chunk sizes. | |
1111 | */ | |
1112 | static void origin_resume(struct dm_target *ti) | |
1113 | { | |
1114 | struct dm_dev *dev = (struct dm_dev *) ti->private; | |
1115 | struct dm_snapshot *snap; | |
1116 | struct origin *o; | |
1117 | chunk_t chunk_size = 0; | |
1118 | ||
1119 | down_read(&_origins_lock); | |
1120 | o = __lookup_origin(dev->bdev); | |
1121 | if (o) | |
1122 | list_for_each_entry (snap, &o->snapshots, list) | |
1123 | chunk_size = min_not_zero(chunk_size, snap->chunk_size); | |
1124 | up_read(&_origins_lock); | |
1125 | ||
1126 | ti->split_io = chunk_size; | |
1127 | } | |
1128 | ||
1129 | static int origin_status(struct dm_target *ti, status_type_t type, char *result, | |
1130 | unsigned int maxlen) | |
1131 | { | |
1132 | struct dm_dev *dev = (struct dm_dev *) ti->private; | |
1133 | ||
1134 | switch (type) { | |
1135 | case STATUSTYPE_INFO: | |
1136 | result[0] = '\0'; | |
1137 | break; | |
1138 | ||
1139 | case STATUSTYPE_TABLE: | |
1140 | snprintf(result, maxlen, "%s", dev->name); | |
1141 | break; | |
1142 | } | |
1143 | ||
1144 | return 0; | |
1145 | } | |
1146 | ||
1147 | static struct target_type origin_target = { | |
1148 | .name = "snapshot-origin", | |
aa14edeb | 1149 | .version = {1, 1, 0}, |
1da177e4 LT |
1150 | .module = THIS_MODULE, |
1151 | .ctr = origin_ctr, | |
1152 | .dtr = origin_dtr, | |
1153 | .map = origin_map, | |
1154 | .resume = origin_resume, | |
1155 | .status = origin_status, | |
1156 | }; | |
1157 | ||
1158 | static struct target_type snapshot_target = { | |
1159 | .name = "snapshot", | |
aa14edeb | 1160 | .version = {1, 1, 0}, |
1da177e4 LT |
1161 | .module = THIS_MODULE, |
1162 | .ctr = snapshot_ctr, | |
1163 | .dtr = snapshot_dtr, | |
1164 | .map = snapshot_map, | |
1165 | .resume = snapshot_resume, | |
1166 | .status = snapshot_status, | |
1167 | }; | |
1168 | ||
1169 | static int __init dm_snapshot_init(void) | |
1170 | { | |
1171 | int r; | |
1172 | ||
1173 | r = dm_register_target(&snapshot_target); | |
1174 | if (r) { | |
1175 | DMERR("snapshot target register failed %d", r); | |
1176 | return r; | |
1177 | } | |
1178 | ||
1179 | r = dm_register_target(&origin_target); | |
1180 | if (r < 0) { | |
1181 | DMERR("Device mapper: Origin: register failed %d\n", r); | |
1182 | goto bad1; | |
1183 | } | |
1184 | ||
1185 | r = init_origin_hash(); | |
1186 | if (r) { | |
1187 | DMERR("init_origin_hash failed."); | |
1188 | goto bad2; | |
1189 | } | |
1190 | ||
1191 | exception_cache = kmem_cache_create("dm-snapshot-ex", | |
1192 | sizeof(struct exception), | |
1193 | __alignof__(struct exception), | |
1194 | 0, NULL, NULL); | |
1195 | if (!exception_cache) { | |
1196 | DMERR("Couldn't create exception cache."); | |
1197 | r = -ENOMEM; | |
1198 | goto bad3; | |
1199 | } | |
1200 | ||
1201 | pending_cache = | |
1202 | kmem_cache_create("dm-snapshot-in", | |
1203 | sizeof(struct pending_exception), | |
1204 | __alignof__(struct pending_exception), | |
1205 | 0, NULL, NULL); | |
1206 | if (!pending_cache) { | |
1207 | DMERR("Couldn't create pending cache."); | |
1208 | r = -ENOMEM; | |
1209 | goto bad4; | |
1210 | } | |
1211 | ||
93d2341c | 1212 | pending_pool = mempool_create_slab_pool(128, pending_cache); |
1da177e4 LT |
1213 | if (!pending_pool) { |
1214 | DMERR("Couldn't create pending pool."); | |
1215 | r = -ENOMEM; | |
1216 | goto bad5; | |
1217 | } | |
1218 | ||
1219 | return 0; | |
1220 | ||
1221 | bad5: | |
1222 | kmem_cache_destroy(pending_cache); | |
1223 | bad4: | |
1224 | kmem_cache_destroy(exception_cache); | |
1225 | bad3: | |
1226 | exit_origin_hash(); | |
1227 | bad2: | |
1228 | dm_unregister_target(&origin_target); | |
1229 | bad1: | |
1230 | dm_unregister_target(&snapshot_target); | |
1231 | return r; | |
1232 | } | |
1233 | ||
1234 | static void __exit dm_snapshot_exit(void) | |
1235 | { | |
1236 | int r; | |
1237 | ||
1238 | r = dm_unregister_target(&snapshot_target); | |
1239 | if (r) | |
1240 | DMERR("snapshot unregister failed %d", r); | |
1241 | ||
1242 | r = dm_unregister_target(&origin_target); | |
1243 | if (r) | |
1244 | DMERR("origin unregister failed %d", r); | |
1245 | ||
1246 | exit_origin_hash(); | |
1247 | mempool_destroy(pending_pool); | |
1248 | kmem_cache_destroy(pending_cache); | |
1249 | kmem_cache_destroy(exception_cache); | |
1250 | } | |
1251 | ||
1252 | /* Module hooks */ | |
1253 | module_init(dm_snapshot_init); | |
1254 | module_exit(dm_snapshot_exit); | |
1255 | ||
1256 | MODULE_DESCRIPTION(DM_NAME " snapshot target"); | |
1257 | MODULE_AUTHOR("Joe Thornber"); | |
1258 | MODULE_LICENSE("GPL"); |