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