Blame view

drivers/md/dm-cache-policy.h 8.15 KB
c6b4fcbad   Joe Thornber   dm: add cache target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  /*
   * Copyright (C) 2012 Red Hat. All rights reserved.
   *
   * This file is released under the GPL.
   */
  
  #ifndef DM_CACHE_POLICY_H
  #define DM_CACHE_POLICY_H
  
  #include "dm-cache-block-types.h"
  
  #include <linux/device-mapper.h>
  
  /*----------------------------------------------------------------*/
  
  /* FIXME: make it clear which methods are optional.  Get debug policy to
   * double check this at start.
   */
  
  /*
   * The cache policy makes the important decisions about which blocks get to
   * live on the faster cache device.
   *
   * When the core target has to remap a bio it calls the 'map' method of the
   * policy.  This returns an instruction telling the core target what to do.
   *
   * POLICY_HIT:
   *   That block is in the cache.  Remap to the cache and carry on.
   *
   * POLICY_MISS:
   *   This block is on the origin device.  Remap and carry on.
   *
   * POLICY_NEW:
   *   This block is currently on the origin device, but the policy wants to
   *   move it.  The core should:
   *
   *   - hold any further io to this origin block
   *   - copy the origin to the given cache block
   *   - release all the held blocks
   *   - remap the original block to the cache
   *
   * POLICY_REPLACE:
   *   This block is currently on the origin device.  The policy wants to
   *   move it to the cache, with the added complication that the destination
   *   cache block needs a writeback first.  The core should:
   *
   *   - hold any further io to this origin block
   *   - hold any further io to the origin block that's being written back
   *   - writeback
   *   - copy new block to cache
   *   - release held blocks
   *   - remap bio to cache and reissue.
   *
   * Should the core run into trouble while processing a POLICY_NEW or
   * POLICY_REPLACE instruction it will roll back the policies mapping using
   * remove_mapping() or force_mapping().  These methods must not fail.  This
   * approach avoids having transactional semantics in the policy (ie, the
   * core informing the policy when a migration is complete), and hence makes
   * it easier to write new policies.
   *
   * In general policy methods should never block, except in the case of the
   * map function when can_migrate is set.  So be careful to implement using
   * bounded, preallocated memory.
   */
  enum policy_operation {
  	POLICY_HIT,
  	POLICY_MISS,
  	POLICY_NEW,
  	POLICY_REPLACE
  };
  
  /*
fb4100ae7   Joe Thornber   dm cache: fix rac...
73
74
75
76
77
78
79
80
81
82
83
84
   * When issuing a POLICY_REPLACE the policy needs to make a callback to
   * lock the block being demoted.  This doesn't need to occur during a
   * writeback operation since the block remains in the cache.
   */
  struct policy_locker;
  typedef int (*policy_lock_fn)(struct policy_locker *l, dm_oblock_t oblock);
  
  struct policy_locker {
  	policy_lock_fn fn;
  };
  
  /*
c6b4fcbad   Joe Thornber   dm: add cache target
85
86
87
88
89
90
91
   * This is the instruction passed back to the core target.
   */
  struct policy_result {
  	enum policy_operation op;
  	dm_oblock_t old_oblock;	/* POLICY_REPLACE */
  	dm_cblock_t cblock;	/* POLICY_HIT, POLICY_NEW, POLICY_REPLACE */
  };
c6b4fcbad   Joe Thornber   dm: add cache target
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  /*
   * The cache policy object.  Just a bunch of methods.  It is envisaged that
   * this structure will be embedded in a bigger, policy specific structure
   * (ie. use container_of()).
   */
  struct dm_cache_policy {
  
  	/*
  	 * FIXME: make it clear which methods are optional, and which may
  	 * block.
  	 */
  
  	/*
  	 * Destroys this object.
  	 */
  	void (*destroy)(struct dm_cache_policy *p);
  
  	/*
  	 * See large comment above.
  	 *
  	 * oblock      - the origin block we're interested in.
  	 *
  	 * can_block - indicates whether the current thread is allowed to
  	 *             block.  -EWOULDBLOCK returned if it can't and would.
  	 *
  	 * can_migrate - gives permission for POLICY_NEW or POLICY_REPLACE
  	 *               instructions.  If denied and the policy would have
  	 *               returned one of these instructions it should
  	 *               return -EWOULDBLOCK.
  	 *
  	 * discarded_oblock - indicates whether the whole origin block is
  	 *               in a discarded state (FIXME: better to tell the
  	 *               policy about this sooner, so it can recycle that
  	 *               cache block if it wants.)
  	 * bio         - the bio that triggered this call.
  	 * result      - gets filled in with the instruction.
  	 *
  	 * May only return 0, or -EWOULDBLOCK (if !can_migrate)
  	 */
  	int (*map)(struct dm_cache_policy *p, dm_oblock_t oblock,
  		   bool can_block, bool can_migrate, bool discarded_oblock,
fb4100ae7   Joe Thornber   dm cache: fix rac...
133
134
  		   struct bio *bio, struct policy_locker *locker,
  		   struct policy_result *result);
c6b4fcbad   Joe Thornber   dm: add cache target
135
136
137
138
139
140
141
  
  	/*
  	 * Sometimes we want to see if a block is in the cache, without
  	 * triggering any update of stats.  (ie. it's not a real hit).
  	 *
  	 * Must not block.
  	 *
e12c1fd9d   Alasdair G Kergon   dm cache policy: ...
142
143
  	 * Returns 0 if in cache, -ENOENT if not, < 0 for other errors
  	 * (-EWOULDBLOCK would be typical).
c6b4fcbad   Joe Thornber   dm: add cache target
144
145
  	 */
  	int (*lookup)(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock);
c6b4fcbad   Joe Thornber   dm: add cache target
146
147
148
149
150
151
152
153
154
  	void (*set_dirty)(struct dm_cache_policy *p, dm_oblock_t oblock);
  	void (*clear_dirty)(struct dm_cache_policy *p, dm_oblock_t oblock);
  
  	/*
  	 * Called when a cache target is first created.  Used to load a
  	 * mapping from the metadata device into the policy.
  	 */
  	int (*load_mapping)(struct dm_cache_policy *p, dm_oblock_t oblock,
  			    dm_cblock_t cblock, uint32_t hint, bool hint_valid);
4e781b498   Joe Thornber   dm cache: speed u...
155
156
157
158
159
  	/*
  	 * Gets the hint for a given cblock.  Called in a single threaded
  	 * context.  So no locking required.
  	 */
  	uint32_t (*get_hint)(struct dm_cache_policy *p, dm_cblock_t cblock);
c6b4fcbad   Joe Thornber   dm: add cache target
160
161
162
163
164
165
166
167
  
  	/*
  	 * Override functions used on the error paths of the core target.
  	 * They must succeed.
  	 */
  	void (*remove_mapping)(struct dm_cache_policy *p, dm_oblock_t oblock);
  	void (*force_mapping)(struct dm_cache_policy *p, dm_oblock_t current_oblock,
  			      dm_oblock_t new_oblock);
532906aa7   Joe Thornber   dm cache: add rem...
168
169
170
171
172
173
174
  	/*
  	 * This is called via the invalidate_cblocks message.  It is
  	 * possible the particular cblock has already been removed due to a
  	 * write io in passthrough mode.  In which case this should return
  	 * -ENODATA.
  	 */
  	int (*remove_cblock)(struct dm_cache_policy *p, dm_cblock_t cblock);
c6b4fcbad   Joe Thornber   dm: add cache target
175

532906aa7   Joe Thornber   dm cache: add rem...
176
  	/*
20f6814b9   Joe Thornber   dm cache: pass a ...
177
178
179
  	 * Provide a dirty block to be written back by the core target.  If
  	 * critical_only is set then the policy should only provide work if
  	 * it urgently needs it.
532906aa7   Joe Thornber   dm cache: add rem...
180
181
182
183
184
185
186
  	 *
  	 * Returns:
  	 *
  	 * 0 and @cblock,@oblock: block to write back provided
  	 *
  	 * -ENODATA: no dirty blocks available
  	 */
20f6814b9   Joe Thornber   dm cache: pass a ...
187
188
  	int (*writeback_work)(struct dm_cache_policy *p, dm_oblock_t *oblock, dm_cblock_t *cblock,
  			      bool critical_only);
c6b4fcbad   Joe Thornber   dm: add cache target
189
190
191
192
193
194
195
196
197
198
  
  	/*
  	 * How full is the cache?
  	 */
  	dm_cblock_t (*residency)(struct dm_cache_policy *p);
  
  	/*
  	 * Because of where we sit in the block layer, we can be asked to
  	 * map a lot of little bios that are all in the same block (no
  	 * queue merging has occurred).  To stop the policy being fooled by
fba10109a   Joe Thornber   dm cache: age and...
199
  	 * these, the core target sends regular tick() calls to the policy.
c6b4fcbad   Joe Thornber   dm: add cache target
200
201
  	 * The policy should only count an entry as hit once per tick.
  	 */
fba10109a   Joe Thornber   dm cache: age and...
202
  	void (*tick)(struct dm_cache_policy *p, bool can_block);
c6b4fcbad   Joe Thornber   dm: add cache target
203
204
205
206
  
  	/*
  	 * Configuration.
  	 */
028ae9f76   Joe Thornber   dm cache: add fai...
207
208
  	int (*emit_config_values)(struct dm_cache_policy *p, char *result,
  				  unsigned maxlen, ssize_t *sz_ptr);
c6b4fcbad   Joe Thornber   dm: add cache target
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  	int (*set_config_value)(struct dm_cache_policy *p,
  				const char *key, const char *value);
  
  	/*
  	 * Book keeping ptr for the policy register, not for general use.
  	 */
  	void *private;
  };
  
  /*----------------------------------------------------------------*/
  
  /*
   * We maintain a little register of the different policy types.
   */
  #define CACHE_POLICY_NAME_SIZE 16
4e7f506f6   Mike Snitzer   dm cache: policy ...
224
  #define CACHE_POLICY_VERSION_SIZE 3
c6b4fcbad   Joe Thornber   dm: add cache target
225
226
227
228
229
230
231
232
233
234
  
  struct dm_cache_policy_type {
  	/* For use by the register code only. */
  	struct list_head list;
  
  	/*
  	 * Policy writers should fill in these fields.  The name field is
  	 * what gets passed on the target line to select your policy.
  	 */
  	char name[CACHE_POLICY_NAME_SIZE];
4e7f506f6   Mike Snitzer   dm cache: policy ...
235
  	unsigned version[CACHE_POLICY_VERSION_SIZE];
c6b4fcbad   Joe Thornber   dm: add cache target
236
237
  
  	/*
2e68c4e6c   Mike Snitzer   dm cache: add pol...
238
239
240
241
242
243
  	 * For use by an alias dm_cache_policy_type to point to the
  	 * real dm_cache_policy_type.
  	 */
  	struct dm_cache_policy_type *real;
  
  	/*
c6b4fcbad   Joe Thornber   dm: add cache target
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  	 * Policies may store a hint for each each cache block.
  	 * Currently the size of this hint must be 0 or 4 bytes but we
  	 * expect to relax this in future.
  	 */
  	size_t hint_size;
  
  	struct module *owner;
  	struct dm_cache_policy *(*create)(dm_cblock_t cache_size,
  					  sector_t origin_size,
  					  sector_t block_size);
  };
  
  int dm_cache_policy_register(struct dm_cache_policy_type *type);
  void dm_cache_policy_unregister(struct dm_cache_policy_type *type);
  
  /*----------------------------------------------------------------*/
  
  #endif	/* DM_CACHE_POLICY_H */