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