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-transaction-manager.h"
10 #include <linux/bitops.h>
11 #include <linux/device-mapper.h>
13 #define DM_MSG_PREFIX "space map common"
15 /*----------------------------------------------------------------*/
20 #define INDEX_CSUM_XOR 160478
22 static void index_prepare_for_write(struct dm_block_validator
*v
,
26 struct disk_metadata_index
*mi_le
= dm_block_data(b
);
28 mi_le
->blocknr
= cpu_to_le64(dm_block_location(b
));
29 mi_le
->csum
= cpu_to_le32(dm_bm_checksum(&mi_le
->padding
,
30 block_size
- sizeof(__le32
),
34 static int index_check(struct dm_block_validator
*v
,
38 struct disk_metadata_index
*mi_le
= dm_block_data(b
);
41 if (dm_block_location(b
) != le64_to_cpu(mi_le
->blocknr
)) {
42 DMERR("index_check failed blocknr %llu wanted %llu",
43 le64_to_cpu(mi_le
->blocknr
), dm_block_location(b
));
47 csum_disk
= cpu_to_le32(dm_bm_checksum(&mi_le
->padding
,
48 block_size
- sizeof(__le32
),
50 if (csum_disk
!= mi_le
->csum
) {
51 DMERR("index_check failed csum %u wanted %u",
52 le32_to_cpu(csum_disk
), le32_to_cpu(mi_le
->csum
));
59 static struct dm_block_validator index_validator
= {
61 .prepare_for_write
= index_prepare_for_write
,
65 /*----------------------------------------------------------------*/
70 #define BITMAP_CSUM_XOR 240779
72 static void bitmap_prepare_for_write(struct dm_block_validator
*v
,
76 struct disk_bitmap_header
*disk_header
= dm_block_data(b
);
78 disk_header
->blocknr
= cpu_to_le64(dm_block_location(b
));
79 disk_header
->csum
= cpu_to_le32(dm_bm_checksum(&disk_header
->not_used
,
80 block_size
- sizeof(__le32
),
84 static int bitmap_check(struct dm_block_validator
*v
,
88 struct disk_bitmap_header
*disk_header
= dm_block_data(b
);
91 if (dm_block_location(b
) != le64_to_cpu(disk_header
->blocknr
)) {
92 DMERR("bitmap check failed blocknr %llu wanted %llu",
93 le64_to_cpu(disk_header
->blocknr
), dm_block_location(b
));
97 csum_disk
= cpu_to_le32(dm_bm_checksum(&disk_header
->not_used
,
98 block_size
- sizeof(__le32
),
100 if (csum_disk
!= disk_header
->csum
) {
101 DMERR("bitmap check failed csum %u wanted %u",
102 le32_to_cpu(csum_disk
), le32_to_cpu(disk_header
->csum
));
109 static struct dm_block_validator dm_sm_bitmap_validator
= {
111 .prepare_for_write
= bitmap_prepare_for_write
,
112 .check
= bitmap_check
115 /*----------------------------------------------------------------*/
117 #define ENTRIES_PER_WORD 32
118 #define ENTRIES_SHIFT 5
120 static void *dm_bitmap_data(struct dm_block
*b
)
122 return dm_block_data(b
) + sizeof(struct disk_bitmap_header
);
125 #define WORD_MASK_HIGH 0xAAAAAAAAAAAAAAAAULL
127 static unsigned bitmap_word_used(void *addr
, unsigned b
)
129 __le64
*words_le
= addr
;
130 __le64
*w_le
= words_le
+ (b
>> ENTRIES_SHIFT
);
132 uint64_t bits
= le64_to_cpu(*w_le
);
133 uint64_t mask
= (bits
+ WORD_MASK_HIGH
+ 1) & WORD_MASK_HIGH
;
135 return !(~bits
& mask
);
138 static unsigned sm_lookup_bitmap(void *addr
, unsigned b
)
140 __le64
*words_le
= addr
;
141 __le64
*w_le
= words_le
+ (b
>> ENTRIES_SHIFT
);
144 b
= (b
& (ENTRIES_PER_WORD
- 1)) << 1;
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
)
152 __le64
*words_le
= addr
;
153 __le64
*w_le
= words_le
+ (b
>> ENTRIES_SHIFT
);
155 b
= (b
& (ENTRIES_PER_WORD
- 1)) << 1;
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
) {
172 if (!(begin
& (ENTRIES_PER_WORD
- 1)) &&
173 bitmap_word_used(addr
, begin
)) {
174 begin
+= ENTRIES_PER_WORD
;
178 if (!sm_lookup_bitmap(addr
, begin
)) {
189 /*----------------------------------------------------------------*/
191 static int sm_ll_init(struct ll_disk
*ll
, struct dm_transaction_manager
*tm
)
195 ll
->bitmap_info
.tm
= tm
;
196 ll
->bitmap_info
.levels
= 1;
199 * Because the new bitmap blocks are created via a shadow
200 * operation, the old entry has already had its reference count
201 * decremented and we don't need the btree to do any bookkeeping.
203 ll
->bitmap_info
.value_type
.size
= sizeof(struct disk_index_entry
);
204 ll
->bitmap_info
.value_type
.inc
= NULL
;
205 ll
->bitmap_info
.value_type
.dec
= NULL
;
206 ll
->bitmap_info
.value_type
.equal
= NULL
;
208 ll
->ref_count_info
.tm
= tm
;
209 ll
->ref_count_info
.levels
= 1;
210 ll
->ref_count_info
.value_type
.size
= sizeof(uint32_t);
211 ll
->ref_count_info
.value_type
.inc
= NULL
;
212 ll
->ref_count_info
.value_type
.dec
= NULL
;
213 ll
->ref_count_info
.value_type
.equal
= NULL
;
215 ll
->block_size
= dm_bm_block_size(dm_tm_get_bm(tm
));
217 if (ll
->block_size
> (1 << 30)) {
218 DMERR("block size too big to hold bitmaps");
222 ll
->entries_per_block
= (ll
->block_size
- sizeof(struct disk_bitmap_header
)) *
226 ll
->ref_count_root
= 0;
231 int sm_ll_extend(struct ll_disk
*ll
, dm_block_t extra_blocks
)
234 dm_block_t i
, nr_blocks
, nr_indexes
;
235 unsigned old_blocks
, blocks
;
237 nr_blocks
= ll
->nr_blocks
+ extra_blocks
;
238 old_blocks
= dm_sector_div_up(ll
->nr_blocks
, ll
->entries_per_block
);
239 blocks
= dm_sector_div_up(nr_blocks
, ll
->entries_per_block
);
241 nr_indexes
= dm_sector_div_up(nr_blocks
, ll
->entries_per_block
);
242 if (nr_indexes
> ll
->max_entries(ll
)) {
243 DMERR("space map too large");
247 for (i
= old_blocks
; i
< blocks
; i
++) {
249 struct disk_index_entry idx
;
251 r
= dm_tm_new_block(ll
->tm
, &dm_sm_bitmap_validator
, &b
);
254 idx
.blocknr
= cpu_to_le64(dm_block_location(b
));
256 r
= dm_tm_unlock(ll
->tm
, b
);
260 idx
.nr_free
= cpu_to_le32(ll
->entries_per_block
);
261 idx
.none_free_before
= 0;
263 r
= ll
->save_ie(ll
, i
, &idx
);
268 ll
->nr_blocks
= nr_blocks
;
272 int sm_ll_lookup_bitmap(struct ll_disk
*ll
, dm_block_t b
, uint32_t *result
)
275 dm_block_t index
= b
;
276 struct disk_index_entry ie_disk
;
277 struct dm_block
*blk
;
279 b
= do_div(index
, ll
->entries_per_block
);
280 r
= ll
->load_ie(ll
, index
, &ie_disk
);
284 r
= dm_tm_read_lock(ll
->tm
, le64_to_cpu(ie_disk
.blocknr
),
285 &dm_sm_bitmap_validator
, &blk
);
289 *result
= sm_lookup_bitmap(dm_bitmap_data(blk
), b
);
291 return dm_tm_unlock(ll
->tm
, blk
);
294 int sm_ll_lookup(struct ll_disk
*ll
, dm_block_t b
, uint32_t *result
)
297 int r
= sm_ll_lookup_bitmap(ll
, b
, result
);
305 r
= dm_btree_lookup(&ll
->ref_count_info
, ll
->ref_count_root
, &b
, &le_rc
);
309 *result
= le32_to_cpu(le_rc
);
314 int sm_ll_find_free_block(struct ll_disk
*ll
, dm_block_t begin
,
315 dm_block_t end
, dm_block_t
*result
)
318 struct disk_index_entry ie_disk
;
319 dm_block_t i
, index_begin
= begin
;
320 dm_block_t index_end
= dm_sector_div_up(end
, ll
->entries_per_block
);
325 begin
= do_div(index_begin
, ll
->entries_per_block
);
326 end
= do_div(end
, ll
->entries_per_block
);
328 for (i
= index_begin
; i
< index_end
; i
++, begin
= 0) {
329 struct dm_block
*blk
;
333 r
= ll
->load_ie(ll
, i
, &ie_disk
);
337 if (le32_to_cpu(ie_disk
.nr_free
) == 0)
340 r
= dm_tm_read_lock(ll
->tm
, le64_to_cpu(ie_disk
.blocknr
),
341 &dm_sm_bitmap_validator
, &blk
);
345 bit_end
= (i
== index_end
- 1) ? end
: ll
->entries_per_block
;
347 r
= sm_find_free(dm_bitmap_data(blk
),
348 max_t(unsigned, begin
, le32_to_cpu(ie_disk
.none_free_before
)),
352 * This might happen because we started searching
353 * part way through the bitmap.
355 dm_tm_unlock(ll
->tm
, blk
);
359 dm_tm_unlock(ll
->tm
, blk
);
363 r
= dm_tm_unlock(ll
->tm
, blk
);
367 *result
= i
* ll
->entries_per_block
+ (dm_block_t
) position
;
374 int sm_ll_insert(struct ll_disk
*ll
, dm_block_t b
,
375 uint32_t ref_count
, enum allocation_event
*ev
)
380 dm_block_t index
= b
;
381 struct disk_index_entry ie_disk
;
385 bit
= do_div(index
, ll
->entries_per_block
);
386 r
= ll
->load_ie(ll
, index
, &ie_disk
);
390 r
= dm_tm_shadow_block(ll
->tm
, le64_to_cpu(ie_disk
.blocknr
),
391 &dm_sm_bitmap_validator
, &nb
, &inc
);
393 DMERR("dm_tm_shadow_block() failed");
396 ie_disk
.blocknr
= cpu_to_le64(dm_block_location(nb
));
398 bm_le
= dm_bitmap_data(nb
);
399 old
= sm_lookup_bitmap(bm_le
, bit
);
401 if (ref_count
<= 2) {
402 sm_set_bitmap(bm_le
, bit
, ref_count
);
404 r
= dm_tm_unlock(ll
->tm
, nb
);
409 r
= dm_btree_remove(&ll
->ref_count_info
,
411 &b
, &ll
->ref_count_root
);
417 __le32 le_rc
= cpu_to_le32(ref_count
);
419 sm_set_bitmap(bm_le
, bit
, 3);
420 r
= dm_tm_unlock(ll
->tm
, nb
);
424 __dm_bless_for_disk(&le_rc
);
425 r
= dm_btree_insert(&ll
->ref_count_info
, ll
->ref_count_root
,
426 &b
, &le_rc
, &ll
->ref_count_root
);
428 DMERR("ref count insert failed");
433 if (ref_count
&& !old
) {
436 ie_disk
.nr_free
= cpu_to_le32(le32_to_cpu(ie_disk
.nr_free
) - 1);
437 if (le32_to_cpu(ie_disk
.none_free_before
) == bit
)
438 ie_disk
.none_free_before
= cpu_to_le32(bit
+ 1);
440 } else if (old
&& !ref_count
) {
443 ie_disk
.nr_free
= cpu_to_le32(le32_to_cpu(ie_disk
.nr_free
) + 1);
444 ie_disk
.none_free_before
= cpu_to_le32(min(le32_to_cpu(ie_disk
.none_free_before
), bit
));
447 return ll
->save_ie(ll
, index
, &ie_disk
);
450 int sm_ll_inc(struct ll_disk
*ll
, dm_block_t b
, enum allocation_event
*ev
)
455 r
= sm_ll_lookup(ll
, b
, &rc
);
459 return sm_ll_insert(ll
, b
, rc
+ 1, ev
);
462 int sm_ll_dec(struct ll_disk
*ll
, dm_block_t b
, enum allocation_event
*ev
)
467 r
= sm_ll_lookup(ll
, b
, &rc
);
474 return sm_ll_insert(ll
, b
, rc
- 1, ev
);
477 int sm_ll_commit(struct ll_disk
*ll
)
479 return ll
->commit(ll
);
482 /*----------------------------------------------------------------*/
484 static int metadata_ll_load_ie(struct ll_disk
*ll
, dm_block_t index
,
485 struct disk_index_entry
*ie
)
487 memcpy(ie
, ll
->mi_le
.index
+ index
, sizeof(*ie
));
491 static int metadata_ll_save_ie(struct ll_disk
*ll
, dm_block_t index
,
492 struct disk_index_entry
*ie
)
494 memcpy(ll
->mi_le
.index
+ index
, ie
, sizeof(*ie
));
498 static int metadata_ll_init_index(struct ll_disk
*ll
)
503 r
= dm_tm_new_block(ll
->tm
, &index_validator
, &b
);
507 memcpy(dm_block_data(b
), &ll
->mi_le
, sizeof(ll
->mi_le
));
508 ll
->bitmap_root
= dm_block_location(b
);
510 return dm_tm_unlock(ll
->tm
, b
);
513 static int metadata_ll_open(struct ll_disk
*ll
)
516 struct dm_block
*block
;
518 r
= dm_tm_read_lock(ll
->tm
, ll
->bitmap_root
,
519 &index_validator
, &block
);
523 memcpy(&ll
->mi_le
, dm_block_data(block
), sizeof(ll
->mi_le
));
524 return dm_tm_unlock(ll
->tm
, block
);
527 static dm_block_t
metadata_ll_max_entries(struct ll_disk
*ll
)
529 return MAX_METADATA_BITMAPS
;
532 static int metadata_ll_commit(struct ll_disk
*ll
)
537 r
= dm_tm_shadow_block(ll
->tm
, ll
->bitmap_root
, &index_validator
, &b
, &inc
);
541 memcpy(dm_block_data(b
), &ll
->mi_le
, sizeof(ll
->mi_le
));
542 ll
->bitmap_root
= dm_block_location(b
);
544 return dm_tm_unlock(ll
->tm
, b
);
547 int sm_ll_new_metadata(struct ll_disk
*ll
, struct dm_transaction_manager
*tm
)
551 r
= sm_ll_init(ll
, tm
);
555 ll
->load_ie
= metadata_ll_load_ie
;
556 ll
->save_ie
= metadata_ll_save_ie
;
557 ll
->init_index
= metadata_ll_init_index
;
558 ll
->open_index
= metadata_ll_open
;
559 ll
->max_entries
= metadata_ll_max_entries
;
560 ll
->commit
= metadata_ll_commit
;
563 ll
->nr_allocated
= 0;
565 r
= ll
->init_index(ll
);
569 r
= dm_btree_empty(&ll
->ref_count_info
, &ll
->ref_count_root
);
576 int sm_ll_open_metadata(struct ll_disk
*ll
, struct dm_transaction_manager
*tm
,
577 void *root_le
, size_t len
)
580 struct disk_sm_root
*smr
= root_le
;
582 if (len
< sizeof(struct disk_sm_root
)) {
583 DMERR("sm_metadata root too small");
587 r
= sm_ll_init(ll
, tm
);
591 ll
->load_ie
= metadata_ll_load_ie
;
592 ll
->save_ie
= metadata_ll_save_ie
;
593 ll
->init_index
= metadata_ll_init_index
;
594 ll
->open_index
= metadata_ll_open
;
595 ll
->max_entries
= metadata_ll_max_entries
;
596 ll
->commit
= metadata_ll_commit
;
598 ll
->nr_blocks
= le64_to_cpu(smr
->nr_blocks
);
599 ll
->nr_allocated
= le64_to_cpu(smr
->nr_allocated
);
600 ll
->bitmap_root
= le64_to_cpu(smr
->bitmap_root
);
601 ll
->ref_count_root
= le64_to_cpu(smr
->ref_count_root
);
603 return ll
->open_index(ll
);
606 /*----------------------------------------------------------------*/
608 static int disk_ll_load_ie(struct ll_disk
*ll
, dm_block_t index
,
609 struct disk_index_entry
*ie
)
611 return dm_btree_lookup(&ll
->bitmap_info
, ll
->bitmap_root
, &index
, ie
);
614 static int disk_ll_save_ie(struct ll_disk
*ll
, dm_block_t index
,
615 struct disk_index_entry
*ie
)
617 __dm_bless_for_disk(ie
);
618 return dm_btree_insert(&ll
->bitmap_info
, ll
->bitmap_root
,
619 &index
, ie
, &ll
->bitmap_root
);
622 static int disk_ll_init_index(struct ll_disk
*ll
)
624 return dm_btree_empty(&ll
->bitmap_info
, &ll
->bitmap_root
);
627 static int disk_ll_open(struct ll_disk
*ll
)
633 static dm_block_t
disk_ll_max_entries(struct ll_disk
*ll
)
638 static int disk_ll_commit(struct ll_disk
*ll
)
643 int sm_ll_new_disk(struct ll_disk
*ll
, struct dm_transaction_manager
*tm
)
647 r
= sm_ll_init(ll
, tm
);
651 ll
->load_ie
= disk_ll_load_ie
;
652 ll
->save_ie
= disk_ll_save_ie
;
653 ll
->init_index
= disk_ll_init_index
;
654 ll
->open_index
= disk_ll_open
;
655 ll
->max_entries
= disk_ll_max_entries
;
656 ll
->commit
= disk_ll_commit
;
659 ll
->nr_allocated
= 0;
661 r
= ll
->init_index(ll
);
665 r
= dm_btree_empty(&ll
->ref_count_info
, &ll
->ref_count_root
);
672 int sm_ll_open_disk(struct ll_disk
*ll
, struct dm_transaction_manager
*tm
,
673 void *root_le
, size_t len
)
676 struct disk_sm_root
*smr
= root_le
;
678 if (len
< sizeof(struct disk_sm_root
)) {
679 DMERR("sm_metadata root too small");
683 r
= sm_ll_init(ll
, tm
);
687 ll
->load_ie
= disk_ll_load_ie
;
688 ll
->save_ie
= disk_ll_save_ie
;
689 ll
->init_index
= disk_ll_init_index
;
690 ll
->open_index
= disk_ll_open
;
691 ll
->max_entries
= disk_ll_max_entries
;
692 ll
->commit
= disk_ll_commit
;
694 ll
->nr_blocks
= le64_to_cpu(smr
->nr_blocks
);
695 ll
->nr_allocated
= le64_to_cpu(smr
->nr_allocated
);
696 ll
->bitmap_root
= le64_to_cpu(smr
->bitmap_root
);
697 ll
->ref_count_root
= le64_to_cpu(smr
->ref_count_root
);
699 return ll
->open_index(ll
);
702 /*----------------------------------------------------------------*/