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 */
24 struct 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
sm_disk_destroy(struct dm_space_map * sm)34 static 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
sm_disk_extend(struct dm_space_map * sm,dm_block_t extra_blocks)41 static 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
sm_disk_get_nr_blocks(struct dm_space_map * sm,dm_block_t * count)48 static 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
sm_disk_get_nr_free(struct dm_space_map * sm,dm_block_t * count)56 static 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
sm_disk_get_count(struct dm_space_map * sm,dm_block_t b,uint32_t * result)64 static 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
sm_disk_count_is_more_than_one(struct dm_space_map * sm,dm_block_t b,int * result)71 static 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 return count > 1;
82 }
83
sm_disk_set_count(struct dm_space_map * sm,dm_block_t b,uint32_t count)84 static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
85 uint32_t count)
86 {
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);
91
92 r = sm_ll_insert(&smd->ll, b, count, &ev);
93 if (!r) {
94 switch (ev) {
95 case SM_NONE:
96 break;
97
98 case SM_ALLOC:
99 /*
100 * This _must_ be free in the prior transaction
101 * otherwise we've lost atomicity.
102 */
103 smd->nr_allocated_this_transaction++;
104 break;
105
106 case SM_FREE:
107 /*
108 * It's only free if it's also free in the last
109 * transaction.
110 */
111 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
112 if (r)
113 return r;
114
115 if (!old_count)
116 smd->nr_allocated_this_transaction--;
117 break;
118 }
119 }
120
121 return r;
122 }
123
sm_disk_inc_block(struct dm_space_map * sm,dm_block_t b)124 static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
125 {
126 int r;
127 enum allocation_event ev;
128 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
129
130 r = sm_ll_inc(&smd->ll, b, &ev);
131 if (!r && (ev == SM_ALLOC))
132 /*
133 * This _must_ be free in the prior transaction
134 * otherwise we've lost atomicity.
135 */
136 smd->nr_allocated_this_transaction++;
137
138 return r;
139 }
140
sm_disk_dec_block(struct dm_space_map * sm,dm_block_t b)141 static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
142 {
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);
147
148 r = sm_ll_dec(&smd->ll, b, &ev);
149 if (!r && (ev == SM_FREE)) {
150 /*
151 * It's only free if it's also free in the last
152 * transaction.
153 */
154 r = sm_ll_lookup(&smd->old_ll, b, &old_count);
155 if (!r && !old_count)
156 smd->nr_allocated_this_transaction--;
157 }
158
159 return r;
160 }
161
sm_disk_new_block(struct dm_space_map * sm,dm_block_t * b)162 static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
163 {
164 int r;
165 enum allocation_event ev;
166 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
167
168 /* FIXME: we should loop round a couple of times */
169 r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
170 if (r)
171 return r;
172
173 smd->begin = *b + 1;
174 r = sm_ll_inc(&smd->ll, *b, &ev);
175 if (!r) {
176 BUG_ON(ev != SM_ALLOC);
177 smd->nr_allocated_this_transaction++;
178 }
179
180 return r;
181 }
182
sm_disk_commit(struct dm_space_map * sm)183 static int sm_disk_commit(struct dm_space_map *sm)
184 {
185 int r;
186 dm_block_t nr_free;
187 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
188
189 r = sm_disk_get_nr_free(sm, &nr_free);
190 if (r)
191 return r;
192
193 r = sm_ll_commit(&smd->ll);
194 if (r)
195 return r;
196
197 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
198 smd->begin = 0;
199 smd->nr_allocated_this_transaction = 0;
200
201 r = sm_disk_get_nr_free(sm, &nr_free);
202 if (r)
203 return r;
204
205 return 0;
206 }
207
sm_disk_root_size(struct dm_space_map * sm,size_t * result)208 static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
209 {
210 *result = sizeof(struct disk_sm_root);
211
212 return 0;
213 }
214
sm_disk_copy_root(struct dm_space_map * sm,void * where_le,size_t max)215 static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
216 {
217 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
218 struct disk_sm_root root_le;
219
220 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
221 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
222 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
223 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
224
225 if (max < sizeof(root_le))
226 return -ENOSPC;
227
228 memcpy(where_le, &root_le, sizeof(root_le));
229
230 return 0;
231 }
232
233 /*----------------------------------------------------------------*/
234
235 static struct dm_space_map ops = {
236 .destroy = sm_disk_destroy,
237 .extend = sm_disk_extend,
238 .get_nr_blocks = sm_disk_get_nr_blocks,
239 .get_nr_free = sm_disk_get_nr_free,
240 .get_count = sm_disk_get_count,
241 .count_is_more_than_one = sm_disk_count_is_more_than_one,
242 .set_count = sm_disk_set_count,
243 .inc_block = sm_disk_inc_block,
244 .dec_block = sm_disk_dec_block,
245 .new_block = sm_disk_new_block,
246 .commit = sm_disk_commit,
247 .root_size = sm_disk_root_size,
248 .copy_root = sm_disk_copy_root,
249 .register_threshold_callback = NULL
250 };
251
dm_sm_disk_create(struct dm_transaction_manager * tm,dm_block_t nr_blocks)252 struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
253 dm_block_t nr_blocks)
254 {
255 int r;
256 struct sm_disk *smd;
257
258 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
259 if (!smd)
260 return ERR_PTR(-ENOMEM);
261
262 smd->begin = 0;
263 smd->nr_allocated_this_transaction = 0;
264 memcpy(&smd->sm, &ops, sizeof(smd->sm));
265
266 r = sm_ll_new_disk(&smd->ll, tm);
267 if (r)
268 goto bad;
269
270 r = sm_ll_extend(&smd->ll, nr_blocks);
271 if (r)
272 goto bad;
273
274 r = sm_disk_commit(&smd->sm);
275 if (r)
276 goto bad;
277
278 return &smd->sm;
279
280 bad:
281 kfree(smd);
282 return ERR_PTR(r);
283 }
284 EXPORT_SYMBOL_GPL(dm_sm_disk_create);
285
dm_sm_disk_open(struct dm_transaction_manager * tm,void * root_le,size_t len)286 struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
287 void *root_le, size_t len)
288 {
289 int r;
290 struct sm_disk *smd;
291
292 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
293 if (!smd)
294 return ERR_PTR(-ENOMEM);
295
296 smd->begin = 0;
297 smd->nr_allocated_this_transaction = 0;
298 memcpy(&smd->sm, &ops, sizeof(smd->sm));
299
300 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
301 if (r)
302 goto bad;
303
304 r = sm_disk_commit(&smd->sm);
305 if (r)
306 goto bad;
307
308 return &smd->sm;
309
310 bad:
311 kfree(smd);
312 return ERR_PTR(r);
313 }
314 EXPORT_SYMBOL_GPL(dm_sm_disk_open);
315
316 /*----------------------------------------------------------------*/
317