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