Blame view

drivers/md/dm-cache-policy.h 5.21 KB
c6b4fcbad   Joe Thornber   dm: add cache target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /*
   * 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>
  
  /*----------------------------------------------------------------*/
c6b4fcbad   Joe Thornber   dm: add cache target
15
16
17
  /*
   * The cache policy makes the important decisions about which blocks get to
   * live on the faster cache device.
c6b4fcbad   Joe Thornber   dm: add cache target
18
19
   */
  enum policy_operation {
b29d4986d   Joe Thornber   dm cache: signifi...
20
21
22
  	POLICY_PROMOTE,
  	POLICY_DEMOTE,
  	POLICY_WRITEBACK
fb4100ae7   Joe Thornber   dm cache: fix rac...
23
24
25
  };
  
  /*
c6b4fcbad   Joe Thornber   dm: add cache target
26
27
   * This is the instruction passed back to the core target.
   */
b29d4986d   Joe Thornber   dm cache: signifi...
28
  struct policy_work {
c6b4fcbad   Joe Thornber   dm: add cache target
29
  	enum policy_operation op;
b29d4986d   Joe Thornber   dm cache: signifi...
30
31
  	dm_oblock_t oblock;
  	dm_cblock_t cblock;
c6b4fcbad   Joe Thornber   dm: add cache target
32
  };
c6b4fcbad   Joe Thornber   dm: add cache target
33
  /*
b29d4986d   Joe Thornber   dm cache: signifi...
34
35
   * The cache policy object.  It is envisaged that this structure will be
   * embedded in a bigger, policy specific structure (ie. use container_of()).
c6b4fcbad   Joe Thornber   dm: add cache target
36
37
   */
  struct dm_cache_policy {
c6b4fcbad   Joe Thornber   dm: add cache target
38
39
40
41
42
43
  	/*
  	 * Destroys this object.
  	 */
  	void (*destroy)(struct dm_cache_policy *p);
  
  	/*
b29d4986d   Joe Thornber   dm cache: signifi...
44
  	 * Find the location of a block.
c6b4fcbad   Joe Thornber   dm: add cache target
45
  	 *
b29d4986d   Joe Thornber   dm cache: signifi...
46
  	 * Must not block.
c6b4fcbad   Joe Thornber   dm: add cache target
47
  	 *
b29d4986d   Joe Thornber   dm cache: signifi...
48
49
50
51
52
  	 * Returns 0 if in cache (cblock will be set), -ENOENT if not, < 0 for
  	 * other errors (-EWOULDBLOCK would be typical).  data_dir should be
  	 * READ or WRITE. fast_copy should be set if migrating this block would
  	 * be 'cheap' somehow (eg, discarded data). background_queued will be set
  	 * if a migration has just been queued.
c6b4fcbad   Joe Thornber   dm: add cache target
53
  	 */
b29d4986d   Joe Thornber   dm cache: signifi...
54
55
  	int (*lookup)(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock,
  		      int data_dir, bool fast_copy, bool *background_queued);
c6b4fcbad   Joe Thornber   dm: add cache target
56
57
  
  	/*
b29d4986d   Joe Thornber   dm cache: signifi...
58
59
60
61
  	 * Sometimes the core target can optimise a migration, eg, the
  	 * block may be discarded, or the bio may cover an entire block.
  	 * In order to optimise it needs the migration immediately though
  	 * so it knows to do something different with the bio.
c6b4fcbad   Joe Thornber   dm: add cache target
62
  	 *
b29d4986d   Joe Thornber   dm cache: signifi...
63
64
  	 * This method is optional (policy-internal will fallback to using
  	 * lookup).
c6b4fcbad   Joe Thornber   dm: add cache target
65
  	 */
b29d4986d   Joe Thornber   dm cache: signifi...
66
67
68
69
  	int (*lookup_with_work)(struct dm_cache_policy *p,
  				dm_oblock_t oblock, dm_cblock_t *cblock,
  				int data_dir, bool fast_copy,
  				struct policy_work **work);
c6b4fcbad   Joe Thornber   dm: add cache target
70
71
  
  	/*
b29d4986d   Joe Thornber   dm cache: signifi...
72
73
  	 * Retrieves background work.  Returns -ENODATA when there's no
  	 * background work.
c6b4fcbad   Joe Thornber   dm: add cache target
74
  	 */
b29d4986d   Joe Thornber   dm cache: signifi...
75
76
  	int (*get_background_work)(struct dm_cache_policy *p, bool idle,
  			           struct policy_work **result);
c6b4fcbad   Joe Thornber   dm: add cache target
77

4e781b498   Joe Thornber   dm cache: speed u...
78
  	/*
b29d4986d   Joe Thornber   dm cache: signifi...
79
80
  	 * You must pass in the same work pointer that you were given, not
  	 * a copy.
4e781b498   Joe Thornber   dm cache: speed u...
81
  	 */
b29d4986d   Joe Thornber   dm cache: signifi...
82
83
84
85
86
87
  	void (*complete_background_work)(struct dm_cache_policy *p,
  					 struct policy_work *work,
  					 bool success);
  
  	void (*set_dirty)(struct dm_cache_policy *p, dm_cblock_t cblock);
  	void (*clear_dirty)(struct dm_cache_policy *p, dm_cblock_t cblock);
c6b4fcbad   Joe Thornber   dm: add cache target
88
89
  
  	/*
b29d4986d   Joe Thornber   dm cache: signifi...
90
91
  	 * Called when a cache target is first created.  Used to load a
  	 * mapping from the metadata device into the policy.
c6b4fcbad   Joe Thornber   dm: add cache target
92
  	 */
b29d4986d   Joe Thornber   dm cache: signifi...
93
94
95
  	int (*load_mapping)(struct dm_cache_policy *p, dm_oblock_t oblock,
  			    dm_cblock_t cblock, bool dirty,
  			    uint32_t hint, bool hint_valid);
c6b4fcbad   Joe Thornber   dm: add cache target
96

532906aa7   Joe Thornber   dm cache: add rem...
97
  	/*
b29d4986d   Joe Thornber   dm cache: signifi...
98
99
  	 * Drops the mapping, irrespective of whether it's clean or dirty.
  	 * Returns -ENODATA if cblock is not mapped.
532906aa7   Joe Thornber   dm cache: add rem...
100
  	 */
b29d4986d   Joe Thornber   dm cache: signifi...
101
  	int (*invalidate_mapping)(struct dm_cache_policy *p, dm_cblock_t cblock);
c6b4fcbad   Joe Thornber   dm: add cache target
102

532906aa7   Joe Thornber   dm cache: add rem...
103
  	/*
b29d4986d   Joe Thornber   dm cache: signifi...
104
105
  	 * Gets the hint for a given cblock.  Called in a single threaded
  	 * context.  So no locking required.
532906aa7   Joe Thornber   dm cache: add rem...
106
  	 */
b29d4986d   Joe Thornber   dm cache: signifi...
107
  	uint32_t (*get_hint)(struct dm_cache_policy *p, dm_cblock_t cblock);
c6b4fcbad   Joe Thornber   dm: add cache target
108
109
110
111
112
113
114
115
116
117
  
  	/*
  	 * 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...
118
  	 * these, the core target sends regular tick() calls to the policy.
c6b4fcbad   Joe Thornber   dm: add cache target
119
  	 * The policy should only count an entry as hit once per tick.
b29d4986d   Joe Thornber   dm cache: signifi...
120
121
  	 *
  	 * This method is optional.
c6b4fcbad   Joe Thornber   dm: add cache target
122
  	 */
fba10109a   Joe Thornber   dm cache: age and...
123
  	void (*tick)(struct dm_cache_policy *p, bool can_block);
c6b4fcbad   Joe Thornber   dm: add cache target
124
125
126
127
  
  	/*
  	 * Configuration.
  	 */
028ae9f76   Joe Thornber   dm cache: add fai...
128
129
  	int (*emit_config_values)(struct dm_cache_policy *p, char *result,
  				  unsigned maxlen, ssize_t *sz_ptr);
c6b4fcbad   Joe Thornber   dm: add cache target
130
131
  	int (*set_config_value)(struct dm_cache_policy *p,
  				const char *key, const char *value);
b29d4986d   Joe Thornber   dm cache: signifi...
132
  	void (*allow_migrations)(struct dm_cache_policy *p, bool allow);
c6b4fcbad   Joe Thornber   dm: add cache target
133
134
135
136
137
138
139
140
141
142
143
144
  	/*
  	 * 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 ...
145
  #define CACHE_POLICY_VERSION_SIZE 3
c6b4fcbad   Joe Thornber   dm: add cache target
146
147
148
149
150
151
152
153
154
155
  
  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 ...
156
  	unsigned version[CACHE_POLICY_VERSION_SIZE];
c6b4fcbad   Joe Thornber   dm: add cache target
157
158
  
  	/*
2e68c4e6c   Mike Snitzer   dm cache: add pol...
159
160
161
162
163
164
  	 * 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  	 * 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 */