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