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