Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1 | /* |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2 | * Copyright (C) 2011-2012 Red Hat UK. |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #include "dm-thin-metadata.h" |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 8 | #include "dm-bio-prison.h" |
Alasdair G Kergon | 1f4e0ff | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 9 | #include "dm.h" |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 10 | |
| 11 | #include <linux/device-mapper.h> |
| 12 | #include <linux/dm-io.h> |
| 13 | #include <linux/dm-kcopyd.h> |
Mike Snitzer | 604ea90 | 2014-10-09 18:43:25 -0400 | [diff] [blame] | 14 | #include <linux/log2.h> |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 15 | #include <linux/list.h> |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 16 | #include <linux/rculist.h> |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 20 | #include <linux/rbtree.h> |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 21 | |
| 22 | #define DM_MSG_PREFIX "thin" |
| 23 | |
| 24 | /* |
| 25 | * Tunable constants |
| 26 | */ |
Alasdair G Kergon | 7768ed3 | 2012-07-27 15:07:57 +0100 | [diff] [blame] | 27 | #define ENDIO_HOOK_POOL_SIZE 1024 |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 28 | #define MAPPING_POOL_SIZE 1024 |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 29 | #define COMMIT_PERIOD HZ |
Mike Snitzer | 80c5789 | 2014-05-20 13:38:33 -0400 | [diff] [blame] | 30 | #define NO_SPACE_TIMEOUT_SECS 60 |
| 31 | |
| 32 | static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 33 | |
Mikulas Patocka | df5d2e9 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 34 | DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle, |
| 35 | "A percentage of time allocated for copy on write"); |
| 36 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 37 | /* |
| 38 | * The block size of the device holding pool data must be |
| 39 | * between 64KB and 1GB. |
| 40 | */ |
| 41 | #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT) |
| 42 | #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT) |
| 43 | |
| 44 | /* |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 45 | * Device id is restricted to 24 bits. |
| 46 | */ |
| 47 | #define MAX_DEV_ID ((1 << 24) - 1) |
| 48 | |
| 49 | /* |
| 50 | * How do we handle breaking sharing of data blocks? |
| 51 | * ================================================= |
| 52 | * |
| 53 | * We use a standard copy-on-write btree to store the mappings for the |
| 54 | * devices (note I'm talking about copy-on-write of the metadata here, not |
| 55 | * the data). When you take an internal snapshot you clone the root node |
| 56 | * of the origin btree. After this there is no concept of an origin or a |
| 57 | * snapshot. They are just two device trees that happen to point to the |
| 58 | * same data blocks. |
| 59 | * |
| 60 | * When we get a write in we decide if it's to a shared data block using |
| 61 | * some timestamp magic. If it is, we have to break sharing. |
| 62 | * |
| 63 | * Let's say we write to a shared block in what was the origin. The |
| 64 | * steps are: |
| 65 | * |
| 66 | * i) plug io further to this physical block. (see bio_prison code). |
| 67 | * |
| 68 | * ii) quiesce any read io to that shared data block. Obviously |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 69 | * including all devices that share this block. (see dm_deferred_set code) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 70 | * |
| 71 | * iii) copy the data block to a newly allocate block. This step can be |
| 72 | * missed out if the io covers the block. (schedule_copy). |
| 73 | * |
| 74 | * iv) insert the new mapping into the origin's btree |
Joe Thornber | fe878f3 | 2012-03-28 18:41:24 +0100 | [diff] [blame] | 75 | * (process_prepared_mapping). This act of inserting breaks some |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 76 | * sharing of btree nodes between the two devices. Breaking sharing only |
| 77 | * effects the btree of that specific device. Btrees for the other |
| 78 | * devices that share the block never change. The btree for the origin |
| 79 | * device as it was after the last commit is untouched, ie. we're using |
| 80 | * persistent data structures in the functional programming sense. |
| 81 | * |
| 82 | * v) unplug io to this physical block, including the io that triggered |
| 83 | * the breaking of sharing. |
| 84 | * |
| 85 | * Steps (ii) and (iii) occur in parallel. |
| 86 | * |
| 87 | * The metadata _doesn't_ need to be committed before the io continues. We |
| 88 | * get away with this because the io is always written to a _new_ block. |
| 89 | * If there's a crash, then: |
| 90 | * |
| 91 | * - The origin mapping will point to the old origin block (the shared |
| 92 | * one). This will contain the data as it was before the io that triggered |
| 93 | * the breaking of sharing came in. |
| 94 | * |
| 95 | * - The snap mapping still points to the old block. As it would after |
| 96 | * the commit. |
| 97 | * |
| 98 | * The downside of this scheme is the timestamp magic isn't perfect, and |
| 99 | * will continue to think that data block in the snapshot device is shared |
| 100 | * even after the write to the origin has broken sharing. I suspect data |
| 101 | * blocks will typically be shared by many different devices, so we're |
| 102 | * breaking sharing n + 1 times, rather than n, where n is the number of |
| 103 | * devices that reference this data block. At the moment I think the |
| 104 | * benefits far, far outweigh the disadvantages. |
| 105 | */ |
| 106 | |
| 107 | /*----------------------------------------------------------------*/ |
| 108 | |
| 109 | /* |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 110 | * Key building. |
| 111 | */ |
| 112 | static void build_data_key(struct dm_thin_device *td, |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 113 | dm_block_t b, struct dm_cell_key *key) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 114 | { |
| 115 | key->virtual = 0; |
| 116 | key->dev = dm_thin_dev_id(td); |
| 117 | key->block = b; |
| 118 | } |
| 119 | |
| 120 | static void build_virtual_key(struct dm_thin_device *td, dm_block_t b, |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 121 | struct dm_cell_key *key) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 122 | { |
| 123 | key->virtual = 1; |
| 124 | key->dev = dm_thin_dev_id(td); |
| 125 | key->block = b; |
| 126 | } |
| 127 | |
| 128 | /*----------------------------------------------------------------*/ |
| 129 | |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 130 | #define THROTTLE_THRESHOLD (1 * HZ) |
| 131 | |
| 132 | struct throttle { |
| 133 | struct rw_semaphore lock; |
| 134 | unsigned long threshold; |
| 135 | bool throttle_applied; |
| 136 | }; |
| 137 | |
| 138 | static void throttle_init(struct throttle *t) |
| 139 | { |
| 140 | init_rwsem(&t->lock); |
| 141 | t->throttle_applied = false; |
| 142 | } |
| 143 | |
| 144 | static void throttle_work_start(struct throttle *t) |
| 145 | { |
| 146 | t->threshold = jiffies + THROTTLE_THRESHOLD; |
| 147 | } |
| 148 | |
| 149 | static void throttle_work_update(struct throttle *t) |
| 150 | { |
| 151 | if (!t->throttle_applied && jiffies > t->threshold) { |
| 152 | down_write(&t->lock); |
| 153 | t->throttle_applied = true; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void throttle_work_complete(struct throttle *t) |
| 158 | { |
| 159 | if (t->throttle_applied) { |
| 160 | t->throttle_applied = false; |
| 161 | up_write(&t->lock); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void throttle_lock(struct throttle *t) |
| 166 | { |
| 167 | down_read(&t->lock); |
| 168 | } |
| 169 | |
| 170 | static void throttle_unlock(struct throttle *t) |
| 171 | { |
| 172 | up_read(&t->lock); |
| 173 | } |
| 174 | |
| 175 | /*----------------------------------------------------------------*/ |
| 176 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 177 | /* |
| 178 | * A pool device ties together a metadata device and a data device. It |
| 179 | * also provides the interface for creating and destroying internal |
| 180 | * devices. |
| 181 | */ |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 182 | struct dm_thin_new_mapping; |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 183 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 184 | /* |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 185 | * The pool runs in 4 modes. Ordered in degraded order for comparisons. |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 186 | */ |
| 187 | enum pool_mode { |
| 188 | PM_WRITE, /* metadata may be changed */ |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 189 | PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */ |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 190 | PM_READ_ONLY, /* metadata may not be changed */ |
| 191 | PM_FAIL, /* all I/O fails */ |
| 192 | }; |
| 193 | |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 194 | struct pool_features { |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 195 | enum pool_mode mode; |
| 196 | |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 197 | bool zero_new_blocks:1; |
| 198 | bool discard_enabled:1; |
| 199 | bool discard_passdown:1; |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 200 | bool error_if_no_space:1; |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 201 | }; |
| 202 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 203 | struct thin_c; |
| 204 | typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio); |
| 205 | typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m); |
| 206 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 207 | struct pool { |
| 208 | struct list_head list; |
| 209 | struct dm_target *ti; /* Only set if a pool target is bound */ |
| 210 | |
| 211 | struct mapped_device *pool_md; |
| 212 | struct block_device *md_dev; |
| 213 | struct dm_pool_metadata *pmd; |
| 214 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 215 | dm_block_t low_water_blocks; |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 216 | uint32_t sectors_per_block; |
Mikulas Patocka | f9a8e0c | 2012-07-27 15:08:03 +0100 | [diff] [blame] | 217 | int sectors_per_block_shift; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 218 | |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 219 | struct pool_features pf; |
Joe Thornber | 88a6621 | 2013-12-04 20:16:12 -0500 | [diff] [blame] | 220 | bool low_water_triggered:1; /* A dm event has been sent */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 221 | |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 222 | struct dm_bio_prison *prison; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 223 | struct dm_kcopyd_client *copier; |
| 224 | |
| 225 | struct workqueue_struct *wq; |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 226 | struct throttle throttle; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 227 | struct work_struct worker; |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 228 | struct delayed_work waker; |
Joe Thornber | 85ad643b | 2014-05-09 15:59:38 +0100 | [diff] [blame] | 229 | struct delayed_work no_space_timeout; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 230 | |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 231 | unsigned long last_commit_jiffies; |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 232 | unsigned ref_count; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 233 | |
| 234 | spinlock_t lock; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 235 | struct bio_list deferred_flush_bios; |
| 236 | struct list_head prepared_mappings; |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 237 | struct list_head prepared_discards; |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 238 | struct list_head active_thins; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 239 | |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 240 | struct dm_deferred_set *shared_read_ds; |
| 241 | struct dm_deferred_set *all_io_ds; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 242 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 243 | struct dm_thin_new_mapping *next_mapping; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 244 | mempool_t *mapping_pool; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 245 | |
| 246 | process_bio_fn process_bio; |
| 247 | process_bio_fn process_discard; |
| 248 | |
| 249 | process_mapping_fn process_prepared_mapping; |
| 250 | process_mapping_fn process_prepared_discard; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 253 | static enum pool_mode get_pool_mode(struct pool *pool); |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 254 | static void metadata_operation_failed(struct pool *pool, const char *op, int r); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 255 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 256 | /* |
| 257 | * Target context for a pool. |
| 258 | */ |
| 259 | struct pool_c { |
| 260 | struct dm_target *ti; |
| 261 | struct pool *pool; |
| 262 | struct dm_dev *data_dev; |
| 263 | struct dm_dev *metadata_dev; |
| 264 | struct dm_target_callbacks callbacks; |
| 265 | |
| 266 | dm_block_t low_water_blocks; |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 267 | struct pool_features requested_pf; /* Features requested during table load */ |
| 268 | struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | /* |
| 272 | * Target context for a thin. |
| 273 | */ |
| 274 | struct thin_c { |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 275 | struct list_head list; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 276 | struct dm_dev *pool_dev; |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 277 | struct dm_dev *origin_dev; |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 278 | sector_t origin_size; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 279 | dm_thin_id dev_id; |
| 280 | |
| 281 | struct pool *pool; |
| 282 | struct dm_thin_device *td; |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 283 | bool requeue_mode:1; |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 284 | spinlock_t lock; |
| 285 | struct bio_list deferred_bio_list; |
| 286 | struct bio_list retry_on_resume_list; |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 287 | struct rb_root sort_bio_list; /* sorted list of deferred bios */ |
Joe Thornber | b10ebd3 | 2014-04-08 11:29:01 +0100 | [diff] [blame] | 288 | |
| 289 | /* |
| 290 | * Ensures the thin is not destroyed until the worker has finished |
| 291 | * iterating the active_thins list. |
| 292 | */ |
| 293 | atomic_t refcount; |
| 294 | struct completion can_destroy; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | /*----------------------------------------------------------------*/ |
| 298 | |
Joe Thornber | 025b968 | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 299 | /* |
| 300 | * wake_worker() is used when new work is queued and when pool_resume is |
| 301 | * ready to continue deferred IO processing. |
| 302 | */ |
| 303 | static void wake_worker(struct pool *pool) |
| 304 | { |
| 305 | queue_work(pool->wq, &pool->worker); |
| 306 | } |
| 307 | |
| 308 | /*----------------------------------------------------------------*/ |
| 309 | |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 310 | static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio, |
| 311 | struct dm_bio_prison_cell **cell_result) |
| 312 | { |
| 313 | int r; |
| 314 | struct dm_bio_prison_cell *cell_prealloc; |
| 315 | |
| 316 | /* |
| 317 | * Allocate a cell from the prison's mempool. |
| 318 | * This might block but it can't fail. |
| 319 | */ |
| 320 | cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO); |
| 321 | |
| 322 | r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result); |
| 323 | if (r) |
| 324 | /* |
| 325 | * We reused an old cell; we can get rid of |
| 326 | * the new one. |
| 327 | */ |
| 328 | dm_bio_prison_free_cell(pool->prison, cell_prealloc); |
| 329 | |
| 330 | return r; |
| 331 | } |
| 332 | |
| 333 | static void cell_release(struct pool *pool, |
| 334 | struct dm_bio_prison_cell *cell, |
| 335 | struct bio_list *bios) |
| 336 | { |
| 337 | dm_cell_release(pool->prison, cell, bios); |
| 338 | dm_bio_prison_free_cell(pool->prison, cell); |
| 339 | } |
| 340 | |
| 341 | static void cell_release_no_holder(struct pool *pool, |
| 342 | struct dm_bio_prison_cell *cell, |
| 343 | struct bio_list *bios) |
| 344 | { |
| 345 | dm_cell_release_no_holder(pool->prison, cell, bios); |
| 346 | dm_bio_prison_free_cell(pool->prison, cell); |
| 347 | } |
| 348 | |
Joe Thornber | 025b968 | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 349 | static void cell_defer_no_holder_no_free(struct thin_c *tc, |
| 350 | struct dm_bio_prison_cell *cell) |
| 351 | { |
| 352 | struct pool *pool = tc->pool; |
| 353 | unsigned long flags; |
| 354 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 355 | spin_lock_irqsave(&tc->lock, flags); |
| 356 | dm_cell_release_no_holder(pool->prison, cell, &tc->deferred_bio_list); |
| 357 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 025b968 | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 358 | |
| 359 | wake_worker(pool); |
| 360 | } |
| 361 | |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 362 | static void cell_error_with_code(struct pool *pool, |
| 363 | struct dm_bio_prison_cell *cell, int error_code) |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 364 | { |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 365 | dm_cell_error(pool->prison, cell, error_code); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 366 | dm_bio_prison_free_cell(pool->prison, cell); |
| 367 | } |
| 368 | |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 369 | static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell) |
| 370 | { |
| 371 | cell_error_with_code(pool, cell, -EIO); |
| 372 | } |
| 373 | |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 374 | /*----------------------------------------------------------------*/ |
| 375 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 376 | /* |
| 377 | * A global list of pools that uses a struct mapped_device as a key. |
| 378 | */ |
| 379 | static struct dm_thin_pool_table { |
| 380 | struct mutex mutex; |
| 381 | struct list_head pools; |
| 382 | } dm_thin_pool_table; |
| 383 | |
| 384 | static void pool_table_init(void) |
| 385 | { |
| 386 | mutex_init(&dm_thin_pool_table.mutex); |
| 387 | INIT_LIST_HEAD(&dm_thin_pool_table.pools); |
| 388 | } |
| 389 | |
| 390 | static void __pool_table_insert(struct pool *pool) |
| 391 | { |
| 392 | BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); |
| 393 | list_add(&pool->list, &dm_thin_pool_table.pools); |
| 394 | } |
| 395 | |
| 396 | static void __pool_table_remove(struct pool *pool) |
| 397 | { |
| 398 | BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); |
| 399 | list_del(&pool->list); |
| 400 | } |
| 401 | |
| 402 | static struct pool *__pool_table_lookup(struct mapped_device *md) |
| 403 | { |
| 404 | struct pool *pool = NULL, *tmp; |
| 405 | |
| 406 | BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); |
| 407 | |
| 408 | list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) { |
| 409 | if (tmp->pool_md == md) { |
| 410 | pool = tmp; |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return pool; |
| 416 | } |
| 417 | |
| 418 | static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev) |
| 419 | { |
| 420 | struct pool *pool = NULL, *tmp; |
| 421 | |
| 422 | BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); |
| 423 | |
| 424 | list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) { |
| 425 | if (tmp->md_dev == md_dev) { |
| 426 | pool = tmp; |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return pool; |
| 432 | } |
| 433 | |
| 434 | /*----------------------------------------------------------------*/ |
| 435 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 436 | struct dm_thin_endio_hook { |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 437 | struct thin_c *tc; |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 438 | struct dm_deferred_entry *shared_read_entry; |
| 439 | struct dm_deferred_entry *all_io_entry; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 440 | struct dm_thin_new_mapping *overwrite_mapping; |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 441 | struct rb_node rb_node; |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 442 | }; |
| 443 | |
Joe Thornber | 18adc57 | 2014-03-03 15:46:42 +0000 | [diff] [blame] | 444 | static void requeue_bio_list(struct thin_c *tc, struct bio_list *master) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 445 | { |
| 446 | struct bio *bio; |
| 447 | struct bio_list bios; |
Joe Thornber | 18adc57 | 2014-03-03 15:46:42 +0000 | [diff] [blame] | 448 | unsigned long flags; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 449 | |
| 450 | bio_list_init(&bios); |
Joe Thornber | 18adc57 | 2014-03-03 15:46:42 +0000 | [diff] [blame] | 451 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 452 | spin_lock_irqsave(&tc->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 453 | bio_list_merge(&bios, master); |
| 454 | bio_list_init(master); |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 455 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 456 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 457 | while ((bio = bio_list_pop(&bios))) |
| 458 | bio_endio(bio, DM_ENDIO_REQUEUE); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static void requeue_io(struct thin_c *tc) |
| 462 | { |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 463 | requeue_bio_list(tc, &tc->deferred_bio_list); |
| 464 | requeue_bio_list(tc, &tc->retry_on_resume_list); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 467 | static void error_thin_retry_list(struct thin_c *tc) |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 468 | { |
| 469 | struct bio *bio; |
| 470 | unsigned long flags; |
| 471 | struct bio_list bios; |
| 472 | |
| 473 | bio_list_init(&bios); |
| 474 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 475 | spin_lock_irqsave(&tc->lock, flags); |
| 476 | bio_list_merge(&bios, &tc->retry_on_resume_list); |
| 477 | bio_list_init(&tc->retry_on_resume_list); |
| 478 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 479 | |
| 480 | while ((bio = bio_list_pop(&bios))) |
| 481 | bio_io_error(bio); |
| 482 | } |
| 483 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 484 | static void error_retry_list(struct pool *pool) |
| 485 | { |
| 486 | struct thin_c *tc; |
| 487 | |
| 488 | rcu_read_lock(); |
| 489 | list_for_each_entry_rcu(tc, &pool->active_thins, list) |
| 490 | error_thin_retry_list(tc); |
| 491 | rcu_read_unlock(); |
| 492 | } |
| 493 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 494 | /* |
| 495 | * This section of code contains the logic for processing a thin device's IO. |
| 496 | * Much of the code depends on pool object resources (lists, workqueues, etc) |
| 497 | * but most is exclusively called from the thin target rather than the thin-pool |
| 498 | * target. |
| 499 | */ |
| 500 | |
Mike Snitzer | 58f77a2 | 2013-03-01 22:45:45 +0000 | [diff] [blame] | 501 | static bool block_size_is_power_of_two(struct pool *pool) |
| 502 | { |
| 503 | return pool->sectors_per_block_shift >= 0; |
| 504 | } |
| 505 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 506 | static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio) |
| 507 | { |
Mike Snitzer | 58f77a2 | 2013-03-01 22:45:45 +0000 | [diff] [blame] | 508 | struct pool *pool = tc->pool; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 509 | sector_t block_nr = bio->bi_iter.bi_sector; |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 510 | |
Mike Snitzer | 58f77a2 | 2013-03-01 22:45:45 +0000 | [diff] [blame] | 511 | if (block_size_is_power_of_two(pool)) |
| 512 | block_nr >>= pool->sectors_per_block_shift; |
Mikulas Patocka | f9a8e0c | 2012-07-27 15:08:03 +0100 | [diff] [blame] | 513 | else |
Mike Snitzer | 58f77a2 | 2013-03-01 22:45:45 +0000 | [diff] [blame] | 514 | (void) sector_div(block_nr, pool->sectors_per_block); |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 515 | |
| 516 | return block_nr; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block) |
| 520 | { |
| 521 | struct pool *pool = tc->pool; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 522 | sector_t bi_sector = bio->bi_iter.bi_sector; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 523 | |
| 524 | bio->bi_bdev = tc->pool_dev->bdev; |
Mike Snitzer | 58f77a2 | 2013-03-01 22:45:45 +0000 | [diff] [blame] | 525 | if (block_size_is_power_of_two(pool)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 526 | bio->bi_iter.bi_sector = |
| 527 | (block << pool->sectors_per_block_shift) | |
| 528 | (bi_sector & (pool->sectors_per_block - 1)); |
Mike Snitzer | 58f77a2 | 2013-03-01 22:45:45 +0000 | [diff] [blame] | 529 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 530 | bio->bi_iter.bi_sector = (block * pool->sectors_per_block) + |
Mike Snitzer | 58f77a2 | 2013-03-01 22:45:45 +0000 | [diff] [blame] | 531 | sector_div(bi_sector, pool->sectors_per_block); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 534 | static void remap_to_origin(struct thin_c *tc, struct bio *bio) |
| 535 | { |
| 536 | bio->bi_bdev = tc->origin_dev->bdev; |
| 537 | } |
| 538 | |
Joe Thornber | 4afdd68 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 539 | static int bio_triggers_commit(struct thin_c *tc, struct bio *bio) |
| 540 | { |
| 541 | return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && |
| 542 | dm_thin_changed_this_transaction(tc->td); |
| 543 | } |
| 544 | |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 545 | static void inc_all_io_entry(struct pool *pool, struct bio *bio) |
| 546 | { |
| 547 | struct dm_thin_endio_hook *h; |
| 548 | |
| 549 | if (bio->bi_rw & REQ_DISCARD) |
| 550 | return; |
| 551 | |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 552 | h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 553 | h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds); |
| 554 | } |
| 555 | |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 556 | static void issue(struct thin_c *tc, struct bio *bio) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 557 | { |
| 558 | struct pool *pool = tc->pool; |
| 559 | unsigned long flags; |
| 560 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 561 | if (!bio_triggers_commit(tc, bio)) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 562 | generic_make_request(bio); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 563 | return; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Complete bio with an error if earlier I/O caused changes to |
| 568 | * the metadata that can't be committed e.g, due to I/O errors |
| 569 | * on the metadata device. |
| 570 | */ |
| 571 | if (dm_thin_aborted_changes(tc->td)) { |
| 572 | bio_io_error(bio); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Batch together any bios that trigger commits and then issue a |
| 578 | * single commit for them in process_deferred_bios(). |
| 579 | */ |
| 580 | spin_lock_irqsave(&pool->lock, flags); |
| 581 | bio_list_add(&pool->deferred_flush_bios, bio); |
| 582 | spin_unlock_irqrestore(&pool->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 585 | static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio) |
| 586 | { |
| 587 | remap_to_origin(tc, bio); |
| 588 | issue(tc, bio); |
| 589 | } |
| 590 | |
| 591 | static void remap_and_issue(struct thin_c *tc, struct bio *bio, |
| 592 | dm_block_t block) |
| 593 | { |
| 594 | remap(tc, bio, block); |
| 595 | issue(tc, bio); |
| 596 | } |
| 597 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 598 | /*----------------------------------------------------------------*/ |
| 599 | |
| 600 | /* |
| 601 | * Bio endio functions. |
| 602 | */ |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 603 | struct dm_thin_new_mapping { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 604 | struct list_head list; |
| 605 | |
Mike Snitzer | 7f21466 | 2013-12-17 13:43:31 -0500 | [diff] [blame] | 606 | bool pass_discard:1; |
| 607 | bool definitely_not_shared:1; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 608 | |
Joe Thornber | 50f3c3e | 2014-06-13 13:57:09 +0100 | [diff] [blame] | 609 | /* |
| 610 | * Track quiescing, copying and zeroing preparation actions. When this |
| 611 | * counter hits zero the block is prepared and can be inserted into the |
| 612 | * btree. |
| 613 | */ |
| 614 | atomic_t prepare_actions; |
| 615 | |
Mike Snitzer | 7f21466 | 2013-12-17 13:43:31 -0500 | [diff] [blame] | 616 | int err; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 617 | struct thin_c *tc; |
| 618 | dm_block_t virt_block; |
| 619 | dm_block_t data_block; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 620 | struct dm_bio_prison_cell *cell, *cell2; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * If the bio covers the whole area of a block then we can avoid |
| 624 | * zeroing or copying. Instead this bio is hooked. The bio will |
| 625 | * still be in the cell, so care has to be taken to avoid issuing |
| 626 | * the bio twice. |
| 627 | */ |
| 628 | struct bio *bio; |
| 629 | bio_end_io_t *saved_bi_end_io; |
| 630 | }; |
| 631 | |
Joe Thornber | 50f3c3e | 2014-06-13 13:57:09 +0100 | [diff] [blame] | 632 | static void __complete_mapping_preparation(struct dm_thin_new_mapping *m) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 633 | { |
| 634 | struct pool *pool = m->tc->pool; |
| 635 | |
Joe Thornber | 50f3c3e | 2014-06-13 13:57:09 +0100 | [diff] [blame] | 636 | if (atomic_dec_and_test(&m->prepare_actions)) { |
Mike Snitzer | daec338 | 2013-12-11 14:01:20 -0500 | [diff] [blame] | 637 | list_add_tail(&m->list, &pool->prepared_mappings); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 638 | wake_worker(pool); |
| 639 | } |
| 640 | } |
| 641 | |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 642 | static void complete_mapping_preparation(struct dm_thin_new_mapping *m) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 643 | { |
| 644 | unsigned long flags; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 645 | struct pool *pool = m->tc->pool; |
| 646 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 647 | spin_lock_irqsave(&pool->lock, flags); |
Joe Thornber | 50f3c3e | 2014-06-13 13:57:09 +0100 | [diff] [blame] | 648 | __complete_mapping_preparation(m); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 649 | spin_unlock_irqrestore(&pool->lock, flags); |
| 650 | } |
| 651 | |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 652 | static void copy_complete(int read_err, unsigned long write_err, void *context) |
| 653 | { |
| 654 | struct dm_thin_new_mapping *m = context; |
| 655 | |
| 656 | m->err = read_err || write_err ? -EIO : 0; |
| 657 | complete_mapping_preparation(m); |
| 658 | } |
| 659 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 660 | static void overwrite_endio(struct bio *bio, int err) |
| 661 | { |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 662 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 663 | struct dm_thin_new_mapping *m = h->overwrite_mapping; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 664 | |
| 665 | m->err = err; |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 666 | complete_mapping_preparation(m); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 669 | /*----------------------------------------------------------------*/ |
| 670 | |
| 671 | /* |
| 672 | * Workqueue. |
| 673 | */ |
| 674 | |
| 675 | /* |
| 676 | * Prepared mapping jobs. |
| 677 | */ |
| 678 | |
| 679 | /* |
| 680 | * This sends the bios in the cell back to the deferred_bios list. |
| 681 | */ |
Joe Thornber | 2aab385 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 682 | static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 683 | { |
| 684 | struct pool *pool = tc->pool; |
| 685 | unsigned long flags; |
| 686 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 687 | spin_lock_irqsave(&tc->lock, flags); |
| 688 | cell_release(pool, cell, &tc->deferred_bio_list); |
| 689 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 690 | |
| 691 | wake_worker(pool); |
| 692 | } |
| 693 | |
| 694 | /* |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 695 | * Same as cell_defer above, except it omits the original holder of the cell. |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 696 | */ |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 697 | static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 698 | { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 699 | struct pool *pool = tc->pool; |
| 700 | unsigned long flags; |
| 701 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 702 | spin_lock_irqsave(&tc->lock, flags); |
| 703 | cell_release_no_holder(pool, cell, &tc->deferred_bio_list); |
| 704 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 705 | |
| 706 | wake_worker(pool); |
| 707 | } |
| 708 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 709 | static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m) |
| 710 | { |
Kent Overstreet | 196d38b | 2013-11-23 18:34:15 -0800 | [diff] [blame] | 711 | if (m->bio) { |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 712 | m->bio->bi_end_io = m->saved_bi_end_io; |
Kent Overstreet | 196d38b | 2013-11-23 18:34:15 -0800 | [diff] [blame] | 713 | atomic_inc(&m->bio->bi_remaining); |
| 714 | } |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 715 | cell_error(m->tc->pool, m->cell); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 716 | list_del(&m->list); |
| 717 | mempool_free(m, m->tc->pool->mapping_pool); |
| 718 | } |
Joe Thornber | 025b968 | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 719 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 720 | static void process_prepared_mapping(struct dm_thin_new_mapping *m) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 721 | { |
| 722 | struct thin_c *tc = m->tc; |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 723 | struct pool *pool = tc->pool; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 724 | struct bio *bio; |
| 725 | int r; |
| 726 | |
| 727 | bio = m->bio; |
Kent Overstreet | 196d38b | 2013-11-23 18:34:15 -0800 | [diff] [blame] | 728 | if (bio) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 729 | bio->bi_end_io = m->saved_bi_end_io; |
Kent Overstreet | 196d38b | 2013-11-23 18:34:15 -0800 | [diff] [blame] | 730 | atomic_inc(&bio->bi_remaining); |
| 731 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 732 | |
| 733 | if (m->err) { |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 734 | cell_error(pool, m->cell); |
Joe Thornber | 905386f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 735 | goto out; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Commit the prepared block into the mapping btree. |
| 740 | * Any I/O for this block arriving after this point will get |
| 741 | * remapped to it directly. |
| 742 | */ |
| 743 | r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block); |
| 744 | if (r) { |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 745 | metadata_operation_failed(pool, "dm_thin_insert_block", r); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 746 | cell_error(pool, m->cell); |
Joe Thornber | 905386f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 747 | goto out; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /* |
| 751 | * Release any bios held while the block was being provisioned. |
| 752 | * If we are processing a write bio that completely covers the block, |
| 753 | * we already processed it so can ignore it now when processing |
| 754 | * the bios in the cell. |
| 755 | */ |
| 756 | if (bio) { |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 757 | cell_defer_no_holder(tc, m->cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 758 | bio_endio(bio, 0); |
| 759 | } else |
Joe Thornber | 2aab385 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 760 | cell_defer(tc, m->cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 761 | |
Joe Thornber | 905386f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 762 | out: |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 763 | list_del(&m->list); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 764 | mempool_free(m, pool->mapping_pool); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 767 | static void process_prepared_discard_fail(struct dm_thin_new_mapping *m) |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 768 | { |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 769 | struct thin_c *tc = m->tc; |
| 770 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 771 | bio_io_error(m->bio); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 772 | cell_defer_no_holder(tc, m->cell); |
| 773 | cell_defer_no_holder(tc, m->cell2); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 774 | mempool_free(m, tc->pool->mapping_pool); |
| 775 | } |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 776 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 777 | static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m) |
| 778 | { |
| 779 | struct thin_c *tc = m->tc; |
| 780 | |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 781 | inc_all_io_entry(tc->pool, m->bio); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 782 | cell_defer_no_holder(tc, m->cell); |
| 783 | cell_defer_no_holder(tc, m->cell2); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 784 | |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 785 | if (m->pass_discard) |
Joe Thornber | 19fa1a6 | 2013-12-17 12:09:40 -0500 | [diff] [blame] | 786 | if (m->definitely_not_shared) |
| 787 | remap_and_issue(tc, m->bio, m->data_block); |
| 788 | else { |
| 789 | bool used = false; |
| 790 | if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used) |
| 791 | bio_endio(m->bio, 0); |
| 792 | else |
| 793 | remap_and_issue(tc, m->bio, m->data_block); |
| 794 | } |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 795 | else |
| 796 | bio_endio(m->bio, 0); |
| 797 | |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 798 | mempool_free(m, tc->pool->mapping_pool); |
| 799 | } |
| 800 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 801 | static void process_prepared_discard(struct dm_thin_new_mapping *m) |
| 802 | { |
| 803 | int r; |
| 804 | struct thin_c *tc = m->tc; |
| 805 | |
| 806 | r = dm_thin_remove_block(tc->td, m->virt_block); |
| 807 | if (r) |
Mike Snitzer | c397741 | 2012-12-21 20:23:34 +0000 | [diff] [blame] | 808 | DMERR_LIMIT("dm_thin_remove_block() failed"); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 809 | |
| 810 | process_prepared_discard_passdown(m); |
| 811 | } |
| 812 | |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 813 | static void process_prepared(struct pool *pool, struct list_head *head, |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 814 | process_mapping_fn *fn) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 815 | { |
| 816 | unsigned long flags; |
| 817 | struct list_head maps; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 818 | struct dm_thin_new_mapping *m, *tmp; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 819 | |
| 820 | INIT_LIST_HEAD(&maps); |
| 821 | spin_lock_irqsave(&pool->lock, flags); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 822 | list_splice_init(head, &maps); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 823 | spin_unlock_irqrestore(&pool->lock, flags); |
| 824 | |
| 825 | list_for_each_entry_safe(m, tmp, &maps, list) |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 826 | (*fn)(m); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Deferred bio jobs. |
| 831 | */ |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 832 | static int io_overlaps_block(struct pool *pool, struct bio *bio) |
| 833 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 834 | return bio->bi_iter.bi_size == |
| 835 | (pool->sectors_per_block << SECTOR_SHIFT); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 838 | static int io_overwrites_block(struct pool *pool, struct bio *bio) |
| 839 | { |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 840 | return (bio_data_dir(bio) == WRITE) && |
| 841 | io_overlaps_block(pool, bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static void save_and_set_endio(struct bio *bio, bio_end_io_t **save, |
| 845 | bio_end_io_t *fn) |
| 846 | { |
| 847 | *save = bio->bi_end_io; |
| 848 | bio->bi_end_io = fn; |
| 849 | } |
| 850 | |
| 851 | static int ensure_next_mapping(struct pool *pool) |
| 852 | { |
| 853 | if (pool->next_mapping) |
| 854 | return 0; |
| 855 | |
| 856 | pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC); |
| 857 | |
| 858 | return pool->next_mapping ? 0 : -ENOMEM; |
| 859 | } |
| 860 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 861 | static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 862 | { |
Mike Snitzer | 16961b0 | 2013-12-17 13:19:11 -0500 | [diff] [blame] | 863 | struct dm_thin_new_mapping *m = pool->next_mapping; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 864 | |
| 865 | BUG_ON(!pool->next_mapping); |
| 866 | |
Mike Snitzer | 16961b0 | 2013-12-17 13:19:11 -0500 | [diff] [blame] | 867 | memset(m, 0, sizeof(struct dm_thin_new_mapping)); |
| 868 | INIT_LIST_HEAD(&m->list); |
| 869 | m->bio = NULL; |
| 870 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 871 | pool->next_mapping = NULL; |
| 872 | |
Mike Snitzer | 16961b0 | 2013-12-17 13:19:11 -0500 | [diff] [blame] | 873 | return m; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 876 | static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m, |
| 877 | sector_t begin, sector_t end) |
| 878 | { |
| 879 | int r; |
| 880 | struct dm_io_region to; |
| 881 | |
| 882 | to.bdev = tc->pool_dev->bdev; |
| 883 | to.sector = begin; |
| 884 | to.count = end - begin; |
| 885 | |
| 886 | r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m); |
| 887 | if (r < 0) { |
| 888 | DMERR_LIMIT("dm_kcopyd_zero() failed"); |
| 889 | copy_complete(1, 1, m); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * A partial copy also needs to zero the uncopied region. |
| 895 | */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 896 | static void schedule_copy(struct thin_c *tc, dm_block_t virt_block, |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 897 | struct dm_dev *origin, dm_block_t data_origin, |
| 898 | dm_block_t data_dest, |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 899 | struct dm_bio_prison_cell *cell, struct bio *bio, |
| 900 | sector_t len) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 901 | { |
| 902 | int r; |
| 903 | struct pool *pool = tc->pool; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 904 | struct dm_thin_new_mapping *m = get_next_mapping(pool); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 905 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 906 | m->tc = tc; |
| 907 | m->virt_block = virt_block; |
| 908 | m->data_block = data_dest; |
| 909 | m->cell = cell; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 910 | |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 911 | /* |
| 912 | * quiesce action + copy action + an extra reference held for the |
| 913 | * duration of this function (we may need to inc later for a |
| 914 | * partial zero). |
| 915 | */ |
| 916 | atomic_set(&m->prepare_actions, 3); |
| 917 | |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 918 | if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list)) |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 919 | complete_mapping_preparation(m); /* already quiesced */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 920 | |
| 921 | /* |
| 922 | * IO to pool_dev remaps to the pool target's data_dev. |
| 923 | * |
| 924 | * If the whole block of data is being overwritten, we can issue the |
| 925 | * bio immediately. Otherwise we use kcopyd to clone the data first. |
| 926 | */ |
| 927 | if (io_overwrites_block(pool, bio)) { |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 928 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 929 | |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 930 | h->overwrite_mapping = m; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 931 | m->bio = bio; |
| 932 | save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 933 | inc_all_io_entry(pool, bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 934 | remap_and_issue(tc, bio, data_dest); |
| 935 | } else { |
| 936 | struct dm_io_region from, to; |
| 937 | |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 938 | from.bdev = origin->bdev; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 939 | from.sector = data_origin * pool->sectors_per_block; |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 940 | from.count = len; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 941 | |
| 942 | to.bdev = tc->pool_dev->bdev; |
| 943 | to.sector = data_dest * pool->sectors_per_block; |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 944 | to.count = len; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 945 | |
| 946 | r = dm_kcopyd_copy(pool->copier, &from, 1, &to, |
| 947 | 0, copy_complete, m); |
| 948 | if (r < 0) { |
Mike Snitzer | c397741 | 2012-12-21 20:23:34 +0000 | [diff] [blame] | 949 | DMERR_LIMIT("dm_kcopyd_copy() failed"); |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 950 | copy_complete(1, 1, m); |
| 951 | |
| 952 | /* |
| 953 | * We allow the zero to be issued, to simplify the |
| 954 | * error path. Otherwise we'd need to start |
| 955 | * worrying about decrementing the prepare_actions |
| 956 | * counter. |
| 957 | */ |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * Do we need to zero a tail region? |
| 962 | */ |
| 963 | if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) { |
| 964 | atomic_inc(&m->prepare_actions); |
| 965 | ll_zero(tc, m, |
| 966 | data_dest * pool->sectors_per_block + len, |
| 967 | (data_dest + 1) * pool->sectors_per_block); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 970 | |
| 971 | complete_mapping_preparation(m); /* drop our ref */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 974 | static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block, |
| 975 | dm_block_t data_origin, dm_block_t data_dest, |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 976 | struct dm_bio_prison_cell *cell, struct bio *bio) |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 977 | { |
| 978 | schedule_copy(tc, virt_block, tc->pool_dev, |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 979 | data_origin, data_dest, cell, bio, |
| 980 | tc->pool->sectors_per_block); |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 981 | } |
| 982 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 983 | static void schedule_zero(struct thin_c *tc, dm_block_t virt_block, |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 984 | dm_block_t data_block, struct dm_bio_prison_cell *cell, |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 985 | struct bio *bio) |
| 986 | { |
| 987 | struct pool *pool = tc->pool; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 988 | struct dm_thin_new_mapping *m = get_next_mapping(pool); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 989 | |
Joe Thornber | 50f3c3e | 2014-06-13 13:57:09 +0100 | [diff] [blame] | 990 | atomic_set(&m->prepare_actions, 1); /* no need to quiesce */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 991 | m->tc = tc; |
| 992 | m->virt_block = virt_block; |
| 993 | m->data_block = data_block; |
| 994 | m->cell = cell; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 995 | |
| 996 | /* |
| 997 | * If the whole block of data is being overwritten or we are not |
| 998 | * zeroing pre-existing data, we can issue the bio immediately. |
| 999 | * Otherwise we use kcopyd to zero the data first. |
| 1000 | */ |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 1001 | if (!pool->pf.zero_new_blocks) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1002 | process_prepared_mapping(m); |
| 1003 | |
| 1004 | else if (io_overwrites_block(pool, bio)) { |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1005 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 1006 | |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1007 | h->overwrite_mapping = m; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1008 | m->bio = bio; |
| 1009 | save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1010 | inc_all_io_entry(pool, bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1011 | remap_and_issue(tc, bio, data_block); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1012 | |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 1013 | } else |
| 1014 | ll_zero(tc, m, |
| 1015 | data_block * pool->sectors_per_block, |
| 1016 | (data_block + 1) * pool->sectors_per_block); |
| 1017 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1018 | |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 1019 | static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block, |
| 1020 | dm_block_t data_dest, |
| 1021 | struct dm_bio_prison_cell *cell, struct bio *bio) |
| 1022 | { |
| 1023 | struct pool *pool = tc->pool; |
| 1024 | sector_t virt_block_begin = virt_block * pool->sectors_per_block; |
| 1025 | sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block; |
| 1026 | |
| 1027 | if (virt_block_end <= tc->origin_size) |
| 1028 | schedule_copy(tc, virt_block, tc->origin_dev, |
| 1029 | virt_block, data_dest, cell, bio, |
| 1030 | pool->sectors_per_block); |
| 1031 | |
| 1032 | else if (virt_block_begin < tc->origin_size) |
| 1033 | schedule_copy(tc, virt_block, tc->origin_dev, |
| 1034 | virt_block, data_dest, cell, bio, |
| 1035 | tc->origin_size - virt_block_begin); |
| 1036 | |
| 1037 | else |
| 1038 | schedule_zero(tc, virt_block, data_dest, cell, bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1041 | /* |
| 1042 | * A non-zero return indicates read_only or fail_io mode. |
| 1043 | * Many callers don't care about the return value. |
| 1044 | */ |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 1045 | static int commit(struct pool *pool) |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1046 | { |
| 1047 | int r; |
| 1048 | |
Joe Thornber | 8d07e8a | 2014-05-06 16:28:14 +0100 | [diff] [blame] | 1049 | if (get_pool_mode(pool) >= PM_READ_ONLY) |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1050 | return -EINVAL; |
| 1051 | |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 1052 | r = dm_pool_commit_metadata(pool->pmd); |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 1053 | if (r) |
| 1054 | metadata_operation_failed(pool, "dm_pool_commit_metadata", r); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1055 | |
| 1056 | return r; |
| 1057 | } |
| 1058 | |
Joe Thornber | 88a6621 | 2013-12-04 20:16:12 -0500 | [diff] [blame] | 1059 | static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks) |
| 1060 | { |
| 1061 | unsigned long flags; |
| 1062 | |
| 1063 | if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) { |
| 1064 | DMWARN("%s: reached low water mark for data device: sending event.", |
| 1065 | dm_device_name(pool->pool_md)); |
| 1066 | spin_lock_irqsave(&pool->lock, flags); |
| 1067 | pool->low_water_triggered = true; |
| 1068 | spin_unlock_irqrestore(&pool->lock, flags); |
| 1069 | dm_table_event(pool->ti->table); |
| 1070 | } |
| 1071 | } |
| 1072 | |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1073 | static void set_pool_mode(struct pool *pool, enum pool_mode new_mode); |
| 1074 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1075 | static int alloc_data_block(struct thin_c *tc, dm_block_t *result) |
| 1076 | { |
| 1077 | int r; |
| 1078 | dm_block_t free_blocks; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1079 | struct pool *pool = tc->pool; |
| 1080 | |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1081 | if (WARN_ON(get_pool_mode(pool) != PM_WRITE)) |
Joe Thornber | 8d30abf | 2013-12-04 19:16:11 -0500 | [diff] [blame] | 1082 | return -EINVAL; |
| 1083 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1084 | r = dm_pool_get_free_block_count(pool->pmd, &free_blocks); |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 1085 | if (r) { |
| 1086 | metadata_operation_failed(pool, "dm_pool_get_free_block_count", r); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1087 | return r; |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 1088 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1089 | |
Joe Thornber | 88a6621 | 2013-12-04 20:16:12 -0500 | [diff] [blame] | 1090 | check_low_water_mark(pool, free_blocks); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (!free_blocks) { |
Mike Snitzer | 94563ba | 2013-08-22 09:56:18 -0400 | [diff] [blame] | 1093 | /* |
| 1094 | * Try to commit to see if that will free up some |
| 1095 | * more space. |
| 1096 | */ |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 1097 | r = commit(pool); |
| 1098 | if (r) |
| 1099 | return r; |
Mike Snitzer | 94563ba | 2013-08-22 09:56:18 -0400 | [diff] [blame] | 1100 | |
| 1101 | r = dm_pool_get_free_block_count(pool->pmd, &free_blocks); |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 1102 | if (r) { |
| 1103 | metadata_operation_failed(pool, "dm_pool_get_free_block_count", r); |
Mike Snitzer | 94563ba | 2013-08-22 09:56:18 -0400 | [diff] [blame] | 1104 | return r; |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 1105 | } |
Mike Snitzer | 94563ba | 2013-08-22 09:56:18 -0400 | [diff] [blame] | 1106 | |
Mike Snitzer | 94563ba | 2013-08-22 09:56:18 -0400 | [diff] [blame] | 1107 | if (!free_blocks) { |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1108 | set_pool_mode(pool, PM_OUT_OF_DATA_SPACE); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1109 | return -ENOSPC; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | r = dm_pool_alloc_data_block(pool->pmd, result); |
Mike Snitzer | 4a02b34 | 2013-12-03 12:20:57 -0500 | [diff] [blame] | 1114 | if (r) { |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 1115 | metadata_operation_failed(pool, "dm_pool_alloc_data_block", r); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1116 | return r; |
Mike Snitzer | 4a02b34 | 2013-12-03 12:20:57 -0500 | [diff] [blame] | 1117 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * If we have run out of space, queue bios until the device is |
| 1124 | * resumed, presumably after having been reloaded with more space. |
| 1125 | */ |
| 1126 | static void retry_on_resume(struct bio *bio) |
| 1127 | { |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1128 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1129 | struct thin_c *tc = h->tc; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1130 | unsigned long flags; |
| 1131 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1132 | spin_lock_irqsave(&tc->lock, flags); |
| 1133 | bio_list_add(&tc->retry_on_resume_list, bio); |
| 1134 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1137 | static int should_error_unserviceable_bio(struct pool *pool) |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1138 | { |
| 1139 | enum pool_mode m = get_pool_mode(pool); |
| 1140 | |
| 1141 | switch (m) { |
| 1142 | case PM_WRITE: |
| 1143 | /* Shouldn't get here */ |
| 1144 | DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode"); |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1145 | return -EIO; |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1146 | |
| 1147 | case PM_OUT_OF_DATA_SPACE: |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1148 | return pool->pf.error_if_no_space ? -ENOSPC : 0; |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1149 | |
| 1150 | case PM_READ_ONLY: |
| 1151 | case PM_FAIL: |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1152 | return -EIO; |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1153 | default: |
| 1154 | /* Shouldn't get here */ |
| 1155 | DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode"); |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1156 | return -EIO; |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
Mike Snitzer | 8c0f0e8 | 2013-12-05 15:47:24 -0500 | [diff] [blame] | 1160 | static void handle_unserviceable_bio(struct pool *pool, struct bio *bio) |
| 1161 | { |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1162 | int error = should_error_unserviceable_bio(pool); |
| 1163 | |
| 1164 | if (error) |
| 1165 | bio_endio(bio, error); |
Mike Snitzer | 6d16202 | 2013-12-20 18:09:02 -0500 | [diff] [blame] | 1166 | else |
| 1167 | retry_on_resume(bio); |
Mike Snitzer | 8c0f0e8 | 2013-12-05 15:47:24 -0500 | [diff] [blame] | 1168 | } |
| 1169 | |
Mike Snitzer | 399cadd | 2013-12-05 16:03:33 -0500 | [diff] [blame] | 1170 | static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1171 | { |
| 1172 | struct bio *bio; |
| 1173 | struct bio_list bios; |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1174 | int error; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1175 | |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1176 | error = should_error_unserviceable_bio(pool); |
| 1177 | if (error) { |
| 1178 | cell_error_with_code(pool, cell, error); |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1179 | return; |
| 1180 | } |
| 1181 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1182 | bio_list_init(&bios); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1183 | cell_release(pool, cell, &bios); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1184 | |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1185 | error = should_error_unserviceable_bio(pool); |
| 1186 | if (error) |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1187 | while ((bio = bio_list_pop(&bios))) |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 1188 | bio_endio(bio, error); |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1189 | else |
| 1190 | while ((bio = bio_list_pop(&bios))) |
| 1191 | retry_on_resume(bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1194 | static void process_discard(struct thin_c *tc, struct bio *bio) |
| 1195 | { |
| 1196 | int r; |
Mike Snitzer | c3a0ce2 | 2012-05-12 01:43:16 +0100 | [diff] [blame] | 1197 | unsigned long flags; |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1198 | struct pool *pool = tc->pool; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 1199 | struct dm_bio_prison_cell *cell, *cell2; |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 1200 | struct dm_cell_key key, key2; |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1201 | dm_block_t block = get_bio_block(tc, bio); |
| 1202 | struct dm_thin_lookup_result lookup_result; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 1203 | struct dm_thin_new_mapping *m; |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1204 | |
| 1205 | build_virtual_key(tc->td, block, &key); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1206 | if (bio_detain(tc->pool, &key, bio, &cell)) |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1207 | return; |
| 1208 | |
| 1209 | r = dm_thin_find_block(tc->td, block, 1, &lookup_result); |
| 1210 | switch (r) { |
| 1211 | case 0: |
| 1212 | /* |
| 1213 | * Check nobody is fiddling with this pool block. This can |
| 1214 | * happen if someone's in the process of breaking sharing |
| 1215 | * on this block. |
| 1216 | */ |
| 1217 | build_data_key(tc->td, lookup_result.block, &key2); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1218 | if (bio_detain(tc->pool, &key2, bio, &cell2)) { |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1219 | cell_defer_no_holder(tc, cell); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1220 | break; |
| 1221 | } |
| 1222 | |
| 1223 | if (io_overlaps_block(pool, bio)) { |
| 1224 | /* |
| 1225 | * IO may still be going to the destination block. We must |
| 1226 | * quiesce before we can do the removal. |
| 1227 | */ |
| 1228 | m = get_next_mapping(pool); |
| 1229 | m->tc = tc; |
Joe Thornber | 19fa1a6 | 2013-12-17 12:09:40 -0500 | [diff] [blame] | 1230 | m->pass_discard = pool->pf.discard_passdown; |
| 1231 | m->definitely_not_shared = !lookup_result.shared; |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1232 | m->virt_block = block; |
| 1233 | m->data_block = lookup_result.block; |
| 1234 | m->cell = cell; |
| 1235 | m->cell2 = cell2; |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1236 | m->bio = bio; |
| 1237 | |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 1238 | if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) { |
Mike Snitzer | c3a0ce2 | 2012-05-12 01:43:16 +0100 | [diff] [blame] | 1239 | spin_lock_irqsave(&pool->lock, flags); |
Mike Snitzer | daec338 | 2013-12-11 14:01:20 -0500 | [diff] [blame] | 1240 | list_add_tail(&m->list, &pool->prepared_discards); |
Mike Snitzer | c3a0ce2 | 2012-05-12 01:43:16 +0100 | [diff] [blame] | 1241 | spin_unlock_irqrestore(&pool->lock, flags); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1242 | wake_worker(pool); |
| 1243 | } |
| 1244 | } else { |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1245 | inc_all_io_entry(pool, bio); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1246 | cell_defer_no_holder(tc, cell); |
| 1247 | cell_defer_no_holder(tc, cell2); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1248 | |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1249 | /* |
Mikulas Patocka | 4929630 | 2012-07-27 15:08:03 +0100 | [diff] [blame] | 1250 | * The DM core makes sure that the discard doesn't span |
| 1251 | * a block boundary. So we submit the discard of a |
| 1252 | * partial block appropriately. |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1253 | */ |
Mikulas Patocka | 650d2a0 | 2012-07-20 14:25:05 +0100 | [diff] [blame] | 1254 | if ((!lookup_result.shared) && pool->pf.discard_passdown) |
| 1255 | remap_and_issue(tc, bio, lookup_result.block); |
| 1256 | else |
| 1257 | bio_endio(bio, 0); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1258 | } |
| 1259 | break; |
| 1260 | |
| 1261 | case -ENODATA: |
| 1262 | /* |
| 1263 | * It isn't provisioned, just forget it. |
| 1264 | */ |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1265 | cell_defer_no_holder(tc, cell); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1266 | bio_endio(bio, 0); |
| 1267 | break; |
| 1268 | |
| 1269 | default: |
Mike Snitzer | c397741 | 2012-12-21 20:23:34 +0000 | [diff] [blame] | 1270 | DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", |
| 1271 | __func__, r); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1272 | cell_defer_no_holder(tc, cell); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1273 | bio_io_error(bio); |
| 1274 | break; |
| 1275 | } |
| 1276 | } |
| 1277 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1278 | static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block, |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 1279 | struct dm_cell_key *key, |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1280 | struct dm_thin_lookup_result *lookup_result, |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 1281 | struct dm_bio_prison_cell *cell) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1282 | { |
| 1283 | int r; |
| 1284 | dm_block_t data_block; |
Mike Snitzer | d6fc204 | 2013-08-21 17:40:11 -0400 | [diff] [blame] | 1285 | struct pool *pool = tc->pool; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1286 | |
| 1287 | r = alloc_data_block(tc, &data_block); |
| 1288 | switch (r) { |
| 1289 | case 0: |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1290 | schedule_internal_copy(tc, block, lookup_result->block, |
| 1291 | data_block, cell, bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1292 | break; |
| 1293 | |
| 1294 | case -ENOSPC: |
Mike Snitzer | 399cadd | 2013-12-05 16:03:33 -0500 | [diff] [blame] | 1295 | retry_bios_on_resume(pool, cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1296 | break; |
| 1297 | |
| 1298 | default: |
Mike Snitzer | c397741 | 2012-12-21 20:23:34 +0000 | [diff] [blame] | 1299 | DMERR_LIMIT("%s: alloc_data_block() failed: error = %d", |
| 1300 | __func__, r); |
Mike Snitzer | d6fc204 | 2013-08-21 17:40:11 -0400 | [diff] [blame] | 1301 | cell_error(pool, cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1302 | break; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | static void process_shared_bio(struct thin_c *tc, struct bio *bio, |
| 1307 | dm_block_t block, |
| 1308 | struct dm_thin_lookup_result *lookup_result) |
| 1309 | { |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 1310 | struct dm_bio_prison_cell *cell; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1311 | struct pool *pool = tc->pool; |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 1312 | struct dm_cell_key key; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1313 | |
| 1314 | /* |
| 1315 | * If cell is already occupied, then sharing is already in the process |
| 1316 | * of being broken so we have nothing further to do here. |
| 1317 | */ |
| 1318 | build_data_key(tc->td, lookup_result->block, &key); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1319 | if (bio_detain(pool, &key, bio, &cell)) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1320 | return; |
| 1321 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1322 | if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1323 | break_sharing(tc, bio, block, &key, lookup_result, cell); |
| 1324 | else { |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1325 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1326 | |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 1327 | h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1328 | inc_all_io_entry(pool, bio); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1329 | cell_defer_no_holder(tc, cell); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1330 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1331 | remap_and_issue(tc, bio, lookup_result->block); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block, |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 1336 | struct dm_bio_prison_cell *cell) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1337 | { |
| 1338 | int r; |
| 1339 | dm_block_t data_block; |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1340 | struct pool *pool = tc->pool; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1341 | |
| 1342 | /* |
| 1343 | * Remap empty bios (flushes) immediately, without provisioning. |
| 1344 | */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1345 | if (!bio->bi_iter.bi_size) { |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1346 | inc_all_io_entry(pool, bio); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1347 | cell_defer_no_holder(tc, cell); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1348 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1349 | remap_and_issue(tc, bio, 0); |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | /* |
| 1354 | * Fill read bios with zeroes and complete them immediately. |
| 1355 | */ |
| 1356 | if (bio_data_dir(bio) == READ) { |
| 1357 | zero_fill_bio(bio); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1358 | cell_defer_no_holder(tc, cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1359 | bio_endio(bio, 0); |
| 1360 | return; |
| 1361 | } |
| 1362 | |
| 1363 | r = alloc_data_block(tc, &data_block); |
| 1364 | switch (r) { |
| 1365 | case 0: |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1366 | if (tc->origin_dev) |
| 1367 | schedule_external_copy(tc, block, data_block, cell, bio); |
| 1368 | else |
| 1369 | schedule_zero(tc, block, data_block, cell, bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | |
| 1372 | case -ENOSPC: |
Mike Snitzer | 399cadd | 2013-12-05 16:03:33 -0500 | [diff] [blame] | 1373 | retry_bios_on_resume(pool, cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1374 | break; |
| 1375 | |
| 1376 | default: |
Mike Snitzer | c397741 | 2012-12-21 20:23:34 +0000 | [diff] [blame] | 1377 | DMERR_LIMIT("%s: alloc_data_block() failed: error = %d", |
| 1378 | __func__, r); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1379 | cell_error(pool, cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1380 | break; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | static void process_bio(struct thin_c *tc, struct bio *bio) |
| 1385 | { |
| 1386 | int r; |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1387 | struct pool *pool = tc->pool; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1388 | dm_block_t block = get_bio_block(tc, bio); |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 1389 | struct dm_bio_prison_cell *cell; |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 1390 | struct dm_cell_key key; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1391 | struct dm_thin_lookup_result lookup_result; |
| 1392 | |
| 1393 | /* |
| 1394 | * If cell is already occupied, then the block is already |
| 1395 | * being provisioned so we have nothing further to do here. |
| 1396 | */ |
| 1397 | build_virtual_key(tc->td, block, &key); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1398 | if (bio_detain(pool, &key, bio, &cell)) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | |
| 1401 | r = dm_thin_find_block(tc->td, block, 1, &lookup_result); |
| 1402 | switch (r) { |
| 1403 | case 0: |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1404 | if (lookup_result.shared) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1405 | process_shared_bio(tc, bio, block, &lookup_result); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1406 | cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */ |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1407 | } else { |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1408 | inc_all_io_entry(pool, bio); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1409 | cell_defer_no_holder(tc, cell); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1410 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1411 | remap_and_issue(tc, bio, lookup_result.block); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1412 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | |
| 1415 | case -ENODATA: |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1416 | if (bio_data_dir(bio) == READ && tc->origin_dev) { |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1417 | inc_all_io_entry(pool, bio); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1418 | cell_defer_no_holder(tc, cell); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1419 | |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 1420 | if (bio_end_sector(bio) <= tc->origin_size) |
| 1421 | remap_to_origin_and_issue(tc, bio); |
| 1422 | |
| 1423 | else if (bio->bi_iter.bi_sector < tc->origin_size) { |
| 1424 | zero_fill_bio(bio); |
| 1425 | bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT; |
| 1426 | remap_to_origin_and_issue(tc, bio); |
| 1427 | |
| 1428 | } else { |
| 1429 | zero_fill_bio(bio); |
| 1430 | bio_endio(bio, 0); |
| 1431 | } |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1432 | } else |
| 1433 | provision_block(tc, bio, block, cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1434 | break; |
| 1435 | |
| 1436 | default: |
Mike Snitzer | c397741 | 2012-12-21 20:23:34 +0000 | [diff] [blame] | 1437 | DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", |
| 1438 | __func__, r); |
Joe Thornber | f286ba0 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 1439 | cell_defer_no_holder(tc, cell); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1440 | bio_io_error(bio); |
| 1441 | break; |
| 1442 | } |
| 1443 | } |
| 1444 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1445 | static void process_bio_read_only(struct thin_c *tc, struct bio *bio) |
| 1446 | { |
| 1447 | int r; |
| 1448 | int rw = bio_data_dir(bio); |
| 1449 | dm_block_t block = get_bio_block(tc, bio); |
| 1450 | struct dm_thin_lookup_result lookup_result; |
| 1451 | |
| 1452 | r = dm_thin_find_block(tc->td, block, 1, &lookup_result); |
| 1453 | switch (r) { |
| 1454 | case 0: |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1455 | if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) |
Mike Snitzer | 8c0f0e8 | 2013-12-05 15:47:24 -0500 | [diff] [blame] | 1456 | handle_unserviceable_bio(tc->pool, bio); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1457 | else { |
| 1458 | inc_all_io_entry(tc->pool, bio); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1459 | remap_and_issue(tc, bio, lookup_result.block); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1460 | } |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1461 | break; |
| 1462 | |
| 1463 | case -ENODATA: |
| 1464 | if (rw != READ) { |
Mike Snitzer | 8c0f0e8 | 2013-12-05 15:47:24 -0500 | [diff] [blame] | 1465 | handle_unserviceable_bio(tc->pool, bio); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1466 | break; |
| 1467 | } |
| 1468 | |
| 1469 | if (tc->origin_dev) { |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1470 | inc_all_io_entry(tc->pool, bio); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1471 | remap_to_origin_and_issue(tc, bio); |
| 1472 | break; |
| 1473 | } |
| 1474 | |
| 1475 | zero_fill_bio(bio); |
| 1476 | bio_endio(bio, 0); |
| 1477 | break; |
| 1478 | |
| 1479 | default: |
Mike Snitzer | c397741 | 2012-12-21 20:23:34 +0000 | [diff] [blame] | 1480 | DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", |
| 1481 | __func__, r); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1482 | bio_io_error(bio); |
| 1483 | break; |
| 1484 | } |
| 1485 | } |
| 1486 | |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1487 | static void process_bio_success(struct thin_c *tc, struct bio *bio) |
| 1488 | { |
| 1489 | bio_endio(bio, 0); |
| 1490 | } |
| 1491 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1492 | static void process_bio_fail(struct thin_c *tc, struct bio *bio) |
| 1493 | { |
| 1494 | bio_io_error(bio); |
| 1495 | } |
| 1496 | |
Joe Thornber | ac8c3f3 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1497 | /* |
| 1498 | * FIXME: should we also commit due to size of transaction, measured in |
| 1499 | * metadata blocks? |
| 1500 | */ |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 1501 | static int need_commit_due_to_time(struct pool *pool) |
| 1502 | { |
| 1503 | return jiffies < pool->last_commit_jiffies || |
| 1504 | jiffies > pool->last_commit_jiffies + COMMIT_PERIOD; |
| 1505 | } |
| 1506 | |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 1507 | #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node) |
| 1508 | #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook)) |
| 1509 | |
| 1510 | static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio) |
| 1511 | { |
| 1512 | struct rb_node **rbp, *parent; |
| 1513 | struct dm_thin_endio_hook *pbd; |
| 1514 | sector_t bi_sector = bio->bi_iter.bi_sector; |
| 1515 | |
| 1516 | rbp = &tc->sort_bio_list.rb_node; |
| 1517 | parent = NULL; |
| 1518 | while (*rbp) { |
| 1519 | parent = *rbp; |
| 1520 | pbd = thin_pbd(parent); |
| 1521 | |
| 1522 | if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector) |
| 1523 | rbp = &(*rbp)->rb_left; |
| 1524 | else |
| 1525 | rbp = &(*rbp)->rb_right; |
| 1526 | } |
| 1527 | |
| 1528 | pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
| 1529 | rb_link_node(&pbd->rb_node, parent, rbp); |
| 1530 | rb_insert_color(&pbd->rb_node, &tc->sort_bio_list); |
| 1531 | } |
| 1532 | |
| 1533 | static void __extract_sorted_bios(struct thin_c *tc) |
| 1534 | { |
| 1535 | struct rb_node *node; |
| 1536 | struct dm_thin_endio_hook *pbd; |
| 1537 | struct bio *bio; |
| 1538 | |
| 1539 | for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) { |
| 1540 | pbd = thin_pbd(node); |
| 1541 | bio = thin_bio(pbd); |
| 1542 | |
| 1543 | bio_list_add(&tc->deferred_bio_list, bio); |
| 1544 | rb_erase(&pbd->rb_node, &tc->sort_bio_list); |
| 1545 | } |
| 1546 | |
| 1547 | WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list)); |
| 1548 | } |
| 1549 | |
| 1550 | static void __sort_thin_deferred_bios(struct thin_c *tc) |
| 1551 | { |
| 1552 | struct bio *bio; |
| 1553 | struct bio_list bios; |
| 1554 | |
| 1555 | bio_list_init(&bios); |
| 1556 | bio_list_merge(&bios, &tc->deferred_bio_list); |
| 1557 | bio_list_init(&tc->deferred_bio_list); |
| 1558 | |
| 1559 | /* Sort deferred_bio_list using rb-tree */ |
| 1560 | while ((bio = bio_list_pop(&bios))) |
| 1561 | __thin_bio_rb_add(tc, bio); |
| 1562 | |
| 1563 | /* |
| 1564 | * Transfer the sorted bios in sort_bio_list back to |
| 1565 | * deferred_bio_list to allow lockless submission of |
| 1566 | * all bios. |
| 1567 | */ |
| 1568 | __extract_sorted_bios(tc); |
| 1569 | } |
| 1570 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1571 | static void process_thin_deferred_bios(struct thin_c *tc) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1572 | { |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1573 | struct pool *pool = tc->pool; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1574 | unsigned long flags; |
| 1575 | struct bio *bio; |
| 1576 | struct bio_list bios; |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 1577 | struct blk_plug plug; |
Joe Thornber | 8a01a6a | 2014-10-06 15:28:30 +0100 | [diff] [blame] | 1578 | unsigned count = 0; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1579 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1580 | if (tc->requeue_mode) { |
| 1581 | requeue_bio_list(tc, &tc->deferred_bio_list); |
| 1582 | return; |
| 1583 | } |
| 1584 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1585 | bio_list_init(&bios); |
| 1586 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1587 | spin_lock_irqsave(&tc->lock, flags); |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 1588 | |
| 1589 | if (bio_list_empty(&tc->deferred_bio_list)) { |
| 1590 | spin_unlock_irqrestore(&tc->lock, flags); |
| 1591 | return; |
| 1592 | } |
| 1593 | |
| 1594 | __sort_thin_deferred_bios(tc); |
| 1595 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1596 | bio_list_merge(&bios, &tc->deferred_bio_list); |
| 1597 | bio_list_init(&tc->deferred_bio_list); |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 1598 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1599 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1600 | |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 1601 | blk_start_plug(&plug); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1602 | while ((bio = bio_list_pop(&bios))) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1603 | /* |
| 1604 | * If we've got no free new_mapping structs, and processing |
| 1605 | * this bio might require one, we pause until there are some |
| 1606 | * prepared mappings to process. |
| 1607 | */ |
| 1608 | if (ensure_next_mapping(pool)) { |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1609 | spin_lock_irqsave(&tc->lock, flags); |
| 1610 | bio_list_add(&tc->deferred_bio_list, bio); |
| 1611 | bio_list_merge(&tc->deferred_bio_list, &bios); |
| 1612 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1613 | break; |
| 1614 | } |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1615 | |
| 1616 | if (bio->bi_rw & REQ_DISCARD) |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1617 | pool->process_discard(tc, bio); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1618 | else |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1619 | pool->process_bio(tc, bio); |
Joe Thornber | 8a01a6a | 2014-10-06 15:28:30 +0100 | [diff] [blame] | 1620 | |
| 1621 | if ((count++ & 127) == 0) { |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 1622 | throttle_work_update(&pool->throttle); |
Joe Thornber | 8a01a6a | 2014-10-06 15:28:30 +0100 | [diff] [blame] | 1623 | dm_pool_issue_prefetches(pool->pmd); |
| 1624 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1625 | } |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 1626 | blk_finish_plug(&plug); |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
Joe Thornber | b10ebd3 | 2014-04-08 11:29:01 +0100 | [diff] [blame] | 1629 | static void thin_get(struct thin_c *tc); |
| 1630 | static void thin_put(struct thin_c *tc); |
| 1631 | |
| 1632 | /* |
| 1633 | * We can't hold rcu_read_lock() around code that can block. So we |
| 1634 | * find a thin with the rcu lock held; bump a refcount; then drop |
| 1635 | * the lock. |
| 1636 | */ |
| 1637 | static struct thin_c *get_first_thin(struct pool *pool) |
| 1638 | { |
| 1639 | struct thin_c *tc = NULL; |
| 1640 | |
| 1641 | rcu_read_lock(); |
| 1642 | if (!list_empty(&pool->active_thins)) { |
| 1643 | tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list); |
| 1644 | thin_get(tc); |
| 1645 | } |
| 1646 | rcu_read_unlock(); |
| 1647 | |
| 1648 | return tc; |
| 1649 | } |
| 1650 | |
| 1651 | static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc) |
| 1652 | { |
| 1653 | struct thin_c *old_tc = tc; |
| 1654 | |
| 1655 | rcu_read_lock(); |
| 1656 | list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) { |
| 1657 | thin_get(tc); |
| 1658 | thin_put(old_tc); |
| 1659 | rcu_read_unlock(); |
| 1660 | return tc; |
| 1661 | } |
| 1662 | thin_put(old_tc); |
| 1663 | rcu_read_unlock(); |
| 1664 | |
| 1665 | return NULL; |
| 1666 | } |
| 1667 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1668 | static void process_deferred_bios(struct pool *pool) |
| 1669 | { |
| 1670 | unsigned long flags; |
| 1671 | struct bio *bio; |
| 1672 | struct bio_list bios; |
| 1673 | struct thin_c *tc; |
| 1674 | |
Joe Thornber | b10ebd3 | 2014-04-08 11:29:01 +0100 | [diff] [blame] | 1675 | tc = get_first_thin(pool); |
| 1676 | while (tc) { |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1677 | process_thin_deferred_bios(tc); |
Joe Thornber | b10ebd3 | 2014-04-08 11:29:01 +0100 | [diff] [blame] | 1678 | tc = get_next_thin(pool, tc); |
| 1679 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1680 | |
| 1681 | /* |
| 1682 | * If there are any deferred flush bios, we must commit |
| 1683 | * the metadata before issuing them. |
| 1684 | */ |
| 1685 | bio_list_init(&bios); |
| 1686 | spin_lock_irqsave(&pool->lock, flags); |
| 1687 | bio_list_merge(&bios, &pool->deferred_flush_bios); |
| 1688 | bio_list_init(&pool->deferred_flush_bios); |
| 1689 | spin_unlock_irqrestore(&pool->lock, flags); |
| 1690 | |
Mike Snitzer | 4d1662a | 2014-02-06 06:08:56 -0500 | [diff] [blame] | 1691 | if (bio_list_empty(&bios) && |
| 1692 | !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool))) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1693 | return; |
| 1694 | |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 1695 | if (commit(pool)) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1696 | while ((bio = bio_list_pop(&bios))) |
| 1697 | bio_io_error(bio); |
| 1698 | return; |
| 1699 | } |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 1700 | pool->last_commit_jiffies = jiffies; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1701 | |
| 1702 | while ((bio = bio_list_pop(&bios))) |
| 1703 | generic_make_request(bio); |
| 1704 | } |
| 1705 | |
| 1706 | static void do_worker(struct work_struct *ws) |
| 1707 | { |
| 1708 | struct pool *pool = container_of(ws, struct pool, worker); |
| 1709 | |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 1710 | throttle_work_start(&pool->throttle); |
Joe Thornber | 8a01a6a | 2014-10-06 15:28:30 +0100 | [diff] [blame] | 1711 | dm_pool_issue_prefetches(pool->pmd); |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 1712 | throttle_work_update(&pool->throttle); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1713 | process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping); |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 1714 | throttle_work_update(&pool->throttle); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1715 | process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard); |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 1716 | throttle_work_update(&pool->throttle); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1717 | process_deferred_bios(pool); |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 1718 | throttle_work_complete(&pool->throttle); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 1721 | /* |
| 1722 | * We want to commit periodically so that not too much |
| 1723 | * unwritten data builds up. |
| 1724 | */ |
| 1725 | static void do_waker(struct work_struct *ws) |
| 1726 | { |
| 1727 | struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker); |
| 1728 | wake_worker(pool); |
| 1729 | queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD); |
| 1730 | } |
| 1731 | |
Joe Thornber | 85ad643b | 2014-05-09 15:59:38 +0100 | [diff] [blame] | 1732 | /* |
| 1733 | * We're holding onto IO to allow userland time to react. After the |
| 1734 | * timeout either the pool will have been resized (and thus back in |
| 1735 | * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO. |
| 1736 | */ |
| 1737 | static void do_no_space_timeout(struct work_struct *ws) |
| 1738 | { |
| 1739 | struct pool *pool = container_of(to_delayed_work(ws), struct pool, |
| 1740 | no_space_timeout); |
| 1741 | |
| 1742 | if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) |
| 1743 | set_pool_mode(pool, PM_READ_ONLY); |
| 1744 | } |
| 1745 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1746 | /*----------------------------------------------------------------*/ |
| 1747 | |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1748 | struct pool_work { |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1749 | struct work_struct worker; |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1750 | struct completion complete; |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1751 | }; |
| 1752 | |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1753 | static struct pool_work *to_pool_work(struct work_struct *ws) |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1754 | { |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1755 | return container_of(ws, struct pool_work, worker); |
| 1756 | } |
| 1757 | |
| 1758 | static void pool_work_complete(struct pool_work *pw) |
| 1759 | { |
| 1760 | complete(&pw->complete); |
| 1761 | } |
| 1762 | |
| 1763 | static void pool_work_wait(struct pool_work *pw, struct pool *pool, |
| 1764 | void (*fn)(struct work_struct *)) |
| 1765 | { |
| 1766 | INIT_WORK_ONSTACK(&pw->worker, fn); |
| 1767 | init_completion(&pw->complete); |
| 1768 | queue_work(pool->wq, &pw->worker); |
| 1769 | wait_for_completion(&pw->complete); |
| 1770 | } |
| 1771 | |
| 1772 | /*----------------------------------------------------------------*/ |
| 1773 | |
| 1774 | struct noflush_work { |
| 1775 | struct pool_work pw; |
| 1776 | struct thin_c *tc; |
| 1777 | }; |
| 1778 | |
| 1779 | static struct noflush_work *to_noflush(struct work_struct *ws) |
| 1780 | { |
| 1781 | return container_of(to_pool_work(ws), struct noflush_work, pw); |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | static void do_noflush_start(struct work_struct *ws) |
| 1785 | { |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1786 | struct noflush_work *w = to_noflush(ws); |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1787 | w->tc->requeue_mode = true; |
| 1788 | requeue_io(w->tc); |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1789 | pool_work_complete(&w->pw); |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | static void do_noflush_stop(struct work_struct *ws) |
| 1793 | { |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1794 | struct noflush_work *w = to_noflush(ws); |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1795 | w->tc->requeue_mode = false; |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1796 | pool_work_complete(&w->pw); |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *)) |
| 1800 | { |
| 1801 | struct noflush_work w; |
| 1802 | |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1803 | w.tc = tc; |
Joe Thornber | e7a3e87 | 2014-05-13 16:14:14 -0400 | [diff] [blame] | 1804 | pool_work_wait(&w.pw, tc->pool, fn); |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | /*----------------------------------------------------------------*/ |
| 1808 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1809 | static enum pool_mode get_pool_mode(struct pool *pool) |
| 1810 | { |
| 1811 | return pool->pf.mode; |
| 1812 | } |
| 1813 | |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1814 | static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode) |
| 1815 | { |
| 1816 | dm_table_event(pool->ti->table); |
| 1817 | DMINFO("%s: switching pool to %s mode", |
| 1818 | dm_device_name(pool->pool_md), new_mode); |
| 1819 | } |
| 1820 | |
Mike Snitzer | 8b64e88 | 2013-12-20 14:27:28 -0500 | [diff] [blame] | 1821 | static void set_pool_mode(struct pool *pool, enum pool_mode new_mode) |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1822 | { |
Mike Snitzer | cdc2b41 | 2014-02-14 18:10:55 -0500 | [diff] [blame] | 1823 | struct pool_c *pt = pool->ti->private; |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 1824 | bool needs_check = dm_pool_metadata_needs_check(pool->pmd); |
| 1825 | enum pool_mode old_mode = get_pool_mode(pool); |
Mike Snitzer | 80c5789 | 2014-05-20 13:38:33 -0400 | [diff] [blame] | 1826 | unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ; |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 1827 | |
| 1828 | /* |
| 1829 | * Never allow the pool to transition to PM_WRITE mode if user |
| 1830 | * intervention is required to verify metadata and data consistency. |
| 1831 | */ |
| 1832 | if (new_mode == PM_WRITE && needs_check) { |
| 1833 | DMERR("%s: unable to switch pool to write mode until repaired.", |
| 1834 | dm_device_name(pool->pool_md)); |
| 1835 | if (old_mode != new_mode) |
| 1836 | new_mode = old_mode; |
| 1837 | else |
| 1838 | new_mode = PM_READ_ONLY; |
| 1839 | } |
| 1840 | /* |
| 1841 | * If we were in PM_FAIL mode, rollback of metadata failed. We're |
| 1842 | * not going to recover without a thin_repair. So we never let the |
| 1843 | * pool move out of the old mode. |
| 1844 | */ |
| 1845 | if (old_mode == PM_FAIL) |
| 1846 | new_mode = old_mode; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1847 | |
Mike Snitzer | 8b64e88 | 2013-12-20 14:27:28 -0500 | [diff] [blame] | 1848 | switch (new_mode) { |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1849 | case PM_FAIL: |
Mike Snitzer | 8b64e88 | 2013-12-20 14:27:28 -0500 | [diff] [blame] | 1850 | if (old_mode != new_mode) |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1851 | notify_of_pool_mode_change(pool, "failure"); |
Joe Thornber | 5383ef3 | 2013-12-04 16:30:01 -0500 | [diff] [blame] | 1852 | dm_pool_metadata_read_only(pool->pmd); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1853 | pool->process_bio = process_bio_fail; |
| 1854 | pool->process_discard = process_bio_fail; |
| 1855 | pool->process_prepared_mapping = process_prepared_mapping_fail; |
| 1856 | pool->process_prepared_discard = process_prepared_discard_fail; |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1857 | |
| 1858 | error_retry_list(pool); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1859 | break; |
| 1860 | |
| 1861 | case PM_READ_ONLY: |
Mike Snitzer | 8b64e88 | 2013-12-20 14:27:28 -0500 | [diff] [blame] | 1862 | if (old_mode != new_mode) |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1863 | notify_of_pool_mode_change(pool, "read-only"); |
| 1864 | dm_pool_metadata_read_only(pool->pmd); |
| 1865 | pool->process_bio = process_bio_read_only; |
| 1866 | pool->process_discard = process_bio_success; |
| 1867 | pool->process_prepared_mapping = process_prepared_mapping_fail; |
| 1868 | pool->process_prepared_discard = process_prepared_discard_passdown; |
| 1869 | |
| 1870 | error_retry_list(pool); |
| 1871 | break; |
| 1872 | |
| 1873 | case PM_OUT_OF_DATA_SPACE: |
| 1874 | /* |
| 1875 | * Ideally we'd never hit this state; the low water mark |
| 1876 | * would trigger userland to extend the pool before we |
| 1877 | * completely run out of data space. However, many small |
| 1878 | * IOs to unprovisioned space can consume data space at an |
| 1879 | * alarming rate. Adjust your low water mark if you're |
| 1880 | * frequently seeing this mode. |
| 1881 | */ |
| 1882 | if (old_mode != new_mode) |
| 1883 | notify_of_pool_mode_change(pool, "out-of-data-space"); |
| 1884 | pool->process_bio = process_bio_read_only; |
| 1885 | pool->process_discard = process_discard; |
| 1886 | pool->process_prepared_mapping = process_prepared_mapping; |
| 1887 | pool->process_prepared_discard = process_prepared_discard_passdown; |
Joe Thornber | 85ad643b | 2014-05-09 15:59:38 +0100 | [diff] [blame] | 1888 | |
Mike Snitzer | 80c5789 | 2014-05-20 13:38:33 -0400 | [diff] [blame] | 1889 | if (!pool->pf.error_if_no_space && no_space_timeout) |
| 1890 | queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1891 | break; |
| 1892 | |
| 1893 | case PM_WRITE: |
Mike Snitzer | 8b64e88 | 2013-12-20 14:27:28 -0500 | [diff] [blame] | 1894 | if (old_mode != new_mode) |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 1895 | notify_of_pool_mode_change(pool, "write"); |
Joe Thornber | 9b7aaa6 | 2013-12-04 16:58:19 -0500 | [diff] [blame] | 1896 | dm_pool_metadata_read_write(pool->pmd); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1897 | pool->process_bio = process_bio; |
| 1898 | pool->process_discard = process_discard; |
| 1899 | pool->process_prepared_mapping = process_prepared_mapping; |
| 1900 | pool->process_prepared_discard = process_prepared_discard; |
| 1901 | break; |
| 1902 | } |
Mike Snitzer | 8b64e88 | 2013-12-20 14:27:28 -0500 | [diff] [blame] | 1903 | |
| 1904 | pool->pf.mode = new_mode; |
Mike Snitzer | cdc2b41 | 2014-02-14 18:10:55 -0500 | [diff] [blame] | 1905 | /* |
| 1906 | * The pool mode may have changed, sync it so bind_control_target() |
| 1907 | * doesn't cause an unexpected mode transition on resume. |
| 1908 | */ |
| 1909 | pt->adjusted_pf.mode = new_mode; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 1912 | static void abort_transaction(struct pool *pool) |
| 1913 | { |
| 1914 | const char *dev_name = dm_device_name(pool->pool_md); |
| 1915 | |
| 1916 | DMERR_LIMIT("%s: aborting current metadata transaction", dev_name); |
| 1917 | if (dm_pool_abort_metadata(pool->pmd)) { |
| 1918 | DMERR("%s: failed to abort metadata transaction", dev_name); |
| 1919 | set_pool_mode(pool, PM_FAIL); |
| 1920 | } |
| 1921 | |
| 1922 | if (dm_pool_metadata_set_needs_check(pool->pmd)) { |
| 1923 | DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name); |
| 1924 | set_pool_mode(pool, PM_FAIL); |
| 1925 | } |
| 1926 | } |
| 1927 | |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 1928 | static void metadata_operation_failed(struct pool *pool, const char *op, int r) |
| 1929 | { |
| 1930 | DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d", |
| 1931 | dm_device_name(pool->pool_md), op, r); |
| 1932 | |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 1933 | abort_transaction(pool); |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 1934 | set_pool_mode(pool, PM_READ_ONLY); |
| 1935 | } |
| 1936 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1937 | /*----------------------------------------------------------------*/ |
| 1938 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1939 | /* |
| 1940 | * Mapping functions. |
| 1941 | */ |
| 1942 | |
| 1943 | /* |
| 1944 | * Called only while mapping a thin bio to hand it over to the workqueue. |
| 1945 | */ |
| 1946 | static void thin_defer_bio(struct thin_c *tc, struct bio *bio) |
| 1947 | { |
| 1948 | unsigned long flags; |
| 1949 | struct pool *pool = tc->pool; |
| 1950 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 1951 | spin_lock_irqsave(&tc->lock, flags); |
| 1952 | bio_list_add(&tc->deferred_bio_list, bio); |
| 1953 | spin_unlock_irqrestore(&tc->lock, flags); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1954 | |
| 1955 | wake_worker(pool); |
| 1956 | } |
| 1957 | |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 1958 | static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio) |
| 1959 | { |
| 1960 | struct pool *pool = tc->pool; |
| 1961 | |
| 1962 | throttle_lock(&pool->throttle); |
| 1963 | thin_defer_bio(tc, bio); |
| 1964 | throttle_unlock(&pool->throttle); |
| 1965 | } |
| 1966 | |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1967 | static void thin_hook_bio(struct thin_c *tc, struct bio *bio) |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1968 | { |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1969 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1970 | |
| 1971 | h->tc = tc; |
| 1972 | h->shared_read_entry = NULL; |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1973 | h->all_io_entry = NULL; |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1974 | h->overwrite_mapping = NULL; |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 1975 | } |
| 1976 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1977 | /* |
| 1978 | * Non-blocking function called from the thin target's map function. |
| 1979 | */ |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1980 | static int thin_bio_map(struct dm_target *ti, struct bio *bio) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1981 | { |
| 1982 | int r; |
| 1983 | struct thin_c *tc = ti->private; |
| 1984 | dm_block_t block = get_bio_block(tc, bio); |
| 1985 | struct dm_thin_device *td = tc->td; |
| 1986 | struct dm_thin_lookup_result result; |
Joe Thornber | 025b968 | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 1987 | struct dm_bio_prison_cell cell1, cell2; |
| 1988 | struct dm_bio_prison_cell *cell_result; |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 1989 | struct dm_cell_key key; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1990 | |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1991 | thin_hook_bio(tc, bio); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1992 | |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 1993 | if (tc->requeue_mode) { |
| 1994 | bio_endio(bio, DM_ENDIO_REQUEUE); |
| 1995 | return DM_MAPIO_SUBMITTED; |
| 1996 | } |
| 1997 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 1998 | if (get_pool_mode(tc->pool) == PM_FAIL) { |
| 1999 | bio_io_error(bio); |
| 2000 | return DM_MAPIO_SUBMITTED; |
| 2001 | } |
| 2002 | |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 2003 | if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) { |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 2004 | thin_defer_bio_with_throttle(tc, bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2005 | return DM_MAPIO_SUBMITTED; |
| 2006 | } |
| 2007 | |
Joe Thornber | c822ed9 | 2014-10-10 09:41:09 +0100 | [diff] [blame] | 2008 | /* |
| 2009 | * We must hold the virtual cell before doing the lookup, otherwise |
| 2010 | * there's a race with discard. |
| 2011 | */ |
| 2012 | build_virtual_key(tc->td, block, &key); |
| 2013 | if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1, &cell_result)) |
| 2014 | return DM_MAPIO_SUBMITTED; |
| 2015 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2016 | r = dm_thin_find_block(td, block, 0, &result); |
| 2017 | |
| 2018 | /* |
| 2019 | * Note that we defer readahead too. |
| 2020 | */ |
| 2021 | switch (r) { |
| 2022 | case 0: |
| 2023 | if (unlikely(result.shared)) { |
| 2024 | /* |
| 2025 | * We have a race condition here between the |
| 2026 | * result.shared value returned by the lookup and |
| 2027 | * snapshot creation, which may cause new |
| 2028 | * sharing. |
| 2029 | * |
| 2030 | * To avoid this always quiesce the origin before |
| 2031 | * taking the snap. You want to do this anyway to |
| 2032 | * ensure a consistent application view |
| 2033 | * (i.e. lockfs). |
| 2034 | * |
| 2035 | * More distant ancestors are irrelevant. The |
| 2036 | * shared flag will be set in their case. |
| 2037 | */ |
| 2038 | thin_defer_bio(tc, bio); |
Joe Thornber | c822ed9 | 2014-10-10 09:41:09 +0100 | [diff] [blame] | 2039 | cell_defer_no_holder_no_free(tc, &cell1); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 2040 | return DM_MAPIO_SUBMITTED; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2041 | } |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 2042 | |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 2043 | build_data_key(tc->td, result.block, &key); |
Joe Thornber | 025b968 | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 2044 | if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2, &cell_result)) { |
| 2045 | cell_defer_no_holder_no_free(tc, &cell1); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 2046 | return DM_MAPIO_SUBMITTED; |
| 2047 | } |
| 2048 | |
| 2049 | inc_all_io_entry(tc->pool, bio); |
Joe Thornber | 025b968 | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 2050 | cell_defer_no_holder_no_free(tc, &cell2); |
| 2051 | cell_defer_no_holder_no_free(tc, &cell1); |
Joe Thornber | e808807 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 2052 | |
| 2053 | remap(tc, bio, result.block); |
| 2054 | return DM_MAPIO_REMAPPED; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2055 | |
| 2056 | case -ENODATA: |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2057 | if (get_pool_mode(tc->pool) == PM_READ_ONLY) { |
| 2058 | /* |
| 2059 | * This block isn't provisioned, and we have no way |
Mike Snitzer | 8c0f0e8 | 2013-12-05 15:47:24 -0500 | [diff] [blame] | 2060 | * of doing so. |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2061 | */ |
Mike Snitzer | 8c0f0e8 | 2013-12-05 15:47:24 -0500 | [diff] [blame] | 2062 | handle_unserviceable_bio(tc->pool, bio); |
Joe Thornber | c822ed9 | 2014-10-10 09:41:09 +0100 | [diff] [blame] | 2063 | cell_defer_no_holder_no_free(tc, &cell1); |
Joe Thornber | 2aab385 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 2064 | return DM_MAPIO_SUBMITTED; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2065 | } |
| 2066 | /* fall through */ |
| 2067 | |
| 2068 | case -EWOULDBLOCK: |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2069 | thin_defer_bio(tc, bio); |
Joe Thornber | c822ed9 | 2014-10-10 09:41:09 +0100 | [diff] [blame] | 2070 | cell_defer_no_holder_no_free(tc, &cell1); |
Joe Thornber | 2aab385 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 2071 | return DM_MAPIO_SUBMITTED; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2072 | |
| 2073 | default: |
| 2074 | /* |
| 2075 | * Must always call bio_io_error on failure. |
| 2076 | * dm_thin_find_block can fail with -EINVAL if the |
| 2077 | * pool is switched to fail-io mode. |
| 2078 | */ |
| 2079 | bio_io_error(bio); |
Joe Thornber | c822ed9 | 2014-10-10 09:41:09 +0100 | [diff] [blame] | 2080 | cell_defer_no_holder_no_free(tc, &cell1); |
Joe Thornber | 2aab385 | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 2081 | return DM_MAPIO_SUBMITTED; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2082 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits) |
| 2086 | { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2087 | struct pool_c *pt = container_of(cb, struct pool_c, callbacks); |
Mike Snitzer | 760fe67 | 2014-03-20 08:36:47 -0400 | [diff] [blame] | 2088 | struct request_queue *q; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2089 | |
Mike Snitzer | 760fe67 | 2014-03-20 08:36:47 -0400 | [diff] [blame] | 2090 | if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE) |
| 2091 | return 1; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2092 | |
Mike Snitzer | 760fe67 | 2014-03-20 08:36:47 -0400 | [diff] [blame] | 2093 | q = bdev_get_queue(pt->data_dev->bdev); |
| 2094 | return bdi_congested(&q->backing_dev_info, bdi_bits); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 2097 | static void requeue_bios(struct pool *pool) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2098 | { |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 2099 | unsigned long flags; |
| 2100 | struct thin_c *tc; |
| 2101 | |
| 2102 | rcu_read_lock(); |
| 2103 | list_for_each_entry_rcu(tc, &pool->active_thins, list) { |
| 2104 | spin_lock_irqsave(&tc->lock, flags); |
| 2105 | bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list); |
| 2106 | bio_list_init(&tc->retry_on_resume_list); |
| 2107 | spin_unlock_irqrestore(&tc->lock, flags); |
| 2108 | } |
| 2109 | rcu_read_unlock(); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | /*---------------------------------------------------------------- |
| 2113 | * Binding of control targets to a pool object |
| 2114 | *--------------------------------------------------------------*/ |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2115 | static bool data_dev_supports_discard(struct pool_c *pt) |
| 2116 | { |
| 2117 | struct request_queue *q = bdev_get_queue(pt->data_dev->bdev); |
| 2118 | |
| 2119 | return q && blk_queue_discard(q); |
| 2120 | } |
| 2121 | |
Joe Thornber | 58051b9 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 2122 | static bool is_factor(sector_t block_size, uint32_t n) |
| 2123 | { |
| 2124 | return !sector_div(block_size, n); |
| 2125 | } |
| 2126 | |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2127 | /* |
| 2128 | * If discard_passdown was enabled verify that the data device |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2129 | * supports discards. Disable discard_passdown if not. |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2130 | */ |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2131 | static void disable_passdown_if_not_supported(struct pool_c *pt) |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2132 | { |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2133 | struct pool *pool = pt->pool; |
| 2134 | struct block_device *data_bdev = pt->data_dev->bdev; |
| 2135 | struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits; |
| 2136 | sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT; |
| 2137 | const char *reason = NULL; |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2138 | char buf[BDEVNAME_SIZE]; |
| 2139 | |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2140 | if (!pt->adjusted_pf.discard_passdown) |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2141 | return; |
| 2142 | |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2143 | if (!data_dev_supports_discard(pt)) |
| 2144 | reason = "discard unsupported"; |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2145 | |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2146 | else if (data_limits->max_discard_sectors < pool->sectors_per_block) |
| 2147 | reason = "max discard sectors smaller than a block"; |
| 2148 | |
| 2149 | else if (data_limits->discard_granularity > block_size) |
| 2150 | reason = "discard granularity larger than a block"; |
| 2151 | |
Joe Thornber | 58051b9 | 2013-03-20 17:21:25 +0000 | [diff] [blame] | 2152 | else if (!is_factor(block_size, data_limits->discard_granularity)) |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2153 | reason = "discard granularity not a factor of block size"; |
| 2154 | |
| 2155 | if (reason) { |
| 2156 | DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason); |
| 2157 | pt->adjusted_pf.discard_passdown = false; |
| 2158 | } |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2159 | } |
| 2160 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2161 | static int bind_control_target(struct pool *pool, struct dm_target *ti) |
| 2162 | { |
| 2163 | struct pool_c *pt = ti->private; |
| 2164 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2165 | /* |
Joe Thornber | 9b7aaa6 | 2013-12-04 16:58:19 -0500 | [diff] [blame] | 2166 | * We want to make sure that a pool in PM_FAIL mode is never upgraded. |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2167 | */ |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 2168 | enum pool_mode old_mode = get_pool_mode(pool); |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2169 | enum pool_mode new_mode = pt->adjusted_pf.mode; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2170 | |
Joe Thornber | 9b7aaa6 | 2013-12-04 16:58:19 -0500 | [diff] [blame] | 2171 | /* |
Mike Snitzer | 8b64e88 | 2013-12-20 14:27:28 -0500 | [diff] [blame] | 2172 | * Don't change the pool's mode until set_pool_mode() below. |
| 2173 | * Otherwise the pool's process_* function pointers may |
| 2174 | * not match the desired pool mode. |
| 2175 | */ |
| 2176 | pt->adjusted_pf.mode = old_mode; |
| 2177 | |
| 2178 | pool->ti = ti; |
| 2179 | pool->pf = pt->adjusted_pf; |
| 2180 | pool->low_water_blocks = pt->low_water_blocks; |
| 2181 | |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2182 | set_pool_mode(pool, new_mode); |
Mike Snitzer | f402693 | 2012-05-19 01:01:01 +0100 | [diff] [blame] | 2183 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2184 | return 0; |
| 2185 | } |
| 2186 | |
| 2187 | static void unbind_control_target(struct pool *pool, struct dm_target *ti) |
| 2188 | { |
| 2189 | if (pool->ti == ti) |
| 2190 | pool->ti = NULL; |
| 2191 | } |
| 2192 | |
| 2193 | /*---------------------------------------------------------------- |
| 2194 | * Pool creation |
| 2195 | *--------------------------------------------------------------*/ |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2196 | /* Initialize pool features. */ |
| 2197 | static void pool_features_init(struct pool_features *pf) |
| 2198 | { |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2199 | pf->mode = PM_WRITE; |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2200 | pf->zero_new_blocks = true; |
| 2201 | pf->discard_enabled = true; |
| 2202 | pf->discard_passdown = true; |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 2203 | pf->error_if_no_space = false; |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2204 | } |
| 2205 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2206 | static void __pool_destroy(struct pool *pool) |
| 2207 | { |
| 2208 | __pool_table_remove(pool); |
| 2209 | |
| 2210 | if (dm_pool_metadata_close(pool->pmd) < 0) |
| 2211 | DMWARN("%s: dm_pool_metadata_close() failed.", __func__); |
| 2212 | |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 2213 | dm_bio_prison_destroy(pool->prison); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2214 | dm_kcopyd_client_destroy(pool->copier); |
| 2215 | |
| 2216 | if (pool->wq) |
| 2217 | destroy_workqueue(pool->wq); |
| 2218 | |
| 2219 | if (pool->next_mapping) |
| 2220 | mempool_free(pool->next_mapping, pool->mapping_pool); |
| 2221 | mempool_destroy(pool->mapping_pool); |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 2222 | dm_deferred_set_destroy(pool->shared_read_ds); |
| 2223 | dm_deferred_set_destroy(pool->all_io_ds); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2224 | kfree(pool); |
| 2225 | } |
| 2226 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 2227 | static struct kmem_cache *_new_mapping_cache; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 2228 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2229 | static struct pool *pool_create(struct mapped_device *pool_md, |
| 2230 | struct block_device *metadata_dev, |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2231 | unsigned long block_size, |
| 2232 | int read_only, char **error) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2233 | { |
| 2234 | int r; |
| 2235 | void *err_p; |
| 2236 | struct pool *pool; |
| 2237 | struct dm_pool_metadata *pmd; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2238 | bool format_device = read_only ? false : true; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2239 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2240 | pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2241 | if (IS_ERR(pmd)) { |
| 2242 | *error = "Error creating metadata object"; |
| 2243 | return (struct pool *)pmd; |
| 2244 | } |
| 2245 | |
| 2246 | pool = kmalloc(sizeof(*pool), GFP_KERNEL); |
| 2247 | if (!pool) { |
| 2248 | *error = "Error allocating memory for pool"; |
| 2249 | err_p = ERR_PTR(-ENOMEM); |
| 2250 | goto bad_pool; |
| 2251 | } |
| 2252 | |
| 2253 | pool->pmd = pmd; |
| 2254 | pool->sectors_per_block = block_size; |
Mikulas Patocka | f9a8e0c | 2012-07-27 15:08:03 +0100 | [diff] [blame] | 2255 | if (block_size & (block_size - 1)) |
| 2256 | pool->sectors_per_block_shift = -1; |
| 2257 | else |
| 2258 | pool->sectors_per_block_shift = __ffs(block_size); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2259 | pool->low_water_blocks = 0; |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2260 | pool_features_init(&pool->pf); |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 2261 | pool->prison = dm_bio_prison_create(); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2262 | if (!pool->prison) { |
| 2263 | *error = "Error creating pool's bio prison"; |
| 2264 | err_p = ERR_PTR(-ENOMEM); |
| 2265 | goto bad_prison; |
| 2266 | } |
| 2267 | |
Mikulas Patocka | df5d2e9 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 2268 | pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2269 | if (IS_ERR(pool->copier)) { |
| 2270 | r = PTR_ERR(pool->copier); |
| 2271 | *error = "Error creating pool's kcopyd client"; |
| 2272 | err_p = ERR_PTR(r); |
| 2273 | goto bad_kcopyd_client; |
| 2274 | } |
| 2275 | |
| 2276 | /* |
| 2277 | * Create singlethreaded workqueue that will service all devices |
| 2278 | * that use this metadata. |
| 2279 | */ |
| 2280 | pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM); |
| 2281 | if (!pool->wq) { |
| 2282 | *error = "Error creating pool's workqueue"; |
| 2283 | err_p = ERR_PTR(-ENOMEM); |
| 2284 | goto bad_wq; |
| 2285 | } |
| 2286 | |
Joe Thornber | 7d327fe | 2014-10-06 15:45:59 +0100 | [diff] [blame] | 2287 | throttle_init(&pool->throttle); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2288 | INIT_WORK(&pool->worker, do_worker); |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 2289 | INIT_DELAYED_WORK(&pool->waker, do_waker); |
Joe Thornber | 85ad643b | 2014-05-09 15:59:38 +0100 | [diff] [blame] | 2290 | INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2291 | spin_lock_init(&pool->lock); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2292 | bio_list_init(&pool->deferred_flush_bios); |
| 2293 | INIT_LIST_HEAD(&pool->prepared_mappings); |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 2294 | INIT_LIST_HEAD(&pool->prepared_discards); |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 2295 | INIT_LIST_HEAD(&pool->active_thins); |
Joe Thornber | 88a6621 | 2013-12-04 20:16:12 -0500 | [diff] [blame] | 2296 | pool->low_water_triggered = false; |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 2297 | |
| 2298 | pool->shared_read_ds = dm_deferred_set_create(); |
| 2299 | if (!pool->shared_read_ds) { |
| 2300 | *error = "Error creating pool's shared read deferred set"; |
| 2301 | err_p = ERR_PTR(-ENOMEM); |
| 2302 | goto bad_shared_read_ds; |
| 2303 | } |
| 2304 | |
| 2305 | pool->all_io_ds = dm_deferred_set_create(); |
| 2306 | if (!pool->all_io_ds) { |
| 2307 | *error = "Error creating pool's all io deferred set"; |
| 2308 | err_p = ERR_PTR(-ENOMEM); |
| 2309 | goto bad_all_io_ds; |
| 2310 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2311 | |
| 2312 | pool->next_mapping = NULL; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 2313 | pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE, |
| 2314 | _new_mapping_cache); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2315 | if (!pool->mapping_pool) { |
| 2316 | *error = "Error creating pool's mapping mempool"; |
| 2317 | err_p = ERR_PTR(-ENOMEM); |
| 2318 | goto bad_mapping_pool; |
| 2319 | } |
| 2320 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2321 | pool->ref_count = 1; |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 2322 | pool->last_commit_jiffies = jiffies; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2323 | pool->pool_md = pool_md; |
| 2324 | pool->md_dev = metadata_dev; |
| 2325 | __pool_table_insert(pool); |
| 2326 | |
| 2327 | return pool; |
| 2328 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2329 | bad_mapping_pool: |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 2330 | dm_deferred_set_destroy(pool->all_io_ds); |
| 2331 | bad_all_io_ds: |
| 2332 | dm_deferred_set_destroy(pool->shared_read_ds); |
| 2333 | bad_shared_read_ds: |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2334 | destroy_workqueue(pool->wq); |
| 2335 | bad_wq: |
| 2336 | dm_kcopyd_client_destroy(pool->copier); |
| 2337 | bad_kcopyd_client: |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 2338 | dm_bio_prison_destroy(pool->prison); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2339 | bad_prison: |
| 2340 | kfree(pool); |
| 2341 | bad_pool: |
| 2342 | if (dm_pool_metadata_close(pmd)) |
| 2343 | DMWARN("%s: dm_pool_metadata_close() failed.", __func__); |
| 2344 | |
| 2345 | return err_p; |
| 2346 | } |
| 2347 | |
| 2348 | static void __pool_inc(struct pool *pool) |
| 2349 | { |
| 2350 | BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); |
| 2351 | pool->ref_count++; |
| 2352 | } |
| 2353 | |
| 2354 | static void __pool_dec(struct pool *pool) |
| 2355 | { |
| 2356 | BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); |
| 2357 | BUG_ON(!pool->ref_count); |
| 2358 | if (!--pool->ref_count) |
| 2359 | __pool_destroy(pool); |
| 2360 | } |
| 2361 | |
| 2362 | static struct pool *__pool_find(struct mapped_device *pool_md, |
| 2363 | struct block_device *metadata_dev, |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2364 | unsigned long block_size, int read_only, |
| 2365 | char **error, int *created) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2366 | { |
| 2367 | struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev); |
| 2368 | |
| 2369 | if (pool) { |
Mike Snitzer | f09996c | 2012-07-27 15:07:59 +0100 | [diff] [blame] | 2370 | if (pool->pool_md != pool_md) { |
| 2371 | *error = "metadata device already in use by a pool"; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2372 | return ERR_PTR(-EBUSY); |
Mike Snitzer | f09996c | 2012-07-27 15:07:59 +0100 | [diff] [blame] | 2373 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2374 | __pool_inc(pool); |
| 2375 | |
| 2376 | } else { |
| 2377 | pool = __pool_table_lookup(pool_md); |
| 2378 | if (pool) { |
Mike Snitzer | f09996c | 2012-07-27 15:07:59 +0100 | [diff] [blame] | 2379 | if (pool->md_dev != metadata_dev) { |
| 2380 | *error = "different pool cannot replace a pool"; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2381 | return ERR_PTR(-EINVAL); |
Mike Snitzer | f09996c | 2012-07-27 15:07:59 +0100 | [diff] [blame] | 2382 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2383 | __pool_inc(pool); |
| 2384 | |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2385 | } else { |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2386 | pool = pool_create(pool_md, metadata_dev, block_size, read_only, error); |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2387 | *created = 1; |
| 2388 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | return pool; |
| 2392 | } |
| 2393 | |
| 2394 | /*---------------------------------------------------------------- |
| 2395 | * Pool target methods |
| 2396 | *--------------------------------------------------------------*/ |
| 2397 | static void pool_dtr(struct dm_target *ti) |
| 2398 | { |
| 2399 | struct pool_c *pt = ti->private; |
| 2400 | |
| 2401 | mutex_lock(&dm_thin_pool_table.mutex); |
| 2402 | |
| 2403 | unbind_control_target(pt->pool, ti); |
| 2404 | __pool_dec(pt->pool); |
| 2405 | dm_put_device(ti, pt->metadata_dev); |
| 2406 | dm_put_device(ti, pt->data_dev); |
| 2407 | kfree(pt); |
| 2408 | |
| 2409 | mutex_unlock(&dm_thin_pool_table.mutex); |
| 2410 | } |
| 2411 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2412 | static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf, |
| 2413 | struct dm_target *ti) |
| 2414 | { |
| 2415 | int r; |
| 2416 | unsigned argc; |
| 2417 | const char *arg_name; |
| 2418 | |
| 2419 | static struct dm_arg _args[] = { |
Mike Snitzer | 74aa45c | 2014-01-15 19:07:58 -0500 | [diff] [blame] | 2420 | {0, 4, "Invalid number of pool feature arguments"}, |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2421 | }; |
| 2422 | |
| 2423 | /* |
| 2424 | * No feature arguments supplied. |
| 2425 | */ |
| 2426 | if (!as->argc) |
| 2427 | return 0; |
| 2428 | |
| 2429 | r = dm_read_arg_group(_args, as, &argc, &ti->error); |
| 2430 | if (r) |
| 2431 | return -EINVAL; |
| 2432 | |
| 2433 | while (argc && !r) { |
| 2434 | arg_name = dm_shift_arg(as); |
| 2435 | argc--; |
| 2436 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2437 | if (!strcasecmp(arg_name, "skip_block_zeroing")) |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2438 | pf->zero_new_blocks = false; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2439 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2440 | else if (!strcasecmp(arg_name, "ignore_discard")) |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2441 | pf->discard_enabled = false; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2442 | |
| 2443 | else if (!strcasecmp(arg_name, "no_discard_passdown")) |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2444 | pf->discard_passdown = false; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2445 | |
| 2446 | else if (!strcasecmp(arg_name, "read_only")) |
| 2447 | pf->mode = PM_READ_ONLY; |
| 2448 | |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 2449 | else if (!strcasecmp(arg_name, "error_if_no_space")) |
| 2450 | pf->error_if_no_space = true; |
| 2451 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2452 | else { |
| 2453 | ti->error = "Unrecognised pool feature requested"; |
| 2454 | r = -EINVAL; |
| 2455 | break; |
| 2456 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | return r; |
| 2460 | } |
| 2461 | |
Joe Thornber | ac8c3f3 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2462 | static void metadata_low_callback(void *context) |
| 2463 | { |
| 2464 | struct pool *pool = context; |
| 2465 | |
| 2466 | DMWARN("%s: reached low water mark for metadata device: sending event.", |
| 2467 | dm_device_name(pool->pool_md)); |
| 2468 | |
| 2469 | dm_table_event(pool->ti->table); |
| 2470 | } |
| 2471 | |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 2472 | static sector_t get_dev_size(struct block_device *bdev) |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2473 | { |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 2474 | return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT; |
| 2475 | } |
| 2476 | |
| 2477 | static void warn_if_metadata_device_too_big(struct block_device *bdev) |
| 2478 | { |
| 2479 | sector_t metadata_dev_size = get_dev_size(bdev); |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2480 | char buffer[BDEVNAME_SIZE]; |
| 2481 | |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 2482 | if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING) |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2483 | DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.", |
| 2484 | bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS); |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | static sector_t get_metadata_dev_size(struct block_device *bdev) |
| 2488 | { |
| 2489 | sector_t metadata_dev_size = get_dev_size(bdev); |
| 2490 | |
| 2491 | if (metadata_dev_size > THIN_METADATA_MAX_SECTORS) |
| 2492 | metadata_dev_size = THIN_METADATA_MAX_SECTORS; |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2493 | |
| 2494 | return metadata_dev_size; |
| 2495 | } |
| 2496 | |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2497 | static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev) |
| 2498 | { |
| 2499 | sector_t metadata_dev_size = get_metadata_dev_size(bdev); |
| 2500 | |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 2501 | sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE); |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2502 | |
| 2503 | return metadata_dev_size; |
| 2504 | } |
| 2505 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2506 | /* |
Joe Thornber | ac8c3f3 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2507 | * When a metadata threshold is crossed a dm event is triggered, and |
| 2508 | * userland should respond by growing the metadata device. We could let |
| 2509 | * userland set the threshold, like we do with the data threshold, but I'm |
| 2510 | * not sure they know enough to do this well. |
| 2511 | */ |
| 2512 | static dm_block_t calc_metadata_threshold(struct pool_c *pt) |
| 2513 | { |
| 2514 | /* |
| 2515 | * 4M is ample for all ops with the possible exception of thin |
| 2516 | * device deletion which is harmless if it fails (just retry the |
| 2517 | * delete after you've grown the device). |
| 2518 | */ |
| 2519 | dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4; |
| 2520 | return min((dm_block_t)1024ULL /* 4M */, quarter); |
| 2521 | } |
| 2522 | |
| 2523 | /* |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2524 | * thin-pool <metadata dev> <data dev> |
| 2525 | * <data block size (sectors)> |
| 2526 | * <low water mark (blocks)> |
| 2527 | * [<#feature args> [<arg>]*] |
| 2528 | * |
| 2529 | * Optional feature arguments are: |
| 2530 | * skip_block_zeroing: skips the zeroing of newly-provisioned blocks. |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2531 | * ignore_discard: disable discard |
| 2532 | * no_discard_passdown: don't pass discards down to the data device |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 2533 | * read_only: Don't allow any changes to be made to the pool metadata. |
| 2534 | * error_if_no_space: error IOs, instead of queueing, if no space. |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2535 | */ |
| 2536 | static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv) |
| 2537 | { |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2538 | int r, pool_created = 0; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2539 | struct pool_c *pt; |
| 2540 | struct pool *pool; |
| 2541 | struct pool_features pf; |
| 2542 | struct dm_arg_set as; |
| 2543 | struct dm_dev *data_dev; |
| 2544 | unsigned long block_size; |
| 2545 | dm_block_t low_water_blocks; |
| 2546 | struct dm_dev *metadata_dev; |
Joe Thornber | 5d0db96 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2547 | fmode_t metadata_mode; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2548 | |
| 2549 | /* |
| 2550 | * FIXME Remove validation from scope of lock. |
| 2551 | */ |
| 2552 | mutex_lock(&dm_thin_pool_table.mutex); |
| 2553 | |
| 2554 | if (argc < 4) { |
| 2555 | ti->error = "Invalid argument count"; |
| 2556 | r = -EINVAL; |
| 2557 | goto out_unlock; |
| 2558 | } |
Joe Thornber | 5d0db96 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2559 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2560 | as.argc = argc; |
| 2561 | as.argv = argv; |
| 2562 | |
Joe Thornber | 5d0db96 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2563 | /* |
| 2564 | * Set default pool features. |
| 2565 | */ |
| 2566 | pool_features_init(&pf); |
| 2567 | |
| 2568 | dm_consume_args(&as, 4); |
| 2569 | r = parse_pool_features(&as, &pf, ti); |
| 2570 | if (r) |
| 2571 | goto out_unlock; |
| 2572 | |
| 2573 | metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE); |
| 2574 | r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2575 | if (r) { |
| 2576 | ti->error = "Error opening metadata block device"; |
| 2577 | goto out_unlock; |
| 2578 | } |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 2579 | warn_if_metadata_device_too_big(metadata_dev->bdev); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2580 | |
| 2581 | r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev); |
| 2582 | if (r) { |
| 2583 | ti->error = "Error getting data device"; |
| 2584 | goto out_metadata; |
| 2585 | } |
| 2586 | |
| 2587 | if (kstrtoul(argv[2], 10, &block_size) || !block_size || |
| 2588 | block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS || |
| 2589 | block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS || |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 2590 | block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2591 | ti->error = "Invalid block size"; |
| 2592 | r = -EINVAL; |
| 2593 | goto out; |
| 2594 | } |
| 2595 | |
| 2596 | if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) { |
| 2597 | ti->error = "Invalid low water mark"; |
| 2598 | r = -EINVAL; |
| 2599 | goto out; |
| 2600 | } |
| 2601 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2602 | pt = kzalloc(sizeof(*pt), GFP_KERNEL); |
| 2603 | if (!pt) { |
| 2604 | r = -ENOMEM; |
| 2605 | goto out; |
| 2606 | } |
| 2607 | |
| 2608 | pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 2609 | block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2610 | if (IS_ERR(pool)) { |
| 2611 | r = PTR_ERR(pool); |
| 2612 | goto out_free_pt; |
| 2613 | } |
| 2614 | |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2615 | /* |
| 2616 | * 'pool_created' reflects whether this is the first table load. |
| 2617 | * Top level discard support is not allowed to be changed after |
| 2618 | * initial load. This would require a pool reload to trigger thin |
| 2619 | * device changes. |
| 2620 | */ |
| 2621 | if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) { |
| 2622 | ti->error = "Discard support cannot be disabled once enabled"; |
| 2623 | r = -EINVAL; |
| 2624 | goto out_flags_changed; |
| 2625 | } |
| 2626 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2627 | pt->pool = pool; |
| 2628 | pt->ti = ti; |
| 2629 | pt->metadata_dev = metadata_dev; |
| 2630 | pt->data_dev = data_dev; |
| 2631 | pt->low_water_blocks = low_water_blocks; |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 2632 | pt->adjusted_pf = pt->requested_pf = pf; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 2633 | ti->num_flush_bios = 1; |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2634 | |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2635 | /* |
| 2636 | * Only need to enable discards if the pool should pass |
| 2637 | * them down to the data device. The thin device's discard |
| 2638 | * processing will cause mappings to be removed from the btree. |
| 2639 | */ |
Mike Snitzer | b60ab99 | 2013-09-19 18:49:11 -0400 | [diff] [blame] | 2640 | ti->discard_zeroes_data_unsupported = true; |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2641 | if (pf.discard_enabled && pf.discard_passdown) { |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 2642 | ti->num_discard_bios = 1; |
Mike Snitzer | 9bc142d | 2012-09-26 23:45:46 +0100 | [diff] [blame] | 2643 | |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2644 | /* |
| 2645 | * Setting 'discards_supported' circumvents the normal |
| 2646 | * stacking of discard limits (this keeps the pool and |
| 2647 | * thin devices' discard limits consistent). |
| 2648 | */ |
Alasdair G Kergon | 0ac5548 | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 2649 | ti->discards_supported = true; |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2650 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2651 | ti->private = pt; |
| 2652 | |
Joe Thornber | ac8c3f3 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 2653 | r = dm_pool_register_metadata_threshold(pt->pool->pmd, |
| 2654 | calc_metadata_threshold(pt), |
| 2655 | metadata_low_callback, |
| 2656 | pool); |
| 2657 | if (r) |
| 2658 | goto out_free_pt; |
| 2659 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2660 | pt->callbacks.congested_fn = pool_is_congested; |
| 2661 | dm_table_add_target_callbacks(ti->table, &pt->callbacks); |
| 2662 | |
| 2663 | mutex_unlock(&dm_thin_pool_table.mutex); |
| 2664 | |
| 2665 | return 0; |
| 2666 | |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 2667 | out_flags_changed: |
| 2668 | __pool_dec(pool); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2669 | out_free_pt: |
| 2670 | kfree(pt); |
| 2671 | out: |
| 2672 | dm_put_device(ti, data_dev); |
| 2673 | out_metadata: |
| 2674 | dm_put_device(ti, metadata_dev); |
| 2675 | out_unlock: |
| 2676 | mutex_unlock(&dm_thin_pool_table.mutex); |
| 2677 | |
| 2678 | return r; |
| 2679 | } |
| 2680 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 2681 | static int pool_map(struct dm_target *ti, struct bio *bio) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2682 | { |
| 2683 | int r; |
| 2684 | struct pool_c *pt = ti->private; |
| 2685 | struct pool *pool = pt->pool; |
| 2686 | unsigned long flags; |
| 2687 | |
| 2688 | /* |
| 2689 | * As this is a singleton target, ti->begin is always zero. |
| 2690 | */ |
| 2691 | spin_lock_irqsave(&pool->lock, flags); |
| 2692 | bio->bi_bdev = pt->data_dev->bdev; |
| 2693 | r = DM_MAPIO_REMAPPED; |
| 2694 | spin_unlock_irqrestore(&pool->lock, flags); |
| 2695 | |
| 2696 | return r; |
| 2697 | } |
| 2698 | |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2699 | static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit) |
| 2700 | { |
| 2701 | int r; |
| 2702 | struct pool_c *pt = ti->private; |
| 2703 | struct pool *pool = pt->pool; |
| 2704 | sector_t data_size = ti->len; |
| 2705 | dm_block_t sb_data_size; |
| 2706 | |
| 2707 | *need_commit = false; |
| 2708 | |
| 2709 | (void) sector_div(data_size, pool->sectors_per_block); |
| 2710 | |
| 2711 | r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size); |
| 2712 | if (r) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 2713 | DMERR("%s: failed to retrieve data device size", |
| 2714 | dm_device_name(pool->pool_md)); |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2715 | return r; |
| 2716 | } |
| 2717 | |
| 2718 | if (data_size < sb_data_size) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 2719 | DMERR("%s: pool target (%llu blocks) too small: expected %llu", |
| 2720 | dm_device_name(pool->pool_md), |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2721 | (unsigned long long)data_size, sb_data_size); |
| 2722 | return -EINVAL; |
| 2723 | |
| 2724 | } else if (data_size > sb_data_size) { |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 2725 | if (dm_pool_metadata_needs_check(pool->pmd)) { |
| 2726 | DMERR("%s: unable to grow the data device until repaired.", |
| 2727 | dm_device_name(pool->pool_md)); |
| 2728 | return 0; |
| 2729 | } |
| 2730 | |
Mike Snitzer | 6f7f51d | 2013-12-04 10:25:53 -0500 | [diff] [blame] | 2731 | if (sb_data_size) |
| 2732 | DMINFO("%s: growing the data device from %llu to %llu blocks", |
| 2733 | dm_device_name(pool->pool_md), |
| 2734 | sb_data_size, (unsigned long long)data_size); |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2735 | r = dm_pool_resize_data_dev(pool->pmd, data_size); |
| 2736 | if (r) { |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 2737 | metadata_operation_failed(pool, "dm_pool_resize_data_dev", r); |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2738 | return r; |
| 2739 | } |
| 2740 | |
| 2741 | *need_commit = true; |
| 2742 | } |
| 2743 | |
| 2744 | return 0; |
| 2745 | } |
| 2746 | |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2747 | static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit) |
| 2748 | { |
| 2749 | int r; |
| 2750 | struct pool_c *pt = ti->private; |
| 2751 | struct pool *pool = pt->pool; |
| 2752 | dm_block_t metadata_dev_size, sb_metadata_dev_size; |
| 2753 | |
| 2754 | *need_commit = false; |
| 2755 | |
Alasdair G Kergon | 610bba8 | 2013-05-19 18:57:50 +0100 | [diff] [blame] | 2756 | metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev); |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2757 | |
| 2758 | r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size); |
| 2759 | if (r) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 2760 | DMERR("%s: failed to retrieve metadata device size", |
| 2761 | dm_device_name(pool->pool_md)); |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2762 | return r; |
| 2763 | } |
| 2764 | |
| 2765 | if (metadata_dev_size < sb_metadata_dev_size) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 2766 | DMERR("%s: metadata device (%llu blocks) too small: expected %llu", |
| 2767 | dm_device_name(pool->pool_md), |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2768 | metadata_dev_size, sb_metadata_dev_size); |
| 2769 | return -EINVAL; |
| 2770 | |
| 2771 | } else if (metadata_dev_size > sb_metadata_dev_size) { |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 2772 | if (dm_pool_metadata_needs_check(pool->pmd)) { |
| 2773 | DMERR("%s: unable to grow the metadata device until repaired.", |
| 2774 | dm_device_name(pool->pool_md)); |
| 2775 | return 0; |
| 2776 | } |
| 2777 | |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 2778 | warn_if_metadata_device_too_big(pool->md_dev); |
Mike Snitzer | 6f7f51d | 2013-12-04 10:25:53 -0500 | [diff] [blame] | 2779 | DMINFO("%s: growing the metadata device from %llu to %llu blocks", |
| 2780 | dm_device_name(pool->pool_md), |
| 2781 | sb_metadata_dev_size, metadata_dev_size); |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2782 | r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size); |
| 2783 | if (r) { |
Joe Thornber | b533065 | 2013-12-04 19:51:33 -0500 | [diff] [blame] | 2784 | metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r); |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2785 | return r; |
| 2786 | } |
| 2787 | |
| 2788 | *need_commit = true; |
| 2789 | } |
| 2790 | |
| 2791 | return 0; |
| 2792 | } |
| 2793 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2794 | /* |
| 2795 | * Retrieves the number of blocks of the data device from |
| 2796 | * the superblock and compares it to the actual device size, |
| 2797 | * thus resizing the data device in case it has grown. |
| 2798 | * |
| 2799 | * This both copes with opening preallocated data devices in the ctr |
| 2800 | * being followed by a resume |
| 2801 | * -and- |
| 2802 | * calling the resume method individually after userspace has |
| 2803 | * grown the data device in reaction to a table event. |
| 2804 | */ |
| 2805 | static int pool_preresume(struct dm_target *ti) |
| 2806 | { |
| 2807 | int r; |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2808 | bool need_commit1, need_commit2; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2809 | struct pool_c *pt = ti->private; |
| 2810 | struct pool *pool = pt->pool; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2811 | |
| 2812 | /* |
| 2813 | * Take control of the pool object. |
| 2814 | */ |
| 2815 | r = bind_control_target(pool, ti); |
| 2816 | if (r) |
| 2817 | return r; |
| 2818 | |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 2819 | r = maybe_resize_data_dev(ti, &need_commit1); |
| 2820 | if (r) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2821 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2822 | |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 2823 | r = maybe_resize_metadata_dev(ti, &need_commit2); |
| 2824 | if (r) |
| 2825 | return r; |
| 2826 | |
| 2827 | if (need_commit1 || need_commit2) |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 2828 | (void) commit(pool); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2829 | |
| 2830 | return 0; |
| 2831 | } |
| 2832 | |
| 2833 | static void pool_resume(struct dm_target *ti) |
| 2834 | { |
| 2835 | struct pool_c *pt = ti->private; |
| 2836 | struct pool *pool = pt->pool; |
| 2837 | unsigned long flags; |
| 2838 | |
| 2839 | spin_lock_irqsave(&pool->lock, flags); |
Joe Thornber | 88a6621 | 2013-12-04 20:16:12 -0500 | [diff] [blame] | 2840 | pool->low_water_triggered = false; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2841 | spin_unlock_irqrestore(&pool->lock, flags); |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 2842 | requeue_bios(pool); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2843 | |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 2844 | do_waker(&pool->waker.work); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | static void pool_postsuspend(struct dm_target *ti) |
| 2848 | { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2849 | struct pool_c *pt = ti->private; |
| 2850 | struct pool *pool = pt->pool; |
| 2851 | |
Joe Thornber | 905e51b | 2012-03-28 18:41:27 +0100 | [diff] [blame] | 2852 | cancel_delayed_work(&pool->waker); |
Joe Thornber | 85ad643b | 2014-05-09 15:59:38 +0100 | [diff] [blame] | 2853 | cancel_delayed_work(&pool->no_space_timeout); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2854 | flush_workqueue(pool->wq); |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 2855 | (void) commit(pool); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | static int check_arg_count(unsigned argc, unsigned args_required) |
| 2859 | { |
| 2860 | if (argc != args_required) { |
| 2861 | DMWARN("Message received with %u arguments instead of %u.", |
| 2862 | argc, args_required); |
| 2863 | return -EINVAL; |
| 2864 | } |
| 2865 | |
| 2866 | return 0; |
| 2867 | } |
| 2868 | |
| 2869 | static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning) |
| 2870 | { |
| 2871 | if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) && |
| 2872 | *dev_id <= MAX_DEV_ID) |
| 2873 | return 0; |
| 2874 | |
| 2875 | if (warning) |
| 2876 | DMWARN("Message received with invalid device id: %s", arg); |
| 2877 | |
| 2878 | return -EINVAL; |
| 2879 | } |
| 2880 | |
| 2881 | static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool) |
| 2882 | { |
| 2883 | dm_thin_id dev_id; |
| 2884 | int r; |
| 2885 | |
| 2886 | r = check_arg_count(argc, 2); |
| 2887 | if (r) |
| 2888 | return r; |
| 2889 | |
| 2890 | r = read_dev_id(argv[1], &dev_id, 1); |
| 2891 | if (r) |
| 2892 | return r; |
| 2893 | |
| 2894 | r = dm_pool_create_thin(pool->pmd, dev_id); |
| 2895 | if (r) { |
| 2896 | DMWARN("Creation of new thinly-provisioned device with id %s failed.", |
| 2897 | argv[1]); |
| 2898 | return r; |
| 2899 | } |
| 2900 | |
| 2901 | return 0; |
| 2902 | } |
| 2903 | |
| 2904 | static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool) |
| 2905 | { |
| 2906 | dm_thin_id dev_id; |
| 2907 | dm_thin_id origin_dev_id; |
| 2908 | int r; |
| 2909 | |
| 2910 | r = check_arg_count(argc, 3); |
| 2911 | if (r) |
| 2912 | return r; |
| 2913 | |
| 2914 | r = read_dev_id(argv[1], &dev_id, 1); |
| 2915 | if (r) |
| 2916 | return r; |
| 2917 | |
| 2918 | r = read_dev_id(argv[2], &origin_dev_id, 1); |
| 2919 | if (r) |
| 2920 | return r; |
| 2921 | |
| 2922 | r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id); |
| 2923 | if (r) { |
| 2924 | DMWARN("Creation of new snapshot %s of device %s failed.", |
| 2925 | argv[1], argv[2]); |
| 2926 | return r; |
| 2927 | } |
| 2928 | |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
| 2932 | static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool) |
| 2933 | { |
| 2934 | dm_thin_id dev_id; |
| 2935 | int r; |
| 2936 | |
| 2937 | r = check_arg_count(argc, 2); |
| 2938 | if (r) |
| 2939 | return r; |
| 2940 | |
| 2941 | r = read_dev_id(argv[1], &dev_id, 1); |
| 2942 | if (r) |
| 2943 | return r; |
| 2944 | |
| 2945 | r = dm_pool_delete_thin_device(pool->pmd, dev_id); |
| 2946 | if (r) |
| 2947 | DMWARN("Deletion of thin device %s failed.", argv[1]); |
| 2948 | |
| 2949 | return r; |
| 2950 | } |
| 2951 | |
| 2952 | static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool) |
| 2953 | { |
| 2954 | dm_thin_id old_id, new_id; |
| 2955 | int r; |
| 2956 | |
| 2957 | r = check_arg_count(argc, 3); |
| 2958 | if (r) |
| 2959 | return r; |
| 2960 | |
| 2961 | if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) { |
| 2962 | DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]); |
| 2963 | return -EINVAL; |
| 2964 | } |
| 2965 | |
| 2966 | if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) { |
| 2967 | DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]); |
| 2968 | return -EINVAL; |
| 2969 | } |
| 2970 | |
| 2971 | r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id); |
| 2972 | if (r) { |
| 2973 | DMWARN("Failed to change transaction id from %s to %s.", |
| 2974 | argv[1], argv[2]); |
| 2975 | return r; |
| 2976 | } |
| 2977 | |
| 2978 | return 0; |
| 2979 | } |
| 2980 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 2981 | static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool) |
| 2982 | { |
| 2983 | int r; |
| 2984 | |
| 2985 | r = check_arg_count(argc, 1); |
| 2986 | if (r) |
| 2987 | return r; |
| 2988 | |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 2989 | (void) commit(pool); |
Joe Thornber | 0d200ae | 2012-07-03 12:55:31 +0100 | [diff] [blame] | 2990 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 2991 | r = dm_pool_reserve_metadata_snap(pool->pmd); |
| 2992 | if (r) |
| 2993 | DMWARN("reserve_metadata_snap message failed."); |
| 2994 | |
| 2995 | return r; |
| 2996 | } |
| 2997 | |
| 2998 | static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool) |
| 2999 | { |
| 3000 | int r; |
| 3001 | |
| 3002 | r = check_arg_count(argc, 1); |
| 3003 | if (r) |
| 3004 | return r; |
| 3005 | |
| 3006 | r = dm_pool_release_metadata_snap(pool->pmd); |
| 3007 | if (r) |
| 3008 | DMWARN("release_metadata_snap message failed."); |
| 3009 | |
| 3010 | return r; |
| 3011 | } |
| 3012 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3013 | /* |
| 3014 | * Messages supported: |
| 3015 | * create_thin <dev_id> |
| 3016 | * create_snap <dev_id> <origin_id> |
| 3017 | * delete <dev_id> |
| 3018 | * trim <dev_id> <new_size_in_sectors> |
| 3019 | * set_transaction_id <current_trans_id> <new_trans_id> |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 3020 | * reserve_metadata_snap |
| 3021 | * release_metadata_snap |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3022 | */ |
| 3023 | static int pool_message(struct dm_target *ti, unsigned argc, char **argv) |
| 3024 | { |
| 3025 | int r = -EINVAL; |
| 3026 | struct pool_c *pt = ti->private; |
| 3027 | struct pool *pool = pt->pool; |
| 3028 | |
| 3029 | if (!strcasecmp(argv[0], "create_thin")) |
| 3030 | r = process_create_thin_mesg(argc, argv, pool); |
| 3031 | |
| 3032 | else if (!strcasecmp(argv[0], "create_snap")) |
| 3033 | r = process_create_snap_mesg(argc, argv, pool); |
| 3034 | |
| 3035 | else if (!strcasecmp(argv[0], "delete")) |
| 3036 | r = process_delete_mesg(argc, argv, pool); |
| 3037 | |
| 3038 | else if (!strcasecmp(argv[0], "set_transaction_id")) |
| 3039 | r = process_set_transaction_id_mesg(argc, argv, pool); |
| 3040 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 3041 | else if (!strcasecmp(argv[0], "reserve_metadata_snap")) |
| 3042 | r = process_reserve_metadata_snap_mesg(argc, argv, pool); |
| 3043 | |
| 3044 | else if (!strcasecmp(argv[0], "release_metadata_snap")) |
| 3045 | r = process_release_metadata_snap_mesg(argc, argv, pool); |
| 3046 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3047 | else |
| 3048 | DMWARN("Unrecognised thin pool target message received: %s", argv[0]); |
| 3049 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3050 | if (!r) |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 3051 | (void) commit(pool); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3052 | |
| 3053 | return r; |
| 3054 | } |
| 3055 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3056 | static void emit_flags(struct pool_features *pf, char *result, |
| 3057 | unsigned sz, unsigned maxlen) |
| 3058 | { |
| 3059 | unsigned count = !pf->zero_new_blocks + !pf->discard_enabled + |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 3060 | !pf->discard_passdown + (pf->mode == PM_READ_ONLY) + |
| 3061 | pf->error_if_no_space; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3062 | DMEMIT("%u ", count); |
| 3063 | |
| 3064 | if (!pf->zero_new_blocks) |
| 3065 | DMEMIT("skip_block_zeroing "); |
| 3066 | |
| 3067 | if (!pf->discard_enabled) |
| 3068 | DMEMIT("ignore_discard "); |
| 3069 | |
| 3070 | if (!pf->discard_passdown) |
| 3071 | DMEMIT("no_discard_passdown "); |
| 3072 | |
| 3073 | if (pf->mode == PM_READ_ONLY) |
| 3074 | DMEMIT("read_only "); |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 3075 | |
| 3076 | if (pf->error_if_no_space) |
| 3077 | DMEMIT("error_if_no_space "); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3078 | } |
| 3079 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3080 | /* |
| 3081 | * Status line is: |
| 3082 | * <transaction id> <used metadata sectors>/<total metadata sectors> |
| 3083 | * <used data sectors>/<total data sectors> <held metadata root> |
| 3084 | */ |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3085 | static void pool_status(struct dm_target *ti, status_type_t type, |
| 3086 | unsigned status_flags, char *result, unsigned maxlen) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3087 | { |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3088 | int r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3089 | unsigned sz = 0; |
| 3090 | uint64_t transaction_id; |
| 3091 | dm_block_t nr_free_blocks_data; |
| 3092 | dm_block_t nr_free_blocks_metadata; |
| 3093 | dm_block_t nr_blocks_data; |
| 3094 | dm_block_t nr_blocks_metadata; |
| 3095 | dm_block_t held_root; |
| 3096 | char buf[BDEVNAME_SIZE]; |
| 3097 | char buf2[BDEVNAME_SIZE]; |
| 3098 | struct pool_c *pt = ti->private; |
| 3099 | struct pool *pool = pt->pool; |
| 3100 | |
| 3101 | switch (type) { |
| 3102 | case STATUSTYPE_INFO: |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3103 | if (get_pool_mode(pool) == PM_FAIL) { |
| 3104 | DMEMIT("Fail"); |
| 3105 | break; |
| 3106 | } |
| 3107 | |
Alasdair G Kergon | 1f4e0ff | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3108 | /* Commit to ensure statistics aren't out-of-date */ |
| 3109 | if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) |
Joe Thornber | 020cc3b | 2013-12-04 15:05:36 -0500 | [diff] [blame] | 3110 | (void) commit(pool); |
Alasdair G Kergon | 1f4e0ff | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3111 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3112 | r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id); |
| 3113 | if (r) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 3114 | DMERR("%s: dm_pool_get_metadata_transaction_id returned %d", |
| 3115 | dm_device_name(pool->pool_md), r); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3116 | goto err; |
| 3117 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3118 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3119 | r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata); |
| 3120 | if (r) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 3121 | DMERR("%s: dm_pool_get_free_metadata_block_count returned %d", |
| 3122 | dm_device_name(pool->pool_md), r); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3123 | goto err; |
| 3124 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3125 | |
| 3126 | r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3127 | if (r) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 3128 | DMERR("%s: dm_pool_get_metadata_dev_size returned %d", |
| 3129 | dm_device_name(pool->pool_md), r); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3130 | goto err; |
| 3131 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3132 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3133 | r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data); |
| 3134 | if (r) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 3135 | DMERR("%s: dm_pool_get_free_block_count returned %d", |
| 3136 | dm_device_name(pool->pool_md), r); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3137 | goto err; |
| 3138 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3139 | |
| 3140 | r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3141 | if (r) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 3142 | DMERR("%s: dm_pool_get_data_dev_size returned %d", |
| 3143 | dm_device_name(pool->pool_md), r); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3144 | goto err; |
| 3145 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3146 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 3147 | r = dm_pool_get_metadata_snap(pool->pmd, &held_root); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3148 | if (r) { |
Mike Snitzer | 4fa5971 | 2013-08-21 17:30:40 -0400 | [diff] [blame] | 3149 | DMERR("%s: dm_pool_get_metadata_snap returned %d", |
| 3150 | dm_device_name(pool->pool_md), r); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3151 | goto err; |
| 3152 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3153 | |
| 3154 | DMEMIT("%llu %llu/%llu %llu/%llu ", |
| 3155 | (unsigned long long)transaction_id, |
| 3156 | (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata), |
| 3157 | (unsigned long long)nr_blocks_metadata, |
| 3158 | (unsigned long long)(nr_blocks_data - nr_free_blocks_data), |
| 3159 | (unsigned long long)nr_blocks_data); |
| 3160 | |
| 3161 | if (held_root) |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3162 | DMEMIT("%llu ", held_root); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3163 | else |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3164 | DMEMIT("- "); |
| 3165 | |
Joe Thornber | 3e1a069 | 2014-03-03 16:03:26 +0000 | [diff] [blame] | 3166 | if (pool->pf.mode == PM_OUT_OF_DATA_SPACE) |
| 3167 | DMEMIT("out_of_data_space "); |
| 3168 | else if (pool->pf.mode == PM_READ_ONLY) |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3169 | DMEMIT("ro "); |
| 3170 | else |
| 3171 | DMEMIT("rw "); |
| 3172 | |
Mike Snitzer | 018debe | 2012-12-21 20:23:32 +0000 | [diff] [blame] | 3173 | if (!pool->pf.discard_enabled) |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 3174 | DMEMIT("ignore_discard "); |
Mike Snitzer | 018debe | 2012-12-21 20:23:32 +0000 | [diff] [blame] | 3175 | else if (pool->pf.discard_passdown) |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 3176 | DMEMIT("discard_passdown "); |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3177 | else |
Mike Snitzer | 787a996c | 2013-12-06 16:21:43 -0500 | [diff] [blame] | 3178 | DMEMIT("no_discard_passdown "); |
| 3179 | |
| 3180 | if (pool->pf.error_if_no_space) |
| 3181 | DMEMIT("error_if_no_space "); |
| 3182 | else |
| 3183 | DMEMIT("queue_if_no_space "); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3184 | |
| 3185 | break; |
| 3186 | |
| 3187 | case STATUSTYPE_TABLE: |
| 3188 | DMEMIT("%s %s %lu %llu ", |
| 3189 | format_dev_t(buf, pt->metadata_dev->bdev->bd_dev), |
| 3190 | format_dev_t(buf2, pt->data_dev->bdev->bd_dev), |
| 3191 | (unsigned long)pool->sectors_per_block, |
| 3192 | (unsigned long long)pt->low_water_blocks); |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3193 | emit_flags(&pt->requested_pf, result, sz, maxlen); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3194 | break; |
| 3195 | } |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3196 | return; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3197 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3198 | err: |
| 3199 | DMEMIT("Error"); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | static int pool_iterate_devices(struct dm_target *ti, |
| 3203 | iterate_devices_callout_fn fn, void *data) |
| 3204 | { |
| 3205 | struct pool_c *pt = ti->private; |
| 3206 | |
| 3207 | return fn(ti, pt->data_dev, 0, ti->len, data); |
| 3208 | } |
| 3209 | |
| 3210 | static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm, |
| 3211 | struct bio_vec *biovec, int max_size) |
| 3212 | { |
| 3213 | struct pool_c *pt = ti->private; |
| 3214 | struct request_queue *q = bdev_get_queue(pt->data_dev->bdev); |
| 3215 | |
| 3216 | if (!q->merge_bvec_fn) |
| 3217 | return max_size; |
| 3218 | |
| 3219 | bvm->bi_bdev = pt->data_dev->bdev; |
| 3220 | |
| 3221 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); |
| 3222 | } |
| 3223 | |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3224 | static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits) |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3225 | { |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3226 | struct pool *pool = pt->pool; |
| 3227 | struct queue_limits *data_limits; |
| 3228 | |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3229 | limits->max_discard_sectors = pool->sectors_per_block; |
| 3230 | |
| 3231 | /* |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3232 | * discard_granularity is just a hint, and not enforced. |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3233 | */ |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3234 | if (pt->adjusted_pf.discard_passdown) { |
| 3235 | data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits; |
Lukas Czerner | 09869de | 2014-06-11 12:28:43 -0400 | [diff] [blame] | 3236 | limits->discard_granularity = max(data_limits->discard_granularity, |
| 3237 | pool->sectors_per_block << SECTOR_SHIFT); |
Mike Snitzer | f13945d | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3238 | } else |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3239 | limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT; |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3240 | } |
| 3241 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3242 | static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits) |
| 3243 | { |
| 3244 | struct pool_c *pt = ti->private; |
| 3245 | struct pool *pool = pt->pool; |
Mike Snitzer | 604ea90 | 2014-10-09 18:43:25 -0400 | [diff] [blame] | 3246 | sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT; |
| 3247 | |
| 3248 | /* |
| 3249 | * Adjust max_sectors_kb to highest possible power-of-2 |
| 3250 | * factor of pool->sectors_per_block. |
| 3251 | */ |
| 3252 | if (limits->max_hw_sectors & (limits->max_hw_sectors - 1)) |
| 3253 | limits->max_sectors = rounddown_pow_of_two(limits->max_hw_sectors); |
| 3254 | else |
| 3255 | limits->max_sectors = limits->max_hw_sectors; |
| 3256 | |
| 3257 | if (limits->max_sectors < pool->sectors_per_block) { |
| 3258 | while (!is_factor(pool->sectors_per_block, limits->max_sectors)) { |
| 3259 | if ((limits->max_sectors & (limits->max_sectors - 1)) == 0) |
| 3260 | limits->max_sectors--; |
| 3261 | limits->max_sectors = rounddown_pow_of_two(limits->max_sectors); |
| 3262 | } |
| 3263 | } else if (block_size_is_power_of_two(pool)) { |
| 3264 | /* max_sectors_kb is >= power-of-2 thinp blocksize */ |
| 3265 | while (!is_factor(limits->max_sectors, pool->sectors_per_block)) { |
| 3266 | if ((limits->max_sectors & (limits->max_sectors - 1)) == 0) |
| 3267 | limits->max_sectors--; |
| 3268 | limits->max_sectors = rounddown_pow_of_two(limits->max_sectors); |
| 3269 | } |
| 3270 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3271 | |
Mike Snitzer | 0cc67cd | 2013-08-20 15:02:41 -0400 | [diff] [blame] | 3272 | /* |
| 3273 | * If the system-determined stacked limits are compatible with the |
| 3274 | * pool's blocksize (io_opt is a factor) do not override them. |
| 3275 | */ |
| 3276 | if (io_opt_sectors < pool->sectors_per_block || |
Mike Snitzer | 604ea90 | 2014-10-09 18:43:25 -0400 | [diff] [blame] | 3277 | !is_factor(io_opt_sectors, pool->sectors_per_block)) { |
| 3278 | if (is_factor(pool->sectors_per_block, limits->max_sectors)) |
| 3279 | blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT); |
| 3280 | else |
| 3281 | blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT); |
Mike Snitzer | 0cc67cd | 2013-08-20 15:02:41 -0400 | [diff] [blame] | 3282 | blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT); |
| 3283 | } |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3284 | |
| 3285 | /* |
| 3286 | * pt->adjusted_pf is a staging area for the actual features to use. |
| 3287 | * They get transferred to the live pool in bind_control_target() |
| 3288 | * called from pool_preresume(). |
| 3289 | */ |
Mike Snitzer | b60ab99 | 2013-09-19 18:49:11 -0400 | [diff] [blame] | 3290 | if (!pt->adjusted_pf.discard_enabled) { |
| 3291 | /* |
| 3292 | * Must explicitly disallow stacking discard limits otherwise the |
| 3293 | * block layer will stack them if pool's data device has support. |
| 3294 | * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the |
| 3295 | * user to see that, so make sure to set all discard limits to 0. |
| 3296 | */ |
| 3297 | limits->discard_granularity = 0; |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3298 | return; |
Mike Snitzer | b60ab99 | 2013-09-19 18:49:11 -0400 | [diff] [blame] | 3299 | } |
Mike Snitzer | 0424caa | 2012-09-26 23:45:47 +0100 | [diff] [blame] | 3300 | |
| 3301 | disable_passdown_if_not_supported(pt); |
| 3302 | |
| 3303 | set_discard_limits(pt, limits); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | static struct target_type pool_target = { |
| 3307 | .name = "thin-pool", |
| 3308 | .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE | |
| 3309 | DM_TARGET_IMMUTABLE, |
Mike Snitzer | 36f12ae | 2014-10-09 15:24:12 -0400 | [diff] [blame^] | 3310 | .version = {1, 14, 0}, |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3311 | .module = THIS_MODULE, |
| 3312 | .ctr = pool_ctr, |
| 3313 | .dtr = pool_dtr, |
| 3314 | .map = pool_map, |
| 3315 | .postsuspend = pool_postsuspend, |
| 3316 | .preresume = pool_preresume, |
| 3317 | .resume = pool_resume, |
| 3318 | .message = pool_message, |
| 3319 | .status = pool_status, |
| 3320 | .merge = pool_merge, |
| 3321 | .iterate_devices = pool_iterate_devices, |
| 3322 | .io_hints = pool_io_hints, |
| 3323 | }; |
| 3324 | |
| 3325 | /*---------------------------------------------------------------- |
| 3326 | * Thin target methods |
| 3327 | *--------------------------------------------------------------*/ |
Joe Thornber | b10ebd3 | 2014-04-08 11:29:01 +0100 | [diff] [blame] | 3328 | static void thin_get(struct thin_c *tc) |
| 3329 | { |
| 3330 | atomic_inc(&tc->refcount); |
| 3331 | } |
| 3332 | |
| 3333 | static void thin_put(struct thin_c *tc) |
| 3334 | { |
| 3335 | if (atomic_dec_and_test(&tc->refcount)) |
| 3336 | complete(&tc->can_destroy); |
| 3337 | } |
| 3338 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3339 | static void thin_dtr(struct dm_target *ti) |
| 3340 | { |
| 3341 | struct thin_c *tc = ti->private; |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 3342 | unsigned long flags; |
| 3343 | |
Joe Thornber | b10ebd3 | 2014-04-08 11:29:01 +0100 | [diff] [blame] | 3344 | thin_put(tc); |
| 3345 | wait_for_completion(&tc->can_destroy); |
| 3346 | |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 3347 | spin_lock_irqsave(&tc->pool->lock, flags); |
| 3348 | list_del_rcu(&tc->list); |
| 3349 | spin_unlock_irqrestore(&tc->pool->lock, flags); |
| 3350 | synchronize_rcu(); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3351 | |
| 3352 | mutex_lock(&dm_thin_pool_table.mutex); |
| 3353 | |
| 3354 | __pool_dec(tc->pool); |
| 3355 | dm_pool_close_thin_device(tc->td); |
| 3356 | dm_put_device(ti, tc->pool_dev); |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3357 | if (tc->origin_dev) |
| 3358 | dm_put_device(ti, tc->origin_dev); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3359 | kfree(tc); |
| 3360 | |
| 3361 | mutex_unlock(&dm_thin_pool_table.mutex); |
| 3362 | } |
| 3363 | |
| 3364 | /* |
| 3365 | * Thin target parameters: |
| 3366 | * |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3367 | * <pool_dev> <dev_id> [origin_dev] |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3368 | * |
| 3369 | * pool_dev: the path to the pool (eg, /dev/mapper/my_pool) |
| 3370 | * dev_id: the internal device identifier |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3371 | * origin_dev: a device external to the pool that should act as the origin |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 3372 | * |
| 3373 | * If the pool device has discards disabled, they get disabled for the thin |
| 3374 | * device as well. |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3375 | */ |
| 3376 | static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv) |
| 3377 | { |
| 3378 | int r; |
| 3379 | struct thin_c *tc; |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3380 | struct dm_dev *pool_dev, *origin_dev; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3381 | struct mapped_device *pool_md; |
Joe Thornber | 5e3283e | 2014-04-08 11:08:41 +0100 | [diff] [blame] | 3382 | unsigned long flags; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3383 | |
| 3384 | mutex_lock(&dm_thin_pool_table.mutex); |
| 3385 | |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3386 | if (argc != 2 && argc != 3) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3387 | ti->error = "Invalid argument count"; |
| 3388 | r = -EINVAL; |
| 3389 | goto out_unlock; |
| 3390 | } |
| 3391 | |
| 3392 | tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL); |
| 3393 | if (!tc) { |
| 3394 | ti->error = "Out of memory"; |
| 3395 | r = -ENOMEM; |
| 3396 | goto out_unlock; |
| 3397 | } |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 3398 | spin_lock_init(&tc->lock); |
| 3399 | bio_list_init(&tc->deferred_bio_list); |
| 3400 | bio_list_init(&tc->retry_on_resume_list); |
Mike Snitzer | 67324ea | 2014-03-21 18:33:41 -0400 | [diff] [blame] | 3401 | tc->sort_bio_list = RB_ROOT; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3402 | |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3403 | if (argc == 3) { |
| 3404 | r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev); |
| 3405 | if (r) { |
| 3406 | ti->error = "Error opening origin device"; |
| 3407 | goto bad_origin_dev; |
| 3408 | } |
| 3409 | tc->origin_dev = origin_dev; |
| 3410 | } |
| 3411 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3412 | r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev); |
| 3413 | if (r) { |
| 3414 | ti->error = "Error opening pool device"; |
| 3415 | goto bad_pool_dev; |
| 3416 | } |
| 3417 | tc->pool_dev = pool_dev; |
| 3418 | |
| 3419 | if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) { |
| 3420 | ti->error = "Invalid device id"; |
| 3421 | r = -EINVAL; |
| 3422 | goto bad_common; |
| 3423 | } |
| 3424 | |
| 3425 | pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev); |
| 3426 | if (!pool_md) { |
| 3427 | ti->error = "Couldn't get pool mapped device"; |
| 3428 | r = -EINVAL; |
| 3429 | goto bad_common; |
| 3430 | } |
| 3431 | |
| 3432 | tc->pool = __pool_table_lookup(pool_md); |
| 3433 | if (!tc->pool) { |
| 3434 | ti->error = "Couldn't find pool object"; |
| 3435 | r = -EINVAL; |
| 3436 | goto bad_pool_lookup; |
| 3437 | } |
| 3438 | __pool_inc(tc->pool); |
| 3439 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3440 | if (get_pool_mode(tc->pool) == PM_FAIL) { |
| 3441 | ti->error = "Couldn't open thin device, Pool is in fail mode"; |
Mike Snitzer | 1acacc0 | 2014-02-19 20:32:33 -0500 | [diff] [blame] | 3442 | r = -EINVAL; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3443 | goto bad_thin_open; |
| 3444 | } |
| 3445 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3446 | r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td); |
| 3447 | if (r) { |
| 3448 | ti->error = "Couldn't open thin internal device"; |
| 3449 | goto bad_thin_open; |
| 3450 | } |
| 3451 | |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 3452 | r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block); |
| 3453 | if (r) |
Mike Snitzer | 1acacc0 | 2014-02-19 20:32:33 -0500 | [diff] [blame] | 3454 | goto bad_target_max_io_len; |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 3455 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 3456 | ti->num_flush_bios = 1; |
Joe Thornber | 16ad3d1 | 2012-07-27 15:08:07 +0100 | [diff] [blame] | 3457 | ti->flush_supported = true; |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 3458 | ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook); |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 3459 | |
| 3460 | /* In case the pool supports discards, pass them on. */ |
Mike Snitzer | b60ab99 | 2013-09-19 18:49:11 -0400 | [diff] [blame] | 3461 | ti->discard_zeroes_data_unsupported = true; |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 3462 | if (tc->pool->pf.discard_enabled) { |
Alasdair G Kergon | 0ac5548 | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 3463 | ti->discards_supported = true; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 3464 | ti->num_discard_bios = 1; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 3465 | /* Discard bios must be split on a block boundary */ |
| 3466 | ti->split_discard_bios = true; |
Joe Thornber | 67e2e2b | 2012-03-28 18:41:29 +0100 | [diff] [blame] | 3467 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3468 | |
| 3469 | dm_put(pool_md); |
| 3470 | |
| 3471 | mutex_unlock(&dm_thin_pool_table.mutex); |
| 3472 | |
Joe Thornber | b10ebd3 | 2014-04-08 11:29:01 +0100 | [diff] [blame] | 3473 | atomic_set(&tc->refcount, 1); |
| 3474 | init_completion(&tc->can_destroy); |
| 3475 | |
Joe Thornber | 5e3283e | 2014-04-08 11:08:41 +0100 | [diff] [blame] | 3476 | spin_lock_irqsave(&tc->pool->lock, flags); |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 3477 | list_add_tail_rcu(&tc->list, &tc->pool->active_thins); |
Joe Thornber | 5e3283e | 2014-04-08 11:08:41 +0100 | [diff] [blame] | 3478 | spin_unlock_irqrestore(&tc->pool->lock, flags); |
Mike Snitzer | c140e1c | 2014-03-20 21:17:14 -0400 | [diff] [blame] | 3479 | /* |
| 3480 | * This synchronize_rcu() call is needed here otherwise we risk a |
| 3481 | * wake_worker() call finding no bios to process (because the newly |
| 3482 | * added tc isn't yet visible). So this reduces latency since we |
| 3483 | * aren't then dependent on the periodic commit to wake_worker(). |
| 3484 | */ |
| 3485 | synchronize_rcu(); |
| 3486 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3487 | return 0; |
| 3488 | |
Mike Snitzer | 1acacc0 | 2014-02-19 20:32:33 -0500 | [diff] [blame] | 3489 | bad_target_max_io_len: |
| 3490 | dm_pool_close_thin_device(tc->td); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3491 | bad_thin_open: |
| 3492 | __pool_dec(tc->pool); |
| 3493 | bad_pool_lookup: |
| 3494 | dm_put(pool_md); |
| 3495 | bad_common: |
| 3496 | dm_put_device(ti, tc->pool_dev); |
| 3497 | bad_pool_dev: |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3498 | if (tc->origin_dev) |
| 3499 | dm_put_device(ti, tc->origin_dev); |
| 3500 | bad_origin_dev: |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3501 | kfree(tc); |
| 3502 | out_unlock: |
| 3503 | mutex_unlock(&dm_thin_pool_table.mutex); |
| 3504 | |
| 3505 | return r; |
| 3506 | } |
| 3507 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 3508 | static int thin_map(struct dm_target *ti, struct bio *bio) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3509 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3510 | bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3511 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 3512 | return thin_bio_map(ti, bio); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 3515 | static int thin_endio(struct dm_target *ti, struct bio *bio, int err) |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3516 | { |
| 3517 | unsigned long flags; |
Mikulas Patocka | 59c3d2c | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 3518 | struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3519 | struct list_head work; |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 3520 | struct dm_thin_new_mapping *m, *tmp; |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3521 | struct pool *pool = h->tc->pool; |
| 3522 | |
| 3523 | if (h->shared_read_entry) { |
| 3524 | INIT_LIST_HEAD(&work); |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 3525 | dm_deferred_entry_dec(h->shared_read_entry, &work); |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3526 | |
| 3527 | spin_lock_irqsave(&pool->lock, flags); |
| 3528 | list_for_each_entry_safe(m, tmp, &work, list) { |
| 3529 | list_del(&m->list); |
Joe Thornber | 50f3c3e | 2014-06-13 13:57:09 +0100 | [diff] [blame] | 3530 | __complete_mapping_preparation(m); |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3531 | } |
| 3532 | spin_unlock_irqrestore(&pool->lock, flags); |
| 3533 | } |
| 3534 | |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3535 | if (h->all_io_entry) { |
| 3536 | INIT_LIST_HEAD(&work); |
Mike Snitzer | 44feb38 | 2012-10-12 21:02:10 +0100 | [diff] [blame] | 3537 | dm_deferred_entry_dec(h->all_io_entry, &work); |
Joe Thornber | 563af18 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 3538 | if (!list_empty(&work)) { |
| 3539 | spin_lock_irqsave(&pool->lock, flags); |
| 3540 | list_for_each_entry_safe(m, tmp, &work, list) |
Mike Snitzer | daec338 | 2013-12-11 14:01:20 -0500 | [diff] [blame] | 3541 | list_add_tail(&m->list, &pool->prepared_discards); |
Joe Thornber | 563af18 | 2012-12-21 20:23:31 +0000 | [diff] [blame] | 3542 | spin_unlock_irqrestore(&pool->lock, flags); |
| 3543 | wake_worker(pool); |
| 3544 | } |
Joe Thornber | 104655f | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3545 | } |
| 3546 | |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3547 | return 0; |
| 3548 | } |
| 3549 | |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 3550 | static void thin_presuspend(struct dm_target *ti) |
| 3551 | { |
| 3552 | struct thin_c *tc = ti->private; |
| 3553 | |
| 3554 | if (dm_noflush_suspending(ti)) |
| 3555 | noflush_work(tc, do_noflush_start); |
| 3556 | } |
| 3557 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3558 | static void thin_postsuspend(struct dm_target *ti) |
| 3559 | { |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 3560 | struct thin_c *tc = ti->private; |
| 3561 | |
| 3562 | /* |
| 3563 | * The dm_noflush_suspending flag has been cleared by now, so |
| 3564 | * unfortunately we must always run this. |
| 3565 | */ |
| 3566 | noflush_work(tc, do_noflush_stop); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 3569 | static int thin_preresume(struct dm_target *ti) |
| 3570 | { |
| 3571 | struct thin_c *tc = ti->private; |
| 3572 | |
| 3573 | if (tc->origin_dev) |
| 3574 | tc->origin_size = get_dev_size(tc->origin_dev->bdev); |
| 3575 | |
| 3576 | return 0; |
| 3577 | } |
| 3578 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3579 | /* |
| 3580 | * <nr mapped sectors> <highest mapped sector> |
| 3581 | */ |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3582 | static void thin_status(struct dm_target *ti, status_type_t type, |
| 3583 | unsigned status_flags, char *result, unsigned maxlen) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3584 | { |
| 3585 | int r; |
| 3586 | ssize_t sz = 0; |
| 3587 | dm_block_t mapped, highest; |
| 3588 | char buf[BDEVNAME_SIZE]; |
| 3589 | struct thin_c *tc = ti->private; |
| 3590 | |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3591 | if (get_pool_mode(tc->pool) == PM_FAIL) { |
| 3592 | DMEMIT("Fail"); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3593 | return; |
Joe Thornber | e49e582 | 2012-07-27 15:08:16 +0100 | [diff] [blame] | 3594 | } |
| 3595 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3596 | if (!tc->td) |
| 3597 | DMEMIT("-"); |
| 3598 | else { |
| 3599 | switch (type) { |
| 3600 | case STATUSTYPE_INFO: |
| 3601 | r = dm_thin_get_mapped_count(tc->td, &mapped); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3602 | if (r) { |
| 3603 | DMERR("dm_thin_get_mapped_count returned %d", r); |
| 3604 | goto err; |
| 3605 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3606 | |
| 3607 | r = dm_thin_get_highest_mapped_block(tc->td, &highest); |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3608 | if (r < 0) { |
| 3609 | DMERR("dm_thin_get_highest_mapped_block returned %d", r); |
| 3610 | goto err; |
| 3611 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3612 | |
| 3613 | DMEMIT("%llu ", mapped * tc->pool->sectors_per_block); |
| 3614 | if (r) |
| 3615 | DMEMIT("%llu", ((highest + 1) * |
| 3616 | tc->pool->sectors_per_block) - 1); |
| 3617 | else |
| 3618 | DMEMIT("-"); |
| 3619 | break; |
| 3620 | |
| 3621 | case STATUSTYPE_TABLE: |
| 3622 | DMEMIT("%s %lu", |
| 3623 | format_dev_t(buf, tc->pool_dev->bdev->bd_dev), |
| 3624 | (unsigned long) tc->dev_id); |
Joe Thornber | 2dd9c25 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3625 | if (tc->origin_dev) |
| 3626 | DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev)); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3627 | break; |
| 3628 | } |
| 3629 | } |
| 3630 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3631 | return; |
| 3632 | |
| 3633 | err: |
| 3634 | DMEMIT("Error"); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Mike Snitzer | 36f12ae | 2014-10-09 15:24:12 -0400 | [diff] [blame^] | 3637 | static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm, |
| 3638 | struct bio_vec *biovec, int max_size) |
| 3639 | { |
| 3640 | struct thin_c *tc = ti->private; |
| 3641 | struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev); |
| 3642 | |
| 3643 | if (!q->merge_bvec_fn) |
| 3644 | return max_size; |
| 3645 | |
| 3646 | bvm->bi_bdev = tc->pool_dev->bdev; |
| 3647 | bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector); |
| 3648 | |
| 3649 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); |
| 3650 | } |
| 3651 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3652 | static int thin_iterate_devices(struct dm_target *ti, |
| 3653 | iterate_devices_callout_fn fn, void *data) |
| 3654 | { |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 3655 | sector_t blocks; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3656 | struct thin_c *tc = ti->private; |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 3657 | struct pool *pool = tc->pool; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3658 | |
| 3659 | /* |
| 3660 | * We can't call dm_pool_get_data_dev_size() since that blocks. So |
| 3661 | * we follow a more convoluted path through to the pool's target. |
| 3662 | */ |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 3663 | if (!pool->ti) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3664 | return 0; /* nothing is bound */ |
| 3665 | |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 3666 | blocks = pool->ti->len; |
| 3667 | (void) sector_div(blocks, pool->sectors_per_block); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3668 | if (blocks) |
Mike Snitzer | 55f2b8b | 2012-07-27 15:08:02 +0100 | [diff] [blame] | 3669 | return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3670 | |
| 3671 | return 0; |
| 3672 | } |
| 3673 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3674 | static struct target_type thin_target = { |
| 3675 | .name = "thin", |
Mike Snitzer | 36f12ae | 2014-10-09 15:24:12 -0400 | [diff] [blame^] | 3676 | .version = {1, 14, 0}, |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3677 | .module = THIS_MODULE, |
| 3678 | .ctr = thin_ctr, |
| 3679 | .dtr = thin_dtr, |
| 3680 | .map = thin_map, |
Joe Thornber | eb2aa48 | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 3681 | .end_io = thin_endio, |
Joe Thornber | e5aea7b | 2014-06-13 14:47:24 +0100 | [diff] [blame] | 3682 | .preresume = thin_preresume, |
Joe Thornber | 738211f | 2014-03-03 15:52:28 +0000 | [diff] [blame] | 3683 | .presuspend = thin_presuspend, |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3684 | .postsuspend = thin_postsuspend, |
| 3685 | .status = thin_status, |
Mike Snitzer | 36f12ae | 2014-10-09 15:24:12 -0400 | [diff] [blame^] | 3686 | .merge = thin_merge, |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3687 | .iterate_devices = thin_iterate_devices, |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3688 | }; |
| 3689 | |
| 3690 | /*----------------------------------------------------------------*/ |
| 3691 | |
| 3692 | static int __init dm_thin_init(void) |
| 3693 | { |
| 3694 | int r; |
| 3695 | |
| 3696 | pool_table_init(); |
| 3697 | |
| 3698 | r = dm_register_target(&thin_target); |
| 3699 | if (r) |
| 3700 | return r; |
| 3701 | |
| 3702 | r = dm_register_target(&pool_target); |
| 3703 | if (r) |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 3704 | goto bad_pool_target; |
| 3705 | |
| 3706 | r = -ENOMEM; |
| 3707 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 3708 | _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0); |
| 3709 | if (!_new_mapping_cache) |
| 3710 | goto bad_new_mapping_cache; |
| 3711 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 3712 | return 0; |
| 3713 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 3714 | bad_new_mapping_cache: |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 3715 | dm_unregister_target(&pool_target); |
| 3716 | bad_pool_target: |
| 3717 | dm_unregister_target(&thin_target); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3718 | |
| 3719 | return r; |
| 3720 | } |
| 3721 | |
| 3722 | static void dm_thin_exit(void) |
| 3723 | { |
| 3724 | dm_unregister_target(&thin_target); |
| 3725 | dm_unregister_target(&pool_target); |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 3726 | |
Mike Snitzer | a24c256 | 2012-06-03 00:30:00 +0100 | [diff] [blame] | 3727 | kmem_cache_destroy(_new_mapping_cache); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | module_init(dm_thin_init); |
| 3731 | module_exit(dm_thin_exit); |
| 3732 | |
Mike Snitzer | 80c5789 | 2014-05-20 13:38:33 -0400 | [diff] [blame] | 3733 | module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR); |
| 3734 | MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds"); |
| 3735 | |
Alasdair G Kergon | 7cab8bf | 2012-05-12 01:43:19 +0100 | [diff] [blame] | 3736 | MODULE_DESCRIPTION(DM_NAME " thin provisioning target"); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3737 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); |
| 3738 | MODULE_LICENSE("GPL"); |