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