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