blob: 91b430b883fd85fdf18bfe1c1e5a39d336eae020 [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>
14#include <linux/list.h>
Mike Snitzerc140e1c2014-03-20 21:17:14 -040015#include <linux/rculist.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000016#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/slab.h>
Mike Snitzer67324ea2014-03-21 18:33:41 -040019#include <linux/rbtree.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000020
21#define DM_MSG_PREFIX "thin"
22
23/*
24 * Tunable constants
25 */
Alasdair G Kergon7768ed32012-07-27 15:07:57 +010026#define ENDIO_HOOK_POOL_SIZE 1024
Joe Thornber991d9fa2011-10-31 20:21:18 +000027#define MAPPING_POOL_SIZE 1024
Joe Thornber905e51b2012-03-28 18:41:27 +010028#define COMMIT_PERIOD HZ
Mike Snitzer80c57892014-05-20 13:38:33 -040029#define NO_SPACE_TIMEOUT_SECS 60
30
31static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
Joe Thornber991d9fa2011-10-31 20:21:18 +000032
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000033DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
34 "A percentage of time allocated for copy on write");
35
Joe Thornber991d9fa2011-10-31 20:21:18 +000036/*
37 * The block size of the device holding pool data must be
38 * between 64KB and 1GB.
39 */
40#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
41#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
42
43/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000044 * Device id is restricted to 24 bits.
45 */
46#define MAX_DEV_ID ((1 << 24) - 1)
47
48/*
49 * How do we handle breaking sharing of data blocks?
50 * =================================================
51 *
52 * We use a standard copy-on-write btree to store the mappings for the
53 * devices (note I'm talking about copy-on-write of the metadata here, not
54 * the data). When you take an internal snapshot you clone the root node
55 * of the origin btree. After this there is no concept of an origin or a
56 * snapshot. They are just two device trees that happen to point to the
57 * same data blocks.
58 *
59 * When we get a write in we decide if it's to a shared data block using
60 * some timestamp magic. If it is, we have to break sharing.
61 *
62 * Let's say we write to a shared block in what was the origin. The
63 * steps are:
64 *
65 * i) plug io further to this physical block. (see bio_prison code).
66 *
67 * ii) quiesce any read io to that shared data block. Obviously
Mike Snitzer44feb382012-10-12 21:02:10 +010068 * including all devices that share this block. (see dm_deferred_set code)
Joe Thornber991d9fa2011-10-31 20:21:18 +000069 *
70 * iii) copy the data block to a newly allocate block. This step can be
71 * missed out if the io covers the block. (schedule_copy).
72 *
73 * iv) insert the new mapping into the origin's btree
Joe Thornberfe878f32012-03-28 18:41:24 +010074 * (process_prepared_mapping). This act of inserting breaks some
Joe Thornber991d9fa2011-10-31 20:21:18 +000075 * sharing of btree nodes between the two devices. Breaking sharing only
76 * effects the btree of that specific device. Btrees for the other
77 * devices that share the block never change. The btree for the origin
78 * device as it was after the last commit is untouched, ie. we're using
79 * persistent data structures in the functional programming sense.
80 *
81 * v) unplug io to this physical block, including the io that triggered
82 * the breaking of sharing.
83 *
84 * Steps (ii) and (iii) occur in parallel.
85 *
86 * The metadata _doesn't_ need to be committed before the io continues. We
87 * get away with this because the io is always written to a _new_ block.
88 * If there's a crash, then:
89 *
90 * - The origin mapping will point to the old origin block (the shared
91 * one). This will contain the data as it was before the io that triggered
92 * the breaking of sharing came in.
93 *
94 * - The snap mapping still points to the old block. As it would after
95 * the commit.
96 *
97 * The downside of this scheme is the timestamp magic isn't perfect, and
98 * will continue to think that data block in the snapshot device is shared
99 * even after the write to the origin has broken sharing. I suspect data
100 * blocks will typically be shared by many different devices, so we're
101 * breaking sharing n + 1 times, rather than n, where n is the number of
102 * devices that reference this data block. At the moment I think the
103 * benefits far, far outweigh the disadvantages.
104 */
105
106/*----------------------------------------------------------------*/
107
108/*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000109 * Key building.
110 */
111static void build_data_key(struct dm_thin_device *td,
Mike Snitzer44feb382012-10-12 21:02:10 +0100112 dm_block_t b, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000113{
114 key->virtual = 0;
115 key->dev = dm_thin_dev_id(td);
116 key->block = b;
117}
118
119static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100120 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000121{
122 key->virtual = 1;
123 key->dev = dm_thin_dev_id(td);
124 key->block = b;
125}
126
127/*----------------------------------------------------------------*/
128
Joe Thornber7d327fe2014-10-06 15:45:59 +0100129#define THROTTLE_THRESHOLD (1 * HZ)
130
131struct throttle {
132 struct rw_semaphore lock;
133 unsigned long threshold;
134 bool throttle_applied;
135};
136
137static void throttle_init(struct throttle *t)
138{
139 init_rwsem(&t->lock);
140 t->throttle_applied = false;
141}
142
143static void throttle_work_start(struct throttle *t)
144{
145 t->threshold = jiffies + THROTTLE_THRESHOLD;
146}
147
148static void throttle_work_update(struct throttle *t)
149{
150 if (!t->throttle_applied && jiffies > t->threshold) {
151 down_write(&t->lock);
152 t->throttle_applied = true;
153 }
154}
155
156static void throttle_work_complete(struct throttle *t)
157{
158 if (t->throttle_applied) {
159 t->throttle_applied = false;
160 up_write(&t->lock);
161 }
162}
163
164static void throttle_lock(struct throttle *t)
165{
166 down_read(&t->lock);
167}
168
169static void throttle_unlock(struct throttle *t)
170{
171 up_read(&t->lock);
172}
173
174/*----------------------------------------------------------------*/
175
Joe Thornber991d9fa2011-10-31 20:21:18 +0000176/*
177 * A pool device ties together a metadata device and a data device. It
178 * also provides the interface for creating and destroying internal
179 * devices.
180 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100181struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100182
Joe Thornbere49e5822012-07-27 15:08:16 +0100183/*
Joe Thornber3e1a0692014-03-03 16:03:26 +0000184 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
Joe Thornbere49e5822012-07-27 15:08:16 +0100185 */
186enum pool_mode {
187 PM_WRITE, /* metadata may be changed */
Joe Thornber3e1a0692014-03-03 16:03:26 +0000188 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
Joe Thornbere49e5822012-07-27 15:08:16 +0100189 PM_READ_ONLY, /* metadata may not be changed */
190 PM_FAIL, /* all I/O fails */
191};
192
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100193struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100194 enum pool_mode mode;
195
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100196 bool zero_new_blocks:1;
197 bool discard_enabled:1;
198 bool discard_passdown:1;
Mike Snitzer787a996c2013-12-06 16:21:43 -0500199 bool error_if_no_space:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100200};
201
Joe Thornbere49e5822012-07-27 15:08:16 +0100202struct thin_c;
203typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
204typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
205
Joe Thornber991d9fa2011-10-31 20:21:18 +0000206struct pool {
207 struct list_head list;
208 struct dm_target *ti; /* Only set if a pool target is bound */
209
210 struct mapped_device *pool_md;
211 struct block_device *md_dev;
212 struct dm_pool_metadata *pmd;
213
Joe Thornber991d9fa2011-10-31 20:21:18 +0000214 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100215 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100216 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000217
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100218 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500219 bool low_water_triggered:1; /* A dm event has been sent */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000220
Mike Snitzer44feb382012-10-12 21:02:10 +0100221 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000222 struct dm_kcopyd_client *copier;
223
224 struct workqueue_struct *wq;
Joe Thornber7d327fe2014-10-06 15:45:59 +0100225 struct throttle throttle;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000226 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100227 struct delayed_work waker;
Joe Thornber85ad6432014-05-09 15:59:38 +0100228 struct delayed_work no_space_timeout;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000229
Joe Thornber905e51b2012-03-28 18:41:27 +0100230 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100231 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000232
233 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000234 struct bio_list deferred_flush_bios;
235 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100236 struct list_head prepared_discards;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400237 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000238
Mike Snitzer44feb382012-10-12 21:02:10 +0100239 struct dm_deferred_set *shared_read_ds;
240 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000241
Mike Snitzera24c2562012-06-03 00:30:00 +0100242 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000243 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100244
245 process_bio_fn process_bio;
246 process_bio_fn process_discard;
247
248 process_mapping_fn process_prepared_mapping;
249 process_mapping_fn process_prepared_discard;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000250};
251
Joe Thornbere49e5822012-07-27 15:08:16 +0100252static enum pool_mode get_pool_mode(struct pool *pool);
Joe Thornberb5330652013-12-04 19:51:33 -0500253static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100254
Joe Thornber991d9fa2011-10-31 20:21:18 +0000255/*
256 * Target context for a pool.
257 */
258struct pool_c {
259 struct dm_target *ti;
260 struct pool *pool;
261 struct dm_dev *data_dev;
262 struct dm_dev *metadata_dev;
263 struct dm_target_callbacks callbacks;
264
265 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100266 struct pool_features requested_pf; /* Features requested during table load */
267 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000268};
269
270/*
271 * Target context for a thin.
272 */
273struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400274 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000275 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100276 struct dm_dev *origin_dev;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100277 sector_t origin_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000278 dm_thin_id dev_id;
279
280 struct pool *pool;
281 struct dm_thin_device *td;
Joe Thornber738211f2014-03-03 15:52:28 +0000282 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400283 spinlock_t lock;
284 struct bio_list deferred_bio_list;
285 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400286 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornberb10ebd32014-04-08 11:29:01 +0100287
288 /*
289 * Ensures the thin is not destroyed until the worker has finished
290 * iterating the active_thins list.
291 */
292 atomic_t refcount;
293 struct completion can_destroy;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000294};
295
296/*----------------------------------------------------------------*/
297
Joe Thornber025b9682013-03-01 22:45:50 +0000298/*
299 * wake_worker() is used when new work is queued and when pool_resume is
300 * ready to continue deferred IO processing.
301 */
302static void wake_worker(struct pool *pool)
303{
304 queue_work(pool->wq, &pool->worker);
305}
306
307/*----------------------------------------------------------------*/
308
Joe Thornber6beca5e2013-03-01 22:45:50 +0000309static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
310 struct dm_bio_prison_cell **cell_result)
311{
312 int r;
313 struct dm_bio_prison_cell *cell_prealloc;
314
315 /*
316 * Allocate a cell from the prison's mempool.
317 * This might block but it can't fail.
318 */
319 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
320
321 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
322 if (r)
323 /*
324 * We reused an old cell; we can get rid of
325 * the new one.
326 */
327 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
328
329 return r;
330}
331
332static void cell_release(struct pool *pool,
333 struct dm_bio_prison_cell *cell,
334 struct bio_list *bios)
335{
336 dm_cell_release(pool->prison, cell, bios);
337 dm_bio_prison_free_cell(pool->prison, cell);
338}
339
340static void cell_release_no_holder(struct pool *pool,
341 struct dm_bio_prison_cell *cell,
342 struct bio_list *bios)
343{
344 dm_cell_release_no_holder(pool->prison, cell, bios);
345 dm_bio_prison_free_cell(pool->prison, cell);
346}
347
Joe Thornber025b9682013-03-01 22:45:50 +0000348static void cell_defer_no_holder_no_free(struct thin_c *tc,
349 struct dm_bio_prison_cell *cell)
350{
351 struct pool *pool = tc->pool;
352 unsigned long flags;
353
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400354 spin_lock_irqsave(&tc->lock, flags);
355 dm_cell_release_no_holder(pool->prison, cell, &tc->deferred_bio_list);
356 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber025b9682013-03-01 22:45:50 +0000357
358 wake_worker(pool);
359}
360
Mike Snitzeraf918052014-05-22 14:32:51 -0400361static void cell_error_with_code(struct pool *pool,
362 struct dm_bio_prison_cell *cell, int error_code)
Joe Thornber6beca5e2013-03-01 22:45:50 +0000363{
Mike Snitzeraf918052014-05-22 14:32:51 -0400364 dm_cell_error(pool->prison, cell, error_code);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000365 dm_bio_prison_free_cell(pool->prison, cell);
366}
367
Mike Snitzeraf918052014-05-22 14:32:51 -0400368static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
369{
370 cell_error_with_code(pool, cell, -EIO);
371}
372
Joe Thornber6beca5e2013-03-01 22:45:50 +0000373/*----------------------------------------------------------------*/
374
Joe Thornber991d9fa2011-10-31 20:21:18 +0000375/*
376 * A global list of pools that uses a struct mapped_device as a key.
377 */
378static struct dm_thin_pool_table {
379 struct mutex mutex;
380 struct list_head pools;
381} dm_thin_pool_table;
382
383static void pool_table_init(void)
384{
385 mutex_init(&dm_thin_pool_table.mutex);
386 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
387}
388
389static void __pool_table_insert(struct pool *pool)
390{
391 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
392 list_add(&pool->list, &dm_thin_pool_table.pools);
393}
394
395static void __pool_table_remove(struct pool *pool)
396{
397 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
398 list_del(&pool->list);
399}
400
401static struct pool *__pool_table_lookup(struct mapped_device *md)
402{
403 struct pool *pool = NULL, *tmp;
404
405 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
406
407 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
408 if (tmp->pool_md == md) {
409 pool = tmp;
410 break;
411 }
412 }
413
414 return pool;
415}
416
417static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
418{
419 struct pool *pool = NULL, *tmp;
420
421 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
422
423 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
424 if (tmp->md_dev == md_dev) {
425 pool = tmp;
426 break;
427 }
428 }
429
430 return pool;
431}
432
433/*----------------------------------------------------------------*/
434
Mike Snitzera24c2562012-06-03 00:30:00 +0100435struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100436 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100437 struct dm_deferred_entry *shared_read_entry;
438 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100439 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400440 struct rb_node rb_node;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100441};
442
Joe Thornber18adc572014-03-03 15:46:42 +0000443static void requeue_bio_list(struct thin_c *tc, struct bio_list *master)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000444{
445 struct bio *bio;
446 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000447 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000448
449 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000450
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400451 spin_lock_irqsave(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000452 bio_list_merge(&bios, master);
453 bio_list_init(master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400454 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000455
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400456 while ((bio = bio_list_pop(&bios)))
457 bio_endio(bio, DM_ENDIO_REQUEUE);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000458}
459
460static void requeue_io(struct thin_c *tc)
461{
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400462 requeue_bio_list(tc, &tc->deferred_bio_list);
463 requeue_bio_list(tc, &tc->retry_on_resume_list);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000464}
465
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400466static void error_thin_retry_list(struct thin_c *tc)
Joe Thornber3e1a0692014-03-03 16:03:26 +0000467{
468 struct bio *bio;
469 unsigned long flags;
470 struct bio_list bios;
471
472 bio_list_init(&bios);
473
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400474 spin_lock_irqsave(&tc->lock, flags);
475 bio_list_merge(&bios, &tc->retry_on_resume_list);
476 bio_list_init(&tc->retry_on_resume_list);
477 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000478
479 while ((bio = bio_list_pop(&bios)))
480 bio_io_error(bio);
481}
482
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400483static void error_retry_list(struct pool *pool)
484{
485 struct thin_c *tc;
486
487 rcu_read_lock();
488 list_for_each_entry_rcu(tc, &pool->active_thins, list)
489 error_thin_retry_list(tc);
490 rcu_read_unlock();
491}
492
Joe Thornber991d9fa2011-10-31 20:21:18 +0000493/*
494 * This section of code contains the logic for processing a thin device's IO.
495 * Much of the code depends on pool object resources (lists, workqueues, etc)
496 * but most is exclusively called from the thin target rather than the thin-pool
497 * target.
498 */
499
Mike Snitzer58f77a22013-03-01 22:45:45 +0000500static bool block_size_is_power_of_two(struct pool *pool)
501{
502 return pool->sectors_per_block_shift >= 0;
503}
504
Joe Thornber991d9fa2011-10-31 20:21:18 +0000505static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
506{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000507 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700508 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100509
Mike Snitzer58f77a22013-03-01 22:45:45 +0000510 if (block_size_is_power_of_two(pool))
511 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100512 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000513 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100514
515 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000516}
517
518static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
519{
520 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700521 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000522
523 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000524 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700525 bio->bi_iter.bi_sector =
526 (block << pool->sectors_per_block_shift) |
527 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000528 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700529 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000530 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000531}
532
Joe Thornber2dd9c252012-03-28 18:41:28 +0100533static void remap_to_origin(struct thin_c *tc, struct bio *bio)
534{
535 bio->bi_bdev = tc->origin_dev->bdev;
536}
537
Joe Thornber4afdd682012-07-27 15:08:14 +0100538static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
539{
540 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
541 dm_thin_changed_this_transaction(tc->td);
542}
543
Joe Thornbere8088072012-12-21 20:23:31 +0000544static void inc_all_io_entry(struct pool *pool, struct bio *bio)
545{
546 struct dm_thin_endio_hook *h;
547
548 if (bio->bi_rw & REQ_DISCARD)
549 return;
550
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000551 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000552 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
553}
554
Joe Thornber2dd9c252012-03-28 18:41:28 +0100555static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000556{
557 struct pool *pool = tc->pool;
558 unsigned long flags;
559
Joe Thornbere49e5822012-07-27 15:08:16 +0100560 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000561 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100562 return;
563 }
564
565 /*
566 * Complete bio with an error if earlier I/O caused changes to
567 * the metadata that can't be committed e.g, due to I/O errors
568 * on the metadata device.
569 */
570 if (dm_thin_aborted_changes(tc->td)) {
571 bio_io_error(bio);
572 return;
573 }
574
575 /*
576 * Batch together any bios that trigger commits and then issue a
577 * single commit for them in process_deferred_bios().
578 */
579 spin_lock_irqsave(&pool->lock, flags);
580 bio_list_add(&pool->deferred_flush_bios, bio);
581 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000582}
583
Joe Thornber2dd9c252012-03-28 18:41:28 +0100584static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
585{
586 remap_to_origin(tc, bio);
587 issue(tc, bio);
588}
589
590static void remap_and_issue(struct thin_c *tc, struct bio *bio,
591 dm_block_t block)
592{
593 remap(tc, bio, block);
594 issue(tc, bio);
595}
596
Joe Thornber991d9fa2011-10-31 20:21:18 +0000597/*----------------------------------------------------------------*/
598
599/*
600 * Bio endio functions.
601 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100602struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000603 struct list_head list;
604
Mike Snitzer7f214662013-12-17 13:43:31 -0500605 bool pass_discard:1;
606 bool definitely_not_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000607
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100608 /*
609 * Track quiescing, copying and zeroing preparation actions. When this
610 * counter hits zero the block is prepared and can be inserted into the
611 * btree.
612 */
613 atomic_t prepare_actions;
614
Mike Snitzer7f214662013-12-17 13:43:31 -0500615 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000616 struct thin_c *tc;
617 dm_block_t virt_block;
618 dm_block_t data_block;
Mike Snitzera24c2562012-06-03 00:30:00 +0100619 struct dm_bio_prison_cell *cell, *cell2;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000620
621 /*
622 * If the bio covers the whole area of a block then we can avoid
623 * zeroing or copying. Instead this bio is hooked. The bio will
624 * still be in the cell, so care has to be taken to avoid issuing
625 * the bio twice.
626 */
627 struct bio *bio;
628 bio_end_io_t *saved_bi_end_io;
629};
630
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100631static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000632{
633 struct pool *pool = m->tc->pool;
634
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100635 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500636 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000637 wake_worker(pool);
638 }
639}
640
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100641static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000642{
643 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000644 struct pool *pool = m->tc->pool;
645
Joe Thornber991d9fa2011-10-31 20:21:18 +0000646 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100647 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000648 spin_unlock_irqrestore(&pool->lock, flags);
649}
650
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100651static void copy_complete(int read_err, unsigned long write_err, void *context)
652{
653 struct dm_thin_new_mapping *m = context;
654
655 m->err = read_err || write_err ? -EIO : 0;
656 complete_mapping_preparation(m);
657}
658
Joe Thornber991d9fa2011-10-31 20:21:18 +0000659static void overwrite_endio(struct bio *bio, int err)
660{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000661 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100662 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000663
664 m->err = err;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100665 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000666}
667
Joe Thornber991d9fa2011-10-31 20:21:18 +0000668/*----------------------------------------------------------------*/
669
670/*
671 * Workqueue.
672 */
673
674/*
675 * Prepared mapping jobs.
676 */
677
678/*
679 * This sends the bios in the cell back to the deferred_bios list.
680 */
Joe Thornber2aab3852012-12-21 20:23:33 +0000681static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000682{
683 struct pool *pool = tc->pool;
684 unsigned long flags;
685
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400686 spin_lock_irqsave(&tc->lock, flags);
687 cell_release(pool, cell, &tc->deferred_bio_list);
688 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000689
690 wake_worker(pool);
691}
692
693/*
Joe Thornber6beca5e2013-03-01 22:45:50 +0000694 * Same as cell_defer above, except it omits the original holder of the cell.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000695 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000696static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000697{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000698 struct pool *pool = tc->pool;
699 unsigned long flags;
700
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400701 spin_lock_irqsave(&tc->lock, flags);
702 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
703 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000704
705 wake_worker(pool);
706}
707
Joe Thornbere49e5822012-07-27 15:08:16 +0100708static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
709{
Kent Overstreet196d38b2013-11-23 18:34:15 -0800710 if (m->bio) {
Joe Thornbere49e5822012-07-27 15:08:16 +0100711 m->bio->bi_end_io = m->saved_bi_end_io;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800712 atomic_inc(&m->bio->bi_remaining);
713 }
Joe Thornber6beca5e2013-03-01 22:45:50 +0000714 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100715 list_del(&m->list);
716 mempool_free(m, m->tc->pool->mapping_pool);
717}
Joe Thornber025b9682013-03-01 22:45:50 +0000718
Mike Snitzera24c2562012-06-03 00:30:00 +0100719static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000720{
721 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000722 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000723 struct bio *bio;
724 int r;
725
726 bio = m->bio;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800727 if (bio) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000728 bio->bi_end_io = m->saved_bi_end_io;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800729 atomic_inc(&bio->bi_remaining);
730 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000731
732 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000733 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100734 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000735 }
736
737 /*
738 * Commit the prepared block into the mapping btree.
739 * Any I/O for this block arriving after this point will get
740 * remapped to it directly.
741 */
742 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
743 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500744 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000745 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100746 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000747 }
748
749 /*
750 * Release any bios held while the block was being provisioned.
751 * If we are processing a write bio that completely covers the block,
752 * we already processed it so can ignore it now when processing
753 * the bios in the cell.
754 */
755 if (bio) {
Joe Thornberf286ba02012-12-21 20:23:33 +0000756 cell_defer_no_holder(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000757 bio_endio(bio, 0);
758 } else
Joe Thornber2aab3852012-12-21 20:23:33 +0000759 cell_defer(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000760
Joe Thornber905386f2012-07-27 15:08:05 +0100761out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000762 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000763 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000764}
765
Joe Thornbere49e5822012-07-27 15:08:16 +0100766static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100767{
Joe Thornber104655f2012-03-28 18:41:28 +0100768 struct thin_c *tc = m->tc;
769
Joe Thornbere49e5822012-07-27 15:08:16 +0100770 bio_io_error(m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000771 cell_defer_no_holder(tc, m->cell);
772 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere49e5822012-07-27 15:08:16 +0100773 mempool_free(m, tc->pool->mapping_pool);
774}
Joe Thornber104655f2012-03-28 18:41:28 +0100775
Joe Thornbere49e5822012-07-27 15:08:16 +0100776static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
777{
778 struct thin_c *tc = m->tc;
779
Joe Thornbere8088072012-12-21 20:23:31 +0000780 inc_all_io_entry(tc->pool, m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000781 cell_defer_no_holder(tc, m->cell);
782 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere8088072012-12-21 20:23:31 +0000783
Joe Thornber104655f2012-03-28 18:41:28 +0100784 if (m->pass_discard)
Joe Thornber19fa1a62013-12-17 12:09:40 -0500785 if (m->definitely_not_shared)
786 remap_and_issue(tc, m->bio, m->data_block);
787 else {
788 bool used = false;
789 if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used)
790 bio_endio(m->bio, 0);
791 else
792 remap_and_issue(tc, m->bio, m->data_block);
793 }
Joe Thornber104655f2012-03-28 18:41:28 +0100794 else
795 bio_endio(m->bio, 0);
796
Joe Thornber104655f2012-03-28 18:41:28 +0100797 mempool_free(m, tc->pool->mapping_pool);
798}
799
Joe Thornbere49e5822012-07-27 15:08:16 +0100800static void process_prepared_discard(struct dm_thin_new_mapping *m)
801{
802 int r;
803 struct thin_c *tc = m->tc;
804
805 r = dm_thin_remove_block(tc->td, m->virt_block);
806 if (r)
Mike Snitzerc3977412012-12-21 20:23:34 +0000807 DMERR_LIMIT("dm_thin_remove_block() failed");
Joe Thornbere49e5822012-07-27 15:08:16 +0100808
809 process_prepared_discard_passdown(m);
810}
811
Joe Thornber104655f2012-03-28 18:41:28 +0100812static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +0100813 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000814{
815 unsigned long flags;
816 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +0100817 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000818
819 INIT_LIST_HEAD(&maps);
820 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100821 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000822 spin_unlock_irqrestore(&pool->lock, flags);
823
824 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +0100825 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000826}
827
828/*
829 * Deferred bio jobs.
830 */
Joe Thornber104655f2012-03-28 18:41:28 +0100831static int io_overlaps_block(struct pool *pool, struct bio *bio)
832{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700833 return bio->bi_iter.bi_size ==
834 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +0100835}
836
Joe Thornber991d9fa2011-10-31 20:21:18 +0000837static int io_overwrites_block(struct pool *pool, struct bio *bio)
838{
Joe Thornber104655f2012-03-28 18:41:28 +0100839 return (bio_data_dir(bio) == WRITE) &&
840 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000841}
842
843static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
844 bio_end_io_t *fn)
845{
846 *save = bio->bi_end_io;
847 bio->bi_end_io = fn;
848}
849
850static int ensure_next_mapping(struct pool *pool)
851{
852 if (pool->next_mapping)
853 return 0;
854
855 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
856
857 return pool->next_mapping ? 0 : -ENOMEM;
858}
859
Mike Snitzera24c2562012-06-03 00:30:00 +0100860static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000861{
Mike Snitzer16961b02013-12-17 13:19:11 -0500862 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000863
864 BUG_ON(!pool->next_mapping);
865
Mike Snitzer16961b02013-12-17 13:19:11 -0500866 memset(m, 0, sizeof(struct dm_thin_new_mapping));
867 INIT_LIST_HEAD(&m->list);
868 m->bio = NULL;
869
Joe Thornber991d9fa2011-10-31 20:21:18 +0000870 pool->next_mapping = NULL;
871
Mike Snitzer16961b02013-12-17 13:19:11 -0500872 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000873}
874
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100875static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
876 sector_t begin, sector_t end)
877{
878 int r;
879 struct dm_io_region to;
880
881 to.bdev = tc->pool_dev->bdev;
882 to.sector = begin;
883 to.count = end - begin;
884
885 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
886 if (r < 0) {
887 DMERR_LIMIT("dm_kcopyd_zero() failed");
888 copy_complete(1, 1, m);
889 }
890}
891
892/*
893 * A partial copy also needs to zero the uncopied region.
894 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000895static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +0100896 struct dm_dev *origin, dm_block_t data_origin,
897 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100898 struct dm_bio_prison_cell *cell, struct bio *bio,
899 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000900{
901 int r;
902 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100903 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000904
Joe Thornber991d9fa2011-10-31 20:21:18 +0000905 m->tc = tc;
906 m->virt_block = virt_block;
907 m->data_block = data_dest;
908 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000909
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100910 /*
911 * quiesce action + copy action + an extra reference held for the
912 * duration of this function (we may need to inc later for a
913 * partial zero).
914 */
915 atomic_set(&m->prepare_actions, 3);
916
Mike Snitzer44feb382012-10-12 21:02:10 +0100917 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100918 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000919
920 /*
921 * IO to pool_dev remaps to the pool target's data_dev.
922 *
923 * If the whole block of data is being overwritten, we can issue the
924 * bio immediately. Otherwise we use kcopyd to clone the data first.
925 */
926 if (io_overwrites_block(pool, bio)) {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000927 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100928
Joe Thornbereb2aa482012-03-28 18:41:28 +0100929 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000930 m->bio = bio;
931 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornbere8088072012-12-21 20:23:31 +0000932 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000933 remap_and_issue(tc, bio, data_dest);
934 } else {
935 struct dm_io_region from, to;
936
Joe Thornber2dd9c252012-03-28 18:41:28 +0100937 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000938 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100939 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000940
941 to.bdev = tc->pool_dev->bdev;
942 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100943 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000944
945 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
946 0, copy_complete, m);
947 if (r < 0) {
Mike Snitzerc3977412012-12-21 20:23:34 +0000948 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100949 copy_complete(1, 1, m);
950
951 /*
952 * We allow the zero to be issued, to simplify the
953 * error path. Otherwise we'd need to start
954 * worrying about decrementing the prepare_actions
955 * counter.
956 */
957 }
958
959 /*
960 * Do we need to zero a tail region?
961 */
962 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
963 atomic_inc(&m->prepare_actions);
964 ll_zero(tc, m,
965 data_dest * pool->sectors_per_block + len,
966 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000967 }
968 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100969
970 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000971}
972
Joe Thornber2dd9c252012-03-28 18:41:28 +0100973static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
974 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100975 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100976{
977 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100978 data_origin, data_dest, cell, bio,
979 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +0100980}
981
Joe Thornber991d9fa2011-10-31 20:21:18 +0000982static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +0100983 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +0000984 struct bio *bio)
985{
986 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100987 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000988
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100989 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000990 m->tc = tc;
991 m->virt_block = virt_block;
992 m->data_block = data_block;
993 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000994
995 /*
996 * If the whole block of data is being overwritten or we are not
997 * zeroing pre-existing data, we can issue the bio immediately.
998 * Otherwise we use kcopyd to zero the data first.
999 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001000 if (!pool->pf.zero_new_blocks)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001001 process_prepared_mapping(m);
1002
1003 else if (io_overwrites_block(pool, bio)) {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001004 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +01001005
Joe Thornbereb2aa482012-03-28 18:41:28 +01001006 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001007 m->bio = bio;
1008 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornbere8088072012-12-21 20:23:31 +00001009 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001010 remap_and_issue(tc, bio, data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001011
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001012 } else
1013 ll_zero(tc, m,
1014 data_block * pool->sectors_per_block,
1015 (data_block + 1) * pool->sectors_per_block);
1016}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001017
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001018static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1019 dm_block_t data_dest,
1020 struct dm_bio_prison_cell *cell, struct bio *bio)
1021{
1022 struct pool *pool = tc->pool;
1023 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1024 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1025
1026 if (virt_block_end <= tc->origin_size)
1027 schedule_copy(tc, virt_block, tc->origin_dev,
1028 virt_block, data_dest, cell, bio,
1029 pool->sectors_per_block);
1030
1031 else if (virt_block_begin < tc->origin_size)
1032 schedule_copy(tc, virt_block, tc->origin_dev,
1033 virt_block, data_dest, cell, bio,
1034 tc->origin_size - virt_block_begin);
1035
1036 else
1037 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001038}
1039
Joe Thornbere49e5822012-07-27 15:08:16 +01001040/*
1041 * A non-zero return indicates read_only or fail_io mode.
1042 * Many callers don't care about the return value.
1043 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001044static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001045{
1046 int r;
1047
Joe Thornber8d07e8a2014-05-06 16:28:14 +01001048 if (get_pool_mode(pool) >= PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01001049 return -EINVAL;
1050
Joe Thornber020cc3b2013-12-04 15:05:36 -05001051 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001052 if (r)
1053 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornbere49e5822012-07-27 15:08:16 +01001054
1055 return r;
1056}
1057
Joe Thornber88a66212013-12-04 20:16:12 -05001058static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1059{
1060 unsigned long flags;
1061
1062 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1063 DMWARN("%s: reached low water mark for data device: sending event.",
1064 dm_device_name(pool->pool_md));
1065 spin_lock_irqsave(&pool->lock, flags);
1066 pool->low_water_triggered = true;
1067 spin_unlock_irqrestore(&pool->lock, flags);
1068 dm_table_event(pool->ti->table);
1069 }
1070}
1071
Joe Thornber3e1a0692014-03-03 16:03:26 +00001072static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1073
Joe Thornber991d9fa2011-10-31 20:21:18 +00001074static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1075{
1076 int r;
1077 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001078 struct pool *pool = tc->pool;
1079
Joe Thornber3e1a0692014-03-03 16:03:26 +00001080 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001081 return -EINVAL;
1082
Joe Thornber991d9fa2011-10-31 20:21:18 +00001083 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001084 if (r) {
1085 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001086 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001087 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001088
Joe Thornber88a66212013-12-04 20:16:12 -05001089 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001090
1091 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001092 /*
1093 * Try to commit to see if that will free up some
1094 * more space.
1095 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001096 r = commit(pool);
1097 if (r)
1098 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001099
1100 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001101 if (r) {
1102 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001103 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001104 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001105
Mike Snitzer94563ba2013-08-22 09:56:18 -04001106 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001107 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001108 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001109 }
1110 }
1111
1112 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001113 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05001114 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001115 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001116 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001117
1118 return 0;
1119}
1120
1121/*
1122 * If we have run out of space, queue bios until the device is
1123 * resumed, presumably after having been reloaded with more space.
1124 */
1125static void retry_on_resume(struct bio *bio)
1126{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001127 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001128 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001129 unsigned long flags;
1130
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001131 spin_lock_irqsave(&tc->lock, flags);
1132 bio_list_add(&tc->retry_on_resume_list, bio);
1133 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001134}
1135
Mike Snitzeraf918052014-05-22 14:32:51 -04001136static int should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001137{
1138 enum pool_mode m = get_pool_mode(pool);
1139
1140 switch (m) {
1141 case PM_WRITE:
1142 /* Shouldn't get here */
1143 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001144 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001145
1146 case PM_OUT_OF_DATA_SPACE:
Mike Snitzeraf918052014-05-22 14:32:51 -04001147 return pool->pf.error_if_no_space ? -ENOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001148
1149 case PM_READ_ONLY:
1150 case PM_FAIL:
Mike Snitzeraf918052014-05-22 14:32:51 -04001151 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001152 default:
1153 /* Shouldn't get here */
1154 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001155 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001156 }
1157}
1158
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001159static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1160{
Mike Snitzeraf918052014-05-22 14:32:51 -04001161 int error = should_error_unserviceable_bio(pool);
1162
1163 if (error)
1164 bio_endio(bio, error);
Mike Snitzer6d162022013-12-20 18:09:02 -05001165 else
1166 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001167}
1168
Mike Snitzer399cadd2013-12-05 16:03:33 -05001169static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001170{
1171 struct bio *bio;
1172 struct bio_list bios;
Mike Snitzeraf918052014-05-22 14:32:51 -04001173 int error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001174
Mike Snitzeraf918052014-05-22 14:32:51 -04001175 error = should_error_unserviceable_bio(pool);
1176 if (error) {
1177 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001178 return;
1179 }
1180
Joe Thornber991d9fa2011-10-31 20:21:18 +00001181 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001182 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001183
Mike Snitzeraf918052014-05-22 14:32:51 -04001184 error = should_error_unserviceable_bio(pool);
1185 if (error)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001186 while ((bio = bio_list_pop(&bios)))
Mike Snitzeraf918052014-05-22 14:32:51 -04001187 bio_endio(bio, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001188 else
1189 while ((bio = bio_list_pop(&bios)))
1190 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001191}
1192
Joe Thornber104655f2012-03-28 18:41:28 +01001193static void process_discard(struct thin_c *tc, struct bio *bio)
1194{
1195 int r;
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01001196 unsigned long flags;
Joe Thornber104655f2012-03-28 18:41:28 +01001197 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001198 struct dm_bio_prison_cell *cell, *cell2;
Mike Snitzer44feb382012-10-12 21:02:10 +01001199 struct dm_cell_key key, key2;
Joe Thornber104655f2012-03-28 18:41:28 +01001200 dm_block_t block = get_bio_block(tc, bio);
1201 struct dm_thin_lookup_result lookup_result;
Mike Snitzera24c2562012-06-03 00:30:00 +01001202 struct dm_thin_new_mapping *m;
Joe Thornber104655f2012-03-28 18:41:28 +01001203
1204 build_virtual_key(tc->td, block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001205 if (bio_detain(tc->pool, &key, bio, &cell))
Joe Thornber104655f2012-03-28 18:41:28 +01001206 return;
1207
1208 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1209 switch (r) {
1210 case 0:
1211 /*
1212 * Check nobody is fiddling with this pool block. This can
1213 * happen if someone's in the process of breaking sharing
1214 * on this block.
1215 */
1216 build_data_key(tc->td, lookup_result.block, &key2);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001217 if (bio_detain(tc->pool, &key2, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +00001218 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001219 break;
1220 }
1221
1222 if (io_overlaps_block(pool, bio)) {
1223 /*
1224 * IO may still be going to the destination block. We must
1225 * quiesce before we can do the removal.
1226 */
1227 m = get_next_mapping(pool);
1228 m->tc = tc;
Joe Thornber19fa1a62013-12-17 12:09:40 -05001229 m->pass_discard = pool->pf.discard_passdown;
1230 m->definitely_not_shared = !lookup_result.shared;
Joe Thornber104655f2012-03-28 18:41:28 +01001231 m->virt_block = block;
1232 m->data_block = lookup_result.block;
1233 m->cell = cell;
1234 m->cell2 = cell2;
Joe Thornber104655f2012-03-28 18:41:28 +01001235 m->bio = bio;
1236
Mike Snitzer44feb382012-10-12 21:02:10 +01001237 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01001238 spin_lock_irqsave(&pool->lock, flags);
Mike Snitzerdaec3382013-12-11 14:01:20 -05001239 list_add_tail(&m->list, &pool->prepared_discards);
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01001240 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01001241 wake_worker(pool);
1242 }
1243 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001244 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001245 cell_defer_no_holder(tc, cell);
1246 cell_defer_no_holder(tc, cell2);
Joe Thornbere8088072012-12-21 20:23:31 +00001247
Joe Thornber104655f2012-03-28 18:41:28 +01001248 /*
Mikulas Patocka49296302012-07-27 15:08:03 +01001249 * The DM core makes sure that the discard doesn't span
1250 * a block boundary. So we submit the discard of a
1251 * partial block appropriately.
Joe Thornber104655f2012-03-28 18:41:28 +01001252 */
Mikulas Patocka650d2a02012-07-20 14:25:05 +01001253 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1254 remap_and_issue(tc, bio, lookup_result.block);
1255 else
1256 bio_endio(bio, 0);
Joe Thornber104655f2012-03-28 18:41:28 +01001257 }
1258 break;
1259
1260 case -ENODATA:
1261 /*
1262 * It isn't provisioned, just forget it.
1263 */
Joe Thornberf286ba02012-12-21 20:23:33 +00001264 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001265 bio_endio(bio, 0);
1266 break;
1267
1268 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001269 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1270 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001271 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001272 bio_io_error(bio);
1273 break;
1274 }
1275}
1276
Joe Thornber991d9fa2011-10-31 20:21:18 +00001277static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001278 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001279 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001280 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001281{
1282 int r;
1283 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001284 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001285
1286 r = alloc_data_block(tc, &data_block);
1287 switch (r) {
1288 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001289 schedule_internal_copy(tc, block, lookup_result->block,
1290 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001291 break;
1292
1293 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001294 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001295 break;
1296
1297 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001298 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1299 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001300 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001301 break;
1302 }
1303}
1304
1305static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1306 dm_block_t block,
1307 struct dm_thin_lookup_result *lookup_result)
1308{
Mike Snitzera24c2562012-06-03 00:30:00 +01001309 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001310 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001311 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001312
1313 /*
1314 * If cell is already occupied, then sharing is already in the process
1315 * of being broken so we have nothing further to do here.
1316 */
1317 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001318 if (bio_detain(pool, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001319 return;
1320
Kent Overstreet4f024f32013-10-11 15:44:27 -07001321 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001322 break_sharing(tc, bio, block, &key, lookup_result, cell);
1323 else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001324 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001325
Mike Snitzer44feb382012-10-12 21:02:10 +01001326 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001327 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001328 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001329
Joe Thornber991d9fa2011-10-31 20:21:18 +00001330 remap_and_issue(tc, bio, lookup_result->block);
1331 }
1332}
1333
1334static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001335 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001336{
1337 int r;
1338 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001339 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001340
1341 /*
1342 * Remap empty bios (flushes) immediately, without provisioning.
1343 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001344 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001345 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001346 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001347
Joe Thornber991d9fa2011-10-31 20:21:18 +00001348 remap_and_issue(tc, bio, 0);
1349 return;
1350 }
1351
1352 /*
1353 * Fill read bios with zeroes and complete them immediately.
1354 */
1355 if (bio_data_dir(bio) == READ) {
1356 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001357 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001358 bio_endio(bio, 0);
1359 return;
1360 }
1361
1362 r = alloc_data_block(tc, &data_block);
1363 switch (r) {
1364 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001365 if (tc->origin_dev)
1366 schedule_external_copy(tc, block, data_block, cell, bio);
1367 else
1368 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001369 break;
1370
1371 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001372 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001373 break;
1374
1375 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001376 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1377 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001378 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001379 break;
1380 }
1381}
1382
1383static void process_bio(struct thin_c *tc, struct bio *bio)
1384{
1385 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001386 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001387 dm_block_t block = get_bio_block(tc, bio);
Mike Snitzera24c2562012-06-03 00:30:00 +01001388 struct dm_bio_prison_cell *cell;
Mike Snitzer44feb382012-10-12 21:02:10 +01001389 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001390 struct dm_thin_lookup_result lookup_result;
1391
1392 /*
1393 * If cell is already occupied, then the block is already
1394 * being provisioned so we have nothing further to do here.
1395 */
1396 build_virtual_key(tc->td, block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001397 if (bio_detain(pool, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001398 return;
1399
1400 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1401 switch (r) {
1402 case 0:
Joe Thornbere8088072012-12-21 20:23:31 +00001403 if (lookup_result.shared) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001404 process_shared_bio(tc, bio, block, &lookup_result);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001405 cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */
Joe Thornbere8088072012-12-21 20:23:31 +00001406 } else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001407 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001408 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001409
Joe Thornber991d9fa2011-10-31 20:21:18 +00001410 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001411 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001412 break;
1413
1414 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001415 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001416 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001417 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001418
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001419 if (bio_end_sector(bio) <= tc->origin_size)
1420 remap_to_origin_and_issue(tc, bio);
1421
1422 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1423 zero_fill_bio(bio);
1424 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1425 remap_to_origin_and_issue(tc, bio);
1426
1427 } else {
1428 zero_fill_bio(bio);
1429 bio_endio(bio, 0);
1430 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001431 } else
1432 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001433 break;
1434
1435 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001436 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1437 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001438 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001439 bio_io_error(bio);
1440 break;
1441 }
1442}
1443
Joe Thornbere49e5822012-07-27 15:08:16 +01001444static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1445{
1446 int r;
1447 int rw = bio_data_dir(bio);
1448 dm_block_t block = get_bio_block(tc, bio);
1449 struct dm_thin_lookup_result lookup_result;
1450
1451 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1452 switch (r) {
1453 case 0:
Kent Overstreet4f024f32013-10-11 15:44:27 -07001454 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size)
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001455 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere8088072012-12-21 20:23:31 +00001456 else {
1457 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001458 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001459 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001460 break;
1461
1462 case -ENODATA:
1463 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001464 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001465 break;
1466 }
1467
1468 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001469 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001470 remap_to_origin_and_issue(tc, bio);
1471 break;
1472 }
1473
1474 zero_fill_bio(bio);
1475 bio_endio(bio, 0);
1476 break;
1477
1478 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001479 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1480 __func__, r);
Joe Thornbere49e5822012-07-27 15:08:16 +01001481 bio_io_error(bio);
1482 break;
1483 }
1484}
1485
Joe Thornber3e1a0692014-03-03 16:03:26 +00001486static void process_bio_success(struct thin_c *tc, struct bio *bio)
1487{
1488 bio_endio(bio, 0);
1489}
1490
Joe Thornbere49e5822012-07-27 15:08:16 +01001491static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1492{
1493 bio_io_error(bio);
1494}
1495
Joe Thornberac8c3f32013-05-10 14:37:21 +01001496/*
1497 * FIXME: should we also commit due to size of transaction, measured in
1498 * metadata blocks?
1499 */
Joe Thornber905e51b2012-03-28 18:41:27 +01001500static int need_commit_due_to_time(struct pool *pool)
1501{
1502 return jiffies < pool->last_commit_jiffies ||
1503 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1504}
1505
Mike Snitzer67324ea2014-03-21 18:33:41 -04001506#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1507#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1508
1509static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1510{
1511 struct rb_node **rbp, *parent;
1512 struct dm_thin_endio_hook *pbd;
1513 sector_t bi_sector = bio->bi_iter.bi_sector;
1514
1515 rbp = &tc->sort_bio_list.rb_node;
1516 parent = NULL;
1517 while (*rbp) {
1518 parent = *rbp;
1519 pbd = thin_pbd(parent);
1520
1521 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1522 rbp = &(*rbp)->rb_left;
1523 else
1524 rbp = &(*rbp)->rb_right;
1525 }
1526
1527 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1528 rb_link_node(&pbd->rb_node, parent, rbp);
1529 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
1530}
1531
1532static void __extract_sorted_bios(struct thin_c *tc)
1533{
1534 struct rb_node *node;
1535 struct dm_thin_endio_hook *pbd;
1536 struct bio *bio;
1537
1538 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
1539 pbd = thin_pbd(node);
1540 bio = thin_bio(pbd);
1541
1542 bio_list_add(&tc->deferred_bio_list, bio);
1543 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
1544 }
1545
1546 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
1547}
1548
1549static void __sort_thin_deferred_bios(struct thin_c *tc)
1550{
1551 struct bio *bio;
1552 struct bio_list bios;
1553
1554 bio_list_init(&bios);
1555 bio_list_merge(&bios, &tc->deferred_bio_list);
1556 bio_list_init(&tc->deferred_bio_list);
1557
1558 /* Sort deferred_bio_list using rb-tree */
1559 while ((bio = bio_list_pop(&bios)))
1560 __thin_bio_rb_add(tc, bio);
1561
1562 /*
1563 * Transfer the sorted bios in sort_bio_list back to
1564 * deferred_bio_list to allow lockless submission of
1565 * all bios.
1566 */
1567 __extract_sorted_bios(tc);
1568}
1569
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001570static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001571{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001572 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001573 unsigned long flags;
1574 struct bio *bio;
1575 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04001576 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001577 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001578
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001579 if (tc->requeue_mode) {
1580 requeue_bio_list(tc, &tc->deferred_bio_list);
1581 return;
1582 }
1583
Joe Thornber991d9fa2011-10-31 20:21:18 +00001584 bio_list_init(&bios);
1585
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001586 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001587
1588 if (bio_list_empty(&tc->deferred_bio_list)) {
1589 spin_unlock_irqrestore(&tc->lock, flags);
1590 return;
1591 }
1592
1593 __sort_thin_deferred_bios(tc);
1594
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001595 bio_list_merge(&bios, &tc->deferred_bio_list);
1596 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001597
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001598 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001599
Mike Snitzer67324ea2014-03-21 18:33:41 -04001600 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001601 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001602 /*
1603 * If we've got no free new_mapping structs, and processing
1604 * this bio might require one, we pause until there are some
1605 * prepared mappings to process.
1606 */
1607 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001608 spin_lock_irqsave(&tc->lock, flags);
1609 bio_list_add(&tc->deferred_bio_list, bio);
1610 bio_list_merge(&tc->deferred_bio_list, &bios);
1611 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001612 break;
1613 }
Joe Thornber104655f2012-03-28 18:41:28 +01001614
1615 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01001616 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01001617 else
Joe Thornbere49e5822012-07-27 15:08:16 +01001618 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001619
1620 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01001621 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001622 dm_pool_issue_prefetches(pool->pmd);
1623 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001624 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04001625 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001626}
1627
Joe Thornberb10ebd32014-04-08 11:29:01 +01001628static void thin_get(struct thin_c *tc);
1629static void thin_put(struct thin_c *tc);
1630
1631/*
1632 * We can't hold rcu_read_lock() around code that can block. So we
1633 * find a thin with the rcu lock held; bump a refcount; then drop
1634 * the lock.
1635 */
1636static struct thin_c *get_first_thin(struct pool *pool)
1637{
1638 struct thin_c *tc = NULL;
1639
1640 rcu_read_lock();
1641 if (!list_empty(&pool->active_thins)) {
1642 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
1643 thin_get(tc);
1644 }
1645 rcu_read_unlock();
1646
1647 return tc;
1648}
1649
1650static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
1651{
1652 struct thin_c *old_tc = tc;
1653
1654 rcu_read_lock();
1655 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
1656 thin_get(tc);
1657 thin_put(old_tc);
1658 rcu_read_unlock();
1659 return tc;
1660 }
1661 thin_put(old_tc);
1662 rcu_read_unlock();
1663
1664 return NULL;
1665}
1666
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001667static void process_deferred_bios(struct pool *pool)
1668{
1669 unsigned long flags;
1670 struct bio *bio;
1671 struct bio_list bios;
1672 struct thin_c *tc;
1673
Joe Thornberb10ebd32014-04-08 11:29:01 +01001674 tc = get_first_thin(pool);
1675 while (tc) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001676 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01001677 tc = get_next_thin(pool, tc);
1678 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001679
1680 /*
1681 * If there are any deferred flush bios, we must commit
1682 * the metadata before issuing them.
1683 */
1684 bio_list_init(&bios);
1685 spin_lock_irqsave(&pool->lock, flags);
1686 bio_list_merge(&bios, &pool->deferred_flush_bios);
1687 bio_list_init(&pool->deferred_flush_bios);
1688 spin_unlock_irqrestore(&pool->lock, flags);
1689
Mike Snitzer4d1662a2014-02-06 06:08:56 -05001690 if (bio_list_empty(&bios) &&
1691 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001692 return;
1693
Joe Thornber020cc3b2013-12-04 15:05:36 -05001694 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001695 while ((bio = bio_list_pop(&bios)))
1696 bio_io_error(bio);
1697 return;
1698 }
Joe Thornber905e51b2012-03-28 18:41:27 +01001699 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001700
1701 while ((bio = bio_list_pop(&bios)))
1702 generic_make_request(bio);
1703}
1704
1705static void do_worker(struct work_struct *ws)
1706{
1707 struct pool *pool = container_of(ws, struct pool, worker);
1708
Joe Thornber7d327fe2014-10-06 15:45:59 +01001709 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001710 dm_pool_issue_prefetches(pool->pmd);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001711 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01001712 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001713 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01001714 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001715 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001716 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001717 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001718}
1719
Joe Thornber905e51b2012-03-28 18:41:27 +01001720/*
1721 * We want to commit periodically so that not too much
1722 * unwritten data builds up.
1723 */
1724static void do_waker(struct work_struct *ws)
1725{
1726 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1727 wake_worker(pool);
1728 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1729}
1730
Joe Thornber85ad6432014-05-09 15:59:38 +01001731/*
1732 * We're holding onto IO to allow userland time to react. After the
1733 * timeout either the pool will have been resized (and thus back in
1734 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
1735 */
1736static void do_no_space_timeout(struct work_struct *ws)
1737{
1738 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
1739 no_space_timeout);
1740
1741 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
1742 set_pool_mode(pool, PM_READ_ONLY);
1743}
1744
Joe Thornber991d9fa2011-10-31 20:21:18 +00001745/*----------------------------------------------------------------*/
1746
Joe Thornbere7a3e872014-05-13 16:14:14 -04001747struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00001748 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04001749 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00001750};
1751
Joe Thornbere7a3e872014-05-13 16:14:14 -04001752static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00001753{
Joe Thornbere7a3e872014-05-13 16:14:14 -04001754 return container_of(ws, struct pool_work, worker);
1755}
1756
1757static void pool_work_complete(struct pool_work *pw)
1758{
1759 complete(&pw->complete);
1760}
1761
1762static void pool_work_wait(struct pool_work *pw, struct pool *pool,
1763 void (*fn)(struct work_struct *))
1764{
1765 INIT_WORK_ONSTACK(&pw->worker, fn);
1766 init_completion(&pw->complete);
1767 queue_work(pool->wq, &pw->worker);
1768 wait_for_completion(&pw->complete);
1769}
1770
1771/*----------------------------------------------------------------*/
1772
1773struct noflush_work {
1774 struct pool_work pw;
1775 struct thin_c *tc;
1776};
1777
1778static struct noflush_work *to_noflush(struct work_struct *ws)
1779{
1780 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00001781}
1782
1783static void do_noflush_start(struct work_struct *ws)
1784{
Joe Thornbere7a3e872014-05-13 16:14:14 -04001785 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00001786 w->tc->requeue_mode = true;
1787 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04001788 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00001789}
1790
1791static void do_noflush_stop(struct work_struct *ws)
1792{
Joe Thornbere7a3e872014-05-13 16:14:14 -04001793 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00001794 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04001795 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00001796}
1797
1798static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
1799{
1800 struct noflush_work w;
1801
Joe Thornber738211f2014-03-03 15:52:28 +00001802 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04001803 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00001804}
1805
1806/*----------------------------------------------------------------*/
1807
Joe Thornbere49e5822012-07-27 15:08:16 +01001808static enum pool_mode get_pool_mode(struct pool *pool)
1809{
1810 return pool->pf.mode;
1811}
1812
Joe Thornber3e1a0692014-03-03 16:03:26 +00001813static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
1814{
1815 dm_table_event(pool->ti->table);
1816 DMINFO("%s: switching pool to %s mode",
1817 dm_device_name(pool->pool_md), new_mode);
1818}
1819
Mike Snitzer8b64e882013-12-20 14:27:28 -05001820static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01001821{
Mike Snitzercdc2b412014-02-14 18:10:55 -05001822 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001823 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
1824 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer80c57892014-05-20 13:38:33 -04001825 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001826
1827 /*
1828 * Never allow the pool to transition to PM_WRITE mode if user
1829 * intervention is required to verify metadata and data consistency.
1830 */
1831 if (new_mode == PM_WRITE && needs_check) {
1832 DMERR("%s: unable to switch pool to write mode until repaired.",
1833 dm_device_name(pool->pool_md));
1834 if (old_mode != new_mode)
1835 new_mode = old_mode;
1836 else
1837 new_mode = PM_READ_ONLY;
1838 }
1839 /*
1840 * If we were in PM_FAIL mode, rollback of metadata failed. We're
1841 * not going to recover without a thin_repair. So we never let the
1842 * pool move out of the old mode.
1843 */
1844 if (old_mode == PM_FAIL)
1845 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01001846
Mike Snitzer8b64e882013-12-20 14:27:28 -05001847 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01001848 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05001849 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001850 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05001851 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01001852 pool->process_bio = process_bio_fail;
1853 pool->process_discard = process_bio_fail;
1854 pool->process_prepared_mapping = process_prepared_mapping_fail;
1855 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001856
1857 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001858 break;
1859
1860 case PM_READ_ONLY:
Mike Snitzer8b64e882013-12-20 14:27:28 -05001861 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001862 notify_of_pool_mode_change(pool, "read-only");
1863 dm_pool_metadata_read_only(pool->pmd);
1864 pool->process_bio = process_bio_read_only;
1865 pool->process_discard = process_bio_success;
1866 pool->process_prepared_mapping = process_prepared_mapping_fail;
1867 pool->process_prepared_discard = process_prepared_discard_passdown;
1868
1869 error_retry_list(pool);
1870 break;
1871
1872 case PM_OUT_OF_DATA_SPACE:
1873 /*
1874 * Ideally we'd never hit this state; the low water mark
1875 * would trigger userland to extend the pool before we
1876 * completely run out of data space. However, many small
1877 * IOs to unprovisioned space can consume data space at an
1878 * alarming rate. Adjust your low water mark if you're
1879 * frequently seeing this mode.
1880 */
1881 if (old_mode != new_mode)
1882 notify_of_pool_mode_change(pool, "out-of-data-space");
1883 pool->process_bio = process_bio_read_only;
1884 pool->process_discard = process_discard;
1885 pool->process_prepared_mapping = process_prepared_mapping;
1886 pool->process_prepared_discard = process_prepared_discard_passdown;
Joe Thornber85ad6432014-05-09 15:59:38 +01001887
Mike Snitzer80c57892014-05-20 13:38:33 -04001888 if (!pool->pf.error_if_no_space && no_space_timeout)
1889 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01001890 break;
1891
1892 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05001893 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001894 notify_of_pool_mode_change(pool, "write");
Joe Thornber9b7aaa62013-12-04 16:58:19 -05001895 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01001896 pool->process_bio = process_bio;
1897 pool->process_discard = process_discard;
1898 pool->process_prepared_mapping = process_prepared_mapping;
1899 pool->process_prepared_discard = process_prepared_discard;
1900 break;
1901 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05001902
1903 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05001904 /*
1905 * The pool mode may have changed, sync it so bind_control_target()
1906 * doesn't cause an unexpected mode transition on resume.
1907 */
1908 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01001909}
1910
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001911static void abort_transaction(struct pool *pool)
1912{
1913 const char *dev_name = dm_device_name(pool->pool_md);
1914
1915 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
1916 if (dm_pool_abort_metadata(pool->pmd)) {
1917 DMERR("%s: failed to abort metadata transaction", dev_name);
1918 set_pool_mode(pool, PM_FAIL);
1919 }
1920
1921 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
1922 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
1923 set_pool_mode(pool, PM_FAIL);
1924 }
1925}
1926
Joe Thornberb5330652013-12-04 19:51:33 -05001927static void metadata_operation_failed(struct pool *pool, const char *op, int r)
1928{
1929 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
1930 dm_device_name(pool->pool_md), op, r);
1931
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001932 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05001933 set_pool_mode(pool, PM_READ_ONLY);
1934}
1935
Joe Thornbere49e5822012-07-27 15:08:16 +01001936/*----------------------------------------------------------------*/
1937
Joe Thornber991d9fa2011-10-31 20:21:18 +00001938/*
1939 * Mapping functions.
1940 */
1941
1942/*
1943 * Called only while mapping a thin bio to hand it over to the workqueue.
1944 */
1945static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1946{
1947 unsigned long flags;
1948 struct pool *pool = tc->pool;
1949
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001950 spin_lock_irqsave(&tc->lock, flags);
1951 bio_list_add(&tc->deferred_bio_list, bio);
1952 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001953
1954 wake_worker(pool);
1955}
1956
Joe Thornber7d327fe2014-10-06 15:45:59 +01001957static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
1958{
1959 struct pool *pool = tc->pool;
1960
1961 throttle_lock(&pool->throttle);
1962 thin_defer_bio(tc, bio);
1963 throttle_unlock(&pool->throttle);
1964}
1965
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001966static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01001967{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001968 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001969
1970 h->tc = tc;
1971 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00001972 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001973 h->overwrite_mapping = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001974}
1975
Joe Thornber991d9fa2011-10-31 20:21:18 +00001976/*
1977 * Non-blocking function called from the thin target's map function.
1978 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001979static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001980{
1981 int r;
1982 struct thin_c *tc = ti->private;
1983 dm_block_t block = get_bio_block(tc, bio);
1984 struct dm_thin_device *td = tc->td;
1985 struct dm_thin_lookup_result result;
Joe Thornber025b9682013-03-01 22:45:50 +00001986 struct dm_bio_prison_cell cell1, cell2;
1987 struct dm_bio_prison_cell *cell_result;
Joe Thornbere8088072012-12-21 20:23:31 +00001988 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001989
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001990 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001991
Joe Thornber738211f2014-03-03 15:52:28 +00001992 if (tc->requeue_mode) {
1993 bio_endio(bio, DM_ENDIO_REQUEUE);
1994 return DM_MAPIO_SUBMITTED;
1995 }
1996
Joe Thornbere49e5822012-07-27 15:08:16 +01001997 if (get_pool_mode(tc->pool) == PM_FAIL) {
1998 bio_io_error(bio);
1999 return DM_MAPIO_SUBMITTED;
2000 }
2001
Joe Thornber104655f2012-03-28 18:41:28 +01002002 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002003 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002004 return DM_MAPIO_SUBMITTED;
2005 }
2006
Joe Thornberc822ed962014-10-10 09:41:09 +01002007 /*
2008 * We must hold the virtual cell before doing the lookup, otherwise
2009 * there's a race with discard.
2010 */
2011 build_virtual_key(tc->td, block, &key);
2012 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1, &cell_result))
2013 return DM_MAPIO_SUBMITTED;
2014
Joe Thornber991d9fa2011-10-31 20:21:18 +00002015 r = dm_thin_find_block(td, block, 0, &result);
2016
2017 /*
2018 * Note that we defer readahead too.
2019 */
2020 switch (r) {
2021 case 0:
2022 if (unlikely(result.shared)) {
2023 /*
2024 * We have a race condition here between the
2025 * result.shared value returned by the lookup and
2026 * snapshot creation, which may cause new
2027 * sharing.
2028 *
2029 * To avoid this always quiesce the origin before
2030 * taking the snap. You want to do this anyway to
2031 * ensure a consistent application view
2032 * (i.e. lockfs).
2033 *
2034 * More distant ancestors are irrelevant. The
2035 * shared flag will be set in their case.
2036 */
2037 thin_defer_bio(tc, bio);
Joe Thornberc822ed962014-10-10 09:41:09 +01002038 cell_defer_no_holder_no_free(tc, &cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00002039 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002040 }
Joe Thornbere8088072012-12-21 20:23:31 +00002041
Joe Thornbere8088072012-12-21 20:23:31 +00002042 build_data_key(tc->td, result.block, &key);
Joe Thornber025b9682013-03-01 22:45:50 +00002043 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2, &cell_result)) {
2044 cell_defer_no_holder_no_free(tc, &cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00002045 return DM_MAPIO_SUBMITTED;
2046 }
2047
2048 inc_all_io_entry(tc->pool, bio);
Joe Thornber025b9682013-03-01 22:45:50 +00002049 cell_defer_no_holder_no_free(tc, &cell2);
2050 cell_defer_no_holder_no_free(tc, &cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00002051
2052 remap(tc, bio, result.block);
2053 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002054
2055 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002056 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
2057 /*
2058 * This block isn't provisioned, and we have no way
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05002059 * of doing so.
Joe Thornbere49e5822012-07-27 15:08:16 +01002060 */
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05002061 handle_unserviceable_bio(tc->pool, bio);
Joe Thornberc822ed962014-10-10 09:41:09 +01002062 cell_defer_no_holder_no_free(tc, &cell1);
Joe Thornber2aab3852012-12-21 20:23:33 +00002063 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002064 }
2065 /* fall through */
2066
2067 case -EWOULDBLOCK:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002068 thin_defer_bio(tc, bio);
Joe Thornberc822ed962014-10-10 09:41:09 +01002069 cell_defer_no_holder_no_free(tc, &cell1);
Joe Thornber2aab3852012-12-21 20:23:33 +00002070 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002071
2072 default:
2073 /*
2074 * Must always call bio_io_error on failure.
2075 * dm_thin_find_block can fail with -EINVAL if the
2076 * pool is switched to fail-io mode.
2077 */
2078 bio_io_error(bio);
Joe Thornberc822ed962014-10-10 09:41:09 +01002079 cell_defer_no_holder_no_free(tc, &cell1);
Joe Thornber2aab3852012-12-21 20:23:33 +00002080 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002081 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002082}
2083
2084static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2085{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002086 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04002087 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002088
Mike Snitzer760fe672014-03-20 08:36:47 -04002089 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2090 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002091
Mike Snitzer760fe672014-03-20 08:36:47 -04002092 q = bdev_get_queue(pt->data_dev->bdev);
2093 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002094}
2095
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002096static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002097{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002098 unsigned long flags;
2099 struct thin_c *tc;
2100
2101 rcu_read_lock();
2102 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2103 spin_lock_irqsave(&tc->lock, flags);
2104 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2105 bio_list_init(&tc->retry_on_resume_list);
2106 spin_unlock_irqrestore(&tc->lock, flags);
2107 }
2108 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002109}
2110
2111/*----------------------------------------------------------------
2112 * Binding of control targets to a pool object
2113 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002114static bool data_dev_supports_discard(struct pool_c *pt)
2115{
2116 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2117
2118 return q && blk_queue_discard(q);
2119}
2120
Joe Thornber58051b92013-03-20 17:21:25 +00002121static bool is_factor(sector_t block_size, uint32_t n)
2122{
2123 return !sector_div(block_size, n);
2124}
2125
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002126/*
2127 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002128 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002129 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002130static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002131{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002132 struct pool *pool = pt->pool;
2133 struct block_device *data_bdev = pt->data_dev->bdev;
2134 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
2135 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
2136 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002137 char buf[BDEVNAME_SIZE];
2138
Mike Snitzer0424caa2012-09-26 23:45:47 +01002139 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002140 return;
2141
Mike Snitzer0424caa2012-09-26 23:45:47 +01002142 if (!data_dev_supports_discard(pt))
2143 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002144
Mike Snitzer0424caa2012-09-26 23:45:47 +01002145 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2146 reason = "max discard sectors smaller than a block";
2147
2148 else if (data_limits->discard_granularity > block_size)
2149 reason = "discard granularity larger than a block";
2150
Joe Thornber58051b92013-03-20 17:21:25 +00002151 else if (!is_factor(block_size, data_limits->discard_granularity))
Mike Snitzer0424caa2012-09-26 23:45:47 +01002152 reason = "discard granularity not a factor of block size";
2153
2154 if (reason) {
2155 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2156 pt->adjusted_pf.discard_passdown = false;
2157 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002158}
2159
Joe Thornber991d9fa2011-10-31 20:21:18 +00002160static int bind_control_target(struct pool *pool, struct dm_target *ti)
2161{
2162 struct pool_c *pt = ti->private;
2163
Joe Thornbere49e5822012-07-27 15:08:16 +01002164 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002165 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002166 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002167 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002168 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002169
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002170 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002171 * Don't change the pool's mode until set_pool_mode() below.
2172 * Otherwise the pool's process_* function pointers may
2173 * not match the desired pool mode.
2174 */
2175 pt->adjusted_pf.mode = old_mode;
2176
2177 pool->ti = ti;
2178 pool->pf = pt->adjusted_pf;
2179 pool->low_water_blocks = pt->low_water_blocks;
2180
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002181 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002182
Joe Thornber991d9fa2011-10-31 20:21:18 +00002183 return 0;
2184}
2185
2186static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2187{
2188 if (pool->ti == ti)
2189 pool->ti = NULL;
2190}
2191
2192/*----------------------------------------------------------------
2193 * Pool creation
2194 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002195/* Initialize pool features. */
2196static void pool_features_init(struct pool_features *pf)
2197{
Joe Thornbere49e5822012-07-27 15:08:16 +01002198 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002199 pf->zero_new_blocks = true;
2200 pf->discard_enabled = true;
2201 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002202 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002203}
2204
Joe Thornber991d9fa2011-10-31 20:21:18 +00002205static void __pool_destroy(struct pool *pool)
2206{
2207 __pool_table_remove(pool);
2208
2209 if (dm_pool_metadata_close(pool->pmd) < 0)
2210 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2211
Mike Snitzer44feb382012-10-12 21:02:10 +01002212 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002213 dm_kcopyd_client_destroy(pool->copier);
2214
2215 if (pool->wq)
2216 destroy_workqueue(pool->wq);
2217
2218 if (pool->next_mapping)
2219 mempool_free(pool->next_mapping, pool->mapping_pool);
2220 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01002221 dm_deferred_set_destroy(pool->shared_read_ds);
2222 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002223 kfree(pool);
2224}
2225
Mike Snitzera24c2562012-06-03 00:30:00 +01002226static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002227
Joe Thornber991d9fa2011-10-31 20:21:18 +00002228static struct pool *pool_create(struct mapped_device *pool_md,
2229 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002230 unsigned long block_size,
2231 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002232{
2233 int r;
2234 void *err_p;
2235 struct pool *pool;
2236 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002237 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002238
Joe Thornbere49e5822012-07-27 15:08:16 +01002239 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002240 if (IS_ERR(pmd)) {
2241 *error = "Error creating metadata object";
2242 return (struct pool *)pmd;
2243 }
2244
2245 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2246 if (!pool) {
2247 *error = "Error allocating memory for pool";
2248 err_p = ERR_PTR(-ENOMEM);
2249 goto bad_pool;
2250 }
2251
2252 pool->pmd = pmd;
2253 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002254 if (block_size & (block_size - 1))
2255 pool->sectors_per_block_shift = -1;
2256 else
2257 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002258 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002259 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002260 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002261 if (!pool->prison) {
2262 *error = "Error creating pool's bio prison";
2263 err_p = ERR_PTR(-ENOMEM);
2264 goto bad_prison;
2265 }
2266
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002267 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002268 if (IS_ERR(pool->copier)) {
2269 r = PTR_ERR(pool->copier);
2270 *error = "Error creating pool's kcopyd client";
2271 err_p = ERR_PTR(r);
2272 goto bad_kcopyd_client;
2273 }
2274
2275 /*
2276 * Create singlethreaded workqueue that will service all devices
2277 * that use this metadata.
2278 */
2279 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2280 if (!pool->wq) {
2281 *error = "Error creating pool's workqueue";
2282 err_p = ERR_PTR(-ENOMEM);
2283 goto bad_wq;
2284 }
2285
Joe Thornber7d327fe2014-10-06 15:45:59 +01002286 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002287 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002288 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad6432014-05-09 15:59:38 +01002289 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002290 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002291 bio_list_init(&pool->deferred_flush_bios);
2292 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002293 INIT_LIST_HEAD(&pool->prepared_discards);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002294 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002295 pool->low_water_triggered = false;
Mike Snitzer44feb382012-10-12 21:02:10 +01002296
2297 pool->shared_read_ds = dm_deferred_set_create();
2298 if (!pool->shared_read_ds) {
2299 *error = "Error creating pool's shared read deferred set";
2300 err_p = ERR_PTR(-ENOMEM);
2301 goto bad_shared_read_ds;
2302 }
2303
2304 pool->all_io_ds = dm_deferred_set_create();
2305 if (!pool->all_io_ds) {
2306 *error = "Error creating pool's all io deferred set";
2307 err_p = ERR_PTR(-ENOMEM);
2308 goto bad_all_io_ds;
2309 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002310
2311 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002312 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2313 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002314 if (!pool->mapping_pool) {
2315 *error = "Error creating pool's mapping mempool";
2316 err_p = ERR_PTR(-ENOMEM);
2317 goto bad_mapping_pool;
2318 }
2319
Joe Thornber991d9fa2011-10-31 20:21:18 +00002320 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01002321 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002322 pool->pool_md = pool_md;
2323 pool->md_dev = metadata_dev;
2324 __pool_table_insert(pool);
2325
2326 return pool;
2327
Joe Thornber991d9fa2011-10-31 20:21:18 +00002328bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01002329 dm_deferred_set_destroy(pool->all_io_ds);
2330bad_all_io_ds:
2331 dm_deferred_set_destroy(pool->shared_read_ds);
2332bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002333 destroy_workqueue(pool->wq);
2334bad_wq:
2335 dm_kcopyd_client_destroy(pool->copier);
2336bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01002337 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002338bad_prison:
2339 kfree(pool);
2340bad_pool:
2341 if (dm_pool_metadata_close(pmd))
2342 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2343
2344 return err_p;
2345}
2346
2347static void __pool_inc(struct pool *pool)
2348{
2349 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2350 pool->ref_count++;
2351}
2352
2353static void __pool_dec(struct pool *pool)
2354{
2355 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2356 BUG_ON(!pool->ref_count);
2357 if (!--pool->ref_count)
2358 __pool_destroy(pool);
2359}
2360
2361static struct pool *__pool_find(struct mapped_device *pool_md,
2362 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002363 unsigned long block_size, int read_only,
2364 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002365{
2366 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2367
2368 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002369 if (pool->pool_md != pool_md) {
2370 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002371 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002372 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002373 __pool_inc(pool);
2374
2375 } else {
2376 pool = __pool_table_lookup(pool_md);
2377 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002378 if (pool->md_dev != metadata_dev) {
2379 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002380 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002381 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002382 __pool_inc(pool);
2383
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002384 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01002385 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002386 *created = 1;
2387 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002388 }
2389
2390 return pool;
2391}
2392
2393/*----------------------------------------------------------------
2394 * Pool target methods
2395 *--------------------------------------------------------------*/
2396static void pool_dtr(struct dm_target *ti)
2397{
2398 struct pool_c *pt = ti->private;
2399
2400 mutex_lock(&dm_thin_pool_table.mutex);
2401
2402 unbind_control_target(pt->pool, ti);
2403 __pool_dec(pt->pool);
2404 dm_put_device(ti, pt->metadata_dev);
2405 dm_put_device(ti, pt->data_dev);
2406 kfree(pt);
2407
2408 mutex_unlock(&dm_thin_pool_table.mutex);
2409}
2410
Joe Thornber991d9fa2011-10-31 20:21:18 +00002411static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2412 struct dm_target *ti)
2413{
2414 int r;
2415 unsigned argc;
2416 const char *arg_name;
2417
2418 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05002419 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002420 };
2421
2422 /*
2423 * No feature arguments supplied.
2424 */
2425 if (!as->argc)
2426 return 0;
2427
2428 r = dm_read_arg_group(_args, as, &argc, &ti->error);
2429 if (r)
2430 return -EINVAL;
2431
2432 while (argc && !r) {
2433 arg_name = dm_shift_arg(as);
2434 argc--;
2435
Joe Thornbere49e5822012-07-27 15:08:16 +01002436 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002437 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002438
Joe Thornbere49e5822012-07-27 15:08:16 +01002439 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002440 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002441
2442 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002443 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002444
2445 else if (!strcasecmp(arg_name, "read_only"))
2446 pf->mode = PM_READ_ONLY;
2447
Mike Snitzer787a996c2013-12-06 16:21:43 -05002448 else if (!strcasecmp(arg_name, "error_if_no_space"))
2449 pf->error_if_no_space = true;
2450
Joe Thornbere49e5822012-07-27 15:08:16 +01002451 else {
2452 ti->error = "Unrecognised pool feature requested";
2453 r = -EINVAL;
2454 break;
2455 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002456 }
2457
2458 return r;
2459}
2460
Joe Thornberac8c3f32013-05-10 14:37:21 +01002461static void metadata_low_callback(void *context)
2462{
2463 struct pool *pool = context;
2464
2465 DMWARN("%s: reached low water mark for metadata device: sending event.",
2466 dm_device_name(pool->pool_md));
2467
2468 dm_table_event(pool->ti->table);
2469}
2470
Mike Snitzer7d489352014-02-12 23:58:15 -05002471static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01002472{
Mike Snitzer7d489352014-02-12 23:58:15 -05002473 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
2474}
2475
2476static void warn_if_metadata_device_too_big(struct block_device *bdev)
2477{
2478 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01002479 char buffer[BDEVNAME_SIZE];
2480
Mike Snitzer7d489352014-02-12 23:58:15 -05002481 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01002482 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2483 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05002484}
2485
2486static sector_t get_metadata_dev_size(struct block_device *bdev)
2487{
2488 sector_t metadata_dev_size = get_dev_size(bdev);
2489
2490 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
2491 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01002492
2493 return metadata_dev_size;
2494}
2495
Joe Thornber24347e92013-05-10 14:37:19 +01002496static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
2497{
2498 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
2499
Mike Snitzer7d489352014-02-12 23:58:15 -05002500 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01002501
2502 return metadata_dev_size;
2503}
2504
Joe Thornber991d9fa2011-10-31 20:21:18 +00002505/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01002506 * When a metadata threshold is crossed a dm event is triggered, and
2507 * userland should respond by growing the metadata device. We could let
2508 * userland set the threshold, like we do with the data threshold, but I'm
2509 * not sure they know enough to do this well.
2510 */
2511static dm_block_t calc_metadata_threshold(struct pool_c *pt)
2512{
2513 /*
2514 * 4M is ample for all ops with the possible exception of thin
2515 * device deletion which is harmless if it fails (just retry the
2516 * delete after you've grown the device).
2517 */
2518 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
2519 return min((dm_block_t)1024ULL /* 4M */, quarter);
2520}
2521
2522/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00002523 * thin-pool <metadata dev> <data dev>
2524 * <data block size (sectors)>
2525 * <low water mark (blocks)>
2526 * [<#feature args> [<arg>]*]
2527 *
2528 * Optional feature arguments are:
2529 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002530 * ignore_discard: disable discard
2531 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05002532 * read_only: Don't allow any changes to be made to the pool metadata.
2533 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002534 */
2535static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
2536{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002537 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002538 struct pool_c *pt;
2539 struct pool *pool;
2540 struct pool_features pf;
2541 struct dm_arg_set as;
2542 struct dm_dev *data_dev;
2543 unsigned long block_size;
2544 dm_block_t low_water_blocks;
2545 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01002546 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002547
2548 /*
2549 * FIXME Remove validation from scope of lock.
2550 */
2551 mutex_lock(&dm_thin_pool_table.mutex);
2552
2553 if (argc < 4) {
2554 ti->error = "Invalid argument count";
2555 r = -EINVAL;
2556 goto out_unlock;
2557 }
Joe Thornber5d0db962013-05-10 14:37:19 +01002558
Joe Thornber991d9fa2011-10-31 20:21:18 +00002559 as.argc = argc;
2560 as.argv = argv;
2561
Joe Thornber5d0db962013-05-10 14:37:19 +01002562 /*
2563 * Set default pool features.
2564 */
2565 pool_features_init(&pf);
2566
2567 dm_consume_args(&as, 4);
2568 r = parse_pool_features(&as, &pf, ti);
2569 if (r)
2570 goto out_unlock;
2571
2572 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
2573 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002574 if (r) {
2575 ti->error = "Error opening metadata block device";
2576 goto out_unlock;
2577 }
Mike Snitzer7d489352014-02-12 23:58:15 -05002578 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002579
2580 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
2581 if (r) {
2582 ti->error = "Error getting data device";
2583 goto out_metadata;
2584 }
2585
2586 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
2587 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2588 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002589 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002590 ti->error = "Invalid block size";
2591 r = -EINVAL;
2592 goto out;
2593 }
2594
2595 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
2596 ti->error = "Invalid low water mark";
2597 r = -EINVAL;
2598 goto out;
2599 }
2600
Joe Thornber991d9fa2011-10-31 20:21:18 +00002601 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
2602 if (!pt) {
2603 r = -ENOMEM;
2604 goto out;
2605 }
2606
2607 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002608 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002609 if (IS_ERR(pool)) {
2610 r = PTR_ERR(pool);
2611 goto out_free_pt;
2612 }
2613
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002614 /*
2615 * 'pool_created' reflects whether this is the first table load.
2616 * Top level discard support is not allowed to be changed after
2617 * initial load. This would require a pool reload to trigger thin
2618 * device changes.
2619 */
2620 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
2621 ti->error = "Discard support cannot be disabled once enabled";
2622 r = -EINVAL;
2623 goto out_flags_changed;
2624 }
2625
Joe Thornber991d9fa2011-10-31 20:21:18 +00002626 pt->pool = pool;
2627 pt->ti = ti;
2628 pt->metadata_dev = metadata_dev;
2629 pt->data_dev = data_dev;
2630 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002631 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002632 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002633
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002634 /*
2635 * Only need to enable discards if the pool should pass
2636 * them down to the data device. The thin device's discard
2637 * processing will cause mappings to be removed from the btree.
2638 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04002639 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002640 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002641 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002642
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002643 /*
2644 * Setting 'discards_supported' circumvents the normal
2645 * stacking of discard limits (this keeps the pool and
2646 * thin devices' discard limits consistent).
2647 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002648 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002649 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002650 ti->private = pt;
2651
Joe Thornberac8c3f32013-05-10 14:37:21 +01002652 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
2653 calc_metadata_threshold(pt),
2654 metadata_low_callback,
2655 pool);
2656 if (r)
2657 goto out_free_pt;
2658
Joe Thornber991d9fa2011-10-31 20:21:18 +00002659 pt->callbacks.congested_fn = pool_is_congested;
2660 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
2661
2662 mutex_unlock(&dm_thin_pool_table.mutex);
2663
2664 return 0;
2665
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002666out_flags_changed:
2667 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002668out_free_pt:
2669 kfree(pt);
2670out:
2671 dm_put_device(ti, data_dev);
2672out_metadata:
2673 dm_put_device(ti, metadata_dev);
2674out_unlock:
2675 mutex_unlock(&dm_thin_pool_table.mutex);
2676
2677 return r;
2678}
2679
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002680static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002681{
2682 int r;
2683 struct pool_c *pt = ti->private;
2684 struct pool *pool = pt->pool;
2685 unsigned long flags;
2686
2687 /*
2688 * As this is a singleton target, ti->begin is always zero.
2689 */
2690 spin_lock_irqsave(&pool->lock, flags);
2691 bio->bi_bdev = pt->data_dev->bdev;
2692 r = DM_MAPIO_REMAPPED;
2693 spin_unlock_irqrestore(&pool->lock, flags);
2694
2695 return r;
2696}
2697
Joe Thornberb17446d2013-05-10 14:37:18 +01002698static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
2699{
2700 int r;
2701 struct pool_c *pt = ti->private;
2702 struct pool *pool = pt->pool;
2703 sector_t data_size = ti->len;
2704 dm_block_t sb_data_size;
2705
2706 *need_commit = false;
2707
2708 (void) sector_div(data_size, pool->sectors_per_block);
2709
2710 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2711 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002712 DMERR("%s: failed to retrieve data device size",
2713 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01002714 return r;
2715 }
2716
2717 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002718 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
2719 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01002720 (unsigned long long)data_size, sb_data_size);
2721 return -EINVAL;
2722
2723 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002724 if (dm_pool_metadata_needs_check(pool->pmd)) {
2725 DMERR("%s: unable to grow the data device until repaired.",
2726 dm_device_name(pool->pool_md));
2727 return 0;
2728 }
2729
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05002730 if (sb_data_size)
2731 DMINFO("%s: growing the data device from %llu to %llu blocks",
2732 dm_device_name(pool->pool_md),
2733 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01002734 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2735 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05002736 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01002737 return r;
2738 }
2739
2740 *need_commit = true;
2741 }
2742
2743 return 0;
2744}
2745
Joe Thornber24347e92013-05-10 14:37:19 +01002746static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
2747{
2748 int r;
2749 struct pool_c *pt = ti->private;
2750 struct pool *pool = pt->pool;
2751 dm_block_t metadata_dev_size, sb_metadata_dev_size;
2752
2753 *need_commit = false;
2754
Alasdair G Kergon610bba82013-05-19 18:57:50 +01002755 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01002756
2757 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
2758 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002759 DMERR("%s: failed to retrieve metadata device size",
2760 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01002761 return r;
2762 }
2763
2764 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002765 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
2766 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01002767 metadata_dev_size, sb_metadata_dev_size);
2768 return -EINVAL;
2769
2770 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002771 if (dm_pool_metadata_needs_check(pool->pmd)) {
2772 DMERR("%s: unable to grow the metadata device until repaired.",
2773 dm_device_name(pool->pool_md));
2774 return 0;
2775 }
2776
Mike Snitzer7d489352014-02-12 23:58:15 -05002777 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05002778 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
2779 dm_device_name(pool->pool_md),
2780 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber24347e92013-05-10 14:37:19 +01002781 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
2782 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05002783 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01002784 return r;
2785 }
2786
2787 *need_commit = true;
2788 }
2789
2790 return 0;
2791}
2792
Joe Thornber991d9fa2011-10-31 20:21:18 +00002793/*
2794 * Retrieves the number of blocks of the data device from
2795 * the superblock and compares it to the actual device size,
2796 * thus resizing the data device in case it has grown.
2797 *
2798 * This both copes with opening preallocated data devices in the ctr
2799 * being followed by a resume
2800 * -and-
2801 * calling the resume method individually after userspace has
2802 * grown the data device in reaction to a table event.
2803 */
2804static int pool_preresume(struct dm_target *ti)
2805{
2806 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01002807 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002808 struct pool_c *pt = ti->private;
2809 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002810
2811 /*
2812 * Take control of the pool object.
2813 */
2814 r = bind_control_target(pool, ti);
2815 if (r)
2816 return r;
2817
Joe Thornberb17446d2013-05-10 14:37:18 +01002818 r = maybe_resize_data_dev(ti, &need_commit1);
2819 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002820 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002821
Joe Thornber24347e92013-05-10 14:37:19 +01002822 r = maybe_resize_metadata_dev(ti, &need_commit2);
2823 if (r)
2824 return r;
2825
2826 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05002827 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002828
2829 return 0;
2830}
2831
2832static void pool_resume(struct dm_target *ti)
2833{
2834 struct pool_c *pt = ti->private;
2835 struct pool *pool = pt->pool;
2836 unsigned long flags;
2837
2838 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05002839 pool->low_water_triggered = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002840 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002841 requeue_bios(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002842
Joe Thornber905e51b2012-03-28 18:41:27 +01002843 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002844}
2845
2846static void pool_postsuspend(struct dm_target *ti)
2847{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002848 struct pool_c *pt = ti->private;
2849 struct pool *pool = pt->pool;
2850
Joe Thornber905e51b2012-03-28 18:41:27 +01002851 cancel_delayed_work(&pool->waker);
Joe Thornber85ad6432014-05-09 15:59:38 +01002852 cancel_delayed_work(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002853 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05002854 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002855}
2856
2857static int check_arg_count(unsigned argc, unsigned args_required)
2858{
2859 if (argc != args_required) {
2860 DMWARN("Message received with %u arguments instead of %u.",
2861 argc, args_required);
2862 return -EINVAL;
2863 }
2864
2865 return 0;
2866}
2867
2868static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
2869{
2870 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
2871 *dev_id <= MAX_DEV_ID)
2872 return 0;
2873
2874 if (warning)
2875 DMWARN("Message received with invalid device id: %s", arg);
2876
2877 return -EINVAL;
2878}
2879
2880static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
2881{
2882 dm_thin_id dev_id;
2883 int r;
2884
2885 r = check_arg_count(argc, 2);
2886 if (r)
2887 return r;
2888
2889 r = read_dev_id(argv[1], &dev_id, 1);
2890 if (r)
2891 return r;
2892
2893 r = dm_pool_create_thin(pool->pmd, dev_id);
2894 if (r) {
2895 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2896 argv[1]);
2897 return r;
2898 }
2899
2900 return 0;
2901}
2902
2903static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2904{
2905 dm_thin_id dev_id;
2906 dm_thin_id origin_dev_id;
2907 int r;
2908
2909 r = check_arg_count(argc, 3);
2910 if (r)
2911 return r;
2912
2913 r = read_dev_id(argv[1], &dev_id, 1);
2914 if (r)
2915 return r;
2916
2917 r = read_dev_id(argv[2], &origin_dev_id, 1);
2918 if (r)
2919 return r;
2920
2921 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
2922 if (r) {
2923 DMWARN("Creation of new snapshot %s of device %s failed.",
2924 argv[1], argv[2]);
2925 return r;
2926 }
2927
2928 return 0;
2929}
2930
2931static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2932{
2933 dm_thin_id dev_id;
2934 int r;
2935
2936 r = check_arg_count(argc, 2);
2937 if (r)
2938 return r;
2939
2940 r = read_dev_id(argv[1], &dev_id, 1);
2941 if (r)
2942 return r;
2943
2944 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2945 if (r)
2946 DMWARN("Deletion of thin device %s failed.", argv[1]);
2947
2948 return r;
2949}
2950
2951static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2952{
2953 dm_thin_id old_id, new_id;
2954 int r;
2955
2956 r = check_arg_count(argc, 3);
2957 if (r)
2958 return r;
2959
2960 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2961 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2962 return -EINVAL;
2963 }
2964
2965 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2966 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2967 return -EINVAL;
2968 }
2969
2970 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2971 if (r) {
2972 DMWARN("Failed to change transaction id from %s to %s.",
2973 argv[1], argv[2]);
2974 return r;
2975 }
2976
2977 return 0;
2978}
2979
Joe Thornbercc8394d2012-06-03 00:30:01 +01002980static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2981{
2982 int r;
2983
2984 r = check_arg_count(argc, 1);
2985 if (r)
2986 return r;
2987
Joe Thornber020cc3b2013-12-04 15:05:36 -05002988 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01002989
Joe Thornbercc8394d2012-06-03 00:30:01 +01002990 r = dm_pool_reserve_metadata_snap(pool->pmd);
2991 if (r)
2992 DMWARN("reserve_metadata_snap message failed.");
2993
2994 return r;
2995}
2996
2997static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2998{
2999 int r;
3000
3001 r = check_arg_count(argc, 1);
3002 if (r)
3003 return r;
3004
3005 r = dm_pool_release_metadata_snap(pool->pmd);
3006 if (r)
3007 DMWARN("release_metadata_snap message failed.");
3008
3009 return r;
3010}
3011
Joe Thornber991d9fa2011-10-31 20:21:18 +00003012/*
3013 * Messages supported:
3014 * create_thin <dev_id>
3015 * create_snap <dev_id> <origin_id>
3016 * delete <dev_id>
3017 * trim <dev_id> <new_size_in_sectors>
3018 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003019 * reserve_metadata_snap
3020 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003021 */
3022static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3023{
3024 int r = -EINVAL;
3025 struct pool_c *pt = ti->private;
3026 struct pool *pool = pt->pool;
3027
3028 if (!strcasecmp(argv[0], "create_thin"))
3029 r = process_create_thin_mesg(argc, argv, pool);
3030
3031 else if (!strcasecmp(argv[0], "create_snap"))
3032 r = process_create_snap_mesg(argc, argv, pool);
3033
3034 else if (!strcasecmp(argv[0], "delete"))
3035 r = process_delete_mesg(argc, argv, pool);
3036
3037 else if (!strcasecmp(argv[0], "set_transaction_id"))
3038 r = process_set_transaction_id_mesg(argc, argv, pool);
3039
Joe Thornbercc8394d2012-06-03 00:30:01 +01003040 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3041 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3042
3043 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3044 r = process_release_metadata_snap_mesg(argc, argv, pool);
3045
Joe Thornber991d9fa2011-10-31 20:21:18 +00003046 else
3047 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3048
Joe Thornbere49e5822012-07-27 15:08:16 +01003049 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003050 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003051
3052 return r;
3053}
3054
Joe Thornbere49e5822012-07-27 15:08:16 +01003055static void emit_flags(struct pool_features *pf, char *result,
3056 unsigned sz, unsigned maxlen)
3057{
3058 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003059 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3060 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003061 DMEMIT("%u ", count);
3062
3063 if (!pf->zero_new_blocks)
3064 DMEMIT("skip_block_zeroing ");
3065
3066 if (!pf->discard_enabled)
3067 DMEMIT("ignore_discard ");
3068
3069 if (!pf->discard_passdown)
3070 DMEMIT("no_discard_passdown ");
3071
3072 if (pf->mode == PM_READ_ONLY)
3073 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003074
3075 if (pf->error_if_no_space)
3076 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003077}
3078
Joe Thornber991d9fa2011-10-31 20:21:18 +00003079/*
3080 * Status line is:
3081 * <transaction id> <used metadata sectors>/<total metadata sectors>
3082 * <used data sectors>/<total data sectors> <held metadata root>
3083 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003084static void pool_status(struct dm_target *ti, status_type_t type,
3085 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003086{
Joe Thornbere49e5822012-07-27 15:08:16 +01003087 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003088 unsigned sz = 0;
3089 uint64_t transaction_id;
3090 dm_block_t nr_free_blocks_data;
3091 dm_block_t nr_free_blocks_metadata;
3092 dm_block_t nr_blocks_data;
3093 dm_block_t nr_blocks_metadata;
3094 dm_block_t held_root;
3095 char buf[BDEVNAME_SIZE];
3096 char buf2[BDEVNAME_SIZE];
3097 struct pool_c *pt = ti->private;
3098 struct pool *pool = pt->pool;
3099
3100 switch (type) {
3101 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003102 if (get_pool_mode(pool) == PM_FAIL) {
3103 DMEMIT("Fail");
3104 break;
3105 }
3106
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003107 /* Commit to ensure statistics aren't out-of-date */
3108 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003109 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003110
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003111 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3112 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003113 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3114 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003115 goto err;
3116 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003117
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003118 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3119 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003120 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3121 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003122 goto err;
3123 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003124
3125 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003126 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003127 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3128 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003129 goto err;
3130 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003131
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003132 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3133 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003134 DMERR("%s: dm_pool_get_free_block_count returned %d",
3135 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003136 goto err;
3137 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003138
3139 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003140 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003141 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3142 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003143 goto err;
3144 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003145
Joe Thornbercc8394d2012-06-03 00:30:01 +01003146 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003147 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003148 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3149 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003150 goto err;
3151 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003152
3153 DMEMIT("%llu %llu/%llu %llu/%llu ",
3154 (unsigned long long)transaction_id,
3155 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3156 (unsigned long long)nr_blocks_metadata,
3157 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3158 (unsigned long long)nr_blocks_data);
3159
3160 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003161 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003162 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003163 DMEMIT("- ");
3164
Joe Thornber3e1a0692014-03-03 16:03:26 +00003165 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3166 DMEMIT("out_of_data_space ");
3167 else if (pool->pf.mode == PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01003168 DMEMIT("ro ");
3169 else
3170 DMEMIT("rw ");
3171
Mike Snitzer018debe2012-12-21 20:23:32 +00003172 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003173 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003174 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003175 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003176 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003177 DMEMIT("no_discard_passdown ");
3178
3179 if (pool->pf.error_if_no_space)
3180 DMEMIT("error_if_no_space ");
3181 else
3182 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003183
3184 break;
3185
3186 case STATUSTYPE_TABLE:
3187 DMEMIT("%s %s %lu %llu ",
3188 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3189 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3190 (unsigned long)pool->sectors_per_block,
3191 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01003192 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003193 break;
3194 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003195 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003196
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003197err:
3198 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003199}
3200
3201static int pool_iterate_devices(struct dm_target *ti,
3202 iterate_devices_callout_fn fn, void *data)
3203{
3204 struct pool_c *pt = ti->private;
3205
3206 return fn(ti, pt->data_dev, 0, ti->len, data);
3207}
3208
3209static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3210 struct bio_vec *biovec, int max_size)
3211{
3212 struct pool_c *pt = ti->private;
3213 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3214
3215 if (!q->merge_bvec_fn)
3216 return max_size;
3217
3218 bvm->bi_bdev = pt->data_dev->bdev;
3219
3220 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3221}
3222
Mike Snitzer0424caa2012-09-26 23:45:47 +01003223static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
Joe Thornber104655f2012-03-28 18:41:28 +01003224{
Mike Snitzer0424caa2012-09-26 23:45:47 +01003225 struct pool *pool = pt->pool;
3226 struct queue_limits *data_limits;
3227
Joe Thornber104655f2012-03-28 18:41:28 +01003228 limits->max_discard_sectors = pool->sectors_per_block;
3229
3230 /*
Mike Snitzer0424caa2012-09-26 23:45:47 +01003231 * discard_granularity is just a hint, and not enforced.
Joe Thornber104655f2012-03-28 18:41:28 +01003232 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01003233 if (pt->adjusted_pf.discard_passdown) {
3234 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
Lukas Czerner09869de2014-06-11 12:28:43 -04003235 limits->discard_granularity = max(data_limits->discard_granularity,
3236 pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf13945d2013-03-01 22:45:44 +00003237 } else
Mike Snitzer0424caa2012-09-26 23:45:47 +01003238 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
Joe Thornber104655f2012-03-28 18:41:28 +01003239}
3240
Joe Thornber991d9fa2011-10-31 20:21:18 +00003241static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3242{
3243 struct pool_c *pt = ti->private;
3244 struct pool *pool = pt->pool;
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003245 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003246
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003247 /*
3248 * If the system-determined stacked limits are compatible with the
3249 * pool's blocksize (io_opt is a factor) do not override them.
3250 */
3251 if (io_opt_sectors < pool->sectors_per_block ||
3252 do_div(io_opt_sectors, pool->sectors_per_block)) {
Mike Snitzerfdfb4c82014-07-18 17:59:43 -04003253 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003254 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3255 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003256
3257 /*
3258 * pt->adjusted_pf is a staging area for the actual features to use.
3259 * They get transferred to the live pool in bind_control_target()
3260 * called from pool_preresume().
3261 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003262 if (!pt->adjusted_pf.discard_enabled) {
3263 /*
3264 * Must explicitly disallow stacking discard limits otherwise the
3265 * block layer will stack them if pool's data device has support.
3266 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3267 * user to see that, so make sure to set all discard limits to 0.
3268 */
3269 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003270 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04003271 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003272
3273 disable_passdown_if_not_supported(pt);
3274
3275 set_discard_limits(pt, limits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003276}
3277
3278static struct target_type pool_target = {
3279 .name = "thin-pool",
3280 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3281 DM_TARGET_IMMUTABLE,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01003282 .version = {1, 13, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003283 .module = THIS_MODULE,
3284 .ctr = pool_ctr,
3285 .dtr = pool_dtr,
3286 .map = pool_map,
3287 .postsuspend = pool_postsuspend,
3288 .preresume = pool_preresume,
3289 .resume = pool_resume,
3290 .message = pool_message,
3291 .status = pool_status,
3292 .merge = pool_merge,
3293 .iterate_devices = pool_iterate_devices,
3294 .io_hints = pool_io_hints,
3295};
3296
3297/*----------------------------------------------------------------
3298 * Thin target methods
3299 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01003300static void thin_get(struct thin_c *tc)
3301{
3302 atomic_inc(&tc->refcount);
3303}
3304
3305static void thin_put(struct thin_c *tc)
3306{
3307 if (atomic_dec_and_test(&tc->refcount))
3308 complete(&tc->can_destroy);
3309}
3310
Joe Thornber991d9fa2011-10-31 20:21:18 +00003311static void thin_dtr(struct dm_target *ti)
3312{
3313 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003314 unsigned long flags;
3315
Joe Thornberb10ebd32014-04-08 11:29:01 +01003316 thin_put(tc);
3317 wait_for_completion(&tc->can_destroy);
3318
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003319 spin_lock_irqsave(&tc->pool->lock, flags);
3320 list_del_rcu(&tc->list);
3321 spin_unlock_irqrestore(&tc->pool->lock, flags);
3322 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00003323
3324 mutex_lock(&dm_thin_pool_table.mutex);
3325
3326 __pool_dec(tc->pool);
3327 dm_pool_close_thin_device(tc->td);
3328 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003329 if (tc->origin_dev)
3330 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003331 kfree(tc);
3332
3333 mutex_unlock(&dm_thin_pool_table.mutex);
3334}
3335
3336/*
3337 * Thin target parameters:
3338 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01003339 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00003340 *
3341 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3342 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01003343 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003344 *
3345 * If the pool device has discards disabled, they get disabled for the thin
3346 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003347 */
3348static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3349{
3350 int r;
3351 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01003352 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003353 struct mapped_device *pool_md;
Joe Thornber5e3283e2014-04-08 11:08:41 +01003354 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003355
3356 mutex_lock(&dm_thin_pool_table.mutex);
3357
Joe Thornber2dd9c252012-03-28 18:41:28 +01003358 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003359 ti->error = "Invalid argument count";
3360 r = -EINVAL;
3361 goto out_unlock;
3362 }
3363
3364 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
3365 if (!tc) {
3366 ti->error = "Out of memory";
3367 r = -ENOMEM;
3368 goto out_unlock;
3369 }
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003370 spin_lock_init(&tc->lock);
3371 bio_list_init(&tc->deferred_bio_list);
3372 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04003373 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003374
Joe Thornber2dd9c252012-03-28 18:41:28 +01003375 if (argc == 3) {
3376 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
3377 if (r) {
3378 ti->error = "Error opening origin device";
3379 goto bad_origin_dev;
3380 }
3381 tc->origin_dev = origin_dev;
3382 }
3383
Joe Thornber991d9fa2011-10-31 20:21:18 +00003384 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
3385 if (r) {
3386 ti->error = "Error opening pool device";
3387 goto bad_pool_dev;
3388 }
3389 tc->pool_dev = pool_dev;
3390
3391 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
3392 ti->error = "Invalid device id";
3393 r = -EINVAL;
3394 goto bad_common;
3395 }
3396
3397 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
3398 if (!pool_md) {
3399 ti->error = "Couldn't get pool mapped device";
3400 r = -EINVAL;
3401 goto bad_common;
3402 }
3403
3404 tc->pool = __pool_table_lookup(pool_md);
3405 if (!tc->pool) {
3406 ti->error = "Couldn't find pool object";
3407 r = -EINVAL;
3408 goto bad_pool_lookup;
3409 }
3410 __pool_inc(tc->pool);
3411
Joe Thornbere49e5822012-07-27 15:08:16 +01003412 if (get_pool_mode(tc->pool) == PM_FAIL) {
3413 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05003414 r = -EINVAL;
Joe Thornbere49e5822012-07-27 15:08:16 +01003415 goto bad_thin_open;
3416 }
3417
Joe Thornber991d9fa2011-10-31 20:21:18 +00003418 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
3419 if (r) {
3420 ti->error = "Couldn't open thin internal device";
3421 goto bad_thin_open;
3422 }
3423
Mike Snitzer542f9032012-07-27 15:08:00 +01003424 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
3425 if (r)
Mike Snitzer1acacc02014-02-19 20:32:33 -05003426 goto bad_target_max_io_len;
Mike Snitzer542f9032012-07-27 15:08:00 +01003427
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003428 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01003429 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003430 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003431
3432 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003433 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003434 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003435 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003436 ti->num_discard_bios = 1;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003437 /* Discard bios must be split on a block boundary */
3438 ti->split_discard_bios = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003439 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003440
3441 dm_put(pool_md);
3442
3443 mutex_unlock(&dm_thin_pool_table.mutex);
3444
Joe Thornberb10ebd32014-04-08 11:29:01 +01003445 atomic_set(&tc->refcount, 1);
3446 init_completion(&tc->can_destroy);
3447
Joe Thornber5e3283e2014-04-08 11:08:41 +01003448 spin_lock_irqsave(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003449 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Joe Thornber5e3283e2014-04-08 11:08:41 +01003450 spin_unlock_irqrestore(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003451 /*
3452 * This synchronize_rcu() call is needed here otherwise we risk a
3453 * wake_worker() call finding no bios to process (because the newly
3454 * added tc isn't yet visible). So this reduces latency since we
3455 * aren't then dependent on the periodic commit to wake_worker().
3456 */
3457 synchronize_rcu();
3458
Joe Thornber991d9fa2011-10-31 20:21:18 +00003459 return 0;
3460
Mike Snitzer1acacc02014-02-19 20:32:33 -05003461bad_target_max_io_len:
3462 dm_pool_close_thin_device(tc->td);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003463bad_thin_open:
3464 __pool_dec(tc->pool);
3465bad_pool_lookup:
3466 dm_put(pool_md);
3467bad_common:
3468 dm_put_device(ti, tc->pool_dev);
3469bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01003470 if (tc->origin_dev)
3471 dm_put_device(ti, tc->origin_dev);
3472bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003473 kfree(tc);
3474out_unlock:
3475 mutex_unlock(&dm_thin_pool_table.mutex);
3476
3477 return r;
3478}
3479
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003480static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003481{
Kent Overstreet4f024f32013-10-11 15:44:27 -07003482 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003483
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003484 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003485}
3486
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003487static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01003488{
3489 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003490 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01003491 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01003492 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01003493 struct pool *pool = h->tc->pool;
3494
3495 if (h->shared_read_entry) {
3496 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003497 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003498
3499 spin_lock_irqsave(&pool->lock, flags);
3500 list_for_each_entry_safe(m, tmp, &work, list) {
3501 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01003502 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003503 }
3504 spin_unlock_irqrestore(&pool->lock, flags);
3505 }
3506
Joe Thornber104655f2012-03-28 18:41:28 +01003507 if (h->all_io_entry) {
3508 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003509 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00003510 if (!list_empty(&work)) {
3511 spin_lock_irqsave(&pool->lock, flags);
3512 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05003513 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00003514 spin_unlock_irqrestore(&pool->lock, flags);
3515 wake_worker(pool);
3516 }
Joe Thornber104655f2012-03-28 18:41:28 +01003517 }
3518
Joe Thornbereb2aa482012-03-28 18:41:28 +01003519 return 0;
3520}
3521
Joe Thornber738211f2014-03-03 15:52:28 +00003522static void thin_presuspend(struct dm_target *ti)
3523{
3524 struct thin_c *tc = ti->private;
3525
3526 if (dm_noflush_suspending(ti))
3527 noflush_work(tc, do_noflush_start);
3528}
3529
Joe Thornber991d9fa2011-10-31 20:21:18 +00003530static void thin_postsuspend(struct dm_target *ti)
3531{
Joe Thornber738211f2014-03-03 15:52:28 +00003532 struct thin_c *tc = ti->private;
3533
3534 /*
3535 * The dm_noflush_suspending flag has been cleared by now, so
3536 * unfortunately we must always run this.
3537 */
3538 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003539}
3540
Joe Thornbere5aea7b2014-06-13 14:47:24 +01003541static int thin_preresume(struct dm_target *ti)
3542{
3543 struct thin_c *tc = ti->private;
3544
3545 if (tc->origin_dev)
3546 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
3547
3548 return 0;
3549}
3550
Joe Thornber991d9fa2011-10-31 20:21:18 +00003551/*
3552 * <nr mapped sectors> <highest mapped sector>
3553 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003554static void thin_status(struct dm_target *ti, status_type_t type,
3555 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003556{
3557 int r;
3558 ssize_t sz = 0;
3559 dm_block_t mapped, highest;
3560 char buf[BDEVNAME_SIZE];
3561 struct thin_c *tc = ti->private;
3562
Joe Thornbere49e5822012-07-27 15:08:16 +01003563 if (get_pool_mode(tc->pool) == PM_FAIL) {
3564 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003565 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01003566 }
3567
Joe Thornber991d9fa2011-10-31 20:21:18 +00003568 if (!tc->td)
3569 DMEMIT("-");
3570 else {
3571 switch (type) {
3572 case STATUSTYPE_INFO:
3573 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003574 if (r) {
3575 DMERR("dm_thin_get_mapped_count returned %d", r);
3576 goto err;
3577 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003578
3579 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003580 if (r < 0) {
3581 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
3582 goto err;
3583 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003584
3585 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
3586 if (r)
3587 DMEMIT("%llu", ((highest + 1) *
3588 tc->pool->sectors_per_block) - 1);
3589 else
3590 DMEMIT("-");
3591 break;
3592
3593 case STATUSTYPE_TABLE:
3594 DMEMIT("%s %lu",
3595 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
3596 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003597 if (tc->origin_dev)
3598 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00003599 break;
3600 }
3601 }
3602
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003603 return;
3604
3605err:
3606 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003607}
3608
3609static int thin_iterate_devices(struct dm_target *ti,
3610 iterate_devices_callout_fn fn, void *data)
3611{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003612 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003613 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003614 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003615
3616 /*
3617 * We can't call dm_pool_get_data_dev_size() since that blocks. So
3618 * we follow a more convoluted path through to the pool's target.
3619 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003620 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003621 return 0; /* nothing is bound */
3622
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003623 blocks = pool->ti->len;
3624 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003625 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003626 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003627
3628 return 0;
3629}
3630
Joe Thornber991d9fa2011-10-31 20:21:18 +00003631static struct target_type thin_target = {
3632 .name = "thin",
Joe Thornbere5aea7b2014-06-13 14:47:24 +01003633 .version = {1, 13, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003634 .module = THIS_MODULE,
3635 .ctr = thin_ctr,
3636 .dtr = thin_dtr,
3637 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01003638 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01003639 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00003640 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003641 .postsuspend = thin_postsuspend,
3642 .status = thin_status,
3643 .iterate_devices = thin_iterate_devices,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003644};
3645
3646/*----------------------------------------------------------------*/
3647
3648static int __init dm_thin_init(void)
3649{
3650 int r;
3651
3652 pool_table_init();
3653
3654 r = dm_register_target(&thin_target);
3655 if (r)
3656 return r;
3657
3658 r = dm_register_target(&pool_target);
3659 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01003660 goto bad_pool_target;
3661
3662 r = -ENOMEM;
3663
Mike Snitzera24c2562012-06-03 00:30:00 +01003664 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
3665 if (!_new_mapping_cache)
3666 goto bad_new_mapping_cache;
3667
Mike Snitzera24c2562012-06-03 00:30:00 +01003668 return 0;
3669
Mike Snitzera24c2562012-06-03 00:30:00 +01003670bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01003671 dm_unregister_target(&pool_target);
3672bad_pool_target:
3673 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003674
3675 return r;
3676}
3677
3678static void dm_thin_exit(void)
3679{
3680 dm_unregister_target(&thin_target);
3681 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01003682
Mike Snitzera24c2562012-06-03 00:30:00 +01003683 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003684}
3685
3686module_init(dm_thin_init);
3687module_exit(dm_thin_exit);
3688
Mike Snitzer80c57892014-05-20 13:38:33 -04003689module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
3690MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
3691
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01003692MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003693MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3694MODULE_LICENSE("GPL");