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