2 * Copyright (C) 2012 Red Hat. All rights reserved.
4 * This file is released under the GPL.
7 #ifndef DM_CACHE_POLICY_INTERNAL_H
8 #define DM_CACHE_POLICY_INTERNAL_H
10 #include <linux/vmalloc.h>
11 #include "dm-cache-policy.h"
13 /*----------------------------------------------------------------*/
16 * Little inline functions that simplify calling the policy methods.
18 static 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
)
23 return p
->map(p
, oblock
, can_block
, can_migrate
, discarded_oblock
, bio
, locker
, result
);
26 static inline int policy_lookup(struct dm_cache_policy
*p
, dm_oblock_t oblock
, dm_cblock_t
*cblock
)
29 return p
->lookup(p
, oblock
, cblock
);
32 static inline void policy_set_dirty(struct dm_cache_policy
*p
, dm_oblock_t oblock
)
35 p
->set_dirty(p
, oblock
);
38 static inline void policy_clear_dirty(struct dm_cache_policy
*p
, dm_oblock_t oblock
)
41 p
->clear_dirty(p
, oblock
);
44 static 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
)
48 return p
->load_mapping(p
, oblock
, cblock
, hint
, hint_valid
);
51 static inline int policy_walk_mappings(struct dm_cache_policy
*p
,
52 policy_walk_fn fn
, void *context
)
54 return p
->walk_mappings
? p
->walk_mappings(p
, fn
, context
) : 0;
57 static inline int policy_writeback_work(struct dm_cache_policy
*p
,
62 return p
->writeback_work
? p
->writeback_work(p
, oblock
, cblock
, critical_only
) : -ENOENT
;
65 static inline void policy_remove_mapping(struct dm_cache_policy
*p
, dm_oblock_t oblock
)
67 p
->remove_mapping(p
, oblock
);
70 static inline int policy_remove_cblock(struct dm_cache_policy
*p
, dm_cblock_t cblock
)
72 return p
->remove_cblock(p
, cblock
);
75 static inline void policy_force_mapping(struct dm_cache_policy
*p
,
76 dm_oblock_t current_oblock
, dm_oblock_t new_oblock
)
78 return p
->force_mapping(p
, current_oblock
, new_oblock
);
81 static inline dm_cblock_t
policy_residency(struct dm_cache_policy
*p
)
83 return p
->residency(p
);
86 static inline void policy_tick(struct dm_cache_policy
*p
, bool can_block
)
89 return p
->tick(p
, can_block
);
92 static inline int policy_emit_config_values(struct dm_cache_policy
*p
, char *result
,
93 unsigned maxlen
, ssize_t
*sz_ptr
)
96 if (p
->emit_config_values
)
97 return p
->emit_config_values(p
, result
, maxlen
, sz_ptr
);
104 static inline int policy_set_config_value(struct dm_cache_policy
*p
,
105 const char *key
, const char *value
)
107 return p
->set_config_value
? p
->set_config_value(p
, key
, value
) : -EINVAL
;
110 /*----------------------------------------------------------------*/
113 * Some utility functions commonly used by policies and the core target.
115 static inline size_t bitset_size_in_bytes(unsigned nr_entries
)
117 return sizeof(unsigned long) * dm_div_up(nr_entries
, BITS_PER_LONG
);
120 static inline unsigned long *alloc_bitset(unsigned nr_entries
)
122 size_t s
= bitset_size_in_bytes(nr_entries
);
126 static inline void clear_bitset(void *bitset
, unsigned nr_entries
)
128 size_t s
= bitset_size_in_bytes(nr_entries
);
129 memset(bitset
, 0, s
);
132 static inline void free_bitset(unsigned long *bits
)
137 /*----------------------------------------------------------------*/
140 * Creates a new cache policy given a policy name, a cache size, an origin size and the block size.
142 struct dm_cache_policy
*dm_cache_policy_create(const char *name
, dm_cblock_t cache_size
,
143 sector_t origin_size
, sector_t block_size
);
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.
150 void dm_cache_policy_destroy(struct dm_cache_policy
*p
);
153 * In case we've forgotten.
155 const char *dm_cache_policy_get_name(struct dm_cache_policy
*p
);
157 const unsigned *dm_cache_policy_get_version(struct dm_cache_policy
*p
);
159 size_t dm_cache_policy_get_hint_size(struct dm_cache_policy
*p
);
161 /*----------------------------------------------------------------*/
163 #endif /* DM_CACHE_POLICY_INTERNAL_H */