blob: 9dc05a52369e60cb280c9bebac3985c1ce9b16d1 [file] [log] [blame]
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001/*
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
Joe Thornber451b9e02015-05-15 15:22:02 +010010#include <linux/vmalloc.h>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000011#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,
Joe Thornberfb4100a2015-05-20 10:30:32 +010020 struct bio *bio, struct policy_locker *locker,
21 struct policy_result *result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000022{
Joe Thornberfb4100a2015-05-20 10:30:32 +010023 return p->map(p, oblock, can_block, can_migrate, discarded_oblock, bio, locker, result);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000024}
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,
Joe Thornber20f68142015-05-15 15:20:09 +010059 dm_cblock_t *cblock,
60 bool critical_only)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000061{
Joe Thornber20f68142015-05-15 15:20:09 +010062 return p->writeback_work ? p->writeback_work(p, oblock, cblock, critical_only) : -ENOENT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000063}
64
65static inline void policy_remove_mapping(struct dm_cache_policy *p, dm_oblock_t oblock)
66{
Joe Thornber33519372013-10-24 14:10:28 -040067 p->remove_mapping(p, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000068}
69
Joe Thornber532906a2013-11-08 16:36:17 +000070static inline int policy_remove_cblock(struct dm_cache_policy *p, dm_cblock_t cblock)
71{
72 return p->remove_cblock(p, cblock);
73}
74
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000075static 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)
87{
88 if (p->tick)
89 return p->tick(p);
90}
91
92static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result, unsigned maxlen)
93{
94 ssize_t sz = 0;
95 if (p->emit_config_values)
96 return p->emit_config_values(p, result, maxlen);
97
98 DMEMIT("0");
99 return 0;
100}
101
102static inline int policy_set_config_value(struct dm_cache_policy *p,
103 const char *key, const char *value)
104{
105 return p->set_config_value ? p->set_config_value(p, key, value) : -EINVAL;
106}
107
108/*----------------------------------------------------------------*/
109
110/*
Joe Thornber451b9e02015-05-15 15:22:02 +0100111 * Some utility functions commonly used by policies and the core target.
112 */
113static inline size_t bitset_size_in_bytes(unsigned nr_entries)
114{
115 return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
116}
117
118static inline unsigned long *alloc_bitset(unsigned nr_entries)
119{
120 size_t s = bitset_size_in_bytes(nr_entries);
121 return vzalloc(s);
122}
123
124static inline void clear_bitset(void *bitset, unsigned nr_entries)
125{
126 size_t s = bitset_size_in_bytes(nr_entries);
127 memset(bitset, 0, s);
128}
129
130static inline void free_bitset(unsigned long *bits)
131{
132 vfree(bits);
133}
134
135/*----------------------------------------------------------------*/
136
137/*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000138 * Creates a new cache policy given a policy name, a cache size, an origin size and the block size.
139 */
140struct dm_cache_policy *dm_cache_policy_create(const char *name, dm_cblock_t cache_size,
141 sector_t origin_size, sector_t block_size);
142
143/*
144 * Destroys the policy. This drops references to the policy module as well
145 * as calling it's destroy method. So always use this rather than calling
146 * the policy->destroy method directly.
147 */
148void dm_cache_policy_destroy(struct dm_cache_policy *p);
149
150/*
151 * In case we've forgotten.
152 */
153const char *dm_cache_policy_get_name(struct dm_cache_policy *p);
154
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000155const unsigned *dm_cache_policy_get_version(struct dm_cache_policy *p);
156
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000157size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p);
158
159/*----------------------------------------------------------------*/
160
161#endif /* DM_CACHE_POLICY_INTERNAL_H */