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