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