Loading...
1/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
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"
12
13#include <linux/list.h>
14#include <linux/slab.h>
15#include <linux/export.h>
16#include <linux/device-mapper.h>
17
18#define DM_MSG_PREFIX "space map disk"
19
20/*----------------------------------------------------------------*/
21
22/*
23 * Space map interface.
24 */
25struct sm_disk {
26 struct dm_space_map sm;
27
28 struct ll_disk ll;
29 struct ll_disk old_ll;
30
31 dm_block_t begin;
32 dm_block_t nr_allocated_this_transaction;
33};
34
35static void sm_disk_destroy(struct dm_space_map *sm)
36{
37 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
38
39 kfree(smd);
40}
41
42static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
43{
44 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
45
46 return sm_ll_extend(&smd->ll, extra_blocks);
47}
48
49static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
50{
51 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
52 *count = smd->old_ll.nr_blocks;
53
54 return 0;
55}
56
57static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
58{
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;
61
62 return 0;
63}
64
65static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
66 uint32_t *result)
67{
68 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
69 return sm_ll_lookup(&smd->ll, b, result);
70}
71
72static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
73 int *result)
74{
75 int r;
76 uint32_t count;
77
78 r = sm_disk_get_count(sm, b, &count);
79 if (r)
80 return r;
81
82 return count > 1;
83}
84
85static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
86 uint32_t count)
87{
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);
92
93 r = sm_ll_insert(&smd->ll, b, count, &ev);
94 if (!r) {
95 switch (ev) {
96 case SM_NONE:
97 break;
98
99 case SM_ALLOC:
100 /*
101 * This _must_ be free in the prior transaction
102 * otherwise we've lost atomicity.
103 */
104 smd->nr_allocated_this_transaction++;
105 break;
106
107 case SM_FREE:
108 /*
109 * It's only free if it's also free in the last
110 * transaction.
111 */
112 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
113 if (r)
114 return r;
115
116 if (!old_count)
117 smd->nr_allocated_this_transaction--;
118 break;
119 }
120 }
121
122 return r;
123}
124
125static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
126{
127 int r;
128 enum allocation_event ev;
129 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
130
131 r = sm_ll_inc(&smd->ll, b, &ev);
132 if (!r && (ev == SM_ALLOC))
133 /*
134 * This _must_ be free in the prior transaction
135 * otherwise we've lost atomicity.
136 */
137 smd->nr_allocated_this_transaction++;
138
139 return r;
140}
141
142static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
143{
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);
148
149 r = sm_ll_dec(&smd->ll, b, &ev);
150 if (!r && (ev == SM_FREE)) {
151 /*
152 * It's only free if it's also free in the last
153 * transaction.
154 */
155 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
156 if (r)
157 return r;
158
159 if (!old_count)
160 smd->nr_allocated_this_transaction--;
161 }
162
163 return r;
164}
165
166static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
167{
168 int r;
169 enum allocation_event ev;
170 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
171
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;
176
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++;
182 }
183
184 return r;
185}
186
187static int sm_disk_commit(struct dm_space_map *sm)
188{
189 int r;
190 dm_block_t nr_free;
191 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
192
193 r = sm_disk_get_nr_free(sm, &nr_free);
194 if (r)
195 return r;
196
197 r = sm_ll_commit(&smd->ll);
198 if (r)
199 return r;
200
201 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
202 smd->begin = 0;
203 smd->nr_allocated_this_transaction = 0;
204
205 r = sm_disk_get_nr_free(sm, &nr_free);
206 if (r)
207 return r;
208
209 return 0;
210}
211
212static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
213{
214 *result = sizeof(struct disk_sm_root);
215
216 return 0;
217}
218
219static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
220{
221 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
222 struct disk_sm_root root_le;
223
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);
228
229 if (max < sizeof(root_le))
230 return -ENOSPC;
231
232 memcpy(where_le, &root_le, sizeof(root_le));
233
234 return 0;
235}
236
237/*----------------------------------------------------------------*/
238
239static 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
253};
254
255static struct dm_space_map *dm_sm_disk_create_real(
256 struct dm_transaction_manager *tm,
257 dm_block_t nr_blocks)
258{
259 int r;
260 struct sm_disk *smd;
261
262 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
263 if (!smd)
264 return ERR_PTR(-ENOMEM);
265
266 smd->begin = 0;
267 smd->nr_allocated_this_transaction = 0;
268 memcpy(&smd->sm, &ops, sizeof(smd->sm));
269
270 r = sm_ll_new_disk(&smd->ll, tm);
271 if (r)
272 goto bad;
273
274 r = sm_ll_extend(&smd->ll, nr_blocks);
275 if (r)
276 goto bad;
277
278 r = sm_disk_commit(&smd->sm);
279 if (r)
280 goto bad;
281
282 return &smd->sm;
283
284bad:
285 kfree(smd);
286 return ERR_PTR(r);
287}
288
289struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
290 dm_block_t nr_blocks)
291{
292 struct dm_space_map *sm = dm_sm_disk_create_real(tm, nr_blocks);
293 struct dm_space_map *smc;
294
295 if (IS_ERR_OR_NULL(sm))
296 return sm;
297
298 smc = dm_sm_checker_create_fresh(sm);
299 if (IS_ERR(smc))
300 dm_sm_destroy(sm);
301
302 return smc;
303}
304EXPORT_SYMBOL_GPL(dm_sm_disk_create);
305
306static struct dm_space_map *dm_sm_disk_open_real(
307 struct dm_transaction_manager *tm,
308 void *root_le, size_t len)
309{
310 int r;
311 struct sm_disk *smd;
312
313 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
314 if (!smd)
315 return ERR_PTR(-ENOMEM);
316
317 smd->begin = 0;
318 smd->nr_allocated_this_transaction = 0;
319 memcpy(&smd->sm, &ops, sizeof(smd->sm));
320
321 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
322 if (r)
323 goto bad;
324
325 r = sm_disk_commit(&smd->sm);
326 if (r)
327 goto bad;
328
329 return &smd->sm;
330
331bad:
332 kfree(smd);
333 return ERR_PTR(r);
334}
335
336struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
337 void *root_le, size_t len)
338{
339 return dm_sm_checker_create(
340 dm_sm_disk_open_real(tm, root_le, len));
341}
342EXPORT_SYMBOL_GPL(dm_sm_disk_open);
343
344/*----------------------------------------------------------------*/
1/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
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"
11
12#include <linux/list.h>
13#include <linux/slab.h>
14#include <linux/export.h>
15#include <linux/device-mapper.h>
16
17#define DM_MSG_PREFIX "space map disk"
18
19/*----------------------------------------------------------------*/
20
21/*
22 * Space map interface.
23 */
24struct sm_disk {
25 struct dm_space_map sm;
26
27 struct ll_disk ll;
28 struct ll_disk old_ll;
29
30 dm_block_t begin;
31 dm_block_t nr_allocated_this_transaction;
32};
33
34static void sm_disk_destroy(struct dm_space_map *sm)
35{
36 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
37
38 kfree(smd);
39}
40
41static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
42{
43 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
44
45 return sm_ll_extend(&smd->ll, extra_blocks);
46}
47
48static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
49{
50 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
51 *count = smd->old_ll.nr_blocks;
52
53 return 0;
54}
55
56static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
57{
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;
60
61 return 0;
62}
63
64static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
65 uint32_t *result)
66{
67 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
68 return sm_ll_lookup(&smd->ll, b, result);
69}
70
71static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
72 int *result)
73{
74 int r;
75 uint32_t count;
76
77 r = sm_disk_get_count(sm, b, &count);
78 if (r)
79 return r;
80
81 *result = count > 1;
82
83 return 0;
84}
85
86static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
87 uint32_t count)
88{
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);
93
94 r = sm_ll_insert(&smd->ll, b, count, &ev);
95 if (!r) {
96 switch (ev) {
97 case SM_NONE:
98 break;
99
100 case SM_ALLOC:
101 /*
102 * This _must_ be free in the prior transaction
103 * otherwise we've lost atomicity.
104 */
105 smd->nr_allocated_this_transaction++;
106 break;
107
108 case SM_FREE:
109 /*
110 * It's only free if it's also free in the last
111 * transaction.
112 */
113 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
114 if (r)
115 return r;
116
117 if (!old_count)
118 smd->nr_allocated_this_transaction--;
119 break;
120 }
121 }
122
123 return r;
124}
125
126static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
127{
128 int r;
129 enum allocation_event ev;
130 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
131
132 r = sm_ll_inc(&smd->ll, b, &ev);
133 if (!r && (ev == SM_ALLOC))
134 /*
135 * This _must_ be free in the prior transaction
136 * otherwise we've lost atomicity.
137 */
138 smd->nr_allocated_this_transaction++;
139
140 return r;
141}
142
143static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
144{
145 enum allocation_event ev;
146 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
147
148 return sm_ll_dec(&smd->ll, b, &ev);
149}
150
151static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
152{
153 int r;
154 enum allocation_event ev;
155 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
156
157 /* FIXME: we should loop round a couple of times */
158 r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
159 if (r)
160 return r;
161
162 smd->begin = *b + 1;
163 r = sm_ll_inc(&smd->ll, *b, &ev);
164 if (!r) {
165 BUG_ON(ev != SM_ALLOC);
166 smd->nr_allocated_this_transaction++;
167 }
168
169 return r;
170}
171
172static int sm_disk_commit(struct dm_space_map *sm)
173{
174 int r;
175 dm_block_t nr_free;
176 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
177
178 r = sm_disk_get_nr_free(sm, &nr_free);
179 if (r)
180 return r;
181
182 r = sm_ll_commit(&smd->ll);
183 if (r)
184 return r;
185
186 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
187 smd->begin = 0;
188 smd->nr_allocated_this_transaction = 0;
189
190 r = sm_disk_get_nr_free(sm, &nr_free);
191 if (r)
192 return r;
193
194 return 0;
195}
196
197static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
198{
199 *result = sizeof(struct disk_sm_root);
200
201 return 0;
202}
203
204static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
205{
206 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
207 struct disk_sm_root root_le;
208
209 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
210 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
211 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
212 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
213
214 if (max < sizeof(root_le))
215 return -ENOSPC;
216
217 memcpy(where_le, &root_le, sizeof(root_le));
218
219 return 0;
220}
221
222/*----------------------------------------------------------------*/
223
224static struct dm_space_map ops = {
225 .destroy = sm_disk_destroy,
226 .extend = sm_disk_extend,
227 .get_nr_blocks = sm_disk_get_nr_blocks,
228 .get_nr_free = sm_disk_get_nr_free,
229 .get_count = sm_disk_get_count,
230 .count_is_more_than_one = sm_disk_count_is_more_than_one,
231 .set_count = sm_disk_set_count,
232 .inc_block = sm_disk_inc_block,
233 .dec_block = sm_disk_dec_block,
234 .new_block = sm_disk_new_block,
235 .commit = sm_disk_commit,
236 .root_size = sm_disk_root_size,
237 .copy_root = sm_disk_copy_root,
238 .register_threshold_callback = NULL
239};
240
241struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
242 dm_block_t nr_blocks)
243{
244 int r;
245 struct sm_disk *smd;
246
247 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
248 if (!smd)
249 return ERR_PTR(-ENOMEM);
250
251 smd->begin = 0;
252 smd->nr_allocated_this_transaction = 0;
253 memcpy(&smd->sm, &ops, sizeof(smd->sm));
254
255 r = sm_ll_new_disk(&smd->ll, tm);
256 if (r)
257 goto bad;
258
259 r = sm_ll_extend(&smd->ll, nr_blocks);
260 if (r)
261 goto bad;
262
263 r = sm_disk_commit(&smd->sm);
264 if (r)
265 goto bad;
266
267 return &smd->sm;
268
269bad:
270 kfree(smd);
271 return ERR_PTR(r);
272}
273EXPORT_SYMBOL_GPL(dm_sm_disk_create);
274
275struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
276 void *root_le, size_t len)
277{
278 int r;
279 struct sm_disk *smd;
280
281 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
282 if (!smd)
283 return ERR_PTR(-ENOMEM);
284
285 smd->begin = 0;
286 smd->nr_allocated_this_transaction = 0;
287 memcpy(&smd->sm, &ops, sizeof(smd->sm));
288
289 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
290 if (r)
291 goto bad;
292
293 r = sm_disk_commit(&smd->sm);
294 if (r)
295 goto bad;
296
297 return &smd->sm;
298
299bad:
300 kfree(smd);
301 return ERR_PTR(r);
302}
303EXPORT_SYMBOL_GPL(dm_sm_disk_open);
304
305/*----------------------------------------------------------------*/