dm-space-map-common.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. struct ll_disk {
  47. struct dm_transaction_manager *tm;
  48. struct dm_btree_info bitmap_info;
  49. struct dm_btree_info ref_count_info;
  50. uint32_t block_size;
  51. uint32_t entries_per_block;
  52. dm_block_t nr_blocks;
  53. dm_block_t nr_allocated;
  54. /*
  55. * bitmap_root may be a btree root or a simple index.
  56. */
  57. dm_block_t bitmap_root;
  58. dm_block_t ref_count_root;
  59. struct disk_metadata_index mi_le;
  60. load_ie_fn load_ie;
  61. save_ie_fn save_ie;
  62. init_index_fn init_index;
  63. open_index_fn open_index;
  64. max_index_entries_fn max_entries;
  65. commit_fn commit;
  66. bool bitmap_index_changed:1;
  67. };
  68. struct disk_sm_root {
  69. __le64 nr_blocks;
  70. __le64 nr_allocated;
  71. __le64 bitmap_root;
  72. __le64 ref_count_root;
  73. } __attribute__ ((packed, aligned(8)));
  74. #define ENTRIES_PER_BYTE 4
  75. struct disk_bitmap_header {
  76. __le32 csum;
  77. __le32 not_used;
  78. __le64 blocknr;
  79. } __attribute__ ((packed, aligned(8)));
  80. enum allocation_event {
  81. SM_NONE,
  82. SM_ALLOC,
  83. SM_FREE,
  84. };
  85. /*----------------------------------------------------------------*/
  86. int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks);
  87. int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result);
  88. int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result);
  89. int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
  90. dm_block_t end, dm_block_t *result);
  91. int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll,
  92. dm_block_t begin, dm_block_t end, dm_block_t *result);
  93. int sm_ll_insert(struct ll_disk *ll, dm_block_t b, uint32_t ref_count, enum allocation_event *ev);
  94. int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev);
  95. int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev);
  96. int sm_ll_commit(struct ll_disk *ll);
  97. int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm);
  98. int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm,
  99. void *root_le, size_t len);
  100. int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm);
  101. int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm,
  102. void *root_le, size_t len);
  103. /*----------------------------------------------------------------*/
  104. #endif /* DM_SPACE_MAP_COMMON_H */