USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / md / persistent-data / dm-space-map-disk.c
blob3d0ed53328831627ab6ec79cb85946f36b0fe7a4
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
7 #include "dm-space-map-checker.h"
8 #include "dm-space-map-common.h"
9 #include "dm-space-map-disk.h"
10 #include "dm-space-map.h"
11 #include "dm-transaction-manager.h"
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/device-mapper.h>
18 #define DM_MSG_PREFIX "space map disk"
20 /*----------------------------------------------------------------*/
23 * Space map interface.
25 struct sm_disk {
26 struct dm_space_map sm;
28 struct ll_disk ll;
29 struct ll_disk old_ll;
31 dm_block_t begin;
32 dm_block_t nr_allocated_this_transaction;
35 static void sm_disk_destroy(struct dm_space_map *sm)
37 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
39 kfree(smd);
42 static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
44 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
46 return sm_ll_extend(&smd->ll, extra_blocks);
49 static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
51 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
52 *count = smd->old_ll.nr_blocks;
54 return 0;
57 static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
59 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
60 *count = (smd->old_ll.nr_blocks - smd->old_ll.nr_allocated) - smd->nr_allocated_this_transaction;
62 return 0;
65 static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
66 uint32_t *result)
68 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
69 return sm_ll_lookup(&smd->ll, b, result);
72 static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
73 int *result)
75 int r;
76 uint32_t count;
78 r = sm_disk_get_count(sm, b, &count);
79 if (r)
80 return r;
82 return count > 1;
85 static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
86 uint32_t count)
88 int r;
89 uint32_t old_count;
90 enum allocation_event ev;
91 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
93 r = sm_ll_insert(&smd->ll, b, count, &ev);
94 if (!r) {
95 switch (ev) {
96 case SM_NONE:
97 break;
99 case SM_ALLOC:
101 * This _must_ be free in the prior transaction
102 * otherwise we've lost atomicity.
104 smd->nr_allocated_this_transaction++;
105 break;
107 case SM_FREE:
109 * It's only free if it's also free in the last
110 * transaction.
112 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
113 if (r)
114 return r;
116 if (!old_count)
117 smd->nr_allocated_this_transaction--;
118 break;
122 return r;
125 static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
127 int r;
128 enum allocation_event ev;
129 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
131 r = sm_ll_inc(&smd->ll, b, &ev);
132 if (!r && (ev == SM_ALLOC))
134 * This _must_ be free in the prior transaction
135 * otherwise we've lost atomicity.
137 smd->nr_allocated_this_transaction++;
139 return r;
142 static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
144 int r;
145 uint32_t old_count;
146 enum allocation_event ev;
147 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
149 r = sm_ll_dec(&smd->ll, b, &ev);
150 if (!r && (ev == SM_FREE)) {
152 * It's only free if it's also free in the last
153 * transaction.
155 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
156 if (r)
157 return r;
159 if (!old_count)
160 smd->nr_allocated_this_transaction--;
163 return r;
166 static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
168 int r;
169 enum allocation_event ev;
170 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
172 /* FIXME: we should loop round a couple of times */
173 r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
174 if (r)
175 return r;
177 smd->begin = *b + 1;
178 r = sm_ll_inc(&smd->ll, *b, &ev);
179 if (!r) {
180 BUG_ON(ev != SM_ALLOC);
181 smd->nr_allocated_this_transaction++;
184 return r;
187 static int sm_disk_commit(struct dm_space_map *sm)
189 int r;
190 dm_block_t nr_free;
191 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
193 r = sm_disk_get_nr_free(sm, &nr_free);
194 if (r)
195 return r;
197 r = sm_ll_commit(&smd->ll);
198 if (r)
199 return r;
201 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
202 smd->begin = 0;
203 smd->nr_allocated_this_transaction = 0;
205 r = sm_disk_get_nr_free(sm, &nr_free);
206 if (r)
207 return r;
209 return 0;
212 static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
214 *result = sizeof(struct disk_sm_root);
216 return 0;
219 static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
221 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
222 struct disk_sm_root root_le;
224 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
225 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
226 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
227 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
229 if (max < sizeof(root_le))
230 return -ENOSPC;
232 memcpy(where_le, &root_le, sizeof(root_le));
234 return 0;
237 /*----------------------------------------------------------------*/
239 static struct dm_space_map ops = {
240 .destroy = sm_disk_destroy,
241 .extend = sm_disk_extend,
242 .get_nr_blocks = sm_disk_get_nr_blocks,
243 .get_nr_free = sm_disk_get_nr_free,
244 .get_count = sm_disk_get_count,
245 .count_is_more_than_one = sm_disk_count_is_more_than_one,
246 .set_count = sm_disk_set_count,
247 .inc_block = sm_disk_inc_block,
248 .dec_block = sm_disk_dec_block,
249 .new_block = sm_disk_new_block,
250 .commit = sm_disk_commit,
251 .root_size = sm_disk_root_size,
252 .copy_root = sm_disk_copy_root
255 static struct dm_space_map *dm_sm_disk_create_real(
256 struct dm_transaction_manager *tm,
257 dm_block_t nr_blocks)
259 int r;
260 struct sm_disk *smd;
262 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
263 if (!smd)
264 return ERR_PTR(-ENOMEM);
266 smd->begin = 0;
267 smd->nr_allocated_this_transaction = 0;
268 memcpy(&smd->sm, &ops, sizeof(smd->sm));
270 r = sm_ll_new_disk(&smd->ll, tm);
271 if (r)
272 goto bad;
274 r = sm_ll_extend(&smd->ll, nr_blocks);
275 if (r)
276 goto bad;
278 r = sm_disk_commit(&smd->sm);
279 if (r)
280 goto bad;
282 return &smd->sm;
284 bad:
285 kfree(smd);
286 return ERR_PTR(r);
289 struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
290 dm_block_t nr_blocks)
292 struct dm_space_map *sm = dm_sm_disk_create_real(tm, nr_blocks);
293 struct dm_space_map *smc;
295 if (IS_ERR_OR_NULL(sm))
296 return sm;
298 smc = dm_sm_checker_create_fresh(sm);
299 if (IS_ERR(smc))
300 dm_sm_destroy(sm);
302 return smc;
304 EXPORT_SYMBOL_GPL(dm_sm_disk_create);
306 static struct dm_space_map *dm_sm_disk_open_real(
307 struct dm_transaction_manager *tm,
308 void *root_le, size_t len)
310 int r;
311 struct sm_disk *smd;
313 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
314 if (!smd)
315 return ERR_PTR(-ENOMEM);
317 smd->begin = 0;
318 smd->nr_allocated_this_transaction = 0;
319 memcpy(&smd->sm, &ops, sizeof(smd->sm));
321 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
322 if (r)
323 goto bad;
325 r = sm_disk_commit(&smd->sm);
326 if (r)
327 goto bad;
329 return &smd->sm;
331 bad:
332 kfree(smd);
333 return ERR_PTR(r);
336 struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
337 void *root_le, size_t len)
339 return dm_sm_checker_create(
340 dm_sm_disk_open_real(tm, root_le, len));
342 EXPORT_SYMBOL_GPL(dm_sm_disk_open);
344 /*----------------------------------------------------------------*/