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