Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Red Hat. All rights reserved. |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #include "dm.h" |
| 8 | #include "dm-bio-prison.h" |
Darrick J. Wong | b844fe6 | 2013-04-05 15:36:32 +0100 | [diff] [blame] | 9 | #include "dm-bio-record.h" |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 10 | #include "dm-cache-metadata.h" |
| 11 | |
| 12 | #include <linux/dm-io.h> |
| 13 | #include <linux/dm-kcopyd.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/mempool.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/vmalloc.h> |
| 19 | |
| 20 | #define DM_MSG_PREFIX "cache" |
| 21 | |
| 22 | DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle, |
| 23 | "A percentage of time allocated for copying to and/or from cache"); |
| 24 | |
| 25 | /*----------------------------------------------------------------*/ |
| 26 | |
| 27 | /* |
| 28 | * Glossary: |
| 29 | * |
| 30 | * oblock: index of an origin block |
| 31 | * cblock: index of a cache block |
| 32 | * promotion: movement of a block from origin to cache |
| 33 | * demotion: movement of a block from cache to origin |
| 34 | * migration: movement of a block between the origin and cache device, |
| 35 | * either direction |
| 36 | */ |
| 37 | |
| 38 | /*----------------------------------------------------------------*/ |
| 39 | |
| 40 | static size_t bitset_size_in_bytes(unsigned nr_entries) |
| 41 | { |
| 42 | return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG); |
| 43 | } |
| 44 | |
| 45 | static unsigned long *alloc_bitset(unsigned nr_entries) |
| 46 | { |
| 47 | size_t s = bitset_size_in_bytes(nr_entries); |
| 48 | return vzalloc(s); |
| 49 | } |
| 50 | |
| 51 | static void clear_bitset(void *bitset, unsigned nr_entries) |
| 52 | { |
| 53 | size_t s = bitset_size_in_bytes(nr_entries); |
| 54 | memset(bitset, 0, s); |
| 55 | } |
| 56 | |
| 57 | static void free_bitset(unsigned long *bits) |
| 58 | { |
| 59 | vfree(bits); |
| 60 | } |
| 61 | |
| 62 | /*----------------------------------------------------------------*/ |
| 63 | |
| 64 | #define PRISON_CELLS 1024 |
| 65 | #define MIGRATION_POOL_SIZE 128 |
| 66 | #define COMMIT_PERIOD HZ |
| 67 | #define MIGRATION_COUNT_WINDOW 10 |
| 68 | |
| 69 | /* |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 70 | * The block size of the device holding cache data must be |
| 71 | * between 32KB and 1GB. |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 72 | */ |
| 73 | #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT) |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 74 | #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * FIXME: the cache is read/write for the time being. |
| 78 | */ |
| 79 | enum cache_mode { |
| 80 | CM_WRITE, /* metadata may be changed */ |
| 81 | CM_READ_ONLY, /* metadata may not be changed */ |
| 82 | }; |
| 83 | |
| 84 | struct cache_features { |
| 85 | enum cache_mode mode; |
| 86 | bool write_through:1; |
| 87 | }; |
| 88 | |
| 89 | struct cache_stats { |
| 90 | atomic_t read_hit; |
| 91 | atomic_t read_miss; |
| 92 | atomic_t write_hit; |
| 93 | atomic_t write_miss; |
| 94 | atomic_t demotion; |
| 95 | atomic_t promotion; |
| 96 | atomic_t copies_avoided; |
| 97 | atomic_t cache_cell_clash; |
| 98 | atomic_t commit_count; |
| 99 | atomic_t discard_count; |
| 100 | }; |
| 101 | |
| 102 | struct cache { |
| 103 | struct dm_target *ti; |
| 104 | struct dm_target_callbacks callbacks; |
| 105 | |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 106 | struct dm_cache_metadata *cmd; |
| 107 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 108 | /* |
| 109 | * Metadata is written to this device. |
| 110 | */ |
| 111 | struct dm_dev *metadata_dev; |
| 112 | |
| 113 | /* |
| 114 | * The slower of the two data devices. Typically a spindle. |
| 115 | */ |
| 116 | struct dm_dev *origin_dev; |
| 117 | |
| 118 | /* |
| 119 | * The faster of the two data devices. Typically an SSD. |
| 120 | */ |
| 121 | struct dm_dev *cache_dev; |
| 122 | |
| 123 | /* |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 124 | * Size of the origin device in _complete_ blocks and native sectors. |
| 125 | */ |
| 126 | dm_oblock_t origin_blocks; |
| 127 | sector_t origin_sectors; |
| 128 | |
| 129 | /* |
| 130 | * Size of the cache device in blocks. |
| 131 | */ |
| 132 | dm_cblock_t cache_size; |
| 133 | |
| 134 | /* |
| 135 | * Fields for converting from sectors to blocks. |
| 136 | */ |
| 137 | uint32_t sectors_per_block; |
| 138 | int sectors_per_block_shift; |
| 139 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 140 | spinlock_t lock; |
| 141 | struct bio_list deferred_bios; |
| 142 | struct bio_list deferred_flush_bios; |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 143 | struct bio_list deferred_writethrough_bios; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 144 | struct list_head quiesced_migrations; |
| 145 | struct list_head completed_migrations; |
| 146 | struct list_head need_commit_migrations; |
| 147 | sector_t migration_threshold; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 148 | wait_queue_head_t migration_wait; |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 149 | atomic_t nr_migrations; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 150 | |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 151 | wait_queue_head_t quiescing_wait; |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 152 | atomic_t quiescing; |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 153 | atomic_t quiescing_ack; |
| 154 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 155 | /* |
| 156 | * cache_size entries, dirty if set |
| 157 | */ |
| 158 | dm_cblock_t nr_dirty; |
| 159 | unsigned long *dirty_bitset; |
| 160 | |
| 161 | /* |
| 162 | * origin_blocks entries, discarded if set. |
| 163 | */ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 164 | dm_dblock_t discard_nr_blocks; |
| 165 | unsigned long *discard_bitset; |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 166 | uint32_t discard_block_size; /* a power of 2 times sectors per block */ |
| 167 | |
| 168 | /* |
| 169 | * Rather than reconstructing the table line for the status we just |
| 170 | * save it and regurgitate. |
| 171 | */ |
| 172 | unsigned nr_ctr_args; |
| 173 | const char **ctr_args; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 174 | |
| 175 | struct dm_kcopyd_client *copier; |
| 176 | struct workqueue_struct *wq; |
| 177 | struct work_struct worker; |
| 178 | |
| 179 | struct delayed_work waker; |
| 180 | unsigned long last_commit_jiffies; |
| 181 | |
| 182 | struct dm_bio_prison *prison; |
| 183 | struct dm_deferred_set *all_io_ds; |
| 184 | |
| 185 | mempool_t *migration_pool; |
| 186 | struct dm_cache_migration *next_migration; |
| 187 | |
| 188 | struct dm_cache_policy *policy; |
| 189 | unsigned policy_nr_args; |
| 190 | |
| 191 | bool need_tick_bio:1; |
| 192 | bool sized:1; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 193 | bool commit_requested:1; |
| 194 | bool loaded_mappings:1; |
| 195 | bool loaded_discards:1; |
| 196 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 197 | /* |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 198 | * Cache features such as write-through. |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 199 | */ |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 200 | struct cache_features features; |
| 201 | |
| 202 | struct cache_stats stats; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | struct per_bio_data { |
| 206 | bool tick:1; |
| 207 | unsigned req_nr:2; |
| 208 | struct dm_deferred_entry *all_io_entry; |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 209 | |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 210 | /* |
| 211 | * writethrough fields. These MUST remain at the end of this |
| 212 | * structure and the 'cache' member must be the first as it |
Joe Thornber | aeed1420 | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 213 | * is used to determine the offset of the writethrough fields. |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 214 | */ |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 215 | struct cache *cache; |
| 216 | dm_cblock_t cblock; |
| 217 | bio_end_io_t *saved_bi_end_io; |
Darrick J. Wong | b844fe6 | 2013-04-05 15:36:32 +0100 | [diff] [blame] | 218 | struct dm_bio_details bio_details; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | struct dm_cache_migration { |
| 222 | struct list_head list; |
| 223 | struct cache *cache; |
| 224 | |
| 225 | unsigned long start_jiffies; |
| 226 | dm_oblock_t old_oblock; |
| 227 | dm_oblock_t new_oblock; |
| 228 | dm_cblock_t cblock; |
| 229 | |
| 230 | bool err:1; |
| 231 | bool writeback:1; |
| 232 | bool demote:1; |
| 233 | bool promote:1; |
| 234 | |
| 235 | struct dm_bio_prison_cell *old_ocell; |
| 236 | struct dm_bio_prison_cell *new_ocell; |
| 237 | }; |
| 238 | |
| 239 | /* |
| 240 | * Processing a bio in the worker thread may require these memory |
| 241 | * allocations. We prealloc to avoid deadlocks (the same worker thread |
| 242 | * frees them back to the mempool). |
| 243 | */ |
| 244 | struct prealloc { |
| 245 | struct dm_cache_migration *mg; |
| 246 | struct dm_bio_prison_cell *cell1; |
| 247 | struct dm_bio_prison_cell *cell2; |
| 248 | }; |
| 249 | |
| 250 | static void wake_worker(struct cache *cache) |
| 251 | { |
| 252 | queue_work(cache->wq, &cache->worker); |
| 253 | } |
| 254 | |
| 255 | /*----------------------------------------------------------------*/ |
| 256 | |
| 257 | static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache) |
| 258 | { |
| 259 | /* FIXME: change to use a local slab. */ |
| 260 | return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT); |
| 261 | } |
| 262 | |
| 263 | static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell) |
| 264 | { |
| 265 | dm_bio_prison_free_cell(cache->prison, cell); |
| 266 | } |
| 267 | |
| 268 | static int prealloc_data_structs(struct cache *cache, struct prealloc *p) |
| 269 | { |
| 270 | if (!p->mg) { |
| 271 | p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT); |
| 272 | if (!p->mg) |
| 273 | return -ENOMEM; |
| 274 | } |
| 275 | |
| 276 | if (!p->cell1) { |
| 277 | p->cell1 = alloc_prison_cell(cache); |
| 278 | if (!p->cell1) |
| 279 | return -ENOMEM; |
| 280 | } |
| 281 | |
| 282 | if (!p->cell2) { |
| 283 | p->cell2 = alloc_prison_cell(cache); |
| 284 | if (!p->cell2) |
| 285 | return -ENOMEM; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static void prealloc_free_structs(struct cache *cache, struct prealloc *p) |
| 292 | { |
| 293 | if (p->cell2) |
| 294 | free_prison_cell(cache, p->cell2); |
| 295 | |
| 296 | if (p->cell1) |
| 297 | free_prison_cell(cache, p->cell1); |
| 298 | |
| 299 | if (p->mg) |
| 300 | mempool_free(p->mg, cache->migration_pool); |
| 301 | } |
| 302 | |
| 303 | static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p) |
| 304 | { |
| 305 | struct dm_cache_migration *mg = p->mg; |
| 306 | |
| 307 | BUG_ON(!mg); |
| 308 | p->mg = NULL; |
| 309 | |
| 310 | return mg; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * You must have a cell within the prealloc struct to return. If not this |
| 315 | * function will BUG() rather than returning NULL. |
| 316 | */ |
| 317 | static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p) |
| 318 | { |
| 319 | struct dm_bio_prison_cell *r = NULL; |
| 320 | |
| 321 | if (p->cell1) { |
| 322 | r = p->cell1; |
| 323 | p->cell1 = NULL; |
| 324 | |
| 325 | } else if (p->cell2) { |
| 326 | r = p->cell2; |
| 327 | p->cell2 = NULL; |
| 328 | } else |
| 329 | BUG(); |
| 330 | |
| 331 | return r; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * You can't have more than two cells in a prealloc struct. BUG() will be |
| 336 | * called if you try and overfill. |
| 337 | */ |
| 338 | static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell) |
| 339 | { |
| 340 | if (!p->cell2) |
| 341 | p->cell2 = cell; |
| 342 | |
| 343 | else if (!p->cell1) |
| 344 | p->cell1 = cell; |
| 345 | |
| 346 | else |
| 347 | BUG(); |
| 348 | } |
| 349 | |
| 350 | /*----------------------------------------------------------------*/ |
| 351 | |
| 352 | static void build_key(dm_oblock_t oblock, struct dm_cell_key *key) |
| 353 | { |
| 354 | key->virtual = 0; |
| 355 | key->dev = 0; |
| 356 | key->block = from_oblock(oblock); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * The caller hands in a preallocated cell, and a free function for it. |
| 361 | * The cell will be freed if there's an error, or if it wasn't used because |
| 362 | * a cell with that key already exists. |
| 363 | */ |
| 364 | typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell); |
| 365 | |
| 366 | static int bio_detain(struct cache *cache, dm_oblock_t oblock, |
| 367 | struct bio *bio, struct dm_bio_prison_cell *cell_prealloc, |
| 368 | cell_free_fn free_fn, void *free_context, |
| 369 | struct dm_bio_prison_cell **cell_result) |
| 370 | { |
| 371 | int r; |
| 372 | struct dm_cell_key key; |
| 373 | |
| 374 | build_key(oblock, &key); |
| 375 | r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result); |
| 376 | if (r) |
| 377 | free_fn(free_context, cell_prealloc); |
| 378 | |
| 379 | return r; |
| 380 | } |
| 381 | |
| 382 | static int get_cell(struct cache *cache, |
| 383 | dm_oblock_t oblock, |
| 384 | struct prealloc *structs, |
| 385 | struct dm_bio_prison_cell **cell_result) |
| 386 | { |
| 387 | int r; |
| 388 | struct dm_cell_key key; |
| 389 | struct dm_bio_prison_cell *cell_prealloc; |
| 390 | |
| 391 | cell_prealloc = prealloc_get_cell(structs); |
| 392 | |
| 393 | build_key(oblock, &key); |
| 394 | r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result); |
| 395 | if (r) |
| 396 | prealloc_put_cell(structs, cell_prealloc); |
| 397 | |
| 398 | return r; |
| 399 | } |
| 400 | |
Joe Thornber | aeed1420 | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 401 | /*----------------------------------------------------------------*/ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 402 | |
| 403 | static bool is_dirty(struct cache *cache, dm_cblock_t b) |
| 404 | { |
| 405 | return test_bit(from_cblock(b), cache->dirty_bitset); |
| 406 | } |
| 407 | |
| 408 | static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock) |
| 409 | { |
| 410 | if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) { |
| 411 | cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) + 1); |
| 412 | policy_set_dirty(cache->policy, oblock); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock) |
| 417 | { |
| 418 | if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) { |
| 419 | policy_clear_dirty(cache->policy, oblock); |
| 420 | cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) - 1); |
| 421 | if (!from_cblock(cache->nr_dirty)) |
| 422 | dm_table_event(cache->ti->table); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /*----------------------------------------------------------------*/ |
Joe Thornber | aeed1420 | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 427 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 428 | static bool block_size_is_power_of_two(struct cache *cache) |
| 429 | { |
| 430 | return cache->sectors_per_block_shift >= 0; |
| 431 | } |
| 432 | |
Mikulas Patocka | 43aeaa2 | 2013-07-10 23:41:17 +0100 | [diff] [blame] | 433 | /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */ |
| 434 | #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6 |
| 435 | __always_inline |
| 436 | #endif |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 437 | static dm_block_t block_div(dm_block_t b, uint32_t n) |
| 438 | { |
| 439 | do_div(b, n); |
| 440 | |
| 441 | return b; |
| 442 | } |
| 443 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 444 | static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock) |
| 445 | { |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 446 | uint32_t discard_blocks = cache->discard_block_size; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 447 | dm_block_t b = from_oblock(oblock); |
| 448 | |
| 449 | if (!block_size_is_power_of_two(cache)) |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 450 | discard_blocks = discard_blocks / cache->sectors_per_block; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 451 | else |
| 452 | discard_blocks >>= cache->sectors_per_block_shift; |
| 453 | |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 454 | b = block_div(b, discard_blocks); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 455 | |
| 456 | return to_dblock(b); |
| 457 | } |
| 458 | |
| 459 | static void set_discard(struct cache *cache, dm_dblock_t b) |
| 460 | { |
| 461 | unsigned long flags; |
| 462 | |
| 463 | atomic_inc(&cache->stats.discard_count); |
| 464 | |
| 465 | spin_lock_irqsave(&cache->lock, flags); |
| 466 | set_bit(from_dblock(b), cache->discard_bitset); |
| 467 | spin_unlock_irqrestore(&cache->lock, flags); |
| 468 | } |
| 469 | |
| 470 | static void clear_discard(struct cache *cache, dm_dblock_t b) |
| 471 | { |
| 472 | unsigned long flags; |
| 473 | |
| 474 | spin_lock_irqsave(&cache->lock, flags); |
| 475 | clear_bit(from_dblock(b), cache->discard_bitset); |
| 476 | spin_unlock_irqrestore(&cache->lock, flags); |
| 477 | } |
| 478 | |
| 479 | static bool is_discarded(struct cache *cache, dm_dblock_t b) |
| 480 | { |
| 481 | int r; |
| 482 | unsigned long flags; |
| 483 | |
| 484 | spin_lock_irqsave(&cache->lock, flags); |
| 485 | r = test_bit(from_dblock(b), cache->discard_bitset); |
| 486 | spin_unlock_irqrestore(&cache->lock, flags); |
| 487 | |
| 488 | return r; |
| 489 | } |
| 490 | |
| 491 | static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b) |
| 492 | { |
| 493 | int r; |
| 494 | unsigned long flags; |
| 495 | |
| 496 | spin_lock_irqsave(&cache->lock, flags); |
| 497 | r = test_bit(from_dblock(oblock_to_dblock(cache, b)), |
| 498 | cache->discard_bitset); |
| 499 | spin_unlock_irqrestore(&cache->lock, flags); |
| 500 | |
| 501 | return r; |
| 502 | } |
| 503 | |
| 504 | /*----------------------------------------------------------------*/ |
| 505 | |
| 506 | static void load_stats(struct cache *cache) |
| 507 | { |
| 508 | struct dm_cache_statistics stats; |
| 509 | |
| 510 | dm_cache_metadata_get_stats(cache->cmd, &stats); |
| 511 | atomic_set(&cache->stats.read_hit, stats.read_hits); |
| 512 | atomic_set(&cache->stats.read_miss, stats.read_misses); |
| 513 | atomic_set(&cache->stats.write_hit, stats.write_hits); |
| 514 | atomic_set(&cache->stats.write_miss, stats.write_misses); |
| 515 | } |
| 516 | |
| 517 | static void save_stats(struct cache *cache) |
| 518 | { |
| 519 | struct dm_cache_statistics stats; |
| 520 | |
| 521 | stats.read_hits = atomic_read(&cache->stats.read_hit); |
| 522 | stats.read_misses = atomic_read(&cache->stats.read_miss); |
| 523 | stats.write_hits = atomic_read(&cache->stats.write_hit); |
| 524 | stats.write_misses = atomic_read(&cache->stats.write_miss); |
| 525 | |
| 526 | dm_cache_metadata_set_stats(cache->cmd, &stats); |
| 527 | } |
| 528 | |
| 529 | /*---------------------------------------------------------------- |
| 530 | * Per bio data |
| 531 | *--------------------------------------------------------------*/ |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 532 | |
| 533 | /* |
| 534 | * If using writeback, leave out struct per_bio_data's writethrough fields. |
| 535 | */ |
| 536 | #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache)) |
| 537 | #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data)) |
| 538 | |
| 539 | static size_t get_per_bio_data_size(struct cache *cache) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 540 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 541 | return cache->features.write_through ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB; |
| 542 | } |
| 543 | |
| 544 | static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size) |
| 545 | { |
| 546 | struct per_bio_data *pb = dm_per_bio_data(bio, data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 547 | BUG_ON(!pb); |
| 548 | return pb; |
| 549 | } |
| 550 | |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 551 | static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 552 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 553 | struct per_bio_data *pb = get_per_bio_data(bio, data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 554 | |
| 555 | pb->tick = false; |
| 556 | pb->req_nr = dm_bio_get_target_bio_nr(bio); |
| 557 | pb->all_io_entry = NULL; |
| 558 | |
| 559 | return pb; |
| 560 | } |
| 561 | |
| 562 | /*---------------------------------------------------------------- |
| 563 | * Remapping |
| 564 | *--------------------------------------------------------------*/ |
| 565 | static void remap_to_origin(struct cache *cache, struct bio *bio) |
| 566 | { |
| 567 | bio->bi_bdev = cache->origin_dev->bdev; |
| 568 | } |
| 569 | |
| 570 | static void remap_to_cache(struct cache *cache, struct bio *bio, |
| 571 | dm_cblock_t cblock) |
| 572 | { |
| 573 | sector_t bi_sector = bio->bi_sector; |
| 574 | |
| 575 | bio->bi_bdev = cache->cache_dev->bdev; |
| 576 | if (!block_size_is_power_of_two(cache)) |
| 577 | bio->bi_sector = (from_cblock(cblock) * cache->sectors_per_block) + |
| 578 | sector_div(bi_sector, cache->sectors_per_block); |
| 579 | else |
| 580 | bio->bi_sector = (from_cblock(cblock) << cache->sectors_per_block_shift) | |
| 581 | (bi_sector & (cache->sectors_per_block - 1)); |
| 582 | } |
| 583 | |
| 584 | static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio) |
| 585 | { |
| 586 | unsigned long flags; |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 587 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 588 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 589 | |
| 590 | spin_lock_irqsave(&cache->lock, flags); |
| 591 | if (cache->need_tick_bio && |
| 592 | !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) { |
| 593 | pb->tick = true; |
| 594 | cache->need_tick_bio = false; |
| 595 | } |
| 596 | spin_unlock_irqrestore(&cache->lock, flags); |
| 597 | } |
| 598 | |
| 599 | static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio, |
| 600 | dm_oblock_t oblock) |
| 601 | { |
| 602 | check_if_tick_bio_needed(cache, bio); |
| 603 | remap_to_origin(cache, bio); |
| 604 | if (bio_data_dir(bio) == WRITE) |
| 605 | clear_discard(cache, oblock_to_dblock(cache, oblock)); |
| 606 | } |
| 607 | |
| 608 | static void remap_to_cache_dirty(struct cache *cache, struct bio *bio, |
| 609 | dm_oblock_t oblock, dm_cblock_t cblock) |
| 610 | { |
Joe Thornber | f8e5f01 | 2013-10-21 12:51:45 +0100 | [diff] [blame] | 611 | check_if_tick_bio_needed(cache, bio); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 612 | remap_to_cache(cache, bio, cblock); |
| 613 | if (bio_data_dir(bio) == WRITE) { |
| 614 | set_dirty(cache, oblock, cblock); |
| 615 | clear_discard(cache, oblock_to_dblock(cache, oblock)); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio) |
| 620 | { |
| 621 | sector_t block_nr = bio->bi_sector; |
| 622 | |
| 623 | if (!block_size_is_power_of_two(cache)) |
| 624 | (void) sector_div(block_nr, cache->sectors_per_block); |
| 625 | else |
| 626 | block_nr >>= cache->sectors_per_block_shift; |
| 627 | |
| 628 | return to_oblock(block_nr); |
| 629 | } |
| 630 | |
| 631 | static int bio_triggers_commit(struct cache *cache, struct bio *bio) |
| 632 | { |
| 633 | return bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 634 | } |
| 635 | |
| 636 | static void issue(struct cache *cache, struct bio *bio) |
| 637 | { |
| 638 | unsigned long flags; |
| 639 | |
| 640 | if (!bio_triggers_commit(cache, bio)) { |
| 641 | generic_make_request(bio); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Batch together any bios that trigger commits and then issue a |
| 647 | * single commit for them in do_worker(). |
| 648 | */ |
| 649 | spin_lock_irqsave(&cache->lock, flags); |
| 650 | cache->commit_requested = true; |
| 651 | bio_list_add(&cache->deferred_flush_bios, bio); |
| 652 | spin_unlock_irqrestore(&cache->lock, flags); |
| 653 | } |
| 654 | |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 655 | static void defer_writethrough_bio(struct cache *cache, struct bio *bio) |
| 656 | { |
| 657 | unsigned long flags; |
| 658 | |
| 659 | spin_lock_irqsave(&cache->lock, flags); |
| 660 | bio_list_add(&cache->deferred_writethrough_bios, bio); |
| 661 | spin_unlock_irqrestore(&cache->lock, flags); |
| 662 | |
| 663 | wake_worker(cache); |
| 664 | } |
| 665 | |
| 666 | static void writethrough_endio(struct bio *bio, int err) |
| 667 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 668 | struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 669 | bio->bi_end_io = pb->saved_bi_end_io; |
| 670 | |
| 671 | if (err) { |
| 672 | bio_endio(bio, err); |
| 673 | return; |
| 674 | } |
| 675 | |
Darrick J. Wong | b844fe6 | 2013-04-05 15:36:32 +0100 | [diff] [blame] | 676 | dm_bio_restore(&pb->bio_details, bio); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 677 | remap_to_cache(pb->cache, bio, pb->cblock); |
| 678 | |
| 679 | /* |
| 680 | * We can't issue this bio directly, since we're in interrupt |
Joe Thornber | aeed1420 | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 681 | * context. So it gets put on a bio list for processing by the |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 682 | * worker thread. |
| 683 | */ |
| 684 | defer_writethrough_bio(pb->cache, bio); |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * When running in writethrough mode we need to send writes to clean blocks |
| 689 | * to both the cache and origin devices. In future we'd like to clone the |
| 690 | * bio and send them in parallel, but for now we're doing them in |
| 691 | * series as this is easier. |
| 692 | */ |
| 693 | static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio, |
| 694 | dm_oblock_t oblock, dm_cblock_t cblock) |
| 695 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 696 | struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 697 | |
| 698 | pb->cache = cache; |
| 699 | pb->cblock = cblock; |
| 700 | pb->saved_bi_end_io = bio->bi_end_io; |
Darrick J. Wong | b844fe6 | 2013-04-05 15:36:32 +0100 | [diff] [blame] | 701 | dm_bio_record(&pb->bio_details, bio); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 702 | bio->bi_end_io = writethrough_endio; |
| 703 | |
| 704 | remap_to_origin_clear_discard(pb->cache, bio, oblock); |
| 705 | } |
| 706 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 707 | /*---------------------------------------------------------------- |
| 708 | * Migration processing |
| 709 | * |
| 710 | * Migration covers moving data from the origin device to the cache, or |
| 711 | * vice versa. |
| 712 | *--------------------------------------------------------------*/ |
| 713 | static void free_migration(struct dm_cache_migration *mg) |
| 714 | { |
| 715 | mempool_free(mg, mg->cache->migration_pool); |
| 716 | } |
| 717 | |
| 718 | static void inc_nr_migrations(struct cache *cache) |
| 719 | { |
| 720 | atomic_inc(&cache->nr_migrations); |
| 721 | } |
| 722 | |
| 723 | static void dec_nr_migrations(struct cache *cache) |
| 724 | { |
| 725 | atomic_dec(&cache->nr_migrations); |
| 726 | |
| 727 | /* |
| 728 | * Wake the worker in case we're suspending the target. |
| 729 | */ |
| 730 | wake_up(&cache->migration_wait); |
| 731 | } |
| 732 | |
| 733 | static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, |
| 734 | bool holder) |
| 735 | { |
| 736 | (holder ? dm_cell_release : dm_cell_release_no_holder) |
| 737 | (cache->prison, cell, &cache->deferred_bios); |
| 738 | free_prison_cell(cache, cell); |
| 739 | } |
| 740 | |
| 741 | static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, |
| 742 | bool holder) |
| 743 | { |
| 744 | unsigned long flags; |
| 745 | |
| 746 | spin_lock_irqsave(&cache->lock, flags); |
| 747 | __cell_defer(cache, cell, holder); |
| 748 | spin_unlock_irqrestore(&cache->lock, flags); |
| 749 | |
| 750 | wake_worker(cache); |
| 751 | } |
| 752 | |
| 753 | static void cleanup_migration(struct dm_cache_migration *mg) |
| 754 | { |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 755 | struct cache *cache = mg->cache; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 756 | free_migration(mg); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 757 | dec_nr_migrations(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | static void migration_failure(struct dm_cache_migration *mg) |
| 761 | { |
| 762 | struct cache *cache = mg->cache; |
| 763 | |
| 764 | if (mg->writeback) { |
| 765 | DMWARN_LIMIT("writeback failed; couldn't copy block"); |
| 766 | set_dirty(cache, mg->old_oblock, mg->cblock); |
| 767 | cell_defer(cache, mg->old_ocell, false); |
| 768 | |
| 769 | } else if (mg->demote) { |
| 770 | DMWARN_LIMIT("demotion failed; couldn't copy block"); |
| 771 | policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock); |
| 772 | |
Heinz Mauelshagen | 80f659f | 2013-10-14 17:10:47 +0200 | [diff] [blame] | 773 | cell_defer(cache, mg->old_ocell, mg->promote ? false : true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 774 | if (mg->promote) |
Heinz Mauelshagen | 80f659f | 2013-10-14 17:10:47 +0200 | [diff] [blame] | 775 | cell_defer(cache, mg->new_ocell, true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 776 | } else { |
| 777 | DMWARN_LIMIT("promotion failed; couldn't copy block"); |
| 778 | policy_remove_mapping(cache->policy, mg->new_oblock); |
Heinz Mauelshagen | 80f659f | 2013-10-14 17:10:47 +0200 | [diff] [blame] | 779 | cell_defer(cache, mg->new_ocell, true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | cleanup_migration(mg); |
| 783 | } |
| 784 | |
| 785 | static void migration_success_pre_commit(struct dm_cache_migration *mg) |
| 786 | { |
| 787 | unsigned long flags; |
| 788 | struct cache *cache = mg->cache; |
| 789 | |
| 790 | if (mg->writeback) { |
| 791 | cell_defer(cache, mg->old_ocell, false); |
| 792 | clear_dirty(cache, mg->old_oblock, mg->cblock); |
| 793 | cleanup_migration(mg); |
| 794 | return; |
| 795 | |
| 796 | } else if (mg->demote) { |
| 797 | if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) { |
| 798 | DMWARN_LIMIT("demotion failed; couldn't update on disk metadata"); |
| 799 | policy_force_mapping(cache->policy, mg->new_oblock, |
| 800 | mg->old_oblock); |
| 801 | if (mg->promote) |
| 802 | cell_defer(cache, mg->new_ocell, true); |
| 803 | cleanup_migration(mg); |
| 804 | return; |
| 805 | } |
| 806 | } else { |
| 807 | if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) { |
| 808 | DMWARN_LIMIT("promotion failed; couldn't update on disk metadata"); |
| 809 | policy_remove_mapping(cache->policy, mg->new_oblock); |
| 810 | cleanup_migration(mg); |
| 811 | return; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | spin_lock_irqsave(&cache->lock, flags); |
| 816 | list_add_tail(&mg->list, &cache->need_commit_migrations); |
| 817 | cache->commit_requested = true; |
| 818 | spin_unlock_irqrestore(&cache->lock, flags); |
| 819 | } |
| 820 | |
| 821 | static void migration_success_post_commit(struct dm_cache_migration *mg) |
| 822 | { |
| 823 | unsigned long flags; |
| 824 | struct cache *cache = mg->cache; |
| 825 | |
| 826 | if (mg->writeback) { |
| 827 | DMWARN("writeback unexpectedly triggered commit"); |
| 828 | return; |
| 829 | |
| 830 | } else if (mg->demote) { |
Heinz Mauelshagen | 80f659f | 2013-10-14 17:10:47 +0200 | [diff] [blame] | 831 | cell_defer(cache, mg->old_ocell, mg->promote ? false : true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 832 | |
| 833 | if (mg->promote) { |
| 834 | mg->demote = false; |
| 835 | |
| 836 | spin_lock_irqsave(&cache->lock, flags); |
| 837 | list_add_tail(&mg->list, &cache->quiesced_migrations); |
| 838 | spin_unlock_irqrestore(&cache->lock, flags); |
| 839 | |
| 840 | } else |
| 841 | cleanup_migration(mg); |
| 842 | |
| 843 | } else { |
| 844 | cell_defer(cache, mg->new_ocell, true); |
| 845 | clear_dirty(cache, mg->new_oblock, mg->cblock); |
| 846 | cleanup_migration(mg); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | static void copy_complete(int read_err, unsigned long write_err, void *context) |
| 851 | { |
| 852 | unsigned long flags; |
| 853 | struct dm_cache_migration *mg = (struct dm_cache_migration *) context; |
| 854 | struct cache *cache = mg->cache; |
| 855 | |
| 856 | if (read_err || write_err) |
| 857 | mg->err = true; |
| 858 | |
| 859 | spin_lock_irqsave(&cache->lock, flags); |
| 860 | list_add_tail(&mg->list, &cache->completed_migrations); |
| 861 | spin_unlock_irqrestore(&cache->lock, flags); |
| 862 | |
| 863 | wake_worker(cache); |
| 864 | } |
| 865 | |
| 866 | static void issue_copy_real(struct dm_cache_migration *mg) |
| 867 | { |
| 868 | int r; |
| 869 | struct dm_io_region o_region, c_region; |
| 870 | struct cache *cache = mg->cache; |
| 871 | |
| 872 | o_region.bdev = cache->origin_dev->bdev; |
| 873 | o_region.count = cache->sectors_per_block; |
| 874 | |
| 875 | c_region.bdev = cache->cache_dev->bdev; |
| 876 | c_region.sector = from_cblock(mg->cblock) * cache->sectors_per_block; |
| 877 | c_region.count = cache->sectors_per_block; |
| 878 | |
| 879 | if (mg->writeback || mg->demote) { |
| 880 | /* demote */ |
| 881 | o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block; |
| 882 | r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg); |
| 883 | } else { |
| 884 | /* promote */ |
| 885 | o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block; |
| 886 | r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg); |
| 887 | } |
| 888 | |
Heinz Mauelshagen | 2c2263c | 2013-10-14 17:14:45 +0200 | [diff] [blame] | 889 | if (r < 0) { |
| 890 | DMERR_LIMIT("issuing migration failed"); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 891 | migration_failure(mg); |
Heinz Mauelshagen | 2c2263c | 2013-10-14 17:14:45 +0200 | [diff] [blame] | 892 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static void avoid_copy(struct dm_cache_migration *mg) |
| 896 | { |
| 897 | atomic_inc(&mg->cache->stats.copies_avoided); |
| 898 | migration_success_pre_commit(mg); |
| 899 | } |
| 900 | |
| 901 | static void issue_copy(struct dm_cache_migration *mg) |
| 902 | { |
| 903 | bool avoid; |
| 904 | struct cache *cache = mg->cache; |
| 905 | |
| 906 | if (mg->writeback || mg->demote) |
| 907 | avoid = !is_dirty(cache, mg->cblock) || |
| 908 | is_discarded_oblock(cache, mg->old_oblock); |
| 909 | else |
| 910 | avoid = is_discarded_oblock(cache, mg->new_oblock); |
| 911 | |
| 912 | avoid ? avoid_copy(mg) : issue_copy_real(mg); |
| 913 | } |
| 914 | |
| 915 | static void complete_migration(struct dm_cache_migration *mg) |
| 916 | { |
| 917 | if (mg->err) |
| 918 | migration_failure(mg); |
| 919 | else |
| 920 | migration_success_pre_commit(mg); |
| 921 | } |
| 922 | |
| 923 | static void process_migrations(struct cache *cache, struct list_head *head, |
| 924 | void (*fn)(struct dm_cache_migration *)) |
| 925 | { |
| 926 | unsigned long flags; |
| 927 | struct list_head list; |
| 928 | struct dm_cache_migration *mg, *tmp; |
| 929 | |
| 930 | INIT_LIST_HEAD(&list); |
| 931 | spin_lock_irqsave(&cache->lock, flags); |
| 932 | list_splice_init(head, &list); |
| 933 | spin_unlock_irqrestore(&cache->lock, flags); |
| 934 | |
| 935 | list_for_each_entry_safe(mg, tmp, &list, list) |
| 936 | fn(mg); |
| 937 | } |
| 938 | |
| 939 | static void __queue_quiesced_migration(struct dm_cache_migration *mg) |
| 940 | { |
| 941 | list_add_tail(&mg->list, &mg->cache->quiesced_migrations); |
| 942 | } |
| 943 | |
| 944 | static void queue_quiesced_migration(struct dm_cache_migration *mg) |
| 945 | { |
| 946 | unsigned long flags; |
| 947 | struct cache *cache = mg->cache; |
| 948 | |
| 949 | spin_lock_irqsave(&cache->lock, flags); |
| 950 | __queue_quiesced_migration(mg); |
| 951 | spin_unlock_irqrestore(&cache->lock, flags); |
| 952 | |
| 953 | wake_worker(cache); |
| 954 | } |
| 955 | |
| 956 | static void queue_quiesced_migrations(struct cache *cache, struct list_head *work) |
| 957 | { |
| 958 | unsigned long flags; |
| 959 | struct dm_cache_migration *mg, *tmp; |
| 960 | |
| 961 | spin_lock_irqsave(&cache->lock, flags); |
| 962 | list_for_each_entry_safe(mg, tmp, work, list) |
| 963 | __queue_quiesced_migration(mg); |
| 964 | spin_unlock_irqrestore(&cache->lock, flags); |
| 965 | |
| 966 | wake_worker(cache); |
| 967 | } |
| 968 | |
| 969 | static void check_for_quiesced_migrations(struct cache *cache, |
| 970 | struct per_bio_data *pb) |
| 971 | { |
| 972 | struct list_head work; |
| 973 | |
| 974 | if (!pb->all_io_entry) |
| 975 | return; |
| 976 | |
| 977 | INIT_LIST_HEAD(&work); |
| 978 | if (pb->all_io_entry) |
| 979 | dm_deferred_entry_dec(pb->all_io_entry, &work); |
| 980 | |
| 981 | if (!list_empty(&work)) |
| 982 | queue_quiesced_migrations(cache, &work); |
| 983 | } |
| 984 | |
| 985 | static void quiesce_migration(struct dm_cache_migration *mg) |
| 986 | { |
| 987 | if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list)) |
| 988 | queue_quiesced_migration(mg); |
| 989 | } |
| 990 | |
| 991 | static void promote(struct cache *cache, struct prealloc *structs, |
| 992 | dm_oblock_t oblock, dm_cblock_t cblock, |
| 993 | struct dm_bio_prison_cell *cell) |
| 994 | { |
| 995 | struct dm_cache_migration *mg = prealloc_get_migration(structs); |
| 996 | |
| 997 | mg->err = false; |
| 998 | mg->writeback = false; |
| 999 | mg->demote = false; |
| 1000 | mg->promote = true; |
| 1001 | mg->cache = cache; |
| 1002 | mg->new_oblock = oblock; |
| 1003 | mg->cblock = cblock; |
| 1004 | mg->old_ocell = NULL; |
| 1005 | mg->new_ocell = cell; |
| 1006 | mg->start_jiffies = jiffies; |
| 1007 | |
| 1008 | inc_nr_migrations(cache); |
| 1009 | quiesce_migration(mg); |
| 1010 | } |
| 1011 | |
| 1012 | static void writeback(struct cache *cache, struct prealloc *structs, |
| 1013 | dm_oblock_t oblock, dm_cblock_t cblock, |
| 1014 | struct dm_bio_prison_cell *cell) |
| 1015 | { |
| 1016 | struct dm_cache_migration *mg = prealloc_get_migration(structs); |
| 1017 | |
| 1018 | mg->err = false; |
| 1019 | mg->writeback = true; |
| 1020 | mg->demote = false; |
| 1021 | mg->promote = false; |
| 1022 | mg->cache = cache; |
| 1023 | mg->old_oblock = oblock; |
| 1024 | mg->cblock = cblock; |
| 1025 | mg->old_ocell = cell; |
| 1026 | mg->new_ocell = NULL; |
| 1027 | mg->start_jiffies = jiffies; |
| 1028 | |
| 1029 | inc_nr_migrations(cache); |
| 1030 | quiesce_migration(mg); |
| 1031 | } |
| 1032 | |
| 1033 | static void demote_then_promote(struct cache *cache, struct prealloc *structs, |
| 1034 | dm_oblock_t old_oblock, dm_oblock_t new_oblock, |
| 1035 | dm_cblock_t cblock, |
| 1036 | struct dm_bio_prison_cell *old_ocell, |
| 1037 | struct dm_bio_prison_cell *new_ocell) |
| 1038 | { |
| 1039 | struct dm_cache_migration *mg = prealloc_get_migration(structs); |
| 1040 | |
| 1041 | mg->err = false; |
| 1042 | mg->writeback = false; |
| 1043 | mg->demote = true; |
| 1044 | mg->promote = true; |
| 1045 | mg->cache = cache; |
| 1046 | mg->old_oblock = old_oblock; |
| 1047 | mg->new_oblock = new_oblock; |
| 1048 | mg->cblock = cblock; |
| 1049 | mg->old_ocell = old_ocell; |
| 1050 | mg->new_ocell = new_ocell; |
| 1051 | mg->start_jiffies = jiffies; |
| 1052 | |
| 1053 | inc_nr_migrations(cache); |
| 1054 | quiesce_migration(mg); |
| 1055 | } |
| 1056 | |
| 1057 | /*---------------------------------------------------------------- |
| 1058 | * bio processing |
| 1059 | *--------------------------------------------------------------*/ |
| 1060 | static void defer_bio(struct cache *cache, struct bio *bio) |
| 1061 | { |
| 1062 | unsigned long flags; |
| 1063 | |
| 1064 | spin_lock_irqsave(&cache->lock, flags); |
| 1065 | bio_list_add(&cache->deferred_bios, bio); |
| 1066 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1067 | |
| 1068 | wake_worker(cache); |
| 1069 | } |
| 1070 | |
| 1071 | static void process_flush_bio(struct cache *cache, struct bio *bio) |
| 1072 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 1073 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 1074 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1075 | |
| 1076 | BUG_ON(bio->bi_size); |
| 1077 | if (!pb->req_nr) |
| 1078 | remap_to_origin(cache, bio); |
| 1079 | else |
| 1080 | remap_to_cache(cache, bio, 0); |
| 1081 | |
| 1082 | issue(cache, bio); |
| 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * People generally discard large parts of a device, eg, the whole device |
| 1087 | * when formatting. Splitting these large discards up into cache block |
| 1088 | * sized ios and then quiescing (always neccessary for discard) takes too |
| 1089 | * long. |
| 1090 | * |
| 1091 | * We keep it simple, and allow any size of discard to come in, and just |
| 1092 | * mark off blocks on the discard bitset. No passdown occurs! |
| 1093 | * |
| 1094 | * To implement passdown we need to change the bio_prison such that a cell |
| 1095 | * can have a key that spans many blocks. |
| 1096 | */ |
| 1097 | static void process_discard_bio(struct cache *cache, struct bio *bio) |
| 1098 | { |
| 1099 | dm_block_t start_block = dm_sector_div_up(bio->bi_sector, |
| 1100 | cache->discard_block_size); |
| 1101 | dm_block_t end_block = bio->bi_sector + bio_sectors(bio); |
| 1102 | dm_block_t b; |
| 1103 | |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 1104 | end_block = block_div(end_block, cache->discard_block_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1105 | |
| 1106 | for (b = start_block; b < end_block; b++) |
| 1107 | set_discard(cache, to_dblock(b)); |
| 1108 | |
| 1109 | bio_endio(bio, 0); |
| 1110 | } |
| 1111 | |
| 1112 | static bool spare_migration_bandwidth(struct cache *cache) |
| 1113 | { |
| 1114 | sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) * |
| 1115 | cache->sectors_per_block; |
| 1116 | return current_volume < cache->migration_threshold; |
| 1117 | } |
| 1118 | |
| 1119 | static bool is_writethrough_io(struct cache *cache, struct bio *bio, |
| 1120 | dm_cblock_t cblock) |
| 1121 | { |
| 1122 | return bio_data_dir(bio) == WRITE && |
| 1123 | cache->features.write_through && !is_dirty(cache, cblock); |
| 1124 | } |
| 1125 | |
| 1126 | static void inc_hit_counter(struct cache *cache, struct bio *bio) |
| 1127 | { |
| 1128 | atomic_inc(bio_data_dir(bio) == READ ? |
| 1129 | &cache->stats.read_hit : &cache->stats.write_hit); |
| 1130 | } |
| 1131 | |
| 1132 | static void inc_miss_counter(struct cache *cache, struct bio *bio) |
| 1133 | { |
| 1134 | atomic_inc(bio_data_dir(bio) == READ ? |
| 1135 | &cache->stats.read_miss : &cache->stats.write_miss); |
| 1136 | } |
| 1137 | |
| 1138 | static void process_bio(struct cache *cache, struct prealloc *structs, |
| 1139 | struct bio *bio) |
| 1140 | { |
| 1141 | int r; |
| 1142 | bool release_cell = true; |
| 1143 | dm_oblock_t block = get_bio_block(cache, bio); |
| 1144 | struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell; |
| 1145 | struct policy_result lookup_result; |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 1146 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 1147 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1148 | bool discarded_block = is_discarded_oblock(cache, block); |
| 1149 | bool can_migrate = discarded_block || spare_migration_bandwidth(cache); |
| 1150 | |
| 1151 | /* |
| 1152 | * Check to see if that block is currently migrating. |
| 1153 | */ |
| 1154 | cell_prealloc = prealloc_get_cell(structs); |
| 1155 | r = bio_detain(cache, block, bio, cell_prealloc, |
| 1156 | (cell_free_fn) prealloc_put_cell, |
| 1157 | structs, &new_ocell); |
| 1158 | if (r > 0) |
| 1159 | return; |
| 1160 | |
| 1161 | r = policy_map(cache->policy, block, true, can_migrate, discarded_block, |
| 1162 | bio, &lookup_result); |
| 1163 | |
| 1164 | if (r == -EWOULDBLOCK) |
| 1165 | /* migration has been denied */ |
| 1166 | lookup_result.op = POLICY_MISS; |
| 1167 | |
| 1168 | switch (lookup_result.op) { |
| 1169 | case POLICY_HIT: |
| 1170 | inc_hit_counter(cache, bio); |
| 1171 | pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds); |
| 1172 | |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1173 | if (is_writethrough_io(cache, bio, lookup_result.cblock)) |
| 1174 | remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock); |
| 1175 | else |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1176 | remap_to_cache_dirty(cache, bio, block, lookup_result.cblock); |
| 1177 | |
| 1178 | issue(cache, bio); |
| 1179 | break; |
| 1180 | |
| 1181 | case POLICY_MISS: |
| 1182 | inc_miss_counter(cache, bio); |
| 1183 | pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1184 | remap_to_origin_clear_discard(cache, bio, block); |
| 1185 | issue(cache, bio); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1186 | break; |
| 1187 | |
| 1188 | case POLICY_NEW: |
| 1189 | atomic_inc(&cache->stats.promotion); |
| 1190 | promote(cache, structs, block, lookup_result.cblock, new_ocell); |
| 1191 | release_cell = false; |
| 1192 | break; |
| 1193 | |
| 1194 | case POLICY_REPLACE: |
| 1195 | cell_prealloc = prealloc_get_cell(structs); |
| 1196 | r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc, |
| 1197 | (cell_free_fn) prealloc_put_cell, |
| 1198 | structs, &old_ocell); |
| 1199 | if (r > 0) { |
| 1200 | /* |
| 1201 | * We have to be careful to avoid lock inversion of |
| 1202 | * the cells. So we back off, and wait for the |
| 1203 | * old_ocell to become free. |
| 1204 | */ |
| 1205 | policy_force_mapping(cache->policy, block, |
| 1206 | lookup_result.old_oblock); |
| 1207 | atomic_inc(&cache->stats.cache_cell_clash); |
| 1208 | break; |
| 1209 | } |
| 1210 | atomic_inc(&cache->stats.demotion); |
| 1211 | atomic_inc(&cache->stats.promotion); |
| 1212 | |
| 1213 | demote_then_promote(cache, structs, lookup_result.old_oblock, |
| 1214 | block, lookup_result.cblock, |
| 1215 | old_ocell, new_ocell); |
| 1216 | release_cell = false; |
| 1217 | break; |
| 1218 | |
| 1219 | default: |
| 1220 | DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__, |
| 1221 | (unsigned) lookup_result.op); |
| 1222 | bio_io_error(bio); |
| 1223 | } |
| 1224 | |
| 1225 | if (release_cell) |
| 1226 | cell_defer(cache, new_ocell, false); |
| 1227 | } |
| 1228 | |
| 1229 | static int need_commit_due_to_time(struct cache *cache) |
| 1230 | { |
| 1231 | return jiffies < cache->last_commit_jiffies || |
| 1232 | jiffies > cache->last_commit_jiffies + COMMIT_PERIOD; |
| 1233 | } |
| 1234 | |
| 1235 | static int commit_if_needed(struct cache *cache) |
| 1236 | { |
| 1237 | if (dm_cache_changed_this_transaction(cache->cmd) && |
| 1238 | (cache->commit_requested || need_commit_due_to_time(cache))) { |
| 1239 | atomic_inc(&cache->stats.commit_count); |
| 1240 | cache->last_commit_jiffies = jiffies; |
| 1241 | cache->commit_requested = false; |
| 1242 | return dm_cache_commit(cache->cmd, false); |
| 1243 | } |
| 1244 | |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | static void process_deferred_bios(struct cache *cache) |
| 1249 | { |
| 1250 | unsigned long flags; |
| 1251 | struct bio_list bios; |
| 1252 | struct bio *bio; |
| 1253 | struct prealloc structs; |
| 1254 | |
| 1255 | memset(&structs, 0, sizeof(structs)); |
| 1256 | bio_list_init(&bios); |
| 1257 | |
| 1258 | spin_lock_irqsave(&cache->lock, flags); |
| 1259 | bio_list_merge(&bios, &cache->deferred_bios); |
| 1260 | bio_list_init(&cache->deferred_bios); |
| 1261 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1262 | |
| 1263 | while (!bio_list_empty(&bios)) { |
| 1264 | /* |
| 1265 | * If we've got no free migration structs, and processing |
| 1266 | * this bio might require one, we pause until there are some |
| 1267 | * prepared mappings to process. |
| 1268 | */ |
| 1269 | if (prealloc_data_structs(cache, &structs)) { |
| 1270 | spin_lock_irqsave(&cache->lock, flags); |
| 1271 | bio_list_merge(&cache->deferred_bios, &bios); |
| 1272 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1273 | break; |
| 1274 | } |
| 1275 | |
| 1276 | bio = bio_list_pop(&bios); |
| 1277 | |
| 1278 | if (bio->bi_rw & REQ_FLUSH) |
| 1279 | process_flush_bio(cache, bio); |
| 1280 | else if (bio->bi_rw & REQ_DISCARD) |
| 1281 | process_discard_bio(cache, bio); |
| 1282 | else |
| 1283 | process_bio(cache, &structs, bio); |
| 1284 | } |
| 1285 | |
| 1286 | prealloc_free_structs(cache, &structs); |
| 1287 | } |
| 1288 | |
| 1289 | static void process_deferred_flush_bios(struct cache *cache, bool submit_bios) |
| 1290 | { |
| 1291 | unsigned long flags; |
| 1292 | struct bio_list bios; |
| 1293 | struct bio *bio; |
| 1294 | |
| 1295 | bio_list_init(&bios); |
| 1296 | |
| 1297 | spin_lock_irqsave(&cache->lock, flags); |
| 1298 | bio_list_merge(&bios, &cache->deferred_flush_bios); |
| 1299 | bio_list_init(&cache->deferred_flush_bios); |
| 1300 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1301 | |
| 1302 | while ((bio = bio_list_pop(&bios))) |
| 1303 | submit_bios ? generic_make_request(bio) : bio_io_error(bio); |
| 1304 | } |
| 1305 | |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1306 | static void process_deferred_writethrough_bios(struct cache *cache) |
| 1307 | { |
| 1308 | unsigned long flags; |
| 1309 | struct bio_list bios; |
| 1310 | struct bio *bio; |
| 1311 | |
| 1312 | bio_list_init(&bios); |
| 1313 | |
| 1314 | spin_lock_irqsave(&cache->lock, flags); |
| 1315 | bio_list_merge(&bios, &cache->deferred_writethrough_bios); |
| 1316 | bio_list_init(&cache->deferred_writethrough_bios); |
| 1317 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1318 | |
| 1319 | while ((bio = bio_list_pop(&bios))) |
| 1320 | generic_make_request(bio); |
| 1321 | } |
| 1322 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1323 | static void writeback_some_dirty_blocks(struct cache *cache) |
| 1324 | { |
| 1325 | int r = 0; |
| 1326 | dm_oblock_t oblock; |
| 1327 | dm_cblock_t cblock; |
| 1328 | struct prealloc structs; |
| 1329 | struct dm_bio_prison_cell *old_ocell; |
| 1330 | |
| 1331 | memset(&structs, 0, sizeof(structs)); |
| 1332 | |
| 1333 | while (spare_migration_bandwidth(cache)) { |
| 1334 | if (prealloc_data_structs(cache, &structs)) |
| 1335 | break; |
| 1336 | |
| 1337 | r = policy_writeback_work(cache->policy, &oblock, &cblock); |
| 1338 | if (r) |
| 1339 | break; |
| 1340 | |
| 1341 | r = get_cell(cache, oblock, &structs, &old_ocell); |
| 1342 | if (r) { |
| 1343 | policy_set_dirty(cache->policy, oblock); |
| 1344 | break; |
| 1345 | } |
| 1346 | |
| 1347 | writeback(cache, &structs, oblock, cblock, old_ocell); |
| 1348 | } |
| 1349 | |
| 1350 | prealloc_free_structs(cache, &structs); |
| 1351 | } |
| 1352 | |
| 1353 | /*---------------------------------------------------------------- |
| 1354 | * Main worker loop |
| 1355 | *--------------------------------------------------------------*/ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1356 | static bool is_quiescing(struct cache *cache) |
| 1357 | { |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 1358 | return atomic_read(&cache->quiescing); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1361 | static void ack_quiescing(struct cache *cache) |
| 1362 | { |
| 1363 | if (is_quiescing(cache)) { |
| 1364 | atomic_inc(&cache->quiescing_ack); |
| 1365 | wake_up(&cache->quiescing_wait); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | static void wait_for_quiescing_ack(struct cache *cache) |
| 1370 | { |
| 1371 | wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack)); |
| 1372 | } |
| 1373 | |
| 1374 | static void start_quiescing(struct cache *cache) |
| 1375 | { |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 1376 | atomic_inc(&cache->quiescing); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1377 | wait_for_quiescing_ack(cache); |
| 1378 | } |
| 1379 | |
| 1380 | static void stop_quiescing(struct cache *cache) |
| 1381 | { |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 1382 | atomic_set(&cache->quiescing, 0); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1383 | atomic_set(&cache->quiescing_ack, 0); |
| 1384 | } |
| 1385 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1386 | static void wait_for_migrations(struct cache *cache) |
| 1387 | { |
| 1388 | wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations)); |
| 1389 | } |
| 1390 | |
| 1391 | static void stop_worker(struct cache *cache) |
| 1392 | { |
| 1393 | cancel_delayed_work(&cache->waker); |
| 1394 | flush_workqueue(cache->wq); |
| 1395 | } |
| 1396 | |
| 1397 | static void requeue_deferred_io(struct cache *cache) |
| 1398 | { |
| 1399 | struct bio *bio; |
| 1400 | struct bio_list bios; |
| 1401 | |
| 1402 | bio_list_init(&bios); |
| 1403 | bio_list_merge(&bios, &cache->deferred_bios); |
| 1404 | bio_list_init(&cache->deferred_bios); |
| 1405 | |
| 1406 | while ((bio = bio_list_pop(&bios))) |
| 1407 | bio_endio(bio, DM_ENDIO_REQUEUE); |
| 1408 | } |
| 1409 | |
| 1410 | static int more_work(struct cache *cache) |
| 1411 | { |
| 1412 | if (is_quiescing(cache)) |
| 1413 | return !list_empty(&cache->quiesced_migrations) || |
| 1414 | !list_empty(&cache->completed_migrations) || |
| 1415 | !list_empty(&cache->need_commit_migrations); |
| 1416 | else |
| 1417 | return !bio_list_empty(&cache->deferred_bios) || |
| 1418 | !bio_list_empty(&cache->deferred_flush_bios) || |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1419 | !bio_list_empty(&cache->deferred_writethrough_bios) || |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1420 | !list_empty(&cache->quiesced_migrations) || |
| 1421 | !list_empty(&cache->completed_migrations) || |
| 1422 | !list_empty(&cache->need_commit_migrations); |
| 1423 | } |
| 1424 | |
| 1425 | static void do_worker(struct work_struct *ws) |
| 1426 | { |
| 1427 | struct cache *cache = container_of(ws, struct cache, worker); |
| 1428 | |
| 1429 | do { |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1430 | if (!is_quiescing(cache)) { |
| 1431 | writeback_some_dirty_blocks(cache); |
| 1432 | process_deferred_writethrough_bios(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1433 | process_deferred_bios(cache); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1434 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1435 | |
| 1436 | process_migrations(cache, &cache->quiesced_migrations, issue_copy); |
| 1437 | process_migrations(cache, &cache->completed_migrations, complete_migration); |
| 1438 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1439 | if (commit_if_needed(cache)) { |
| 1440 | process_deferred_flush_bios(cache, false); |
| 1441 | |
| 1442 | /* |
| 1443 | * FIXME: rollback metadata or just go into a |
| 1444 | * failure mode and error everything |
| 1445 | */ |
| 1446 | } else { |
| 1447 | process_deferred_flush_bios(cache, true); |
| 1448 | process_migrations(cache, &cache->need_commit_migrations, |
| 1449 | migration_success_post_commit); |
| 1450 | } |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1451 | |
| 1452 | ack_quiescing(cache); |
| 1453 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1454 | } while (more_work(cache)); |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * We want to commit periodically so that not too much |
| 1459 | * unwritten metadata builds up. |
| 1460 | */ |
| 1461 | static void do_waker(struct work_struct *ws) |
| 1462 | { |
| 1463 | struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker); |
Joe Thornber | f8350da | 2013-05-10 14:37:16 +0100 | [diff] [blame] | 1464 | policy_tick(cache->policy); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1465 | wake_worker(cache); |
| 1466 | queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD); |
| 1467 | } |
| 1468 | |
| 1469 | /*----------------------------------------------------------------*/ |
| 1470 | |
| 1471 | static int is_congested(struct dm_dev *dev, int bdi_bits) |
| 1472 | { |
| 1473 | struct request_queue *q = bdev_get_queue(dev->bdev); |
| 1474 | return bdi_congested(&q->backing_dev_info, bdi_bits); |
| 1475 | } |
| 1476 | |
| 1477 | static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits) |
| 1478 | { |
| 1479 | struct cache *cache = container_of(cb, struct cache, callbacks); |
| 1480 | |
| 1481 | return is_congested(cache->origin_dev, bdi_bits) || |
| 1482 | is_congested(cache->cache_dev, bdi_bits); |
| 1483 | } |
| 1484 | |
| 1485 | /*---------------------------------------------------------------- |
| 1486 | * Target methods |
| 1487 | *--------------------------------------------------------------*/ |
| 1488 | |
| 1489 | /* |
| 1490 | * This function gets called on the error paths of the constructor, so we |
| 1491 | * have to cope with a partially initialised struct. |
| 1492 | */ |
| 1493 | static void destroy(struct cache *cache) |
| 1494 | { |
| 1495 | unsigned i; |
| 1496 | |
| 1497 | if (cache->next_migration) |
| 1498 | mempool_free(cache->next_migration, cache->migration_pool); |
| 1499 | |
| 1500 | if (cache->migration_pool) |
| 1501 | mempool_destroy(cache->migration_pool); |
| 1502 | |
| 1503 | if (cache->all_io_ds) |
| 1504 | dm_deferred_set_destroy(cache->all_io_ds); |
| 1505 | |
| 1506 | if (cache->prison) |
| 1507 | dm_bio_prison_destroy(cache->prison); |
| 1508 | |
| 1509 | if (cache->wq) |
| 1510 | destroy_workqueue(cache->wq); |
| 1511 | |
| 1512 | if (cache->dirty_bitset) |
| 1513 | free_bitset(cache->dirty_bitset); |
| 1514 | |
| 1515 | if (cache->discard_bitset) |
| 1516 | free_bitset(cache->discard_bitset); |
| 1517 | |
| 1518 | if (cache->copier) |
| 1519 | dm_kcopyd_client_destroy(cache->copier); |
| 1520 | |
| 1521 | if (cache->cmd) |
| 1522 | dm_cache_metadata_close(cache->cmd); |
| 1523 | |
| 1524 | if (cache->metadata_dev) |
| 1525 | dm_put_device(cache->ti, cache->metadata_dev); |
| 1526 | |
| 1527 | if (cache->origin_dev) |
| 1528 | dm_put_device(cache->ti, cache->origin_dev); |
| 1529 | |
| 1530 | if (cache->cache_dev) |
| 1531 | dm_put_device(cache->ti, cache->cache_dev); |
| 1532 | |
| 1533 | if (cache->policy) |
| 1534 | dm_cache_policy_destroy(cache->policy); |
| 1535 | |
| 1536 | for (i = 0; i < cache->nr_ctr_args ; i++) |
| 1537 | kfree(cache->ctr_args[i]); |
| 1538 | kfree(cache->ctr_args); |
| 1539 | |
| 1540 | kfree(cache); |
| 1541 | } |
| 1542 | |
| 1543 | static void cache_dtr(struct dm_target *ti) |
| 1544 | { |
| 1545 | struct cache *cache = ti->private; |
| 1546 | |
| 1547 | destroy(cache); |
| 1548 | } |
| 1549 | |
| 1550 | static sector_t get_dev_size(struct dm_dev *dev) |
| 1551 | { |
| 1552 | return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT; |
| 1553 | } |
| 1554 | |
| 1555 | /*----------------------------------------------------------------*/ |
| 1556 | |
| 1557 | /* |
| 1558 | * Construct a cache device mapping. |
| 1559 | * |
| 1560 | * cache <metadata dev> <cache dev> <origin dev> <block size> |
| 1561 | * <#feature args> [<feature arg>]* |
| 1562 | * <policy> <#policy args> [<policy arg>]* |
| 1563 | * |
| 1564 | * metadata dev : fast device holding the persistent metadata |
| 1565 | * cache dev : fast device holding cached data blocks |
| 1566 | * origin dev : slow device holding original data blocks |
| 1567 | * block size : cache unit size in sectors |
| 1568 | * |
| 1569 | * #feature args : number of feature arguments passed |
| 1570 | * feature args : writethrough. (The default is writeback.) |
| 1571 | * |
| 1572 | * policy : the replacement policy to use |
| 1573 | * #policy args : an even number of policy arguments corresponding |
| 1574 | * to key/value pairs passed to the policy |
| 1575 | * policy args : key/value pairs passed to the policy |
| 1576 | * E.g. 'sequential_threshold 1024' |
| 1577 | * See cache-policies.txt for details. |
| 1578 | * |
| 1579 | * Optional feature arguments are: |
| 1580 | * writethrough : write through caching that prohibits cache block |
| 1581 | * content from being different from origin block content. |
| 1582 | * Without this argument, the default behaviour is to write |
| 1583 | * back cache block contents later for performance reasons, |
| 1584 | * so they may differ from the corresponding origin blocks. |
| 1585 | */ |
| 1586 | struct cache_args { |
| 1587 | struct dm_target *ti; |
| 1588 | |
| 1589 | struct dm_dev *metadata_dev; |
| 1590 | |
| 1591 | struct dm_dev *cache_dev; |
| 1592 | sector_t cache_sectors; |
| 1593 | |
| 1594 | struct dm_dev *origin_dev; |
| 1595 | sector_t origin_sectors; |
| 1596 | |
| 1597 | uint32_t block_size; |
| 1598 | |
| 1599 | const char *policy_name; |
| 1600 | int policy_argc; |
| 1601 | const char **policy_argv; |
| 1602 | |
| 1603 | struct cache_features features; |
| 1604 | }; |
| 1605 | |
| 1606 | static void destroy_cache_args(struct cache_args *ca) |
| 1607 | { |
| 1608 | if (ca->metadata_dev) |
| 1609 | dm_put_device(ca->ti, ca->metadata_dev); |
| 1610 | |
| 1611 | if (ca->cache_dev) |
| 1612 | dm_put_device(ca->ti, ca->cache_dev); |
| 1613 | |
| 1614 | if (ca->origin_dev) |
| 1615 | dm_put_device(ca->ti, ca->origin_dev); |
| 1616 | |
| 1617 | kfree(ca); |
| 1618 | } |
| 1619 | |
| 1620 | static bool at_least_one_arg(struct dm_arg_set *as, char **error) |
| 1621 | { |
| 1622 | if (!as->argc) { |
| 1623 | *error = "Insufficient args"; |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
| 1627 | return true; |
| 1628 | } |
| 1629 | |
| 1630 | static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as, |
| 1631 | char **error) |
| 1632 | { |
| 1633 | int r; |
| 1634 | sector_t metadata_dev_size; |
| 1635 | char b[BDEVNAME_SIZE]; |
| 1636 | |
| 1637 | if (!at_least_one_arg(as, error)) |
| 1638 | return -EINVAL; |
| 1639 | |
| 1640 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, |
| 1641 | &ca->metadata_dev); |
| 1642 | if (r) { |
| 1643 | *error = "Error opening metadata device"; |
| 1644 | return r; |
| 1645 | } |
| 1646 | |
| 1647 | metadata_dev_size = get_dev_size(ca->metadata_dev); |
| 1648 | if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING) |
| 1649 | DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.", |
| 1650 | bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS); |
| 1651 | |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
| 1655 | static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as, |
| 1656 | char **error) |
| 1657 | { |
| 1658 | int r; |
| 1659 | |
| 1660 | if (!at_least_one_arg(as, error)) |
| 1661 | return -EINVAL; |
| 1662 | |
| 1663 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, |
| 1664 | &ca->cache_dev); |
| 1665 | if (r) { |
| 1666 | *error = "Error opening cache device"; |
| 1667 | return r; |
| 1668 | } |
| 1669 | ca->cache_sectors = get_dev_size(ca->cache_dev); |
| 1670 | |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as, |
| 1675 | char **error) |
| 1676 | { |
| 1677 | int r; |
| 1678 | |
| 1679 | if (!at_least_one_arg(as, error)) |
| 1680 | return -EINVAL; |
| 1681 | |
| 1682 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, |
| 1683 | &ca->origin_dev); |
| 1684 | if (r) { |
| 1685 | *error = "Error opening origin device"; |
| 1686 | return r; |
| 1687 | } |
| 1688 | |
| 1689 | ca->origin_sectors = get_dev_size(ca->origin_dev); |
| 1690 | if (ca->ti->len > ca->origin_sectors) { |
| 1691 | *error = "Device size larger than cached device"; |
| 1692 | return -EINVAL; |
| 1693 | } |
| 1694 | |
| 1695 | return 0; |
| 1696 | } |
| 1697 | |
| 1698 | static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as, |
| 1699 | char **error) |
| 1700 | { |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 1701 | unsigned long block_size; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1702 | |
| 1703 | if (!at_least_one_arg(as, error)) |
| 1704 | return -EINVAL; |
| 1705 | |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 1706 | if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size || |
| 1707 | block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS || |
| 1708 | block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS || |
| 1709 | block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1710 | *error = "Invalid data block size"; |
| 1711 | return -EINVAL; |
| 1712 | } |
| 1713 | |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 1714 | if (block_size > ca->cache_sectors) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1715 | *error = "Data block size is larger than the cache device"; |
| 1716 | return -EINVAL; |
| 1717 | } |
| 1718 | |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 1719 | ca->block_size = block_size; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1720 | |
| 1721 | return 0; |
| 1722 | } |
| 1723 | |
| 1724 | static void init_features(struct cache_features *cf) |
| 1725 | { |
| 1726 | cf->mode = CM_WRITE; |
| 1727 | cf->write_through = false; |
| 1728 | } |
| 1729 | |
| 1730 | static int parse_features(struct cache_args *ca, struct dm_arg_set *as, |
| 1731 | char **error) |
| 1732 | { |
| 1733 | static struct dm_arg _args[] = { |
| 1734 | {0, 1, "Invalid number of cache feature arguments"}, |
| 1735 | }; |
| 1736 | |
| 1737 | int r; |
| 1738 | unsigned argc; |
| 1739 | const char *arg; |
| 1740 | struct cache_features *cf = &ca->features; |
| 1741 | |
| 1742 | init_features(cf); |
| 1743 | |
| 1744 | r = dm_read_arg_group(_args, as, &argc, error); |
| 1745 | if (r) |
| 1746 | return -EINVAL; |
| 1747 | |
| 1748 | while (argc--) { |
| 1749 | arg = dm_shift_arg(as); |
| 1750 | |
| 1751 | if (!strcasecmp(arg, "writeback")) |
| 1752 | cf->write_through = false; |
| 1753 | |
| 1754 | else if (!strcasecmp(arg, "writethrough")) |
| 1755 | cf->write_through = true; |
| 1756 | |
| 1757 | else { |
| 1758 | *error = "Unrecognised cache feature requested"; |
| 1759 | return -EINVAL; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | static int parse_policy(struct cache_args *ca, struct dm_arg_set *as, |
| 1767 | char **error) |
| 1768 | { |
| 1769 | static struct dm_arg _args[] = { |
| 1770 | {0, 1024, "Invalid number of policy arguments"}, |
| 1771 | }; |
| 1772 | |
| 1773 | int r; |
| 1774 | |
| 1775 | if (!at_least_one_arg(as, error)) |
| 1776 | return -EINVAL; |
| 1777 | |
| 1778 | ca->policy_name = dm_shift_arg(as); |
| 1779 | |
| 1780 | r = dm_read_arg_group(_args, as, &ca->policy_argc, error); |
| 1781 | if (r) |
| 1782 | return -EINVAL; |
| 1783 | |
| 1784 | ca->policy_argv = (const char **)as->argv; |
| 1785 | dm_consume_args(as, ca->policy_argc); |
| 1786 | |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | static int parse_cache_args(struct cache_args *ca, int argc, char **argv, |
| 1791 | char **error) |
| 1792 | { |
| 1793 | int r; |
| 1794 | struct dm_arg_set as; |
| 1795 | |
| 1796 | as.argc = argc; |
| 1797 | as.argv = argv; |
| 1798 | |
| 1799 | r = parse_metadata_dev(ca, &as, error); |
| 1800 | if (r) |
| 1801 | return r; |
| 1802 | |
| 1803 | r = parse_cache_dev(ca, &as, error); |
| 1804 | if (r) |
| 1805 | return r; |
| 1806 | |
| 1807 | r = parse_origin_dev(ca, &as, error); |
| 1808 | if (r) |
| 1809 | return r; |
| 1810 | |
| 1811 | r = parse_block_size(ca, &as, error); |
| 1812 | if (r) |
| 1813 | return r; |
| 1814 | |
| 1815 | r = parse_features(ca, &as, error); |
| 1816 | if (r) |
| 1817 | return r; |
| 1818 | |
| 1819 | r = parse_policy(ca, &as, error); |
| 1820 | if (r) |
| 1821 | return r; |
| 1822 | |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
| 1826 | /*----------------------------------------------------------------*/ |
| 1827 | |
| 1828 | static struct kmem_cache *migration_cache; |
| 1829 | |
Alasdair G Kergon | 2c73c47 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1830 | #define NOT_CORE_OPTION 1 |
| 1831 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1832 | static int process_config_option(struct cache *cache, const char *key, const char *value) |
Alasdair G Kergon | 2c73c47 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1833 | { |
| 1834 | unsigned long tmp; |
| 1835 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1836 | if (!strcasecmp(key, "migration_threshold")) { |
| 1837 | if (kstrtoul(value, 10, &tmp)) |
Alasdair G Kergon | 2c73c47 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1838 | return -EINVAL; |
| 1839 | |
| 1840 | cache->migration_threshold = tmp; |
| 1841 | return 0; |
| 1842 | } |
| 1843 | |
| 1844 | return NOT_CORE_OPTION; |
| 1845 | } |
| 1846 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1847 | static int set_config_value(struct cache *cache, const char *key, const char *value) |
| 1848 | { |
| 1849 | int r = process_config_option(cache, key, value); |
| 1850 | |
| 1851 | if (r == NOT_CORE_OPTION) |
| 1852 | r = policy_set_config_value(cache->policy, key, value); |
| 1853 | |
| 1854 | if (r) |
| 1855 | DMWARN("bad config value for %s: %s", key, value); |
| 1856 | |
| 1857 | return r; |
| 1858 | } |
| 1859 | |
| 1860 | static int set_config_values(struct cache *cache, int argc, const char **argv) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1861 | { |
| 1862 | int r = 0; |
| 1863 | |
| 1864 | if (argc & 1) { |
| 1865 | DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs."); |
| 1866 | return -EINVAL; |
| 1867 | } |
| 1868 | |
| 1869 | while (argc) { |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1870 | r = set_config_value(cache, argv[0], argv[1]); |
| 1871 | if (r) |
| 1872 | break; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1873 | |
| 1874 | argc -= 2; |
| 1875 | argv += 2; |
| 1876 | } |
| 1877 | |
| 1878 | return r; |
| 1879 | } |
| 1880 | |
| 1881 | static int create_cache_policy(struct cache *cache, struct cache_args *ca, |
| 1882 | char **error) |
| 1883 | { |
Mikulas Patocka | 4cb3e1d | 2013-10-01 18:35:39 -0400 | [diff] [blame] | 1884 | struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name, |
| 1885 | cache->cache_size, |
| 1886 | cache->origin_sectors, |
| 1887 | cache->sectors_per_block); |
| 1888 | if (IS_ERR(p)) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1889 | *error = "Error creating cache's policy"; |
Mikulas Patocka | 4cb3e1d | 2013-10-01 18:35:39 -0400 | [diff] [blame] | 1890 | return PTR_ERR(p); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1891 | } |
Mikulas Patocka | 4cb3e1d | 2013-10-01 18:35:39 -0400 | [diff] [blame] | 1892 | cache->policy = p; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1893 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1894 | return 0; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | /* |
| 1898 | * We want the discard block size to be a power of two, at least the size |
| 1899 | * of the cache block size, and have no more than 2^14 discard blocks |
| 1900 | * across the origin. |
| 1901 | */ |
| 1902 | #define MAX_DISCARD_BLOCKS (1 << 14) |
| 1903 | |
| 1904 | static bool too_many_discard_blocks(sector_t discard_block_size, |
| 1905 | sector_t origin_size) |
| 1906 | { |
| 1907 | (void) sector_div(origin_size, discard_block_size); |
| 1908 | |
| 1909 | return origin_size > MAX_DISCARD_BLOCKS; |
| 1910 | } |
| 1911 | |
| 1912 | static sector_t calculate_discard_block_size(sector_t cache_block_size, |
| 1913 | sector_t origin_size) |
| 1914 | { |
| 1915 | sector_t discard_block_size; |
| 1916 | |
| 1917 | discard_block_size = roundup_pow_of_two(cache_block_size); |
| 1918 | |
| 1919 | if (origin_size) |
| 1920 | while (too_many_discard_blocks(discard_block_size, origin_size)) |
| 1921 | discard_block_size *= 2; |
| 1922 | |
| 1923 | return discard_block_size; |
| 1924 | } |
| 1925 | |
Joe Thornber | f8350da | 2013-05-10 14:37:16 +0100 | [diff] [blame] | 1926 | #define DEFAULT_MIGRATION_THRESHOLD 2048 |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1927 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1928 | static int cache_create(struct cache_args *ca, struct cache **result) |
| 1929 | { |
| 1930 | int r = 0; |
| 1931 | char **error = &ca->ti->error; |
| 1932 | struct cache *cache; |
| 1933 | struct dm_target *ti = ca->ti; |
| 1934 | dm_block_t origin_blocks; |
| 1935 | struct dm_cache_metadata *cmd; |
| 1936 | bool may_format = ca->features.mode == CM_WRITE; |
| 1937 | |
| 1938 | cache = kzalloc(sizeof(*cache), GFP_KERNEL); |
| 1939 | if (!cache) |
| 1940 | return -ENOMEM; |
| 1941 | |
| 1942 | cache->ti = ca->ti; |
| 1943 | ti->private = cache; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1944 | ti->num_flush_bios = 2; |
| 1945 | ti->flush_supported = true; |
| 1946 | |
| 1947 | ti->num_discard_bios = 1; |
| 1948 | ti->discards_supported = true; |
| 1949 | ti->discard_zeroes_data_unsupported = true; |
| 1950 | |
Joe Thornber | 8c5008f | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 1951 | cache->features = ca->features; |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 1952 | ti->per_bio_data_size = get_per_bio_data_size(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1953 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1954 | cache->callbacks.congested_fn = cache_is_congested; |
| 1955 | dm_table_add_target_callbacks(ti->table, &cache->callbacks); |
| 1956 | |
| 1957 | cache->metadata_dev = ca->metadata_dev; |
| 1958 | cache->origin_dev = ca->origin_dev; |
| 1959 | cache->cache_dev = ca->cache_dev; |
| 1960 | |
| 1961 | ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL; |
| 1962 | |
| 1963 | /* FIXME: factor out this whole section */ |
| 1964 | origin_blocks = cache->origin_sectors = ca->origin_sectors; |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 1965 | origin_blocks = block_div(origin_blocks, ca->block_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1966 | cache->origin_blocks = to_oblock(origin_blocks); |
| 1967 | |
| 1968 | cache->sectors_per_block = ca->block_size; |
| 1969 | if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) { |
| 1970 | r = -EINVAL; |
| 1971 | goto bad; |
| 1972 | } |
| 1973 | |
| 1974 | if (ca->block_size & (ca->block_size - 1)) { |
| 1975 | dm_block_t cache_size = ca->cache_sectors; |
| 1976 | |
| 1977 | cache->sectors_per_block_shift = -1; |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 1978 | cache_size = block_div(cache_size, ca->block_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1979 | cache->cache_size = to_cblock(cache_size); |
| 1980 | } else { |
| 1981 | cache->sectors_per_block_shift = __ffs(ca->block_size); |
| 1982 | cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift); |
| 1983 | } |
| 1984 | |
| 1985 | r = create_cache_policy(cache, ca, error); |
| 1986 | if (r) |
| 1987 | goto bad; |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1988 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1989 | cache->policy_nr_args = ca->policy_argc; |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1990 | cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD; |
| 1991 | |
| 1992 | r = set_config_values(cache, ca->policy_argc, ca->policy_argv); |
| 1993 | if (r) { |
| 1994 | *error = "Error setting cache policy's config values"; |
| 1995 | goto bad; |
| 1996 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1997 | |
| 1998 | cmd = dm_cache_metadata_open(cache->metadata_dev->bdev, |
| 1999 | ca->block_size, may_format, |
| 2000 | dm_cache_policy_get_hint_size(cache->policy)); |
| 2001 | if (IS_ERR(cmd)) { |
| 2002 | *error = "Error creating metadata object"; |
| 2003 | r = PTR_ERR(cmd); |
| 2004 | goto bad; |
| 2005 | } |
| 2006 | cache->cmd = cmd; |
| 2007 | |
| 2008 | spin_lock_init(&cache->lock); |
| 2009 | bio_list_init(&cache->deferred_bios); |
| 2010 | bio_list_init(&cache->deferred_flush_bios); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 2011 | bio_list_init(&cache->deferred_writethrough_bios); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2012 | INIT_LIST_HEAD(&cache->quiesced_migrations); |
| 2013 | INIT_LIST_HEAD(&cache->completed_migrations); |
| 2014 | INIT_LIST_HEAD(&cache->need_commit_migrations); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2015 | atomic_set(&cache->nr_migrations, 0); |
| 2016 | init_waitqueue_head(&cache->migration_wait); |
| 2017 | |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 2018 | init_waitqueue_head(&cache->quiescing_wait); |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 2019 | atomic_set(&cache->quiescing, 0); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 2020 | atomic_set(&cache->quiescing_ack, 0); |
| 2021 | |
Wei Yongjun | fa4d683 | 2013-05-10 14:37:14 +0100 | [diff] [blame] | 2022 | r = -ENOMEM; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2023 | cache->nr_dirty = 0; |
| 2024 | cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size)); |
| 2025 | if (!cache->dirty_bitset) { |
| 2026 | *error = "could not allocate dirty bitset"; |
| 2027 | goto bad; |
| 2028 | } |
| 2029 | clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size)); |
| 2030 | |
| 2031 | cache->discard_block_size = |
| 2032 | calculate_discard_block_size(cache->sectors_per_block, |
| 2033 | cache->origin_sectors); |
| 2034 | cache->discard_nr_blocks = oblock_to_dblock(cache, cache->origin_blocks); |
| 2035 | cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks)); |
| 2036 | if (!cache->discard_bitset) { |
| 2037 | *error = "could not allocate discard bitset"; |
| 2038 | goto bad; |
| 2039 | } |
| 2040 | clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks)); |
| 2041 | |
| 2042 | cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle); |
| 2043 | if (IS_ERR(cache->copier)) { |
| 2044 | *error = "could not create kcopyd client"; |
| 2045 | r = PTR_ERR(cache->copier); |
| 2046 | goto bad; |
| 2047 | } |
| 2048 | |
| 2049 | cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM); |
| 2050 | if (!cache->wq) { |
| 2051 | *error = "could not create workqueue for metadata object"; |
| 2052 | goto bad; |
| 2053 | } |
| 2054 | INIT_WORK(&cache->worker, do_worker); |
| 2055 | INIT_DELAYED_WORK(&cache->waker, do_waker); |
| 2056 | cache->last_commit_jiffies = jiffies; |
| 2057 | |
| 2058 | cache->prison = dm_bio_prison_create(PRISON_CELLS); |
| 2059 | if (!cache->prison) { |
| 2060 | *error = "could not create bio prison"; |
| 2061 | goto bad; |
| 2062 | } |
| 2063 | |
| 2064 | cache->all_io_ds = dm_deferred_set_create(); |
| 2065 | if (!cache->all_io_ds) { |
| 2066 | *error = "could not create all_io deferred set"; |
| 2067 | goto bad; |
| 2068 | } |
| 2069 | |
| 2070 | cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE, |
| 2071 | migration_cache); |
| 2072 | if (!cache->migration_pool) { |
| 2073 | *error = "Error creating cache's migration mempool"; |
| 2074 | goto bad; |
| 2075 | } |
| 2076 | |
| 2077 | cache->next_migration = NULL; |
| 2078 | |
| 2079 | cache->need_tick_bio = true; |
| 2080 | cache->sized = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2081 | cache->commit_requested = false; |
| 2082 | cache->loaded_mappings = false; |
| 2083 | cache->loaded_discards = false; |
| 2084 | |
| 2085 | load_stats(cache); |
| 2086 | |
| 2087 | atomic_set(&cache->stats.demotion, 0); |
| 2088 | atomic_set(&cache->stats.promotion, 0); |
| 2089 | atomic_set(&cache->stats.copies_avoided, 0); |
| 2090 | atomic_set(&cache->stats.cache_cell_clash, 0); |
| 2091 | atomic_set(&cache->stats.commit_count, 0); |
| 2092 | atomic_set(&cache->stats.discard_count, 0); |
| 2093 | |
| 2094 | *result = cache; |
| 2095 | return 0; |
| 2096 | |
| 2097 | bad: |
| 2098 | destroy(cache); |
| 2099 | return r; |
| 2100 | } |
| 2101 | |
| 2102 | static int copy_ctr_args(struct cache *cache, int argc, const char **argv) |
| 2103 | { |
| 2104 | unsigned i; |
| 2105 | const char **copy; |
| 2106 | |
| 2107 | copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL); |
| 2108 | if (!copy) |
| 2109 | return -ENOMEM; |
| 2110 | for (i = 0; i < argc; i++) { |
| 2111 | copy[i] = kstrdup(argv[i], GFP_KERNEL); |
| 2112 | if (!copy[i]) { |
| 2113 | while (i--) |
| 2114 | kfree(copy[i]); |
| 2115 | kfree(copy); |
| 2116 | return -ENOMEM; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | cache->nr_ctr_args = argc; |
| 2121 | cache->ctr_args = copy; |
| 2122 | |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
| 2126 | static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv) |
| 2127 | { |
| 2128 | int r = -EINVAL; |
| 2129 | struct cache_args *ca; |
| 2130 | struct cache *cache = NULL; |
| 2131 | |
| 2132 | ca = kzalloc(sizeof(*ca), GFP_KERNEL); |
| 2133 | if (!ca) { |
| 2134 | ti->error = "Error allocating memory for cache"; |
| 2135 | return -ENOMEM; |
| 2136 | } |
| 2137 | ca->ti = ti; |
| 2138 | |
| 2139 | r = parse_cache_args(ca, argc, argv, &ti->error); |
| 2140 | if (r) |
| 2141 | goto out; |
| 2142 | |
| 2143 | r = cache_create(ca, &cache); |
Heinz Mauelshagen | 617a0b8 | 2013-03-20 17:21:26 +0000 | [diff] [blame] | 2144 | if (r) |
| 2145 | goto out; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2146 | |
| 2147 | r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3); |
| 2148 | if (r) { |
| 2149 | destroy(cache); |
| 2150 | goto out; |
| 2151 | } |
| 2152 | |
| 2153 | ti->private = cache; |
| 2154 | |
| 2155 | out: |
| 2156 | destroy_cache_args(ca); |
| 2157 | return r; |
| 2158 | } |
| 2159 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2160 | static int cache_map(struct dm_target *ti, struct bio *bio) |
| 2161 | { |
| 2162 | struct cache *cache = ti->private; |
| 2163 | |
| 2164 | int r; |
| 2165 | dm_oblock_t block = get_bio_block(cache, bio); |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 2166 | size_t pb_data_size = get_per_bio_data_size(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2167 | bool can_migrate = false; |
| 2168 | bool discarded_block; |
| 2169 | struct dm_bio_prison_cell *cell; |
| 2170 | struct policy_result lookup_result; |
| 2171 | struct per_bio_data *pb; |
| 2172 | |
| 2173 | if (from_oblock(block) > from_oblock(cache->origin_blocks)) { |
| 2174 | /* |
| 2175 | * This can only occur if the io goes to a partial block at |
| 2176 | * the end of the origin device. We don't cache these. |
| 2177 | * Just remap to the origin and carry on. |
| 2178 | */ |
| 2179 | remap_to_origin_clear_discard(cache, bio, block); |
| 2180 | return DM_MAPIO_REMAPPED; |
| 2181 | } |
| 2182 | |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 2183 | pb = init_per_bio_data(bio, pb_data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2184 | |
| 2185 | if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) { |
| 2186 | defer_bio(cache, bio); |
| 2187 | return DM_MAPIO_SUBMITTED; |
| 2188 | } |
| 2189 | |
| 2190 | /* |
| 2191 | * Check to see if that block is currently migrating. |
| 2192 | */ |
| 2193 | cell = alloc_prison_cell(cache); |
| 2194 | if (!cell) { |
| 2195 | defer_bio(cache, bio); |
| 2196 | return DM_MAPIO_SUBMITTED; |
| 2197 | } |
| 2198 | |
| 2199 | r = bio_detain(cache, block, bio, cell, |
| 2200 | (cell_free_fn) free_prison_cell, |
| 2201 | cache, &cell); |
| 2202 | if (r) { |
| 2203 | if (r < 0) |
| 2204 | defer_bio(cache, bio); |
| 2205 | |
| 2206 | return DM_MAPIO_SUBMITTED; |
| 2207 | } |
| 2208 | |
| 2209 | discarded_block = is_discarded_oblock(cache, block); |
| 2210 | |
| 2211 | r = policy_map(cache->policy, block, false, can_migrate, discarded_block, |
| 2212 | bio, &lookup_result); |
| 2213 | if (r == -EWOULDBLOCK) { |
| 2214 | cell_defer(cache, cell, true); |
| 2215 | return DM_MAPIO_SUBMITTED; |
| 2216 | |
| 2217 | } else if (r) { |
| 2218 | DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r); |
| 2219 | bio_io_error(bio); |
| 2220 | return DM_MAPIO_SUBMITTED; |
| 2221 | } |
| 2222 | |
| 2223 | switch (lookup_result.op) { |
| 2224 | case POLICY_HIT: |
| 2225 | inc_hit_counter(cache, bio); |
| 2226 | pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds); |
| 2227 | |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 2228 | if (is_writethrough_io(cache, bio, lookup_result.cblock)) |
| 2229 | remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock); |
| 2230 | else |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2231 | remap_to_cache_dirty(cache, bio, block, lookup_result.cblock); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 2232 | |
| 2233 | cell_defer(cache, cell, false); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2234 | break; |
| 2235 | |
| 2236 | case POLICY_MISS: |
| 2237 | inc_miss_counter(cache, bio); |
| 2238 | pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds); |
| 2239 | |
| 2240 | if (pb->req_nr != 0) { |
| 2241 | /* |
| 2242 | * This is a duplicate writethrough io that is no |
| 2243 | * longer needed because the block has been demoted. |
| 2244 | */ |
| 2245 | bio_endio(bio, 0); |
| 2246 | cell_defer(cache, cell, false); |
| 2247 | return DM_MAPIO_SUBMITTED; |
| 2248 | } else { |
| 2249 | remap_to_origin_clear_discard(cache, bio, block); |
| 2250 | cell_defer(cache, cell, false); |
| 2251 | } |
| 2252 | break; |
| 2253 | |
| 2254 | default: |
| 2255 | DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__, |
| 2256 | (unsigned) lookup_result.op); |
| 2257 | bio_io_error(bio); |
| 2258 | return DM_MAPIO_SUBMITTED; |
| 2259 | } |
| 2260 | |
| 2261 | return DM_MAPIO_REMAPPED; |
| 2262 | } |
| 2263 | |
| 2264 | static int cache_end_io(struct dm_target *ti, struct bio *bio, int error) |
| 2265 | { |
| 2266 | struct cache *cache = ti->private; |
| 2267 | unsigned long flags; |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 2268 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 2269 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2270 | |
| 2271 | if (pb->tick) { |
| 2272 | policy_tick(cache->policy); |
| 2273 | |
| 2274 | spin_lock_irqsave(&cache->lock, flags); |
| 2275 | cache->need_tick_bio = true; |
| 2276 | spin_unlock_irqrestore(&cache->lock, flags); |
| 2277 | } |
| 2278 | |
| 2279 | check_for_quiesced_migrations(cache, pb); |
| 2280 | |
| 2281 | return 0; |
| 2282 | } |
| 2283 | |
| 2284 | static int write_dirty_bitset(struct cache *cache) |
| 2285 | { |
| 2286 | unsigned i, r; |
| 2287 | |
| 2288 | for (i = 0; i < from_cblock(cache->cache_size); i++) { |
| 2289 | r = dm_cache_set_dirty(cache->cmd, to_cblock(i), |
| 2290 | is_dirty(cache, to_cblock(i))); |
| 2291 | if (r) |
| 2292 | return r; |
| 2293 | } |
| 2294 | |
| 2295 | return 0; |
| 2296 | } |
| 2297 | |
| 2298 | static int write_discard_bitset(struct cache *cache) |
| 2299 | { |
| 2300 | unsigned i, r; |
| 2301 | |
| 2302 | r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size, |
| 2303 | cache->discard_nr_blocks); |
| 2304 | if (r) { |
| 2305 | DMERR("could not resize on-disk discard bitset"); |
| 2306 | return r; |
| 2307 | } |
| 2308 | |
| 2309 | for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) { |
| 2310 | r = dm_cache_set_discard(cache->cmd, to_dblock(i), |
| 2311 | is_discarded(cache, to_dblock(i))); |
| 2312 | if (r) |
| 2313 | return r; |
| 2314 | } |
| 2315 | |
| 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock, |
| 2320 | uint32_t hint) |
| 2321 | { |
| 2322 | struct cache *cache = context; |
| 2323 | return dm_cache_save_hint(cache->cmd, cblock, hint); |
| 2324 | } |
| 2325 | |
| 2326 | static int write_hints(struct cache *cache) |
| 2327 | { |
| 2328 | int r; |
| 2329 | |
| 2330 | r = dm_cache_begin_hints(cache->cmd, cache->policy); |
| 2331 | if (r) { |
| 2332 | DMERR("dm_cache_begin_hints failed"); |
| 2333 | return r; |
| 2334 | } |
| 2335 | |
| 2336 | r = policy_walk_mappings(cache->policy, save_hint, cache); |
| 2337 | if (r) |
| 2338 | DMERR("policy_walk_mappings failed"); |
| 2339 | |
| 2340 | return r; |
| 2341 | } |
| 2342 | |
| 2343 | /* |
| 2344 | * returns true on success |
| 2345 | */ |
| 2346 | static bool sync_metadata(struct cache *cache) |
| 2347 | { |
| 2348 | int r1, r2, r3, r4; |
| 2349 | |
| 2350 | r1 = write_dirty_bitset(cache); |
| 2351 | if (r1) |
| 2352 | DMERR("could not write dirty bitset"); |
| 2353 | |
| 2354 | r2 = write_discard_bitset(cache); |
| 2355 | if (r2) |
| 2356 | DMERR("could not write discard bitset"); |
| 2357 | |
| 2358 | save_stats(cache); |
| 2359 | |
| 2360 | r3 = write_hints(cache); |
| 2361 | if (r3) |
| 2362 | DMERR("could not write hints"); |
| 2363 | |
| 2364 | /* |
| 2365 | * If writing the above metadata failed, we still commit, but don't |
| 2366 | * set the clean shutdown flag. This will effectively force every |
| 2367 | * dirty bit to be set on reload. |
| 2368 | */ |
| 2369 | r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3); |
| 2370 | if (r4) |
| 2371 | DMERR("could not write cache metadata. Data loss may occur."); |
| 2372 | |
| 2373 | return !r1 && !r2 && !r3 && !r4; |
| 2374 | } |
| 2375 | |
| 2376 | static void cache_postsuspend(struct dm_target *ti) |
| 2377 | { |
| 2378 | struct cache *cache = ti->private; |
| 2379 | |
| 2380 | start_quiescing(cache); |
| 2381 | wait_for_migrations(cache); |
| 2382 | stop_worker(cache); |
| 2383 | requeue_deferred_io(cache); |
| 2384 | stop_quiescing(cache); |
| 2385 | |
| 2386 | (void) sync_metadata(cache); |
| 2387 | } |
| 2388 | |
| 2389 | static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock, |
| 2390 | bool dirty, uint32_t hint, bool hint_valid) |
| 2391 | { |
| 2392 | int r; |
| 2393 | struct cache *cache = context; |
| 2394 | |
| 2395 | r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid); |
| 2396 | if (r) |
| 2397 | return r; |
| 2398 | |
| 2399 | if (dirty) |
| 2400 | set_dirty(cache, oblock, cblock); |
| 2401 | else |
| 2402 | clear_dirty(cache, oblock, cblock); |
| 2403 | |
| 2404 | return 0; |
| 2405 | } |
| 2406 | |
| 2407 | static int load_discard(void *context, sector_t discard_block_size, |
| 2408 | dm_dblock_t dblock, bool discard) |
| 2409 | { |
| 2410 | struct cache *cache = context; |
| 2411 | |
| 2412 | /* FIXME: handle mis-matched block size */ |
| 2413 | |
| 2414 | if (discard) |
| 2415 | set_discard(cache, dblock); |
| 2416 | else |
| 2417 | clear_discard(cache, dblock); |
| 2418 | |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
| 2422 | static int cache_preresume(struct dm_target *ti) |
| 2423 | { |
| 2424 | int r = 0; |
| 2425 | struct cache *cache = ti->private; |
| 2426 | sector_t actual_cache_size = get_dev_size(cache->cache_dev); |
| 2427 | (void) sector_div(actual_cache_size, cache->sectors_per_block); |
| 2428 | |
| 2429 | /* |
| 2430 | * Check to see if the cache has resized. |
| 2431 | */ |
| 2432 | if (from_cblock(cache->cache_size) != actual_cache_size || !cache->sized) { |
| 2433 | cache->cache_size = to_cblock(actual_cache_size); |
| 2434 | |
| 2435 | r = dm_cache_resize(cache->cmd, cache->cache_size); |
| 2436 | if (r) { |
| 2437 | DMERR("could not resize cache metadata"); |
| 2438 | return r; |
| 2439 | } |
| 2440 | |
| 2441 | cache->sized = true; |
| 2442 | } |
| 2443 | |
| 2444 | if (!cache->loaded_mappings) { |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 2445 | r = dm_cache_load_mappings(cache->cmd, cache->policy, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2446 | load_mapping, cache); |
| 2447 | if (r) { |
| 2448 | DMERR("could not load cache mappings"); |
| 2449 | return r; |
| 2450 | } |
| 2451 | |
| 2452 | cache->loaded_mappings = true; |
| 2453 | } |
| 2454 | |
| 2455 | if (!cache->loaded_discards) { |
| 2456 | r = dm_cache_load_discards(cache->cmd, load_discard, cache); |
| 2457 | if (r) { |
| 2458 | DMERR("could not load origin discards"); |
| 2459 | return r; |
| 2460 | } |
| 2461 | |
| 2462 | cache->loaded_discards = true; |
| 2463 | } |
| 2464 | |
| 2465 | return r; |
| 2466 | } |
| 2467 | |
| 2468 | static void cache_resume(struct dm_target *ti) |
| 2469 | { |
| 2470 | struct cache *cache = ti->private; |
| 2471 | |
| 2472 | cache->need_tick_bio = true; |
| 2473 | do_waker(&cache->waker.work); |
| 2474 | } |
| 2475 | |
| 2476 | /* |
| 2477 | * Status format: |
| 2478 | * |
| 2479 | * <#used metadata blocks>/<#total metadata blocks> |
| 2480 | * <#read hits> <#read misses> <#write hits> <#write misses> |
| 2481 | * <#demotions> <#promotions> <#blocks in cache> <#dirty> |
| 2482 | * <#features> <features>* |
| 2483 | * <#core args> <core args> |
| 2484 | * <#policy args> <policy args>* |
| 2485 | */ |
| 2486 | static void cache_status(struct dm_target *ti, status_type_t type, |
| 2487 | unsigned status_flags, char *result, unsigned maxlen) |
| 2488 | { |
| 2489 | int r = 0; |
| 2490 | unsigned i; |
| 2491 | ssize_t sz = 0; |
| 2492 | dm_block_t nr_free_blocks_metadata = 0; |
| 2493 | dm_block_t nr_blocks_metadata = 0; |
| 2494 | char buf[BDEVNAME_SIZE]; |
| 2495 | struct cache *cache = ti->private; |
| 2496 | dm_cblock_t residency; |
| 2497 | |
| 2498 | switch (type) { |
| 2499 | case STATUSTYPE_INFO: |
| 2500 | /* Commit to ensure statistics aren't out-of-date */ |
| 2501 | if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) { |
| 2502 | r = dm_cache_commit(cache->cmd, false); |
| 2503 | if (r) |
| 2504 | DMERR("could not commit metadata for accurate status"); |
| 2505 | } |
| 2506 | |
| 2507 | r = dm_cache_get_free_metadata_block_count(cache->cmd, |
| 2508 | &nr_free_blocks_metadata); |
| 2509 | if (r) { |
| 2510 | DMERR("could not get metadata free block count"); |
| 2511 | goto err; |
| 2512 | } |
| 2513 | |
| 2514 | r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata); |
| 2515 | if (r) { |
| 2516 | DMERR("could not get metadata device size"); |
| 2517 | goto err; |
| 2518 | } |
| 2519 | |
| 2520 | residency = policy_residency(cache->policy); |
| 2521 | |
| 2522 | DMEMIT("%llu/%llu %u %u %u %u %u %u %llu %u ", |
| 2523 | (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata), |
| 2524 | (unsigned long long)nr_blocks_metadata, |
| 2525 | (unsigned) atomic_read(&cache->stats.read_hit), |
| 2526 | (unsigned) atomic_read(&cache->stats.read_miss), |
| 2527 | (unsigned) atomic_read(&cache->stats.write_hit), |
| 2528 | (unsigned) atomic_read(&cache->stats.write_miss), |
| 2529 | (unsigned) atomic_read(&cache->stats.demotion), |
| 2530 | (unsigned) atomic_read(&cache->stats.promotion), |
| 2531 | (unsigned long long) from_cblock(residency), |
| 2532 | cache->nr_dirty); |
| 2533 | |
| 2534 | if (cache->features.write_through) |
| 2535 | DMEMIT("1 writethrough "); |
| 2536 | else |
| 2537 | DMEMIT("0 "); |
| 2538 | |
| 2539 | DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold); |
| 2540 | if (sz < maxlen) { |
| 2541 | r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz); |
| 2542 | if (r) |
| 2543 | DMERR("policy_emit_config_values returned %d", r); |
| 2544 | } |
| 2545 | |
| 2546 | break; |
| 2547 | |
| 2548 | case STATUSTYPE_TABLE: |
| 2549 | format_dev_t(buf, cache->metadata_dev->bdev->bd_dev); |
| 2550 | DMEMIT("%s ", buf); |
| 2551 | format_dev_t(buf, cache->cache_dev->bdev->bd_dev); |
| 2552 | DMEMIT("%s ", buf); |
| 2553 | format_dev_t(buf, cache->origin_dev->bdev->bd_dev); |
| 2554 | DMEMIT("%s", buf); |
| 2555 | |
| 2556 | for (i = 0; i < cache->nr_ctr_args - 1; i++) |
| 2557 | DMEMIT(" %s", cache->ctr_args[i]); |
| 2558 | if (cache->nr_ctr_args) |
| 2559 | DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]); |
| 2560 | } |
| 2561 | |
| 2562 | return; |
| 2563 | |
| 2564 | err: |
| 2565 | DMEMIT("Error"); |
| 2566 | } |
| 2567 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2568 | /* |
| 2569 | * Supports <key> <value>. |
| 2570 | * |
| 2571 | * The key migration_threshold is supported by the cache target core. |
| 2572 | */ |
| 2573 | static int cache_message(struct dm_target *ti, unsigned argc, char **argv) |
| 2574 | { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2575 | struct cache *cache = ti->private; |
| 2576 | |
| 2577 | if (argc != 2) |
| 2578 | return -EINVAL; |
| 2579 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2580 | return set_config_value(cache, argv[0], argv[1]); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | static int cache_iterate_devices(struct dm_target *ti, |
| 2584 | iterate_devices_callout_fn fn, void *data) |
| 2585 | { |
| 2586 | int r = 0; |
| 2587 | struct cache *cache = ti->private; |
| 2588 | |
| 2589 | r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data); |
| 2590 | if (!r) |
| 2591 | r = fn(ti, cache->origin_dev, 0, ti->len, data); |
| 2592 | |
| 2593 | return r; |
| 2594 | } |
| 2595 | |
| 2596 | /* |
| 2597 | * We assume I/O is going to the origin (which is the volume |
| 2598 | * more likely to have restrictions e.g. by being striped). |
| 2599 | * (Looking up the exact location of the data would be expensive |
| 2600 | * and could always be out of date by the time the bio is submitted.) |
| 2601 | */ |
| 2602 | static int cache_bvec_merge(struct dm_target *ti, |
| 2603 | struct bvec_merge_data *bvm, |
| 2604 | struct bio_vec *biovec, int max_size) |
| 2605 | { |
| 2606 | struct cache *cache = ti->private; |
| 2607 | struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev); |
| 2608 | |
| 2609 | if (!q->merge_bvec_fn) |
| 2610 | return max_size; |
| 2611 | |
| 2612 | bvm->bi_bdev = cache->origin_dev->bdev; |
| 2613 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); |
| 2614 | } |
| 2615 | |
| 2616 | static void set_discard_limits(struct cache *cache, struct queue_limits *limits) |
| 2617 | { |
| 2618 | /* |
| 2619 | * FIXME: these limits may be incompatible with the cache device |
| 2620 | */ |
| 2621 | limits->max_discard_sectors = cache->discard_block_size * 1024; |
| 2622 | limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT; |
| 2623 | } |
| 2624 | |
| 2625 | static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits) |
| 2626 | { |
| 2627 | struct cache *cache = ti->private; |
Mike Snitzer | f610937 | 2013-08-20 15:02:41 -0400 | [diff] [blame] | 2628 | uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2629 | |
Mike Snitzer | f610937 | 2013-08-20 15:02:41 -0400 | [diff] [blame] | 2630 | /* |
| 2631 | * If the system-determined stacked limits are compatible with the |
| 2632 | * cache's blocksize (io_opt is a factor) do not override them. |
| 2633 | */ |
| 2634 | if (io_opt_sectors < cache->sectors_per_block || |
| 2635 | do_div(io_opt_sectors, cache->sectors_per_block)) { |
| 2636 | blk_limits_io_min(limits, 0); |
| 2637 | blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT); |
| 2638 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2639 | set_discard_limits(cache, limits); |
| 2640 | } |
| 2641 | |
| 2642 | /*----------------------------------------------------------------*/ |
| 2643 | |
| 2644 | static struct target_type cache_target = { |
| 2645 | .name = "cache", |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2646 | .version = {1, 1, 1}, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2647 | .module = THIS_MODULE, |
| 2648 | .ctr = cache_ctr, |
| 2649 | .dtr = cache_dtr, |
| 2650 | .map = cache_map, |
| 2651 | .end_io = cache_end_io, |
| 2652 | .postsuspend = cache_postsuspend, |
| 2653 | .preresume = cache_preresume, |
| 2654 | .resume = cache_resume, |
| 2655 | .status = cache_status, |
| 2656 | .message = cache_message, |
| 2657 | .iterate_devices = cache_iterate_devices, |
| 2658 | .merge = cache_bvec_merge, |
| 2659 | .io_hints = cache_io_hints, |
| 2660 | }; |
| 2661 | |
| 2662 | static int __init dm_cache_init(void) |
| 2663 | { |
| 2664 | int r; |
| 2665 | |
| 2666 | r = dm_register_target(&cache_target); |
| 2667 | if (r) { |
| 2668 | DMERR("cache target registration failed: %d", r); |
| 2669 | return r; |
| 2670 | } |
| 2671 | |
| 2672 | migration_cache = KMEM_CACHE(dm_cache_migration, 0); |
| 2673 | if (!migration_cache) { |
| 2674 | dm_unregister_target(&cache_target); |
| 2675 | return -ENOMEM; |
| 2676 | } |
| 2677 | |
| 2678 | return 0; |
| 2679 | } |
| 2680 | |
| 2681 | static void __exit dm_cache_exit(void) |
| 2682 | { |
| 2683 | dm_unregister_target(&cache_target); |
| 2684 | kmem_cache_destroy(migration_cache); |
| 2685 | } |
| 2686 | |
| 2687 | module_init(dm_cache_init); |
| 2688 | module_exit(dm_cache_exit); |
| 2689 | |
| 2690 | MODULE_DESCRIPTION(DM_NAME " cache target"); |
| 2691 | MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>"); |
| 2692 | MODULE_LICENSE("GPL"); |