blob: d0a8d5e73c28064d787bb2b0d05305ec9dbaedf4 [file] [log] [blame]
Joe Thornber3241b1d2011-10-31 20:19:11 +00001/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
Joe Thornber3241b1d2011-10-31 20:19:11 +00007#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>
Paul Gortmaker1944ce62011-09-28 18:29:32 -040014#include <linux/export.h>
Joe Thornber3241b1d2011-10-31 20:19:11 +000015#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
Mike Snitzer145b9002015-02-12 16:25:05 -050081 *result = count > 1;
82
83 return 0;
Joe Thornber3241b1d2011-10-31 20:19:11 +000084}
85
86static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
87 uint32_t count)
88{
89 int r;
Joe Thornberbe500ed2021-04-13 11:03:45 +010090 int32_t nr_allocations;
Joe Thornber3241b1d2011-10-31 20:19:11 +000091 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
92
Joe Thornberbe500ed2021-04-13 11:03:45 +010093 r = sm_ll_insert(&smd->ll, b, count, &nr_allocations);
Joe Thornber3241b1d2011-10-31 20:19:11 +000094 if (!r) {
Joe Thornberbe500ed2021-04-13 11:03:45 +010095 smd->nr_allocated_this_transaction += nr_allocations;
Joe Thornber3241b1d2011-10-31 20:19:11 +000096 }
97
98 return r;
99}
100
Joe Thornberbe500ed2021-04-13 11:03:45 +0100101static int sm_disk_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000102{
103 int r;
Joe Thornberbe500ed2021-04-13 11:03:45 +0100104 int32_t nr_allocations;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000105 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
106
Joe Thornberbe500ed2021-04-13 11:03:45 +0100107 r = sm_ll_inc(&smd->ll, b, e, &nr_allocations);
108 if (!r)
109 smd->nr_allocated_this_transaction += nr_allocations;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000110
111 return r;
112}
113
Joe Thornberbe500ed2021-04-13 11:03:45 +0100114static int sm_disk_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000115{
Joe Thornber0377a072017-05-15 09:45:40 -0400116 int r;
Joe Thornberbe500ed2021-04-13 11:03:45 +0100117 int32_t nr_allocations;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000118 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
119
Joe Thornberbe500ed2021-04-13 11:03:45 +0100120 r = sm_ll_dec(&smd->ll, b, e, &nr_allocations);
121 if (!r)
122 smd->nr_allocated_this_transaction += nr_allocations;
Joe Thornber0377a072017-05-15 09:45:40 -0400123
124 return r;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000125}
126
127static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
128{
129 int r;
Joe Thornberbe500ed2021-04-13 11:03:45 +0100130 int32_t nr_allocations;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000131 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
132
Joe Thornber4feaef82020-01-07 11:58:42 +0000133 /*
134 * Any block we allocate has to be free in both the old and current ll.
135 */
136 r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
Joe Thornber5faafc72021-04-13 09:03:49 +0100137 if (r == -ENOSPC) {
138 /*
139 * There's no free block between smd->begin and the end of the metadata device.
140 * We search before smd->begin in case something has been freed.
141 */
142 r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, 0, smd->begin, b);
143 }
144
Joe Thornber3241b1d2011-10-31 20:19:11 +0000145 if (r)
146 return r;
147
148 smd->begin = *b + 1;
Joe Thornberbe500ed2021-04-13 11:03:45 +0100149 r = sm_ll_inc(&smd->ll, *b, *b + 1, &nr_allocations);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000150 if (!r) {
Joe Thornberbe500ed2021-04-13 11:03:45 +0100151 smd->nr_allocated_this_transaction += nr_allocations;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000152 }
153
154 return r;
155}
156
157static int sm_disk_commit(struct dm_space_map *sm)
158{
159 int r;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000160 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
161
Joe Thornber3241b1d2011-10-31 20:19:11 +0000162 r = sm_ll_commit(&smd->ll);
163 if (r)
164 return r;
165
166 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000167 smd->nr_allocated_this_transaction = 0;
168
Joe Thornber3241b1d2011-10-31 20:19:11 +0000169 return 0;
170}
171
172static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
173{
174 *result = sizeof(struct disk_sm_root);
175
176 return 0;
177}
178
179static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
180{
181 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
182 struct disk_sm_root root_le;
183
184 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
185 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
186 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
187 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
188
189 if (max < sizeof(root_le))
190 return -ENOSPC;
191
192 memcpy(where_le, &root_le, sizeof(root_le));
193
194 return 0;
195}
196
197/*----------------------------------------------------------------*/
198
199static struct dm_space_map ops = {
200 .destroy = sm_disk_destroy,
201 .extend = sm_disk_extend,
202 .get_nr_blocks = sm_disk_get_nr_blocks,
203 .get_nr_free = sm_disk_get_nr_free,
204 .get_count = sm_disk_get_count,
205 .count_is_more_than_one = sm_disk_count_is_more_than_one,
206 .set_count = sm_disk_set_count,
Joe Thornberbe500ed2021-04-13 11:03:45 +0100207 .inc_blocks = sm_disk_inc_blocks,
208 .dec_blocks = sm_disk_dec_blocks,
Joe Thornber3241b1d2011-10-31 20:19:11 +0000209 .new_block = sm_disk_new_block,
210 .commit = sm_disk_commit,
211 .root_size = sm_disk_root_size,
Joe Thornber7c3d3f22013-05-10 14:37:20 +0100212 .copy_root = sm_disk_copy_root,
213 .register_threshold_callback = NULL
Joe Thornber3241b1d2011-10-31 20:19:11 +0000214};
215
Joe Thornber3caf6d72012-07-27 15:07:58 +0100216struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
217 dm_block_t nr_blocks)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000218{
219 int r;
220 struct sm_disk *smd;
221
222 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
223 if (!smd)
224 return ERR_PTR(-ENOMEM);
225
226 smd->begin = 0;
227 smd->nr_allocated_this_transaction = 0;
228 memcpy(&smd->sm, &ops, sizeof(smd->sm));
229
230 r = sm_ll_new_disk(&smd->ll, tm);
231 if (r)
232 goto bad;
233
234 r = sm_ll_extend(&smd->ll, nr_blocks);
235 if (r)
236 goto bad;
237
238 r = sm_disk_commit(&smd->sm);
239 if (r)
240 goto bad;
241
242 return &smd->sm;
243
244bad:
245 kfree(smd);
246 return ERR_PTR(r);
247}
Joe Thornber3241b1d2011-10-31 20:19:11 +0000248EXPORT_SYMBOL_GPL(dm_sm_disk_create);
249
Joe Thornber3caf6d72012-07-27 15:07:58 +0100250struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
251 void *root_le, size_t len)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000252{
253 int r;
254 struct sm_disk *smd;
255
256 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
257 if (!smd)
258 return ERR_PTR(-ENOMEM);
259
260 smd->begin = 0;
261 smd->nr_allocated_this_transaction = 0;
262 memcpy(&smd->sm, &ops, sizeof(smd->sm));
263
264 r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
265 if (r)
266 goto bad;
267
268 r = sm_disk_commit(&smd->sm);
269 if (r)
270 goto bad;
271
272 return &smd->sm;
273
274bad:
275 kfree(smd);
276 return ERR_PTR(r);
277}
Joe Thornber3241b1d2011-10-31 20:19:11 +0000278EXPORT_SYMBOL_GPL(dm_sm_disk_open);
279
280/*----------------------------------------------------------------*/