Linux 3.4.102
[linux/fpc-iii.git] / drivers / md / persistent-data / dm-space-map-common.c
blob79c02d90ce39d5d2d404a4f8902dbd9bd2970b7c
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
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 /*----------------------------------------------------------------*/
18 * Index validator.
20 #define INDEX_CSUM_XOR 160478
22 static void index_prepare_for_write(struct dm_block_validator *v,
23 struct dm_block *b,
24 size_t block_size)
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),
31 INDEX_CSUM_XOR));
34 static int index_check(struct dm_block_validator *v,
35 struct dm_block *b,
36 size_t block_size)
38 struct disk_metadata_index *mi_le = dm_block_data(b);
39 __le32 csum_disk;
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));
44 return -ENOTBLK;
47 csum_disk = cpu_to_le32(dm_bm_checksum(&mi_le->padding,
48 block_size - sizeof(__le32),
49 INDEX_CSUM_XOR));
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));
53 return -EILSEQ;
56 return 0;
59 static struct dm_block_validator index_validator = {
60 .name = "index",
61 .prepare_for_write = index_prepare_for_write,
62 .check = index_check
65 /*----------------------------------------------------------------*/
68 * Bitmap validator
70 #define BITMAP_CSUM_XOR 240779
72 static void bitmap_prepare_for_write(struct dm_block_validator *v,
73 struct dm_block *b,
74 size_t block_size)
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),
81 BITMAP_CSUM_XOR));
84 static int bitmap_check(struct dm_block_validator *v,
85 struct dm_block *b,
86 size_t block_size)
88 struct disk_bitmap_header *disk_header = dm_block_data(b);
89 __le32 csum_disk;
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));
94 return -ENOTBLK;
97 csum_disk = cpu_to_le32(dm_bm_checksum(&disk_header->not_used,
98 block_size - sizeof(__le32),
99 BITMAP_CSUM_XOR));
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));
103 return -EILSEQ;
106 return 0;
109 static struct dm_block_validator dm_sm_bitmap_validator = {
110 .name = "sm_bitmap",
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);
142 unsigned hi, lo;
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;
157 if (val & 2)
158 __set_bit_le(b, (void *) w_le);
159 else
160 __clear_bit_le(b, (void *) w_le);
162 if (val & 1)
163 __set_bit_le(b + 1, (void *) w_le);
164 else
165 __clear_bit_le(b + 1, (void *) w_le);
168 static int sm_find_free(void *addr, unsigned begin, unsigned end,
169 unsigned *result)
171 while (begin < end) {
172 if (!(begin & (ENTRIES_PER_WORD - 1)) &&
173 bitmap_word_used(addr, begin)) {
174 begin += ENTRIES_PER_WORD;
175 continue;
178 if (!sm_lookup_bitmap(addr, begin)) {
179 *result = begin;
180 return 0;
183 begin++;
186 return -ENOSPC;
189 /*----------------------------------------------------------------*/
191 static int sm_ll_init(struct ll_disk *ll, struct dm_transaction_manager *tm)
193 ll->tm = 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");
219 return -EINVAL;
222 ll->entries_per_block = (ll->block_size - sizeof(struct disk_bitmap_header)) *
223 ENTRIES_PER_BYTE;
224 ll->nr_blocks = 0;
225 ll->bitmap_root = 0;
226 ll->ref_count_root = 0;
228 return 0;
231 int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks)
233 int r;
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");
244 return -EINVAL;
248 * We need to set this before the dm_tm_new_block() call below.
250 ll->nr_blocks = nr_blocks;
251 for (i = old_blocks; i < blocks; i++) {
252 struct dm_block *b;
253 struct disk_index_entry idx;
255 r = dm_tm_new_block(ll->tm, &dm_sm_bitmap_validator, &b);
256 if (r < 0)
257 return r;
259 idx.blocknr = cpu_to_le64(dm_block_location(b));
261 r = dm_tm_unlock(ll->tm, b);
262 if (r < 0)
263 return r;
265 idx.nr_free = cpu_to_le32(ll->entries_per_block);
266 idx.none_free_before = 0;
268 r = ll->save_ie(ll, i, &idx);
269 if (r < 0)
270 return r;
273 return 0;
276 int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result)
278 int r;
279 dm_block_t index = b;
280 struct disk_index_entry ie_disk;
281 struct dm_block *blk;
283 b = do_div(index, ll->entries_per_block);
284 r = ll->load_ie(ll, index, &ie_disk);
285 if (r < 0)
286 return r;
288 r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
289 &dm_sm_bitmap_validator, &blk);
290 if (r < 0)
291 return r;
293 *result = sm_lookup_bitmap(dm_bitmap_data(blk), b);
295 return dm_tm_unlock(ll->tm, blk);
298 int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result)
300 __le32 le_rc;
301 int r = sm_ll_lookup_bitmap(ll, b, result);
303 if (r)
304 return r;
306 if (*result != 3)
307 return r;
309 r = dm_btree_lookup(&ll->ref_count_info, ll->ref_count_root, &b, &le_rc);
310 if (r < 0)
311 return r;
313 *result = le32_to_cpu(le_rc);
315 return r;
318 int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
319 dm_block_t end, dm_block_t *result)
321 int r;
322 struct disk_index_entry ie_disk;
323 dm_block_t i, index_begin = begin;
324 dm_block_t index_end = dm_sector_div_up(end, ll->entries_per_block);
327 * FIXME: Use shifts
329 begin = do_div(index_begin, ll->entries_per_block);
330 end = do_div(end, ll->entries_per_block);
332 for (i = index_begin; i < index_end; i++, begin = 0) {
333 struct dm_block *blk;
334 unsigned position;
335 uint32_t bit_end;
337 r = ll->load_ie(ll, i, &ie_disk);
338 if (r < 0)
339 return r;
341 if (le32_to_cpu(ie_disk.nr_free) == 0)
342 continue;
344 r = dm_tm_read_lock(ll->tm, le64_to_cpu(ie_disk.blocknr),
345 &dm_sm_bitmap_validator, &blk);
346 if (r < 0)
347 return r;
349 bit_end = (i == index_end - 1) ? end : ll->entries_per_block;
351 r = sm_find_free(dm_bitmap_data(blk),
352 max_t(unsigned, begin, le32_to_cpu(ie_disk.none_free_before)),
353 bit_end, &position);
354 if (r == -ENOSPC) {
356 * This might happen because we started searching
357 * part way through the bitmap.
359 dm_tm_unlock(ll->tm, blk);
360 continue;
362 } else if (r < 0) {
363 dm_tm_unlock(ll->tm, blk);
364 return r;
367 r = dm_tm_unlock(ll->tm, blk);
368 if (r < 0)
369 return r;
371 *result = i * ll->entries_per_block + (dm_block_t) position;
372 return 0;
375 return -ENOSPC;
378 int sm_ll_insert(struct ll_disk *ll, dm_block_t b,
379 uint32_t ref_count, enum allocation_event *ev)
381 int r;
382 uint32_t bit, old;
383 struct dm_block *nb;
384 dm_block_t index = b;
385 struct disk_index_entry ie_disk;
386 void *bm_le;
387 int inc;
389 bit = do_div(index, ll->entries_per_block);
390 r = ll->load_ie(ll, index, &ie_disk);
391 if (r < 0)
392 return r;
394 r = dm_tm_shadow_block(ll->tm, le64_to_cpu(ie_disk.blocknr),
395 &dm_sm_bitmap_validator, &nb, &inc);
396 if (r < 0) {
397 DMERR("dm_tm_shadow_block() failed");
398 return r;
400 ie_disk.blocknr = cpu_to_le64(dm_block_location(nb));
402 bm_le = dm_bitmap_data(nb);
403 old = sm_lookup_bitmap(bm_le, bit);
405 if (ref_count <= 2) {
406 sm_set_bitmap(bm_le, bit, ref_count);
408 r = dm_tm_unlock(ll->tm, nb);
409 if (r < 0)
410 return r;
412 if (old > 2) {
413 r = dm_btree_remove(&ll->ref_count_info,
414 ll->ref_count_root,
415 &b, &ll->ref_count_root);
416 if (r)
417 return r;
420 } else {
421 __le32 le_rc = cpu_to_le32(ref_count);
423 sm_set_bitmap(bm_le, bit, 3);
424 r = dm_tm_unlock(ll->tm, nb);
425 if (r < 0)
426 return r;
428 __dm_bless_for_disk(&le_rc);
429 r = dm_btree_insert(&ll->ref_count_info, ll->ref_count_root,
430 &b, &le_rc, &ll->ref_count_root);
431 if (r < 0) {
432 DMERR("ref count insert failed");
433 return r;
437 if (ref_count && !old) {
438 *ev = SM_ALLOC;
439 ll->nr_allocated++;
440 ie_disk.nr_free = cpu_to_le32(le32_to_cpu(ie_disk.nr_free) - 1);
441 if (le32_to_cpu(ie_disk.none_free_before) == bit)
442 ie_disk.none_free_before = cpu_to_le32(bit + 1);
444 } else if (old && !ref_count) {
445 *ev = SM_FREE;
446 ll->nr_allocated--;
447 ie_disk.nr_free = cpu_to_le32(le32_to_cpu(ie_disk.nr_free) + 1);
448 ie_disk.none_free_before = cpu_to_le32(min(le32_to_cpu(ie_disk.none_free_before), bit));
451 return ll->save_ie(ll, index, &ie_disk);
454 int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
456 int r;
457 uint32_t rc;
459 r = sm_ll_lookup(ll, b, &rc);
460 if (r)
461 return r;
463 return sm_ll_insert(ll, b, rc + 1, ev);
466 int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev)
468 int r;
469 uint32_t rc;
471 r = sm_ll_lookup(ll, b, &rc);
472 if (r)
473 return r;
475 if (!rc)
476 return -EINVAL;
478 return sm_ll_insert(ll, b, rc - 1, ev);
481 int sm_ll_commit(struct ll_disk *ll)
483 return ll->commit(ll);
486 /*----------------------------------------------------------------*/
488 static int metadata_ll_load_ie(struct ll_disk *ll, dm_block_t index,
489 struct disk_index_entry *ie)
491 memcpy(ie, ll->mi_le.index + index, sizeof(*ie));
492 return 0;
495 static int metadata_ll_save_ie(struct ll_disk *ll, dm_block_t index,
496 struct disk_index_entry *ie)
498 memcpy(ll->mi_le.index + index, ie, sizeof(*ie));
499 return 0;
502 static int metadata_ll_init_index(struct ll_disk *ll)
504 int r;
505 struct dm_block *b;
507 r = dm_tm_new_block(ll->tm, &index_validator, &b);
508 if (r < 0)
509 return r;
511 memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
512 ll->bitmap_root = dm_block_location(b);
514 return dm_tm_unlock(ll->tm, b);
517 static int metadata_ll_open(struct ll_disk *ll)
519 int r;
520 struct dm_block *block;
522 r = dm_tm_read_lock(ll->tm, ll->bitmap_root,
523 &index_validator, &block);
524 if (r)
525 return r;
527 memcpy(&ll->mi_le, dm_block_data(block), sizeof(ll->mi_le));
528 return dm_tm_unlock(ll->tm, block);
531 static dm_block_t metadata_ll_max_entries(struct ll_disk *ll)
533 return MAX_METADATA_BITMAPS;
536 static int metadata_ll_commit(struct ll_disk *ll)
538 int r, inc;
539 struct dm_block *b;
541 r = dm_tm_shadow_block(ll->tm, ll->bitmap_root, &index_validator, &b, &inc);
542 if (r)
543 return r;
545 memcpy(dm_block_data(b), &ll->mi_le, sizeof(ll->mi_le));
546 ll->bitmap_root = dm_block_location(b);
548 return dm_tm_unlock(ll->tm, b);
551 int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm)
553 int r;
555 r = sm_ll_init(ll, tm);
556 if (r < 0)
557 return r;
559 ll->load_ie = metadata_ll_load_ie;
560 ll->save_ie = metadata_ll_save_ie;
561 ll->init_index = metadata_ll_init_index;
562 ll->open_index = metadata_ll_open;
563 ll->max_entries = metadata_ll_max_entries;
564 ll->commit = metadata_ll_commit;
566 ll->nr_blocks = 0;
567 ll->nr_allocated = 0;
569 r = ll->init_index(ll);
570 if (r < 0)
571 return r;
573 r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
574 if (r < 0)
575 return r;
577 return 0;
580 int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
581 void *root_le, size_t len)
583 int r;
584 struct disk_sm_root *smr = root_le;
586 if (len < sizeof(struct disk_sm_root)) {
587 DMERR("sm_metadata root too small");
588 return -ENOMEM;
591 r = sm_ll_init(ll, tm);
592 if (r < 0)
593 return r;
595 ll->load_ie = metadata_ll_load_ie;
596 ll->save_ie = metadata_ll_save_ie;
597 ll->init_index = metadata_ll_init_index;
598 ll->open_index = metadata_ll_open;
599 ll->max_entries = metadata_ll_max_entries;
600 ll->commit = metadata_ll_commit;
602 ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
603 ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
604 ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
605 ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
607 return ll->open_index(ll);
610 /*----------------------------------------------------------------*/
612 static int disk_ll_load_ie(struct ll_disk *ll, dm_block_t index,
613 struct disk_index_entry *ie)
615 return dm_btree_lookup(&ll->bitmap_info, ll->bitmap_root, &index, ie);
618 static int disk_ll_save_ie(struct ll_disk *ll, dm_block_t index,
619 struct disk_index_entry *ie)
621 __dm_bless_for_disk(ie);
622 return dm_btree_insert(&ll->bitmap_info, ll->bitmap_root,
623 &index, ie, &ll->bitmap_root);
626 static int disk_ll_init_index(struct ll_disk *ll)
628 return dm_btree_empty(&ll->bitmap_info, &ll->bitmap_root);
631 static int disk_ll_open(struct ll_disk *ll)
633 /* nothing to do */
634 return 0;
637 static dm_block_t disk_ll_max_entries(struct ll_disk *ll)
639 return -1ULL;
642 static int disk_ll_commit(struct ll_disk *ll)
644 return 0;
647 int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm)
649 int r;
651 r = sm_ll_init(ll, tm);
652 if (r < 0)
653 return r;
655 ll->load_ie = disk_ll_load_ie;
656 ll->save_ie = disk_ll_save_ie;
657 ll->init_index = disk_ll_init_index;
658 ll->open_index = disk_ll_open;
659 ll->max_entries = disk_ll_max_entries;
660 ll->commit = disk_ll_commit;
662 ll->nr_blocks = 0;
663 ll->nr_allocated = 0;
665 r = ll->init_index(ll);
666 if (r < 0)
667 return r;
669 r = dm_btree_empty(&ll->ref_count_info, &ll->ref_count_root);
670 if (r < 0)
671 return r;
673 return 0;
676 int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
677 void *root_le, size_t len)
679 int r;
680 struct disk_sm_root *smr = root_le;
682 if (len < sizeof(struct disk_sm_root)) {
683 DMERR("sm_metadata root too small");
684 return -ENOMEM;
687 r = sm_ll_init(ll, tm);
688 if (r < 0)
689 return r;
691 ll->load_ie = disk_ll_load_ie;
692 ll->save_ie = disk_ll_save_ie;
693 ll->init_index = disk_ll_init_index;
694 ll->open_index = disk_ll_open;
695 ll->max_entries = disk_ll_max_entries;
696 ll->commit = disk_ll_commit;
698 ll->nr_blocks = le64_to_cpu(smr->nr_blocks);
699 ll->nr_allocated = le64_to_cpu(smr->nr_allocated);
700 ll->bitmap_root = le64_to_cpu(smr->bitmap_root);
701 ll->ref_count_root = le64_to_cpu(smr->ref_count_root);
703 return ll->open_index(ll);
706 /*----------------------------------------------------------------*/