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