x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / md / persistent-data / dm-space-map-metadata.c
blob579b58200bf2696c5d2f9089e32f69012f1e301b
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
7 #include "dm-space-map.h"
8 #include "dm-space-map-common.h"
9 #include "dm-space-map-metadata.h"
11 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/device-mapper.h>
15 #define DM_MSG_PREFIX "space map metadata"
17 /*----------------------------------------------------------------*/
20 * An edge triggered threshold.
22 struct threshold {
23 bool threshold_set;
24 bool value_set;
25 dm_block_t threshold;
26 dm_block_t current_value;
27 dm_sm_threshold_fn fn;
28 void *context;
31 static void threshold_init(struct threshold *t)
33 t->threshold_set = false;
34 t->value_set = false;
37 static void set_threshold(struct threshold *t, dm_block_t value,
38 dm_sm_threshold_fn fn, void *context)
40 t->threshold_set = true;
41 t->threshold = value;
42 t->fn = fn;
43 t->context = context;
46 static bool below_threshold(struct threshold *t, dm_block_t value)
48 return t->threshold_set && value <= t->threshold;
51 static bool threshold_already_triggered(struct threshold *t)
53 return t->value_set && below_threshold(t, t->current_value);
56 static void check_threshold(struct threshold *t, dm_block_t value)
58 if (below_threshold(t, value) &&
59 !threshold_already_triggered(t))
60 t->fn(t->context);
62 t->value_set = true;
63 t->current_value = value;
66 /*----------------------------------------------------------------*/
69 * Space map interface.
71 * The low level disk format is written using the standard btree and
72 * transaction manager. This means that performing disk operations may
73 * cause us to recurse into the space map in order to allocate new blocks.
74 * For this reason we have a pool of pre-allocated blocks large enough to
75 * service any metadata_ll_disk operation.
79 * FIXME: we should calculate this based on the size of the device.
80 * Only the metadata space map needs this functionality.
82 #define MAX_RECURSIVE_ALLOCATIONS 1024
84 enum block_op_type {
85 BOP_INC,
86 BOP_DEC
89 struct block_op {
90 enum block_op_type type;
91 dm_block_t block;
94 struct bop_ring_buffer {
95 unsigned begin;
96 unsigned end;
97 struct block_op bops[MAX_RECURSIVE_ALLOCATIONS + 1];
100 static void brb_init(struct bop_ring_buffer *brb)
102 brb->begin = 0;
103 brb->end = 0;
106 static bool brb_empty(struct bop_ring_buffer *brb)
108 return brb->begin == brb->end;
111 static unsigned brb_next(struct bop_ring_buffer *brb, unsigned old)
113 unsigned r = old + 1;
114 return (r >= (sizeof(brb->bops) / sizeof(*brb->bops))) ? 0 : r;
117 static int brb_push(struct bop_ring_buffer *brb,
118 enum block_op_type type, dm_block_t b)
120 struct block_op *bop;
121 unsigned next = brb_next(brb, brb->end);
124 * We don't allow the last bop to be filled, this way we can
125 * differentiate between full and empty.
127 if (next == brb->begin)
128 return -ENOMEM;
130 bop = brb->bops + brb->end;
131 bop->type = type;
132 bop->block = b;
134 brb->end = next;
136 return 0;
139 static int brb_pop(struct bop_ring_buffer *brb, struct block_op *result)
141 struct block_op *bop;
143 if (brb_empty(brb))
144 return -ENODATA;
146 bop = brb->bops + brb->begin;
147 result->type = bop->type;
148 result->block = bop->block;
150 brb->begin = brb_next(brb, brb->begin);
152 return 0;
155 /*----------------------------------------------------------------*/
157 struct sm_metadata {
158 struct dm_space_map sm;
160 struct ll_disk ll;
161 struct ll_disk old_ll;
163 dm_block_t begin;
165 unsigned recursion_count;
166 unsigned allocated_this_transaction;
167 struct bop_ring_buffer uncommitted;
169 struct threshold threshold;
172 static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
174 int r = brb_push(&smm->uncommitted, type, b);
176 if (r) {
177 DMERR("too many recursive allocations");
178 return -ENOMEM;
181 return 0;
184 static int commit_bop(struct sm_metadata *smm, struct block_op *op)
186 int r = 0;
187 enum allocation_event ev;
189 switch (op->type) {
190 case BOP_INC:
191 r = sm_ll_inc(&smm->ll, op->block, &ev);
192 break;
194 case BOP_DEC:
195 r = sm_ll_dec(&smm->ll, op->block, &ev);
196 break;
199 return r;
202 static void in(struct sm_metadata *smm)
204 smm->recursion_count++;
207 static int out(struct sm_metadata *smm)
209 int r = 0;
212 * If we're not recursing then very bad things are happening.
214 if (!smm->recursion_count) {
215 DMERR("lost track of recursion depth");
216 return -ENOMEM;
219 if (smm->recursion_count == 1) {
220 while (!brb_empty(&smm->uncommitted)) {
221 struct block_op bop;
223 r = brb_pop(&smm->uncommitted, &bop);
224 if (r) {
225 DMERR("bug in bop ring buffer");
226 break;
229 r = commit_bop(smm, &bop);
230 if (r)
231 break;
235 smm->recursion_count--;
237 return r;
241 * When using the out() function above, we often want to combine an error
242 * code for the operation run in the recursive context with that from
243 * out().
245 static int combine_errors(int r1, int r2)
247 return r1 ? r1 : r2;
250 static int recursing(struct sm_metadata *smm)
252 return smm->recursion_count;
255 static void sm_metadata_destroy(struct dm_space_map *sm)
257 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
259 kfree(smm);
262 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
264 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
266 *count = smm->ll.nr_blocks;
268 return 0;
271 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
273 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
275 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
276 smm->allocated_this_transaction;
278 return 0;
281 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
282 uint32_t *result)
284 int r;
285 unsigned i;
286 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
287 unsigned adjustment = 0;
290 * We may have some uncommitted adjustments to add. This list
291 * should always be really short.
293 for (i = smm->uncommitted.begin;
294 i != smm->uncommitted.end;
295 i = brb_next(&smm->uncommitted, i)) {
296 struct block_op *op = smm->uncommitted.bops + i;
298 if (op->block != b)
299 continue;
301 switch (op->type) {
302 case BOP_INC:
303 adjustment++;
304 break;
306 case BOP_DEC:
307 adjustment--;
308 break;
312 r = sm_ll_lookup(&smm->ll, b, result);
313 if (r)
314 return r;
316 *result += adjustment;
318 return 0;
321 static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
322 dm_block_t b, int *result)
324 int r, adjustment = 0;
325 unsigned i;
326 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
327 uint32_t rc;
330 * We may have some uncommitted adjustments to add. This list
331 * should always be really short.
333 for (i = smm->uncommitted.begin;
334 i != smm->uncommitted.end;
335 i = brb_next(&smm->uncommitted, i)) {
337 struct block_op *op = smm->uncommitted.bops + i;
339 if (op->block != b)
340 continue;
342 switch (op->type) {
343 case BOP_INC:
344 adjustment++;
345 break;
347 case BOP_DEC:
348 adjustment--;
349 break;
353 if (adjustment > 1) {
354 *result = 1;
355 return 0;
358 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
359 if (r)
360 return r;
362 if (rc == 3)
364 * We err on the side of caution, and always return true.
366 *result = 1;
367 else
368 *result = rc + adjustment > 1;
370 return 0;
373 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
374 uint32_t count)
376 int r, r2;
377 enum allocation_event ev;
378 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
380 if (smm->recursion_count) {
381 DMERR("cannot recurse set_count()");
382 return -EINVAL;
385 in(smm);
386 r = sm_ll_insert(&smm->ll, b, count, &ev);
387 r2 = out(smm);
389 return combine_errors(r, r2);
392 static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
394 int r, r2 = 0;
395 enum allocation_event ev;
396 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
398 if (recursing(smm))
399 r = add_bop(smm, BOP_INC, b);
400 else {
401 in(smm);
402 r = sm_ll_inc(&smm->ll, b, &ev);
403 r2 = out(smm);
406 return combine_errors(r, r2);
409 static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
411 int r, r2 = 0;
412 enum allocation_event ev;
413 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
415 if (recursing(smm))
416 r = add_bop(smm, BOP_DEC, b);
417 else {
418 in(smm);
419 r = sm_ll_dec(&smm->ll, b, &ev);
420 r2 = out(smm);
423 return combine_errors(r, r2);
426 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
428 int r, r2 = 0;
429 enum allocation_event ev;
430 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
432 r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
433 if (r)
434 return r;
436 smm->begin = *b + 1;
438 if (recursing(smm))
439 r = add_bop(smm, BOP_INC, *b);
440 else {
441 in(smm);
442 r = sm_ll_inc(&smm->ll, *b, &ev);
443 r2 = out(smm);
446 if (!r)
447 smm->allocated_this_transaction++;
449 return combine_errors(r, r2);
452 static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
454 dm_block_t count;
455 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
457 int r = sm_metadata_new_block_(sm, b);
458 if (r) {
459 DMERR("unable to allocate new metadata block");
460 return r;
463 r = sm_metadata_get_nr_free(sm, &count);
464 if (r) {
465 DMERR("couldn't get free block count");
466 return r;
469 check_threshold(&smm->threshold, count);
471 return r;
474 static int sm_metadata_commit(struct dm_space_map *sm)
476 int r;
477 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
479 r = sm_ll_commit(&smm->ll);
480 if (r)
481 return r;
483 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
484 smm->begin = 0;
485 smm->allocated_this_transaction = 0;
487 return 0;
490 static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
491 dm_block_t threshold,
492 dm_sm_threshold_fn fn,
493 void *context)
495 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
497 set_threshold(&smm->threshold, threshold, fn, context);
499 return 0;
502 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
504 *result = sizeof(struct disk_sm_root);
506 return 0;
509 static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
511 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
512 struct disk_sm_root root_le;
514 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
515 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
516 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
517 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
519 if (max < sizeof(root_le))
520 return -ENOSPC;
522 memcpy(where_le, &root_le, sizeof(root_le));
524 return 0;
527 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
529 static struct dm_space_map ops = {
530 .destroy = sm_metadata_destroy,
531 .extend = sm_metadata_extend,
532 .get_nr_blocks = sm_metadata_get_nr_blocks,
533 .get_nr_free = sm_metadata_get_nr_free,
534 .get_count = sm_metadata_get_count,
535 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
536 .set_count = sm_metadata_set_count,
537 .inc_block = sm_metadata_inc_block,
538 .dec_block = sm_metadata_dec_block,
539 .new_block = sm_metadata_new_block,
540 .commit = sm_metadata_commit,
541 .root_size = sm_metadata_root_size,
542 .copy_root = sm_metadata_copy_root,
543 .register_threshold_callback = sm_metadata_register_threshold_callback
546 /*----------------------------------------------------------------*/
549 * When a new space map is created that manages its own space. We use
550 * this tiny bootstrap allocator.
552 static void sm_bootstrap_destroy(struct dm_space_map *sm)
556 static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
558 DMERR("bootstrap doesn't support extend");
560 return -EINVAL;
563 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
565 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
567 return smm->ll.nr_blocks;
570 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
572 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
574 *count = smm->ll.nr_blocks - smm->begin;
576 return 0;
579 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
580 uint32_t *result)
582 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
584 return b < smm->begin ? 1 : 0;
587 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
588 dm_block_t b, int *result)
590 *result = 0;
592 return 0;
595 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
596 uint32_t count)
598 DMERR("bootstrap doesn't support set_count");
600 return -EINVAL;
603 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
605 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
608 * We know the entire device is unused.
610 if (smm->begin == smm->ll.nr_blocks)
611 return -ENOSPC;
613 *b = smm->begin++;
615 return 0;
618 static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
620 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
622 return add_bop(smm, BOP_INC, b);
625 static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
627 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
629 return add_bop(smm, BOP_DEC, b);
632 static int sm_bootstrap_commit(struct dm_space_map *sm)
634 return 0;
637 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
639 DMERR("bootstrap doesn't support root_size");
641 return -EINVAL;
644 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
645 size_t max)
647 DMERR("bootstrap doesn't support copy_root");
649 return -EINVAL;
652 static struct dm_space_map bootstrap_ops = {
653 .destroy = sm_bootstrap_destroy,
654 .extend = sm_bootstrap_extend,
655 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
656 .get_nr_free = sm_bootstrap_get_nr_free,
657 .get_count = sm_bootstrap_get_count,
658 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
659 .set_count = sm_bootstrap_set_count,
660 .inc_block = sm_bootstrap_inc_block,
661 .dec_block = sm_bootstrap_dec_block,
662 .new_block = sm_bootstrap_new_block,
663 .commit = sm_bootstrap_commit,
664 .root_size = sm_bootstrap_root_size,
665 .copy_root = sm_bootstrap_copy_root,
666 .register_threshold_callback = NULL
669 /*----------------------------------------------------------------*/
671 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
673 int r, i;
674 enum allocation_event ev;
675 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
676 dm_block_t old_len = smm->ll.nr_blocks;
679 * Flick into a mode where all blocks get allocated in the new area.
681 smm->begin = old_len;
682 memcpy(sm, &bootstrap_ops, sizeof(*sm));
685 * Extend.
687 r = sm_ll_extend(&smm->ll, extra_blocks);
688 if (r)
689 goto out;
692 * We repeatedly increment then commit until the commit doesn't
693 * allocate any new blocks.
695 do {
696 for (i = old_len; !r && i < smm->begin; i++) {
697 r = sm_ll_inc(&smm->ll, i, &ev);
698 if (r)
699 goto out;
701 old_len = smm->begin;
703 r = sm_ll_commit(&smm->ll);
704 if (r)
705 goto out;
707 } while (old_len != smm->begin);
709 out:
711 * Switch back to normal behaviour.
713 memcpy(sm, &ops, sizeof(*sm));
714 return r;
717 /*----------------------------------------------------------------*/
719 struct dm_space_map *dm_sm_metadata_init(void)
721 struct sm_metadata *smm;
723 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
724 if (!smm)
725 return ERR_PTR(-ENOMEM);
727 memcpy(&smm->sm, &ops, sizeof(smm->sm));
729 return &smm->sm;
732 int dm_sm_metadata_create(struct dm_space_map *sm,
733 struct dm_transaction_manager *tm,
734 dm_block_t nr_blocks,
735 dm_block_t superblock)
737 int r;
738 dm_block_t i;
739 enum allocation_event ev;
740 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
742 smm->begin = superblock + 1;
743 smm->recursion_count = 0;
744 smm->allocated_this_transaction = 0;
745 brb_init(&smm->uncommitted);
746 threshold_init(&smm->threshold);
748 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
750 r = sm_ll_new_metadata(&smm->ll, tm);
751 if (r)
752 return r;
754 r = sm_ll_extend(&smm->ll, nr_blocks);
755 if (r)
756 return r;
758 memcpy(&smm->sm, &ops, sizeof(smm->sm));
761 * Now we need to update the newly created data structures with the
762 * allocated blocks that they were built from.
764 for (i = superblock; !r && i < smm->begin; i++)
765 r = sm_ll_inc(&smm->ll, i, &ev);
767 if (r)
768 return r;
770 return sm_metadata_commit(sm);
773 int dm_sm_metadata_open(struct dm_space_map *sm,
774 struct dm_transaction_manager *tm,
775 void *root_le, size_t len)
777 int r;
778 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
780 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
781 if (r)
782 return r;
784 smm->begin = 0;
785 smm->recursion_count = 0;
786 smm->allocated_this_transaction = 0;
787 brb_init(&smm->uncommitted);
788 threshold_init(&smm->threshold);
790 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
791 return 0;