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