Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Red Hat. All rights reserved. |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #ifndef DM_CACHE_POLICY_INTERNAL_H |
| 8 | #define DM_CACHE_POLICY_INTERNAL_H |
| 9 | |
Joe Thornber | 451b9e0 | 2015-05-15 15:22:02 +0100 | [diff] [blame] | 10 | #include <linux/vmalloc.h> |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 11 | #include "dm-cache-policy.h" |
| 12 | |
| 13 | /*----------------------------------------------------------------*/ |
| 14 | |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 15 | static inline int policy_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock, |
| 16 | int data_dir, bool fast_copy, bool *background_queued) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 17 | { |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 18 | return p->lookup(p, oblock, cblock, data_dir, fast_copy, background_queued); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 21 | static inline int policy_lookup_with_work(struct dm_cache_policy *p, |
| 22 | dm_oblock_t oblock, dm_cblock_t *cblock, |
| 23 | int data_dir, bool fast_copy, |
| 24 | struct policy_work **work) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 25 | { |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 26 | if (!p->lookup_with_work) { |
| 27 | *work = NULL; |
| 28 | return p->lookup(p, oblock, cblock, data_dir, fast_copy, NULL); |
| 29 | } |
| 30 | |
| 31 | return p->lookup_with_work(p, oblock, cblock, data_dir, fast_copy, work); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 34 | static inline int policy_get_background_work(struct dm_cache_policy *p, |
| 35 | bool idle, struct policy_work **result) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 36 | { |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 37 | return p->get_background_work(p, idle, result); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 40 | static inline void policy_complete_background_work(struct dm_cache_policy *p, |
| 41 | struct policy_work *work, |
| 42 | bool success) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 43 | { |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 44 | return p->complete_background_work(p, work, success); |
| 45 | } |
| 46 | |
| 47 | static inline void policy_set_dirty(struct dm_cache_policy *p, dm_cblock_t cblock) |
| 48 | { |
| 49 | p->set_dirty(p, cblock); |
| 50 | } |
| 51 | |
| 52 | static inline void policy_clear_dirty(struct dm_cache_policy *p, dm_cblock_t cblock) |
| 53 | { |
| 54 | p->clear_dirty(p, cblock); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static inline int policy_load_mapping(struct dm_cache_policy *p, |
| 58 | dm_oblock_t oblock, dm_cblock_t cblock, |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 59 | bool dirty, uint32_t hint, bool hint_valid) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 60 | { |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 61 | return p->load_mapping(p, oblock, cblock, dirty, hint, hint_valid); |
| 62 | } |
| 63 | |
| 64 | static inline int policy_invalidate_mapping(struct dm_cache_policy *p, |
| 65 | dm_cblock_t cblock) |
| 66 | { |
| 67 | return p->invalidate_mapping(p, cblock); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Joe Thornber | 4e781b4 | 2016-09-15 09:23:46 -0400 | [diff] [blame] | 70 | static inline uint32_t policy_get_hint(struct dm_cache_policy *p, |
| 71 | dm_cblock_t cblock) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 72 | { |
Joe Thornber | 4e781b4 | 2016-09-15 09:23:46 -0400 | [diff] [blame] | 73 | return p->get_hint ? p->get_hint(p, cblock) : 0; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 76 | static inline dm_cblock_t policy_residency(struct dm_cache_policy *p) |
| 77 | { |
| 78 | return p->residency(p); |
| 79 | } |
| 80 | |
Joe Thornber | fba1010 | 2015-05-29 10:20:56 +0100 | [diff] [blame] | 81 | static inline void policy_tick(struct dm_cache_policy *p, bool can_block) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 82 | { |
| 83 | if (p->tick) |
Joe Thornber | fba1010 | 2015-05-29 10:20:56 +0100 | [diff] [blame] | 84 | return p->tick(p, can_block); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 87 | static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result, |
| 88 | unsigned maxlen, ssize_t *sz_ptr) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 89 | { |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 90 | ssize_t sz = *sz_ptr; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 91 | if (p->emit_config_values) |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 92 | return p->emit_config_values(p, result, maxlen, sz_ptr); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 93 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 94 | DMEMIT("0 "); |
| 95 | *sz_ptr = sz; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static inline int policy_set_config_value(struct dm_cache_policy *p, |
| 100 | const char *key, const char *value) |
| 101 | { |
| 102 | return p->set_config_value ? p->set_config_value(p, key, value) : -EINVAL; |
| 103 | } |
| 104 | |
Joe Thornber | b29d498 | 2016-12-15 04:57:31 -0500 | [diff] [blame] | 105 | static inline void policy_allow_migrations(struct dm_cache_policy *p, bool allow) |
| 106 | { |
| 107 | return p->allow_migrations(p, allow); |
| 108 | } |
| 109 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 110 | /*----------------------------------------------------------------*/ |
| 111 | |
| 112 | /* |
Joe Thornber | 451b9e0 | 2015-05-15 15:22:02 +0100 | [diff] [blame] | 113 | * Some utility functions commonly used by policies and the core target. |
| 114 | */ |
| 115 | static inline size_t bitset_size_in_bytes(unsigned nr_entries) |
| 116 | { |
| 117 | return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG); |
| 118 | } |
| 119 | |
| 120 | static inline unsigned long *alloc_bitset(unsigned nr_entries) |
| 121 | { |
| 122 | size_t s = bitset_size_in_bytes(nr_entries); |
| 123 | return vzalloc(s); |
| 124 | } |
| 125 | |
| 126 | static inline void clear_bitset(void *bitset, unsigned nr_entries) |
| 127 | { |
| 128 | size_t s = bitset_size_in_bytes(nr_entries); |
| 129 | memset(bitset, 0, s); |
| 130 | } |
| 131 | |
| 132 | static inline void free_bitset(unsigned long *bits) |
| 133 | { |
| 134 | vfree(bits); |
| 135 | } |
| 136 | |
| 137 | /*----------------------------------------------------------------*/ |
| 138 | |
| 139 | /* |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 140 | * Creates a new cache policy given a policy name, a cache size, an origin size and the block size. |
| 141 | */ |
| 142 | struct dm_cache_policy *dm_cache_policy_create(const char *name, dm_cblock_t cache_size, |
| 143 | sector_t origin_size, sector_t block_size); |
| 144 | |
| 145 | /* |
| 146 | * Destroys the policy. This drops references to the policy module as well |
| 147 | * as calling it's destroy method. So always use this rather than calling |
| 148 | * the policy->destroy method directly. |
| 149 | */ |
| 150 | void dm_cache_policy_destroy(struct dm_cache_policy *p); |
| 151 | |
| 152 | /* |
| 153 | * In case we've forgotten. |
| 154 | */ |
| 155 | const char *dm_cache_policy_get_name(struct dm_cache_policy *p); |
| 156 | |
Mike Snitzer | 4e7f506 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 157 | const unsigned *dm_cache_policy_get_version(struct dm_cache_policy *p); |
| 158 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 159 | size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p); |
| 160 | |
| 161 | /*----------------------------------------------------------------*/ |
| 162 | |
| 163 | #endif /* DM_CACHE_POLICY_INTERNAL_H */ |