]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/ocf/src/metadata/metadata.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / ocf / src / metadata / metadata.h
1 /*
2 * Copyright(c) 2012-2018 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
4 */
5
6 #ifndef __METADATA_H__
7 #define __METADATA_H__
8
9 #include "metadata_common.h"
10 #include "../ocf_cache_priv.h"
11 #include "../ocf_ctx_priv.h"
12
13 static inline void ocf_metadata_eviction_lock(struct ocf_cache *cache)
14 {
15 env_spinlock_lock(&cache->metadata.lock.eviction);
16 }
17
18 static inline void ocf_metadata_eviction_unlock(struct ocf_cache *cache)
19 {
20 env_spinlock_unlock(&cache->metadata.lock.eviction);
21 }
22
23 #define OCF_METADATA_EVICTION_LOCK() \
24 ocf_metadata_eviction_lock(cache)
25
26 #define OCF_METADATA_EVICTION_UNLOCK() \
27 ocf_metadata_eviction_unlock(cache)
28
29 static inline void ocf_metadata_lock(struct ocf_cache *cache, int rw)
30 {
31 if (rw == OCF_METADATA_WR)
32 env_rwsem_down_write(&cache->metadata.lock.collision);
33 else if (rw == OCF_METADATA_RD)
34 env_rwsem_down_read(&cache->metadata.lock.collision);
35 else
36 ENV_BUG();
37 }
38
39
40 static inline void ocf_metadata_unlock(struct ocf_cache *cache, int rw)
41 {
42 if (rw == OCF_METADATA_WR)
43 env_rwsem_up_write(&cache->metadata.lock.collision);
44 else if (rw == OCF_METADATA_RD)
45 env_rwsem_up_read(&cache->metadata.lock.collision);
46 else
47 ENV_BUG();
48 }
49
50 static inline int ocf_metadata_try_lock(struct ocf_cache *cache, int rw)
51 {
52 int result = 0;
53
54 if (rw == OCF_METADATA_WR) {
55 result = env_rwsem_down_write_trylock(
56 &cache->metadata.lock.collision);
57 } else if (rw == OCF_METADATA_RD) {
58 result = env_rwsem_down_read_trylock(
59 &cache->metadata.lock.collision);
60 } else {
61 ENV_BUG();
62 }
63
64 if (result)
65 return -1;
66
67 return 0;
68 }
69
70 static inline void ocf_metadata_status_bits_lock(
71 struct ocf_cache *cache, int rw)
72 {
73 if (rw == OCF_METADATA_WR)
74 env_rwlock_write_lock(&cache->metadata.lock.status);
75 else if (rw == OCF_METADATA_RD)
76 env_rwlock_read_lock(&cache->metadata.lock.status);
77 else
78 ENV_BUG();
79 }
80
81 static inline void ocf_metadata_status_bits_unlock(
82 struct ocf_cache *cache, int rw)
83 {
84 if (rw == OCF_METADATA_WR)
85 env_rwlock_write_unlock(&cache->metadata.lock.status);
86 else if (rw == OCF_METADATA_RD)
87 env_rwlock_read_unlock(&cache->metadata.lock.status);
88 else
89 ENV_BUG();
90 }
91
92 #define OCF_METADATA_LOCK_RD() \
93 ocf_metadata_lock(cache, OCF_METADATA_RD)
94
95 #define OCF_METADATA_UNLOCK_RD() \
96 ocf_metadata_unlock(cache, OCF_METADATA_RD)
97
98 #define OCF_METADATA_LOCK_RD_TRY() \
99 ocf_metadata_try_lock(cache, OCF_METADATA_RD)
100
101 #define OCF_METADATA_LOCK_WR() \
102 ocf_metadata_lock(cache, OCF_METADATA_WR)
103
104 #define OCF_METADATA_LOCK_WR_TRY() \
105 ocf_metadata_try_lock(cache, OCF_METADATA_WR)
106
107 #define OCF_METADATA_UNLOCK_WR() \
108 ocf_metadata_unlock(cache, OCF_METADATA_WR)
109
110 #define OCF_METADATA_BITS_LOCK_RD() \
111 ocf_metadata_status_bits_lock(cache, OCF_METADATA_RD)
112
113 #define OCF_METADATA_BITS_UNLOCK_RD() \
114 ocf_metadata_status_bits_unlock(cache, OCF_METADATA_RD)
115
116 #define OCF_METADATA_BITS_LOCK_WR() \
117 ocf_metadata_status_bits_lock(cache, OCF_METADATA_WR)
118
119 #define OCF_METADATA_BITS_UNLOCK_WR() \
120 ocf_metadata_status_bits_unlock(cache, OCF_METADATA_WR)
121
122 #define OCF_METADATA_FLUSH_LOCK() \
123 ocf_metadata_flush_lock(cache)
124
125 #define OCF_METADATA_FLUSH_UNLOCK() \
126 ocf_metadata_flush_unlock(cache)
127
128 #include "metadata_cleaning_policy.h"
129 #include "metadata_eviction_policy.h"
130 #include "metadata_partition.h"
131 #include "metadata_hash.h"
132 #include "metadata_superblock.h"
133 #include "metadata_status.h"
134 #include "metadata_collision.h"
135 #include "metadata_core.h"
136 #include "metadata_misc.h"
137
138 #define INVALID 0
139 #define VALID 1
140 #define CLEAN 2
141 #define DIRTY 3
142
143 /**
144 * @brief Initialize metadata
145 *
146 * @param cache - Cache instance
147 * @param cache_line_size Cache line size
148 * @return 0 - Operation success otherwise failure
149 */
150 int ocf_metadata_init(struct ocf_cache *cache,
151 ocf_cache_line_size_t cache_line_size);
152
153 /**
154 * @brief Initialize per-cacheline metadata
155 *
156 * @param cache - Cache instance
157 * @param device_size - Device size in bytes
158 * @param cache_line_size Cache line size
159 * @return 0 - Operation success otherwise failure
160 */
161 int ocf_metadata_init_variable_size(struct ocf_cache *cache,
162 uint64_t device_size, ocf_cache_line_size_t cache_line_size,
163 ocf_metadata_layout_t layout);
164
165 /**
166 * @brief Initialize collision table
167 *
168 * @param cache - Cache instance
169 */
170 void ocf_metadata_init_freelist_partition(struct ocf_cache *cache);
171
172 /**
173 * @brief Initialize hash table
174 *
175 * @param cache - Cache instance
176 */
177 void ocf_metadata_init_hash_table(struct ocf_cache *cache);
178
179 /**
180 * @brief De-Initialize metadata
181 *
182 * @param cache - Cache instance
183 */
184 void ocf_metadata_deinit(struct ocf_cache *cache);
185
186 /**
187 * @brief De-Initialize per-cacheline metadata
188 *
189 * @param cache - Cache instance
190 */
191 void ocf_metadata_deinit_variable_size(struct ocf_cache *cache);
192
193 /**
194 * @brief Get memory footprint
195 *
196 * @param cache - Cache instance
197 * @return 0 - memory footprint
198 */
199 size_t ocf_metadata_size_of(struct ocf_cache *cache);
200
201 /**
202 * @brief Handle metadata error
203 *
204 * @param cache - Cache instance
205 */
206 void ocf_metadata_error(struct ocf_cache *cache);
207
208 /**
209 * @brief Get amount of cache lines
210 *
211 * @param cache - Cache instance
212 * @return Amount of cache lines (cache device lines - metadata space)
213 */
214 ocf_cache_line_t
215 ocf_metadata_get_cachelines_count(struct ocf_cache *cache);
216
217 /**
218 * @brief Get amount of pages required for metadata
219 *
220 * @param cache - Cache instance
221 * @return Pages required for store metadata on cache device
222 */
223 ocf_cache_line_t ocf_metadata_get_pages_count(struct ocf_cache *cache);
224
225 /**
226 * @brief Flush metadata
227 *
228 * @param cache - Cache instance
229 * @param cmpl - Completion callback
230 * @param priv - Completion context
231 */
232 void ocf_metadata_flush_all(ocf_cache_t cache,
233 ocf_metadata_end_t cmpl, void *priv);
234
235 /**
236 * @brief Mark specified cache line to be flushed
237 *
238 * @param[in] cache - Cache instance
239 * @param[in] line - cache line which to be flushed
240 */
241 void ocf_metadata_flush_mark(struct ocf_cache *cache, struct ocf_request *req,
242 uint32_t map_idx, int to_state, uint8_t start, uint8_t stop);
243
244 /**
245 * @brief Flush marked cache lines asynchronously
246 *
247 * @param cache - Cache instance
248 * @param queue - I/O queue to which metadata flush should be submitted
249 * @param remaining - request remaining
250 * @param complete - flushing request callback
251 * @param context - context that will be passed into callback
252 */
253 void ocf_metadata_flush_do_asynch(struct ocf_cache *cache,
254 struct ocf_request *req, ocf_req_end_t complete);
255
256 /**
257 * @brief Load metadata
258 *
259 * @param cache - Cache instance
260 * @param cmpl - Completion callback
261 * @param priv - Completion context
262 */
263 void ocf_metadata_load_all(ocf_cache_t cache,
264 ocf_metadata_end_t cmpl, void *priv);
265
266 /**
267 * @brief Load metadata required for recovery procedure
268 *
269 * @param cache Cache instance
270 * @param cmpl - Completion callback
271 * @param priv - Completion context
272 */
273 void ocf_metadata_load_recovery(ocf_cache_t cache,
274 ocf_metadata_end_t cmpl, void *priv);
275
276
277 /**
278 * @brief Get reserved area lba
279 *
280 * @param cache Cache instance
281 */
282 static inline uint64_t ocf_metadata_get_reserved_lba(ocf_cache_t cache)
283 {
284 return cache->metadata.iface.get_reserved_lba(cache);
285 }
286
287 /*
288 * NOTE Hash table is specific for hash table metadata service implementation
289 * and should be used internally by metadata service.
290 * At the moment there is no high level metadata interface because of that
291 * temporary defined in this file.
292 */
293
294 static inline ocf_cache_line_t
295 ocf_metadata_get_hash(struct ocf_cache *cache, ocf_cache_line_t index)
296 {
297 return cache->metadata.iface.get_hash(cache, index);
298 }
299
300 static inline void ocf_metadata_set_hash(struct ocf_cache *cache,
301 ocf_cache_line_t index, ocf_cache_line_t line)
302 {
303 cache->metadata.iface.set_hash(cache, index, line);
304 }
305
306 static inline ocf_cache_line_t ocf_metadata_entries_hash(
307 struct ocf_cache *cache)
308 {
309 return cache->metadata.iface.entries_hash(cache);
310 }
311
312 struct ocf_metadata_load_properties {
313 enum ocf_metadata_shutdown_status shutdown_status;
314 uint8_t dirty_flushed;
315 ocf_metadata_layout_t layout;
316 ocf_cache_line_size_t line_size;
317 ocf_cache_mode_t cache_mode;
318 };
319
320 typedef void (*ocf_metadata_load_properties_end_t)(void *priv, int error,
321 struct ocf_metadata_load_properties *properties);
322
323 void ocf_metadata_load_properties(ocf_volume_t volume,
324 ocf_metadata_load_properties_end_t cmpl, void *priv);
325
326 /**
327 * @brief Validate cache line size
328 *
329 * @param size Cache line size
330 * @return true - cache line size is valid, false - cache line is invalid
331 */
332 static inline bool ocf_metadata_line_size_is_valid(uint32_t size)
333 {
334 switch (size) {
335 case 4 * KiB:
336 case 8 * KiB:
337 case 16 * KiB:
338 case 32 * KiB:
339 case 64 * KiB:
340 return true;
341 default:
342 return false;
343 }
344 }
345
346 #endif /* METADATA_H_ */