1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Red Hat, Inc.
5 * This file is released under the GPL.
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.
26 struct dm_space_map sm
;
29 struct ll_disk old_ll
;
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
);
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
);
53 *count
= smd
->old_ll
.nr_blocks
;
58 static int sm_disk_get_nr_free(struct dm_space_map
*sm
, dm_block_t
*count
)
60 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
62 *count
= (smd
->old_ll
.nr_blocks
- smd
->old_ll
.nr_allocated
) - smd
->nr_allocated_this_transaction
;
67 static int sm_disk_get_count(struct dm_space_map
*sm
, dm_block_t b
,
70 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
72 return sm_ll_lookup(&smd
->ll
, b
, result
);
75 static int sm_disk_count_is_more_than_one(struct dm_space_map
*sm
, dm_block_t b
,
81 r
= sm_disk_get_count(sm
, b
, &count
);
90 static int sm_disk_set_count(struct dm_space_map
*sm
, dm_block_t b
,
94 int32_t nr_allocations
;
95 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
97 r
= sm_ll_insert(&smd
->ll
, b
, count
, &nr_allocations
);
99 smd
->nr_allocated_this_transaction
+= nr_allocations
;
104 static int sm_disk_inc_blocks(struct dm_space_map
*sm
, dm_block_t b
, dm_block_t e
)
107 int32_t nr_allocations
;
108 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
110 r
= sm_ll_inc(&smd
->ll
, b
, e
, &nr_allocations
);
112 smd
->nr_allocated_this_transaction
+= nr_allocations
;
117 static int sm_disk_dec_blocks(struct dm_space_map
*sm
, dm_block_t b
, dm_block_t e
)
120 int32_t nr_allocations
;
121 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
123 r
= sm_ll_dec(&smd
->ll
, b
, e
, &nr_allocations
);
125 smd
->nr_allocated_this_transaction
+= nr_allocations
;
130 static int sm_disk_new_block(struct dm_space_map
*sm
, dm_block_t
*b
)
133 int32_t nr_allocations
;
134 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
137 * Any block we allocate has to be free in both the old and current ll.
139 r
= sm_ll_find_common_free_block(&smd
->old_ll
, &smd
->ll
, smd
->begin
, smd
->ll
.nr_blocks
, b
);
142 * There's no free block between smd->begin and the end of the metadata device.
143 * We search before smd->begin in case something has been freed.
145 r
= sm_ll_find_common_free_block(&smd
->old_ll
, &smd
->ll
, 0, smd
->begin
, b
);
151 r
= sm_ll_inc(&smd
->ll
, *b
, *b
+ 1, &nr_allocations
);
153 smd
->nr_allocated_this_transaction
+= nr_allocations
;
158 static int sm_disk_commit(struct dm_space_map
*sm
)
161 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
163 r
= sm_ll_commit(&smd
->ll
);
167 memcpy(&smd
->old_ll
, &smd
->ll
, sizeof(smd
->old_ll
));
168 smd
->nr_allocated_this_transaction
= 0;
173 static int sm_disk_root_size(struct dm_space_map
*sm
, size_t *result
)
175 *result
= sizeof(struct disk_sm_root
);
180 static int sm_disk_copy_root(struct dm_space_map
*sm
, void *where_le
, size_t max
)
182 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
183 struct disk_sm_root root_le
;
185 root_le
.nr_blocks
= cpu_to_le64(smd
->ll
.nr_blocks
);
186 root_le
.nr_allocated
= cpu_to_le64(smd
->ll
.nr_allocated
);
187 root_le
.bitmap_root
= cpu_to_le64(smd
->ll
.bitmap_root
);
188 root_le
.ref_count_root
= cpu_to_le64(smd
->ll
.ref_count_root
);
190 if (max
< sizeof(root_le
))
193 memcpy(where_le
, &root_le
, sizeof(root_le
));
198 /*----------------------------------------------------------------*/
200 static struct dm_space_map ops
= {
201 .destroy
= sm_disk_destroy
,
202 .extend
= sm_disk_extend
,
203 .get_nr_blocks
= sm_disk_get_nr_blocks
,
204 .get_nr_free
= sm_disk_get_nr_free
,
205 .get_count
= sm_disk_get_count
,
206 .count_is_more_than_one
= sm_disk_count_is_more_than_one
,
207 .set_count
= sm_disk_set_count
,
208 .inc_blocks
= sm_disk_inc_blocks
,
209 .dec_blocks
= sm_disk_dec_blocks
,
210 .new_block
= sm_disk_new_block
,
211 .commit
= sm_disk_commit
,
212 .root_size
= sm_disk_root_size
,
213 .copy_root
= sm_disk_copy_root
,
214 .register_threshold_callback
= NULL
217 struct dm_space_map
*dm_sm_disk_create(struct dm_transaction_manager
*tm
,
218 dm_block_t nr_blocks
)
223 smd
= kmalloc(sizeof(*smd
), GFP_KERNEL
);
225 return ERR_PTR(-ENOMEM
);
228 smd
->nr_allocated_this_transaction
= 0;
229 memcpy(&smd
->sm
, &ops
, sizeof(smd
->sm
));
231 r
= sm_ll_new_disk(&smd
->ll
, tm
);
235 r
= sm_ll_extend(&smd
->ll
, nr_blocks
);
239 r
= sm_disk_commit(&smd
->sm
);
249 EXPORT_SYMBOL_GPL(dm_sm_disk_create
);
251 struct dm_space_map
*dm_sm_disk_open(struct dm_transaction_manager
*tm
,
252 void *root_le
, size_t len
)
257 smd
= kmalloc(sizeof(*smd
), GFP_KERNEL
);
259 return ERR_PTR(-ENOMEM
);
262 smd
->nr_allocated_this_transaction
= 0;
263 memcpy(&smd
->sm
, &ops
, sizeof(smd
->sm
));
265 r
= sm_ll_open_disk(&smd
->ll
, tm
, root_le
, len
);
269 r
= sm_disk_commit(&smd
->sm
);
279 EXPORT_SYMBOL_GPL(dm_sm_disk_open
);
281 /*----------------------------------------------------------------*/