blob: 476a4f6794fcaa64666dce91a358665ba82c232f [file] [log] [blame]
Heinz Mauelshagen3bd94002023-01-25 21:00:44 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002/*
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
Joe Thornber451b9e02015-05-15 15:22:02 +010011#include <linux/vmalloc.h>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000012#include "dm-cache-policy.h"
13
14/*----------------------------------------------------------------*/
15
Joe Thornberb29d4982016-12-15 04:57:31 -050016static 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)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000018{
Joe Thornberb29d4982016-12-15 04:57:31 -050019 return p->lookup(p, oblock, cblock, data_dir, fast_copy, background_queued);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000020}
21
Joe Thornberb29d4982016-12-15 04:57:31 -050022static 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)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000026{
Joe Thornberb29d4982016-12-15 04:57:31 -050027 if (!p->lookup_with_work) {
28 *work = NULL;
29 return p->lookup(p, oblock, cblock, data_dir, fast_copy, NULL);
30 }
31
32 return p->lookup_with_work(p, oblock, cblock, data_dir, fast_copy, work);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000033}
34
Joe Thornberb29d4982016-12-15 04:57:31 -050035static inline int policy_get_background_work(struct dm_cache_policy *p,
36 bool idle, struct policy_work **result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000037{
Joe Thornberb29d4982016-12-15 04:57:31 -050038 return p->get_background_work(p, idle, result);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000039}
40
Joe Thornberb29d4982016-12-15 04:57:31 -050041static inline void policy_complete_background_work(struct dm_cache_policy *p,
42 struct policy_work *work,
43 bool success)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000044{
Joe Thornberb29d4982016-12-15 04:57:31 -050045 return p->complete_background_work(p, work, success);
46}
47
48static inline void policy_set_dirty(struct dm_cache_policy *p, dm_cblock_t cblock)
49{
50 p->set_dirty(p, cblock);
51}
52
53static inline void policy_clear_dirty(struct dm_cache_policy *p, dm_cblock_t cblock)
54{
55 p->clear_dirty(p, cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000056}
57
58static inline int policy_load_mapping(struct dm_cache_policy *p,
59 dm_oblock_t oblock, dm_cblock_t cblock,
Joe Thornberb29d4982016-12-15 04:57:31 -050060 bool dirty, uint32_t hint, bool hint_valid)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000061{
Joe Thornberb29d4982016-12-15 04:57:31 -050062 return p->load_mapping(p, oblock, cblock, dirty, hint, hint_valid);
63}
64
65static inline int policy_invalidate_mapping(struct dm_cache_policy *p,
66 dm_cblock_t cblock)
67{
68 return p->invalidate_mapping(p, cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000069}
70
Joe Thornber4e781b42016-09-15 09:23:46 -040071static inline uint32_t policy_get_hint(struct dm_cache_policy *p,
72 dm_cblock_t cblock)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000073{
Joe Thornber4e781b42016-09-15 09:23:46 -040074 return p->get_hint ? p->get_hint(p, cblock) : 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000075}
76
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000077static inline dm_cblock_t policy_residency(struct dm_cache_policy *p)
78{
79 return p->residency(p);
80}
81
Joe Thornberfba10102015-05-29 10:20:56 +010082static inline void policy_tick(struct dm_cache_policy *p, bool can_block)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000083{
84 if (p->tick)
Joe Thornberfba10102015-05-29 10:20:56 +010085 return p->tick(p, can_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000086}
87
Joe Thornber028ae9f2015-04-22 16:42:35 -040088static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result,
Heinz Mauelshagen86a32382023-01-25 21:14:58 +010089 unsigned int maxlen, ssize_t *sz_ptr)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000090{
Joe Thornber028ae9f2015-04-22 16:42:35 -040091 ssize_t sz = *sz_ptr;
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +010092
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000093 if (p->emit_config_values)
Joe Thornber028ae9f2015-04-22 16:42:35 -040094 return p->emit_config_values(p, result, maxlen, sz_ptr);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000095
Joe Thornber028ae9f2015-04-22 16:42:35 -040096 DMEMIT("0 ");
97 *sz_ptr = sz;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000098 return 0;
99}
100
101static 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
Joe Thornberb29d4982016-12-15 04:57:31 -0500107static inline void policy_allow_migrations(struct dm_cache_policy *p, bool allow)
108{
109 return p->allow_migrations(p, allow);
110}
111
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000112/*----------------------------------------------------------------*/
113
114/*
Joe Thornber451b9e02015-05-15 15:22:02 +0100115 * Some utility functions commonly used by policies and the core target.
116 */
Heinz Mauelshagen86a32382023-01-25 21:14:58 +0100117static inline size_t bitset_size_in_bytes(unsigned int nr_entries)
Joe Thornber451b9e02015-05-15 15:22:02 +0100118{
119 return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
120}
121
Heinz Mauelshagen86a32382023-01-25 21:14:58 +0100122static inline unsigned long *alloc_bitset(unsigned int nr_entries)
Joe Thornber451b9e02015-05-15 15:22:02 +0100123{
124 size_t s = bitset_size_in_bytes(nr_entries);
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +0100125
Joe Thornber451b9e02015-05-15 15:22:02 +0100126 return vzalloc(s);
127}
128
Heinz Mauelshagen86a32382023-01-25 21:14:58 +0100129static inline void clear_bitset(void *bitset, unsigned int nr_entries)
Joe Thornber451b9e02015-05-15 15:22:02 +0100130{
131 size_t s = bitset_size_in_bytes(nr_entries);
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +0100132
Joe Thornber451b9e02015-05-15 15:22:02 +0100133 memset(bitset, 0, s);
134}
135
136static inline void free_bitset(unsigned long *bits)
137{
138 vfree(bits);
139}
140
141/*----------------------------------------------------------------*/
142
143/*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000144 * Creates a new cache policy given a policy name, a cache size, an origin size and the block size.
145 */
146struct 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 */
154void dm_cache_policy_destroy(struct dm_cache_policy *p);
155
156/*
157 * In case we've forgotten.
158 */
159const char *dm_cache_policy_get_name(struct dm_cache_policy *p);
160
Heinz Mauelshagen86a32382023-01-25 21:14:58 +0100161const unsigned int *dm_cache_policy_get_version(struct dm_cache_policy *p);
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000162
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000163size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p);
164
165/*----------------------------------------------------------------*/
166
167#endif /* DM_CACHE_POLICY_INTERNAL_H */