13bd94003SHeinz Mauelshagen /* SPDX-License-Identifier: GPL-2.0-only */
2c6b4fcbaSJoe Thornber /*
3c6b4fcbaSJoe Thornber * Copyright (C) 2012 Red Hat. All rights reserved.
4c6b4fcbaSJoe Thornber *
5c6b4fcbaSJoe Thornber * This file is released under the GPL.
6c6b4fcbaSJoe Thornber */
7c6b4fcbaSJoe Thornber
8c6b4fcbaSJoe Thornber #ifndef DM_CACHE_POLICY_INTERNAL_H
9c6b4fcbaSJoe Thornber #define DM_CACHE_POLICY_INTERNAL_H
10c6b4fcbaSJoe Thornber
11451b9e00SJoe Thornber #include <linux/vmalloc.h>
12c6b4fcbaSJoe Thornber #include "dm-cache-policy.h"
13c6b4fcbaSJoe Thornber
14c6b4fcbaSJoe Thornber /*----------------------------------------------------------------*/
15c6b4fcbaSJoe Thornber
policy_lookup(struct dm_cache_policy * p,dm_oblock_t oblock,dm_cblock_t * cblock,int data_dir,bool fast_copy,bool * background_queued)16b29d4986SJoe Thornber static inline int policy_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock,
17b29d4986SJoe Thornber int data_dir, bool fast_copy, bool *background_queued)
18c6b4fcbaSJoe Thornber {
19b29d4986SJoe Thornber return p->lookup(p, oblock, cblock, data_dir, fast_copy, background_queued);
20c6b4fcbaSJoe Thornber }
21c6b4fcbaSJoe Thornber
policy_lookup_with_work(struct dm_cache_policy * p,dm_oblock_t oblock,dm_cblock_t * cblock,int data_dir,bool fast_copy,struct policy_work ** work)22b29d4986SJoe Thornber static inline int policy_lookup_with_work(struct dm_cache_policy *p,
23b29d4986SJoe Thornber dm_oblock_t oblock, dm_cblock_t *cblock,
24b29d4986SJoe Thornber int data_dir, bool fast_copy,
25b29d4986SJoe Thornber struct policy_work **work)
26c6b4fcbaSJoe Thornber {
27b29d4986SJoe Thornber if (!p->lookup_with_work) {
28b29d4986SJoe Thornber *work = NULL;
29b29d4986SJoe Thornber return p->lookup(p, oblock, cblock, data_dir, fast_copy, NULL);
30c6b4fcbaSJoe Thornber }
31c6b4fcbaSJoe Thornber
32b29d4986SJoe Thornber return p->lookup_with_work(p, oblock, cblock, data_dir, fast_copy, work);
33c6b4fcbaSJoe Thornber }
34c6b4fcbaSJoe Thornber
policy_get_background_work(struct dm_cache_policy * p,bool idle,struct policy_work ** result)35b29d4986SJoe Thornber static inline int policy_get_background_work(struct dm_cache_policy *p,
36b29d4986SJoe Thornber bool idle, struct policy_work **result)
37c6b4fcbaSJoe Thornber {
38b29d4986SJoe Thornber return p->get_background_work(p, idle, result);
39b29d4986SJoe Thornber }
40b29d4986SJoe Thornber
policy_complete_background_work(struct dm_cache_policy * p,struct policy_work * work,bool success)41b29d4986SJoe Thornber static inline void policy_complete_background_work(struct dm_cache_policy *p,
42b29d4986SJoe Thornber struct policy_work *work,
43b29d4986SJoe Thornber bool success)
44b29d4986SJoe Thornber {
45b29d4986SJoe Thornber return p->complete_background_work(p, work, success);
46b29d4986SJoe Thornber }
47b29d4986SJoe Thornber
policy_set_dirty(struct dm_cache_policy * p,dm_cblock_t cblock)48b29d4986SJoe Thornber static inline void policy_set_dirty(struct dm_cache_policy *p, dm_cblock_t cblock)
49b29d4986SJoe Thornber {
50b29d4986SJoe Thornber p->set_dirty(p, cblock);
51b29d4986SJoe Thornber }
52b29d4986SJoe Thornber
policy_clear_dirty(struct dm_cache_policy * p,dm_cblock_t cblock)53b29d4986SJoe Thornber static inline void policy_clear_dirty(struct dm_cache_policy *p, dm_cblock_t cblock)
54b29d4986SJoe Thornber {
55b29d4986SJoe Thornber p->clear_dirty(p, cblock);
56c6b4fcbaSJoe Thornber }
57c6b4fcbaSJoe Thornber
policy_load_mapping(struct dm_cache_policy * p,dm_oblock_t oblock,dm_cblock_t cblock,bool dirty,uint32_t hint,bool hint_valid)58c6b4fcbaSJoe Thornber static inline int policy_load_mapping(struct dm_cache_policy *p,
59c6b4fcbaSJoe Thornber dm_oblock_t oblock, dm_cblock_t cblock,
60b29d4986SJoe Thornber bool dirty, uint32_t hint, bool hint_valid)
61c6b4fcbaSJoe Thornber {
62b29d4986SJoe Thornber return p->load_mapping(p, oblock, cblock, dirty, hint, hint_valid);
63b29d4986SJoe Thornber }
64b29d4986SJoe Thornber
policy_invalidate_mapping(struct dm_cache_policy * p,dm_cblock_t cblock)65b29d4986SJoe Thornber static inline int policy_invalidate_mapping(struct dm_cache_policy *p,
66b29d4986SJoe Thornber dm_cblock_t cblock)
67b29d4986SJoe Thornber {
68b29d4986SJoe Thornber return p->invalidate_mapping(p, cblock);
69c6b4fcbaSJoe Thornber }
70c6b4fcbaSJoe Thornber
policy_get_hint(struct dm_cache_policy * p,dm_cblock_t cblock)714e781b49SJoe Thornber static inline uint32_t policy_get_hint(struct dm_cache_policy *p,
724e781b49SJoe Thornber dm_cblock_t cblock)
73c6b4fcbaSJoe Thornber {
744e781b49SJoe Thornber return p->get_hint ? p->get_hint(p, cblock) : 0;
75c6b4fcbaSJoe Thornber }
76c6b4fcbaSJoe Thornber
policy_residency(struct dm_cache_policy * p)77c6b4fcbaSJoe Thornber static inline dm_cblock_t policy_residency(struct dm_cache_policy *p)
78c6b4fcbaSJoe Thornber {
79c6b4fcbaSJoe Thornber return p->residency(p);
80c6b4fcbaSJoe Thornber }
81c6b4fcbaSJoe Thornber
policy_tick(struct dm_cache_policy * p,bool can_block)82fba10109SJoe Thornber static inline void policy_tick(struct dm_cache_policy *p, bool can_block)
83c6b4fcbaSJoe Thornber {
84c6b4fcbaSJoe Thornber if (p->tick)
85fba10109SJoe Thornber return p->tick(p, can_block);
86c6b4fcbaSJoe Thornber }
87c6b4fcbaSJoe Thornber
policy_emit_config_values(struct dm_cache_policy * p,char * result,unsigned int maxlen,ssize_t * sz_ptr)88028ae9f7SJoe Thornber static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result,
8986a3238cSHeinz Mauelshagen unsigned int maxlen, ssize_t *sz_ptr)
90c6b4fcbaSJoe Thornber {
91028ae9f7SJoe Thornber ssize_t sz = *sz_ptr;
92*0ef0b471SHeinz Mauelshagen
93c6b4fcbaSJoe Thornber if (p->emit_config_values)
94028ae9f7SJoe Thornber return p->emit_config_values(p, result, maxlen, sz_ptr);
95c6b4fcbaSJoe Thornber
96c6b4fcbaSJoe Thornber DMEMIT("0 ");
97028ae9f7SJoe Thornber *sz_ptr = sz;
98c6b4fcbaSJoe Thornber return 0;
99c6b4fcbaSJoe Thornber }
100c6b4fcbaSJoe Thornber
policy_set_config_value(struct dm_cache_policy * p,const char * key,const char * value)101c6b4fcbaSJoe Thornber static inline int policy_set_config_value(struct dm_cache_policy *p,
102c6b4fcbaSJoe Thornber const char *key, const char *value)
103c6b4fcbaSJoe Thornber {
104c6b4fcbaSJoe Thornber return p->set_config_value ? p->set_config_value(p, key, value) : -EINVAL;
105c6b4fcbaSJoe Thornber }
106c6b4fcbaSJoe Thornber
policy_allow_migrations(struct dm_cache_policy * p,bool allow)107b29d4986SJoe Thornber static inline void policy_allow_migrations(struct dm_cache_policy *p, bool allow)
108b29d4986SJoe Thornber {
109b29d4986SJoe Thornber return p->allow_migrations(p, allow);
110b29d4986SJoe Thornber }
111b29d4986SJoe Thornber
112c6b4fcbaSJoe Thornber /*----------------------------------------------------------------*/
113c6b4fcbaSJoe Thornber
114c6b4fcbaSJoe Thornber /*
115451b9e00SJoe Thornber * Some utility functions commonly used by policies and the core target.
116451b9e00SJoe Thornber */
bitset_size_in_bytes(unsigned int nr_entries)11786a3238cSHeinz Mauelshagen static inline size_t bitset_size_in_bytes(unsigned int nr_entries)
118451b9e00SJoe Thornber {
119451b9e00SJoe Thornber return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
120451b9e00SJoe Thornber }
121451b9e00SJoe Thornber
alloc_bitset(unsigned int nr_entries)12286a3238cSHeinz Mauelshagen static inline unsigned long *alloc_bitset(unsigned int nr_entries)
123451b9e00SJoe Thornber {
124451b9e00SJoe Thornber size_t s = bitset_size_in_bytes(nr_entries);
125*0ef0b471SHeinz Mauelshagen
126451b9e00SJoe Thornber return vzalloc(s);
127451b9e00SJoe Thornber }
128451b9e00SJoe Thornber
clear_bitset(void * bitset,unsigned int nr_entries)12986a3238cSHeinz Mauelshagen static inline void clear_bitset(void *bitset, unsigned int nr_entries)
130451b9e00SJoe Thornber {
131451b9e00SJoe Thornber size_t s = bitset_size_in_bytes(nr_entries);
132*0ef0b471SHeinz Mauelshagen
133451b9e00SJoe Thornber memset(bitset, 0, s);
134451b9e00SJoe Thornber }
135451b9e00SJoe Thornber
free_bitset(unsigned long * bits)136451b9e00SJoe Thornber static inline void free_bitset(unsigned long *bits)
137451b9e00SJoe Thornber {
138451b9e00SJoe Thornber vfree(bits);
139451b9e00SJoe Thornber }
140451b9e00SJoe Thornber
141451b9e00SJoe Thornber /*----------------------------------------------------------------*/
142451b9e00SJoe Thornber
143451b9e00SJoe Thornber /*
144c6b4fcbaSJoe Thornber * Creates a new cache policy given a policy name, a cache size, an origin size and the block size.
145c6b4fcbaSJoe Thornber */
146c6b4fcbaSJoe Thornber struct dm_cache_policy *dm_cache_policy_create(const char *name, dm_cblock_t cache_size,
147c6b4fcbaSJoe Thornber sector_t origin_size, sector_t block_size);
148c6b4fcbaSJoe Thornber
149c6b4fcbaSJoe Thornber /*
150c6b4fcbaSJoe Thornber * Destroys the policy. This drops references to the policy module as well
151c6b4fcbaSJoe Thornber * as calling it's destroy method. So always use this rather than calling
152c6b4fcbaSJoe Thornber * the policy->destroy method directly.
153c6b4fcbaSJoe Thornber */
154c6b4fcbaSJoe Thornber void dm_cache_policy_destroy(struct dm_cache_policy *p);
155c6b4fcbaSJoe Thornber
156c6b4fcbaSJoe Thornber /*
157c6b4fcbaSJoe Thornber * In case we've forgotten.
158c6b4fcbaSJoe Thornber */
159c6b4fcbaSJoe Thornber const char *dm_cache_policy_get_name(struct dm_cache_policy *p);
160c6b4fcbaSJoe Thornber
16186a3238cSHeinz Mauelshagen const unsigned int *dm_cache_policy_get_version(struct dm_cache_policy *p);
1624e7f506fSMike Snitzer
163c6b4fcbaSJoe Thornber size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p);
164c6b4fcbaSJoe Thornber
165c6b4fcbaSJoe Thornber /*----------------------------------------------------------------*/
166c6b4fcbaSJoe Thornber
167c6b4fcbaSJoe Thornber #endif /* DM_CACHE_POLICY_INTERNAL_H */
168