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 "dm-cache-policy.h"
11 
12 /*----------------------------------------------------------------*/
13 
14 /*
15  * Little inline functions that simplify calling the policy methods.
16  */
17 static inline int policy_map(struct dm_cache_policy *p, dm_oblock_t oblock,
18 			     bool can_block, bool can_migrate, bool discarded_oblock,
19 			     struct bio *bio, struct policy_result *result)
20 {
21 	return p->map(p, oblock, can_block, can_migrate, discarded_oblock, bio, result);
22 }
23 
24 static inline int policy_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock)
25 {
26 	BUG_ON(!p->lookup);
27 	return p->lookup(p, oblock, cblock);
28 }
29 
30 static inline void policy_set_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
31 {
32 	if (p->set_dirty)
33 		p->set_dirty(p, oblock);
34 }
35 
36 static inline void policy_clear_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
37 {
38 	if (p->clear_dirty)
39 		p->clear_dirty(p, oblock);
40 }
41 
42 static inline int policy_load_mapping(struct dm_cache_policy *p,
43 				      dm_oblock_t oblock, dm_cblock_t cblock,
44 				      uint32_t hint, bool hint_valid)
45 {
46 	return p->load_mapping(p, oblock, cblock, hint, hint_valid);
47 }
48 
49 static inline int policy_walk_mappings(struct dm_cache_policy *p,
50 				      policy_walk_fn fn, void *context)
51 {
52 	return p->walk_mappings ? p->walk_mappings(p, fn, context) : 0;
53 }
54 
55 static inline int policy_writeback_work(struct dm_cache_policy *p,
56 					dm_oblock_t *oblock,
57 					dm_cblock_t *cblock)
58 {
59 	return p->writeback_work ? p->writeback_work(p, oblock, cblock) : -ENOENT;
60 }
61 
62 static inline void policy_remove_mapping(struct dm_cache_policy *p, dm_oblock_t oblock)
63 {
64 	return p->remove_mapping(p, oblock);
65 }
66 
67 static inline void policy_force_mapping(struct dm_cache_policy *p,
68 					dm_oblock_t current_oblock, dm_oblock_t new_oblock)
69 {
70 	return p->force_mapping(p, current_oblock, new_oblock);
71 }
72 
73 static inline dm_cblock_t policy_residency(struct dm_cache_policy *p)
74 {
75 	return p->residency(p);
76 }
77 
78 static inline void policy_tick(struct dm_cache_policy *p)
79 {
80 	if (p->tick)
81 		return p->tick(p);
82 }
83 
84 static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result, unsigned maxlen)
85 {
86 	ssize_t sz = 0;
87 	if (p->emit_config_values)
88 		return p->emit_config_values(p, result, maxlen);
89 
90 	DMEMIT("0");
91 	return 0;
92 }
93 
94 static inline int policy_set_config_value(struct dm_cache_policy *p,
95 					  const char *key, const char *value)
96 {
97 	return p->set_config_value ? p->set_config_value(p, key, value) : -EINVAL;
98 }
99 
100 /*----------------------------------------------------------------*/
101 
102 /*
103  * Creates a new cache policy given a policy name, a cache size, an origin size and the block size.
104  */
105 struct dm_cache_policy *dm_cache_policy_create(const char *name, dm_cblock_t cache_size,
106 					       sector_t origin_size, sector_t block_size);
107 
108 /*
109  * Destroys the policy.  This drops references to the policy module as well
110  * as calling it's destroy method.  So always use this rather than calling
111  * the policy->destroy method directly.
112  */
113 void dm_cache_policy_destroy(struct dm_cache_policy *p);
114 
115 /*
116  * In case we've forgotten.
117  */
118 const char *dm_cache_policy_get_name(struct dm_cache_policy *p);
119 
120 const unsigned *dm_cache_policy_get_version(struct dm_cache_policy *p);
121 
122 size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p);
123 
124 /*----------------------------------------------------------------*/
125 
126 #endif /* DM_CACHE_POLICY_INTERNAL_H */
127