blob: 5036d4b3f368ce600a53549eaedeaf66e674d8b0 [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>
Mike Snitzer604ea902014-10-09 18:43:25 -040014#include <linux/log2.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000015#include <linux/list.h>
Mike Snitzerc140e1c2014-03-20 21:17:14 -040016#include <linux/rculist.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000017#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/slab.h>
Mike Snitzer67324ea2014-03-21 18:33:41 -040020#include <linux/rbtree.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000021
22#define DM_MSG_PREFIX "thin"
23
24/*
25 * Tunable constants
26 */
Alasdair G Kergon7768ed32012-07-27 15:07:57 +010027#define ENDIO_HOOK_POOL_SIZE 1024
Joe Thornber991d9fa2011-10-31 20:21:18 +000028#define MAPPING_POOL_SIZE 1024
Joe Thornber905e51b2012-03-28 18:41:27 +010029#define COMMIT_PERIOD HZ
Mike Snitzer80c57892014-05-20 13:38:33 -040030#define NO_SPACE_TIMEOUT_SECS 60
31
32static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
Joe Thornber991d9fa2011-10-31 20:21:18 +000033
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000034DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
35 "A percentage of time allocated for copy on write");
36
Joe Thornber991d9fa2011-10-31 20:21:18 +000037/*
38 * The block size of the device holding pool data must be
39 * between 64KB and 1GB.
40 */
41#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
42#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
43
44/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000045 * Device id is restricted to 24 bits.
46 */
47#define MAX_DEV_ID ((1 << 24) - 1)
48
49/*
50 * How do we handle breaking sharing of data blocks?
51 * =================================================
52 *
53 * We use a standard copy-on-write btree to store the mappings for the
54 * devices (note I'm talking about copy-on-write of the metadata here, not
55 * the data). When you take an internal snapshot you clone the root node
56 * of the origin btree. After this there is no concept of an origin or a
57 * snapshot. They are just two device trees that happen to point to the
58 * same data blocks.
59 *
60 * When we get a write in we decide if it's to a shared data block using
61 * some timestamp magic. If it is, we have to break sharing.
62 *
63 * Let's say we write to a shared block in what was the origin. The
64 * steps are:
65 *
66 * i) plug io further to this physical block. (see bio_prison code).
67 *
68 * ii) quiesce any read io to that shared data block. Obviously
Mike Snitzer44feb382012-10-12 21:02:10 +010069 * including all devices that share this block. (see dm_deferred_set code)
Joe Thornber991d9fa2011-10-31 20:21:18 +000070 *
71 * iii) copy the data block to a newly allocate block. This step can be
72 * missed out if the io covers the block. (schedule_copy).
73 *
74 * iv) insert the new mapping into the origin's btree
Joe Thornberfe878f32012-03-28 18:41:24 +010075 * (process_prepared_mapping). This act of inserting breaks some
Joe Thornber991d9fa2011-10-31 20:21:18 +000076 * sharing of btree nodes between the two devices. Breaking sharing only
77 * effects the btree of that specific device. Btrees for the other
78 * devices that share the block never change. The btree for the origin
79 * device as it was after the last commit is untouched, ie. we're using
80 * persistent data structures in the functional programming sense.
81 *
82 * v) unplug io to this physical block, including the io that triggered
83 * the breaking of sharing.
84 *
85 * Steps (ii) and (iii) occur in parallel.
86 *
87 * The metadata _doesn't_ need to be committed before the io continues. We
88 * get away with this because the io is always written to a _new_ block.
89 * If there's a crash, then:
90 *
91 * - The origin mapping will point to the old origin block (the shared
92 * one). This will contain the data as it was before the io that triggered
93 * the breaking of sharing came in.
94 *
95 * - The snap mapping still points to the old block. As it would after
96 * the commit.
97 *
98 * The downside of this scheme is the timestamp magic isn't perfect, and
99 * will continue to think that data block in the snapshot device is shared
100 * even after the write to the origin has broken sharing. I suspect data
101 * blocks will typically be shared by many different devices, so we're
102 * breaking sharing n + 1 times, rather than n, where n is the number of
103 * devices that reference this data block. At the moment I think the
104 * benefits far, far outweigh the disadvantages.
105 */
106
107/*----------------------------------------------------------------*/
108
109/*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000110 * Key building.
111 */
112static void build_data_key(struct dm_thin_device *td,
Mike Snitzer44feb382012-10-12 21:02:10 +0100113 dm_block_t b, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000114{
115 key->virtual = 0;
116 key->dev = dm_thin_dev_id(td);
117 key->block = b;
118}
119
120static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100121 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000122{
123 key->virtual = 1;
124 key->dev = dm_thin_dev_id(td);
125 key->block = b;
126}
127
128/*----------------------------------------------------------------*/
129
Joe Thornber7d327fe2014-10-06 15:45:59 +0100130#define THROTTLE_THRESHOLD (1 * HZ)
131
132struct throttle {
133 struct rw_semaphore lock;
134 unsigned long threshold;
135 bool throttle_applied;
136};
137
138static void throttle_init(struct throttle *t)
139{
140 init_rwsem(&t->lock);
141 t->throttle_applied = false;
142}
143
144static void throttle_work_start(struct throttle *t)
145{
146 t->threshold = jiffies + THROTTLE_THRESHOLD;
147}
148
149static void throttle_work_update(struct throttle *t)
150{
151 if (!t->throttle_applied && jiffies > t->threshold) {
152 down_write(&t->lock);
153 t->throttle_applied = true;
154 }
155}
156
157static void throttle_work_complete(struct throttle *t)
158{
159 if (t->throttle_applied) {
160 t->throttle_applied = false;
161 up_write(&t->lock);
162 }
163}
164
165static void throttle_lock(struct throttle *t)
166{
167 down_read(&t->lock);
168}
169
170static void throttle_unlock(struct throttle *t)
171{
172 up_read(&t->lock);
173}
174
175/*----------------------------------------------------------------*/
176
Joe Thornber991d9fa2011-10-31 20:21:18 +0000177/*
178 * A pool device ties together a metadata device and a data device. It
179 * also provides the interface for creating and destroying internal
180 * devices.
181 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100182struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100183
Joe Thornbere49e5822012-07-27 15:08:16 +0100184/*
Joe Thornber3e1a0692014-03-03 16:03:26 +0000185 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
Joe Thornbere49e5822012-07-27 15:08:16 +0100186 */
187enum pool_mode {
188 PM_WRITE, /* metadata may be changed */
Joe Thornber3e1a0692014-03-03 16:03:26 +0000189 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
Joe Thornbere49e5822012-07-27 15:08:16 +0100190 PM_READ_ONLY, /* metadata may not be changed */
191 PM_FAIL, /* all I/O fails */
192};
193
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100194struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100195 enum pool_mode mode;
196
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100197 bool zero_new_blocks:1;
198 bool discard_enabled:1;
199 bool discard_passdown:1;
Mike Snitzer787a996c2013-12-06 16:21:43 -0500200 bool error_if_no_space:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100201};
202
Joe Thornbere49e5822012-07-27 15:08:16 +0100203struct thin_c;
204typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
Joe Thornbera374bb22014-10-10 13:43:14 +0100205typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100206typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
207
Joe Thornber991d9fa2011-10-31 20:21:18 +0000208struct pool {
209 struct list_head list;
210 struct dm_target *ti; /* Only set if a pool target is bound */
211
212 struct mapped_device *pool_md;
213 struct block_device *md_dev;
214 struct dm_pool_metadata *pmd;
215
Joe Thornber991d9fa2011-10-31 20:21:18 +0000216 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100217 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100218 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000219
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100220 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500221 bool low_water_triggered:1; /* A dm event has been sent */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000222
Mike Snitzer44feb382012-10-12 21:02:10 +0100223 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000224 struct dm_kcopyd_client *copier;
225
226 struct workqueue_struct *wq;
Joe Thornber7d327fe2014-10-06 15:45:59 +0100227 struct throttle throttle;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000228 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100229 struct delayed_work waker;
Joe Thornber85ad643b2014-05-09 15:59:38 +0100230 struct delayed_work no_space_timeout;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000231
Joe Thornber905e51b2012-03-28 18:41:27 +0100232 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100233 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000234
235 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000236 struct bio_list deferred_flush_bios;
237 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100238 struct list_head prepared_discards;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400239 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000240
Mike Snitzer44feb382012-10-12 21:02:10 +0100241 struct dm_deferred_set *shared_read_ds;
242 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000243
Mike Snitzera24c2562012-06-03 00:30:00 +0100244 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000245 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100246
247 process_bio_fn process_bio;
248 process_bio_fn process_discard;
249
Joe Thornbera374bb22014-10-10 13:43:14 +0100250 process_cell_fn process_cell;
251 process_cell_fn process_discard_cell;
252
Joe Thornbere49e5822012-07-27 15:08:16 +0100253 process_mapping_fn process_prepared_mapping;
254 process_mapping_fn process_prepared_discard;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000255};
256
Joe Thornbere49e5822012-07-27 15:08:16 +0100257static enum pool_mode get_pool_mode(struct pool *pool);
Joe Thornberb5330652013-12-04 19:51:33 -0500258static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100259
Joe Thornber991d9fa2011-10-31 20:21:18 +0000260/*
261 * Target context for a pool.
262 */
263struct pool_c {
264 struct dm_target *ti;
265 struct pool *pool;
266 struct dm_dev *data_dev;
267 struct dm_dev *metadata_dev;
268 struct dm_target_callbacks callbacks;
269
270 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100271 struct pool_features requested_pf; /* Features requested during table load */
272 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000273};
274
275/*
276 * Target context for a thin.
277 */
278struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400279 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000280 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100281 struct dm_dev *origin_dev;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100282 sector_t origin_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000283 dm_thin_id dev_id;
284
285 struct pool *pool;
286 struct dm_thin_device *td;
Joe Thornber738211f2014-03-03 15:52:28 +0000287 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400288 spinlock_t lock;
Joe Thornbera374bb22014-10-10 13:43:14 +0100289 struct list_head deferred_cells;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400290 struct bio_list deferred_bio_list;
291 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400292 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornberb10ebd32014-04-08 11:29:01 +0100293
294 /*
295 * Ensures the thin is not destroyed until the worker has finished
296 * iterating the active_thins list.
297 */
298 atomic_t refcount;
299 struct completion can_destroy;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000300};
301
302/*----------------------------------------------------------------*/
303
Joe Thornber025b9682013-03-01 22:45:50 +0000304/*
305 * wake_worker() is used when new work is queued and when pool_resume is
306 * ready to continue deferred IO processing.
307 */
308static void wake_worker(struct pool *pool)
309{
310 queue_work(pool->wq, &pool->worker);
311}
312
313/*----------------------------------------------------------------*/
314
Joe Thornber6beca5e2013-03-01 22:45:50 +0000315static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
316 struct dm_bio_prison_cell **cell_result)
317{
318 int r;
319 struct dm_bio_prison_cell *cell_prealloc;
320
321 /*
322 * Allocate a cell from the prison's mempool.
323 * This might block but it can't fail.
324 */
325 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
326
327 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
328 if (r)
329 /*
330 * We reused an old cell; we can get rid of
331 * the new one.
332 */
333 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
334
335 return r;
336}
337
338static void cell_release(struct pool *pool,
339 struct dm_bio_prison_cell *cell,
340 struct bio_list *bios)
341{
342 dm_cell_release(pool->prison, cell, bios);
343 dm_bio_prison_free_cell(pool->prison, cell);
344}
345
Joe Thornber2d759a42014-10-10 15:27:16 +0100346static void cell_visit_release(struct pool *pool,
347 void (*fn)(void *, struct dm_bio_prison_cell *),
348 void *context,
349 struct dm_bio_prison_cell *cell)
350{
351 dm_cell_visit_release(pool->prison, fn, context, cell);
352 dm_bio_prison_free_cell(pool->prison, cell);
353}
354
Joe Thornber6beca5e2013-03-01 22:45:50 +0000355static void cell_release_no_holder(struct pool *pool,
356 struct dm_bio_prison_cell *cell,
357 struct bio_list *bios)
358{
359 dm_cell_release_no_holder(pool->prison, cell, bios);
360 dm_bio_prison_free_cell(pool->prison, cell);
361}
362
Mike Snitzeraf918052014-05-22 14:32:51 -0400363static void cell_error_with_code(struct pool *pool,
364 struct dm_bio_prison_cell *cell, int error_code)
Joe Thornber6beca5e2013-03-01 22:45:50 +0000365{
Mike Snitzeraf918052014-05-22 14:32:51 -0400366 dm_cell_error(pool->prison, cell, error_code);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000367 dm_bio_prison_free_cell(pool->prison, cell);
368}
369
Mike Snitzeraf918052014-05-22 14:32:51 -0400370static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
371{
372 cell_error_with_code(pool, cell, -EIO);
373}
374
Joe Thornbera374bb22014-10-10 13:43:14 +0100375static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
376{
377 cell_error_with_code(pool, cell, 0);
378}
379
380static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
381{
382 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
383}
384
Joe Thornber6beca5e2013-03-01 22:45:50 +0000385/*----------------------------------------------------------------*/
386
Joe Thornber991d9fa2011-10-31 20:21:18 +0000387/*
388 * A global list of pools that uses a struct mapped_device as a key.
389 */
390static struct dm_thin_pool_table {
391 struct mutex mutex;
392 struct list_head pools;
393} dm_thin_pool_table;
394
395static void pool_table_init(void)
396{
397 mutex_init(&dm_thin_pool_table.mutex);
398 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
399}
400
401static void __pool_table_insert(struct pool *pool)
402{
403 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
404 list_add(&pool->list, &dm_thin_pool_table.pools);
405}
406
407static void __pool_table_remove(struct pool *pool)
408{
409 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
410 list_del(&pool->list);
411}
412
413static struct pool *__pool_table_lookup(struct mapped_device *md)
414{
415 struct pool *pool = NULL, *tmp;
416
417 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
418
419 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
420 if (tmp->pool_md == md) {
421 pool = tmp;
422 break;
423 }
424 }
425
426 return pool;
427}
428
429static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
430{
431 struct pool *pool = NULL, *tmp;
432
433 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
434
435 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
436 if (tmp->md_dev == md_dev) {
437 pool = tmp;
438 break;
439 }
440 }
441
442 return pool;
443}
444
445/*----------------------------------------------------------------*/
446
Mike Snitzera24c2562012-06-03 00:30:00 +0100447struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100448 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100449 struct dm_deferred_entry *shared_read_entry;
450 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100451 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400452 struct rb_node rb_node;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100453};
454
Joe Thornber18adc572014-03-03 15:46:42 +0000455static void requeue_bio_list(struct thin_c *tc, struct bio_list *master)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000456{
457 struct bio *bio;
458 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000459 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000460
461 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000462
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400463 spin_lock_irqsave(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000464 bio_list_merge(&bios, master);
465 bio_list_init(master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400466 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000467
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400468 while ((bio = bio_list_pop(&bios)))
469 bio_endio(bio, DM_ENDIO_REQUEUE);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000470}
471
Joe Thornbera374bb22014-10-10 13:43:14 +0100472static void requeue_deferred_cells(struct thin_c *tc)
473{
474 struct pool *pool = tc->pool;
475 unsigned long flags;
476 struct list_head cells;
477 struct dm_bio_prison_cell *cell, *tmp;
478
479 INIT_LIST_HEAD(&cells);
480
481 spin_lock_irqsave(&tc->lock, flags);
482 list_splice_init(&tc->deferred_cells, &cells);
483 spin_unlock_irqrestore(&tc->lock, flags);
484
485 list_for_each_entry_safe(cell, tmp, &cells, user_list)
486 cell_requeue(pool, cell);
487}
488
Joe Thornber991d9fa2011-10-31 20:21:18 +0000489static void requeue_io(struct thin_c *tc)
490{
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400491 requeue_bio_list(tc, &tc->deferred_bio_list);
492 requeue_bio_list(tc, &tc->retry_on_resume_list);
Joe Thornbera374bb22014-10-10 13:43:14 +0100493 requeue_deferred_cells(tc);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000494}
495
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400496static void error_thin_retry_list(struct thin_c *tc)
Joe Thornber3e1a0692014-03-03 16:03:26 +0000497{
498 struct bio *bio;
499 unsigned long flags;
500 struct bio_list bios;
501
502 bio_list_init(&bios);
503
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400504 spin_lock_irqsave(&tc->lock, flags);
505 bio_list_merge(&bios, &tc->retry_on_resume_list);
506 bio_list_init(&tc->retry_on_resume_list);
507 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000508
509 while ((bio = bio_list_pop(&bios)))
510 bio_io_error(bio);
511}
512
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400513static void error_retry_list(struct pool *pool)
514{
515 struct thin_c *tc;
516
517 rcu_read_lock();
518 list_for_each_entry_rcu(tc, &pool->active_thins, list)
519 error_thin_retry_list(tc);
520 rcu_read_unlock();
521}
522
Joe Thornber991d9fa2011-10-31 20:21:18 +0000523/*
524 * This section of code contains the logic for processing a thin device's IO.
525 * Much of the code depends on pool object resources (lists, workqueues, etc)
526 * but most is exclusively called from the thin target rather than the thin-pool
527 * target.
528 */
529
Mike Snitzer58f77a22013-03-01 22:45:45 +0000530static bool block_size_is_power_of_two(struct pool *pool)
531{
532 return pool->sectors_per_block_shift >= 0;
533}
534
Joe Thornber991d9fa2011-10-31 20:21:18 +0000535static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
536{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000537 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700538 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100539
Mike Snitzer58f77a22013-03-01 22:45:45 +0000540 if (block_size_is_power_of_two(pool))
541 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100542 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000543 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100544
545 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000546}
547
548static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
549{
550 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700551 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000552
553 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000554 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700555 bio->bi_iter.bi_sector =
556 (block << pool->sectors_per_block_shift) |
557 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000558 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700559 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000560 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000561}
562
Joe Thornber2dd9c252012-03-28 18:41:28 +0100563static void remap_to_origin(struct thin_c *tc, struct bio *bio)
564{
565 bio->bi_bdev = tc->origin_dev->bdev;
566}
567
Joe Thornber4afdd682012-07-27 15:08:14 +0100568static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
569{
570 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
571 dm_thin_changed_this_transaction(tc->td);
572}
573
Joe Thornbere8088072012-12-21 20:23:31 +0000574static void inc_all_io_entry(struct pool *pool, struct bio *bio)
575{
576 struct dm_thin_endio_hook *h;
577
578 if (bio->bi_rw & REQ_DISCARD)
579 return;
580
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000581 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000582 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
583}
584
Joe Thornber2dd9c252012-03-28 18:41:28 +0100585static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000586{
587 struct pool *pool = tc->pool;
588 unsigned long flags;
589
Joe Thornbere49e5822012-07-27 15:08:16 +0100590 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000591 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100592 return;
593 }
594
595 /*
596 * Complete bio with an error if earlier I/O caused changes to
597 * the metadata that can't be committed e.g, due to I/O errors
598 * on the metadata device.
599 */
600 if (dm_thin_aborted_changes(tc->td)) {
601 bio_io_error(bio);
602 return;
603 }
604
605 /*
606 * Batch together any bios that trigger commits and then issue a
607 * single commit for them in process_deferred_bios().
608 */
609 spin_lock_irqsave(&pool->lock, flags);
610 bio_list_add(&pool->deferred_flush_bios, bio);
611 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000612}
613
Joe Thornber2dd9c252012-03-28 18:41:28 +0100614static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
615{
616 remap_to_origin(tc, bio);
617 issue(tc, bio);
618}
619
620static void remap_and_issue(struct thin_c *tc, struct bio *bio,
621 dm_block_t block)
622{
623 remap(tc, bio, block);
624 issue(tc, bio);
625}
626
Joe Thornber991d9fa2011-10-31 20:21:18 +0000627/*----------------------------------------------------------------*/
628
629/*
630 * Bio endio functions.
631 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100632struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000633 struct list_head list;
634
Mike Snitzer7f214662013-12-17 13:43:31 -0500635 bool pass_discard:1;
636 bool definitely_not_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000637
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100638 /*
639 * Track quiescing, copying and zeroing preparation actions. When this
640 * counter hits zero the block is prepared and can be inserted into the
641 * btree.
642 */
643 atomic_t prepare_actions;
644
Mike Snitzer7f214662013-12-17 13:43:31 -0500645 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000646 struct thin_c *tc;
647 dm_block_t virt_block;
648 dm_block_t data_block;
Mike Snitzera24c2562012-06-03 00:30:00 +0100649 struct dm_bio_prison_cell *cell, *cell2;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000650
651 /*
652 * If the bio covers the whole area of a block then we can avoid
653 * zeroing or copying. Instead this bio is hooked. The bio will
654 * still be in the cell, so care has to be taken to avoid issuing
655 * the bio twice.
656 */
657 struct bio *bio;
658 bio_end_io_t *saved_bi_end_io;
659};
660
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100661static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000662{
663 struct pool *pool = m->tc->pool;
664
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100665 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500666 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000667 wake_worker(pool);
668 }
669}
670
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100671static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000672{
673 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000674 struct pool *pool = m->tc->pool;
675
Joe Thornber991d9fa2011-10-31 20:21:18 +0000676 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100677 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000678 spin_unlock_irqrestore(&pool->lock, flags);
679}
680
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100681static void copy_complete(int read_err, unsigned long write_err, void *context)
682{
683 struct dm_thin_new_mapping *m = context;
684
685 m->err = read_err || write_err ? -EIO : 0;
686 complete_mapping_preparation(m);
687}
688
Joe Thornber991d9fa2011-10-31 20:21:18 +0000689static void overwrite_endio(struct bio *bio, int err)
690{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000691 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100692 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000693
694 m->err = err;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100695 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000696}
697
Joe Thornber991d9fa2011-10-31 20:21:18 +0000698/*----------------------------------------------------------------*/
699
700/*
701 * Workqueue.
702 */
703
704/*
705 * Prepared mapping jobs.
706 */
707
708/*
Joe Thornber2d759a42014-10-10 15:27:16 +0100709 * This sends the bios in the cell, except the original holder, back
710 * to the deferred_bios list.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000711 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000712static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000713{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000714 struct pool *pool = tc->pool;
715 unsigned long flags;
716
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400717 spin_lock_irqsave(&tc->lock, flags);
718 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
719 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000720
721 wake_worker(pool);
722}
723
Joe Thornbera374bb22014-10-10 13:43:14 +0100724static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
725
Joe Thornber2d759a42014-10-10 15:27:16 +0100726struct remap_info {
727 struct thin_c *tc;
728 struct bio_list defer_bios;
729 struct bio_list issue_bios;
730};
731
732static void __inc_remap_and_issue_cell(void *context,
733 struct dm_bio_prison_cell *cell)
734{
735 struct remap_info *info = context;
736 struct bio *bio;
737
738 while ((bio = bio_list_pop(&cell->bios))) {
739 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
740 bio_list_add(&info->defer_bios, bio);
741 else {
742 inc_all_io_entry(info->tc->pool, bio);
743
744 /*
745 * We can't issue the bios with the bio prison lock
746 * held, so we add them to a list to issue on
747 * return from this function.
748 */
749 bio_list_add(&info->issue_bios, bio);
750 }
751 }
752}
753
Joe Thornbera374bb22014-10-10 13:43:14 +0100754static void inc_remap_and_issue_cell(struct thin_c *tc,
755 struct dm_bio_prison_cell *cell,
756 dm_block_t block)
757{
758 struct bio *bio;
Joe Thornber2d759a42014-10-10 15:27:16 +0100759 struct remap_info info;
Joe Thornbera374bb22014-10-10 13:43:14 +0100760
Joe Thornber2d759a42014-10-10 15:27:16 +0100761 info.tc = tc;
762 bio_list_init(&info.defer_bios);
763 bio_list_init(&info.issue_bios);
Joe Thornbera374bb22014-10-10 13:43:14 +0100764
Joe Thornber2d759a42014-10-10 15:27:16 +0100765 /*
766 * We have to be careful to inc any bios we're about to issue
767 * before the cell is released, and avoid a race with new bios
768 * being added to the cell.
769 */
770 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
771 &info, cell);
772
773 while ((bio = bio_list_pop(&info.defer_bios)))
774 thin_defer_bio(tc, bio);
775
776 while ((bio = bio_list_pop(&info.issue_bios)))
777 remap_and_issue(info.tc, bio, block);
Joe Thornbera374bb22014-10-10 13:43:14 +0100778}
779
Joe Thornbere49e5822012-07-27 15:08:16 +0100780static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
781{
Kent Overstreet196d38b2013-11-23 18:34:15 -0800782 if (m->bio) {
Joe Thornbere49e5822012-07-27 15:08:16 +0100783 m->bio->bi_end_io = m->saved_bi_end_io;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800784 atomic_inc(&m->bio->bi_remaining);
785 }
Joe Thornber6beca5e2013-03-01 22:45:50 +0000786 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100787 list_del(&m->list);
788 mempool_free(m, m->tc->pool->mapping_pool);
789}
Joe Thornber025b9682013-03-01 22:45:50 +0000790
Mike Snitzera24c2562012-06-03 00:30:00 +0100791static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000792{
793 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000794 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000795 struct bio *bio;
796 int r;
797
798 bio = m->bio;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800799 if (bio) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000800 bio->bi_end_io = m->saved_bi_end_io;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800801 atomic_inc(&bio->bi_remaining);
802 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000803
804 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000805 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100806 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000807 }
808
809 /*
810 * Commit the prepared block into the mapping btree.
811 * Any I/O for this block arriving after this point will get
812 * remapped to it directly.
813 */
814 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
815 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500816 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000817 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100818 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000819 }
820
821 /*
822 * Release any bios held while the block was being provisioned.
823 * If we are processing a write bio that completely covers the block,
824 * we already processed it so can ignore it now when processing
825 * the bios in the cell.
826 */
827 if (bio) {
Joe Thornber2d759a42014-10-10 15:27:16 +0100828 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000829 bio_endio(bio, 0);
Joe Thornber2d759a42014-10-10 15:27:16 +0100830 } else {
831 inc_all_io_entry(tc->pool, m->cell->holder);
832 remap_and_issue(tc, m->cell->holder, m->data_block);
833 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
834 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000835
Joe Thornber905386f2012-07-27 15:08:05 +0100836out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000837 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000838 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000839}
840
Joe Thornbere49e5822012-07-27 15:08:16 +0100841static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100842{
Joe Thornber104655f2012-03-28 18:41:28 +0100843 struct thin_c *tc = m->tc;
844
Joe Thornbere49e5822012-07-27 15:08:16 +0100845 bio_io_error(m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000846 cell_defer_no_holder(tc, m->cell);
847 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere49e5822012-07-27 15:08:16 +0100848 mempool_free(m, tc->pool->mapping_pool);
849}
Joe Thornber104655f2012-03-28 18:41:28 +0100850
Joe Thornbere49e5822012-07-27 15:08:16 +0100851static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
852{
853 struct thin_c *tc = m->tc;
854
Joe Thornbere8088072012-12-21 20:23:31 +0000855 inc_all_io_entry(tc->pool, m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000856 cell_defer_no_holder(tc, m->cell);
857 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere8088072012-12-21 20:23:31 +0000858
Joe Thornber104655f2012-03-28 18:41:28 +0100859 if (m->pass_discard)
Joe Thornber19fa1a62013-12-17 12:09:40 -0500860 if (m->definitely_not_shared)
861 remap_and_issue(tc, m->bio, m->data_block);
862 else {
863 bool used = false;
864 if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used)
865 bio_endio(m->bio, 0);
866 else
867 remap_and_issue(tc, m->bio, m->data_block);
868 }
Joe Thornber104655f2012-03-28 18:41:28 +0100869 else
870 bio_endio(m->bio, 0);
871
Joe Thornber104655f2012-03-28 18:41:28 +0100872 mempool_free(m, tc->pool->mapping_pool);
873}
874
Joe Thornbere49e5822012-07-27 15:08:16 +0100875static void process_prepared_discard(struct dm_thin_new_mapping *m)
876{
877 int r;
878 struct thin_c *tc = m->tc;
879
880 r = dm_thin_remove_block(tc->td, m->virt_block);
881 if (r)
Mike Snitzerc3977412012-12-21 20:23:34 +0000882 DMERR_LIMIT("dm_thin_remove_block() failed");
Joe Thornbere49e5822012-07-27 15:08:16 +0100883
884 process_prepared_discard_passdown(m);
885}
886
Joe Thornber104655f2012-03-28 18:41:28 +0100887static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +0100888 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000889{
890 unsigned long flags;
891 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +0100892 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000893
894 INIT_LIST_HEAD(&maps);
895 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100896 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000897 spin_unlock_irqrestore(&pool->lock, flags);
898
899 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +0100900 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000901}
902
903/*
904 * Deferred bio jobs.
905 */
Joe Thornber104655f2012-03-28 18:41:28 +0100906static int io_overlaps_block(struct pool *pool, struct bio *bio)
907{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700908 return bio->bi_iter.bi_size ==
909 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +0100910}
911
Joe Thornber991d9fa2011-10-31 20:21:18 +0000912static int io_overwrites_block(struct pool *pool, struct bio *bio)
913{
Joe Thornber104655f2012-03-28 18:41:28 +0100914 return (bio_data_dir(bio) == WRITE) &&
915 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000916}
917
918static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
919 bio_end_io_t *fn)
920{
921 *save = bio->bi_end_io;
922 bio->bi_end_io = fn;
923}
924
925static int ensure_next_mapping(struct pool *pool)
926{
927 if (pool->next_mapping)
928 return 0;
929
930 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
931
932 return pool->next_mapping ? 0 : -ENOMEM;
933}
934
Mike Snitzera24c2562012-06-03 00:30:00 +0100935static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000936{
Mike Snitzer16961b02013-12-17 13:19:11 -0500937 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000938
939 BUG_ON(!pool->next_mapping);
940
Mike Snitzer16961b02013-12-17 13:19:11 -0500941 memset(m, 0, sizeof(struct dm_thin_new_mapping));
942 INIT_LIST_HEAD(&m->list);
943 m->bio = NULL;
944
Joe Thornber991d9fa2011-10-31 20:21:18 +0000945 pool->next_mapping = NULL;
946
Mike Snitzer16961b02013-12-17 13:19:11 -0500947 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000948}
949
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100950static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
951 sector_t begin, sector_t end)
952{
953 int r;
954 struct dm_io_region to;
955
956 to.bdev = tc->pool_dev->bdev;
957 to.sector = begin;
958 to.count = end - begin;
959
960 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
961 if (r < 0) {
962 DMERR_LIMIT("dm_kcopyd_zero() failed");
963 copy_complete(1, 1, m);
964 }
965}
966
Mike Snitzer452d7a62014-10-09 19:20:21 -0400967static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
968 dm_block_t data_block,
969 struct dm_thin_new_mapping *m)
970{
971 struct pool *pool = tc->pool;
972 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
973
974 h->overwrite_mapping = m;
975 m->bio = bio;
976 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
977 inc_all_io_entry(pool, bio);
978 remap_and_issue(tc, bio, data_block);
979}
980
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100981/*
982 * A partial copy also needs to zero the uncopied region.
983 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000984static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +0100985 struct dm_dev *origin, dm_block_t data_origin,
986 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100987 struct dm_bio_prison_cell *cell, struct bio *bio,
988 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000989{
990 int r;
991 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100992 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000993
Joe Thornber991d9fa2011-10-31 20:21:18 +0000994 m->tc = tc;
995 m->virt_block = virt_block;
996 m->data_block = data_dest;
997 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000998
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100999 /*
1000 * quiesce action + copy action + an extra reference held for the
1001 * duration of this function (we may need to inc later for a
1002 * partial zero).
1003 */
1004 atomic_set(&m->prepare_actions, 3);
1005
Mike Snitzer44feb382012-10-12 21:02:10 +01001006 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001007 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001008
1009 /*
1010 * IO to pool_dev remaps to the pool target's data_dev.
1011 *
1012 * If the whole block of data is being overwritten, we can issue the
1013 * bio immediately. Otherwise we use kcopyd to clone the data first.
1014 */
Mike Snitzer452d7a62014-10-09 19:20:21 -04001015 if (io_overwrites_block(pool, bio))
1016 remap_and_issue_overwrite(tc, bio, data_dest, m);
1017 else {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001018 struct dm_io_region from, to;
1019
Joe Thornber2dd9c252012-03-28 18:41:28 +01001020 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001021 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001022 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001023
1024 to.bdev = tc->pool_dev->bdev;
1025 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001026 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001027
1028 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1029 0, copy_complete, m);
1030 if (r < 0) {
Mike Snitzerc3977412012-12-21 20:23:34 +00001031 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001032 copy_complete(1, 1, m);
1033
1034 /*
1035 * We allow the zero to be issued, to simplify the
1036 * error path. Otherwise we'd need to start
1037 * worrying about decrementing the prepare_actions
1038 * counter.
1039 */
1040 }
1041
1042 /*
1043 * Do we need to zero a tail region?
1044 */
1045 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1046 atomic_inc(&m->prepare_actions);
1047 ll_zero(tc, m,
1048 data_dest * pool->sectors_per_block + len,
1049 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001050 }
1051 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001052
1053 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001054}
1055
Joe Thornber2dd9c252012-03-28 18:41:28 +01001056static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1057 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +01001058 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +01001059{
1060 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001061 data_origin, data_dest, cell, bio,
1062 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001063}
1064
Joe Thornber991d9fa2011-10-31 20:21:18 +00001065static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001066 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001067 struct bio *bio)
1068{
1069 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001070 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001071
Joe Thornber50f3c3e2014-06-13 13:57:09 +01001072 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001073 m->tc = tc;
1074 m->virt_block = virt_block;
1075 m->data_block = data_block;
1076 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001077
1078 /*
1079 * If the whole block of data is being overwritten or we are not
1080 * zeroing pre-existing data, we can issue the bio immediately.
1081 * Otherwise we use kcopyd to zero the data first.
1082 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001083 if (!pool->pf.zero_new_blocks)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001084 process_prepared_mapping(m);
1085
Mike Snitzer452d7a62014-10-09 19:20:21 -04001086 else if (io_overwrites_block(pool, bio))
1087 remap_and_issue_overwrite(tc, bio, data_block, m);
Mike Snitzera24c2562012-06-03 00:30:00 +01001088
Mike Snitzer452d7a62014-10-09 19:20:21 -04001089 else
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001090 ll_zero(tc, m,
1091 data_block * pool->sectors_per_block,
1092 (data_block + 1) * pool->sectors_per_block);
1093}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001094
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001095static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1096 dm_block_t data_dest,
1097 struct dm_bio_prison_cell *cell, struct bio *bio)
1098{
1099 struct pool *pool = tc->pool;
1100 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1101 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1102
1103 if (virt_block_end <= tc->origin_size)
1104 schedule_copy(tc, virt_block, tc->origin_dev,
1105 virt_block, data_dest, cell, bio,
1106 pool->sectors_per_block);
1107
1108 else if (virt_block_begin < tc->origin_size)
1109 schedule_copy(tc, virt_block, tc->origin_dev,
1110 virt_block, data_dest, cell, bio,
1111 tc->origin_size - virt_block_begin);
1112
1113 else
1114 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001115}
1116
Joe Thornbere49e5822012-07-27 15:08:16 +01001117/*
1118 * A non-zero return indicates read_only or fail_io mode.
1119 * Many callers don't care about the return value.
1120 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001121static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001122{
1123 int r;
1124
Joe Thornber8d07e8a2014-05-06 16:28:14 +01001125 if (get_pool_mode(pool) >= PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01001126 return -EINVAL;
1127
Joe Thornber020cc3b2013-12-04 15:05:36 -05001128 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001129 if (r)
1130 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornbere49e5822012-07-27 15:08:16 +01001131
1132 return r;
1133}
1134
Joe Thornber88a66212013-12-04 20:16:12 -05001135static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1136{
1137 unsigned long flags;
1138
1139 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1140 DMWARN("%s: reached low water mark for data device: sending event.",
1141 dm_device_name(pool->pool_md));
1142 spin_lock_irqsave(&pool->lock, flags);
1143 pool->low_water_triggered = true;
1144 spin_unlock_irqrestore(&pool->lock, flags);
1145 dm_table_event(pool->ti->table);
1146 }
1147}
1148
Joe Thornber3e1a0692014-03-03 16:03:26 +00001149static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1150
Joe Thornber991d9fa2011-10-31 20:21:18 +00001151static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1152{
1153 int r;
1154 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001155 struct pool *pool = tc->pool;
1156
Joe Thornber3e1a0692014-03-03 16:03:26 +00001157 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001158 return -EINVAL;
1159
Joe Thornber991d9fa2011-10-31 20:21:18 +00001160 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001161 if (r) {
1162 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001163 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001164 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001165
Joe Thornber88a66212013-12-04 20:16:12 -05001166 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001167
1168 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001169 /*
1170 * Try to commit to see if that will free up some
1171 * more space.
1172 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001173 r = commit(pool);
1174 if (r)
1175 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001176
1177 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001178 if (r) {
1179 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001180 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001181 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001182
Mike Snitzer94563ba2013-08-22 09:56:18 -04001183 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001184 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001185 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001186 }
1187 }
1188
1189 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001190 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05001191 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001192 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001193 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001194
1195 return 0;
1196}
1197
1198/*
1199 * If we have run out of space, queue bios until the device is
1200 * resumed, presumably after having been reloaded with more space.
1201 */
1202static void retry_on_resume(struct bio *bio)
1203{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001204 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001205 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001206 unsigned long flags;
1207
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001208 spin_lock_irqsave(&tc->lock, flags);
1209 bio_list_add(&tc->retry_on_resume_list, bio);
1210 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001211}
1212
Mike Snitzeraf918052014-05-22 14:32:51 -04001213static int should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001214{
1215 enum pool_mode m = get_pool_mode(pool);
1216
1217 switch (m) {
1218 case PM_WRITE:
1219 /* Shouldn't get here */
1220 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001221 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001222
1223 case PM_OUT_OF_DATA_SPACE:
Mike Snitzeraf918052014-05-22 14:32:51 -04001224 return pool->pf.error_if_no_space ? -ENOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001225
1226 case PM_READ_ONLY:
1227 case PM_FAIL:
Mike Snitzeraf918052014-05-22 14:32:51 -04001228 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001229 default:
1230 /* Shouldn't get here */
1231 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001232 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001233 }
1234}
1235
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001236static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1237{
Mike Snitzeraf918052014-05-22 14:32:51 -04001238 int error = should_error_unserviceable_bio(pool);
1239
1240 if (error)
1241 bio_endio(bio, error);
Mike Snitzer6d162022013-12-20 18:09:02 -05001242 else
1243 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001244}
1245
Mike Snitzer399cadd2013-12-05 16:03:33 -05001246static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001247{
1248 struct bio *bio;
1249 struct bio_list bios;
Mike Snitzeraf918052014-05-22 14:32:51 -04001250 int error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001251
Mike Snitzeraf918052014-05-22 14:32:51 -04001252 error = should_error_unserviceable_bio(pool);
1253 if (error) {
1254 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001255 return;
1256 }
1257
Joe Thornber991d9fa2011-10-31 20:21:18 +00001258 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001259 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001260
Mike Snitzeraf918052014-05-22 14:32:51 -04001261 error = should_error_unserviceable_bio(pool);
1262 if (error)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001263 while ((bio = bio_list_pop(&bios)))
Mike Snitzeraf918052014-05-22 14:32:51 -04001264 bio_endio(bio, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001265 else
1266 while ((bio = bio_list_pop(&bios)))
1267 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001268}
1269
Joe Thornbera374bb22014-10-10 13:43:14 +01001270static void process_discard_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber104655f2012-03-28 18:41:28 +01001271{
1272 int r;
Joe Thornbera374bb22014-10-10 13:43:14 +01001273 struct bio *bio = cell->holder;
Joe Thornber104655f2012-03-28 18:41:28 +01001274 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001275 struct dm_bio_prison_cell *cell2;
1276 struct dm_cell_key key2;
Joe Thornber104655f2012-03-28 18:41:28 +01001277 dm_block_t block = get_bio_block(tc, bio);
1278 struct dm_thin_lookup_result lookup_result;
Mike Snitzera24c2562012-06-03 00:30:00 +01001279 struct dm_thin_new_mapping *m;
Joe Thornber104655f2012-03-28 18:41:28 +01001280
Joe Thornbera374bb22014-10-10 13:43:14 +01001281 if (tc->requeue_mode) {
1282 cell_requeue(pool, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001283 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001284 }
Joe Thornber104655f2012-03-28 18:41:28 +01001285
1286 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1287 switch (r) {
1288 case 0:
1289 /*
1290 * Check nobody is fiddling with this pool block. This can
1291 * happen if someone's in the process of breaking sharing
1292 * on this block.
1293 */
1294 build_data_key(tc->td, lookup_result.block, &key2);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001295 if (bio_detain(tc->pool, &key2, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +00001296 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001297 break;
1298 }
1299
1300 if (io_overlaps_block(pool, bio)) {
1301 /*
1302 * IO may still be going to the destination block. We must
1303 * quiesce before we can do the removal.
1304 */
1305 m = get_next_mapping(pool);
1306 m->tc = tc;
Joe Thornber19fa1a62013-12-17 12:09:40 -05001307 m->pass_discard = pool->pf.discard_passdown;
1308 m->definitely_not_shared = !lookup_result.shared;
Joe Thornber104655f2012-03-28 18:41:28 +01001309 m->virt_block = block;
1310 m->data_block = lookup_result.block;
1311 m->cell = cell;
1312 m->cell2 = cell2;
Joe Thornber104655f2012-03-28 18:41:28 +01001313 m->bio = bio;
1314
Joe Thornber7a7e97c2014-09-12 11:34:01 +01001315 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1316 pool->process_prepared_discard(m);
1317
Joe Thornber104655f2012-03-28 18:41:28 +01001318 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001319 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001320 cell_defer_no_holder(tc, cell);
1321 cell_defer_no_holder(tc, cell2);
Joe Thornbere8088072012-12-21 20:23:31 +00001322
Joe Thornber104655f2012-03-28 18:41:28 +01001323 /*
Mikulas Patocka49296302012-07-27 15:08:03 +01001324 * The DM core makes sure that the discard doesn't span
1325 * a block boundary. So we submit the discard of a
1326 * partial block appropriately.
Joe Thornber104655f2012-03-28 18:41:28 +01001327 */
Mikulas Patocka650d2a02012-07-20 14:25:05 +01001328 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1329 remap_and_issue(tc, bio, lookup_result.block);
1330 else
1331 bio_endio(bio, 0);
Joe Thornber104655f2012-03-28 18:41:28 +01001332 }
1333 break;
1334
1335 case -ENODATA:
1336 /*
1337 * It isn't provisioned, just forget it.
1338 */
Joe Thornberf286ba02012-12-21 20:23:33 +00001339 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001340 bio_endio(bio, 0);
1341 break;
1342
1343 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001344 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1345 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001346 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001347 bio_io_error(bio);
1348 break;
1349 }
1350}
1351
Joe Thornbera374bb22014-10-10 13:43:14 +01001352static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1353{
1354 struct dm_bio_prison_cell *cell;
1355 struct dm_cell_key key;
1356 dm_block_t block = get_bio_block(tc, bio);
1357
1358 build_virtual_key(tc->td, block, &key);
1359 if (bio_detain(tc->pool, &key, bio, &cell))
1360 return;
1361
1362 process_discard_cell(tc, cell);
1363}
1364
Joe Thornber991d9fa2011-10-31 20:21:18 +00001365static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001366 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001367 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001368 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001369{
1370 int r;
1371 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001372 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001373
1374 r = alloc_data_block(tc, &data_block);
1375 switch (r) {
1376 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001377 schedule_internal_copy(tc, block, lookup_result->block,
1378 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001379 break;
1380
1381 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001382 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001383 break;
1384
1385 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001386 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1387 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001388 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001389 break;
1390 }
1391}
1392
1393static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1394 dm_block_t block,
1395 struct dm_thin_lookup_result *lookup_result)
1396{
Mike Snitzera24c2562012-06-03 00:30:00 +01001397 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001398 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001399 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001400
1401 /*
1402 * If cell is already occupied, then sharing is already in the process
1403 * of being broken so we have nothing further to do here.
1404 */
1405 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001406 if (bio_detain(pool, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001407 return;
1408
Kent Overstreet4f024f32013-10-11 15:44:27 -07001409 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001410 break_sharing(tc, bio, block, &key, lookup_result, cell);
1411 else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001412 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001413
Mike Snitzer44feb382012-10-12 21:02:10 +01001414 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001415 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001416 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001417
Joe Thornber991d9fa2011-10-31 20:21:18 +00001418 remap_and_issue(tc, bio, lookup_result->block);
1419 }
1420}
1421
1422static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001423 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001424{
1425 int r;
1426 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001427 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001428
1429 /*
1430 * Remap empty bios (flushes) immediately, without provisioning.
1431 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001432 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001433 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001434 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001435
Joe Thornber991d9fa2011-10-31 20:21:18 +00001436 remap_and_issue(tc, bio, 0);
1437 return;
1438 }
1439
1440 /*
1441 * Fill read bios with zeroes and complete them immediately.
1442 */
1443 if (bio_data_dir(bio) == READ) {
1444 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001445 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001446 bio_endio(bio, 0);
1447 return;
1448 }
1449
1450 r = alloc_data_block(tc, &data_block);
1451 switch (r) {
1452 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001453 if (tc->origin_dev)
1454 schedule_external_copy(tc, block, data_block, cell, bio);
1455 else
1456 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001457 break;
1458
1459 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001460 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001461 break;
1462
1463 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001464 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1465 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001466 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001467 break;
1468 }
1469}
1470
Joe Thornbera374bb22014-10-10 13:43:14 +01001471static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001472{
1473 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001474 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001475 struct bio *bio = cell->holder;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001476 dm_block_t block = get_bio_block(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001477 struct dm_thin_lookup_result lookup_result;
1478
Joe Thornbera374bb22014-10-10 13:43:14 +01001479 if (tc->requeue_mode) {
1480 cell_requeue(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001481 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001482 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001483
1484 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1485 switch (r) {
1486 case 0:
Joe Thornbere8088072012-12-21 20:23:31 +00001487 if (lookup_result.shared) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001488 process_shared_bio(tc, bio, block, &lookup_result);
Joe Thornbera374bb22014-10-10 13:43:14 +01001489 // FIXME: we can't remap because we're waiting on a commit
Joe Thornber6beca5e2013-03-01 22:45:50 +00001490 cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */
Joe Thornbere8088072012-12-21 20:23:31 +00001491 } else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001492 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001493 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001494 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001495 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001496 break;
1497
1498 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001499 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001500 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001501 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001502
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001503 if (bio_end_sector(bio) <= tc->origin_size)
1504 remap_to_origin_and_issue(tc, bio);
1505
1506 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1507 zero_fill_bio(bio);
1508 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1509 remap_to_origin_and_issue(tc, bio);
1510
1511 } else {
1512 zero_fill_bio(bio);
1513 bio_endio(bio, 0);
1514 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001515 } else
1516 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001517 break;
1518
1519 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001520 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1521 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001522 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001523 bio_io_error(bio);
1524 break;
1525 }
1526}
1527
Joe Thornbera374bb22014-10-10 13:43:14 +01001528static void process_bio(struct thin_c *tc, struct bio *bio)
1529{
1530 struct pool *pool = tc->pool;
1531 dm_block_t block = get_bio_block(tc, bio);
1532 struct dm_bio_prison_cell *cell;
1533 struct dm_cell_key key;
1534
1535 /*
1536 * If cell is already occupied, then the block is already
1537 * being provisioned so we have nothing further to do here.
1538 */
1539 build_virtual_key(tc->td, block, &key);
1540 if (bio_detain(pool, &key, bio, &cell))
1541 return;
1542
1543 process_cell(tc, cell);
1544}
1545
1546static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1547 struct dm_bio_prison_cell *cell)
Joe Thornbere49e5822012-07-27 15:08:16 +01001548{
1549 int r;
1550 int rw = bio_data_dir(bio);
1551 dm_block_t block = get_bio_block(tc, bio);
1552 struct dm_thin_lookup_result lookup_result;
1553
1554 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1555 switch (r) {
1556 case 0:
Joe Thornbera374bb22014-10-10 13:43:14 +01001557 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001558 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01001559 if (cell)
1560 cell_defer_no_holder(tc, cell);
1561 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001562 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001563 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001564 if (cell)
1565 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001566 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001567 break;
1568
1569 case -ENODATA:
Joe Thornbera374bb22014-10-10 13:43:14 +01001570 if (cell)
1571 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001572 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001573 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001574 break;
1575 }
1576
1577 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001578 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001579 remap_to_origin_and_issue(tc, bio);
1580 break;
1581 }
1582
1583 zero_fill_bio(bio);
1584 bio_endio(bio, 0);
1585 break;
1586
1587 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001588 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1589 __func__, r);
Joe Thornbera374bb22014-10-10 13:43:14 +01001590 if (cell)
1591 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001592 bio_io_error(bio);
1593 break;
1594 }
1595}
1596
Joe Thornbera374bb22014-10-10 13:43:14 +01001597static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1598{
1599 __process_bio_read_only(tc, bio, NULL);
1600}
1601
1602static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1603{
1604 __process_bio_read_only(tc, cell->holder, cell);
1605}
1606
Joe Thornber3e1a0692014-03-03 16:03:26 +00001607static void process_bio_success(struct thin_c *tc, struct bio *bio)
1608{
1609 bio_endio(bio, 0);
1610}
1611
Joe Thornbere49e5822012-07-27 15:08:16 +01001612static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1613{
1614 bio_io_error(bio);
1615}
1616
Joe Thornbera374bb22014-10-10 13:43:14 +01001617static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1618{
1619 cell_success(tc->pool, cell);
1620}
1621
1622static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1623{
1624 cell_error(tc->pool, cell);
1625}
1626
Joe Thornberac8c3f32013-05-10 14:37:21 +01001627/*
1628 * FIXME: should we also commit due to size of transaction, measured in
1629 * metadata blocks?
1630 */
Joe Thornber905e51b2012-03-28 18:41:27 +01001631static int need_commit_due_to_time(struct pool *pool)
1632{
1633 return jiffies < pool->last_commit_jiffies ||
1634 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1635}
1636
Mike Snitzer67324ea2014-03-21 18:33:41 -04001637#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1638#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1639
1640static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1641{
1642 struct rb_node **rbp, *parent;
1643 struct dm_thin_endio_hook *pbd;
1644 sector_t bi_sector = bio->bi_iter.bi_sector;
1645
1646 rbp = &tc->sort_bio_list.rb_node;
1647 parent = NULL;
1648 while (*rbp) {
1649 parent = *rbp;
1650 pbd = thin_pbd(parent);
1651
1652 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1653 rbp = &(*rbp)->rb_left;
1654 else
1655 rbp = &(*rbp)->rb_right;
1656 }
1657
1658 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1659 rb_link_node(&pbd->rb_node, parent, rbp);
1660 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
1661}
1662
1663static void __extract_sorted_bios(struct thin_c *tc)
1664{
1665 struct rb_node *node;
1666 struct dm_thin_endio_hook *pbd;
1667 struct bio *bio;
1668
1669 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
1670 pbd = thin_pbd(node);
1671 bio = thin_bio(pbd);
1672
1673 bio_list_add(&tc->deferred_bio_list, bio);
1674 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
1675 }
1676
1677 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
1678}
1679
1680static void __sort_thin_deferred_bios(struct thin_c *tc)
1681{
1682 struct bio *bio;
1683 struct bio_list bios;
1684
1685 bio_list_init(&bios);
1686 bio_list_merge(&bios, &tc->deferred_bio_list);
1687 bio_list_init(&tc->deferred_bio_list);
1688
1689 /* Sort deferred_bio_list using rb-tree */
1690 while ((bio = bio_list_pop(&bios)))
1691 __thin_bio_rb_add(tc, bio);
1692
1693 /*
1694 * Transfer the sorted bios in sort_bio_list back to
1695 * deferred_bio_list to allow lockless submission of
1696 * all bios.
1697 */
1698 __extract_sorted_bios(tc);
1699}
1700
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001701static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001702{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001703 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001704 unsigned long flags;
1705 struct bio *bio;
1706 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04001707 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001708 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001709
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001710 if (tc->requeue_mode) {
1711 requeue_bio_list(tc, &tc->deferred_bio_list);
1712 return;
1713 }
1714
Joe Thornber991d9fa2011-10-31 20:21:18 +00001715 bio_list_init(&bios);
1716
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001717 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001718
1719 if (bio_list_empty(&tc->deferred_bio_list)) {
1720 spin_unlock_irqrestore(&tc->lock, flags);
1721 return;
1722 }
1723
1724 __sort_thin_deferred_bios(tc);
1725
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001726 bio_list_merge(&bios, &tc->deferred_bio_list);
1727 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001728
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001729 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001730
Mike Snitzer67324ea2014-03-21 18:33:41 -04001731 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001732 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001733 /*
1734 * If we've got no free new_mapping structs, and processing
1735 * this bio might require one, we pause until there are some
1736 * prepared mappings to process.
1737 */
1738 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001739 spin_lock_irqsave(&tc->lock, flags);
1740 bio_list_add(&tc->deferred_bio_list, bio);
1741 bio_list_merge(&tc->deferred_bio_list, &bios);
1742 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001743 break;
1744 }
Joe Thornber104655f2012-03-28 18:41:28 +01001745
1746 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01001747 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01001748 else
Joe Thornbere49e5822012-07-27 15:08:16 +01001749 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001750
1751 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01001752 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001753 dm_pool_issue_prefetches(pool->pmd);
1754 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001755 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04001756 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001757}
1758
Joe Thornbera374bb22014-10-10 13:43:14 +01001759static void process_thin_deferred_cells(struct thin_c *tc)
1760{
1761 struct pool *pool = tc->pool;
1762 unsigned long flags;
1763 struct list_head cells;
1764 struct dm_bio_prison_cell *cell, *tmp;
1765
1766 INIT_LIST_HEAD(&cells);
1767
1768 spin_lock_irqsave(&tc->lock, flags);
1769 list_splice_init(&tc->deferred_cells, &cells);
1770 spin_unlock_irqrestore(&tc->lock, flags);
1771
1772 if (list_empty(&cells))
1773 return;
1774
1775 list_for_each_entry_safe(cell, tmp, &cells, user_list) {
1776 BUG_ON(!cell->holder);
1777
1778 /*
1779 * If we've got no free new_mapping structs, and processing
1780 * this bio might require one, we pause until there are some
1781 * prepared mappings to process.
1782 */
1783 if (ensure_next_mapping(pool)) {
1784 spin_lock_irqsave(&tc->lock, flags);
1785 list_add(&cell->user_list, &tc->deferred_cells);
1786 list_splice(&cells, &tc->deferred_cells);
1787 spin_unlock_irqrestore(&tc->lock, flags);
1788 break;
1789 }
1790
1791 if (cell->holder->bi_rw & REQ_DISCARD)
1792 pool->process_discard_cell(tc, cell);
1793 else
1794 pool->process_cell(tc, cell);
1795 }
1796}
1797
Joe Thornberb10ebd32014-04-08 11:29:01 +01001798static void thin_get(struct thin_c *tc);
1799static void thin_put(struct thin_c *tc);
1800
1801/*
1802 * We can't hold rcu_read_lock() around code that can block. So we
1803 * find a thin with the rcu lock held; bump a refcount; then drop
1804 * the lock.
1805 */
1806static struct thin_c *get_first_thin(struct pool *pool)
1807{
1808 struct thin_c *tc = NULL;
1809
1810 rcu_read_lock();
1811 if (!list_empty(&pool->active_thins)) {
1812 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
1813 thin_get(tc);
1814 }
1815 rcu_read_unlock();
1816
1817 return tc;
1818}
1819
1820static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
1821{
1822 struct thin_c *old_tc = tc;
1823
1824 rcu_read_lock();
1825 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
1826 thin_get(tc);
1827 thin_put(old_tc);
1828 rcu_read_unlock();
1829 return tc;
1830 }
1831 thin_put(old_tc);
1832 rcu_read_unlock();
1833
1834 return NULL;
1835}
1836
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001837static void process_deferred_bios(struct pool *pool)
1838{
1839 unsigned long flags;
1840 struct bio *bio;
1841 struct bio_list bios;
1842 struct thin_c *tc;
1843
Joe Thornberb10ebd32014-04-08 11:29:01 +01001844 tc = get_first_thin(pool);
1845 while (tc) {
Joe Thornbera374bb22014-10-10 13:43:14 +01001846 process_thin_deferred_cells(tc);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001847 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01001848 tc = get_next_thin(pool, tc);
1849 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001850
1851 /*
1852 * If there are any deferred flush bios, we must commit
1853 * the metadata before issuing them.
1854 */
1855 bio_list_init(&bios);
1856 spin_lock_irqsave(&pool->lock, flags);
1857 bio_list_merge(&bios, &pool->deferred_flush_bios);
1858 bio_list_init(&pool->deferred_flush_bios);
1859 spin_unlock_irqrestore(&pool->lock, flags);
1860
Mike Snitzer4d1662a2014-02-06 06:08:56 -05001861 if (bio_list_empty(&bios) &&
1862 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001863 return;
1864
Joe Thornber020cc3b2013-12-04 15:05:36 -05001865 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001866 while ((bio = bio_list_pop(&bios)))
1867 bio_io_error(bio);
1868 return;
1869 }
Joe Thornber905e51b2012-03-28 18:41:27 +01001870 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001871
1872 while ((bio = bio_list_pop(&bios)))
1873 generic_make_request(bio);
1874}
1875
1876static void do_worker(struct work_struct *ws)
1877{
1878 struct pool *pool = container_of(ws, struct pool, worker);
1879
Joe Thornber7d327fe2014-10-06 15:45:59 +01001880 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001881 dm_pool_issue_prefetches(pool->pmd);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001882 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01001883 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001884 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01001885 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001886 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001887 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001888 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001889}
1890
Joe Thornber905e51b2012-03-28 18:41:27 +01001891/*
1892 * We want to commit periodically so that not too much
1893 * unwritten data builds up.
1894 */
1895static void do_waker(struct work_struct *ws)
1896{
1897 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1898 wake_worker(pool);
1899 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1900}
1901
Joe Thornber85ad643b2014-05-09 15:59:38 +01001902/*
1903 * We're holding onto IO to allow userland time to react. After the
1904 * timeout either the pool will have been resized (and thus back in
1905 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
1906 */
1907static void do_no_space_timeout(struct work_struct *ws)
1908{
1909 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
1910 no_space_timeout);
1911
1912 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
1913 set_pool_mode(pool, PM_READ_ONLY);
1914}
1915
Joe Thornber991d9fa2011-10-31 20:21:18 +00001916/*----------------------------------------------------------------*/
1917
Joe Thornbere7a3e872014-05-13 16:14:14 -04001918struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00001919 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04001920 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00001921};
1922
Joe Thornbere7a3e872014-05-13 16:14:14 -04001923static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00001924{
Joe Thornbere7a3e872014-05-13 16:14:14 -04001925 return container_of(ws, struct pool_work, worker);
1926}
1927
1928static void pool_work_complete(struct pool_work *pw)
1929{
1930 complete(&pw->complete);
1931}
1932
1933static void pool_work_wait(struct pool_work *pw, struct pool *pool,
1934 void (*fn)(struct work_struct *))
1935{
1936 INIT_WORK_ONSTACK(&pw->worker, fn);
1937 init_completion(&pw->complete);
1938 queue_work(pool->wq, &pw->worker);
1939 wait_for_completion(&pw->complete);
1940}
1941
1942/*----------------------------------------------------------------*/
1943
1944struct noflush_work {
1945 struct pool_work pw;
1946 struct thin_c *tc;
1947};
1948
1949static struct noflush_work *to_noflush(struct work_struct *ws)
1950{
1951 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00001952}
1953
1954static void do_noflush_start(struct work_struct *ws)
1955{
Joe Thornbere7a3e872014-05-13 16:14:14 -04001956 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00001957 w->tc->requeue_mode = true;
1958 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04001959 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00001960}
1961
1962static void do_noflush_stop(struct work_struct *ws)
1963{
Joe Thornbere7a3e872014-05-13 16:14:14 -04001964 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00001965 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04001966 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00001967}
1968
1969static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
1970{
1971 struct noflush_work w;
1972
Joe Thornber738211f2014-03-03 15:52:28 +00001973 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04001974 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00001975}
1976
1977/*----------------------------------------------------------------*/
1978
Joe Thornbere49e5822012-07-27 15:08:16 +01001979static enum pool_mode get_pool_mode(struct pool *pool)
1980{
1981 return pool->pf.mode;
1982}
1983
Joe Thornber3e1a0692014-03-03 16:03:26 +00001984static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
1985{
1986 dm_table_event(pool->ti->table);
1987 DMINFO("%s: switching pool to %s mode",
1988 dm_device_name(pool->pool_md), new_mode);
1989}
1990
Mike Snitzer8b64e882013-12-20 14:27:28 -05001991static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01001992{
Mike Snitzercdc2b412014-02-14 18:10:55 -05001993 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001994 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
1995 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer80c57892014-05-20 13:38:33 -04001996 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001997
1998 /*
1999 * Never allow the pool to transition to PM_WRITE mode if user
2000 * intervention is required to verify metadata and data consistency.
2001 */
2002 if (new_mode == PM_WRITE && needs_check) {
2003 DMERR("%s: unable to switch pool to write mode until repaired.",
2004 dm_device_name(pool->pool_md));
2005 if (old_mode != new_mode)
2006 new_mode = old_mode;
2007 else
2008 new_mode = PM_READ_ONLY;
2009 }
2010 /*
2011 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2012 * not going to recover without a thin_repair. So we never let the
2013 * pool move out of the old mode.
2014 */
2015 if (old_mode == PM_FAIL)
2016 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002017
Mike Snitzer8b64e882013-12-20 14:27:28 -05002018 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01002019 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002020 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002021 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05002022 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002023 pool->process_bio = process_bio_fail;
2024 pool->process_discard = process_bio_fail;
Joe Thornbera374bb22014-10-10 13:43:14 +01002025 pool->process_cell = process_cell_fail;
2026 pool->process_discard_cell = process_cell_fail;
Joe Thornbere49e5822012-07-27 15:08:16 +01002027 pool->process_prepared_mapping = process_prepared_mapping_fail;
2028 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002029
2030 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002031 break;
2032
2033 case PM_READ_ONLY:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002034 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002035 notify_of_pool_mode_change(pool, "read-only");
2036 dm_pool_metadata_read_only(pool->pmd);
2037 pool->process_bio = process_bio_read_only;
2038 pool->process_discard = process_bio_success;
Joe Thornbera374bb22014-10-10 13:43:14 +01002039 pool->process_cell = process_cell_read_only;
2040 pool->process_discard_cell = process_cell_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002041 pool->process_prepared_mapping = process_prepared_mapping_fail;
2042 pool->process_prepared_discard = process_prepared_discard_passdown;
2043
2044 error_retry_list(pool);
2045 break;
2046
2047 case PM_OUT_OF_DATA_SPACE:
2048 /*
2049 * Ideally we'd never hit this state; the low water mark
2050 * would trigger userland to extend the pool before we
2051 * completely run out of data space. However, many small
2052 * IOs to unprovisioned space can consume data space at an
2053 * alarming rate. Adjust your low water mark if you're
2054 * frequently seeing this mode.
2055 */
2056 if (old_mode != new_mode)
2057 notify_of_pool_mode_change(pool, "out-of-data-space");
2058 pool->process_bio = process_bio_read_only;
Joe Thornbera374bb22014-10-10 13:43:14 +01002059 pool->process_discard = process_discard_bio;
2060 pool->process_cell = process_cell_read_only;
2061 pool->process_discard_cell = process_discard_cell;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002062 pool->process_prepared_mapping = process_prepared_mapping;
2063 pool->process_prepared_discard = process_prepared_discard_passdown;
Joe Thornber85ad643b2014-05-09 15:59:38 +01002064
Mike Snitzer80c57892014-05-20 13:38:33 -04002065 if (!pool->pf.error_if_no_space && no_space_timeout)
2066 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01002067 break;
2068
2069 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002070 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002071 notify_of_pool_mode_change(pool, "write");
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002072 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002073 pool->process_bio = process_bio;
Joe Thornbera374bb22014-10-10 13:43:14 +01002074 pool->process_discard = process_discard_bio;
2075 pool->process_cell = process_cell;
2076 pool->process_discard_cell = process_discard_cell;
Joe Thornbere49e5822012-07-27 15:08:16 +01002077 pool->process_prepared_mapping = process_prepared_mapping;
2078 pool->process_prepared_discard = process_prepared_discard;
2079 break;
2080 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05002081
2082 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05002083 /*
2084 * The pool mode may have changed, sync it so bind_control_target()
2085 * doesn't cause an unexpected mode transition on resume.
2086 */
2087 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002088}
2089
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002090static void abort_transaction(struct pool *pool)
2091{
2092 const char *dev_name = dm_device_name(pool->pool_md);
2093
2094 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2095 if (dm_pool_abort_metadata(pool->pmd)) {
2096 DMERR("%s: failed to abort metadata transaction", dev_name);
2097 set_pool_mode(pool, PM_FAIL);
2098 }
2099
2100 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2101 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2102 set_pool_mode(pool, PM_FAIL);
2103 }
2104}
2105
Joe Thornberb5330652013-12-04 19:51:33 -05002106static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2107{
2108 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2109 dm_device_name(pool->pool_md), op, r);
2110
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002111 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05002112 set_pool_mode(pool, PM_READ_ONLY);
2113}
2114
Joe Thornbere49e5822012-07-27 15:08:16 +01002115/*----------------------------------------------------------------*/
2116
Joe Thornber991d9fa2011-10-31 20:21:18 +00002117/*
2118 * Mapping functions.
2119 */
2120
2121/*
2122 * Called only while mapping a thin bio to hand it over to the workqueue.
2123 */
2124static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2125{
2126 unsigned long flags;
2127 struct pool *pool = tc->pool;
2128
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002129 spin_lock_irqsave(&tc->lock, flags);
2130 bio_list_add(&tc->deferred_bio_list, bio);
2131 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002132
2133 wake_worker(pool);
2134}
2135
Joe Thornber7d327fe2014-10-06 15:45:59 +01002136static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2137{
2138 struct pool *pool = tc->pool;
2139
2140 throttle_lock(&pool->throttle);
2141 thin_defer_bio(tc, bio);
2142 throttle_unlock(&pool->throttle);
2143}
2144
Joe Thornbera374bb22014-10-10 13:43:14 +01002145static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2146{
2147 unsigned long flags;
2148 struct pool *pool = tc->pool;
2149
2150 throttle_lock(&pool->throttle);
2151 spin_lock_irqsave(&tc->lock, flags);
2152 list_add_tail(&cell->user_list, &tc->deferred_cells);
2153 spin_unlock_irqrestore(&tc->lock, flags);
2154 throttle_unlock(&pool->throttle);
2155
2156 wake_worker(pool);
2157}
2158
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002159static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002160{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002161 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002162
2163 h->tc = tc;
2164 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00002165 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002166 h->overwrite_mapping = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002167}
2168
Joe Thornber991d9fa2011-10-31 20:21:18 +00002169/*
2170 * Non-blocking function called from the thin target's map function.
2171 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002172static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002173{
2174 int r;
2175 struct thin_c *tc = ti->private;
2176 dm_block_t block = get_bio_block(tc, bio);
2177 struct dm_thin_device *td = tc->td;
2178 struct dm_thin_lookup_result result;
Joe Thornbera374bb22014-10-10 13:43:14 +01002179 struct dm_bio_prison_cell *virt_cell, *data_cell;
Joe Thornbere8088072012-12-21 20:23:31 +00002180 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002181
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002182 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002183
Joe Thornber738211f2014-03-03 15:52:28 +00002184 if (tc->requeue_mode) {
2185 bio_endio(bio, DM_ENDIO_REQUEUE);
2186 return DM_MAPIO_SUBMITTED;
2187 }
2188
Joe Thornbere49e5822012-07-27 15:08:16 +01002189 if (get_pool_mode(tc->pool) == PM_FAIL) {
2190 bio_io_error(bio);
2191 return DM_MAPIO_SUBMITTED;
2192 }
2193
Joe Thornber104655f2012-03-28 18:41:28 +01002194 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002195 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002196 return DM_MAPIO_SUBMITTED;
2197 }
2198
Joe Thornberc822ed92014-10-10 09:41:09 +01002199 /*
2200 * We must hold the virtual cell before doing the lookup, otherwise
2201 * there's a race with discard.
2202 */
2203 build_virtual_key(tc->td, block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002204 if (bio_detain(tc->pool, &key, bio, &virt_cell))
Joe Thornberc822ed92014-10-10 09:41:09 +01002205 return DM_MAPIO_SUBMITTED;
2206
Joe Thornber991d9fa2011-10-31 20:21:18 +00002207 r = dm_thin_find_block(td, block, 0, &result);
2208
2209 /*
2210 * Note that we defer readahead too.
2211 */
2212 switch (r) {
2213 case 0:
2214 if (unlikely(result.shared)) {
2215 /*
2216 * We have a race condition here between the
2217 * result.shared value returned by the lookup and
2218 * snapshot creation, which may cause new
2219 * sharing.
2220 *
2221 * To avoid this always quiesce the origin before
2222 * taking the snap. You want to do this anyway to
2223 * ensure a consistent application view
2224 * (i.e. lockfs).
2225 *
2226 * More distant ancestors are irrelevant. The
2227 * shared flag will be set in their case.
2228 */
Joe Thornbera374bb22014-10-10 13:43:14 +01002229 thin_defer_cell(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002230 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002231 }
Joe Thornbere8088072012-12-21 20:23:31 +00002232
Joe Thornbere8088072012-12-21 20:23:31 +00002233 build_data_key(tc->td, result.block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002234 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2235 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002236 return DM_MAPIO_SUBMITTED;
2237 }
2238
2239 inc_all_io_entry(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002240 cell_defer_no_holder(tc, data_cell);
2241 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002242
2243 remap(tc, bio, result.block);
2244 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002245
2246 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002247 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
2248 /*
2249 * This block isn't provisioned, and we have no way
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05002250 * of doing so.
Joe Thornbere49e5822012-07-27 15:08:16 +01002251 */
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05002252 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002253 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002254 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002255 }
2256 /* fall through */
2257
2258 case -EWOULDBLOCK:
Joe Thornbera374bb22014-10-10 13:43:14 +01002259 thin_defer_cell(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002260 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002261
2262 default:
2263 /*
2264 * Must always call bio_io_error on failure.
2265 * dm_thin_find_block can fail with -EINVAL if the
2266 * pool is switched to fail-io mode.
2267 */
2268 bio_io_error(bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002269 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002270 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002271 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002272}
2273
2274static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2275{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002276 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04002277 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002278
Mike Snitzer760fe672014-03-20 08:36:47 -04002279 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2280 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002281
Mike Snitzer760fe672014-03-20 08:36:47 -04002282 q = bdev_get_queue(pt->data_dev->bdev);
2283 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002284}
2285
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002286static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002287{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002288 unsigned long flags;
2289 struct thin_c *tc;
2290
2291 rcu_read_lock();
2292 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2293 spin_lock_irqsave(&tc->lock, flags);
2294 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2295 bio_list_init(&tc->retry_on_resume_list);
2296 spin_unlock_irqrestore(&tc->lock, flags);
2297 }
2298 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002299}
2300
2301/*----------------------------------------------------------------
2302 * Binding of control targets to a pool object
2303 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002304static bool data_dev_supports_discard(struct pool_c *pt)
2305{
2306 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2307
2308 return q && blk_queue_discard(q);
2309}
2310
Joe Thornber58051b92013-03-20 17:21:25 +00002311static bool is_factor(sector_t block_size, uint32_t n)
2312{
2313 return !sector_div(block_size, n);
2314}
2315
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002316/*
2317 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002318 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002319 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002320static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002321{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002322 struct pool *pool = pt->pool;
2323 struct block_device *data_bdev = pt->data_dev->bdev;
2324 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
2325 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
2326 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002327 char buf[BDEVNAME_SIZE];
2328
Mike Snitzer0424caa2012-09-26 23:45:47 +01002329 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002330 return;
2331
Mike Snitzer0424caa2012-09-26 23:45:47 +01002332 if (!data_dev_supports_discard(pt))
2333 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002334
Mike Snitzer0424caa2012-09-26 23:45:47 +01002335 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2336 reason = "max discard sectors smaller than a block";
2337
2338 else if (data_limits->discard_granularity > block_size)
2339 reason = "discard granularity larger than a block";
2340
Joe Thornber58051b92013-03-20 17:21:25 +00002341 else if (!is_factor(block_size, data_limits->discard_granularity))
Mike Snitzer0424caa2012-09-26 23:45:47 +01002342 reason = "discard granularity not a factor of block size";
2343
2344 if (reason) {
2345 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2346 pt->adjusted_pf.discard_passdown = false;
2347 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002348}
2349
Joe Thornber991d9fa2011-10-31 20:21:18 +00002350static int bind_control_target(struct pool *pool, struct dm_target *ti)
2351{
2352 struct pool_c *pt = ti->private;
2353
Joe Thornbere49e5822012-07-27 15:08:16 +01002354 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002355 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002356 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002357 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002358 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002359
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002360 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002361 * Don't change the pool's mode until set_pool_mode() below.
2362 * Otherwise the pool's process_* function pointers may
2363 * not match the desired pool mode.
2364 */
2365 pt->adjusted_pf.mode = old_mode;
2366
2367 pool->ti = ti;
2368 pool->pf = pt->adjusted_pf;
2369 pool->low_water_blocks = pt->low_water_blocks;
2370
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002371 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002372
Joe Thornber991d9fa2011-10-31 20:21:18 +00002373 return 0;
2374}
2375
2376static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2377{
2378 if (pool->ti == ti)
2379 pool->ti = NULL;
2380}
2381
2382/*----------------------------------------------------------------
2383 * Pool creation
2384 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002385/* Initialize pool features. */
2386static void pool_features_init(struct pool_features *pf)
2387{
Joe Thornbere49e5822012-07-27 15:08:16 +01002388 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002389 pf->zero_new_blocks = true;
2390 pf->discard_enabled = true;
2391 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002392 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002393}
2394
Joe Thornber991d9fa2011-10-31 20:21:18 +00002395static void __pool_destroy(struct pool *pool)
2396{
2397 __pool_table_remove(pool);
2398
2399 if (dm_pool_metadata_close(pool->pmd) < 0)
2400 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2401
Mike Snitzer44feb382012-10-12 21:02:10 +01002402 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002403 dm_kcopyd_client_destroy(pool->copier);
2404
2405 if (pool->wq)
2406 destroy_workqueue(pool->wq);
2407
2408 if (pool->next_mapping)
2409 mempool_free(pool->next_mapping, pool->mapping_pool);
2410 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01002411 dm_deferred_set_destroy(pool->shared_read_ds);
2412 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002413 kfree(pool);
2414}
2415
Mike Snitzera24c2562012-06-03 00:30:00 +01002416static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002417
Joe Thornber991d9fa2011-10-31 20:21:18 +00002418static struct pool *pool_create(struct mapped_device *pool_md,
2419 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002420 unsigned long block_size,
2421 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002422{
2423 int r;
2424 void *err_p;
2425 struct pool *pool;
2426 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002427 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002428
Joe Thornbere49e5822012-07-27 15:08:16 +01002429 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002430 if (IS_ERR(pmd)) {
2431 *error = "Error creating metadata object";
2432 return (struct pool *)pmd;
2433 }
2434
2435 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2436 if (!pool) {
2437 *error = "Error allocating memory for pool";
2438 err_p = ERR_PTR(-ENOMEM);
2439 goto bad_pool;
2440 }
2441
2442 pool->pmd = pmd;
2443 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002444 if (block_size & (block_size - 1))
2445 pool->sectors_per_block_shift = -1;
2446 else
2447 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002448 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002449 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002450 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002451 if (!pool->prison) {
2452 *error = "Error creating pool's bio prison";
2453 err_p = ERR_PTR(-ENOMEM);
2454 goto bad_prison;
2455 }
2456
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002457 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002458 if (IS_ERR(pool->copier)) {
2459 r = PTR_ERR(pool->copier);
2460 *error = "Error creating pool's kcopyd client";
2461 err_p = ERR_PTR(r);
2462 goto bad_kcopyd_client;
2463 }
2464
2465 /*
2466 * Create singlethreaded workqueue that will service all devices
2467 * that use this metadata.
2468 */
2469 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2470 if (!pool->wq) {
2471 *error = "Error creating pool's workqueue";
2472 err_p = ERR_PTR(-ENOMEM);
2473 goto bad_wq;
2474 }
2475
Joe Thornber7d327fe2014-10-06 15:45:59 +01002476 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002477 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002478 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002479 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002480 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002481 bio_list_init(&pool->deferred_flush_bios);
2482 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002483 INIT_LIST_HEAD(&pool->prepared_discards);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002484 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002485 pool->low_water_triggered = false;
Mike Snitzer44feb382012-10-12 21:02:10 +01002486
2487 pool->shared_read_ds = dm_deferred_set_create();
2488 if (!pool->shared_read_ds) {
2489 *error = "Error creating pool's shared read deferred set";
2490 err_p = ERR_PTR(-ENOMEM);
2491 goto bad_shared_read_ds;
2492 }
2493
2494 pool->all_io_ds = dm_deferred_set_create();
2495 if (!pool->all_io_ds) {
2496 *error = "Error creating pool's all io deferred set";
2497 err_p = ERR_PTR(-ENOMEM);
2498 goto bad_all_io_ds;
2499 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002500
2501 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002502 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2503 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002504 if (!pool->mapping_pool) {
2505 *error = "Error creating pool's mapping mempool";
2506 err_p = ERR_PTR(-ENOMEM);
2507 goto bad_mapping_pool;
2508 }
2509
Joe Thornber991d9fa2011-10-31 20:21:18 +00002510 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01002511 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002512 pool->pool_md = pool_md;
2513 pool->md_dev = metadata_dev;
2514 __pool_table_insert(pool);
2515
2516 return pool;
2517
Joe Thornber991d9fa2011-10-31 20:21:18 +00002518bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01002519 dm_deferred_set_destroy(pool->all_io_ds);
2520bad_all_io_ds:
2521 dm_deferred_set_destroy(pool->shared_read_ds);
2522bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002523 destroy_workqueue(pool->wq);
2524bad_wq:
2525 dm_kcopyd_client_destroy(pool->copier);
2526bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01002527 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002528bad_prison:
2529 kfree(pool);
2530bad_pool:
2531 if (dm_pool_metadata_close(pmd))
2532 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2533
2534 return err_p;
2535}
2536
2537static void __pool_inc(struct pool *pool)
2538{
2539 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2540 pool->ref_count++;
2541}
2542
2543static void __pool_dec(struct pool *pool)
2544{
2545 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2546 BUG_ON(!pool->ref_count);
2547 if (!--pool->ref_count)
2548 __pool_destroy(pool);
2549}
2550
2551static struct pool *__pool_find(struct mapped_device *pool_md,
2552 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002553 unsigned long block_size, int read_only,
2554 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002555{
2556 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2557
2558 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002559 if (pool->pool_md != pool_md) {
2560 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002561 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002562 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002563 __pool_inc(pool);
2564
2565 } else {
2566 pool = __pool_table_lookup(pool_md);
2567 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002568 if (pool->md_dev != metadata_dev) {
2569 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002570 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002571 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002572 __pool_inc(pool);
2573
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002574 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01002575 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002576 *created = 1;
2577 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002578 }
2579
2580 return pool;
2581}
2582
2583/*----------------------------------------------------------------
2584 * Pool target methods
2585 *--------------------------------------------------------------*/
2586static void pool_dtr(struct dm_target *ti)
2587{
2588 struct pool_c *pt = ti->private;
2589
2590 mutex_lock(&dm_thin_pool_table.mutex);
2591
2592 unbind_control_target(pt->pool, ti);
2593 __pool_dec(pt->pool);
2594 dm_put_device(ti, pt->metadata_dev);
2595 dm_put_device(ti, pt->data_dev);
2596 kfree(pt);
2597
2598 mutex_unlock(&dm_thin_pool_table.mutex);
2599}
2600
Joe Thornber991d9fa2011-10-31 20:21:18 +00002601static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2602 struct dm_target *ti)
2603{
2604 int r;
2605 unsigned argc;
2606 const char *arg_name;
2607
2608 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05002609 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002610 };
2611
2612 /*
2613 * No feature arguments supplied.
2614 */
2615 if (!as->argc)
2616 return 0;
2617
2618 r = dm_read_arg_group(_args, as, &argc, &ti->error);
2619 if (r)
2620 return -EINVAL;
2621
2622 while (argc && !r) {
2623 arg_name = dm_shift_arg(as);
2624 argc--;
2625
Joe Thornbere49e5822012-07-27 15:08:16 +01002626 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002627 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002628
Joe Thornbere49e5822012-07-27 15:08:16 +01002629 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002630 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002631
2632 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002633 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002634
2635 else if (!strcasecmp(arg_name, "read_only"))
2636 pf->mode = PM_READ_ONLY;
2637
Mike Snitzer787a996c2013-12-06 16:21:43 -05002638 else if (!strcasecmp(arg_name, "error_if_no_space"))
2639 pf->error_if_no_space = true;
2640
Joe Thornbere49e5822012-07-27 15:08:16 +01002641 else {
2642 ti->error = "Unrecognised pool feature requested";
2643 r = -EINVAL;
2644 break;
2645 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002646 }
2647
2648 return r;
2649}
2650
Joe Thornberac8c3f32013-05-10 14:37:21 +01002651static void metadata_low_callback(void *context)
2652{
2653 struct pool *pool = context;
2654
2655 DMWARN("%s: reached low water mark for metadata device: sending event.",
2656 dm_device_name(pool->pool_md));
2657
2658 dm_table_event(pool->ti->table);
2659}
2660
Mike Snitzer7d489352014-02-12 23:58:15 -05002661static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01002662{
Mike Snitzer7d489352014-02-12 23:58:15 -05002663 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
2664}
2665
2666static void warn_if_metadata_device_too_big(struct block_device *bdev)
2667{
2668 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01002669 char buffer[BDEVNAME_SIZE];
2670
Mike Snitzer7d489352014-02-12 23:58:15 -05002671 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01002672 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2673 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05002674}
2675
2676static sector_t get_metadata_dev_size(struct block_device *bdev)
2677{
2678 sector_t metadata_dev_size = get_dev_size(bdev);
2679
2680 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
2681 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01002682
2683 return metadata_dev_size;
2684}
2685
Joe Thornber24347e92013-05-10 14:37:19 +01002686static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
2687{
2688 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
2689
Mike Snitzer7d489352014-02-12 23:58:15 -05002690 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01002691
2692 return metadata_dev_size;
2693}
2694
Joe Thornber991d9fa2011-10-31 20:21:18 +00002695/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01002696 * When a metadata threshold is crossed a dm event is triggered, and
2697 * userland should respond by growing the metadata device. We could let
2698 * userland set the threshold, like we do with the data threshold, but I'm
2699 * not sure they know enough to do this well.
2700 */
2701static dm_block_t calc_metadata_threshold(struct pool_c *pt)
2702{
2703 /*
2704 * 4M is ample for all ops with the possible exception of thin
2705 * device deletion which is harmless if it fails (just retry the
2706 * delete after you've grown the device).
2707 */
2708 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
2709 return min((dm_block_t)1024ULL /* 4M */, quarter);
2710}
2711
2712/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00002713 * thin-pool <metadata dev> <data dev>
2714 * <data block size (sectors)>
2715 * <low water mark (blocks)>
2716 * [<#feature args> [<arg>]*]
2717 *
2718 * Optional feature arguments are:
2719 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002720 * ignore_discard: disable discard
2721 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05002722 * read_only: Don't allow any changes to be made to the pool metadata.
2723 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002724 */
2725static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
2726{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002727 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002728 struct pool_c *pt;
2729 struct pool *pool;
2730 struct pool_features pf;
2731 struct dm_arg_set as;
2732 struct dm_dev *data_dev;
2733 unsigned long block_size;
2734 dm_block_t low_water_blocks;
2735 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01002736 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002737
2738 /*
2739 * FIXME Remove validation from scope of lock.
2740 */
2741 mutex_lock(&dm_thin_pool_table.mutex);
2742
2743 if (argc < 4) {
2744 ti->error = "Invalid argument count";
2745 r = -EINVAL;
2746 goto out_unlock;
2747 }
Joe Thornber5d0db962013-05-10 14:37:19 +01002748
Joe Thornber991d9fa2011-10-31 20:21:18 +00002749 as.argc = argc;
2750 as.argv = argv;
2751
Joe Thornber5d0db962013-05-10 14:37:19 +01002752 /*
2753 * Set default pool features.
2754 */
2755 pool_features_init(&pf);
2756
2757 dm_consume_args(&as, 4);
2758 r = parse_pool_features(&as, &pf, ti);
2759 if (r)
2760 goto out_unlock;
2761
2762 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
2763 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002764 if (r) {
2765 ti->error = "Error opening metadata block device";
2766 goto out_unlock;
2767 }
Mike Snitzer7d489352014-02-12 23:58:15 -05002768 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002769
2770 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
2771 if (r) {
2772 ti->error = "Error getting data device";
2773 goto out_metadata;
2774 }
2775
2776 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
2777 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2778 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002779 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002780 ti->error = "Invalid block size";
2781 r = -EINVAL;
2782 goto out;
2783 }
2784
2785 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
2786 ti->error = "Invalid low water mark";
2787 r = -EINVAL;
2788 goto out;
2789 }
2790
Joe Thornber991d9fa2011-10-31 20:21:18 +00002791 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
2792 if (!pt) {
2793 r = -ENOMEM;
2794 goto out;
2795 }
2796
2797 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002798 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002799 if (IS_ERR(pool)) {
2800 r = PTR_ERR(pool);
2801 goto out_free_pt;
2802 }
2803
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002804 /*
2805 * 'pool_created' reflects whether this is the first table load.
2806 * Top level discard support is not allowed to be changed after
2807 * initial load. This would require a pool reload to trigger thin
2808 * device changes.
2809 */
2810 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
2811 ti->error = "Discard support cannot be disabled once enabled";
2812 r = -EINVAL;
2813 goto out_flags_changed;
2814 }
2815
Joe Thornber991d9fa2011-10-31 20:21:18 +00002816 pt->pool = pool;
2817 pt->ti = ti;
2818 pt->metadata_dev = metadata_dev;
2819 pt->data_dev = data_dev;
2820 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002821 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002822 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002823
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002824 /*
2825 * Only need to enable discards if the pool should pass
2826 * them down to the data device. The thin device's discard
2827 * processing will cause mappings to be removed from the btree.
2828 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04002829 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002830 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002831 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002832
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002833 /*
2834 * Setting 'discards_supported' circumvents the normal
2835 * stacking of discard limits (this keeps the pool and
2836 * thin devices' discard limits consistent).
2837 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002838 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002839 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002840 ti->private = pt;
2841
Joe Thornberac8c3f32013-05-10 14:37:21 +01002842 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
2843 calc_metadata_threshold(pt),
2844 metadata_low_callback,
2845 pool);
2846 if (r)
2847 goto out_free_pt;
2848
Joe Thornber991d9fa2011-10-31 20:21:18 +00002849 pt->callbacks.congested_fn = pool_is_congested;
2850 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
2851
2852 mutex_unlock(&dm_thin_pool_table.mutex);
2853
2854 return 0;
2855
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002856out_flags_changed:
2857 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002858out_free_pt:
2859 kfree(pt);
2860out:
2861 dm_put_device(ti, data_dev);
2862out_metadata:
2863 dm_put_device(ti, metadata_dev);
2864out_unlock:
2865 mutex_unlock(&dm_thin_pool_table.mutex);
2866
2867 return r;
2868}
2869
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002870static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002871{
2872 int r;
2873 struct pool_c *pt = ti->private;
2874 struct pool *pool = pt->pool;
2875 unsigned long flags;
2876
2877 /*
2878 * As this is a singleton target, ti->begin is always zero.
2879 */
2880 spin_lock_irqsave(&pool->lock, flags);
2881 bio->bi_bdev = pt->data_dev->bdev;
2882 r = DM_MAPIO_REMAPPED;
2883 spin_unlock_irqrestore(&pool->lock, flags);
2884
2885 return r;
2886}
2887
Joe Thornberb17446d2013-05-10 14:37:18 +01002888static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
2889{
2890 int r;
2891 struct pool_c *pt = ti->private;
2892 struct pool *pool = pt->pool;
2893 sector_t data_size = ti->len;
2894 dm_block_t sb_data_size;
2895
2896 *need_commit = false;
2897
2898 (void) sector_div(data_size, pool->sectors_per_block);
2899
2900 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2901 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002902 DMERR("%s: failed to retrieve data device size",
2903 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01002904 return r;
2905 }
2906
2907 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002908 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
2909 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01002910 (unsigned long long)data_size, sb_data_size);
2911 return -EINVAL;
2912
2913 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002914 if (dm_pool_metadata_needs_check(pool->pmd)) {
2915 DMERR("%s: unable to grow the data device until repaired.",
2916 dm_device_name(pool->pool_md));
2917 return 0;
2918 }
2919
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05002920 if (sb_data_size)
2921 DMINFO("%s: growing the data device from %llu to %llu blocks",
2922 dm_device_name(pool->pool_md),
2923 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01002924 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2925 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05002926 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01002927 return r;
2928 }
2929
2930 *need_commit = true;
2931 }
2932
2933 return 0;
2934}
2935
Joe Thornber24347e92013-05-10 14:37:19 +01002936static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
2937{
2938 int r;
2939 struct pool_c *pt = ti->private;
2940 struct pool *pool = pt->pool;
2941 dm_block_t metadata_dev_size, sb_metadata_dev_size;
2942
2943 *need_commit = false;
2944
Alasdair G Kergon610bba82013-05-19 18:57:50 +01002945 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01002946
2947 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
2948 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002949 DMERR("%s: failed to retrieve metadata device size",
2950 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01002951 return r;
2952 }
2953
2954 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002955 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
2956 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01002957 metadata_dev_size, sb_metadata_dev_size);
2958 return -EINVAL;
2959
2960 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002961 if (dm_pool_metadata_needs_check(pool->pmd)) {
2962 DMERR("%s: unable to grow the metadata device until repaired.",
2963 dm_device_name(pool->pool_md));
2964 return 0;
2965 }
2966
Mike Snitzer7d489352014-02-12 23:58:15 -05002967 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05002968 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
2969 dm_device_name(pool->pool_md),
2970 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber24347e92013-05-10 14:37:19 +01002971 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
2972 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05002973 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01002974 return r;
2975 }
2976
2977 *need_commit = true;
2978 }
2979
2980 return 0;
2981}
2982
Joe Thornber991d9fa2011-10-31 20:21:18 +00002983/*
2984 * Retrieves the number of blocks of the data device from
2985 * the superblock and compares it to the actual device size,
2986 * thus resizing the data device in case it has grown.
2987 *
2988 * This both copes with opening preallocated data devices in the ctr
2989 * being followed by a resume
2990 * -and-
2991 * calling the resume method individually after userspace has
2992 * grown the data device in reaction to a table event.
2993 */
2994static int pool_preresume(struct dm_target *ti)
2995{
2996 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01002997 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002998 struct pool_c *pt = ti->private;
2999 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003000
3001 /*
3002 * Take control of the pool object.
3003 */
3004 r = bind_control_target(pool, ti);
3005 if (r)
3006 return r;
3007
Joe Thornberb17446d2013-05-10 14:37:18 +01003008 r = maybe_resize_data_dev(ti, &need_commit1);
3009 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003010 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003011
Joe Thornber24347e92013-05-10 14:37:19 +01003012 r = maybe_resize_metadata_dev(ti, &need_commit2);
3013 if (r)
3014 return r;
3015
3016 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003017 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003018
3019 return 0;
3020}
3021
3022static void pool_resume(struct dm_target *ti)
3023{
3024 struct pool_c *pt = ti->private;
3025 struct pool *pool = pt->pool;
3026 unsigned long flags;
3027
3028 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05003029 pool->low_water_triggered = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003030 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003031 requeue_bios(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003032
Joe Thornber905e51b2012-03-28 18:41:27 +01003033 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003034}
3035
3036static void pool_postsuspend(struct dm_target *ti)
3037{
Joe Thornber991d9fa2011-10-31 20:21:18 +00003038 struct pool_c *pt = ti->private;
3039 struct pool *pool = pt->pool;
3040
Joe Thornber905e51b2012-03-28 18:41:27 +01003041 cancel_delayed_work(&pool->waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01003042 cancel_delayed_work(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003043 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05003044 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003045}
3046
3047static int check_arg_count(unsigned argc, unsigned args_required)
3048{
3049 if (argc != args_required) {
3050 DMWARN("Message received with %u arguments instead of %u.",
3051 argc, args_required);
3052 return -EINVAL;
3053 }
3054
3055 return 0;
3056}
3057
3058static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3059{
3060 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3061 *dev_id <= MAX_DEV_ID)
3062 return 0;
3063
3064 if (warning)
3065 DMWARN("Message received with invalid device id: %s", arg);
3066
3067 return -EINVAL;
3068}
3069
3070static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3071{
3072 dm_thin_id dev_id;
3073 int r;
3074
3075 r = check_arg_count(argc, 2);
3076 if (r)
3077 return r;
3078
3079 r = read_dev_id(argv[1], &dev_id, 1);
3080 if (r)
3081 return r;
3082
3083 r = dm_pool_create_thin(pool->pmd, dev_id);
3084 if (r) {
3085 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3086 argv[1]);
3087 return r;
3088 }
3089
3090 return 0;
3091}
3092
3093static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3094{
3095 dm_thin_id dev_id;
3096 dm_thin_id origin_dev_id;
3097 int r;
3098
3099 r = check_arg_count(argc, 3);
3100 if (r)
3101 return r;
3102
3103 r = read_dev_id(argv[1], &dev_id, 1);
3104 if (r)
3105 return r;
3106
3107 r = read_dev_id(argv[2], &origin_dev_id, 1);
3108 if (r)
3109 return r;
3110
3111 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3112 if (r) {
3113 DMWARN("Creation of new snapshot %s of device %s failed.",
3114 argv[1], argv[2]);
3115 return r;
3116 }
3117
3118 return 0;
3119}
3120
3121static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3122{
3123 dm_thin_id dev_id;
3124 int r;
3125
3126 r = check_arg_count(argc, 2);
3127 if (r)
3128 return r;
3129
3130 r = read_dev_id(argv[1], &dev_id, 1);
3131 if (r)
3132 return r;
3133
3134 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3135 if (r)
3136 DMWARN("Deletion of thin device %s failed.", argv[1]);
3137
3138 return r;
3139}
3140
3141static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3142{
3143 dm_thin_id old_id, new_id;
3144 int r;
3145
3146 r = check_arg_count(argc, 3);
3147 if (r)
3148 return r;
3149
3150 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3151 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3152 return -EINVAL;
3153 }
3154
3155 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3156 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3157 return -EINVAL;
3158 }
3159
3160 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3161 if (r) {
3162 DMWARN("Failed to change transaction id from %s to %s.",
3163 argv[1], argv[2]);
3164 return r;
3165 }
3166
3167 return 0;
3168}
3169
Joe Thornbercc8394d2012-06-03 00:30:01 +01003170static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3171{
3172 int r;
3173
3174 r = check_arg_count(argc, 1);
3175 if (r)
3176 return r;
3177
Joe Thornber020cc3b2013-12-04 15:05:36 -05003178 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01003179
Joe Thornbercc8394d2012-06-03 00:30:01 +01003180 r = dm_pool_reserve_metadata_snap(pool->pmd);
3181 if (r)
3182 DMWARN("reserve_metadata_snap message failed.");
3183
3184 return r;
3185}
3186
3187static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3188{
3189 int r;
3190
3191 r = check_arg_count(argc, 1);
3192 if (r)
3193 return r;
3194
3195 r = dm_pool_release_metadata_snap(pool->pmd);
3196 if (r)
3197 DMWARN("release_metadata_snap message failed.");
3198
3199 return r;
3200}
3201
Joe Thornber991d9fa2011-10-31 20:21:18 +00003202/*
3203 * Messages supported:
3204 * create_thin <dev_id>
3205 * create_snap <dev_id> <origin_id>
3206 * delete <dev_id>
3207 * trim <dev_id> <new_size_in_sectors>
3208 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003209 * reserve_metadata_snap
3210 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003211 */
3212static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3213{
3214 int r = -EINVAL;
3215 struct pool_c *pt = ti->private;
3216 struct pool *pool = pt->pool;
3217
3218 if (!strcasecmp(argv[0], "create_thin"))
3219 r = process_create_thin_mesg(argc, argv, pool);
3220
3221 else if (!strcasecmp(argv[0], "create_snap"))
3222 r = process_create_snap_mesg(argc, argv, pool);
3223
3224 else if (!strcasecmp(argv[0], "delete"))
3225 r = process_delete_mesg(argc, argv, pool);
3226
3227 else if (!strcasecmp(argv[0], "set_transaction_id"))
3228 r = process_set_transaction_id_mesg(argc, argv, pool);
3229
Joe Thornbercc8394d2012-06-03 00:30:01 +01003230 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3231 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3232
3233 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3234 r = process_release_metadata_snap_mesg(argc, argv, pool);
3235
Joe Thornber991d9fa2011-10-31 20:21:18 +00003236 else
3237 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3238
Joe Thornbere49e5822012-07-27 15:08:16 +01003239 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003240 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003241
3242 return r;
3243}
3244
Joe Thornbere49e5822012-07-27 15:08:16 +01003245static void emit_flags(struct pool_features *pf, char *result,
3246 unsigned sz, unsigned maxlen)
3247{
3248 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003249 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3250 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003251 DMEMIT("%u ", count);
3252
3253 if (!pf->zero_new_blocks)
3254 DMEMIT("skip_block_zeroing ");
3255
3256 if (!pf->discard_enabled)
3257 DMEMIT("ignore_discard ");
3258
3259 if (!pf->discard_passdown)
3260 DMEMIT("no_discard_passdown ");
3261
3262 if (pf->mode == PM_READ_ONLY)
3263 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003264
3265 if (pf->error_if_no_space)
3266 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003267}
3268
Joe Thornber991d9fa2011-10-31 20:21:18 +00003269/*
3270 * Status line is:
3271 * <transaction id> <used metadata sectors>/<total metadata sectors>
3272 * <used data sectors>/<total data sectors> <held metadata root>
3273 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003274static void pool_status(struct dm_target *ti, status_type_t type,
3275 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003276{
Joe Thornbere49e5822012-07-27 15:08:16 +01003277 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003278 unsigned sz = 0;
3279 uint64_t transaction_id;
3280 dm_block_t nr_free_blocks_data;
3281 dm_block_t nr_free_blocks_metadata;
3282 dm_block_t nr_blocks_data;
3283 dm_block_t nr_blocks_metadata;
3284 dm_block_t held_root;
3285 char buf[BDEVNAME_SIZE];
3286 char buf2[BDEVNAME_SIZE];
3287 struct pool_c *pt = ti->private;
3288 struct pool *pool = pt->pool;
3289
3290 switch (type) {
3291 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003292 if (get_pool_mode(pool) == PM_FAIL) {
3293 DMEMIT("Fail");
3294 break;
3295 }
3296
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003297 /* Commit to ensure statistics aren't out-of-date */
3298 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003299 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003300
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003301 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3302 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003303 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3304 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003305 goto err;
3306 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003307
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003308 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3309 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003310 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3311 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003312 goto err;
3313 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003314
3315 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003316 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003317 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3318 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003319 goto err;
3320 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003321
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003322 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3323 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003324 DMERR("%s: dm_pool_get_free_block_count returned %d",
3325 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003326 goto err;
3327 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003328
3329 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003330 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003331 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3332 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003333 goto err;
3334 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003335
Joe Thornbercc8394d2012-06-03 00:30:01 +01003336 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003337 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003338 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3339 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003340 goto err;
3341 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003342
3343 DMEMIT("%llu %llu/%llu %llu/%llu ",
3344 (unsigned long long)transaction_id,
3345 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3346 (unsigned long long)nr_blocks_metadata,
3347 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3348 (unsigned long long)nr_blocks_data);
3349
3350 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003351 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003352 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003353 DMEMIT("- ");
3354
Joe Thornber3e1a0692014-03-03 16:03:26 +00003355 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3356 DMEMIT("out_of_data_space ");
3357 else if (pool->pf.mode == PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01003358 DMEMIT("ro ");
3359 else
3360 DMEMIT("rw ");
3361
Mike Snitzer018debe2012-12-21 20:23:32 +00003362 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003363 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003364 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003365 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003366 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003367 DMEMIT("no_discard_passdown ");
3368
3369 if (pool->pf.error_if_no_space)
3370 DMEMIT("error_if_no_space ");
3371 else
3372 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003373
3374 break;
3375
3376 case STATUSTYPE_TABLE:
3377 DMEMIT("%s %s %lu %llu ",
3378 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3379 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3380 (unsigned long)pool->sectors_per_block,
3381 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01003382 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003383 break;
3384 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003385 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003386
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003387err:
3388 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003389}
3390
3391static int pool_iterate_devices(struct dm_target *ti,
3392 iterate_devices_callout_fn fn, void *data)
3393{
3394 struct pool_c *pt = ti->private;
3395
3396 return fn(ti, pt->data_dev, 0, ti->len, data);
3397}
3398
3399static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3400 struct bio_vec *biovec, int max_size)
3401{
3402 struct pool_c *pt = ti->private;
3403 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3404
3405 if (!q->merge_bvec_fn)
3406 return max_size;
3407
3408 bvm->bi_bdev = pt->data_dev->bdev;
3409
3410 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3411}
3412
Mike Snitzer0424caa2012-09-26 23:45:47 +01003413static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
Joe Thornber104655f2012-03-28 18:41:28 +01003414{
Mike Snitzer0424caa2012-09-26 23:45:47 +01003415 struct pool *pool = pt->pool;
3416 struct queue_limits *data_limits;
3417
Joe Thornber104655f2012-03-28 18:41:28 +01003418 limits->max_discard_sectors = pool->sectors_per_block;
3419
3420 /*
Mike Snitzer0424caa2012-09-26 23:45:47 +01003421 * discard_granularity is just a hint, and not enforced.
Joe Thornber104655f2012-03-28 18:41:28 +01003422 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01003423 if (pt->adjusted_pf.discard_passdown) {
3424 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
Lukas Czerner09869de2014-06-11 12:28:43 -04003425 limits->discard_granularity = max(data_limits->discard_granularity,
3426 pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf13945d2013-03-01 22:45:44 +00003427 } else
Mike Snitzer0424caa2012-09-26 23:45:47 +01003428 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
Joe Thornber104655f2012-03-28 18:41:28 +01003429}
3430
Joe Thornber991d9fa2011-10-31 20:21:18 +00003431static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3432{
3433 struct pool_c *pt = ti->private;
3434 struct pool *pool = pt->pool;
Mike Snitzer604ea902014-10-09 18:43:25 -04003435 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3436
3437 /*
3438 * Adjust max_sectors_kb to highest possible power-of-2
3439 * factor of pool->sectors_per_block.
3440 */
3441 if (limits->max_hw_sectors & (limits->max_hw_sectors - 1))
3442 limits->max_sectors = rounddown_pow_of_two(limits->max_hw_sectors);
3443 else
3444 limits->max_sectors = limits->max_hw_sectors;
3445
3446 if (limits->max_sectors < pool->sectors_per_block) {
3447 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3448 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3449 limits->max_sectors--;
3450 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3451 }
3452 } else if (block_size_is_power_of_two(pool)) {
3453 /* max_sectors_kb is >= power-of-2 thinp blocksize */
3454 while (!is_factor(limits->max_sectors, pool->sectors_per_block)) {
3455 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3456 limits->max_sectors--;
3457 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3458 }
3459 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003460
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003461 /*
3462 * If the system-determined stacked limits are compatible with the
3463 * pool's blocksize (io_opt is a factor) do not override them.
3464 */
3465 if (io_opt_sectors < pool->sectors_per_block ||
Mike Snitzer604ea902014-10-09 18:43:25 -04003466 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3467 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3468 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3469 else
3470 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003471 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3472 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003473
3474 /*
3475 * pt->adjusted_pf is a staging area for the actual features to use.
3476 * They get transferred to the live pool in bind_control_target()
3477 * called from pool_preresume().
3478 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003479 if (!pt->adjusted_pf.discard_enabled) {
3480 /*
3481 * Must explicitly disallow stacking discard limits otherwise the
3482 * block layer will stack them if pool's data device has support.
3483 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3484 * user to see that, so make sure to set all discard limits to 0.
3485 */
3486 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003487 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04003488 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003489
3490 disable_passdown_if_not_supported(pt);
3491
3492 set_discard_limits(pt, limits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003493}
3494
3495static struct target_type pool_target = {
3496 .name = "thin-pool",
3497 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3498 DM_TARGET_IMMUTABLE,
Mike Snitzer36f12ae2014-10-09 15:24:12 -04003499 .version = {1, 14, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003500 .module = THIS_MODULE,
3501 .ctr = pool_ctr,
3502 .dtr = pool_dtr,
3503 .map = pool_map,
3504 .postsuspend = pool_postsuspend,
3505 .preresume = pool_preresume,
3506 .resume = pool_resume,
3507 .message = pool_message,
3508 .status = pool_status,
3509 .merge = pool_merge,
3510 .iterate_devices = pool_iterate_devices,
3511 .io_hints = pool_io_hints,
3512};
3513
3514/*----------------------------------------------------------------
3515 * Thin target methods
3516 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01003517static void thin_get(struct thin_c *tc)
3518{
3519 atomic_inc(&tc->refcount);
3520}
3521
3522static void thin_put(struct thin_c *tc)
3523{
3524 if (atomic_dec_and_test(&tc->refcount))
3525 complete(&tc->can_destroy);
3526}
3527
Joe Thornber991d9fa2011-10-31 20:21:18 +00003528static void thin_dtr(struct dm_target *ti)
3529{
3530 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003531 unsigned long flags;
3532
Joe Thornberb10ebd32014-04-08 11:29:01 +01003533 thin_put(tc);
3534 wait_for_completion(&tc->can_destroy);
3535
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003536 spin_lock_irqsave(&tc->pool->lock, flags);
3537 list_del_rcu(&tc->list);
3538 spin_unlock_irqrestore(&tc->pool->lock, flags);
3539 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00003540
3541 mutex_lock(&dm_thin_pool_table.mutex);
3542
3543 __pool_dec(tc->pool);
3544 dm_pool_close_thin_device(tc->td);
3545 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003546 if (tc->origin_dev)
3547 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003548 kfree(tc);
3549
3550 mutex_unlock(&dm_thin_pool_table.mutex);
3551}
3552
3553/*
3554 * Thin target parameters:
3555 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01003556 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00003557 *
3558 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3559 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01003560 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003561 *
3562 * If the pool device has discards disabled, they get disabled for the thin
3563 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003564 */
3565static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3566{
3567 int r;
3568 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01003569 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003570 struct mapped_device *pool_md;
Joe Thornber5e3283e2014-04-08 11:08:41 +01003571 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003572
3573 mutex_lock(&dm_thin_pool_table.mutex);
3574
Joe Thornber2dd9c252012-03-28 18:41:28 +01003575 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003576 ti->error = "Invalid argument count";
3577 r = -EINVAL;
3578 goto out_unlock;
3579 }
3580
3581 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
3582 if (!tc) {
3583 ti->error = "Out of memory";
3584 r = -ENOMEM;
3585 goto out_unlock;
3586 }
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003587 spin_lock_init(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01003588 INIT_LIST_HEAD(&tc->deferred_cells);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003589 bio_list_init(&tc->deferred_bio_list);
3590 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04003591 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003592
Joe Thornber2dd9c252012-03-28 18:41:28 +01003593 if (argc == 3) {
3594 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
3595 if (r) {
3596 ti->error = "Error opening origin device";
3597 goto bad_origin_dev;
3598 }
3599 tc->origin_dev = origin_dev;
3600 }
3601
Joe Thornber991d9fa2011-10-31 20:21:18 +00003602 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
3603 if (r) {
3604 ti->error = "Error opening pool device";
3605 goto bad_pool_dev;
3606 }
3607 tc->pool_dev = pool_dev;
3608
3609 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
3610 ti->error = "Invalid device id";
3611 r = -EINVAL;
3612 goto bad_common;
3613 }
3614
3615 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
3616 if (!pool_md) {
3617 ti->error = "Couldn't get pool mapped device";
3618 r = -EINVAL;
3619 goto bad_common;
3620 }
3621
3622 tc->pool = __pool_table_lookup(pool_md);
3623 if (!tc->pool) {
3624 ti->error = "Couldn't find pool object";
3625 r = -EINVAL;
3626 goto bad_pool_lookup;
3627 }
3628 __pool_inc(tc->pool);
3629
Joe Thornbere49e5822012-07-27 15:08:16 +01003630 if (get_pool_mode(tc->pool) == PM_FAIL) {
3631 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05003632 r = -EINVAL;
Joe Thornbere49e5822012-07-27 15:08:16 +01003633 goto bad_thin_open;
3634 }
3635
Joe Thornber991d9fa2011-10-31 20:21:18 +00003636 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
3637 if (r) {
3638 ti->error = "Couldn't open thin internal device";
3639 goto bad_thin_open;
3640 }
3641
Mike Snitzer542f9032012-07-27 15:08:00 +01003642 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
3643 if (r)
Mike Snitzer1acacc02014-02-19 20:32:33 -05003644 goto bad_target_max_io_len;
Mike Snitzer542f9032012-07-27 15:08:00 +01003645
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003646 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01003647 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003648 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003649
3650 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003651 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003652 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003653 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003654 ti->num_discard_bios = 1;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003655 /* Discard bios must be split on a block boundary */
3656 ti->split_discard_bios = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003657 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003658
3659 dm_put(pool_md);
3660
3661 mutex_unlock(&dm_thin_pool_table.mutex);
3662
Joe Thornberb10ebd32014-04-08 11:29:01 +01003663 atomic_set(&tc->refcount, 1);
3664 init_completion(&tc->can_destroy);
3665
Joe Thornber5e3283e2014-04-08 11:08:41 +01003666 spin_lock_irqsave(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003667 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Joe Thornber5e3283e2014-04-08 11:08:41 +01003668 spin_unlock_irqrestore(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003669 /*
3670 * This synchronize_rcu() call is needed here otherwise we risk a
3671 * wake_worker() call finding no bios to process (because the newly
3672 * added tc isn't yet visible). So this reduces latency since we
3673 * aren't then dependent on the periodic commit to wake_worker().
3674 */
3675 synchronize_rcu();
3676
Joe Thornber991d9fa2011-10-31 20:21:18 +00003677 return 0;
3678
Mike Snitzer1acacc02014-02-19 20:32:33 -05003679bad_target_max_io_len:
3680 dm_pool_close_thin_device(tc->td);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003681bad_thin_open:
3682 __pool_dec(tc->pool);
3683bad_pool_lookup:
3684 dm_put(pool_md);
3685bad_common:
3686 dm_put_device(ti, tc->pool_dev);
3687bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01003688 if (tc->origin_dev)
3689 dm_put_device(ti, tc->origin_dev);
3690bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003691 kfree(tc);
3692out_unlock:
3693 mutex_unlock(&dm_thin_pool_table.mutex);
3694
3695 return r;
3696}
3697
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003698static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003699{
Kent Overstreet4f024f32013-10-11 15:44:27 -07003700 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003701
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003702 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003703}
3704
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003705static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01003706{
3707 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003708 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01003709 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01003710 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01003711 struct pool *pool = h->tc->pool;
3712
3713 if (h->shared_read_entry) {
3714 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003715 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003716
3717 spin_lock_irqsave(&pool->lock, flags);
3718 list_for_each_entry_safe(m, tmp, &work, list) {
3719 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01003720 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003721 }
3722 spin_unlock_irqrestore(&pool->lock, flags);
3723 }
3724
Joe Thornber104655f2012-03-28 18:41:28 +01003725 if (h->all_io_entry) {
3726 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003727 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00003728 if (!list_empty(&work)) {
3729 spin_lock_irqsave(&pool->lock, flags);
3730 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05003731 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00003732 spin_unlock_irqrestore(&pool->lock, flags);
3733 wake_worker(pool);
3734 }
Joe Thornber104655f2012-03-28 18:41:28 +01003735 }
3736
Joe Thornbereb2aa482012-03-28 18:41:28 +01003737 return 0;
3738}
3739
Joe Thornber738211f2014-03-03 15:52:28 +00003740static void thin_presuspend(struct dm_target *ti)
3741{
3742 struct thin_c *tc = ti->private;
3743
3744 if (dm_noflush_suspending(ti))
3745 noflush_work(tc, do_noflush_start);
3746}
3747
Joe Thornber991d9fa2011-10-31 20:21:18 +00003748static void thin_postsuspend(struct dm_target *ti)
3749{
Joe Thornber738211f2014-03-03 15:52:28 +00003750 struct thin_c *tc = ti->private;
3751
3752 /*
3753 * The dm_noflush_suspending flag has been cleared by now, so
3754 * unfortunately we must always run this.
3755 */
3756 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003757}
3758
Joe Thornbere5aea7b2014-06-13 14:47:24 +01003759static int thin_preresume(struct dm_target *ti)
3760{
3761 struct thin_c *tc = ti->private;
3762
3763 if (tc->origin_dev)
3764 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
3765
3766 return 0;
3767}
3768
Joe Thornber991d9fa2011-10-31 20:21:18 +00003769/*
3770 * <nr mapped sectors> <highest mapped sector>
3771 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003772static void thin_status(struct dm_target *ti, status_type_t type,
3773 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003774{
3775 int r;
3776 ssize_t sz = 0;
3777 dm_block_t mapped, highest;
3778 char buf[BDEVNAME_SIZE];
3779 struct thin_c *tc = ti->private;
3780
Joe Thornbere49e5822012-07-27 15:08:16 +01003781 if (get_pool_mode(tc->pool) == PM_FAIL) {
3782 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003783 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01003784 }
3785
Joe Thornber991d9fa2011-10-31 20:21:18 +00003786 if (!tc->td)
3787 DMEMIT("-");
3788 else {
3789 switch (type) {
3790 case STATUSTYPE_INFO:
3791 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003792 if (r) {
3793 DMERR("dm_thin_get_mapped_count returned %d", r);
3794 goto err;
3795 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003796
3797 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003798 if (r < 0) {
3799 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
3800 goto err;
3801 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003802
3803 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
3804 if (r)
3805 DMEMIT("%llu", ((highest + 1) *
3806 tc->pool->sectors_per_block) - 1);
3807 else
3808 DMEMIT("-");
3809 break;
3810
3811 case STATUSTYPE_TABLE:
3812 DMEMIT("%s %lu",
3813 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
3814 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003815 if (tc->origin_dev)
3816 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00003817 break;
3818 }
3819 }
3820
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003821 return;
3822
3823err:
3824 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003825}
3826
Mike Snitzer36f12ae2014-10-09 15:24:12 -04003827static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3828 struct bio_vec *biovec, int max_size)
3829{
3830 struct thin_c *tc = ti->private;
3831 struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
3832
3833 if (!q->merge_bvec_fn)
3834 return max_size;
3835
3836 bvm->bi_bdev = tc->pool_dev->bdev;
3837 bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
3838
3839 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3840}
3841
Joe Thornber991d9fa2011-10-31 20:21:18 +00003842static int thin_iterate_devices(struct dm_target *ti,
3843 iterate_devices_callout_fn fn, void *data)
3844{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003845 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003846 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003847 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003848
3849 /*
3850 * We can't call dm_pool_get_data_dev_size() since that blocks. So
3851 * we follow a more convoluted path through to the pool's target.
3852 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003853 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003854 return 0; /* nothing is bound */
3855
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003856 blocks = pool->ti->len;
3857 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003858 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003859 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003860
3861 return 0;
3862}
3863
Joe Thornber991d9fa2011-10-31 20:21:18 +00003864static struct target_type thin_target = {
3865 .name = "thin",
Mike Snitzer36f12ae2014-10-09 15:24:12 -04003866 .version = {1, 14, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003867 .module = THIS_MODULE,
3868 .ctr = thin_ctr,
3869 .dtr = thin_dtr,
3870 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01003871 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01003872 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00003873 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003874 .postsuspend = thin_postsuspend,
3875 .status = thin_status,
Mike Snitzer36f12ae2014-10-09 15:24:12 -04003876 .merge = thin_merge,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003877 .iterate_devices = thin_iterate_devices,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003878};
3879
3880/*----------------------------------------------------------------*/
3881
3882static int __init dm_thin_init(void)
3883{
3884 int r;
3885
3886 pool_table_init();
3887
3888 r = dm_register_target(&thin_target);
3889 if (r)
3890 return r;
3891
3892 r = dm_register_target(&pool_target);
3893 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01003894 goto bad_pool_target;
3895
3896 r = -ENOMEM;
3897
Mike Snitzera24c2562012-06-03 00:30:00 +01003898 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
3899 if (!_new_mapping_cache)
3900 goto bad_new_mapping_cache;
3901
Mike Snitzera24c2562012-06-03 00:30:00 +01003902 return 0;
3903
Mike Snitzera24c2562012-06-03 00:30:00 +01003904bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01003905 dm_unregister_target(&pool_target);
3906bad_pool_target:
3907 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003908
3909 return r;
3910}
3911
3912static void dm_thin_exit(void)
3913{
3914 dm_unregister_target(&thin_target);
3915 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01003916
Mike Snitzera24c2562012-06-03 00:30:00 +01003917 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003918}
3919
3920module_init(dm_thin_init);
3921module_exit(dm_thin_exit);
3922
Mike Snitzer80c57892014-05-20 13:38:33 -04003923module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
3924MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
3925
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01003926MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003927MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3928MODULE_LICENSE("GPL");