10 #include <linux/bitops.h>
13 #define DM_MSG_PREFIX "space map common"
20 #define INDEX_CSUM_XOR 160478
30 block_size -
sizeof(
__le32),
42 DMERR(
"index_check failed blocknr %llu wanted %llu",
48 block_size -
sizeof(
__le32),
50 if (csum_disk != mi_le->
csum) {
51 DMERR(
"index_check failed csum %u wanted %u",
61 .prepare_for_write = index_prepare_for_write,
70 #define BITMAP_CSUM_XOR 240779
80 block_size -
sizeof(
__le32),
92 DMERR(
"bitmap check failed blocknr %llu wanted %llu",
98 block_size -
sizeof(
__le32),
100 if (csum_disk != disk_header->
csum) {
101 DMERR(
"bitmap check failed csum %u wanted %u",
111 .prepare_for_write = bitmap_prepare_for_write,
112 .check = bitmap_check
117 #define ENTRIES_PER_WORD 32
118 #define ENTRIES_SHIFT 5
120 static void *dm_bitmap_data(
struct dm_block *b)
125 #define WORD_MASK_HIGH 0xAAAAAAAAAAAAAAAAULL
127 static unsigned bitmap_word_used(
void *
addr,
unsigned b)
135 return !(~bits &
mask);
138 static unsigned sm_lookup_bitmap(
void *
addr,
unsigned b)
145 hi = !!test_bit_le(b, (
void *) w_le);
146 lo = !!test_bit_le(b + 1, (
void *) w_le);
147 return (hi << 1) | lo;
150 static void sm_set_bitmap(
void *addr,
unsigned b,
unsigned val)
158 __set_bit_le(b, (
void *) w_le);
160 __clear_bit_le(b, (
void *) w_le);
163 __set_bit_le(b + 1, (
void *) w_le);
165 __clear_bit_le(b + 1, (
void *) w_le);
168 static int sm_find_free(
void *addr,
unsigned begin,
unsigned end,
171 while (begin < end) {
173 bitmap_word_used(addr, begin)) {
178 if (!sm_lookup_bitmap(addr, begin)) {
218 DMERR(
"block size too big to hold bitmaps");
236 unsigned old_blocks, blocks;
238 nr_blocks = ll->
nr_blocks + extra_blocks;
244 DMERR(
"space map too large");
248 for (i = old_blocks; i < blocks; i++) {
278 struct dm_block *
blk;
281 r = ll->
load_ie(ll, index, &ie_disk);
286 &dm_sm_bitmap_validator, &blk);
290 *result = sm_lookup_bitmap(dm_bitmap_data(blk), b);
329 for (i = index_begin; i < index_end; i++, begin = 0) {
330 struct dm_block *
blk;
334 r = ll->
load_ie(ll, i, &ie_disk);
342 &dm_sm_bitmap_validator, &blk);
348 r = sm_find_free(dm_bitmap_data(blk),
387 r = ll->
load_ie(ll, index, &ie_disk);
392 &dm_sm_bitmap_validator, &nb, &inc);
394 DMERR(
"dm_tm_shadow_block() failed");
399 bm_le = dm_bitmap_data(nb);
400 old = sm_lookup_bitmap(bm_le, bit);
402 if (ref_count <= 2) {
403 sm_set_bitmap(bm_le, bit, ref_count);
420 sm_set_bitmap(bm_le, bit, 3);
429 DMERR(
"ref count insert failed");
434 if (ref_count && !old) {
437 le32_add_cpu(&ie_disk.
nr_free, -1);
441 }
else if (old && !ref_count) {
444 le32_add_cpu(&ie_disk.
nr_free, 1);
448 return ll->
save_ie(ll, index, &ie_disk);
508 static int metadata_ll_init_index(
struct ll_disk *ll)
523 static int metadata_ll_open(
struct ll_disk *ll)
526 struct dm_block *
block;
529 &index_validator, &block);
542 static int metadata_ll_commit(
struct ll_disk *ll)
561 r = sm_ll_init(ll, tm);
565 ll->
load_ie = metadata_ll_load_ie;
566 ll->
save_ie = metadata_ll_save_ie;
570 ll->
commit = metadata_ll_commit;
587 void *root_le,
size_t len)
593 DMERR(
"sm_metadata root too small");
597 r = sm_ll_init(ll, tm);
601 ll->
load_ie = metadata_ll_load_ie;
602 ll->
save_ie = metadata_ll_save_ie;
606 ll->
commit = metadata_ll_commit;
632 static int disk_ll_init_index(
struct ll_disk *ll)
637 static int disk_ll_open(
struct ll_disk *ll)
648 static int disk_ll_commit(
struct ll_disk *ll)
657 r = sm_ll_init(ll, tm);
666 ll->
commit = disk_ll_commit;
683 void *root_le,
size_t len)
689 DMERR(
"sm_metadata root too small");
693 r = sm_ll_init(ll, tm);
702 ll->
commit = disk_ll_commit;