Commit | Line | Data |
---|---|---|
4db6bfe0 AK |
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" | |
4db6bfe0 AK |
9 | |
10 | #include <linux/mm.h> | |
11 | #include <linux/pagemap.h> | |
12 | #include <linux/vmalloc.h> | |
daaa5f7c | 13 | #include <linux/export.h> |
4db6bfe0 AK |
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 | ||
493df71c | 26 | static void transient_dtr(struct dm_exception_store *store) |
4db6bfe0 AK |
27 | { |
28 | kfree(store->context); | |
29 | } | |
30 | ||
a159c1ac JB |
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) | |
4db6bfe0 AK |
35 | { |
36 | return 0; | |
37 | } | |
38 | ||
a159c1ac | 39 | static int transient_prepare_exception(struct dm_exception_store *store, |
1d4989c8 | 40 | struct dm_exception *e) |
4db6bfe0 | 41 | { |
b2a11465 | 42 | struct transient_c *tc = store->context; |
fc56f6fb | 43 | sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev); |
4db6bfe0 | 44 | |
d0216849 | 45 | if (size < (tc->next_free + store->chunk_size)) |
4db6bfe0 AK |
46 | return -1; |
47 | ||
71fab00a | 48 | e->new_chunk = sector_to_chunk(store, tc->next_free); |
d0216849 | 49 | tc->next_free += store->chunk_size; |
4db6bfe0 AK |
50 | |
51 | return 0; | |
52 | } | |
53 | ||
a159c1ac | 54 | static void transient_commit_exception(struct dm_exception_store *store, |
385277bf | 55 | struct dm_exception *e, int valid, |
a159c1ac JB |
56 | void (*callback) (void *, int success), |
57 | void *callback_context) | |
4db6bfe0 AK |
58 | { |
59 | /* Just succeed */ | |
385277bf | 60 | callback(callback_context, valid); |
4db6bfe0 AK |
61 | } |
62 | ||
985903bb MS |
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) | |
4db6bfe0 | 67 | { |
985903bb | 68 | *sectors_allocated = ((struct transient_c *) store->context)->next_free; |
fc56f6fb | 69 | *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev); |
985903bb | 70 | *metadata_sectors = 0; |
4db6bfe0 AK |
71 | } |
72 | ||
b0d3cc01 | 73 | static int transient_ctr(struct dm_exception_store *store, char *options) |
4db6bfe0 AK |
74 | { |
75 | struct transient_c *tc; | |
76 | ||
4db6bfe0 AK |
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 | ||
1e302a92 JB |
87 | static unsigned transient_status(struct dm_exception_store *store, |
88 | status_type_t status, char *result, | |
89 | unsigned maxlen) | |
493df71c | 90 | { |
1e302a92 JB |
91 | unsigned sz = 0; |
92 | ||
93 | switch (status) { | |
94 | case STATUSTYPE_INFO: | |
95 | break; | |
96 | case STATUSTYPE_TABLE: | |
fc56f6fb | 97 | DMEMIT(" N %llu", (unsigned long long)store->chunk_size); |
8ec45662 TS |
98 | break; |
99 | case STATUSTYPE_IMA: | |
100 | *result = '\0'; | |
101 | break; | |
1e302a92 | 102 | } |
493df71c JB |
103 | |
104 | return sz; | |
105 | } | |
106 | ||
107 | static struct dm_exception_store_type _transient_type = { | |
108 | .name = "transient", | |
109 | .module = THIS_MODULE, | |
110 | .ctr = transient_ctr, | |
111 | .dtr = transient_dtr, | |
112 | .read_metadata = transient_read_metadata, | |
113 | .prepare_exception = transient_prepare_exception, | |
114 | .commit_exception = transient_commit_exception, | |
985903bb | 115 | .usage = transient_usage, |
493df71c JB |
116 | .status = transient_status, |
117 | }; | |
118 | ||
119 | static struct dm_exception_store_type _transient_compat_type = { | |
120 | .name = "N", | |
121 | .module = THIS_MODULE, | |
122 | .ctr = transient_ctr, | |
123 | .dtr = transient_dtr, | |
124 | .read_metadata = transient_read_metadata, | |
125 | .prepare_exception = transient_prepare_exception, | |
126 | .commit_exception = transient_commit_exception, | |
985903bb | 127 | .usage = transient_usage, |
493df71c JB |
128 | .status = transient_status, |
129 | }; | |
130 | ||
4db6bfe0 AK |
131 | int dm_transient_snapshot_init(void) |
132 | { | |
493df71c JB |
133 | int r; |
134 | ||
135 | r = dm_exception_store_type_register(&_transient_type); | |
136 | if (r) { | |
137 | DMWARN("Unable to register transient exception store type"); | |
138 | return r; | |
139 | } | |
140 | ||
141 | r = dm_exception_store_type_register(&_transient_compat_type); | |
142 | if (r) { | |
143 | DMWARN("Unable to register old-style transient " | |
144 | "exception store type"); | |
145 | dm_exception_store_type_unregister(&_transient_type); | |
146 | return r; | |
147 | } | |
148 | ||
149 | return r; | |
4db6bfe0 AK |
150 | } |
151 | ||
152 | void dm_transient_snapshot_exit(void) | |
153 | { | |
493df71c JB |
154 | dm_exception_store_type_unregister(&_transient_type); |
155 | dm_exception_store_type_unregister(&_transient_compat_type); | |
4db6bfe0 | 156 | } |