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