2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
7 #include "dm-space-map-common.h"
8 #include "dm-space-map-disk.h"
9 #include "dm-space-map.h"
10 #include "dm-transaction-manager.h"
12 #include <linux/list.h>
13 #include <linux/slab.h>
14 #include <linux/export.h>
15 #include <linux/device-mapper.h>
17 #define DM_MSG_PREFIX "space map disk"
19 /*----------------------------------------------------------------*/
22 * Space map interface.
25 struct dm_space_map sm;
28 struct ll_disk old_ll;
31 dm_block_t nr_allocated_this_transaction;
34 static void sm_disk_destroy(struct dm_space_map *sm)
36 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
41 static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
43 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
45 return sm_ll_extend(&smd->ll, extra_blocks);
48 static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
50 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
51 *count = smd->old_ll.nr_blocks;
56 static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
58 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
59 *count = (smd->old_ll.nr_blocks - smd->old_ll.nr_allocated) - smd->nr_allocated_this_transaction;
64 static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
67 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
68 return sm_ll_lookup(&smd->ll, b, result);
71 static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
77 r = sm_disk_get_count(sm, b, &count);
86 static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
90 int32_t nr_allocations;
91 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
93 r = sm_ll_insert(&smd->ll, b, count, &nr_allocations);
95 smd->nr_allocated_this_transaction += nr_allocations;
101 static int sm_disk_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
104 int32_t nr_allocations;
105 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
107 r = sm_ll_inc(&smd->ll, b, e, &nr_allocations);
109 smd->nr_allocated_this_transaction += nr_allocations;
114 static int sm_disk_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
117 int32_t nr_allocations;
118 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
120 r = sm_ll_dec(&smd->ll, b, e, &nr_allocations);
122 smd->nr_allocated_this_transaction += nr_allocations;
127 static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
130 int32_t nr_allocations;
131 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
134 * Any block we allocate has to be free in both the old and current ll.
136 r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
139 * There's no free block between smd->begin and the end of the metadata device.
140 * We search before smd->begin in case something has been freed.
142 r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, 0, smd->begin, b);
149 r = sm_ll_inc(&smd->ll, *b, *b + 1, &nr_allocations);
151 smd->nr_allocated_this_transaction += nr_allocations;
157 static int sm_disk_commit(struct dm_space_map *sm)
160 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
162 r = sm_ll_commit(&smd->ll);
166 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
167 smd->nr_allocated_this_transaction = 0;
172 static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
174 *result = sizeof(struct disk_sm_root);
179 static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
181 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
182 struct disk_sm_root root_le;
184 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
185 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
186 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
187 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
189 if (max < sizeof(root_le))
192 memcpy(where_le, &root_le, sizeof(root_le));
197 /*----------------------------------------------------------------*/
199 static struct dm_space_map ops = {
200 .destroy = sm_disk_destroy,
201 .extend = sm_disk_extend,
202 .get_nr_blocks = sm_disk_get_nr_blocks,
203 .get_nr_free = sm_disk_get_nr_free,
204 .get_count = sm_disk_get_count,
205 .count_is_more_than_one = sm_disk_count_is_more_than_one,
206 .set_count = sm_disk_set_count,
207 .inc_blocks = sm_disk_inc_blocks,
208 .dec_blocks = sm_disk_dec_blocks,
209 .new_block = sm_disk_new_block,
210 .commit = sm_disk_commit,
211 .root_size = sm_disk_root_size,
212 .copy_root = sm_disk_copy_root,
213 .register_threshold_callback = NULL
216 struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
217 dm_block_t nr_blocks)
222 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
224 return ERR_PTR(-ENOMEM);
227 smd->nr_allocated_this_transaction = 0;
228 memcpy(&smd->sm, &ops, sizeof(smd->sm));
230 r = sm_ll_new_disk(&smd->ll, tm);
234 r = sm_ll_extend(&smd->ll, nr_blocks);
238 r = sm_disk_commit(&smd->sm);
248 EXPORT_SYMBOL_GPL(dm_sm_disk_create);
250 struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
251 void *root_le, size_t len)
256 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
258 return ERR_PTR(-ENOMEM);
261 smd->nr_allocated_this_transaction = 0;
262 memcpy(&smd->sm, &ops, sizeof(smd->sm));
264 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
268 r = sm_disk_commit(&smd->sm);
278 EXPORT_SYMBOL_GPL(dm_sm_disk_open);
280 /*----------------------------------------------------------------*/