dm-space-map-common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #ifndef DM_SPACE_MAP_COMMON_H
  7. #define DM_SPACE_MAP_COMMON_H
  8. #include "dm-btree.h"
  9. /*----------------------------------------------------------------*/
  10. /*
  11. * Low level disk format
  12. *
  13. * Bitmap btree
  14. * ------------
  15. *
  16. * Each value stored in the btree is an index_entry. This points to a
  17. * block that is used as a bitmap. Within the bitmap hold 2 bits per
  18. * entry, which represent UNUSED = 0, REF_COUNT = 1, REF_COUNT = 2 and
  19. * REF_COUNT = many.
  20. *
  21. * Refcount btree
  22. * --------------
  23. *
  24. * Any entry that has a ref count higher than 2 gets entered in the ref
  25. * count tree. The leaf values for this tree is the 32-bit ref count.
  26. */
  27. struct disk_index_entry {
  28. __le64 blocknr;
  29. __le32 nr_free;
  30. __le32 none_free_before;
  31. } __attribute__ ((packed, aligned(8)));
  32. #define MAX_METADATA_BITMAPS 255
  33. struct disk_metadata_index {
  34. __le32 csum;
  35. __le32 padding;
  36. __le64 blocknr;
  37. struct disk_index_entry index[MAX_METADATA_BITMAPS];
  38. } __attribute__ ((packed, aligned(8)));
  39. struct ll_disk;
  40. typedef int (*load_ie_fn)(struct ll_disk *ll, dm_block_t index, struct disk_index_entry *result);
  41. typedef int (*save_ie_fn)(struct ll_disk *ll, dm_block_t index, struct disk_index_entry *ie);
  42. typedef int (*init_index_fn)(struct ll_disk *ll);
  43. typedef int (*open_index_fn)(struct ll_disk *ll);
  44. typedef dm_block_t (*max_index_entries_fn)(struct ll_disk *ll);
  45. typedef int (*commit_fn)(struct ll_disk *ll);
  46. /*
  47. * A lot of time can be wasted reading and writing the same
  48. * index entry. So we cache a few entries.
  49. */
  50. #define IE_CACHE_SIZE 64
  51. #define IE_CACHE_MASK (IE_CACHE_SIZE - 1)
  52. struct ie_cache {
  53. bool valid;
  54. bool dirty;
  55. dm_block_t index;
  56. struct disk_index_entry ie;
  57. };
  58. struct ll_disk {
  59. struct dm_transaction_manager *tm;
  60. struct dm_btree_info bitmap_info;
  61. struct dm_btree_info ref_count_info;
  62. uint32_t block_size;
  63. uint32_t entries_per_block;
  64. dm_block_t nr_blocks;
  65. dm_block_t nr_allocated;
  66. /*
  67. * bitmap_root may be a btree root or a simple index.
  68. */
  69. dm_block_t bitmap_root;
  70. dm_block_t ref_count_root;
  71. struct disk_metadata_index mi_le;
  72. load_ie_fn load_ie;
  73. save_ie_fn save_ie;
  74. init_index_fn init_index;
  75. open_index_fn open_index;
  76. max_index_entries_fn max_entries;
  77. commit_fn commit;
  78. bool bitmap_index_changed:1;
  79. struct ie_cache ie_cache[IE_CACHE_SIZE];
  80. };
  81. struct disk_sm_root {
  82. __le64 nr_blocks;
  83. __le64 nr_allocated;
  84. __le64 bitmap_root;
  85. __le64 ref_count_root;
  86. } __attribute__ ((packed, aligned(8)));
  87. #define ENTRIES_PER_BYTE 4
  88. struct disk_bitmap_header {
  89. __le32 csum;
  90. __le32 not_used;
  91. __le64 blocknr;
  92. } __attribute__ ((packed, aligned(8)));
  93. /*----------------------------------------------------------------*/
  94. int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks);
  95. int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result);
  96. int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result);
  97. int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
  98. dm_block_t end, dm_block_t *result);
  99. int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll,
  100. dm_block_t begin, dm_block_t end, dm_block_t *result);
  101. /*
  102. * The next three functions return (via nr_allocations) the net number of
  103. * allocations that were made. This number may be negative if there were
  104. * more frees than allocs.
  105. */
  106. int sm_ll_insert(struct ll_disk *ll, dm_block_t b, uint32_t ref_count, int32_t *nr_allocations);
  107. int sm_ll_inc(struct ll_disk *ll, dm_block_t b, dm_block_t e, int32_t *nr_allocations);
  108. int sm_ll_dec(struct ll_disk *ll, dm_block_t b, dm_block_t e, int32_t *nr_allocations);
  109. int sm_ll_commit(struct ll_disk *ll);
  110. int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm);
  111. int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
  112. void *root_le, size_t len);
  113. int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm);
  114. int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
  115. void *root_le, size_t len);
  116. /*----------------------------------------------------------------*/
  117. #endif /* DM_SPACE_MAP_COMMON_H */