blob: cb58bb3187824bfd065c1b517e75cfc063ff3b19 [file] [log] [blame]
Joe Thornber991d9fa2011-10-31 20:21:18 +00001/*
Joe Thornbere49e5822012-07-27 15:08:16 +01002 * Copyright (C) 2011-2012 Red Hat UK.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-thin-metadata.h"
Mike Snitzer4f81a412012-10-12 21:02:13 +01008#include "dm-bio-prison.h"
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01009#include "dm.h"
Joe Thornber991d9fa2011-10-31 20:21:18 +000010
11#include <linux/device-mapper.h>
12#include <linux/dm-io.h>
13#include <linux/dm-kcopyd.h>
Manuel Schölling0f30af92014-05-22 22:42:37 +020014#include <linux/jiffies.h>
Mike Snitzer604ea902014-10-09 18:43:25 -040015#include <linux/log2.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000016#include <linux/list.h>
Mike Snitzerc140e1c2014-03-20 21:17:14 -040017#include <linux/rculist.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000018#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/slab.h>
Joe Thornberb2ce1402015-07-03 10:22:42 +010021#include <linux/vmalloc.h>
Joe Thornberac4c3f32014-10-10 16:42:10 +010022#include <linux/sort.h>
Mike Snitzer67324ea2014-03-21 18:33:41 -040023#include <linux/rbtree.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000024
25#define DM_MSG_PREFIX "thin"
26
27/*
28 * Tunable constants
29 */
Alasdair G Kergon7768ed32012-07-27 15:07:57 +010030#define ENDIO_HOOK_POOL_SIZE 1024
Joe Thornber991d9fa2011-10-31 20:21:18 +000031#define MAPPING_POOL_SIZE 1024
Joe Thornber905e51b2012-03-28 18:41:27 +010032#define COMMIT_PERIOD HZ
Mike Snitzer80c57892014-05-20 13:38:33 -040033#define NO_SPACE_TIMEOUT_SECS 60
34
35static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
Joe Thornber991d9fa2011-10-31 20:21:18 +000036
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000037DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
38 "A percentage of time allocated for copy on write");
39
Joe Thornber991d9fa2011-10-31 20:21:18 +000040/*
41 * The block size of the device holding pool data must be
42 * between 64KB and 1GB.
43 */
44#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
45#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
46
47/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000048 * Device id is restricted to 24 bits.
49 */
50#define MAX_DEV_ID ((1 << 24) - 1)
51
52/*
53 * How do we handle breaking sharing of data blocks?
54 * =================================================
55 *
56 * We use a standard copy-on-write btree to store the mappings for the
57 * devices (note I'm talking about copy-on-write of the metadata here, not
58 * the data). When you take an internal snapshot you clone the root node
59 * of the origin btree. After this there is no concept of an origin or a
60 * snapshot. They are just two device trees that happen to point to the
61 * same data blocks.
62 *
63 * When we get a write in we decide if it's to a shared data block using
64 * some timestamp magic. If it is, we have to break sharing.
65 *
66 * Let's say we write to a shared block in what was the origin. The
67 * steps are:
68 *
69 * i) plug io further to this physical block. (see bio_prison code).
70 *
71 * ii) quiesce any read io to that shared data block. Obviously
Mike Snitzer44feb382012-10-12 21:02:10 +010072 * including all devices that share this block. (see dm_deferred_set code)
Joe Thornber991d9fa2011-10-31 20:21:18 +000073 *
74 * iii) copy the data block to a newly allocate block. This step can be
75 * missed out if the io covers the block. (schedule_copy).
76 *
77 * iv) insert the new mapping into the origin's btree
Joe Thornberfe878f32012-03-28 18:41:24 +010078 * (process_prepared_mapping). This act of inserting breaks some
Joe Thornber991d9fa2011-10-31 20:21:18 +000079 * sharing of btree nodes between the two devices. Breaking sharing only
80 * effects the btree of that specific device. Btrees for the other
81 * devices that share the block never change. The btree for the origin
82 * device as it was after the last commit is untouched, ie. we're using
83 * persistent data structures in the functional programming sense.
84 *
85 * v) unplug io to this physical block, including the io that triggered
86 * the breaking of sharing.
87 *
88 * Steps (ii) and (iii) occur in parallel.
89 *
90 * The metadata _doesn't_ need to be committed before the io continues. We
91 * get away with this because the io is always written to a _new_ block.
92 * If there's a crash, then:
93 *
94 * - The origin mapping will point to the old origin block (the shared
95 * one). This will contain the data as it was before the io that triggered
96 * the breaking of sharing came in.
97 *
98 * - The snap mapping still points to the old block. As it would after
99 * the commit.
100 *
101 * The downside of this scheme is the timestamp magic isn't perfect, and
102 * will continue to think that data block in the snapshot device is shared
103 * even after the write to the origin has broken sharing. I suspect data
104 * blocks will typically be shared by many different devices, so we're
105 * breaking sharing n + 1 times, rather than n, where n is the number of
106 * devices that reference this data block. At the moment I think the
107 * benefits far, far outweigh the disadvantages.
108 */
109
110/*----------------------------------------------------------------*/
111
112/*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000113 * Key building.
114 */
115static void build_data_key(struct dm_thin_device *td,
Mike Snitzer44feb382012-10-12 21:02:10 +0100116 dm_block_t b, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000117{
118 key->virtual = 0;
119 key->dev = dm_thin_dev_id(td);
Joe Thornber5f274d82014-09-17 10:17:39 +0100120 key->block_begin = b;
121 key->block_end = b + 1ULL;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000122}
123
124static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100125 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000126{
127 key->virtual = 1;
128 key->dev = dm_thin_dev_id(td);
Joe Thornber5f274d82014-09-17 10:17:39 +0100129 key->block_begin = b;
130 key->block_end = b + 1ULL;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000131}
132
133/*----------------------------------------------------------------*/
134
Joe Thornber7d327fe2014-10-06 15:45:59 +0100135#define THROTTLE_THRESHOLD (1 * HZ)
136
137struct throttle {
138 struct rw_semaphore lock;
139 unsigned long threshold;
140 bool throttle_applied;
141};
142
143static void throttle_init(struct throttle *t)
144{
145 init_rwsem(&t->lock);
146 t->throttle_applied = false;
147}
148
149static void throttle_work_start(struct throttle *t)
150{
151 t->threshold = jiffies + THROTTLE_THRESHOLD;
152}
153
154static void throttle_work_update(struct throttle *t)
155{
156 if (!t->throttle_applied && jiffies > t->threshold) {
157 down_write(&t->lock);
158 t->throttle_applied = true;
159 }
160}
161
162static void throttle_work_complete(struct throttle *t)
163{
164 if (t->throttle_applied) {
165 t->throttle_applied = false;
166 up_write(&t->lock);
167 }
168}
169
170static void throttle_lock(struct throttle *t)
171{
172 down_read(&t->lock);
173}
174
175static void throttle_unlock(struct throttle *t)
176{
177 up_read(&t->lock);
178}
179
180/*----------------------------------------------------------------*/
181
Joe Thornber991d9fa2011-10-31 20:21:18 +0000182/*
183 * A pool device ties together a metadata device and a data device. It
184 * also provides the interface for creating and destroying internal
185 * devices.
186 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100187struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100188
Joe Thornbere49e5822012-07-27 15:08:16 +0100189/*
Joe Thornber3e1a0692014-03-03 16:03:26 +0000190 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
Joe Thornbere49e5822012-07-27 15:08:16 +0100191 */
192enum pool_mode {
193 PM_WRITE, /* metadata may be changed */
Joe Thornber3e1a0692014-03-03 16:03:26 +0000194 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
Joe Thornbere49e5822012-07-27 15:08:16 +0100195 PM_READ_ONLY, /* metadata may not be changed */
196 PM_FAIL, /* all I/O fails */
197};
198
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100199struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100200 enum pool_mode mode;
201
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100202 bool zero_new_blocks:1;
203 bool discard_enabled:1;
204 bool discard_passdown:1;
Mike Snitzer787a996c2013-12-06 16:21:43 -0500205 bool error_if_no_space:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100206};
207
Joe Thornbere49e5822012-07-27 15:08:16 +0100208struct thin_c;
209typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
Joe Thornbera374bb22014-10-10 13:43:14 +0100210typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100211typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
212
Joe Thornberac4c3f32014-10-10 16:42:10 +0100213#define CELL_SORT_ARRAY_SIZE 8192
214
Joe Thornber991d9fa2011-10-31 20:21:18 +0000215struct pool {
216 struct list_head list;
217 struct dm_target *ti; /* Only set if a pool target is bound */
218
219 struct mapped_device *pool_md;
220 struct block_device *md_dev;
221 struct dm_pool_metadata *pmd;
222
Joe Thornber991d9fa2011-10-31 20:21:18 +0000223 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100224 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100225 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000226
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100227 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500228 bool low_water_triggered:1; /* A dm event has been sent */
Mike Snitzer80e96c52014-11-07 15:09:46 -0500229 bool suspended:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000230
Mike Snitzer44feb382012-10-12 21:02:10 +0100231 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000232 struct dm_kcopyd_client *copier;
233
234 struct workqueue_struct *wq;
Joe Thornber7d327fe2014-10-06 15:45:59 +0100235 struct throttle throttle;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000236 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100237 struct delayed_work waker;
Joe Thornber85ad6432014-05-09 15:59:38 +0100238 struct delayed_work no_space_timeout;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000239
Joe Thornber905e51b2012-03-28 18:41:27 +0100240 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100241 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000242
243 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000244 struct bio_list deferred_flush_bios;
245 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100246 struct list_head prepared_discards;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400247 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000248
Mike Snitzer44feb382012-10-12 21:02:10 +0100249 struct dm_deferred_set *shared_read_ds;
250 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000251
Mike Snitzera24c2562012-06-03 00:30:00 +0100252 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000253 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100254
255 process_bio_fn process_bio;
256 process_bio_fn process_discard;
257
Joe Thornbera374bb22014-10-10 13:43:14 +0100258 process_cell_fn process_cell;
259 process_cell_fn process_discard_cell;
260
Joe Thornbere49e5822012-07-27 15:08:16 +0100261 process_mapping_fn process_prepared_mapping;
262 process_mapping_fn process_prepared_discard;
Joe Thornberac4c3f32014-10-10 16:42:10 +0100263
Joe Thornberb2ce1402015-07-03 10:22:42 +0100264 struct dm_bio_prison_cell **cell_sort_array;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000265};
266
Joe Thornbere49e5822012-07-27 15:08:16 +0100267static enum pool_mode get_pool_mode(struct pool *pool);
Joe Thornberb5330652013-12-04 19:51:33 -0500268static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100269
Joe Thornber991d9fa2011-10-31 20:21:18 +0000270/*
271 * Target context for a pool.
272 */
273struct pool_c {
274 struct dm_target *ti;
275 struct pool *pool;
276 struct dm_dev *data_dev;
277 struct dm_dev *metadata_dev;
278 struct dm_target_callbacks callbacks;
279
280 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100281 struct pool_features requested_pf; /* Features requested during table load */
282 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000283};
284
285/*
286 * Target context for a thin.
287 */
288struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400289 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000290 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100291 struct dm_dev *origin_dev;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100292 sector_t origin_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000293 dm_thin_id dev_id;
294
295 struct pool *pool;
296 struct dm_thin_device *td;
Mike Snitzer583024d2014-10-28 20:58:45 -0400297 struct mapped_device *thin_md;
298
Joe Thornber738211f2014-03-03 15:52:28 +0000299 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400300 spinlock_t lock;
Joe Thornbera374bb22014-10-10 13:43:14 +0100301 struct list_head deferred_cells;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400302 struct bio_list deferred_bio_list;
303 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400304 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornberb10ebd32014-04-08 11:29:01 +0100305
306 /*
307 * Ensures the thin is not destroyed until the worker has finished
308 * iterating the active_thins list.
309 */
310 atomic_t refcount;
311 struct completion can_destroy;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000312};
313
314/*----------------------------------------------------------------*/
315
Joe Thornber025b9682013-03-01 22:45:50 +0000316/*
317 * wake_worker() is used when new work is queued and when pool_resume is
318 * ready to continue deferred IO processing.
319 */
320static void wake_worker(struct pool *pool)
321{
322 queue_work(pool->wq, &pool->worker);
323}
324
325/*----------------------------------------------------------------*/
326
Joe Thornber6beca5e2013-03-01 22:45:50 +0000327static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
328 struct dm_bio_prison_cell **cell_result)
329{
330 int r;
331 struct dm_bio_prison_cell *cell_prealloc;
332
333 /*
334 * Allocate a cell from the prison's mempool.
335 * This might block but it can't fail.
336 */
337 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
338
339 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
340 if (r)
341 /*
342 * We reused an old cell; we can get rid of
343 * the new one.
344 */
345 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
346
347 return r;
348}
349
350static void cell_release(struct pool *pool,
351 struct dm_bio_prison_cell *cell,
352 struct bio_list *bios)
353{
354 dm_cell_release(pool->prison, cell, bios);
355 dm_bio_prison_free_cell(pool->prison, cell);
356}
357
Joe Thornber2d759a42014-10-10 15:27:16 +0100358static void cell_visit_release(struct pool *pool,
359 void (*fn)(void *, struct dm_bio_prison_cell *),
360 void *context,
361 struct dm_bio_prison_cell *cell)
362{
363 dm_cell_visit_release(pool->prison, fn, context, cell);
364 dm_bio_prison_free_cell(pool->prison, cell);
365}
366
Joe Thornber6beca5e2013-03-01 22:45:50 +0000367static void cell_release_no_holder(struct pool *pool,
368 struct dm_bio_prison_cell *cell,
369 struct bio_list *bios)
370{
371 dm_cell_release_no_holder(pool->prison, cell, bios);
372 dm_bio_prison_free_cell(pool->prison, cell);
373}
374
Mike Snitzeraf918052014-05-22 14:32:51 -0400375static void cell_error_with_code(struct pool *pool,
376 struct dm_bio_prison_cell *cell, int error_code)
Joe Thornber6beca5e2013-03-01 22:45:50 +0000377{
Mike Snitzeraf918052014-05-22 14:32:51 -0400378 dm_cell_error(pool->prison, cell, error_code);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000379 dm_bio_prison_free_cell(pool->prison, cell);
380}
381
Mike Snitzeraf918052014-05-22 14:32:51 -0400382static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
383{
384 cell_error_with_code(pool, cell, -EIO);
385}
386
Joe Thornbera374bb22014-10-10 13:43:14 +0100387static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
388{
389 cell_error_with_code(pool, cell, 0);
390}
391
392static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
393{
394 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
395}
396
Joe Thornber6beca5e2013-03-01 22:45:50 +0000397/*----------------------------------------------------------------*/
398
Joe Thornber991d9fa2011-10-31 20:21:18 +0000399/*
400 * A global list of pools that uses a struct mapped_device as a key.
401 */
402static struct dm_thin_pool_table {
403 struct mutex mutex;
404 struct list_head pools;
405} dm_thin_pool_table;
406
407static void pool_table_init(void)
408{
409 mutex_init(&dm_thin_pool_table.mutex);
410 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
411}
412
413static void __pool_table_insert(struct pool *pool)
414{
415 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
416 list_add(&pool->list, &dm_thin_pool_table.pools);
417}
418
419static void __pool_table_remove(struct pool *pool)
420{
421 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
422 list_del(&pool->list);
423}
424
425static struct pool *__pool_table_lookup(struct mapped_device *md)
426{
427 struct pool *pool = NULL, *tmp;
428
429 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
430
431 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
432 if (tmp->pool_md == md) {
433 pool = tmp;
434 break;
435 }
436 }
437
438 return pool;
439}
440
441static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
442{
443 struct pool *pool = NULL, *tmp;
444
445 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
446
447 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
448 if (tmp->md_dev == md_dev) {
449 pool = tmp;
450 break;
451 }
452 }
453
454 return pool;
455}
456
457/*----------------------------------------------------------------*/
458
Mike Snitzera24c2562012-06-03 00:30:00 +0100459struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100460 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100461 struct dm_deferred_entry *shared_read_entry;
462 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100463 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400464 struct rb_node rb_node;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100465};
466
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400467static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
468{
469 bio_list_merge(bios, master);
470 bio_list_init(master);
471}
472
473static void error_bio_list(struct bio_list *bios, int error)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000474{
475 struct bio *bio;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400476
477 while ((bio = bio_list_pop(bios)))
478 bio_endio(bio, error);
479}
480
481static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
482{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000483 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000484 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000485
486 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000487
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400488 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400489 __merge_bio_list(&bios, master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400490 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000491
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400492 error_bio_list(&bios, error);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000493}
494
Joe Thornbera374bb22014-10-10 13:43:14 +0100495static void requeue_deferred_cells(struct thin_c *tc)
496{
497 struct pool *pool = tc->pool;
498 unsigned long flags;
499 struct list_head cells;
500 struct dm_bio_prison_cell *cell, *tmp;
501
502 INIT_LIST_HEAD(&cells);
503
504 spin_lock_irqsave(&tc->lock, flags);
505 list_splice_init(&tc->deferred_cells, &cells);
506 spin_unlock_irqrestore(&tc->lock, flags);
507
508 list_for_each_entry_safe(cell, tmp, &cells, user_list)
509 cell_requeue(pool, cell);
510}
511
Joe Thornber991d9fa2011-10-31 20:21:18 +0000512static void requeue_io(struct thin_c *tc)
513{
Joe Thornber3e1a0692014-03-03 16:03:26 +0000514 struct bio_list bios;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400515 unsigned long flags;
Joe Thornber3e1a0692014-03-03 16:03:26 +0000516
517 bio_list_init(&bios);
518
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400519 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400520 __merge_bio_list(&bios, &tc->deferred_bio_list);
521 __merge_bio_list(&bios, &tc->retry_on_resume_list);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400522 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000523
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400524 error_bio_list(&bios, DM_ENDIO_REQUEUE);
525 requeue_deferred_cells(tc);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000526}
527
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400528static void error_retry_list(struct pool *pool)
529{
530 struct thin_c *tc;
531
532 rcu_read_lock();
533 list_for_each_entry_rcu(tc, &pool->active_thins, list)
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400534 error_thin_bio_list(tc, &tc->retry_on_resume_list, -EIO);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400535 rcu_read_unlock();
536}
537
Joe Thornber991d9fa2011-10-31 20:21:18 +0000538/*
539 * This section of code contains the logic for processing a thin device's IO.
540 * Much of the code depends on pool object resources (lists, workqueues, etc)
541 * but most is exclusively called from the thin target rather than the thin-pool
542 * target.
543 */
544
Mike Snitzer58f77a22013-03-01 22:45:45 +0000545static bool block_size_is_power_of_two(struct pool *pool)
546{
547 return pool->sectors_per_block_shift >= 0;
548}
549
Joe Thornber991d9fa2011-10-31 20:21:18 +0000550static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
551{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000552 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700553 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100554
Mike Snitzer58f77a22013-03-01 22:45:45 +0000555 if (block_size_is_power_of_two(pool))
556 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100557 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000558 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100559
560 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000561}
562
563static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
564{
565 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700566 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000567
568 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000569 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700570 bio->bi_iter.bi_sector =
571 (block << pool->sectors_per_block_shift) |
572 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000573 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700574 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000575 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000576}
577
Joe Thornber2dd9c252012-03-28 18:41:28 +0100578static void remap_to_origin(struct thin_c *tc, struct bio *bio)
579{
580 bio->bi_bdev = tc->origin_dev->bdev;
581}
582
Joe Thornber4afdd682012-07-27 15:08:14 +0100583static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
584{
585 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
586 dm_thin_changed_this_transaction(tc->td);
587}
588
Joe Thornbere8088072012-12-21 20:23:31 +0000589static void inc_all_io_entry(struct pool *pool, struct bio *bio)
590{
591 struct dm_thin_endio_hook *h;
592
593 if (bio->bi_rw & REQ_DISCARD)
594 return;
595
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000596 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000597 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
598}
599
Joe Thornber2dd9c252012-03-28 18:41:28 +0100600static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000601{
602 struct pool *pool = tc->pool;
603 unsigned long flags;
604
Joe Thornbere49e5822012-07-27 15:08:16 +0100605 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000606 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100607 return;
608 }
609
610 /*
611 * Complete bio with an error if earlier I/O caused changes to
612 * the metadata that can't be committed e.g, due to I/O errors
613 * on the metadata device.
614 */
615 if (dm_thin_aborted_changes(tc->td)) {
616 bio_io_error(bio);
617 return;
618 }
619
620 /*
621 * Batch together any bios that trigger commits and then issue a
622 * single commit for them in process_deferred_bios().
623 */
624 spin_lock_irqsave(&pool->lock, flags);
625 bio_list_add(&pool->deferred_flush_bios, bio);
626 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000627}
628
Joe Thornber2dd9c252012-03-28 18:41:28 +0100629static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
630{
631 remap_to_origin(tc, bio);
632 issue(tc, bio);
633}
634
635static void remap_and_issue(struct thin_c *tc, struct bio *bio,
636 dm_block_t block)
637{
638 remap(tc, bio, block);
639 issue(tc, bio);
640}
641
Joe Thornber991d9fa2011-10-31 20:21:18 +0000642/*----------------------------------------------------------------*/
643
644/*
645 * Bio endio functions.
646 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100647struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000648 struct list_head list;
649
Mike Snitzer7f214662013-12-17 13:43:31 -0500650 bool pass_discard:1;
651 bool definitely_not_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000652
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100653 /*
654 * Track quiescing, copying and zeroing preparation actions. When this
655 * counter hits zero the block is prepared and can be inserted into the
656 * btree.
657 */
658 atomic_t prepare_actions;
659
Mike Snitzer7f214662013-12-17 13:43:31 -0500660 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000661 struct thin_c *tc;
662 dm_block_t virt_block;
663 dm_block_t data_block;
Mike Snitzera24c2562012-06-03 00:30:00 +0100664 struct dm_bio_prison_cell *cell, *cell2;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000665
666 /*
667 * If the bio covers the whole area of a block then we can avoid
668 * zeroing or copying. Instead this bio is hooked. The bio will
669 * still be in the cell, so care has to be taken to avoid issuing
670 * the bio twice.
671 */
672 struct bio *bio;
673 bio_end_io_t *saved_bi_end_io;
674};
675
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100676static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000677{
678 struct pool *pool = m->tc->pool;
679
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100680 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500681 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000682 wake_worker(pool);
683 }
684}
685
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100686static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000687{
688 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000689 struct pool *pool = m->tc->pool;
690
Joe Thornber991d9fa2011-10-31 20:21:18 +0000691 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100692 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000693 spin_unlock_irqrestore(&pool->lock, flags);
694}
695
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100696static void copy_complete(int read_err, unsigned long write_err, void *context)
697{
698 struct dm_thin_new_mapping *m = context;
699
700 m->err = read_err || write_err ? -EIO : 0;
701 complete_mapping_preparation(m);
702}
703
Joe Thornber991d9fa2011-10-31 20:21:18 +0000704static void overwrite_endio(struct bio *bio, int err)
705{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000706 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100707 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000708
709 m->err = err;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100710 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000711}
712
Joe Thornber991d9fa2011-10-31 20:21:18 +0000713/*----------------------------------------------------------------*/
714
715/*
716 * Workqueue.
717 */
718
719/*
720 * Prepared mapping jobs.
721 */
722
723/*
Joe Thornber2d759a42014-10-10 15:27:16 +0100724 * This sends the bios in the cell, except the original holder, back
725 * to the deferred_bios list.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000726 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000727static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000728{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000729 struct pool *pool = tc->pool;
730 unsigned long flags;
731
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400732 spin_lock_irqsave(&tc->lock, flags);
733 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
734 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000735
736 wake_worker(pool);
737}
738
Joe Thornbera374bb22014-10-10 13:43:14 +0100739static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
740
Joe Thornber2d759a42014-10-10 15:27:16 +0100741struct remap_info {
742 struct thin_c *tc;
743 struct bio_list defer_bios;
744 struct bio_list issue_bios;
745};
746
747static void __inc_remap_and_issue_cell(void *context,
748 struct dm_bio_prison_cell *cell)
749{
750 struct remap_info *info = context;
751 struct bio *bio;
752
753 while ((bio = bio_list_pop(&cell->bios))) {
754 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
755 bio_list_add(&info->defer_bios, bio);
756 else {
757 inc_all_io_entry(info->tc->pool, bio);
758
759 /*
760 * We can't issue the bios with the bio prison lock
761 * held, so we add them to a list to issue on
762 * return from this function.
763 */
764 bio_list_add(&info->issue_bios, bio);
765 }
766 }
767}
768
Joe Thornbera374bb22014-10-10 13:43:14 +0100769static void inc_remap_and_issue_cell(struct thin_c *tc,
770 struct dm_bio_prison_cell *cell,
771 dm_block_t block)
772{
773 struct bio *bio;
Joe Thornber2d759a42014-10-10 15:27:16 +0100774 struct remap_info info;
Joe Thornbera374bb22014-10-10 13:43:14 +0100775
Joe Thornber2d759a42014-10-10 15:27:16 +0100776 info.tc = tc;
777 bio_list_init(&info.defer_bios);
778 bio_list_init(&info.issue_bios);
Joe Thornbera374bb22014-10-10 13:43:14 +0100779
Joe Thornber2d759a42014-10-10 15:27:16 +0100780 /*
781 * We have to be careful to inc any bios we're about to issue
782 * before the cell is released, and avoid a race with new bios
783 * being added to the cell.
784 */
785 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
786 &info, cell);
787
788 while ((bio = bio_list_pop(&info.defer_bios)))
789 thin_defer_bio(tc, bio);
790
791 while ((bio = bio_list_pop(&info.issue_bios)))
792 remap_and_issue(info.tc, bio, block);
Joe Thornbera374bb22014-10-10 13:43:14 +0100793}
794
Joe Thornbere49e5822012-07-27 15:08:16 +0100795static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
796{
Kent Overstreet196d38b2013-11-23 18:34:15 -0800797 if (m->bio) {
Joe Thornbere49e5822012-07-27 15:08:16 +0100798 m->bio->bi_end_io = m->saved_bi_end_io;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800799 atomic_inc(&m->bio->bi_remaining);
800 }
Joe Thornber6beca5e2013-03-01 22:45:50 +0000801 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100802 list_del(&m->list);
803 mempool_free(m, m->tc->pool->mapping_pool);
804}
Joe Thornber025b9682013-03-01 22:45:50 +0000805
Mike Snitzera24c2562012-06-03 00:30:00 +0100806static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000807{
808 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000809 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000810 struct bio *bio;
811 int r;
812
813 bio = m->bio;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800814 if (bio) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000815 bio->bi_end_io = m->saved_bi_end_io;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800816 atomic_inc(&bio->bi_remaining);
817 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000818
819 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000820 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100821 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000822 }
823
824 /*
825 * Commit the prepared block into the mapping btree.
826 * Any I/O for this block arriving after this point will get
827 * remapped to it directly.
828 */
829 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
830 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500831 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000832 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100833 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000834 }
835
836 /*
837 * Release any bios held while the block was being provisioned.
838 * If we are processing a write bio that completely covers the block,
839 * we already processed it so can ignore it now when processing
840 * the bios in the cell.
841 */
842 if (bio) {
Joe Thornber2d759a42014-10-10 15:27:16 +0100843 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000844 bio_endio(bio, 0);
Joe Thornber2d759a42014-10-10 15:27:16 +0100845 } else {
846 inc_all_io_entry(tc->pool, m->cell->holder);
847 remap_and_issue(tc, m->cell->holder, m->data_block);
848 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
849 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000850
Joe Thornber905386f2012-07-27 15:08:05 +0100851out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000852 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000853 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000854}
855
Joe Thornbere49e5822012-07-27 15:08:16 +0100856static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100857{
Joe Thornber104655f2012-03-28 18:41:28 +0100858 struct thin_c *tc = m->tc;
859
Joe Thornbere49e5822012-07-27 15:08:16 +0100860 bio_io_error(m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000861 cell_defer_no_holder(tc, m->cell);
862 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere49e5822012-07-27 15:08:16 +0100863 mempool_free(m, tc->pool->mapping_pool);
864}
Joe Thornber104655f2012-03-28 18:41:28 +0100865
Joe Thornbere49e5822012-07-27 15:08:16 +0100866static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
867{
868 struct thin_c *tc = m->tc;
869
Joe Thornbere8088072012-12-21 20:23:31 +0000870 inc_all_io_entry(tc->pool, m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000871 cell_defer_no_holder(tc, m->cell);
872 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere8088072012-12-21 20:23:31 +0000873
Joe Thornber104655f2012-03-28 18:41:28 +0100874 if (m->pass_discard)
Joe Thornber19fa1a62013-12-17 12:09:40 -0500875 if (m->definitely_not_shared)
876 remap_and_issue(tc, m->bio, m->data_block);
877 else {
878 bool used = false;
879 if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used)
880 bio_endio(m->bio, 0);
881 else
882 remap_and_issue(tc, m->bio, m->data_block);
883 }
Joe Thornber104655f2012-03-28 18:41:28 +0100884 else
885 bio_endio(m->bio, 0);
886
Joe Thornber104655f2012-03-28 18:41:28 +0100887 mempool_free(m, tc->pool->mapping_pool);
888}
889
Joe Thornbere49e5822012-07-27 15:08:16 +0100890static void process_prepared_discard(struct dm_thin_new_mapping *m)
891{
892 int r;
893 struct thin_c *tc = m->tc;
894
895 r = dm_thin_remove_block(tc->td, m->virt_block);
896 if (r)
Mike Snitzerc3977412012-12-21 20:23:34 +0000897 DMERR_LIMIT("dm_thin_remove_block() failed");
Joe Thornbere49e5822012-07-27 15:08:16 +0100898
899 process_prepared_discard_passdown(m);
900}
901
Joe Thornber104655f2012-03-28 18:41:28 +0100902static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +0100903 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000904{
905 unsigned long flags;
906 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +0100907 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000908
909 INIT_LIST_HEAD(&maps);
910 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100911 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000912 spin_unlock_irqrestore(&pool->lock, flags);
913
914 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +0100915 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000916}
917
918/*
919 * Deferred bio jobs.
920 */
Joe Thornber104655f2012-03-28 18:41:28 +0100921static int io_overlaps_block(struct pool *pool, struct bio *bio)
922{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700923 return bio->bi_iter.bi_size ==
924 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +0100925}
926
Joe Thornber991d9fa2011-10-31 20:21:18 +0000927static int io_overwrites_block(struct pool *pool, struct bio *bio)
928{
Joe Thornber104655f2012-03-28 18:41:28 +0100929 return (bio_data_dir(bio) == WRITE) &&
930 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000931}
932
933static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
934 bio_end_io_t *fn)
935{
936 *save = bio->bi_end_io;
937 bio->bi_end_io = fn;
938}
939
940static int ensure_next_mapping(struct pool *pool)
941{
942 if (pool->next_mapping)
943 return 0;
944
945 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
946
947 return pool->next_mapping ? 0 : -ENOMEM;
948}
949
Mike Snitzera24c2562012-06-03 00:30:00 +0100950static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000951{
Mike Snitzer16961b02013-12-17 13:19:11 -0500952 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000953
954 BUG_ON(!pool->next_mapping);
955
Mike Snitzer16961b02013-12-17 13:19:11 -0500956 memset(m, 0, sizeof(struct dm_thin_new_mapping));
957 INIT_LIST_HEAD(&m->list);
958 m->bio = NULL;
959
Joe Thornber991d9fa2011-10-31 20:21:18 +0000960 pool->next_mapping = NULL;
961
Mike Snitzer16961b02013-12-17 13:19:11 -0500962 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000963}
964
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100965static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
966 sector_t begin, sector_t end)
967{
968 int r;
969 struct dm_io_region to;
970
971 to.bdev = tc->pool_dev->bdev;
972 to.sector = begin;
973 to.count = end - begin;
974
975 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
976 if (r < 0) {
977 DMERR_LIMIT("dm_kcopyd_zero() failed");
978 copy_complete(1, 1, m);
979 }
980}
981
Mike Snitzer452d7a62014-10-09 19:20:21 -0400982static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
983 dm_block_t data_block,
984 struct dm_thin_new_mapping *m)
985{
986 struct pool *pool = tc->pool;
987 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
988
989 h->overwrite_mapping = m;
990 m->bio = bio;
991 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
992 inc_all_io_entry(pool, bio);
993 remap_and_issue(tc, bio, data_block);
994}
995
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100996/*
997 * A partial copy also needs to zero the uncopied region.
998 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000999static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +01001000 struct dm_dev *origin, dm_block_t data_origin,
1001 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001002 struct dm_bio_prison_cell *cell, struct bio *bio,
1003 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001004{
1005 int r;
1006 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001007 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001008
Joe Thornber991d9fa2011-10-31 20:21:18 +00001009 m->tc = tc;
1010 m->virt_block = virt_block;
1011 m->data_block = data_dest;
1012 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001013
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001014 /*
1015 * quiesce action + copy action + an extra reference held for the
1016 * duration of this function (we may need to inc later for a
1017 * partial zero).
1018 */
1019 atomic_set(&m->prepare_actions, 3);
1020
Mike Snitzer44feb382012-10-12 21:02:10 +01001021 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001022 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001023
1024 /*
1025 * IO to pool_dev remaps to the pool target's data_dev.
1026 *
1027 * If the whole block of data is being overwritten, we can issue the
1028 * bio immediately. Otherwise we use kcopyd to clone the data first.
1029 */
Mike Snitzer452d7a62014-10-09 19:20:21 -04001030 if (io_overwrites_block(pool, bio))
1031 remap_and_issue_overwrite(tc, bio, data_dest, m);
1032 else {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001033 struct dm_io_region from, to;
1034
Joe Thornber2dd9c252012-03-28 18:41:28 +01001035 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001036 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001037 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001038
1039 to.bdev = tc->pool_dev->bdev;
1040 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001041 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001042
1043 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1044 0, copy_complete, m);
1045 if (r < 0) {
Mike Snitzerc3977412012-12-21 20:23:34 +00001046 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001047 copy_complete(1, 1, m);
1048
1049 /*
1050 * We allow the zero to be issued, to simplify the
1051 * error path. Otherwise we'd need to start
1052 * worrying about decrementing the prepare_actions
1053 * counter.
1054 */
1055 }
1056
1057 /*
1058 * Do we need to zero a tail region?
1059 */
1060 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1061 atomic_inc(&m->prepare_actions);
1062 ll_zero(tc, m,
1063 data_dest * pool->sectors_per_block + len,
1064 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001065 }
1066 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001067
1068 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001069}
1070
Joe Thornber2dd9c252012-03-28 18:41:28 +01001071static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1072 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +01001073 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +01001074{
1075 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001076 data_origin, data_dest, cell, bio,
1077 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001078}
1079
Joe Thornber991d9fa2011-10-31 20:21:18 +00001080static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001081 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001082 struct bio *bio)
1083{
1084 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001085 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001086
Joe Thornber50f3c3e2014-06-13 13:57:09 +01001087 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001088 m->tc = tc;
1089 m->virt_block = virt_block;
1090 m->data_block = data_block;
1091 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001092
1093 /*
1094 * If the whole block of data is being overwritten or we are not
1095 * zeroing pre-existing data, we can issue the bio immediately.
1096 * Otherwise we use kcopyd to zero the data first.
1097 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001098 if (!pool->pf.zero_new_blocks)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001099 process_prepared_mapping(m);
1100
Mike Snitzer452d7a62014-10-09 19:20:21 -04001101 else if (io_overwrites_block(pool, bio))
1102 remap_and_issue_overwrite(tc, bio, data_block, m);
Mike Snitzera24c2562012-06-03 00:30:00 +01001103
Mike Snitzer452d7a62014-10-09 19:20:21 -04001104 else
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001105 ll_zero(tc, m,
1106 data_block * pool->sectors_per_block,
1107 (data_block + 1) * pool->sectors_per_block);
1108}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001109
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001110static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1111 dm_block_t data_dest,
1112 struct dm_bio_prison_cell *cell, struct bio *bio)
1113{
1114 struct pool *pool = tc->pool;
1115 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1116 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1117
1118 if (virt_block_end <= tc->origin_size)
1119 schedule_copy(tc, virt_block, tc->origin_dev,
1120 virt_block, data_dest, cell, bio,
1121 pool->sectors_per_block);
1122
1123 else if (virt_block_begin < tc->origin_size)
1124 schedule_copy(tc, virt_block, tc->origin_dev,
1125 virt_block, data_dest, cell, bio,
1126 tc->origin_size - virt_block_begin);
1127
1128 else
1129 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001130}
1131
Joe Thornber2c43fd22014-12-11 11:12:19 +00001132static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1133
1134static void check_for_space(struct pool *pool)
1135{
1136 int r;
1137 dm_block_t nr_free;
1138
1139 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1140 return;
1141
1142 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1143 if (r)
1144 return;
1145
1146 if (nr_free)
1147 set_pool_mode(pool, PM_WRITE);
1148}
1149
Joe Thornbere49e5822012-07-27 15:08:16 +01001150/*
1151 * A non-zero return indicates read_only or fail_io mode.
1152 * Many callers don't care about the return value.
1153 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001154static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001155{
1156 int r;
1157
Joe Thornber8d07e8a2014-05-06 16:28:14 +01001158 if (get_pool_mode(pool) >= PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01001159 return -EINVAL;
1160
Joe Thornber020cc3b2013-12-04 15:05:36 -05001161 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001162 if (r)
1163 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornber2c43fd22014-12-11 11:12:19 +00001164 else
1165 check_for_space(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001166
1167 return r;
1168}
1169
Joe Thornber88a66212013-12-04 20:16:12 -05001170static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1171{
1172 unsigned long flags;
1173
1174 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1175 DMWARN("%s: reached low water mark for data device: sending event.",
1176 dm_device_name(pool->pool_md));
1177 spin_lock_irqsave(&pool->lock, flags);
1178 pool->low_water_triggered = true;
1179 spin_unlock_irqrestore(&pool->lock, flags);
1180 dm_table_event(pool->ti->table);
1181 }
1182}
1183
Joe Thornber991d9fa2011-10-31 20:21:18 +00001184static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1185{
1186 int r;
1187 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001188 struct pool *pool = tc->pool;
1189
Joe Thornber3e1a0692014-03-03 16:03:26 +00001190 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001191 return -EINVAL;
1192
Joe Thornber991d9fa2011-10-31 20:21:18 +00001193 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001194 if (r) {
1195 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001196 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001197 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001198
Joe Thornber88a66212013-12-04 20:16:12 -05001199 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001200
1201 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001202 /*
1203 * Try to commit to see if that will free up some
1204 * more space.
1205 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001206 r = commit(pool);
1207 if (r)
1208 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001209
1210 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001211 if (r) {
1212 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001213 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001214 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001215
Mike Snitzer94563ba2013-08-22 09:56:18 -04001216 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001217 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001218 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001219 }
1220 }
1221
1222 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001223 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05001224 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001225 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001226 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001227
1228 return 0;
1229}
1230
1231/*
1232 * If we have run out of space, queue bios until the device is
1233 * resumed, presumably after having been reloaded with more space.
1234 */
1235static void retry_on_resume(struct bio *bio)
1236{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001237 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001238 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001239 unsigned long flags;
1240
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001241 spin_lock_irqsave(&tc->lock, flags);
1242 bio_list_add(&tc->retry_on_resume_list, bio);
1243 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001244}
1245
Mike Snitzeraf918052014-05-22 14:32:51 -04001246static int should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001247{
1248 enum pool_mode m = get_pool_mode(pool);
1249
1250 switch (m) {
1251 case PM_WRITE:
1252 /* Shouldn't get here */
1253 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001254 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001255
1256 case PM_OUT_OF_DATA_SPACE:
Mike Snitzeraf918052014-05-22 14:32:51 -04001257 return pool->pf.error_if_no_space ? -ENOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001258
1259 case PM_READ_ONLY:
1260 case PM_FAIL:
Mike Snitzeraf918052014-05-22 14:32:51 -04001261 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001262 default:
1263 /* Shouldn't get here */
1264 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001265 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001266 }
1267}
1268
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001269static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1270{
Mike Snitzeraf918052014-05-22 14:32:51 -04001271 int error = should_error_unserviceable_bio(pool);
1272
1273 if (error)
1274 bio_endio(bio, error);
Mike Snitzer6d162022013-12-20 18:09:02 -05001275 else
1276 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001277}
1278
Mike Snitzer399cadd2013-12-05 16:03:33 -05001279static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001280{
1281 struct bio *bio;
1282 struct bio_list bios;
Mike Snitzeraf918052014-05-22 14:32:51 -04001283 int error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001284
Mike Snitzeraf918052014-05-22 14:32:51 -04001285 error = should_error_unserviceable_bio(pool);
1286 if (error) {
1287 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001288 return;
1289 }
1290
Joe Thornber991d9fa2011-10-31 20:21:18 +00001291 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001292 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001293
Mike Snitzer9d094ee2014-10-19 08:23:09 -04001294 while ((bio = bio_list_pop(&bios)))
1295 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001296}
1297
Joe Thornbera374bb22014-10-10 13:43:14 +01001298static void process_discard_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber104655f2012-03-28 18:41:28 +01001299{
1300 int r;
Joe Thornbera374bb22014-10-10 13:43:14 +01001301 struct bio *bio = cell->holder;
Joe Thornber104655f2012-03-28 18:41:28 +01001302 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001303 struct dm_bio_prison_cell *cell2;
1304 struct dm_cell_key key2;
Joe Thornber104655f2012-03-28 18:41:28 +01001305 dm_block_t block = get_bio_block(tc, bio);
1306 struct dm_thin_lookup_result lookup_result;
Mike Snitzera24c2562012-06-03 00:30:00 +01001307 struct dm_thin_new_mapping *m;
Joe Thornber104655f2012-03-28 18:41:28 +01001308
Joe Thornbera374bb22014-10-10 13:43:14 +01001309 if (tc->requeue_mode) {
1310 cell_requeue(pool, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001311 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001312 }
Joe Thornber104655f2012-03-28 18:41:28 +01001313
1314 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1315 switch (r) {
1316 case 0:
1317 /*
1318 * Check nobody is fiddling with this pool block. This can
1319 * happen if someone's in the process of breaking sharing
1320 * on this block.
1321 */
1322 build_data_key(tc->td, lookup_result.block, &key2);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001323 if (bio_detain(tc->pool, &key2, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +00001324 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001325 break;
1326 }
1327
1328 if (io_overlaps_block(pool, bio)) {
1329 /*
1330 * IO may still be going to the destination block. We must
1331 * quiesce before we can do the removal.
1332 */
1333 m = get_next_mapping(pool);
1334 m->tc = tc;
Joe Thornber19fa1a62013-12-17 12:09:40 -05001335 m->pass_discard = pool->pf.discard_passdown;
1336 m->definitely_not_shared = !lookup_result.shared;
Joe Thornber104655f2012-03-28 18:41:28 +01001337 m->virt_block = block;
1338 m->data_block = lookup_result.block;
1339 m->cell = cell;
1340 m->cell2 = cell2;
Joe Thornber104655f2012-03-28 18:41:28 +01001341 m->bio = bio;
1342
Joe Thornber7a7e97c2014-09-12 11:34:01 +01001343 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1344 pool->process_prepared_discard(m);
1345
Joe Thornber104655f2012-03-28 18:41:28 +01001346 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001347 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001348 cell_defer_no_holder(tc, cell);
1349 cell_defer_no_holder(tc, cell2);
Joe Thornbere8088072012-12-21 20:23:31 +00001350
Joe Thornber104655f2012-03-28 18:41:28 +01001351 /*
Mikulas Patocka49296302012-07-27 15:08:03 +01001352 * The DM core makes sure that the discard doesn't span
1353 * a block boundary. So we submit the discard of a
1354 * partial block appropriately.
Joe Thornber104655f2012-03-28 18:41:28 +01001355 */
Mikulas Patocka650d2a02012-07-20 14:25:05 +01001356 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1357 remap_and_issue(tc, bio, lookup_result.block);
1358 else
1359 bio_endio(bio, 0);
Joe Thornber104655f2012-03-28 18:41:28 +01001360 }
1361 break;
1362
1363 case -ENODATA:
1364 /*
1365 * It isn't provisioned, just forget it.
1366 */
Joe Thornberf286ba02012-12-21 20:23:33 +00001367 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001368 bio_endio(bio, 0);
1369 break;
1370
1371 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001372 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1373 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001374 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001375 bio_io_error(bio);
1376 break;
1377 }
1378}
1379
Joe Thornbera374bb22014-10-10 13:43:14 +01001380static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1381{
1382 struct dm_bio_prison_cell *cell;
1383 struct dm_cell_key key;
1384 dm_block_t block = get_bio_block(tc, bio);
1385
1386 build_virtual_key(tc->td, block, &key);
1387 if (bio_detain(tc->pool, &key, bio, &cell))
1388 return;
1389
1390 process_discard_cell(tc, cell);
1391}
1392
Joe Thornber991d9fa2011-10-31 20:21:18 +00001393static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001394 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001395 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001396 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001397{
1398 int r;
1399 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001400 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001401
1402 r = alloc_data_block(tc, &data_block);
1403 switch (r) {
1404 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001405 schedule_internal_copy(tc, block, lookup_result->block,
1406 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001407 break;
1408
1409 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001410 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001411 break;
1412
1413 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001414 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1415 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001416 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001417 break;
1418 }
1419}
1420
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001421static void __remap_and_issue_shared_cell(void *context,
1422 struct dm_bio_prison_cell *cell)
1423{
1424 struct remap_info *info = context;
1425 struct bio *bio;
1426
1427 while ((bio = bio_list_pop(&cell->bios))) {
1428 if ((bio_data_dir(bio) == WRITE) ||
1429 (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)))
1430 bio_list_add(&info->defer_bios, bio);
1431 else {
1432 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
1433
1434 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1435 inc_all_io_entry(info->tc->pool, bio);
1436 bio_list_add(&info->issue_bios, bio);
1437 }
1438 }
1439}
1440
1441static void remap_and_issue_shared_cell(struct thin_c *tc,
1442 struct dm_bio_prison_cell *cell,
1443 dm_block_t block)
1444{
1445 struct bio *bio;
1446 struct remap_info info;
1447
1448 info.tc = tc;
1449 bio_list_init(&info.defer_bios);
1450 bio_list_init(&info.issue_bios);
1451
1452 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1453 &info, cell);
1454
1455 while ((bio = bio_list_pop(&info.defer_bios)))
1456 thin_defer_bio(tc, bio);
1457
1458 while ((bio = bio_list_pop(&info.issue_bios)))
1459 remap_and_issue(tc, bio, block);
1460}
1461
Joe Thornber991d9fa2011-10-31 20:21:18 +00001462static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1463 dm_block_t block,
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001464 struct dm_thin_lookup_result *lookup_result,
1465 struct dm_bio_prison_cell *virt_cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001466{
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001467 struct dm_bio_prison_cell *data_cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001468 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001469 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001470
1471 /*
1472 * If cell is already occupied, then sharing is already in the process
1473 * of being broken so we have nothing further to do here.
1474 */
1475 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001476 if (bio_detain(pool, &key, bio, &data_cell)) {
1477 cell_defer_no_holder(tc, virt_cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001478 return;
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001479 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001480
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001481 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1482 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1483 cell_defer_no_holder(tc, virt_cell);
1484 } else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001485 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001486
Mike Snitzer44feb382012-10-12 21:02:10 +01001487 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001488 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001489 remap_and_issue(tc, bio, lookup_result->block);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001490
1491 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1492 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001493 }
1494}
1495
1496static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001497 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001498{
1499 int r;
1500 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001501 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001502
1503 /*
1504 * Remap empty bios (flushes) immediately, without provisioning.
1505 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001506 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001507 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001508 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001509
Joe Thornber991d9fa2011-10-31 20:21:18 +00001510 remap_and_issue(tc, bio, 0);
1511 return;
1512 }
1513
1514 /*
1515 * Fill read bios with zeroes and complete them immediately.
1516 */
1517 if (bio_data_dir(bio) == READ) {
1518 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001519 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001520 bio_endio(bio, 0);
1521 return;
1522 }
1523
1524 r = alloc_data_block(tc, &data_block);
1525 switch (r) {
1526 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001527 if (tc->origin_dev)
1528 schedule_external_copy(tc, block, data_block, cell, bio);
1529 else
1530 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001531 break;
1532
1533 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001534 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001535 break;
1536
1537 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001538 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1539 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001540 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001541 break;
1542 }
1543}
1544
Joe Thornbera374bb22014-10-10 13:43:14 +01001545static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001546{
1547 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001548 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001549 struct bio *bio = cell->holder;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001550 dm_block_t block = get_bio_block(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001551 struct dm_thin_lookup_result lookup_result;
1552
Joe Thornbera374bb22014-10-10 13:43:14 +01001553 if (tc->requeue_mode) {
1554 cell_requeue(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001555 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001556 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001557
1558 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1559 switch (r) {
1560 case 0:
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001561 if (lookup_result.shared)
1562 process_shared_bio(tc, bio, block, &lookup_result, cell);
1563 else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001564 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001565 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001566 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001567 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001568 break;
1569
1570 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001571 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001572 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001573 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001574
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001575 if (bio_end_sector(bio) <= tc->origin_size)
1576 remap_to_origin_and_issue(tc, bio);
1577
1578 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1579 zero_fill_bio(bio);
1580 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1581 remap_to_origin_and_issue(tc, bio);
1582
1583 } else {
1584 zero_fill_bio(bio);
1585 bio_endio(bio, 0);
1586 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001587 } else
1588 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001589 break;
1590
1591 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001592 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1593 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001594 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001595 bio_io_error(bio);
1596 break;
1597 }
1598}
1599
Joe Thornbera374bb22014-10-10 13:43:14 +01001600static void process_bio(struct thin_c *tc, struct bio *bio)
1601{
1602 struct pool *pool = tc->pool;
1603 dm_block_t block = get_bio_block(tc, bio);
1604 struct dm_bio_prison_cell *cell;
1605 struct dm_cell_key key;
1606
1607 /*
1608 * If cell is already occupied, then the block is already
1609 * being provisioned so we have nothing further to do here.
1610 */
1611 build_virtual_key(tc->td, block, &key);
1612 if (bio_detain(pool, &key, bio, &cell))
1613 return;
1614
1615 process_cell(tc, cell);
1616}
1617
1618static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1619 struct dm_bio_prison_cell *cell)
Joe Thornbere49e5822012-07-27 15:08:16 +01001620{
1621 int r;
1622 int rw = bio_data_dir(bio);
1623 dm_block_t block = get_bio_block(tc, bio);
1624 struct dm_thin_lookup_result lookup_result;
1625
1626 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1627 switch (r) {
1628 case 0:
Joe Thornbera374bb22014-10-10 13:43:14 +01001629 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001630 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01001631 if (cell)
1632 cell_defer_no_holder(tc, cell);
1633 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001634 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001635 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001636 if (cell)
1637 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001638 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001639 break;
1640
1641 case -ENODATA:
Joe Thornbera374bb22014-10-10 13:43:14 +01001642 if (cell)
1643 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001644 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001645 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001646 break;
1647 }
1648
1649 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001650 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001651 remap_to_origin_and_issue(tc, bio);
1652 break;
1653 }
1654
1655 zero_fill_bio(bio);
1656 bio_endio(bio, 0);
1657 break;
1658
1659 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001660 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1661 __func__, r);
Joe Thornbera374bb22014-10-10 13:43:14 +01001662 if (cell)
1663 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001664 bio_io_error(bio);
1665 break;
1666 }
1667}
1668
Joe Thornbera374bb22014-10-10 13:43:14 +01001669static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1670{
1671 __process_bio_read_only(tc, bio, NULL);
1672}
1673
1674static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1675{
1676 __process_bio_read_only(tc, cell->holder, cell);
1677}
1678
Joe Thornber3e1a0692014-03-03 16:03:26 +00001679static void process_bio_success(struct thin_c *tc, struct bio *bio)
1680{
1681 bio_endio(bio, 0);
1682}
1683
Joe Thornbere49e5822012-07-27 15:08:16 +01001684static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1685{
1686 bio_io_error(bio);
1687}
1688
Joe Thornbera374bb22014-10-10 13:43:14 +01001689static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1690{
1691 cell_success(tc->pool, cell);
1692}
1693
1694static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1695{
1696 cell_error(tc->pool, cell);
1697}
1698
Joe Thornberac8c3f32013-05-10 14:37:21 +01001699/*
1700 * FIXME: should we also commit due to size of transaction, measured in
1701 * metadata blocks?
1702 */
Joe Thornber905e51b2012-03-28 18:41:27 +01001703static int need_commit_due_to_time(struct pool *pool)
1704{
Manuel Schölling0f30af92014-05-22 22:42:37 +02001705 return !time_in_range(jiffies, pool->last_commit_jiffies,
1706 pool->last_commit_jiffies + COMMIT_PERIOD);
Joe Thornber905e51b2012-03-28 18:41:27 +01001707}
1708
Mike Snitzer67324ea2014-03-21 18:33:41 -04001709#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1710#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1711
1712static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1713{
1714 struct rb_node **rbp, *parent;
1715 struct dm_thin_endio_hook *pbd;
1716 sector_t bi_sector = bio->bi_iter.bi_sector;
1717
1718 rbp = &tc->sort_bio_list.rb_node;
1719 parent = NULL;
1720 while (*rbp) {
1721 parent = *rbp;
1722 pbd = thin_pbd(parent);
1723
1724 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1725 rbp = &(*rbp)->rb_left;
1726 else
1727 rbp = &(*rbp)->rb_right;
1728 }
1729
1730 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1731 rb_link_node(&pbd->rb_node, parent, rbp);
1732 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
1733}
1734
1735static void __extract_sorted_bios(struct thin_c *tc)
1736{
1737 struct rb_node *node;
1738 struct dm_thin_endio_hook *pbd;
1739 struct bio *bio;
1740
1741 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
1742 pbd = thin_pbd(node);
1743 bio = thin_bio(pbd);
1744
1745 bio_list_add(&tc->deferred_bio_list, bio);
1746 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
1747 }
1748
1749 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
1750}
1751
1752static void __sort_thin_deferred_bios(struct thin_c *tc)
1753{
1754 struct bio *bio;
1755 struct bio_list bios;
1756
1757 bio_list_init(&bios);
1758 bio_list_merge(&bios, &tc->deferred_bio_list);
1759 bio_list_init(&tc->deferred_bio_list);
1760
1761 /* Sort deferred_bio_list using rb-tree */
1762 while ((bio = bio_list_pop(&bios)))
1763 __thin_bio_rb_add(tc, bio);
1764
1765 /*
1766 * Transfer the sorted bios in sort_bio_list back to
1767 * deferred_bio_list to allow lockless submission of
1768 * all bios.
1769 */
1770 __extract_sorted_bios(tc);
1771}
1772
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001773static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001774{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001775 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001776 unsigned long flags;
1777 struct bio *bio;
1778 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04001779 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001780 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001781
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001782 if (tc->requeue_mode) {
Mike Snitzer42d6a8c2014-10-19 07:52:44 -04001783 error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001784 return;
1785 }
1786
Joe Thornber991d9fa2011-10-31 20:21:18 +00001787 bio_list_init(&bios);
1788
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001789 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001790
1791 if (bio_list_empty(&tc->deferred_bio_list)) {
1792 spin_unlock_irqrestore(&tc->lock, flags);
1793 return;
1794 }
1795
1796 __sort_thin_deferred_bios(tc);
1797
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001798 bio_list_merge(&bios, &tc->deferred_bio_list);
1799 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001800
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001801 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001802
Mike Snitzer67324ea2014-03-21 18:33:41 -04001803 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001804 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001805 /*
1806 * If we've got no free new_mapping structs, and processing
1807 * this bio might require one, we pause until there are some
1808 * prepared mappings to process.
1809 */
1810 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001811 spin_lock_irqsave(&tc->lock, flags);
1812 bio_list_add(&tc->deferred_bio_list, bio);
1813 bio_list_merge(&tc->deferred_bio_list, &bios);
1814 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001815 break;
1816 }
Joe Thornber104655f2012-03-28 18:41:28 +01001817
1818 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01001819 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01001820 else
Joe Thornbere49e5822012-07-27 15:08:16 +01001821 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001822
1823 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01001824 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001825 dm_pool_issue_prefetches(pool->pmd);
1826 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001827 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04001828 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001829}
1830
Joe Thornberac4c3f32014-10-10 16:42:10 +01001831static int cmp_cells(const void *lhs, const void *rhs)
1832{
1833 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
1834 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
1835
1836 BUG_ON(!lhs_cell->holder);
1837 BUG_ON(!rhs_cell->holder);
1838
1839 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
1840 return -1;
1841
1842 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
1843 return 1;
1844
1845 return 0;
1846}
1847
1848static unsigned sort_cells(struct pool *pool, struct list_head *cells)
1849{
1850 unsigned count = 0;
1851 struct dm_bio_prison_cell *cell, *tmp;
1852
1853 list_for_each_entry_safe(cell, tmp, cells, user_list) {
1854 if (count >= CELL_SORT_ARRAY_SIZE)
1855 break;
1856
1857 pool->cell_sort_array[count++] = cell;
1858 list_del(&cell->user_list);
1859 }
1860
1861 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
1862
1863 return count;
1864}
1865
Joe Thornbera374bb22014-10-10 13:43:14 +01001866static void process_thin_deferred_cells(struct thin_c *tc)
1867{
1868 struct pool *pool = tc->pool;
1869 unsigned long flags;
1870 struct list_head cells;
Joe Thornberac4c3f32014-10-10 16:42:10 +01001871 struct dm_bio_prison_cell *cell;
1872 unsigned i, j, count;
Joe Thornbera374bb22014-10-10 13:43:14 +01001873
1874 INIT_LIST_HEAD(&cells);
1875
1876 spin_lock_irqsave(&tc->lock, flags);
1877 list_splice_init(&tc->deferred_cells, &cells);
1878 spin_unlock_irqrestore(&tc->lock, flags);
1879
1880 if (list_empty(&cells))
1881 return;
1882
Joe Thornberac4c3f32014-10-10 16:42:10 +01001883 do {
1884 count = sort_cells(tc->pool, &cells);
Joe Thornbera374bb22014-10-10 13:43:14 +01001885
Joe Thornberac4c3f32014-10-10 16:42:10 +01001886 for (i = 0; i < count; i++) {
1887 cell = pool->cell_sort_array[i];
1888 BUG_ON(!cell->holder);
1889
1890 /*
1891 * If we've got no free new_mapping structs, and processing
1892 * this bio might require one, we pause until there are some
1893 * prepared mappings to process.
1894 */
1895 if (ensure_next_mapping(pool)) {
1896 for (j = i; j < count; j++)
1897 list_add(&pool->cell_sort_array[j]->user_list, &cells);
1898
1899 spin_lock_irqsave(&tc->lock, flags);
1900 list_splice(&cells, &tc->deferred_cells);
1901 spin_unlock_irqrestore(&tc->lock, flags);
1902 return;
1903 }
1904
1905 if (cell->holder->bi_rw & REQ_DISCARD)
1906 pool->process_discard_cell(tc, cell);
1907 else
1908 pool->process_cell(tc, cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01001909 }
Joe Thornberac4c3f32014-10-10 16:42:10 +01001910 } while (!list_empty(&cells));
Joe Thornbera374bb22014-10-10 13:43:14 +01001911}
1912
Joe Thornberb10ebd32014-04-08 11:29:01 +01001913static void thin_get(struct thin_c *tc);
1914static void thin_put(struct thin_c *tc);
1915
1916/*
1917 * We can't hold rcu_read_lock() around code that can block. So we
1918 * find a thin with the rcu lock held; bump a refcount; then drop
1919 * the lock.
1920 */
1921static struct thin_c *get_first_thin(struct pool *pool)
1922{
1923 struct thin_c *tc = NULL;
1924
1925 rcu_read_lock();
1926 if (!list_empty(&pool->active_thins)) {
1927 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
1928 thin_get(tc);
1929 }
1930 rcu_read_unlock();
1931
1932 return tc;
1933}
1934
1935static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
1936{
1937 struct thin_c *old_tc = tc;
1938
1939 rcu_read_lock();
1940 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
1941 thin_get(tc);
1942 thin_put(old_tc);
1943 rcu_read_unlock();
1944 return tc;
1945 }
1946 thin_put(old_tc);
1947 rcu_read_unlock();
1948
1949 return NULL;
1950}
1951
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001952static void process_deferred_bios(struct pool *pool)
1953{
1954 unsigned long flags;
1955 struct bio *bio;
1956 struct bio_list bios;
1957 struct thin_c *tc;
1958
Joe Thornberb10ebd32014-04-08 11:29:01 +01001959 tc = get_first_thin(pool);
1960 while (tc) {
Joe Thornbera374bb22014-10-10 13:43:14 +01001961 process_thin_deferred_cells(tc);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001962 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01001963 tc = get_next_thin(pool, tc);
1964 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001965
1966 /*
1967 * If there are any deferred flush bios, we must commit
1968 * the metadata before issuing them.
1969 */
1970 bio_list_init(&bios);
1971 spin_lock_irqsave(&pool->lock, flags);
1972 bio_list_merge(&bios, &pool->deferred_flush_bios);
1973 bio_list_init(&pool->deferred_flush_bios);
1974 spin_unlock_irqrestore(&pool->lock, flags);
1975
Mike Snitzer4d1662a2014-02-06 06:08:56 -05001976 if (bio_list_empty(&bios) &&
1977 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001978 return;
1979
Joe Thornber020cc3b2013-12-04 15:05:36 -05001980 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001981 while ((bio = bio_list_pop(&bios)))
1982 bio_io_error(bio);
1983 return;
1984 }
Joe Thornber905e51b2012-03-28 18:41:27 +01001985 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001986
1987 while ((bio = bio_list_pop(&bios)))
1988 generic_make_request(bio);
1989}
1990
1991static void do_worker(struct work_struct *ws)
1992{
1993 struct pool *pool = container_of(ws, struct pool, worker);
1994
Joe Thornber7d327fe2014-10-06 15:45:59 +01001995 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001996 dm_pool_issue_prefetches(pool->pmd);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001997 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01001998 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001999 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002000 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002001 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002002 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002003 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002004}
2005
Joe Thornber905e51b2012-03-28 18:41:27 +01002006/*
2007 * We want to commit periodically so that not too much
2008 * unwritten data builds up.
2009 */
2010static void do_waker(struct work_struct *ws)
2011{
2012 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2013 wake_worker(pool);
2014 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2015}
2016
Joe Thornber85ad6432014-05-09 15:59:38 +01002017/*
2018 * We're holding onto IO to allow userland time to react. After the
2019 * timeout either the pool will have been resized (and thus back in
2020 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
2021 */
2022static void do_no_space_timeout(struct work_struct *ws)
2023{
2024 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2025 no_space_timeout);
2026
2027 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
2028 set_pool_mode(pool, PM_READ_ONLY);
2029}
2030
Joe Thornber991d9fa2011-10-31 20:21:18 +00002031/*----------------------------------------------------------------*/
2032
Joe Thornbere7a3e872014-05-13 16:14:14 -04002033struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00002034 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002035 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00002036};
2037
Joe Thornbere7a3e872014-05-13 16:14:14 -04002038static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00002039{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002040 return container_of(ws, struct pool_work, worker);
2041}
2042
2043static void pool_work_complete(struct pool_work *pw)
2044{
2045 complete(&pw->complete);
2046}
2047
2048static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2049 void (*fn)(struct work_struct *))
2050{
2051 INIT_WORK_ONSTACK(&pw->worker, fn);
2052 init_completion(&pw->complete);
2053 queue_work(pool->wq, &pw->worker);
2054 wait_for_completion(&pw->complete);
2055}
2056
2057/*----------------------------------------------------------------*/
2058
2059struct noflush_work {
2060 struct pool_work pw;
2061 struct thin_c *tc;
2062};
2063
2064static struct noflush_work *to_noflush(struct work_struct *ws)
2065{
2066 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002067}
2068
2069static void do_noflush_start(struct work_struct *ws)
2070{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002071 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002072 w->tc->requeue_mode = true;
2073 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04002074 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002075}
2076
2077static void do_noflush_stop(struct work_struct *ws)
2078{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002079 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002080 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002081 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002082}
2083
2084static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2085{
2086 struct noflush_work w;
2087
Joe Thornber738211f2014-03-03 15:52:28 +00002088 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002089 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00002090}
2091
2092/*----------------------------------------------------------------*/
2093
Joe Thornbere49e5822012-07-27 15:08:16 +01002094static enum pool_mode get_pool_mode(struct pool *pool)
2095{
2096 return pool->pf.mode;
2097}
2098
Joe Thornber3e1a0692014-03-03 16:03:26 +00002099static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
2100{
2101 dm_table_event(pool->ti->table);
2102 DMINFO("%s: switching pool to %s mode",
2103 dm_device_name(pool->pool_md), new_mode);
2104}
2105
Mike Snitzer8b64e882013-12-20 14:27:28 -05002106static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01002107{
Mike Snitzercdc2b412014-02-14 18:10:55 -05002108 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002109 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2110 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer80c57892014-05-20 13:38:33 -04002111 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002112
2113 /*
2114 * Never allow the pool to transition to PM_WRITE mode if user
2115 * intervention is required to verify metadata and data consistency.
2116 */
2117 if (new_mode == PM_WRITE && needs_check) {
2118 DMERR("%s: unable to switch pool to write mode until repaired.",
2119 dm_device_name(pool->pool_md));
2120 if (old_mode != new_mode)
2121 new_mode = old_mode;
2122 else
2123 new_mode = PM_READ_ONLY;
2124 }
2125 /*
2126 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2127 * not going to recover without a thin_repair. So we never let the
2128 * pool move out of the old mode.
2129 */
2130 if (old_mode == PM_FAIL)
2131 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002132
Mike Snitzer8b64e882013-12-20 14:27:28 -05002133 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01002134 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002135 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002136 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05002137 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002138 pool->process_bio = process_bio_fail;
2139 pool->process_discard = process_bio_fail;
Joe Thornbera374bb22014-10-10 13:43:14 +01002140 pool->process_cell = process_cell_fail;
2141 pool->process_discard_cell = process_cell_fail;
Joe Thornbere49e5822012-07-27 15:08:16 +01002142 pool->process_prepared_mapping = process_prepared_mapping_fail;
2143 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002144
2145 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002146 break;
2147
2148 case PM_READ_ONLY:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002149 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002150 notify_of_pool_mode_change(pool, "read-only");
2151 dm_pool_metadata_read_only(pool->pmd);
2152 pool->process_bio = process_bio_read_only;
2153 pool->process_discard = process_bio_success;
Joe Thornbera374bb22014-10-10 13:43:14 +01002154 pool->process_cell = process_cell_read_only;
2155 pool->process_discard_cell = process_cell_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002156 pool->process_prepared_mapping = process_prepared_mapping_fail;
2157 pool->process_prepared_discard = process_prepared_discard_passdown;
2158
2159 error_retry_list(pool);
2160 break;
2161
2162 case PM_OUT_OF_DATA_SPACE:
2163 /*
2164 * Ideally we'd never hit this state; the low water mark
2165 * would trigger userland to extend the pool before we
2166 * completely run out of data space. However, many small
2167 * IOs to unprovisioned space can consume data space at an
2168 * alarming rate. Adjust your low water mark if you're
2169 * frequently seeing this mode.
2170 */
2171 if (old_mode != new_mode)
2172 notify_of_pool_mode_change(pool, "out-of-data-space");
2173 pool->process_bio = process_bio_read_only;
Joe Thornbera374bb22014-10-10 13:43:14 +01002174 pool->process_discard = process_discard_bio;
2175 pool->process_cell = process_cell_read_only;
2176 pool->process_discard_cell = process_discard_cell;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002177 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber45ec9bd2014-12-10 17:06:57 +00002178 pool->process_prepared_discard = process_prepared_discard;
Joe Thornber85ad6432014-05-09 15:59:38 +01002179
Mike Snitzer80c57892014-05-20 13:38:33 -04002180 if (!pool->pf.error_if_no_space && no_space_timeout)
2181 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01002182 break;
2183
2184 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002185 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002186 notify_of_pool_mode_change(pool, "write");
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002187 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002188 pool->process_bio = process_bio;
Joe Thornbera374bb22014-10-10 13:43:14 +01002189 pool->process_discard = process_discard_bio;
2190 pool->process_cell = process_cell;
2191 pool->process_discard_cell = process_discard_cell;
Joe Thornbere49e5822012-07-27 15:08:16 +01002192 pool->process_prepared_mapping = process_prepared_mapping;
2193 pool->process_prepared_discard = process_prepared_discard;
2194 break;
2195 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05002196
2197 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05002198 /*
2199 * The pool mode may have changed, sync it so bind_control_target()
2200 * doesn't cause an unexpected mode transition on resume.
2201 */
2202 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002203}
2204
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002205static void abort_transaction(struct pool *pool)
2206{
2207 const char *dev_name = dm_device_name(pool->pool_md);
2208
2209 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2210 if (dm_pool_abort_metadata(pool->pmd)) {
2211 DMERR("%s: failed to abort metadata transaction", dev_name);
2212 set_pool_mode(pool, PM_FAIL);
2213 }
2214
2215 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2216 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2217 set_pool_mode(pool, PM_FAIL);
2218 }
2219}
2220
Joe Thornberb5330652013-12-04 19:51:33 -05002221static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2222{
2223 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2224 dm_device_name(pool->pool_md), op, r);
2225
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002226 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05002227 set_pool_mode(pool, PM_READ_ONLY);
2228}
2229
Joe Thornbere49e5822012-07-27 15:08:16 +01002230/*----------------------------------------------------------------*/
2231
Joe Thornber991d9fa2011-10-31 20:21:18 +00002232/*
2233 * Mapping functions.
2234 */
2235
2236/*
2237 * Called only while mapping a thin bio to hand it over to the workqueue.
2238 */
2239static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2240{
2241 unsigned long flags;
2242 struct pool *pool = tc->pool;
2243
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002244 spin_lock_irqsave(&tc->lock, flags);
2245 bio_list_add(&tc->deferred_bio_list, bio);
2246 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002247
2248 wake_worker(pool);
2249}
2250
Joe Thornber7d327fe2014-10-06 15:45:59 +01002251static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2252{
2253 struct pool *pool = tc->pool;
2254
2255 throttle_lock(&pool->throttle);
2256 thin_defer_bio(tc, bio);
2257 throttle_unlock(&pool->throttle);
2258}
2259
Joe Thornbera374bb22014-10-10 13:43:14 +01002260static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2261{
2262 unsigned long flags;
2263 struct pool *pool = tc->pool;
2264
2265 throttle_lock(&pool->throttle);
2266 spin_lock_irqsave(&tc->lock, flags);
2267 list_add_tail(&cell->user_list, &tc->deferred_cells);
2268 spin_unlock_irqrestore(&tc->lock, flags);
2269 throttle_unlock(&pool->throttle);
2270
2271 wake_worker(pool);
2272}
2273
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002274static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002275{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002276 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002277
2278 h->tc = tc;
2279 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00002280 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002281 h->overwrite_mapping = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002282}
2283
Joe Thornber991d9fa2011-10-31 20:21:18 +00002284/*
2285 * Non-blocking function called from the thin target's map function.
2286 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002287static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002288{
2289 int r;
2290 struct thin_c *tc = ti->private;
2291 dm_block_t block = get_bio_block(tc, bio);
2292 struct dm_thin_device *td = tc->td;
2293 struct dm_thin_lookup_result result;
Joe Thornbera374bb22014-10-10 13:43:14 +01002294 struct dm_bio_prison_cell *virt_cell, *data_cell;
Joe Thornbere8088072012-12-21 20:23:31 +00002295 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002296
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002297 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002298
Joe Thornber738211f2014-03-03 15:52:28 +00002299 if (tc->requeue_mode) {
2300 bio_endio(bio, DM_ENDIO_REQUEUE);
2301 return DM_MAPIO_SUBMITTED;
2302 }
2303
Joe Thornbere49e5822012-07-27 15:08:16 +01002304 if (get_pool_mode(tc->pool) == PM_FAIL) {
2305 bio_io_error(bio);
2306 return DM_MAPIO_SUBMITTED;
2307 }
2308
Joe Thornber104655f2012-03-28 18:41:28 +01002309 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002310 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002311 return DM_MAPIO_SUBMITTED;
2312 }
2313
Joe Thornberc822ed962014-10-10 09:41:09 +01002314 /*
2315 * We must hold the virtual cell before doing the lookup, otherwise
2316 * there's a race with discard.
2317 */
2318 build_virtual_key(tc->td, block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002319 if (bio_detain(tc->pool, &key, bio, &virt_cell))
Joe Thornberc822ed962014-10-10 09:41:09 +01002320 return DM_MAPIO_SUBMITTED;
2321
Joe Thornber991d9fa2011-10-31 20:21:18 +00002322 r = dm_thin_find_block(td, block, 0, &result);
2323
2324 /*
2325 * Note that we defer readahead too.
2326 */
2327 switch (r) {
2328 case 0:
2329 if (unlikely(result.shared)) {
2330 /*
2331 * We have a race condition here between the
2332 * result.shared value returned by the lookup and
2333 * snapshot creation, which may cause new
2334 * sharing.
2335 *
2336 * To avoid this always quiesce the origin before
2337 * taking the snap. You want to do this anyway to
2338 * ensure a consistent application view
2339 * (i.e. lockfs).
2340 *
2341 * More distant ancestors are irrelevant. The
2342 * shared flag will be set in their case.
2343 */
Joe Thornbera374bb22014-10-10 13:43:14 +01002344 thin_defer_cell(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002345 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002346 }
Joe Thornbere8088072012-12-21 20:23:31 +00002347
Joe Thornbere8088072012-12-21 20:23:31 +00002348 build_data_key(tc->td, result.block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002349 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2350 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002351 return DM_MAPIO_SUBMITTED;
2352 }
2353
2354 inc_all_io_entry(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002355 cell_defer_no_holder(tc, data_cell);
2356 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002357
2358 remap(tc, bio, result.block);
2359 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002360
2361 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002362 case -EWOULDBLOCK:
Joe Thornbera374bb22014-10-10 13:43:14 +01002363 thin_defer_cell(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002364 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002365
2366 default:
2367 /*
2368 * Must always call bio_io_error on failure.
2369 * dm_thin_find_block can fail with -EINVAL if the
2370 * pool is switched to fail-io mode.
2371 */
2372 bio_io_error(bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002373 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002374 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002375 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002376}
2377
2378static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2379{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002380 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04002381 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002382
Mike Snitzer760fe672014-03-20 08:36:47 -04002383 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2384 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002385
Mike Snitzer760fe672014-03-20 08:36:47 -04002386 q = bdev_get_queue(pt->data_dev->bdev);
2387 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002388}
2389
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002390static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002391{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002392 unsigned long flags;
2393 struct thin_c *tc;
2394
2395 rcu_read_lock();
2396 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2397 spin_lock_irqsave(&tc->lock, flags);
2398 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2399 bio_list_init(&tc->retry_on_resume_list);
2400 spin_unlock_irqrestore(&tc->lock, flags);
2401 }
2402 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002403}
2404
2405/*----------------------------------------------------------------
2406 * Binding of control targets to a pool object
2407 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002408static bool data_dev_supports_discard(struct pool_c *pt)
2409{
2410 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2411
2412 return q && blk_queue_discard(q);
2413}
2414
Joe Thornber58051b92013-03-20 17:21:25 +00002415static bool is_factor(sector_t block_size, uint32_t n)
2416{
2417 return !sector_div(block_size, n);
2418}
2419
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002420/*
2421 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002422 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002423 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002424static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002425{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002426 struct pool *pool = pt->pool;
2427 struct block_device *data_bdev = pt->data_dev->bdev;
2428 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
2429 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
2430 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002431 char buf[BDEVNAME_SIZE];
2432
Mike Snitzer0424caa2012-09-26 23:45:47 +01002433 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002434 return;
2435
Mike Snitzer0424caa2012-09-26 23:45:47 +01002436 if (!data_dev_supports_discard(pt))
2437 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002438
Mike Snitzer0424caa2012-09-26 23:45:47 +01002439 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2440 reason = "max discard sectors smaller than a block";
2441
2442 else if (data_limits->discard_granularity > block_size)
2443 reason = "discard granularity larger than a block";
2444
Joe Thornber58051b92013-03-20 17:21:25 +00002445 else if (!is_factor(block_size, data_limits->discard_granularity))
Mike Snitzer0424caa2012-09-26 23:45:47 +01002446 reason = "discard granularity not a factor of block size";
2447
2448 if (reason) {
2449 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2450 pt->adjusted_pf.discard_passdown = false;
2451 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002452}
2453
Joe Thornber991d9fa2011-10-31 20:21:18 +00002454static int bind_control_target(struct pool *pool, struct dm_target *ti)
2455{
2456 struct pool_c *pt = ti->private;
2457
Joe Thornbere49e5822012-07-27 15:08:16 +01002458 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002459 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002460 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002461 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002462 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002463
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002464 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002465 * Don't change the pool's mode until set_pool_mode() below.
2466 * Otherwise the pool's process_* function pointers may
2467 * not match the desired pool mode.
2468 */
2469 pt->adjusted_pf.mode = old_mode;
2470
2471 pool->ti = ti;
2472 pool->pf = pt->adjusted_pf;
2473 pool->low_water_blocks = pt->low_water_blocks;
2474
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002475 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002476
Joe Thornber991d9fa2011-10-31 20:21:18 +00002477 return 0;
2478}
2479
2480static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2481{
2482 if (pool->ti == ti)
2483 pool->ti = NULL;
2484}
2485
2486/*----------------------------------------------------------------
2487 * Pool creation
2488 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002489/* Initialize pool features. */
2490static void pool_features_init(struct pool_features *pf)
2491{
Joe Thornbere49e5822012-07-27 15:08:16 +01002492 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002493 pf->zero_new_blocks = true;
2494 pf->discard_enabled = true;
2495 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002496 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002497}
2498
Joe Thornber991d9fa2011-10-31 20:21:18 +00002499static void __pool_destroy(struct pool *pool)
2500{
2501 __pool_table_remove(pool);
2502
Joe Thornberb2ce1402015-07-03 10:22:42 +01002503 vfree(pool->cell_sort_array);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002504 if (dm_pool_metadata_close(pool->pmd) < 0)
2505 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2506
Mike Snitzer44feb382012-10-12 21:02:10 +01002507 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002508 dm_kcopyd_client_destroy(pool->copier);
2509
2510 if (pool->wq)
2511 destroy_workqueue(pool->wq);
2512
2513 if (pool->next_mapping)
2514 mempool_free(pool->next_mapping, pool->mapping_pool);
2515 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01002516 dm_deferred_set_destroy(pool->shared_read_ds);
2517 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002518 kfree(pool);
2519}
2520
Mike Snitzera24c2562012-06-03 00:30:00 +01002521static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002522
Joe Thornber991d9fa2011-10-31 20:21:18 +00002523static struct pool *pool_create(struct mapped_device *pool_md,
2524 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002525 unsigned long block_size,
2526 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002527{
2528 int r;
2529 void *err_p;
2530 struct pool *pool;
2531 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002532 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002533
Joe Thornbere49e5822012-07-27 15:08:16 +01002534 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002535 if (IS_ERR(pmd)) {
2536 *error = "Error creating metadata object";
2537 return (struct pool *)pmd;
2538 }
2539
2540 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2541 if (!pool) {
2542 *error = "Error allocating memory for pool";
2543 err_p = ERR_PTR(-ENOMEM);
2544 goto bad_pool;
2545 }
2546
2547 pool->pmd = pmd;
2548 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002549 if (block_size & (block_size - 1))
2550 pool->sectors_per_block_shift = -1;
2551 else
2552 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002553 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002554 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002555 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002556 if (!pool->prison) {
2557 *error = "Error creating pool's bio prison";
2558 err_p = ERR_PTR(-ENOMEM);
2559 goto bad_prison;
2560 }
2561
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002562 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002563 if (IS_ERR(pool->copier)) {
2564 r = PTR_ERR(pool->copier);
2565 *error = "Error creating pool's kcopyd client";
2566 err_p = ERR_PTR(r);
2567 goto bad_kcopyd_client;
2568 }
2569
2570 /*
2571 * Create singlethreaded workqueue that will service all devices
2572 * that use this metadata.
2573 */
2574 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2575 if (!pool->wq) {
2576 *error = "Error creating pool's workqueue";
2577 err_p = ERR_PTR(-ENOMEM);
2578 goto bad_wq;
2579 }
2580
Joe Thornber7d327fe2014-10-06 15:45:59 +01002581 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002582 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002583 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad6432014-05-09 15:59:38 +01002584 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002585 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002586 bio_list_init(&pool->deferred_flush_bios);
2587 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002588 INIT_LIST_HEAD(&pool->prepared_discards);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002589 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002590 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05002591 pool->suspended = true;
Mike Snitzer44feb382012-10-12 21:02:10 +01002592
2593 pool->shared_read_ds = dm_deferred_set_create();
2594 if (!pool->shared_read_ds) {
2595 *error = "Error creating pool's shared read deferred set";
2596 err_p = ERR_PTR(-ENOMEM);
2597 goto bad_shared_read_ds;
2598 }
2599
2600 pool->all_io_ds = dm_deferred_set_create();
2601 if (!pool->all_io_ds) {
2602 *error = "Error creating pool's all io deferred set";
2603 err_p = ERR_PTR(-ENOMEM);
2604 goto bad_all_io_ds;
2605 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002606
2607 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002608 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2609 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002610 if (!pool->mapping_pool) {
2611 *error = "Error creating pool's mapping mempool";
2612 err_p = ERR_PTR(-ENOMEM);
2613 goto bad_mapping_pool;
2614 }
2615
Joe Thornberb2ce1402015-07-03 10:22:42 +01002616 pool->cell_sort_array = vmalloc(sizeof(*pool->cell_sort_array) * CELL_SORT_ARRAY_SIZE);
2617 if (!pool->cell_sort_array) {
2618 *error = "Error allocating cell sort array";
2619 err_p = ERR_PTR(-ENOMEM);
2620 goto bad_sort_array;
2621 }
2622
Joe Thornber991d9fa2011-10-31 20:21:18 +00002623 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01002624 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002625 pool->pool_md = pool_md;
2626 pool->md_dev = metadata_dev;
2627 __pool_table_insert(pool);
2628
2629 return pool;
2630
Joe Thornberb2ce1402015-07-03 10:22:42 +01002631bad_sort_array:
2632 mempool_destroy(pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002633bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01002634 dm_deferred_set_destroy(pool->all_io_ds);
2635bad_all_io_ds:
2636 dm_deferred_set_destroy(pool->shared_read_ds);
2637bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002638 destroy_workqueue(pool->wq);
2639bad_wq:
2640 dm_kcopyd_client_destroy(pool->copier);
2641bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01002642 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002643bad_prison:
2644 kfree(pool);
2645bad_pool:
2646 if (dm_pool_metadata_close(pmd))
2647 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2648
2649 return err_p;
2650}
2651
2652static void __pool_inc(struct pool *pool)
2653{
2654 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2655 pool->ref_count++;
2656}
2657
2658static void __pool_dec(struct pool *pool)
2659{
2660 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2661 BUG_ON(!pool->ref_count);
2662 if (!--pool->ref_count)
2663 __pool_destroy(pool);
2664}
2665
2666static struct pool *__pool_find(struct mapped_device *pool_md,
2667 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002668 unsigned long block_size, int read_only,
2669 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002670{
2671 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2672
2673 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002674 if (pool->pool_md != pool_md) {
2675 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002676 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002677 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002678 __pool_inc(pool);
2679
2680 } else {
2681 pool = __pool_table_lookup(pool_md);
2682 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002683 if (pool->md_dev != metadata_dev) {
2684 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002685 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002686 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002687 __pool_inc(pool);
2688
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002689 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01002690 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002691 *created = 1;
2692 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002693 }
2694
2695 return pool;
2696}
2697
2698/*----------------------------------------------------------------
2699 * Pool target methods
2700 *--------------------------------------------------------------*/
2701static void pool_dtr(struct dm_target *ti)
2702{
2703 struct pool_c *pt = ti->private;
2704
2705 mutex_lock(&dm_thin_pool_table.mutex);
2706
2707 unbind_control_target(pt->pool, ti);
2708 __pool_dec(pt->pool);
2709 dm_put_device(ti, pt->metadata_dev);
2710 dm_put_device(ti, pt->data_dev);
2711 kfree(pt);
2712
2713 mutex_unlock(&dm_thin_pool_table.mutex);
2714}
2715
Joe Thornber991d9fa2011-10-31 20:21:18 +00002716static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2717 struct dm_target *ti)
2718{
2719 int r;
2720 unsigned argc;
2721 const char *arg_name;
2722
2723 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05002724 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002725 };
2726
2727 /*
2728 * No feature arguments supplied.
2729 */
2730 if (!as->argc)
2731 return 0;
2732
2733 r = dm_read_arg_group(_args, as, &argc, &ti->error);
2734 if (r)
2735 return -EINVAL;
2736
2737 while (argc && !r) {
2738 arg_name = dm_shift_arg(as);
2739 argc--;
2740
Joe Thornbere49e5822012-07-27 15:08:16 +01002741 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002742 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002743
Joe Thornbere49e5822012-07-27 15:08:16 +01002744 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002745 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002746
2747 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002748 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002749
2750 else if (!strcasecmp(arg_name, "read_only"))
2751 pf->mode = PM_READ_ONLY;
2752
Mike Snitzer787a996c2013-12-06 16:21:43 -05002753 else if (!strcasecmp(arg_name, "error_if_no_space"))
2754 pf->error_if_no_space = true;
2755
Joe Thornbere49e5822012-07-27 15:08:16 +01002756 else {
2757 ti->error = "Unrecognised pool feature requested";
2758 r = -EINVAL;
2759 break;
2760 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002761 }
2762
2763 return r;
2764}
2765
Joe Thornberac8c3f32013-05-10 14:37:21 +01002766static void metadata_low_callback(void *context)
2767{
2768 struct pool *pool = context;
2769
2770 DMWARN("%s: reached low water mark for metadata device: sending event.",
2771 dm_device_name(pool->pool_md));
2772
2773 dm_table_event(pool->ti->table);
2774}
2775
Mike Snitzer7d489352014-02-12 23:58:15 -05002776static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01002777{
Mike Snitzer7d489352014-02-12 23:58:15 -05002778 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
2779}
2780
2781static void warn_if_metadata_device_too_big(struct block_device *bdev)
2782{
2783 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01002784 char buffer[BDEVNAME_SIZE];
2785
Mike Snitzer7d489352014-02-12 23:58:15 -05002786 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01002787 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2788 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05002789}
2790
2791static sector_t get_metadata_dev_size(struct block_device *bdev)
2792{
2793 sector_t metadata_dev_size = get_dev_size(bdev);
2794
2795 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
2796 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01002797
2798 return metadata_dev_size;
2799}
2800
Joe Thornber24347e92013-05-10 14:37:19 +01002801static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
2802{
2803 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
2804
Mike Snitzer7d489352014-02-12 23:58:15 -05002805 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01002806
2807 return metadata_dev_size;
2808}
2809
Joe Thornber991d9fa2011-10-31 20:21:18 +00002810/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01002811 * When a metadata threshold is crossed a dm event is triggered, and
2812 * userland should respond by growing the metadata device. We could let
2813 * userland set the threshold, like we do with the data threshold, but I'm
2814 * not sure they know enough to do this well.
2815 */
2816static dm_block_t calc_metadata_threshold(struct pool_c *pt)
2817{
2818 /*
2819 * 4M is ample for all ops with the possible exception of thin
2820 * device deletion which is harmless if it fails (just retry the
2821 * delete after you've grown the device).
2822 */
2823 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
2824 return min((dm_block_t)1024ULL /* 4M */, quarter);
2825}
2826
2827/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00002828 * thin-pool <metadata dev> <data dev>
2829 * <data block size (sectors)>
2830 * <low water mark (blocks)>
2831 * [<#feature args> [<arg>]*]
2832 *
2833 * Optional feature arguments are:
2834 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002835 * ignore_discard: disable discard
2836 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05002837 * read_only: Don't allow any changes to be made to the pool metadata.
2838 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002839 */
2840static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
2841{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002842 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002843 struct pool_c *pt;
2844 struct pool *pool;
2845 struct pool_features pf;
2846 struct dm_arg_set as;
2847 struct dm_dev *data_dev;
2848 unsigned long block_size;
2849 dm_block_t low_water_blocks;
2850 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01002851 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002852
2853 /*
2854 * FIXME Remove validation from scope of lock.
2855 */
2856 mutex_lock(&dm_thin_pool_table.mutex);
2857
2858 if (argc < 4) {
2859 ti->error = "Invalid argument count";
2860 r = -EINVAL;
2861 goto out_unlock;
2862 }
Joe Thornber5d0db962013-05-10 14:37:19 +01002863
Joe Thornber991d9fa2011-10-31 20:21:18 +00002864 as.argc = argc;
2865 as.argv = argv;
2866
Joe Thornber5d0db962013-05-10 14:37:19 +01002867 /*
2868 * Set default pool features.
2869 */
2870 pool_features_init(&pf);
2871
2872 dm_consume_args(&as, 4);
2873 r = parse_pool_features(&as, &pf, ti);
2874 if (r)
2875 goto out_unlock;
2876
2877 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
2878 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002879 if (r) {
2880 ti->error = "Error opening metadata block device";
2881 goto out_unlock;
2882 }
Mike Snitzer7d489352014-02-12 23:58:15 -05002883 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002884
2885 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
2886 if (r) {
2887 ti->error = "Error getting data device";
2888 goto out_metadata;
2889 }
2890
2891 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
2892 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2893 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002894 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002895 ti->error = "Invalid block size";
2896 r = -EINVAL;
2897 goto out;
2898 }
2899
2900 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
2901 ti->error = "Invalid low water mark";
2902 r = -EINVAL;
2903 goto out;
2904 }
2905
Joe Thornber991d9fa2011-10-31 20:21:18 +00002906 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
2907 if (!pt) {
2908 r = -ENOMEM;
2909 goto out;
2910 }
2911
2912 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002913 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002914 if (IS_ERR(pool)) {
2915 r = PTR_ERR(pool);
2916 goto out_free_pt;
2917 }
2918
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002919 /*
2920 * 'pool_created' reflects whether this is the first table load.
2921 * Top level discard support is not allowed to be changed after
2922 * initial load. This would require a pool reload to trigger thin
2923 * device changes.
2924 */
2925 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
2926 ti->error = "Discard support cannot be disabled once enabled";
2927 r = -EINVAL;
2928 goto out_flags_changed;
2929 }
2930
Joe Thornber991d9fa2011-10-31 20:21:18 +00002931 pt->pool = pool;
2932 pt->ti = ti;
2933 pt->metadata_dev = metadata_dev;
2934 pt->data_dev = data_dev;
2935 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002936 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002937 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002938
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002939 /*
2940 * Only need to enable discards if the pool should pass
2941 * them down to the data device. The thin device's discard
2942 * processing will cause mappings to be removed from the btree.
2943 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04002944 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002945 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002946 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002947
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002948 /*
2949 * Setting 'discards_supported' circumvents the normal
2950 * stacking of discard limits (this keeps the pool and
2951 * thin devices' discard limits consistent).
2952 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002953 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002954 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002955 ti->private = pt;
2956
Joe Thornberac8c3f32013-05-10 14:37:21 +01002957 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
2958 calc_metadata_threshold(pt),
2959 metadata_low_callback,
2960 pool);
2961 if (r)
Mike Snitzer015ec5d2015-10-13 12:04:28 -04002962 goto out_flags_changed;
Joe Thornberac8c3f32013-05-10 14:37:21 +01002963
Joe Thornber991d9fa2011-10-31 20:21:18 +00002964 pt->callbacks.congested_fn = pool_is_congested;
2965 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
2966
2967 mutex_unlock(&dm_thin_pool_table.mutex);
2968
2969 return 0;
2970
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002971out_flags_changed:
2972 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002973out_free_pt:
2974 kfree(pt);
2975out:
2976 dm_put_device(ti, data_dev);
2977out_metadata:
2978 dm_put_device(ti, metadata_dev);
2979out_unlock:
2980 mutex_unlock(&dm_thin_pool_table.mutex);
2981
2982 return r;
2983}
2984
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002985static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002986{
2987 int r;
2988 struct pool_c *pt = ti->private;
2989 struct pool *pool = pt->pool;
2990 unsigned long flags;
2991
2992 /*
2993 * As this is a singleton target, ti->begin is always zero.
2994 */
2995 spin_lock_irqsave(&pool->lock, flags);
2996 bio->bi_bdev = pt->data_dev->bdev;
2997 r = DM_MAPIO_REMAPPED;
2998 spin_unlock_irqrestore(&pool->lock, flags);
2999
3000 return r;
3001}
3002
Joe Thornberb17446d2013-05-10 14:37:18 +01003003static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
3004{
3005 int r;
3006 struct pool_c *pt = ti->private;
3007 struct pool *pool = pt->pool;
3008 sector_t data_size = ti->len;
3009 dm_block_t sb_data_size;
3010
3011 *need_commit = false;
3012
3013 (void) sector_div(data_size, pool->sectors_per_block);
3014
3015 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3016 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003017 DMERR("%s: failed to retrieve data device size",
3018 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01003019 return r;
3020 }
3021
3022 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003023 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3024 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01003025 (unsigned long long)data_size, sb_data_size);
3026 return -EINVAL;
3027
3028 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003029 if (dm_pool_metadata_needs_check(pool->pmd)) {
3030 DMERR("%s: unable to grow the data device until repaired.",
3031 dm_device_name(pool->pool_md));
3032 return 0;
3033 }
3034
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003035 if (sb_data_size)
3036 DMINFO("%s: growing the data device from %llu to %llu blocks",
3037 dm_device_name(pool->pool_md),
3038 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01003039 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3040 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003041 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01003042 return r;
3043 }
3044
3045 *need_commit = true;
3046 }
3047
3048 return 0;
3049}
3050
Joe Thornber24347e92013-05-10 14:37:19 +01003051static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3052{
3053 int r;
3054 struct pool_c *pt = ti->private;
3055 struct pool *pool = pt->pool;
3056 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3057
3058 *need_commit = false;
3059
Alasdair G Kergon610bba82013-05-19 18:57:50 +01003060 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01003061
3062 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3063 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003064 DMERR("%s: failed to retrieve metadata device size",
3065 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01003066 return r;
3067 }
3068
3069 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003070 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3071 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01003072 metadata_dev_size, sb_metadata_dev_size);
3073 return -EINVAL;
3074
3075 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003076 if (dm_pool_metadata_needs_check(pool->pmd)) {
3077 DMERR("%s: unable to grow the metadata device until repaired.",
3078 dm_device_name(pool->pool_md));
3079 return 0;
3080 }
3081
Mike Snitzer7d489352014-02-12 23:58:15 -05003082 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003083 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3084 dm_device_name(pool->pool_md),
3085 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber24347e92013-05-10 14:37:19 +01003086 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3087 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003088 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01003089 return r;
3090 }
3091
3092 *need_commit = true;
3093 }
3094
3095 return 0;
3096}
3097
Joe Thornber991d9fa2011-10-31 20:21:18 +00003098/*
3099 * Retrieves the number of blocks of the data device from
3100 * the superblock and compares it to the actual device size,
3101 * thus resizing the data device in case it has grown.
3102 *
3103 * This both copes with opening preallocated data devices in the ctr
3104 * being followed by a resume
3105 * -and-
3106 * calling the resume method individually after userspace has
3107 * grown the data device in reaction to a table event.
3108 */
3109static int pool_preresume(struct dm_target *ti)
3110{
3111 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01003112 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003113 struct pool_c *pt = ti->private;
3114 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003115
3116 /*
3117 * Take control of the pool object.
3118 */
3119 r = bind_control_target(pool, ti);
3120 if (r)
3121 return r;
3122
Joe Thornberb17446d2013-05-10 14:37:18 +01003123 r = maybe_resize_data_dev(ti, &need_commit1);
3124 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003125 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003126
Joe Thornber24347e92013-05-10 14:37:19 +01003127 r = maybe_resize_metadata_dev(ti, &need_commit2);
3128 if (r)
3129 return r;
3130
3131 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003132 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003133
3134 return 0;
3135}
3136
Mike Snitzer583024d2014-10-28 20:58:45 -04003137static void pool_suspend_active_thins(struct pool *pool)
3138{
3139 struct thin_c *tc;
3140
3141 /* Suspend all active thin devices */
3142 tc = get_first_thin(pool);
3143 while (tc) {
3144 dm_internal_suspend_noflush(tc->thin_md);
3145 tc = get_next_thin(pool, tc);
3146 }
3147}
3148
3149static void pool_resume_active_thins(struct pool *pool)
3150{
3151 struct thin_c *tc;
3152
3153 /* Resume all active thin devices */
3154 tc = get_first_thin(pool);
3155 while (tc) {
3156 dm_internal_resume(tc->thin_md);
3157 tc = get_next_thin(pool, tc);
3158 }
3159}
3160
Joe Thornber991d9fa2011-10-31 20:21:18 +00003161static void pool_resume(struct dm_target *ti)
3162{
3163 struct pool_c *pt = ti->private;
3164 struct pool *pool = pt->pool;
3165 unsigned long flags;
3166
Mike Snitzer583024d2014-10-28 20:58:45 -04003167 /*
3168 * Must requeue active_thins' bios and then resume
3169 * active_thins _before_ clearing 'suspend' flag.
3170 */
3171 requeue_bios(pool);
3172 pool_resume_active_thins(pool);
3173
Joe Thornber991d9fa2011-10-31 20:21:18 +00003174 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05003175 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003176 pool->suspended = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003177 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003178
Joe Thornber905e51b2012-03-28 18:41:27 +01003179 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003180}
3181
Mike Snitzer80e96c52014-11-07 15:09:46 -05003182static void pool_presuspend(struct dm_target *ti)
3183{
3184 struct pool_c *pt = ti->private;
3185 struct pool *pool = pt->pool;
3186 unsigned long flags;
3187
3188 spin_lock_irqsave(&pool->lock, flags);
3189 pool->suspended = true;
3190 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer583024d2014-10-28 20:58:45 -04003191
3192 pool_suspend_active_thins(pool);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003193}
3194
3195static void pool_presuspend_undo(struct dm_target *ti)
3196{
3197 struct pool_c *pt = ti->private;
3198 struct pool *pool = pt->pool;
3199 unsigned long flags;
3200
Mike Snitzer583024d2014-10-28 20:58:45 -04003201 pool_resume_active_thins(pool);
3202
Mike Snitzer80e96c52014-11-07 15:09:46 -05003203 spin_lock_irqsave(&pool->lock, flags);
3204 pool->suspended = false;
3205 spin_unlock_irqrestore(&pool->lock, flags);
3206}
3207
Joe Thornber991d9fa2011-10-31 20:21:18 +00003208static void pool_postsuspend(struct dm_target *ti)
3209{
Joe Thornber991d9fa2011-10-31 20:21:18 +00003210 struct pool_c *pt = ti->private;
3211 struct pool *pool = pt->pool;
3212
Nikolay Borisov9e612a0002015-12-17 18:03:35 +02003213 cancel_delayed_work_sync(&pool->waker);
3214 cancel_delayed_work_sync(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003215 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05003216 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003217}
3218
3219static int check_arg_count(unsigned argc, unsigned args_required)
3220{
3221 if (argc != args_required) {
3222 DMWARN("Message received with %u arguments instead of %u.",
3223 argc, args_required);
3224 return -EINVAL;
3225 }
3226
3227 return 0;
3228}
3229
3230static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3231{
3232 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3233 *dev_id <= MAX_DEV_ID)
3234 return 0;
3235
3236 if (warning)
3237 DMWARN("Message received with invalid device id: %s", arg);
3238
3239 return -EINVAL;
3240}
3241
3242static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3243{
3244 dm_thin_id dev_id;
3245 int r;
3246
3247 r = check_arg_count(argc, 2);
3248 if (r)
3249 return r;
3250
3251 r = read_dev_id(argv[1], &dev_id, 1);
3252 if (r)
3253 return r;
3254
3255 r = dm_pool_create_thin(pool->pmd, dev_id);
3256 if (r) {
3257 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3258 argv[1]);
3259 return r;
3260 }
3261
3262 return 0;
3263}
3264
3265static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3266{
3267 dm_thin_id dev_id;
3268 dm_thin_id origin_dev_id;
3269 int r;
3270
3271 r = check_arg_count(argc, 3);
3272 if (r)
3273 return r;
3274
3275 r = read_dev_id(argv[1], &dev_id, 1);
3276 if (r)
3277 return r;
3278
3279 r = read_dev_id(argv[2], &origin_dev_id, 1);
3280 if (r)
3281 return r;
3282
3283 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3284 if (r) {
3285 DMWARN("Creation of new snapshot %s of device %s failed.",
3286 argv[1], argv[2]);
3287 return r;
3288 }
3289
3290 return 0;
3291}
3292
3293static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3294{
3295 dm_thin_id dev_id;
3296 int r;
3297
3298 r = check_arg_count(argc, 2);
3299 if (r)
3300 return r;
3301
3302 r = read_dev_id(argv[1], &dev_id, 1);
3303 if (r)
3304 return r;
3305
3306 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3307 if (r)
3308 DMWARN("Deletion of thin device %s failed.", argv[1]);
3309
3310 return r;
3311}
3312
3313static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3314{
3315 dm_thin_id old_id, new_id;
3316 int r;
3317
3318 r = check_arg_count(argc, 3);
3319 if (r)
3320 return r;
3321
3322 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3323 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3324 return -EINVAL;
3325 }
3326
3327 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3328 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3329 return -EINVAL;
3330 }
3331
3332 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3333 if (r) {
3334 DMWARN("Failed to change transaction id from %s to %s.",
3335 argv[1], argv[2]);
3336 return r;
3337 }
3338
3339 return 0;
3340}
3341
Joe Thornbercc8394d2012-06-03 00:30:01 +01003342static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3343{
3344 int r;
3345
3346 r = check_arg_count(argc, 1);
3347 if (r)
3348 return r;
3349
Joe Thornber020cc3b2013-12-04 15:05:36 -05003350 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01003351
Joe Thornbercc8394d2012-06-03 00:30:01 +01003352 r = dm_pool_reserve_metadata_snap(pool->pmd);
3353 if (r)
3354 DMWARN("reserve_metadata_snap message failed.");
3355
3356 return r;
3357}
3358
3359static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3360{
3361 int r;
3362
3363 r = check_arg_count(argc, 1);
3364 if (r)
3365 return r;
3366
3367 r = dm_pool_release_metadata_snap(pool->pmd);
3368 if (r)
3369 DMWARN("release_metadata_snap message failed.");
3370
3371 return r;
3372}
3373
Joe Thornber991d9fa2011-10-31 20:21:18 +00003374/*
3375 * Messages supported:
3376 * create_thin <dev_id>
3377 * create_snap <dev_id> <origin_id>
3378 * delete <dev_id>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003379 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003380 * reserve_metadata_snap
3381 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003382 */
3383static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3384{
3385 int r = -EINVAL;
3386 struct pool_c *pt = ti->private;
3387 struct pool *pool = pt->pool;
3388
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003389 if (get_pool_mode(pool) >= PM_READ_ONLY) {
3390 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3391 dm_device_name(pool->pool_md));
3392 return -EINVAL;
3393 }
3394
Joe Thornber991d9fa2011-10-31 20:21:18 +00003395 if (!strcasecmp(argv[0], "create_thin"))
3396 r = process_create_thin_mesg(argc, argv, pool);
3397
3398 else if (!strcasecmp(argv[0], "create_snap"))
3399 r = process_create_snap_mesg(argc, argv, pool);
3400
3401 else if (!strcasecmp(argv[0], "delete"))
3402 r = process_delete_mesg(argc, argv, pool);
3403
3404 else if (!strcasecmp(argv[0], "set_transaction_id"))
3405 r = process_set_transaction_id_mesg(argc, argv, pool);
3406
Joe Thornbercc8394d2012-06-03 00:30:01 +01003407 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3408 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3409
3410 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3411 r = process_release_metadata_snap_mesg(argc, argv, pool);
3412
Joe Thornber991d9fa2011-10-31 20:21:18 +00003413 else
3414 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3415
Joe Thornbere49e5822012-07-27 15:08:16 +01003416 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003417 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003418
3419 return r;
3420}
3421
Joe Thornbere49e5822012-07-27 15:08:16 +01003422static void emit_flags(struct pool_features *pf, char *result,
3423 unsigned sz, unsigned maxlen)
3424{
3425 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003426 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3427 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003428 DMEMIT("%u ", count);
3429
3430 if (!pf->zero_new_blocks)
3431 DMEMIT("skip_block_zeroing ");
3432
3433 if (!pf->discard_enabled)
3434 DMEMIT("ignore_discard ");
3435
3436 if (!pf->discard_passdown)
3437 DMEMIT("no_discard_passdown ");
3438
3439 if (pf->mode == PM_READ_ONLY)
3440 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003441
3442 if (pf->error_if_no_space)
3443 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003444}
3445
Joe Thornber991d9fa2011-10-31 20:21:18 +00003446/*
3447 * Status line is:
3448 * <transaction id> <used metadata sectors>/<total metadata sectors>
3449 * <used data sectors>/<total data sectors> <held metadata root>
3450 */
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003451static void pool_status(struct dm_target *ti, status_type_t type,
3452 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003453{
Joe Thornbere49e5822012-07-27 15:08:16 +01003454 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003455 unsigned sz = 0;
3456 uint64_t transaction_id;
3457 dm_block_t nr_free_blocks_data;
3458 dm_block_t nr_free_blocks_metadata;
3459 dm_block_t nr_blocks_data;
3460 dm_block_t nr_blocks_metadata;
3461 dm_block_t held_root;
3462 char buf[BDEVNAME_SIZE];
3463 char buf2[BDEVNAME_SIZE];
3464 struct pool_c *pt = ti->private;
3465 struct pool *pool = pt->pool;
3466
3467 switch (type) {
3468 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003469 if (get_pool_mode(pool) == PM_FAIL) {
3470 DMEMIT("Fail");
3471 break;
3472 }
3473
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003474 /* Commit to ensure statistics aren't out-of-date */
3475 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003476 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003477
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003478 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3479 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003480 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3481 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003482 goto err;
3483 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003484
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003485 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3486 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003487 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3488 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003489 goto err;
3490 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003491
3492 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003493 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003494 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3495 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003496 goto err;
3497 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003498
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003499 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3500 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003501 DMERR("%s: dm_pool_get_free_block_count returned %d",
3502 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003503 goto err;
3504 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003505
3506 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003507 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003508 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3509 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003510 goto err;
3511 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003512
Joe Thornbercc8394d2012-06-03 00:30:01 +01003513 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003514 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003515 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3516 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003517 goto err;
3518 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003519
3520 DMEMIT("%llu %llu/%llu %llu/%llu ",
3521 (unsigned long long)transaction_id,
3522 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3523 (unsigned long long)nr_blocks_metadata,
3524 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3525 (unsigned long long)nr_blocks_data);
3526
3527 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003528 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003529 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003530 DMEMIT("- ");
3531
Joe Thornber3e1a0692014-03-03 16:03:26 +00003532 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3533 DMEMIT("out_of_data_space ");
3534 else if (pool->pf.mode == PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01003535 DMEMIT("ro ");
3536 else
3537 DMEMIT("rw ");
3538
Mike Snitzer018debe2012-12-21 20:23:32 +00003539 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003540 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003541 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003542 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003543 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003544 DMEMIT("no_discard_passdown ");
3545
3546 if (pool->pf.error_if_no_space)
3547 DMEMIT("error_if_no_space ");
3548 else
3549 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003550
3551 break;
3552
3553 case STATUSTYPE_TABLE:
3554 DMEMIT("%s %s %lu %llu ",
3555 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3556 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3557 (unsigned long)pool->sectors_per_block,
3558 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01003559 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003560 break;
3561 }
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003562 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003563
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003564err:
3565 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003566}
3567
3568static int pool_iterate_devices(struct dm_target *ti,
3569 iterate_devices_callout_fn fn, void *data)
3570{
3571 struct pool_c *pt = ti->private;
3572
3573 return fn(ti, pt->data_dev, 0, ti->len, data);
3574}
3575
3576static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3577 struct bio_vec *biovec, int max_size)
3578{
3579 struct pool_c *pt = ti->private;
3580 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3581
3582 if (!q->merge_bvec_fn)
3583 return max_size;
3584
3585 bvm->bi_bdev = pt->data_dev->bdev;
3586
3587 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3588}
3589
Mike Snitzer0424caa2012-09-26 23:45:47 +01003590static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
Joe Thornber104655f2012-03-28 18:41:28 +01003591{
Mike Snitzer0424caa2012-09-26 23:45:47 +01003592 struct pool *pool = pt->pool;
3593 struct queue_limits *data_limits;
3594
Joe Thornber104655f2012-03-28 18:41:28 +01003595 limits->max_discard_sectors = pool->sectors_per_block;
3596
3597 /*
Mike Snitzer0424caa2012-09-26 23:45:47 +01003598 * discard_granularity is just a hint, and not enforced.
Joe Thornber104655f2012-03-28 18:41:28 +01003599 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01003600 if (pt->adjusted_pf.discard_passdown) {
3601 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
Lukas Czerner09869de2014-06-11 12:28:43 -04003602 limits->discard_granularity = max(data_limits->discard_granularity,
3603 pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf13945d2013-03-01 22:45:44 +00003604 } else
Mike Snitzer0424caa2012-09-26 23:45:47 +01003605 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
Joe Thornber104655f2012-03-28 18:41:28 +01003606}
3607
Joe Thornber991d9fa2011-10-31 20:21:18 +00003608static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3609{
3610 struct pool_c *pt = ti->private;
3611 struct pool *pool = pt->pool;
Mike Snitzer604ea902014-10-09 18:43:25 -04003612 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3613
3614 /*
Mike Snitzerd200c302014-11-20 18:07:43 -05003615 * If max_sectors is smaller than pool->sectors_per_block adjust it
3616 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3617 * This is especially beneficial when the pool's data device is a RAID
3618 * device that has a full stripe width that matches pool->sectors_per_block
3619 * -- because even though partial RAID stripe-sized IOs will be issued to a
3620 * single RAID stripe; when aggregated they will end on a full RAID stripe
3621 * boundary.. which avoids additional partial RAID stripe writes cascading
Mike Snitzer604ea902014-10-09 18:43:25 -04003622 */
Mike Snitzer604ea902014-10-09 18:43:25 -04003623 if (limits->max_sectors < pool->sectors_per_block) {
3624 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3625 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3626 limits->max_sectors--;
3627 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3628 }
Mike Snitzer604ea902014-10-09 18:43:25 -04003629 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003630
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003631 /*
3632 * If the system-determined stacked limits are compatible with the
3633 * pool's blocksize (io_opt is a factor) do not override them.
3634 */
3635 if (io_opt_sectors < pool->sectors_per_block ||
Mike Snitzer604ea902014-10-09 18:43:25 -04003636 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3637 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3638 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3639 else
3640 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003641 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3642 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003643
3644 /*
3645 * pt->adjusted_pf is a staging area for the actual features to use.
3646 * They get transferred to the live pool in bind_control_target()
3647 * called from pool_preresume().
3648 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003649 if (!pt->adjusted_pf.discard_enabled) {
3650 /*
3651 * Must explicitly disallow stacking discard limits otherwise the
3652 * block layer will stack them if pool's data device has support.
3653 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3654 * user to see that, so make sure to set all discard limits to 0.
3655 */
3656 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003657 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04003658 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003659
3660 disable_passdown_if_not_supported(pt);
3661
3662 set_discard_limits(pt, limits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003663}
3664
3665static struct target_type pool_target = {
3666 .name = "thin-pool",
3667 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3668 DM_TARGET_IMMUTABLE,
Mike Snitzer36f12aeb2014-10-09 15:24:12 -04003669 .version = {1, 14, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003670 .module = THIS_MODULE,
3671 .ctr = pool_ctr,
3672 .dtr = pool_dtr,
3673 .map = pool_map,
Mike Snitzer80e96c52014-11-07 15:09:46 -05003674 .presuspend = pool_presuspend,
3675 .presuspend_undo = pool_presuspend_undo,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003676 .postsuspend = pool_postsuspend,
3677 .preresume = pool_preresume,
3678 .resume = pool_resume,
3679 .message = pool_message,
3680 .status = pool_status,
3681 .merge = pool_merge,
3682 .iterate_devices = pool_iterate_devices,
3683 .io_hints = pool_io_hints,
3684};
3685
3686/*----------------------------------------------------------------
3687 * Thin target methods
3688 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01003689static void thin_get(struct thin_c *tc)
3690{
3691 atomic_inc(&tc->refcount);
3692}
3693
3694static void thin_put(struct thin_c *tc)
3695{
3696 if (atomic_dec_and_test(&tc->refcount))
3697 complete(&tc->can_destroy);
3698}
3699
Joe Thornber991d9fa2011-10-31 20:21:18 +00003700static void thin_dtr(struct dm_target *ti)
3701{
3702 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003703 unsigned long flags;
3704
3705 spin_lock_irqsave(&tc->pool->lock, flags);
3706 list_del_rcu(&tc->list);
3707 spin_unlock_irqrestore(&tc->pool->lock, flags);
3708 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00003709
Mikulas Patocka17181fb2014-11-05 17:00:13 -05003710 thin_put(tc);
3711 wait_for_completion(&tc->can_destroy);
3712
Joe Thornber991d9fa2011-10-31 20:21:18 +00003713 mutex_lock(&dm_thin_pool_table.mutex);
3714
3715 __pool_dec(tc->pool);
3716 dm_pool_close_thin_device(tc->td);
3717 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003718 if (tc->origin_dev)
3719 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003720 kfree(tc);
3721
3722 mutex_unlock(&dm_thin_pool_table.mutex);
3723}
3724
3725/*
3726 * Thin target parameters:
3727 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01003728 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00003729 *
3730 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3731 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01003732 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003733 *
3734 * If the pool device has discards disabled, they get disabled for the thin
3735 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003736 */
3737static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3738{
3739 int r;
3740 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01003741 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003742 struct mapped_device *pool_md;
Joe Thornber5e3283e2014-04-08 11:08:41 +01003743 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003744
3745 mutex_lock(&dm_thin_pool_table.mutex);
3746
Joe Thornber2dd9c252012-03-28 18:41:28 +01003747 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003748 ti->error = "Invalid argument count";
3749 r = -EINVAL;
3750 goto out_unlock;
3751 }
3752
3753 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
3754 if (!tc) {
3755 ti->error = "Out of memory";
3756 r = -ENOMEM;
3757 goto out_unlock;
3758 }
Mike Snitzer583024d2014-10-28 20:58:45 -04003759 tc->thin_md = dm_table_get_md(ti->table);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003760 spin_lock_init(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01003761 INIT_LIST_HEAD(&tc->deferred_cells);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003762 bio_list_init(&tc->deferred_bio_list);
3763 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04003764 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003765
Joe Thornber2dd9c252012-03-28 18:41:28 +01003766 if (argc == 3) {
3767 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
3768 if (r) {
3769 ti->error = "Error opening origin device";
3770 goto bad_origin_dev;
3771 }
3772 tc->origin_dev = origin_dev;
3773 }
3774
Joe Thornber991d9fa2011-10-31 20:21:18 +00003775 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
3776 if (r) {
3777 ti->error = "Error opening pool device";
3778 goto bad_pool_dev;
3779 }
3780 tc->pool_dev = pool_dev;
3781
3782 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
3783 ti->error = "Invalid device id";
3784 r = -EINVAL;
3785 goto bad_common;
3786 }
3787
3788 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
3789 if (!pool_md) {
3790 ti->error = "Couldn't get pool mapped device";
3791 r = -EINVAL;
3792 goto bad_common;
3793 }
3794
3795 tc->pool = __pool_table_lookup(pool_md);
3796 if (!tc->pool) {
3797 ti->error = "Couldn't find pool object";
3798 r = -EINVAL;
3799 goto bad_pool_lookup;
3800 }
3801 __pool_inc(tc->pool);
3802
Joe Thornbere49e5822012-07-27 15:08:16 +01003803 if (get_pool_mode(tc->pool) == PM_FAIL) {
3804 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05003805 r = -EINVAL;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003806 goto bad_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +01003807 }
3808
Joe Thornber991d9fa2011-10-31 20:21:18 +00003809 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
3810 if (r) {
3811 ti->error = "Couldn't open thin internal device";
Mike Snitzer80e96c52014-11-07 15:09:46 -05003812 goto bad_pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003813 }
3814
Mike Snitzer542f9032012-07-27 15:08:00 +01003815 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
3816 if (r)
Mike Snitzer80e96c52014-11-07 15:09:46 -05003817 goto bad;
Mike Snitzer542f9032012-07-27 15:08:00 +01003818
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003819 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01003820 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003821 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003822
3823 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003824 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003825 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003826 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003827 ti->num_discard_bios = 1;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003828 /* Discard bios must be split on a block boundary */
3829 ti->split_discard_bios = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003830 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003831
Joe Thornber991d9fa2011-10-31 20:21:18 +00003832 mutex_unlock(&dm_thin_pool_table.mutex);
3833
Joe Thornber5e3283e2014-04-08 11:08:41 +01003834 spin_lock_irqsave(&tc->pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003835 if (tc->pool->suspended) {
3836 spin_unlock_irqrestore(&tc->pool->lock, flags);
3837 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
3838 ti->error = "Unable to activate thin device while pool is suspended";
3839 r = -EINVAL;
3840 goto bad;
3841 }
Marc Dionne2b94e892014-12-17 07:59:59 -05003842 atomic_set(&tc->refcount, 1);
3843 init_completion(&tc->can_destroy);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003844 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Joe Thornber5e3283e2014-04-08 11:08:41 +01003845 spin_unlock_irqrestore(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003846 /*
3847 * This synchronize_rcu() call is needed here otherwise we risk a
3848 * wake_worker() call finding no bios to process (because the newly
3849 * added tc isn't yet visible). So this reduces latency since we
3850 * aren't then dependent on the periodic commit to wake_worker().
3851 */
3852 synchronize_rcu();
3853
Mike Snitzer80e96c52014-11-07 15:09:46 -05003854 dm_put(pool_md);
3855
Joe Thornber991d9fa2011-10-31 20:21:18 +00003856 return 0;
3857
Mike Snitzer80e96c52014-11-07 15:09:46 -05003858bad:
Mike Snitzer1acacc02014-02-19 20:32:33 -05003859 dm_pool_close_thin_device(tc->td);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003860bad_pool:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003861 __pool_dec(tc->pool);
3862bad_pool_lookup:
3863 dm_put(pool_md);
3864bad_common:
3865 dm_put_device(ti, tc->pool_dev);
3866bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01003867 if (tc->origin_dev)
3868 dm_put_device(ti, tc->origin_dev);
3869bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003870 kfree(tc);
3871out_unlock:
3872 mutex_unlock(&dm_thin_pool_table.mutex);
3873
3874 return r;
3875}
3876
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003877static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003878{
Kent Overstreet4f024f32013-10-11 15:44:27 -07003879 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003880
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003881 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003882}
3883
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003884static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01003885{
3886 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003887 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01003888 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01003889 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01003890 struct pool *pool = h->tc->pool;
3891
3892 if (h->shared_read_entry) {
3893 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003894 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003895
3896 spin_lock_irqsave(&pool->lock, flags);
3897 list_for_each_entry_safe(m, tmp, &work, list) {
3898 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01003899 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003900 }
3901 spin_unlock_irqrestore(&pool->lock, flags);
3902 }
3903
Joe Thornber104655f2012-03-28 18:41:28 +01003904 if (h->all_io_entry) {
3905 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003906 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00003907 if (!list_empty(&work)) {
3908 spin_lock_irqsave(&pool->lock, flags);
3909 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05003910 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00003911 spin_unlock_irqrestore(&pool->lock, flags);
3912 wake_worker(pool);
3913 }
Joe Thornber104655f2012-03-28 18:41:28 +01003914 }
3915
Joe Thornbereb2aa482012-03-28 18:41:28 +01003916 return 0;
3917}
3918
Joe Thornber738211f2014-03-03 15:52:28 +00003919static void thin_presuspend(struct dm_target *ti)
3920{
3921 struct thin_c *tc = ti->private;
3922
3923 if (dm_noflush_suspending(ti))
3924 noflush_work(tc, do_noflush_start);
3925}
3926
Joe Thornber991d9fa2011-10-31 20:21:18 +00003927static void thin_postsuspend(struct dm_target *ti)
3928{
Joe Thornber738211f2014-03-03 15:52:28 +00003929 struct thin_c *tc = ti->private;
3930
3931 /*
3932 * The dm_noflush_suspending flag has been cleared by now, so
3933 * unfortunately we must always run this.
3934 */
3935 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003936}
3937
Joe Thornbere5aea7b2014-06-13 14:47:24 +01003938static int thin_preresume(struct dm_target *ti)
3939{
3940 struct thin_c *tc = ti->private;
3941
3942 if (tc->origin_dev)
3943 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
3944
3945 return 0;
3946}
3947
Joe Thornber991d9fa2011-10-31 20:21:18 +00003948/*
3949 * <nr mapped sectors> <highest mapped sector>
3950 */
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003951static void thin_status(struct dm_target *ti, status_type_t type,
3952 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003953{
3954 int r;
3955 ssize_t sz = 0;
3956 dm_block_t mapped, highest;
3957 char buf[BDEVNAME_SIZE];
3958 struct thin_c *tc = ti->private;
3959
Joe Thornbere49e5822012-07-27 15:08:16 +01003960 if (get_pool_mode(tc->pool) == PM_FAIL) {
3961 DMEMIT("Fail");
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003962 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01003963 }
3964
Joe Thornber991d9fa2011-10-31 20:21:18 +00003965 if (!tc->td)
3966 DMEMIT("-");
3967 else {
3968 switch (type) {
3969 case STATUSTYPE_INFO:
3970 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003971 if (r) {
3972 DMERR("dm_thin_get_mapped_count returned %d", r);
3973 goto err;
3974 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003975
3976 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00003977 if (r < 0) {
3978 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
3979 goto err;
3980 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003981
3982 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
3983 if (r)
3984 DMEMIT("%llu", ((highest + 1) *
3985 tc->pool->sectors_per_block) - 1);
3986 else
3987 DMEMIT("-");
3988 break;
3989
3990 case STATUSTYPE_TABLE:
3991 DMEMIT("%s %lu",
3992 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
3993 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003994 if (tc->origin_dev)
3995 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00003996 break;
3997 }
3998 }
3999
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00004000 return;
4001
4002err:
4003 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004004}
4005
Mike Snitzer36f12aeb2014-10-09 15:24:12 -04004006static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
4007 struct bio_vec *biovec, int max_size)
4008{
4009 struct thin_c *tc = ti->private;
4010 struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
4011
4012 if (!q->merge_bvec_fn)
4013 return max_size;
4014
4015 bvm->bi_bdev = tc->pool_dev->bdev;
4016 bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
4017
4018 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
4019}
4020
Joe Thornber991d9fa2011-10-31 20:21:18 +00004021static int thin_iterate_devices(struct dm_target *ti,
4022 iterate_devices_callout_fn fn, void *data)
4023{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004024 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004025 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004026 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004027
4028 /*
4029 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4030 * we follow a more convoluted path through to the pool's target.
4031 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004032 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004033 return 0; /* nothing is bound */
4034
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004035 blocks = pool->ti->len;
4036 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004037 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004038 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004039
4040 return 0;
4041}
4042
Joe Thornber991d9fa2011-10-31 20:21:18 +00004043static struct target_type thin_target = {
4044 .name = "thin",
Mike Snitzer36f12aeb2014-10-09 15:24:12 -04004045 .version = {1, 14, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004046 .module = THIS_MODULE,
4047 .ctr = thin_ctr,
4048 .dtr = thin_dtr,
4049 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01004050 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004051 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00004052 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004053 .postsuspend = thin_postsuspend,
4054 .status = thin_status,
Mike Snitzer36f12aeb2014-10-09 15:24:12 -04004055 .merge = thin_merge,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004056 .iterate_devices = thin_iterate_devices,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004057};
4058
4059/*----------------------------------------------------------------*/
4060
4061static int __init dm_thin_init(void)
4062{
4063 int r;
4064
4065 pool_table_init();
4066
4067 r = dm_register_target(&thin_target);
4068 if (r)
4069 return r;
4070
4071 r = dm_register_target(&pool_target);
4072 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01004073 goto bad_pool_target;
4074
4075 r = -ENOMEM;
4076
Mike Snitzera24c2562012-06-03 00:30:00 +01004077 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4078 if (!_new_mapping_cache)
4079 goto bad_new_mapping_cache;
4080
Mike Snitzera24c2562012-06-03 00:30:00 +01004081 return 0;
4082
Mike Snitzera24c2562012-06-03 00:30:00 +01004083bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01004084 dm_unregister_target(&pool_target);
4085bad_pool_target:
4086 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004087
4088 return r;
4089}
4090
4091static void dm_thin_exit(void)
4092{
4093 dm_unregister_target(&thin_target);
4094 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01004095
Mike Snitzera24c2562012-06-03 00:30:00 +01004096 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004097}
4098
4099module_init(dm_thin_init);
4100module_exit(dm_thin_exit);
4101
Mike Snitzer80c57892014-05-20 13:38:33 -04004102module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4103MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4104
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01004105MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004106MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4107MODULE_LICENSE("GPL");