2 * Copyright (C) 2012 Red Hat, Inc.
4 * This file is released under the GPL.
7 #ifndef DM_CACHE_METADATA_H
8 #define DM_CACHE_METADATA_H
10 #include "dm-cache-block-types.h"
11 #include "dm-cache-policy-internal.h"
13 /*----------------------------------------------------------------*/
15 #define DM_CACHE_METADATA_BLOCK_SIZE 4096
17 /* FIXME: remove this restriction */
19 * The metadata device is currently limited in size.
21 * We have one block of index, which can hold 255 index entries. Each
22 * index entry contains allocation info about 16k metadata blocks.
24 #define DM_CACHE_METADATA_MAX_SECTORS (255 * (1 << 14) * (DM_CACHE_METADATA_BLOCK_SIZE / (1 << SECTOR_SHIFT)))
27 * A metadata device larger than 16GB triggers a warning.
29 #define DM_CACHE_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
31 /*----------------------------------------------------------------*/
34 * Ext[234]-style compat feature flags.
36 * A new feature which old metadata will still be compatible with should
37 * define a DM_CACHE_FEATURE_COMPAT_* flag (rarely useful).
39 * A new feature that is not compatible with old code should define a
40 * DM_CACHE_FEATURE_INCOMPAT_* flag and guard the relevant code with
43 * A new feature that is not compatible with old code accessing the
44 * metadata RDWR should define a DM_CACHE_FEATURE_RO_COMPAT_* flag and
45 * guard the relevant code with that flag.
47 * As these various flags are defined they should be added to the
50 #define DM_CACHE_FEATURE_COMPAT_SUPP 0UL
51 #define DM_CACHE_FEATURE_COMPAT_RO_SUPP 0UL
52 #define DM_CACHE_FEATURE_INCOMPAT_SUPP 0UL
55 * Reopens or creates a new, empty metadata volume.
56 * Returns an ERR_PTR on failure.
58 struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
59 sector_t data_block_size,
60 bool may_format_device,
61 size_t policy_hint_size);
63 void dm_cache_metadata_close(struct dm_cache_metadata *cmd);
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.
70 int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size);
71 dm_cblock_t dm_cache_size(struct dm_cache_metadata *cmd);
73 int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
74 sector_t discard_block_size,
75 dm_dblock_t new_nr_entries);
77 typedef int (*load_discard_fn)(void *context, sector_t discard_block_size,
78 dm_dblock_t dblock, bool discarded);
79 int dm_cache_load_discards(struct dm_cache_metadata *cmd,
80 load_discard_fn fn, void *context);
82 int dm_cache_set_discard(struct dm_cache_metadata *cmd, dm_dblock_t dblock, bool discard);
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);
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,
92 struct dm_cache_policy *policy,
96 int dm_cache_set_dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty);
98 struct dm_cache_statistics {
100 uint32_t read_misses;
102 uint32_t write_misses;
105 void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
106 struct dm_cache_statistics *stats);
107 void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
108 struct dm_cache_statistics *stats);
110 int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown);
112 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
115 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
118 void dm_cache_dump(struct dm_cache_metadata *cmd);
121 * The policy is invited to save a 32bit hint value for every cblock (eg,
122 * for a hit count). These are stored against the policy name. If
123 * policies are changed, then hints will be lost. If the machine crashes,
124 * hints will be lost.
126 * The hints are indexed by the cblock, but many policies will not
127 * neccessarily have a fast way of accessing efficiently via cblock. So
128 * rather than querying the policy for each cblock, we let it walk its data
129 * structures and fill in the hints in whatever order it wishes.
132 int dm_cache_begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *p);
135 * requests hints for every cblock and stores in the metadata device.
137 int dm_cache_save_hint(struct dm_cache_metadata *cmd,
138 dm_cblock_t cblock, uint32_t hint);
140 /*----------------------------------------------------------------*/
142 #endif /* DM_CACHE_METADATA_H */