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> |
Manuel Schölling | 0f30af9 | 2014-05-22 22:42:37 +0200 | [diff] [blame^] | 14 | #include <linux/jiffies.h> |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/mempool.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/vmalloc.h> |
| 20 | |
| 21 | #define DM_MSG_PREFIX "cache" |
| 22 | |
| 23 | DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle, |
| 24 | "A percentage of time allocated for copying to and/or from cache"); |
| 25 | |
| 26 | /*----------------------------------------------------------------*/ |
| 27 | |
| 28 | /* |
| 29 | * Glossary: |
| 30 | * |
| 31 | * oblock: index of an origin block |
| 32 | * cblock: index of a cache block |
| 33 | * promotion: movement of a block from origin to cache |
| 34 | * demotion: movement of a block from cache to origin |
| 35 | * migration: movement of a block between the origin and cache device, |
| 36 | * either direction |
| 37 | */ |
| 38 | |
| 39 | /*----------------------------------------------------------------*/ |
| 40 | |
| 41 | static size_t bitset_size_in_bytes(unsigned nr_entries) |
| 42 | { |
| 43 | return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG); |
| 44 | } |
| 45 | |
| 46 | static unsigned long *alloc_bitset(unsigned nr_entries) |
| 47 | { |
| 48 | size_t s = bitset_size_in_bytes(nr_entries); |
| 49 | return vzalloc(s); |
| 50 | } |
| 51 | |
| 52 | static void clear_bitset(void *bitset, unsigned nr_entries) |
| 53 | { |
| 54 | size_t s = bitset_size_in_bytes(nr_entries); |
| 55 | memset(bitset, 0, s); |
| 56 | } |
| 57 | |
| 58 | static void free_bitset(unsigned long *bits) |
| 59 | { |
| 60 | vfree(bits); |
| 61 | } |
| 62 | |
| 63 | /*----------------------------------------------------------------*/ |
| 64 | |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 65 | /* |
| 66 | * There are a couple of places where we let a bio run, but want to do some |
| 67 | * work before calling its endio function. We do this by temporarily |
| 68 | * changing the endio fn. |
| 69 | */ |
| 70 | struct dm_hook_info { |
| 71 | bio_end_io_t *bi_end_io; |
| 72 | void *bi_private; |
| 73 | }; |
| 74 | |
| 75 | static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio, |
| 76 | bio_end_io_t *bi_end_io, void *bi_private) |
| 77 | { |
| 78 | h->bi_end_io = bio->bi_end_io; |
| 79 | h->bi_private = bio->bi_private; |
| 80 | |
| 81 | bio->bi_end_io = bi_end_io; |
| 82 | bio->bi_private = bi_private; |
| 83 | } |
| 84 | |
| 85 | static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio) |
| 86 | { |
| 87 | bio->bi_end_io = h->bi_end_io; |
| 88 | bio->bi_private = h->bi_private; |
Mike Snitzer | 8d30726 | 2013-12-03 19:16:04 -0700 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Must bump bi_remaining to allow bio to complete with |
| 92 | * restored bi_end_io. |
| 93 | */ |
| 94 | atomic_inc(&bio->bi_remaining); |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /*----------------------------------------------------------------*/ |
| 98 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 99 | #define MIGRATION_POOL_SIZE 128 |
| 100 | #define COMMIT_PERIOD HZ |
| 101 | #define MIGRATION_COUNT_WINDOW 10 |
| 102 | |
| 103 | /* |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 104 | * The block size of the device holding cache data must be |
| 105 | * between 32KB and 1GB. |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 106 | */ |
| 107 | #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT) |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 108 | #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * FIXME: the cache is read/write for the time being. |
| 112 | */ |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 113 | enum cache_metadata_mode { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 114 | CM_WRITE, /* metadata may be changed */ |
| 115 | CM_READ_ONLY, /* metadata may not be changed */ |
| 116 | }; |
| 117 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 118 | enum cache_io_mode { |
| 119 | /* |
| 120 | * Data is written to cached blocks only. These blocks are marked |
| 121 | * dirty. If you lose the cache device you will lose data. |
| 122 | * Potential performance increase for both reads and writes. |
| 123 | */ |
| 124 | CM_IO_WRITEBACK, |
| 125 | |
| 126 | /* |
| 127 | * Data is written to both cache and origin. Blocks are never |
| 128 | * dirty. Potential performance benfit for reads only. |
| 129 | */ |
| 130 | CM_IO_WRITETHROUGH, |
| 131 | |
| 132 | /* |
| 133 | * A degraded mode useful for various cache coherency situations |
| 134 | * (eg, rolling back snapshots). Reads and writes always go to the |
| 135 | * origin. If a write goes to a cached oblock, then the cache |
| 136 | * block is invalidated. |
| 137 | */ |
| 138 | CM_IO_PASSTHROUGH |
| 139 | }; |
| 140 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 141 | struct cache_features { |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 142 | enum cache_metadata_mode mode; |
| 143 | enum cache_io_mode io_mode; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | struct cache_stats { |
| 147 | atomic_t read_hit; |
| 148 | atomic_t read_miss; |
| 149 | atomic_t write_hit; |
| 150 | atomic_t write_miss; |
| 151 | atomic_t demotion; |
| 152 | atomic_t promotion; |
| 153 | atomic_t copies_avoided; |
| 154 | atomic_t cache_cell_clash; |
| 155 | atomic_t commit_count; |
| 156 | atomic_t discard_count; |
| 157 | }; |
| 158 | |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 159 | /* |
| 160 | * Defines a range of cblocks, begin to (end - 1) are in the range. end is |
| 161 | * the one-past-the-end value. |
| 162 | */ |
| 163 | struct cblock_range { |
| 164 | dm_cblock_t begin; |
| 165 | dm_cblock_t end; |
| 166 | }; |
| 167 | |
| 168 | struct invalidation_request { |
| 169 | struct list_head list; |
| 170 | struct cblock_range *cblocks; |
| 171 | |
| 172 | atomic_t complete; |
| 173 | int err; |
| 174 | |
| 175 | wait_queue_head_t result_wait; |
| 176 | }; |
| 177 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 178 | struct cache { |
| 179 | struct dm_target *ti; |
| 180 | struct dm_target_callbacks callbacks; |
| 181 | |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 182 | struct dm_cache_metadata *cmd; |
| 183 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Metadata is written to this device. |
| 186 | */ |
| 187 | struct dm_dev *metadata_dev; |
| 188 | |
| 189 | /* |
| 190 | * The slower of the two data devices. Typically a spindle. |
| 191 | */ |
| 192 | struct dm_dev *origin_dev; |
| 193 | |
| 194 | /* |
| 195 | * The faster of the two data devices. Typically an SSD. |
| 196 | */ |
| 197 | struct dm_dev *cache_dev; |
| 198 | |
| 199 | /* |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 200 | * Size of the origin device in _complete_ blocks and native sectors. |
| 201 | */ |
| 202 | dm_oblock_t origin_blocks; |
| 203 | sector_t origin_sectors; |
| 204 | |
| 205 | /* |
| 206 | * Size of the cache device in blocks. |
| 207 | */ |
| 208 | dm_cblock_t cache_size; |
| 209 | |
| 210 | /* |
| 211 | * Fields for converting from sectors to blocks. |
| 212 | */ |
| 213 | uint32_t sectors_per_block; |
| 214 | int sectors_per_block_shift; |
| 215 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 216 | spinlock_t lock; |
| 217 | struct bio_list deferred_bios; |
| 218 | struct bio_list deferred_flush_bios; |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 219 | struct bio_list deferred_writethrough_bios; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 220 | struct list_head quiesced_migrations; |
| 221 | struct list_head completed_migrations; |
| 222 | struct list_head need_commit_migrations; |
| 223 | sector_t migration_threshold; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 224 | wait_queue_head_t migration_wait; |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 225 | atomic_t nr_migrations; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 226 | |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 227 | wait_queue_head_t quiescing_wait; |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 228 | atomic_t quiescing; |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 229 | atomic_t quiescing_ack; |
| 230 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 231 | /* |
| 232 | * cache_size entries, dirty if set |
| 233 | */ |
Anssi Hannula | 44fa816 | 2014-08-01 11:55:47 -0400 | [diff] [blame] | 234 | atomic_t nr_dirty; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 235 | unsigned long *dirty_bitset; |
| 236 | |
| 237 | /* |
| 238 | * origin_blocks entries, discarded if set. |
| 239 | */ |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 240 | dm_dblock_t discard_nr_blocks; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 241 | unsigned long *discard_bitset; |
Joe Thornber | 08b1845 | 2014-11-06 14:38:01 +0000 | [diff] [blame] | 242 | uint32_t discard_block_size; /* a power of 2 times sectors per block */ |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * Rather than reconstructing the table line for the status we just |
| 246 | * save it and regurgitate. |
| 247 | */ |
| 248 | unsigned nr_ctr_args; |
| 249 | const char **ctr_args; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 250 | |
| 251 | struct dm_kcopyd_client *copier; |
| 252 | struct workqueue_struct *wq; |
| 253 | struct work_struct worker; |
| 254 | |
| 255 | struct delayed_work waker; |
| 256 | unsigned long last_commit_jiffies; |
| 257 | |
| 258 | struct dm_bio_prison *prison; |
| 259 | struct dm_deferred_set *all_io_ds; |
| 260 | |
| 261 | mempool_t *migration_pool; |
| 262 | struct dm_cache_migration *next_migration; |
| 263 | |
| 264 | struct dm_cache_policy *policy; |
| 265 | unsigned policy_nr_args; |
| 266 | |
| 267 | bool need_tick_bio:1; |
| 268 | bool sized:1; |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 269 | bool invalidate:1; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 270 | bool commit_requested:1; |
| 271 | bool loaded_mappings:1; |
| 272 | bool loaded_discards:1; |
| 273 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 274 | /* |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 275 | * Cache features such as write-through. |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 276 | */ |
Mike Snitzer | c9ec5d7 | 2013-08-16 10:54:21 -0400 | [diff] [blame] | 277 | struct cache_features features; |
| 278 | |
| 279 | struct cache_stats stats; |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * Invalidation fields. |
| 283 | */ |
| 284 | spinlock_t invalidation_lock; |
| 285 | struct list_head invalidation_requests; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | struct per_bio_data { |
| 289 | bool tick:1; |
| 290 | unsigned req_nr:2; |
| 291 | struct dm_deferred_entry *all_io_entry; |
Mike Snitzer | c6eda5e | 2014-01-31 14:11:54 -0500 | [diff] [blame] | 292 | struct dm_hook_info hook_info; |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 293 | |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 294 | /* |
| 295 | * writethrough fields. These MUST remain at the end of this |
| 296 | * structure and the 'cache' member must be the first as it |
Joe Thornber | aeed142 | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 297 | * is used to determine the offset of the writethrough fields. |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 298 | */ |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 299 | struct cache *cache; |
| 300 | dm_cblock_t cblock; |
Darrick J. Wong | b844fe6 | 2013-04-05 15:36:32 +0100 | [diff] [blame] | 301 | struct dm_bio_details bio_details; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | struct dm_cache_migration { |
| 305 | struct list_head list; |
| 306 | struct cache *cache; |
| 307 | |
| 308 | unsigned long start_jiffies; |
| 309 | dm_oblock_t old_oblock; |
| 310 | dm_oblock_t new_oblock; |
| 311 | dm_cblock_t cblock; |
| 312 | |
| 313 | bool err:1; |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 314 | bool discard:1; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 315 | bool writeback:1; |
| 316 | bool demote:1; |
| 317 | bool promote:1; |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 318 | bool requeue_holder:1; |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 319 | bool invalidate:1; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 320 | |
| 321 | struct dm_bio_prison_cell *old_ocell; |
| 322 | struct dm_bio_prison_cell *new_ocell; |
| 323 | }; |
| 324 | |
| 325 | /* |
| 326 | * Processing a bio in the worker thread may require these memory |
| 327 | * allocations. We prealloc to avoid deadlocks (the same worker thread |
| 328 | * frees them back to the mempool). |
| 329 | */ |
| 330 | struct prealloc { |
| 331 | struct dm_cache_migration *mg; |
| 332 | struct dm_bio_prison_cell *cell1; |
| 333 | struct dm_bio_prison_cell *cell2; |
| 334 | }; |
| 335 | |
| 336 | static void wake_worker(struct cache *cache) |
| 337 | { |
| 338 | queue_work(cache->wq, &cache->worker); |
| 339 | } |
| 340 | |
| 341 | /*----------------------------------------------------------------*/ |
| 342 | |
| 343 | static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache) |
| 344 | { |
| 345 | /* FIXME: change to use a local slab. */ |
| 346 | return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT); |
| 347 | } |
| 348 | |
| 349 | static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell) |
| 350 | { |
| 351 | dm_bio_prison_free_cell(cache->prison, cell); |
| 352 | } |
| 353 | |
| 354 | static int prealloc_data_structs(struct cache *cache, struct prealloc *p) |
| 355 | { |
| 356 | if (!p->mg) { |
| 357 | p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT); |
| 358 | if (!p->mg) |
| 359 | return -ENOMEM; |
| 360 | } |
| 361 | |
| 362 | if (!p->cell1) { |
| 363 | p->cell1 = alloc_prison_cell(cache); |
| 364 | if (!p->cell1) |
| 365 | return -ENOMEM; |
| 366 | } |
| 367 | |
| 368 | if (!p->cell2) { |
| 369 | p->cell2 = alloc_prison_cell(cache); |
| 370 | if (!p->cell2) |
| 371 | return -ENOMEM; |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static void prealloc_free_structs(struct cache *cache, struct prealloc *p) |
| 378 | { |
| 379 | if (p->cell2) |
| 380 | free_prison_cell(cache, p->cell2); |
| 381 | |
| 382 | if (p->cell1) |
| 383 | free_prison_cell(cache, p->cell1); |
| 384 | |
| 385 | if (p->mg) |
| 386 | mempool_free(p->mg, cache->migration_pool); |
| 387 | } |
| 388 | |
| 389 | static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p) |
| 390 | { |
| 391 | struct dm_cache_migration *mg = p->mg; |
| 392 | |
| 393 | BUG_ON(!mg); |
| 394 | p->mg = NULL; |
| 395 | |
| 396 | return mg; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * You must have a cell within the prealloc struct to return. If not this |
| 401 | * function will BUG() rather than returning NULL. |
| 402 | */ |
| 403 | static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p) |
| 404 | { |
| 405 | struct dm_bio_prison_cell *r = NULL; |
| 406 | |
| 407 | if (p->cell1) { |
| 408 | r = p->cell1; |
| 409 | p->cell1 = NULL; |
| 410 | |
| 411 | } else if (p->cell2) { |
| 412 | r = p->cell2; |
| 413 | p->cell2 = NULL; |
| 414 | } else |
| 415 | BUG(); |
| 416 | |
| 417 | return r; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * You can't have more than two cells in a prealloc struct. BUG() will be |
| 422 | * called if you try and overfill. |
| 423 | */ |
| 424 | static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell) |
| 425 | { |
| 426 | if (!p->cell2) |
| 427 | p->cell2 = cell; |
| 428 | |
| 429 | else if (!p->cell1) |
| 430 | p->cell1 = cell; |
| 431 | |
| 432 | else |
| 433 | BUG(); |
| 434 | } |
| 435 | |
| 436 | /*----------------------------------------------------------------*/ |
| 437 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 438 | static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key *key) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 439 | { |
| 440 | key->virtual = 0; |
| 441 | key->dev = 0; |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 442 | key->block_begin = from_oblock(begin); |
| 443 | key->block_end = from_oblock(end); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * The caller hands in a preallocated cell, and a free function for it. |
| 448 | * The cell will be freed if there's an error, or if it wasn't used because |
| 449 | * a cell with that key already exists. |
| 450 | */ |
| 451 | typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell); |
| 452 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 453 | static int bio_detain_range(struct cache *cache, dm_oblock_t oblock_begin, dm_oblock_t oblock_end, |
| 454 | struct bio *bio, struct dm_bio_prison_cell *cell_prealloc, |
| 455 | cell_free_fn free_fn, void *free_context, |
| 456 | struct dm_bio_prison_cell **cell_result) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 457 | { |
| 458 | int r; |
| 459 | struct dm_cell_key key; |
| 460 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 461 | build_key(oblock_begin, oblock_end, &key); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 462 | r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result); |
| 463 | if (r) |
| 464 | free_fn(free_context, cell_prealloc); |
| 465 | |
| 466 | return r; |
| 467 | } |
| 468 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 469 | static int bio_detain(struct cache *cache, dm_oblock_t oblock, |
| 470 | struct bio *bio, struct dm_bio_prison_cell *cell_prealloc, |
| 471 | cell_free_fn free_fn, void *free_context, |
| 472 | struct dm_bio_prison_cell **cell_result) |
| 473 | { |
| 474 | dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL); |
| 475 | return bio_detain_range(cache, oblock, end, bio, |
| 476 | cell_prealloc, free_fn, free_context, cell_result); |
| 477 | } |
| 478 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 479 | static int get_cell(struct cache *cache, |
| 480 | dm_oblock_t oblock, |
| 481 | struct prealloc *structs, |
| 482 | struct dm_bio_prison_cell **cell_result) |
| 483 | { |
| 484 | int r; |
| 485 | struct dm_cell_key key; |
| 486 | struct dm_bio_prison_cell *cell_prealloc; |
| 487 | |
| 488 | cell_prealloc = prealloc_get_cell(structs); |
| 489 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 490 | build_key(oblock, to_oblock(from_oblock(oblock) + 1ULL), &key); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 491 | r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result); |
| 492 | if (r) |
| 493 | prealloc_put_cell(structs, cell_prealloc); |
| 494 | |
| 495 | return r; |
| 496 | } |
| 497 | |
Joe Thornber | aeed142 | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 498 | /*----------------------------------------------------------------*/ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 499 | |
| 500 | static bool is_dirty(struct cache *cache, dm_cblock_t b) |
| 501 | { |
| 502 | return test_bit(from_cblock(b), cache->dirty_bitset); |
| 503 | } |
| 504 | |
| 505 | static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock) |
| 506 | { |
| 507 | if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) { |
Anssi Hannula | 44fa816 | 2014-08-01 11:55:47 -0400 | [diff] [blame] | 508 | atomic_inc(&cache->nr_dirty); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 509 | policy_set_dirty(cache->policy, oblock); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock) |
| 514 | { |
| 515 | if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) { |
| 516 | policy_clear_dirty(cache->policy, oblock); |
Anssi Hannula | 44fa816 | 2014-08-01 11:55:47 -0400 | [diff] [blame] | 517 | if (atomic_dec_return(&cache->nr_dirty) == 0) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 518 | dm_table_event(cache->ti->table); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /*----------------------------------------------------------------*/ |
Joe Thornber | aeed142 | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 523 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 524 | static bool block_size_is_power_of_two(struct cache *cache) |
| 525 | { |
| 526 | return cache->sectors_per_block_shift >= 0; |
| 527 | } |
| 528 | |
Mikulas Patocka | 43aeaa2 | 2013-07-10 23:41:17 +0100 | [diff] [blame] | 529 | /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */ |
| 530 | #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6 |
| 531 | __always_inline |
| 532 | #endif |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 533 | static dm_block_t block_div(dm_block_t b, uint32_t n) |
| 534 | { |
| 535 | do_div(b, n); |
| 536 | |
| 537 | return b; |
| 538 | } |
| 539 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 540 | static dm_block_t oblocks_per_dblock(struct cache *cache) |
| 541 | { |
| 542 | dm_block_t oblocks = cache->discard_block_size; |
| 543 | |
| 544 | if (block_size_is_power_of_two(cache)) |
| 545 | oblocks >>= cache->sectors_per_block_shift; |
| 546 | else |
| 547 | oblocks = block_div(oblocks, cache->sectors_per_block); |
| 548 | |
| 549 | return oblocks; |
| 550 | } |
| 551 | |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 552 | static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock) |
| 553 | { |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 554 | return to_dblock(block_div(from_oblock(oblock), |
| 555 | oblocks_per_dblock(cache))); |
| 556 | } |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 557 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 558 | static dm_oblock_t dblock_to_oblock(struct cache *cache, dm_dblock_t dblock) |
| 559 | { |
| 560 | return to_oblock(from_dblock(dblock) * oblocks_per_dblock(cache)); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static void set_discard(struct cache *cache, dm_dblock_t b) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 564 | { |
| 565 | unsigned long flags; |
| 566 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 567 | BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 568 | atomic_inc(&cache->stats.discard_count); |
| 569 | |
| 570 | spin_lock_irqsave(&cache->lock, flags); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 571 | set_bit(from_dblock(b), cache->discard_bitset); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 572 | spin_unlock_irqrestore(&cache->lock, flags); |
| 573 | } |
| 574 | |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 575 | static void clear_discard(struct cache *cache, dm_dblock_t b) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 576 | { |
| 577 | unsigned long flags; |
| 578 | |
| 579 | spin_lock_irqsave(&cache->lock, flags); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 580 | clear_bit(from_dblock(b), cache->discard_bitset); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 581 | spin_unlock_irqrestore(&cache->lock, flags); |
| 582 | } |
| 583 | |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 584 | static bool is_discarded(struct cache *cache, dm_dblock_t b) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 585 | { |
| 586 | int r; |
| 587 | unsigned long flags; |
| 588 | |
| 589 | spin_lock_irqsave(&cache->lock, flags); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 590 | r = test_bit(from_dblock(b), cache->discard_bitset); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 591 | spin_unlock_irqrestore(&cache->lock, flags); |
| 592 | |
| 593 | return r; |
| 594 | } |
| 595 | |
| 596 | static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b) |
| 597 | { |
| 598 | int r; |
| 599 | unsigned long flags; |
| 600 | |
| 601 | spin_lock_irqsave(&cache->lock, flags); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 602 | r = test_bit(from_dblock(oblock_to_dblock(cache, b)), |
| 603 | cache->discard_bitset); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 604 | spin_unlock_irqrestore(&cache->lock, flags); |
| 605 | |
| 606 | return r; |
| 607 | } |
| 608 | |
| 609 | /*----------------------------------------------------------------*/ |
| 610 | |
| 611 | static void load_stats(struct cache *cache) |
| 612 | { |
| 613 | struct dm_cache_statistics stats; |
| 614 | |
| 615 | dm_cache_metadata_get_stats(cache->cmd, &stats); |
| 616 | atomic_set(&cache->stats.read_hit, stats.read_hits); |
| 617 | atomic_set(&cache->stats.read_miss, stats.read_misses); |
| 618 | atomic_set(&cache->stats.write_hit, stats.write_hits); |
| 619 | atomic_set(&cache->stats.write_miss, stats.write_misses); |
| 620 | } |
| 621 | |
| 622 | static void save_stats(struct cache *cache) |
| 623 | { |
| 624 | struct dm_cache_statistics stats; |
| 625 | |
| 626 | stats.read_hits = atomic_read(&cache->stats.read_hit); |
| 627 | stats.read_misses = atomic_read(&cache->stats.read_miss); |
| 628 | stats.write_hits = atomic_read(&cache->stats.write_hit); |
| 629 | stats.write_misses = atomic_read(&cache->stats.write_miss); |
| 630 | |
| 631 | dm_cache_metadata_set_stats(cache->cmd, &stats); |
| 632 | } |
| 633 | |
| 634 | /*---------------------------------------------------------------- |
| 635 | * Per bio data |
| 636 | *--------------------------------------------------------------*/ |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * If using writeback, leave out struct per_bio_data's writethrough fields. |
| 640 | */ |
| 641 | #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache)) |
| 642 | #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data)) |
| 643 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 644 | static bool writethrough_mode(struct cache_features *f) |
| 645 | { |
| 646 | return f->io_mode == CM_IO_WRITETHROUGH; |
| 647 | } |
| 648 | |
| 649 | static bool writeback_mode(struct cache_features *f) |
| 650 | { |
| 651 | return f->io_mode == CM_IO_WRITEBACK; |
| 652 | } |
| 653 | |
| 654 | static bool passthrough_mode(struct cache_features *f) |
| 655 | { |
| 656 | return f->io_mode == CM_IO_PASSTHROUGH; |
| 657 | } |
| 658 | |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 659 | static size_t get_per_bio_data_size(struct cache *cache) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 660 | { |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 661 | return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB; |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size) |
| 665 | { |
| 666 | struct per_bio_data *pb = dm_per_bio_data(bio, data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 667 | BUG_ON(!pb); |
| 668 | return pb; |
| 669 | } |
| 670 | |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 671 | 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] | 672 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 673 | struct per_bio_data *pb = get_per_bio_data(bio, data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 674 | |
| 675 | pb->tick = false; |
| 676 | pb->req_nr = dm_bio_get_target_bio_nr(bio); |
| 677 | pb->all_io_entry = NULL; |
| 678 | |
| 679 | return pb; |
| 680 | } |
| 681 | |
| 682 | /*---------------------------------------------------------------- |
| 683 | * Remapping |
| 684 | *--------------------------------------------------------------*/ |
| 685 | static void remap_to_origin(struct cache *cache, struct bio *bio) |
| 686 | { |
| 687 | bio->bi_bdev = cache->origin_dev->bdev; |
| 688 | } |
| 689 | |
| 690 | static void remap_to_cache(struct cache *cache, struct bio *bio, |
| 691 | dm_cblock_t cblock) |
| 692 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 693 | sector_t bi_sector = bio->bi_iter.bi_sector; |
Heinz Mauelshagen | e0d849f | 2014-02-27 22:46:48 +0100 | [diff] [blame] | 694 | sector_t block = from_cblock(cblock); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 695 | |
| 696 | bio->bi_bdev = cache->cache_dev->bdev; |
| 697 | if (!block_size_is_power_of_two(cache)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 698 | bio->bi_iter.bi_sector = |
Heinz Mauelshagen | e0d849f | 2014-02-27 22:46:48 +0100 | [diff] [blame] | 699 | (block * cache->sectors_per_block) + |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 700 | sector_div(bi_sector, cache->sectors_per_block); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 701 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 702 | bio->bi_iter.bi_sector = |
Heinz Mauelshagen | e0d849f | 2014-02-27 22:46:48 +0100 | [diff] [blame] | 703 | (block << cache->sectors_per_block_shift) | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 704 | (bi_sector & (cache->sectors_per_block - 1)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio) |
| 708 | { |
| 709 | unsigned long flags; |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 710 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 711 | 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] | 712 | |
| 713 | spin_lock_irqsave(&cache->lock, flags); |
| 714 | if (cache->need_tick_bio && |
| 715 | !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) { |
| 716 | pb->tick = true; |
| 717 | cache->need_tick_bio = false; |
| 718 | } |
| 719 | spin_unlock_irqrestore(&cache->lock, flags); |
| 720 | } |
| 721 | |
| 722 | static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio, |
| 723 | dm_oblock_t oblock) |
| 724 | { |
| 725 | check_if_tick_bio_needed(cache, bio); |
| 726 | remap_to_origin(cache, bio); |
| 727 | if (bio_data_dir(bio) == WRITE) |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 728 | clear_discard(cache, oblock_to_dblock(cache, oblock)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | static void remap_to_cache_dirty(struct cache *cache, struct bio *bio, |
| 732 | dm_oblock_t oblock, dm_cblock_t cblock) |
| 733 | { |
Joe Thornber | f8e5f01 | 2013-10-21 12:51:45 +0100 | [diff] [blame] | 734 | check_if_tick_bio_needed(cache, bio); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 735 | remap_to_cache(cache, bio, cblock); |
| 736 | if (bio_data_dir(bio) == WRITE) { |
| 737 | set_dirty(cache, oblock, cblock); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 738 | clear_discard(cache, oblock_to_dblock(cache, oblock)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio) |
| 743 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 744 | sector_t block_nr = bio->bi_iter.bi_sector; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 745 | |
| 746 | if (!block_size_is_power_of_two(cache)) |
| 747 | (void) sector_div(block_nr, cache->sectors_per_block); |
| 748 | else |
| 749 | block_nr >>= cache->sectors_per_block_shift; |
| 750 | |
| 751 | return to_oblock(block_nr); |
| 752 | } |
| 753 | |
| 754 | static int bio_triggers_commit(struct cache *cache, struct bio *bio) |
| 755 | { |
| 756 | return bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 757 | } |
| 758 | |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 759 | /* |
| 760 | * You must increment the deferred set whilst the prison cell is held. To |
| 761 | * encourage this, we ask for 'cell' to be passed in. |
| 762 | */ |
| 763 | static void inc_ds(struct cache *cache, struct bio *bio, |
| 764 | struct dm_bio_prison_cell *cell) |
| 765 | { |
| 766 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 767 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); |
| 768 | |
| 769 | BUG_ON(!cell); |
| 770 | BUG_ON(pb->all_io_entry); |
| 771 | |
| 772 | pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds); |
| 773 | } |
| 774 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 775 | static void issue(struct cache *cache, struct bio *bio) |
| 776 | { |
| 777 | unsigned long flags; |
| 778 | |
| 779 | if (!bio_triggers_commit(cache, bio)) { |
| 780 | generic_make_request(bio); |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Batch together any bios that trigger commits and then issue a |
| 786 | * single commit for them in do_worker(). |
| 787 | */ |
| 788 | spin_lock_irqsave(&cache->lock, flags); |
| 789 | cache->commit_requested = true; |
| 790 | bio_list_add(&cache->deferred_flush_bios, bio); |
| 791 | spin_unlock_irqrestore(&cache->lock, flags); |
| 792 | } |
| 793 | |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 794 | static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell) |
| 795 | { |
| 796 | inc_ds(cache, bio, cell); |
| 797 | issue(cache, bio); |
| 798 | } |
| 799 | |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 800 | static void defer_writethrough_bio(struct cache *cache, struct bio *bio) |
| 801 | { |
| 802 | unsigned long flags; |
| 803 | |
| 804 | spin_lock_irqsave(&cache->lock, flags); |
| 805 | bio_list_add(&cache->deferred_writethrough_bios, bio); |
| 806 | spin_unlock_irqrestore(&cache->lock, flags); |
| 807 | |
| 808 | wake_worker(cache); |
| 809 | } |
| 810 | |
| 811 | static void writethrough_endio(struct bio *bio, int err) |
| 812 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 813 | struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT); |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 814 | |
| 815 | dm_unhook_bio(&pb->hook_info, bio); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 816 | |
| 817 | if (err) { |
| 818 | bio_endio(bio, err); |
| 819 | return; |
| 820 | } |
| 821 | |
Darrick J. Wong | b844fe6 | 2013-04-05 15:36:32 +0100 | [diff] [blame] | 822 | dm_bio_restore(&pb->bio_details, bio); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 823 | remap_to_cache(pb->cache, bio, pb->cblock); |
| 824 | |
| 825 | /* |
| 826 | * We can't issue this bio directly, since we're in interrupt |
Joe Thornber | aeed142 | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 827 | * 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] | 828 | * worker thread. |
| 829 | */ |
| 830 | defer_writethrough_bio(pb->cache, bio); |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * When running in writethrough mode we need to send writes to clean blocks |
| 835 | * to both the cache and origin devices. In future we'd like to clone the |
| 836 | * bio and send them in parallel, but for now we're doing them in |
| 837 | * series as this is easier. |
| 838 | */ |
| 839 | static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio, |
| 840 | dm_oblock_t oblock, dm_cblock_t cblock) |
| 841 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 842 | 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] | 843 | |
| 844 | pb->cache = cache; |
| 845 | pb->cblock = cblock; |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 846 | dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL); |
Darrick J. Wong | b844fe6 | 2013-04-05 15:36:32 +0100 | [diff] [blame] | 847 | dm_bio_record(&pb->bio_details, bio); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 848 | |
| 849 | remap_to_origin_clear_discard(pb->cache, bio, oblock); |
| 850 | } |
| 851 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 852 | /*---------------------------------------------------------------- |
| 853 | * Migration processing |
| 854 | * |
| 855 | * Migration covers moving data from the origin device to the cache, or |
| 856 | * vice versa. |
| 857 | *--------------------------------------------------------------*/ |
| 858 | static void free_migration(struct dm_cache_migration *mg) |
| 859 | { |
| 860 | mempool_free(mg, mg->cache->migration_pool); |
| 861 | } |
| 862 | |
| 863 | static void inc_nr_migrations(struct cache *cache) |
| 864 | { |
| 865 | atomic_inc(&cache->nr_migrations); |
| 866 | } |
| 867 | |
| 868 | static void dec_nr_migrations(struct cache *cache) |
| 869 | { |
| 870 | atomic_dec(&cache->nr_migrations); |
| 871 | |
| 872 | /* |
| 873 | * Wake the worker in case we're suspending the target. |
| 874 | */ |
| 875 | wake_up(&cache->migration_wait); |
| 876 | } |
| 877 | |
| 878 | static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, |
| 879 | bool holder) |
| 880 | { |
| 881 | (holder ? dm_cell_release : dm_cell_release_no_holder) |
| 882 | (cache->prison, cell, &cache->deferred_bios); |
| 883 | free_prison_cell(cache, cell); |
| 884 | } |
| 885 | |
| 886 | static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, |
| 887 | bool holder) |
| 888 | { |
| 889 | unsigned long flags; |
| 890 | |
| 891 | spin_lock_irqsave(&cache->lock, flags); |
| 892 | __cell_defer(cache, cell, holder); |
| 893 | spin_unlock_irqrestore(&cache->lock, flags); |
| 894 | |
| 895 | wake_worker(cache); |
| 896 | } |
| 897 | |
| 898 | static void cleanup_migration(struct dm_cache_migration *mg) |
| 899 | { |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 900 | struct cache *cache = mg->cache; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 901 | free_migration(mg); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 902 | dec_nr_migrations(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static void migration_failure(struct dm_cache_migration *mg) |
| 906 | { |
| 907 | struct cache *cache = mg->cache; |
| 908 | |
| 909 | if (mg->writeback) { |
| 910 | DMWARN_LIMIT("writeback failed; couldn't copy block"); |
| 911 | set_dirty(cache, mg->old_oblock, mg->cblock); |
| 912 | cell_defer(cache, mg->old_ocell, false); |
| 913 | |
| 914 | } else if (mg->demote) { |
| 915 | DMWARN_LIMIT("demotion failed; couldn't copy block"); |
| 916 | policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock); |
| 917 | |
Heinz Mauelshagen | 80f659f | 2013-10-14 17:10:47 +0200 | [diff] [blame] | 918 | cell_defer(cache, mg->old_ocell, mg->promote ? false : true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 919 | if (mg->promote) |
Heinz Mauelshagen | 80f659f | 2013-10-14 17:10:47 +0200 | [diff] [blame] | 920 | cell_defer(cache, mg->new_ocell, true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 921 | } else { |
| 922 | DMWARN_LIMIT("promotion failed; couldn't copy block"); |
| 923 | policy_remove_mapping(cache->policy, mg->new_oblock); |
Heinz Mauelshagen | 80f659f | 2013-10-14 17:10:47 +0200 | [diff] [blame] | 924 | cell_defer(cache, mg->new_ocell, true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | cleanup_migration(mg); |
| 928 | } |
| 929 | |
| 930 | static void migration_success_pre_commit(struct dm_cache_migration *mg) |
| 931 | { |
| 932 | unsigned long flags; |
| 933 | struct cache *cache = mg->cache; |
| 934 | |
| 935 | if (mg->writeback) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 936 | clear_dirty(cache, mg->old_oblock, mg->cblock); |
Anssi Hannula | 40aa978 | 2014-09-05 03:11:28 +0300 | [diff] [blame] | 937 | cell_defer(cache, mg->old_ocell, false); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 938 | cleanup_migration(mg); |
| 939 | return; |
| 940 | |
| 941 | } else if (mg->demote) { |
| 942 | if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) { |
| 943 | DMWARN_LIMIT("demotion failed; couldn't update on disk metadata"); |
| 944 | policy_force_mapping(cache->policy, mg->new_oblock, |
| 945 | mg->old_oblock); |
| 946 | if (mg->promote) |
| 947 | cell_defer(cache, mg->new_ocell, true); |
| 948 | cleanup_migration(mg); |
| 949 | return; |
| 950 | } |
| 951 | } else { |
| 952 | if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) { |
| 953 | DMWARN_LIMIT("promotion failed; couldn't update on disk metadata"); |
| 954 | policy_remove_mapping(cache->policy, mg->new_oblock); |
| 955 | cleanup_migration(mg); |
| 956 | return; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | spin_lock_irqsave(&cache->lock, flags); |
| 961 | list_add_tail(&mg->list, &cache->need_commit_migrations); |
| 962 | cache->commit_requested = true; |
| 963 | spin_unlock_irqrestore(&cache->lock, flags); |
| 964 | } |
| 965 | |
| 966 | static void migration_success_post_commit(struct dm_cache_migration *mg) |
| 967 | { |
| 968 | unsigned long flags; |
| 969 | struct cache *cache = mg->cache; |
| 970 | |
| 971 | if (mg->writeback) { |
| 972 | DMWARN("writeback unexpectedly triggered commit"); |
| 973 | return; |
| 974 | |
| 975 | } else if (mg->demote) { |
Heinz Mauelshagen | 80f659f | 2013-10-14 17:10:47 +0200 | [diff] [blame] | 976 | cell_defer(cache, mg->old_ocell, mg->promote ? false : true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 977 | |
| 978 | if (mg->promote) { |
| 979 | mg->demote = false; |
| 980 | |
| 981 | spin_lock_irqsave(&cache->lock, flags); |
| 982 | list_add_tail(&mg->list, &cache->quiesced_migrations); |
| 983 | spin_unlock_irqrestore(&cache->lock, flags); |
| 984 | |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 985 | } else { |
| 986 | if (mg->invalidate) |
| 987 | policy_remove_mapping(cache->policy, mg->old_oblock); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 988 | cleanup_migration(mg); |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 989 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 990 | |
| 991 | } else { |
Joe Thornber | 1e32134 | 2014-11-27 12:26:46 +0000 | [diff] [blame] | 992 | if (mg->requeue_holder) { |
| 993 | clear_dirty(cache, mg->new_oblock, mg->cblock); |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 994 | cell_defer(cache, mg->new_ocell, true); |
Joe Thornber | 1e32134 | 2014-11-27 12:26:46 +0000 | [diff] [blame] | 995 | } else { |
| 996 | /* |
| 997 | * The block was promoted via an overwrite, so it's dirty. |
| 998 | */ |
| 999 | set_dirty(cache, mg->new_oblock, mg->cblock); |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1000 | bio_endio(mg->new_ocell->holder, 0); |
| 1001 | cell_defer(cache, mg->new_ocell, false); |
| 1002 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1003 | cleanup_migration(mg); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | static void copy_complete(int read_err, unsigned long write_err, void *context) |
| 1008 | { |
| 1009 | unsigned long flags; |
| 1010 | struct dm_cache_migration *mg = (struct dm_cache_migration *) context; |
| 1011 | struct cache *cache = mg->cache; |
| 1012 | |
| 1013 | if (read_err || write_err) |
| 1014 | mg->err = true; |
| 1015 | |
| 1016 | spin_lock_irqsave(&cache->lock, flags); |
| 1017 | list_add_tail(&mg->list, &cache->completed_migrations); |
| 1018 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1019 | |
| 1020 | wake_worker(cache); |
| 1021 | } |
| 1022 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1023 | static void issue_copy(struct dm_cache_migration *mg) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1024 | { |
| 1025 | int r; |
| 1026 | struct dm_io_region o_region, c_region; |
| 1027 | struct cache *cache = mg->cache; |
Heinz Mauelshagen | 8b9d966 | 2014-03-12 00:40:05 +0100 | [diff] [blame] | 1028 | sector_t cblock = from_cblock(mg->cblock); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1029 | |
| 1030 | o_region.bdev = cache->origin_dev->bdev; |
| 1031 | o_region.count = cache->sectors_per_block; |
| 1032 | |
| 1033 | c_region.bdev = cache->cache_dev->bdev; |
Heinz Mauelshagen | 8b9d966 | 2014-03-12 00:40:05 +0100 | [diff] [blame] | 1034 | c_region.sector = cblock * cache->sectors_per_block; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1035 | c_region.count = cache->sectors_per_block; |
| 1036 | |
| 1037 | if (mg->writeback || mg->demote) { |
| 1038 | /* demote */ |
| 1039 | o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block; |
| 1040 | r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg); |
| 1041 | } else { |
| 1042 | /* promote */ |
| 1043 | o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block; |
| 1044 | r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg); |
| 1045 | } |
| 1046 | |
Heinz Mauelshagen | 2c2263c | 2013-10-14 17:14:45 +0200 | [diff] [blame] | 1047 | if (r < 0) { |
| 1048 | DMERR_LIMIT("issuing migration failed"); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1049 | migration_failure(mg); |
Heinz Mauelshagen | 2c2263c | 2013-10-14 17:14:45 +0200 | [diff] [blame] | 1050 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1053 | static void overwrite_endio(struct bio *bio, int err) |
| 1054 | { |
| 1055 | struct dm_cache_migration *mg = bio->bi_private; |
| 1056 | struct cache *cache = mg->cache; |
| 1057 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 1058 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); |
| 1059 | unsigned long flags; |
| 1060 | |
Mike Snitzer | 80ae49a | 2014-01-31 14:30:37 -0500 | [diff] [blame] | 1061 | dm_unhook_bio(&pb->hook_info, bio); |
| 1062 | |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1063 | if (err) |
| 1064 | mg->err = true; |
| 1065 | |
Mike Snitzer | 80ae49a | 2014-01-31 14:30:37 -0500 | [diff] [blame] | 1066 | mg->requeue_holder = false; |
| 1067 | |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1068 | spin_lock_irqsave(&cache->lock, flags); |
| 1069 | list_add_tail(&mg->list, &cache->completed_migrations); |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1070 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1071 | |
| 1072 | wake_worker(cache); |
| 1073 | } |
| 1074 | |
| 1075 | static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio) |
| 1076 | { |
| 1077 | size_t pb_data_size = get_per_bio_data_size(mg->cache); |
| 1078 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); |
| 1079 | |
| 1080 | dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg); |
| 1081 | remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | * No need to inc_ds() here, since the cell will be held for the |
| 1085 | * duration of the io. |
| 1086 | */ |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1087 | generic_make_request(bio); |
| 1088 | } |
| 1089 | |
| 1090 | static bool bio_writes_complete_block(struct cache *cache, struct bio *bio) |
| 1091 | { |
| 1092 | return (bio_data_dir(bio) == WRITE) && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1093 | (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT)); |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1096 | static void avoid_copy(struct dm_cache_migration *mg) |
| 1097 | { |
| 1098 | atomic_inc(&mg->cache->stats.copies_avoided); |
| 1099 | migration_success_pre_commit(mg); |
| 1100 | } |
| 1101 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1102 | static void calc_discard_block_range(struct cache *cache, struct bio *bio, |
| 1103 | dm_dblock_t *b, dm_dblock_t *e) |
| 1104 | { |
| 1105 | sector_t sb = bio->bi_iter.bi_sector; |
| 1106 | sector_t se = bio_end_sector(bio); |
| 1107 | |
| 1108 | *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size)); |
| 1109 | |
| 1110 | if (se - sb < cache->discard_block_size) |
| 1111 | *e = *b; |
| 1112 | else |
| 1113 | *e = to_dblock(block_div(se, cache->discard_block_size)); |
| 1114 | } |
| 1115 | |
| 1116 | static void issue_discard(struct dm_cache_migration *mg) |
| 1117 | { |
| 1118 | dm_dblock_t b, e; |
| 1119 | struct bio *bio = mg->new_ocell->holder; |
| 1120 | |
| 1121 | calc_discard_block_range(mg->cache, bio, &b, &e); |
| 1122 | while (b != e) { |
| 1123 | set_discard(mg->cache, b); |
| 1124 | b = to_dblock(from_dblock(b) + 1); |
| 1125 | } |
| 1126 | |
| 1127 | bio_endio(bio, 0); |
| 1128 | cell_defer(mg->cache, mg->new_ocell, false); |
| 1129 | free_migration(mg); |
| 1130 | } |
| 1131 | |
| 1132 | static void issue_copy_or_discard(struct dm_cache_migration *mg) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1133 | { |
| 1134 | bool avoid; |
| 1135 | struct cache *cache = mg->cache; |
| 1136 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1137 | if (mg->discard) { |
| 1138 | issue_discard(mg); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1142 | if (mg->writeback || mg->demote) |
| 1143 | avoid = !is_dirty(cache, mg->cblock) || |
| 1144 | is_discarded_oblock(cache, mg->old_oblock); |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1145 | else { |
| 1146 | struct bio *bio = mg->new_ocell->holder; |
| 1147 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1148 | avoid = is_discarded_oblock(cache, mg->new_oblock); |
| 1149 | |
Joe Thornber | f29a314 | 2014-11-27 12:21:08 +0000 | [diff] [blame] | 1150 | if (writeback_mode(&cache->features) && |
| 1151 | !avoid && bio_writes_complete_block(cache, bio)) { |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1152 | issue_overwrite(mg, bio); |
| 1153 | return; |
| 1154 | } |
| 1155 | } |
| 1156 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1157 | avoid ? avoid_copy(mg) : issue_copy(mg); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | static void complete_migration(struct dm_cache_migration *mg) |
| 1161 | { |
| 1162 | if (mg->err) |
| 1163 | migration_failure(mg); |
| 1164 | else |
| 1165 | migration_success_pre_commit(mg); |
| 1166 | } |
| 1167 | |
| 1168 | static void process_migrations(struct cache *cache, struct list_head *head, |
| 1169 | void (*fn)(struct dm_cache_migration *)) |
| 1170 | { |
| 1171 | unsigned long flags; |
| 1172 | struct list_head list; |
| 1173 | struct dm_cache_migration *mg, *tmp; |
| 1174 | |
| 1175 | INIT_LIST_HEAD(&list); |
| 1176 | spin_lock_irqsave(&cache->lock, flags); |
| 1177 | list_splice_init(head, &list); |
| 1178 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1179 | |
| 1180 | list_for_each_entry_safe(mg, tmp, &list, list) |
| 1181 | fn(mg); |
| 1182 | } |
| 1183 | |
| 1184 | static void __queue_quiesced_migration(struct dm_cache_migration *mg) |
| 1185 | { |
| 1186 | list_add_tail(&mg->list, &mg->cache->quiesced_migrations); |
| 1187 | } |
| 1188 | |
| 1189 | static void queue_quiesced_migration(struct dm_cache_migration *mg) |
| 1190 | { |
| 1191 | unsigned long flags; |
| 1192 | struct cache *cache = mg->cache; |
| 1193 | |
| 1194 | spin_lock_irqsave(&cache->lock, flags); |
| 1195 | __queue_quiesced_migration(mg); |
| 1196 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1197 | |
| 1198 | wake_worker(cache); |
| 1199 | } |
| 1200 | |
| 1201 | static void queue_quiesced_migrations(struct cache *cache, struct list_head *work) |
| 1202 | { |
| 1203 | unsigned long flags; |
| 1204 | struct dm_cache_migration *mg, *tmp; |
| 1205 | |
| 1206 | spin_lock_irqsave(&cache->lock, flags); |
| 1207 | list_for_each_entry_safe(mg, tmp, work, list) |
| 1208 | __queue_quiesced_migration(mg); |
| 1209 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1210 | |
| 1211 | wake_worker(cache); |
| 1212 | } |
| 1213 | |
| 1214 | static void check_for_quiesced_migrations(struct cache *cache, |
| 1215 | struct per_bio_data *pb) |
| 1216 | { |
| 1217 | struct list_head work; |
| 1218 | |
| 1219 | if (!pb->all_io_entry) |
| 1220 | return; |
| 1221 | |
| 1222 | INIT_LIST_HEAD(&work); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 1223 | dm_deferred_entry_dec(pb->all_io_entry, &work); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1224 | |
| 1225 | if (!list_empty(&work)) |
| 1226 | queue_quiesced_migrations(cache, &work); |
| 1227 | } |
| 1228 | |
| 1229 | static void quiesce_migration(struct dm_cache_migration *mg) |
| 1230 | { |
| 1231 | if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list)) |
| 1232 | queue_quiesced_migration(mg); |
| 1233 | } |
| 1234 | |
| 1235 | static void promote(struct cache *cache, struct prealloc *structs, |
| 1236 | dm_oblock_t oblock, dm_cblock_t cblock, |
| 1237 | struct dm_bio_prison_cell *cell) |
| 1238 | { |
| 1239 | struct dm_cache_migration *mg = prealloc_get_migration(structs); |
| 1240 | |
| 1241 | mg->err = false; |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1242 | mg->discard = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1243 | mg->writeback = false; |
| 1244 | mg->demote = false; |
| 1245 | mg->promote = true; |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1246 | mg->requeue_holder = true; |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 1247 | mg->invalidate = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1248 | mg->cache = cache; |
| 1249 | mg->new_oblock = oblock; |
| 1250 | mg->cblock = cblock; |
| 1251 | mg->old_ocell = NULL; |
| 1252 | mg->new_ocell = cell; |
| 1253 | mg->start_jiffies = jiffies; |
| 1254 | |
| 1255 | inc_nr_migrations(cache); |
| 1256 | quiesce_migration(mg); |
| 1257 | } |
| 1258 | |
| 1259 | static void writeback(struct cache *cache, struct prealloc *structs, |
| 1260 | dm_oblock_t oblock, dm_cblock_t cblock, |
| 1261 | struct dm_bio_prison_cell *cell) |
| 1262 | { |
| 1263 | struct dm_cache_migration *mg = prealloc_get_migration(structs); |
| 1264 | |
| 1265 | mg->err = false; |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1266 | mg->discard = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1267 | mg->writeback = true; |
| 1268 | mg->demote = false; |
| 1269 | mg->promote = false; |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1270 | mg->requeue_holder = true; |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 1271 | mg->invalidate = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1272 | mg->cache = cache; |
| 1273 | mg->old_oblock = oblock; |
| 1274 | mg->cblock = cblock; |
| 1275 | mg->old_ocell = cell; |
| 1276 | mg->new_ocell = NULL; |
| 1277 | mg->start_jiffies = jiffies; |
| 1278 | |
| 1279 | inc_nr_migrations(cache); |
| 1280 | quiesce_migration(mg); |
| 1281 | } |
| 1282 | |
| 1283 | static void demote_then_promote(struct cache *cache, struct prealloc *structs, |
| 1284 | dm_oblock_t old_oblock, dm_oblock_t new_oblock, |
| 1285 | dm_cblock_t cblock, |
| 1286 | struct dm_bio_prison_cell *old_ocell, |
| 1287 | struct dm_bio_prison_cell *new_ocell) |
| 1288 | { |
| 1289 | struct dm_cache_migration *mg = prealloc_get_migration(structs); |
| 1290 | |
| 1291 | mg->err = false; |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1292 | mg->discard = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1293 | mg->writeback = false; |
| 1294 | mg->demote = true; |
| 1295 | mg->promote = true; |
Joe Thornber | c9d28d5 | 2013-10-31 13:55:48 -0400 | [diff] [blame] | 1296 | mg->requeue_holder = true; |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 1297 | mg->invalidate = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1298 | mg->cache = cache; |
| 1299 | mg->old_oblock = old_oblock; |
| 1300 | mg->new_oblock = new_oblock; |
| 1301 | mg->cblock = cblock; |
| 1302 | mg->old_ocell = old_ocell; |
| 1303 | mg->new_ocell = new_ocell; |
| 1304 | mg->start_jiffies = jiffies; |
| 1305 | |
| 1306 | inc_nr_migrations(cache); |
| 1307 | quiesce_migration(mg); |
| 1308 | } |
| 1309 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1310 | /* |
| 1311 | * Invalidate a cache entry. No writeback occurs; any changes in the cache |
| 1312 | * block are thrown away. |
| 1313 | */ |
| 1314 | static void invalidate(struct cache *cache, struct prealloc *structs, |
| 1315 | dm_oblock_t oblock, dm_cblock_t cblock, |
| 1316 | struct dm_bio_prison_cell *cell) |
| 1317 | { |
| 1318 | struct dm_cache_migration *mg = prealloc_get_migration(structs); |
| 1319 | |
| 1320 | mg->err = false; |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1321 | mg->discard = false; |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1322 | mg->writeback = false; |
| 1323 | mg->demote = true; |
| 1324 | mg->promote = false; |
| 1325 | mg->requeue_holder = true; |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 1326 | mg->invalidate = true; |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1327 | mg->cache = cache; |
| 1328 | mg->old_oblock = oblock; |
| 1329 | mg->cblock = cblock; |
| 1330 | mg->old_ocell = cell; |
| 1331 | mg->new_ocell = NULL; |
| 1332 | mg->start_jiffies = jiffies; |
| 1333 | |
| 1334 | inc_nr_migrations(cache); |
| 1335 | quiesce_migration(mg); |
| 1336 | } |
| 1337 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1338 | static void discard(struct cache *cache, struct prealloc *structs, |
| 1339 | struct dm_bio_prison_cell *cell) |
| 1340 | { |
| 1341 | struct dm_cache_migration *mg = prealloc_get_migration(structs); |
| 1342 | |
| 1343 | mg->err = false; |
| 1344 | mg->discard = true; |
| 1345 | mg->writeback = false; |
| 1346 | mg->demote = false; |
| 1347 | mg->promote = false; |
| 1348 | mg->requeue_holder = false; |
| 1349 | mg->invalidate = false; |
| 1350 | mg->cache = cache; |
| 1351 | mg->old_ocell = NULL; |
| 1352 | mg->new_ocell = cell; |
| 1353 | mg->start_jiffies = jiffies; |
| 1354 | |
| 1355 | quiesce_migration(mg); |
| 1356 | } |
| 1357 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1358 | /*---------------------------------------------------------------- |
| 1359 | * bio processing |
| 1360 | *--------------------------------------------------------------*/ |
| 1361 | static void defer_bio(struct cache *cache, struct bio *bio) |
| 1362 | { |
| 1363 | unsigned long flags; |
| 1364 | |
| 1365 | spin_lock_irqsave(&cache->lock, flags); |
| 1366 | bio_list_add(&cache->deferred_bios, bio); |
| 1367 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1368 | |
| 1369 | wake_worker(cache); |
| 1370 | } |
| 1371 | |
| 1372 | static void process_flush_bio(struct cache *cache, struct bio *bio) |
| 1373 | { |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 1374 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 1375 | 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] | 1376 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1377 | BUG_ON(bio->bi_iter.bi_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1378 | if (!pb->req_nr) |
| 1379 | remap_to_origin(cache, bio); |
| 1380 | else |
| 1381 | remap_to_cache(cache, bio, 0); |
| 1382 | |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 1383 | /* |
| 1384 | * REQ_FLUSH is not directed at any particular block so we don't |
| 1385 | * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH |
| 1386 | * by dm-core. |
| 1387 | */ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1388 | issue(cache, bio); |
| 1389 | } |
| 1390 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1391 | static void process_discard_bio(struct cache *cache, struct prealloc *structs, |
| 1392 | struct bio *bio) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1393 | { |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1394 | int r; |
| 1395 | dm_dblock_t b, e; |
| 1396 | struct dm_bio_prison_cell *cell_prealloc, *new_ocell; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1397 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1398 | calc_discard_block_range(cache, bio, &b, &e); |
| 1399 | if (b == e) { |
| 1400 | bio_endio(bio, 0); |
| 1401 | return; |
| 1402 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1403 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1404 | cell_prealloc = prealloc_get_cell(structs); |
| 1405 | r = bio_detain_range(cache, dblock_to_oblock(cache, b), dblock_to_oblock(cache, e), bio, cell_prealloc, |
| 1406 | (cell_free_fn) prealloc_put_cell, |
| 1407 | structs, &new_ocell); |
| 1408 | if (r > 0) |
| 1409 | return; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1410 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1411 | discard(cache, structs, new_ocell); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | static bool spare_migration_bandwidth(struct cache *cache) |
| 1415 | { |
| 1416 | sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) * |
| 1417 | cache->sectors_per_block; |
| 1418 | return current_volume < cache->migration_threshold; |
| 1419 | } |
| 1420 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1421 | static void inc_hit_counter(struct cache *cache, struct bio *bio) |
| 1422 | { |
| 1423 | atomic_inc(bio_data_dir(bio) == READ ? |
| 1424 | &cache->stats.read_hit : &cache->stats.write_hit); |
| 1425 | } |
| 1426 | |
| 1427 | static void inc_miss_counter(struct cache *cache, struct bio *bio) |
| 1428 | { |
| 1429 | atomic_inc(bio_data_dir(bio) == READ ? |
| 1430 | &cache->stats.read_miss : &cache->stats.write_miss); |
| 1431 | } |
| 1432 | |
| 1433 | static void process_bio(struct cache *cache, struct prealloc *structs, |
| 1434 | struct bio *bio) |
| 1435 | { |
| 1436 | int r; |
| 1437 | bool release_cell = true; |
| 1438 | dm_oblock_t block = get_bio_block(cache, bio); |
| 1439 | struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell; |
| 1440 | struct policy_result lookup_result; |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1441 | bool passthrough = passthrough_mode(&cache->features); |
Joe Thornber | 43c32bf | 2014-11-25 13:14:57 +0000 | [diff] [blame] | 1442 | bool discarded_block, can_migrate; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | * Check to see if that block is currently migrating. |
| 1446 | */ |
| 1447 | cell_prealloc = prealloc_get_cell(structs); |
| 1448 | r = bio_detain(cache, block, bio, cell_prealloc, |
| 1449 | (cell_free_fn) prealloc_put_cell, |
| 1450 | structs, &new_ocell); |
| 1451 | if (r > 0) |
| 1452 | return; |
| 1453 | |
Joe Thornber | 43c32bf | 2014-11-25 13:14:57 +0000 | [diff] [blame] | 1454 | discarded_block = is_discarded_oblock(cache, block); |
| 1455 | can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache)); |
| 1456 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1457 | r = policy_map(cache->policy, block, true, can_migrate, discarded_block, |
| 1458 | bio, &lookup_result); |
| 1459 | |
| 1460 | if (r == -EWOULDBLOCK) |
| 1461 | /* migration has been denied */ |
| 1462 | lookup_result.op = POLICY_MISS; |
| 1463 | |
| 1464 | switch (lookup_result.op) { |
| 1465 | case POLICY_HIT: |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1466 | if (passthrough) { |
| 1467 | inc_miss_counter(cache, bio); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1468 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1469 | /* |
| 1470 | * Passthrough always maps to the origin, |
| 1471 | * invalidating any cache blocks that are written |
| 1472 | * to. |
| 1473 | */ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1474 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1475 | if (bio_data_dir(bio) == WRITE) { |
| 1476 | atomic_inc(&cache->stats.demotion); |
| 1477 | invalidate(cache, structs, block, lookup_result.cblock, new_ocell); |
| 1478 | release_cell = false; |
| 1479 | |
| 1480 | } else { |
| 1481 | /* FIXME: factor out issue_origin() */ |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1482 | remap_to_origin_clear_discard(cache, bio, block); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 1483 | inc_and_issue(cache, bio, new_ocell); |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1484 | } |
| 1485 | } else { |
| 1486 | inc_hit_counter(cache, bio); |
| 1487 | |
| 1488 | if (bio_data_dir(bio) == WRITE && |
| 1489 | writethrough_mode(&cache->features) && |
| 1490 | !is_dirty(cache, lookup_result.cblock)) { |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1491 | remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 1492 | inc_and_issue(cache, bio, new_ocell); |
| 1493 | |
| 1494 | } else { |
| 1495 | remap_to_cache_dirty(cache, bio, block, lookup_result.cblock); |
| 1496 | inc_and_issue(cache, bio, new_ocell); |
| 1497 | } |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1498 | } |
| 1499 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1500 | break; |
| 1501 | |
| 1502 | case POLICY_MISS: |
| 1503 | inc_miss_counter(cache, bio); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1504 | remap_to_origin_clear_discard(cache, bio, block); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 1505 | inc_and_issue(cache, bio, new_ocell); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1506 | break; |
| 1507 | |
| 1508 | case POLICY_NEW: |
| 1509 | atomic_inc(&cache->stats.promotion); |
| 1510 | promote(cache, structs, block, lookup_result.cblock, new_ocell); |
| 1511 | release_cell = false; |
| 1512 | break; |
| 1513 | |
| 1514 | case POLICY_REPLACE: |
| 1515 | cell_prealloc = prealloc_get_cell(structs); |
| 1516 | r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc, |
| 1517 | (cell_free_fn) prealloc_put_cell, |
| 1518 | structs, &old_ocell); |
| 1519 | if (r > 0) { |
| 1520 | /* |
| 1521 | * We have to be careful to avoid lock inversion of |
| 1522 | * the cells. So we back off, and wait for the |
| 1523 | * old_ocell to become free. |
| 1524 | */ |
| 1525 | policy_force_mapping(cache->policy, block, |
| 1526 | lookup_result.old_oblock); |
| 1527 | atomic_inc(&cache->stats.cache_cell_clash); |
| 1528 | break; |
| 1529 | } |
| 1530 | atomic_inc(&cache->stats.demotion); |
| 1531 | atomic_inc(&cache->stats.promotion); |
| 1532 | |
| 1533 | demote_then_promote(cache, structs, lookup_result.old_oblock, |
| 1534 | block, lookup_result.cblock, |
| 1535 | old_ocell, new_ocell); |
| 1536 | release_cell = false; |
| 1537 | break; |
| 1538 | |
| 1539 | default: |
| 1540 | DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__, |
| 1541 | (unsigned) lookup_result.op); |
| 1542 | bio_io_error(bio); |
| 1543 | } |
| 1544 | |
| 1545 | if (release_cell) |
| 1546 | cell_defer(cache, new_ocell, false); |
| 1547 | } |
| 1548 | |
| 1549 | static int need_commit_due_to_time(struct cache *cache) |
| 1550 | { |
Manuel Schölling | 0f30af9 | 2014-05-22 22:42:37 +0200 | [diff] [blame^] | 1551 | return !time_in_range(jiffies, cache->last_commit_jiffies, |
| 1552 | cache->last_commit_jiffies + COMMIT_PERIOD); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | static int commit_if_needed(struct cache *cache) |
| 1556 | { |
Heinz Mauelshagen | ffcbcb6 | 2013-10-14 17:24:43 +0200 | [diff] [blame] | 1557 | int r = 0; |
| 1558 | |
| 1559 | if ((cache->commit_requested || need_commit_due_to_time(cache)) && |
| 1560 | dm_cache_changed_this_transaction(cache->cmd)) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1561 | atomic_inc(&cache->stats.commit_count); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1562 | cache->commit_requested = false; |
Heinz Mauelshagen | ffcbcb6 | 2013-10-14 17:24:43 +0200 | [diff] [blame] | 1563 | r = dm_cache_commit(cache->cmd, false); |
| 1564 | cache->last_commit_jiffies = jiffies; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Heinz Mauelshagen | ffcbcb6 | 2013-10-14 17:24:43 +0200 | [diff] [blame] | 1567 | return r; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | static void process_deferred_bios(struct cache *cache) |
| 1571 | { |
| 1572 | unsigned long flags; |
| 1573 | struct bio_list bios; |
| 1574 | struct bio *bio; |
| 1575 | struct prealloc structs; |
| 1576 | |
| 1577 | memset(&structs, 0, sizeof(structs)); |
| 1578 | bio_list_init(&bios); |
| 1579 | |
| 1580 | spin_lock_irqsave(&cache->lock, flags); |
| 1581 | bio_list_merge(&bios, &cache->deferred_bios); |
| 1582 | bio_list_init(&cache->deferred_bios); |
| 1583 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1584 | |
| 1585 | while (!bio_list_empty(&bios)) { |
| 1586 | /* |
| 1587 | * If we've got no free migration structs, and processing |
| 1588 | * this bio might require one, we pause until there are some |
| 1589 | * prepared mappings to process. |
| 1590 | */ |
| 1591 | if (prealloc_data_structs(cache, &structs)) { |
| 1592 | spin_lock_irqsave(&cache->lock, flags); |
| 1593 | bio_list_merge(&cache->deferred_bios, &bios); |
| 1594 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1595 | break; |
| 1596 | } |
| 1597 | |
| 1598 | bio = bio_list_pop(&bios); |
| 1599 | |
| 1600 | if (bio->bi_rw & REQ_FLUSH) |
| 1601 | process_flush_bio(cache, bio); |
| 1602 | else if (bio->bi_rw & REQ_DISCARD) |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1603 | process_discard_bio(cache, &structs, bio); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1604 | else |
| 1605 | process_bio(cache, &structs, bio); |
| 1606 | } |
| 1607 | |
| 1608 | prealloc_free_structs(cache, &structs); |
| 1609 | } |
| 1610 | |
| 1611 | static void process_deferred_flush_bios(struct cache *cache, bool submit_bios) |
| 1612 | { |
| 1613 | unsigned long flags; |
| 1614 | struct bio_list bios; |
| 1615 | struct bio *bio; |
| 1616 | |
| 1617 | bio_list_init(&bios); |
| 1618 | |
| 1619 | spin_lock_irqsave(&cache->lock, flags); |
| 1620 | bio_list_merge(&bios, &cache->deferred_flush_bios); |
| 1621 | bio_list_init(&cache->deferred_flush_bios); |
| 1622 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1623 | |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 1624 | /* |
| 1625 | * These bios have already been through inc_ds() |
| 1626 | */ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1627 | while ((bio = bio_list_pop(&bios))) |
| 1628 | submit_bios ? generic_make_request(bio) : bio_io_error(bio); |
| 1629 | } |
| 1630 | |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1631 | static void process_deferred_writethrough_bios(struct cache *cache) |
| 1632 | { |
| 1633 | unsigned long flags; |
| 1634 | struct bio_list bios; |
| 1635 | struct bio *bio; |
| 1636 | |
| 1637 | bio_list_init(&bios); |
| 1638 | |
| 1639 | spin_lock_irqsave(&cache->lock, flags); |
| 1640 | bio_list_merge(&bios, &cache->deferred_writethrough_bios); |
| 1641 | bio_list_init(&cache->deferred_writethrough_bios); |
| 1642 | spin_unlock_irqrestore(&cache->lock, flags); |
| 1643 | |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 1644 | /* |
| 1645 | * These bios have already been through inc_ds() |
| 1646 | */ |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1647 | while ((bio = bio_list_pop(&bios))) |
| 1648 | generic_make_request(bio); |
| 1649 | } |
| 1650 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1651 | static void writeback_some_dirty_blocks(struct cache *cache) |
| 1652 | { |
| 1653 | int r = 0; |
| 1654 | dm_oblock_t oblock; |
| 1655 | dm_cblock_t cblock; |
| 1656 | struct prealloc structs; |
| 1657 | struct dm_bio_prison_cell *old_ocell; |
| 1658 | |
| 1659 | memset(&structs, 0, sizeof(structs)); |
| 1660 | |
| 1661 | while (spare_migration_bandwidth(cache)) { |
| 1662 | if (prealloc_data_structs(cache, &structs)) |
| 1663 | break; |
| 1664 | |
| 1665 | r = policy_writeback_work(cache->policy, &oblock, &cblock); |
| 1666 | if (r) |
| 1667 | break; |
| 1668 | |
| 1669 | r = get_cell(cache, oblock, &structs, &old_ocell); |
| 1670 | if (r) { |
| 1671 | policy_set_dirty(cache->policy, oblock); |
| 1672 | break; |
| 1673 | } |
| 1674 | |
| 1675 | writeback(cache, &structs, oblock, cblock, old_ocell); |
| 1676 | } |
| 1677 | |
| 1678 | prealloc_free_structs(cache, &structs); |
| 1679 | } |
| 1680 | |
| 1681 | /*---------------------------------------------------------------- |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 1682 | * Invalidations. |
| 1683 | * Dropping something from the cache *without* writing back. |
| 1684 | *--------------------------------------------------------------*/ |
| 1685 | |
| 1686 | static void process_invalidation_request(struct cache *cache, struct invalidation_request *req) |
| 1687 | { |
| 1688 | int r = 0; |
| 1689 | uint64_t begin = from_cblock(req->cblocks->begin); |
| 1690 | uint64_t end = from_cblock(req->cblocks->end); |
| 1691 | |
| 1692 | while (begin != end) { |
| 1693 | r = policy_remove_cblock(cache->policy, to_cblock(begin)); |
| 1694 | if (!r) { |
| 1695 | r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin)); |
| 1696 | if (r) |
| 1697 | break; |
| 1698 | |
| 1699 | } else if (r == -ENODATA) { |
| 1700 | /* harmless, already unmapped */ |
| 1701 | r = 0; |
| 1702 | |
| 1703 | } else { |
| 1704 | DMERR("policy_remove_cblock failed"); |
| 1705 | break; |
| 1706 | } |
| 1707 | |
| 1708 | begin++; |
| 1709 | } |
| 1710 | |
| 1711 | cache->commit_requested = true; |
| 1712 | |
| 1713 | req->err = r; |
| 1714 | atomic_set(&req->complete, 1); |
| 1715 | |
| 1716 | wake_up(&req->result_wait); |
| 1717 | } |
| 1718 | |
| 1719 | static void process_invalidation_requests(struct cache *cache) |
| 1720 | { |
| 1721 | struct list_head list; |
| 1722 | struct invalidation_request *req, *tmp; |
| 1723 | |
| 1724 | INIT_LIST_HEAD(&list); |
| 1725 | spin_lock(&cache->invalidation_lock); |
| 1726 | list_splice_init(&cache->invalidation_requests, &list); |
| 1727 | spin_unlock(&cache->invalidation_lock); |
| 1728 | |
| 1729 | list_for_each_entry_safe (req, tmp, &list, list) |
| 1730 | process_invalidation_request(cache, req); |
| 1731 | } |
| 1732 | |
| 1733 | /*---------------------------------------------------------------- |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1734 | * Main worker loop |
| 1735 | *--------------------------------------------------------------*/ |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1736 | static bool is_quiescing(struct cache *cache) |
| 1737 | { |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 1738 | return atomic_read(&cache->quiescing); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1741 | static void ack_quiescing(struct cache *cache) |
| 1742 | { |
| 1743 | if (is_quiescing(cache)) { |
| 1744 | atomic_inc(&cache->quiescing_ack); |
| 1745 | wake_up(&cache->quiescing_wait); |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | static void wait_for_quiescing_ack(struct cache *cache) |
| 1750 | { |
| 1751 | wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack)); |
| 1752 | } |
| 1753 | |
| 1754 | static void start_quiescing(struct cache *cache) |
| 1755 | { |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 1756 | atomic_inc(&cache->quiescing); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1757 | wait_for_quiescing_ack(cache); |
| 1758 | } |
| 1759 | |
| 1760 | static void stop_quiescing(struct cache *cache) |
| 1761 | { |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 1762 | atomic_set(&cache->quiescing, 0); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1763 | atomic_set(&cache->quiescing_ack, 0); |
| 1764 | } |
| 1765 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1766 | static void wait_for_migrations(struct cache *cache) |
| 1767 | { |
| 1768 | wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations)); |
| 1769 | } |
| 1770 | |
| 1771 | static void stop_worker(struct cache *cache) |
| 1772 | { |
| 1773 | cancel_delayed_work(&cache->waker); |
| 1774 | flush_workqueue(cache->wq); |
| 1775 | } |
| 1776 | |
| 1777 | static void requeue_deferred_io(struct cache *cache) |
| 1778 | { |
| 1779 | struct bio *bio; |
| 1780 | struct bio_list bios; |
| 1781 | |
| 1782 | bio_list_init(&bios); |
| 1783 | bio_list_merge(&bios, &cache->deferred_bios); |
| 1784 | bio_list_init(&cache->deferred_bios); |
| 1785 | |
| 1786 | while ((bio = bio_list_pop(&bios))) |
| 1787 | bio_endio(bio, DM_ENDIO_REQUEUE); |
| 1788 | } |
| 1789 | |
| 1790 | static int more_work(struct cache *cache) |
| 1791 | { |
| 1792 | if (is_quiescing(cache)) |
| 1793 | return !list_empty(&cache->quiesced_migrations) || |
| 1794 | !list_empty(&cache->completed_migrations) || |
| 1795 | !list_empty(&cache->need_commit_migrations); |
| 1796 | else |
| 1797 | return !bio_list_empty(&cache->deferred_bios) || |
| 1798 | !bio_list_empty(&cache->deferred_flush_bios) || |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1799 | !bio_list_empty(&cache->deferred_writethrough_bios) || |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1800 | !list_empty(&cache->quiesced_migrations) || |
| 1801 | !list_empty(&cache->completed_migrations) || |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 1802 | !list_empty(&cache->need_commit_migrations) || |
| 1803 | cache->invalidate; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | static void do_worker(struct work_struct *ws) |
| 1807 | { |
| 1808 | struct cache *cache = container_of(ws, struct cache, worker); |
| 1809 | |
| 1810 | do { |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1811 | if (!is_quiescing(cache)) { |
| 1812 | writeback_some_dirty_blocks(cache); |
| 1813 | process_deferred_writethrough_bios(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1814 | process_deferred_bios(cache); |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 1815 | process_invalidation_requests(cache); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1816 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1817 | |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 1818 | process_migrations(cache, &cache->quiesced_migrations, issue_copy_or_discard); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1819 | process_migrations(cache, &cache->completed_migrations, complete_migration); |
| 1820 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1821 | if (commit_if_needed(cache)) { |
| 1822 | process_deferred_flush_bios(cache, false); |
Joe Thornber | 304affa | 2014-06-24 15:36:58 -0400 | [diff] [blame] | 1823 | process_migrations(cache, &cache->need_commit_migrations, migration_failure); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1824 | |
| 1825 | /* |
| 1826 | * FIXME: rollback metadata or just go into a |
| 1827 | * failure mode and error everything |
| 1828 | */ |
| 1829 | } else { |
| 1830 | process_deferred_flush_bios(cache, true); |
| 1831 | process_migrations(cache, &cache->need_commit_migrations, |
| 1832 | migration_success_post_commit); |
| 1833 | } |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 1834 | |
| 1835 | ack_quiescing(cache); |
| 1836 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1837 | } while (more_work(cache)); |
| 1838 | } |
| 1839 | |
| 1840 | /* |
| 1841 | * We want to commit periodically so that not too much |
| 1842 | * unwritten metadata builds up. |
| 1843 | */ |
| 1844 | static void do_waker(struct work_struct *ws) |
| 1845 | { |
| 1846 | struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker); |
Joe Thornber | f8350da | 2013-05-10 14:37:16 +0100 | [diff] [blame] | 1847 | policy_tick(cache->policy); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1848 | wake_worker(cache); |
| 1849 | queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD); |
| 1850 | } |
| 1851 | |
| 1852 | /*----------------------------------------------------------------*/ |
| 1853 | |
| 1854 | static int is_congested(struct dm_dev *dev, int bdi_bits) |
| 1855 | { |
| 1856 | struct request_queue *q = bdev_get_queue(dev->bdev); |
| 1857 | return bdi_congested(&q->backing_dev_info, bdi_bits); |
| 1858 | } |
| 1859 | |
| 1860 | static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits) |
| 1861 | { |
| 1862 | struct cache *cache = container_of(cb, struct cache, callbacks); |
| 1863 | |
| 1864 | return is_congested(cache->origin_dev, bdi_bits) || |
| 1865 | is_congested(cache->cache_dev, bdi_bits); |
| 1866 | } |
| 1867 | |
| 1868 | /*---------------------------------------------------------------- |
| 1869 | * Target methods |
| 1870 | *--------------------------------------------------------------*/ |
| 1871 | |
| 1872 | /* |
| 1873 | * This function gets called on the error paths of the constructor, so we |
| 1874 | * have to cope with a partially initialised struct. |
| 1875 | */ |
| 1876 | static void destroy(struct cache *cache) |
| 1877 | { |
| 1878 | unsigned i; |
| 1879 | |
| 1880 | if (cache->next_migration) |
| 1881 | mempool_free(cache->next_migration, cache->migration_pool); |
| 1882 | |
| 1883 | if (cache->migration_pool) |
| 1884 | mempool_destroy(cache->migration_pool); |
| 1885 | |
| 1886 | if (cache->all_io_ds) |
| 1887 | dm_deferred_set_destroy(cache->all_io_ds); |
| 1888 | |
| 1889 | if (cache->prison) |
| 1890 | dm_bio_prison_destroy(cache->prison); |
| 1891 | |
| 1892 | if (cache->wq) |
| 1893 | destroy_workqueue(cache->wq); |
| 1894 | |
| 1895 | if (cache->dirty_bitset) |
| 1896 | free_bitset(cache->dirty_bitset); |
| 1897 | |
| 1898 | if (cache->discard_bitset) |
| 1899 | free_bitset(cache->discard_bitset); |
| 1900 | |
| 1901 | if (cache->copier) |
| 1902 | dm_kcopyd_client_destroy(cache->copier); |
| 1903 | |
| 1904 | if (cache->cmd) |
| 1905 | dm_cache_metadata_close(cache->cmd); |
| 1906 | |
| 1907 | if (cache->metadata_dev) |
| 1908 | dm_put_device(cache->ti, cache->metadata_dev); |
| 1909 | |
| 1910 | if (cache->origin_dev) |
| 1911 | dm_put_device(cache->ti, cache->origin_dev); |
| 1912 | |
| 1913 | if (cache->cache_dev) |
| 1914 | dm_put_device(cache->ti, cache->cache_dev); |
| 1915 | |
| 1916 | if (cache->policy) |
| 1917 | dm_cache_policy_destroy(cache->policy); |
| 1918 | |
| 1919 | for (i = 0; i < cache->nr_ctr_args ; i++) |
| 1920 | kfree(cache->ctr_args[i]); |
| 1921 | kfree(cache->ctr_args); |
| 1922 | |
| 1923 | kfree(cache); |
| 1924 | } |
| 1925 | |
| 1926 | static void cache_dtr(struct dm_target *ti) |
| 1927 | { |
| 1928 | struct cache *cache = ti->private; |
| 1929 | |
| 1930 | destroy(cache); |
| 1931 | } |
| 1932 | |
| 1933 | static sector_t get_dev_size(struct dm_dev *dev) |
| 1934 | { |
| 1935 | return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT; |
| 1936 | } |
| 1937 | |
| 1938 | /*----------------------------------------------------------------*/ |
| 1939 | |
| 1940 | /* |
| 1941 | * Construct a cache device mapping. |
| 1942 | * |
| 1943 | * cache <metadata dev> <cache dev> <origin dev> <block size> |
| 1944 | * <#feature args> [<feature arg>]* |
| 1945 | * <policy> <#policy args> [<policy arg>]* |
| 1946 | * |
| 1947 | * metadata dev : fast device holding the persistent metadata |
| 1948 | * cache dev : fast device holding cached data blocks |
| 1949 | * origin dev : slow device holding original data blocks |
| 1950 | * block size : cache unit size in sectors |
| 1951 | * |
| 1952 | * #feature args : number of feature arguments passed |
| 1953 | * feature args : writethrough. (The default is writeback.) |
| 1954 | * |
| 1955 | * policy : the replacement policy to use |
| 1956 | * #policy args : an even number of policy arguments corresponding |
| 1957 | * to key/value pairs passed to the policy |
| 1958 | * policy args : key/value pairs passed to the policy |
| 1959 | * E.g. 'sequential_threshold 1024' |
| 1960 | * See cache-policies.txt for details. |
| 1961 | * |
| 1962 | * Optional feature arguments are: |
| 1963 | * writethrough : write through caching that prohibits cache block |
| 1964 | * content from being different from origin block content. |
| 1965 | * Without this argument, the default behaviour is to write |
| 1966 | * back cache block contents later for performance reasons, |
| 1967 | * so they may differ from the corresponding origin blocks. |
| 1968 | */ |
| 1969 | struct cache_args { |
| 1970 | struct dm_target *ti; |
| 1971 | |
| 1972 | struct dm_dev *metadata_dev; |
| 1973 | |
| 1974 | struct dm_dev *cache_dev; |
| 1975 | sector_t cache_sectors; |
| 1976 | |
| 1977 | struct dm_dev *origin_dev; |
| 1978 | sector_t origin_sectors; |
| 1979 | |
| 1980 | uint32_t block_size; |
| 1981 | |
| 1982 | const char *policy_name; |
| 1983 | int policy_argc; |
| 1984 | const char **policy_argv; |
| 1985 | |
| 1986 | struct cache_features features; |
| 1987 | }; |
| 1988 | |
| 1989 | static void destroy_cache_args(struct cache_args *ca) |
| 1990 | { |
| 1991 | if (ca->metadata_dev) |
| 1992 | dm_put_device(ca->ti, ca->metadata_dev); |
| 1993 | |
| 1994 | if (ca->cache_dev) |
| 1995 | dm_put_device(ca->ti, ca->cache_dev); |
| 1996 | |
| 1997 | if (ca->origin_dev) |
| 1998 | dm_put_device(ca->ti, ca->origin_dev); |
| 1999 | |
| 2000 | kfree(ca); |
| 2001 | } |
| 2002 | |
| 2003 | static bool at_least_one_arg(struct dm_arg_set *as, char **error) |
| 2004 | { |
| 2005 | if (!as->argc) { |
| 2006 | *error = "Insufficient args"; |
| 2007 | return false; |
| 2008 | } |
| 2009 | |
| 2010 | return true; |
| 2011 | } |
| 2012 | |
| 2013 | static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as, |
| 2014 | char **error) |
| 2015 | { |
| 2016 | int r; |
| 2017 | sector_t metadata_dev_size; |
| 2018 | char b[BDEVNAME_SIZE]; |
| 2019 | |
| 2020 | if (!at_least_one_arg(as, error)) |
| 2021 | return -EINVAL; |
| 2022 | |
| 2023 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, |
| 2024 | &ca->metadata_dev); |
| 2025 | if (r) { |
| 2026 | *error = "Error opening metadata device"; |
| 2027 | return r; |
| 2028 | } |
| 2029 | |
| 2030 | metadata_dev_size = get_dev_size(ca->metadata_dev); |
| 2031 | if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING) |
| 2032 | DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.", |
| 2033 | bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS); |
| 2034 | |
| 2035 | return 0; |
| 2036 | } |
| 2037 | |
| 2038 | static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as, |
| 2039 | char **error) |
| 2040 | { |
| 2041 | int r; |
| 2042 | |
| 2043 | if (!at_least_one_arg(as, error)) |
| 2044 | return -EINVAL; |
| 2045 | |
| 2046 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, |
| 2047 | &ca->cache_dev); |
| 2048 | if (r) { |
| 2049 | *error = "Error opening cache device"; |
| 2050 | return r; |
| 2051 | } |
| 2052 | ca->cache_sectors = get_dev_size(ca->cache_dev); |
| 2053 | |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
| 2057 | static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as, |
| 2058 | char **error) |
| 2059 | { |
| 2060 | int r; |
| 2061 | |
| 2062 | if (!at_least_one_arg(as, error)) |
| 2063 | return -EINVAL; |
| 2064 | |
| 2065 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, |
| 2066 | &ca->origin_dev); |
| 2067 | if (r) { |
| 2068 | *error = "Error opening origin device"; |
| 2069 | return r; |
| 2070 | } |
| 2071 | |
| 2072 | ca->origin_sectors = get_dev_size(ca->origin_dev); |
| 2073 | if (ca->ti->len > ca->origin_sectors) { |
| 2074 | *error = "Device size larger than cached device"; |
| 2075 | return -EINVAL; |
| 2076 | } |
| 2077 | |
| 2078 | return 0; |
| 2079 | } |
| 2080 | |
| 2081 | static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as, |
| 2082 | char **error) |
| 2083 | { |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 2084 | unsigned long block_size; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2085 | |
| 2086 | if (!at_least_one_arg(as, error)) |
| 2087 | return -EINVAL; |
| 2088 | |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 2089 | if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size || |
| 2090 | block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS || |
| 2091 | block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS || |
| 2092 | block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2093 | *error = "Invalid data block size"; |
| 2094 | return -EINVAL; |
| 2095 | } |
| 2096 | |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 2097 | if (block_size > ca->cache_sectors) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2098 | *error = "Data block size is larger than the cache device"; |
| 2099 | return -EINVAL; |
| 2100 | } |
| 2101 | |
Mike Snitzer | 0547304 | 2013-08-16 10:54:19 -0400 | [diff] [blame] | 2102 | ca->block_size = block_size; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2103 | |
| 2104 | return 0; |
| 2105 | } |
| 2106 | |
| 2107 | static void init_features(struct cache_features *cf) |
| 2108 | { |
| 2109 | cf->mode = CM_WRITE; |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2110 | cf->io_mode = CM_IO_WRITEBACK; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | static int parse_features(struct cache_args *ca, struct dm_arg_set *as, |
| 2114 | char **error) |
| 2115 | { |
| 2116 | static struct dm_arg _args[] = { |
| 2117 | {0, 1, "Invalid number of cache feature arguments"}, |
| 2118 | }; |
| 2119 | |
| 2120 | int r; |
| 2121 | unsigned argc; |
| 2122 | const char *arg; |
| 2123 | struct cache_features *cf = &ca->features; |
| 2124 | |
| 2125 | init_features(cf); |
| 2126 | |
| 2127 | r = dm_read_arg_group(_args, as, &argc, error); |
| 2128 | if (r) |
| 2129 | return -EINVAL; |
| 2130 | |
| 2131 | while (argc--) { |
| 2132 | arg = dm_shift_arg(as); |
| 2133 | |
| 2134 | if (!strcasecmp(arg, "writeback")) |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2135 | cf->io_mode = CM_IO_WRITEBACK; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2136 | |
| 2137 | else if (!strcasecmp(arg, "writethrough")) |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2138 | cf->io_mode = CM_IO_WRITETHROUGH; |
| 2139 | |
| 2140 | else if (!strcasecmp(arg, "passthrough")) |
| 2141 | cf->io_mode = CM_IO_PASSTHROUGH; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2142 | |
| 2143 | else { |
| 2144 | *error = "Unrecognised cache feature requested"; |
| 2145 | return -EINVAL; |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
| 2152 | static int parse_policy(struct cache_args *ca, struct dm_arg_set *as, |
| 2153 | char **error) |
| 2154 | { |
| 2155 | static struct dm_arg _args[] = { |
| 2156 | {0, 1024, "Invalid number of policy arguments"}, |
| 2157 | }; |
| 2158 | |
| 2159 | int r; |
| 2160 | |
| 2161 | if (!at_least_one_arg(as, error)) |
| 2162 | return -EINVAL; |
| 2163 | |
| 2164 | ca->policy_name = dm_shift_arg(as); |
| 2165 | |
| 2166 | r = dm_read_arg_group(_args, as, &ca->policy_argc, error); |
| 2167 | if (r) |
| 2168 | return -EINVAL; |
| 2169 | |
| 2170 | ca->policy_argv = (const char **)as->argv; |
| 2171 | dm_consume_args(as, ca->policy_argc); |
| 2172 | |
| 2173 | return 0; |
| 2174 | } |
| 2175 | |
| 2176 | static int parse_cache_args(struct cache_args *ca, int argc, char **argv, |
| 2177 | char **error) |
| 2178 | { |
| 2179 | int r; |
| 2180 | struct dm_arg_set as; |
| 2181 | |
| 2182 | as.argc = argc; |
| 2183 | as.argv = argv; |
| 2184 | |
| 2185 | r = parse_metadata_dev(ca, &as, error); |
| 2186 | if (r) |
| 2187 | return r; |
| 2188 | |
| 2189 | r = parse_cache_dev(ca, &as, error); |
| 2190 | if (r) |
| 2191 | return r; |
| 2192 | |
| 2193 | r = parse_origin_dev(ca, &as, error); |
| 2194 | if (r) |
| 2195 | return r; |
| 2196 | |
| 2197 | r = parse_block_size(ca, &as, error); |
| 2198 | if (r) |
| 2199 | return r; |
| 2200 | |
| 2201 | r = parse_features(ca, &as, error); |
| 2202 | if (r) |
| 2203 | return r; |
| 2204 | |
| 2205 | r = parse_policy(ca, &as, error); |
| 2206 | if (r) |
| 2207 | return r; |
| 2208 | |
| 2209 | return 0; |
| 2210 | } |
| 2211 | |
| 2212 | /*----------------------------------------------------------------*/ |
| 2213 | |
| 2214 | static struct kmem_cache *migration_cache; |
| 2215 | |
Alasdair G Kergon | 2c73c47 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2216 | #define NOT_CORE_OPTION 1 |
| 2217 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2218 | 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] | 2219 | { |
| 2220 | unsigned long tmp; |
| 2221 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2222 | if (!strcasecmp(key, "migration_threshold")) { |
| 2223 | if (kstrtoul(value, 10, &tmp)) |
Alasdair G Kergon | 2c73c47 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2224 | return -EINVAL; |
| 2225 | |
| 2226 | cache->migration_threshold = tmp; |
| 2227 | return 0; |
| 2228 | } |
| 2229 | |
| 2230 | return NOT_CORE_OPTION; |
| 2231 | } |
| 2232 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2233 | static int set_config_value(struct cache *cache, const char *key, const char *value) |
| 2234 | { |
| 2235 | int r = process_config_option(cache, key, value); |
| 2236 | |
| 2237 | if (r == NOT_CORE_OPTION) |
| 2238 | r = policy_set_config_value(cache->policy, key, value); |
| 2239 | |
| 2240 | if (r) |
| 2241 | DMWARN("bad config value for %s: %s", key, value); |
| 2242 | |
| 2243 | return r; |
| 2244 | } |
| 2245 | |
| 2246 | 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] | 2247 | { |
| 2248 | int r = 0; |
| 2249 | |
| 2250 | if (argc & 1) { |
| 2251 | DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs."); |
| 2252 | return -EINVAL; |
| 2253 | } |
| 2254 | |
| 2255 | while (argc) { |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2256 | r = set_config_value(cache, argv[0], argv[1]); |
| 2257 | if (r) |
| 2258 | break; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2259 | |
| 2260 | argc -= 2; |
| 2261 | argv += 2; |
| 2262 | } |
| 2263 | |
| 2264 | return r; |
| 2265 | } |
| 2266 | |
| 2267 | static int create_cache_policy(struct cache *cache, struct cache_args *ca, |
| 2268 | char **error) |
| 2269 | { |
Mikulas Patocka | 4cb3e1d | 2013-10-01 18:35:39 -0400 | [diff] [blame] | 2270 | struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name, |
| 2271 | cache->cache_size, |
| 2272 | cache->origin_sectors, |
| 2273 | cache->sectors_per_block); |
| 2274 | if (IS_ERR(p)) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2275 | *error = "Error creating cache's policy"; |
Mikulas Patocka | 4cb3e1d | 2013-10-01 18:35:39 -0400 | [diff] [blame] | 2276 | return PTR_ERR(p); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2277 | } |
Mikulas Patocka | 4cb3e1d | 2013-10-01 18:35:39 -0400 | [diff] [blame] | 2278 | cache->policy = p; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2279 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2280 | return 0; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Joe Thornber | 08b1845 | 2014-11-06 14:38:01 +0000 | [diff] [blame] | 2283 | /* |
Joe Thornber | 2bb812d | 2014-11-26 16:07:50 +0000 | [diff] [blame] | 2284 | * We want the discard block size to be at least the size of the cache |
| 2285 | * block size and have no more than 2^14 discard blocks across the origin. |
Joe Thornber | 08b1845 | 2014-11-06 14:38:01 +0000 | [diff] [blame] | 2286 | */ |
| 2287 | #define MAX_DISCARD_BLOCKS (1 << 14) |
| 2288 | |
| 2289 | static bool too_many_discard_blocks(sector_t discard_block_size, |
| 2290 | sector_t origin_size) |
| 2291 | { |
| 2292 | (void) sector_div(origin_size, discard_block_size); |
| 2293 | |
| 2294 | return origin_size > MAX_DISCARD_BLOCKS; |
| 2295 | } |
| 2296 | |
| 2297 | static sector_t calculate_discard_block_size(sector_t cache_block_size, |
| 2298 | sector_t origin_size) |
| 2299 | { |
Joe Thornber | 2bb812d | 2014-11-26 16:07:50 +0000 | [diff] [blame] | 2300 | sector_t discard_block_size = cache_block_size; |
Joe Thornber | 08b1845 | 2014-11-06 14:38:01 +0000 | [diff] [blame] | 2301 | |
| 2302 | if (origin_size) |
| 2303 | while (too_many_discard_blocks(discard_block_size, origin_size)) |
| 2304 | discard_block_size *= 2; |
| 2305 | |
| 2306 | return discard_block_size; |
| 2307 | } |
| 2308 | |
Joe Thornber | d1d9220 | 2014-11-11 11:58:32 +0000 | [diff] [blame] | 2309 | static void set_cache_size(struct cache *cache, dm_cblock_t size) |
| 2310 | { |
| 2311 | dm_block_t nr_blocks = from_cblock(size); |
| 2312 | |
| 2313 | if (nr_blocks > (1 << 20) && cache->cache_size != size) |
| 2314 | DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n" |
| 2315 | "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n" |
| 2316 | "Please consider increasing the cache block size to reduce the overall cache block count.", |
| 2317 | (unsigned long long) nr_blocks); |
| 2318 | |
| 2319 | cache->cache_size = size; |
| 2320 | } |
| 2321 | |
Joe Thornber | f8350da | 2013-05-10 14:37:16 +0100 | [diff] [blame] | 2322 | #define DEFAULT_MIGRATION_THRESHOLD 2048 |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2323 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2324 | static int cache_create(struct cache_args *ca, struct cache **result) |
| 2325 | { |
| 2326 | int r = 0; |
| 2327 | char **error = &ca->ti->error; |
| 2328 | struct cache *cache; |
| 2329 | struct dm_target *ti = ca->ti; |
| 2330 | dm_block_t origin_blocks; |
| 2331 | struct dm_cache_metadata *cmd; |
| 2332 | bool may_format = ca->features.mode == CM_WRITE; |
| 2333 | |
| 2334 | cache = kzalloc(sizeof(*cache), GFP_KERNEL); |
| 2335 | if (!cache) |
| 2336 | return -ENOMEM; |
| 2337 | |
| 2338 | cache->ti = ca->ti; |
| 2339 | ti->private = cache; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2340 | ti->num_flush_bios = 2; |
| 2341 | ti->flush_supported = true; |
| 2342 | |
| 2343 | ti->num_discard_bios = 1; |
| 2344 | ti->discards_supported = true; |
| 2345 | ti->discard_zeroes_data_unsupported = true; |
Joe Thornber | 2572629 | 2014-11-24 14:05:16 +0000 | [diff] [blame] | 2346 | ti->split_discard_bios = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2347 | |
Joe Thornber | 8c5008f | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2348 | cache->features = ca->features; |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 2349 | ti->per_bio_data_size = get_per_bio_data_size(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2350 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2351 | cache->callbacks.congested_fn = cache_is_congested; |
| 2352 | dm_table_add_target_callbacks(ti->table, &cache->callbacks); |
| 2353 | |
| 2354 | cache->metadata_dev = ca->metadata_dev; |
| 2355 | cache->origin_dev = ca->origin_dev; |
| 2356 | cache->cache_dev = ca->cache_dev; |
| 2357 | |
| 2358 | ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL; |
| 2359 | |
| 2360 | /* FIXME: factor out this whole section */ |
| 2361 | origin_blocks = cache->origin_sectors = ca->origin_sectors; |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 2362 | origin_blocks = block_div(origin_blocks, ca->block_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2363 | cache->origin_blocks = to_oblock(origin_blocks); |
| 2364 | |
| 2365 | cache->sectors_per_block = ca->block_size; |
| 2366 | if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) { |
| 2367 | r = -EINVAL; |
| 2368 | goto bad; |
| 2369 | } |
| 2370 | |
| 2371 | if (ca->block_size & (ca->block_size - 1)) { |
| 2372 | dm_block_t cache_size = ca->cache_sectors; |
| 2373 | |
| 2374 | cache->sectors_per_block_shift = -1; |
Joe Thornber | 414dd67 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 2375 | cache_size = block_div(cache_size, ca->block_size); |
Joe Thornber | d1d9220 | 2014-11-11 11:58:32 +0000 | [diff] [blame] | 2376 | set_cache_size(cache, to_cblock(cache_size)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2377 | } else { |
| 2378 | cache->sectors_per_block_shift = __ffs(ca->block_size); |
Joe Thornber | d1d9220 | 2014-11-11 11:58:32 +0000 | [diff] [blame] | 2379 | set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | r = create_cache_policy(cache, ca, error); |
| 2383 | if (r) |
| 2384 | goto bad; |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2385 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2386 | cache->policy_nr_args = ca->policy_argc; |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2387 | cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD; |
| 2388 | |
| 2389 | r = set_config_values(cache, ca->policy_argc, ca->policy_argv); |
| 2390 | if (r) { |
| 2391 | *error = "Error setting cache policy's config values"; |
| 2392 | goto bad; |
| 2393 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2394 | |
| 2395 | cmd = dm_cache_metadata_open(cache->metadata_dev->bdev, |
| 2396 | ca->block_size, may_format, |
| 2397 | dm_cache_policy_get_hint_size(cache->policy)); |
| 2398 | if (IS_ERR(cmd)) { |
| 2399 | *error = "Error creating metadata object"; |
| 2400 | r = PTR_ERR(cmd); |
| 2401 | goto bad; |
| 2402 | } |
| 2403 | cache->cmd = cmd; |
| 2404 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2405 | if (passthrough_mode(&cache->features)) { |
| 2406 | bool all_clean; |
| 2407 | |
| 2408 | r = dm_cache_metadata_all_clean(cache->cmd, &all_clean); |
| 2409 | if (r) { |
| 2410 | *error = "dm_cache_metadata_all_clean() failed"; |
| 2411 | goto bad; |
| 2412 | } |
| 2413 | |
| 2414 | if (!all_clean) { |
| 2415 | *error = "Cannot enter passthrough mode unless all blocks are clean"; |
| 2416 | r = -EINVAL; |
| 2417 | goto bad; |
| 2418 | } |
| 2419 | } |
| 2420 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2421 | spin_lock_init(&cache->lock); |
| 2422 | bio_list_init(&cache->deferred_bios); |
| 2423 | bio_list_init(&cache->deferred_flush_bios); |
Joe Thornber | e2e74d6 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 2424 | bio_list_init(&cache->deferred_writethrough_bios); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2425 | INIT_LIST_HEAD(&cache->quiesced_migrations); |
| 2426 | INIT_LIST_HEAD(&cache->completed_migrations); |
| 2427 | INIT_LIST_HEAD(&cache->need_commit_migrations); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2428 | atomic_set(&cache->nr_migrations, 0); |
| 2429 | init_waitqueue_head(&cache->migration_wait); |
| 2430 | |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 2431 | init_waitqueue_head(&cache->quiescing_wait); |
Joe Thornber | 238f836 | 2013-10-30 17:29:30 +0000 | [diff] [blame] | 2432 | atomic_set(&cache->quiescing, 0); |
Joe Thornber | 66cb191 | 2013-10-30 17:11:58 +0000 | [diff] [blame] | 2433 | atomic_set(&cache->quiescing_ack, 0); |
| 2434 | |
Wei Yongjun | fa4d683 | 2013-05-10 14:37:14 +0100 | [diff] [blame] | 2435 | r = -ENOMEM; |
Anssi Hannula | 44fa816 | 2014-08-01 11:55:47 -0400 | [diff] [blame] | 2436 | atomic_set(&cache->nr_dirty, 0); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2437 | cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size)); |
| 2438 | if (!cache->dirty_bitset) { |
| 2439 | *error = "could not allocate dirty bitset"; |
| 2440 | goto bad; |
| 2441 | } |
| 2442 | clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size)); |
| 2443 | |
Joe Thornber | 08b1845 | 2014-11-06 14:38:01 +0000 | [diff] [blame] | 2444 | cache->discard_block_size = |
| 2445 | calculate_discard_block_size(cache->sectors_per_block, |
| 2446 | cache->origin_sectors); |
Joe Thornber | 2572629 | 2014-11-24 14:05:16 +0000 | [diff] [blame] | 2447 | cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors, |
| 2448 | cache->discard_block_size)); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 2449 | cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2450 | if (!cache->discard_bitset) { |
| 2451 | *error = "could not allocate discard bitset"; |
| 2452 | goto bad; |
| 2453 | } |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 2454 | clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2455 | |
| 2456 | cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle); |
| 2457 | if (IS_ERR(cache->copier)) { |
| 2458 | *error = "could not create kcopyd client"; |
| 2459 | r = PTR_ERR(cache->copier); |
| 2460 | goto bad; |
| 2461 | } |
| 2462 | |
| 2463 | cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM); |
| 2464 | if (!cache->wq) { |
| 2465 | *error = "could not create workqueue for metadata object"; |
| 2466 | goto bad; |
| 2467 | } |
| 2468 | INIT_WORK(&cache->worker, do_worker); |
| 2469 | INIT_DELAYED_WORK(&cache->waker, do_waker); |
| 2470 | cache->last_commit_jiffies = jiffies; |
| 2471 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 2472 | cache->prison = dm_bio_prison_create(); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2473 | if (!cache->prison) { |
| 2474 | *error = "could not create bio prison"; |
| 2475 | goto bad; |
| 2476 | } |
| 2477 | |
| 2478 | cache->all_io_ds = dm_deferred_set_create(); |
| 2479 | if (!cache->all_io_ds) { |
| 2480 | *error = "could not create all_io deferred set"; |
| 2481 | goto bad; |
| 2482 | } |
| 2483 | |
| 2484 | cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE, |
| 2485 | migration_cache); |
| 2486 | if (!cache->migration_pool) { |
| 2487 | *error = "Error creating cache's migration mempool"; |
| 2488 | goto bad; |
| 2489 | } |
| 2490 | |
| 2491 | cache->next_migration = NULL; |
| 2492 | |
| 2493 | cache->need_tick_bio = true; |
| 2494 | cache->sized = false; |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 2495 | cache->invalidate = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2496 | cache->commit_requested = false; |
| 2497 | cache->loaded_mappings = false; |
| 2498 | cache->loaded_discards = false; |
| 2499 | |
| 2500 | load_stats(cache); |
| 2501 | |
| 2502 | atomic_set(&cache->stats.demotion, 0); |
| 2503 | atomic_set(&cache->stats.promotion, 0); |
| 2504 | atomic_set(&cache->stats.copies_avoided, 0); |
| 2505 | atomic_set(&cache->stats.cache_cell_clash, 0); |
| 2506 | atomic_set(&cache->stats.commit_count, 0); |
| 2507 | atomic_set(&cache->stats.discard_count, 0); |
| 2508 | |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 2509 | spin_lock_init(&cache->invalidation_lock); |
| 2510 | INIT_LIST_HEAD(&cache->invalidation_requests); |
| 2511 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2512 | *result = cache; |
| 2513 | return 0; |
| 2514 | |
| 2515 | bad: |
| 2516 | destroy(cache); |
| 2517 | return r; |
| 2518 | } |
| 2519 | |
| 2520 | static int copy_ctr_args(struct cache *cache, int argc, const char **argv) |
| 2521 | { |
| 2522 | unsigned i; |
| 2523 | const char **copy; |
| 2524 | |
| 2525 | copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL); |
| 2526 | if (!copy) |
| 2527 | return -ENOMEM; |
| 2528 | for (i = 0; i < argc; i++) { |
| 2529 | copy[i] = kstrdup(argv[i], GFP_KERNEL); |
| 2530 | if (!copy[i]) { |
| 2531 | while (i--) |
| 2532 | kfree(copy[i]); |
| 2533 | kfree(copy); |
| 2534 | return -ENOMEM; |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | cache->nr_ctr_args = argc; |
| 2539 | cache->ctr_args = copy; |
| 2540 | |
| 2541 | return 0; |
| 2542 | } |
| 2543 | |
| 2544 | static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv) |
| 2545 | { |
| 2546 | int r = -EINVAL; |
| 2547 | struct cache_args *ca; |
| 2548 | struct cache *cache = NULL; |
| 2549 | |
| 2550 | ca = kzalloc(sizeof(*ca), GFP_KERNEL); |
| 2551 | if (!ca) { |
| 2552 | ti->error = "Error allocating memory for cache"; |
| 2553 | return -ENOMEM; |
| 2554 | } |
| 2555 | ca->ti = ti; |
| 2556 | |
| 2557 | r = parse_cache_args(ca, argc, argv, &ti->error); |
| 2558 | if (r) |
| 2559 | goto out; |
| 2560 | |
| 2561 | r = cache_create(ca, &cache); |
Heinz Mauelshagen | 617a0b8 | 2013-03-20 17:21:26 +0000 | [diff] [blame] | 2562 | if (r) |
| 2563 | goto out; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2564 | |
| 2565 | r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3); |
| 2566 | if (r) { |
| 2567 | destroy(cache); |
| 2568 | goto out; |
| 2569 | } |
| 2570 | |
| 2571 | ti->private = cache; |
| 2572 | |
| 2573 | out: |
| 2574 | destroy_cache_args(ca); |
| 2575 | return r; |
| 2576 | } |
| 2577 | |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2578 | static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2579 | { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2580 | int r; |
| 2581 | dm_oblock_t block = get_bio_block(cache, bio); |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 2582 | size_t pb_data_size = get_per_bio_data_size(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2583 | bool can_migrate = false; |
| 2584 | bool discarded_block; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2585 | struct policy_result lookup_result; |
Heinz Mauelshagen | e893fba | 2014-03-12 16:13:39 +0100 | [diff] [blame] | 2586 | struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2587 | |
Heinz Mauelshagen | e893fba | 2014-03-12 16:13:39 +0100 | [diff] [blame] | 2588 | if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2589 | /* |
| 2590 | * This can only occur if the io goes to a partial block at |
| 2591 | * the end of the origin device. We don't cache these. |
| 2592 | * Just remap to the origin and carry on. |
| 2593 | */ |
Heinz Mauelshagen | e893fba | 2014-03-12 16:13:39 +0100 | [diff] [blame] | 2594 | remap_to_origin(cache, bio); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2595 | return DM_MAPIO_REMAPPED; |
| 2596 | } |
| 2597 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2598 | if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) { |
| 2599 | defer_bio(cache, bio); |
| 2600 | return DM_MAPIO_SUBMITTED; |
| 2601 | } |
| 2602 | |
| 2603 | /* |
| 2604 | * Check to see if that block is currently migrating. |
| 2605 | */ |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2606 | *cell = alloc_prison_cell(cache); |
| 2607 | if (!*cell) { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2608 | defer_bio(cache, bio); |
| 2609 | return DM_MAPIO_SUBMITTED; |
| 2610 | } |
| 2611 | |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2612 | r = bio_detain(cache, block, bio, *cell, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2613 | (cell_free_fn) free_prison_cell, |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2614 | cache, cell); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2615 | if (r) { |
| 2616 | if (r < 0) |
| 2617 | defer_bio(cache, bio); |
| 2618 | |
| 2619 | return DM_MAPIO_SUBMITTED; |
| 2620 | } |
| 2621 | |
| 2622 | discarded_block = is_discarded_oblock(cache, block); |
| 2623 | |
| 2624 | r = policy_map(cache->policy, block, false, can_migrate, discarded_block, |
| 2625 | bio, &lookup_result); |
| 2626 | if (r == -EWOULDBLOCK) { |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2627 | cell_defer(cache, *cell, true); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2628 | return DM_MAPIO_SUBMITTED; |
| 2629 | |
| 2630 | } else if (r) { |
| 2631 | DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2632 | cell_defer(cache, *cell, false); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2633 | bio_io_error(bio); |
| 2634 | return DM_MAPIO_SUBMITTED; |
| 2635 | } |
| 2636 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2637 | r = DM_MAPIO_REMAPPED; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2638 | switch (lookup_result.op) { |
| 2639 | case POLICY_HIT: |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2640 | if (passthrough_mode(&cache->features)) { |
| 2641 | if (bio_data_dir(bio) == WRITE) { |
| 2642 | /* |
| 2643 | * We need to invalidate this block, so |
| 2644 | * defer for the worker thread. |
| 2645 | */ |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2646 | cell_defer(cache, *cell, true); |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2647 | r = DM_MAPIO_SUBMITTED; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2648 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2649 | } else { |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2650 | inc_miss_counter(cache, bio); |
| 2651 | remap_to_origin_clear_discard(cache, bio, block); |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | } else { |
| 2655 | inc_hit_counter(cache, bio); |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2656 | if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) && |
| 2657 | !is_dirty(cache, lookup_result.cblock)) |
| 2658 | remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock); |
| 2659 | else |
| 2660 | remap_to_cache_dirty(cache, bio, block, lookup_result.cblock); |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2661 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2662 | break; |
| 2663 | |
| 2664 | case POLICY_MISS: |
| 2665 | inc_miss_counter(cache, bio); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2666 | if (pb->req_nr != 0) { |
| 2667 | /* |
| 2668 | * This is a duplicate writethrough io that is no |
| 2669 | * longer needed because the block has been demoted. |
| 2670 | */ |
| 2671 | bio_endio(bio, 0); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2672 | cell_defer(cache, *cell, false); |
| 2673 | r = DM_MAPIO_SUBMITTED; |
| 2674 | |
| 2675 | } else |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2676 | remap_to_origin_clear_discard(cache, bio, block); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2677 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2678 | break; |
| 2679 | |
| 2680 | default: |
| 2681 | DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__, |
| 2682 | (unsigned) lookup_result.op); |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2683 | cell_defer(cache, *cell, false); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2684 | bio_io_error(bio); |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2685 | r = DM_MAPIO_SUBMITTED; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 2688 | return r; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2691 | static int cache_map(struct dm_target *ti, struct bio *bio) |
| 2692 | { |
| 2693 | int r; |
Joe Thornber | f824a2a | 2014-11-28 09:48:25 +0000 | [diff] [blame] | 2694 | struct dm_bio_prison_cell *cell = NULL; |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2695 | struct cache *cache = ti->private; |
| 2696 | |
| 2697 | r = __cache_map(cache, bio, &cell); |
Joe Thornber | f824a2a | 2014-11-28 09:48:25 +0000 | [diff] [blame] | 2698 | if (r == DM_MAPIO_REMAPPED && cell) { |
Joe Thornber | 8c081b5 | 2014-05-13 16:18:38 +0100 | [diff] [blame] | 2699 | inc_ds(cache, bio, cell); |
| 2700 | cell_defer(cache, cell, false); |
| 2701 | } |
| 2702 | |
| 2703 | return r; |
| 2704 | } |
| 2705 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2706 | static int cache_end_io(struct dm_target *ti, struct bio *bio, int error) |
| 2707 | { |
| 2708 | struct cache *cache = ti->private; |
| 2709 | unsigned long flags; |
Mike Snitzer | 19b0092 | 2013-04-05 15:36:34 +0100 | [diff] [blame] | 2710 | size_t pb_data_size = get_per_bio_data_size(cache); |
| 2711 | 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] | 2712 | |
| 2713 | if (pb->tick) { |
| 2714 | policy_tick(cache->policy); |
| 2715 | |
| 2716 | spin_lock_irqsave(&cache->lock, flags); |
| 2717 | cache->need_tick_bio = true; |
| 2718 | spin_unlock_irqrestore(&cache->lock, flags); |
| 2719 | } |
| 2720 | |
| 2721 | check_for_quiesced_migrations(cache, pb); |
| 2722 | |
| 2723 | return 0; |
| 2724 | } |
| 2725 | |
| 2726 | static int write_dirty_bitset(struct cache *cache) |
| 2727 | { |
| 2728 | unsigned i, r; |
| 2729 | |
| 2730 | for (i = 0; i < from_cblock(cache->cache_size); i++) { |
| 2731 | r = dm_cache_set_dirty(cache->cmd, to_cblock(i), |
| 2732 | is_dirty(cache, to_cblock(i))); |
| 2733 | if (r) |
| 2734 | return r; |
| 2735 | } |
| 2736 | |
| 2737 | return 0; |
| 2738 | } |
| 2739 | |
| 2740 | static int write_discard_bitset(struct cache *cache) |
| 2741 | { |
| 2742 | unsigned i, r; |
| 2743 | |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 2744 | r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size, |
| 2745 | cache->discard_nr_blocks); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2746 | if (r) { |
| 2747 | DMERR("could not resize on-disk discard bitset"); |
| 2748 | return r; |
| 2749 | } |
| 2750 | |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 2751 | for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) { |
| 2752 | r = dm_cache_set_discard(cache->cmd, to_dblock(i), |
| 2753 | is_discarded(cache, to_dblock(i))); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2754 | if (r) |
| 2755 | return r; |
| 2756 | } |
| 2757 | |
| 2758 | return 0; |
| 2759 | } |
| 2760 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2761 | /* |
| 2762 | * returns true on success |
| 2763 | */ |
| 2764 | static bool sync_metadata(struct cache *cache) |
| 2765 | { |
| 2766 | int r1, r2, r3, r4; |
| 2767 | |
| 2768 | r1 = write_dirty_bitset(cache); |
| 2769 | if (r1) |
| 2770 | DMERR("could not write dirty bitset"); |
| 2771 | |
| 2772 | r2 = write_discard_bitset(cache); |
| 2773 | if (r2) |
| 2774 | DMERR("could not write discard bitset"); |
| 2775 | |
| 2776 | save_stats(cache); |
| 2777 | |
Joe Thornber | 0596661 | 2014-04-03 16:16:44 +0100 | [diff] [blame] | 2778 | r3 = dm_cache_write_hints(cache->cmd, cache->policy); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2779 | if (r3) |
| 2780 | DMERR("could not write hints"); |
| 2781 | |
| 2782 | /* |
| 2783 | * If writing the above metadata failed, we still commit, but don't |
| 2784 | * set the clean shutdown flag. This will effectively force every |
| 2785 | * dirty bit to be set on reload. |
| 2786 | */ |
| 2787 | r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3); |
| 2788 | if (r4) |
| 2789 | DMERR("could not write cache metadata. Data loss may occur."); |
| 2790 | |
| 2791 | return !r1 && !r2 && !r3 && !r4; |
| 2792 | } |
| 2793 | |
| 2794 | static void cache_postsuspend(struct dm_target *ti) |
| 2795 | { |
| 2796 | struct cache *cache = ti->private; |
| 2797 | |
| 2798 | start_quiescing(cache); |
| 2799 | wait_for_migrations(cache); |
| 2800 | stop_worker(cache); |
| 2801 | requeue_deferred_io(cache); |
| 2802 | stop_quiescing(cache); |
| 2803 | |
| 2804 | (void) sync_metadata(cache); |
| 2805 | } |
| 2806 | |
| 2807 | static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock, |
| 2808 | bool dirty, uint32_t hint, bool hint_valid) |
| 2809 | { |
| 2810 | int r; |
| 2811 | struct cache *cache = context; |
| 2812 | |
| 2813 | r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid); |
| 2814 | if (r) |
| 2815 | return r; |
| 2816 | |
| 2817 | if (dirty) |
| 2818 | set_dirty(cache, oblock, cblock); |
| 2819 | else |
| 2820 | clear_dirty(cache, oblock, cblock); |
| 2821 | |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
Joe Thornber | 3e2e1c3 | 2014-11-24 14:06:22 +0000 | [diff] [blame] | 2825 | /* |
| 2826 | * The discard block size in the on disk metadata is not |
| 2827 | * neccessarily the same as we're currently using. So we have to |
| 2828 | * be careful to only set the discarded attribute if we know it |
| 2829 | * covers a complete block of the new size. |
| 2830 | */ |
| 2831 | struct discard_load_info { |
| 2832 | struct cache *cache; |
| 2833 | |
| 2834 | /* |
| 2835 | * These blocks are sized using the on disk dblock size, rather |
| 2836 | * than the current one. |
| 2837 | */ |
| 2838 | dm_block_t block_size; |
| 2839 | dm_block_t discard_begin, discard_end; |
| 2840 | }; |
| 2841 | |
| 2842 | static void discard_load_info_init(struct cache *cache, |
| 2843 | struct discard_load_info *li) |
| 2844 | { |
| 2845 | li->cache = cache; |
| 2846 | li->discard_begin = li->discard_end = 0; |
| 2847 | } |
| 2848 | |
| 2849 | static void set_discard_range(struct discard_load_info *li) |
| 2850 | { |
| 2851 | sector_t b, e; |
| 2852 | |
| 2853 | if (li->discard_begin == li->discard_end) |
| 2854 | return; |
| 2855 | |
| 2856 | /* |
| 2857 | * Convert to sectors. |
| 2858 | */ |
| 2859 | b = li->discard_begin * li->block_size; |
| 2860 | e = li->discard_end * li->block_size; |
| 2861 | |
| 2862 | /* |
| 2863 | * Then convert back to the current dblock size. |
| 2864 | */ |
| 2865 | b = dm_sector_div_up(b, li->cache->discard_block_size); |
| 2866 | sector_div(e, li->cache->discard_block_size); |
| 2867 | |
| 2868 | /* |
| 2869 | * The origin may have shrunk, so we need to check we're still in |
| 2870 | * bounds. |
| 2871 | */ |
| 2872 | if (e > from_dblock(li->cache->discard_nr_blocks)) |
| 2873 | e = from_dblock(li->cache->discard_nr_blocks); |
| 2874 | |
| 2875 | for (; b < e; b++) |
| 2876 | set_discard(li->cache, to_dblock(b)); |
| 2877 | } |
| 2878 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2879 | static int load_discard(void *context, sector_t discard_block_size, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 2880 | dm_dblock_t dblock, bool discard) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2881 | { |
Joe Thornber | 3e2e1c3 | 2014-11-24 14:06:22 +0000 | [diff] [blame] | 2882 | struct discard_load_info *li = context; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2883 | |
Joe Thornber | 3e2e1c3 | 2014-11-24 14:06:22 +0000 | [diff] [blame] | 2884 | li->block_size = discard_block_size; |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 2885 | |
Joe Thornber | 3e2e1c3 | 2014-11-24 14:06:22 +0000 | [diff] [blame] | 2886 | if (discard) { |
| 2887 | if (from_dblock(dblock) == li->discard_end) |
| 2888 | /* |
| 2889 | * We're already in a discard range, just extend it. |
| 2890 | */ |
| 2891 | li->discard_end = li->discard_end + 1ULL; |
| 2892 | |
| 2893 | else { |
| 2894 | /* |
| 2895 | * Emit the old range and start a new one. |
| 2896 | */ |
| 2897 | set_discard_range(li); |
| 2898 | li->discard_begin = from_dblock(dblock); |
| 2899 | li->discard_end = li->discard_begin + 1ULL; |
| 2900 | } |
| 2901 | } else { |
| 2902 | set_discard_range(li); |
| 2903 | li->discard_begin = li->discard_end = 0; |
| 2904 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2905 | |
| 2906 | return 0; |
| 2907 | } |
| 2908 | |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 2909 | static dm_cblock_t get_cache_dev_size(struct cache *cache) |
| 2910 | { |
| 2911 | sector_t size = get_dev_size(cache->cache_dev); |
| 2912 | (void) sector_div(size, cache->sectors_per_block); |
| 2913 | return to_cblock(size); |
| 2914 | } |
| 2915 | |
| 2916 | static bool can_resize(struct cache *cache, dm_cblock_t new_size) |
| 2917 | { |
| 2918 | if (from_cblock(new_size) > from_cblock(cache->cache_size)) |
| 2919 | return true; |
| 2920 | |
| 2921 | /* |
| 2922 | * We can't drop a dirty block when shrinking the cache. |
| 2923 | */ |
| 2924 | while (from_cblock(new_size) < from_cblock(cache->cache_size)) { |
| 2925 | new_size = to_cblock(from_cblock(new_size) + 1); |
| 2926 | if (is_dirty(cache, new_size)) { |
| 2927 | DMERR("unable to shrink cache; cache block %llu is dirty", |
| 2928 | (unsigned long long) from_cblock(new_size)); |
| 2929 | return false; |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | return true; |
| 2934 | } |
| 2935 | |
| 2936 | static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size) |
| 2937 | { |
| 2938 | int r; |
| 2939 | |
Vincent Pelletier | 0884480 | 2013-11-30 12:58:42 +0100 | [diff] [blame] | 2940 | r = dm_cache_resize(cache->cmd, new_size); |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 2941 | if (r) { |
| 2942 | DMERR("could not resize cache metadata"); |
| 2943 | return r; |
| 2944 | } |
| 2945 | |
Joe Thornber | d1d9220 | 2014-11-11 11:58:32 +0000 | [diff] [blame] | 2946 | set_cache_size(cache, new_size); |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 2947 | |
| 2948 | return 0; |
| 2949 | } |
| 2950 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2951 | static int cache_preresume(struct dm_target *ti) |
| 2952 | { |
| 2953 | int r = 0; |
| 2954 | struct cache *cache = ti->private; |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 2955 | dm_cblock_t csize = get_cache_dev_size(cache); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2956 | |
| 2957 | /* |
| 2958 | * Check to see if the cache has resized. |
| 2959 | */ |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 2960 | if (!cache->sized) { |
| 2961 | r = resize_cache_dev(cache, csize); |
| 2962 | if (r) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2963 | return r; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2964 | |
| 2965 | cache->sized = true; |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 2966 | |
| 2967 | } else if (csize != cache->cache_size) { |
| 2968 | if (!can_resize(cache, csize)) |
| 2969 | return -EINVAL; |
| 2970 | |
| 2971 | r = resize_cache_dev(cache, csize); |
| 2972 | if (r) |
| 2973 | return r; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | if (!cache->loaded_mappings) { |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 2977 | r = dm_cache_load_mappings(cache->cmd, cache->policy, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2978 | load_mapping, cache); |
| 2979 | if (r) { |
| 2980 | DMERR("could not load cache mappings"); |
| 2981 | return r; |
| 2982 | } |
| 2983 | |
| 2984 | cache->loaded_mappings = true; |
| 2985 | } |
| 2986 | |
| 2987 | if (!cache->loaded_discards) { |
Joe Thornber | 3e2e1c3 | 2014-11-24 14:06:22 +0000 | [diff] [blame] | 2988 | struct discard_load_info li; |
| 2989 | |
| 2990 | /* |
| 2991 | * The discard bitset could have been resized, or the |
| 2992 | * discard block size changed. To be safe we start by |
| 2993 | * setting every dblock to not discarded. |
| 2994 | */ |
| 2995 | clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks)); |
| 2996 | |
| 2997 | discard_load_info_init(cache, &li); |
| 2998 | r = dm_cache_load_discards(cache->cmd, load_discard, &li); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 2999 | if (r) { |
| 3000 | DMERR("could not load origin discards"); |
| 3001 | return r; |
| 3002 | } |
Joe Thornber | 3e2e1c3 | 2014-11-24 14:06:22 +0000 | [diff] [blame] | 3003 | set_discard_range(&li); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3004 | |
| 3005 | cache->loaded_discards = true; |
| 3006 | } |
| 3007 | |
| 3008 | return r; |
| 3009 | } |
| 3010 | |
| 3011 | static void cache_resume(struct dm_target *ti) |
| 3012 | { |
| 3013 | struct cache *cache = ti->private; |
| 3014 | |
| 3015 | cache->need_tick_bio = true; |
| 3016 | do_waker(&cache->waker.work); |
| 3017 | } |
| 3018 | |
| 3019 | /* |
| 3020 | * Status format: |
| 3021 | * |
Mike Snitzer | 6a38861 | 2014-01-09 16:04:12 -0500 | [diff] [blame] | 3022 | * <metadata block size> <#used metadata blocks>/<#total metadata blocks> |
| 3023 | * <cache block size> <#used cache blocks>/<#total cache blocks> |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3024 | * <#read hits> <#read misses> <#write hits> <#write misses> |
Mike Snitzer | 6a38861 | 2014-01-09 16:04:12 -0500 | [diff] [blame] | 3025 | * <#demotions> <#promotions> <#dirty> |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3026 | * <#features> <features>* |
| 3027 | * <#core args> <core args> |
Mike Snitzer | 2e68c4e | 2014-01-15 21:06:55 -0500 | [diff] [blame] | 3028 | * <policy name> <#policy args> <policy args>* |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3029 | */ |
| 3030 | static void cache_status(struct dm_target *ti, status_type_t type, |
| 3031 | unsigned status_flags, char *result, unsigned maxlen) |
| 3032 | { |
| 3033 | int r = 0; |
| 3034 | unsigned i; |
| 3035 | ssize_t sz = 0; |
| 3036 | dm_block_t nr_free_blocks_metadata = 0; |
| 3037 | dm_block_t nr_blocks_metadata = 0; |
| 3038 | char buf[BDEVNAME_SIZE]; |
| 3039 | struct cache *cache = ti->private; |
| 3040 | dm_cblock_t residency; |
| 3041 | |
| 3042 | switch (type) { |
| 3043 | case STATUSTYPE_INFO: |
| 3044 | /* Commit to ensure statistics aren't out-of-date */ |
| 3045 | if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) { |
| 3046 | r = dm_cache_commit(cache->cmd, false); |
| 3047 | if (r) |
| 3048 | DMERR("could not commit metadata for accurate status"); |
| 3049 | } |
| 3050 | |
| 3051 | r = dm_cache_get_free_metadata_block_count(cache->cmd, |
| 3052 | &nr_free_blocks_metadata); |
| 3053 | if (r) { |
| 3054 | DMERR("could not get metadata free block count"); |
| 3055 | goto err; |
| 3056 | } |
| 3057 | |
| 3058 | r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata); |
| 3059 | if (r) { |
| 3060 | DMERR("could not get metadata device size"); |
| 3061 | goto err; |
| 3062 | } |
| 3063 | |
| 3064 | residency = policy_residency(cache->policy); |
| 3065 | |
Anssi Hannula | 44fa816 | 2014-08-01 11:55:47 -0400 | [diff] [blame] | 3066 | DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ", |
Mike Snitzer | 895b47d | 2014-07-14 15:37:18 -0400 | [diff] [blame] | 3067 | (unsigned)DM_CACHE_METADATA_BLOCK_SIZE, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3068 | (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata), |
| 3069 | (unsigned long long)nr_blocks_metadata, |
Mike Snitzer | 6a38861 | 2014-01-09 16:04:12 -0500 | [diff] [blame] | 3070 | cache->sectors_per_block, |
| 3071 | (unsigned long long) from_cblock(residency), |
| 3072 | (unsigned long long) from_cblock(cache->cache_size), |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3073 | (unsigned) atomic_read(&cache->stats.read_hit), |
| 3074 | (unsigned) atomic_read(&cache->stats.read_miss), |
| 3075 | (unsigned) atomic_read(&cache->stats.write_hit), |
| 3076 | (unsigned) atomic_read(&cache->stats.write_miss), |
| 3077 | (unsigned) atomic_read(&cache->stats.demotion), |
| 3078 | (unsigned) atomic_read(&cache->stats.promotion), |
Anssi Hannula | 44fa816 | 2014-08-01 11:55:47 -0400 | [diff] [blame] | 3079 | (unsigned long) atomic_read(&cache->nr_dirty)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3080 | |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 3081 | if (writethrough_mode(&cache->features)) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3082 | DMEMIT("1 writethrough "); |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 3083 | |
| 3084 | else if (passthrough_mode(&cache->features)) |
| 3085 | DMEMIT("1 passthrough "); |
| 3086 | |
| 3087 | else if (writeback_mode(&cache->features)) |
| 3088 | DMEMIT("1 writeback "); |
| 3089 | |
| 3090 | else { |
| 3091 | DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode); |
| 3092 | goto err; |
| 3093 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3094 | |
| 3095 | DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold); |
Mike Snitzer | 2e68c4e | 2014-01-15 21:06:55 -0500 | [diff] [blame] | 3096 | |
| 3097 | DMEMIT("%s ", dm_cache_policy_get_name(cache->policy)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3098 | if (sz < maxlen) { |
| 3099 | r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz); |
| 3100 | if (r) |
| 3101 | DMERR("policy_emit_config_values returned %d", r); |
| 3102 | } |
| 3103 | |
| 3104 | break; |
| 3105 | |
| 3106 | case STATUSTYPE_TABLE: |
| 3107 | format_dev_t(buf, cache->metadata_dev->bdev->bd_dev); |
| 3108 | DMEMIT("%s ", buf); |
| 3109 | format_dev_t(buf, cache->cache_dev->bdev->bd_dev); |
| 3110 | DMEMIT("%s ", buf); |
| 3111 | format_dev_t(buf, cache->origin_dev->bdev->bd_dev); |
| 3112 | DMEMIT("%s", buf); |
| 3113 | |
| 3114 | for (i = 0; i < cache->nr_ctr_args - 1; i++) |
| 3115 | DMEMIT(" %s", cache->ctr_args[i]); |
| 3116 | if (cache->nr_ctr_args) |
| 3117 | DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]); |
| 3118 | } |
| 3119 | |
| 3120 | return; |
| 3121 | |
| 3122 | err: |
| 3123 | DMEMIT("Error"); |
| 3124 | } |
| 3125 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3126 | /* |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 3127 | * A cache block range can take two forms: |
| 3128 | * |
| 3129 | * i) A single cblock, eg. '3456' |
| 3130 | * ii) A begin and end cblock with dots between, eg. 123-234 |
| 3131 | */ |
| 3132 | static int parse_cblock_range(struct cache *cache, const char *str, |
| 3133 | struct cblock_range *result) |
| 3134 | { |
| 3135 | char dummy; |
| 3136 | uint64_t b, e; |
| 3137 | int r; |
| 3138 | |
| 3139 | /* |
| 3140 | * Try and parse form (ii) first. |
| 3141 | */ |
| 3142 | r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy); |
| 3143 | if (r < 0) |
| 3144 | return r; |
| 3145 | |
| 3146 | if (r == 2) { |
| 3147 | result->begin = to_cblock(b); |
| 3148 | result->end = to_cblock(e); |
| 3149 | return 0; |
| 3150 | } |
| 3151 | |
| 3152 | /* |
| 3153 | * That didn't work, try form (i). |
| 3154 | */ |
| 3155 | r = sscanf(str, "%llu%c", &b, &dummy); |
| 3156 | if (r < 0) |
| 3157 | return r; |
| 3158 | |
| 3159 | if (r == 1) { |
| 3160 | result->begin = to_cblock(b); |
| 3161 | result->end = to_cblock(from_cblock(result->begin) + 1u); |
| 3162 | return 0; |
| 3163 | } |
| 3164 | |
| 3165 | DMERR("invalid cblock range '%s'", str); |
| 3166 | return -EINVAL; |
| 3167 | } |
| 3168 | |
| 3169 | static int validate_cblock_range(struct cache *cache, struct cblock_range *range) |
| 3170 | { |
| 3171 | uint64_t b = from_cblock(range->begin); |
| 3172 | uint64_t e = from_cblock(range->end); |
| 3173 | uint64_t n = from_cblock(cache->cache_size); |
| 3174 | |
| 3175 | if (b >= n) { |
| 3176 | DMERR("begin cblock out of range: %llu >= %llu", b, n); |
| 3177 | return -EINVAL; |
| 3178 | } |
| 3179 | |
| 3180 | if (e > n) { |
| 3181 | DMERR("end cblock out of range: %llu > %llu", e, n); |
| 3182 | return -EINVAL; |
| 3183 | } |
| 3184 | |
| 3185 | if (b >= e) { |
| 3186 | DMERR("invalid cblock range: %llu >= %llu", b, e); |
| 3187 | return -EINVAL; |
| 3188 | } |
| 3189 | |
| 3190 | return 0; |
| 3191 | } |
| 3192 | |
| 3193 | static int request_invalidation(struct cache *cache, struct cblock_range *range) |
| 3194 | { |
| 3195 | struct invalidation_request req; |
| 3196 | |
| 3197 | INIT_LIST_HEAD(&req.list); |
| 3198 | req.cblocks = range; |
| 3199 | atomic_set(&req.complete, 0); |
| 3200 | req.err = 0; |
| 3201 | init_waitqueue_head(&req.result_wait); |
| 3202 | |
| 3203 | spin_lock(&cache->invalidation_lock); |
| 3204 | list_add(&req.list, &cache->invalidation_requests); |
| 3205 | spin_unlock(&cache->invalidation_lock); |
| 3206 | wake_worker(cache); |
| 3207 | |
| 3208 | wait_event(req.result_wait, atomic_read(&req.complete)); |
| 3209 | return req.err; |
| 3210 | } |
| 3211 | |
| 3212 | static int process_invalidate_cblocks_message(struct cache *cache, unsigned count, |
| 3213 | const char **cblock_ranges) |
| 3214 | { |
| 3215 | int r = 0; |
| 3216 | unsigned i; |
| 3217 | struct cblock_range range; |
| 3218 | |
| 3219 | if (!passthrough_mode(&cache->features)) { |
| 3220 | DMERR("cache has to be in passthrough mode for invalidation"); |
| 3221 | return -EPERM; |
| 3222 | } |
| 3223 | |
| 3224 | for (i = 0; i < count; i++) { |
| 3225 | r = parse_cblock_range(cache, cblock_ranges[i], &range); |
| 3226 | if (r) |
| 3227 | break; |
| 3228 | |
| 3229 | r = validate_cblock_range(cache, &range); |
| 3230 | if (r) |
| 3231 | break; |
| 3232 | |
| 3233 | /* |
| 3234 | * Pass begin and end origin blocks to the worker and wake it. |
| 3235 | */ |
| 3236 | r = request_invalidation(cache, &range); |
| 3237 | if (r) |
| 3238 | break; |
| 3239 | } |
| 3240 | |
| 3241 | return r; |
| 3242 | } |
| 3243 | |
| 3244 | /* |
| 3245 | * Supports |
| 3246 | * "<key> <value>" |
| 3247 | * and |
| 3248 | * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]* |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3249 | * |
| 3250 | * The key migration_threshold is supported by the cache target core. |
| 3251 | */ |
| 3252 | static int cache_message(struct dm_target *ti, unsigned argc, char **argv) |
| 3253 | { |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3254 | struct cache *cache = ti->private; |
| 3255 | |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 3256 | if (!argc) |
| 3257 | return -EINVAL; |
| 3258 | |
Mike Snitzer | 7b6b2bc | 2013-11-12 12:17:43 -0500 | [diff] [blame] | 3259 | if (!strcasecmp(argv[0], "invalidate_cblocks")) |
Joe Thornber | 65790ff | 2013-11-08 16:39:50 +0000 | [diff] [blame] | 3260 | return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1); |
| 3261 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3262 | if (argc != 2) |
| 3263 | return -EINVAL; |
| 3264 | |
Joe Thornber | 2f14f4b | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 3265 | return set_config_value(cache, argv[0], argv[1]); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | static int cache_iterate_devices(struct dm_target *ti, |
| 3269 | iterate_devices_callout_fn fn, void *data) |
| 3270 | { |
| 3271 | int r = 0; |
| 3272 | struct cache *cache = ti->private; |
| 3273 | |
| 3274 | r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data); |
| 3275 | if (!r) |
| 3276 | r = fn(ti, cache->origin_dev, 0, ti->len, data); |
| 3277 | |
| 3278 | return r; |
| 3279 | } |
| 3280 | |
| 3281 | /* |
| 3282 | * We assume I/O is going to the origin (which is the volume |
| 3283 | * more likely to have restrictions e.g. by being striped). |
| 3284 | * (Looking up the exact location of the data would be expensive |
| 3285 | * and could always be out of date by the time the bio is submitted.) |
| 3286 | */ |
| 3287 | static int cache_bvec_merge(struct dm_target *ti, |
| 3288 | struct bvec_merge_data *bvm, |
| 3289 | struct bio_vec *biovec, int max_size) |
| 3290 | { |
| 3291 | struct cache *cache = ti->private; |
| 3292 | struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev); |
| 3293 | |
| 3294 | if (!q->merge_bvec_fn) |
| 3295 | return max_size; |
| 3296 | |
| 3297 | bvm->bi_bdev = cache->origin_dev->bdev; |
| 3298 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); |
| 3299 | } |
| 3300 | |
| 3301 | static void set_discard_limits(struct cache *cache, struct queue_limits *limits) |
| 3302 | { |
| 3303 | /* |
| 3304 | * FIXME: these limits may be incompatible with the cache device |
| 3305 | */ |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 3306 | limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024, |
| 3307 | cache->origin_sectors); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 3308 | limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits) |
| 3312 | { |
| 3313 | struct cache *cache = ti->private; |
Mike Snitzer | f610937 | 2013-08-20 15:02:41 -0400 | [diff] [blame] | 3314 | uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3315 | |
Mike Snitzer | f610937 | 2013-08-20 15:02:41 -0400 | [diff] [blame] | 3316 | /* |
| 3317 | * If the system-determined stacked limits are compatible with the |
| 3318 | * cache's blocksize (io_opt is a factor) do not override them. |
| 3319 | */ |
| 3320 | if (io_opt_sectors < cache->sectors_per_block || |
| 3321 | do_div(io_opt_sectors, cache->sectors_per_block)) { |
Mike Snitzer | b024653 | 2014-07-19 13:25:46 -0400 | [diff] [blame] | 3322 | blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT); |
Mike Snitzer | f610937 | 2013-08-20 15:02:41 -0400 | [diff] [blame] | 3323 | blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT); |
| 3324 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3325 | set_discard_limits(cache, limits); |
| 3326 | } |
| 3327 | |
| 3328 | /*----------------------------------------------------------------*/ |
| 3329 | |
| 3330 | static struct target_type cache_target = { |
| 3331 | .name = "cache", |
Joe Thornber | 7ae34e7 | 2014-11-06 10:18:04 +0000 | [diff] [blame] | 3332 | .version = {1, 6, 0}, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 3333 | .module = THIS_MODULE, |
| 3334 | .ctr = cache_ctr, |
| 3335 | .dtr = cache_dtr, |
| 3336 | .map = cache_map, |
| 3337 | .end_io = cache_end_io, |
| 3338 | .postsuspend = cache_postsuspend, |
| 3339 | .preresume = cache_preresume, |
| 3340 | .resume = cache_resume, |
| 3341 | .status = cache_status, |
| 3342 | .message = cache_message, |
| 3343 | .iterate_devices = cache_iterate_devices, |
| 3344 | .merge = cache_bvec_merge, |
| 3345 | .io_hints = cache_io_hints, |
| 3346 | }; |
| 3347 | |
| 3348 | static int __init dm_cache_init(void) |
| 3349 | { |
| 3350 | int r; |
| 3351 | |
| 3352 | r = dm_register_target(&cache_target); |
| 3353 | if (r) { |
| 3354 | DMERR("cache target registration failed: %d", r); |
| 3355 | return r; |
| 3356 | } |
| 3357 | |
| 3358 | migration_cache = KMEM_CACHE(dm_cache_migration, 0); |
| 3359 | if (!migration_cache) { |
| 3360 | dm_unregister_target(&cache_target); |
| 3361 | return -ENOMEM; |
| 3362 | } |
| 3363 | |
| 3364 | return 0; |
| 3365 | } |
| 3366 | |
| 3367 | static void __exit dm_cache_exit(void) |
| 3368 | { |
| 3369 | dm_unregister_target(&cache_target); |
| 3370 | kmem_cache_destroy(migration_cache); |
| 3371 | } |
| 3372 | |
| 3373 | module_init(dm_cache_init); |
| 3374 | module_exit(dm_cache_exit); |
| 3375 | |
| 3376 | MODULE_DESCRIPTION(DM_NAME " cache target"); |
| 3377 | MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>"); |
| 3378 | MODULE_LICENSE("GPL"); |