Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / md / persistent-data / dm-space-map-disk.c
blob32adf6b4a9c7097e12796f8b396f80bfc8867c0a
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
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.
24 struct sm_disk {
25 struct dm_space_map sm;
27 struct ll_disk ll;
28 struct ll_disk old_ll;
30 dm_block_t begin;
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);
38 kfree(smd);
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;
53 return 0;
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;
61 return 0;
64 static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
65 uint32_t *result)
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,
72 int *result)
74 int r;
75 uint32_t count;
77 r = sm_disk_get_count(sm, b, &count);
78 if (r)
79 return r;
81 *result = count > 1;
83 return 0;
86 static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
87 uint32_t count)
89 int r;
90 uint32_t old_count;
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);
95 if (!r) {
96 switch (ev) {
97 case SM_NONE:
98 break;
100 case SM_ALLOC:
102 * This _must_ be free in the prior transaction
103 * otherwise we've lost atomicity.
105 smd->nr_allocated_this_transaction++;
106 break;
108 case SM_FREE:
110 * It's only free if it's also free in the last
111 * transaction.
113 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
114 if (r)
115 return r;
117 if (!old_count)
118 smd->nr_allocated_this_transaction--;
119 break;
123 return r;
126 static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
128 int r;
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++;
140 return r;
143 static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
145 int r;
146 uint32_t old_count;
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
154 * transaction.
156 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
157 if (!r && !old_count)
158 smd->nr_allocated_this_transaction--;
161 return r;
164 static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
166 int r;
167 enum allocation_event ev;
168 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
170 /* FIXME: we should loop round a couple of times */
171 r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
172 if (r)
173 return r;
175 smd->begin = *b + 1;
176 r = sm_ll_inc(&smd->ll, *b, &ev);
177 if (!r) {
178 BUG_ON(ev != SM_ALLOC);
179 smd->nr_allocated_this_transaction++;
182 return r;
185 static int sm_disk_commit(struct dm_space_map *sm)
187 int r;
188 dm_block_t nr_free;
189 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
191 r = sm_disk_get_nr_free(sm, &nr_free);
192 if (r)
193 return r;
195 r = sm_ll_commit(&smd->ll);
196 if (r)
197 return r;
199 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
200 smd->begin = 0;
201 smd->nr_allocated_this_transaction = 0;
203 r = sm_disk_get_nr_free(sm, &nr_free);
204 if (r)
205 return r;
207 return 0;
210 static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
212 *result = sizeof(struct disk_sm_root);
214 return 0;
217 static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
219 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
220 struct disk_sm_root root_le;
222 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
223 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
224 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
225 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
227 if (max < sizeof(root_le))
228 return -ENOSPC;
230 memcpy(where_le, &root_le, sizeof(root_le));
232 return 0;
235 /*----------------------------------------------------------------*/
237 static struct dm_space_map ops = {
238 .destroy = sm_disk_destroy,
239 .extend = sm_disk_extend,
240 .get_nr_blocks = sm_disk_get_nr_blocks,
241 .get_nr_free = sm_disk_get_nr_free,
242 .get_count = sm_disk_get_count,
243 .count_is_more_than_one = sm_disk_count_is_more_than_one,
244 .set_count = sm_disk_set_count,
245 .inc_block = sm_disk_inc_block,
246 .dec_block = sm_disk_dec_block,
247 .new_block = sm_disk_new_block,
248 .commit = sm_disk_commit,
249 .root_size = sm_disk_root_size,
250 .copy_root = sm_disk_copy_root,
251 .register_threshold_callback = NULL
254 struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
255 dm_block_t nr_blocks)
257 int r;
258 struct sm_disk *smd;
260 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
261 if (!smd)
262 return ERR_PTR(-ENOMEM);
264 smd->begin = 0;
265 smd->nr_allocated_this_transaction = 0;
266 memcpy(&smd->sm, &ops, sizeof(smd->sm));
268 r = sm_ll_new_disk(&smd->ll, tm);
269 if (r)
270 goto bad;
272 r = sm_ll_extend(&smd->ll, nr_blocks);
273 if (r)
274 goto bad;
276 r = sm_disk_commit(&smd->sm);
277 if (r)
278 goto bad;
280 return &smd->sm;
282 bad:
283 kfree(smd);
284 return ERR_PTR(r);
286 EXPORT_SYMBOL_GPL(dm_sm_disk_create);
288 struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
289 void *root_le, size_t len)
291 int r;
292 struct sm_disk *smd;
294 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
295 if (!smd)
296 return ERR_PTR(-ENOMEM);
298 smd->begin = 0;
299 smd->nr_allocated_this_transaction = 0;
300 memcpy(&smd->sm, &ops, sizeof(smd->sm));
302 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
303 if (r)
304 goto bad;
306 r = sm_disk_commit(&smd->sm);
307 if (r)
308 goto bad;
310 return &smd->sm;
312 bad:
313 kfree(smd);
314 return ERR_PTR(r);
316 EXPORT_SYMBOL_GPL(dm_sm_disk_open);
318 /*----------------------------------------------------------------*/