| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2001-2002 Sistina Software (UK) Limited. | 
|  | 3 | * Copyright (C) 2006-2008 Red Hat GmbH | 
|  | 4 | * | 
|  | 5 | * This file is released under the GPL. | 
|  | 6 | */ | 
|  | 7 |  | 
|  | 8 | #include "dm-exception-store.h" | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 9 |  | 
|  | 10 | #include <linux/mm.h> | 
|  | 11 | #include <linux/pagemap.h> | 
|  | 12 | #include <linux/vmalloc.h> | 
|  | 13 | #include <linux/slab.h> | 
|  | 14 | #include <linux/dm-io.h> | 
|  | 15 |  | 
|  | 16 | #define DM_MSG_PREFIX "transient snapshot" | 
|  | 17 |  | 
|  | 18 | /*----------------------------------------------------------------- | 
|  | 19 | * Implementation of the store for non-persistent snapshots. | 
|  | 20 | *---------------------------------------------------------------*/ | 
|  | 21 | struct transient_c { | 
|  | 22 | sector_t next_free; | 
|  | 23 | }; | 
|  | 24 |  | 
| Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 25 | static void transient_dtr(struct dm_exception_store *store) | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 26 | { | 
|  | 27 | kfree(store->context); | 
|  | 28 | } | 
|  | 29 |  | 
| Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 30 | static int transient_read_metadata(struct dm_exception_store *store, | 
|  | 31 | int (*callback)(void *callback_context, | 
|  | 32 | chunk_t old, chunk_t new), | 
|  | 33 | void *callback_context) | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 34 | { | 
|  | 35 | return 0; | 
|  | 36 | } | 
|  | 37 |  | 
| Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 38 | static int transient_prepare_exception(struct dm_exception_store *store, | 
|  | 39 | struct dm_snap_exception *e) | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 40 | { | 
| Jonathan Brassow | b2a1146 | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 41 | struct transient_c *tc = store->context; | 
| Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 42 | sector_t size = get_dev_size(store->cow->bdev); | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 43 |  | 
| Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 44 | if (size < (tc->next_free + store->chunk_size)) | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 45 | return -1; | 
|  | 46 |  | 
| Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 47 | e->new_chunk = sector_to_chunk(store, tc->next_free); | 
| Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 48 | tc->next_free += store->chunk_size; | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 49 |  | 
|  | 50 | return 0; | 
|  | 51 | } | 
|  | 52 |  | 
| Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 53 | static void transient_commit_exception(struct dm_exception_store *store, | 
|  | 54 | struct dm_snap_exception *e, | 
|  | 55 | void (*callback) (void *, int success), | 
|  | 56 | void *callback_context) | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 57 | { | 
|  | 58 | /* Just succeed */ | 
|  | 59 | callback(callback_context, 1); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | static void transient_fraction_full(struct dm_exception_store *store, | 
|  | 63 | sector_t *numerator, sector_t *denominator) | 
|  | 64 | { | 
|  | 65 | *numerator = ((struct transient_c *) store->context)->next_free; | 
| Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 66 | *denominator = get_dev_size(store->cow->bdev); | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 69 | static int transient_ctr(struct dm_exception_store *store, | 
|  | 70 | unsigned argc, char **argv) | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 71 | { | 
|  | 72 | struct transient_c *tc; | 
|  | 73 |  | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 74 | tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL); | 
|  | 75 | if (!tc) | 
|  | 76 | return -ENOMEM; | 
|  | 77 |  | 
|  | 78 | tc->next_free = 0; | 
|  | 79 | store->context = tc; | 
|  | 80 |  | 
|  | 81 | return 0; | 
|  | 82 | } | 
|  | 83 |  | 
| Jonathan Brassow | 1e302a9 | 2009-04-02 19:55:35 +0100 | [diff] [blame] | 84 | static unsigned transient_status(struct dm_exception_store *store, | 
|  | 85 | status_type_t status, char *result, | 
|  | 86 | unsigned maxlen) | 
| Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 87 | { | 
| Jonathan Brassow | 1e302a9 | 2009-04-02 19:55:35 +0100 | [diff] [blame] | 88 | unsigned sz = 0; | 
|  | 89 |  | 
|  | 90 | switch (status) { | 
|  | 91 | case STATUSTYPE_INFO: | 
|  | 92 | break; | 
|  | 93 | case STATUSTYPE_TABLE: | 
|  | 94 | DMEMIT(" %s N %llu", store->cow->name, | 
|  | 95 | (unsigned long long)store->chunk_size); | 
|  | 96 | } | 
| Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 97 |  | 
|  | 98 | return sz; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | static struct dm_exception_store_type _transient_type = { | 
|  | 102 | .name = "transient", | 
|  | 103 | .module = THIS_MODULE, | 
|  | 104 | .ctr = transient_ctr, | 
|  | 105 | .dtr = transient_dtr, | 
|  | 106 | .read_metadata = transient_read_metadata, | 
|  | 107 | .prepare_exception = transient_prepare_exception, | 
|  | 108 | .commit_exception = transient_commit_exception, | 
|  | 109 | .fraction_full = transient_fraction_full, | 
|  | 110 | .status = transient_status, | 
|  | 111 | }; | 
|  | 112 |  | 
|  | 113 | static struct dm_exception_store_type _transient_compat_type = { | 
|  | 114 | .name = "N", | 
|  | 115 | .module = THIS_MODULE, | 
|  | 116 | .ctr = transient_ctr, | 
|  | 117 | .dtr = transient_dtr, | 
|  | 118 | .read_metadata = transient_read_metadata, | 
|  | 119 | .prepare_exception = transient_prepare_exception, | 
|  | 120 | .commit_exception = transient_commit_exception, | 
|  | 121 | .fraction_full = transient_fraction_full, | 
|  | 122 | .status = transient_status, | 
|  | 123 | }; | 
|  | 124 |  | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 125 | int dm_transient_snapshot_init(void) | 
|  | 126 | { | 
| Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 127 | int r; | 
|  | 128 |  | 
|  | 129 | r = dm_exception_store_type_register(&_transient_type); | 
|  | 130 | if (r) { | 
|  | 131 | DMWARN("Unable to register transient exception store type"); | 
|  | 132 | return r; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | r = dm_exception_store_type_register(&_transient_compat_type); | 
|  | 136 | if (r) { | 
|  | 137 | DMWARN("Unable to register old-style transient " | 
|  | 138 | "exception store type"); | 
|  | 139 | dm_exception_store_type_unregister(&_transient_type); | 
|  | 140 | return r; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | return r; | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | void dm_transient_snapshot_exit(void) | 
|  | 147 | { | 
| Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 148 | dm_exception_store_type_unregister(&_transient_type); | 
|  | 149 | dm_exception_store_type_unregister(&_transient_compat_type); | 
| Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 150 | } |