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