2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
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 * Space map interface.
22 * The low level disk format is written using the standard btree and
23 * transaction manager. This means that performing disk operations may
24 * cause us to recurse into the space map in order to allocate new blocks.
25 * For this reason we have a pool of pre-allocated blocks large enough to
26 * service any metadata_ll_disk operation.
30 * FIXME: we should calculate this based on the size of the device.
31 * Only the metadata space map needs this functionality.
33 #define MAX_RECURSIVE_ALLOCATIONS 1024
41 enum block_op_type type;
46 struct dm_space_map sm;
49 struct ll_disk old_ll;
53 unsigned recursion_count;
54 unsigned allocated_this_transaction;
55 unsigned nr_uncommitted;
56 struct block_op uncommitted[MAX_RECURSIVE_ALLOCATIONS];
59 static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
63 if (smm->nr_uncommitted == MAX_RECURSIVE_ALLOCATIONS) {
64 DMERR("too many recursive allocations");
68 op = smm->uncommitted + smm->nr_uncommitted++;
75 static int commit_bop(struct sm_metadata *smm, struct block_op *op)
78 enum allocation_event ev;
82 r = sm_ll_inc(&smm->ll, op->block, &ev);
86 r = sm_ll_dec(&smm->ll, op->block, &ev);
93 static void in(struct sm_metadata *smm)
95 smm->recursion_count++;
98 static int out(struct sm_metadata *smm)
103 * If we're not recursing then very bad things are happening.
105 if (!smm->recursion_count) {
106 DMERR("lost track of recursion depth");
110 if (smm->recursion_count == 1 && smm->nr_uncommitted) {
111 while (smm->nr_uncommitted && !r) {
112 smm->nr_uncommitted--;
113 r = commit_bop(smm, smm->uncommitted +
114 smm->nr_uncommitted);
120 smm->recursion_count--;
126 * When using the out() function above, we often want to combine an error
127 * code for the operation run in the recursive context with that from
130 static int combine_errors(int r1, int r2)
135 static int recursing(struct sm_metadata *smm)
137 return smm->recursion_count;
140 static void sm_metadata_destroy(struct dm_space_map *sm)
142 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
147 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
149 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
151 *count = smm->ll.nr_blocks;
156 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
158 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
160 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
161 smm->allocated_this_transaction;
166 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
170 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
171 unsigned adjustment = 0;
174 * We may have some uncommitted adjustments to add. This list
175 * should always be really short.
177 for (i = 0; i < smm->nr_uncommitted; i++) {
178 struct block_op *op = smm->uncommitted + i;
194 r = sm_ll_lookup(&smm->ll, b, result);
198 *result += adjustment;
203 static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
204 dm_block_t b, int *result)
206 int r, i, adjustment = 0;
207 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
211 * We may have some uncommitted adjustments to add. This list
212 * should always be really short.
214 for (i = 0; i < smm->nr_uncommitted; i++) {
215 struct block_op *op = smm->uncommitted + i;
231 if (adjustment > 1) {
236 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
242 * We err on the side of caution, and always return true.
246 *result = rc + adjustment > 1;
251 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
255 enum allocation_event ev;
256 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
258 if (smm->recursion_count) {
259 DMERR("cannot recurse set_count()");
264 r = sm_ll_insert(&smm->ll, b, count, &ev);
267 return combine_errors(r, r2);
270 static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
273 enum allocation_event ev;
274 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
277 r = add_bop(smm, BOP_INC, b);
280 r = sm_ll_inc(&smm->ll, b, &ev);
284 return combine_errors(r, r2);
287 static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
290 enum allocation_event ev;
291 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
294 r = add_bop(smm, BOP_DEC, b);
297 r = sm_ll_dec(&smm->ll, b, &ev);
301 return combine_errors(r, r2);
304 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
307 enum allocation_event ev;
308 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
310 r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
317 r = add_bop(smm, BOP_INC, *b);
320 r = sm_ll_inc(&smm->ll, *b, &ev);
325 smm->allocated_this_transaction++;
327 return combine_errors(r, r2);
330 static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
332 int r = sm_metadata_new_block_(sm, b);
334 DMERR("unable to allocate new metadata block");
338 static int sm_metadata_commit(struct dm_space_map *sm)
341 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
343 r = sm_ll_commit(&smm->ll);
347 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
349 smm->allocated_this_transaction = 0;
354 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
356 *result = sizeof(struct disk_sm_root);
361 static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
363 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
364 struct disk_sm_root root_le;
366 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
367 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
368 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
369 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
371 if (max < sizeof(root_le))
374 memcpy(where_le, &root_le, sizeof(root_le));
379 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
381 static struct dm_space_map ops = {
382 .destroy = sm_metadata_destroy,
383 .extend = sm_metadata_extend,
384 .get_nr_blocks = sm_metadata_get_nr_blocks,
385 .get_nr_free = sm_metadata_get_nr_free,
386 .get_count = sm_metadata_get_count,
387 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
388 .set_count = sm_metadata_set_count,
389 .inc_block = sm_metadata_inc_block,
390 .dec_block = sm_metadata_dec_block,
391 .new_block = sm_metadata_new_block,
392 .commit = sm_metadata_commit,
393 .root_size = sm_metadata_root_size,
394 .copy_root = sm_metadata_copy_root,
395 .register_threshold_callback = NULL
398 /*----------------------------------------------------------------*/
401 * When a new space map is created that manages its own space. We use
402 * this tiny bootstrap allocator.
404 static void sm_bootstrap_destroy(struct dm_space_map *sm)
408 static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
410 DMERR("bootstrap doesn't support extend");
415 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
417 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
419 return smm->ll.nr_blocks;
422 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
424 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
426 *count = smm->ll.nr_blocks - smm->begin;
431 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
434 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
436 return b < smm->begin ? 1 : 0;
439 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
440 dm_block_t b, int *result)
447 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
450 DMERR("bootstrap doesn't support set_count");
455 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
457 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
460 * We know the entire device is unused.
462 if (smm->begin == smm->ll.nr_blocks)
470 static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
472 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
474 return add_bop(smm, BOP_INC, b);
477 static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
479 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
481 return add_bop(smm, BOP_DEC, b);
484 static int sm_bootstrap_commit(struct dm_space_map *sm)
489 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
491 DMERR("bootstrap doesn't support root_size");
496 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
499 DMERR("bootstrap doesn't support copy_root");
504 static struct dm_space_map bootstrap_ops = {
505 .destroy = sm_bootstrap_destroy,
506 .extend = sm_bootstrap_extend,
507 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
508 .get_nr_free = sm_bootstrap_get_nr_free,
509 .get_count = sm_bootstrap_get_count,
510 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
511 .set_count = sm_bootstrap_set_count,
512 .inc_block = sm_bootstrap_inc_block,
513 .dec_block = sm_bootstrap_dec_block,
514 .new_block = sm_bootstrap_new_block,
515 .commit = sm_bootstrap_commit,
516 .root_size = sm_bootstrap_root_size,
517 .copy_root = sm_bootstrap_copy_root,
518 .register_threshold_callback = NULL
521 /*----------------------------------------------------------------*/
523 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
526 enum allocation_event ev;
527 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
528 dm_block_t old_len = smm->ll.nr_blocks;
531 * Flick into a mode where all blocks get allocated in the new area.
533 smm->begin = old_len;
534 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
539 r = sm_ll_extend(&smm->ll, extra_blocks);
542 * Switch back to normal behaviour.
544 memcpy(&smm->sm, &ops, sizeof(smm->sm));
545 for (i = old_len; !r && i < smm->begin; i++)
546 r = sm_ll_inc(&smm->ll, i, &ev);
551 /*----------------------------------------------------------------*/
553 struct dm_space_map *dm_sm_metadata_init(void)
555 struct sm_metadata *smm;
557 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
559 return ERR_PTR(-ENOMEM);
561 memcpy(&smm->sm, &ops, sizeof(smm->sm));
566 int dm_sm_metadata_create(struct dm_space_map *sm,
567 struct dm_transaction_manager *tm,
568 dm_block_t nr_blocks,
569 dm_block_t superblock)
573 enum allocation_event ev;
574 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
576 smm->begin = superblock + 1;
577 smm->recursion_count = 0;
578 smm->allocated_this_transaction = 0;
579 smm->nr_uncommitted = 0;
581 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
583 r = sm_ll_new_metadata(&smm->ll, tm);
587 r = sm_ll_extend(&smm->ll, nr_blocks);
591 memcpy(&smm->sm, &ops, sizeof(smm->sm));
594 * Now we need to update the newly created data structures with the
595 * allocated blocks that they were built from.
597 for (i = superblock; !r && i < smm->begin; i++)
598 r = sm_ll_inc(&smm->ll, i, &ev);
603 return sm_metadata_commit(sm);
606 int dm_sm_metadata_open(struct dm_space_map *sm,
607 struct dm_transaction_manager *tm,
608 void *root_le, size_t len)
611 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
613 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
618 smm->recursion_count = 0;
619 smm->allocated_this_transaction = 0;
620 smm->nr_uncommitted = 0;
622 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));