2 * Copyright(c) 2012-2018 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
9 #include "metadata_common.h"
10 #include "../ocf_cache_priv.h"
11 #include "../ocf_ctx_priv.h"
13 static inline void ocf_metadata_eviction_lock(struct ocf_cache
*cache
)
15 env_spinlock_lock(&cache
->metadata
.lock
.eviction
);
18 static inline void ocf_metadata_eviction_unlock(struct ocf_cache
*cache
)
20 env_spinlock_unlock(&cache
->metadata
.lock
.eviction
);
23 #define OCF_METADATA_EVICTION_LOCK() \
24 ocf_metadata_eviction_lock(cache)
26 #define OCF_METADATA_EVICTION_UNLOCK() \
27 ocf_metadata_eviction_unlock(cache)
29 static inline void ocf_metadata_lock(struct ocf_cache
*cache
, int rw
)
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
);
40 static inline void ocf_metadata_unlock(struct ocf_cache
*cache
, int rw
)
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
);
50 static inline int ocf_metadata_try_lock(struct ocf_cache
*cache
, int rw
)
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
);
70 static inline void ocf_metadata_status_bits_lock(
71 struct ocf_cache
*cache
, int rw
)
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
);
81 static inline void ocf_metadata_status_bits_unlock(
82 struct ocf_cache
*cache
, int rw
)
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
);
92 #define OCF_METADATA_LOCK_RD() \
93 ocf_metadata_lock(cache, OCF_METADATA_RD)
95 #define OCF_METADATA_UNLOCK_RD() \
96 ocf_metadata_unlock(cache, OCF_METADATA_RD)
98 #define OCF_METADATA_LOCK_RD_TRY() \
99 ocf_metadata_try_lock(cache, OCF_METADATA_RD)
101 #define OCF_METADATA_LOCK_WR() \
102 ocf_metadata_lock(cache, OCF_METADATA_WR)
104 #define OCF_METADATA_LOCK_WR_TRY() \
105 ocf_metadata_try_lock(cache, OCF_METADATA_WR)
107 #define OCF_METADATA_UNLOCK_WR() \
108 ocf_metadata_unlock(cache, OCF_METADATA_WR)
110 #define OCF_METADATA_BITS_LOCK_RD() \
111 ocf_metadata_status_bits_lock(cache, OCF_METADATA_RD)
113 #define OCF_METADATA_BITS_UNLOCK_RD() \
114 ocf_metadata_status_bits_unlock(cache, OCF_METADATA_RD)
116 #define OCF_METADATA_BITS_LOCK_WR() \
117 ocf_metadata_status_bits_lock(cache, OCF_METADATA_WR)
119 #define OCF_METADATA_BITS_UNLOCK_WR() \
120 ocf_metadata_status_bits_unlock(cache, OCF_METADATA_WR)
122 #define OCF_METADATA_FLUSH_LOCK() \
123 ocf_metadata_flush_lock(cache)
125 #define OCF_METADATA_FLUSH_UNLOCK() \
126 ocf_metadata_flush_unlock(cache)
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"
144 * @brief Initialize metadata
146 * @param cache - Cache instance
147 * @param cache_line_size Cache line size
148 * @return 0 - Operation success otherwise failure
150 int ocf_metadata_init(struct ocf_cache
*cache
,
151 ocf_cache_line_size_t cache_line_size
);
154 * @brief Initialize per-cacheline metadata
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
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
);
166 * @brief Initialize collision table
168 * @param cache - Cache instance
170 void ocf_metadata_init_freelist_partition(struct ocf_cache
*cache
);
173 * @brief Initialize hash table
175 * @param cache - Cache instance
177 void ocf_metadata_init_hash_table(struct ocf_cache
*cache
);
180 * @brief De-Initialize metadata
182 * @param cache - Cache instance
184 void ocf_metadata_deinit(struct ocf_cache
*cache
);
187 * @brief De-Initialize per-cacheline metadata
189 * @param cache - Cache instance
191 void ocf_metadata_deinit_variable_size(struct ocf_cache
*cache
);
194 * @brief Get memory footprint
196 * @param cache - Cache instance
197 * @return 0 - memory footprint
199 size_t ocf_metadata_size_of(struct ocf_cache
*cache
);
202 * @brief Handle metadata error
204 * @param cache - Cache instance
206 void ocf_metadata_error(struct ocf_cache
*cache
);
209 * @brief Get amount of cache lines
211 * @param cache - Cache instance
212 * @return Amount of cache lines (cache device lines - metadata space)
215 ocf_metadata_get_cachelines_count(struct ocf_cache
*cache
);
218 * @brief Get amount of pages required for metadata
220 * @param cache - Cache instance
221 * @return Pages required for store metadata on cache device
223 ocf_cache_line_t
ocf_metadata_get_pages_count(struct ocf_cache
*cache
);
226 * @brief Flush metadata
228 * @param cache - Cache instance
229 * @param cmpl - Completion callback
230 * @param priv - Completion context
232 void ocf_metadata_flush_all(ocf_cache_t cache
,
233 ocf_metadata_end_t cmpl
, void *priv
);
236 * @brief Mark specified cache line to be flushed
238 * @param[in] cache - Cache instance
239 * @param[in] line - cache line which to be flushed
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
);
245 * @brief Flush marked cache lines asynchronously
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
253 void ocf_metadata_flush_do_asynch(struct ocf_cache
*cache
,
254 struct ocf_request
*req
, ocf_req_end_t complete
);
257 * @brief Load metadata
259 * @param cache - Cache instance
260 * @param cmpl - Completion callback
261 * @param priv - Completion context
263 void ocf_metadata_load_all(ocf_cache_t cache
,
264 ocf_metadata_end_t cmpl
, void *priv
);
267 * @brief Load metadata required for recovery procedure
269 * @param cache Cache instance
270 * @param cmpl - Completion callback
271 * @param priv - Completion context
273 void ocf_metadata_load_recovery(ocf_cache_t cache
,
274 ocf_metadata_end_t cmpl
, void *priv
);
278 * @brief Get reserved area lba
280 * @param cache Cache instance
282 static inline uint64_t ocf_metadata_get_reserved_lba(ocf_cache_t cache
)
284 return cache
->metadata
.iface
.get_reserved_lba(cache
);
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.
294 static inline ocf_cache_line_t
295 ocf_metadata_get_hash(struct ocf_cache
*cache
, ocf_cache_line_t index
)
297 return cache
->metadata
.iface
.get_hash(cache
, index
);
300 static inline void ocf_metadata_set_hash(struct ocf_cache
*cache
,
301 ocf_cache_line_t index
, ocf_cache_line_t line
)
303 cache
->metadata
.iface
.set_hash(cache
, index
, line
);
306 static inline ocf_cache_line_t
ocf_metadata_entries_hash(
307 struct ocf_cache
*cache
)
309 return cache
->metadata
.iface
.entries_hash(cache
);
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
;
320 typedef void (*ocf_metadata_load_properties_end_t
)(void *priv
, int error
,
321 struct ocf_metadata_load_properties
*properties
);
323 void ocf_metadata_load_properties(ocf_volume_t volume
,
324 ocf_metadata_load_properties_end_t cmpl
, void *priv
);
327 * @brief Validate cache line size
329 * @param size Cache line size
330 * @return true - cache line size is valid, false - cache line is invalid
332 static inline bool ocf_metadata_line_size_is_valid(uint32_t size
)
346 #endif /* METADATA_H_ */