Linux 3.12.39
[linux/fpc-iii.git] / drivers / md / persistent-data / dm-space-map-disk.c
blobe735a6d5a793dfff9c71330fd7555469d5a58501
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 return count > 1;
84 static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
85 uint32_t count)
87 int r;
88 uint32_t old_count;
89 enum allocation_event ev;
90 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
92 r = sm_ll_insert(&smd->ll, b, count, &ev);
93 if (!r) {
94 switch (ev) {
95 case SM_NONE:
96 break;
98 case SM_ALLOC:
100 * This _must_ be free in the prior transaction
101 * otherwise we've lost atomicity.
103 smd->nr_allocated_this_transaction++;
104 break;
106 case SM_FREE:
108 * It's only free if it's also free in the last
109 * transaction.
111 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
112 if (r)
113 return r;
115 if (!old_count)
116 smd->nr_allocated_this_transaction--;
117 break;
121 return r;
124 static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
126 int r;
127 enum allocation_event ev;
128 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
130 r = sm_ll_inc(&smd->ll, b, &ev);
131 if (!r && (ev == SM_ALLOC))
133 * This _must_ be free in the prior transaction
134 * otherwise we've lost atomicity.
136 smd->nr_allocated_this_transaction++;
138 return r;
141 static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
143 int r;
144 uint32_t old_count;
145 enum allocation_event ev;
146 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
148 r = sm_ll_dec(&smd->ll, b, &ev);
149 if (!r && (ev == SM_FREE)) {
151 * It's only free if it's also free in the last
152 * transaction.
154 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
155 if (r)
156 return r;
158 if (!old_count)
159 smd->nr_allocated_this_transaction--;
162 return r;
165 static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
167 int r;
168 enum allocation_event ev;
169 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
171 /* FIXME: we should loop round a couple of times */
172 r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
173 if (r)
174 return r;
176 smd->begin = *b + 1;
177 r = sm_ll_inc(&smd->ll, *b, &ev);
178 if (!r) {
179 BUG_ON(ev != SM_ALLOC);
180 smd->nr_allocated_this_transaction++;
183 return r;
186 static int sm_disk_commit(struct dm_space_map *sm)
188 int r;
189 dm_block_t nr_free;
190 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
192 r = sm_disk_get_nr_free(sm, &nr_free);
193 if (r)
194 return r;
196 r = sm_ll_commit(&smd->ll);
197 if (r)
198 return r;
200 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
201 smd->begin = 0;
202 smd->nr_allocated_this_transaction = 0;
204 r = sm_disk_get_nr_free(sm, &nr_free);
205 if (r)
206 return r;
208 return 0;
211 static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
213 *result = sizeof(struct disk_sm_root);
215 return 0;
218 static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
220 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
221 struct disk_sm_root root_le;
223 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
224 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
225 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
226 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
228 if (max < sizeof(root_le))
229 return -ENOSPC;
231 memcpy(where_le, &root_le, sizeof(root_le));
233 return 0;
236 /*----------------------------------------------------------------*/
238 static struct dm_space_map ops = {
239 .destroy = sm_disk_destroy,
240 .extend = sm_disk_extend,
241 .get_nr_blocks = sm_disk_get_nr_blocks,
242 .get_nr_free = sm_disk_get_nr_free,
243 .get_count = sm_disk_get_count,
244 .count_is_more_than_one = sm_disk_count_is_more_than_one,
245 .set_count = sm_disk_set_count,
246 .inc_block = sm_disk_inc_block,
247 .dec_block = sm_disk_dec_block,
248 .new_block = sm_disk_new_block,
249 .commit = sm_disk_commit,
250 .root_size = sm_disk_root_size,
251 .copy_root = sm_disk_copy_root,
252 .register_threshold_callback = NULL
255 struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
256 dm_block_t nr_blocks)
258 int r;
259 struct sm_disk *smd;
261 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
262 if (!smd)
263 return ERR_PTR(-ENOMEM);
265 smd->begin = 0;
266 smd->nr_allocated_this_transaction = 0;
267 memcpy(&smd->sm, &ops, sizeof(smd->sm));
269 r = sm_ll_new_disk(&smd->ll, tm);
270 if (r)
271 goto bad;
273 r = sm_ll_extend(&smd->ll, nr_blocks);
274 if (r)
275 goto bad;
277 r = sm_disk_commit(&smd->sm);
278 if (r)
279 goto bad;
281 return &smd->sm;
283 bad:
284 kfree(smd);
285 return ERR_PTR(r);
287 EXPORT_SYMBOL_GPL(dm_sm_disk_create);
289 struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
290 void *root_le, size_t len)
292 int r;
293 struct sm_disk *smd;
295 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
296 if (!smd)
297 return ERR_PTR(-ENOMEM);
299 smd->begin = 0;
300 smd->nr_allocated_this_transaction = 0;
301 memcpy(&smd->sm, &ops, sizeof(smd->sm));
303 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
304 if (r)
305 goto bad;
307 r = sm_disk_commit(&smd->sm);
308 if (r)
309 goto bad;
311 return &smd->sm;
313 bad:
314 kfree(smd);
315 return ERR_PTR(r);
317 EXPORT_SYMBOL_GPL(dm_sm_disk_open);
319 /*----------------------------------------------------------------*/