Commit | Line | Data |
---|---|---|
3bd94003 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
3241b1d3 JT |
2 | /* |
3 | * Copyright (C) 2011 Red Hat, Inc. | |
4 | * | |
5 | * This file is released under the GPL. | |
6 | */ | |
7 | #include "dm-transaction-manager.h" | |
8 | #include "dm-space-map.h" | |
3241b1d3 JT |
9 | #include "dm-space-map-disk.h" |
10 | #include "dm-space-map-metadata.h" | |
11 | #include "dm-persistent-data-internal.h" | |
12 | ||
1944ce60 | 13 | #include <linux/export.h> |
4646015d JT |
14 | #include <linux/mutex.h> |
15 | #include <linux/hash.h> | |
3241b1d3 JT |
16 | #include <linux/slab.h> |
17 | #include <linux/device-mapper.h> | |
18 | ||
19 | #define DM_MSG_PREFIX "transaction manager" | |
20 | ||
21 | /*----------------------------------------------------------------*/ | |
22 | ||
4646015d JT |
23 | #define PREFETCH_SIZE 128 |
24 | #define PREFETCH_BITS 7 | |
25 | #define PREFETCH_SENTINEL ((dm_block_t) -1ULL) | |
26 | ||
27 | struct prefetch_set { | |
28 | struct mutex lock; | |
29 | dm_block_t blocks[PREFETCH_SIZE]; | |
30 | }; | |
31 | ||
32 | static unsigned prefetch_hash(dm_block_t b) | |
33 | { | |
34 | return hash_64(b, PREFETCH_BITS); | |
35 | } | |
36 | ||
37 | static void prefetch_wipe(struct prefetch_set *p) | |
38 | { | |
39 | unsigned i; | |
40 | for (i = 0; i < PREFETCH_SIZE; i++) | |
41 | p->blocks[i] = PREFETCH_SENTINEL; | |
42 | } | |
43 | ||
44 | static void prefetch_init(struct prefetch_set *p) | |
45 | { | |
46 | mutex_init(&p->lock); | |
47 | prefetch_wipe(p); | |
48 | } | |
49 | ||
50 | static void prefetch_add(struct prefetch_set *p, dm_block_t b) | |
51 | { | |
52 | unsigned h = prefetch_hash(b); | |
53 | ||
54 | mutex_lock(&p->lock); | |
55 | if (p->blocks[h] == PREFETCH_SENTINEL) | |
56 | p->blocks[h] = b; | |
57 | ||
58 | mutex_unlock(&p->lock); | |
59 | } | |
60 | ||
61 | static void prefetch_issue(struct prefetch_set *p, struct dm_block_manager *bm) | |
62 | { | |
63 | unsigned i; | |
64 | ||
65 | mutex_lock(&p->lock); | |
66 | ||
67 | for (i = 0; i < PREFETCH_SIZE; i++) | |
68 | if (p->blocks[i] != PREFETCH_SENTINEL) { | |
69 | dm_bm_prefetch(bm, p->blocks[i]); | |
70 | p->blocks[i] = PREFETCH_SENTINEL; | |
71 | } | |
72 | ||
73 | mutex_unlock(&p->lock); | |
74 | } | |
75 | ||
76 | /*----------------------------------------------------------------*/ | |
77 | ||
3241b1d3 JT |
78 | struct shadow_info { |
79 | struct hlist_node hlist; | |
80 | dm_block_t where; | |
81 | }; | |
82 | ||
83 | /* | |
84 | * It would be nice if we scaled with the size of transaction. | |
85 | */ | |
df855798 AM |
86 | #define DM_HASH_SIZE 256 |
87 | #define DM_HASH_MASK (DM_HASH_SIZE - 1) | |
3241b1d3 JT |
88 | |
89 | struct dm_transaction_manager { | |
90 | int is_clone; | |
91 | struct dm_transaction_manager *real; | |
92 | ||
93 | struct dm_block_manager *bm; | |
94 | struct dm_space_map *sm; | |
95 | ||
96 | spinlock_t lock; | |
df855798 | 97 | struct hlist_head buckets[DM_HASH_SIZE]; |
4646015d JT |
98 | |
99 | struct prefetch_set prefetches; | |
3241b1d3 JT |
100 | }; |
101 | ||
102 | /*----------------------------------------------------------------*/ | |
103 | ||
104 | static int is_shadow(struct dm_transaction_manager *tm, dm_block_t b) | |
105 | { | |
106 | int r = 0; | |
df855798 | 107 | unsigned bucket = dm_hash_block(b, DM_HASH_MASK); |
3241b1d3 | 108 | struct shadow_info *si; |
3241b1d3 JT |
109 | |
110 | spin_lock(&tm->lock); | |
b67bfe0d | 111 | hlist_for_each_entry(si, tm->buckets + bucket, hlist) |
3241b1d3 JT |
112 | if (si->where == b) { |
113 | r = 1; | |
114 | break; | |
115 | } | |
116 | spin_unlock(&tm->lock); | |
117 | ||
118 | return r; | |
119 | } | |
120 | ||
121 | /* | |
122 | * This can silently fail if there's no memory. We're ok with this since | |
123 | * creating redundant shadows causes no harm. | |
124 | */ | |
125 | static void insert_shadow(struct dm_transaction_manager *tm, dm_block_t b) | |
126 | { | |
127 | unsigned bucket; | |
128 | struct shadow_info *si; | |
129 | ||
130 | si = kmalloc(sizeof(*si), GFP_NOIO); | |
131 | if (si) { | |
132 | si->where = b; | |
df855798 | 133 | bucket = dm_hash_block(b, DM_HASH_MASK); |
3241b1d3 JT |
134 | spin_lock(&tm->lock); |
135 | hlist_add_head(&si->hlist, tm->buckets + bucket); | |
136 | spin_unlock(&tm->lock); | |
137 | } | |
138 | } | |
139 | ||
140 | static void wipe_shadow_table(struct dm_transaction_manager *tm) | |
141 | { | |
142 | struct shadow_info *si; | |
b67bfe0d | 143 | struct hlist_node *tmp; |
3241b1d3 JT |
144 | struct hlist_head *bucket; |
145 | int i; | |
146 | ||
147 | spin_lock(&tm->lock); | |
df855798 | 148 | for (i = 0; i < DM_HASH_SIZE; i++) { |
3241b1d3 | 149 | bucket = tm->buckets + i; |
b67bfe0d | 150 | hlist_for_each_entry_safe(si, tmp, bucket, hlist) |
3241b1d3 JT |
151 | kfree(si); |
152 | ||
153 | INIT_HLIST_HEAD(bucket); | |
154 | } | |
155 | ||
156 | spin_unlock(&tm->lock); | |
157 | } | |
158 | ||
159 | /*----------------------------------------------------------------*/ | |
160 | ||
161 | static struct dm_transaction_manager *dm_tm_create(struct dm_block_manager *bm, | |
162 | struct dm_space_map *sm) | |
163 | { | |
164 | int i; | |
165 | struct dm_transaction_manager *tm; | |
166 | ||
167 | tm = kmalloc(sizeof(*tm), GFP_KERNEL); | |
168 | if (!tm) | |
169 | return ERR_PTR(-ENOMEM); | |
170 | ||
171 | tm->is_clone = 0; | |
172 | tm->real = NULL; | |
173 | tm->bm = bm; | |
174 | tm->sm = sm; | |
175 | ||
176 | spin_lock_init(&tm->lock); | |
df855798 | 177 | for (i = 0; i < DM_HASH_SIZE; i++) |
3241b1d3 JT |
178 | INIT_HLIST_HEAD(tm->buckets + i); |
179 | ||
4646015d JT |
180 | prefetch_init(&tm->prefetches); |
181 | ||
3241b1d3 JT |
182 | return tm; |
183 | } | |
184 | ||
185 | struct dm_transaction_manager *dm_tm_create_non_blocking_clone(struct dm_transaction_manager *real) | |
186 | { | |
187 | struct dm_transaction_manager *tm; | |
188 | ||
189 | tm = kmalloc(sizeof(*tm), GFP_KERNEL); | |
190 | if (tm) { | |
191 | tm->is_clone = 1; | |
192 | tm->real = real; | |
193 | } | |
194 | ||
195 | return tm; | |
196 | } | |
197 | EXPORT_SYMBOL_GPL(dm_tm_create_non_blocking_clone); | |
198 | ||
199 | void dm_tm_destroy(struct dm_transaction_manager *tm) | |
200 | { | |
25d7cd6f MS |
201 | if (!tm->is_clone) |
202 | wipe_shadow_table(tm); | |
203 | ||
3241b1d3 JT |
204 | kfree(tm); |
205 | } | |
206 | EXPORT_SYMBOL_GPL(dm_tm_destroy); | |
207 | ||
208 | int dm_tm_pre_commit(struct dm_transaction_manager *tm) | |
209 | { | |
210 | int r; | |
211 | ||
212 | if (tm->is_clone) | |
213 | return -EWOULDBLOCK; | |
214 | ||
215 | r = dm_sm_commit(tm->sm); | |
216 | if (r < 0) | |
217 | return r; | |
218 | ||
a9d45396 | 219 | return dm_bm_flush(tm->bm); |
3241b1d3 JT |
220 | } |
221 | EXPORT_SYMBOL_GPL(dm_tm_pre_commit); | |
222 | ||
223 | int dm_tm_commit(struct dm_transaction_manager *tm, struct dm_block *root) | |
224 | { | |
225 | if (tm->is_clone) | |
226 | return -EWOULDBLOCK; | |
227 | ||
228 | wipe_shadow_table(tm); | |
a9d45396 | 229 | dm_bm_unlock(root); |
3241b1d3 | 230 | |
a9d45396 | 231 | return dm_bm_flush(tm->bm); |
3241b1d3 JT |
232 | } |
233 | EXPORT_SYMBOL_GPL(dm_tm_commit); | |
234 | ||
235 | int dm_tm_new_block(struct dm_transaction_manager *tm, | |
236 | struct dm_block_validator *v, | |
237 | struct dm_block **result) | |
238 | { | |
239 | int r; | |
240 | dm_block_t new_block; | |
241 | ||
242 | if (tm->is_clone) | |
243 | return -EWOULDBLOCK; | |
244 | ||
245 | r = dm_sm_new_block(tm->sm, &new_block); | |
246 | if (r < 0) | |
247 | return r; | |
248 | ||
249 | r = dm_bm_write_lock_zero(tm->bm, new_block, v, result); | |
250 | if (r < 0) { | |
251 | dm_sm_dec_block(tm->sm, new_block); | |
252 | return r; | |
253 | } | |
254 | ||
255 | /* | |
256 | * New blocks count as shadows in that they don't need to be | |
257 | * shadowed again. | |
258 | */ | |
259 | insert_shadow(tm, new_block); | |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
264 | static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig, | |
265 | struct dm_block_validator *v, | |
266 | struct dm_block **result) | |
267 | { | |
268 | int r; | |
269 | dm_block_t new; | |
270 | struct dm_block *orig_block; | |
271 | ||
272 | r = dm_sm_new_block(tm->sm, &new); | |
273 | if (r < 0) | |
274 | return r; | |
275 | ||
276 | r = dm_sm_dec_block(tm->sm, orig); | |
277 | if (r < 0) | |
278 | return r; | |
279 | ||
280 | r = dm_bm_read_lock(tm->bm, orig, v, &orig_block); | |
281 | if (r < 0) | |
282 | return r; | |
283 | ||
3c9ad9bd JT |
284 | /* |
285 | * It would be tempting to use dm_bm_unlock_move here, but some | |
286 | * code, such as the space maps, keeps using the old data structures | |
287 | * secure in the knowledge they won't be changed until the next | |
288 | * transaction. Using unlock_move would force a synchronous read | |
289 | * since the old block would no longer be in the cache. | |
290 | */ | |
291 | r = dm_bm_write_lock_zero(tm->bm, new, v, result); | |
292 | if (r) { | |
3241b1d3 JT |
293 | dm_bm_unlock(orig_block); |
294 | return r; | |
295 | } | |
296 | ||
3c9ad9bd JT |
297 | memcpy(dm_block_data(*result), dm_block_data(orig_block), |
298 | dm_bm_block_size(tm->bm)); | |
299 | ||
300 | dm_bm_unlock(orig_block); | |
301 | return r; | |
3241b1d3 JT |
302 | } |
303 | ||
304 | int dm_tm_shadow_block(struct dm_transaction_manager *tm, dm_block_t orig, | |
305 | struct dm_block_validator *v, struct dm_block **result, | |
306 | int *inc_children) | |
307 | { | |
308 | int r; | |
309 | ||
310 | if (tm->is_clone) | |
311 | return -EWOULDBLOCK; | |
312 | ||
313 | r = dm_sm_count_is_more_than_one(tm->sm, orig, inc_children); | |
314 | if (r < 0) | |
315 | return r; | |
316 | ||
317 | if (is_shadow(tm, orig) && !*inc_children) | |
318 | return dm_bm_write_lock(tm->bm, orig, v, result); | |
319 | ||
320 | r = __shadow_block(tm, orig, v, result); | |
321 | if (r < 0) | |
322 | return r; | |
323 | insert_shadow(tm, dm_block_location(*result)); | |
324 | ||
325 | return r; | |
326 | } | |
cc8394d8 | 327 | EXPORT_SYMBOL_GPL(dm_tm_shadow_block); |
3241b1d3 JT |
328 | |
329 | int dm_tm_read_lock(struct dm_transaction_manager *tm, dm_block_t b, | |
330 | struct dm_block_validator *v, | |
331 | struct dm_block **blk) | |
332 | { | |
4646015d JT |
333 | if (tm->is_clone) { |
334 | int r = dm_bm_read_try_lock(tm->real->bm, b, v, blk); | |
335 | ||
336 | if (r == -EWOULDBLOCK) | |
337 | prefetch_add(&tm->real->prefetches, b); | |
338 | ||
339 | return r; | |
340 | } | |
3241b1d3 JT |
341 | |
342 | return dm_bm_read_lock(tm->bm, b, v, blk); | |
343 | } | |
cc8394d8 | 344 | EXPORT_SYMBOL_GPL(dm_tm_read_lock); |
3241b1d3 | 345 | |
4c7da06f | 346 | void dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b) |
3241b1d3 | 347 | { |
4c7da06f | 348 | dm_bm_unlock(b); |
3241b1d3 JT |
349 | } |
350 | EXPORT_SYMBOL_GPL(dm_tm_unlock); | |
351 | ||
352 | void dm_tm_inc(struct dm_transaction_manager *tm, dm_block_t b) | |
353 | { | |
354 | /* | |
355 | * The non-blocking clone doesn't support this. | |
356 | */ | |
357 | BUG_ON(tm->is_clone); | |
358 | ||
359 | dm_sm_inc_block(tm->sm, b); | |
360 | } | |
361 | EXPORT_SYMBOL_GPL(dm_tm_inc); | |
362 | ||
be500ed7 JT |
363 | void dm_tm_inc_range(struct dm_transaction_manager *tm, dm_block_t b, dm_block_t e) |
364 | { | |
365 | /* | |
366 | * The non-blocking clone doesn't support this. | |
367 | */ | |
368 | BUG_ON(tm->is_clone); | |
369 | ||
370 | dm_sm_inc_blocks(tm->sm, b, e); | |
371 | } | |
372 | EXPORT_SYMBOL_GPL(dm_tm_inc_range); | |
373 | ||
3241b1d3 JT |
374 | void dm_tm_dec(struct dm_transaction_manager *tm, dm_block_t b) |
375 | { | |
376 | /* | |
377 | * The non-blocking clone doesn't support this. | |
378 | */ | |
379 | BUG_ON(tm->is_clone); | |
380 | ||
381 | dm_sm_dec_block(tm->sm, b); | |
382 | } | |
383 | EXPORT_SYMBOL_GPL(dm_tm_dec); | |
384 | ||
be500ed7 JT |
385 | void dm_tm_dec_range(struct dm_transaction_manager *tm, dm_block_t b, dm_block_t e) |
386 | { | |
387 | /* | |
388 | * The non-blocking clone doesn't support this. | |
389 | */ | |
390 | BUG_ON(tm->is_clone); | |
391 | ||
392 | dm_sm_dec_blocks(tm->sm, b, e); | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(dm_tm_dec_range); | |
395 | ||
396 | void dm_tm_with_runs(struct dm_transaction_manager *tm, | |
397 | const __le64 *value_le, unsigned count, dm_tm_run_fn fn) | |
398 | { | |
399 | uint64_t b, begin, end; | |
400 | bool in_run = false; | |
401 | unsigned i; | |
402 | ||
403 | for (i = 0; i < count; i++, value_le++) { | |
404 | b = le64_to_cpu(*value_le); | |
405 | ||
406 | if (in_run) { | |
407 | if (b == end) | |
408 | end++; | |
409 | else { | |
410 | fn(tm, begin, end); | |
411 | begin = b; | |
412 | end = b + 1; | |
413 | } | |
414 | } else { | |
415 | in_run = true; | |
416 | begin = b; | |
417 | end = b + 1; | |
418 | } | |
419 | } | |
420 | ||
421 | if (in_run) | |
422 | fn(tm, begin, end); | |
423 | } | |
424 | EXPORT_SYMBOL_GPL(dm_tm_with_runs); | |
425 | ||
3241b1d3 JT |
426 | int dm_tm_ref(struct dm_transaction_manager *tm, dm_block_t b, |
427 | uint32_t *result) | |
428 | { | |
429 | if (tm->is_clone) | |
430 | return -EWOULDBLOCK; | |
431 | ||
432 | return dm_sm_get_count(tm->sm, b, result); | |
433 | } | |
434 | ||
4eafdb15 JT |
435 | int dm_tm_block_is_shared(struct dm_transaction_manager *tm, dm_block_t b, |
436 | int *result) | |
437 | { | |
438 | if (tm->is_clone) | |
439 | return -EWOULDBLOCK; | |
440 | ||
441 | return dm_sm_count_is_more_than_one(tm->sm, b, result); | |
442 | } | |
443 | ||
3241b1d3 JT |
444 | struct dm_block_manager *dm_tm_get_bm(struct dm_transaction_manager *tm) |
445 | { | |
446 | return tm->bm; | |
447 | } | |
448 | ||
4646015d JT |
449 | void dm_tm_issue_prefetches(struct dm_transaction_manager *tm) |
450 | { | |
451 | prefetch_issue(&tm->prefetches, tm->bm); | |
452 | } | |
453 | EXPORT_SYMBOL_GPL(dm_tm_issue_prefetches); | |
454 | ||
3241b1d3 JT |
455 | /*----------------------------------------------------------------*/ |
456 | ||
457 | static int dm_tm_create_internal(struct dm_block_manager *bm, | |
458 | dm_block_t sb_location, | |
3241b1d3 JT |
459 | struct dm_transaction_manager **tm, |
460 | struct dm_space_map **sm, | |
384ef0e6 JT |
461 | int create, |
462 | void *sm_root, size_t sm_len) | |
3241b1d3 JT |
463 | { |
464 | int r; | |
3241b1d3 | 465 | |
3caf6d73 JT |
466 | *sm = dm_sm_metadata_init(); |
467 | if (IS_ERR(*sm)) | |
468 | return PTR_ERR(*sm); | |
3241b1d3 | 469 | |
3caf6d73 | 470 | *tm = dm_tm_create(bm, *sm); |
3241b1d3 | 471 | if (IS_ERR(*tm)) { |
3caf6d73 | 472 | dm_sm_destroy(*sm); |
3241b1d3 JT |
473 | return PTR_ERR(*tm); |
474 | } | |
475 | ||
476 | if (create) { | |
3caf6d73 | 477 | r = dm_sm_metadata_create(*sm, *tm, dm_bm_nr_blocks(bm), |
3241b1d3 JT |
478 | sb_location); |
479 | if (r) { | |
480 | DMERR("couldn't create metadata space map"); | |
384ef0e6 | 481 | goto bad; |
3241b1d3 JT |
482 | } |
483 | ||
3241b1d3 | 484 | } else { |
384ef0e6 | 485 | r = dm_sm_metadata_open(*sm, *tm, sm_root, sm_len); |
3241b1d3 JT |
486 | if (r) { |
487 | DMERR("couldn't open metadata space map"); | |
384ef0e6 | 488 | goto bad; |
3241b1d3 | 489 | } |
3241b1d3 JT |
490 | } |
491 | ||
492 | return 0; | |
493 | ||
384ef0e6 | 494 | bad: |
3241b1d3 | 495 | dm_tm_destroy(*tm); |
384ef0e6 | 496 | dm_sm_destroy(*sm); |
3241b1d3 JT |
497 | return r; |
498 | } | |
499 | ||
500 | int dm_tm_create_with_sm(struct dm_block_manager *bm, dm_block_t sb_location, | |
3241b1d3 | 501 | struct dm_transaction_manager **tm, |
384ef0e6 | 502 | struct dm_space_map **sm) |
3241b1d3 | 503 | { |
384ef0e6 | 504 | return dm_tm_create_internal(bm, sb_location, tm, sm, 1, NULL, 0); |
3241b1d3 JT |
505 | } |
506 | EXPORT_SYMBOL_GPL(dm_tm_create_with_sm); | |
507 | ||
508 | int dm_tm_open_with_sm(struct dm_block_manager *bm, dm_block_t sb_location, | |
384ef0e6 | 509 | void *sm_root, size_t root_len, |
3241b1d3 | 510 | struct dm_transaction_manager **tm, |
384ef0e6 | 511 | struct dm_space_map **sm) |
3241b1d3 | 512 | { |
384ef0e6 | 513 | return dm_tm_create_internal(bm, sb_location, tm, sm, 0, sm_root, root_len); |
3241b1d3 JT |
514 | } |
515 | EXPORT_SYMBOL_GPL(dm_tm_open_with_sm); | |
516 | ||
517 | /*----------------------------------------------------------------*/ |