dm-space-map-common.h 4.1 KB

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