ocfs2: fix several issues of append dio
[linux/fpc-iii.git] / drivers / md / persistent-data / dm-space-map-metadata.c
blob53091295fce9b7d9ffec9b91a6a0bdddd20ed67f
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 apply_bops(struct sm_metadata *smm)
209 int r = 0;
211 while (!brb_empty(&smm->uncommitted)) {
212 struct block_op bop;
214 r = brb_pop(&smm->uncommitted, &bop);
215 if (r) {
216 DMERR("bug in bop ring buffer");
217 break;
220 r = commit_bop(smm, &bop);
221 if (r)
222 break;
225 return r;
228 static int out(struct sm_metadata *smm)
230 int r = 0;
233 * If we're not recursing then very bad things are happening.
235 if (!smm->recursion_count) {
236 DMERR("lost track of recursion depth");
237 return -ENOMEM;
240 if (smm->recursion_count == 1)
241 apply_bops(smm);
243 smm->recursion_count--;
245 return r;
249 * When using the out() function above, we often want to combine an error
250 * code for the operation run in the recursive context with that from
251 * out().
253 static int combine_errors(int r1, int r2)
255 return r1 ? r1 : r2;
258 static int recursing(struct sm_metadata *smm)
260 return smm->recursion_count;
263 static void sm_metadata_destroy(struct dm_space_map *sm)
265 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
267 kfree(smm);
270 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
272 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
274 *count = smm->ll.nr_blocks;
276 return 0;
279 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
281 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
283 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
284 smm->allocated_this_transaction;
286 return 0;
289 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
290 uint32_t *result)
292 int r;
293 unsigned i;
294 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
295 unsigned adjustment = 0;
298 * We may have some uncommitted adjustments to add. This list
299 * should always be really short.
301 for (i = smm->uncommitted.begin;
302 i != smm->uncommitted.end;
303 i = brb_next(&smm->uncommitted, i)) {
304 struct block_op *op = smm->uncommitted.bops + i;
306 if (op->block != b)
307 continue;
309 switch (op->type) {
310 case BOP_INC:
311 adjustment++;
312 break;
314 case BOP_DEC:
315 adjustment--;
316 break;
320 r = sm_ll_lookup(&smm->ll, b, result);
321 if (r)
322 return r;
324 *result += adjustment;
326 return 0;
329 static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
330 dm_block_t b, int *result)
332 int r, adjustment = 0;
333 unsigned i;
334 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
335 uint32_t rc;
338 * We may have some uncommitted adjustments to add. This list
339 * should always be really short.
341 for (i = smm->uncommitted.begin;
342 i != smm->uncommitted.end;
343 i = brb_next(&smm->uncommitted, i)) {
345 struct block_op *op = smm->uncommitted.bops + i;
347 if (op->block != b)
348 continue;
350 switch (op->type) {
351 case BOP_INC:
352 adjustment++;
353 break;
355 case BOP_DEC:
356 adjustment--;
357 break;
361 if (adjustment > 1) {
362 *result = 1;
363 return 0;
366 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
367 if (r)
368 return r;
370 if (rc == 3)
372 * We err on the side of caution, and always return true.
374 *result = 1;
375 else
376 *result = rc + adjustment > 1;
378 return 0;
381 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
382 uint32_t count)
384 int r, r2;
385 enum allocation_event ev;
386 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
388 if (smm->recursion_count) {
389 DMERR("cannot recurse set_count()");
390 return -EINVAL;
393 in(smm);
394 r = sm_ll_insert(&smm->ll, b, count, &ev);
395 r2 = out(smm);
397 return combine_errors(r, r2);
400 static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
402 int r, r2 = 0;
403 enum allocation_event ev;
404 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
406 if (recursing(smm))
407 r = add_bop(smm, BOP_INC, b);
408 else {
409 in(smm);
410 r = sm_ll_inc(&smm->ll, b, &ev);
411 r2 = out(smm);
414 return combine_errors(r, r2);
417 static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
419 int r, r2 = 0;
420 enum allocation_event ev;
421 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
423 if (recursing(smm))
424 r = add_bop(smm, BOP_DEC, b);
425 else {
426 in(smm);
427 r = sm_ll_dec(&smm->ll, b, &ev);
428 r2 = out(smm);
431 return combine_errors(r, r2);
434 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
436 int r, r2 = 0;
437 enum allocation_event ev;
438 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
440 r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
441 if (r)
442 return r;
444 smm->begin = *b + 1;
446 if (recursing(smm))
447 r = add_bop(smm, BOP_INC, *b);
448 else {
449 in(smm);
450 r = sm_ll_inc(&smm->ll, *b, &ev);
451 r2 = out(smm);
454 if (!r)
455 smm->allocated_this_transaction++;
457 return combine_errors(r, r2);
460 static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
462 dm_block_t count;
463 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
465 int r = sm_metadata_new_block_(sm, b);
466 if (r) {
467 DMERR_LIMIT("unable to allocate new metadata block");
468 return r;
471 r = sm_metadata_get_nr_free(sm, &count);
472 if (r) {
473 DMERR_LIMIT("couldn't get free block count");
474 return r;
477 check_threshold(&smm->threshold, count);
479 return r;
482 static int sm_metadata_commit(struct dm_space_map *sm)
484 int r;
485 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
487 r = sm_ll_commit(&smm->ll);
488 if (r)
489 return r;
491 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
492 smm->begin = 0;
493 smm->allocated_this_transaction = 0;
495 return 0;
498 static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
499 dm_block_t threshold,
500 dm_sm_threshold_fn fn,
501 void *context)
503 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
505 set_threshold(&smm->threshold, threshold, fn, context);
507 return 0;
510 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
512 *result = sizeof(struct disk_sm_root);
514 return 0;
517 static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
519 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
520 struct disk_sm_root root_le;
522 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
523 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
524 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
525 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
527 if (max < sizeof(root_le))
528 return -ENOSPC;
530 memcpy(where_le, &root_le, sizeof(root_le));
532 return 0;
535 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
537 static struct dm_space_map ops = {
538 .destroy = sm_metadata_destroy,
539 .extend = sm_metadata_extend,
540 .get_nr_blocks = sm_metadata_get_nr_blocks,
541 .get_nr_free = sm_metadata_get_nr_free,
542 .get_count = sm_metadata_get_count,
543 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
544 .set_count = sm_metadata_set_count,
545 .inc_block = sm_metadata_inc_block,
546 .dec_block = sm_metadata_dec_block,
547 .new_block = sm_metadata_new_block,
548 .commit = sm_metadata_commit,
549 .root_size = sm_metadata_root_size,
550 .copy_root = sm_metadata_copy_root,
551 .register_threshold_callback = sm_metadata_register_threshold_callback
554 /*----------------------------------------------------------------*/
557 * When a new space map is created that manages its own space. We use
558 * this tiny bootstrap allocator.
560 static void sm_bootstrap_destroy(struct dm_space_map *sm)
564 static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
566 DMERR("bootstrap doesn't support extend");
568 return -EINVAL;
571 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
573 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
575 *count = smm->ll.nr_blocks;
577 return 0;
580 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
582 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
584 *count = smm->ll.nr_blocks - smm->begin;
586 return 0;
589 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
590 uint32_t *result)
592 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
594 *result = (b < smm->begin) ? 1 : 0;
596 return 0;
599 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
600 dm_block_t b, int *result)
602 *result = 0;
604 return 0;
607 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
608 uint32_t count)
610 DMERR("bootstrap doesn't support set_count");
612 return -EINVAL;
615 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
617 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
620 * We know the entire device is unused.
622 if (smm->begin == smm->ll.nr_blocks)
623 return -ENOSPC;
625 *b = smm->begin++;
627 return 0;
630 static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
632 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
634 return add_bop(smm, BOP_INC, b);
637 static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
639 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
641 return add_bop(smm, BOP_DEC, b);
644 static int sm_bootstrap_commit(struct dm_space_map *sm)
646 return 0;
649 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
651 DMERR("bootstrap doesn't support root_size");
653 return -EINVAL;
656 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
657 size_t max)
659 DMERR("bootstrap doesn't support copy_root");
661 return -EINVAL;
664 static struct dm_space_map bootstrap_ops = {
665 .destroy = sm_bootstrap_destroy,
666 .extend = sm_bootstrap_extend,
667 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
668 .get_nr_free = sm_bootstrap_get_nr_free,
669 .get_count = sm_bootstrap_get_count,
670 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
671 .set_count = sm_bootstrap_set_count,
672 .inc_block = sm_bootstrap_inc_block,
673 .dec_block = sm_bootstrap_dec_block,
674 .new_block = sm_bootstrap_new_block,
675 .commit = sm_bootstrap_commit,
676 .root_size = sm_bootstrap_root_size,
677 .copy_root = sm_bootstrap_copy_root,
678 .register_threshold_callback = NULL
681 /*----------------------------------------------------------------*/
683 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
685 int r, i;
686 enum allocation_event ev;
687 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
688 dm_block_t old_len = smm->ll.nr_blocks;
691 * Flick into a mode where all blocks get allocated in the new area.
693 smm->begin = old_len;
694 memcpy(sm, &bootstrap_ops, sizeof(*sm));
697 * Extend.
699 r = sm_ll_extend(&smm->ll, extra_blocks);
700 if (r)
701 goto out;
704 * We repeatedly increment then commit until the commit doesn't
705 * allocate any new blocks.
707 do {
708 for (i = old_len; !r && i < smm->begin; i++) {
709 r = sm_ll_inc(&smm->ll, i, &ev);
710 if (r)
711 goto out;
713 old_len = smm->begin;
715 r = apply_bops(smm);
716 if (r) {
717 DMERR("%s: apply_bops failed", __func__);
718 goto out;
721 r = sm_ll_commit(&smm->ll);
722 if (r)
723 goto out;
725 } while (old_len != smm->begin);
727 out:
729 * Switch back to normal behaviour.
731 memcpy(sm, &ops, sizeof(*sm));
732 return r;
735 /*----------------------------------------------------------------*/
737 struct dm_space_map *dm_sm_metadata_init(void)
739 struct sm_metadata *smm;
741 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
742 if (!smm)
743 return ERR_PTR(-ENOMEM);
745 memcpy(&smm->sm, &ops, sizeof(smm->sm));
747 return &smm->sm;
750 int dm_sm_metadata_create(struct dm_space_map *sm,
751 struct dm_transaction_manager *tm,
752 dm_block_t nr_blocks,
753 dm_block_t superblock)
755 int r;
756 dm_block_t i;
757 enum allocation_event ev;
758 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
760 smm->begin = superblock + 1;
761 smm->recursion_count = 0;
762 smm->allocated_this_transaction = 0;
763 brb_init(&smm->uncommitted);
764 threshold_init(&smm->threshold);
766 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
768 r = sm_ll_new_metadata(&smm->ll, tm);
769 if (r)
770 return r;
772 if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
773 nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
774 r = sm_ll_extend(&smm->ll, nr_blocks);
775 if (r)
776 return r;
778 memcpy(&smm->sm, &ops, sizeof(smm->sm));
781 * Now we need to update the newly created data structures with the
782 * allocated blocks that they were built from.
784 for (i = superblock; !r && i < smm->begin; i++)
785 r = sm_ll_inc(&smm->ll, i, &ev);
787 if (r)
788 return r;
790 r = apply_bops(smm);
791 if (r) {
792 DMERR("%s: apply_bops failed", __func__);
793 return r;
796 return sm_metadata_commit(sm);
799 int dm_sm_metadata_open(struct dm_space_map *sm,
800 struct dm_transaction_manager *tm,
801 void *root_le, size_t len)
803 int r;
804 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
806 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
807 if (r)
808 return r;
810 smm->begin = 0;
811 smm->recursion_count = 0;
812 smm->allocated_this_transaction = 0;
813 brb_init(&smm->uncommitted);
814 threshold_init(&smm->threshold);
816 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
817 return 0;