13bd94003SHeinz Mauelshagen /* SPDX-License-Identifier: GPL-2.0-only */ 23241b1d3SJoe Thornber /* 33241b1d3SJoe Thornber * Copyright (C) 2011 Red Hat, Inc. 43241b1d3SJoe Thornber * 53241b1d3SJoe Thornber * This file is released under the GPL. 63241b1d3SJoe Thornber */ 73241b1d3SJoe Thornber 83241b1d3SJoe Thornber #ifndef DM_SPACE_MAP_COMMON_H 93241b1d3SJoe Thornber #define DM_SPACE_MAP_COMMON_H 103241b1d3SJoe Thornber 113241b1d3SJoe Thornber #include "dm-btree.h" 123241b1d3SJoe Thornber 133241b1d3SJoe Thornber /*----------------------------------------------------------------*/ 143241b1d3SJoe Thornber 153241b1d3SJoe Thornber /* 163241b1d3SJoe Thornber * Low level disk format 173241b1d3SJoe Thornber * 183241b1d3SJoe Thornber * Bitmap btree 193241b1d3SJoe Thornber * ------------ 203241b1d3SJoe Thornber * 213241b1d3SJoe Thornber * Each value stored in the btree is an index_entry. This points to a 223241b1d3SJoe Thornber * block that is used as a bitmap. Within the bitmap hold 2 bits per 233241b1d3SJoe Thornber * entry, which represent UNUSED = 0, REF_COUNT = 1, REF_COUNT = 2 and 243241b1d3SJoe Thornber * REF_COUNT = many. 253241b1d3SJoe Thornber * 263241b1d3SJoe Thornber * Refcount btree 273241b1d3SJoe Thornber * -------------- 283241b1d3SJoe Thornber * 293241b1d3SJoe Thornber * Any entry that has a ref count higher than 2 gets entered in the ref 303241b1d3SJoe Thornber * count tree. The leaf values for this tree is the 32-bit ref count. 313241b1d3SJoe Thornber */ 323241b1d3SJoe Thornber 333241b1d3SJoe Thornber struct disk_index_entry { 343241b1d3SJoe Thornber __le64 blocknr; 353241b1d3SJoe Thornber __le32 nr_free; 363241b1d3SJoe Thornber __le32 none_free_before; 37*ae99111eSHeinz Mauelshagen } __packed __aligned(8); 383241b1d3SJoe Thornber 393241b1d3SJoe Thornber 403241b1d3SJoe Thornber #define MAX_METADATA_BITMAPS 255 413241b1d3SJoe Thornber struct disk_metadata_index { 423241b1d3SJoe Thornber __le32 csum; 433241b1d3SJoe Thornber __le32 padding; 443241b1d3SJoe Thornber __le64 blocknr; 453241b1d3SJoe Thornber 463241b1d3SJoe Thornber struct disk_index_entry index[MAX_METADATA_BITMAPS]; 47*ae99111eSHeinz Mauelshagen } __packed __aligned(8); 483241b1d3SJoe Thornber 493241b1d3SJoe Thornber struct ll_disk; 503241b1d3SJoe Thornber 513241b1d3SJoe Thornber typedef int (*load_ie_fn)(struct ll_disk *ll, dm_block_t index, struct disk_index_entry *result); 523241b1d3SJoe Thornber typedef int (*save_ie_fn)(struct ll_disk *ll, dm_block_t index, struct disk_index_entry *ie); 533241b1d3SJoe Thornber typedef int (*init_index_fn)(struct ll_disk *ll); 543241b1d3SJoe Thornber typedef int (*open_index_fn)(struct ll_disk *ll); 553241b1d3SJoe Thornber typedef dm_block_t (*max_index_entries_fn)(struct ll_disk *ll); 563241b1d3SJoe Thornber typedef int (*commit_fn)(struct ll_disk *ll); 573241b1d3SJoe Thornber 586b06dd5aSJoe Thornber /* 596b06dd5aSJoe Thornber * A lot of time can be wasted reading and writing the same 606b06dd5aSJoe Thornber * index entry. So we cache a few entries. 616b06dd5aSJoe Thornber */ 626b06dd5aSJoe Thornber #define IE_CACHE_SIZE 64 636b06dd5aSJoe Thornber #define IE_CACHE_MASK (IE_CACHE_SIZE - 1) 646b06dd5aSJoe Thornber 656b06dd5aSJoe Thornber struct ie_cache { 666b06dd5aSJoe Thornber bool valid; 676b06dd5aSJoe Thornber bool dirty; 686b06dd5aSJoe Thornber dm_block_t index; 696b06dd5aSJoe Thornber struct disk_index_entry ie; 706b06dd5aSJoe Thornber }; 716b06dd5aSJoe Thornber 723241b1d3SJoe Thornber struct ll_disk { 733241b1d3SJoe Thornber struct dm_transaction_manager *tm; 743241b1d3SJoe Thornber struct dm_btree_info bitmap_info; 753241b1d3SJoe Thornber struct dm_btree_info ref_count_info; 763241b1d3SJoe Thornber 773241b1d3SJoe Thornber uint32_t block_size; 783241b1d3SJoe Thornber uint32_t entries_per_block; 793241b1d3SJoe Thornber dm_block_t nr_blocks; 803241b1d3SJoe Thornber dm_block_t nr_allocated; 813241b1d3SJoe Thornber 823241b1d3SJoe Thornber /* 833241b1d3SJoe Thornber * bitmap_root may be a btree root or a simple index. 843241b1d3SJoe Thornber */ 853241b1d3SJoe Thornber dm_block_t bitmap_root; 863241b1d3SJoe Thornber 873241b1d3SJoe Thornber dm_block_t ref_count_root; 883241b1d3SJoe Thornber 893241b1d3SJoe Thornber struct disk_metadata_index mi_le; 903241b1d3SJoe Thornber load_ie_fn load_ie; 913241b1d3SJoe Thornber save_ie_fn save_ie; 923241b1d3SJoe Thornber init_index_fn init_index; 933241b1d3SJoe Thornber open_index_fn open_index; 943241b1d3SJoe Thornber max_index_entries_fn max_entries; 953241b1d3SJoe Thornber commit_fn commit; 96f4b90369SJoe Thornber bool bitmap_index_changed:1; 976b06dd5aSJoe Thornber 986b06dd5aSJoe Thornber struct ie_cache ie_cache[IE_CACHE_SIZE]; 993241b1d3SJoe Thornber }; 1003241b1d3SJoe Thornber 1013241b1d3SJoe Thornber struct disk_sm_root { 1023241b1d3SJoe Thornber __le64 nr_blocks; 1033241b1d3SJoe Thornber __le64 nr_allocated; 1043241b1d3SJoe Thornber __le64 bitmap_root; 1053241b1d3SJoe Thornber __le64 ref_count_root; 106*ae99111eSHeinz Mauelshagen } __packed __aligned(8); 1073241b1d3SJoe Thornber 1083241b1d3SJoe Thornber #define ENTRIES_PER_BYTE 4 1093241b1d3SJoe Thornber 1103241b1d3SJoe Thornber struct disk_bitmap_header { 1113241b1d3SJoe Thornber __le32 csum; 1123241b1d3SJoe Thornber __le32 not_used; 1133241b1d3SJoe Thornber __le64 blocknr; 114*ae99111eSHeinz Mauelshagen } __packed __aligned(8); 1153241b1d3SJoe Thornber 1163241b1d3SJoe Thornber /*----------------------------------------------------------------*/ 1173241b1d3SJoe Thornber 1183241b1d3SJoe Thornber int sm_ll_extend(struct ll_disk *ll, dm_block_t extra_blocks); 1193241b1d3SJoe Thornber int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result); 1203241b1d3SJoe Thornber int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result); 1213241b1d3SJoe Thornber int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin, 1223241b1d3SJoe Thornber dm_block_t end, dm_block_t *result); 1234feaef83SJoe Thornber int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll, 1244feaef83SJoe Thornber dm_block_t begin, dm_block_t end, dm_block_t *result); 125be500ed7SJoe Thornber 126be500ed7SJoe Thornber /* 127be500ed7SJoe Thornber * The next three functions return (via nr_allocations) the net number of 128be500ed7SJoe Thornber * allocations that were made. This number may be negative if there were 129be500ed7SJoe Thornber * more frees than allocs. 130be500ed7SJoe Thornber */ 131be500ed7SJoe Thornber int sm_ll_insert(struct ll_disk *ll, dm_block_t b, uint32_t ref_count, int32_t *nr_allocations); 132be500ed7SJoe Thornber int sm_ll_inc(struct ll_disk *ll, dm_block_t b, dm_block_t e, int32_t *nr_allocations); 133be500ed7SJoe Thornber int sm_ll_dec(struct ll_disk *ll, dm_block_t b, dm_block_t e, int32_t *nr_allocations); 1343241b1d3SJoe Thornber int sm_ll_commit(struct ll_disk *ll); 1353241b1d3SJoe Thornber 1363241b1d3SJoe Thornber int sm_ll_new_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm); 1373241b1d3SJoe Thornber int sm_ll_open_metadata(struct ll_disk *ll, struct dm_transaction_manager *tm, 1383241b1d3SJoe Thornber void *root_le, size_t len); 1393241b1d3SJoe Thornber 1403241b1d3SJoe Thornber int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm); 1413241b1d3SJoe Thornber int sm_ll_open_disk(struct ll_disk *ll, struct dm_transaction_manager *tm, 1423241b1d3SJoe Thornber void *root_le, size_t len); 1433241b1d3SJoe Thornber 1443241b1d3SJoe Thornber /*----------------------------------------------------------------*/ 1453241b1d3SJoe Thornber 1463241b1d3SJoe Thornber #endif /* DM_SPACE_MAP_COMMON_H */ 147