2 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
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/bitops.h>
15 #include <linux/export.h>
16 #include <linux/device-mapper.h>
18 #define DM_MSG_PREFIX "space map disk"
23 static void bitmap_prepare_for_write(struct dm_block_validator
*v
,
27 struct disk_bitmap_header
*disk_header
= dm_block_data(b
);
29 disk_header
->blocknr
= cpu_to_le64(dm_block_location(b
));
30 disk_header
->csum
= cpu_to_le32(dm_block_csum_data(&disk_header
->not_used
, block_size
- sizeof(__le32
)));
33 static int bitmap_check(struct dm_block_validator
*v
,
37 struct disk_bitmap_header
*disk_header
= dm_block_data(b
);
40 if (dm_block_location(b
) != le64_to_cpu(disk_header
->blocknr
)) {
41 DMERR("bitmap check failed blocknr %llu wanted %llu",
42 le64_to_cpu(disk_header
->blocknr
), dm_block_location(b
));
46 csum_disk
= cpu_to_le32(dm_block_csum_data(&disk_header
->not_used
, block_size
- sizeof(__le32
)));
47 if (csum_disk
!= disk_header
->csum
) {
48 DMERR("bitmap check failed csum %u wanted %u",
49 le32_to_cpu(csum_disk
), le32_to_cpu(disk_header
->csum
));
56 struct dm_block_validator dm_sm_bitmap_validator
= {
58 .prepare_for_write
= bitmap_prepare_for_write
,
62 /*----------------------------------------------------------------*/
64 #define ENTRIES_PER_WORD 32
65 #define ENTRIES_SHIFT 5
67 void *dm_bitmap_data(struct dm_block
*b
)
69 return dm_block_data(b
) + sizeof(struct disk_bitmap_header
);
72 #define WORD_MASK_LOW 0x5555555555555555ULL
73 #define WORD_MASK_HIGH 0xAAAAAAAAAAAAAAAAULL
74 #define WORD_MASK_ALL 0xFFFFFFFFFFFFFFFFULL
76 static unsigned bitmap_word_used(void *addr
, unsigned b
)
78 __le64
*words_le
= addr
;
79 __le64
*w_le
= words_le
+ (b
>> ENTRIES_SHIFT
);
81 uint64_t bits
= le64_to_cpu(*w_le
);
83 return ((bits
& WORD_MASK_LOW
) == WORD_MASK_LOW
||
84 (bits
& WORD_MASK_HIGH
) == WORD_MASK_HIGH
||
85 (bits
& WORD_MASK_ALL
) == WORD_MASK_ALL
);
88 unsigned sm_lookup_bitmap(void *addr
, unsigned b
)
90 __le64
*words_le
= addr
;
91 __le64
*w_le
= words_le
+ (b
>> ENTRIES_SHIFT
);
93 b
= (b
& (ENTRIES_PER_WORD
- 1)) << 1;
95 return (!!test_bit_le(b
, (void *) w_le
) << 1) |
96 (!!test_bit_le(b
+ 1, (void *) w_le
));
99 void sm_set_bitmap(void *addr
, unsigned b
, unsigned val
)
101 __le64
*words_le
= addr
;
102 __le64
*w_le
= words_le
+ (b
>> ENTRIES_SHIFT
);
104 b
= (b
& (ENTRIES_PER_WORD
- 1)) << 1;
107 __set_bit_le(b
, (void *) w_le
);
109 __clear_bit_le(b
, (void *) w_le
);
112 __set_bit_le(b
+ 1, (void *) w_le
);
114 __clear_bit_le(b
+ 1, (void *) w_le
);
117 int sm_find_free(void *addr
, unsigned begin
, unsigned end
,
120 while (begin
< end
) {
121 if (!(begin
& (ENTRIES_PER_WORD
- 1)) &&
122 bitmap_word_used(addr
, begin
)) {
123 begin
+= ENTRIES_PER_WORD
;
127 if (!sm_lookup_bitmap(addr
, begin
)) {
138 static int disk_ll_init(struct ll_disk
*io
, struct dm_transaction_manager
*tm
)
141 io
->bitmap_info
.tm
= tm
;
142 io
->bitmap_info
.levels
= 1;
145 * Because the new bitmap blocks are created via a shadow
146 * operation, the old entry has already had its reference count
147 * decremented and we don't need the btree to do any bookkeeping.
149 io
->bitmap_info
.value_type
.size
= sizeof(struct disk_index_entry
);
150 io
->bitmap_info
.value_type
.inc
= NULL
;
151 io
->bitmap_info
.value_type
.dec
= NULL
;
152 io
->bitmap_info
.value_type
.equal
= NULL
;
154 io
->ref_count_info
.tm
= tm
;
155 io
->ref_count_info
.levels
= 1;
156 io
->ref_count_info
.value_type
.size
= sizeof(uint32_t);
157 io
->ref_count_info
.value_type
.inc
= NULL
;
158 io
->ref_count_info
.value_type
.dec
= NULL
;
159 io
->ref_count_info
.value_type
.equal
= NULL
;
161 io
->block_size
= dm_bm_block_size(dm_tm_get_bm(tm
));
163 if (io
->block_size
> (1 << 30)) {
164 DMERR("block size too big to hold bitmaps");
168 io
->entries_per_block
= (io
->block_size
- sizeof(struct disk_bitmap_header
)) *
172 io
->ref_count_root
= 0;
177 static int disk_ll_new(struct ll_disk
*io
, struct dm_transaction_manager
*tm
)
181 r
= disk_ll_init(io
, tm
);
186 io
->nr_allocated
= 0;
187 r
= dm_btree_create(&io
->bitmap_info
, &io
->bitmap_root
);
191 r
= dm_btree_create(&io
->ref_count_info
, &io
->ref_count_root
);
193 dm_btree_destroy(&io
->bitmap_info
, io
->bitmap_root
);
200 static int disk_ll_extend(struct ll_disk
*io
, dm_block_t extra_blocks
)
203 dm_block_t i
, nr_blocks
;
204 unsigned old_blocks
, blocks
;
206 nr_blocks
= io
->nr_blocks
+ extra_blocks
;
207 old_blocks
= dm_sector_div_up(io
->nr_blocks
, io
->entries_per_block
);
208 blocks
= dm_sector_div_up(nr_blocks
, io
->entries_per_block
);
210 for (i
= old_blocks
; i
< blocks
; i
++) {
212 struct disk_index_entry idx
;
214 r
= dm_tm_new_block(io
->tm
, &dm_sm_bitmap_validator
, &b
);
217 idx
.blocknr
= cpu_to_le64(dm_block_location(b
));
219 r
= dm_tm_unlock(io
->tm
, b
);
223 idx
.nr_free
= cpu_to_le32(io
->entries_per_block
);
224 idx
.none_free_before
= 0;
225 __dm_bless_for_disk(&idx
);
227 r
= dm_btree_insert(&io
->bitmap_info
, io
->bitmap_root
,
228 &i
, &idx
, &io
->bitmap_root
);
233 io
->nr_blocks
= nr_blocks
;
237 static int disk_ll_open(struct ll_disk
*ll
, struct dm_transaction_manager
*tm
,
238 void *root_le
, size_t len
)
241 struct disk_sm_root
*smr
= root_le
;
243 if (len
< sizeof(struct disk_sm_root
)) {
244 DMERR("sm_disk root too small");
248 r
= disk_ll_init(ll
, tm
);
252 ll
->nr_blocks
= le64_to_cpu(smr
->nr_blocks
);
253 ll
->nr_allocated
= le64_to_cpu(smr
->nr_allocated
);
254 ll
->bitmap_root
= le64_to_cpu(smr
->bitmap_root
);
255 ll
->ref_count_root
= le64_to_cpu(smr
->ref_count_root
);
260 static int disk_ll_lookup_bitmap(struct ll_disk
*io
, dm_block_t b
, uint32_t *result
)
263 dm_block_t index
= b
;
264 struct disk_index_entry ie_disk
;
265 struct dm_block
*blk
;
267 do_div(index
, io
->entries_per_block
);
268 r
= dm_btree_lookup(&io
->bitmap_info
, io
->bitmap_root
, &index
, &ie_disk
);
272 r
= dm_tm_read_lock(io
->tm
, le64_to_cpu(ie_disk
.blocknr
), &dm_sm_bitmap_validator
, &blk
);
276 *result
= sm_lookup_bitmap(dm_bitmap_data(blk
), do_div(b
, io
->entries_per_block
));
278 return dm_tm_unlock(io
->tm
, blk
);
281 static int disk_ll_lookup(struct ll_disk
*io
, dm_block_t b
, uint32_t *result
)
284 int r
= disk_ll_lookup_bitmap(io
, b
, result
);
292 r
= dm_btree_lookup(&io
->ref_count_info
, io
->ref_count_root
, &b
, &rc_le
);
296 *result
= le32_to_cpu(rc_le
);
301 static int disk_ll_find_free_block(struct ll_disk
*io
, dm_block_t begin
,
302 dm_block_t end
, dm_block_t
*result
)
305 struct disk_index_entry ie_disk
;
306 dm_block_t i
, index_begin
= begin
;
307 dm_block_t index_end
= dm_sector_div_up(end
, io
->entries_per_block
);
309 begin
= do_div(index_begin
, io
->entries_per_block
);
311 for (i
= index_begin
; i
< index_end
; i
++, begin
= 0) {
312 struct dm_block
*blk
;
316 r
= dm_btree_lookup(&io
->bitmap_info
, io
->bitmap_root
, &i
, &ie_disk
);
320 if (le32_to_cpu(ie_disk
.nr_free
) <= 0)
323 r
= dm_tm_read_lock(io
->tm
, le64_to_cpu(ie_disk
.blocknr
),
324 &dm_sm_bitmap_validator
, &blk
);
328 bit_end
= (i
== index_end
- 1) ?
329 do_div(end
, io
->entries_per_block
) : io
->entries_per_block
;
331 r
= sm_find_free(dm_bitmap_data(blk
),
332 max((unsigned)begin
, (unsigned)le32_to_cpu(ie_disk
.none_free_before
)),
335 dm_tm_unlock(io
->tm
, blk
);
339 r
= dm_tm_unlock(io
->tm
, blk
);
343 *result
= i
* io
->entries_per_block
+ (dm_block_t
) position
;
351 static int disk_ll_insert(struct ll_disk
*io
, dm_block_t b
, uint32_t ref_count
)
356 dm_block_t index
= b
;
357 struct disk_index_entry ie_disk
;
361 do_div(index
, io
->entries_per_block
);
362 r
= dm_btree_lookup(&io
->bitmap_info
, io
->bitmap_root
, &index
, &ie_disk
);
366 r
= dm_tm_shadow_block(io
->tm
, le64_to_cpu(ie_disk
.blocknr
),
367 &dm_sm_bitmap_validator
, &nb
, &inc
);
369 DMERR("dm_tm_shadow_block() failed");
372 ie_disk
.blocknr
= cpu_to_le64(dm_block_location(nb
));
374 bm_le
= dm_bitmap_data(nb
);
375 bit
= do_div(b
, io
->entries_per_block
);
376 old
= sm_lookup_bitmap(bm_le
, bit
);
378 if (ref_count
<= 2) {
379 sm_set_bitmap(bm_le
, bit
, ref_count
);
382 r
= dm_btree_remove(&io
->ref_count_info
, io
->ref_count_root
,
383 &b
, &io
->ref_count_root
);
385 dm_tm_unlock(io
->tm
, nb
);
390 __le32 rc_le
= cpu_to_le32(ref_count
);
392 __dm_bless_for_disk(&rc_le
);
394 sm_set_bitmap(bm_le
, bit
, 3);
395 r
= dm_btree_insert(&io
->ref_count_info
, io
->ref_count_root
,
396 &b
, &rc_le
, &io
->ref_count_root
);
398 dm_tm_unlock(io
->tm
, nb
);
399 DMERR("ref count insert failed");
404 r
= dm_tm_unlock(io
->tm
, nb
);
408 if (ref_count
&& !old
) {
410 ie_disk
.nr_free
= cpu_to_le32(le32_to_cpu(ie_disk
.nr_free
) - 1);
411 if (le32_to_cpu(ie_disk
.none_free_before
) == b
)
412 ie_disk
.none_free_before
= cpu_to_le32(b
+ 1);
414 } else if (old
&& !ref_count
) {
416 ie_disk
.nr_free
= cpu_to_le32(le32_to_cpu(ie_disk
.nr_free
) + 1);
417 ie_disk
.none_free_before
= cpu_to_le32(min((dm_block_t
) le32_to_cpu(ie_disk
.none_free_before
), b
));
420 __dm_bless_for_disk(&ie_disk
);
422 r
= dm_btree_insert(&io
->bitmap_info
, io
->bitmap_root
, &index
, &ie_disk
, &io
->bitmap_root
);
429 static int disk_ll_inc(struct ll_disk
*ll
, dm_block_t b
)
434 r
= disk_ll_lookup(ll
, b
, &rc
);
438 return disk_ll_insert(ll
, b
, rc
+ 1);
441 static int disk_ll_dec(struct ll_disk
*ll
, dm_block_t b
)
446 r
= disk_ll_lookup(ll
, b
, &rc
);
453 return disk_ll_insert(ll
, b
, rc
- 1);
456 /*--------------------------------------------------------------*/
459 * Space map interface.
462 struct dm_space_map sm
;
467 static void sm_disk_destroy(struct dm_space_map
*sm
)
469 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
474 static int sm_disk_extend(struct dm_space_map
*sm
, dm_block_t extra_blocks
)
476 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
478 return disk_ll_extend(&smd
->ll
, extra_blocks
);
481 static int sm_disk_get_nr_blocks(struct dm_space_map
*sm
, dm_block_t
*count
)
483 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
485 *count
= smd
->ll
.nr_blocks
;
490 static int sm_disk_get_nr_free(struct dm_space_map
*sm
, dm_block_t
*count
)
492 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
494 *count
= smd
->ll
.nr_blocks
- smd
->ll
.nr_allocated
;
499 static int sm_disk_get_count(struct dm_space_map
*sm
, dm_block_t b
,
502 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
504 return disk_ll_lookup(&smd
->ll
, b
, result
);
507 static int sm_disk_count_is_more_than_one(struct dm_space_map
*sm
, dm_block_t b
,
513 r
= sm_disk_get_count(sm
, b
, &count
);
520 static int sm_disk_set_count(struct dm_space_map
*sm
, dm_block_t b
,
523 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
525 return disk_ll_insert(&smd
->ll
, b
, count
);
528 static int sm_disk_inc_block(struct dm_space_map
*sm
, dm_block_t b
)
530 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
532 return disk_ll_inc(&smd
->ll
, b
);
535 static int sm_disk_dec_block(struct dm_space_map
*sm
, dm_block_t b
)
537 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
539 return disk_ll_dec(&smd
->ll
, b
);
542 static int sm_disk_new_block(struct dm_space_map
*sm
, dm_block_t
*b
)
545 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
548 * FIXME: We should start the search where we left off.
550 r
= disk_ll_find_free_block(&smd
->ll
, 0, smd
->ll
.nr_blocks
, b
);
554 return disk_ll_inc(&smd
->ll
, *b
);
557 static int sm_disk_commit(struct dm_space_map
*sm
)
562 static int sm_disk_root_size(struct dm_space_map
*sm
, size_t *result
)
564 *result
= sizeof(struct disk_sm_root
);
569 static int sm_disk_copy_root(struct dm_space_map
*sm
, void *where_le
, size_t max
)
571 struct sm_disk
*smd
= container_of(sm
, struct sm_disk
, sm
);
572 struct disk_sm_root root_le
;
574 root_le
.nr_blocks
= cpu_to_le64(smd
->ll
.nr_blocks
);
575 root_le
.nr_allocated
= cpu_to_le64(smd
->ll
.nr_allocated
);
576 root_le
.bitmap_root
= cpu_to_le64(smd
->ll
.bitmap_root
);
577 root_le
.ref_count_root
= cpu_to_le64(smd
->ll
.ref_count_root
);
579 if (max
< sizeof(root_le
))
582 memcpy(where_le
, &root_le
, sizeof(root_le
));
587 /*----------------------------------------------------------------*/
589 static struct dm_space_map ops
= {
590 .destroy
= sm_disk_destroy
,
591 .extend
= sm_disk_extend
,
592 .get_nr_blocks
= sm_disk_get_nr_blocks
,
593 .get_nr_free
= sm_disk_get_nr_free
,
594 .get_count
= sm_disk_get_count
,
595 .count_is_more_than_one
= sm_disk_count_is_more_than_one
,
596 .set_count
= sm_disk_set_count
,
597 .inc_block
= sm_disk_inc_block
,
598 .dec_block
= sm_disk_dec_block
,
599 .new_block
= sm_disk_new_block
,
600 .commit
= sm_disk_commit
,
601 .root_size
= sm_disk_root_size
,
602 .copy_root
= sm_disk_copy_root
605 struct dm_space_map
*dm_sm_disk_create(struct dm_transaction_manager
*tm
,
606 dm_block_t nr_blocks
)
611 smd
= kmalloc(sizeof(*smd
), GFP_KERNEL
);
613 return ERR_PTR(-ENOMEM
);
615 memcpy(&smd
->sm
, &ops
, sizeof(smd
->sm
));
617 r
= disk_ll_new(&smd
->ll
, tm
);
621 r
= disk_ll_extend(&smd
->ll
, nr_blocks
);
625 r
= sm_disk_commit(&smd
->sm
);
635 EXPORT_SYMBOL_GPL(dm_sm_disk_create
);
637 struct dm_space_map
*dm_sm_disk_open(struct dm_transaction_manager
*tm
,
638 void *root_le
, size_t len
)
643 smd
= kmalloc(sizeof(*smd
), GFP_KERNEL
);
645 return ERR_PTR(-ENOMEM
);
647 memcpy(&smd
->sm
, &ops
, sizeof(smd
->sm
));
649 r
= disk_ll_open(&smd
->ll
, tm
, root_le
, len
);
653 r
= sm_disk_commit(&smd
->sm
);
663 EXPORT_SYMBOL_GPL(dm_sm_disk_open
);