Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Red Hat, Inc. |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #ifndef DM_CACHE_METADATA_H |
| 8 | #define DM_CACHE_METADATA_H |
| 9 | |
| 10 | #include "dm-cache-block-types.h" |
| 11 | #include "dm-cache-policy-internal.h" |
Mike Snitzer | 895b47d | 2014-07-14 15:37:18 -0400 | [diff] [blame] | 12 | #include "persistent-data/dm-space-map-metadata.h" |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 13 | |
| 14 | /*----------------------------------------------------------------*/ |
| 15 | |
Mike Snitzer | 895b47d | 2014-07-14 15:37:18 -0400 | [diff] [blame] | 16 | #define DM_CACHE_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 17 | |
| 18 | /* FIXME: remove this restriction */ |
| 19 | /* |
| 20 | * The metadata device is currently limited in size. |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 21 | */ |
Mike Snitzer | 895b47d | 2014-07-14 15:37:18 -0400 | [diff] [blame] | 22 | #define DM_CACHE_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * A metadata device larger than 16GB triggers a warning. |
| 26 | */ |
| 27 | #define DM_CACHE_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT)) |
| 28 | |
| 29 | /*----------------------------------------------------------------*/ |
| 30 | |
| 31 | /* |
| 32 | * Ext[234]-style compat feature flags. |
| 33 | * |
| 34 | * A new feature which old metadata will still be compatible with should |
| 35 | * define a DM_CACHE_FEATURE_COMPAT_* flag (rarely useful). |
| 36 | * |
| 37 | * A new feature that is not compatible with old code should define a |
| 38 | * DM_CACHE_FEATURE_INCOMPAT_* flag and guard the relevant code with |
| 39 | * that flag. |
| 40 | * |
| 41 | * A new feature that is not compatible with old code accessing the |
| 42 | * metadata RDWR should define a DM_CACHE_FEATURE_RO_COMPAT_* flag and |
| 43 | * guard the relevant code with that flag. |
| 44 | * |
| 45 | * As these various flags are defined they should be added to the |
| 46 | * following masks. |
| 47 | */ |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 48 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 49 | #define DM_CACHE_FEATURE_COMPAT_SUPP 0UL |
| 50 | #define DM_CACHE_FEATURE_COMPAT_RO_SUPP 0UL |
| 51 | #define DM_CACHE_FEATURE_INCOMPAT_SUPP 0UL |
| 52 | |
| 53 | /* |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 54 | * Reopens or creates a new, empty metadata volume. Returns an ERR_PTR on |
| 55 | * failure. If reopening then features must match. |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 56 | */ |
| 57 | struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev, |
| 58 | sector_t data_block_size, |
| 59 | bool may_format_device, |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 60 | size_t policy_hint_size, |
| 61 | unsigned metadata_version); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 62 | |
| 63 | void dm_cache_metadata_close(struct dm_cache_metadata *cmd); |
| 64 | |
| 65 | /* |
| 66 | * The metadata needs to know how many cache blocks there are. We don't |
| 67 | * care about the origin, assuming the core target is giving us valid |
| 68 | * origin blocks to map to. |
| 69 | */ |
| 70 | int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size); |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 71 | int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 72 | |
| 73 | int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd, |
| 74 | sector_t discard_block_size, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 75 | dm_dblock_t new_nr_entries); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 76 | |
| 77 | typedef int (*load_discard_fn)(void *context, sector_t discard_block_size, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 78 | dm_dblock_t dblock, bool discarded); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 79 | int dm_cache_load_discards(struct dm_cache_metadata *cmd, |
| 80 | load_discard_fn fn, void *context); |
| 81 | |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 82 | int dm_cache_set_discard(struct dm_cache_metadata *cmd, dm_dblock_t dblock, bool discard); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 83 | |
| 84 | int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock); |
| 85 | int dm_cache_insert_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock, dm_oblock_t oblock); |
| 86 | int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd); |
| 87 | |
| 88 | typedef int (*load_mapping_fn)(void *context, dm_oblock_t oblock, |
| 89 | dm_cblock_t cblock, bool dirty, |
| 90 | uint32_t hint, bool hint_valid); |
| 91 | int dm_cache_load_mappings(struct dm_cache_metadata *cmd, |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 92 | struct dm_cache_policy *policy, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 93 | load_mapping_fn fn, |
| 94 | void *context); |
| 95 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 96 | int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd, |
| 97 | unsigned nr_bits, unsigned long *bits); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 98 | |
| 99 | struct dm_cache_statistics { |
| 100 | uint32_t read_hits; |
| 101 | uint32_t read_misses; |
| 102 | uint32_t write_hits; |
| 103 | uint32_t write_misses; |
| 104 | }; |
| 105 | |
| 106 | void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd, |
| 107 | struct dm_cache_statistics *stats); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * 'void' because it's no big deal if it fails. |
| 111 | */ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 112 | void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd, |
| 113 | struct dm_cache_statistics *stats); |
| 114 | |
| 115 | int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown); |
| 116 | |
| 117 | int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd, |
| 118 | dm_block_t *result); |
| 119 | |
| 120 | int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd, |
| 121 | dm_block_t *result); |
| 122 | |
| 123 | void dm_cache_dump(struct dm_cache_metadata *cmd); |
| 124 | |
| 125 | /* |
| 126 | * The policy is invited to save a 32bit hint value for every cblock (eg, |
| 127 | * for a hit count). These are stored against the policy name. If |
| 128 | * policies are changed, then hints will be lost. If the machine crashes, |
| 129 | * hints will be lost. |
| 130 | * |
| 131 | * The hints are indexed by the cblock, but many policies will not |
| 132 | * neccessarily have a fast way of accessing efficiently via cblock. So |
| 133 | * rather than querying the policy for each cblock, we let it walk its data |
| 134 | * structures and fill in the hints in whatever order it wishes. |
| 135 | */ |
Joe Thornber | 0596661 | 2014-04-03 16:16:44 +0100 | [diff] [blame] | 136 | int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *p); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 137 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 138 | /* |
| 139 | * Query method. Are all the blocks in the cache clean? |
| 140 | */ |
| 141 | int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result); |
| 142 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 143 | int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 144 | int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd); |
| 145 | void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd); |
| 146 | void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd); |
| 147 | int dm_cache_metadata_abort(struct dm_cache_metadata *cmd); |
| 148 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 149 | /*----------------------------------------------------------------*/ |
| 150 | |
| 151 | #endif /* DM_CACHE_METADATA_H */ |