2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
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"
12 #include <linux/list.h>
13 #include <linux/slab.h>
14 #include <linux/export.h>
15 #include <linux/device-mapper.h>
17 #define DM_MSG_PREFIX "space map disk"
19 /*----------------------------------------------------------------*/
22 * Space map interface.
25 struct dm_space_map sm
;
28 struct ll_disk old_ll
;
31 dm_block_t nr_allocated_this_transaction
;
34 static void sm_disk_destroy(struct dm_space_map
*sm
)
36 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
41 static int sm_disk_extend(struct dm_space_map
*sm
, dm_block_t extra_blocks
)
43 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
45 return sm_ll_extend(&smd
->ll
, extra_blocks
);
48 static int sm_disk_get_nr_blocks(struct dm_space_map
*sm
, dm_block_t
*count
)
50 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
51 *count
= smd
->old_ll
.nr_blocks
;
56 static int sm_disk_get_nr_free(struct dm_space_map
*sm
, dm_block_t
*count
)
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
;
64 static int sm_disk_get_count(struct dm_space_map
*sm
, dm_block_t b
,
67 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
68 return sm_ll_lookup(&smd
->ll
, b
, result
);
71 static int sm_disk_count_is_more_than_one(struct dm_space_map
*sm
, dm_block_t b
,
77 r
= sm_disk_get_count(sm
, b
, &count
);
84 static int sm_disk_set_count(struct dm_space_map
*sm
, dm_block_t b
,
89 enum allocation_event ev
;
90 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
92 r
= sm_ll_insert(&smd
->ll
, b
, count
, &ev
);
100 * This _must_ be free in the prior transaction
101 * otherwise we've lost atomicity.
103 smd
->nr_allocated_this_transaction
++;
108 * It's only free if it's also free in the last
111 r
= sm_ll_lookup(&smd
->old_ll
, b
, &old_count
);
116 smd
->nr_allocated_this_transaction
--;
124 static int sm_disk_inc_block(struct dm_space_map
*sm
, dm_block_t b
)
127 enum allocation_event ev
;
128 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
130 r
= sm_ll_inc(&smd
->ll
, b
, &ev
);
131 if (!r
&& (ev
== SM_ALLOC
))
133 * This _must_ be free in the prior transaction
134 * otherwise we've lost atomicity.
136 smd
->nr_allocated_this_transaction
++;
141 static int sm_disk_dec_block(struct dm_space_map
*sm
, dm_block_t b
)
143 enum allocation_event ev
;
144 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
146 return sm_ll_dec(&smd
->ll
, b
, &ev
);
149 static int sm_disk_new_block(struct dm_space_map
*sm
, dm_block_t
*b
)
152 enum allocation_event ev
;
153 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
155 /* FIXME: we should loop round a couple of times */
156 r
= sm_ll_find_free_block(&smd
->old_ll
, smd
->begin
, smd
->old_ll
.nr_blocks
, b
);
161 r
= sm_ll_inc(&smd
->ll
, *b
, &ev
);
163 BUG_ON(ev
!= SM_ALLOC
);
164 smd
->nr_allocated_this_transaction
++;
170 static int sm_disk_commit(struct dm_space_map
*sm
)
174 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
176 r
= sm_disk_get_nr_free(sm
, &nr_free
);
180 r
= sm_ll_commit(&smd
->ll
);
184 memcpy(&smd
->old_ll
, &smd
->ll
, sizeof(smd
->old_ll
));
186 smd
->nr_allocated_this_transaction
= 0;
188 r
= sm_disk_get_nr_free(sm
, &nr_free
);
195 static int sm_disk_root_size(struct dm_space_map
*sm
, size_t *result
)
197 *result
= sizeof(struct disk_sm_root
);
202 static int sm_disk_copy_root(struct dm_space_map
*sm
, void *where_le
, size_t max
)
204 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
205 struct disk_sm_root root_le
;
207 root_le
.nr_blocks
= cpu_to_le64(smd
->ll
.nr_blocks
);
208 root_le
.nr_allocated
= cpu_to_le64(smd
->ll
.nr_allocated
);
209 root_le
.bitmap_root
= cpu_to_le64(smd
->ll
.bitmap_root
);
210 root_le
.ref_count_root
= cpu_to_le64(smd
->ll
.ref_count_root
);
212 if (max
< sizeof(root_le
))
215 memcpy(where_le
, &root_le
, sizeof(root_le
));
220 /*----------------------------------------------------------------*/
222 static struct dm_space_map ops
= {
223 .destroy
= sm_disk_destroy
,
224 .extend
= sm_disk_extend
,
225 .get_nr_blocks
= sm_disk_get_nr_blocks
,
226 .get_nr_free
= sm_disk_get_nr_free
,
227 .get_count
= sm_disk_get_count
,
228 .count_is_more_than_one
= sm_disk_count_is_more_than_one
,
229 .set_count
= sm_disk_set_count
,
230 .inc_block
= sm_disk_inc_block
,
231 .dec_block
= sm_disk_dec_block
,
232 .new_block
= sm_disk_new_block
,
233 .commit
= sm_disk_commit
,
234 .root_size
= sm_disk_root_size
,
235 .copy_root
= sm_disk_copy_root
,
236 .register_threshold_callback
= NULL
239 struct dm_space_map
*dm_sm_disk_create(struct dm_transaction_manager
*tm
,
240 dm_block_t nr_blocks
)
245 smd
= kmalloc(sizeof(*smd
), GFP_KERNEL
);
247 return ERR_PTR(-ENOMEM
);
250 smd
->nr_allocated_this_transaction
= 0;
251 memcpy(&smd
->sm
, &ops
, sizeof(smd
->sm
));
253 r
= sm_ll_new_disk(&smd
->ll
, tm
);
257 r
= sm_ll_extend(&smd
->ll
, nr_blocks
);
261 r
= sm_disk_commit(&smd
->sm
);
271 EXPORT_SYMBOL_GPL(dm_sm_disk_create
);
273 struct dm_space_map
*dm_sm_disk_open(struct dm_transaction_manager
*tm
,
274 void *root_le
, size_t len
)
279 smd
= kmalloc(sizeof(*smd
), GFP_KERNEL
);
281 return ERR_PTR(-ENOMEM
);
284 smd
->nr_allocated_this_transaction
= 0;
285 memcpy(&smd
->sm
, &ops
, sizeof(smd
->sm
));
287 r
= sm_ll_open_disk(&smd
->ll
, tm
, root_le
, len
);
291 r
= sm_disk_commit(&smd
->sm
);
301 EXPORT_SYMBOL_GPL(dm_sm_disk_open
);
303 /*----------------------------------------------------------------*/