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> |
Paul Gortmaker | daaa5f7 | 2011-05-27 15:50:58 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 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, |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 40 | struct dm_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; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 43 | sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 44 | |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 45 | if (size < (tc->next_free + store->chunk_size)) |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 46 | return -1; |
| 47 | |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 48 | e->new_chunk = sector_to_chunk(store, tc->next_free); |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 49 | tc->next_free += store->chunk_size; |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 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, |
Mikulas Patocka | 385277b | 2016-01-08 19:07:55 -0500 | [diff] [blame] | 55 | struct dm_exception *e, int valid, |
Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 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 */ |
Mikulas Patocka | 385277b | 2016-01-08 19:07:55 -0500 | [diff] [blame] | 60 | callback(callback_context, valid); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Mike Snitzer | 985903b | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 63 | static void transient_usage(struct dm_exception_store *store, |
| 64 | sector_t *total_sectors, |
| 65 | sector_t *sectors_allocated, |
| 66 | sector_t *metadata_sectors) |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 67 | { |
Mike Snitzer | 985903b | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 68 | *sectors_allocated = ((struct transient_c *) store->context)->next_free; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 69 | *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev); |
Mike Snitzer | 985903b | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 70 | *metadata_sectors = 0; |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mike Snitzer | b0d3cc0 | 2015-10-08 18:05:41 -0400 | [diff] [blame] | 73 | static int transient_ctr(struct dm_exception_store *store, char *options) |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 74 | { |
| 75 | struct transient_c *tc; |
| 76 | |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 77 | tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL); |
| 78 | if (!tc) |
| 79 | return -ENOMEM; |
| 80 | |
| 81 | tc->next_free = 0; |
| 82 | store->context = tc; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Jonathan Brassow | 1e302a9 | 2009-04-02 19:55:35 +0100 | [diff] [blame] | 87 | static unsigned transient_status(struct dm_exception_store *store, |
| 88 | status_type_t status, char *result, |
| 89 | unsigned maxlen) |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 90 | { |
Jonathan Brassow | 1e302a9 | 2009-04-02 19:55:35 +0100 | [diff] [blame] | 91 | unsigned sz = 0; |
| 92 | |
| 93 | switch (status) { |
| 94 | case STATUSTYPE_INFO: |
| 95 | break; |
| 96 | case STATUSTYPE_TABLE: |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 97 | DMEMIT(" N %llu", (unsigned long long)store->chunk_size); |
Jonathan Brassow | 1e302a9 | 2009-04-02 19:55:35 +0100 | [diff] [blame] | 98 | } |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 99 | |
| 100 | return sz; |
| 101 | } |
| 102 | |
| 103 | static struct dm_exception_store_type _transient_type = { |
| 104 | .name = "transient", |
| 105 | .module = THIS_MODULE, |
| 106 | .ctr = transient_ctr, |
| 107 | .dtr = transient_dtr, |
| 108 | .read_metadata = transient_read_metadata, |
| 109 | .prepare_exception = transient_prepare_exception, |
| 110 | .commit_exception = transient_commit_exception, |
Mike Snitzer | 985903b | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 111 | .usage = transient_usage, |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 112 | .status = transient_status, |
| 113 | }; |
| 114 | |
| 115 | static struct dm_exception_store_type _transient_compat_type = { |
| 116 | .name = "N", |
| 117 | .module = THIS_MODULE, |
| 118 | .ctr = transient_ctr, |
| 119 | .dtr = transient_dtr, |
| 120 | .read_metadata = transient_read_metadata, |
| 121 | .prepare_exception = transient_prepare_exception, |
| 122 | .commit_exception = transient_commit_exception, |
Mike Snitzer | 985903b | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 123 | .usage = transient_usage, |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 124 | .status = transient_status, |
| 125 | }; |
| 126 | |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 127 | int dm_transient_snapshot_init(void) |
| 128 | { |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 129 | int r; |
| 130 | |
| 131 | r = dm_exception_store_type_register(&_transient_type); |
| 132 | if (r) { |
| 133 | DMWARN("Unable to register transient exception store type"); |
| 134 | return r; |
| 135 | } |
| 136 | |
| 137 | r = dm_exception_store_type_register(&_transient_compat_type); |
| 138 | if (r) { |
| 139 | DMWARN("Unable to register old-style transient " |
| 140 | "exception store type"); |
| 141 | dm_exception_store_type_unregister(&_transient_type); |
| 142 | return r; |
| 143 | } |
| 144 | |
| 145 | return r; |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void dm_transient_snapshot_exit(void) |
| 149 | { |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 150 | dm_exception_store_type_unregister(&_transient_type); |
| 151 | dm_exception_store_type_unregister(&_transient_compat_type); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 152 | } |