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