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,
91 enum allocation_event ev;
92 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
94 r = sm_ll_insert(&smd->ll, b, count, &ev);
102 * This _must_ be free in the prior transaction
103 * otherwise we've lost atomicity.
105 smd->nr_allocated_this_transaction++;
110 * It's only free if it's also free in the last
113 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
118 smd->nr_allocated_this_transaction--;
126 static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
129 enum allocation_event ev;
130 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
132 r = sm_ll_inc(&smd->ll, b, &ev);
133 if (!r && (ev == SM_ALLOC))
135 * This _must_ be free in the prior transaction
136 * otherwise we've lost atomicity.
138 smd->nr_allocated_this_transaction++;
143 static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
147 enum allocation_event ev;
148 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
150 r = sm_ll_dec(&smd->ll, b, &ev);
151 if (!r && (ev == SM_FREE)) {
153 * It's only free if it's also free in the last
156 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
157 if (!r && !old_count)
158 smd->nr_allocated_this_transaction--;
164 static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
167 enum allocation_event ev;
168 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
171 * Any block we allocate has to be free in both the old and current ll.
173 r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
178 r = sm_ll_inc(&smd->ll, *b, &ev);
180 BUG_ON(ev != SM_ALLOC);
181 smd->nr_allocated_this_transaction++;
187 static int sm_disk_commit(struct dm_space_map *sm)
190 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
192 r = sm_ll_commit(&smd->ll);
196 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
198 smd->nr_allocated_this_transaction = 0;
203 static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
205 *result = sizeof(struct disk_sm_root);
210 static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
212 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
213 struct disk_sm_root root_le;
215 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
216 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
217 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
218 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
220 if (max < sizeof(root_le))
223 memcpy(where_le, &root_le, sizeof(root_le));
228 /*----------------------------------------------------------------*/
230 static struct dm_space_map ops = {
231 .destroy = sm_disk_destroy,
232 .extend = sm_disk_extend,
233 .get_nr_blocks = sm_disk_get_nr_blocks,
234 .get_nr_free = sm_disk_get_nr_free,
235 .get_count = sm_disk_get_count,
236 .count_is_more_than_one = sm_disk_count_is_more_than_one,
237 .set_count = sm_disk_set_count,
238 .inc_block = sm_disk_inc_block,
239 .dec_block = sm_disk_dec_block,
240 .new_block = sm_disk_new_block,
241 .commit = sm_disk_commit,
242 .root_size = sm_disk_root_size,
243 .copy_root = sm_disk_copy_root,
244 .register_threshold_callback = NULL
247 struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
248 dm_block_t nr_blocks)
253 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
255 return ERR_PTR(-ENOMEM);
258 smd->nr_allocated_this_transaction = 0;
259 memcpy(&smd->sm, &ops, sizeof(smd->sm));
261 r = sm_ll_new_disk(&smd->ll, tm);
265 r = sm_ll_extend(&smd->ll, nr_blocks);
269 r = sm_disk_commit(&smd->sm);
279 EXPORT_SYMBOL_GPL(dm_sm_disk_create);
281 struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
282 void *root_le, size_t len)
287 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
289 return ERR_PTR(-ENOMEM);
292 smd->nr_allocated_this_transaction = 0;
293 memcpy(&smd->sm, &ops, sizeof(smd->sm));
295 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
299 r = sm_disk_commit(&smd->sm);
309 EXPORT_SYMBOL_GPL(dm_sm_disk_open);
311 /*----------------------------------------------------------------*/