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