1c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/* 2c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * Copyright (C) 2012 Red Hat, Inc. 3c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * 4c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * This file is released under the GPL. 5c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber */ 6c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 7c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#ifndef DM_CACHE_METADATA_H 8c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#define DM_CACHE_METADATA_H 9c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 10c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#include "dm-cache-block-types.h" 11c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#include "dm-cache-policy-internal.h" 12895b47d7989af3aacea16380b190b1bb8f046362Mike Snitzer#include "persistent-data/dm-space-map-metadata.h" 13c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 14c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/*----------------------------------------------------------------*/ 15c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 16895b47d7989af3aacea16380b190b1bb8f046362Mike Snitzer#define DM_CACHE_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE 17c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 18c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/* FIXME: remove this restriction */ 19c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/* 20c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * The metadata device is currently limited in size. 21c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber */ 22895b47d7989af3aacea16380b190b1bb8f046362Mike Snitzer#define DM_CACHE_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS 23c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 24c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/* 25c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * A metadata device larger than 16GB triggers a warning. 26c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber */ 27c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#define DM_CACHE_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT)) 28c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 29c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/*----------------------------------------------------------------*/ 30c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 31c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/* 32c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * Ext[234]-style compat feature flags. 33c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * 34c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * A new feature which old metadata will still be compatible with should 35c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * define a DM_CACHE_FEATURE_COMPAT_* flag (rarely useful). 36c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * 37c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * A new feature that is not compatible with old code should define a 38c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * DM_CACHE_FEATURE_INCOMPAT_* flag and guard the relevant code with 39c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * that flag. 40c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * 41c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * A new feature that is not compatible with old code accessing the 42c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * metadata RDWR should define a DM_CACHE_FEATURE_RO_COMPAT_* flag and 43c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * guard the relevant code with that flag. 44c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * 45c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * As these various flags are defined they should be added to the 46c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * following masks. 47c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber */ 48c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#define DM_CACHE_FEATURE_COMPAT_SUPP 0UL 49c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#define DM_CACHE_FEATURE_COMPAT_RO_SUPP 0UL 50c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#define DM_CACHE_FEATURE_INCOMPAT_SUPP 0UL 51c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 52c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/* 53c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * Reopens or creates a new, empty metadata volume. 54c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * Returns an ERR_PTR on failure. 55c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber */ 56c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberstruct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev, 57c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber sector_t data_block_size, 58c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber bool may_format_device, 59c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber size_t policy_hint_size); 60c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 61c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornbervoid dm_cache_metadata_close(struct dm_cache_metadata *cmd); 62c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 63c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/* 64c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * The metadata needs to know how many cache blocks there are. We don't 65c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * care about the origin, assuming the core target is giving us valid 66c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * origin blocks to map to. 67c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber */ 68c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size); 69c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberdm_cblock_t dm_cache_size(struct dm_cache_metadata *cmd); 70c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 71c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd, 72c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber sector_t discard_block_size, 7364ab346a360a4b15c28fb8531918d4a01f4eabd9Heinz Mauelshagen dm_oblock_t new_nr_entries); 74c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 75c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornbertypedef int (*load_discard_fn)(void *context, sector_t discard_block_size, 7664ab346a360a4b15c28fb8531918d4a01f4eabd9Heinz Mauelshagen dm_oblock_t dblock, bool discarded); 77c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_load_discards(struct dm_cache_metadata *cmd, 78c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber load_discard_fn fn, void *context); 79c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 8064ab346a360a4b15c28fb8531918d4a01f4eabd9Heinz Mauelshagenint dm_cache_set_discard(struct dm_cache_metadata *cmd, dm_oblock_t dblock, bool discard); 81c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 82c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock); 83c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_insert_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock, dm_oblock_t oblock); 84c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd); 85c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 86c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornbertypedef int (*load_mapping_fn)(void *context, dm_oblock_t oblock, 87c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber dm_cblock_t cblock, bool dirty, 88c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber uint32_t hint, bool hint_valid); 89c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_load_mappings(struct dm_cache_metadata *cmd, 90ea2dd8c1ed0becee9812cf0840a9cd553ed398feMike Snitzer struct dm_cache_policy *policy, 91c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber load_mapping_fn fn, 92c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber void *context); 93c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 94c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_set_dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty); 95c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 96c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberstruct dm_cache_statistics { 97c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber uint32_t read_hits; 98c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber uint32_t read_misses; 99c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber uint32_t write_hits; 100c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber uint32_t write_misses; 101c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber}; 102c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 103c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornbervoid dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd, 104c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber struct dm_cache_statistics *stats); 105c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornbervoid dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd, 106c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber struct dm_cache_statistics *stats); 107c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 108c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown); 109c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 110c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd, 111c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber dm_block_t *result); 112c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 113c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornberint dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd, 114c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber dm_block_t *result); 115c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 116c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornbervoid dm_cache_dump(struct dm_cache_metadata *cmd); 117c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 118c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/* 119c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * The policy is invited to save a 32bit hint value for every cblock (eg, 120c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * for a hit count). These are stored against the policy name. If 121c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * policies are changed, then hints will be lost. If the machine crashes, 122c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * hints will be lost. 123c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * 124c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * The hints are indexed by the cblock, but many policies will not 125c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * neccessarily have a fast way of accessing efficiently via cblock. So 126c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * rather than querying the policy for each cblock, we let it walk its data 127c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber * structures and fill in the hints in whatever order it wishes. 128c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber */ 1290596661f0a16d9d69bf1033320e70b6ff52b5e81Joe Thornberint dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *p); 130c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 1312ee57d587357f0d752af6c2e3e46434a74b1bee3Joe Thornber/* 1322ee57d587357f0d752af6c2e3e46434a74b1bee3Joe Thornber * Query method. Are all the blocks in the cache clean? 1332ee57d587357f0d752af6c2e3e46434a74b1bee3Joe Thornber */ 1342ee57d587357f0d752af6c2e3e46434a74b1bee3Joe Thornberint dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result); 1352ee57d587357f0d752af6c2e3e46434a74b1bee3Joe Thornber 136c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber/*----------------------------------------------------------------*/ 137c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber 138c6b4fcbad044e6fffcc75bba160e720eb8d67d17Joe Thornber#endif /* DM_CACHE_METADATA_H */ 139