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