Merge branch 'akpm'
[linux-2.6/next.git] / drivers / md / persistent-data / dm-space-map-disk.c
blobe6b9d67270eed1eae4ddb21b81b243b7c06345d5
1 /*
2 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
4 * This file is released under the GPL.
5 */
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"
21 * Bitmap validator
23 static void bitmap_prepare_for_write(struct dm_block_validator *v,
24 struct dm_block *b,
25 size_t block_size)
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,
34 struct dm_block *b,
35 size_t block_size)
37 struct disk_bitmap_header *disk_header = dm_block_data(b);
38 __le32 csum_disk;
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));
43 return -ENOTBLK;
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));
50 return -EILSEQ;
53 return 0;
56 struct dm_block_validator dm_sm_bitmap_validator = {
57 .name = "sm_bitmap",
58 .prepare_for_write = bitmap_prepare_for_write,
59 .check = bitmap_check
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;
106 if (val & 2)
107 __set_bit_le(b, (void *) w_le);
108 else
109 __clear_bit_le(b, (void *) w_le);
111 if (val & 1)
112 __set_bit_le(b + 1, (void *) w_le);
113 else
114 __clear_bit_le(b + 1, (void *) w_le);
117 int sm_find_free(void *addr, unsigned begin, unsigned end,
118 unsigned *result)
120 while (begin < end) {
121 if (!(begin & (ENTRIES_PER_WORD - 1)) &&
122 bitmap_word_used(addr, begin)) {
123 begin += ENTRIES_PER_WORD;
124 continue;
127 if (!sm_lookup_bitmap(addr, begin)) {
128 *result = begin;
129 return 0;
132 begin++;
135 return -ENOSPC;
138 static int disk_ll_init(struct ll_disk *io, struct dm_transaction_manager *tm)
140 io->tm = 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");
165 return -EINVAL;
168 io->entries_per_block = (io->block_size - sizeof(struct disk_bitmap_header)) *
169 ENTRIES_PER_BYTE;
170 io->nr_blocks = 0;
171 io->bitmap_root = 0;
172 io->ref_count_root = 0;
174 return 0;
177 static int disk_ll_new(struct ll_disk *io, struct dm_transaction_manager *tm)
179 int r;
181 r = disk_ll_init(io, tm);
182 if (r < 0)
183 return r;
185 io->nr_blocks = 0;
186 io->nr_allocated = 0;
187 r = dm_btree_create(&io->bitmap_info, &io->bitmap_root);
188 if (r < 0)
189 return r;
191 r = dm_btree_create(&io->ref_count_info, &io->ref_count_root);
192 if (r < 0) {
193 dm_btree_destroy(&io->bitmap_info, io->bitmap_root);
194 return r;
197 return 0;
200 static int disk_ll_extend(struct ll_disk *io, dm_block_t extra_blocks)
202 int r;
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++) {
211 struct dm_block *b;
212 struct disk_index_entry idx;
214 r = dm_tm_new_block(io->tm, &dm_sm_bitmap_validator, &b);
215 if (r < 0)
216 return r;
217 idx.blocknr = cpu_to_le64(dm_block_location(b));
219 r = dm_tm_unlock(io->tm, b);
220 if (r < 0)
221 return r;
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);
229 if (r < 0)
230 return r;
233 io->nr_blocks = nr_blocks;
234 return 0;
237 static int disk_ll_open(struct ll_disk *ll, struct dm_transaction_manager *tm,
238 void *root_le, size_t len)
240 int r;
241 struct disk_sm_root *smr = root_le;
243 if (len < sizeof(struct disk_sm_root)) {
244 DMERR("sm_disk root too small");
245 return -ENOMEM;
248 r = disk_ll_init(ll, tm);
249 if (r < 0)
250 return r;
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);
257 return 0;
260 static int disk_ll_lookup_bitmap(struct ll_disk *io, dm_block_t b, uint32_t *result)
262 int r;
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);
269 if (r < 0)
270 return r;
272 r = dm_tm_read_lock(io->tm, le64_to_cpu(ie_disk.blocknr), &dm_sm_bitmap_validator, &blk);
273 if (r < 0)
274 return r;
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)
283 __le32 rc_le;
284 int r = disk_ll_lookup_bitmap(io, b, result);
286 if (r)
287 return r;
289 if (*result != 3)
290 return r;
292 r = dm_btree_lookup(&io->ref_count_info, io->ref_count_root, &b, &rc_le);
293 if (r < 0)
294 return r;
296 *result = le32_to_cpu(rc_le);
298 return r;
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)
304 int r;
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;
313 unsigned position;
314 uint32_t bit_end;
316 r = dm_btree_lookup(&io->bitmap_info, io->bitmap_root, &i, &ie_disk);
317 if (r < 0)
318 return r;
320 if (le32_to_cpu(ie_disk.nr_free) <= 0)
321 continue;
323 r = dm_tm_read_lock(io->tm, le64_to_cpu(ie_disk.blocknr),
324 &dm_sm_bitmap_validator, &blk);
325 if (r < 0)
326 return r;
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)),
333 bit_end, &position);
334 if (r < 0) {
335 dm_tm_unlock(io->tm, blk);
336 continue;
339 r = dm_tm_unlock(io->tm, blk);
340 if (r < 0)
341 return r;
343 *result = i * io->entries_per_block + (dm_block_t) position;
345 return 0;
348 return -ENOSPC;
351 static int disk_ll_insert(struct ll_disk *io, dm_block_t b, uint32_t ref_count)
353 int r;
354 uint32_t bit, old;
355 struct dm_block *nb;
356 dm_block_t index = b;
357 struct disk_index_entry ie_disk;
358 void *bm_le;
359 int inc;
361 do_div(index, io->entries_per_block);
362 r = dm_btree_lookup(&io->bitmap_info, io->bitmap_root, &index, &ie_disk);
363 if (r < 0)
364 return r;
366 r = dm_tm_shadow_block(io->tm, le64_to_cpu(ie_disk.blocknr),
367 &dm_sm_bitmap_validator, &nb, &inc);
368 if (r < 0) {
369 DMERR("dm_tm_shadow_block() failed");
370 return r;
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);
381 if (old > 2) {
382 r = dm_btree_remove(&io->ref_count_info, io->ref_count_root,
383 &b, &io->ref_count_root);
384 if (r) {
385 dm_tm_unlock(io->tm, nb);
386 return r;
389 } else {
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);
397 if (r < 0) {
398 dm_tm_unlock(io->tm, nb);
399 DMERR("ref count insert failed");
400 return r;
404 r = dm_tm_unlock(io->tm, nb);
405 if (r < 0)
406 return r;
408 if (ref_count && !old) {
409 io->nr_allocated++;
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) {
415 io->nr_allocated--;
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);
423 if (r < 0)
424 return r;
426 return 0;
429 static int disk_ll_inc(struct ll_disk *ll, dm_block_t b)
431 int r;
432 uint32_t rc;
434 r = disk_ll_lookup(ll, b, &rc);
435 if (r)
436 return r;
438 return disk_ll_insert(ll, b, rc + 1);
441 static int disk_ll_dec(struct ll_disk *ll, dm_block_t b)
443 int r;
444 uint32_t rc;
446 r = disk_ll_lookup(ll, b, &rc);
447 if (r)
448 return r;
450 if (!rc)
451 return -EINVAL;
453 return disk_ll_insert(ll, b, rc - 1);
456 /*--------------------------------------------------------------*/
459 * Space map interface.
461 struct sm_disk {
462 struct dm_space_map sm;
464 struct ll_disk ll;
467 static void sm_disk_destroy(struct dm_space_map *sm)
469 struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
471 kfree(smd);
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;
487 return 0;
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;
496 return 0;
499 static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
500 uint32_t *result)
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,
508 int *result)
510 int r;
511 uint32_t count;
513 r = sm_disk_get_count(sm, b, &count);
514 if (r)
515 return r;
517 return count > 1;
520 static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
521 uint32_t count)
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)
544 int r;
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);
551 if (r)
552 return r;
554 return disk_ll_inc(&smd->ll, *b);
557 static int sm_disk_commit(struct dm_space_map *sm)
559 return 0;
562 static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
564 *result = sizeof(struct disk_sm_root);
566 return 0;
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))
580 return -ENOSPC;
582 memcpy(where_le, &root_le, sizeof(root_le));
584 return 0;
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)
608 int r;
609 struct sm_disk *smd;
611 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
612 if (!smd)
613 return ERR_PTR(-ENOMEM);
615 memcpy(&smd->sm, &ops, sizeof(smd->sm));
617 r = disk_ll_new(&smd->ll, tm);
618 if (r)
619 goto bad;
621 r = disk_ll_extend(&smd->ll, nr_blocks);
622 if (r)
623 goto bad;
625 r = sm_disk_commit(&smd->sm);
626 if (r)
627 goto bad;
629 return &smd->sm;
631 bad:
632 kfree(smd);
633 return ERR_PTR(r);
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)
640 int r;
641 struct sm_disk *smd;
643 smd = kmalloc(sizeof(*smd), GFP_KERNEL);
644 if (!smd)
645 return ERR_PTR(-ENOMEM);
647 memcpy(&smd->sm, &ops, sizeof(smd->sm));
649 r = disk_ll_open(&smd->ll, tm, root_le, len);
650 if (r)
651 goto bad;
653 r = sm_disk_commit(&smd->sm);
654 if (r)
655 goto bad;
657 return &smd->sm;
659 bad:
660 kfree(smd);
661 return ERR_PTR(r);
663 EXPORT_SYMBOL_GPL(dm_sm_disk_open);