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