blob: b4f05830af07657a8a7f7434f109ef459c5eb06e [file] [log] [blame]
Joe Thornber3241b1d2011-10-31 20:19:11 +00001/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6#include "dm-transaction-manager.h"
7#include "dm-space-map.h"
Joe Thornber3241b1d2011-10-31 20:19:11 +00008#include "dm-space-map-disk.h"
9#include "dm-space-map-metadata.h"
10#include "dm-persistent-data-internal.h"
11
Paul Gortmaker1944ce62011-09-28 18:29:32 -040012#include <linux/export.h>
Joe Thornber3241b1d2011-10-31 20:19:11 +000013#include <linux/slab.h>
14#include <linux/device-mapper.h>
15
16#define DM_MSG_PREFIX "transaction manager"
17
18/*----------------------------------------------------------------*/
19
20struct shadow_info {
21 struct hlist_node hlist;
22 dm_block_t where;
23};
24
25/*
26 * It would be nice if we scaled with the size of transaction.
27 */
28#define HASH_SIZE 256
29#define HASH_MASK (HASH_SIZE - 1)
30
31struct dm_transaction_manager {
32 int is_clone;
33 struct dm_transaction_manager *real;
34
35 struct dm_block_manager *bm;
36 struct dm_space_map *sm;
37
38 spinlock_t lock;
39 struct hlist_head buckets[HASH_SIZE];
40};
41
42/*----------------------------------------------------------------*/
43
44static int is_shadow(struct dm_transaction_manager *tm, dm_block_t b)
45{
46 int r = 0;
47 unsigned bucket = dm_hash_block(b, HASH_MASK);
48 struct shadow_info *si;
49 struct hlist_node *n;
50
51 spin_lock(&tm->lock);
52 hlist_for_each_entry(si, n, tm->buckets + bucket, hlist)
53 if (si->where == b) {
54 r = 1;
55 break;
56 }
57 spin_unlock(&tm->lock);
58
59 return r;
60}
61
62/*
63 * This can silently fail if there's no memory. We're ok with this since
64 * creating redundant shadows causes no harm.
65 */
66static void insert_shadow(struct dm_transaction_manager *tm, dm_block_t b)
67{
68 unsigned bucket;
69 struct shadow_info *si;
70
71 si = kmalloc(sizeof(*si), GFP_NOIO);
72 if (si) {
73 si->where = b;
74 bucket = dm_hash_block(b, HASH_MASK);
75 spin_lock(&tm->lock);
76 hlist_add_head(&si->hlist, tm->buckets + bucket);
77 spin_unlock(&tm->lock);
78 }
79}
80
81static void wipe_shadow_table(struct dm_transaction_manager *tm)
82{
83 struct shadow_info *si;
84 struct hlist_node *n, *tmp;
85 struct hlist_head *bucket;
86 int i;
87
88 spin_lock(&tm->lock);
89 for (i = 0; i < HASH_SIZE; i++) {
90 bucket = tm->buckets + i;
91 hlist_for_each_entry_safe(si, n, tmp, bucket, hlist)
92 kfree(si);
93
94 INIT_HLIST_HEAD(bucket);
95 }
96
97 spin_unlock(&tm->lock);
98}
99
100/*----------------------------------------------------------------*/
101
102static struct dm_transaction_manager *dm_tm_create(struct dm_block_manager *bm,
103 struct dm_space_map *sm)
104{
105 int i;
106 struct dm_transaction_manager *tm;
107
108 tm = kmalloc(sizeof(*tm), GFP_KERNEL);
109 if (!tm)
110 return ERR_PTR(-ENOMEM);
111
112 tm->is_clone = 0;
113 tm->real = NULL;
114 tm->bm = bm;
115 tm->sm = sm;
116
117 spin_lock_init(&tm->lock);
118 for (i = 0; i < HASH_SIZE; i++)
119 INIT_HLIST_HEAD(tm->buckets + i);
120
121 return tm;
122}
123
124struct dm_transaction_manager *dm_tm_create_non_blocking_clone(struct dm_transaction_manager *real)
125{
126 struct dm_transaction_manager *tm;
127
128 tm = kmalloc(sizeof(*tm), GFP_KERNEL);
129 if (tm) {
130 tm->is_clone = 1;
131 tm->real = real;
132 }
133
134 return tm;
135}
136EXPORT_SYMBOL_GPL(dm_tm_create_non_blocking_clone);
137
138void dm_tm_destroy(struct dm_transaction_manager *tm)
139{
Mike Snitzer25d7cd62012-07-03 12:55:33 +0100140 if (!tm->is_clone)
141 wipe_shadow_table(tm);
142
Joe Thornber3241b1d2011-10-31 20:19:11 +0000143 kfree(tm);
144}
145EXPORT_SYMBOL_GPL(dm_tm_destroy);
146
147int dm_tm_pre_commit(struct dm_transaction_manager *tm)
148{
149 int r;
150
151 if (tm->is_clone)
152 return -EWOULDBLOCK;
153
154 r = dm_sm_commit(tm->sm);
155 if (r < 0)
156 return r;
157
158 return 0;
159}
160EXPORT_SYMBOL_GPL(dm_tm_pre_commit);
161
162int dm_tm_commit(struct dm_transaction_manager *tm, struct dm_block *root)
163{
164 if (tm->is_clone)
165 return -EWOULDBLOCK;
166
167 wipe_shadow_table(tm);
168
169 return dm_bm_flush_and_unlock(tm->bm, root);
170}
171EXPORT_SYMBOL_GPL(dm_tm_commit);
172
173int dm_tm_new_block(struct dm_transaction_manager *tm,
174 struct dm_block_validator *v,
175 struct dm_block **result)
176{
177 int r;
178 dm_block_t new_block;
179
180 if (tm->is_clone)
181 return -EWOULDBLOCK;
182
183 r = dm_sm_new_block(tm->sm, &new_block);
184 if (r < 0)
185 return r;
186
187 r = dm_bm_write_lock_zero(tm->bm, new_block, v, result);
188 if (r < 0) {
189 dm_sm_dec_block(tm->sm, new_block);
190 return r;
191 }
192
193 /*
194 * New blocks count as shadows in that they don't need to be
195 * shadowed again.
196 */
197 insert_shadow(tm, new_block);
198
199 return 0;
200}
201
202static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
203 struct dm_block_validator *v,
204 struct dm_block **result)
205{
206 int r;
207 dm_block_t new;
208 struct dm_block *orig_block;
209
210 r = dm_sm_new_block(tm->sm, &new);
211 if (r < 0)
212 return r;
213
214 r = dm_sm_dec_block(tm->sm, orig);
215 if (r < 0)
216 return r;
217
218 r = dm_bm_read_lock(tm->bm, orig, v, &orig_block);
219 if (r < 0)
220 return r;
221
222 r = dm_bm_unlock_move(orig_block, new);
223 if (r < 0) {
224 dm_bm_unlock(orig_block);
225 return r;
226 }
227
228 return dm_bm_write_lock(tm->bm, new, v, result);
229}
230
231int dm_tm_shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
232 struct dm_block_validator *v, struct dm_block **result,
233 int *inc_children)
234{
235 int r;
236
237 if (tm->is_clone)
238 return -EWOULDBLOCK;
239
240 r = dm_sm_count_is_more_than_one(tm->sm, orig, inc_children);
241 if (r < 0)
242 return r;
243
244 if (is_shadow(tm, orig) && !*inc_children)
245 return dm_bm_write_lock(tm->bm, orig, v, result);
246
247 r = __shadow_block(tm, orig, v, result);
248 if (r < 0)
249 return r;
250 insert_shadow(tm, dm_block_location(*result));
251
252 return r;
253}
Joe Thornbercc8394d2012-06-03 00:30:01 +0100254EXPORT_SYMBOL_GPL(dm_tm_shadow_block);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000255
256int dm_tm_read_lock(struct dm_transaction_manager *tm, dm_block_t b,
257 struct dm_block_validator *v,
258 struct dm_block **blk)
259{
260 if (tm->is_clone)
261 return dm_bm_read_try_lock(tm->real->bm, b, v, blk);
262
263 return dm_bm_read_lock(tm->bm, b, v, blk);
264}
Joe Thornbercc8394d2012-06-03 00:30:01 +0100265EXPORT_SYMBOL_GPL(dm_tm_read_lock);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000266
267int dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b)
268{
269 return dm_bm_unlock(b);
270}
271EXPORT_SYMBOL_GPL(dm_tm_unlock);
272
273void dm_tm_inc(struct dm_transaction_manager *tm, dm_block_t b)
274{
275 /*
276 * The non-blocking clone doesn't support this.
277 */
278 BUG_ON(tm->is_clone);
279
280 dm_sm_inc_block(tm->sm, b);
281}
282EXPORT_SYMBOL_GPL(dm_tm_inc);
283
284void dm_tm_dec(struct dm_transaction_manager *tm, dm_block_t b)
285{
286 /*
287 * The non-blocking clone doesn't support this.
288 */
289 BUG_ON(tm->is_clone);
290
291 dm_sm_dec_block(tm->sm, b);
292}
293EXPORT_SYMBOL_GPL(dm_tm_dec);
294
295int dm_tm_ref(struct dm_transaction_manager *tm, dm_block_t b,
296 uint32_t *result)
297{
298 if (tm->is_clone)
299 return -EWOULDBLOCK;
300
301 return dm_sm_get_count(tm->sm, b, result);
302}
303
304struct dm_block_manager *dm_tm_get_bm(struct dm_transaction_manager *tm)
305{
306 return tm->bm;
307}
308
309/*----------------------------------------------------------------*/
310
311static int dm_tm_create_internal(struct dm_block_manager *bm,
312 dm_block_t sb_location,
Joe Thornber3241b1d2011-10-31 20:19:11 +0000313 struct dm_transaction_manager **tm,
314 struct dm_space_map **sm,
Joe Thornber384ef0e2012-07-27 15:08:09 +0100315 int create,
316 void *sm_root, size_t sm_len)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000317{
318 int r;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000319
Joe Thornber3caf6d72012-07-27 15:07:58 +0100320 *sm = dm_sm_metadata_init();
321 if (IS_ERR(*sm))
322 return PTR_ERR(*sm);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000323
Joe Thornber3caf6d72012-07-27 15:07:58 +0100324 *tm = dm_tm_create(bm, *sm);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000325 if (IS_ERR(*tm)) {
Joe Thornber3caf6d72012-07-27 15:07:58 +0100326 dm_sm_destroy(*sm);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000327 return PTR_ERR(*tm);
328 }
329
330 if (create) {
Joe Thornber3caf6d72012-07-27 15:07:58 +0100331 r = dm_sm_metadata_create(*sm, *tm, dm_bm_nr_blocks(bm),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000332 sb_location);
333 if (r) {
334 DMERR("couldn't create metadata space map");
Joe Thornber384ef0e2012-07-27 15:08:09 +0100335 goto bad;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000336 }
337
Joe Thornber3241b1d2011-10-31 20:19:11 +0000338 } else {
Joe Thornber384ef0e2012-07-27 15:08:09 +0100339 r = dm_sm_metadata_open(*sm, *tm, sm_root, sm_len);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000340 if (r) {
341 DMERR("couldn't open metadata space map");
Joe Thornber384ef0e2012-07-27 15:08:09 +0100342 goto bad;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000343 }
Joe Thornber3241b1d2011-10-31 20:19:11 +0000344 }
345
346 return 0;
347
Joe Thornber384ef0e2012-07-27 15:08:09 +0100348bad:
Joe Thornber3241b1d2011-10-31 20:19:11 +0000349 dm_tm_destroy(*tm);
Joe Thornber384ef0e2012-07-27 15:08:09 +0100350 dm_sm_destroy(*sm);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000351 return r;
352}
353
354int dm_tm_create_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
Joe Thornber3241b1d2011-10-31 20:19:11 +0000355 struct dm_transaction_manager **tm,
Joe Thornber384ef0e2012-07-27 15:08:09 +0100356 struct dm_space_map **sm)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000357{
Joe Thornber384ef0e2012-07-27 15:08:09 +0100358 return dm_tm_create_internal(bm, sb_location, tm, sm, 1, NULL, 0);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000359}
360EXPORT_SYMBOL_GPL(dm_tm_create_with_sm);
361
362int dm_tm_open_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
Joe Thornber384ef0e2012-07-27 15:08:09 +0100363 void *sm_root, size_t root_len,
Joe Thornber3241b1d2011-10-31 20:19:11 +0000364 struct dm_transaction_manager **tm,
Joe Thornber384ef0e2012-07-27 15:08:09 +0100365 struct dm_space_map **sm)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000366{
Joe Thornber384ef0e2012-07-27 15:08:09 +0100367 return dm_tm_create_internal(bm, sb_location, tm, sm, 0, sm_root, root_len);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000368}
369EXPORT_SYMBOL_GPL(dm_tm_open_with_sm);
370
371/*----------------------------------------------------------------*/