^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2012 Red Hat. All rights reserved.
^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_CACHE_POLICY_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define DM_CACHE_POLICY_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "dm-cache-block-types.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device-mapper.h>
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * The cache policy makes the important decisions about which blocks get to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * live on the faster cache device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) enum policy_operation {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) POLICY_PROMOTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) POLICY_DEMOTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) POLICY_WRITEBACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * This is the instruction passed back to the core target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct policy_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) enum policy_operation op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) dm_oblock_t oblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) dm_cblock_t cblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * The cache policy object. It is envisaged that this structure will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * embedded in a bigger, policy specific structure (ie. use container_of()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct dm_cache_policy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Destroys this object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void (*destroy)(struct dm_cache_policy *p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Find the location of a block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Must not block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Returns 0 if in cache (cblock will be set), -ENOENT if not, < 0 for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * other errors (-EWOULDBLOCK would be typical). data_dir should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * READ or WRITE. fast_copy should be set if migrating this block would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * be 'cheap' somehow (eg, discarded data). background_queued will be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * if a migration has just been queued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int (*lookup)(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int data_dir, bool fast_copy, bool *background_queued);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Sometimes the core target can optimise a migration, eg, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * block may be discarded, or the bio may cover an entire block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * In order to optimise it needs the migration immediately though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * so it knows to do something different with the bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * This method is optional (policy-internal will fallback to using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * lookup).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int (*lookup_with_work)(struct dm_cache_policy *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dm_oblock_t oblock, dm_cblock_t *cblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int data_dir, bool fast_copy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct policy_work **work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * Retrieves background work. Returns -ENODATA when there's no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * background work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int (*get_background_work)(struct dm_cache_policy *p, bool idle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct policy_work **result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * You must pass in the same work pointer that you were given, not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * a copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) void (*complete_background_work)(struct dm_cache_policy *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct policy_work *work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) bool success);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) void (*set_dirty)(struct dm_cache_policy *p, dm_cblock_t cblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void (*clear_dirty)(struct dm_cache_policy *p, dm_cblock_t cblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Called when a cache target is first created. Used to load a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * mapping from the metadata device into the policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int (*load_mapping)(struct dm_cache_policy *p, dm_oblock_t oblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dm_cblock_t cblock, bool dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) uint32_t hint, bool hint_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Drops the mapping, irrespective of whether it's clean or dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Returns -ENODATA if cblock is not mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int (*invalidate_mapping)(struct dm_cache_policy *p, dm_cblock_t cblock);
^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) * Gets the hint for a given cblock. Called in a single threaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * context. So no locking required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) uint32_t (*get_hint)(struct dm_cache_policy *p, dm_cblock_t cblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * How full is the cache?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) dm_cblock_t (*residency)(struct dm_cache_policy *p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Because of where we sit in the block layer, we can be asked to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * map a lot of little bios that are all in the same block (no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * queue merging has occurred). To stop the policy being fooled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * these, the core target sends regular tick() calls to the policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * The policy should only count an entry as hit once per tick.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * This method is optional.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) void (*tick)(struct dm_cache_policy *p, bool can_block);
^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) * Configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int (*emit_config_values)(struct dm_cache_policy *p, char *result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned maxlen, ssize_t *sz_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int (*set_config_value)(struct dm_cache_policy *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) const char *key, const char *value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) void (*allow_migrations)(struct dm_cache_policy *p, bool allow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Book keeping ptr for the policy register, not for general use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) void *private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * We maintain a little register of the different policy types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #define CACHE_POLICY_NAME_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define CACHE_POLICY_VERSION_SIZE 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct dm_cache_policy_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* For use by the register code only. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * Policy writers should fill in these fields. The name field is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * what gets passed on the target line to select your policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) char name[CACHE_POLICY_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) unsigned version[CACHE_POLICY_VERSION_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * For use by an alias dm_cache_policy_type to point to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * real dm_cache_policy_type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct dm_cache_policy_type *real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * Policies may store a hint for each each cache block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * Currently the size of this hint must be 0 or 4 bytes but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * expect to relax this in future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) size_t hint_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct dm_cache_policy *(*create)(dm_cblock_t cache_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) sector_t origin_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) sector_t block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int dm_cache_policy_register(struct dm_cache_policy_type *type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) void dm_cache_policy_unregister(struct dm_cache_policy_type *type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #endif /* DM_CACHE_POLICY_H */